[Freeswitch-svn] [commit] r9425 - in freeswitch/trunk/libs/spandsp: . config debian doc doc/t38_manual spandsp spandsp-sim spandsp-sim/spandsp src src/msvc src/msvc/sys src/spandsp test-data test-data/etsi test-data/etsi/fax test-data/itu test-data/itu/fax test-data/local tests

Freeswitch SVN mikej at freeswitch.org
Wed Sep 3 15:02:00 EDT 2008


Author: mikej
Date: Wed Sep  3 15:02:00 2008
New Revision: 9425

Added:
   freeswitch/trunk/libs/spandsp/
   freeswitch/trunk/libs/spandsp/AUTHORS
   freeswitch/trunk/libs/spandsp/COPYING
   freeswitch/trunk/libs/spandsp/ChangeLog
   freeswitch/trunk/libs/spandsp/DueDiligence
   freeswitch/trunk/libs/spandsp/INSTALL
   freeswitch/trunk/libs/spandsp/Makefile.am
   freeswitch/trunk/libs/spandsp/NEWS
   freeswitch/trunk/libs/spandsp/README
   freeswitch/trunk/libs/spandsp/README.testdata
   freeswitch/trunk/libs/spandsp/aclocal.m4
   freeswitch/trunk/libs/spandsp/autogen.sh   (contents, props changed)
   freeswitch/trunk/libs/spandsp/config/
   freeswitch/trunk/libs/spandsp/config-h.in
   freeswitch/trunk/libs/spandsp/config/ax_c99_features.m4
   freeswitch/trunk/libs/spandsp/config/ax_check_real_file.m4
   freeswitch/trunk/libs/spandsp/config/ax_compiler_vendor.m4
   freeswitch/trunk/libs/spandsp/config/ax_fixed_point_machine.m4
   freeswitch/trunk/libs/spandsp/config/ax_misaligned_access_fails.m4
   freeswitch/trunk/libs/spandsp/configure.ac
   freeswitch/trunk/libs/spandsp/debian/
   freeswitch/trunk/libs/spandsp/debian/changelog
   freeswitch/trunk/libs/spandsp/debian/compat
   freeswitch/trunk/libs/spandsp/debian/control
   freeswitch/trunk/libs/spandsp/debian/copyright
   freeswitch/trunk/libs/spandsp/debian/libspandsp-dev.install
   freeswitch/trunk/libs/spandsp/debian/libspandsp-doc.install
   freeswitch/trunk/libs/spandsp/debian/libspandsp5.install
   freeswitch/trunk/libs/spandsp/debian/rules   (contents, props changed)
   freeswitch/trunk/libs/spandsp/debian/watch
   freeswitch/trunk/libs/spandsp/doc/
   freeswitch/trunk/libs/spandsp/doc/Makefile.am
   freeswitch/trunk/libs/spandsp/doc/css.css
   freeswitch/trunk/libs/spandsp/doc/doxygen.in
   freeswitch/trunk/libs/spandsp/doc/t38-gateway.dia   (contents, props changed)
   freeswitch/trunk/libs/spandsp/doc/t38-terminal.dia   (contents, props changed)
   freeswitch/trunk/libs/spandsp/doc/t38_manual/
   freeswitch/trunk/libs/spandsp/doc/t38_manual.xml
   freeswitch/trunk/libs/spandsp/doc/t38_manual/css.css
   freeswitch/trunk/libs/spandsp/doc/wrapper.xsl
   freeswitch/trunk/libs/spandsp/libspandsp.vcproj
   freeswitch/trunk/libs/spandsp/spandsp/
   freeswitch/trunk/libs/spandsp/spandsp-sim/
   freeswitch/trunk/libs/spandsp/spandsp-sim/Makefile.am
   freeswitch/trunk/libs/spandsp/spandsp-sim/g1050.c
   freeswitch/trunk/libs/spandsp/spandsp-sim/line_model.c
   freeswitch/trunk/libs/spandsp/spandsp-sim/make_line_models.c
   freeswitch/trunk/libs/spandsp/spandsp-sim/rfc2198_sim.c
   freeswitch/trunk/libs/spandsp/spandsp-sim/spandsp/
   freeswitch/trunk/libs/spandsp/spandsp-sim/spandsp-sim.h
   freeswitch/trunk/libs/spandsp/spandsp-sim/spandsp/g1050.h
   freeswitch/trunk/libs/spandsp/spandsp-sim/spandsp/line_model.h
   freeswitch/trunk/libs/spandsp/spandsp-sim/spandsp/line_models.h
   freeswitch/trunk/libs/spandsp/spandsp-sim/spandsp/rfc2198_sim.h
   freeswitch/trunk/libs/spandsp/spandsp-sim/spandsp/test_utils.h
   freeswitch/trunk/libs/spandsp/spandsp-sim/test_utils.c
   freeswitch/trunk/libs/spandsp/spandsp.spec
   freeswitch/trunk/libs/spandsp/spandsp.spec.in
   freeswitch/trunk/libs/spandsp/spandsp/global-tones.xml
   freeswitch/trunk/libs/spandsp/spandsp/tones.dtd
   freeswitch/trunk/libs/spandsp/spandsp/tsb85.xml
   freeswitch/trunk/libs/spandsp/src/
   freeswitch/trunk/libs/spandsp/src/Makefile.am
   freeswitch/trunk/libs/spandsp/src/adsi.c
   freeswitch/trunk/libs/spandsp/src/async.c
   freeswitch/trunk/libs/spandsp/src/at_interpreter.c
   freeswitch/trunk/libs/spandsp/src/awgn.c
   freeswitch/trunk/libs/spandsp/src/bell_r2_mf.c
   freeswitch/trunk/libs/spandsp/src/bert.c
   freeswitch/trunk/libs/spandsp/src/bit_operations.c
   freeswitch/trunk/libs/spandsp/src/bitstream.c
   freeswitch/trunk/libs/spandsp/src/complex_filters.c
   freeswitch/trunk/libs/spandsp/src/complex_vector_float.c
   freeswitch/trunk/libs/spandsp/src/crc.c
   freeswitch/trunk/libs/spandsp/src/dds_float.c
   freeswitch/trunk/libs/spandsp/src/dds_int.c
   freeswitch/trunk/libs/spandsp/src/dtmf.c
   freeswitch/trunk/libs/spandsp/src/echo.c
   freeswitch/trunk/libs/spandsp/src/fax.c
   freeswitch/trunk/libs/spandsp/src/faxfont.h
   freeswitch/trunk/libs/spandsp/src/filter_tools.c
   freeswitch/trunk/libs/spandsp/src/filter_tools.h
   freeswitch/trunk/libs/spandsp/src/floating_fudge.h
   freeswitch/trunk/libs/spandsp/src/fsk.c
   freeswitch/trunk/libs/spandsp/src/g711.c
   freeswitch/trunk/libs/spandsp/src/g722_decode.c
   freeswitch/trunk/libs/spandsp/src/g722_encode.c
   freeswitch/trunk/libs/spandsp/src/g726.c
   freeswitch/trunk/libs/spandsp/src/gsm0610_decode.c
   freeswitch/trunk/libs/spandsp/src/gsm0610_encode.c
   freeswitch/trunk/libs/spandsp/src/gsm0610_local.h
   freeswitch/trunk/libs/spandsp/src/gsm0610_long_term.c
   freeswitch/trunk/libs/spandsp/src/gsm0610_lpc.c
   freeswitch/trunk/libs/spandsp/src/gsm0610_preprocess.c
   freeswitch/trunk/libs/spandsp/src/gsm0610_rpe.c
   freeswitch/trunk/libs/spandsp/src/gsm0610_short_term.c
   freeswitch/trunk/libs/spandsp/src/hdlc.c
   freeswitch/trunk/libs/spandsp/src/ima_adpcm.c
   freeswitch/trunk/libs/spandsp/src/libspandsp.dsp
   freeswitch/trunk/libs/spandsp/src/libspandsp.sln
   freeswitch/trunk/libs/spandsp/src/logging.c
   freeswitch/trunk/libs/spandsp/src/lpc10_analyse.c
   freeswitch/trunk/libs/spandsp/src/lpc10_decode.c
   freeswitch/trunk/libs/spandsp/src/lpc10_encdecs.h
   freeswitch/trunk/libs/spandsp/src/lpc10_encode.c
   freeswitch/trunk/libs/spandsp/src/lpc10_placev.c
   freeswitch/trunk/libs/spandsp/src/lpc10_voicing.c
   freeswitch/trunk/libs/spandsp/src/make_at_dictionary.c
   freeswitch/trunk/libs/spandsp/src/make_modem_filter.c
   freeswitch/trunk/libs/spandsp/src/modem_connect_tones.c
   freeswitch/trunk/libs/spandsp/src/modem_echo.c
   freeswitch/trunk/libs/spandsp/src/msvc/
   freeswitch/trunk/libs/spandsp/src/msvc/gettimeofday.c
   freeswitch/trunk/libs/spandsp/src/msvc/inttypes.h
   freeswitch/trunk/libs/spandsp/src/msvc/msvcproj.foot
   freeswitch/trunk/libs/spandsp/src/msvc/msvcproj.head
   freeswitch/trunk/libs/spandsp/src/msvc/spandsp.def
   freeswitch/trunk/libs/spandsp/src/msvc/sys/
   freeswitch/trunk/libs/spandsp/src/msvc/sys/time.h
   freeswitch/trunk/libs/spandsp/src/msvc/tgmath.h
   freeswitch/trunk/libs/spandsp/src/msvc/unistd.h
   freeswitch/trunk/libs/spandsp/src/msvc/vc8proj.foot
   freeswitch/trunk/libs/spandsp/src/msvc/vc8proj.head
   freeswitch/trunk/libs/spandsp/src/noise.c
   freeswitch/trunk/libs/spandsp/src/oki_adpcm.c
   freeswitch/trunk/libs/spandsp/src/playout.c
   freeswitch/trunk/libs/spandsp/src/plc.c
   freeswitch/trunk/libs/spandsp/src/power_meter.c
   freeswitch/trunk/libs/spandsp/src/queue.c
   freeswitch/trunk/libs/spandsp/src/schedule.c
   freeswitch/trunk/libs/spandsp/src/sig_tone.c
   freeswitch/trunk/libs/spandsp/src/silence_gen.c
   freeswitch/trunk/libs/spandsp/src/spandsp/
   freeswitch/trunk/libs/spandsp/src/spandsp.h.in
   freeswitch/trunk/libs/spandsp/src/spandsp/adsi.h
   freeswitch/trunk/libs/spandsp/src/spandsp/arctan2.h
   freeswitch/trunk/libs/spandsp/src/spandsp/async.h
   freeswitch/trunk/libs/spandsp/src/spandsp/at_interpreter.h
   freeswitch/trunk/libs/spandsp/src/spandsp/awgn.h
   freeswitch/trunk/libs/spandsp/src/spandsp/bell_r2_mf.h
   freeswitch/trunk/libs/spandsp/src/spandsp/bert.h
   freeswitch/trunk/libs/spandsp/src/spandsp/biquad.h
   freeswitch/trunk/libs/spandsp/src/spandsp/bit_operations.h
   freeswitch/trunk/libs/spandsp/src/spandsp/bitstream.h
   freeswitch/trunk/libs/spandsp/src/spandsp/complex.h
   freeswitch/trunk/libs/spandsp/src/spandsp/complex_filters.h
   freeswitch/trunk/libs/spandsp/src/spandsp/complex_vector_float.h
   freeswitch/trunk/libs/spandsp/src/spandsp/crc.h
   freeswitch/trunk/libs/spandsp/src/spandsp/dc_restore.h
   freeswitch/trunk/libs/spandsp/src/spandsp/dds.h
   freeswitch/trunk/libs/spandsp/src/spandsp/dtmf.h
   freeswitch/trunk/libs/spandsp/src/spandsp/echo.h
   freeswitch/trunk/libs/spandsp/src/spandsp/fax.h
   freeswitch/trunk/libs/spandsp/src/spandsp/fax_modems.h
   freeswitch/trunk/libs/spandsp/src/spandsp/fir.h
   freeswitch/trunk/libs/spandsp/src/spandsp/fsk.h
   freeswitch/trunk/libs/spandsp/src/spandsp/g168models.h
   freeswitch/trunk/libs/spandsp/src/spandsp/g711.h
   freeswitch/trunk/libs/spandsp/src/spandsp/g722.h
   freeswitch/trunk/libs/spandsp/src/spandsp/g726.h
   freeswitch/trunk/libs/spandsp/src/spandsp/gsm0610.h
   freeswitch/trunk/libs/spandsp/src/spandsp/hdlc.h
   freeswitch/trunk/libs/spandsp/src/spandsp/ima_adpcm.h
   freeswitch/trunk/libs/spandsp/src/spandsp/logging.h
   freeswitch/trunk/libs/spandsp/src/spandsp/lpc10.h
   freeswitch/trunk/libs/spandsp/src/spandsp/modem_connect_tones.h
   freeswitch/trunk/libs/spandsp/src/spandsp/modem_echo.h
   freeswitch/trunk/libs/spandsp/src/spandsp/noise.h
   freeswitch/trunk/libs/spandsp/src/spandsp/oki_adpcm.h
   freeswitch/trunk/libs/spandsp/src/spandsp/playout.h
   freeswitch/trunk/libs/spandsp/src/spandsp/plc.h
   freeswitch/trunk/libs/spandsp/src/spandsp/power_meter.h
   freeswitch/trunk/libs/spandsp/src/spandsp/queue.h
   freeswitch/trunk/libs/spandsp/src/spandsp/schedule.h
   freeswitch/trunk/libs/spandsp/src/spandsp/sig_tone.h
   freeswitch/trunk/libs/spandsp/src/spandsp/silence_gen.h
   freeswitch/trunk/libs/spandsp/src/spandsp/super_tone_rx.h
   freeswitch/trunk/libs/spandsp/src/spandsp/super_tone_tx.h
   freeswitch/trunk/libs/spandsp/src/spandsp/t30.h
   freeswitch/trunk/libs/spandsp/src/spandsp/t30_api.h
   freeswitch/trunk/libs/spandsp/src/spandsp/t30_fcf.h
   freeswitch/trunk/libs/spandsp/src/spandsp/t30_logging.h
   freeswitch/trunk/libs/spandsp/src/spandsp/t31.h
   freeswitch/trunk/libs/spandsp/src/spandsp/t35.h
   freeswitch/trunk/libs/spandsp/src/spandsp/t38_core.h
   freeswitch/trunk/libs/spandsp/src/spandsp/t38_gateway.h
   freeswitch/trunk/libs/spandsp/src/spandsp/t38_non_ecm_buffer.h
   freeswitch/trunk/libs/spandsp/src/spandsp/t38_terminal.h
   freeswitch/trunk/libs/spandsp/src/spandsp/t4.h
   freeswitch/trunk/libs/spandsp/src/spandsp/telephony.h
   freeswitch/trunk/libs/spandsp/src/spandsp/time_scale.h
   freeswitch/trunk/libs/spandsp/src/spandsp/timing.h
   freeswitch/trunk/libs/spandsp/src/spandsp/tone_detect.h
   freeswitch/trunk/libs/spandsp/src/spandsp/tone_generate.h
   freeswitch/trunk/libs/spandsp/src/spandsp/v17rx.h
   freeswitch/trunk/libs/spandsp/src/spandsp/v17tx.h
   freeswitch/trunk/libs/spandsp/src/spandsp/v22bis.h
   freeswitch/trunk/libs/spandsp/src/spandsp/v27ter_rx.h
   freeswitch/trunk/libs/spandsp/src/spandsp/v27ter_tx.h
   freeswitch/trunk/libs/spandsp/src/spandsp/v29rx.h
   freeswitch/trunk/libs/spandsp/src/spandsp/v29tx.h
   freeswitch/trunk/libs/spandsp/src/spandsp/v42.h
   freeswitch/trunk/libs/spandsp/src/spandsp/v42bis.h
   freeswitch/trunk/libs/spandsp/src/spandsp/v8.h
   freeswitch/trunk/libs/spandsp/src/spandsp/vector_float.h
   freeswitch/trunk/libs/spandsp/src/spandsp/vector_int.h
   freeswitch/trunk/libs/spandsp/src/spandsp/version.h
   freeswitch/trunk/libs/spandsp/src/spandsp/version.h.in
   freeswitch/trunk/libs/spandsp/src/super_tone_rx.c
   freeswitch/trunk/libs/spandsp/src/super_tone_tx.c
   freeswitch/trunk/libs/spandsp/src/t30.c
   freeswitch/trunk/libs/spandsp/src/t30_api.c
   freeswitch/trunk/libs/spandsp/src/t30_local.h
   freeswitch/trunk/libs/spandsp/src/t30_logging.c
   freeswitch/trunk/libs/spandsp/src/t31.c
   freeswitch/trunk/libs/spandsp/src/t35.c
   freeswitch/trunk/libs/spandsp/src/t38_core.c
   freeswitch/trunk/libs/spandsp/src/t38_gateway.c
   freeswitch/trunk/libs/spandsp/src/t38_non_ecm_buffer.c
   freeswitch/trunk/libs/spandsp/src/t38_terminal.c
   freeswitch/trunk/libs/spandsp/src/t4.c
   freeswitch/trunk/libs/spandsp/src/t4_states.h
   freeswitch/trunk/libs/spandsp/src/testcpuid.c
   freeswitch/trunk/libs/spandsp/src/time_scale.c
   freeswitch/trunk/libs/spandsp/src/tone_detect.c
   freeswitch/trunk/libs/spandsp/src/tone_generate.c
   freeswitch/trunk/libs/spandsp/src/v17rx.c
   freeswitch/trunk/libs/spandsp/src/v17rx_constellation_maps.h
   freeswitch/trunk/libs/spandsp/src/v17tx.c
   freeswitch/trunk/libs/spandsp/src/v17tx_constellation_maps.h
   freeswitch/trunk/libs/spandsp/src/v22bis_rx.c
   freeswitch/trunk/libs/spandsp/src/v22bis_tx.c
   freeswitch/trunk/libs/spandsp/src/v27ter_rx.c
   freeswitch/trunk/libs/spandsp/src/v27ter_tx.c
   freeswitch/trunk/libs/spandsp/src/v29rx.c
   freeswitch/trunk/libs/spandsp/src/v29tx.c
   freeswitch/trunk/libs/spandsp/src/v29tx_constellation_maps.h
   freeswitch/trunk/libs/spandsp/src/v42.c
   freeswitch/trunk/libs/spandsp/src/v42bis.c
   freeswitch/trunk/libs/spandsp/src/v8.c
   freeswitch/trunk/libs/spandsp/src/vector_float.c
   freeswitch/trunk/libs/spandsp/src/vector_int.c
   freeswitch/trunk/libs/spandsp/test-data/
   freeswitch/trunk/libs/spandsp/test-data/Makefile.am
   freeswitch/trunk/libs/spandsp/test-data/etsi/
   freeswitch/trunk/libs/spandsp/test-data/etsi/Makefile.am
   freeswitch/trunk/libs/spandsp/test-data/etsi/fax/
   freeswitch/trunk/libs/spandsp/test-data/etsi/fax/Makefile.am
   freeswitch/trunk/libs/spandsp/test-data/etsi/fax/generate_etsi_300_242_pages.c
   freeswitch/trunk/libs/spandsp/test-data/itu/
   freeswitch/trunk/libs/spandsp/test-data/itu/Makefile.am
   freeswitch/trunk/libs/spandsp/test-data/itu/fax/
   freeswitch/trunk/libs/spandsp/test-data/itu/fax/Makefile.am
   freeswitch/trunk/libs/spandsp/test-data/itu/fax/generate_dithered_tif.c
   freeswitch/trunk/libs/spandsp/test-data/itu/fax/generate_sized_pages.c
   freeswitch/trunk/libs/spandsp/test-data/itu/fax/itu1.pbm   (contents, props changed)
   freeswitch/trunk/libs/spandsp/test-data/itu/fax/itu2.pbm   (contents, props changed)
   freeswitch/trunk/libs/spandsp/test-data/itu/fax/itu3.pbm   (contents, props changed)
   freeswitch/trunk/libs/spandsp/test-data/itu/fax/itu4.pbm   (contents, props changed)
   freeswitch/trunk/libs/spandsp/test-data/itu/fax/itu5.pbm   (contents, props changed)
   freeswitch/trunk/libs/spandsp/test-data/itu/fax/itu6.pbm   (contents, props changed)
   freeswitch/trunk/libs/spandsp/test-data/itu/fax/itu7.pbm   (contents, props changed)
   freeswitch/trunk/libs/spandsp/test-data/itu/fax/itu8.pbm   (contents, props changed)
   freeswitch/trunk/libs/spandsp/test-data/itu/fax/test1.pbm   (contents, props changed)
   freeswitch/trunk/libs/spandsp/test-data/itu/fax/test2.pbm   (contents, props changed)
   freeswitch/trunk/libs/spandsp/test-data/itu/fax/test3.pbm   (contents, props changed)
   freeswitch/trunk/libs/spandsp/test-data/itu/fax/test4.pbm   (contents, props changed)
   freeswitch/trunk/libs/spandsp/test-data/local/
   freeswitch/trunk/libs/spandsp/test-data/local/Makefile.am
   freeswitch/trunk/libs/spandsp/test-data/local/dam9.wav   (contents, props changed)
   freeswitch/trunk/libs/spandsp/test-data/local/dam9_lpc55.wav   (contents, props changed)
   freeswitch/trunk/libs/spandsp/test-data/local/short_nb_voice.wav   (contents, props changed)
   freeswitch/trunk/libs/spandsp/test-data/local/short_wb_voice.wav   (contents, props changed)
   freeswitch/trunk/libs/spandsp/tests/
   freeswitch/trunk/libs/spandsp/tests/Makefile.am
   freeswitch/trunk/libs/spandsp/tests/adsi_tests.c
   freeswitch/trunk/libs/spandsp/tests/async_tests.c
   freeswitch/trunk/libs/spandsp/tests/at_interpreter_tests.c
   freeswitch/trunk/libs/spandsp/tests/awgn_tests.c
   freeswitch/trunk/libs/spandsp/tests/bell_mf_rx_tests.c
   freeswitch/trunk/libs/spandsp/tests/bell_mf_tx_tests.c
   freeswitch/trunk/libs/spandsp/tests/bert_tests.c
   freeswitch/trunk/libs/spandsp/tests/bit_operations_tests.c
   freeswitch/trunk/libs/spandsp/tests/crc_tests.c
   freeswitch/trunk/libs/spandsp/tests/dc_restore_tests.c
   freeswitch/trunk/libs/spandsp/tests/dds_tests.c
   freeswitch/trunk/libs/spandsp/tests/dtmf_rx_tests.c
   freeswitch/trunk/libs/spandsp/tests/dtmf_tx_tests.c
   freeswitch/trunk/libs/spandsp/tests/echo_monitor.cpp
   freeswitch/trunk/libs/spandsp/tests/echo_monitor.h
   freeswitch/trunk/libs/spandsp/tests/echo_tests.c
   freeswitch/trunk/libs/spandsp/tests/fax_decode.c
   freeswitch/trunk/libs/spandsp/tests/fax_tester.c
   freeswitch/trunk/libs/spandsp/tests/fax_tester.h
   freeswitch/trunk/libs/spandsp/tests/fax_tests.c
   freeswitch/trunk/libs/spandsp/tests/fax_tests.sh   (contents, props changed)
   freeswitch/trunk/libs/spandsp/tests/fsk_tests.c
   freeswitch/trunk/libs/spandsp/tests/g1050_tests.c
   freeswitch/trunk/libs/spandsp/tests/g168_tests.c
   freeswitch/trunk/libs/spandsp/tests/g711_tests.c
   freeswitch/trunk/libs/spandsp/tests/g722_tests.c
   freeswitch/trunk/libs/spandsp/tests/g726_tests.c
   freeswitch/trunk/libs/spandsp/tests/gsm0610_tests.c
   freeswitch/trunk/libs/spandsp/tests/hdlc_tests.c
   freeswitch/trunk/libs/spandsp/tests/ima_adpcm_tests.c
   freeswitch/trunk/libs/spandsp/tests/line_model_monitor.cpp
   freeswitch/trunk/libs/spandsp/tests/line_model_monitor.h
   freeswitch/trunk/libs/spandsp/tests/line_model_tests.c
   freeswitch/trunk/libs/spandsp/tests/logging_tests.c
   freeswitch/trunk/libs/spandsp/tests/lpc10_tests.c
   freeswitch/trunk/libs/spandsp/tests/make_g168_css.c
   freeswitch/trunk/libs/spandsp/tests/media_monitor.cpp
   freeswitch/trunk/libs/spandsp/tests/media_monitor.h
   freeswitch/trunk/libs/spandsp/tests/modem_connect_tones_tests.c
   freeswitch/trunk/libs/spandsp/tests/modem_echo_tests.c
   freeswitch/trunk/libs/spandsp/tests/modem_monitor.cpp
   freeswitch/trunk/libs/spandsp/tests/modem_monitor.h
   freeswitch/trunk/libs/spandsp/tests/noise_tests.c
   freeswitch/trunk/libs/spandsp/tests/oki_adpcm_tests.c
   freeswitch/trunk/libs/spandsp/tests/playout_tests.c
   freeswitch/trunk/libs/spandsp/tests/plc_tests.c
   freeswitch/trunk/libs/spandsp/tests/power_meter_tests.c
   freeswitch/trunk/libs/spandsp/tests/queue_tests.c
   freeswitch/trunk/libs/spandsp/tests/r2_mf_rx_tests.c
   freeswitch/trunk/libs/spandsp/tests/r2_mf_tx_tests.c
   freeswitch/trunk/libs/spandsp/tests/regression_tests.sh   (contents, props changed)
   freeswitch/trunk/libs/spandsp/tests/rfc2198_sim_tests.c
   freeswitch/trunk/libs/spandsp/tests/schedule_tests.c
   freeswitch/trunk/libs/spandsp/tests/sig_tone_tests.c
   freeswitch/trunk/libs/spandsp/tests/super_tone_rx_tests.c
   freeswitch/trunk/libs/spandsp/tests/super_tone_tx_tests.c
   freeswitch/trunk/libs/spandsp/tests/t31_tests.c
   freeswitch/trunk/libs/spandsp/tests/t38_core_tests.c
   freeswitch/trunk/libs/spandsp/tests/t38_gateway_tests.c
   freeswitch/trunk/libs/spandsp/tests/t38_gateway_to_terminal_tests.c
   freeswitch/trunk/libs/spandsp/tests/t38_non_ecm_buffer_tests.c
   freeswitch/trunk/libs/spandsp/tests/t38_terminal_tests.c
   freeswitch/trunk/libs/spandsp/tests/t38_terminal_to_gateway_tests.c
   freeswitch/trunk/libs/spandsp/tests/t4_tests.c
   freeswitch/trunk/libs/spandsp/tests/testadsi.c
   freeswitch/trunk/libs/spandsp/tests/testfax.c
   freeswitch/trunk/libs/spandsp/tests/time_scale_tests.c
   freeswitch/trunk/libs/spandsp/tests/tone_detect_tests.c
   freeswitch/trunk/libs/spandsp/tests/tone_generate_tests.c
   freeswitch/trunk/libs/spandsp/tests/tsb85_tests.c
   freeswitch/trunk/libs/spandsp/tests/tsb85_tests.sh   (contents, props changed)
   freeswitch/trunk/libs/spandsp/tests/v17_tests.c
   freeswitch/trunk/libs/spandsp/tests/v22bis_tests.c
   freeswitch/trunk/libs/spandsp/tests/v27ter_tests.c
   freeswitch/trunk/libs/spandsp/tests/v29_tests.c
   freeswitch/trunk/libs/spandsp/tests/v42_tests.c
   freeswitch/trunk/libs/spandsp/tests/v42bis_tests.c
   freeswitch/trunk/libs/spandsp/tests/v42bis_tests.sh   (contents, props changed)
   freeswitch/trunk/libs/spandsp/tests/v8_tests.c
   freeswitch/trunk/libs/spandsp/tests/vector_float_tests.c
   freeswitch/trunk/libs/spandsp/tests/vector_int_tests.c
   freeswitch/trunk/libs/spandsp/unpack_g722_data.sh   (contents, props changed)
   freeswitch/trunk/libs/spandsp/unpack_g726_data.sh   (contents, props changed)
   freeswitch/trunk/libs/spandsp/unpack_gsm0610_data.sh   (contents, props changed)
   freeswitch/trunk/libs/spandsp/unpack_v56ter_data.sh   (contents, props changed)
   freeswitch/trunk/libs/spandsp/wrapper.xsl

Log:
inital checkin of spandsp from http://www.soft-switch.org/downloads/snapshots/spandsp/spandsp-20080829.tar.gz

Added: freeswitch/trunk/libs/spandsp/AUTHORS
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/AUTHORS	Wed Sep  3 15:02:00 2008
@@ -0,0 +1 @@
+Steve Underwood <steveu at coppice.org>

Added: freeswitch/trunk/libs/spandsp/COPYING
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/COPYING	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,853 @@
+The spandsp library is licenced under LGPL 2.1. The test suite, and some of
+the supporting code, are licenced under GPL 2. The full text of both licences
+may be found below.
+
+
+
+		  GNU LESSER GENERAL PUBLIC LICENSE
+		       Version 2.1, February 1999
+
+ Copyright (C) 1991, 1999 Free Software Foundation, Inc.
+     51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ Everyone is permitted to copy and distribute verbatim copies
+ of this license document, but changing it is not allowed.
+
+[This is the first released version of the Lesser GPL.  It also counts
+ as the successor of the GNU Library Public License, version 2, hence
+ the version number 2.1.]
+
+			    Preamble
+
+  The licenses for most software are designed to take away your
+freedom to share and change it.  By contrast, the GNU General Public
+Licenses are intended to guarantee your freedom to share and change
+free software--to make sure the software is free for all its users.
+
+  This license, the Lesser General Public License, applies to some
+specially designated software packages--typically libraries--of the
+Free Software Foundation and other authors who decide to use it.  You
+can use it too, but we suggest you first think carefully about whether
+this license or the ordinary General Public License is the better
+strategy to use in any particular case, based on the explanations below.
+
+  When we speak of free software, we are referring to freedom of use,
+not price.  Our General Public Licenses are designed to make sure that
+you have the freedom to distribute copies of free software (and charge
+for this service if you wish); that you receive source code or can get
+it if you want it; that you can change the software and use pieces of
+it in new free programs; and that you are informed that you can do
+these things.
+
+  To protect your rights, we need to make restrictions that forbid
+distributors to deny you these rights or to ask you to surrender these
+rights.  These restrictions translate to certain responsibilities for
+you if you distribute copies of the library or if you modify it.
+
+  For example, if you distribute copies of the library, whether gratis
+or for a fee, you must give the recipients all the rights that we gave
+you.  You must make sure that they, too, receive or can get the source
+code.  If you link other code with the library, you must provide
+complete object files to the recipients, so that they can relink them
+with the library after making changes to the library and recompiling
+it.  And you must show them these terms so they know their rights.
+
+  We protect your rights with a two-step method: (1) we copyright the
+library, and (2) we offer you this license, which gives you legal
+permission to copy, distribute and/or modify the library.
+
+  To protect each distributor, we want to make it very clear that
+there is no warranty for the free library.  Also, if the library is
+modified by someone else and passed on, the recipients should know
+that what they have is not the original version, so that the original
+author's reputation will not be affected by problems that might be
+introduced by others.
+
+  Finally, software patents pose a constant threat to the existence of
+any free program.  We wish to make sure that a company cannot
+effectively restrict the users of a free program by obtaining a
+restrictive license from a patent holder.  Therefore, we insist that
+any patent license obtained for a version of the library must be
+consistent with the full freedom of use specified in this license.
+
+  Most GNU software, including some libraries, is covered by the
+ordinary GNU General Public License.  This license, the GNU Lesser
+General Public License, applies to certain designated libraries, and
+is quite different from the ordinary General Public License.  We use
+this license for certain libraries in order to permit linking those
+libraries into non-free programs.
+
+  When a program is linked with a library, whether statically or using
+a shared library, the combination of the two is legally speaking a
+combined work, a derivative of the original library.  The ordinary
+General Public License therefore permits such linking only if the
+entire combination fits its criteria of freedom.  The Lesser General
+Public License permits more lax criteria for linking other code with
+the library.
+
+  We call this license the "Lesser" General Public License because it
+does Less to protect the user's freedom than the ordinary General
+Public License.  It also provides other free software developers Less
+of an advantage over competing non-free programs.  These disadvantages
+are the reason we use the ordinary General Public License for many
+libraries.  However, the Lesser license provides advantages in certain
+special circumstances.
+
+  For example, on rare occasions, there may be a special need to
+encourage the widest possible use of a certain library, so that it becomes
+a de-facto standard.  To achieve this, non-free programs must be
+allowed to use the library.  A more frequent case is that a free
+library does the same job as widely used non-free libraries.  In this
+case, there is little to gain by limiting the free library to free
+software only, so we use the Lesser General Public License.
+
+  In other cases, permission to use a particular library in non-free
+programs enables a greater number of people to use a large body of
+free software.  For example, permission to use the GNU C Library in
+non-free programs enables many more people to use the whole GNU
+operating system, as well as its variant, the GNU/Linux operating
+system.
+
+  Although the Lesser General Public License is Less protective of the
+users' freedom, it does ensure that the user of a program that is
+linked with the Library has the freedom and the wherewithal to run
+that program using a modified version of the Library.
+
+  The precise terms and conditions for copying, distribution and
+modification follow.  Pay close attention to the difference between a
+"work based on the library" and a "work that uses the library".  The
+former contains code derived from the library, whereas the latter must
+be combined with the library in order to run.
+
+		  GNU LESSER GENERAL PUBLIC LICENSE
+   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
+
+  0. This License Agreement applies to any software library or other
+program which contains a notice placed by the copyright holder or
+other authorized party saying it may be distributed under the terms of
+this Lesser General Public License (also called "this License").
+Each licensee is addressed as "you".
+
+  A "library" means a collection of software functions and/or data
+prepared so as to be conveniently linked with application programs
+(which use some of those functions and data) to form executables.
+
+  The "Library", below, refers to any such software library or work
+which has been distributed under these terms.  A "work based on the
+Library" means either the Library or any derivative work under
+copyright law: that is to say, a work containing the Library or a
+portion of it, either verbatim or with modifications and/or translated
+straightforwardly into another language.  (Hereinafter, translation is
+included without limitation in the term "modification".)
+
+  "Source code" for a work means the preferred form of the work for
+making modifications to it.  For a library, complete source code means
+all the source code for all modules it contains, plus any associated
+interface definition files, plus the scripts used to control compilation
+and installation of the library.
+
+  Activities other than copying, distribution and modification are not
+covered by this License; they are outside its scope.  The act of
+running a program using the Library is not restricted, and output from
+such a program is covered only if its contents constitute a work based
+on the Library (independent of the use of the Library in a tool for
+writing it).  Whether that is true depends on what the Library does
+and what the program that uses the Library does.
+  
+  1. You may copy and distribute verbatim copies of the Library's
+complete source code as you receive it, in any medium, provided that
+you conspicuously and appropriately publish on each copy an
+appropriate copyright notice and disclaimer of warranty; keep intact
+all the notices that refer to this License and to the absence of any
+warranty; and distribute a copy of this License along with the
+Library.
+
+  You may charge a fee for the physical act of transferring a copy,
+and you may at your option offer warranty protection in exchange for a
+fee.
+
+  2. You may modify your copy or copies of the Library or any portion
+of it, thus forming a work based on the Library, and copy and
+distribute such modifications or work under the terms of Section 1
+above, provided that you also meet all of these conditions:
+
+    a) The modified work must itself be a software library.
+
+    b) You must cause the files modified to carry prominent notices
+    stating that you changed the files and the date of any change.
+
+    c) You must cause the whole of the work to be licensed at no
+    charge to all third parties under the terms of this License.
+
+    d) If a facility in the modified Library refers to a function or a
+    table of data to be supplied by an application program that uses
+    the facility, other than as an argument passed when the facility
+    is invoked, then you must make a good faith effort to ensure that,
+    in the event an application does not supply such function or
+    table, the facility still operates, and performs whatever part of
+    its purpose remains meaningful.
+
+    (For example, a function in a library to compute square roots has
+    a purpose that is entirely well-defined independent of the
+    application.  Therefore, Subsection 2d requires that any
+    application-supplied function or table used by this function must
+    be optional: if the application does not supply it, the square
+    root function must still compute square roots.)
+
+These requirements apply to the modified work as a whole.  If
+identifiable sections of that work are not derived from the Library,
+and can be reasonably considered independent and separate works in
+themselves, then this License, and its terms, do not apply to those
+sections when you distribute them as separate works.  But when you
+distribute the same sections as part of a whole which is a work based
+on the Library, the distribution of the whole must be on the terms of
+this License, whose permissions for other licensees extend to the
+entire whole, and thus to each and every part regardless of who wrote
+it.
+
+Thus, it is not the intent of this section to claim rights or contest
+your rights to work written entirely by you; rather, the intent is to
+exercise the right to control the distribution of derivative or
+collective works based on the Library.
+
+In addition, mere aggregation of another work not based on the Library
+with the Library (or with a work based on the Library) on a volume of
+a storage or distribution medium does not bring the other work under
+the scope of this License.
+
+  3. You may opt to apply the terms of the ordinary GNU General Public
+License instead of this License to a given copy of the Library.  To do
+this, you must alter all the notices that refer to this License, so
+that they refer to the ordinary GNU General Public License, version 2,
+instead of to this License.  (If a newer version than version 2 of the
+ordinary GNU General Public License has appeared, then you can specify
+that version instead if you wish.)  Do not make any other change in
+these notices.
+
+  Once this change is made in a given copy, it is irreversible for
+that copy, so the ordinary GNU General Public License applies to all
+subsequent copies and derivative works made from that copy.
+
+  This option is useful when you wish to copy part of the code of
+the Library into a program that is not a library.
+
+  4. You may copy and distribute the Library (or a portion or
+derivative of it, under Section 2) in object code or executable form
+under the terms of Sections 1 and 2 above provided that you accompany
+it with the complete corresponding machine-readable source code, which
+must be distributed under the terms of Sections 1 and 2 above on a
+medium customarily used for software interchange.
+
+  If distribution of object code is made by offering access to copy
+from a designated place, then offering equivalent access to copy the
+source code from the same place satisfies the requirement to
+distribute the source code, even though third parties are not
+compelled to copy the source along with the object code.
+
+  5. A program that contains no derivative of any portion of the
+Library, but is designed to work with the Library by being compiled or
+linked with it, is called a "work that uses the Library".  Such a
+work, in isolation, is not a derivative work of the Library, and
+therefore falls outside the scope of this License.
+
+  However, linking a "work that uses the Library" with the Library
+creates an executable that is a derivative of the Library (because it
+contains portions of the Library), rather than a "work that uses the
+library".  The executable is therefore covered by this License.
+Section 6 states terms for distribution of such executables.
+
+  When a "work that uses the Library" uses material from a header file
+that is part of the Library, the object code for the work may be a
+derivative work of the Library even though the source code is not.
+Whether this is true is especially significant if the work can be
+linked without the Library, or if the work is itself a library.  The
+threshold for this to be true is not precisely defined by law.
+
+  If such an object file uses only numerical parameters, data
+structure layouts and accessors, and small macros and small inline
+functions (ten lines or less in length), then the use of the object
+file is unrestricted, regardless of whether it is legally a derivative
+work.  (Executables containing this object code plus portions of the
+Library will still fall under Section 6.)
+
+  Otherwise, if the work is a derivative of the Library, you may
+distribute the object code for the work under the terms of Section 6.
+Any executables containing that work also fall under Section 6,
+whether or not they are linked directly with the Library itself.
+
+  6. As an exception to the Sections above, you may also combine or
+link a "work that uses the Library" with the Library to produce a
+work containing portions of the Library, and distribute that work
+under terms of your choice, provided that the terms permit
+modification of the work for the customer's own use and reverse
+engineering for debugging such modifications.
+
+  You must give prominent notice with each copy of the work that the
+Library is used in it and that the Library and its use are covered by
+this License.  You must supply a copy of this License.  If the work
+during execution displays copyright notices, you must include the
+copyright notice for the Library among them, as well as a reference
+directing the user to the copy of this License.  Also, you must do one
+of these things:
+
+    a) Accompany the work with the complete corresponding
+    machine-readable source code for the Library including whatever
+    changes were used in the work (which must be distributed under
+    Sections 1 and 2 above); and, if the work is an executable linked
+    with the Library, with the complete machine-readable "work that
+    uses the Library", as object code and/or source code, so that the
+    user can modify the Library and then relink to produce a modified
+    executable containing the modified Library.  (It is understood
+    that the user who changes the contents of definitions files in the
+    Library will not necessarily be able to recompile the application
+    to use the modified definitions.)
+
+    b) Use a suitable shared library mechanism for linking with the
+    Library.  A suitable mechanism is one that (1) uses at run time a
+    copy of the library already present on the user's computer system,
+    rather than copying library functions into the executable, and (2)
+    will operate properly with a modified version of the library, if
+    the user installs one, as long as the modified version is
+    interface-compatible with the version that the work was made with.
+
+    c) Accompany the work with a written offer, valid for at
+    least three years, to give the same user the materials
+    specified in Subsection 6a, above, for a charge no more
+    than the cost of performing this distribution.
+
+    d) If distribution of the work is made by offering access to copy
+    from a designated place, offer equivalent access to copy the above
+    specified materials from the same place.
+
+    e) Verify that the user has already received a copy of these
+    materials or that you have already sent this user a copy.
+
+  For an executable, the required form of the "work that uses the
+Library" must include any data and utility programs needed for
+reproducing the executable from it.  However, as a special exception,
+the materials to be distributed need not include anything that is
+normally distributed (in either source or binary form) with the major
+components (compiler, kernel, and so on) of the operating system on
+which the executable runs, unless that component itself accompanies
+the executable.
+
+  It may happen that this requirement contradicts the license
+restrictions of other proprietary libraries that do not normally
+accompany the operating system.  Such a contradiction means you cannot
+use both them and the Library together in an executable that you
+distribute.
+
+  7. You may place library facilities that are a work based on the
+Library side-by-side in a single library together with other library
+facilities not covered by this License, and distribute such a combined
+library, provided that the separate distribution of the work based on
+the Library and of the other library facilities is otherwise
+permitted, and provided that you do these two things:
+
+    a) Accompany the combined library with a copy of the same work
+    based on the Library, uncombined with any other library
+    facilities.  This must be distributed under the terms of the
+    Sections above.
+
+    b) Give prominent notice with the combined library of the fact
+    that part of it is a work based on the Library, and explaining
+    where to find the accompanying uncombined form of the same work.
+
+  8. You may not copy, modify, sublicense, link with, or distribute
+the Library except as expressly provided under this License.  Any
+attempt otherwise to copy, modify, sublicense, link with, or
+distribute the Library is void, and will automatically terminate your
+rights under this License.  However, parties who have received copies,
+or rights, from you under this License will not have their licenses
+terminated so long as such parties remain in full compliance.
+
+  9. You are not required to accept this License, since you have not
+signed it.  However, nothing else grants you permission to modify or
+distribute the Library or its derivative works.  These actions are
+prohibited by law if you do not accept this License.  Therefore, by
+modifying or distributing the Library (or any work based on the
+Library), you indicate your acceptance of this License to do so, and
+all its terms and conditions for copying, distributing or modifying
+the Library or works based on it.
+
+  10. Each time you redistribute the Library (or any work based on the
+Library), the recipient automatically receives a license from the
+original licensor to copy, distribute, link with or modify the Library
+subject to these terms and conditions.  You may not impose any further
+restrictions on the recipients' exercise of the rights granted herein.
+You are not responsible for enforcing compliance by third parties with
+this License.
+
+  11. If, as a consequence of a court judgment or allegation of patent
+infringement or for any other reason (not limited to patent issues),
+conditions are imposed on you (whether by court order, agreement or
+otherwise) that contradict the conditions of this License, they do not
+excuse you from the conditions of this License.  If you cannot
+distribute so as to satisfy simultaneously your obligations under this
+License and any other pertinent obligations, then as a consequence you
+may not distribute the Library at all.  For example, if a patent
+license would not permit royalty-free redistribution of the Library by
+all those who receive copies directly or indirectly through you, then
+the only way you could satisfy both it and this License would be to
+refrain entirely from distribution of the Library.
+
+If any portion of this section is held invalid or unenforceable under any
+particular circumstance, the balance of the section is intended to apply,
+and the section as a whole is intended to apply in other circumstances.
+
+It is not the purpose of this section to induce you to infringe any
+patents or other property right claims or to contest validity of any
+such claims; this section has the sole purpose of protecting the
+integrity of the free software distribution system which is
+implemented by public license practices.  Many people have made
+generous contributions to the wide range of software distributed
+through that system in reliance on consistent application of that
+system; it is up to the author/donor to decide if he or she is willing
+to distribute software through any other system and a licensee cannot
+impose that choice.
+
+This section is intended to make thoroughly clear what is believed to
+be a consequence of the rest of this License.
+
+  12. If the distribution and/or use of the Library is restricted in
+certain countries either by patents or by copyrighted interfaces, the
+original copyright holder who places the Library under this License may add
+an explicit geographical distribution limitation excluding those countries,
+so that distribution is permitted only in or among countries not thus
+excluded.  In such case, this License incorporates the limitation as if
+written in the body of this License.
+
+  13. The Free Software Foundation may publish revised and/or new
+versions of the Lesser General Public License from time to time.
+Such new versions will be similar in spirit to the present version,
+but may differ in detail to address new problems or concerns.
+
+Each version is given a distinguishing version number.  If the Library
+specifies a version number of this License which applies to it and
+"any later version", you have the option of following the terms and
+conditions either of that version or of any later version published by
+the Free Software Foundation.  If the Library does not specify a
+license version number, you may choose any version ever published by
+the Free Software Foundation.
+
+  14. If you wish to incorporate parts of the Library into other free
+programs whose distribution conditions are incompatible with these,
+write to the author to ask for permission.  For software which is
+copyrighted by the Free Software Foundation, write to the Free
+Software Foundation; we sometimes make exceptions for this.  Our
+decision will be guided by the two goals of preserving the free status
+of all derivatives of our free software and of promoting the sharing
+and reuse of software generally.
+
+			    NO WARRANTY
+
+  15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO
+WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW.
+EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR
+OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY
+KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+PURPOSE.  THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE
+LIBRARY IS WITH YOU.  SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME
+THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
+
+  16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN
+WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY
+AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU
+FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR
+CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE
+LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
+RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
+FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
+SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
+DAMAGES.
+
+		     END OF TERMS AND CONDITIONS
+
+           How to Apply These Terms to Your New Libraries
+
+  If you develop a new library, and you want it to be of the greatest
+possible use to the public, we recommend making it free software that
+everyone can redistribute and change.  You can do so by permitting
+redistribution under these terms (or, alternatively, under the terms of the
+ordinary General Public License).
+
+  To apply these terms, attach the following notices to the library.  It is
+safest to attach them to the start of each source file to most effectively
+convey the exclusion of warranty; and each file should have at least the
+"copyright" line and a pointer to where the full notice is found.
+
+    <one line to give the library's name and a brief idea of what it does.>
+    Copyright (C) <year>  <name of author>
+
+    This library is free software; you can redistribute it and/or
+    modify it under the terms of the GNU Lesser General Public
+    License as published by the Free Software Foundation; either
+    version 2.1 of the License, or (at your option) any later version.
+
+    This library is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+    Lesser General Public License for more details.
+
+    You should have received a copy of the GNU Lesser General Public
+    License along with this library; if not, write to the Free Software
+    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+
+Also add information on how to contact you by electronic and paper mail.
+
+You should also get your employer (if you work as a programmer) or your
+school, if any, to sign a "copyright disclaimer" for the library, if
+necessary.  Here is a sample; alter the names:
+
+  Yoyodyne, Inc., hereby disclaims all copyright interest in the
+  library `Frob' (a library for tweaking knobs) written by James Random Hacker.
+
+  <signature of Ty Coon>, 1 April 1990
+  Ty Coon, President of Vice
+
+That's all there is to it!
+
+
+
+
+
+		    GNU GENERAL PUBLIC LICENSE
+		       Version 2, June 1991
+
+ Copyright (C) 1989, 1991 Free Software Foundation, Inc.
+     59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ Everyone is permitted to copy and distribute verbatim copies
+ of this license document, but changing it is not allowed.
+
+			    Preamble
+
+  The licenses for most software are designed to take away your
+freedom to share and change it.  By contrast, the GNU General Public
+License is intended to guarantee your freedom to share and change free
+software--to make sure the software is free for all its users.  This
+General Public License applies to most of the Free Software
+Foundation's software and to any other program whose authors commit to
+using it.  (Some other Free Software Foundation software is covered by
+the GNU Library General Public License instead.)  You can apply it to
+your programs, too.
+
+  When we speak of free software, we are referring to freedom, not
+price.  Our General Public Licenses are designed to make sure that you
+have the freedom to distribute copies of free software (and charge for
+this service if you wish), that you receive source code or can get it
+if you want it, that you can change the software or use pieces of it
+in new free programs; and that you know you can do these things.
+
+  To protect your rights, we need to make restrictions that forbid
+anyone to deny you these rights or to ask you to surrender the rights.
+These restrictions translate to certain responsibilities for you if you
+distribute copies of the software, or if you modify it.
+
+  For example, if you distribute copies of such a program, whether
+gratis or for a fee, you must give the recipients all the rights that
+you have.  You must make sure that they, too, receive or can get the
+source code.  And you must show them these terms so they know their
+rights.
+
+  We protect your rights with two steps: (1) copyright the software, and
+(2) offer you this license which gives you legal permission to copy,
+distribute and/or modify the software.
+
+  Also, for each author's protection and ours, we want to make certain
+that everyone understands that there is no warranty for this free
+software.  If the software is modified by someone else and passed on, we
+want its recipients to know that what they have is not the original, so
+that any problems introduced by others will not reflect on the original
+authors' reputations.
+
+  Finally, any free program is threatened constantly by software
+patents.  We wish to avoid the danger that redistributors of a free
+program will individually obtain patent licenses, in effect making the
+program proprietary.  To prevent this, we have made it clear that any
+patent must be licensed for everyone's free use or not licensed at all.
+
+  The precise terms and conditions for copying, distribution and
+modification follow.
+
+		    GNU GENERAL PUBLIC LICENSE
+   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
+
+  0. This License applies to any program or other work which contains
+a notice placed by the copyright holder saying it may be distributed
+under the terms of this General Public License.  The "Program", below,
+refers to any such program or work, and a "work based on the Program"
+means either the Program or any derivative work under copyright law:
+that is to say, a work containing the Program or a portion of it,
+either verbatim or with modifications and/or translated into another
+language.  (Hereinafter, translation is included without limitation in
+the term "modification".)  Each licensee is addressed as "you".
+
+Activities other than copying, distribution and modification are not
+covered by this License; they are outside its scope.  The act of
+running the Program is not restricted, and the output from the Program
+is covered only if its contents constitute a work based on the
+Program (independent of having been made by running the Program).
+Whether that is true depends on what the Program does.
+
+  1. You may copy and distribute verbatim copies of the Program's
+source code as you receive it, in any medium, provided that you
+conspicuously and appropriately publish on each copy an appropriate
+copyright notice and disclaimer of warranty; keep intact all the
+notices that refer to this License and to the absence of any warranty;
+and give any other recipients of the Program a copy of this License
+along with the Program.
+
+You may charge a fee for the physical act of transferring a copy, and
+you may at your option offer warranty protection in exchange for a fee.
+
+  2. You may modify your copy or copies of the Program or any portion
+of it, thus forming a work based on the Program, and copy and
+distribute such modifications or work under the terms of Section 1
+above, provided that you also meet all of these conditions:
+
+    a) You must cause the modified files to carry prominent notices
+    stating that you changed the files and the date of any change.
+
+    b) You must cause any work that you distribute or publish, that in
+    whole or in part contains or is derived from the Program or any
+    part thereof, to be licensed as a whole at no charge to all third
+    parties under the terms of this License.
+
+    c) If the modified program normally reads commands interactively
+    when run, you must cause it, when started running for such
+    interactive use in the most ordinary way, to print or display an
+    announcement including an appropriate copyright notice and a
+    notice that there is no warranty (or else, saying that you provide
+    a warranty) and that users may redistribute the program under
+    these conditions, and telling the user how to view a copy of this
+    License.  (Exception: if the Program itself is interactive but
+    does not normally print such an announcement, your work based on
+    the Program is not required to print an announcement.)
+
+These requirements apply to the modified work as a whole.  If
+identifiable sections of that work are not derived from the Program,
+and can be reasonably considered independent and separate works in
+themselves, then this License, and its terms, do not apply to those
+sections when you distribute them as separate works.  But when you
+distribute the same sections as part of a whole which is a work based
+on the Program, the distribution of the whole must be on the terms of
+this License, whose permissions for other licensees extend to the
+entire whole, and thus to each and every part regardless of who wrote it.
+
+Thus, it is not the intent of this section to claim rights or contest
+your rights to work written entirely by you; rather, the intent is to
+exercise the right to control the distribution of derivative or
+collective works based on the Program.
+
+In addition, mere aggregation of another work not based on the Program
+with the Program (or with a work based on the Program) on a volume of
+a storage or distribution medium does not bring the other work under
+the scope of this License.
+
+  3. You may copy and distribute the Program (or a work based on it,
+under Section 2) in object code or executable form under the terms of
+Sections 1 and 2 above provided that you also do one of the following:
+
+    a) Accompany it with the complete corresponding machine-readable
+    source code, which must be distributed under the terms of Sections
+    1 and 2 above on a medium customarily used for software interchange; or,
+
+    b) Accompany it with a written offer, valid for at least three
+    years, to give any third party, for a charge no more than your
+    cost of physically performing source distribution, a complete
+    machine-readable copy of the corresponding source code, to be
+    distributed under the terms of Sections 1 and 2 above on a medium
+    customarily used for software interchange; or,
+
+    c) Accompany it with the information you received as to the offer
+    to distribute corresponding source code.  (This alternative is
+    allowed only for noncommercial distribution and only if you
+    received the program in object code or executable form with such
+    an offer, in accord with Subsection b above.)
+
+The source code for a work means the preferred form of the work for
+making modifications to it.  For an executable work, complete source
+code means all the source code for all modules it contains, plus any
+associated interface definition files, plus the scripts used to
+control compilation and installation of the executable.  However, as a
+special exception, the source code distributed need not include
+anything that is normally distributed (in either source or binary
+form) with the major components (compiler, kernel, and so on) of the
+operating system on which the executable runs, unless that component
+itself accompanies the executable.
+
+If distribution of executable or object code is made by offering
+access to copy from a designated place, then offering equivalent
+access to copy the source code from the same place counts as
+distribution of the source code, even though third parties are not
+compelled to copy the source along with the object code.
+
+  4. You may not copy, modify, sublicense, or distribute the Program
+except as expressly provided under this License.  Any attempt
+otherwise to copy, modify, sublicense or distribute the Program is
+void, and will automatically terminate your rights under this License.
+However, parties who have received copies, or rights, from you under
+this License will not have their licenses terminated so long as such
+parties remain in full compliance.
+
+  5. You are not required to accept this License, since you have not
+signed it.  However, nothing else grants you permission to modify or
+distribute the Program or its derivative works.  These actions are
+prohibited by law if you do not accept this License.  Therefore, by
+modifying or distributing the Program (or any work based on the
+Program), you indicate your acceptance of this License to do so, and
+all its terms and conditions for copying, distributing or modifying
+the Program or works based on it.
+
+  6. Each time you redistribute the Program (or any work based on the
+Program), the recipient automatically receives a license from the
+original licensor to copy, distribute or modify the Program subject to
+these terms and conditions.  You may not impose any further
+restrictions on the recipients' exercise of the rights granted herein.
+You are not responsible for enforcing compliance by third parties to
+this License.
+
+  7. If, as a consequence of a court judgment or allegation of patent
+infringement or for any other reason (not limited to patent issues),
+conditions are imposed on you (whether by court order, agreement or
+otherwise) that contradict the conditions of this License, they do not
+excuse you from the conditions of this License.  If you cannot
+distribute so as to satisfy simultaneously your obligations under this
+License and any other pertinent obligations, then as a consequence you
+may not distribute the Program at all.  For example, if a patent
+license would not permit royalty-free redistribution of the Program by
+all those who receive copies directly or indirectly through you, then
+the only way you could satisfy both it and this License would be to
+refrain entirely from distribution of the Program.
+
+If any portion of this section is held invalid or unenforceable under
+any particular circumstance, the balance of the section is intended to
+apply and the section as a whole is intended to apply in other
+circumstances.
+
+It is not the purpose of this section to induce you to infringe any
+patents or other property right claims or to contest validity of any
+such claims; this section has the sole purpose of protecting the
+integrity of the free software distribution system, which is
+implemented by public license practices.  Many people have made
+generous contributions to the wide range of software distributed
+through that system in reliance on consistent application of that
+system; it is up to the author/donor to decide if he or she is willing
+to distribute software through any other system and a licensee cannot
+impose that choice.
+
+This section is intended to make thoroughly clear what is believed to
+be a consequence of the rest of this License.
+
+  8. If the distribution and/or use of the Program is restricted in
+certain countries either by patents or by copyrighted interfaces, the
+original copyright holder who places the Program under this License
+may add an explicit geographical distribution limitation excluding
+those countries, so that distribution is permitted only in or among
+countries not thus excluded.  In such case, this License incorporates
+the limitation as if written in the body of this License.
+
+  9. The Free Software Foundation may publish revised and/or new versions
+of the General Public License from time to time.  Such new versions will
+be similar in spirit to the present version, but may differ in detail to
+address new problems or concerns.
+
+Each version is given a distinguishing version number.  If the Program
+specifies a version number of this License which applies to it and "any
+later version", you have the option of following the terms and conditions
+either of that version or of any later version published by the Free
+Software Foundation.  If the Program does not specify a version number of
+this License, you may choose any version ever published by the Free Software
+Foundation.
+
+  10. If you wish to incorporate parts of the Program into other free
+programs whose distribution conditions are different, write to the author
+to ask for permission.  For software which is copyrighted by the Free
+Software Foundation, write to the Free Software Foundation; we sometimes
+make exceptions for this.  Our decision will be guided by the two goals
+of preserving the free status of all derivatives of our free software and
+of promoting the sharing and reuse of software generally.
+
+			    NO WARRANTY
+
+  11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
+FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW.  EXCEPT WHEN
+OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
+PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
+OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS
+TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE
+PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
+REPAIR OR CORRECTION.
+
+  12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
+WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
+REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
+INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
+OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
+TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
+YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
+PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
+POSSIBILITY OF SUCH DAMAGES.
+
+		     END OF TERMS AND CONDITIONS
+
+	    How to Apply These Terms to Your New Programs
+
+  If you develop a new program, and you want it to be of the greatest
+possible use to the public, the best way to achieve this is to make it
+free software which everyone can redistribute and change under these terms.
+
+  To do so, attach the following notices to the program.  It is safest
+to attach them to the start of each source file to most effectively
+convey the exclusion of warranty; and each file should have at least
+the "copyright" line and a pointer to where the full notice is found.
+
+    <one line to give the program's name and a brief idea of what it does.>
+    Copyright (C) <year>  <name of author>
+
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program; if not, write to the Free Software
+    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+
+
+Also add information on how to contact you by electronic and paper mail.
+
+If the program is interactive, make it output a short notice like this
+when it starts in an interactive mode:
+
+    Gnomovision version 69, Copyright (C) year  name of author
+    Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
+    This is free software, and you are welcome to redistribute it
+    under certain conditions; type `show c' for details.
+
+The hypothetical commands `show w' and `show c' should show the appropriate
+parts of the General Public License.  Of course, the commands you use may
+be called something other than `show w' and `show c'; they could even be
+mouse-clicks or menu items--whatever suits your program.
+
+You should also get your employer (if you work as a programmer) or your
+school, if any, to sign a "copyright disclaimer" for the program, if
+necessary.  Here is a sample; alter the names:
+
+  Yoyodyne, Inc., hereby disclaims all copyright interest in the program
+  `Gnomovision' (which makes passes at compilers) written by James Hacker.
+
+  <signature of Ty Coon>, 1 April 1989
+  Ty Coon, President of Vice
+
+This General Public License does not permit incorporating your program into
+proprietary programs.  If your program is a subroutine library, you may
+consider it more useful to permit linking proprietary applications with the
+library.  If this is what you want to do, use the GNU Library General
+Public License instead of this License.

Added: freeswitch/trunk/libs/spandsp/ChangeLog
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/ChangeLog	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,47 @@
+07.08.29 - 0.0.4 - Steve Underwood <steveu at coppice.org>
+    - 
+
+06.05.23 - 0.0.3 - Steve Underwood <steveu at coppice.org>
+    - T.38 now implemented, though it needs further polishing.
+    - G.726 and G.722 now implemented.
+
+04.08.29 - 0.0.2 - Steve Underwood <steveu at coppice.org>
+    - T.4 no longer uses libtiff for compresion and decompression on the line side
+      (it is still used to handle the TIFF files). Spandsp no longer depends on
+      accessing the "internals" of libtiff. New 1D and 2D compression and
+      decompression code now handles the line side. This should be more robust than
+      using libtiff, and handles the fudging of bad scan lines rather better.
+    - T.30 line turn-around timing corrected.
+    - T.30 DCS header contents corrected.
+    - T.30 provision for V.17 added, as an option.
+    - T.30 now has a statistics interface, for applications to find details about
+      image transfers.
+    - T.30 processing now correctly closes TIFF files are they have been sent.
+    - FAX header line insertion is now supported for transmitted FAXes.
+    - asynchronous serial processing now has a V.14 compatible rate adaption option.
+    - V.8 modem negotiation module added.
+    - OKI ADPCM now supports Dialogic compatible operation at 6k samples/second, as
+      well as 8k samples/second, using its own sample rate converter.
+    - message queuing added to ease things like T.31 handling
+    - incomplete skeleton (still rather nasty, to be honest) T.31 processing.
+    - incomplete skeleton of V.22bis added. There is probably quite a bit to do
+      to get this complete and robust. It probably requires the T.31 processing
+      be expanded to something more like V.250, so it doesn't just handle FAX.
+    - Incomplete V.17 added (there could be an IBM patent related to the TCM part of
+      V.17, but I think it has expired. Don't distribute binaries of spandsp built
+      with V.17 enabled until this is resolved - and the modem is completed, of
+      course :-) ). The current version transmits OK. It receives OK with the
+      long training sequence, but the symbol and carrier syncing isn't good enough
+      for robust receiving with the short training sequence.
+    - A basic BER tester has been added.
+    - The faster modems now have proper shutdown procedures, whether one is defined in
+      the spec. or not.
+    - The modems (except simple FSK) now have APIs to access performance information.
+    - A common GUI (using FLTK) added for the modem tests.
+    - A bit more documentation added.
+    - Some API inconsistencies cleaned up.
+    - The usual bundle of assorted buglet fixes and polishing.
+
+04.04.29 - 0.0.1k - Steve Underwood <steveu at coppice.org>
+    - The first version which gives solid faxing for a large number of people.
+

Added: freeswitch/trunk/libs/spandsp/DueDiligence
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/DueDiligence	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,55 @@
+Intellectual Property Due Diligence
+-----------------------------------
+
+Modems and voice coding are heavily patented areas. Implementing these without
+serious consideration of IP issues would be foolish. This document describes
+the basis on which the software has been implemented.
+
+
+A check of the intellectual property information at the ITU web site shows a
+number of patent claims against the current standards implemented by spandsp.
+It is important to realise, however, that some of these patents have long
+since expired (group III fax dates back to the 1970s). Also, many are
+related to recent additions to the FAX standard, such as colour FAX handling,
+which few people ever use.
+
+The V.14 rate adaption standard seems free of patent encumberance.
+
+One patent is listed as relevant to the V.17 standard. It is a patent from
+IBM, but the ITU database does not specify its nature. I believe it is
+related to the trellis coding used, and I think it has expired. I do not
+know for sure. The techniques used in the implementation should be free of
+patent encumberance. Most of the implementation is similar to the V.29
+modem. The key addition the trellis code processing. The trellis encoding
+is trivial. The decoding uses Viterbi techniques, which are quite old.
+
+The V.21 standard dates from the 1950s. The V.23 standard is also very old.
+There is no possibility that any patents related to it are still in force.
+However, the implementation also needs to be free of patented techniques.
+The implementation only uses very mature numerical oscillator and quadrature
+correlation techniques, so there should be no patent issues.
+
+Only one patent is listed as relevant to the V.29 standard. This dates from
+the 1970s, and must have expired. The modem has been implemented using only
+very mature techniques, none of which can be less than 20 years old. There
+seem no possibility, therefore, that any patents are still in force related
+to the techniques used.
+
+Some aspect of the V.8 standard seems to have patents associated with it,
+according to the ITU patent database. I am unclear what these are. V.8 is a
+very simple standard. There seems to be nothing innovative about it.
+
+Many patents are listed as relevant to the T.30 standard. However, they all
+appear to relate to newer features, such as colour FAX, added in recent years.
+The current implementation only covers the original features from the late
+1970s, where there appear to be patent issues.
+
+The T.4 standard defines the image compression and decompression techniques
+used for group 3 FAXes. The spandsp implementation is based on code derived
+from freely available implementations of T.4. These have existed for a number
+of years without IP issues. The standard is old enough for any patents to have
+expired, anyway.
+
+V.42bis compression uses the LZW algorithm. This is the same algorithm used in
+GIF files. Unisys patented this algorithm. However, the Unisys patent has now
+expired.

Added: freeswitch/trunk/libs/spandsp/INSTALL
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/INSTALL	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,182 @@
+Basic Installation
+==================
+
+   These are generic installation instructions.
+
+   The `configure' shell script attempts to guess correct values for
+various system-dependent variables used during compilation.  It uses
+those values to create a `Makefile' in each directory of the package.
+It may also create one or more `.h' files containing system-dependent
+definitions.  Finally, it creates a shell script `config.status' that
+you can run in the future to recreate the current configuration, a file
+`config.cache' that saves the results of its tests to speed up
+reconfiguring, and a file `config.log' containing compiler output
+(useful mainly for debugging `configure').
+
+   If you need to do unusual things to compile the package, please try
+to figure out how `configure' could check whether to do them, and mail
+diffs or instructions to the address given in the `README' so they can
+be considered for the next release.  If at some point `config.cache'
+contains results you don't want to keep, you may remove or edit it.
+
+   The file `configure.ac' is used to create `configure' by a program
+called `autoconf'.  You only need `configure.ac' if you want to change
+it or regenerate `configure' using a newer version of `autoconf'.
+
+The simplest way to compile this package is:
+
+  1. `cd' to the directory containing the package's source code and type
+     `./configure' to configure the package for your system.  If you're
+     using `csh' on an old version of System V, you might need to type
+     `sh ./configure' instead to prevent `csh' from trying to execute
+     `configure' itself.
+
+     Running `configure' takes awhile.  While running, it prints some
+     messages telling which features it is checking for.
+
+  2. Type `make' to compile the package.
+
+  3. Optionally, type `make check' to run any self-tests that come with
+     the package.
+
+  4. Type `make install' to install the programs and any data files and
+     documentation.
+
+  5. You can remove the program binaries and object files from the
+     source code directory by typing `make clean'.  To also remove the
+     files that `configure' created (so you can compile the package for
+     a different kind of computer), type `make distclean'.  There is
+     also a `make maintainer-clean' target, but that is intended mainly
+     for the package's developers.  If you use it, you may have to get
+     all sorts of other programs in order to regenerate files that came
+     with the distribution.
+
+Compilers and Options
+=====================
+
+   Some systems require unusual options for compilation or linking that
+the `configure' script does not know about.  You can give `configure'
+initial values for variables by setting them in the environment.  Using
+a Bourne-compatible shell, you can do that on the command line like
+this:
+     CC=c89 CFLAGS=-O2 LIBS=-lposix ./configure
+
+Or on systems that have the `env' program, you can do it like this:
+     env CPPFLAGS=-I/usr/local/include LDFLAGS=-s ./configure
+
+Compiling For Multiple Architectures
+====================================
+
+   You can compile the package for more than one kind of computer at the
+same time, by placing the object files for each architecture in their
+own directory.  To do this, you must use a version of `make' that
+supports the `VPATH' variable, such as GNU `make'.  `cd' to the
+directory where you want the object files and executables to go and run
+the `configure' script.  `configure' automatically checks for the
+source code in the directory that `configure' is in and in `..'.
+
+   If you have to use a `make' that does not supports the `VPATH'
+variable, you have to compile the package for one architecture at a time
+in the source code directory.  After you have installed the package for
+one architecture, use `make distclean' before reconfiguring for another
+architecture.
+
+Installation Names
+==================
+
+   By default, `make install' will install the package's files in
+`/usr/local/bin', `/usr/local/man', etc.  You can specify an
+installation prefix other than `/usr/local' by giving `configure' the
+option `--prefix=PATH'.
+
+   You can specify separate installation prefixes for
+architecture-specific files and architecture-independent files.  If you
+give `configure' the option `--exec-prefix=PATH', the package will use
+PATH as the prefix for installing programs and libraries.
+Documentation and other data files will still use the regular prefix.
+
+   In addition, if you use an unusual directory layout you can give
+options like `--bindir=PATH' to specify different values for particular
+kinds of files.  Run `configure --help' for a list of the directories
+you can set and what kinds of files go in them.
+
+   If the package supports it, you can cause programs to be installed
+with an extra prefix or suffix on their names by giving `configure' the
+option `--program-prefix=PREFIX' or `--program-suffix=SUFFIX'.
+
+Optional Features
+=================
+
+   Some packages pay attention to `--enable-FEATURE' options to
+`configure', where FEATURE indicates an optional part of the package.
+They may also pay attention to `--with-PACKAGE' options, where PACKAGE
+is something like `gnu-as' or `x' (for the X Window System).  The
+`README' should mention any `--enable-' and `--with-' options that the
+package recognizes.
+
+   For packages that use the X Window System, `configure' can usually
+find the X include and library files automatically, but if it doesn't,
+you can use the `configure' options `--x-includes=DIR' and
+`--x-libraries=DIR' to specify their locations.
+
+Specifying the System Type
+==========================
+
+   There may be some features `configure' can not figure out
+automatically, but needs to determine by the type of host the package
+will run on.  Usually `configure' can figure that out, but if it prints
+a message saying it can not guess the host type, give it the
+`--host=TYPE' option.  TYPE can either be a short name for the system
+type, such as `sun4', or a canonical name with three fields:
+     CPU-COMPANY-SYSTEM
+
+See the file `config.sub' for the possible values of each field.  If
+`config.sub' isn't included in this package, then this package doesn't
+need to know the host type.
+
+   If you are building compiler tools for cross-compiling, you can also
+use the `--target=TYPE' option to select the type of system they will
+produce code for and the `--build=TYPE' option to select the type of
+system on which you are compiling the package.
+
+Sharing Defaults
+================
+
+   If you want to set default values for `configure' scripts to share,
+you can create a site shell script called `config.site' that gives
+default values for variables like `CC', `cache_file', and `prefix'.
+`configure' looks for `PREFIX/share/config.site' if it exists, then
+`PREFIX/etc/config.site' if it exists.  Or, you can set the
+`CONFIG_SITE' environment variable to the location of the site script.
+A warning: not all `configure' scripts look for a site script.
+
+Operation Controls
+==================
+
+   `configure' recognizes the following options to control how it
+operates.
+
+`--cache-file=FILE'
+     Use and save the results of the tests in FILE instead of
+     `./config.cache'.  Set FILE to `/dev/null' to disable caching, for
+     debugging `configure'.
+
+`--help'
+     Print a summary of the options to `configure', and exit.
+
+`--quiet'
+`--silent'
+`-q'
+     Do not print messages saying which checks are being made.  To
+     suppress all normal output, redirect it to `/dev/null' (any error
+     messages will still be shown).
+
+`--srcdir=DIR'
+     Look for the package's source code in directory DIR.  Usually
+     `configure' can determine that directory automatically.
+
+`--version'
+     Print the version of Autoconf used to generate the `configure'
+     script, and exit.
+
+`configure' also accepts some other, not widely useful, options.

Added: freeswitch/trunk/libs/spandsp/Makefile.am
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/Makefile.am	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,104 @@
+##
+## SpanDSP - a series of DSP components for telephony
+##
+## Makefile.am - Process this file with automake to produce Makefile.in
+##
+## This program is free software; you can redistribute it and/or modify
+## it under the terms of the GNU Lesser General Public License version 2.1,
+## as published by the Free Software Foundation.
+##
+## This program is distributed in the hope that it will be useful,
+## but WITHOUT ANY WARRANTY; without even the implied warranty of
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+## GNU Lesser General Public License for more details.
+##
+## You should have received a copy of the GNU Lesser General Public
+## License along with this program; if not, write to the Free Software
+## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+##
+## $Id: Makefile.am,v 1.40 2008/07/15 14:28:20 steveu Exp $
+
+AM_CFLAGS = $(COMP_VENDOR_CFLAGS)
+AM_LDFLAGS = $(COMP_VENDOR_LDFLAGS)
+
+noinst_SCRIPTS = spandsp.spec
+
+MAINTAINERCLEANFILES = Makefile.in
+
+EXTRA_DIST = autogen.sh \
+             DueDiligence \
+             README.testdata \
+             spandsp.spec \
+             wrapper.xsl \
+             libspandsp.vcproj \
+             unpack_g722_data.sh \
+             unpack_g726_data.sh \
+             unpack_gsm0610_data.sh \
+             unpack_v56ter_data.sh \
+             doc/doxygen.in \
+             src/floating_fudge.h \
+             src/spandsp/version.h.in \
+             src/libspandsp.dsp \
+             src/libspandsp.sln \
+             src/msvc/gettimeofday.c \
+             src/msvc/inttypes.h \
+             src/msvc/tgmath.h \
+             src/msvc/unistd.h \
+             src/msvc/sys/time.h \
+             src/msvc/spandsp.def \
+             src/msvc/msvcproj.head \
+             src/msvc/msvcproj.foot \
+             src/msvc/vc8proj.head \
+             src/msvc/vc8proj.foot \
+             spandsp/global-tones.xml \
+             spandsp/tones.dtd \
+             spandsp/tsb85.xml \
+             debian/changelog \
+             debian/compat \
+             debian/control \
+             debian/copyright \
+             debian/libspandsp5.install \
+             debian/libspandsp-dev.install \
+             debian/libspandsp-doc.install \
+             debian/rules \
+             debian/watch
+
+if COND_DOC
+    MAYBE_DOC=doc
+endif
+if COND_TESTS
+    MAYBE_TESTS=spandsp-sim tests
+endif
+if COND_TESTDATA
+    MAYBE_TESTDATA=test-data
+endif
+SUBDIRS = src $(MAYBE_DOC) $(MAYBE_TESTS) $(MAYBE_TESTDATA)
+
+DIST_SUBDIRS = src doc spandsp-sim tests test-data
+
+faq: faq.xml
+	cd faq ; xsltproc ../wrapper.xsl ../faq.xml
+
+rpm: rpm-build
+
+rpm-build:
+	$(MAKE) -$(MAKEFLAGS) bump.rpm.release
+	$(MAKE) -$(MAKEFLAGS) dist
+	rm -rf rpm/BUILD/*
+	rm -f rpm/RPMS/*/*
+	rm -f rpm/SOURCES/*
+	rm -f rpm/SPECS/*
+	rm -f rpm/SRPMS/*
+	rpm -ta --sign @PACKAGE at -@VERSION at .tar.gz
+
+bump.rpm.release: spandsp.spec
+	VERSION="x"; \
+	test -f $(srcdir)/rpm.release && . $(srcdir)/rpm.release; \
+	NEXT_RELEASE=0; \
+	test "$$VERSION" = "@VERSION@" && NEXT_RELEASE="$$RELEASE"; \
+	RELEASE=`expr $$NEXT_RELEASE + 1`; \
+	echo "VERSION=@VERSION@" >$(srcdir)/rpm.release; \
+	echo "RELEASE=$$RELEASE" >>$(srcdir)/rpm.release; \
+	sed 's/^Release: .*/Release: '$$RELEASE'/' \
+		<spandsp.spec >spandsp.spec.new; \
+	mv spandsp.spec.new spandsp.spec

Added: freeswitch/trunk/libs/spandsp/NEWS
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/NEWS	Wed Sep  3 15:02:00 2008
@@ -0,0 +1 @@
+No news is good news!
\ No newline at end of file

Added: freeswitch/trunk/libs/spandsp/README
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/README	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,35 @@
+spandsp 0.0.4 - A DSP library for telephony
+-------------------------------------------
+
+SpanDSP is a library of DSP functions for telephony, in the 8000 sample per
+second world of E1s, T1s, and higher order PCM channels. It contains low level
+functions, such as basic filters. It also contains higher level functions, such
+as cadenced supervisory tone detection, and a complete software FAX machine.
+The software has been designed to avoid intellectual property issues, using
+mature techniques where all relevant patents have expired. See the file
+DueDiligence for important information about these intellectual property issues.
+
+The library is licenced under the LGPL 2.1 licence. The test suite, and some support
+programs are licenced under the GPL 2 licence. The full text of these licences can
+be found in the file COPYING.
+
+Dependencies
+------------
+
+spandsp depends on various other packages for various tasks. Most of these
+dependencies relate to building the test suite.
+
+libtiff (and libtiff-devel on most Linux distributions) is required to
+build the spandsp library.
+
+libaudiofile (and libaudiofile-devel) is required to build the test suite
+fftw (and fftw-devel) is required to build the test suite. Version 2 or 3 of
+FFTW may be used. Spandsp adapts to the differences between them.
+
+fltk (and fltk-devel), Fl_Cartesian and Fl_Audio_Meter are required to build the
+test suite with GUI interfaces for some of the tests. The tests will build
+without these packages, but the GUI features will not be available.
+Fl_Cartesian and Fl_Audio_Meter can be downloaded from
+http://www.soft-switch.org/downloads.
+
+Steve Underwood <steveu at coppice.org>

Added: freeswitch/trunk/libs/spandsp/README.testdata
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/README.testdata	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,53 @@
+Setting up test data for the supplied suite of test programs.
+-------------------------------------------------------------
+
+Some of the tests in the test suite for this package require test data files. These test
+data files fall into four categories:
+
+    - Some data files, such as those for some of the FAX tests, are generated by programs
+      in the package.
+
+    - Some are freely distributable data files, from various sources. These are supplied
+      with the package.
+
+    - Some are test data files from a standards body. These are the copyright material of
+      the standards body, and so cannot be distributed with this package. However, most of
+      these files can currently be downloaded at no charge from the standards body's web
+      site. This file describes which files are needed, and how they may be processed to
+      produce the right files in the right places for the tests.
+
+    - The two industry standard sources of test data for DTMF decoders are Bellcore/Telcordia
+      and Mitel. Neither of these is either free, or supplied in a form directly usable
+      with the test suite. These data sources are expected by several tests, and not just
+      the DTMF decoder tests. Sad to say, you are on your own when trying to source data
+      for these tests.
+
+For the G.722 tests, obtain the file T-REC-G.722-198703-I!AppII!ZPF-E.zip from the ITU web site,
+and place it in this directory. Then run
+
+./unpack_g722_data.sh
+
+For the G.726 tests, obtain the file T-REC-G.726-199103-I!AppII!SOFT-ZST-E.zip from the ITU web
+site, and place it in this directory. Then run
+
+./unpack_g726_data.sh
+
+For the GSM 06.10 tests, obtain the file en_300961v080101p0.zip from the ETSI web site,
+and place it in this directory. If you are able to run .exe (i.e. MS DOS or Windows)
+files you can simply run
+
+./unpack_gsm0610_data.sh
+
+If you are unable to run .exe files on the machine you are using (e.e. you are not using Cygwin,
+Mingw, Wine, FreeDOS or something of that sort) you will need to find a machine which will. First,
+run
+
+./unpack_gsm0610_data.sh --no-exe
+
+This will leave 6 .EXE files in the etsitetss/gsm0610 directory. Take these to a machine which
+can run .EXE files, and execute all of them. This should result in a number of files which end
+with .COD, INP and .OUT. Copy these to the etsitests/gsm0610 directory, and run
+
+./unpack_gsm0610_data.sh --no-exe-continue
+
+The remainder of the extraction and repacking of files should then take place.

Added: freeswitch/trunk/libs/spandsp/aclocal.m4
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/aclocal.m4	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,7489 @@
+# generated automatically by aclocal 1.9.6 -*- Autoconf -*-
+
+# Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
+# 2005  Free Software Foundation, Inc.
+# This file is free software; the Free Software Foundation
+# gives unlimited permission to copy and/or distribute it,
+# with or without modifications, as long as this notice is preserved.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY, to the extent permitted by law; without
+# even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+# PARTICULAR PURPOSE.
+
+# libtool.m4 - Configure libtool for the host system. -*-Autoconf-*-
+
+# serial 52 AC_PROG_LIBTOOL
+
+
+# AC_PROVIDE_IFELSE(MACRO-NAME, IF-PROVIDED, IF-NOT-PROVIDED)
+# -----------------------------------------------------------
+# If this macro is not defined by Autoconf, define it here.
+m4_ifdef([AC_PROVIDE_IFELSE],
+         [],
+         [m4_define([AC_PROVIDE_IFELSE],
+	         [m4_ifdef([AC_PROVIDE_$1],
+		           [$2], [$3])])])
+
+
+# AC_PROG_LIBTOOL
+# ---------------
+AC_DEFUN([AC_PROG_LIBTOOL],
+[AC_REQUIRE([_AC_PROG_LIBTOOL])dnl
+dnl If AC_PROG_CXX has already been expanded, run AC_LIBTOOL_CXX
+dnl immediately, otherwise, hook it in at the end of AC_PROG_CXX.
+  AC_PROVIDE_IFELSE([AC_PROG_CXX],
+    [AC_LIBTOOL_CXX],
+    [define([AC_PROG_CXX], defn([AC_PROG_CXX])[AC_LIBTOOL_CXX
+  ])])
+dnl And a similar setup for Fortran 77 support
+  AC_PROVIDE_IFELSE([AC_PROG_F77],
+    [AC_LIBTOOL_F77],
+    [define([AC_PROG_F77], defn([AC_PROG_F77])[AC_LIBTOOL_F77
+])])
+
+dnl Quote A][M_PROG_GCJ so that aclocal doesn't bring it in needlessly.
+dnl If either AC_PROG_GCJ or A][M_PROG_GCJ have already been expanded, run
+dnl AC_LIBTOOL_GCJ immediately, otherwise, hook it in at the end of both.
+  AC_PROVIDE_IFELSE([AC_PROG_GCJ],
+    [AC_LIBTOOL_GCJ],
+    [AC_PROVIDE_IFELSE([A][M_PROG_GCJ],
+      [AC_LIBTOOL_GCJ],
+      [AC_PROVIDE_IFELSE([LT_AC_PROG_GCJ],
+	[AC_LIBTOOL_GCJ],
+      [ifdef([AC_PROG_GCJ],
+	     [define([AC_PROG_GCJ], defn([AC_PROG_GCJ])[AC_LIBTOOL_GCJ])])
+       ifdef([A][M_PROG_GCJ],
+	     [define([A][M_PROG_GCJ], defn([A][M_PROG_GCJ])[AC_LIBTOOL_GCJ])])
+       ifdef([LT_AC_PROG_GCJ],
+	     [define([LT_AC_PROG_GCJ],
+		defn([LT_AC_PROG_GCJ])[AC_LIBTOOL_GCJ])])])])
+])])# AC_PROG_LIBTOOL
+
+
+# _AC_PROG_LIBTOOL
+# ----------------
+AC_DEFUN([_AC_PROG_LIBTOOL],
+[AC_REQUIRE([AC_LIBTOOL_SETUP])dnl
+AC_BEFORE([$0],[AC_LIBTOOL_CXX])dnl
+AC_BEFORE([$0],[AC_LIBTOOL_F77])dnl
+AC_BEFORE([$0],[AC_LIBTOOL_GCJ])dnl
+
+# This can be used to rebuild libtool when needed
+LIBTOOL_DEPS="$ac_aux_dir/ltmain.sh"
+
+# Always use our own libtool.
+LIBTOOL='$(SHELL) $(top_builddir)/libtool'
+AC_SUBST(LIBTOOL)dnl
+
+# Prevent multiple expansion
+define([AC_PROG_LIBTOOL], [])
+])# _AC_PROG_LIBTOOL
+
+
+# AC_LIBTOOL_SETUP
+# ----------------
+AC_DEFUN([AC_LIBTOOL_SETUP],
+[AC_PREREQ(2.50)dnl
+AC_REQUIRE([AC_ENABLE_SHARED])dnl
+AC_REQUIRE([AC_ENABLE_STATIC])dnl
+AC_REQUIRE([AC_ENABLE_FAST_INSTALL])dnl
+AC_REQUIRE([AC_CANONICAL_HOST])dnl
+AC_REQUIRE([AC_CANONICAL_BUILD])dnl
+AC_REQUIRE([AC_PROG_CC])dnl
+AC_REQUIRE([AC_PROG_LD])dnl
+AC_REQUIRE([AC_PROG_LD_RELOAD_FLAG])dnl
+AC_REQUIRE([AC_PROG_NM])dnl
+
+AC_REQUIRE([AC_PROG_LN_S])dnl
+AC_REQUIRE([AC_DEPLIBS_CHECK_METHOD])dnl
+# Autoconf 2.13's AC_OBJEXT and AC_EXEEXT macros only works for C compilers!
+AC_REQUIRE([AC_OBJEXT])dnl
+AC_REQUIRE([AC_EXEEXT])dnl
+dnl
+AC_LIBTOOL_SYS_MAX_CMD_LEN
+AC_LIBTOOL_SYS_GLOBAL_SYMBOL_PIPE
+AC_LIBTOOL_OBJDIR
+
+AC_REQUIRE([_LT_AC_SYS_COMPILER])dnl
+_LT_AC_PROG_ECHO_BACKSLASH
+
+case $host_os in
+aix3*)
+  # AIX sometimes has problems with the GCC collect2 program.  For some
+  # reason, if we set the COLLECT_NAMES environment variable, the problems
+  # vanish in a puff of smoke.
+  if test "X${COLLECT_NAMES+set}" != Xset; then
+    COLLECT_NAMES=
+    export COLLECT_NAMES
+  fi
+  ;;
+esac
+
+# Sed substitution that helps us do robust quoting.  It backslashifies
+# metacharacters that are still active within double-quoted strings.
+Xsed='sed -e 1s/^X//'
+[sed_quote_subst='s/\([\\"\\`$\\\\]\)/\\\1/g']
+
+# Same as above, but do not quote variable references.
+[double_quote_subst='s/\([\\"\\`\\\\]\)/\\\1/g']
+
+# Sed substitution to delay expansion of an escaped shell variable in a
+# double_quote_subst'ed string.
+delay_variable_subst='s/\\\\\\\\\\\$/\\\\\\$/g'
+
+# Sed substitution to avoid accidental globbing in evaled expressions
+no_glob_subst='s/\*/\\\*/g'
+
+# Constants:
+rm="rm -f"
+
+# Global variables:
+default_ofile=libtool
+can_build_shared=yes
+
+# All known linkers require a `.a' archive for static linking (except MSVC,
+# which needs '.lib').
+libext=a
+ltmain="$ac_aux_dir/ltmain.sh"
+ofile="$default_ofile"
+with_gnu_ld="$lt_cv_prog_gnu_ld"
+
+AC_CHECK_TOOL(AR, ar, false)
+AC_CHECK_TOOL(RANLIB, ranlib, :)
+AC_CHECK_TOOL(STRIP, strip, :)
+
+old_CC="$CC"
+old_CFLAGS="$CFLAGS"
+
+# Set sane defaults for various variables
+test -z "$AR" && AR=ar
+test -z "$AR_FLAGS" && AR_FLAGS=cru
+test -z "$AS" && AS=as
+test -z "$CC" && CC=cc
+test -z "$LTCC" && LTCC=$CC
+test -z "$LTCFLAGS" && LTCFLAGS=$CFLAGS
+test -z "$DLLTOOL" && DLLTOOL=dlltool
+test -z "$LD" && LD=ld
+test -z "$LN_S" && LN_S="ln -s"
+test -z "$MAGIC_CMD" && MAGIC_CMD=file
+test -z "$NM" && NM=nm
+test -z "$SED" && SED=sed
+test -z "$OBJDUMP" && OBJDUMP=objdump
+test -z "$RANLIB" && RANLIB=:
+test -z "$STRIP" && STRIP=:
+test -z "$ac_objext" && ac_objext=o
+
+# Determine commands to create old-style static archives.
+old_archive_cmds='$AR $AR_FLAGS $oldlib$oldobjs'
+old_postinstall_cmds='chmod 644 $oldlib'
+old_postuninstall_cmds=
+
+if test -n "$RANLIB"; then
+  case $host_os in
+  openbsd*)
+    old_postinstall_cmds="$old_postinstall_cmds~\$RANLIB -t \$oldlib"
+    ;;
+  *)
+    old_postinstall_cmds="$old_postinstall_cmds~\$RANLIB \$oldlib"
+    ;;
+  esac
+  old_archive_cmds="$old_archive_cmds~\$RANLIB \$oldlib"
+fi
+
+_LT_CC_BASENAME([$compiler])
+
+# Only perform the check for file, if the check method requires it
+case $deplibs_check_method in
+file_magic*)
+  if test "$file_magic_cmd" = '$MAGIC_CMD'; then
+    AC_PATH_MAGIC
+  fi
+  ;;
+esac
+
+_LT_REQUIRED_DARWIN_CHECKS
+
+AC_PROVIDE_IFELSE([AC_LIBTOOL_DLOPEN], enable_dlopen=yes, enable_dlopen=no)
+AC_PROVIDE_IFELSE([AC_LIBTOOL_WIN32_DLL],
+enable_win32_dll=yes, enable_win32_dll=no)
+
+AC_ARG_ENABLE([libtool-lock],
+    [AC_HELP_STRING([--disable-libtool-lock],
+	[avoid locking (might break parallel builds)])])
+test "x$enable_libtool_lock" != xno && enable_libtool_lock=yes
+
+AC_ARG_WITH([pic],
+    [AC_HELP_STRING([--with-pic],
+	[try to use only PIC/non-PIC objects @<:@default=use both@:>@])],
+    [pic_mode="$withval"],
+    [pic_mode=default])
+test -z "$pic_mode" && pic_mode=default
+
+# Use C for the default configuration in the libtool script
+tagname=
+AC_LIBTOOL_LANG_C_CONFIG
+_LT_AC_TAGCONFIG
+])# AC_LIBTOOL_SETUP
+
+
+# _LT_AC_SYS_COMPILER
+# -------------------
+AC_DEFUN([_LT_AC_SYS_COMPILER],
+[AC_REQUIRE([AC_PROG_CC])dnl
+
+# If no C compiler was specified, use CC.
+LTCC=${LTCC-"$CC"}
+
+# If no C compiler flags were specified, use CFLAGS.
+LTCFLAGS=${LTCFLAGS-"$CFLAGS"}
+
+# Allow CC to be a program name with arguments.
+compiler=$CC
+])# _LT_AC_SYS_COMPILER
+
+
+# _LT_CC_BASENAME(CC)
+# -------------------
+# Calculate cc_basename.  Skip known compiler wrappers and cross-prefix.
+AC_DEFUN([_LT_CC_BASENAME],
+[for cc_temp in $1""; do
+  case $cc_temp in
+    compile | *[[\\/]]compile | ccache | *[[\\/]]ccache ) ;;
+    distcc | *[[\\/]]distcc | purify | *[[\\/]]purify ) ;;
+    \-*) ;;
+    *) break;;
+  esac
+done
+cc_basename=`$echo "X$cc_temp" | $Xsed -e 's%.*/%%' -e "s%^$host_alias-%%"`
+])
+
+
+# _LT_COMPILER_BOILERPLATE
+# ------------------------
+# Check for compiler boilerplate output or warnings with
+# the simple compiler test code.
+AC_DEFUN([_LT_COMPILER_BOILERPLATE],
+[AC_REQUIRE([LT_AC_PROG_SED])dnl
+ac_outfile=conftest.$ac_objext
+echo "$lt_simple_compile_test_code" >conftest.$ac_ext
+eval "$ac_compile" 2>&1 >/dev/null | $SED '/^$/d; /^ *+/d' >conftest.err
+_lt_compiler_boilerplate=`cat conftest.err`
+$rm conftest*
+])# _LT_COMPILER_BOILERPLATE
+
+
+# _LT_LINKER_BOILERPLATE
+# ----------------------
+# Check for linker boilerplate output or warnings with
+# the simple link test code.
+AC_DEFUN([_LT_LINKER_BOILERPLATE],
+[AC_REQUIRE([LT_AC_PROG_SED])dnl
+ac_outfile=conftest.$ac_objext
+echo "$lt_simple_link_test_code" >conftest.$ac_ext
+eval "$ac_link" 2>&1 >/dev/null | $SED '/^$/d; /^ *+/d' >conftest.err
+_lt_linker_boilerplate=`cat conftest.err`
+$rm -r conftest*
+])# _LT_LINKER_BOILERPLATE
+
+# _LT_REQUIRED_DARWIN_CHECKS
+# --------------------------
+# Check for some things on darwin
+AC_DEFUN([_LT_REQUIRED_DARWIN_CHECKS],[
+  case $host_os in
+    rhapsody* | darwin*)
+    AC_CHECK_TOOL([DSYMUTIL], [dsymutil], [:])
+    AC_CHECK_TOOL([NMEDIT], [nmedit], [:])
+
+    AC_CACHE_CHECK([for -single_module linker flag],[lt_cv_apple_cc_single_mod],
+      [lt_cv_apple_cc_single_mod=no
+      if test -z "${LT_MULTI_MODULE}"; then
+   # By default we will add the -single_module flag. You can override
+   # by either setting the environment variable LT_MULTI_MODULE
+   # non-empty at configure time, or by adding -multi_module to the
+   # link flags.
+   echo "int foo(void){return 1;}" > conftest.c
+   $LTCC $LTCFLAGS $LDFLAGS -o libconftest.dylib \
+     -dynamiclib ${wl}-single_module conftest.c
+   if test -f libconftest.dylib; then
+     lt_cv_apple_cc_single_mod=yes
+     rm -rf libconftest.dylib*
+   fi
+   rm conftest.c
+      fi])
+    AC_CACHE_CHECK([for -exported_symbols_list linker flag],
+      [lt_cv_ld_exported_symbols_list],
+      [lt_cv_ld_exported_symbols_list=no
+      save_LDFLAGS=$LDFLAGS
+      echo "_main" > conftest.sym
+      LDFLAGS="$LDFLAGS -Wl,-exported_symbols_list,conftest.sym"
+      AC_LINK_IFELSE([AC_LANG_PROGRAM([],[])],
+   [lt_cv_ld_exported_symbols_list=yes],
+   [lt_cv_ld_exported_symbols_list=no])
+   LDFLAGS="$save_LDFLAGS"
+    ])
+    case $host_os in
+    rhapsody* | darwin1.[[0123]])
+      _lt_dar_allow_undefined='${wl}-undefined ${wl}suppress' ;;
+    darwin1.*)
+     _lt_dar_allow_undefined='${wl}-flat_namespace ${wl}-undefined ${wl}suppress' ;;
+    darwin*)
+      # if running on 10.5 or later, the deployment target defaults
+      # to the OS version, if on x86, and 10.4, the deployment
+      # target defaults to 10.4. Don't you love it?
+      case ${MACOSX_DEPLOYMENT_TARGET-10.0},$host in
+   10.0,*86*-darwin8*|10.0,*-darwin[[91]]*)
+     _lt_dar_allow_undefined='${wl}-undefined ${wl}dynamic_lookup' ;;
+   10.[[012]]*)
+     _lt_dar_allow_undefined='${wl}-flat_namespace ${wl}-undefined ${wl}suppress' ;;
+   10.*)
+     _lt_dar_allow_undefined='${wl}-undefined ${wl}dynamic_lookup' ;;
+      esac
+    ;;
+  esac
+    if test "$lt_cv_apple_cc_single_mod" = "yes"; then
+      _lt_dar_single_mod='$single_module'
+    fi
+    if test "$lt_cv_ld_exported_symbols_list" = "yes"; then
+      _lt_dar_export_syms=' ${wl}-exported_symbols_list,$output_objdir/${libname}-symbols.expsym'
+    else
+      _lt_dar_export_syms="~$NMEDIT -s \$output_objdir/\${libname}-symbols.expsym \${lib}"
+    fi
+    if test "$DSYMUTIL" != ":"; then
+      _lt_dsymutil="~$DSYMUTIL \$lib || :"
+    else
+      _lt_dsymutil=
+    fi
+    ;;
+  esac
+])
+
+# _LT_AC_SYS_LIBPATH_AIX
+# ----------------------
+# Links a minimal program and checks the executable
+# for the system default hardcoded library path. In most cases,
+# this is /usr/lib:/lib, but when the MPI compilers are used
+# the location of the communication and MPI libs are included too.
+# If we don't find anything, use the default library path according
+# to the aix ld manual.
+AC_DEFUN([_LT_AC_SYS_LIBPATH_AIX],
+[AC_REQUIRE([LT_AC_PROG_SED])dnl
+AC_LINK_IFELSE(AC_LANG_PROGRAM,[
+lt_aix_libpath_sed='
+    /Import File Strings/,/^$/ {
+	/^0/ {
+	    s/^0  *\(.*\)$/\1/
+	    p
+	}
+    }'
+aix_libpath=`dump -H conftest$ac_exeext 2>/dev/null | $SED -n -e "$lt_aix_libpath_sed"`
+# Check for a 64-bit object if we didn't find anything.
+if test -z "$aix_libpath"; then
+  aix_libpath=`dump -HX64 conftest$ac_exeext 2>/dev/null | $SED -n -e "$lt_aix_libpath_sed"`
+fi],[])
+if test -z "$aix_libpath"; then aix_libpath="/usr/lib:/lib"; fi
+])# _LT_AC_SYS_LIBPATH_AIX
+
+
+# _LT_AC_SHELL_INIT(ARG)
+# ----------------------
+AC_DEFUN([_LT_AC_SHELL_INIT],
+[ifdef([AC_DIVERSION_NOTICE],
+	     [AC_DIVERT_PUSH(AC_DIVERSION_NOTICE)],
+	 [AC_DIVERT_PUSH(NOTICE)])
+$1
+AC_DIVERT_POP
+])# _LT_AC_SHELL_INIT
+
+
+# _LT_AC_PROG_ECHO_BACKSLASH
+# --------------------------
+# Add some code to the start of the generated configure script which
+# will find an echo command which doesn't interpret backslashes.
+AC_DEFUN([_LT_AC_PROG_ECHO_BACKSLASH],
+[_LT_AC_SHELL_INIT([
+# Check that we are running under the correct shell.
+SHELL=${CONFIG_SHELL-/bin/sh}
+
+case X$ECHO in
+X*--fallback-echo)
+  # Remove one level of quotation (which was required for Make).
+  ECHO=`echo "$ECHO" | sed 's,\\\\\[$]\\[$]0,'[$]0','`
+  ;;
+esac
+
+echo=${ECHO-echo}
+if test "X[$]1" = X--no-reexec; then
+  # Discard the --no-reexec flag, and continue.
+  shift
+elif test "X[$]1" = X--fallback-echo; then
+  # Avoid inline document here, it may be left over
+  :
+elif test "X`($echo '\t') 2>/dev/null`" = 'X\t' ; then
+  # Yippee, $echo works!
+  :
+else
+  # Restart under the correct shell.
+  exec $SHELL "[$]0" --no-reexec ${1+"[$]@"}
+fi
+
+if test "X[$]1" = X--fallback-echo; then
+  # used as fallback echo
+  shift
+  cat <<EOF
+[$]*
+EOF
+  exit 0
+fi
+
+# The HP-UX ksh and POSIX shell print the target directory to stdout
+# if CDPATH is set.
+(unset CDPATH) >/dev/null 2>&1 && unset CDPATH
+
+if test -z "$ECHO"; then
+if test "X${echo_test_string+set}" != Xset; then
+# find a string as large as possible, as long as the shell can cope with it
+  for cmd in 'sed 50q "[$]0"' 'sed 20q "[$]0"' 'sed 10q "[$]0"' 'sed 2q "[$]0"' 'echo test'; do
+    # expected sizes: less than 2Kb, 1Kb, 512 bytes, 16 bytes, ...
+    if (echo_test_string=`eval $cmd`) 2>/dev/null &&
+       echo_test_string=`eval $cmd` &&
+       (test "X$echo_test_string" = "X$echo_test_string") 2>/dev/null
+    then
+      break
+    fi
+  done
+fi
+
+if test "X`($echo '\t') 2>/dev/null`" = 'X\t' &&
+   echo_testing_string=`($echo "$echo_test_string") 2>/dev/null` &&
+   test "X$echo_testing_string" = "X$echo_test_string"; then
+  :
+else
+  # The Solaris, AIX, and Digital Unix default echo programs unquote
+  # backslashes.  This makes it impossible to quote backslashes using
+  #   echo "$something" | sed 's/\\/\\\\/g'
+  #
+  # So, first we look for a working echo in the user's PATH.
+
+  lt_save_ifs="$IFS"; IFS=$PATH_SEPARATOR
+  for dir in $PATH /usr/ucb; do
+    IFS="$lt_save_ifs"
+    if (test -f $dir/echo || test -f $dir/echo$ac_exeext) &&
+       test "X`($dir/echo '\t') 2>/dev/null`" = 'X\t' &&
+       echo_testing_string=`($dir/echo "$echo_test_string") 2>/dev/null` &&
+       test "X$echo_testing_string" = "X$echo_test_string"; then
+      echo="$dir/echo"
+      break
+    fi
+  done
+  IFS="$lt_save_ifs"
+
+  if test "X$echo" = Xecho; then
+    # We didn't find a better echo, so look for alternatives.
+    if test "X`(print -r '\t') 2>/dev/null`" = 'X\t' &&
+       echo_testing_string=`(print -r "$echo_test_string") 2>/dev/null` &&
+       test "X$echo_testing_string" = "X$echo_test_string"; then
+      # This shell has a builtin print -r that does the trick.
+      echo='print -r'
+    elif (test -f /bin/ksh || test -f /bin/ksh$ac_exeext) &&
+	 test "X$CONFIG_SHELL" != X/bin/ksh; then
+      # If we have ksh, try running configure again with it.
+      ORIGINAL_CONFIG_SHELL=${CONFIG_SHELL-/bin/sh}
+      export ORIGINAL_CONFIG_SHELL
+      CONFIG_SHELL=/bin/ksh
+      export CONFIG_SHELL
+      exec $CONFIG_SHELL "[$]0" --no-reexec ${1+"[$]@"}
+    else
+      # Try using printf.
+      echo='printf %s\n'
+      if test "X`($echo '\t') 2>/dev/null`" = 'X\t' &&
+	 echo_testing_string=`($echo "$echo_test_string") 2>/dev/null` &&
+	 test "X$echo_testing_string" = "X$echo_test_string"; then
+	# Cool, printf works
+	:
+      elif echo_testing_string=`($ORIGINAL_CONFIG_SHELL "[$]0" --fallback-echo '\t') 2>/dev/null` &&
+	   test "X$echo_testing_string" = 'X\t' &&
+	   echo_testing_string=`($ORIGINAL_CONFIG_SHELL "[$]0" --fallback-echo "$echo_test_string") 2>/dev/null` &&
+	   test "X$echo_testing_string" = "X$echo_test_string"; then
+	CONFIG_SHELL=$ORIGINAL_CONFIG_SHELL
+	export CONFIG_SHELL
+	SHELL="$CONFIG_SHELL"
+	export SHELL
+	echo="$CONFIG_SHELL [$]0 --fallback-echo"
+      elif echo_testing_string=`($CONFIG_SHELL "[$]0" --fallback-echo '\t') 2>/dev/null` &&
+	   test "X$echo_testing_string" = 'X\t' &&
+	   echo_testing_string=`($CONFIG_SHELL "[$]0" --fallback-echo "$echo_test_string") 2>/dev/null` &&
+	   test "X$echo_testing_string" = "X$echo_test_string"; then
+	echo="$CONFIG_SHELL [$]0 --fallback-echo"
+      else
+	# maybe with a smaller string...
+	prev=:
+
+	for cmd in 'echo test' 'sed 2q "[$]0"' 'sed 10q "[$]0"' 'sed 20q "[$]0"' 'sed 50q "[$]0"'; do
+	  if (test "X$echo_test_string" = "X`eval $cmd`") 2>/dev/null
+	  then
+	    break
+	  fi
+	  prev="$cmd"
+	done
+
+	if test "$prev" != 'sed 50q "[$]0"'; then
+	  echo_test_string=`eval $prev`
+	  export echo_test_string
+	  exec ${ORIGINAL_CONFIG_SHELL-${CONFIG_SHELL-/bin/sh}} "[$]0" ${1+"[$]@"}
+	else
+	  # Oops.  We lost completely, so just stick with echo.
+	  echo=echo
+	fi
+      fi
+    fi
+  fi
+fi
+fi
+
+# Copy echo and quote the copy suitably for passing to libtool from
+# the Makefile, instead of quoting the original, which is used later.
+ECHO=$echo
+if test "X$ECHO" = "X$CONFIG_SHELL [$]0 --fallback-echo"; then
+   ECHO="$CONFIG_SHELL \\\$\[$]0 --fallback-echo"
+fi
+
+AC_SUBST(ECHO)
+])])# _LT_AC_PROG_ECHO_BACKSLASH
+
+
+# _LT_AC_LOCK
+# -----------
+AC_DEFUN([_LT_AC_LOCK],
+[AC_ARG_ENABLE([libtool-lock],
+    [AC_HELP_STRING([--disable-libtool-lock],
+	[avoid locking (might break parallel builds)])])
+test "x$enable_libtool_lock" != xno && enable_libtool_lock=yes
+
+# Some flags need to be propagated to the compiler or linker for good
+# libtool support.
+case $host in
+ia64-*-hpux*)
+  # Find out which ABI we are using.
+  echo 'int i;' > conftest.$ac_ext
+  if AC_TRY_EVAL(ac_compile); then
+    case `/usr/bin/file conftest.$ac_objext` in
+    *ELF-32*)
+      HPUX_IA64_MODE="32"
+      ;;
+    *ELF-64*)
+      HPUX_IA64_MODE="64"
+      ;;
+    esac
+  fi
+  rm -rf conftest*
+  ;;
+*-*-irix6*)
+  # Find out which ABI we are using.
+  echo '[#]line __oline__ "configure"' > conftest.$ac_ext
+  if AC_TRY_EVAL(ac_compile); then
+   if test "$lt_cv_prog_gnu_ld" = yes; then
+    case `/usr/bin/file conftest.$ac_objext` in
+    *32-bit*)
+      LD="${LD-ld} -melf32bsmip"
+      ;;
+    *N32*)
+      LD="${LD-ld} -melf32bmipn32"
+      ;;
+    *64-bit*)
+      LD="${LD-ld} -melf64bmip"
+      ;;
+    esac
+   else
+    case `/usr/bin/file conftest.$ac_objext` in
+    *32-bit*)
+      LD="${LD-ld} -32"
+      ;;
+    *N32*)
+      LD="${LD-ld} -n32"
+      ;;
+    *64-bit*)
+      LD="${LD-ld} -64"
+      ;;
+    esac
+   fi
+  fi
+  rm -rf conftest*
+  ;;
+
+x86_64-*kfreebsd*-gnu|x86_64-*linux*|ppc*-*linux*|powerpc*-*linux*| \
+s390*-*linux*|sparc*-*linux*)
+  # Find out which ABI we are using.
+  echo 'int i;' > conftest.$ac_ext
+  if AC_TRY_EVAL(ac_compile); then
+    case `/usr/bin/file conftest.o` in
+    *32-bit*)
+      case $host in
+        x86_64-*kfreebsd*-gnu)
+          LD="${LD-ld} -m elf_i386_fbsd"
+          ;;
+        x86_64-*linux*)
+          LD="${LD-ld} -m elf_i386"
+          ;;
+        ppc64-*linux*|powerpc64-*linux*)
+          LD="${LD-ld} -m elf32ppclinux"
+          ;;
+        s390x-*linux*)
+          LD="${LD-ld} -m elf_s390"
+          ;;
+        sparc64-*linux*)
+          LD="${LD-ld} -m elf32_sparc"
+          ;;
+      esac
+      ;;
+    *64-bit*)
+      libsuff=64
+      case $host in
+        x86_64-*kfreebsd*-gnu)
+          LD="${LD-ld} -m elf_x86_64_fbsd"
+          ;;
+        x86_64-*linux*)
+          LD="${LD-ld} -m elf_x86_64"
+          ;;
+        ppc*-*linux*|powerpc*-*linux*)
+          LD="${LD-ld} -m elf64ppc"
+          ;;
+        s390*-*linux*)
+          LD="${LD-ld} -m elf64_s390"
+          ;;
+        sparc*-*linux*)
+          LD="${LD-ld} -m elf64_sparc"
+          ;;
+      esac
+      ;;
+    esac
+  fi
+  rm -rf conftest*
+  ;;
+
+*-*-sco3.2v5*)
+  # On SCO OpenServer 5, we need -belf to get full-featured binaries.
+  SAVE_CFLAGS="$CFLAGS"
+  CFLAGS="$CFLAGS -belf"
+  AC_CACHE_CHECK([whether the C compiler needs -belf], lt_cv_cc_needs_belf,
+    [AC_LANG_PUSH(C)
+     AC_TRY_LINK([],[],[lt_cv_cc_needs_belf=yes],[lt_cv_cc_needs_belf=no])
+     AC_LANG_POP])
+  if test x"$lt_cv_cc_needs_belf" != x"yes"; then
+    # this is probably gcc 2.8.0, egcs 1.0 or newer; no need for -belf
+    CFLAGS="$SAVE_CFLAGS"
+  fi
+  ;;
+sparc*-*solaris*)
+  # Find out which ABI we are using.
+  echo 'int i;' > conftest.$ac_ext
+  if AC_TRY_EVAL(ac_compile); then
+    case `/usr/bin/file conftest.o` in
+    *64-bit*)
+      case $lt_cv_prog_gnu_ld in
+      yes*) LD="${LD-ld} -m elf64_sparc" ;;
+      *)
+        if ${LD-ld} -64 -r -o conftest2.o conftest.o >/dev/null 2>&1; then
+	  LD="${LD-ld} -64"
+	fi
+	;;
+      esac
+      ;;
+    esac
+  fi
+  rm -rf conftest*
+  ;;
+
+AC_PROVIDE_IFELSE([AC_LIBTOOL_WIN32_DLL],
+[*-*-cygwin* | *-*-mingw* | *-*-pw32*)
+  AC_CHECK_TOOL(DLLTOOL, dlltool, false)
+  AC_CHECK_TOOL(AS, as, false)
+  AC_CHECK_TOOL(OBJDUMP, objdump, false)
+  ;;
+  ])
+esac
+
+need_locks="$enable_libtool_lock"
+
+])# _LT_AC_LOCK
+
+
+# AC_LIBTOOL_COMPILER_OPTION(MESSAGE, VARIABLE-NAME, FLAGS,
+#		[OUTPUT-FILE], [ACTION-SUCCESS], [ACTION-FAILURE])
+# ----------------------------------------------------------------
+# Check whether the given compiler option works
+AC_DEFUN([AC_LIBTOOL_COMPILER_OPTION],
+[AC_REQUIRE([LT_AC_PROG_SED])
+AC_CACHE_CHECK([$1], [$2],
+  [$2=no
+  ifelse([$4], , [ac_outfile=conftest.$ac_objext], [ac_outfile=$4])
+   echo "$lt_simple_compile_test_code" > conftest.$ac_ext
+   lt_compiler_flag="$3"
+   # Insert the option either (1) after the last *FLAGS variable, or
+   # (2) before a word containing "conftest.", or (3) at the end.
+   # Note that $ac_compile itself does not contain backslashes and begins
+   # with a dollar sign (not a hyphen), so the echo should work correctly.
+   # The option is referenced via a variable to avoid confusing sed.
+   lt_compile=`echo "$ac_compile" | $SED \
+   -e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \
+   -e 's: [[^ ]]*conftest\.: $lt_compiler_flag&:; t' \
+   -e 's:$: $lt_compiler_flag:'`
+   (eval echo "\"\$as_me:__oline__: $lt_compile\"" >&AS_MESSAGE_LOG_FD)
+   (eval "$lt_compile" 2>conftest.err)
+   ac_status=$?
+   cat conftest.err >&AS_MESSAGE_LOG_FD
+   echo "$as_me:__oline__: \$? = $ac_status" >&AS_MESSAGE_LOG_FD
+   if (exit $ac_status) && test -s "$ac_outfile"; then
+     # The compiler can only warn and ignore the option if not recognized
+     # So say no if there are warnings other than the usual output.
+     $echo "X$_lt_compiler_boilerplate" | $Xsed -e '/^$/d' >conftest.exp
+     $SED '/^$/d; /^ *+/d' conftest.err >conftest.er2
+     if test ! -s conftest.er2 || diff conftest.exp conftest.er2 >/dev/null; then
+       $2=yes
+     fi
+   fi
+   $rm conftest*
+])
+
+if test x"[$]$2" = xyes; then
+    ifelse([$5], , :, [$5])
+else
+    ifelse([$6], , :, [$6])
+fi
+])# AC_LIBTOOL_COMPILER_OPTION
+
+
+# AC_LIBTOOL_LINKER_OPTION(MESSAGE, VARIABLE-NAME, FLAGS,
+#                          [ACTION-SUCCESS], [ACTION-FAILURE])
+# ------------------------------------------------------------
+# Check whether the given compiler option works
+AC_DEFUN([AC_LIBTOOL_LINKER_OPTION],
+[AC_REQUIRE([LT_AC_PROG_SED])dnl
+AC_CACHE_CHECK([$1], [$2],
+  [$2=no
+   save_LDFLAGS="$LDFLAGS"
+   LDFLAGS="$LDFLAGS $3"
+   echo "$lt_simple_link_test_code" > conftest.$ac_ext
+   if (eval $ac_link 2>conftest.err) && test -s conftest$ac_exeext; then
+     # The linker can only warn and ignore the option if not recognized
+     # So say no if there are warnings
+     if test -s conftest.err; then
+       # Append any errors to the config.log.
+       cat conftest.err 1>&AS_MESSAGE_LOG_FD
+       $echo "X$_lt_linker_boilerplate" | $Xsed -e '/^$/d' > conftest.exp
+       $SED '/^$/d; /^ *+/d' conftest.err >conftest.er2
+       if diff conftest.exp conftest.er2 >/dev/null; then
+         $2=yes
+       fi
+     else
+       $2=yes
+     fi
+   fi
+   $rm -r conftest*
+   LDFLAGS="$save_LDFLAGS"
+])
+
+if test x"[$]$2" = xyes; then
+    ifelse([$4], , :, [$4])
+else
+    ifelse([$5], , :, [$5])
+fi
+])# AC_LIBTOOL_LINKER_OPTION
+
+
+# AC_LIBTOOL_SYS_MAX_CMD_LEN
+# --------------------------
+AC_DEFUN([AC_LIBTOOL_SYS_MAX_CMD_LEN],
+[# find the maximum length of command line arguments
+AC_MSG_CHECKING([the maximum length of command line arguments])
+AC_CACHE_VAL([lt_cv_sys_max_cmd_len], [dnl
+  i=0
+  teststring="ABCD"
+
+  case $build_os in
+  msdosdjgpp*)
+    # On DJGPP, this test can blow up pretty badly due to problems in libc
+    # (any single argument exceeding 2000 bytes causes a buffer overrun
+    # during glob expansion).  Even if it were fixed, the result of this
+    # check would be larger than it should be.
+    lt_cv_sys_max_cmd_len=12288;    # 12K is about right
+    ;;
+
+  gnu*)
+    # Under GNU Hurd, this test is not required because there is
+    # no limit to the length of command line arguments.
+    # Libtool will interpret -1 as no limit whatsoever
+    lt_cv_sys_max_cmd_len=-1;
+    ;;
+
+  cygwin* | mingw*)
+    # On Win9x/ME, this test blows up -- it succeeds, but takes
+    # about 5 minutes as the teststring grows exponentially.
+    # Worse, since 9x/ME are not pre-emptively multitasking,
+    # you end up with a "frozen" computer, even though with patience
+    # the test eventually succeeds (with a max line length of 256k).
+    # Instead, let's just punt: use the minimum linelength reported by
+    # all of the supported platforms: 8192 (on NT/2K/XP).
+    lt_cv_sys_max_cmd_len=8192;
+    ;;
+
+  amigaos*)
+    # On AmigaOS with pdksh, this test takes hours, literally.
+    # So we just punt and use a minimum line length of 8192.
+    lt_cv_sys_max_cmd_len=8192;
+    ;;
+
+  netbsd* | freebsd* | openbsd* | darwin* | dragonfly*)
+    # This has been around since 386BSD, at least.  Likely further.
+    if test -x /sbin/sysctl; then
+      lt_cv_sys_max_cmd_len=`/sbin/sysctl -n kern.argmax`
+    elif test -x /usr/sbin/sysctl; then
+      lt_cv_sys_max_cmd_len=`/usr/sbin/sysctl -n kern.argmax`
+    else
+      lt_cv_sys_max_cmd_len=65536	# usable default for all BSDs
+    fi
+    # And add a safety zone
+    lt_cv_sys_max_cmd_len=`expr $lt_cv_sys_max_cmd_len \/ 4`
+    lt_cv_sys_max_cmd_len=`expr $lt_cv_sys_max_cmd_len \* 3`
+    ;;
+
+  interix*)
+    # We know the value 262144 and hardcode it with a safety zone (like BSD)
+    lt_cv_sys_max_cmd_len=196608
+    ;;
+
+  osf*)
+    # Dr. Hans Ekkehard Plesser reports seeing a kernel panic running configure
+    # due to this test when exec_disable_arg_limit is 1 on Tru64. It is not
+    # nice to cause kernel panics so lets avoid the loop below.
+    # First set a reasonable default.
+    lt_cv_sys_max_cmd_len=16384
+    #
+    if test -x /sbin/sysconfig; then
+      case `/sbin/sysconfig -q proc exec_disable_arg_limit` in
+        *1*) lt_cv_sys_max_cmd_len=-1 ;;
+      esac
+    fi
+    ;;
+  sco3.2v5*)
+    lt_cv_sys_max_cmd_len=102400
+    ;;
+  sysv5* | sco5v6* | sysv4.2uw2*)
+    kargmax=`grep ARG_MAX /etc/conf/cf.d/stune 2>/dev/null`
+    if test -n "$kargmax"; then
+      lt_cv_sys_max_cmd_len=`echo $kargmax | sed 's/.*[[ 	]]//'`
+    else
+      lt_cv_sys_max_cmd_len=32768
+    fi
+    ;;
+  *)
+    lt_cv_sys_max_cmd_len=`(getconf ARG_MAX) 2> /dev/null`
+    if test -n "$lt_cv_sys_max_cmd_len"; then
+      lt_cv_sys_max_cmd_len=`expr $lt_cv_sys_max_cmd_len \/ 4`
+      lt_cv_sys_max_cmd_len=`expr $lt_cv_sys_max_cmd_len \* 3`
+    else
+      SHELL=${SHELL-${CONFIG_SHELL-/bin/sh}}
+      while (test "X"`$SHELL [$]0 --fallback-echo "X$teststring" 2>/dev/null` \
+	       = "XX$teststring") >/dev/null 2>&1 &&
+	      new_result=`expr "X$teststring" : ".*" 2>&1` &&
+	      lt_cv_sys_max_cmd_len=$new_result &&
+	      test $i != 17 # 1/2 MB should be enough
+      do
+        i=`expr $i + 1`
+        teststring=$teststring$teststring
+      done
+      teststring=
+      # Add a significant safety factor because C++ compilers can tack on massive
+      # amounts of additional arguments before passing them to the linker.
+      # It appears as though 1/2 is a usable value.
+      lt_cv_sys_max_cmd_len=`expr $lt_cv_sys_max_cmd_len \/ 2`
+    fi
+    ;;
+  esac
+])
+if test -n $lt_cv_sys_max_cmd_len ; then
+  AC_MSG_RESULT($lt_cv_sys_max_cmd_len)
+else
+  AC_MSG_RESULT(none)
+fi
+])# AC_LIBTOOL_SYS_MAX_CMD_LEN
+
+
+# _LT_AC_CHECK_DLFCN
+# ------------------
+AC_DEFUN([_LT_AC_CHECK_DLFCN],
+[AC_CHECK_HEADERS(dlfcn.h)dnl
+])# _LT_AC_CHECK_DLFCN
+
+
+# _LT_AC_TRY_DLOPEN_SELF (ACTION-IF-TRUE, ACTION-IF-TRUE-W-USCORE,
+#                           ACTION-IF-FALSE, ACTION-IF-CROSS-COMPILING)
+# ---------------------------------------------------------------------
+AC_DEFUN([_LT_AC_TRY_DLOPEN_SELF],
+[AC_REQUIRE([_LT_AC_CHECK_DLFCN])dnl
+if test "$cross_compiling" = yes; then :
+  [$4]
+else
+  lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
+  lt_status=$lt_dlunknown
+  cat > conftest.$ac_ext <<EOF
+[#line __oline__ "configure"
+#include "confdefs.h"
+
+#if HAVE_DLFCN_H
+#include <dlfcn.h>
+#endif
+
+#include <stdio.h>
+
+#ifdef RTLD_GLOBAL
+#  define LT_DLGLOBAL		RTLD_GLOBAL
+#else
+#  ifdef DL_GLOBAL
+#    define LT_DLGLOBAL		DL_GLOBAL
+#  else
+#    define LT_DLGLOBAL		0
+#  endif
+#endif
+
+/* We may have to define LT_DLLAZY_OR_NOW in the command line if we
+   find out it does not work in some platform. */
+#ifndef LT_DLLAZY_OR_NOW
+#  ifdef RTLD_LAZY
+#    define LT_DLLAZY_OR_NOW		RTLD_LAZY
+#  else
+#    ifdef DL_LAZY
+#      define LT_DLLAZY_OR_NOW		DL_LAZY
+#    else
+#      ifdef RTLD_NOW
+#        define LT_DLLAZY_OR_NOW	RTLD_NOW
+#      else
+#        ifdef DL_NOW
+#          define LT_DLLAZY_OR_NOW	DL_NOW
+#        else
+#          define LT_DLLAZY_OR_NOW	0
+#        endif
+#      endif
+#    endif
+#  endif
+#endif
+
+#ifdef __cplusplus
+extern "C" void exit (int);
+#endif
+
+void fnord() { int i=42;}
+int main ()
+{
+  void *self = dlopen (0, LT_DLGLOBAL|LT_DLLAZY_OR_NOW);
+  int status = $lt_dlunknown;
+
+  if (self)
+    {
+      if (dlsym (self,"fnord"))       status = $lt_dlno_uscore;
+      else if (dlsym( self,"_fnord")) status = $lt_dlneed_uscore;
+      /* dlclose (self); */
+    }
+  else
+    puts (dlerror ());
+
+    exit (status);
+}]
+EOF
+  if AC_TRY_EVAL(ac_link) && test -s conftest${ac_exeext} 2>/dev/null; then
+    (./conftest; exit; ) >&AS_MESSAGE_LOG_FD 2>/dev/null
+    lt_status=$?
+    case x$lt_status in
+      x$lt_dlno_uscore) $1 ;;
+      x$lt_dlneed_uscore) $2 ;;
+      x$lt_dlunknown|x*) $3 ;;
+    esac
+  else :
+    # compilation failed
+    $3
+  fi
+fi
+rm -fr conftest*
+])# _LT_AC_TRY_DLOPEN_SELF
+
+
+# AC_LIBTOOL_DLOPEN_SELF
+# ----------------------
+AC_DEFUN([AC_LIBTOOL_DLOPEN_SELF],
+[AC_REQUIRE([_LT_AC_CHECK_DLFCN])dnl
+if test "x$enable_dlopen" != xyes; then
+  enable_dlopen=unknown
+  enable_dlopen_self=unknown
+  enable_dlopen_self_static=unknown
+else
+  lt_cv_dlopen=no
+  lt_cv_dlopen_libs=
+
+  case $host_os in
+  beos*)
+    lt_cv_dlopen="load_add_on"
+    lt_cv_dlopen_libs=
+    lt_cv_dlopen_self=yes
+    ;;
+
+  mingw* | pw32*)
+    lt_cv_dlopen="LoadLibrary"
+    lt_cv_dlopen_libs=
+   ;;
+
+  cygwin*)
+    lt_cv_dlopen="dlopen"
+    lt_cv_dlopen_libs=
+   ;;
+
+  darwin*)
+  # if libdl is installed we need to link against it
+    AC_CHECK_LIB([dl], [dlopen],
+		[lt_cv_dlopen="dlopen" lt_cv_dlopen_libs="-ldl"],[
+    lt_cv_dlopen="dyld"
+    lt_cv_dlopen_libs=
+    lt_cv_dlopen_self=yes
+    ])
+   ;;
+
+  *)
+    AC_CHECK_FUNC([shl_load],
+	  [lt_cv_dlopen="shl_load"],
+      [AC_CHECK_LIB([dld], [shl_load],
+	    [lt_cv_dlopen="shl_load" lt_cv_dlopen_libs="-ldld"],
+	[AC_CHECK_FUNC([dlopen],
+	      [lt_cv_dlopen="dlopen"],
+	  [AC_CHECK_LIB([dl], [dlopen],
+		[lt_cv_dlopen="dlopen" lt_cv_dlopen_libs="-ldl"],
+	    [AC_CHECK_LIB([svld], [dlopen],
+		  [lt_cv_dlopen="dlopen" lt_cv_dlopen_libs="-lsvld"],
+	      [AC_CHECK_LIB([dld], [dld_link],
+		    [lt_cv_dlopen="dld_link" lt_cv_dlopen_libs="-ldld"])
+	      ])
+	    ])
+	  ])
+	])
+      ])
+    ;;
+  esac
+
+  if test "x$lt_cv_dlopen" != xno; then
+    enable_dlopen=yes
+  else
+    enable_dlopen=no
+  fi
+
+  case $lt_cv_dlopen in
+  dlopen)
+    save_CPPFLAGS="$CPPFLAGS"
+    test "x$ac_cv_header_dlfcn_h" = xyes && CPPFLAGS="$CPPFLAGS -DHAVE_DLFCN_H"
+
+    save_LDFLAGS="$LDFLAGS"
+    wl=$lt_prog_compiler_wl eval LDFLAGS=\"\$LDFLAGS $export_dynamic_flag_spec\"
+
+    save_LIBS="$LIBS"
+    LIBS="$lt_cv_dlopen_libs $LIBS"
+
+    AC_CACHE_CHECK([whether a program can dlopen itself],
+	  lt_cv_dlopen_self, [dnl
+	  _LT_AC_TRY_DLOPEN_SELF(
+	    lt_cv_dlopen_self=yes, lt_cv_dlopen_self=yes,
+	    lt_cv_dlopen_self=no, lt_cv_dlopen_self=cross)
+    ])
+
+    if test "x$lt_cv_dlopen_self" = xyes; then
+      wl=$lt_prog_compiler_wl eval LDFLAGS=\"\$LDFLAGS $lt_prog_compiler_static\"
+      AC_CACHE_CHECK([whether a statically linked program can dlopen itself],
+    	  lt_cv_dlopen_self_static, [dnl
+	  _LT_AC_TRY_DLOPEN_SELF(
+	    lt_cv_dlopen_self_static=yes, lt_cv_dlopen_self_static=yes,
+	    lt_cv_dlopen_self_static=no,  lt_cv_dlopen_self_static=cross)
+      ])
+    fi
+
+    CPPFLAGS="$save_CPPFLAGS"
+    LDFLAGS="$save_LDFLAGS"
+    LIBS="$save_LIBS"
+    ;;
+  esac
+
+  case $lt_cv_dlopen_self in
+  yes|no) enable_dlopen_self=$lt_cv_dlopen_self ;;
+  *) enable_dlopen_self=unknown ;;
+  esac
+
+  case $lt_cv_dlopen_self_static in
+  yes|no) enable_dlopen_self_static=$lt_cv_dlopen_self_static ;;
+  *) enable_dlopen_self_static=unknown ;;
+  esac
+fi
+])# AC_LIBTOOL_DLOPEN_SELF
+
+
+# AC_LIBTOOL_PROG_CC_C_O([TAGNAME])
+# ---------------------------------
+# Check to see if options -c and -o are simultaneously supported by compiler
+AC_DEFUN([AC_LIBTOOL_PROG_CC_C_O],
+[AC_REQUIRE([LT_AC_PROG_SED])dnl
+AC_REQUIRE([_LT_AC_SYS_COMPILER])dnl
+AC_CACHE_CHECK([if $compiler supports -c -o file.$ac_objext],
+  [_LT_AC_TAGVAR(lt_cv_prog_compiler_c_o, $1)],
+  [_LT_AC_TAGVAR(lt_cv_prog_compiler_c_o, $1)=no
+   $rm -r conftest 2>/dev/null
+   mkdir conftest
+   cd conftest
+   mkdir out
+   echo "$lt_simple_compile_test_code" > conftest.$ac_ext
+
+   lt_compiler_flag="-o out/conftest2.$ac_objext"
+   # Insert the option either (1) after the last *FLAGS variable, or
+   # (2) before a word containing "conftest.", or (3) at the end.
+   # Note that $ac_compile itself does not contain backslashes and begins
+   # with a dollar sign (not a hyphen), so the echo should work correctly.
+   lt_compile=`echo "$ac_compile" | $SED \
+   -e 's:.*FLAGS}\{0,1\} :&$lt_compiler_flag :; t' \
+   -e 's: [[^ ]]*conftest\.: $lt_compiler_flag&:; t' \
+   -e 's:$: $lt_compiler_flag:'`
+   (eval echo "\"\$as_me:__oline__: $lt_compile\"" >&AS_MESSAGE_LOG_FD)
+   (eval "$lt_compile" 2>out/conftest.err)
+   ac_status=$?
+   cat out/conftest.err >&AS_MESSAGE_LOG_FD
+   echo "$as_me:__oline__: \$? = $ac_status" >&AS_MESSAGE_LOG_FD
+   if (exit $ac_status) && test -s out/conftest2.$ac_objext
+   then
+     # The compiler can only warn and ignore the option if not recognized
+     # So say no if there are warnings
+     $echo "X$_lt_compiler_boilerplate" | $Xsed -e '/^$/d' > out/conftest.exp
+     $SED '/^$/d; /^ *+/d' out/conftest.err >out/conftest.er2
+     if test ! -s out/conftest.er2 || diff out/conftest.exp out/conftest.er2 >/dev/null; then
+       _LT_AC_TAGVAR(lt_cv_prog_compiler_c_o, $1)=yes
+     fi
+   fi
+   chmod u+w . 2>&AS_MESSAGE_LOG_FD
+   $rm conftest*
+   # SGI C++ compiler will create directory out/ii_files/ for
+   # template instantiation
+   test -d out/ii_files && $rm out/ii_files/* && rmdir out/ii_files
+   $rm out/* && rmdir out
+   cd ..
+   rmdir conftest
+   $rm conftest*
+])
+])# AC_LIBTOOL_PROG_CC_C_O
+
+
+# AC_LIBTOOL_SYS_HARD_LINK_LOCKS([TAGNAME])
+# -----------------------------------------
+# Check to see if we can do hard links to lock some files if needed
+AC_DEFUN([AC_LIBTOOL_SYS_HARD_LINK_LOCKS],
+[AC_REQUIRE([_LT_AC_LOCK])dnl
+
+hard_links="nottested"
+if test "$_LT_AC_TAGVAR(lt_cv_prog_compiler_c_o, $1)" = no && test "$need_locks" != no; then
+  # do not overwrite the value of need_locks provided by the user
+  AC_MSG_CHECKING([if we can lock with hard links])
+  hard_links=yes
+  $rm conftest*
+  ln conftest.a conftest.b 2>/dev/null && hard_links=no
+  touch conftest.a
+  ln conftest.a conftest.b 2>&5 || hard_links=no
+  ln conftest.a conftest.b 2>/dev/null && hard_links=no
+  AC_MSG_RESULT([$hard_links])
+  if test "$hard_links" = no; then
+    AC_MSG_WARN([`$CC' does not support `-c -o', so `make -j' may be unsafe])
+    need_locks=warn
+  fi
+else
+  need_locks=no
+fi
+])# AC_LIBTOOL_SYS_HARD_LINK_LOCKS
+
+
+# AC_LIBTOOL_OBJDIR
+# -----------------
+AC_DEFUN([AC_LIBTOOL_OBJDIR],
+[AC_CACHE_CHECK([for objdir], [lt_cv_objdir],
+[rm -f .libs 2>/dev/null
+mkdir .libs 2>/dev/null
+if test -d .libs; then
+  lt_cv_objdir=.libs
+else
+  # MS-DOS does not allow filenames that begin with a dot.
+  lt_cv_objdir=_libs
+fi
+rmdir .libs 2>/dev/null])
+objdir=$lt_cv_objdir
+])# AC_LIBTOOL_OBJDIR
+
+
+# AC_LIBTOOL_PROG_LD_HARDCODE_LIBPATH([TAGNAME])
+# ----------------------------------------------
+# Check hardcoding attributes.
+AC_DEFUN([AC_LIBTOOL_PROG_LD_HARDCODE_LIBPATH],
+[AC_MSG_CHECKING([how to hardcode library paths into programs])
+_LT_AC_TAGVAR(hardcode_action, $1)=
+if test -n "$_LT_AC_TAGVAR(hardcode_libdir_flag_spec, $1)" || \
+   test -n "$_LT_AC_TAGVAR(runpath_var, $1)" || \
+   test "X$_LT_AC_TAGVAR(hardcode_automatic, $1)" = "Xyes" ; then
+
+  # We can hardcode non-existant directories.
+  if test "$_LT_AC_TAGVAR(hardcode_direct, $1)" != no &&
+     # If the only mechanism to avoid hardcoding is shlibpath_var, we
+     # have to relink, otherwise we might link with an installed library
+     # when we should be linking with a yet-to-be-installed one
+     ## test "$_LT_AC_TAGVAR(hardcode_shlibpath_var, $1)" != no &&
+     test "$_LT_AC_TAGVAR(hardcode_minus_L, $1)" != no; then
+    # Linking always hardcodes the temporary library directory.
+    _LT_AC_TAGVAR(hardcode_action, $1)=relink
+  else
+    # We can link without hardcoding, and we can hardcode nonexisting dirs.
+    _LT_AC_TAGVAR(hardcode_action, $1)=immediate
+  fi
+else
+  # We cannot hardcode anything, or else we can only hardcode existing
+  # directories.
+  _LT_AC_TAGVAR(hardcode_action, $1)=unsupported
+fi
+AC_MSG_RESULT([$_LT_AC_TAGVAR(hardcode_action, $1)])
+
+if test "$_LT_AC_TAGVAR(hardcode_action, $1)" = relink; then
+  # Fast installation is not supported
+  enable_fast_install=no
+elif test "$shlibpath_overrides_runpath" = yes ||
+     test "$enable_shared" = no; then
+  # Fast installation is not necessary
+  enable_fast_install=needless
+fi
+])# AC_LIBTOOL_PROG_LD_HARDCODE_LIBPATH
+
+
+# AC_LIBTOOL_SYS_LIB_STRIP
+# ------------------------
+AC_DEFUN([AC_LIBTOOL_SYS_LIB_STRIP],
+[striplib=
+old_striplib=
+AC_MSG_CHECKING([whether stripping libraries is possible])
+if test -n "$STRIP" && $STRIP -V 2>&1 | grep "GNU strip" >/dev/null; then
+  test -z "$old_striplib" && old_striplib="$STRIP --strip-debug"
+  test -z "$striplib" && striplib="$STRIP --strip-unneeded"
+  AC_MSG_RESULT([yes])
+else
+# FIXME - insert some real tests, host_os isn't really good enough
+  case $host_os in
+   darwin*)
+       if test -n "$STRIP" ; then
+         striplib="$STRIP -x"
+         old_striplib="$STRIP -S"
+         AC_MSG_RESULT([yes])
+       else
+  AC_MSG_RESULT([no])
+fi
+       ;;
+   *)
+  AC_MSG_RESULT([no])
+    ;;
+  esac
+fi
+])# AC_LIBTOOL_SYS_LIB_STRIP
+
+
+# AC_LIBTOOL_SYS_DYNAMIC_LINKER
+# -----------------------------
+# PORTME Fill in your ld.so characteristics
+AC_DEFUN([AC_LIBTOOL_SYS_DYNAMIC_LINKER],
+[AC_REQUIRE([LT_AC_PROG_SED])dnl
+AC_MSG_CHECKING([dynamic linker characteristics])
+library_names_spec=
+libname_spec='lib$name'
+soname_spec=
+shrext_cmds=".so"
+postinstall_cmds=
+postuninstall_cmds=
+finish_cmds=
+finish_eval=
+shlibpath_var=
+shlibpath_overrides_runpath=unknown
+version_type=none
+dynamic_linker="$host_os ld.so"
+sys_lib_dlsearch_path_spec="/lib /usr/lib"
+m4_if($1,[],[
+if test "$GCC" = yes; then
+  case $host_os in
+    darwin*) lt_awk_arg="/^libraries:/,/LR/" ;;
+    *) lt_awk_arg="/^libraries:/" ;;
+  esac
+  lt_search_path_spec=`$CC -print-search-dirs | awk $lt_awk_arg | $SED -e "s/^libraries://" -e "s,=/,/,g"`
+  if echo "$lt_search_path_spec" | grep ';' >/dev/null ; then
+    # if the path contains ";" then we assume it to be the separator
+    # otherwise default to the standard path separator (i.e. ":") - it is
+    # assumed that no part of a normal pathname contains ";" but that should
+    # okay in the real world where ";" in dirpaths is itself problematic.
+    lt_search_path_spec=`echo "$lt_search_path_spec" | $SED -e 's/;/ /g'`
+  else
+    lt_search_path_spec=`echo "$lt_search_path_spec" | $SED  -e "s/$PATH_SEPARATOR/ /g"`
+  fi
+  # Ok, now we have the path, separated by spaces, we can step through it
+  # and add multilib dir if necessary.
+  lt_tmp_lt_search_path_spec=
+  lt_multi_os_dir=`$CC $CPPFLAGS $CFLAGS $LDFLAGS -print-multi-os-directory 2>/dev/null`
+  for lt_sys_path in $lt_search_path_spec; do
+    if test -d "$lt_sys_path/$lt_multi_os_dir"; then
+      lt_tmp_lt_search_path_spec="$lt_tmp_lt_search_path_spec $lt_sys_path/$lt_multi_os_dir"
+    else
+      test -d "$lt_sys_path" && \
+	lt_tmp_lt_search_path_spec="$lt_tmp_lt_search_path_spec $lt_sys_path"
+    fi
+  done
+  lt_search_path_spec=`echo $lt_tmp_lt_search_path_spec | awk '
+BEGIN {RS=" "; FS="/|\n";} {
+  lt_foo="";
+  lt_count=0;
+  for (lt_i = NF; lt_i > 0; lt_i--) {
+    if ($lt_i != "" && $lt_i != ".") {
+      if ($lt_i == "..") {
+        lt_count++;
+      } else {
+        if (lt_count == 0) {
+          lt_foo="/" $lt_i lt_foo;
+        } else {
+          lt_count--;
+        }
+      }
+    }
+  }
+  if (lt_foo != "") { lt_freq[[lt_foo]]++; }
+  if (lt_freq[[lt_foo]] == 1) { print lt_foo; }
+}'`
+  sys_lib_search_path_spec=`echo $lt_search_path_spec`
+else
+  sys_lib_search_path_spec="/lib /usr/lib /usr/local/lib"
+fi])
+need_lib_prefix=unknown
+hardcode_into_libs=no
+
+# when you set need_version to no, make sure it does not cause -set_version
+# flags to be left without arguments
+need_version=unknown
+
+case $host_os in
+aix3*)
+  version_type=linux
+  library_names_spec='${libname}${release}${shared_ext}$versuffix $libname.a'
+  shlibpath_var=LIBPATH
+
+  # AIX 3 has no versioning support, so we append a major version to the name.
+  soname_spec='${libname}${release}${shared_ext}$major'
+  ;;
+
+aix[[4-9]]*)
+  version_type=linux
+  need_lib_prefix=no
+  need_version=no
+  hardcode_into_libs=yes
+  if test "$host_cpu" = ia64; then
+    # AIX 5 supports IA64
+    library_names_spec='${libname}${release}${shared_ext}$major ${libname}${release}${shared_ext}$versuffix $libname${shared_ext}'
+    shlibpath_var=LD_LIBRARY_PATH
+  else
+    # With GCC up to 2.95.x, collect2 would create an import file
+    # for dependence libraries.  The import file would start with
+    # the line `#! .'.  This would cause the generated library to
+    # depend on `.', always an invalid library.  This was fixed in
+    # development snapshots of GCC prior to 3.0.
+    case $host_os in
+      aix4 | aix4.[[01]] | aix4.[[01]].*)
+      if { echo '#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 97)'
+	   echo ' yes '
+	   echo '#endif'; } | ${CC} -E - | grep yes > /dev/null; then
+	:
+      else
+	can_build_shared=no
+      fi
+      ;;
+    esac
+    # AIX (on Power*) has no versioning support, so currently we can not hardcode correct
+    # soname into executable. Probably we can add versioning support to
+    # collect2, so additional links can be useful in future.
+    if test "$aix_use_runtimelinking" = yes; then
+      # If using run time linking (on AIX 4.2 or later) use lib<name>.so
+      # instead of lib<name>.a to let people know that these are not
+      # typical AIX shared libraries.
+      library_names_spec='${libname}${release}${shared_ext}$versuffix ${libname}${release}${shared_ext}$major $libname${shared_ext}'
+    else
+      # We preserve .a as extension for shared libraries through AIX4.2
+      # and later when we are not doing run time linking.
+      library_names_spec='${libname}${release}.a $libname.a'
+      soname_spec='${libname}${release}${shared_ext}$major'
+    fi
+    shlibpath_var=LIBPATH
+  fi
+  ;;
+
+amigaos*)
+  library_names_spec='$libname.ixlibrary $libname.a'
+  # Create ${libname}_ixlibrary.a entries in /sys/libs.
+  finish_eval='for lib in `ls $libdir/*.ixlibrary 2>/dev/null`; do libname=`$echo "X$lib" | $Xsed -e '\''s%^.*/\([[^/]]*\)\.ixlibrary$%\1%'\''`; test $rm /sys/libs/${libname}_ixlibrary.a; $show "cd /sys/libs && $LN_S $lib ${libname}_ixlibrary.a"; cd /sys/libs && $LN_S $lib ${libname}_ixlibrary.a || exit 1; done'
+  ;;
+
+beos*)
+  library_names_spec='${libname}${shared_ext}'
+  dynamic_linker="$host_os ld.so"
+  shlibpath_var=LIBRARY_PATH
+  ;;
+
+bsdi[[45]]*)
+  version_type=linux
+  need_version=no
+  library_names_spec='${libname}${release}${shared_ext}$versuffix ${libname}${release}${shared_ext}$major $libname${shared_ext}'
+  soname_spec='${libname}${release}${shared_ext}$major'
+  finish_cmds='PATH="\$PATH:/sbin" ldconfig $libdir'
+  shlibpath_var=LD_LIBRARY_PATH
+  sys_lib_search_path_spec="/shlib /usr/lib /usr/X11/lib /usr/contrib/lib /lib /usr/local/lib"
+  sys_lib_dlsearch_path_spec="/shlib /usr/lib /usr/local/lib"
+  # the default ld.so.conf also contains /usr/contrib/lib and
+  # /usr/X11R6/lib (/usr/X11 is a link to /usr/X11R6), but let us allow
+  # libtool to hard-code these into programs
+  ;;
+
+cygwin* | mingw* | pw32*)
+  version_type=windows
+  shrext_cmds=".dll"
+  need_version=no
+  need_lib_prefix=no
+
+  case $GCC,$host_os in
+  yes,cygwin* | yes,mingw* | yes,pw32*)
+    library_names_spec='$libname.dll.a'
+    # DLL is installed to $(libdir)/../bin by postinstall_cmds
+    postinstall_cmds='base_file=`basename \${file}`~
+      dlpath=`$SHELL 2>&1 -c '\''. $dir/'\''\${base_file}'\''i;echo \$dlname'\''`~
+      dldir=$destdir/`dirname \$dlpath`~
+      test -d \$dldir || mkdir -p \$dldir~
+      $install_prog $dir/$dlname \$dldir/$dlname~
+      chmod a+x \$dldir/$dlname'
+    postuninstall_cmds='dldll=`$SHELL 2>&1 -c '\''. $file; echo \$dlname'\''`~
+      dlpath=$dir/\$dldll~
+       $rm \$dlpath'
+    shlibpath_overrides_runpath=yes
+
+    case $host_os in
+    cygwin*)
+      # Cygwin DLLs use 'cyg' prefix rather than 'lib'
+      soname_spec='`echo ${libname} | sed -e 's/^lib/cyg/'``echo ${release} | $SED -e 's/[[.]]/-/g'`${versuffix}${shared_ext}'
+      sys_lib_search_path_spec="/usr/lib /lib/w32api /lib /usr/local/lib"
+      ;;
+    mingw*)
+      # MinGW DLLs use traditional 'lib' prefix
+      soname_spec='${libname}`echo ${release} | $SED -e 's/[[.]]/-/g'`${versuffix}${shared_ext}'
+      sys_lib_search_path_spec=`$CC -print-search-dirs | grep "^libraries:" | $SED -e "s/^libraries://" -e "s,=/,/,g"`
+      if echo "$sys_lib_search_path_spec" | [grep ';[c-zC-Z]:/' >/dev/null]; then
+        # It is most probably a Windows format PATH printed by
+        # mingw gcc, but we are running on Cygwin. Gcc prints its search
+        # path with ; separators, and with drive letters. We can handle the
+        # drive letters (cygwin fileutils understands them), so leave them,
+        # especially as we might pass files found there to a mingw objdump,
+        # which wouldn't understand a cygwinified path. Ahh.
+        sys_lib_search_path_spec=`echo "$sys_lib_search_path_spec" | $SED -e 's/;/ /g'`
+      else
+        sys_lib_search_path_spec=`echo "$sys_lib_search_path_spec" | $SED  -e "s/$PATH_SEPARATOR/ /g"`
+      fi
+      ;;
+    pw32*)
+      # pw32 DLLs use 'pw' prefix rather than 'lib'
+      library_names_spec='`echo ${libname} | sed -e 's/^lib/pw/'``echo ${release} | $SED -e 's/[[.]]/-/g'`${versuffix}${shared_ext}'
+      ;;
+    esac
+    ;;
+
+  *)
+    library_names_spec='${libname}`echo ${release} | $SED -e 's/[[.]]/-/g'`${versuffix}${shared_ext} $libname.lib'
+    ;;
+  esac
+  dynamic_linker='Win32 ld.exe'
+  # FIXME: first we should search . and the directory the executable is in
+  shlibpath_var=PATH
+  ;;
+
+darwin* | rhapsody*)
+  dynamic_linker="$host_os dyld"
+  version_type=darwin
+  need_lib_prefix=no
+  need_version=no
+  library_names_spec='${libname}${release}${versuffix}$shared_ext ${libname}${release}${major}$shared_ext ${libname}$shared_ext'
+  soname_spec='${libname}${release}${major}$shared_ext'
+  shlibpath_overrides_runpath=yes
+  shlibpath_var=DYLD_LIBRARY_PATH
+  shrext_cmds='`test .$module = .yes && echo .so || echo .dylib`'
+  m4_if([$1], [],[
+  sys_lib_search_path_spec="$sys_lib_search_path_spec /usr/local/lib"]) 
+  sys_lib_dlsearch_path_spec='/usr/local/lib /lib /usr/lib'
+  ;;
+
+dgux*)
+  version_type=linux
+  need_lib_prefix=no
+  need_version=no
+  library_names_spec='${libname}${release}${shared_ext}$versuffix ${libname}${release}${shared_ext}$major $libname$shared_ext'
+  soname_spec='${libname}${release}${shared_ext}$major'
+  shlibpath_var=LD_LIBRARY_PATH
+  ;;
+
+freebsd1*)
+  dynamic_linker=no
+  ;;
+
+freebsd* | dragonfly*)
+  # DragonFly does not have aout.  When/if they implement a new
+  # versioning mechanism, adjust this.
+  if test -x /usr/bin/objformat; then
+    objformat=`/usr/bin/objformat`
+  else
+    case $host_os in
+    freebsd[[123]]*) objformat=aout ;;
+    *) objformat=elf ;;
+    esac
+  fi
+  version_type=freebsd-$objformat
+  case $version_type in
+    freebsd-elf*)
+      library_names_spec='${libname}${release}${shared_ext}$versuffix ${libname}${release}${shared_ext} $libname${shared_ext}'
+      need_version=no
+      need_lib_prefix=no
+      ;;
+    freebsd-*)
+      library_names_spec='${libname}${release}${shared_ext}$versuffix $libname${shared_ext}$versuffix'
+      need_version=yes
+      ;;
+  esac
+  shlibpath_var=LD_LIBRARY_PATH
+  case $host_os in
+  freebsd2*)
+    shlibpath_overrides_runpath=yes
+    ;;
+  freebsd3.[[01]]* | freebsdelf3.[[01]]*)
+    shlibpath_overrides_runpath=yes
+    hardcode_into_libs=yes
+    ;;
+  freebsd3.[[2-9]]* | freebsdelf3.[[2-9]]* | \
+  freebsd4.[[0-5]] | freebsdelf4.[[0-5]] | freebsd4.1.1 | freebsdelf4.1.1)
+    shlibpath_overrides_runpath=no
+    hardcode_into_libs=yes
+    ;;
+  *) # from 4.6 on, and DragonFly
+    shlibpath_overrides_runpath=yes
+    hardcode_into_libs=yes
+    ;;
+  esac
+  ;;
+
+gnu*)
+  version_type=linux
+  need_lib_prefix=no
+  need_version=no
+  library_names_spec='${libname}${release}${shared_ext}$versuffix ${libname}${release}${shared_ext}${major} ${libname}${shared_ext}'
+  soname_spec='${libname}${release}${shared_ext}$major'
+  shlibpath_var=LD_LIBRARY_PATH
+  hardcode_into_libs=yes
+  ;;
+
+hpux9* | hpux10* | hpux11*)
+  # Give a soname corresponding to the major version so that dld.sl refuses to
+  # link against other versions.
+  version_type=sunos
+  need_lib_prefix=no
+  need_version=no
+  case $host_cpu in
+  ia64*)
+    shrext_cmds='.so'
+    hardcode_into_libs=yes
+    dynamic_linker="$host_os dld.so"
+    shlibpath_var=LD_LIBRARY_PATH
+    shlibpath_overrides_runpath=yes # Unless +noenvvar is specified.
+    library_names_spec='${libname}${release}${shared_ext}$versuffix ${libname}${release}${shared_ext}$major $libname${shared_ext}'
+    soname_spec='${libname}${release}${shared_ext}$major'
+    if test "X$HPUX_IA64_MODE" = X32; then
+      sys_lib_search_path_spec="/usr/lib/hpux32 /usr/local/lib/hpux32 /usr/local/lib"
+    else
+      sys_lib_search_path_spec="/usr/lib/hpux64 /usr/local/lib/hpux64"
+    fi
+    sys_lib_dlsearch_path_spec=$sys_lib_search_path_spec
+    ;;
+   hppa*64*)
+     shrext_cmds='.sl'
+     hardcode_into_libs=yes
+     dynamic_linker="$host_os dld.sl"
+     shlibpath_var=LD_LIBRARY_PATH # How should we handle SHLIB_PATH
+     shlibpath_overrides_runpath=yes # Unless +noenvvar is specified.
+     library_names_spec='${libname}${release}${shared_ext}$versuffix ${libname}${release}${shared_ext}$major $libname${shared_ext}'
+     soname_spec='${libname}${release}${shared_ext}$major'
+     sys_lib_search_path_spec="/usr/lib/pa20_64 /usr/ccs/lib/pa20_64"
+     sys_lib_dlsearch_path_spec=$sys_lib_search_path_spec
+     ;;
+   *)
+    shrext_cmds='.sl'
+    dynamic_linker="$host_os dld.sl"
+    shlibpath_var=SHLIB_PATH
+    shlibpath_overrides_runpath=no # +s is required to enable SHLIB_PATH
+    library_names_spec='${libname}${release}${shared_ext}$versuffix ${libname}${release}${shared_ext}$major $libname${shared_ext}'
+    soname_spec='${libname}${release}${shared_ext}$major'
+    ;;
+  esac
+  # HP-UX runs *really* slowly unless shared libraries are mode 555.
+  postinstall_cmds='chmod 555 $lib'
+  ;;
+
+interix[[3-9]]*)
+  version_type=linux
+  need_lib_prefix=no
+  need_version=no
+  library_names_spec='${libname}${release}${shared_ext}$versuffix ${libname}${release}${shared_ext}$major ${libname}${shared_ext}'
+  soname_spec='${libname}${release}${shared_ext}$major'
+  dynamic_linker='Interix 3.x ld.so.1 (PE, like ELF)'
+  shlibpath_var=LD_LIBRARY_PATH
+  shlibpath_overrides_runpath=no
+  hardcode_into_libs=yes
+  ;;
+
+irix5* | irix6* | nonstopux*)
+  case $host_os in
+    nonstopux*) version_type=nonstopux ;;
+    *)
+	if test "$lt_cv_prog_gnu_ld" = yes; then
+		version_type=linux
+	else
+		version_type=irix
+	fi ;;
+  esac
+  need_lib_prefix=no
+  need_version=no
+  soname_spec='${libname}${release}${shared_ext}$major'
+  library_names_spec='${libname}${release}${shared_ext}$versuffix ${libname}${release}${shared_ext}$major ${libname}${release}${shared_ext} $libname${shared_ext}'
+  case $host_os in
+  irix5* | nonstopux*)
+    libsuff= shlibsuff=
+    ;;
+  *)
+    case $LD in # libtool.m4 will add one of these switches to LD
+    *-32|*"-32 "|*-melf32bsmip|*"-melf32bsmip ")
+      libsuff= shlibsuff= libmagic=32-bit;;
+    *-n32|*"-n32 "|*-melf32bmipn32|*"-melf32bmipn32 ")
+      libsuff=32 shlibsuff=N32 libmagic=N32;;
+    *-64|*"-64 "|*-melf64bmip|*"-melf64bmip ")
+      libsuff=64 shlibsuff=64 libmagic=64-bit;;
+    *) libsuff= shlibsuff= libmagic=never-match;;
+    esac
+    ;;
+  esac
+  shlibpath_var=LD_LIBRARY${shlibsuff}_PATH
+  shlibpath_overrides_runpath=no
+  sys_lib_search_path_spec="/usr/lib${libsuff} /lib${libsuff} /usr/local/lib${libsuff}"
+  sys_lib_dlsearch_path_spec="/usr/lib${libsuff} /lib${libsuff}"
+  hardcode_into_libs=yes
+  ;;
+
+# No shared lib support for Linux oldld, aout, or coff.
+linux*oldld* | linux*aout* | linux*coff*)
+  dynamic_linker=no
+  ;;
+
+# This must be Linux ELF.
+linux* | k*bsd*-gnu)
+  version_type=linux
+  need_lib_prefix=no
+  need_version=no
+  library_names_spec='${libname}${release}${shared_ext}$versuffix ${libname}${release}${shared_ext}$major $libname${shared_ext}'
+  soname_spec='${libname}${release}${shared_ext}$major'
+  finish_cmds='PATH="\$PATH:/sbin" ldconfig -n $libdir'
+  shlibpath_var=LD_LIBRARY_PATH
+  shlibpath_overrides_runpath=no
+  # This implies no fast_install, which is unacceptable.
+  # Some rework will be needed to allow for fast_install
+  # before this can be enabled.
+  hardcode_into_libs=yes
+  sys_lib_search_path_spec="/usr/lib${libsuff} /lib${libsuff} /usr/local/lib${libsuff}"
+  sys_lib_dlsearch_path_spec="/usr/lib${libsuff} /lib${libsuff}"
+
+  # Append ld.so.conf contents to the search path
+  if test -f /etc/ld.so.conf; then
+    lt_ld_extra=`awk '/^include / { system(sprintf("cd /etc; cat %s 2>/dev/null", \[$]2)); skip = 1; } { if (!skip) print \[$]0; skip = 0; }' < /etc/ld.so.conf | $SED -e 's/#.*//;/^[ 	]*hwcap[ 	]/d;s/[:,	]/ /g;s/=[^=]*$//;s/=[^= ]* / /g;/^$/d' | tr '\n' ' '`
+    sys_lib_dlsearch_path_spec="$sys_lib_dlsearch_path_spec $lt_ld_extra"
+  fi
+
+  # We used to test for /lib/ld.so.1 and disable shared libraries on
+  # powerpc, because MkLinux only supported shared libraries with the
+  # GNU dynamic linker.  Since this was broken with cross compilers,
+  # most powerpc-linux boxes support dynamic linking these days and
+  # people can always --disable-shared, the test was removed, and we
+  # assume the GNU/Linux dynamic linker is in use.
+  dynamic_linker='GNU/Linux ld.so'
+  ;;
+
+netbsd*)
+  version_type=sunos
+  need_lib_prefix=no
+  need_version=no
+  if echo __ELF__ | $CC -E - | grep __ELF__ >/dev/null; then
+    library_names_spec='${libname}${release}${shared_ext}$versuffix ${libname}${shared_ext}$versuffix'
+    finish_cmds='PATH="\$PATH:/sbin" ldconfig -m $libdir'
+    dynamic_linker='NetBSD (a.out) ld.so'
+  else
+    library_names_spec='${libname}${release}${shared_ext}$versuffix ${libname}${release}${shared_ext}$major ${libname}${shared_ext}'
+    soname_spec='${libname}${release}${shared_ext}$major'
+    dynamic_linker='NetBSD ld.elf_so'
+  fi
+  shlibpath_var=LD_LIBRARY_PATH
+  shlibpath_overrides_runpath=yes
+  hardcode_into_libs=yes
+  ;;
+
+newsos6)
+  version_type=linux
+  library_names_spec='${libname}${release}${shared_ext}$versuffix ${libname}${release}${shared_ext}$major $libname${shared_ext}'
+  shlibpath_var=LD_LIBRARY_PATH
+  shlibpath_overrides_runpath=yes
+  ;;
+
+nto-qnx*)
+  version_type=linux
+  need_lib_prefix=no
+  need_version=no
+  library_names_spec='${libname}${release}${shared_ext}$versuffix ${libname}${release}${shared_ext}$major $libname${shared_ext}'
+  soname_spec='${libname}${release}${shared_ext}$major'
+  shlibpath_var=LD_LIBRARY_PATH
+  shlibpath_overrides_runpath=yes
+  ;;
+
+openbsd*)
+  version_type=sunos
+  sys_lib_dlsearch_path_spec="/usr/lib"
+  need_lib_prefix=no
+  # Some older versions of OpenBSD (3.3 at least) *do* need versioned libs.
+  case $host_os in
+    openbsd3.3 | openbsd3.3.*) need_version=yes ;;
+    *)                         need_version=no  ;;
+  esac
+  library_names_spec='${libname}${release}${shared_ext}$versuffix ${libname}${shared_ext}$versuffix'
+  finish_cmds='PATH="\$PATH:/sbin" ldconfig -m $libdir'
+  shlibpath_var=LD_LIBRARY_PATH
+  if test -z "`echo __ELF__ | $CC -E - | grep __ELF__`" || test "$host_os-$host_cpu" = "openbsd2.8-powerpc"; then
+    case $host_os in
+      openbsd2.[[89]] | openbsd2.[[89]].*)
+	shlibpath_overrides_runpath=no
+	;;
+      *)
+	shlibpath_overrides_runpath=yes
+	;;
+      esac
+  else
+    shlibpath_overrides_runpath=yes
+  fi
+  ;;
+
+os2*)
+  libname_spec='$name'
+  shrext_cmds=".dll"
+  need_lib_prefix=no
+  library_names_spec='$libname${shared_ext} $libname.a'
+  dynamic_linker='OS/2 ld.exe'
+  shlibpath_var=LIBPATH
+  ;;
+
+osf3* | osf4* | osf5*)
+  version_type=osf
+  need_lib_prefix=no
+  need_version=no
+  soname_spec='${libname}${release}${shared_ext}$major'
+  library_names_spec='${libname}${release}${shared_ext}$versuffix ${libname}${release}${shared_ext}$major $libname${shared_ext}'
+  shlibpath_var=LD_LIBRARY_PATH
+  sys_lib_search_path_spec="/usr/shlib /usr/ccs/lib /usr/lib/cmplrs/cc /usr/lib /usr/local/lib /var/shlib"
+  sys_lib_dlsearch_path_spec="$sys_lib_search_path_spec"
+  ;;
+
+rdos*)
+  dynamic_linker=no
+  ;;
+
+solaris*)
+  version_type=linux
+  need_lib_prefix=no
+  need_version=no
+  library_names_spec='${libname}${release}${shared_ext}$versuffix ${libname}${release}${shared_ext}$major $libname${shared_ext}'
+  soname_spec='${libname}${release}${shared_ext}$major'
+  shlibpath_var=LD_LIBRARY_PATH
+  shlibpath_overrides_runpath=yes
+  hardcode_into_libs=yes
+  # ldd complains unless libraries are executable
+  postinstall_cmds='chmod +x $lib'
+  ;;
+
+sunos4*)
+  version_type=sunos
+  library_names_spec='${libname}${release}${shared_ext}$versuffix ${libname}${shared_ext}$versuffix'
+  finish_cmds='PATH="\$PATH:/usr/etc" ldconfig $libdir'
+  shlibpath_var=LD_LIBRARY_PATH
+  shlibpath_overrides_runpath=yes
+  if test "$with_gnu_ld" = yes; then
+    need_lib_prefix=no
+  fi
+  need_version=yes
+  ;;
+
+sysv4 | sysv4.3*)
+  version_type=linux
+  library_names_spec='${libname}${release}${shared_ext}$versuffix ${libname}${release}${shared_ext}$major $libname${shared_ext}'
+  soname_spec='${libname}${release}${shared_ext}$major'
+  shlibpath_var=LD_LIBRARY_PATH
+  case $host_vendor in
+    sni)
+      shlibpath_overrides_runpath=no
+      need_lib_prefix=no
+      export_dynamic_flag_spec='${wl}-Blargedynsym'
+      runpath_var=LD_RUN_PATH
+      ;;
+    siemens)
+      need_lib_prefix=no
+      ;;
+    motorola)
+      need_lib_prefix=no
+      need_version=no
+      shlibpath_overrides_runpath=no
+      sys_lib_search_path_spec='/lib /usr/lib /usr/ccs/lib'
+      ;;
+  esac
+  ;;
+
+sysv4*MP*)
+  if test -d /usr/nec ;then
+    version_type=linux
+    library_names_spec='$libname${shared_ext}.$versuffix $libname${shared_ext}.$major $libname${shared_ext}'
+    soname_spec='$libname${shared_ext}.$major'
+    shlibpath_var=LD_LIBRARY_PATH
+  fi
+  ;;
+
+sysv5* | sco3.2v5* | sco5v6* | unixware* | OpenUNIX* | sysv4*uw2*)
+  version_type=freebsd-elf
+  need_lib_prefix=no
+  need_version=no
+  library_names_spec='${libname}${release}${shared_ext}$versuffix ${libname}${release}${shared_ext} $libname${shared_ext}'
+  soname_spec='${libname}${release}${shared_ext}$major'
+  shlibpath_var=LD_LIBRARY_PATH
+  hardcode_into_libs=yes
+  if test "$with_gnu_ld" = yes; then
+    sys_lib_search_path_spec='/usr/local/lib /usr/gnu/lib /usr/ccs/lib /usr/lib /lib'
+    shlibpath_overrides_runpath=no
+  else
+    sys_lib_search_path_spec='/usr/ccs/lib /usr/lib'
+    shlibpath_overrides_runpath=yes
+    case $host_os in
+      sco3.2v5*)
+        sys_lib_search_path_spec="$sys_lib_search_path_spec /lib"
+	;;
+    esac
+  fi
+  sys_lib_dlsearch_path_spec='/usr/lib'
+  ;;
+
+uts4*)
+  version_type=linux
+  library_names_spec='${libname}${release}${shared_ext}$versuffix ${libname}${release}${shared_ext}$major $libname${shared_ext}'
+  soname_spec='${libname}${release}${shared_ext}$major'
+  shlibpath_var=LD_LIBRARY_PATH
+  ;;
+
+*)
+  dynamic_linker=no
+  ;;
+esac
+AC_MSG_RESULT([$dynamic_linker])
+test "$dynamic_linker" = no && can_build_shared=no
+
+AC_CACHE_VAL([lt_cv_sys_lib_search_path_spec],
+[lt_cv_sys_lib_search_path_spec="$sys_lib_search_path_spec"])
+sys_lib_search_path_spec="$lt_cv_sys_lib_search_path_spec"
+AC_CACHE_VAL([lt_cv_sys_lib_dlsearch_path_spec],
+[lt_cv_sys_lib_dlsearch_path_spec="$sys_lib_dlsearch_path_spec"])
+sys_lib_dlsearch_path_spec="$lt_cv_sys_lib_dlsearch_path_spec"
+
+variables_saved_for_relink="PATH $shlibpath_var $runpath_var"
+if test "$GCC" = yes; then
+  variables_saved_for_relink="$variables_saved_for_relink GCC_EXEC_PREFIX COMPILER_PATH LIBRARY_PATH"
+fi
+])# AC_LIBTOOL_SYS_DYNAMIC_LINKER
+
+
+# _LT_AC_TAGCONFIG
+# ----------------
+AC_DEFUN([_LT_AC_TAGCONFIG],
+[AC_REQUIRE([LT_AC_PROG_SED])dnl
+AC_ARG_WITH([tags],
+    [AC_HELP_STRING([--with-tags@<:@=TAGS@:>@],
+        [include additional configurations @<:@automatic@:>@])],
+    [tagnames="$withval"])
+
+if test -f "$ltmain" && test -n "$tagnames"; then
+  if test ! -f "${ofile}"; then
+    AC_MSG_WARN([output file `$ofile' does not exist])
+  fi
+
+  if test -z "$LTCC"; then
+    eval "`$SHELL ${ofile} --config | grep '^LTCC='`"
+    if test -z "$LTCC"; then
+      AC_MSG_WARN([output file `$ofile' does not look like a libtool script])
+    else
+      AC_MSG_WARN([using `LTCC=$LTCC', extracted from `$ofile'])
+    fi
+  fi
+  if test -z "$LTCFLAGS"; then
+    eval "`$SHELL ${ofile} --config | grep '^LTCFLAGS='`"
+  fi
+
+  # Extract list of available tagged configurations in $ofile.
+  # Note that this assumes the entire list is on one line.
+  available_tags=`grep "^available_tags=" "${ofile}" | $SED -e 's/available_tags=\(.*$\)/\1/' -e 's/\"//g'`
+
+  lt_save_ifs="$IFS"; IFS="${IFS}$PATH_SEPARATOR,"
+  for tagname in $tagnames; do
+    IFS="$lt_save_ifs"
+    # Check whether tagname contains only valid characters
+    case `$echo "X$tagname" | $Xsed -e 's:[[-_ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890,/]]::g'` in
+    "") ;;
+    *)  AC_MSG_ERROR([invalid tag name: $tagname])
+	;;
+    esac
+
+    if grep "^# ### BEGIN LIBTOOL TAG CONFIG: $tagname$" < "${ofile}" > /dev/null
+    then
+      AC_MSG_ERROR([tag name \"$tagname\" already exists])
+    fi
+
+    # Update the list of available tags.
+    if test -n "$tagname"; then
+      echo appending configuration tag \"$tagname\" to $ofile
+
+      case $tagname in
+      CXX)
+	if test -n "$CXX" && ( test "X$CXX" != "Xno" &&
+	    ( (test "X$CXX" = "Xg++" && `g++ -v >/dev/null 2>&1` ) ||
+	    (test "X$CXX" != "Xg++"))) ; then
+	  AC_LIBTOOL_LANG_CXX_CONFIG
+	else
+	  tagname=""
+	fi
+	;;
+
+      F77)
+	if test -n "$F77" && test "X$F77" != "Xno"; then
+	  AC_LIBTOOL_LANG_F77_CONFIG
+	else
+	  tagname=""
+	fi
+	;;
+
+      GCJ)
+	if test -n "$GCJ" && test "X$GCJ" != "Xno"; then
+	  AC_LIBTOOL_LANG_GCJ_CONFIG
+	else
+	  tagname=""
+	fi
+	;;
+
+      RC)
+	AC_LIBTOOL_LANG_RC_CONFIG
+	;;
+
+      *)
+	AC_MSG_ERROR([Unsupported tag name: $tagname])
+	;;
+      esac
+
+      # Append the new tag name to the list of available tags.
+      if test -n "$tagname" ; then
+      available_tags="$available_tags $tagname"
+    fi
+    fi
+  done
+  IFS="$lt_save_ifs"
+
+  # Now substitute the updated list of available tags.
+  if eval "sed -e 's/^available_tags=.*\$/available_tags=\"$available_tags\"/' \"$ofile\" > \"${ofile}T\""; then
+    mv "${ofile}T" "$ofile"
+    chmod +x "$ofile"
+  else
+    rm -f "${ofile}T"
+    AC_MSG_ERROR([unable to update list of available tagged configurations.])
+  fi
+fi
+])# _LT_AC_TAGCONFIG
+
+
+# AC_LIBTOOL_DLOPEN
+# -----------------
+# enable checks for dlopen support
+AC_DEFUN([AC_LIBTOOL_DLOPEN],
+ [AC_BEFORE([$0],[AC_LIBTOOL_SETUP])
+])# AC_LIBTOOL_DLOPEN
+
+
+# AC_LIBTOOL_WIN32_DLL
+# --------------------
+# declare package support for building win32 DLLs
+AC_DEFUN([AC_LIBTOOL_WIN32_DLL],
+[AC_BEFORE([$0], [AC_LIBTOOL_SETUP])
+])# AC_LIBTOOL_WIN32_DLL
+
+
+# AC_ENABLE_SHARED([DEFAULT])
+# ---------------------------
+# implement the --enable-shared flag
+# DEFAULT is either `yes' or `no'.  If omitted, it defaults to `yes'.
+AC_DEFUN([AC_ENABLE_SHARED],
+[define([AC_ENABLE_SHARED_DEFAULT], ifelse($1, no, no, yes))dnl
+AC_ARG_ENABLE([shared],
+    [AC_HELP_STRING([--enable-shared@<:@=PKGS@:>@],
+	[build shared libraries @<:@default=]AC_ENABLE_SHARED_DEFAULT[@:>@])],
+    [p=${PACKAGE-default}
+    case $enableval in
+    yes) enable_shared=yes ;;
+    no) enable_shared=no ;;
+    *)
+      enable_shared=no
+      # Look at the argument we got.  We use all the common list separators.
+      lt_save_ifs="$IFS"; IFS="${IFS}$PATH_SEPARATOR,"
+      for pkg in $enableval; do
+	IFS="$lt_save_ifs"
+	if test "X$pkg" = "X$p"; then
+	  enable_shared=yes
+	fi
+      done
+      IFS="$lt_save_ifs"
+      ;;
+    esac],
+    [enable_shared=]AC_ENABLE_SHARED_DEFAULT)
+])# AC_ENABLE_SHARED
+
+
+# AC_DISABLE_SHARED
+# -----------------
+# set the default shared flag to --disable-shared
+AC_DEFUN([AC_DISABLE_SHARED],
+[AC_BEFORE([$0],[AC_LIBTOOL_SETUP])dnl
+AC_ENABLE_SHARED(no)
+])# AC_DISABLE_SHARED
+
+
+# AC_ENABLE_STATIC([DEFAULT])
+# ---------------------------
+# implement the --enable-static flag
+# DEFAULT is either `yes' or `no'.  If omitted, it defaults to `yes'.
+AC_DEFUN([AC_ENABLE_STATIC],
+[define([AC_ENABLE_STATIC_DEFAULT], ifelse($1, no, no, yes))dnl
+AC_ARG_ENABLE([static],
+    [AC_HELP_STRING([--enable-static@<:@=PKGS@:>@],
+	[build static libraries @<:@default=]AC_ENABLE_STATIC_DEFAULT[@:>@])],
+    [p=${PACKAGE-default}
+    case $enableval in
+    yes) enable_static=yes ;;
+    no) enable_static=no ;;
+    *)
+     enable_static=no
+      # Look at the argument we got.  We use all the common list separators.
+      lt_save_ifs="$IFS"; IFS="${IFS}$PATH_SEPARATOR,"
+      for pkg in $enableval; do
+	IFS="$lt_save_ifs"
+	if test "X$pkg" = "X$p"; then
+	  enable_static=yes
+	fi
+      done
+      IFS="$lt_save_ifs"
+      ;;
+    esac],
+    [enable_static=]AC_ENABLE_STATIC_DEFAULT)
+])# AC_ENABLE_STATIC
+
+
+# AC_DISABLE_STATIC
+# -----------------
+# set the default static flag to --disable-static
+AC_DEFUN([AC_DISABLE_STATIC],
+[AC_BEFORE([$0],[AC_LIBTOOL_SETUP])dnl
+AC_ENABLE_STATIC(no)
+])# AC_DISABLE_STATIC
+
+
+# AC_ENABLE_FAST_INSTALL([DEFAULT])
+# ---------------------------------
+# implement the --enable-fast-install flag
+# DEFAULT is either `yes' or `no'.  If omitted, it defaults to `yes'.
+AC_DEFUN([AC_ENABLE_FAST_INSTALL],
+[define([AC_ENABLE_FAST_INSTALL_DEFAULT], ifelse($1, no, no, yes))dnl
+AC_ARG_ENABLE([fast-install],
+    [AC_HELP_STRING([--enable-fast-install@<:@=PKGS@:>@],
+    [optimize for fast installation @<:@default=]AC_ENABLE_FAST_INSTALL_DEFAULT[@:>@])],
+    [p=${PACKAGE-default}
+    case $enableval in
+    yes) enable_fast_install=yes ;;
+    no) enable_fast_install=no ;;
+    *)
+      enable_fast_install=no
+      # Look at the argument we got.  We use all the common list separators.
+      lt_save_ifs="$IFS"; IFS="${IFS}$PATH_SEPARATOR,"
+      for pkg in $enableval; do
+	IFS="$lt_save_ifs"
+	if test "X$pkg" = "X$p"; then
+	  enable_fast_install=yes
+	fi
+      done
+      IFS="$lt_save_ifs"
+      ;;
+    esac],
+    [enable_fast_install=]AC_ENABLE_FAST_INSTALL_DEFAULT)
+])# AC_ENABLE_FAST_INSTALL
+
+
+# AC_DISABLE_FAST_INSTALL
+# -----------------------
+# set the default to --disable-fast-install
+AC_DEFUN([AC_DISABLE_FAST_INSTALL],
+[AC_BEFORE([$0],[AC_LIBTOOL_SETUP])dnl
+AC_ENABLE_FAST_INSTALL(no)
+])# AC_DISABLE_FAST_INSTALL
+
+
+# AC_LIBTOOL_PICMODE([MODE])
+# --------------------------
+# implement the --with-pic flag
+# MODE is either `yes' or `no'.  If omitted, it defaults to `both'.
+AC_DEFUN([AC_LIBTOOL_PICMODE],
+[AC_BEFORE([$0],[AC_LIBTOOL_SETUP])dnl
+pic_mode=ifelse($#,1,$1,default)
+])# AC_LIBTOOL_PICMODE
+
+
+# AC_PROG_EGREP
+# -------------
+# This is predefined starting with Autoconf 2.54, so this conditional
+# definition can be removed once we require Autoconf 2.54 or later.
+m4_ifndef([AC_PROG_EGREP], [AC_DEFUN([AC_PROG_EGREP],
+[AC_CACHE_CHECK([for egrep], [ac_cv_prog_egrep],
+   [if echo a | (grep -E '(a|b)') >/dev/null 2>&1
+    then ac_cv_prog_egrep='grep -E'
+    else ac_cv_prog_egrep='egrep'
+    fi])
+ EGREP=$ac_cv_prog_egrep
+ AC_SUBST([EGREP])
+])])
+
+
+# AC_PATH_TOOL_PREFIX
+# -------------------
+# find a file program which can recognize shared library
+AC_DEFUN([AC_PATH_TOOL_PREFIX],
+[AC_REQUIRE([AC_PROG_EGREP])dnl
+AC_MSG_CHECKING([for $1])
+AC_CACHE_VAL(lt_cv_path_MAGIC_CMD,
+[case $MAGIC_CMD in
+[[\\/*] |  ?:[\\/]*])
+  lt_cv_path_MAGIC_CMD="$MAGIC_CMD" # Let the user override the test with a path.
+  ;;
+*)
+  lt_save_MAGIC_CMD="$MAGIC_CMD"
+  lt_save_ifs="$IFS"; IFS=$PATH_SEPARATOR
+dnl $ac_dummy forces splitting on constant user-supplied paths.
+dnl POSIX.2 word splitting is done only on the output of word expansions,
+dnl not every word.  This closes a longstanding sh security hole.
+  ac_dummy="ifelse([$2], , $PATH, [$2])"
+  for ac_dir in $ac_dummy; do
+    IFS="$lt_save_ifs"
+    test -z "$ac_dir" && ac_dir=.
+    if test -f $ac_dir/$1; then
+      lt_cv_path_MAGIC_CMD="$ac_dir/$1"
+      if test -n "$file_magic_test_file"; then
+	case $deplibs_check_method in
+	"file_magic "*)
+	  file_magic_regex=`expr "$deplibs_check_method" : "file_magic \(.*\)"`
+	  MAGIC_CMD="$lt_cv_path_MAGIC_CMD"
+	  if eval $file_magic_cmd \$file_magic_test_file 2> /dev/null |
+	    $EGREP "$file_magic_regex" > /dev/null; then
+	    :
+	  else
+	    cat <<EOF 1>&2
+
+*** Warning: the command libtool uses to detect shared libraries,
+*** $file_magic_cmd, produces output that libtool cannot recognize.
+*** The result is that libtool may fail to recognize shared libraries
+*** as such.  This will affect the creation of libtool libraries that
+*** depend on shared libraries, but programs linked with such libtool
+*** libraries will work regardless of this problem.  Nevertheless, you
+*** may want to report the problem to your system manager and/or to
+*** bug-libtool at gnu.org
+
+EOF
+	  fi ;;
+	esac
+      fi
+      break
+    fi
+  done
+  IFS="$lt_save_ifs"
+  MAGIC_CMD="$lt_save_MAGIC_CMD"
+  ;;
+esac])
+MAGIC_CMD="$lt_cv_path_MAGIC_CMD"
+if test -n "$MAGIC_CMD"; then
+  AC_MSG_RESULT($MAGIC_CMD)
+else
+  AC_MSG_RESULT(no)
+fi
+])# AC_PATH_TOOL_PREFIX
+
+
+# AC_PATH_MAGIC
+# -------------
+# find a file program which can recognize a shared library
+AC_DEFUN([AC_PATH_MAGIC],
+[AC_PATH_TOOL_PREFIX(${ac_tool_prefix}file, /usr/bin$PATH_SEPARATOR$PATH)
+if test -z "$lt_cv_path_MAGIC_CMD"; then
+  if test -n "$ac_tool_prefix"; then
+    AC_PATH_TOOL_PREFIX(file, /usr/bin$PATH_SEPARATOR$PATH)
+  else
+    MAGIC_CMD=:
+  fi
+fi
+])# AC_PATH_MAGIC
+
+
+# AC_PROG_LD
+# ----------
+# find the pathname to the GNU or non-GNU linker
+AC_DEFUN([AC_PROG_LD],
+[AC_ARG_WITH([gnu-ld],
+    [AC_HELP_STRING([--with-gnu-ld],
+	[assume the C compiler uses GNU ld @<:@default=no@:>@])],
+    [test "$withval" = no || with_gnu_ld=yes],
+    [with_gnu_ld=no])
+AC_REQUIRE([LT_AC_PROG_SED])dnl
+AC_REQUIRE([AC_PROG_CC])dnl
+AC_REQUIRE([AC_CANONICAL_HOST])dnl
+AC_REQUIRE([AC_CANONICAL_BUILD])dnl
+ac_prog=ld
+if test "$GCC" = yes; then
+  # Check if gcc -print-prog-name=ld gives a path.
+  AC_MSG_CHECKING([for ld used by $CC])
+  case $host in
+  *-*-mingw*)
+    # gcc leaves a trailing carriage return which upsets mingw
+    ac_prog=`($CC -print-prog-name=ld) 2>&5 | tr -d '\015'` ;;
+  *)
+    ac_prog=`($CC -print-prog-name=ld) 2>&5` ;;
+  esac
+  case $ac_prog in
+    # Accept absolute paths.
+    [[\\/]]* | ?:[[\\/]]*)
+      re_direlt='/[[^/]][[^/]]*/\.\./'
+      # Canonicalize the pathname of ld
+      ac_prog=`echo $ac_prog| $SED 's%\\\\%/%g'`
+      while echo $ac_prog | grep "$re_direlt" > /dev/null 2>&1; do
+	ac_prog=`echo $ac_prog| $SED "s%$re_direlt%/%"`
+      done
+      test -z "$LD" && LD="$ac_prog"
+      ;;
+  "")
+    # If it fails, then pretend we aren't using GCC.
+    ac_prog=ld
+    ;;
+  *)
+    # If it is relative, then search for the first ld in PATH.
+    with_gnu_ld=unknown
+    ;;
+  esac
+elif test "$with_gnu_ld" = yes; then
+  AC_MSG_CHECKING([for GNU ld])
+else
+  AC_MSG_CHECKING([for non-GNU ld])
+fi
+AC_CACHE_VAL(lt_cv_path_LD,
+[if test -z "$LD"; then
+  lt_save_ifs="$IFS"; IFS=$PATH_SEPARATOR
+  for ac_dir in $PATH; do
+    IFS="$lt_save_ifs"
+    test -z "$ac_dir" && ac_dir=.
+    if test -f "$ac_dir/$ac_prog" || test -f "$ac_dir/$ac_prog$ac_exeext"; then
+      lt_cv_path_LD="$ac_dir/$ac_prog"
+      # Check to see if the program is GNU ld.  I'd rather use --version,
+      # but apparently some variants of GNU ld only accept -v.
+      # Break only if it was the GNU/non-GNU ld that we prefer.
+      case `"$lt_cv_path_LD" -v 2>&1 </dev/null` in
+      *GNU* | *'with BFD'*)
+	test "$with_gnu_ld" != no && break
+	;;
+      *)
+	test "$with_gnu_ld" != yes && break
+	;;
+      esac
+    fi
+  done
+  IFS="$lt_save_ifs"
+else
+  lt_cv_path_LD="$LD" # Let the user override the test with a path.
+fi])
+LD="$lt_cv_path_LD"
+if test -n "$LD"; then
+  AC_MSG_RESULT($LD)
+else
+  AC_MSG_RESULT(no)
+fi
+test -z "$LD" && AC_MSG_ERROR([no acceptable ld found in \$PATH])
+AC_PROG_LD_GNU
+])# AC_PROG_LD
+
+
+# AC_PROG_LD_GNU
+# --------------
+AC_DEFUN([AC_PROG_LD_GNU],
+[AC_REQUIRE([AC_PROG_EGREP])dnl
+AC_CACHE_CHECK([if the linker ($LD) is GNU ld], lt_cv_prog_gnu_ld,
+[# I'd rather use --version here, but apparently some GNU lds only accept -v.
+case `$LD -v 2>&1 </dev/null` in
+*GNU* | *'with BFD'*)
+  lt_cv_prog_gnu_ld=yes
+  ;;
+*)
+  lt_cv_prog_gnu_ld=no
+  ;;
+esac])
+with_gnu_ld=$lt_cv_prog_gnu_ld
+])# AC_PROG_LD_GNU
+
+
+# AC_PROG_LD_RELOAD_FLAG
+# ----------------------
+# find reload flag for linker
+#   -- PORTME Some linkers may need a different reload flag.
+AC_DEFUN([AC_PROG_LD_RELOAD_FLAG],
+[AC_CACHE_CHECK([for $LD option to reload object files],
+  lt_cv_ld_reload_flag,
+  [lt_cv_ld_reload_flag='-r'])
+reload_flag=$lt_cv_ld_reload_flag
+case $reload_flag in
+"" | " "*) ;;
+*) reload_flag=" $reload_flag" ;;
+esac
+reload_cmds='$LD$reload_flag -o $output$reload_objs'
+case $host_os in
+  darwin*)
+    if test "$GCC" = yes; then
+      reload_cmds='$LTCC $LTCFLAGS -nostdlib ${wl}-r -o $output$reload_objs'
+    else
+      reload_cmds='$LD$reload_flag -o $output$reload_objs'
+    fi
+    ;;
+esac
+])# AC_PROG_LD_RELOAD_FLAG
+
+
+# AC_DEPLIBS_CHECK_METHOD
+# -----------------------
+# how to check for library dependencies
+#  -- PORTME fill in with the dynamic library characteristics
+AC_DEFUN([AC_DEPLIBS_CHECK_METHOD],
+[AC_CACHE_CHECK([how to recognize dependent libraries],
+lt_cv_deplibs_check_method,
+[lt_cv_file_magic_cmd='$MAGIC_CMD'
+lt_cv_file_magic_test_file=
+lt_cv_deplibs_check_method='unknown'
+# Need to set the preceding variable on all platforms that support
+# interlibrary dependencies.
+# 'none' -- dependencies not supported.
+# `unknown' -- same as none, but documents that we really don't know.
+# 'pass_all' -- all dependencies passed with no checks.
+# 'test_compile' -- check by making test program.
+# 'file_magic [[regex]]' -- check by looking for files in library path
+# which responds to the $file_magic_cmd with a given extended regex.
+# If you have `file' or equivalent on your system and you're not sure
+# whether `pass_all' will *always* work, you probably want this one.
+
+case $host_os in
+aix[[4-9]]*)
+  lt_cv_deplibs_check_method=pass_all
+  ;;
+
+beos*)
+  lt_cv_deplibs_check_method=pass_all
+  ;;
+
+bsdi[[45]]*)
+  lt_cv_deplibs_check_method='file_magic ELF [[0-9]][[0-9]]*-bit [[ML]]SB (shared object|dynamic lib)'
+  lt_cv_file_magic_cmd='/usr/bin/file -L'
+  lt_cv_file_magic_test_file=/shlib/libc.so
+  ;;
+
+cygwin*)
+  # func_win32_libid is a shell function defined in ltmain.sh
+  lt_cv_deplibs_check_method='file_magic ^x86 archive import|^x86 DLL'
+  lt_cv_file_magic_cmd='func_win32_libid'
+  ;;
+
+mingw* | pw32*)
+  # Base MSYS/MinGW do not provide the 'file' command needed by
+  # func_win32_libid shell function, so use a weaker test based on 'objdump',
+  # unless we find 'file', for example because we are cross-compiling.
+  if ( file / ) >/dev/null 2>&1; then
+    lt_cv_deplibs_check_method='file_magic ^x86 archive import|^x86 DLL'
+    lt_cv_file_magic_cmd='func_win32_libid'
+  else
+    lt_cv_deplibs_check_method='file_magic file format pei*-i386(.*architecture: i386)?'
+    lt_cv_file_magic_cmd='$OBJDUMP -f'
+  fi
+  ;;
+
+darwin* | rhapsody*)
+  lt_cv_deplibs_check_method=pass_all
+  ;;
+
+freebsd* | dragonfly*)
+  if echo __ELF__ | $CC -E - | grep __ELF__ > /dev/null; then
+    case $host_cpu in
+    i*86 )
+      # Not sure whether the presence of OpenBSD here was a mistake.
+      # Let's accept both of them until this is cleared up.
+      lt_cv_deplibs_check_method='file_magic (FreeBSD|OpenBSD|DragonFly)/i[[3-9]]86 (compact )?demand paged shared library'
+      lt_cv_file_magic_cmd=/usr/bin/file
+      lt_cv_file_magic_test_file=`echo /usr/lib/libc.so.*`
+      ;;
+    esac
+  else
+    lt_cv_deplibs_check_method=pass_all
+  fi
+  ;;
+
+gnu*)
+  lt_cv_deplibs_check_method=pass_all
+  ;;
+
+hpux10.20* | hpux11*)
+  lt_cv_file_magic_cmd=/usr/bin/file
+  case $host_cpu in
+  ia64*)
+    lt_cv_deplibs_check_method='file_magic (s[[0-9]][[0-9]][[0-9]]|ELF-[[0-9]][[0-9]]) shared object file - IA64'
+    lt_cv_file_magic_test_file=/usr/lib/hpux32/libc.so
+    ;;
+  hppa*64*)
+    [lt_cv_deplibs_check_method='file_magic (s[0-9][0-9][0-9]|ELF-[0-9][0-9]) shared object file - PA-RISC [0-9].[0-9]']
+    lt_cv_file_magic_test_file=/usr/lib/pa20_64/libc.sl
+    ;;
+  *)
+    lt_cv_deplibs_check_method='file_magic (s[[0-9]][[0-9]][[0-9]]|PA-RISC[[0-9]].[[0-9]]) shared library'
+    lt_cv_file_magic_test_file=/usr/lib/libc.sl
+    ;;
+  esac
+  ;;
+
+interix[[3-9]]*)
+  # PIC code is broken on Interix 3.x, that's why |\.a not |_pic\.a here
+  lt_cv_deplibs_check_method='match_pattern /lib[[^/]]+(\.so|\.a)$'
+  ;;
+
+irix5* | irix6* | nonstopux*)
+  case $LD in
+  *-32|*"-32 ") libmagic=32-bit;;
+  *-n32|*"-n32 ") libmagic=N32;;
+  *-64|*"-64 ") libmagic=64-bit;;
+  *) libmagic=never-match;;
+  esac
+  lt_cv_deplibs_check_method=pass_all
+  ;;
+
+# This must be Linux ELF.
+linux* | k*bsd*-gnu)
+  lt_cv_deplibs_check_method=pass_all
+  ;;
+
+netbsd*)
+  if echo __ELF__ | $CC -E - | grep __ELF__ > /dev/null; then
+    lt_cv_deplibs_check_method='match_pattern /lib[[^/]]+(\.so\.[[0-9]]+\.[[0-9]]+|_pic\.a)$'
+  else
+    lt_cv_deplibs_check_method='match_pattern /lib[[^/]]+(\.so|_pic\.a)$'
+  fi
+  ;;
+
+newos6*)
+  lt_cv_deplibs_check_method='file_magic ELF [[0-9]][[0-9]]*-bit [[ML]]SB (executable|dynamic lib)'
+  lt_cv_file_magic_cmd=/usr/bin/file
+  lt_cv_file_magic_test_file=/usr/lib/libnls.so
+  ;;
+
+nto-qnx*)
+  lt_cv_deplibs_check_method=unknown
+  ;;
+
+openbsd*)
+  if test -z "`echo __ELF__ | $CC -E - | grep __ELF__`" || test "$host_os-$host_cpu" = "openbsd2.8-powerpc"; then
+    lt_cv_deplibs_check_method='match_pattern /lib[[^/]]+(\.so\.[[0-9]]+\.[[0-9]]+|\.so|_pic\.a)$'
+  else
+    lt_cv_deplibs_check_method='match_pattern /lib[[^/]]+(\.so\.[[0-9]]+\.[[0-9]]+|_pic\.a)$'
+  fi
+  ;;
+
+osf3* | osf4* | osf5*)
+  lt_cv_deplibs_check_method=pass_all
+  ;;
+
+rdos*)
+  lt_cv_deplibs_check_method=pass_all
+  ;;
+
+solaris*)
+  lt_cv_deplibs_check_method=pass_all
+  ;;
+
+sysv4 | sysv4.3*)
+  case $host_vendor in
+  motorola)
+    lt_cv_deplibs_check_method='file_magic ELF [[0-9]][[0-9]]*-bit [[ML]]SB (shared object|dynamic lib) M[[0-9]][[0-9]]* Version [[0-9]]'
+    lt_cv_file_magic_test_file=`echo /usr/lib/libc.so*`
+    ;;
+  ncr)
+    lt_cv_deplibs_check_method=pass_all
+    ;;
+  sequent)
+    lt_cv_file_magic_cmd='/bin/file'
+    lt_cv_deplibs_check_method='file_magic ELF [[0-9]][[0-9]]*-bit [[LM]]SB (shared object|dynamic lib )'
+    ;;
+  sni)
+    lt_cv_file_magic_cmd='/bin/file'
+    lt_cv_deplibs_check_method="file_magic ELF [[0-9]][[0-9]]*-bit [[LM]]SB dynamic lib"
+    lt_cv_file_magic_test_file=/lib/libc.so
+    ;;
+  siemens)
+    lt_cv_deplibs_check_method=pass_all
+    ;;
+  pc)
+    lt_cv_deplibs_check_method=pass_all
+    ;;
+  esac
+  ;;
+
+sysv5* | sco3.2v5* | sco5v6* | unixware* | OpenUNIX* | sysv4*uw2*)
+  lt_cv_deplibs_check_method=pass_all
+  ;;
+esac
+])
+file_magic_cmd=$lt_cv_file_magic_cmd
+deplibs_check_method=$lt_cv_deplibs_check_method
+test -z "$deplibs_check_method" && deplibs_check_method=unknown
+])# AC_DEPLIBS_CHECK_METHOD
+
+
+# AC_PROG_NM
+# ----------
+# find the pathname to a BSD-compatible name lister
+AC_DEFUN([AC_PROG_NM],
+[AC_CACHE_CHECK([for BSD-compatible nm], lt_cv_path_NM,
+[if test -n "$NM"; then
+  # Let the user override the test.
+  lt_cv_path_NM="$NM"
+else
+  lt_nm_to_check="${ac_tool_prefix}nm"
+  if test -n "$ac_tool_prefix" && test "$build" = "$host"; then
+    lt_nm_to_check="$lt_nm_to_check nm"
+  fi
+  for lt_tmp_nm in $lt_nm_to_check; do
+    lt_save_ifs="$IFS"; IFS=$PATH_SEPARATOR
+    for ac_dir in $PATH /usr/ccs/bin/elf /usr/ccs/bin /usr/ucb /bin; do
+      IFS="$lt_save_ifs"
+      test -z "$ac_dir" && ac_dir=.
+      tmp_nm="$ac_dir/$lt_tmp_nm"
+      if test -f "$tmp_nm" || test -f "$tmp_nm$ac_exeext" ; then
+	# Check to see if the nm accepts a BSD-compat flag.
+	# Adding the `sed 1q' prevents false positives on HP-UX, which says:
+	#   nm: unknown option "B" ignored
+	# Tru64's nm complains that /dev/null is an invalid object file
+	case `"$tmp_nm" -B /dev/null 2>&1 | sed '1q'` in
+	*/dev/null* | *'Invalid file or object type'*)
+	  lt_cv_path_NM="$tmp_nm -B"
+	  break
+	  ;;
+	*)
+	  case `"$tmp_nm" -p /dev/null 2>&1 | sed '1q'` in
+	  */dev/null*)
+	    lt_cv_path_NM="$tmp_nm -p"
+	    break
+	    ;;
+	  *)
+	    lt_cv_path_NM=${lt_cv_path_NM="$tmp_nm"} # keep the first match, but
+	    continue # so that we can try to find one that supports BSD flags
+	    ;;
+	  esac
+	  ;;
+	esac
+      fi
+    done
+    IFS="$lt_save_ifs"
+  done
+  test -z "$lt_cv_path_NM" && lt_cv_path_NM=nm
+fi])
+NM="$lt_cv_path_NM"
+])# AC_PROG_NM
+
+
+# AC_CHECK_LIBM
+# -------------
+# check for math library
+AC_DEFUN([AC_CHECK_LIBM],
+[AC_REQUIRE([AC_CANONICAL_HOST])dnl
+LIBM=
+case $host in
+*-*-beos* | *-*-cygwin* | *-*-pw32* | *-*-darwin*)
+  # These system don't have libm, or don't need it
+  ;;
+*-ncr-sysv4.3*)
+  AC_CHECK_LIB(mw, _mwvalidcheckl, LIBM="-lmw")
+  AC_CHECK_LIB(m, cos, LIBM="$LIBM -lm")
+  ;;
+*)
+  AC_CHECK_LIB(m, cos, LIBM="-lm")
+  ;;
+esac
+])# AC_CHECK_LIBM
+
+
+# AC_LIBLTDL_CONVENIENCE([DIRECTORY])
+# -----------------------------------
+# sets LIBLTDL to the link flags for the libltdl convenience library and
+# LTDLINCL to the include flags for the libltdl header and adds
+# --enable-ltdl-convenience to the configure arguments.  Note that
+# AC_CONFIG_SUBDIRS is not called here.  If DIRECTORY is not provided,
+# it is assumed to be `libltdl'.  LIBLTDL will be prefixed with
+# '${top_builddir}/' and LTDLINCL will be prefixed with '${top_srcdir}/'
+# (note the single quotes!).  If your package is not flat and you're not
+# using automake, define top_builddir and top_srcdir appropriately in
+# the Makefiles.
+AC_DEFUN([AC_LIBLTDL_CONVENIENCE],
+[AC_BEFORE([$0],[AC_LIBTOOL_SETUP])dnl
+  case $enable_ltdl_convenience in
+  no) AC_MSG_ERROR([this package needs a convenience libltdl]) ;;
+  "") enable_ltdl_convenience=yes
+      ac_configure_args="$ac_configure_args --enable-ltdl-convenience" ;;
+  esac
+  LIBLTDL='${top_builddir}/'ifelse($#,1,[$1],['libltdl'])/libltdlc.la
+  LTDLINCL='-I${top_srcdir}/'ifelse($#,1,[$1],['libltdl'])
+  # For backwards non-gettext consistent compatibility...
+  INCLTDL="$LTDLINCL"
+])# AC_LIBLTDL_CONVENIENCE
+
+
+# AC_LIBLTDL_INSTALLABLE([DIRECTORY])
+# -----------------------------------
+# sets LIBLTDL to the link flags for the libltdl installable library and
+# LTDLINCL to the include flags for the libltdl header and adds
+# --enable-ltdl-install to the configure arguments.  Note that
+# AC_CONFIG_SUBDIRS is not called here.  If DIRECTORY is not provided,
+# and an installed libltdl is not found, it is assumed to be `libltdl'.
+# LIBLTDL will be prefixed with '${top_builddir}/'# and LTDLINCL with
+# '${top_srcdir}/' (note the single quotes!).  If your package is not
+# flat and you're not using automake, define top_builddir and top_srcdir
+# appropriately in the Makefiles.
+# In the future, this macro may have to be called after AC_PROG_LIBTOOL.
+AC_DEFUN([AC_LIBLTDL_INSTALLABLE],
+[AC_BEFORE([$0],[AC_LIBTOOL_SETUP])dnl
+  AC_CHECK_LIB(ltdl, lt_dlinit,
+  [test x"$enable_ltdl_install" != xyes && enable_ltdl_install=no],
+  [if test x"$enable_ltdl_install" = xno; then
+     AC_MSG_WARN([libltdl not installed, but installation disabled])
+   else
+     enable_ltdl_install=yes
+   fi
+  ])
+  if test x"$enable_ltdl_install" = x"yes"; then
+    ac_configure_args="$ac_configure_args --enable-ltdl-install"
+    LIBLTDL='${top_builddir}/'ifelse($#,1,[$1],['libltdl'])/libltdl.la
+    LTDLINCL='-I${top_srcdir}/'ifelse($#,1,[$1],['libltdl'])
+  else
+    ac_configure_args="$ac_configure_args --enable-ltdl-install=no"
+    LIBLTDL="-lltdl"
+    LTDLINCL=
+  fi
+  # For backwards non-gettext consistent compatibility...
+  INCLTDL="$LTDLINCL"
+])# AC_LIBLTDL_INSTALLABLE
+
+
+# AC_LIBTOOL_CXX
+# --------------
+# enable support for C++ libraries
+AC_DEFUN([AC_LIBTOOL_CXX],
+[AC_REQUIRE([_LT_AC_LANG_CXX])
+])# AC_LIBTOOL_CXX
+
+
+# _LT_AC_LANG_CXX
+# ---------------
+AC_DEFUN([_LT_AC_LANG_CXX],
+[AC_REQUIRE([AC_PROG_CXX])
+AC_REQUIRE([_LT_AC_PROG_CXXCPP])
+_LT_AC_SHELL_INIT([tagnames=${tagnames+${tagnames},}CXX])
+])# _LT_AC_LANG_CXX
+
+# _LT_AC_PROG_CXXCPP
+# ------------------
+AC_DEFUN([_LT_AC_PROG_CXXCPP],
+[
+AC_REQUIRE([AC_PROG_CXX])
+if test -n "$CXX" && ( test "X$CXX" != "Xno" &&
+    ( (test "X$CXX" = "Xg++" && `g++ -v >/dev/null 2>&1` ) ||
+    (test "X$CXX" != "Xg++"))) ; then
+  AC_PROG_CXXCPP
+fi
+])# _LT_AC_PROG_CXXCPP
+
+# AC_LIBTOOL_F77
+# --------------
+# enable support for Fortran 77 libraries
+AC_DEFUN([AC_LIBTOOL_F77],
+[AC_REQUIRE([_LT_AC_LANG_F77])
+])# AC_LIBTOOL_F77
+
+
+# _LT_AC_LANG_F77
+# ---------------
+AC_DEFUN([_LT_AC_LANG_F77],
+[AC_REQUIRE([AC_PROG_F77])
+_LT_AC_SHELL_INIT([tagnames=${tagnames+${tagnames},}F77])
+])# _LT_AC_LANG_F77
+
+
+# AC_LIBTOOL_GCJ
+# --------------
+# enable support for GCJ libraries
+AC_DEFUN([AC_LIBTOOL_GCJ],
+[AC_REQUIRE([_LT_AC_LANG_GCJ])
+])# AC_LIBTOOL_GCJ
+
+
+# _LT_AC_LANG_GCJ
+# ---------------
+AC_DEFUN([_LT_AC_LANG_GCJ],
+[AC_PROVIDE_IFELSE([AC_PROG_GCJ],[],
+  [AC_PROVIDE_IFELSE([A][M_PROG_GCJ],[],
+    [AC_PROVIDE_IFELSE([LT_AC_PROG_GCJ],[],
+      [ifdef([AC_PROG_GCJ],[AC_REQUIRE([AC_PROG_GCJ])],
+	 [ifdef([A][M_PROG_GCJ],[AC_REQUIRE([A][M_PROG_GCJ])],
+	   [AC_REQUIRE([A][C_PROG_GCJ_OR_A][M_PROG_GCJ])])])])])])
+_LT_AC_SHELL_INIT([tagnames=${tagnames+${tagnames},}GCJ])
+])# _LT_AC_LANG_GCJ
+
+
+# AC_LIBTOOL_RC
+# -------------
+# enable support for Windows resource files
+AC_DEFUN([AC_LIBTOOL_RC],
+[AC_REQUIRE([LT_AC_PROG_RC])
+_LT_AC_SHELL_INIT([tagnames=${tagnames+${tagnames},}RC])
+])# AC_LIBTOOL_RC
+
+
+# AC_LIBTOOL_LANG_C_CONFIG
+# ------------------------
+# Ensure that the configuration vars for the C compiler are
+# suitably defined.  Those variables are subsequently used by
+# AC_LIBTOOL_CONFIG to write the compiler configuration to `libtool'.
+AC_DEFUN([AC_LIBTOOL_LANG_C_CONFIG], [_LT_AC_LANG_C_CONFIG])
+AC_DEFUN([_LT_AC_LANG_C_CONFIG],
+[lt_save_CC="$CC"
+AC_LANG_PUSH(C)
+
+# Source file extension for C test sources.
+ac_ext=c
+
+# Object file extension for compiled C test sources.
+objext=o
+_LT_AC_TAGVAR(objext, $1)=$objext
+
+# Code to be used in simple compile tests
+lt_simple_compile_test_code="int some_variable = 0;"
+
+# Code to be used in simple link tests
+lt_simple_link_test_code='int main(){return(0);}'
+
+_LT_AC_SYS_COMPILER
+
+# save warnings/boilerplate of simple test code
+_LT_COMPILER_BOILERPLATE
+_LT_LINKER_BOILERPLATE
+
+AC_LIBTOOL_PROG_COMPILER_NO_RTTI($1)
+AC_LIBTOOL_PROG_COMPILER_PIC($1)
+AC_LIBTOOL_PROG_CC_C_O($1)
+AC_LIBTOOL_SYS_HARD_LINK_LOCKS($1)
+AC_LIBTOOL_PROG_LD_SHLIBS($1)
+AC_LIBTOOL_SYS_DYNAMIC_LINKER($1)
+AC_LIBTOOL_PROG_LD_HARDCODE_LIBPATH($1)
+AC_LIBTOOL_SYS_LIB_STRIP
+AC_LIBTOOL_DLOPEN_SELF
+
+# Report which library types will actually be built
+AC_MSG_CHECKING([if libtool supports shared libraries])
+AC_MSG_RESULT([$can_build_shared])
+
+AC_MSG_CHECKING([whether to build shared libraries])
+test "$can_build_shared" = "no" && enable_shared=no
+
+# On AIX, shared libraries and static libraries use the same namespace, and
+# are all built from PIC.
+case $host_os in
+aix3*)
+  test "$enable_shared" = yes && enable_static=no
+  if test -n "$RANLIB"; then
+    archive_cmds="$archive_cmds~\$RANLIB \$lib"
+    postinstall_cmds='$RANLIB $lib'
+  fi
+  ;;
+
+aix[[4-9]]*)
+  if test "$host_cpu" != ia64 && test "$aix_use_runtimelinking" = no ; then
+    test "$enable_shared" = yes && enable_static=no
+  fi
+    ;;
+esac
+AC_MSG_RESULT([$enable_shared])
+
+AC_MSG_CHECKING([whether to build static libraries])
+# Make sure either enable_shared or enable_static is yes.
+test "$enable_shared" = yes || enable_static=yes
+AC_MSG_RESULT([$enable_static])
+
+AC_LIBTOOL_CONFIG($1)
+
+AC_LANG_POP
+CC="$lt_save_CC"
+])# AC_LIBTOOL_LANG_C_CONFIG
+
+
+# AC_LIBTOOL_LANG_CXX_CONFIG
+# --------------------------
+# Ensure that the configuration vars for the C compiler are
+# suitably defined.  Those variables are subsequently used by
+# AC_LIBTOOL_CONFIG to write the compiler configuration to `libtool'.
+AC_DEFUN([AC_LIBTOOL_LANG_CXX_CONFIG], [_LT_AC_LANG_CXX_CONFIG(CXX)])
+AC_DEFUN([_LT_AC_LANG_CXX_CONFIG],
+[AC_LANG_PUSH(C++)
+AC_REQUIRE([AC_PROG_CXX])
+AC_REQUIRE([_LT_AC_PROG_CXXCPP])
+
+_LT_AC_TAGVAR(archive_cmds_need_lc, $1)=no
+_LT_AC_TAGVAR(allow_undefined_flag, $1)=
+_LT_AC_TAGVAR(always_export_symbols, $1)=no
+_LT_AC_TAGVAR(archive_expsym_cmds, $1)=
+_LT_AC_TAGVAR(export_dynamic_flag_spec, $1)=
+_LT_AC_TAGVAR(hardcode_direct, $1)=no
+_LT_AC_TAGVAR(hardcode_libdir_flag_spec, $1)=
+_LT_AC_TAGVAR(hardcode_libdir_flag_spec_ld, $1)=
+_LT_AC_TAGVAR(hardcode_libdir_separator, $1)=
+_LT_AC_TAGVAR(hardcode_minus_L, $1)=no
+_LT_AC_TAGVAR(hardcode_shlibpath_var, $1)=unsupported
+_LT_AC_TAGVAR(hardcode_automatic, $1)=no
+_LT_AC_TAGVAR(module_cmds, $1)=
+_LT_AC_TAGVAR(module_expsym_cmds, $1)=
+_LT_AC_TAGVAR(link_all_deplibs, $1)=unknown
+_LT_AC_TAGVAR(old_archive_cmds, $1)=$old_archive_cmds
+_LT_AC_TAGVAR(no_undefined_flag, $1)=
+_LT_AC_TAGVAR(whole_archive_flag_spec, $1)=
+_LT_AC_TAGVAR(enable_shared_with_static_runtimes, $1)=no
+
+# Dependencies to place before and after the object being linked:
+_LT_AC_TAGVAR(predep_objects, $1)=
+_LT_AC_TAGVAR(postdep_objects, $1)=
+_LT_AC_TAGVAR(predeps, $1)=
+_LT_AC_TAGVAR(postdeps, $1)=
+_LT_AC_TAGVAR(compiler_lib_search_path, $1)=
+_LT_AC_TAGVAR(compiler_lib_search_dirs, $1)=
+
+# Source file extension for C++ test sources.
+ac_ext=cpp
+
+# Object file extension for compiled C++ test sources.
+objext=o
+_LT_AC_TAGVAR(objext, $1)=$objext
+
+# Code to be used in simple compile tests
+lt_simple_compile_test_code="int some_variable = 0;"
+
+# Code to be used in simple link tests
+lt_simple_link_test_code='int main(int, char *[[]]) { return(0); }'
+
+# ltmain only uses $CC for tagged configurations so make sure $CC is set.
+_LT_AC_SYS_COMPILER
+
+# save warnings/boilerplate of simple test code
+_LT_COMPILER_BOILERPLATE
+_LT_LINKER_BOILERPLATE
+
+# Allow CC to be a program name with arguments.
+lt_save_CC=$CC
+lt_save_LD=$LD
+lt_save_GCC=$GCC
+GCC=$GXX
+lt_save_with_gnu_ld=$with_gnu_ld
+lt_save_path_LD=$lt_cv_path_LD
+if test -n "${lt_cv_prog_gnu_ldcxx+set}"; then
+  lt_cv_prog_gnu_ld=$lt_cv_prog_gnu_ldcxx
+else
+  $as_unset lt_cv_prog_gnu_ld
+fi
+if test -n "${lt_cv_path_LDCXX+set}"; then
+  lt_cv_path_LD=$lt_cv_path_LDCXX
+else
+  $as_unset lt_cv_path_LD
+fi
+test -z "${LDCXX+set}" || LD=$LDCXX
+CC=${CXX-"c++"}
+compiler=$CC
+_LT_AC_TAGVAR(compiler, $1)=$CC
+_LT_CC_BASENAME([$compiler])
+
+# We don't want -fno-exception wen compiling C++ code, so set the
+# no_builtin_flag separately
+if test "$GXX" = yes; then
+  _LT_AC_TAGVAR(lt_prog_compiler_no_builtin_flag, $1)=' -fno-builtin'
+else
+  _LT_AC_TAGVAR(lt_prog_compiler_no_builtin_flag, $1)=
+fi
+
+if test "$GXX" = yes; then
+  # Set up default GNU C++ configuration
+
+  AC_PROG_LD
+
+  # Check if GNU C++ uses GNU ld as the underlying linker, since the
+  # archiving commands below assume that GNU ld is being used.
+  if test "$with_gnu_ld" = yes; then
+    _LT_AC_TAGVAR(archive_cmds, $1)='$CC -shared -nostdlib $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags ${wl}-soname $wl$soname -o $lib'
+    _LT_AC_TAGVAR(archive_expsym_cmds, $1)='$CC -shared -nostdlib $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags ${wl}-soname $wl$soname ${wl}-retain-symbols-file $wl$export_symbols -o $lib'
+
+    _LT_AC_TAGVAR(hardcode_libdir_flag_spec, $1)='${wl}--rpath ${wl}$libdir'
+    _LT_AC_TAGVAR(export_dynamic_flag_spec, $1)='${wl}--export-dynamic'
+
+    # If archive_cmds runs LD, not CC, wlarc should be empty
+    # XXX I think wlarc can be eliminated in ltcf-cxx, but I need to
+    #     investigate it a little bit more. (MM)
+    wlarc='${wl}'
+
+    # ancient GNU ld didn't support --whole-archive et. al.
+    if eval "`$CC -print-prog-name=ld` --help 2>&1" | \
+	grep 'no-whole-archive' > /dev/null; then
+      _LT_AC_TAGVAR(whole_archive_flag_spec, $1)="$wlarc"'--whole-archive$convenience '"$wlarc"'--no-whole-archive'
+    else
+      _LT_AC_TAGVAR(whole_archive_flag_spec, $1)=
+    fi
+  else
+    with_gnu_ld=no
+    wlarc=
+
+    # A generic and very simple default shared library creation
+    # command for GNU C++ for the case where it uses the native
+    # linker, instead of GNU ld.  If possible, this setting should
+    # overridden to take advantage of the native linker features on
+    # the platform it is being used on.
+    _LT_AC_TAGVAR(archive_cmds, $1)='$CC -shared -nostdlib $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags -o $lib'
+  fi
+
+  # Commands to make compiler produce verbose output that lists
+  # what "hidden" libraries, object files and flags are used when
+  # linking a shared library.
+  output_verbose_link_cmd='$CC -shared $CFLAGS -v conftest.$objext 2>&1 | grep "\-L"'
+
+else
+  GXX=no
+  with_gnu_ld=no
+  wlarc=
+fi
+
+# PORTME: fill in a description of your system's C++ link characteristics
+AC_MSG_CHECKING([whether the $compiler linker ($LD) supports shared libraries])
+_LT_AC_TAGVAR(ld_shlibs, $1)=yes
+case $host_os in
+  aix3*)
+    # FIXME: insert proper C++ library support
+    _LT_AC_TAGVAR(ld_shlibs, $1)=no
+    ;;
+  aix[[4-9]]*)
+    if test "$host_cpu" = ia64; then
+      # On IA64, the linker does run time linking by default, so we don't
+      # have to do anything special.
+      aix_use_runtimelinking=no
+      exp_sym_flag='-Bexport'
+      no_entry_flag=""
+    else
+      aix_use_runtimelinking=no
+
+      # Test if we are trying to use run time linking or normal
+      # AIX style linking. If -brtl is somewhere in LDFLAGS, we
+      # need to do runtime linking.
+      case $host_os in aix4.[[23]]|aix4.[[23]].*|aix[[5-9]]*)
+	for ld_flag in $LDFLAGS; do
+	  case $ld_flag in
+	  *-brtl*)
+	    aix_use_runtimelinking=yes
+	    break
+	    ;;
+	  esac
+	done
+	;;
+      esac
+
+      exp_sym_flag='-bexport'
+      no_entry_flag='-bnoentry'
+    fi
+
+    # When large executables or shared objects are built, AIX ld can
+    # have problems creating the table of contents.  If linking a library
+    # or program results in "error TOC overflow" add -mminimal-toc to
+    # CXXFLAGS/CFLAGS for g++/gcc.  In the cases where that is not
+    # enough to fix the problem, add -Wl,-bbigtoc to LDFLAGS.
+
+    _LT_AC_TAGVAR(archive_cmds, $1)=''
+    _LT_AC_TAGVAR(hardcode_direct, $1)=yes
+    _LT_AC_TAGVAR(hardcode_libdir_separator, $1)=':'
+    _LT_AC_TAGVAR(link_all_deplibs, $1)=yes
+
+    if test "$GXX" = yes; then
+      case $host_os in aix4.[[012]]|aix4.[[012]].*)
+      # We only want to do this on AIX 4.2 and lower, the check
+      # below for broken collect2 doesn't work under 4.3+
+	collect2name=`${CC} -print-prog-name=collect2`
+	if test -f "$collect2name" && \
+	   strings "$collect2name" | grep resolve_lib_name >/dev/null
+	then
+	  # We have reworked collect2
+	  :
+	else
+	  # We have old collect2
+	  _LT_AC_TAGVAR(hardcode_direct, $1)=unsupported
+	  # It fails to find uninstalled libraries when the uninstalled
+	  # path is not listed in the libpath.  Setting hardcode_minus_L
+	  # to unsupported forces relinking
+	  _LT_AC_TAGVAR(hardcode_minus_L, $1)=yes
+	  _LT_AC_TAGVAR(hardcode_libdir_flag_spec, $1)='-L$libdir'
+	  _LT_AC_TAGVAR(hardcode_libdir_separator, $1)=
+	fi
+	;;
+      esac
+      shared_flag='-shared'
+      if test "$aix_use_runtimelinking" = yes; then
+	shared_flag="$shared_flag "'${wl}-G'
+      fi
+    else
+      # not using gcc
+      if test "$host_cpu" = ia64; then
+	# VisualAge C++, Version 5.5 for AIX 5L for IA-64, Beta 3 Release
+	# chokes on -Wl,-G. The following line is correct:
+	shared_flag='-G'
+      else
+	if test "$aix_use_runtimelinking" = yes; then
+	  shared_flag='${wl}-G'
+	else
+	  shared_flag='${wl}-bM:SRE'
+	fi
+      fi
+    fi
+
+    # It seems that -bexpall does not export symbols beginning with
+    # underscore (_), so it is better to generate a list of symbols to export.
+    _LT_AC_TAGVAR(always_export_symbols, $1)=yes
+    if test "$aix_use_runtimelinking" = yes; then
+      # Warning - without using the other runtime loading flags (-brtl),
+      # -berok will link without error, but may produce a broken library.
+      _LT_AC_TAGVAR(allow_undefined_flag, $1)='-berok'
+      # Determine the default libpath from the value encoded in an empty executable.
+      _LT_AC_SYS_LIBPATH_AIX
+      _LT_AC_TAGVAR(hardcode_libdir_flag_spec, $1)='${wl}-blibpath:$libdir:'"$aix_libpath"
+
+      _LT_AC_TAGVAR(archive_expsym_cmds, $1)="\$CC"' -o $output_objdir/$soname $libobjs $deplibs '"\${wl}$no_entry_flag"' $compiler_flags `if test "x${allow_undefined_flag}" != "x"; then echo "${wl}${allow_undefined_flag}"; else :; fi` '"\${wl}$exp_sym_flag:\$export_symbols $shared_flag"
+     else
+      if test "$host_cpu" = ia64; then
+	_LT_AC_TAGVAR(hardcode_libdir_flag_spec, $1)='${wl}-R $libdir:/usr/lib:/lib'
+	_LT_AC_TAGVAR(allow_undefined_flag, $1)="-z nodefs"
+	_LT_AC_TAGVAR(archive_expsym_cmds, $1)="\$CC $shared_flag"' -o $output_objdir/$soname $libobjs $deplibs '"\${wl}$no_entry_flag"' $compiler_flags ${wl}${allow_undefined_flag} '"\${wl}$exp_sym_flag:\$export_symbols"
+      else
+	# Determine the default libpath from the value encoded in an empty executable.
+	_LT_AC_SYS_LIBPATH_AIX
+	_LT_AC_TAGVAR(hardcode_libdir_flag_spec, $1)='${wl}-blibpath:$libdir:'"$aix_libpath"
+	# Warning - without using the other run time loading flags,
+	# -berok will link without error, but may produce a broken library.
+	_LT_AC_TAGVAR(no_undefined_flag, $1)=' ${wl}-bernotok'
+	_LT_AC_TAGVAR(allow_undefined_flag, $1)=' ${wl}-berok'
+	# Exported symbols can be pulled into shared objects from archives
+	_LT_AC_TAGVAR(whole_archive_flag_spec, $1)='$convenience'
+	_LT_AC_TAGVAR(archive_cmds_need_lc, $1)=yes
+	# This is similar to how AIX traditionally builds its shared libraries.
+	_LT_AC_TAGVAR(archive_expsym_cmds, $1)="\$CC $shared_flag"' -o $output_objdir/$soname $libobjs $deplibs ${wl}-bnoentry $compiler_flags ${wl}-bE:$export_symbols${allow_undefined_flag}~$AR $AR_FLAGS $output_objdir/$libname$release.a $output_objdir/$soname'
+      fi
+    fi
+    ;;
+
+  beos*)
+    if $LD --help 2>&1 | grep ': supported targets:.* elf' > /dev/null; then
+      _LT_AC_TAGVAR(allow_undefined_flag, $1)=unsupported
+      # Joseph Beckenbach <jrb3 at best.com> says some releases of gcc
+      # support --undefined.  This deserves some investigation.  FIXME
+      _LT_AC_TAGVAR(archive_cmds, $1)='$CC -nostart $libobjs $deplibs $compiler_flags ${wl}-soname $wl$soname -o $lib'
+    else
+      _LT_AC_TAGVAR(ld_shlibs, $1)=no
+    fi
+    ;;
+
+  chorus*)
+    case $cc_basename in
+      *)
+	# FIXME: insert proper C++ library support
+	_LT_AC_TAGVAR(ld_shlibs, $1)=no
+	;;
+    esac
+    ;;
+
+  cygwin* | mingw* | pw32*)
+    # _LT_AC_TAGVAR(hardcode_libdir_flag_spec, $1) is actually meaningless,
+    # as there is no search path for DLLs.
+    _LT_AC_TAGVAR(hardcode_libdir_flag_spec, $1)='-L$libdir'
+    _LT_AC_TAGVAR(allow_undefined_flag, $1)=unsupported
+    _LT_AC_TAGVAR(always_export_symbols, $1)=no
+    _LT_AC_TAGVAR(enable_shared_with_static_runtimes, $1)=yes
+
+    if $LD --help 2>&1 | grep 'auto-import' > /dev/null; then
+      _LT_AC_TAGVAR(archive_cmds, $1)='$CC -shared -nostdlib $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags -o $output_objdir/$soname ${wl}--enable-auto-image-base -Xlinker --out-implib -Xlinker $lib'
+      # If the export-symbols file already is a .def file (1st line
+      # is EXPORTS), use it as is; otherwise, prepend...
+      _LT_AC_TAGVAR(archive_expsym_cmds, $1)='if test "x`$SED 1q $export_symbols`" = xEXPORTS; then
+	cp $export_symbols $output_objdir/$soname.def;
+      else
+	echo EXPORTS > $output_objdir/$soname.def;
+	cat $export_symbols >> $output_objdir/$soname.def;
+      fi~
+      $CC -shared -nostdlib $output_objdir/$soname.def $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags -o $output_objdir/$soname ${wl}--enable-auto-image-base -Xlinker --out-implib -Xlinker $lib'
+    else
+      _LT_AC_TAGVAR(ld_shlibs, $1)=no
+    fi
+  ;;
+      darwin* | rhapsody*)
+      _LT_AC_TAGVAR(archive_cmds_need_lc, $1)=no
+      _LT_AC_TAGVAR(hardcode_direct, $1)=no
+      _LT_AC_TAGVAR(hardcode_automatic, $1)=yes
+      _LT_AC_TAGVAR(hardcode_shlibpath_var, $1)=unsupported
+      _LT_AC_TAGVAR(whole_archive_flag_spec, $1)=''
+      _LT_AC_TAGVAR(link_all_deplibs, $1)=yes
+      _LT_AC_TAGVAR(allow_undefined_flag, $1)="$_lt_dar_allow_undefined"
+      if test "$GXX" = yes ; then
+      output_verbose_link_cmd='echo'
+      _LT_AC_TAGVAR(archive_cmds, $1)="\$CC -dynamiclib \$allow_undefined_flag -o \$lib \$libobjs \$deplibs \$compiler_flags -install_name \$rpath/\$soname \$verstring $_lt_dar_single_mod${_lt_dsymutil}"
+      _LT_AC_TAGVAR(module_cmds, $1)="\$CC \$allow_undefined_flag -o \$lib -bundle \$libobjs \$deplibs \$compiler_flags${_lt_dsymutil}"
+      _LT_AC_TAGVAR(archive_expsym_cmds, $1)="sed 's,^,_,' < \$export_symbols > \$output_objdir/\${libname}-symbols.expsym~\$CC -dynamiclib \$allow_undefined_flag -o \$lib \$libobjs \$deplibs \$compiler_flags -install_name \$rpath/\$soname \$verstring ${_lt_dar_single_mod}${_lt_dar_export_syms}${_lt_dsymutil}"
+      _LT_AC_TAGVAR(module_expsym_cmds, $1)="sed -e 's,^,_,' < \$export_symbols > \$output_objdir/\${libname}-symbols.expsym~\$CC \$allow_undefined_flag -o \$lib -bundle \$libobjs \$deplibs \$compiler_flags${_lt_dar_export_syms}${_lt_dsymutil}"
+      if test "$lt_cv_apple_cc_single_mod" != "yes"; then
+        _LT_AC_TAGVAR(archive_cmds, $1)="\$CC -r -keep_private_externs -nostdlib -o \${lib}-master.o \$libobjs~\$CC -dynamiclib \$allow_undefined_flag -o \$lib \${lib}-master.o \$deplibs \$compiler_flags -install_name \$rpath/\$soname \$verstring${_lt_dsymutil}"
+        _LT_AC_TAGVAR(archive_expsym_cmds, $1)="sed 's,^,_,' < \$export_symbols > \$output_objdir/\${libname}-symbols.expsym~\$CC -r -keep_private_externs -nostdlib -o \${lib}-master.o \$libobjs~\$CC -dynamiclib \$allow_undefined_flag -o \$lib \${lib}-master.o \$deplibs \$compiler_flags -install_name \$rpath/\$soname \$verstring${_lt_dar_export_syms}${_lt_dsymutil}"
+      fi
+      else
+      case $cc_basename in
+        xlc*)
+         output_verbose_link_cmd='echo'
+          _LT_AC_TAGVAR(archive_cmds, $1)='$CC -qmkshrobj ${wl}-single_module $allow_undefined_flag -o $lib $libobjs $deplibs $compiler_flags ${wl}-install_name ${wl}`echo $rpath/$soname` $xlcverstring'
+          _LT_AC_TAGVAR(module_cmds, $1)='$CC $allow_undefined_flag -o $lib -bundle $libobjs $deplibs$compiler_flags'
+          # Don't fix this by using the ld -exported_symbols_list flag, it doesn't exist in older darwin lds
+          _LT_AC_TAGVAR(archive_expsym_cmds, $1)='sed -e "s,#.*,," -e "s,^[    ]*,," -e "s,^\(..*\),_&," < $export_symbols > $output_objdir/${libname}-symbols.expsym~$CC -qmkshrobj ${wl}-single_module $allow_undefined_flag -o $lib $libobjs $deplibs $compiler_flags ${wl}-install_name ${wl}$rpath/$soname $xlcverstring~nmedit -s $output_objdir/${libname}-symbols.expsym ${lib}'
+          _LT_AC_TAGVAR(module_expsym_cmds, $1)='sed -e "s,#.*,," -e "s,^[    ]*,," -e "s,^\(..*\),_&," < $export_symbols > $output_objdir/${libname}-symbols.expsym~$CC $allow_undefined_flag  -o $lib -bundle $libobjs $deplibs$compiler_flags~nmedit -s $output_objdir/${libname}-symbols.expsym ${lib}'
+          ;;
+       *)
+         _LT_AC_TAGVAR(ld_shlibs, $1)=no
+          ;;
+      esac
+      fi
+        ;;
+
+  dgux*)
+    case $cc_basename in
+      ec++*)
+	# FIXME: insert proper C++ library support
+	_LT_AC_TAGVAR(ld_shlibs, $1)=no
+	;;
+      ghcx*)
+	# Green Hills C++ Compiler
+	# FIXME: insert proper C++ library support
+	_LT_AC_TAGVAR(ld_shlibs, $1)=no
+	;;
+      *)
+	# FIXME: insert proper C++ library support
+	_LT_AC_TAGVAR(ld_shlibs, $1)=no
+	;;
+    esac
+    ;;
+  freebsd[[12]]*)
+    # C++ shared libraries reported to be fairly broken before switch to ELF
+    _LT_AC_TAGVAR(ld_shlibs, $1)=no
+    ;;
+  freebsd-elf*)
+    _LT_AC_TAGVAR(archive_cmds_need_lc, $1)=no
+    ;;
+  freebsd* | dragonfly*)
+    # FreeBSD 3 and later use GNU C++ and GNU ld with standard ELF
+    # conventions
+    _LT_AC_TAGVAR(ld_shlibs, $1)=yes
+    ;;
+  gnu*)
+    ;;
+  hpux9*)
+    _LT_AC_TAGVAR(hardcode_libdir_flag_spec, $1)='${wl}+b ${wl}$libdir'
+    _LT_AC_TAGVAR(hardcode_libdir_separator, $1)=:
+    _LT_AC_TAGVAR(export_dynamic_flag_spec, $1)='${wl}-E'
+    _LT_AC_TAGVAR(hardcode_direct, $1)=yes
+    _LT_AC_TAGVAR(hardcode_minus_L, $1)=yes # Not in the search PATH,
+				# but as the default
+				# location of the library.
+
+    case $cc_basename in
+    CC*)
+      # FIXME: insert proper C++ library support
+      _LT_AC_TAGVAR(ld_shlibs, $1)=no
+      ;;
+    aCC*)
+      _LT_AC_TAGVAR(archive_cmds, $1)='$rm $output_objdir/$soname~$CC -b ${wl}+b ${wl}$install_libdir -o $output_objdir/$soname $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags~test $output_objdir/$soname = $lib || mv $output_objdir/$soname $lib'
+      # Commands to make compiler produce verbose output that lists
+      # what "hidden" libraries, object files and flags are used when
+      # linking a shared library.
+      #
+      # There doesn't appear to be a way to prevent this compiler from
+      # explicitly linking system object files so we need to strip them
+      # from the output so that they don't get included in the library
+      # dependencies.
+      output_verbose_link_cmd='templist=`($CC -b $CFLAGS -v conftest.$objext 2>&1) | grep "[[-]]L"`; list=""; for z in $templist; do case $z in conftest.$objext) list="$list $z";; *.$objext);; *) list="$list $z";;esac; done; echo $list'
+      ;;
+    *)
+      if test "$GXX" = yes; then
+        _LT_AC_TAGVAR(archive_cmds, $1)='$rm $output_objdir/$soname~$CC -shared -nostdlib -fPIC ${wl}+b ${wl}$install_libdir -o $output_objdir/$soname $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags~test $output_objdir/$soname = $lib || mv $output_objdir/$soname $lib'
+      else
+        # FIXME: insert proper C++ library support
+        _LT_AC_TAGVAR(ld_shlibs, $1)=no
+      fi
+      ;;
+    esac
+    ;;
+  hpux10*|hpux11*)
+    if test $with_gnu_ld = no; then
+      _LT_AC_TAGVAR(hardcode_libdir_flag_spec, $1)='${wl}+b ${wl}$libdir'
+      _LT_AC_TAGVAR(hardcode_libdir_separator, $1)=:
+
+      case $host_cpu in
+      hppa*64*|ia64*) ;;
+      *)
+	_LT_AC_TAGVAR(export_dynamic_flag_spec, $1)='${wl}-E'
+        ;;
+      esac
+    fi
+    case $host_cpu in
+    hppa*64*|ia64*)
+      _LT_AC_TAGVAR(hardcode_direct, $1)=no
+      _LT_AC_TAGVAR(hardcode_shlibpath_var, $1)=no
+      ;;
+    *)
+      _LT_AC_TAGVAR(hardcode_direct, $1)=yes
+      _LT_AC_TAGVAR(hardcode_minus_L, $1)=yes # Not in the search PATH,
+					      # but as the default
+					      # location of the library.
+      ;;
+    esac
+
+    case $cc_basename in
+      CC*)
+	# FIXME: insert proper C++ library support
+	_LT_AC_TAGVAR(ld_shlibs, $1)=no
+	;;
+      aCC*)
+	case $host_cpu in
+	hppa*64*)
+	  _LT_AC_TAGVAR(archive_cmds, $1)='$CC -b ${wl}+h ${wl}$soname -o $lib $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags'
+	  ;;
+	ia64*)
+	  _LT_AC_TAGVAR(archive_cmds, $1)='$CC -b ${wl}+h ${wl}$soname ${wl}+nodefaultrpath -o $lib $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags'
+	  ;;
+	*)
+	  _LT_AC_TAGVAR(archive_cmds, $1)='$CC -b ${wl}+h ${wl}$soname ${wl}+b ${wl}$install_libdir -o $lib $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags'
+	  ;;
+	esac
+	# Commands to make compiler produce verbose output that lists
+	# what "hidden" libraries, object files and flags are used when
+	# linking a shared library.
+	#
+	# There doesn't appear to be a way to prevent this compiler from
+	# explicitly linking system object files so we need to strip them
+	# from the output so that they don't get included in the library
+	# dependencies.
+	output_verbose_link_cmd='templist=`($CC -b $CFLAGS -v conftest.$objext 2>&1) | grep "\-L"`; list=""; for z in $templist; do case $z in conftest.$objext) list="$list $z";; *.$objext);; *) list="$list $z";;esac; done; echo $list'
+	;;
+      *)
+	if test "$GXX" = yes; then
+	  if test $with_gnu_ld = no; then
+	    case $host_cpu in
+	    hppa*64*)
+	      _LT_AC_TAGVAR(archive_cmds, $1)='$CC -shared -nostdlib -fPIC ${wl}+h ${wl}$soname -o $lib $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags'
+	      ;;
+	    ia64*)
+	      _LT_AC_TAGVAR(archive_cmds, $1)='$CC -shared -nostdlib -fPIC ${wl}+h ${wl}$soname ${wl}+nodefaultrpath -o $lib $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags'
+	      ;;
+	    *)
+	      _LT_AC_TAGVAR(archive_cmds, $1)='$CC -shared -nostdlib -fPIC ${wl}+h ${wl}$soname ${wl}+b ${wl}$install_libdir -o $lib $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags'
+	      ;;
+	    esac
+	  fi
+	else
+	  # FIXME: insert proper C++ library support
+	  _LT_AC_TAGVAR(ld_shlibs, $1)=no
+	fi
+	;;
+    esac
+    ;;
+  interix[[3-9]]*)
+    _LT_AC_TAGVAR(hardcode_direct, $1)=no
+    _LT_AC_TAGVAR(hardcode_shlibpath_var, $1)=no
+    _LT_AC_TAGVAR(hardcode_libdir_flag_spec, $1)='${wl}-rpath,$libdir'
+    _LT_AC_TAGVAR(export_dynamic_flag_spec, $1)='${wl}-E'
+    # Hack: On Interix 3.x, we cannot compile PIC because of a broken gcc.
+    # Instead, shared libraries are loaded at an image base (0x10000000 by
+    # default) and relocated if they conflict, which is a slow very memory
+    # consuming and fragmenting process.  To avoid this, we pick a random,
+    # 256 KiB-aligned image base between 0x50000000 and 0x6FFC0000 at link
+    # time.  Moving up from 0x10000000 also allows more sbrk(2) space.
+    _LT_AC_TAGVAR(archive_cmds, $1)='$CC -shared $pic_flag $libobjs $deplibs $compiler_flags ${wl}-h,$soname ${wl}--image-base,`expr ${RANDOM-$$} % 4096 / 2 \* 262144 + 1342177280` -o $lib'
+    _LT_AC_TAGVAR(archive_expsym_cmds, $1)='sed "s,^,_," $export_symbols >$output_objdir/$soname.expsym~$CC -shared $pic_flag $libobjs $deplibs $compiler_flags ${wl}-h,$soname ${wl}--retain-symbols-file,$output_objdir/$soname.expsym ${wl}--image-base,`expr ${RANDOM-$$} % 4096 / 2 \* 262144 + 1342177280` -o $lib'
+    ;;
+  irix5* | irix6*)
+    case $cc_basename in
+      CC*)
+	# SGI C++
+	_LT_AC_TAGVAR(archive_cmds, $1)='$CC -shared -all -multigot $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags -soname $soname `test -n "$verstring" && echo -set_version $verstring` -update_registry ${output_objdir}/so_locations -o $lib'
+
+	# Archives containing C++ object files must be created using
+	# "CC -ar", where "CC" is the IRIX C++ compiler.  This is
+	# necessary to make sure instantiated templates are included
+	# in the archive.
+	_LT_AC_TAGVAR(old_archive_cmds, $1)='$CC -ar -WR,-u -o $oldlib $oldobjs'
+	;;
+      *)
+	if test "$GXX" = yes; then
+	  if test "$with_gnu_ld" = no; then
+	    _LT_AC_TAGVAR(archive_cmds, $1)='$CC -shared -nostdlib $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags ${wl}-soname ${wl}$soname `test -n "$verstring" && echo ${wl}-set_version ${wl}$verstring` ${wl}-update_registry ${wl}${output_objdir}/so_locations -o $lib'
+	  else
+	    _LT_AC_TAGVAR(archive_cmds, $1)='$CC -shared -nostdlib $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags ${wl}-soname ${wl}$soname `test -n "$verstring" && echo ${wl}-set_version ${wl}$verstring` -o $lib'
+	  fi
+	fi
+	_LT_AC_TAGVAR(link_all_deplibs, $1)=yes
+	;;
+    esac
+    _LT_AC_TAGVAR(hardcode_libdir_flag_spec, $1)='${wl}-rpath ${wl}$libdir'
+    _LT_AC_TAGVAR(hardcode_libdir_separator, $1)=:
+    ;;
+  linux* | k*bsd*-gnu)
+    case $cc_basename in
+      KCC*)
+	# Kuck and Associates, Inc. (KAI) C++ Compiler
+
+	# KCC will only create a shared library if the output file
+	# ends with ".so" (or ".sl" for HP-UX), so rename the library
+	# to its proper name (with version) after linking.
+	_LT_AC_TAGVAR(archive_cmds, $1)='tempext=`echo $shared_ext | $SED -e '\''s/\([[^()0-9A-Za-z{}]]\)/\\\\\1/g'\''`; templib=`echo $lib | $SED -e "s/\${tempext}\..*/.so/"`; $CC $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags --soname $soname -o \$templib; mv \$templib $lib'
+	_LT_AC_TAGVAR(archive_expsym_cmds, $1)='tempext=`echo $shared_ext | $SED -e '\''s/\([[^()0-9A-Za-z{}]]\)/\\\\\1/g'\''`; templib=`echo $lib | $SED -e "s/\${tempext}\..*/.so/"`; $CC $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags --soname $soname -o \$templib ${wl}-retain-symbols-file,$export_symbols; mv \$templib $lib'
+	# Commands to make compiler produce verbose output that lists
+	# what "hidden" libraries, object files and flags are used when
+	# linking a shared library.
+	#
+	# There doesn't appear to be a way to prevent this compiler from
+	# explicitly linking system object files so we need to strip them
+	# from the output so that they don't get included in the library
+	# dependencies.
+	output_verbose_link_cmd='templist=`$CC $CFLAGS -v conftest.$objext -o libconftest$shared_ext 2>&1 | grep "ld"`; rm -f libconftest$shared_ext; list=""; for z in $templist; do case $z in conftest.$objext) list="$list $z";; *.$objext);; *) list="$list $z";;esac; done; echo $list'
+
+	_LT_AC_TAGVAR(hardcode_libdir_flag_spec, $1)='${wl}--rpath,$libdir'
+	_LT_AC_TAGVAR(export_dynamic_flag_spec, $1)='${wl}--export-dynamic'
+
+	# Archives containing C++ object files must be created using
+	# "CC -Bstatic", where "CC" is the KAI C++ compiler.
+	_LT_AC_TAGVAR(old_archive_cmds, $1)='$CC -Bstatic -o $oldlib $oldobjs'
+	;;
+      icpc*)
+	# Intel C++
+	with_gnu_ld=yes
+	# version 8.0 and above of icpc choke on multiply defined symbols
+	# if we add $predep_objects and $postdep_objects, however 7.1 and
+	# earlier do not add the objects themselves.
+	case `$CC -V 2>&1` in
+	*"Version 7."*)
+  	  _LT_AC_TAGVAR(archive_cmds, $1)='$CC -shared $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags ${wl}-soname $wl$soname -o $lib'
+  	  _LT_AC_TAGVAR(archive_expsym_cmds, $1)='$CC -shared $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags ${wl}-soname $wl$soname ${wl}-retain-symbols-file $wl$export_symbols -o $lib'
+	  ;;
+	*)  # Version 8.0 or newer
+	  tmp_idyn=
+	  case $host_cpu in
+	    ia64*) tmp_idyn=' -i_dynamic';;
+	  esac
+  	  _LT_AC_TAGVAR(archive_cmds, $1)='$CC -shared'"$tmp_idyn"' $libobjs $deplibs $compiler_flags ${wl}-soname $wl$soname -o $lib'
+	  _LT_AC_TAGVAR(archive_expsym_cmds, $1)='$CC -shared'"$tmp_idyn"' $libobjs $deplibs $compiler_flags ${wl}-soname $wl$soname ${wl}-retain-symbols-file $wl$export_symbols -o $lib'
+	  ;;
+	esac
+	_LT_AC_TAGVAR(archive_cmds_need_lc, $1)=no
+	_LT_AC_TAGVAR(hardcode_libdir_flag_spec, $1)='${wl}-rpath,$libdir'
+	_LT_AC_TAGVAR(export_dynamic_flag_spec, $1)='${wl}--export-dynamic'
+	_LT_AC_TAGVAR(whole_archive_flag_spec, $1)='${wl}--whole-archive$convenience ${wl}--no-whole-archive'
+	;;
+      pgCC* | pgcpp*)
+        # Portland Group C++ compiler
+	_LT_AC_TAGVAR(archive_cmds, $1)='$CC -shared $pic_flag $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags ${wl}-soname ${wl}$soname -o $lib'
+  	_LT_AC_TAGVAR(archive_expsym_cmds, $1)='$CC -shared $pic_flag $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags ${wl}-soname ${wl}$soname ${wl}-retain-symbols-file ${wl}$export_symbols -o $lib'
+
+	_LT_AC_TAGVAR(hardcode_libdir_flag_spec, $1)='${wl}--rpath ${wl}$libdir'
+	_LT_AC_TAGVAR(export_dynamic_flag_spec, $1)='${wl}--export-dynamic'
+	_LT_AC_TAGVAR(whole_archive_flag_spec, $1)='${wl}--whole-archive`for conv in $convenience\"\"; do test  -n \"$conv\" && new_convenience=\"$new_convenience,$conv\"; done; $echo \"$new_convenience\"` ${wl}--no-whole-archive'
+        ;;
+      cxx*)
+	# Compaq C++
+	_LT_AC_TAGVAR(archive_cmds, $1)='$CC -shared $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags ${wl}-soname $wl$soname -o $lib'
+	_LT_AC_TAGVAR(archive_expsym_cmds, $1)='$CC -shared $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags ${wl}-soname $wl$soname  -o $lib ${wl}-retain-symbols-file $wl$export_symbols'
+
+	runpath_var=LD_RUN_PATH
+	_LT_AC_TAGVAR(hardcode_libdir_flag_spec, $1)='-rpath $libdir'
+	_LT_AC_TAGVAR(hardcode_libdir_separator, $1)=:
+
+	# Commands to make compiler produce verbose output that lists
+	# what "hidden" libraries, object files and flags are used when
+	# linking a shared library.
+	#
+	# There doesn't appear to be a way to prevent this compiler from
+	# explicitly linking system object files so we need to strip them
+	# from the output so that they don't get included in the library
+	# dependencies.
+	output_verbose_link_cmd='templist=`$CC -shared $CFLAGS -v conftest.$objext 2>&1 | grep "ld"`; templist=`echo $templist | $SED "s/\(^.*ld.*\)\( .*ld .*$\)/\1/"`; list=""; for z in $templist; do case $z in conftest.$objext) list="$list $z";; *.$objext);; *) list="$list $z";;esac; done; echo $list'
+	;;
+      *)
+	case `$CC -V 2>&1 | sed 5q` in
+	*Sun\ C*)
+	  # Sun C++ 5.9
+	  _LT_AC_TAGVAR(no_undefined_flag, $1)=' -zdefs'
+	  _LT_AC_TAGVAR(archive_cmds, $1)='$CC -G${allow_undefined_flag} -h$soname -o $lib $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags'
+	  _LT_AC_TAGVAR(archive_expsym_cmds, $1)='$CC -G${allow_undefined_flag} -h$soname -o $lib $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags ${wl}-retain-symbols-file ${wl}$export_symbols'
+	  _LT_AC_TAGVAR(hardcode_libdir_flag_spec, $1)='-R$libdir'
+	  _LT_AC_TAGVAR(whole_archive_flag_spec, $1)='${wl}--whole-archive`new_convenience=; for conv in $convenience\"\"; do test -z \"$conv\" || new_convenience=\"$new_convenience,$conv\"; done; $echo \"$new_convenience\"` ${wl}--no-whole-archive'
+
+	  # Not sure whether something based on
+	  # $CC $CFLAGS -v conftest.$objext -o libconftest$shared_ext 2>&1
+	  # would be better.
+	  output_verbose_link_cmd='echo'
+
+	  # Archives containing C++ object files must be created using
+	  # "CC -xar", where "CC" is the Sun C++ compiler.  This is
+	  # necessary to make sure instantiated templates are included
+	  # in the archive.
+	  _LT_AC_TAGVAR(old_archive_cmds, $1)='$CC -xar -o $oldlib $oldobjs'
+	  ;;
+	esac
+	;;
+    esac
+    ;;
+  lynxos*)
+    # FIXME: insert proper C++ library support
+    _LT_AC_TAGVAR(ld_shlibs, $1)=no
+    ;;
+  m88k*)
+    # FIXME: insert proper C++ library support
+    _LT_AC_TAGVAR(ld_shlibs, $1)=no
+    ;;
+  mvs*)
+    case $cc_basename in
+      cxx*)
+	# FIXME: insert proper C++ library support
+	_LT_AC_TAGVAR(ld_shlibs, $1)=no
+	;;
+      *)
+	# FIXME: insert proper C++ library support
+	_LT_AC_TAGVAR(ld_shlibs, $1)=no
+	;;
+    esac
+    ;;
+  netbsd*)
+    if echo __ELF__ | $CC -E - | grep __ELF__ >/dev/null; then
+      _LT_AC_TAGVAR(archive_cmds, $1)='$LD -Bshareable  -o $lib $predep_objects $libobjs $deplibs $postdep_objects $linker_flags'
+      wlarc=
+      _LT_AC_TAGVAR(hardcode_libdir_flag_spec, $1)='-R$libdir'
+      _LT_AC_TAGVAR(hardcode_direct, $1)=yes
+      _LT_AC_TAGVAR(hardcode_shlibpath_var, $1)=no
+    fi
+    # Workaround some broken pre-1.5 toolchains
+    output_verbose_link_cmd='$CC -shared $CFLAGS -v conftest.$objext 2>&1 | grep conftest.$objext | $SED -e "s:-lgcc -lc -lgcc::"'
+    ;;
+  openbsd2*)
+    # C++ shared libraries are fairly broken
+    _LT_AC_TAGVAR(ld_shlibs, $1)=no
+    ;;
+  openbsd*)
+    if test -f /usr/libexec/ld.so; then
+      _LT_AC_TAGVAR(hardcode_direct, $1)=yes
+      _LT_AC_TAGVAR(hardcode_shlibpath_var, $1)=no
+      _LT_AC_TAGVAR(archive_cmds, $1)='$CC -shared $pic_flag $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags -o $lib'
+      _LT_AC_TAGVAR(hardcode_libdir_flag_spec, $1)='${wl}-rpath,$libdir'
+      if test -z "`echo __ELF__ | $CC -E - | grep __ELF__`" || test "$host_os-$host_cpu" = "openbsd2.8-powerpc"; then
+	_LT_AC_TAGVAR(archive_expsym_cmds, $1)='$CC -shared $pic_flag $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags ${wl}-retain-symbols-file,$export_symbols -o $lib'
+	_LT_AC_TAGVAR(export_dynamic_flag_spec, $1)='${wl}-E'
+	_LT_AC_TAGVAR(whole_archive_flag_spec, $1)="$wlarc"'--whole-archive$convenience '"$wlarc"'--no-whole-archive'
+      fi
+      output_verbose_link_cmd='echo'
+    else
+      _LT_AC_TAGVAR(ld_shlibs, $1)=no
+    fi
+    ;;
+  osf3*)
+    case $cc_basename in
+      KCC*)
+	# Kuck and Associates, Inc. (KAI) C++ Compiler
+
+	# KCC will only create a shared library if the output file
+	# ends with ".so" (or ".sl" for HP-UX), so rename the library
+	# to its proper name (with version) after linking.
+	_LT_AC_TAGVAR(archive_cmds, $1)='tempext=`echo $shared_ext | $SED -e '\''s/\([[^()0-9A-Za-z{}]]\)/\\\\\1/g'\''`; templib=`echo $lib | $SED -e "s/\${tempext}\..*/.so/"`; $CC $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags --soname $soname -o \$templib; mv \$templib $lib'
+
+	_LT_AC_TAGVAR(hardcode_libdir_flag_spec, $1)='${wl}-rpath,$libdir'
+	_LT_AC_TAGVAR(hardcode_libdir_separator, $1)=:
+
+	# Archives containing C++ object files must be created using
+	# "CC -Bstatic", where "CC" is the KAI C++ compiler.
+	_LT_AC_TAGVAR(old_archive_cmds, $1)='$CC -Bstatic -o $oldlib $oldobjs'
+
+	;;
+      RCC*)
+	# Rational C++ 2.4.1
+	# FIXME: insert proper C++ library support
+	_LT_AC_TAGVAR(ld_shlibs, $1)=no
+	;;
+      cxx*)
+	_LT_AC_TAGVAR(allow_undefined_flag, $1)=' ${wl}-expect_unresolved ${wl}\*'
+	_LT_AC_TAGVAR(archive_cmds, $1)='$CC -shared${allow_undefined_flag} $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags ${wl}-soname $soname `test -n "$verstring" && echo ${wl}-set_version $verstring` -update_registry ${output_objdir}/so_locations -o $lib'
+
+	_LT_AC_TAGVAR(hardcode_libdir_flag_spec, $1)='${wl}-rpath ${wl}$libdir'
+	_LT_AC_TAGVAR(hardcode_libdir_separator, $1)=:
+
+	# Commands to make compiler produce verbose output that lists
+	# what "hidden" libraries, object files and flags are used when
+	# linking a shared library.
+	#
+	# There doesn't appear to be a way to prevent this compiler from
+	# explicitly linking system object files so we need to strip them
+	# from the output so that they don't get included in the library
+	# dependencies.
+	output_verbose_link_cmd='templist=`$CC -shared $CFLAGS -v conftest.$objext 2>&1 | grep "ld" | grep -v "ld:"`; templist=`echo $templist | $SED "s/\(^.*ld.*\)\( .*ld.*$\)/\1/"`; list=""; for z in $templist; do case $z in conftest.$objext) list="$list $z";; *.$objext);; *) list="$list $z";;esac; done; echo $list'
+	;;
+      *)
+	if test "$GXX" = yes && test "$with_gnu_ld" = no; then
+	  _LT_AC_TAGVAR(allow_undefined_flag, $1)=' ${wl}-expect_unresolved ${wl}\*'
+	  _LT_AC_TAGVAR(archive_cmds, $1)='$CC -shared -nostdlib ${allow_undefined_flag} $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags ${wl}-soname ${wl}$soname `test -n "$verstring" && echo ${wl}-set_version ${wl}$verstring` ${wl}-update_registry ${wl}${output_objdir}/so_locations -o $lib'
+
+	  _LT_AC_TAGVAR(hardcode_libdir_flag_spec, $1)='${wl}-rpath ${wl}$libdir'
+	  _LT_AC_TAGVAR(hardcode_libdir_separator, $1)=:
+
+	  # Commands to make compiler produce verbose output that lists
+	  # what "hidden" libraries, object files and flags are used when
+	  # linking a shared library.
+	  output_verbose_link_cmd='$CC -shared $CFLAGS -v conftest.$objext 2>&1 | grep "\-L"'
+
+	else
+	  # FIXME: insert proper C++ library support
+	  _LT_AC_TAGVAR(ld_shlibs, $1)=no
+	fi
+	;;
+    esac
+    ;;
+  osf4* | osf5*)
+    case $cc_basename in
+      KCC*)
+	# Kuck and Associates, Inc. (KAI) C++ Compiler
+
+	# KCC will only create a shared library if the output file
+	# ends with ".so" (or ".sl" for HP-UX), so rename the library
+	# to its proper name (with version) after linking.
+	_LT_AC_TAGVAR(archive_cmds, $1)='tempext=`echo $shared_ext | $SED -e '\''s/\([[^()0-9A-Za-z{}]]\)/\\\\\1/g'\''`; templib=`echo $lib | $SED -e "s/\${tempext}\..*/.so/"`; $CC $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags --soname $soname -o \$templib; mv \$templib $lib'
+
+	_LT_AC_TAGVAR(hardcode_libdir_flag_spec, $1)='${wl}-rpath,$libdir'
+	_LT_AC_TAGVAR(hardcode_libdir_separator, $1)=:
+
+	# Archives containing C++ object files must be created using
+	# the KAI C++ compiler.
+	_LT_AC_TAGVAR(old_archive_cmds, $1)='$CC -o $oldlib $oldobjs'
+	;;
+      RCC*)
+	# Rational C++ 2.4.1
+	# FIXME: insert proper C++ library support
+	_LT_AC_TAGVAR(ld_shlibs, $1)=no
+	;;
+      cxx*)
+	_LT_AC_TAGVAR(allow_undefined_flag, $1)=' -expect_unresolved \*'
+	_LT_AC_TAGVAR(archive_cmds, $1)='$CC -shared${allow_undefined_flag} $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags -msym -soname $soname `test -n "$verstring" && echo -set_version $verstring` -update_registry ${output_objdir}/so_locations -o $lib'
+	_LT_AC_TAGVAR(archive_expsym_cmds, $1)='for i in `cat $export_symbols`; do printf "%s %s\\n" -exported_symbol "\$i" >> $lib.exp; done~
+	  echo "-hidden">> $lib.exp~
+	  $CC -shared$allow_undefined_flag $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags -msym -soname $soname -Wl,-input -Wl,$lib.exp  `test -n "$verstring" && echo -set_version	$verstring` -update_registry ${output_objdir}/so_locations -o $lib~
+	  $rm $lib.exp'
+
+	_LT_AC_TAGVAR(hardcode_libdir_flag_spec, $1)='-rpath $libdir'
+	_LT_AC_TAGVAR(hardcode_libdir_separator, $1)=:
+
+	# Commands to make compiler produce verbose output that lists
+	# what "hidden" libraries, object files and flags are used when
+	# linking a shared library.
+	#
+	# There doesn't appear to be a way to prevent this compiler from
+	# explicitly linking system object files so we need to strip them
+	# from the output so that they don't get included in the library
+	# dependencies.
+	output_verbose_link_cmd='templist=`$CC -shared $CFLAGS -v conftest.$objext 2>&1 | grep "ld" | grep -v "ld:"`; templist=`echo $templist | $SED "s/\(^.*ld.*\)\( .*ld.*$\)/\1/"`; list=""; for z in $templist; do case $z in conftest.$objext) list="$list $z";; *.$objext);; *) list="$list $z";;esac; done; echo $list'
+	;;
+      *)
+	if test "$GXX" = yes && test "$with_gnu_ld" = no; then
+	  _LT_AC_TAGVAR(allow_undefined_flag, $1)=' ${wl}-expect_unresolved ${wl}\*'
+	 _LT_AC_TAGVAR(archive_cmds, $1)='$CC -shared -nostdlib ${allow_undefined_flag} $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags ${wl}-msym ${wl}-soname ${wl}$soname `test -n "$verstring" && echo ${wl}-set_version ${wl}$verstring` ${wl}-update_registry ${wl}${output_objdir}/so_locations -o $lib'
+
+	  _LT_AC_TAGVAR(hardcode_libdir_flag_spec, $1)='${wl}-rpath ${wl}$libdir'
+	  _LT_AC_TAGVAR(hardcode_libdir_separator, $1)=:
+
+	  # Commands to make compiler produce verbose output that lists
+	  # what "hidden" libraries, object files and flags are used when
+	  # linking a shared library.
+	  output_verbose_link_cmd='$CC -shared $CFLAGS -v conftest.$objext 2>&1 | grep "\-L"'
+
+	else
+	  # FIXME: insert proper C++ library support
+	  _LT_AC_TAGVAR(ld_shlibs, $1)=no
+	fi
+	;;
+    esac
+    ;;
+  psos*)
+    # FIXME: insert proper C++ library support
+    _LT_AC_TAGVAR(ld_shlibs, $1)=no
+    ;;
+  sunos4*)
+    case $cc_basename in
+      CC*)
+	# Sun C++ 4.x
+	# FIXME: insert proper C++ library support
+	_LT_AC_TAGVAR(ld_shlibs, $1)=no
+	;;
+      lcc*)
+	# Lucid
+	# FIXME: insert proper C++ library support
+	_LT_AC_TAGVAR(ld_shlibs, $1)=no
+	;;
+      *)
+	# FIXME: insert proper C++ library support
+	_LT_AC_TAGVAR(ld_shlibs, $1)=no
+	;;
+    esac
+    ;;
+  solaris*)
+    case $cc_basename in
+      CC*)
+	# Sun C++ 4.2, 5.x and Centerline C++
+        _LT_AC_TAGVAR(archive_cmds_need_lc,$1)=yes
+	_LT_AC_TAGVAR(no_undefined_flag, $1)=' -zdefs'
+	_LT_AC_TAGVAR(archive_cmds, $1)='$CC -G${allow_undefined_flag}  -h$soname -o $lib $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags'
+	_LT_AC_TAGVAR(archive_expsym_cmds, $1)='$echo "{ global:" > $lib.exp~cat $export_symbols | $SED -e "s/\(.*\)/\1;/" >> $lib.exp~$echo "local: *; };" >> $lib.exp~
+	$CC -G${allow_undefined_flag}  ${wl}-M ${wl}$lib.exp -h$soname -o $lib $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags~$rm $lib.exp'
+
+	_LT_AC_TAGVAR(hardcode_libdir_flag_spec, $1)='-R$libdir'
+	_LT_AC_TAGVAR(hardcode_shlibpath_var, $1)=no
+	case $host_os in
+	  solaris2.[[0-5]] | solaris2.[[0-5]].*) ;;
+	  *)
+	    # The compiler driver will combine and reorder linker options,
+	    # but understands `-z linker_flag'.
+	    # Supported since Solaris 2.6 (maybe 2.5.1?)
+	    _LT_AC_TAGVAR(whole_archive_flag_spec, $1)='-z allextract$convenience -z defaultextract'
+	    ;;
+	esac
+	_LT_AC_TAGVAR(link_all_deplibs, $1)=yes
+
+	output_verbose_link_cmd='echo'
+
+	# Archives containing C++ object files must be created using
+	# "CC -xar", where "CC" is the Sun C++ compiler.  This is
+	# necessary to make sure instantiated templates are included
+	# in the archive.
+	_LT_AC_TAGVAR(old_archive_cmds, $1)='$CC -xar -o $oldlib $oldobjs'
+	;;
+      gcx*)
+	# Green Hills C++ Compiler
+	_LT_AC_TAGVAR(archive_cmds, $1)='$CC -shared $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags ${wl}-h $wl$soname -o $lib'
+
+	# The C++ compiler must be used to create the archive.
+	_LT_AC_TAGVAR(old_archive_cmds, $1)='$CC $LDFLAGS -archive -o $oldlib $oldobjs'
+	;;
+      *)
+	# GNU C++ compiler with Solaris linker
+	if test "$GXX" = yes && test "$with_gnu_ld" = no; then
+	  _LT_AC_TAGVAR(no_undefined_flag, $1)=' ${wl}-z ${wl}defs'
+	  if $CC --version | grep -v '^2\.7' > /dev/null; then
+	    _LT_AC_TAGVAR(archive_cmds, $1)='$CC -shared -nostdlib $LDFLAGS $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags ${wl}-h $wl$soname -o $lib'
+	    _LT_AC_TAGVAR(archive_expsym_cmds, $1)='$echo "{ global:" > $lib.exp~cat $export_symbols | $SED -e "s/\(.*\)/\1;/" >> $lib.exp~$echo "local: *; };" >> $lib.exp~
+		$CC -shared -nostdlib ${wl}-M $wl$lib.exp -o $lib $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags~$rm $lib.exp'
+
+	    # Commands to make compiler produce verbose output that lists
+	    # what "hidden" libraries, object files and flags are used when
+	    # linking a shared library.
+	    output_verbose_link_cmd="$CC -shared $CFLAGS -v conftest.$objext 2>&1 | grep \"\-L\""
+	  else
+	    # g++ 2.7 appears to require `-G' NOT `-shared' on this
+	    # platform.
+	    _LT_AC_TAGVAR(archive_cmds, $1)='$CC -G -nostdlib $LDFLAGS $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags ${wl}-h $wl$soname -o $lib'
+	    _LT_AC_TAGVAR(archive_expsym_cmds, $1)='$echo "{ global:" > $lib.exp~cat $export_symbols | $SED -e "s/\(.*\)/\1;/" >> $lib.exp~$echo "local: *; };" >> $lib.exp~
+		$CC -G -nostdlib ${wl}-M $wl$lib.exp -o $lib $predep_objects $libobjs $deplibs $postdep_objects $compiler_flags~$rm $lib.exp'
+
+	    # Commands to make compiler produce verbose output that lists
+	    # what "hidden" libraries, object files and flags are used when
+	    # linking a shared library.
+	    output_verbose_link_cmd="$CC -G $CFLAGS -v conftest.$objext 2>&1 | grep \"\-L\""
+	  fi
+
+	  _LT_AC_TAGVAR(hardcode_libdir_flag_spec, $1)='${wl}-R $wl$libdir'
+	  case $host_os in
+	  solaris2.[[0-5]] | solaris2.[[0-5]].*) ;;
+	  *)
+	    _LT_AC_TAGVAR(whole_archive_flag_spec, $1)='${wl}-z ${wl}allextract$convenience ${wl}-z ${wl}defaultextract'
+	    ;;
+	  esac
+	fi
+	;;
+    esac
+    ;;
+  sysv4*uw2* | sysv5OpenUNIX* | sysv5UnixWare7.[[01]].[[10]]* | unixware7* | sco3.2v5.0.[[024]]*)
+    _LT_AC_TAGVAR(no_undefined_flag, $1)='${wl}-z,text'
+    _LT_AC_TAGVAR(archive_cmds_need_lc, $1)=no
+    _LT_AC_TAGVAR(hardcode_shlibpath_var, $1)=no
+    runpath_var='LD_RUN_PATH'
+
+    case $cc_basename in
+      CC*)
+	_LT_AC_TAGVAR(archive_cmds, $1)='$CC -G ${wl}-h,$soname -o $lib $libobjs $deplibs $compiler_flags'
+	_LT_AC_TAGVAR(archive_expsym_cmds, $1)='$CC -G ${wl}-Bexport:$export_symbols ${wl}-h,$soname -o $lib $libobjs $deplibs $compiler_flags'
+	;;
+      *)
+	_LT_AC_TAGVAR(archive_cmds, $1)='$CC -shared ${wl}-h,$soname -o $lib $libobjs $deplibs $compiler_flags'
+	_LT_AC_TAGVAR(archive_expsym_cmds, $1)='$CC -shared ${wl}-Bexport:$export_symbols ${wl}-h,$soname -o $lib $libobjs $deplibs $compiler_flags'
+	;;
+    esac
+    ;;
+  sysv5* | sco3.2v5* | sco5v6*)
+    # Note: We can NOT use -z defs as we might desire, because we do not
+    # link with -lc, and that would cause any symbols used from libc to
+    # always be unresolved, which means just about no library would
+    # ever link correctly.  If we're not using GNU ld we use -z text
+    # though, which does catch some bad symbols but isn't as heavy-handed
+    # as -z defs.
+    # For security reasons, it is highly recommended that you always
+    # use absolute paths for naming shared libraries, and exclude the
+    # DT_RUNPATH tag from executables and libraries.  But doing so
+    # requires that you compile everything twice, which is a pain.
+    # So that behaviour is only enabled if SCOABSPATH is set to a
+    # non-empty value in the environment.  Most likely only useful for
+    # creating official distributions of packages.
+    # This is a hack until libtool officially supports absolute path
+    # names for shared libraries.
+    _LT_AC_TAGVAR(no_undefined_flag, $1)='${wl}-z,text'
+    _LT_AC_TAGVAR(allow_undefined_flag, $1)='${wl}-z,nodefs'
+    _LT_AC_TAGVAR(archive_cmds_need_lc, $1)=no
+    _LT_AC_TAGVAR(hardcode_shlibpath_var, $1)=no
+    _LT_AC_TAGVAR(hardcode_libdir_flag_spec, $1)='`test -z "$SCOABSPATH" && echo ${wl}-R,$libdir`'
+    _LT_AC_TAGVAR(hardcode_libdir_separator, $1)=':'
+    _LT_AC_TAGVAR(link_all_deplibs, $1)=yes
+    _LT_AC_TAGVAR(export_dynamic_flag_spec, $1)='${wl}-Bexport'
+    runpath_var='LD_RUN_PATH'
+
+    case $cc_basename in
+      CC*)
+	_LT_AC_TAGVAR(archive_cmds, $1)='$CC -G ${wl}-h,\${SCOABSPATH:+${install_libdir}/}$soname -o $lib $libobjs $deplibs $compiler_flags'
+	_LT_AC_TAGVAR(archive_expsym_cmds, $1)='$CC -G ${wl}-Bexport:$export_symbols ${wl}-h,\${SCOABSPATH:+${install_libdir}/}$soname -o $lib $libobjs $deplibs $compiler_flags'
+	;;
+      *)
+	_LT_AC_TAGVAR(archive_cmds, $1)='$CC -shared ${wl}-h,\${SCOABSPATH:+${install_libdir}/}$soname -o $lib $libobjs $deplibs $compiler_flags'
+	_LT_AC_TAGVAR(archive_expsym_cmds, $1)='$CC -shared ${wl}-Bexport:$export_symbols ${wl}-h,\${SCOABSPATH:+${install_libdir}/}$soname -o $lib $libobjs $deplibs $compiler_flags'
+	;;
+    esac
+    ;;
+  tandem*)
+    case $cc_basename in
+      NCC*)
+	# NonStop-UX NCC 3.20
+	# FIXME: insert proper C++ library support
+	_LT_AC_TAGVAR(ld_shlibs, $1)=no
+	;;
+      *)
+	# FIXME: insert proper C++ library support
+	_LT_AC_TAGVAR(ld_shlibs, $1)=no
+	;;
+    esac
+    ;;
+  vxworks*)
+    # FIXME: insert proper C++ library support
+    _LT_AC_TAGVAR(ld_shlibs, $1)=no
+    ;;
+  *)
+    # FIXME: insert proper C++ library support
+    _LT_AC_TAGVAR(ld_shlibs, $1)=no
+    ;;
+esac
+AC_MSG_RESULT([$_LT_AC_TAGVAR(ld_shlibs, $1)])
+test "$_LT_AC_TAGVAR(ld_shlibs, $1)" = no && can_build_shared=no
+
+_LT_AC_TAGVAR(GCC, $1)="$GXX"
+_LT_AC_TAGVAR(LD, $1)="$LD"
+
+AC_LIBTOOL_POSTDEP_PREDEP($1)
+AC_LIBTOOL_PROG_COMPILER_PIC($1)
+AC_LIBTOOL_PROG_CC_C_O($1)
+AC_LIBTOOL_SYS_HARD_LINK_LOCKS($1)
+AC_LIBTOOL_PROG_LD_SHLIBS($1)
+AC_LIBTOOL_SYS_DYNAMIC_LINKER($1)
+AC_LIBTOOL_PROG_LD_HARDCODE_LIBPATH($1)
+
+AC_LIBTOOL_CONFIG($1)
+
+AC_LANG_POP
+CC=$lt_save_CC
+LDCXX=$LD
+LD=$lt_save_LD
+GCC=$lt_save_GCC
+with_gnu_ldcxx=$with_gnu_ld
+with_gnu_ld=$lt_save_with_gnu_ld
+lt_cv_path_LDCXX=$lt_cv_path_LD
+lt_cv_path_LD=$lt_save_path_LD
+lt_cv_prog_gnu_ldcxx=$lt_cv_prog_gnu_ld
+lt_cv_prog_gnu_ld=$lt_save_with_gnu_ld
+])# AC_LIBTOOL_LANG_CXX_CONFIG
+
+# AC_LIBTOOL_POSTDEP_PREDEP([TAGNAME])
+# ------------------------------------
+# Figure out "hidden" library dependencies from verbose
+# compiler output when linking a shared library.
+# Parse the compiler output and extract the necessary
+# objects, libraries and library flags.
+AC_DEFUN([AC_LIBTOOL_POSTDEP_PREDEP],
+[AC_REQUIRE([LT_AC_PROG_SED])dnl
+dnl we can't use the lt_simple_compile_test_code here,
+dnl because it contains code intended for an executable,
+dnl not a library.  It's possible we should let each
+dnl tag define a new lt_????_link_test_code variable,
+dnl but it's only used here...
+ifelse([$1],[],[cat > conftest.$ac_ext <<EOF
+int a;
+void foo (void) { a = 0; }
+EOF
+],[$1],[CXX],[cat > conftest.$ac_ext <<EOF
+class Foo
+{
+public:
+  Foo (void) { a = 0; }
+private:
+  int a;
+};
+EOF
+],[$1],[F77],[cat > conftest.$ac_ext <<EOF
+      subroutine foo
+      implicit none
+      integer*4 a
+      a=0
+      return
+      end
+EOF
+],[$1],[GCJ],[cat > conftest.$ac_ext <<EOF
+public class foo {
+  private int a;
+  public void bar (void) {
+    a = 0;
+  }
+};
+EOF
+])
+dnl Parse the compiler output and extract the necessary
+dnl objects, libraries and library flags.
+if AC_TRY_EVAL(ac_compile); then
+  # Parse the compiler output and extract the necessary
+  # objects, libraries and library flags.
+
+  # Sentinel used to keep track of whether or not we are before
+  # the conftest object file.
+  pre_test_object_deps_done=no
+
+  # The `*' in the case matches for architectures that use `case' in
+  # $output_verbose_cmd can trigger glob expansion during the loop
+  # eval without this substitution.
+  output_verbose_link_cmd=`$echo "X$output_verbose_link_cmd" | $Xsed -e "$no_glob_subst"`
+
+  for p in `eval $output_verbose_link_cmd`; do
+    case $p in
+
+    -L* | -R* | -l*)
+       # Some compilers place space between "-{L,R}" and the path.
+       # Remove the space.
+       if test $p = "-L" \
+	  || test $p = "-R"; then
+	 prev=$p
+	 continue
+       else
+	 prev=
+       fi
+
+       if test "$pre_test_object_deps_done" = no; then
+	 case $p in
+	 -L* | -R*)
+	   # Internal compiler library paths should come after those
+	   # provided the user.  The postdeps already come after the
+	   # user supplied libs so there is no need to process them.
+	   if test -z "$_LT_AC_TAGVAR(compiler_lib_search_path, $1)"; then
+	     _LT_AC_TAGVAR(compiler_lib_search_path, $1)="${prev}${p}"
+	   else
+	     _LT_AC_TAGVAR(compiler_lib_search_path, $1)="${_LT_AC_TAGVAR(compiler_lib_search_path, $1)} ${prev}${p}"
+	   fi
+	   ;;
+	 # The "-l" case would never come before the object being
+	 # linked, so don't bother handling this case.
+	 esac
+       else
+	 if test -z "$_LT_AC_TAGVAR(postdeps, $1)"; then
+	   _LT_AC_TAGVAR(postdeps, $1)="${prev}${p}"
+	 else
+	   _LT_AC_TAGVAR(postdeps, $1)="${_LT_AC_TAGVAR(postdeps, $1)} ${prev}${p}"
+	 fi
+       fi
+       ;;
+
+    *.$objext)
+       # This assumes that the test object file only shows up
+       # once in the compiler output.
+       if test "$p" = "conftest.$objext"; then
+	 pre_test_object_deps_done=yes
+	 continue
+       fi
+
+       if test "$pre_test_object_deps_done" = no; then
+	 if test -z "$_LT_AC_TAGVAR(predep_objects, $1)"; then
+	   _LT_AC_TAGVAR(predep_objects, $1)="$p"
+	 else
+	   _LT_AC_TAGVAR(predep_objects, $1)="$_LT_AC_TAGVAR(predep_objects, $1) $p"
+	 fi
+       else
+	 if test -z "$_LT_AC_TAGVAR(postdep_objects, $1)"; then
+	   _LT_AC_TAGVAR(postdep_objects, $1)="$p"
+	 else
+	   _LT_AC_TAGVAR(postdep_objects, $1)="$_LT_AC_TAGVAR(postdep_objects, $1) $p"
+	 fi
+       fi
+       ;;
+
+    *) ;; # Ignore the rest.
+
+    esac
+  done
+
+  # Clean up.
+  rm -f a.out a.exe
+else
+  echo "libtool.m4: error: problem compiling $1 test program"
+fi
+
+$rm -f confest.$objext
+
+_LT_AC_TAGVAR(compiler_lib_search_dirs, $1)=
+if test -n "$_LT_AC_TAGVAR(compiler_lib_search_path, $1)"; then
+  _LT_AC_TAGVAR(compiler_lib_search_dirs, $1)=`echo " ${_LT_AC_TAGVAR(compiler_lib_search_path, $1)}" | ${SED} -e 's! -L! !g' -e 's!^ !!'`
+fi
+
+# PORTME: override above test on systems where it is broken
+ifelse([$1],[CXX],
+[case $host_os in
+interix[[3-9]]*)
+  # Interix 3.5 installs completely hosed .la files for C++, so rather than
+  # hack all around it, let's just trust "g++" to DTRT.
+  _LT_AC_TAGVAR(predep_objects,$1)=
+  _LT_AC_TAGVAR(postdep_objects,$1)=
+  _LT_AC_TAGVAR(postdeps,$1)=
+  ;;
+
+linux*)
+  case `$CC -V 2>&1 | sed 5q` in
+  *Sun\ C*)
+    # Sun C++ 5.9
+    #
+    # The more standards-conforming stlport4 library is
+    # incompatible with the Cstd library. Avoid specifying
+    # it if it's in CXXFLAGS. Ignore libCrun as
+    # -library=stlport4 depends on it.
+    case " $CXX $CXXFLAGS " in
+    *" -library=stlport4 "*)
+      solaris_use_stlport4=yes
+      ;;
+    esac
+    if test "$solaris_use_stlport4" != yes; then
+      _LT_AC_TAGVAR(postdeps,$1)='-library=Cstd -library=Crun'
+    fi
+    ;;
+  esac
+  ;;
+
+solaris*)
+  case $cc_basename in
+  CC*)
+    # The more standards-conforming stlport4 library is
+    # incompatible with the Cstd library. Avoid specifying
+    # it if it's in CXXFLAGS. Ignore libCrun as
+    # -library=stlport4 depends on it.
+    case " $CXX $CXXFLAGS " in
+    *" -library=stlport4 "*)
+      solaris_use_stlport4=yes
+      ;;
+    esac
+
+    # Adding this requires a known-good setup of shared libraries for
+    # Sun compiler versions before 5.6, else PIC objects from an old
+    # archive will be linked into the output, leading to subtle bugs.
+    if test "$solaris_use_stlport4" != yes; then
+      _LT_AC_TAGVAR(postdeps,$1)='-library=Cstd -library=Crun'
+    fi
+    ;;
+  esac
+  ;;
+esac
+])
+case " $_LT_AC_TAGVAR(postdeps, $1) " in
+*" -lc "*) _LT_AC_TAGVAR(archive_cmds_need_lc, $1)=no ;;
+esac
+])# AC_LIBTOOL_POSTDEP_PREDEP
+
+# AC_LIBTOOL_LANG_F77_CONFIG
+# --------------------------
+# Ensure that the configuration vars for the C compiler are
+# suitably defined.  Those variables are subsequently used by
+# AC_LIBTOOL_CONFIG to write the compiler configuration to `libtool'.
+AC_DEFUN([AC_LIBTOOL_LANG_F77_CONFIG], [_LT_AC_LANG_F77_CONFIG(F77)])
+AC_DEFUN([_LT_AC_LANG_F77_CONFIG],
+[AC_REQUIRE([AC_PROG_F77])
+AC_LANG_PUSH(Fortran 77)
+
+_LT_AC_TAGVAR(archive_cmds_need_lc, $1)=no
+_LT_AC_TAGVAR(allow_undefined_flag, $1)=
+_LT_AC_TAGVAR(always_export_symbols, $1)=no
+_LT_AC_TAGVAR(archive_expsym_cmds, $1)=
+_LT_AC_TAGVAR(export_dynamic_flag_spec, $1)=
+_LT_AC_TAGVAR(hardcode_direct, $1)=no
+_LT_AC_TAGVAR(hardcode_libdir_flag_spec, $1)=
+_LT_AC_TAGVAR(hardcode_libdir_flag_spec_ld, $1)=
+_LT_AC_TAGVAR(hardcode_libdir_separator, $1)=
+_LT_AC_TAGVAR(hardcode_minus_L, $1)=no
+_LT_AC_TAGVAR(hardcode_automatic, $1)=no
+_LT_AC_TAGVAR(module_cmds, $1)=
+_LT_AC_TAGVAR(module_expsym_cmds, $1)=
+_LT_AC_TAGVAR(link_all_deplibs, $1)=unknown
+_LT_AC_TAGVAR(old_archive_cmds, $1)=$old_archive_cmds
+_LT_AC_TAGVAR(no_undefined_flag, $1)=
+_LT_AC_TAGVAR(whole_archive_flag_spec, $1)=
+_LT_AC_TAGVAR(enable_shared_with_static_runtimes, $1)=no
+
+# Source file extension for f77 test sources.
+ac_ext=f
+
+# Object file extension for compiled f77 test sources.
+objext=o
+_LT_AC_TAGVAR(objext, $1)=$objext
+
+# Code to be used in simple compile tests
+lt_simple_compile_test_code="\
+      subroutine t
+      return
+      end
+"
+
+# Code to be used in simple link tests
+lt_simple_link_test_code="\
+      program t
+      end
+"
+
+# ltmain only uses $CC for tagged configurations so make sure $CC is set.
+_LT_AC_SYS_COMPILER
+
+# save warnings/boilerplate of simple test code
+_LT_COMPILER_BOILERPLATE
+_LT_LINKER_BOILERPLATE
+
+# Allow CC to be a program name with arguments.
+lt_save_CC="$CC"
+CC=${F77-"f77"}
+compiler=$CC
+_LT_AC_TAGVAR(compiler, $1)=$CC
+_LT_CC_BASENAME([$compiler])
+
+AC_MSG_CHECKING([if libtool supports shared libraries])
+AC_MSG_RESULT([$can_build_shared])
+
+AC_MSG_CHECKING([whether to build shared libraries])
+test "$can_build_shared" = "no" && enable_shared=no
+
+# On AIX, shared libraries and static libraries use the same namespace, and
+# are all built from PIC.
+case $host_os in
+aix3*)
+  test "$enable_shared" = yes && enable_static=no
+  if test -n "$RANLIB"; then
+    archive_cmds="$archive_cmds~\$RANLIB \$lib"
+    postinstall_cmds='$RANLIB $lib'
+  fi
+  ;;
+aix[[4-9]]*)
+  if test "$host_cpu" != ia64 && test "$aix_use_runtimelinking" = no ; then
+    test "$enable_shared" = yes && enable_static=no
+  fi
+  ;;
+esac
+AC_MSG_RESULT([$enable_shared])
+
+AC_MSG_CHECKING([whether to build static libraries])
+# Make sure either enable_shared or enable_static is yes.
+test "$enable_shared" = yes || enable_static=yes
+AC_MSG_RESULT([$enable_static])
+
+_LT_AC_TAGVAR(GCC, $1)="$G77"
+_LT_AC_TAGVAR(LD, $1)="$LD"
+
+AC_LIBTOOL_PROG_COMPILER_PIC($1)
+AC_LIBTOOL_PROG_CC_C_O($1)
+AC_LIBTOOL_SYS_HARD_LINK_LOCKS($1)
+AC_LIBTOOL_PROG_LD_SHLIBS($1)
+AC_LIBTOOL_SYS_DYNAMIC_LINKER($1)
+AC_LIBTOOL_PROG_LD_HARDCODE_LIBPATH($1)
+
+AC_LIBTOOL_CONFIG($1)
+
+AC_LANG_POP
+CC="$lt_save_CC"
+])# AC_LIBTOOL_LANG_F77_CONFIG
+
+
+# AC_LIBTOOL_LANG_GCJ_CONFIG
+# --------------------------
+# Ensure that the configuration vars for the C compiler are
+# suitably defined.  Those variables are subsequently used by
+# AC_LIBTOOL_CONFIG to write the compiler configuration to `libtool'.
+AC_DEFUN([AC_LIBTOOL_LANG_GCJ_CONFIG], [_LT_AC_LANG_GCJ_CONFIG(GCJ)])
+AC_DEFUN([_LT_AC_LANG_GCJ_CONFIG],
+[AC_LANG_SAVE
+
+# Source file extension for Java test sources.
+ac_ext=java
+
+# Object file extension for compiled Java test sources.
+objext=o
+_LT_AC_TAGVAR(objext, $1)=$objext
+
+# Code to be used in simple compile tests
+lt_simple_compile_test_code="class foo {}"
+
+# Code to be used in simple link tests
+lt_simple_link_test_code='public class conftest { public static void main(String[[]] argv) {}; }'
+
+# ltmain only uses $CC for tagged configurations so make sure $CC is set.
+_LT_AC_SYS_COMPILER
+
+# save warnings/boilerplate of simple test code
+_LT_COMPILER_BOILERPLATE
+_LT_LINKER_BOILERPLATE
+
+# Allow CC to be a program name with arguments.
+lt_save_CC="$CC"
+CC=${GCJ-"gcj"}
+compiler=$CC
+_LT_AC_TAGVAR(compiler, $1)=$CC
+_LT_CC_BASENAME([$compiler])
+
+# GCJ did not exist at the time GCC didn't implicitly link libc in.
+_LT_AC_TAGVAR(archive_cmds_need_lc, $1)=no
+
+_LT_AC_TAGVAR(old_archive_cmds, $1)=$old_archive_cmds
+
+AC_LIBTOOL_PROG_COMPILER_NO_RTTI($1)
+AC_LIBTOOL_PROG_COMPILER_PIC($1)
+AC_LIBTOOL_PROG_CC_C_O($1)
+AC_LIBTOOL_SYS_HARD_LINK_LOCKS($1)
+AC_LIBTOOL_PROG_LD_SHLIBS($1)
+AC_LIBTOOL_SYS_DYNAMIC_LINKER($1)
+AC_LIBTOOL_PROG_LD_HARDCODE_LIBPATH($1)
+
+AC_LIBTOOL_CONFIG($1)
+
+AC_LANG_RESTORE
+CC="$lt_save_CC"
+])# AC_LIBTOOL_LANG_GCJ_CONFIG
+
+
+# AC_LIBTOOL_LANG_RC_CONFIG
+# -------------------------
+# Ensure that the configuration vars for the Windows resource compiler are
+# suitably defined.  Those variables are subsequently used by
+# AC_LIBTOOL_CONFIG to write the compiler configuration to `libtool'.
+AC_DEFUN([AC_LIBTOOL_LANG_RC_CONFIG], [_LT_AC_LANG_RC_CONFIG(RC)])
+AC_DEFUN([_LT_AC_LANG_RC_CONFIG],
+[AC_LANG_SAVE
+
+# Source file extension for RC test sources.
+ac_ext=rc
+
+# Object file extension for compiled RC test sources.
+objext=o
+_LT_AC_TAGVAR(objext, $1)=$objext
+
+# Code to be used in simple compile tests
+lt_simple_compile_test_code='sample MENU { MENUITEM "&Soup", 100, CHECKED }'
+
+# Code to be used in simple link tests
+lt_simple_link_test_code="$lt_simple_compile_test_code"
+
+# ltmain only uses $CC for tagged configurations so make sure $CC is set.
+_LT_AC_SYS_COMPILER
+
+# save warnings/boilerplate of simple test code
+_LT_COMPILER_BOILERPLATE
+_LT_LINKER_BOILERPLATE
+
+# Allow CC to be a program name with arguments.
+lt_save_CC="$CC"
+CC=${RC-"windres"}
+compiler=$CC
+_LT_AC_TAGVAR(compiler, $1)=$CC
+_LT_CC_BASENAME([$compiler])
+_LT_AC_TAGVAR(lt_cv_prog_compiler_c_o, $1)=yes
+
+AC_LIBTOOL_CONFIG($1)
+
+AC_LANG_RESTORE
+CC="$lt_save_CC"
+])# AC_LIBTOOL_LANG_RC_CONFIG
+
+
+# AC_LIBTOOL_CONFIG([TAGNAME])
+# ----------------------------
+# If TAGNAME is not passed, then create an initial libtool script
+# with a default configuration from the untagged config vars.  Otherwise
+# add code to config.status for appending the configuration named by
+# TAGNAME from the matching tagged config vars.
+AC_DEFUN([AC_LIBTOOL_CONFIG],
+[# The else clause should only fire when bootstrapping the
+# libtool distribution, otherwise you forgot to ship ltmain.sh
+# with your package, and you will get complaints that there are
+# no rules to generate ltmain.sh.
+if test -f "$ltmain"; then
+  # See if we are running on zsh, and set the options which allow our commands through
+  # without removal of \ escapes.
+  if test -n "${ZSH_VERSION+set}" ; then
+    setopt NO_GLOB_SUBST
+  fi
+  # Now quote all the things that may contain metacharacters while being
+  # careful not to overquote the AC_SUBSTed values.  We take copies of the
+  # variables and quote the copies for generation of the libtool script.
+  for var in echo old_CC old_CFLAGS AR AR_FLAGS EGREP RANLIB LN_S LTCC LTCFLAGS NM \
+    SED SHELL STRIP \
+    libname_spec library_names_spec soname_spec extract_expsyms_cmds \
+    old_striplib striplib file_magic_cmd finish_cmds finish_eval \
+    deplibs_check_method reload_flag reload_cmds need_locks \
+    lt_cv_sys_global_symbol_pipe lt_cv_sys_global_symbol_to_cdecl \
+    lt_cv_sys_global_symbol_to_c_name_address \
+    sys_lib_search_path_spec sys_lib_dlsearch_path_spec \
+    old_postinstall_cmds old_postuninstall_cmds \
+    _LT_AC_TAGVAR(compiler, $1) \
+    _LT_AC_TAGVAR(CC, $1) \
+    _LT_AC_TAGVAR(LD, $1) \
+    _LT_AC_TAGVAR(lt_prog_compiler_wl, $1) \
+    _LT_AC_TAGVAR(lt_prog_compiler_pic, $1) \
+    _LT_AC_TAGVAR(lt_prog_compiler_static, $1) \
+    _LT_AC_TAGVAR(lt_prog_compiler_no_builtin_flag, $1) \
+    _LT_AC_TAGVAR(export_dynamic_flag_spec, $1) \
+    _LT_AC_TAGVAR(thread_safe_flag_spec, $1) \
+    _LT_AC_TAGVAR(whole_archive_flag_spec, $1) \
+    _LT_AC_TAGVAR(enable_shared_with_static_runtimes, $1) \
+    _LT_AC_TAGVAR(old_archive_cmds, $1) \
+    _LT_AC_TAGVAR(old_archive_from_new_cmds, $1) \
+    _LT_AC_TAGVAR(predep_objects, $1) \
+    _LT_AC_TAGVAR(postdep_objects, $1) \
+    _LT_AC_TAGVAR(predeps, $1) \
+    _LT_AC_TAGVAR(postdeps, $1) \
+    _LT_AC_TAGVAR(compiler_lib_search_path, $1) \
+    _LT_AC_TAGVAR(compiler_lib_search_dirs, $1) \
+    _LT_AC_TAGVAR(archive_cmds, $1) \
+    _LT_AC_TAGVAR(archive_expsym_cmds, $1) \
+    _LT_AC_TAGVAR(postinstall_cmds, $1) \
+    _LT_AC_TAGVAR(postuninstall_cmds, $1) \
+    _LT_AC_TAGVAR(old_archive_from_expsyms_cmds, $1) \
+    _LT_AC_TAGVAR(allow_undefined_flag, $1) \
+    _LT_AC_TAGVAR(no_undefined_flag, $1) \
+    _LT_AC_TAGVAR(export_symbols_cmds, $1) \
+    _LT_AC_TAGVAR(hardcode_libdir_flag_spec, $1) \
+    _LT_AC_TAGVAR(hardcode_libdir_flag_spec_ld, $1) \
+    _LT_AC_TAGVAR(hardcode_libdir_separator, $1) \
+    _LT_AC_TAGVAR(hardcode_automatic, $1) \
+    _LT_AC_TAGVAR(module_cmds, $1) \
+    _LT_AC_TAGVAR(module_expsym_cmds, $1) \
+    _LT_AC_TAGVAR(lt_cv_prog_compiler_c_o, $1) \
+    _LT_AC_TAGVAR(fix_srcfile_path, $1) \
+    _LT_AC_TAGVAR(exclude_expsyms, $1) \
+    _LT_AC_TAGVAR(include_expsyms, $1); do
+
+    case $var in
+    _LT_AC_TAGVAR(old_archive_cmds, $1) | \
+    _LT_AC_TAGVAR(old_archive_from_new_cmds, $1) | \
+    _LT_AC_TAGVAR(archive_cmds, $1) | \
+    _LT_AC_TAGVAR(archive_expsym_cmds, $1) | \
+    _LT_AC_TAGVAR(module_cmds, $1) | \
+    _LT_AC_TAGVAR(module_expsym_cmds, $1) | \
+    _LT_AC_TAGVAR(old_archive_from_expsyms_cmds, $1) | \
+    _LT_AC_TAGVAR(export_symbols_cmds, $1) | \
+    extract_expsyms_cmds | reload_cmds | finish_cmds | \
+    postinstall_cmds | postuninstall_cmds | \
+    old_postinstall_cmds | old_postuninstall_cmds | \
+    sys_lib_search_path_spec | sys_lib_dlsearch_path_spec)
+      # Double-quote double-evaled strings.
+      eval "lt_$var=\\\"\`\$echo \"X\$$var\" | \$Xsed -e \"\$double_quote_subst\" -e \"\$sed_quote_subst\" -e \"\$delay_variable_subst\"\`\\\""
+      ;;
+    *)
+      eval "lt_$var=\\\"\`\$echo \"X\$$var\" | \$Xsed -e \"\$sed_quote_subst\"\`\\\""
+      ;;
+    esac
+  done
+
+  case $lt_echo in
+  *'\[$]0 --fallback-echo"')
+    lt_echo=`$echo "X$lt_echo" | $Xsed -e 's/\\\\\\\[$]0 --fallback-echo"[$]/[$]0 --fallback-echo"/'`
+    ;;
+  esac
+
+ifelse([$1], [],
+  [cfgfile="${ofile}T"
+  trap "$rm \"$cfgfile\"; exit 1" 1 2 15
+  $rm -f "$cfgfile"
+  AC_MSG_NOTICE([creating $ofile])],
+  [cfgfile="$ofile"])
+
+  cat <<__EOF__ >> "$cfgfile"
+ifelse([$1], [],
+[#! $SHELL
+
+# `$echo "$cfgfile" | sed 's%^.*/%%'` - Provide generalized library-building support services.
+# Generated automatically by $PROGRAM (GNU $PACKAGE $VERSION$TIMESTAMP)
+# NOTE: Changes made to this file will be lost: look at ltmain.sh.
+#
+# Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
+# Free Software Foundation, Inc.
+#
+# This file is part of GNU Libtool:
+# Originally by Gordon Matzigkeit <gord at gnu.ai.mit.edu>, 1996
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# As a special exception to the GNU General Public License, if you
+# distribute this file as part of a program that contains a
+# configuration script generated by Autoconf, you may include it under
+# the same distribution terms that you use for the rest of that program.
+
+# A sed program that does not truncate output.
+SED=$lt_SED
+
+# Sed that helps us avoid accidentally triggering echo(1) options like -n.
+Xsed="$SED -e 1s/^X//"
+
+# The HP-UX ksh and POSIX shell print the target directory to stdout
+# if CDPATH is set.
+(unset CDPATH) >/dev/null 2>&1 && unset CDPATH
+
+# The names of the tagged configurations supported by this script.
+available_tags=
+
+# ### BEGIN LIBTOOL CONFIG],
+[# ### BEGIN LIBTOOL TAG CONFIG: $tagname])
+
+# Libtool was configured on host `(hostname || uname -n) 2>/dev/null | sed 1q`:
+
+# Shell to use when invoking shell scripts.
+SHELL=$lt_SHELL
+
+# Whether or not to build shared libraries.
+build_libtool_libs=$enable_shared
+
+# Whether or not to build static libraries.
+build_old_libs=$enable_static
+
+# Whether or not to add -lc for building shared libraries.
+build_libtool_need_lc=$_LT_AC_TAGVAR(archive_cmds_need_lc, $1)
+
+# Whether or not to disallow shared libs when runtime libs are static
+allow_libtool_libs_with_static_runtimes=$_LT_AC_TAGVAR(enable_shared_with_static_runtimes, $1)
+
+# Whether or not to optimize for fast installation.
+fast_install=$enable_fast_install
+
+# The host system.
+host_alias=$host_alias
+host=$host
+host_os=$host_os
+
+# The build system.
+build_alias=$build_alias
+build=$build
+build_os=$build_os
+
+# An echo program that does not interpret backslashes.
+echo=$lt_echo
+
+# The archiver.
+AR=$lt_AR
+AR_FLAGS=$lt_AR_FLAGS
+
+# A C compiler.
+LTCC=$lt_LTCC
+
+# LTCC compiler flags.
+LTCFLAGS=$lt_LTCFLAGS
+
+# A language-specific compiler.
+CC=$lt_[]_LT_AC_TAGVAR(compiler, $1)
+
+# Is the compiler the GNU C compiler?
+with_gcc=$_LT_AC_TAGVAR(GCC, $1)
+
+# An ERE matcher.
+EGREP=$lt_EGREP
+
+# The linker used to build libraries.
+LD=$lt_[]_LT_AC_TAGVAR(LD, $1)
+
+# Whether we need hard or soft links.
+LN_S=$lt_LN_S
+
+# A BSD-compatible nm program.
+NM=$lt_NM
+
+# A symbol stripping program
+STRIP=$lt_STRIP
+
+# Used to examine libraries when file_magic_cmd begins "file"
+MAGIC_CMD=$MAGIC_CMD
+
+# Used on cygwin: DLL creation program.
+DLLTOOL="$DLLTOOL"
+
+# Used on cygwin: object dumper.
+OBJDUMP="$OBJDUMP"
+
+# Used on cygwin: assembler.
+AS="$AS"
+
+# The name of the directory that contains temporary libtool files.
+objdir=$objdir
+
+# How to create reloadable object files.
+reload_flag=$lt_reload_flag
+reload_cmds=$lt_reload_cmds
+
+# How to pass a linker flag through the compiler.
+wl=$lt_[]_LT_AC_TAGVAR(lt_prog_compiler_wl, $1)
+
+# Object file suffix (normally "o").
+objext="$ac_objext"
+
+# Old archive suffix (normally "a").
+libext="$libext"
+
+# Shared library suffix (normally ".so").
+shrext_cmds='$shrext_cmds'
+
+# Executable file suffix (normally "").
+exeext="$exeext"
+
+# Additional compiler flags for building library objects.
+pic_flag=$lt_[]_LT_AC_TAGVAR(lt_prog_compiler_pic, $1)
+pic_mode=$pic_mode
+
+# What is the maximum length of a command?
+max_cmd_len=$lt_cv_sys_max_cmd_len
+
+# Does compiler simultaneously support -c and -o options?
+compiler_c_o=$lt_[]_LT_AC_TAGVAR(lt_cv_prog_compiler_c_o, $1)
+
+# Must we lock files when doing compilation?
+need_locks=$lt_need_locks
+
+# Do we need the lib prefix for modules?
+need_lib_prefix=$need_lib_prefix
+
+# Do we need a version for libraries?
+need_version=$need_version
+
+# Whether dlopen is supported.
+dlopen_support=$enable_dlopen
+
+# Whether dlopen of programs is supported.
+dlopen_self=$enable_dlopen_self
+
+# Whether dlopen of statically linked programs is supported.
+dlopen_self_static=$enable_dlopen_self_static
+
+# Compiler flag to prevent dynamic linking.
+link_static_flag=$lt_[]_LT_AC_TAGVAR(lt_prog_compiler_static, $1)
+
+# Compiler flag to turn off builtin functions.
+no_builtin_flag=$lt_[]_LT_AC_TAGVAR(lt_prog_compiler_no_builtin_flag, $1)
+
+# Compiler flag to allow reflexive dlopens.
+export_dynamic_flag_spec=$lt_[]_LT_AC_TAGVAR(export_dynamic_flag_spec, $1)
+
+# Compiler flag to generate shared objects directly from archives.
+whole_archive_flag_spec=$lt_[]_LT_AC_TAGVAR(whole_archive_flag_spec, $1)
+
+# Compiler flag to generate thread-safe objects.
+thread_safe_flag_spec=$lt_[]_LT_AC_TAGVAR(thread_safe_flag_spec, $1)
+
+# Library versioning type.
+version_type=$version_type
+
+# Format of library name prefix.
+libname_spec=$lt_libname_spec
+
+# List of archive names.  First name is the real one, the rest are links.
+# The last name is the one that the linker finds with -lNAME.
+library_names_spec=$lt_library_names_spec
+
+# The coded name of the library, if different from the real name.
+soname_spec=$lt_soname_spec
+
+# Commands used to build and install an old-style archive.
+RANLIB=$lt_RANLIB
+old_archive_cmds=$lt_[]_LT_AC_TAGVAR(old_archive_cmds, $1)
+old_postinstall_cmds=$lt_old_postinstall_cmds
+old_postuninstall_cmds=$lt_old_postuninstall_cmds
+
+# Create an old-style archive from a shared archive.
+old_archive_from_new_cmds=$lt_[]_LT_AC_TAGVAR(old_archive_from_new_cmds, $1)
+
+# Create a temporary old-style archive to link instead of a shared archive.
+old_archive_from_expsyms_cmds=$lt_[]_LT_AC_TAGVAR(old_archive_from_expsyms_cmds, $1)
+
+# Commands used to build and install a shared archive.
+archive_cmds=$lt_[]_LT_AC_TAGVAR(archive_cmds, $1)
+archive_expsym_cmds=$lt_[]_LT_AC_TAGVAR(archive_expsym_cmds, $1)
+postinstall_cmds=$lt_postinstall_cmds
+postuninstall_cmds=$lt_postuninstall_cmds
+
+# Commands used to build a loadable module (assumed same as above if empty)
+module_cmds=$lt_[]_LT_AC_TAGVAR(module_cmds, $1)
+module_expsym_cmds=$lt_[]_LT_AC_TAGVAR(module_expsym_cmds, $1)
+
+# Commands to strip libraries.
+old_striplib=$lt_old_striplib
+striplib=$lt_striplib
+
+# Dependencies to place before the objects being linked to create a
+# shared library.
+predep_objects=$lt_[]_LT_AC_TAGVAR(predep_objects, $1)
+
+# Dependencies to place after the objects being linked to create a
+# shared library.
+postdep_objects=$lt_[]_LT_AC_TAGVAR(postdep_objects, $1)
+
+# Dependencies to place before the objects being linked to create a
+# shared library.
+predeps=$lt_[]_LT_AC_TAGVAR(predeps, $1)
+
+# Dependencies to place after the objects being linked to create a
+# shared library.
+postdeps=$lt_[]_LT_AC_TAGVAR(postdeps, $1)
+
+# The directories searched by this compiler when creating a shared
+# library
+compiler_lib_search_dirs=$lt_[]_LT_AC_TAGVAR(compiler_lib_search_dirs, $1)
+
+# The library search path used internally by the compiler when linking
+# a shared library.
+compiler_lib_search_path=$lt_[]_LT_AC_TAGVAR(compiler_lib_search_path, $1)
+
+# Method to check whether dependent libraries are shared objects.
+deplibs_check_method=$lt_deplibs_check_method
+
+# Command to use when deplibs_check_method == file_magic.
+file_magic_cmd=$lt_file_magic_cmd
+
+# Flag that allows shared libraries with undefined symbols to be built.
+allow_undefined_flag=$lt_[]_LT_AC_TAGVAR(allow_undefined_flag, $1)
+
+# Flag that forces no undefined symbols.
+no_undefined_flag=$lt_[]_LT_AC_TAGVAR(no_undefined_flag, $1)
+
+# Commands used to finish a libtool library installation in a directory.
+finish_cmds=$lt_finish_cmds
+
+# Same as above, but a single script fragment to be evaled but not shown.
+finish_eval=$lt_finish_eval
+
+# Take the output of nm and produce a listing of raw symbols and C names.
+global_symbol_pipe=$lt_lt_cv_sys_global_symbol_pipe
+
+# Transform the output of nm in a proper C declaration
+global_symbol_to_cdecl=$lt_lt_cv_sys_global_symbol_to_cdecl
+
+# Transform the output of nm in a C name address pair
+global_symbol_to_c_name_address=$lt_lt_cv_sys_global_symbol_to_c_name_address
+
+# This is the shared library runtime path variable.
+runpath_var=$runpath_var
+
+# This is the shared library path variable.
+shlibpath_var=$shlibpath_var
+
+# Is shlibpath searched before the hard-coded library search path?
+shlibpath_overrides_runpath=$shlibpath_overrides_runpath
+
+# How to hardcode a shared library path into an executable.
+hardcode_action=$_LT_AC_TAGVAR(hardcode_action, $1)
+
+# Whether we should hardcode library paths into libraries.
+hardcode_into_libs=$hardcode_into_libs
+
+# Flag to hardcode \$libdir into a binary during linking.
+# This must work even if \$libdir does not exist.
+hardcode_libdir_flag_spec=$lt_[]_LT_AC_TAGVAR(hardcode_libdir_flag_spec, $1)
+
+# If ld is used when linking, flag to hardcode \$libdir into
+# a binary during linking. This must work even if \$libdir does
+# not exist.
+hardcode_libdir_flag_spec_ld=$lt_[]_LT_AC_TAGVAR(hardcode_libdir_flag_spec_ld, $1)
+
+# Whether we need a single -rpath flag with a separated argument.
+hardcode_libdir_separator=$lt_[]_LT_AC_TAGVAR(hardcode_libdir_separator, $1)
+
+# Set to yes if using DIR/libNAME${shared_ext} during linking hardcodes DIR into the
+# resulting binary.
+hardcode_direct=$_LT_AC_TAGVAR(hardcode_direct, $1)
+
+# Set to yes if using the -LDIR flag during linking hardcodes DIR into the
+# resulting binary.
+hardcode_minus_L=$_LT_AC_TAGVAR(hardcode_minus_L, $1)
+
+# Set to yes if using SHLIBPATH_VAR=DIR during linking hardcodes DIR into
+# the resulting binary.
+hardcode_shlibpath_var=$_LT_AC_TAGVAR(hardcode_shlibpath_var, $1)
+
+# Set to yes if building a shared library automatically hardcodes DIR into the library
+# and all subsequent libraries and executables linked against it.
+hardcode_automatic=$_LT_AC_TAGVAR(hardcode_automatic, $1)
+
+# Variables whose values should be saved in libtool wrapper scripts and
+# restored at relink time.
+variables_saved_for_relink="$variables_saved_for_relink"
+
+# Whether libtool must link a program against all its dependency libraries.
+link_all_deplibs=$_LT_AC_TAGVAR(link_all_deplibs, $1)
+
+# Compile-time system search path for libraries
+sys_lib_search_path_spec=$lt_sys_lib_search_path_spec
+
+# Run-time system search path for libraries
+sys_lib_dlsearch_path_spec=$lt_sys_lib_dlsearch_path_spec
+
+# Fix the shell variable \$srcfile for the compiler.
+fix_srcfile_path=$lt_fix_srcfile_path
+
+# Set to yes if exported symbols are required.
+always_export_symbols=$_LT_AC_TAGVAR(always_export_symbols, $1)
+
+# The commands to list exported symbols.
+export_symbols_cmds=$lt_[]_LT_AC_TAGVAR(export_symbols_cmds, $1)
+
+# The commands to extract the exported symbol list from a shared archive.
+extract_expsyms_cmds=$lt_extract_expsyms_cmds
+
+# Symbols that should not be listed in the preloaded symbols.
+exclude_expsyms=$lt_[]_LT_AC_TAGVAR(exclude_expsyms, $1)
+
+# Symbols that must always be exported.
+include_expsyms=$lt_[]_LT_AC_TAGVAR(include_expsyms, $1)
+
+ifelse([$1],[],
+[# ### END LIBTOOL CONFIG],
+[# ### END LIBTOOL TAG CONFIG: $tagname])
+
+__EOF__
+
+ifelse([$1],[], [
+  case $host_os in
+  aix3*)
+    cat <<\EOF >> "$cfgfile"
+
+# AIX sometimes has problems with the GCC collect2 program.  For some
+# reason, if we set the COLLECT_NAMES environment variable, the problems
+# vanish in a puff of smoke.
+if test "X${COLLECT_NAMES+set}" != Xset; then
+  COLLECT_NAMES=
+  export COLLECT_NAMES
+fi
+EOF
+    ;;
+  esac
+
+  # We use sed instead of cat because bash on DJGPP gets confused if
+  # if finds mixed CR/LF and LF-only lines.  Since sed operates in
+  # text mode, it properly converts lines to CR/LF.  This bash problem
+  # is reportedly fixed, but why not run on old versions too?
+  sed '$q' "$ltmain" >> "$cfgfile" || (rm -f "$cfgfile"; exit 1)
+
+  mv -f "$cfgfile" "$ofile" || \
+    (rm -f "$ofile" && cp "$cfgfile" "$ofile" && rm -f "$cfgfile")
+  chmod +x "$ofile"
+])
+else
+  # If there is no Makefile yet, we rely on a make rule to execute
+  # `config.status --recheck' to rerun these tests and create the
+  # libtool script then.
+  ltmain_in=`echo $ltmain | sed -e 's/\.sh$/.in/'`
+  if test -f "$ltmain_in"; then
+    test -f Makefile && make "$ltmain"
+  fi
+fi
+])# AC_LIBTOOL_CONFIG
+
+
+# AC_LIBTOOL_PROG_COMPILER_NO_RTTI([TAGNAME])
+# -------------------------------------------
+AC_DEFUN([AC_LIBTOOL_PROG_COMPILER_NO_RTTI],
+[AC_REQUIRE([_LT_AC_SYS_COMPILER])dnl
+
+_LT_AC_TAGVAR(lt_prog_compiler_no_builtin_flag, $1)=
+
+if test "$GCC" = yes; then
+  _LT_AC_TAGVAR(lt_prog_compiler_no_builtin_flag, $1)=' -fno-builtin'
+
+  AC_LIBTOOL_COMPILER_OPTION([if $compiler supports -fno-rtti -fno-exceptions],
+    lt_cv_prog_compiler_rtti_exceptions,
+    [-fno-rtti -fno-exceptions], [],
+    [_LT_AC_TAGVAR(lt_prog_compiler_no_builtin_flag, $1)="$_LT_AC_TAGVAR(lt_prog_compiler_no_builtin_flag, $1) -fno-rtti -fno-exceptions"])
+fi
+])# AC_LIBTOOL_PROG_COMPILER_NO_RTTI
+
+
+# AC_LIBTOOL_SYS_GLOBAL_SYMBOL_PIPE
+# ---------------------------------
+AC_DEFUN([AC_LIBTOOL_SYS_GLOBAL_SYMBOL_PIPE],
+[AC_REQUIRE([AC_CANONICAL_HOST])
+AC_REQUIRE([LT_AC_PROG_SED])
+AC_REQUIRE([AC_PROG_NM])
+AC_REQUIRE([AC_OBJEXT])
+# Check for command to grab the raw symbol name followed by C symbol from nm.
+AC_MSG_CHECKING([command to parse $NM output from $compiler object])
+AC_CACHE_VAL([lt_cv_sys_global_symbol_pipe],
+[
+# These are sane defaults that work on at least a few old systems.
+# [They come from Ultrix.  What could be older than Ultrix?!! ;)]
+
+# Character class describing NM global symbol codes.
+symcode='[[BCDEGRST]]'
+
+# Regexp to match symbols that can be accessed directly from C.
+sympat='\([[_A-Za-z]][[_A-Za-z0-9]]*\)'
+
+# Transform an extracted symbol line into a proper C declaration
+lt_cv_sys_global_symbol_to_cdecl="sed -n -e 's/^. .* \(.*\)$/extern int \1;/p'"
+
+# Transform an extracted symbol line into symbol name and symbol address
+lt_cv_sys_global_symbol_to_c_name_address="sed -n -e 's/^: \([[^ ]]*\) $/  {\\\"\1\\\", (lt_ptr) 0},/p' -e 's/^$symcode \([[^ ]]*\) \([[^ ]]*\)$/  {\"\2\", (lt_ptr) \&\2},/p'"
+
+# Define system-specific variables.
+case $host_os in
+aix*)
+  symcode='[[BCDT]]'
+  ;;
+cygwin* | mingw* | pw32*)
+  symcode='[[ABCDGISTW]]'
+  ;;
+hpux*) # Its linker distinguishes data from code symbols
+  if test "$host_cpu" = ia64; then
+    symcode='[[ABCDEGRST]]'
+  fi
+  lt_cv_sys_global_symbol_to_cdecl="sed -n -e 's/^T .* \(.*\)$/extern int \1();/p' -e 's/^$symcode* .* \(.*\)$/extern char \1;/p'"
+  lt_cv_sys_global_symbol_to_c_name_address="sed -n -e 's/^: \([[^ ]]*\) $/  {\\\"\1\\\", (lt_ptr) 0},/p' -e 's/^$symcode* \([[^ ]]*\) \([[^ ]]*\)$/  {\"\2\", (lt_ptr) \&\2},/p'"
+  ;;
+linux* | k*bsd*-gnu)
+  if test "$host_cpu" = ia64; then
+    symcode='[[ABCDGIRSTW]]'
+    lt_cv_sys_global_symbol_to_cdecl="sed -n -e 's/^T .* \(.*\)$/extern int \1();/p' -e 's/^$symcode* .* \(.*\)$/extern char \1;/p'"
+    lt_cv_sys_global_symbol_to_c_name_address="sed -n -e 's/^: \([[^ ]]*\) $/  {\\\"\1\\\", (lt_ptr) 0},/p' -e 's/^$symcode* \([[^ ]]*\) \([[^ ]]*\)$/  {\"\2\", (lt_ptr) \&\2},/p'"
+  fi
+  ;;
+irix* | nonstopux*)
+  symcode='[[BCDEGRST]]'
+  ;;
+osf*)
+  symcode='[[BCDEGQRST]]'
+  ;;
+solaris*)
+  symcode='[[BDRT]]'
+  ;;
+sco3.2v5*)
+  symcode='[[DT]]'
+  ;;
+sysv4.2uw2*)
+  symcode='[[DT]]'
+  ;;
+sysv5* | sco5v6* | unixware* | OpenUNIX*)
+  symcode='[[ABDT]]'
+  ;;
+sysv4)
+  symcode='[[DFNSTU]]'
+  ;;
+esac
+
+# Handle CRLF in mingw tool chain
+opt_cr=
+case $build_os in
+mingw*)
+  opt_cr=`echo 'x\{0,1\}' | tr x '\015'` # option cr in regexp
+  ;;
+esac
+
+# If we're using GNU nm, then use its standard symbol codes.
+case `$NM -V 2>&1` in
+*GNU* | *'with BFD'*)
+  symcode='[[ABCDGIRSTW]]' ;;
+esac
+
+# Try without a prefix undercore, then with it.
+for ac_symprfx in "" "_"; do
+
+  # Transform symcode, sympat, and symprfx into a raw symbol and a C symbol.
+  symxfrm="\\1 $ac_symprfx\\2 \\2"
+
+  # Write the raw and C identifiers.
+  lt_cv_sys_global_symbol_pipe="sed -n -e 's/^.*[[ 	]]\($symcode$symcode*\)[[ 	]][[ 	]]*$ac_symprfx$sympat$opt_cr$/$symxfrm/p'"
+
+  # Check to see that the pipe works correctly.
+  pipe_works=no
+
+  rm -f conftest*
+  cat > conftest.$ac_ext <<EOF
+#ifdef __cplusplus
+extern "C" {
+#endif
+char nm_test_var;
+void nm_test_func(){}
+#ifdef __cplusplus
+}
+#endif
+int main(){nm_test_var='a';nm_test_func();return(0);}
+EOF
+
+  if AC_TRY_EVAL(ac_compile); then
+    # Now try to grab the symbols.
+    nlist=conftest.nm
+    if AC_TRY_EVAL(NM conftest.$ac_objext \| $lt_cv_sys_global_symbol_pipe \> $nlist) && test -s "$nlist"; then
+      # Try sorting and uniquifying the output.
+      if sort "$nlist" | uniq > "$nlist"T; then
+	mv -f "$nlist"T "$nlist"
+      else
+	rm -f "$nlist"T
+      fi
+
+      # Make sure that we snagged all the symbols we need.
+      if grep ' nm_test_var$' "$nlist" >/dev/null; then
+	if grep ' nm_test_func$' "$nlist" >/dev/null; then
+	  cat <<EOF > conftest.$ac_ext
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+EOF
+	  # Now generate the symbol file.
+	  eval "$lt_cv_sys_global_symbol_to_cdecl"' < "$nlist" | grep -v main >> conftest.$ac_ext'
+
+	  cat <<EOF >> conftest.$ac_ext
+#if defined (__STDC__) && __STDC__
+# define lt_ptr_t void *
+#else
+# define lt_ptr_t char *
+# define const
+#endif
+
+/* The mapping between symbol names and symbols. */
+const struct {
+  const char *name;
+  lt_ptr_t address;
+}
+lt_preloaded_symbols[[]] =
+{
+EOF
+	  $SED "s/^$symcode$symcode* \(.*\) \(.*\)$/  {\"\2\", (lt_ptr_t) \&\2},/" < "$nlist" | grep -v main >> conftest.$ac_ext
+	  cat <<\EOF >> conftest.$ac_ext
+  {0, (lt_ptr_t) 0}
+};
+
+#ifdef __cplusplus
+}
+#endif
+EOF
+	  # Now try linking the two files.
+	  mv conftest.$ac_objext conftstm.$ac_objext
+	  lt_save_LIBS="$LIBS"
+	  lt_save_CFLAGS="$CFLAGS"
+	  LIBS="conftstm.$ac_objext"
+	  CFLAGS="$CFLAGS$_LT_AC_TAGVAR(lt_prog_compiler_no_builtin_flag, $1)"
+	  if AC_TRY_EVAL(ac_link) && test -s conftest${ac_exeext}; then
+	    pipe_works=yes
+	  fi
+	  LIBS="$lt_save_LIBS"
+	  CFLAGS="$lt_save_CFLAGS"
+	else
+	  echo "cannot find nm_test_func in $nlist" >&AS_MESSAGE_LOG_FD
+	fi
+      else
+	echo "cannot find nm_test_var in $nlist" >&AS_MESSAGE_LOG_FD
+      fi
+    else
+      echo "cannot run $lt_cv_sys_global_symbol_pipe" >&AS_MESSAGE_LOG_FD
+    fi
+  else
+    echo "$progname: failed program was:" >&AS_MESSAGE_LOG_FD
+    cat conftest.$ac_ext >&5
+  fi
+  rm -rf conftest* conftst*
+
+  # Do not use the global_symbol_pipe unless it works.
+  if test "$pipe_works" = yes; then
+    break
+  else
+    lt_cv_sys_global_symbol_pipe=
+  fi
+done
+])
+if test -z "$lt_cv_sys_global_symbol_pipe"; then
+  lt_cv_sys_global_symbol_to_cdecl=
+fi
+if test -z "$lt_cv_sys_global_symbol_pipe$lt_cv_sys_global_symbol_to_cdecl"; then
+  AC_MSG_RESULT(failed)
+else
+  AC_MSG_RESULT(ok)
+fi
+]) # AC_LIBTOOL_SYS_GLOBAL_SYMBOL_PIPE
+
+
+# AC_LIBTOOL_PROG_COMPILER_PIC([TAGNAME])
+# ---------------------------------------
+AC_DEFUN([AC_LIBTOOL_PROG_COMPILER_PIC],
+[_LT_AC_TAGVAR(lt_prog_compiler_wl, $1)=
+_LT_AC_TAGVAR(lt_prog_compiler_pic, $1)=
+_LT_AC_TAGVAR(lt_prog_compiler_static, $1)=
+
+AC_MSG_CHECKING([for $compiler option to produce PIC])
+ ifelse([$1],[CXX],[
+  # C++ specific cases for pic, static, wl, etc.
+  if test "$GXX" = yes; then
+    _LT_AC_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,'
+    _LT_AC_TAGVAR(lt_prog_compiler_static, $1)='-static'
+
+    case $host_os in
+    aix*)
+      # All AIX code is PIC.
+      if test "$host_cpu" = ia64; then
+	# AIX 5 now supports IA64 processor
+	_LT_AC_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic'
+      fi
+      ;;
+    amigaos*)
+      # FIXME: we need at least 68020 code to build shared libraries, but
+      # adding the `-m68020' flag to GCC prevents building anything better,
+      # like `-m68040'.
+      _LT_AC_TAGVAR(lt_prog_compiler_pic, $1)='-m68020 -resident32 -malways-restore-a4'
+      ;;
+    beos* | irix5* | irix6* | nonstopux* | osf3* | osf4* | osf5*)
+      # PIC is the default for these OSes.
+      ;;
+    mingw* | cygwin* | os2* | pw32*)
+      # This hack is so that the source file can tell whether it is being
+      # built for inclusion in a dll (and should export symbols for example).
+      # Although the cygwin gcc ignores -fPIC, still need this for old-style
+      # (--disable-auto-import) libraries
+      m4_if([$1], [GCJ], [],
+	[_LT_AC_TAGVAR(lt_prog_compiler_pic, $1)='-DDLL_EXPORT'])
+      ;;
+    darwin* | rhapsody*)
+      # PIC is the default on this platform
+      # Common symbols not allowed in MH_DYLIB files
+      _LT_AC_TAGVAR(lt_prog_compiler_pic, $1)='-fno-common'
+      ;;
+    *djgpp*)
+      # DJGPP does not support shared libraries at all
+      _LT_AC_TAGVAR(lt_prog_compiler_pic, $1)=
+      ;;
+    interix[[3-9]]*)
+      # Interix 3.x gcc -fpic/-fPIC options generate broken code.
+      # Instead, we relocate shared libraries at runtime.
+      ;;
+    sysv4*MP*)
+      if test -d /usr/nec; then
+	_LT_AC_TAGVAR(lt_prog_compiler_pic, $1)=-Kconform_pic
+      fi
+      ;;
+    hpux*)
+      # PIC is the default for IA64 HP-UX and 64-bit HP-UX, but
+      # not for PA HP-UX.
+      case $host_cpu in
+      hppa*64*|ia64*)
+	;;
+      *)
+	_LT_AC_TAGVAR(lt_prog_compiler_pic, $1)='-fPIC'
+	;;
+      esac
+      ;;
+    *)
+      _LT_AC_TAGVAR(lt_prog_compiler_pic, $1)='-fPIC'
+      ;;
+    esac
+  else
+    case $host_os in
+      aix[[4-9]]*)
+	# All AIX code is PIC.
+	if test "$host_cpu" = ia64; then
+	  # AIX 5 now supports IA64 processor
+	  _LT_AC_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic'
+	else
+	  _LT_AC_TAGVAR(lt_prog_compiler_static, $1)='-bnso -bI:/lib/syscalls.exp'
+	fi
+	;;
+      chorus*)
+	case $cc_basename in
+	cxch68*)
+	  # Green Hills C++ Compiler
+	  # _LT_AC_TAGVAR(lt_prog_compiler_static, $1)="--no_auto_instantiation -u __main -u __premain -u _abort -r $COOL_DIR/lib/libOrb.a $MVME_DIR/lib/CC/libC.a $MVME_DIR/lib/classix/libcx.s.a"
+	  ;;
+	esac
+	;;
+       darwin*)
+         # PIC is the default on this platform
+         # Common symbols not allowed in MH_DYLIB files
+         case $cc_basename in
+           xlc*)
+           _LT_AC_TAGVAR(lt_prog_compiler_pic, $1)='-qnocommon'
+           _LT_AC_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,'
+           ;;
+         esac
+       ;;
+      dgux*)
+	case $cc_basename in
+	  ec++*)
+	    _LT_AC_TAGVAR(lt_prog_compiler_pic, $1)='-KPIC'
+	    ;;
+	  ghcx*)
+	    # Green Hills C++ Compiler
+	    _LT_AC_TAGVAR(lt_prog_compiler_pic, $1)='-pic'
+	    ;;
+	  *)
+	    ;;
+	esac
+	;;
+      freebsd* | dragonfly*)
+	# FreeBSD uses GNU C++
+	;;
+      hpux9* | hpux10* | hpux11*)
+	case $cc_basename in
+	  CC*)
+	    _LT_AC_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,'
+	    _LT_AC_TAGVAR(lt_prog_compiler_static, $1)='${wl}-a ${wl}archive'
+	    if test "$host_cpu" != ia64; then
+	      _LT_AC_TAGVAR(lt_prog_compiler_pic, $1)='+Z'
+	    fi
+	    ;;
+	  aCC*)
+	    _LT_AC_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,'
+	    _LT_AC_TAGVAR(lt_prog_compiler_static, $1)='${wl}-a ${wl}archive'
+	    case $host_cpu in
+	    hppa*64*|ia64*)
+	      # +Z the default
+	      ;;
+	    *)
+	      _LT_AC_TAGVAR(lt_prog_compiler_pic, $1)='+Z'
+	      ;;
+	    esac
+	    ;;
+	  *)
+	    ;;
+	esac
+	;;
+      interix*)
+	# This is c89, which is MS Visual C++ (no shared libs)
+	# Anyone wants to do a port?
+	;;
+      irix5* | irix6* | nonstopux*)
+	case $cc_basename in
+	  CC*)
+	    _LT_AC_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,'
+	    _LT_AC_TAGVAR(lt_prog_compiler_static, $1)='-non_shared'
+	    # CC pic flag -KPIC is the default.
+	    ;;
+	  *)
+	    ;;
+	esac
+	;;
+      linux* | k*bsd*-gnu)
+	case $cc_basename in
+	  KCC*)
+	    # KAI C++ Compiler
+	    _LT_AC_TAGVAR(lt_prog_compiler_wl, $1)='--backend -Wl,'
+	    _LT_AC_TAGVAR(lt_prog_compiler_pic, $1)='-fPIC'
+	    ;;
+	  icpc* | ecpc*)
+	    # Intel C++
+	    _LT_AC_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,'
+	    _LT_AC_TAGVAR(lt_prog_compiler_pic, $1)='-KPIC'
+	    _LT_AC_TAGVAR(lt_prog_compiler_static, $1)='-static'
+	    ;;
+	  pgCC* | pgcpp*)
+	    # Portland Group C++ compiler.
+	    _LT_AC_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,'
+	    _LT_AC_TAGVAR(lt_prog_compiler_pic, $1)='-fpic'
+	    _LT_AC_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic'
+	    ;;
+	  cxx*)
+	    # Compaq C++
+	    # Make sure the PIC flag is empty.  It appears that all Alpha
+	    # Linux and Compaq Tru64 Unix objects are PIC.
+	    _LT_AC_TAGVAR(lt_prog_compiler_pic, $1)=
+	    _LT_AC_TAGVAR(lt_prog_compiler_static, $1)='-non_shared'
+	    ;;
+	  *)
+	    case `$CC -V 2>&1 | sed 5q` in
+	    *Sun\ C*)
+	      # Sun C++ 5.9
+	      _LT_AC_TAGVAR(lt_prog_compiler_pic, $1)='-KPIC'
+	      _LT_AC_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic'
+	      _LT_AC_TAGVAR(lt_prog_compiler_wl, $1)='-Qoption ld '
+	      ;;
+	    esac
+	    ;;
+	esac
+	;;
+      lynxos*)
+	;;
+      m88k*)
+	;;
+      mvs*)
+	case $cc_basename in
+	  cxx*)
+	    _LT_AC_TAGVAR(lt_prog_compiler_pic, $1)='-W c,exportall'
+	    ;;
+	  *)
+	    ;;
+	esac
+	;;
+      netbsd*)
+	;;
+      osf3* | osf4* | osf5*)
+	case $cc_basename in
+	  KCC*)
+	    _LT_AC_TAGVAR(lt_prog_compiler_wl, $1)='--backend -Wl,'
+	    ;;
+	  RCC*)
+	    # Rational C++ 2.4.1
+	    _LT_AC_TAGVAR(lt_prog_compiler_pic, $1)='-pic'
+	    ;;
+	  cxx*)
+	    # Digital/Compaq C++
+	    _LT_AC_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,'
+	    # Make sure the PIC flag is empty.  It appears that all Alpha
+	    # Linux and Compaq Tru64 Unix objects are PIC.
+	    _LT_AC_TAGVAR(lt_prog_compiler_pic, $1)=
+	    _LT_AC_TAGVAR(lt_prog_compiler_static, $1)='-non_shared'
+	    ;;
+	  *)
+	    ;;
+	esac
+	;;
+      psos*)
+	;;
+      solaris*)
+	case $cc_basename in
+	  CC*)
+	    # Sun C++ 4.2, 5.x and Centerline C++
+	    _LT_AC_TAGVAR(lt_prog_compiler_pic, $1)='-KPIC'
+	    _LT_AC_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic'
+	    _LT_AC_TAGVAR(lt_prog_compiler_wl, $1)='-Qoption ld '
+	    ;;
+	  gcx*)
+	    # Green Hills C++ Compiler
+	    _LT_AC_TAGVAR(lt_prog_compiler_pic, $1)='-PIC'
+	    ;;
+	  *)
+	    ;;
+	esac
+	;;
+      sunos4*)
+	case $cc_basename in
+	  CC*)
+	    # Sun C++ 4.x
+	    _LT_AC_TAGVAR(lt_prog_compiler_pic, $1)='-pic'
+	    _LT_AC_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic'
+	    ;;
+	  lcc*)
+	    # Lucid
+	    _LT_AC_TAGVAR(lt_prog_compiler_pic, $1)='-pic'
+	    ;;
+	  *)
+	    ;;
+	esac
+	;;
+      tandem*)
+	case $cc_basename in
+	  NCC*)
+	    # NonStop-UX NCC 3.20
+	    _LT_AC_TAGVAR(lt_prog_compiler_pic, $1)='-KPIC'
+	    ;;
+	  *)
+	    ;;
+	esac
+	;;
+      sysv5* | unixware* | sco3.2v5* | sco5v6* | OpenUNIX*)
+	case $cc_basename in
+	  CC*)
+	    _LT_AC_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,'
+	    _LT_AC_TAGVAR(lt_prog_compiler_pic, $1)='-KPIC'
+	    _LT_AC_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic'
+	    ;;
+	esac
+	;;
+      vxworks*)
+	;;
+      *)
+	_LT_AC_TAGVAR(lt_prog_compiler_can_build_shared, $1)=no
+	;;
+    esac
+  fi
+],
+[
+  if test "$GCC" = yes; then
+    _LT_AC_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,'
+    _LT_AC_TAGVAR(lt_prog_compiler_static, $1)='-static'
+
+    case $host_os in
+      aix*)
+      # All AIX code is PIC.
+      if test "$host_cpu" = ia64; then
+	# AIX 5 now supports IA64 processor
+	_LT_AC_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic'
+      fi
+      ;;
+
+    amigaos*)
+      # FIXME: we need at least 68020 code to build shared libraries, but
+      # adding the `-m68020' flag to GCC prevents building anything better,
+      # like `-m68040'.
+      _LT_AC_TAGVAR(lt_prog_compiler_pic, $1)='-m68020 -resident32 -malways-restore-a4'
+      ;;
+
+    beos* | irix5* | irix6* | nonstopux* | osf3* | osf4* | osf5*)
+      # PIC is the default for these OSes.
+      ;;
+
+    mingw* | cygwin* | pw32* | os2*)
+      # This hack is so that the source file can tell whether it is being
+      # built for inclusion in a dll (and should export symbols for example).
+      # Although the cygwin gcc ignores -fPIC, still need this for old-style
+      # (--disable-auto-import) libraries
+      m4_if([$1], [GCJ], [],
+	[_LT_AC_TAGVAR(lt_prog_compiler_pic, $1)='-DDLL_EXPORT'])
+      ;;
+
+    darwin* | rhapsody*)
+      # PIC is the default on this platform
+      # Common symbols not allowed in MH_DYLIB files
+      _LT_AC_TAGVAR(lt_prog_compiler_pic, $1)='-fno-common'
+      ;;
+
+    interix[[3-9]]*)
+      # Interix 3.x gcc -fpic/-fPIC options generate broken code.
+      # Instead, we relocate shared libraries at runtime.
+      ;;
+
+    msdosdjgpp*)
+      # Just because we use GCC doesn't mean we suddenly get shared libraries
+      # on systems that don't support them.
+      _LT_AC_TAGVAR(lt_prog_compiler_can_build_shared, $1)=no
+      enable_shared=no
+      ;;
+
+    sysv4*MP*)
+      if test -d /usr/nec; then
+	_LT_AC_TAGVAR(lt_prog_compiler_pic, $1)=-Kconform_pic
+      fi
+      ;;
+
+    hpux*)
+      # PIC is the default for IA64 HP-UX and 64-bit HP-UX, but
+      # not for PA HP-UX.
+      case $host_cpu in
+      hppa*64*|ia64*)
+	# +Z the default
+	;;
+      *)
+	_LT_AC_TAGVAR(lt_prog_compiler_pic, $1)='-fPIC'
+	;;
+      esac
+      ;;
+
+    *)
+      _LT_AC_TAGVAR(lt_prog_compiler_pic, $1)='-fPIC'
+      ;;
+    esac
+  else
+    # PORTME Check for flag to pass linker flags through the system compiler.
+    case $host_os in
+    aix*)
+      _LT_AC_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,'
+      if test "$host_cpu" = ia64; then
+	# AIX 5 now supports IA64 processor
+	_LT_AC_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic'
+      else
+	_LT_AC_TAGVAR(lt_prog_compiler_static, $1)='-bnso -bI:/lib/syscalls.exp'
+      fi
+      ;;
+      darwin*)
+        # PIC is the default on this platform
+        # Common symbols not allowed in MH_DYLIB files
+       case $cc_basename in
+         xlc*)
+         _LT_AC_TAGVAR(lt_prog_compiler_pic, $1)='-qnocommon'
+         _LT_AC_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,'
+         ;;
+       esac
+       ;;
+
+    mingw* | cygwin* | pw32* | os2*)
+      # This hack is so that the source file can tell whether it is being
+      # built for inclusion in a dll (and should export symbols for example).
+      m4_if([$1], [GCJ], [],
+	[_LT_AC_TAGVAR(lt_prog_compiler_pic, $1)='-DDLL_EXPORT'])
+      ;;
+
+    hpux9* | hpux10* | hpux11*)
+      _LT_AC_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,'
+      # PIC is the default for IA64 HP-UX and 64-bit HP-UX, but
+      # not for PA HP-UX.
+      case $host_cpu in
+      hppa*64*|ia64*)
+	# +Z the default
+	;;
+      *)
+	_LT_AC_TAGVAR(lt_prog_compiler_pic, $1)='+Z'
+	;;
+      esac
+      # Is there a better lt_prog_compiler_static that works with the bundled CC?
+      _LT_AC_TAGVAR(lt_prog_compiler_static, $1)='${wl}-a ${wl}archive'
+      ;;
+
+    irix5* | irix6* | nonstopux*)
+      _LT_AC_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,'
+      # PIC (with -KPIC) is the default.
+      _LT_AC_TAGVAR(lt_prog_compiler_static, $1)='-non_shared'
+      ;;
+
+    newsos6)
+      _LT_AC_TAGVAR(lt_prog_compiler_pic, $1)='-KPIC'
+      _LT_AC_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic'
+      ;;
+
+    linux* | k*bsd*-gnu)
+      case $cc_basename in
+      icc* | ecc*)
+	_LT_AC_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,'
+	_LT_AC_TAGVAR(lt_prog_compiler_pic, $1)='-KPIC'
+	_LT_AC_TAGVAR(lt_prog_compiler_static, $1)='-static'
+        ;;
+      pgcc* | pgf77* | pgf90* | pgf95*)
+        # Portland Group compilers (*not* the Pentium gcc compiler,
+	# which looks to be a dead project)
+	_LT_AC_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,'
+	_LT_AC_TAGVAR(lt_prog_compiler_pic, $1)='-fpic'
+	_LT_AC_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic'
+        ;;
+      ccc*)
+        _LT_AC_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,'
+        # All Alpha code is PIC.
+        _LT_AC_TAGVAR(lt_prog_compiler_static, $1)='-non_shared'
+        ;;
+      *)
+        case `$CC -V 2>&1 | sed 5q` in
+	*Sun\ C*)
+	  # Sun C 5.9
+	  _LT_AC_TAGVAR(lt_prog_compiler_pic, $1)='-KPIC'
+	  _LT_AC_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic'
+	  _LT_AC_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,'
+	  ;;
+	*Sun\ F*)
+	  # Sun Fortran 8.3 passes all unrecognized flags to the linker
+	  _LT_AC_TAGVAR(lt_prog_compiler_pic, $1)='-KPIC'
+	  _LT_AC_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic'
+	  _LT_AC_TAGVAR(lt_prog_compiler_wl, $1)=''
+	  ;;
+	esac
+	;;
+      esac
+      ;;
+
+    osf3* | osf4* | osf5*)
+      _LT_AC_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,'
+      # All OSF/1 code is PIC.
+      _LT_AC_TAGVAR(lt_prog_compiler_static, $1)='-non_shared'
+      ;;
+
+    rdos*)
+      _LT_AC_TAGVAR(lt_prog_compiler_static, $1)='-non_shared'
+      ;;
+
+    solaris*)
+      _LT_AC_TAGVAR(lt_prog_compiler_pic, $1)='-KPIC'
+      _LT_AC_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic'
+      case $cc_basename in
+      f77* | f90* | f95*)
+	_LT_AC_TAGVAR(lt_prog_compiler_wl, $1)='-Qoption ld ';;
+      *)
+	_LT_AC_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,';;
+      esac
+      ;;
+
+    sunos4*)
+      _LT_AC_TAGVAR(lt_prog_compiler_wl, $1)='-Qoption ld '
+      _LT_AC_TAGVAR(lt_prog_compiler_pic, $1)='-PIC'
+      _LT_AC_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic'
+      ;;
+
+    sysv4 | sysv4.2uw2* | sysv4.3*)
+      _LT_AC_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,'
+      _LT_AC_TAGVAR(lt_prog_compiler_pic, $1)='-KPIC'
+      _LT_AC_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic'
+      ;;
+
+    sysv4*MP*)
+      if test -d /usr/nec ;then
+	_LT_AC_TAGVAR(lt_prog_compiler_pic, $1)='-Kconform_pic'
+	_LT_AC_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic'
+      fi
+      ;;
+
+    sysv5* | unixware* | sco3.2v5* | sco5v6* | OpenUNIX*)
+      _LT_AC_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,'
+      _LT_AC_TAGVAR(lt_prog_compiler_pic, $1)='-KPIC'
+      _LT_AC_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic'
+      ;;
+
+    unicos*)
+      _LT_AC_TAGVAR(lt_prog_compiler_wl, $1)='-Wl,'
+      _LT_AC_TAGVAR(lt_prog_compiler_can_build_shared, $1)=no
+      ;;
+
+    uts4*)
+      _LT_AC_TAGVAR(lt_prog_compiler_pic, $1)='-pic'
+      _LT_AC_TAGVAR(lt_prog_compiler_static, $1)='-Bstatic'
+      ;;
+
+    *)
+      _LT_AC_TAGVAR(lt_prog_compiler_can_build_shared, $1)=no
+      ;;
+    esac
+  fi
+])
+AC_MSG_RESULT([$_LT_AC_TAGVAR(lt_prog_compiler_pic, $1)])
+
+#
+# Check to make sure the PIC flag actually works.
+#
+if test -n "$_LT_AC_TAGVAR(lt_prog_compiler_pic, $1)"; then
+  AC_LIBTOOL_COMPILER_OPTION([if $compiler PIC flag $_LT_AC_TAGVAR(lt_prog_compiler_pic, $1) works],
+    _LT_AC_TAGVAR(lt_cv_prog_compiler_pic_works, $1),
+    [$_LT_AC_TAGVAR(lt_prog_compiler_pic, $1)ifelse([$1],[],[ -DPIC],[ifelse([$1],[CXX],[ -DPIC],[])])], [],
+    [case $_LT_AC_TAGVAR(lt_prog_compiler_pic, $1) in
+     "" | " "*) ;;
+     *) _LT_AC_TAGVAR(lt_prog_compiler_pic, $1)=" $_LT_AC_TAGVAR(lt_prog_compiler_pic, $1)" ;;
+     esac],
+    [_LT_AC_TAGVAR(lt_prog_compiler_pic, $1)=
+     _LT_AC_TAGVAR(lt_prog_compiler_can_build_shared, $1)=no])
+fi
+case $host_os in
+  # For platforms which do not support PIC, -DPIC is meaningless:
+  *djgpp*)
+    _LT_AC_TAGVAR(lt_prog_compiler_pic, $1)=
+    ;;
+  *)
+    _LT_AC_TAGVAR(lt_prog_compiler_pic, $1)="$_LT_AC_TAGVAR(lt_prog_compiler_pic, $1)ifelse([$1],[],[ -DPIC],[ifelse([$1],[CXX],[ -DPIC],[])])"
+    ;;
+esac
+
+#
+# Check to make sure the static flag actually works.
+#
+wl=$_LT_AC_TAGVAR(lt_prog_compiler_wl, $1) eval lt_tmp_static_flag=\"$_LT_AC_TAGVAR(lt_prog_compiler_static, $1)\"
+AC_LIBTOOL_LINKER_OPTION([if $compiler static flag $lt_tmp_static_flag works],
+  _LT_AC_TAGVAR(lt_cv_prog_compiler_static_works, $1),
+  $lt_tmp_static_flag,
+  [],
+  [_LT_AC_TAGVAR(lt_prog_compiler_static, $1)=])
+])
+
+
+# AC_LIBTOOL_PROG_LD_SHLIBS([TAGNAME])
+# ------------------------------------
+# See if the linker supports building shared libraries.
+AC_DEFUN([AC_LIBTOOL_PROG_LD_SHLIBS],
+[AC_REQUIRE([LT_AC_PROG_SED])dnl
+AC_MSG_CHECKING([whether the $compiler linker ($LD) supports shared libraries])
+ifelse([$1],[CXX],[
+  _LT_AC_TAGVAR(export_symbols_cmds, $1)='$NM $libobjs $convenience | $global_symbol_pipe | $SED '\''s/.* //'\'' | sort | uniq > $export_symbols'
+  case $host_os in
+  aix[[4-9]]*)
+    # If we're using GNU nm, then we don't want the "-C" option.
+    # -C means demangle to AIX nm, but means don't demangle with GNU nm
+    if $NM -V 2>&1 | grep 'GNU' > /dev/null; then
+      _LT_AC_TAGVAR(export_symbols_cmds, $1)='$NM -Bpg $libobjs $convenience | awk '\''{ if (((\[$]2 == "T") || (\[$]2 == "D") || (\[$]2 == "B")) && ([substr](\[$]3,1,1) != ".")) { print \[$]3 } }'\'' | sort -u > $export_symbols'
+    else
+      _LT_AC_TAGVAR(export_symbols_cmds, $1)='$NM -BCpg $libobjs $convenience | awk '\''{ if (((\[$]2 == "T") || (\[$]2 == "D") || (\[$]2 == "B")) && ([substr](\[$]3,1,1) != ".")) { print \[$]3 } }'\'' | sort -u > $export_symbols'
+    fi
+    ;;
+  pw32*)
+    _LT_AC_TAGVAR(export_symbols_cmds, $1)="$ltdll_cmds"
+  ;;
+  cygwin* | mingw*)
+    _LT_AC_TAGVAR(export_symbols_cmds, $1)='$NM $libobjs $convenience | $global_symbol_pipe | $SED -e '\''/^[[BCDGRS]][[ ]]/s/.*[[ ]]\([[^ ]]*\)/\1 DATA/;/^.*[[ ]]__nm__/s/^.*[[ ]]__nm__\([[^ ]]*\)[[ ]][[^ ]]*/\1 DATA/;/^I[[ ]]/d;/^[[AITW]][[ ]]/s/.*[[ ]]//'\'' | sort | uniq > $export_symbols'
+  ;;
+  *)
+    _LT_AC_TAGVAR(export_symbols_cmds, $1)='$NM $libobjs $convenience | $global_symbol_pipe | $SED '\''s/.* //'\'' | sort | uniq > $export_symbols'
+  ;;
+  esac
+  _LT_AC_TAGVAR(exclude_expsyms, $1)=['_GLOBAL_OFFSET_TABLE_|_GLOBAL__F[ID]_.*']
+],[
+  runpath_var=
+  _LT_AC_TAGVAR(allow_undefined_flag, $1)=
+  _LT_AC_TAGVAR(enable_shared_with_static_runtimes, $1)=no
+  _LT_AC_TAGVAR(archive_cmds, $1)=
+  _LT_AC_TAGVAR(archive_expsym_cmds, $1)=
+  _LT_AC_TAGVAR(old_archive_From_new_cmds, $1)=
+  _LT_AC_TAGVAR(old_archive_from_expsyms_cmds, $1)=
+  _LT_AC_TAGVAR(export_dynamic_flag_spec, $1)=
+  _LT_AC_TAGVAR(whole_archive_flag_spec, $1)=
+  _LT_AC_TAGVAR(thread_safe_flag_spec, $1)=
+  _LT_AC_TAGVAR(hardcode_libdir_flag_spec, $1)=
+  _LT_AC_TAGVAR(hardcode_libdir_flag_spec_ld, $1)=
+  _LT_AC_TAGVAR(hardcode_libdir_separator, $1)=
+  _LT_AC_TAGVAR(hardcode_direct, $1)=no
+  _LT_AC_TAGVAR(hardcode_minus_L, $1)=no
+  _LT_AC_TAGVAR(hardcode_shlibpath_var, $1)=unsupported
+  _LT_AC_TAGVAR(link_all_deplibs, $1)=unknown
+  _LT_AC_TAGVAR(hardcode_automatic, $1)=no
+  _LT_AC_TAGVAR(module_cmds, $1)=
+  _LT_AC_TAGVAR(module_expsym_cmds, $1)=
+  _LT_AC_TAGVAR(always_export_symbols, $1)=no
+  _LT_AC_TAGVAR(export_symbols_cmds, $1)='$NM $libobjs $convenience | $global_symbol_pipe | $SED '\''s/.* //'\'' | sort | uniq > $export_symbols'
+  # include_expsyms should be a list of space-separated symbols to be *always*
+  # included in the symbol list
+  _LT_AC_TAGVAR(include_expsyms, $1)=
+  # exclude_expsyms can be an extended regexp of symbols to exclude
+  # it will be wrapped by ` (' and `)$', so one must not match beginning or
+  # end of line.  Example: `a|bc|.*d.*' will exclude the symbols `a' and `bc',
+  # as well as any symbol that contains `d'.
+  _LT_AC_TAGVAR(exclude_expsyms, $1)=['_GLOBAL_OFFSET_TABLE_|_GLOBAL__F[ID]_.*']
+  # Although _GLOBAL_OFFSET_TABLE_ is a valid symbol C name, most a.out
+  # platforms (ab)use it in PIC code, but their linkers get confused if
+  # the symbol is explicitly referenced.  Since portable code cannot
+  # rely on this symbol name, it's probably fine to never include it in
+  # preloaded symbol tables.
+  # Exclude shared library initialization/finalization symbols.
+dnl Note also adjust exclude_expsyms for C++ above.
+  extract_expsyms_cmds=
+  # Just being paranoid about ensuring that cc_basename is set.
+  _LT_CC_BASENAME([$compiler])
+  case $host_os in
+  cygwin* | mingw* | pw32*)
+    # FIXME: the MSVC++ port hasn't been tested in a loooong time
+    # When not using gcc, we currently assume that we are using
+    # Microsoft Visual C++.
+    if test "$GCC" != yes; then
+      with_gnu_ld=no
+    fi
+    ;;
+  interix*)
+    # we just hope/assume this is gcc and not c89 (= MSVC++)
+    with_gnu_ld=yes
+    ;;
+  openbsd*)
+    with_gnu_ld=no
+    ;;
+  esac
+
+  _LT_AC_TAGVAR(ld_shlibs, $1)=yes
+  if test "$with_gnu_ld" = yes; then
+    # If archive_cmds runs LD, not CC, wlarc should be empty
+    wlarc='${wl}'
+
+    # Set some defaults for GNU ld with shared library support. These
+    # are reset later if shared libraries are not supported. Putting them
+    # here allows them to be overridden if necessary.
+    runpath_var=LD_RUN_PATH
+    _LT_AC_TAGVAR(hardcode_libdir_flag_spec, $1)='${wl}--rpath ${wl}$libdir'
+    _LT_AC_TAGVAR(export_dynamic_flag_spec, $1)='${wl}--export-dynamic'
+    # ancient GNU ld didn't support --whole-archive et. al.
+    if $LD --help 2>&1 | grep 'no-whole-archive' > /dev/null; then
+	_LT_AC_TAGVAR(whole_archive_flag_spec, $1)="$wlarc"'--whole-archive$convenience '"$wlarc"'--no-whole-archive'
+      else
+  	_LT_AC_TAGVAR(whole_archive_flag_spec, $1)=
+    fi
+    supports_anon_versioning=no
+    case `$LD -v 2>/dev/null` in
+      *\ [[01]].* | *\ 2.[[0-9]].* | *\ 2.10.*) ;; # catch versions < 2.11
+      *\ 2.11.93.0.2\ *) supports_anon_versioning=yes ;; # RH7.3 ...
+      *\ 2.11.92.0.12\ *) supports_anon_versioning=yes ;; # Mandrake 8.2 ...
+      *\ 2.11.*) ;; # other 2.11 versions
+      *) supports_anon_versioning=yes ;;
+    esac
+
+    # See if GNU ld supports shared libraries.
+    case $host_os in
+    aix[[3-9]]*)
+      # On AIX/PPC, the GNU linker is very broken
+      if test "$host_cpu" != ia64; then
+	_LT_AC_TAGVAR(ld_shlibs, $1)=no
+	cat <<EOF 1>&2
+
+*** Warning: the GNU linker, at least up to release 2.9.1, is reported
+*** to be unable to reliably create shared libraries on AIX.
+*** Therefore, libtool is disabling shared libraries support.  If you
+*** really care for shared libraries, you may want to modify your PATH
+*** so that a non-GNU linker is found, and then restart.
+
+EOF
+      fi
+      ;;
+
+    amigaos*)
+      _LT_AC_TAGVAR(archive_cmds, $1)='$rm $output_objdir/a2ixlibrary.data~$echo "#define NAME $libname" > $output_objdir/a2ixlibrary.data~$echo "#define LIBRARY_ID 1" >> $output_objdir/a2ixlibrary.data~$echo "#define VERSION $major" >> $output_objdir/a2ixlibrary.data~$echo "#define REVISION $revision" >> $output_objdir/a2ixlibrary.data~$AR $AR_FLAGS $lib $libobjs~$RANLIB $lib~(cd $output_objdir && a2ixlibrary -32)'
+      _LT_AC_TAGVAR(hardcode_libdir_flag_spec, $1)='-L$libdir'
+      _LT_AC_TAGVAR(hardcode_minus_L, $1)=yes
+
+      # Samuel A. Falvo II <kc5tja at dolphin.openprojects.net> reports
+      # that the semantics of dynamic libraries on AmigaOS, at least up
+      # to version 4, is to share data among multiple programs linked
+      # with the same dynamic library.  Since this doesn't match the
+      # behavior of shared libraries on other platforms, we can't use
+      # them.
+      _LT_AC_TAGVAR(ld_shlibs, $1)=no
+      ;;
+
+    beos*)
+      if $LD --help 2>&1 | grep ': supported targets:.* elf' > /dev/null; then
+	_LT_AC_TAGVAR(allow_undefined_flag, $1)=unsupported
+	# Joseph Beckenbach <jrb3 at best.com> says some releases of gcc
+	# support --undefined.  This deserves some investigation.  FIXME
+	_LT_AC_TAGVAR(archive_cmds, $1)='$CC -nostart $libobjs $deplibs $compiler_flags ${wl}-soname $wl$soname -o $lib'
+      else
+	_LT_AC_TAGVAR(ld_shlibs, $1)=no
+      fi
+      ;;
+
+    cygwin* | mingw* | pw32*)
+      # _LT_AC_TAGVAR(hardcode_libdir_flag_spec, $1) is actually meaningless,
+      # as there is no search path for DLLs.
+      _LT_AC_TAGVAR(hardcode_libdir_flag_spec, $1)='-L$libdir'
+      _LT_AC_TAGVAR(allow_undefined_flag, $1)=unsupported
+      _LT_AC_TAGVAR(always_export_symbols, $1)=no
+      _LT_AC_TAGVAR(enable_shared_with_static_runtimes, $1)=yes
+      _LT_AC_TAGVAR(export_symbols_cmds, $1)='$NM $libobjs $convenience | $global_symbol_pipe | $SED -e '\''/^[[BCDGRS]][[ ]]/s/.*[[ ]]\([[^ ]]*\)/\1 DATA/'\'' -e '\''/^[[AITW]][[ ]]/s/.*[[ ]]//'\'' | sort | uniq > $export_symbols'
+
+      if $LD --help 2>&1 | grep 'auto-import' > /dev/null; then
+        _LT_AC_TAGVAR(archive_cmds, $1)='$CC -shared $libobjs $deplibs $compiler_flags -o $output_objdir/$soname ${wl}--enable-auto-image-base -Xlinker --out-implib -Xlinker $lib'
+	# If the export-symbols file already is a .def file (1st line
+	# is EXPORTS), use it as is; otherwise, prepend...
+	_LT_AC_TAGVAR(archive_expsym_cmds, $1)='if test "x`$SED 1q $export_symbols`" = xEXPORTS; then
+	  cp $export_symbols $output_objdir/$soname.def;
+	else
+	  echo EXPORTS > $output_objdir/$soname.def;
+	  cat $export_symbols >> $output_objdir/$soname.def;
+	fi~
+	$CC -shared $output_objdir/$soname.def $libobjs $deplibs $compiler_flags -o $output_objdir/$soname ${wl}--enable-auto-image-base -Xlinker --out-implib -Xlinker $lib'
+      else
+	_LT_AC_TAGVAR(ld_shlibs, $1)=no
+      fi
+      ;;
+
+    interix[[3-9]]*)
+      _LT_AC_TAGVAR(hardcode_direct, $1)=no
+      _LT_AC_TAGVAR(hardcode_shlibpath_var, $1)=no
+      _LT_AC_TAGVAR(hardcode_libdir_flag_spec, $1)='${wl}-rpath,$libdir'
+      _LT_AC_TAGVAR(export_dynamic_flag_spec, $1)='${wl}-E'
+      # Hack: On Interix 3.x, we cannot compile PIC because of a broken gcc.
+      # Instead, shared libraries are loaded at an image base (0x10000000 by
+      # default) and relocated if they conflict, which is a slow very memory
+      # consuming and fragmenting process.  To avoid this, we pick a random,
+      # 256 KiB-aligned image base between 0x50000000 and 0x6FFC0000 at link
+      # time.  Moving up from 0x10000000 also allows more sbrk(2) space.
+      _LT_AC_TAGVAR(archive_cmds, $1)='$CC -shared $pic_flag $libobjs $deplibs $compiler_flags ${wl}-h,$soname ${wl}--image-base,`expr ${RANDOM-$$} % 4096 / 2 \* 262144 + 1342177280` -o $lib'
+      _LT_AC_TAGVAR(archive_expsym_cmds, $1)='sed "s,^,_," $export_symbols >$output_objdir/$soname.expsym~$CC -shared $pic_flag $libobjs $deplibs $compiler_flags ${wl}-h,$soname ${wl}--retain-symbols-file,$output_objdir/$soname.expsym ${wl}--image-base,`expr ${RANDOM-$$} % 4096 / 2 \* 262144 + 1342177280` -o $lib'
+      ;;
+
+    gnu* | linux* | k*bsd*-gnu)
+      if $LD --help 2>&1 | grep ': supported targets:.* elf' > /dev/null; then
+	tmp_addflag=
+	case $cc_basename,$host_cpu in
+	pgcc*)				# Portland Group C compiler
+	  _LT_AC_TAGVAR(whole_archive_flag_spec, $1)='${wl}--whole-archive`for conv in $convenience\"\"; do test  -n \"$conv\" && new_convenience=\"$new_convenience,$conv\"; done; $echo \"$new_convenience\"` ${wl}--no-whole-archive'
+	  tmp_addflag=' $pic_flag'
+	  ;;
+	pgf77* | pgf90* | pgf95*)	# Portland Group f77 and f90 compilers
+	  _LT_AC_TAGVAR(whole_archive_flag_spec, $1)='${wl}--whole-archive`for conv in $convenience\"\"; do test  -n \"$conv\" && new_convenience=\"$new_convenience,$conv\"; done; $echo \"$new_convenience\"` ${wl}--no-whole-archive'
+	  tmp_addflag=' $pic_flag -Mnomain' ;;
+	ecc*,ia64* | icc*,ia64*)		# Intel C compiler on ia64
+	  tmp_addflag=' -i_dynamic' ;;
+	efc*,ia64* | ifort*,ia64*)	# Intel Fortran compiler on ia64
+	  tmp_addflag=' -i_dynamic -nofor_main' ;;
+	ifc* | ifort*)			# Intel Fortran compiler
+	  tmp_addflag=' -nofor_main' ;;
+	esac
+	case `$CC -V 2>&1 | sed 5q` in
+	*Sun\ C*)			# Sun C 5.9
+	  _LT_AC_TAGVAR(whole_archive_flag_spec, $1)='${wl}--whole-archive`new_convenience=; for conv in $convenience\"\"; do test -z \"$conv\" || new_convenience=\"$new_convenience,$conv\"; done; $echo \"$new_convenience\"` ${wl}--no-whole-archive'
+	  tmp_sharedflag='-G' ;;
+	*Sun\ F*)			# Sun Fortran 8.3
+	  tmp_sharedflag='-G' ;;
+	*)
+	  tmp_sharedflag='-shared' ;;
+	esac
+	_LT_AC_TAGVAR(archive_cmds, $1)='$CC '"$tmp_sharedflag""$tmp_addflag"' $libobjs $deplibs $compiler_flags ${wl}-soname $wl$soname -o $lib'
+
+	if test $supports_anon_versioning = yes; then
+	  _LT_AC_TAGVAR(archive_expsym_cmds, $1)='$echo "{ global:" > $output_objdir/$libname.ver~
+  cat $export_symbols | sed -e "s/\(.*\)/\1;/" >> $output_objdir/$libname.ver~
+  $echo "local: *; };" >> $output_objdir/$libname.ver~
+	  $CC '"$tmp_sharedflag""$tmp_addflag"' $libobjs $deplibs $compiler_flags ${wl}-soname $wl$soname ${wl}-version-script ${wl}$output_objdir/$libname.ver -o $lib'
+	fi
+      else
+	_LT_AC_TAGVAR(ld_shlibs, $1)=no
+      fi
+      ;;
+
+    netbsd*)
+      if echo __ELF__ | $CC -E - | grep __ELF__ >/dev/null; then
+	_LT_AC_TAGVAR(archive_cmds, $1)='$LD -Bshareable $libobjs $deplibs $linker_flags -o $lib'
+	wlarc=
+      else
+	_LT_AC_TAGVAR(archive_cmds, $1)='$CC -shared $libobjs $deplibs $compiler_flags ${wl}-soname $wl$soname -o $lib'
+	_LT_AC_TAGVAR(archive_expsym_cmds, $1)='$CC -shared $libobjs $deplibs $compiler_flags ${wl}-soname $wl$soname ${wl}-retain-symbols-file $wl$export_symbols -o $lib'
+      fi
+      ;;
+
+    solaris*)
+      if $LD -v 2>&1 | grep 'BFD 2\.8' > /dev/null; then
+	_LT_AC_TAGVAR(ld_shlibs, $1)=no
+	cat <<EOF 1>&2
+
+*** Warning: The releases 2.8.* of the GNU linker cannot reliably
+*** create shared libraries on Solaris systems.  Therefore, libtool
+*** is disabling shared libraries support.  We urge you to upgrade GNU
+*** binutils to release 2.9.1 or newer.  Another option is to modify
+*** your PATH or compiler configuration so that the native linker is
+*** used, and then restart.
+
+EOF
+      elif $LD --help 2>&1 | grep ': supported targets:.* elf' > /dev/null; then
+	_LT_AC_TAGVAR(archive_cmds, $1)='$CC -shared $libobjs $deplibs $compiler_flags ${wl}-soname $wl$soname -o $lib'
+	_LT_AC_TAGVAR(archive_expsym_cmds, $1)='$CC -shared $libobjs $deplibs $compiler_flags ${wl}-soname $wl$soname ${wl}-retain-symbols-file $wl$export_symbols -o $lib'
+      else
+	_LT_AC_TAGVAR(ld_shlibs, $1)=no
+      fi
+      ;;
+
+    sysv5* | sco3.2v5* | sco5v6* | unixware* | OpenUNIX*)
+      case `$LD -v 2>&1` in
+        *\ [[01]].* | *\ 2.[[0-9]].* | *\ 2.1[[0-5]].*)
+	_LT_AC_TAGVAR(ld_shlibs, $1)=no
+	cat <<_LT_EOF 1>&2
+
+*** Warning: Releases of the GNU linker prior to 2.16.91.0.3 can not
+*** reliably create shared libraries on SCO systems.  Therefore, libtool
+*** is disabling shared libraries support.  We urge you to upgrade GNU
+*** binutils to release 2.16.91.0.3 or newer.  Another option is to modify
+*** your PATH or compiler configuration so that the native linker is
+*** used, and then restart.
+
+_LT_EOF
+	;;
+	*)
+	  if $LD --help 2>&1 | grep ': supported targets:.* elf' > /dev/null; then
+	    _LT_AC_TAGVAR(hardcode_libdir_flag_spec, $1)='`test -z "$SCOABSPATH" && echo ${wl}-rpath,$libdir`'
+	    _LT_AC_TAGVAR(archive_cmds, $1)='$CC -shared $libobjs $deplibs $compiler_flags ${wl}-soname,\${SCOABSPATH:+${install_libdir}/}$soname -o $lib'
+	    _LT_AC_TAGVAR(archive_expsym_cmds, $1)='$CC -shared $libobjs $deplibs $compiler_flags ${wl}-soname,\${SCOABSPATH:+${install_libdir}/}$soname,-retain-symbols-file,$export_symbols -o $lib'
+	  else
+	    _LT_AC_TAGVAR(ld_shlibs, $1)=no
+	  fi
+	;;
+      esac
+      ;;
+
+    sunos4*)
+      _LT_AC_TAGVAR(archive_cmds, $1)='$LD -assert pure-text -Bshareable -o $lib $libobjs $deplibs $linker_flags'
+      wlarc=
+      _LT_AC_TAGVAR(hardcode_direct, $1)=yes
+      _LT_AC_TAGVAR(hardcode_shlibpath_var, $1)=no
+      ;;
+
+    *)
+      if $LD --help 2>&1 | grep ': supported targets:.* elf' > /dev/null; then
+	_LT_AC_TAGVAR(archive_cmds, $1)='$CC -shared $libobjs $deplibs $compiler_flags ${wl}-soname $wl$soname -o $lib'
+	_LT_AC_TAGVAR(archive_expsym_cmds, $1)='$CC -shared $libobjs $deplibs $compiler_flags ${wl}-soname $wl$soname ${wl}-retain-symbols-file $wl$export_symbols -o $lib'
+      else
+	_LT_AC_TAGVAR(ld_shlibs, $1)=no
+      fi
+      ;;
+    esac
+
+    if test "$_LT_AC_TAGVAR(ld_shlibs, $1)" = no; then
+      runpath_var=
+      _LT_AC_TAGVAR(hardcode_libdir_flag_spec, $1)=
+      _LT_AC_TAGVAR(export_dynamic_flag_spec, $1)=
+      _LT_AC_TAGVAR(whole_archive_flag_spec, $1)=
+    fi
+  else
+    # PORTME fill in a description of your system's linker (not GNU ld)
+    case $host_os in
+    aix3*)
+      _LT_AC_TAGVAR(allow_undefined_flag, $1)=unsupported
+      _LT_AC_TAGVAR(always_export_symbols, $1)=yes
+      _LT_AC_TAGVAR(archive_expsym_cmds, $1)='$LD -o $output_objdir/$soname $libobjs $deplibs $linker_flags -bE:$export_symbols -T512 -H512 -bM:SRE~$AR $AR_FLAGS $lib $output_objdir/$soname'
+      # Note: this linker hardcodes the directories in LIBPATH if there
+      # are no directories specified by -L.
+      _LT_AC_TAGVAR(hardcode_minus_L, $1)=yes
+      if test "$GCC" = yes && test -z "$lt_prog_compiler_static"; then
+	# Neither direct hardcoding nor static linking is supported with a
+	# broken collect2.
+	_LT_AC_TAGVAR(hardcode_direct, $1)=unsupported
+      fi
+      ;;
+
+    aix[[4-9]]*)
+      if test "$host_cpu" = ia64; then
+	# On IA64, the linker does run time linking by default, so we don't
+	# have to do anything special.
+	aix_use_runtimelinking=no
+	exp_sym_flag='-Bexport'
+	no_entry_flag=""
+      else
+	# If we're using GNU nm, then we don't want the "-C" option.
+	# -C means demangle to AIX nm, but means don't demangle with GNU nm
+	if $NM -V 2>&1 | grep 'GNU' > /dev/null; then
+	  _LT_AC_TAGVAR(export_symbols_cmds, $1)='$NM -Bpg $libobjs $convenience | awk '\''{ if (((\[$]2 == "T") || (\[$]2 == "D") || (\[$]2 == "B")) && ([substr](\[$]3,1,1) != ".")) { print \[$]3 } }'\'' | sort -u > $export_symbols'
+	else
+	  _LT_AC_TAGVAR(export_symbols_cmds, $1)='$NM -BCpg $libobjs $convenience | awk '\''{ if (((\[$]2 == "T") || (\[$]2 == "D") || (\[$]2 == "B")) && ([substr](\[$]3,1,1) != ".")) { print \[$]3 } }'\'' | sort -u > $export_symbols'
+	fi
+	aix_use_runtimelinking=no
+
+	# Test if we are trying to use run time linking or normal
+	# AIX style linking. If -brtl is somewhere in LDFLAGS, we
+	# need to do runtime linking.
+	case $host_os in aix4.[[23]]|aix4.[[23]].*|aix[[5-9]]*)
+	  for ld_flag in $LDFLAGS; do
+  	  if (test $ld_flag = "-brtl" || test $ld_flag = "-Wl,-brtl"); then
+  	    aix_use_runtimelinking=yes
+  	    break
+  	  fi
+	  done
+	  ;;
+	esac
+
+	exp_sym_flag='-bexport'
+	no_entry_flag='-bnoentry'
+      fi
+
+      # When large executables or shared objects are built, AIX ld can
+      # have problems creating the table of contents.  If linking a library
+      # or program results in "error TOC overflow" add -mminimal-toc to
+      # CXXFLAGS/CFLAGS for g++/gcc.  In the cases where that is not
+      # enough to fix the problem, add -Wl,-bbigtoc to LDFLAGS.
+
+      _LT_AC_TAGVAR(archive_cmds, $1)=''
+      _LT_AC_TAGVAR(hardcode_direct, $1)=yes
+      _LT_AC_TAGVAR(hardcode_libdir_separator, $1)=':'
+      _LT_AC_TAGVAR(link_all_deplibs, $1)=yes
+
+      if test "$GCC" = yes; then
+	case $host_os in aix4.[[012]]|aix4.[[012]].*)
+	# We only want to do this on AIX 4.2 and lower, the check
+	# below for broken collect2 doesn't work under 4.3+
+	  collect2name=`${CC} -print-prog-name=collect2`
+	  if test -f "$collect2name" && \
+  	   strings "$collect2name" | grep resolve_lib_name >/dev/null
+	  then
+  	  # We have reworked collect2
+  	  :
+	  else
+  	  # We have old collect2
+  	  _LT_AC_TAGVAR(hardcode_direct, $1)=unsupported
+  	  # It fails to find uninstalled libraries when the uninstalled
+  	  # path is not listed in the libpath.  Setting hardcode_minus_L
+  	  # to unsupported forces relinking
+  	  _LT_AC_TAGVAR(hardcode_minus_L, $1)=yes
+  	  _LT_AC_TAGVAR(hardcode_libdir_flag_spec, $1)='-L$libdir'
+  	  _LT_AC_TAGVAR(hardcode_libdir_separator, $1)=
+	  fi
+	  ;;
+	esac
+	shared_flag='-shared'
+	if test "$aix_use_runtimelinking" = yes; then
+	  shared_flag="$shared_flag "'${wl}-G'
+	fi
+      else
+	# not using gcc
+	if test "$host_cpu" = ia64; then
+  	# VisualAge C++, Version 5.5 for AIX 5L for IA-64, Beta 3 Release
+  	# chokes on -Wl,-G. The following line is correct:
+	  shared_flag='-G'
+	else
+	  if test "$aix_use_runtimelinking" = yes; then
+	    shared_flag='${wl}-G'
+	  else
+	    shared_flag='${wl}-bM:SRE'
+	  fi
+	fi
+      fi
+
+      # It seems that -bexpall does not export symbols beginning with
+      # underscore (_), so it is better to generate a list of symbols to export.
+      _LT_AC_TAGVAR(always_export_symbols, $1)=yes
+      if test "$aix_use_runtimelinking" = yes; then
+	# Warning - without using the other runtime loading flags (-brtl),
+	# -berok will link without error, but may produce a broken library.
+	_LT_AC_TAGVAR(allow_undefined_flag, $1)='-berok'
+       # Determine the default libpath from the value encoded in an empty executable.
+       _LT_AC_SYS_LIBPATH_AIX
+       _LT_AC_TAGVAR(hardcode_libdir_flag_spec, $1)='${wl}-blibpath:$libdir:'"$aix_libpath"
+	_LT_AC_TAGVAR(archive_expsym_cmds, $1)="\$CC"' -o $output_objdir/$soname $libobjs $deplibs '"\${wl}$no_entry_flag"' $compiler_flags `if test "x${allow_undefined_flag}" != "x"; then echo "${wl}${allow_undefined_flag}"; else :; fi` '"\${wl}$exp_sym_flag:\$export_symbols $shared_flag"
+       else
+	if test "$host_cpu" = ia64; then
+	  _LT_AC_TAGVAR(hardcode_libdir_flag_spec, $1)='${wl}-R $libdir:/usr/lib:/lib'
+	  _LT_AC_TAGVAR(allow_undefined_flag, $1)="-z nodefs"
+	  _LT_AC_TAGVAR(archive_expsym_cmds, $1)="\$CC $shared_flag"' -o $output_objdir/$soname $libobjs $deplibs '"\${wl}$no_entry_flag"' $compiler_flags ${wl}${allow_undefined_flag} '"\${wl}$exp_sym_flag:\$export_symbols"
+	else
+	 # Determine the default libpath from the value encoded in an empty executable.
+	 _LT_AC_SYS_LIBPATH_AIX
+	 _LT_AC_TAGVAR(hardcode_libdir_flag_spec, $1)='${wl}-blibpath:$libdir:'"$aix_libpath"
+	  # Warning - without using the other run time loading flags,
+	  # -berok will link without error, but may produce a broken library.
+	  _LT_AC_TAGVAR(no_undefined_flag, $1)=' ${wl}-bernotok'
+	  _LT_AC_TAGVAR(allow_undefined_flag, $1)=' ${wl}-berok'
+	  # Exported symbols can be pulled into shared objects from archives
+	  _LT_AC_TAGVAR(whole_archive_flag_spec, $1)='$convenience'
+	  _LT_AC_TAGVAR(archive_cmds_need_lc, $1)=yes
+	  # This is similar to how AIX traditionally builds its shared libraries.
+	  _LT_AC_TAGVAR(archive_expsym_cmds, $1)="\$CC $shared_flag"' -o $output_objdir/$soname $libobjs $deplibs ${wl}-bnoentry $compiler_flags ${wl}-bE:$export_symbols${allow_undefined_flag}~$AR $AR_FLAGS $output_objdir/$libname$release.a $output_objdir/$soname'
+	fi
+      fi
+      ;;
+
+    amigaos*)
+      _LT_AC_TAGVAR(archive_cmds, $1)='$rm $output_objdir/a2ixlibrary.data~$echo "#define NAME $libname" > $output_objdir/a2ixlibrary.data~$echo "#define LIBRARY_ID 1" >> $output_objdir/a2ixlibrary.data~$echo "#define VERSION $major" >> $output_objdir/a2ixlibrary.data~$echo "#define REVISION $revision" >> $output_objdir/a2ixlibrary.data~$AR $AR_FLAGS $lib $libobjs~$RANLIB $lib~(cd $output_objdir && a2ixlibrary -32)'
+      _LT_AC_TAGVAR(hardcode_libdir_flag_spec, $1)='-L$libdir'
+      _LT_AC_TAGVAR(hardcode_minus_L, $1)=yes
+      # see comment about different semantics on the GNU ld section
+      _LT_AC_TAGVAR(ld_shlibs, $1)=no
+      ;;
+
+    bsdi[[45]]*)
+      _LT_AC_TAGVAR(export_dynamic_flag_spec, $1)=-rdynamic
+      ;;
+
+    cygwin* | mingw* | pw32*)
+      # When not using gcc, we currently assume that we are using
+      # Microsoft Visual C++.
+      # hardcode_libdir_flag_spec is actually meaningless, as there is
+      # no search path for DLLs.
+      _LT_AC_TAGVAR(hardcode_libdir_flag_spec, $1)=' '
+      _LT_AC_TAGVAR(allow_undefined_flag, $1)=unsupported
+      # Tell ltmain to make .lib files, not .a files.
+      libext=lib
+      # Tell ltmain to make .dll files, not .so files.
+      shrext_cmds=".dll"
+      # FIXME: Setting linknames here is a bad hack.
+      _LT_AC_TAGVAR(archive_cmds, $1)='$CC -o $lib $libobjs $compiler_flags `echo "$deplibs" | $SED -e '\''s/ -lc$//'\''` -link -dll~linknames='
+      # The linker will automatically build a .lib file if we build a DLL.
+      _LT_AC_TAGVAR(old_archive_From_new_cmds, $1)='true'
+      # FIXME: Should let the user specify the lib program.
+      _LT_AC_TAGVAR(old_archive_cmds, $1)='lib -OUT:$oldlib$oldobjs$old_deplibs'
+      _LT_AC_TAGVAR(fix_srcfile_path, $1)='`cygpath -w "$srcfile"`'
+      _LT_AC_TAGVAR(enable_shared_with_static_runtimes, $1)=yes
+      ;;
+
+    darwin* | rhapsody*)
+      case $host_os in
+        rhapsody* | darwin1.[[012]])
+         _LT_AC_TAGVAR(allow_undefined_flag, $1)='${wl}-undefined ${wl}suppress'
+         ;;
+       *) # Darwin 1.3 on
+         if test -z ${MACOSX_DEPLOYMENT_TARGET} ; then
+           _LT_AC_TAGVAR(allow_undefined_flag, $1)='${wl}-flat_namespace ${wl}-undefined ${wl}suppress'
+         else
+           case ${MACOSX_DEPLOYMENT_TARGET} in
+             10.[[012]])
+               _LT_AC_TAGVAR(allow_undefined_flag, $1)='${wl}-flat_namespace ${wl}-undefined ${wl}suppress'
+               ;;
+             10.*)
+               _LT_AC_TAGVAR(allow_undefined_flag, $1)='${wl}-undefined ${wl}dynamic_lookup'
+               ;;
+           esac
+         fi
+         ;;
+      esac
+      _LT_AC_TAGVAR(archive_cmds_need_lc, $1)=no
+      _LT_AC_TAGVAR(hardcode_direct, $1)=no
+      _LT_AC_TAGVAR(hardcode_automatic, $1)=yes
+      _LT_AC_TAGVAR(hardcode_shlibpath_var, $1)=unsupported
+      _LT_AC_TAGVAR(whole_archive_flag_spec, $1)=''
+      _LT_AC_TAGVAR(link_all_deplibs, $1)=yes
+    if test "$GCC" = yes ; then
+    	output_verbose_link_cmd='echo'
+        _LT_AC_TAGVAR(archive_cmds, $1)="\$CC -dynamiclib \$allow_undefined_flag -o \$lib \$libobjs \$deplibs \$compiler_flags -install_name \$rpath/\$soname \$verstring $_lt_dar_single_mod${_lt_dsymutil}"
+        _LT_AC_TAGVAR(module_cmds, $1)="\$CC \$allow_undefined_flag -o \$lib -bundle \$libobjs \$deplibs \$compiler_flags${_lt_dsymutil}"
+        _LT_AC_TAGVAR(archive_expsym_cmds, $1)="sed 's,^,_,' < \$export_symbols > \$output_objdir/\${libname}-symbols.expsym~\$CC -dynamiclib \$allow_undefined_flag -o \$lib \$libobjs \$deplibs \$compiler_flags -install_name \$rpath/\$soname \$verstring ${_lt_dar_single_mod}${_lt_dar_export_syms}${_lt_dsymutil}"
+        _LT_AC_TAGVAR(module_expsym_cmds, $1)="sed -e 's,^,_,' < \$export_symbols > \$output_objdir/\${libname}-symbols.expsym~\$CC \$allow_undefined_flag -o \$lib -bundle \$libobjs \$deplibs \$compiler_flags${_lt_dar_export_syms}${_lt_dsymutil}"
+    else
+      case $cc_basename in
+        xlc*)
+         output_verbose_link_cmd='echo'
+         _LT_AC_TAGVAR(archive_cmds, $1)='$CC -qmkshrobj $allow_undefined_flag -o $lib $libobjs $deplibs $compiler_flags ${wl}-install_name ${wl}`echo $rpath/$soname` $xlcverstring'
+         _LT_AC_TAGVAR(module_cmds, $1)='$CC $allow_undefined_flag -o $lib -bundle $libobjs $deplibs$compiler_flags'
+          # Don't fix this by using the ld -exported_symbols_list flag, it doesn't exist in older darwin lds
+         _LT_AC_TAGVAR(archive_expsym_cmds, $1)='sed -e "s,#.*,," -e "s,^[    ]*,," -e "s,^\(..*\),_&," < $export_symbols > $output_objdir/${libname}-symbols.expsym~$CC -qmkshrobj $allow_undefined_flag -o $lib $libobjs $deplibs $compiler_flags ${wl}-install_name ${wl}$rpath/$soname $xlcverstring~nmedit -s $output_objdir/${libname}-symbols.expsym ${lib}'
+          _LT_AC_TAGVAR(module_expsym_cmds, $1)='sed -e "s,#.*,," -e "s,^[    ]*,," -e "s,^\(..*\),_&," < $export_symbols > $output_objdir/${libname}-symbols.expsym~$CC $allow_undefined_flag  -o $lib -bundle $libobjs $deplibs$compiler_flags~nmedit -s $output_objdir/${libname}-symbols.expsym ${lib}'
+          ;;
+       *)
+         _LT_AC_TAGVAR(ld_shlibs, $1)=no
+          ;;
+      esac
+    fi
+      ;;
+
+    dgux*)
+      _LT_AC_TAGVAR(archive_cmds, $1)='$LD -G -h $soname -o $lib $libobjs $deplibs $linker_flags'
+      _LT_AC_TAGVAR(hardcode_libdir_flag_spec, $1)='-L$libdir'
+      _LT_AC_TAGVAR(hardcode_shlibpath_var, $1)=no
+      ;;
+
+    freebsd1*)
+      _LT_AC_TAGVAR(ld_shlibs, $1)=no
+      ;;
+
+    # FreeBSD 2.2.[012] allows us to include c++rt0.o to get C++ constructor
+    # support.  Future versions do this automatically, but an explicit c++rt0.o
+    # does not break anything, and helps significantly (at the cost of a little
+    # extra space).
+    freebsd2.2*)
+      _LT_AC_TAGVAR(archive_cmds, $1)='$LD -Bshareable -o $lib $libobjs $deplibs $linker_flags /usr/lib/c++rt0.o'
+      _LT_AC_TAGVAR(hardcode_libdir_flag_spec, $1)='-R$libdir'
+      _LT_AC_TAGVAR(hardcode_direct, $1)=yes
+      _LT_AC_TAGVAR(hardcode_shlibpath_var, $1)=no
+      ;;
+
+    # Unfortunately, older versions of FreeBSD 2 do not have this feature.
+    freebsd2*)
+      _LT_AC_TAGVAR(archive_cmds, $1)='$LD -Bshareable -o $lib $libobjs $deplibs $linker_flags'
+      _LT_AC_TAGVAR(hardcode_direct, $1)=yes
+      _LT_AC_TAGVAR(hardcode_minus_L, $1)=yes
+      _LT_AC_TAGVAR(hardcode_shlibpath_var, $1)=no
+      ;;
+
+    # FreeBSD 3 and greater uses gcc -shared to do shared libraries.
+    freebsd* | dragonfly*)
+      _LT_AC_TAGVAR(archive_cmds, $1)='$CC -shared -o $lib $libobjs $deplibs $compiler_flags'
+      _LT_AC_TAGVAR(hardcode_libdir_flag_spec, $1)='-R$libdir'
+      _LT_AC_TAGVAR(hardcode_direct, $1)=yes
+      _LT_AC_TAGVAR(hardcode_shlibpath_var, $1)=no
+      ;;
+
+    hpux9*)
+      if test "$GCC" = yes; then
+	_LT_AC_TAGVAR(archive_cmds, $1)='$rm $output_objdir/$soname~$CC -shared -fPIC ${wl}+b ${wl}$install_libdir -o $output_objdir/$soname $libobjs $deplibs $compiler_flags~test $output_objdir/$soname = $lib || mv $output_objdir/$soname $lib'
+      else
+	_LT_AC_TAGVAR(archive_cmds, $1)='$rm $output_objdir/$soname~$LD -b +b $install_libdir -o $output_objdir/$soname $libobjs $deplibs $linker_flags~test $output_objdir/$soname = $lib || mv $output_objdir/$soname $lib'
+      fi
+      _LT_AC_TAGVAR(hardcode_libdir_flag_spec, $1)='${wl}+b ${wl}$libdir'
+      _LT_AC_TAGVAR(hardcode_libdir_separator, $1)=:
+      _LT_AC_TAGVAR(hardcode_direct, $1)=yes
+
+      # hardcode_minus_L: Not really in the search PATH,
+      # but as the default location of the library.
+      _LT_AC_TAGVAR(hardcode_minus_L, $1)=yes
+      _LT_AC_TAGVAR(export_dynamic_flag_spec, $1)='${wl}-E'
+      ;;
+
+    hpux10*)
+      if test "$GCC" = yes -a "$with_gnu_ld" = no; then
+	_LT_AC_TAGVAR(archive_cmds, $1)='$CC -shared -fPIC ${wl}+h ${wl}$soname ${wl}+b ${wl}$install_libdir -o $lib $libobjs $deplibs $compiler_flags'
+      else
+	_LT_AC_TAGVAR(archive_cmds, $1)='$LD -b +h $soname +b $install_libdir -o $lib $libobjs $deplibs $linker_flags'
+      fi
+      if test "$with_gnu_ld" = no; then
+	_LT_AC_TAGVAR(hardcode_libdir_flag_spec, $1)='${wl}+b ${wl}$libdir'
+	_LT_AC_TAGVAR(hardcode_libdir_separator, $1)=:
+
+	_LT_AC_TAGVAR(hardcode_direct, $1)=yes
+	_LT_AC_TAGVAR(export_dynamic_flag_spec, $1)='${wl}-E'
+
+	# hardcode_minus_L: Not really in the search PATH,
+	# but as the default location of the library.
+	_LT_AC_TAGVAR(hardcode_minus_L, $1)=yes
+      fi
+      ;;
+
+    hpux11*)
+      if test "$GCC" = yes -a "$with_gnu_ld" = no; then
+	case $host_cpu in
+	hppa*64*)
+	  _LT_AC_TAGVAR(archive_cmds, $1)='$CC -shared ${wl}+h ${wl}$soname -o $lib $libobjs $deplibs $compiler_flags'
+	  ;;
+	ia64*)
+	  _LT_AC_TAGVAR(archive_cmds, $1)='$CC -shared ${wl}+h ${wl}$soname ${wl}+nodefaultrpath -o $lib $libobjs $deplibs $compiler_flags'
+	  ;;
+	*)
+	  _LT_AC_TAGVAR(archive_cmds, $1)='$CC -shared -fPIC ${wl}+h ${wl}$soname ${wl}+b ${wl}$install_libdir -o $lib $libobjs $deplibs $compiler_flags'
+	  ;;
+	esac
+      else
+	case $host_cpu in
+	hppa*64*)
+	  _LT_AC_TAGVAR(archive_cmds, $1)='$CC -b ${wl}+h ${wl}$soname -o $lib $libobjs $deplibs $compiler_flags'
+	  ;;
+	ia64*)
+	  _LT_AC_TAGVAR(archive_cmds, $1)='$CC -b ${wl}+h ${wl}$soname ${wl}+nodefaultrpath -o $lib $libobjs $deplibs $compiler_flags'
+	  ;;
+	*)
+	  _LT_AC_TAGVAR(archive_cmds, $1)='$CC -b ${wl}+h ${wl}$soname ${wl}+b ${wl}$install_libdir -o $lib $libobjs $deplibs $compiler_flags'
+	  ;;
+	esac
+      fi
+      if test "$with_gnu_ld" = no; then
+	_LT_AC_TAGVAR(hardcode_libdir_flag_spec, $1)='${wl}+b ${wl}$libdir'
+	_LT_AC_TAGVAR(hardcode_libdir_separator, $1)=:
+
+	case $host_cpu in
+	hppa*64*|ia64*)
+	  _LT_AC_TAGVAR(hardcode_libdir_flag_spec_ld, $1)='+b $libdir'
+	  _LT_AC_TAGVAR(hardcode_direct, $1)=no
+	  _LT_AC_TAGVAR(hardcode_shlibpath_var, $1)=no
+	  ;;
+	*)
+	  _LT_AC_TAGVAR(hardcode_direct, $1)=yes
+	  _LT_AC_TAGVAR(export_dynamic_flag_spec, $1)='${wl}-E'
+
+	  # hardcode_minus_L: Not really in the search PATH,
+	  # but as the default location of the library.
+	  _LT_AC_TAGVAR(hardcode_minus_L, $1)=yes
+	  ;;
+	esac
+      fi
+      ;;
+
+    irix5* | irix6* | nonstopux*)
+      if test "$GCC" = yes; then
+	_LT_AC_TAGVAR(archive_cmds, $1)='$CC -shared $libobjs $deplibs $compiler_flags ${wl}-soname ${wl}$soname `test -n "$verstring" && echo ${wl}-set_version ${wl}$verstring` ${wl}-update_registry ${wl}${output_objdir}/so_locations -o $lib'
+      else
+	_LT_AC_TAGVAR(archive_cmds, $1)='$LD -shared $libobjs $deplibs $linker_flags -soname $soname `test -n "$verstring" && echo -set_version $verstring` -update_registry ${output_objdir}/so_locations -o $lib'
+	_LT_AC_TAGVAR(hardcode_libdir_flag_spec_ld, $1)='-rpath $libdir'
+      fi
+      _LT_AC_TAGVAR(hardcode_libdir_flag_spec, $1)='${wl}-rpath ${wl}$libdir'
+      _LT_AC_TAGVAR(hardcode_libdir_separator, $1)=:
+      _LT_AC_TAGVAR(link_all_deplibs, $1)=yes
+      ;;
+
+    netbsd*)
+      if echo __ELF__ | $CC -E - | grep __ELF__ >/dev/null; then
+	_LT_AC_TAGVAR(archive_cmds, $1)='$LD -Bshareable -o $lib $libobjs $deplibs $linker_flags'  # a.out
+      else
+	_LT_AC_TAGVAR(archive_cmds, $1)='$LD -shared -o $lib $libobjs $deplibs $linker_flags'      # ELF
+      fi
+      _LT_AC_TAGVAR(hardcode_libdir_flag_spec, $1)='-R$libdir'
+      _LT_AC_TAGVAR(hardcode_direct, $1)=yes
+      _LT_AC_TAGVAR(hardcode_shlibpath_var, $1)=no
+      ;;
+
+    newsos6)
+      _LT_AC_TAGVAR(archive_cmds, $1)='$LD -G -h $soname -o $lib $libobjs $deplibs $linker_flags'
+      _LT_AC_TAGVAR(hardcode_direct, $1)=yes
+      _LT_AC_TAGVAR(hardcode_libdir_flag_spec, $1)='${wl}-rpath ${wl}$libdir'
+      _LT_AC_TAGVAR(hardcode_libdir_separator, $1)=:
+      _LT_AC_TAGVAR(hardcode_shlibpath_var, $1)=no
+      ;;
+
+    openbsd*)
+      if test -f /usr/libexec/ld.so; then
+	_LT_AC_TAGVAR(hardcode_direct, $1)=yes
+	_LT_AC_TAGVAR(hardcode_shlibpath_var, $1)=no
+	if test -z "`echo __ELF__ | $CC -E - | grep __ELF__`" || test "$host_os-$host_cpu" = "openbsd2.8-powerpc"; then
+	  _LT_AC_TAGVAR(archive_cmds, $1)='$CC -shared $pic_flag -o $lib $libobjs $deplibs $compiler_flags'
+	  _LT_AC_TAGVAR(archive_expsym_cmds, $1)='$CC -shared $pic_flag -o $lib $libobjs $deplibs $compiler_flags ${wl}-retain-symbols-file,$export_symbols'
+	  _LT_AC_TAGVAR(hardcode_libdir_flag_spec, $1)='${wl}-rpath,$libdir'
+	  _LT_AC_TAGVAR(export_dynamic_flag_spec, $1)='${wl}-E'
+	else
+	  case $host_os in
+	   openbsd[[01]].* | openbsd2.[[0-7]] | openbsd2.[[0-7]].*)
+	     _LT_AC_TAGVAR(archive_cmds, $1)='$LD -Bshareable -o $lib $libobjs $deplibs $linker_flags'
+	     _LT_AC_TAGVAR(hardcode_libdir_flag_spec, $1)='-R$libdir'
+	     ;;
+	   *)
+	     _LT_AC_TAGVAR(archive_cmds, $1)='$CC -shared $pic_flag -o $lib $libobjs $deplibs $compiler_flags'
+	     _LT_AC_TAGVAR(hardcode_libdir_flag_spec, $1)='${wl}-rpath,$libdir'
+	     ;;
+	  esac
+        fi
+      else
+	_LT_AC_TAGVAR(ld_shlibs, $1)=no
+      fi
+      ;;
+
+    os2*)
+      _LT_AC_TAGVAR(hardcode_libdir_flag_spec, $1)='-L$libdir'
+      _LT_AC_TAGVAR(hardcode_minus_L, $1)=yes
+      _LT_AC_TAGVAR(allow_undefined_flag, $1)=unsupported
+      _LT_AC_TAGVAR(archive_cmds, $1)='$echo "LIBRARY $libname INITINSTANCE" > $output_objdir/$libname.def~$echo "DESCRIPTION \"$libname\"" >> $output_objdir/$libname.def~$echo DATA >> $output_objdir/$libname.def~$echo " SINGLE NONSHARED" >> $output_objdir/$libname.def~$echo EXPORTS >> $output_objdir/$libname.def~emxexp $libobjs >> $output_objdir/$libname.def~$CC -Zdll -Zcrtdll -o $lib $libobjs $deplibs $compiler_flags $output_objdir/$libname.def'
+      _LT_AC_TAGVAR(old_archive_From_new_cmds, $1)='emximp -o $output_objdir/$libname.a $output_objdir/$libname.def'
+      ;;
+
+    osf3*)
+      if test "$GCC" = yes; then
+	_LT_AC_TAGVAR(allow_undefined_flag, $1)=' ${wl}-expect_unresolved ${wl}\*'
+	_LT_AC_TAGVAR(archive_cmds, $1)='$CC -shared${allow_undefined_flag} $libobjs $deplibs $compiler_flags ${wl}-soname ${wl}$soname `test -n "$verstring" && echo ${wl}-set_version ${wl}$verstring` ${wl}-update_registry ${wl}${output_objdir}/so_locations -o $lib'
+      else
+	_LT_AC_TAGVAR(allow_undefined_flag, $1)=' -expect_unresolved \*'
+	_LT_AC_TAGVAR(archive_cmds, $1)='$LD -shared${allow_undefined_flag} $libobjs $deplibs $linker_flags -soname $soname `test -n "$verstring" && echo -set_version $verstring` -update_registry ${output_objdir}/so_locations -o $lib'
+      fi
+      _LT_AC_TAGVAR(hardcode_libdir_flag_spec, $1)='${wl}-rpath ${wl}$libdir'
+      _LT_AC_TAGVAR(hardcode_libdir_separator, $1)=:
+      ;;
+
+    osf4* | osf5*)	# as osf3* with the addition of -msym flag
+      if test "$GCC" = yes; then
+	_LT_AC_TAGVAR(allow_undefined_flag, $1)=' ${wl}-expect_unresolved ${wl}\*'
+	_LT_AC_TAGVAR(archive_cmds, $1)='$CC -shared${allow_undefined_flag} $libobjs $deplibs $compiler_flags ${wl}-msym ${wl}-soname ${wl}$soname `test -n "$verstring" && echo ${wl}-set_version ${wl}$verstring` ${wl}-update_registry ${wl}${output_objdir}/so_locations -o $lib'
+	_LT_AC_TAGVAR(hardcode_libdir_flag_spec, $1)='${wl}-rpath ${wl}$libdir'
+      else
+	_LT_AC_TAGVAR(allow_undefined_flag, $1)=' -expect_unresolved \*'
+	_LT_AC_TAGVAR(archive_cmds, $1)='$LD -shared${allow_undefined_flag} $libobjs $deplibs $linker_flags -msym -soname $soname `test -n "$verstring" && echo -set_version $verstring` -update_registry ${output_objdir}/so_locations -o $lib'
+	_LT_AC_TAGVAR(archive_expsym_cmds, $1)='for i in `cat $export_symbols`; do printf "%s %s\\n" -exported_symbol "\$i" >> $lib.exp; done; echo "-hidden">> $lib.exp~
+	$LD -shared${allow_undefined_flag} -input $lib.exp $linker_flags $libobjs $deplibs -soname $soname `test -n "$verstring" && echo -set_version $verstring` -update_registry ${output_objdir}/so_locations -o $lib~$rm $lib.exp'
+
+	# Both c and cxx compiler support -rpath directly
+	_LT_AC_TAGVAR(hardcode_libdir_flag_spec, $1)='-rpath $libdir'
+      fi
+      _LT_AC_TAGVAR(hardcode_libdir_separator, $1)=:
+      ;;
+
+    solaris*)
+      _LT_AC_TAGVAR(no_undefined_flag, $1)=' -z text'
+      if test "$GCC" = yes; then
+	wlarc='${wl}'
+	_LT_AC_TAGVAR(archive_cmds, $1)='$CC -shared ${wl}-h ${wl}$soname -o $lib $libobjs $deplibs $compiler_flags'
+	_LT_AC_TAGVAR(archive_expsym_cmds, $1)='$echo "{ global:" > $lib.exp~cat $export_symbols | $SED -e "s/\(.*\)/\1;/" >> $lib.exp~$echo "local: *; };" >> $lib.exp~
+	  $CC -shared ${wl}-M ${wl}$lib.exp ${wl}-h ${wl}$soname -o $lib $libobjs $deplibs $compiler_flags~$rm $lib.exp'
+      else
+	wlarc=''
+	_LT_AC_TAGVAR(archive_cmds, $1)='$LD -G${allow_undefined_flag} -h $soname -o $lib $libobjs $deplibs $linker_flags'
+	_LT_AC_TAGVAR(archive_expsym_cmds, $1)='$echo "{ global:" > $lib.exp~cat $export_symbols | $SED -e "s/\(.*\)/\1;/" >> $lib.exp~$echo "local: *; };" >> $lib.exp~
+  	$LD -G${allow_undefined_flag} -M $lib.exp -h $soname -o $lib $libobjs $deplibs $linker_flags~$rm $lib.exp'
+      fi
+      _LT_AC_TAGVAR(hardcode_libdir_flag_spec, $1)='-R$libdir'
+      _LT_AC_TAGVAR(hardcode_shlibpath_var, $1)=no
+      case $host_os in
+      solaris2.[[0-5]] | solaris2.[[0-5]].*) ;;
+      *)
+	# The compiler driver will combine and reorder linker options,
+	# but understands `-z linker_flag'.  GCC discards it without `$wl',
+	# but is careful enough not to reorder.
+ 	# Supported since Solaris 2.6 (maybe 2.5.1?)
+	if test "$GCC" = yes; then
+	  _LT_AC_TAGVAR(whole_archive_flag_spec, $1)='${wl}-z ${wl}allextract$convenience ${wl}-z ${wl}defaultextract'
+	else
+	  _LT_AC_TAGVAR(whole_archive_flag_spec, $1)='-z allextract$convenience -z defaultextract'
+	fi
+	;;
+      esac
+      _LT_AC_TAGVAR(link_all_deplibs, $1)=yes
+      ;;
+
+    sunos4*)
+      if test "x$host_vendor" = xsequent; then
+	# Use $CC to link under sequent, because it throws in some extra .o
+	# files that make .init and .fini sections work.
+	_LT_AC_TAGVAR(archive_cmds, $1)='$CC -G ${wl}-h $soname -o $lib $libobjs $deplibs $compiler_flags'
+      else
+	_LT_AC_TAGVAR(archive_cmds, $1)='$LD -assert pure-text -Bstatic -o $lib $libobjs $deplibs $linker_flags'
+      fi
+      _LT_AC_TAGVAR(hardcode_libdir_flag_spec, $1)='-L$libdir'
+      _LT_AC_TAGVAR(hardcode_direct, $1)=yes
+      _LT_AC_TAGVAR(hardcode_minus_L, $1)=yes
+      _LT_AC_TAGVAR(hardcode_shlibpath_var, $1)=no
+      ;;
+
+    sysv4)
+      case $host_vendor in
+	sni)
+	  _LT_AC_TAGVAR(archive_cmds, $1)='$LD -G -h $soname -o $lib $libobjs $deplibs $linker_flags'
+	  _LT_AC_TAGVAR(hardcode_direct, $1)=yes # is this really true???
+	;;
+	siemens)
+	  ## LD is ld it makes a PLAMLIB
+	  ## CC just makes a GrossModule.
+	  _LT_AC_TAGVAR(archive_cmds, $1)='$LD -G -o $lib $libobjs $deplibs $linker_flags'
+	  _LT_AC_TAGVAR(reload_cmds, $1)='$CC -r -o $output$reload_objs'
+	  _LT_AC_TAGVAR(hardcode_direct, $1)=no
+        ;;
+	motorola)
+	  _LT_AC_TAGVAR(archive_cmds, $1)='$LD -G -h $soname -o $lib $libobjs $deplibs $linker_flags'
+	  _LT_AC_TAGVAR(hardcode_direct, $1)=no #Motorola manual says yes, but my tests say they lie
+	;;
+      esac
+      runpath_var='LD_RUN_PATH'
+      _LT_AC_TAGVAR(hardcode_shlibpath_var, $1)=no
+      ;;
+
+    sysv4.3*)
+      _LT_AC_TAGVAR(archive_cmds, $1)='$LD -G -h $soname -o $lib $libobjs $deplibs $linker_flags'
+      _LT_AC_TAGVAR(hardcode_shlibpath_var, $1)=no
+      _LT_AC_TAGVAR(export_dynamic_flag_spec, $1)='-Bexport'
+      ;;
+
+    sysv4*MP*)
+      if test -d /usr/nec; then
+	_LT_AC_TAGVAR(archive_cmds, $1)='$LD -G -h $soname -o $lib $libobjs $deplibs $linker_flags'
+	_LT_AC_TAGVAR(hardcode_shlibpath_var, $1)=no
+	runpath_var=LD_RUN_PATH
+	hardcode_runpath_var=yes
+	_LT_AC_TAGVAR(ld_shlibs, $1)=yes
+      fi
+      ;;
+
+    sysv4*uw2* | sysv5OpenUNIX* | sysv5UnixWare7.[[01]].[[10]]* | unixware7* | sco3.2v5.0.[[024]]*)
+      _LT_AC_TAGVAR(no_undefined_flag, $1)='${wl}-z,text'
+      _LT_AC_TAGVAR(archive_cmds_need_lc, $1)=no
+      _LT_AC_TAGVAR(hardcode_shlibpath_var, $1)=no
+      runpath_var='LD_RUN_PATH'
+
+      if test "$GCC" = yes; then
+	_LT_AC_TAGVAR(archive_cmds, $1)='$CC -shared ${wl}-h,$soname -o $lib $libobjs $deplibs $compiler_flags'
+	_LT_AC_TAGVAR(archive_expsym_cmds, $1)='$CC -shared ${wl}-Bexport:$export_symbols ${wl}-h,$soname -o $lib $libobjs $deplibs $compiler_flags'
+      else
+	_LT_AC_TAGVAR(archive_cmds, $1)='$CC -G ${wl}-h,$soname -o $lib $libobjs $deplibs $compiler_flags'
+	_LT_AC_TAGVAR(archive_expsym_cmds, $1)='$CC -G ${wl}-Bexport:$export_symbols ${wl}-h,$soname -o $lib $libobjs $deplibs $compiler_flags'
+      fi
+      ;;
+
+    sysv5* | sco3.2v5* | sco5v6*)
+      # Note: We can NOT use -z defs as we might desire, because we do not
+      # link with -lc, and that would cause any symbols used from libc to
+      # always be unresolved, which means just about no library would
+      # ever link correctly.  If we're not using GNU ld we use -z text
+      # though, which does catch some bad symbols but isn't as heavy-handed
+      # as -z defs.
+      _LT_AC_TAGVAR(no_undefined_flag, $1)='${wl}-z,text'
+      _LT_AC_TAGVAR(allow_undefined_flag, $1)='${wl}-z,nodefs'
+      _LT_AC_TAGVAR(archive_cmds_need_lc, $1)=no
+      _LT_AC_TAGVAR(hardcode_shlibpath_var, $1)=no
+      _LT_AC_TAGVAR(hardcode_libdir_flag_spec, $1)='`test -z "$SCOABSPATH" && echo ${wl}-R,$libdir`'
+      _LT_AC_TAGVAR(hardcode_libdir_separator, $1)=':'
+      _LT_AC_TAGVAR(link_all_deplibs, $1)=yes
+      _LT_AC_TAGVAR(export_dynamic_flag_spec, $1)='${wl}-Bexport'
+      runpath_var='LD_RUN_PATH'
+
+      if test "$GCC" = yes; then
+	_LT_AC_TAGVAR(archive_cmds, $1)='$CC -shared ${wl}-h,\${SCOABSPATH:+${install_libdir}/}$soname -o $lib $libobjs $deplibs $compiler_flags'
+	_LT_AC_TAGVAR(archive_expsym_cmds, $1)='$CC -shared ${wl}-Bexport:$export_symbols ${wl}-h,\${SCOABSPATH:+${install_libdir}/}$soname -o $lib $libobjs $deplibs $compiler_flags'
+      else
+	_LT_AC_TAGVAR(archive_cmds, $1)='$CC -G ${wl}-h,\${SCOABSPATH:+${install_libdir}/}$soname -o $lib $libobjs $deplibs $compiler_flags'
+	_LT_AC_TAGVAR(archive_expsym_cmds, $1)='$CC -G ${wl}-Bexport:$export_symbols ${wl}-h,\${SCOABSPATH:+${install_libdir}/}$soname -o $lib $libobjs $deplibs $compiler_flags'
+      fi
+      ;;
+
+    uts4*)
+      _LT_AC_TAGVAR(archive_cmds, $1)='$LD -G -h $soname -o $lib $libobjs $deplibs $linker_flags'
+      _LT_AC_TAGVAR(hardcode_libdir_flag_spec, $1)='-L$libdir'
+      _LT_AC_TAGVAR(hardcode_shlibpath_var, $1)=no
+      ;;
+
+    *)
+      _LT_AC_TAGVAR(ld_shlibs, $1)=no
+      ;;
+    esac
+  fi
+])
+AC_MSG_RESULT([$_LT_AC_TAGVAR(ld_shlibs, $1)])
+test "$_LT_AC_TAGVAR(ld_shlibs, $1)" = no && can_build_shared=no
+
+#
+# Do we need to explicitly link libc?
+#
+case "x$_LT_AC_TAGVAR(archive_cmds_need_lc, $1)" in
+x|xyes)
+  # Assume -lc should be added
+  _LT_AC_TAGVAR(archive_cmds_need_lc, $1)=yes
+
+  if test "$enable_shared" = yes && test "$GCC" = yes; then
+    case $_LT_AC_TAGVAR(archive_cmds, $1) in
+    *'~'*)
+      # FIXME: we may have to deal with multi-command sequences.
+      ;;
+    '$CC '*)
+      # Test whether the compiler implicitly links with -lc since on some
+      # systems, -lgcc has to come before -lc. If gcc already passes -lc
+      # to ld, don't add -lc before -lgcc.
+      AC_MSG_CHECKING([whether -lc should be explicitly linked in])
+      $rm conftest*
+      echo "$lt_simple_compile_test_code" > conftest.$ac_ext
+
+      if AC_TRY_EVAL(ac_compile) 2>conftest.err; then
+        soname=conftest
+        lib=conftest
+        libobjs=conftest.$ac_objext
+        deplibs=
+        wl=$_LT_AC_TAGVAR(lt_prog_compiler_wl, $1)
+	pic_flag=$_LT_AC_TAGVAR(lt_prog_compiler_pic, $1)
+        compiler_flags=-v
+        linker_flags=-v
+        verstring=
+        output_objdir=.
+        libname=conftest
+        lt_save_allow_undefined_flag=$_LT_AC_TAGVAR(allow_undefined_flag, $1)
+        _LT_AC_TAGVAR(allow_undefined_flag, $1)=
+        if AC_TRY_EVAL(_LT_AC_TAGVAR(archive_cmds, $1) 2\>\&1 \| grep \" -lc \" \>/dev/null 2\>\&1)
+        then
+	  _LT_AC_TAGVAR(archive_cmds_need_lc, $1)=no
+        else
+	  _LT_AC_TAGVAR(archive_cmds_need_lc, $1)=yes
+        fi
+        _LT_AC_TAGVAR(allow_undefined_flag, $1)=$lt_save_allow_undefined_flag
+      else
+        cat conftest.err 1>&5
+      fi
+      $rm conftest*
+      AC_MSG_RESULT([$_LT_AC_TAGVAR(archive_cmds_need_lc, $1)])
+      ;;
+    esac
+  fi
+  ;;
+esac
+])# AC_LIBTOOL_PROG_LD_SHLIBS
+
+
+# _LT_AC_FILE_LTDLL_C
+# -------------------
+# Be careful that the start marker always follows a newline.
+AC_DEFUN([_LT_AC_FILE_LTDLL_C], [
+# /* ltdll.c starts here */
+# #define WIN32_LEAN_AND_MEAN
+# #include <windows.h>
+# #undef WIN32_LEAN_AND_MEAN
+# #include <stdio.h>
+#
+# #ifndef __CYGWIN__
+# #  ifdef __CYGWIN32__
+# #    define __CYGWIN__ __CYGWIN32__
+# #  endif
+# #endif
+#
+# #ifdef __cplusplus
+# extern "C" {
+# #endif
+# BOOL APIENTRY DllMain (HINSTANCE hInst, DWORD reason, LPVOID reserved);
+# #ifdef __cplusplus
+# }
+# #endif
+#
+# #ifdef __CYGWIN__
+# #include <cygwin/cygwin_dll.h>
+# DECLARE_CYGWIN_DLL( DllMain );
+# #endif
+# HINSTANCE __hDllInstance_base;
+#
+# BOOL APIENTRY
+# DllMain (HINSTANCE hInst, DWORD reason, LPVOID reserved)
+# {
+#   __hDllInstance_base = hInst;
+#   return TRUE;
+# }
+# /* ltdll.c ends here */
+])# _LT_AC_FILE_LTDLL_C
+
+
+# _LT_AC_TAGVAR(VARNAME, [TAGNAME])
+# ---------------------------------
+AC_DEFUN([_LT_AC_TAGVAR], [ifelse([$2], [], [$1], [$1_$2])])
+
+
+# old names
+AC_DEFUN([AM_PROG_LIBTOOL],   [AC_PROG_LIBTOOL])
+AC_DEFUN([AM_ENABLE_SHARED],  [AC_ENABLE_SHARED($@)])
+AC_DEFUN([AM_ENABLE_STATIC],  [AC_ENABLE_STATIC($@)])
+AC_DEFUN([AM_DISABLE_SHARED], [AC_DISABLE_SHARED($@)])
+AC_DEFUN([AM_DISABLE_STATIC], [AC_DISABLE_STATIC($@)])
+AC_DEFUN([AM_PROG_LD],        [AC_PROG_LD])
+AC_DEFUN([AM_PROG_NM],        [AC_PROG_NM])
+
+# This is just to silence aclocal about the macro not being used
+ifelse([AC_DISABLE_FAST_INSTALL])
+
+AC_DEFUN([LT_AC_PROG_GCJ],
+[AC_CHECK_TOOL(GCJ, gcj, no)
+  test "x${GCJFLAGS+set}" = xset || GCJFLAGS="-g -O2"
+  AC_SUBST(GCJFLAGS)
+])
+
+AC_DEFUN([LT_AC_PROG_RC],
+[AC_CHECK_TOOL(RC, windres, no)
+])
+
+
+# Cheap backport of AS_EXECUTABLE_P and required macros
+# from Autoconf 2.59; we should not use $as_executable_p directly.
+
+# _AS_TEST_PREPARE
+# ----------------
+m4_ifndef([_AS_TEST_PREPARE],
+[m4_defun([_AS_TEST_PREPARE],
+[if test -x / >/dev/null 2>&1; then
+  as_executable_p='test -x'
+else
+  as_executable_p='test -f'
+fi
+])])# _AS_TEST_PREPARE
+
+# AS_EXECUTABLE_P
+# ---------------
+# Check whether a file is executable.
+m4_ifndef([AS_EXECUTABLE_P],
+[m4_defun([AS_EXECUTABLE_P],
+[AS_REQUIRE([_AS_TEST_PREPARE])dnl
+$as_executable_p $1[]dnl
+])])# AS_EXECUTABLE_P
+
+# NOTE: This macro has been submitted for inclusion into   #
+#  GNU Autoconf as AC_PROG_SED.  When it is available in   #
+#  a released version of Autoconf we should remove this    #
+#  macro and use it instead.                               #
+# LT_AC_PROG_SED
+# --------------
+# Check for a fully-functional sed program, that truncates
+# as few characters as possible.  Prefer GNU sed if found.
+AC_DEFUN([LT_AC_PROG_SED],
+[AC_MSG_CHECKING([for a sed that does not truncate output])
+AC_CACHE_VAL(lt_cv_path_SED,
+[# Loop through the user's path and test for sed and gsed.
+# Then use that list of sed's as ones to test for truncation.
+as_save_IFS=$IFS; IFS=$PATH_SEPARATOR
+for as_dir in $PATH
+do
+  IFS=$as_save_IFS
+  test -z "$as_dir" && as_dir=.
+  for lt_ac_prog in sed gsed; do
+    for ac_exec_ext in '' $ac_executable_extensions; do
+      if AS_EXECUTABLE_P(["$as_dir/$lt_ac_prog$ac_exec_ext"]); then
+        lt_ac_sed_list="$lt_ac_sed_list $as_dir/$lt_ac_prog$ac_exec_ext"
+      fi
+    done
+  done
+done
+IFS=$as_save_IFS
+lt_ac_max=0
+lt_ac_count=0
+# Add /usr/xpg4/bin/sed as it is typically found on Solaris
+# along with /bin/sed that truncates output.
+for lt_ac_sed in $lt_ac_sed_list /usr/xpg4/bin/sed; do
+  test ! -f $lt_ac_sed && continue
+  cat /dev/null > conftest.in
+  lt_ac_count=0
+  echo $ECHO_N "0123456789$ECHO_C" >conftest.in
+  # Check for GNU sed and select it if it is found.
+  if "$lt_ac_sed" --version 2>&1 < /dev/null | grep 'GNU' > /dev/null; then
+    lt_cv_path_SED=$lt_ac_sed
+    break
+  fi
+  while true; do
+    cat conftest.in conftest.in >conftest.tmp
+    mv conftest.tmp conftest.in
+    cp conftest.in conftest.nl
+    echo >>conftest.nl
+    $lt_ac_sed -e 's/a$//' < conftest.nl >conftest.out || break
+    cmp -s conftest.out conftest.nl || break
+    # 10000 chars as input seems more than enough
+    test $lt_ac_count -gt 10 && break
+    lt_ac_count=`expr $lt_ac_count + 1`
+    if test $lt_ac_count -gt $lt_ac_max; then
+      lt_ac_max=$lt_ac_count
+      lt_cv_path_SED=$lt_ac_sed
+    fi
+  done
+done
+])
+SED=$lt_cv_path_SED
+AC_SUBST([SED])
+AC_MSG_RESULT([$SED])
+])
+
+# Copyright (C) 2002, 2003, 2005  Free Software Foundation, Inc.
+#
+# This file is free software; the Free Software Foundation
+# gives unlimited permission to copy and/or distribute it,
+# with or without modifications, as long as this notice is preserved.
+
+# AM_AUTOMAKE_VERSION(VERSION)
+# ----------------------------
+# Automake X.Y traces this macro to ensure aclocal.m4 has been
+# generated from the m4 files accompanying Automake X.Y.
+AC_DEFUN([AM_AUTOMAKE_VERSION], [am__api_version="1.9"])
+
+# AM_SET_CURRENT_AUTOMAKE_VERSION
+# -------------------------------
+# Call AM_AUTOMAKE_VERSION so it can be traced.
+# This function is AC_REQUIREd by AC_INIT_AUTOMAKE.
+AC_DEFUN([AM_SET_CURRENT_AUTOMAKE_VERSION],
+	 [AM_AUTOMAKE_VERSION([1.9.6])])
+
+# AM_AUX_DIR_EXPAND                                         -*- Autoconf -*-
+
+# Copyright (C) 2001, 2003, 2005  Free Software Foundation, Inc.
+#
+# This file is free software; the Free Software Foundation
+# gives unlimited permission to copy and/or distribute it,
+# with or without modifications, as long as this notice is preserved.
+
+# For projects using AC_CONFIG_AUX_DIR([foo]), Autoconf sets
+# $ac_aux_dir to `$srcdir/foo'.  In other projects, it is set to
+# `$srcdir', `$srcdir/..', or `$srcdir/../..'.
+#
+# Of course, Automake must honor this variable whenever it calls a
+# tool from the auxiliary directory.  The problem is that $srcdir (and
+# therefore $ac_aux_dir as well) can be either absolute or relative,
+# depending on how configure is run.  This is pretty annoying, since
+# it makes $ac_aux_dir quite unusable in subdirectories: in the top
+# source directory, any form will work fine, but in subdirectories a
+# relative path needs to be adjusted first.
+#
+# $ac_aux_dir/missing
+#    fails when called from a subdirectory if $ac_aux_dir is relative
+# $top_srcdir/$ac_aux_dir/missing
+#    fails if $ac_aux_dir is absolute,
+#    fails when called from a subdirectory in a VPATH build with
+#          a relative $ac_aux_dir
+#
+# The reason of the latter failure is that $top_srcdir and $ac_aux_dir
+# are both prefixed by $srcdir.  In an in-source build this is usually
+# harmless because $srcdir is `.', but things will broke when you
+# start a VPATH build or use an absolute $srcdir.
+#
+# So we could use something similar to $top_srcdir/$ac_aux_dir/missing,
+# iff we strip the leading $srcdir from $ac_aux_dir.  That would be:
+#   am_aux_dir='\$(top_srcdir)/'`expr "$ac_aux_dir" : "$srcdir//*\(.*\)"`
+# and then we would define $MISSING as
+#   MISSING="\${SHELL} $am_aux_dir/missing"
+# This will work as long as MISSING is not called from configure, because
+# unfortunately $(top_srcdir) has no meaning in configure.
+# However there are other variables, like CC, which are often used in
+# configure, and could therefore not use this "fixed" $ac_aux_dir.
+#
+# Another solution, used here, is to always expand $ac_aux_dir to an
+# absolute PATH.  The drawback is that using absolute paths prevent a
+# configured tree to be moved without reconfiguration.
+
+AC_DEFUN([AM_AUX_DIR_EXPAND],
+[dnl Rely on autoconf to set up CDPATH properly.
+AC_PREREQ([2.50])dnl
+# expand $ac_aux_dir to an absolute path
+am_aux_dir=`cd $ac_aux_dir && pwd`
+])
+
+# AM_CONDITIONAL                                            -*- Autoconf -*-
+
+# Copyright (C) 1997, 2000, 2001, 2003, 2004, 2005
+# Free Software Foundation, Inc.
+#
+# This file is free software; the Free Software Foundation
+# gives unlimited permission to copy and/or distribute it,
+# with or without modifications, as long as this notice is preserved.
+
+# serial 7
+
+# AM_CONDITIONAL(NAME, SHELL-CONDITION)
+# -------------------------------------
+# Define a conditional.
+AC_DEFUN([AM_CONDITIONAL],
+[AC_PREREQ(2.52)dnl
+ ifelse([$1], [TRUE],  [AC_FATAL([$0: invalid condition: $1])],
+	[$1], [FALSE], [AC_FATAL([$0: invalid condition: $1])])dnl
+AC_SUBST([$1_TRUE])
+AC_SUBST([$1_FALSE])
+if $2; then
+  $1_TRUE=
+  $1_FALSE='#'
+else
+  $1_TRUE='#'
+  $1_FALSE=
+fi
+AC_CONFIG_COMMANDS_PRE(
+[if test -z "${$1_TRUE}" && test -z "${$1_FALSE}"; then
+  AC_MSG_ERROR([[conditional "$1" was never defined.
+Usually this means the macro was only invoked conditionally.]])
+fi])])
+
+
+# Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005
+# Free Software Foundation, Inc.
+#
+# This file is free software; the Free Software Foundation
+# gives unlimited permission to copy and/or distribute it,
+# with or without modifications, as long as this notice is preserved.
+
+# serial 8
+
+# There are a few dirty hacks below to avoid letting `AC_PROG_CC' be
+# written in clear, in which case automake, when reading aclocal.m4,
+# will think it sees a *use*, and therefore will trigger all it's
+# C support machinery.  Also note that it means that autoscan, seeing
+# CC etc. in the Makefile, will ask for an AC_PROG_CC use...
+
+
+# _AM_DEPENDENCIES(NAME)
+# ----------------------
+# See how the compiler implements dependency checking.
+# NAME is "CC", "CXX", "GCJ", or "OBJC".
+# We try a few techniques and use that to set a single cache variable.
+#
+# We don't AC_REQUIRE the corresponding AC_PROG_CC since the latter was
+# modified to invoke _AM_DEPENDENCIES(CC); we would have a circular
+# dependency, and given that the user is not expected to run this macro,
+# just rely on AC_PROG_CC.
+AC_DEFUN([_AM_DEPENDENCIES],
+[AC_REQUIRE([AM_SET_DEPDIR])dnl
+AC_REQUIRE([AM_OUTPUT_DEPENDENCY_COMMANDS])dnl
+AC_REQUIRE([AM_MAKE_INCLUDE])dnl
+AC_REQUIRE([AM_DEP_TRACK])dnl
+
+ifelse([$1], CC,   [depcc="$CC"   am_compiler_list=],
+       [$1], CXX,  [depcc="$CXX"  am_compiler_list=],
+       [$1], OBJC, [depcc="$OBJC" am_compiler_list='gcc3 gcc'],
+       [$1], GCJ,  [depcc="$GCJ"  am_compiler_list='gcc3 gcc'],
+                   [depcc="$$1"   am_compiler_list=])
+
+AC_CACHE_CHECK([dependency style of $depcc],
+               [am_cv_$1_dependencies_compiler_type],
+[if test -z "$AMDEP_TRUE" && test -f "$am_depcomp"; then
+  # We make a subdir and do the tests there.  Otherwise we can end up
+  # making bogus files that we don't know about and never remove.  For
+  # instance it was reported that on HP-UX the gcc test will end up
+  # making a dummy file named `D' -- because `-MD' means `put the output
+  # in D'.
+  mkdir conftest.dir
+  # Copy depcomp to subdir because otherwise we won't find it if we're
+  # using a relative directory.
+  cp "$am_depcomp" conftest.dir
+  cd conftest.dir
+  # We will build objects and dependencies in a subdirectory because
+  # it helps to detect inapplicable dependency modes.  For instance
+  # both Tru64's cc and ICC support -MD to output dependencies as a
+  # side effect of compilation, but ICC will put the dependencies in
+  # the current directory while Tru64 will put them in the object
+  # directory.
+  mkdir sub
+
+  am_cv_$1_dependencies_compiler_type=none
+  if test "$am_compiler_list" = ""; then
+     am_compiler_list=`sed -n ['s/^#*\([a-zA-Z0-9]*\))$/\1/p'] < ./depcomp`
+  fi
+  for depmode in $am_compiler_list; do
+    # Setup a source with many dependencies, because some compilers
+    # like to wrap large dependency lists on column 80 (with \), and
+    # we should not choose a depcomp mode which is confused by this.
+    #
+    # We need to recreate these files for each test, as the compiler may
+    # overwrite some of them when testing with obscure command lines.
+    # This happens at least with the AIX C compiler.
+    : > sub/conftest.c
+    for i in 1 2 3 4 5 6; do
+      echo '#include "conftst'$i'.h"' >> sub/conftest.c
+      # Using `: > sub/conftst$i.h' creates only sub/conftst1.h with
+      # Solaris 8's {/usr,}/bin/sh.
+      touch sub/conftst$i.h
+    done
+    echo "${am__include} ${am__quote}sub/conftest.Po${am__quote}" > confmf
+
+    case $depmode in
+    nosideeffect)
+      # after this tag, mechanisms are not by side-effect, so they'll
+      # only be used when explicitly requested
+      if test "x$enable_dependency_tracking" = xyes; then
+	continue
+      else
+	break
+      fi
+      ;;
+    none) break ;;
+    esac
+    # We check with `-c' and `-o' for the sake of the "dashmstdout"
+    # mode.  It turns out that the SunPro C++ compiler does not properly
+    # handle `-M -o', and we need to detect this.
+    if depmode=$depmode \
+       source=sub/conftest.c object=sub/conftest.${OBJEXT-o} \
+       depfile=sub/conftest.Po tmpdepfile=sub/conftest.TPo \
+       $SHELL ./depcomp $depcc -c -o sub/conftest.${OBJEXT-o} sub/conftest.c \
+         >/dev/null 2>conftest.err &&
+       grep sub/conftst6.h sub/conftest.Po > /dev/null 2>&1 &&
+       grep sub/conftest.${OBJEXT-o} sub/conftest.Po > /dev/null 2>&1 &&
+       ${MAKE-make} -s -f confmf > /dev/null 2>&1; then
+      # icc doesn't choke on unknown options, it will just issue warnings
+      # or remarks (even with -Werror).  So we grep stderr for any message
+      # that says an option was ignored or not supported.
+      # When given -MP, icc 7.0 and 7.1 complain thusly:
+      #   icc: Command line warning: ignoring option '-M'; no argument required
+      # The diagnosis changed in icc 8.0:
+      #   icc: Command line remark: option '-MP' not supported
+      if (grep 'ignoring option' conftest.err ||
+          grep 'not supported' conftest.err) >/dev/null 2>&1; then :; else
+        am_cv_$1_dependencies_compiler_type=$depmode
+        break
+      fi
+    fi
+  done
+
+  cd ..
+  rm -rf conftest.dir
+else
+  am_cv_$1_dependencies_compiler_type=none
+fi
+])
+AC_SUBST([$1DEPMODE], [depmode=$am_cv_$1_dependencies_compiler_type])
+AM_CONDITIONAL([am__fastdep$1], [
+  test "x$enable_dependency_tracking" != xno \
+  && test "$am_cv_$1_dependencies_compiler_type" = gcc3])
+])
+
+
+# AM_SET_DEPDIR
+# -------------
+# Choose a directory name for dependency files.
+# This macro is AC_REQUIREd in _AM_DEPENDENCIES
+AC_DEFUN([AM_SET_DEPDIR],
+[AC_REQUIRE([AM_SET_LEADING_DOT])dnl
+AC_SUBST([DEPDIR], ["${am__leading_dot}deps"])dnl
+])
+
+
+# AM_DEP_TRACK
+# ------------
+AC_DEFUN([AM_DEP_TRACK],
+[AC_ARG_ENABLE(dependency-tracking,
+[  --disable-dependency-tracking  speeds up one-time build
+  --enable-dependency-tracking   do not reject slow dependency extractors])
+if test "x$enable_dependency_tracking" != xno; then
+  am_depcomp="$ac_aux_dir/depcomp"
+  AMDEPBACKSLASH='\'
+fi
+AM_CONDITIONAL([AMDEP], [test "x$enable_dependency_tracking" != xno])
+AC_SUBST([AMDEPBACKSLASH])
+])
+
+# Generate code to set up dependency tracking.              -*- Autoconf -*-
+
+# Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005
+# Free Software Foundation, Inc.
+#
+# This file is free software; the Free Software Foundation
+# gives unlimited permission to copy and/or distribute it,
+# with or without modifications, as long as this notice is preserved.
+
+#serial 3
+
+# _AM_OUTPUT_DEPENDENCY_COMMANDS
+# ------------------------------
+AC_DEFUN([_AM_OUTPUT_DEPENDENCY_COMMANDS],
+[for mf in $CONFIG_FILES; do
+  # Strip MF so we end up with the name of the file.
+  mf=`echo "$mf" | sed -e 's/:.*$//'`
+  # Check whether this is an Automake generated Makefile or not.
+  # We used to match only the files named `Makefile.in', but
+  # some people rename them; so instead we look at the file content.
+  # Grep'ing the first line is not enough: some people post-process
+  # each Makefile.in and add a new line on top of each file to say so.
+  # So let's grep whole file.
+  if grep '^#.*generated by automake' $mf > /dev/null 2>&1; then
+    dirpart=`AS_DIRNAME("$mf")`
+  else
+    continue
+  fi
+  # Extract the definition of DEPDIR, am__include, and am__quote
+  # from the Makefile without running `make'.
+  DEPDIR=`sed -n 's/^DEPDIR = //p' < "$mf"`
+  test -z "$DEPDIR" && continue
+  am__include=`sed -n 's/^am__include = //p' < "$mf"`
+  test -z "am__include" && continue
+  am__quote=`sed -n 's/^am__quote = //p' < "$mf"`
+  # When using ansi2knr, U may be empty or an underscore; expand it
+  U=`sed -n 's/^U = //p' < "$mf"`
+  # Find all dependency output files, they are included files with
+  # $(DEPDIR) in their names.  We invoke sed twice because it is the
+  # simplest approach to changing $(DEPDIR) to its actual value in the
+  # expansion.
+  for file in `sed -n "
+    s/^$am__include $am__quote\(.*(DEPDIR).*\)$am__quote"'$/\1/p' <"$mf" | \
+       sed -e 's/\$(DEPDIR)/'"$DEPDIR"'/g' -e 's/\$U/'"$U"'/g'`; do
+    # Make sure the directory exists.
+    test -f "$dirpart/$file" && continue
+    fdir=`AS_DIRNAME(["$file"])`
+    AS_MKDIR_P([$dirpart/$fdir])
+    # echo "creating $dirpart/$file"
+    echo '# dummy' > "$dirpart/$file"
+  done
+done
+])# _AM_OUTPUT_DEPENDENCY_COMMANDS
+
+
+# AM_OUTPUT_DEPENDENCY_COMMANDS
+# -----------------------------
+# This macro should only be invoked once -- use via AC_REQUIRE.
+#
+# This code is only required when automatic dependency tracking
+# is enabled.  FIXME.  This creates each `.P' file that we will
+# need in order to bootstrap the dependency handling code.
+AC_DEFUN([AM_OUTPUT_DEPENDENCY_COMMANDS],
+[AC_CONFIG_COMMANDS([depfiles],
+     [test x"$AMDEP_TRUE" != x"" || _AM_OUTPUT_DEPENDENCY_COMMANDS],
+     [AMDEP_TRUE="$AMDEP_TRUE" ac_aux_dir="$ac_aux_dir"])
+])
+
+# Do all the work for Automake.                             -*- Autoconf -*-
+
+# Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
+# Free Software Foundation, Inc.
+#
+# This file is free software; the Free Software Foundation
+# gives unlimited permission to copy and/or distribute it,
+# with or without modifications, as long as this notice is preserved.
+
+# serial 12
+
+# This macro actually does too much.  Some checks are only needed if
+# your package does certain things.  But this isn't really a big deal.
+
+# AM_INIT_AUTOMAKE(PACKAGE, VERSION, [NO-DEFINE])
+# AM_INIT_AUTOMAKE([OPTIONS])
+# -----------------------------------------------
+# The call with PACKAGE and VERSION arguments is the old style
+# call (pre autoconf-2.50), which is being phased out.  PACKAGE
+# and VERSION should now be passed to AC_INIT and removed from
+# the call to AM_INIT_AUTOMAKE.
+# We support both call styles for the transition.  After
+# the next Automake release, Autoconf can make the AC_INIT
+# arguments mandatory, and then we can depend on a new Autoconf
+# release and drop the old call support.
+AC_DEFUN([AM_INIT_AUTOMAKE],
+[AC_PREREQ([2.58])dnl
+dnl Autoconf wants to disallow AM_ names.  We explicitly allow
+dnl the ones we care about.
+m4_pattern_allow([^AM_[A-Z]+FLAGS$])dnl
+AC_REQUIRE([AM_SET_CURRENT_AUTOMAKE_VERSION])dnl
+AC_REQUIRE([AC_PROG_INSTALL])dnl
+# test to see if srcdir already configured
+if test "`cd $srcdir && pwd`" != "`pwd`" &&
+   test -f $srcdir/config.status; then
+  AC_MSG_ERROR([source directory already configured; run "make distclean" there first])
+fi
+
+# test whether we have cygpath
+if test -z "$CYGPATH_W"; then
+  if (cygpath --version) >/dev/null 2>/dev/null; then
+    CYGPATH_W='cygpath -w'
+  else
+    CYGPATH_W=echo
+  fi
+fi
+AC_SUBST([CYGPATH_W])
+
+# Define the identity of the package.
+dnl Distinguish between old-style and new-style calls.
+m4_ifval([$2],
+[m4_ifval([$3], [_AM_SET_OPTION([no-define])])dnl
+ AC_SUBST([PACKAGE], [$1])dnl
+ AC_SUBST([VERSION], [$2])],
+[_AM_SET_OPTIONS([$1])dnl
+ AC_SUBST([PACKAGE], ['AC_PACKAGE_TARNAME'])dnl
+ AC_SUBST([VERSION], ['AC_PACKAGE_VERSION'])])dnl
+
+_AM_IF_OPTION([no-define],,
+[AC_DEFINE_UNQUOTED(PACKAGE, "$PACKAGE", [Name of package])
+ AC_DEFINE_UNQUOTED(VERSION, "$VERSION", [Version number of package])])dnl
+
+# Some tools Automake needs.
+AC_REQUIRE([AM_SANITY_CHECK])dnl
+AC_REQUIRE([AC_ARG_PROGRAM])dnl
+AM_MISSING_PROG(ACLOCAL, aclocal-${am__api_version})
+AM_MISSING_PROG(AUTOCONF, autoconf)
+AM_MISSING_PROG(AUTOMAKE, automake-${am__api_version})
+AM_MISSING_PROG(AUTOHEADER, autoheader)
+AM_MISSING_PROG(MAKEINFO, makeinfo)
+AM_PROG_INSTALL_SH
+AM_PROG_INSTALL_STRIP
+AC_REQUIRE([AM_PROG_MKDIR_P])dnl
+# We need awk for the "check" target.  The system "awk" is bad on
+# some platforms.
+AC_REQUIRE([AC_PROG_AWK])dnl
+AC_REQUIRE([AC_PROG_MAKE_SET])dnl
+AC_REQUIRE([AM_SET_LEADING_DOT])dnl
+_AM_IF_OPTION([tar-ustar], [_AM_PROG_TAR([ustar])],
+              [_AM_IF_OPTION([tar-pax], [_AM_PROG_TAR([pax])],
+	      		     [_AM_PROG_TAR([v7])])])
+_AM_IF_OPTION([no-dependencies],,
+[AC_PROVIDE_IFELSE([AC_PROG_CC],
+                  [_AM_DEPENDENCIES(CC)],
+                  [define([AC_PROG_CC],
+                          defn([AC_PROG_CC])[_AM_DEPENDENCIES(CC)])])dnl
+AC_PROVIDE_IFELSE([AC_PROG_CXX],
+                  [_AM_DEPENDENCIES(CXX)],
+                  [define([AC_PROG_CXX],
+                          defn([AC_PROG_CXX])[_AM_DEPENDENCIES(CXX)])])dnl
+])
+])
+
+
+# When config.status generates a header, we must update the stamp-h file.
+# This file resides in the same directory as the config header
+# that is generated.  The stamp files are numbered to have different names.
+
+# Autoconf calls _AC_AM_CONFIG_HEADER_HOOK (when defined) in the
+# loop where config.status creates the headers, so we can generate
+# our stamp files there.
+AC_DEFUN([_AC_AM_CONFIG_HEADER_HOOK],
+[# Compute $1's index in $config_headers.
+_am_stamp_count=1
+for _am_header in $config_headers :; do
+  case $_am_header in
+    $1 | $1:* )
+      break ;;
+    * )
+      _am_stamp_count=`expr $_am_stamp_count + 1` ;;
+  esac
+done
+echo "timestamp for $1" >`AS_DIRNAME([$1])`/stamp-h[]$_am_stamp_count])
+
+# Copyright (C) 2001, 2003, 2005  Free Software Foundation, Inc.
+#
+# This file is free software; the Free Software Foundation
+# gives unlimited permission to copy and/or distribute it,
+# with or without modifications, as long as this notice is preserved.
+
+# AM_PROG_INSTALL_SH
+# ------------------
+# Define $install_sh.
+AC_DEFUN([AM_PROG_INSTALL_SH],
+[AC_REQUIRE([AM_AUX_DIR_EXPAND])dnl
+install_sh=${install_sh-"$am_aux_dir/install-sh"}
+AC_SUBST(install_sh)])
+
+# Copyright (C) 2003, 2005  Free Software Foundation, Inc.
+#
+# This file is free software; the Free Software Foundation
+# gives unlimited permission to copy and/or distribute it,
+# with or without modifications, as long as this notice is preserved.
+
+# serial 2
+
+# Check whether the underlying file-system supports filenames
+# with a leading dot.  For instance MS-DOS doesn't.
+AC_DEFUN([AM_SET_LEADING_DOT],
+[rm -rf .tst 2>/dev/null
+mkdir .tst 2>/dev/null
+if test -d .tst; then
+  am__leading_dot=.
+else
+  am__leading_dot=_
+fi
+rmdir .tst 2>/dev/null
+AC_SUBST([am__leading_dot])])
+
+# Check to see how 'make' treats includes.	            -*- Autoconf -*-
+
+# Copyright (C) 2001, 2002, 2003, 2005  Free Software Foundation, Inc.
+#
+# This file is free software; the Free Software Foundation
+# gives unlimited permission to copy and/or distribute it,
+# with or without modifications, as long as this notice is preserved.
+
+# serial 3
+
+# AM_MAKE_INCLUDE()
+# -----------------
+# Check to see how make treats includes.
+AC_DEFUN([AM_MAKE_INCLUDE],
+[am_make=${MAKE-make}
+cat > confinc << 'END'
+am__doit:
+	@echo done
+.PHONY: am__doit
+END
+# If we don't find an include directive, just comment out the code.
+AC_MSG_CHECKING([for style of include used by $am_make])
+am__include="#"
+am__quote=
+_am_result=none
+# First try GNU make style include.
+echo "include confinc" > confmf
+# We grep out `Entering directory' and `Leaving directory'
+# messages which can occur if `w' ends up in MAKEFLAGS.
+# In particular we don't look at `^make:' because GNU make might
+# be invoked under some other name (usually "gmake"), in which
+# case it prints its new name instead of `make'.
+if test "`$am_make -s -f confmf 2> /dev/null | grep -v 'ing directory'`" = "done"; then
+   am__include=include
+   am__quote=
+   _am_result=GNU
+fi
+# Now try BSD make style include.
+if test "$am__include" = "#"; then
+   echo '.include "confinc"' > confmf
+   if test "`$am_make -s -f confmf 2> /dev/null`" = "done"; then
+      am__include=.include
+      am__quote="\""
+      _am_result=BSD
+   fi
+fi
+AC_SUBST([am__include])
+AC_SUBST([am__quote])
+AC_MSG_RESULT([$_am_result])
+rm -f confinc confmf
+])
+
+# Fake the existence of programs that GNU maintainers use.  -*- Autoconf -*-
+
+# Copyright (C) 1997, 1999, 2000, 2001, 2003, 2005
+# Free Software Foundation, Inc.
+#
+# This file is free software; the Free Software Foundation
+# gives unlimited permission to copy and/or distribute it,
+# with or without modifications, as long as this notice is preserved.
+
+# serial 4
+
+# AM_MISSING_PROG(NAME, PROGRAM)
+# ------------------------------
+AC_DEFUN([AM_MISSING_PROG],
+[AC_REQUIRE([AM_MISSING_HAS_RUN])
+$1=${$1-"${am_missing_run}$2"}
+AC_SUBST($1)])
+
+
+# AM_MISSING_HAS_RUN
+# ------------------
+# Define MISSING if not defined so far and test if it supports --run.
+# If it does, set am_missing_run to use it, otherwise, to nothing.
+AC_DEFUN([AM_MISSING_HAS_RUN],
+[AC_REQUIRE([AM_AUX_DIR_EXPAND])dnl
+test x"${MISSING+set}" = xset || MISSING="\${SHELL} $am_aux_dir/missing"
+# Use eval to expand $SHELL
+if eval "$MISSING --run true"; then
+  am_missing_run="$MISSING --run "
+else
+  am_missing_run=
+  AC_MSG_WARN([`missing' script is too old or missing])
+fi
+])
+
+# Copyright (C) 2003, 2004, 2005  Free Software Foundation, Inc.
+#
+# This file is free software; the Free Software Foundation
+# gives unlimited permission to copy and/or distribute it,
+# with or without modifications, as long as this notice is preserved.
+
+# AM_PROG_MKDIR_P
+# ---------------
+# Check whether `mkdir -p' is supported, fallback to mkinstalldirs otherwise.
+#
+# Automake 1.8 used `mkdir -m 0755 -p --' to ensure that directories
+# created by `make install' are always world readable, even if the
+# installer happens to have an overly restrictive umask (e.g. 077).
+# This was a mistake.  There are at least two reasons why we must not
+# use `-m 0755':
+#   - it causes special bits like SGID to be ignored,
+#   - it may be too restrictive (some setups expect 775 directories).
+#
+# Do not use -m 0755 and let people choose whatever they expect by
+# setting umask.
+#
+# We cannot accept any implementation of `mkdir' that recognizes `-p'.
+# Some implementations (such as Solaris 8's) are not thread-safe: if a
+# parallel make tries to run `mkdir -p a/b' and `mkdir -p a/c'
+# concurrently, both version can detect that a/ is missing, but only
+# one can create it and the other will error out.  Consequently we
+# restrict ourselves to GNU make (using the --version option ensures
+# this.)
+AC_DEFUN([AM_PROG_MKDIR_P],
+[if mkdir -p --version . >/dev/null 2>&1 && test ! -d ./--version; then
+  # We used to keeping the `.' as first argument, in order to
+  # allow $(mkdir_p) to be used without argument.  As in
+  #   $(mkdir_p) $(somedir)
+  # where $(somedir) is conditionally defined.  However this is wrong
+  # for two reasons:
+  #  1. if the package is installed by a user who cannot write `.'
+  #     make install will fail,
+  #  2. the above comment should most certainly read
+  #     $(mkdir_p) $(DESTDIR)$(somedir)
+  #     so it does not work when $(somedir) is undefined and
+  #     $(DESTDIR) is not.
+  #  To support the latter case, we have to write
+  #     test -z "$(somedir)" || $(mkdir_p) $(DESTDIR)$(somedir),
+  #  so the `.' trick is pointless.
+  mkdir_p='mkdir -p --'
+else
+  # On NextStep and OpenStep, the `mkdir' command does not
+  # recognize any option.  It will interpret all options as
+  # directories to create, and then abort because `.' already
+  # exists.
+  for d in ./-p ./--version;
+  do
+    test -d $d && rmdir $d
+  done
+  # $(mkinstalldirs) is defined by Automake if mkinstalldirs exists.
+  if test -f "$ac_aux_dir/mkinstalldirs"; then
+    mkdir_p='$(mkinstalldirs)'
+  else
+    mkdir_p='$(install_sh) -d'
+  fi
+fi
+AC_SUBST([mkdir_p])])
+
+# Helper functions for option handling.                     -*- Autoconf -*-
+
+# Copyright (C) 2001, 2002, 2003, 2005  Free Software Foundation, Inc.
+#
+# This file is free software; the Free Software Foundation
+# gives unlimited permission to copy and/or distribute it,
+# with or without modifications, as long as this notice is preserved.
+
+# serial 3
+
+# _AM_MANGLE_OPTION(NAME)
+# -----------------------
+AC_DEFUN([_AM_MANGLE_OPTION],
+[[_AM_OPTION_]m4_bpatsubst($1, [[^a-zA-Z0-9_]], [_])])
+
+# _AM_SET_OPTION(NAME)
+# ------------------------------
+# Set option NAME.  Presently that only means defining a flag for this option.
+AC_DEFUN([_AM_SET_OPTION],
+[m4_define(_AM_MANGLE_OPTION([$1]), 1)])
+
+# _AM_SET_OPTIONS(OPTIONS)
+# ----------------------------------
+# OPTIONS is a space-separated list of Automake options.
+AC_DEFUN([_AM_SET_OPTIONS],
+[AC_FOREACH([_AM_Option], [$1], [_AM_SET_OPTION(_AM_Option)])])
+
+# _AM_IF_OPTION(OPTION, IF-SET, [IF-NOT-SET])
+# -------------------------------------------
+# Execute IF-SET if OPTION is set, IF-NOT-SET otherwise.
+AC_DEFUN([_AM_IF_OPTION],
+[m4_ifset(_AM_MANGLE_OPTION([$1]), [$2], [$3])])
+
+# Check to make sure that the build environment is sane.    -*- Autoconf -*-
+
+# Copyright (C) 1996, 1997, 2000, 2001, 2003, 2005
+# Free Software Foundation, Inc.
+#
+# This file is free software; the Free Software Foundation
+# gives unlimited permission to copy and/or distribute it,
+# with or without modifications, as long as this notice is preserved.
+
+# serial 4
+
+# AM_SANITY_CHECK
+# ---------------
+AC_DEFUN([AM_SANITY_CHECK],
+[AC_MSG_CHECKING([whether build environment is sane])
+# Just in case
+sleep 1
+echo timestamp > conftest.file
+# Do `set' in a subshell so we don't clobber the current shell's
+# arguments.  Must try -L first in case configure is actually a
+# symlink; some systems play weird games with the mod time of symlinks
+# (eg FreeBSD returns the mod time of the symlink's containing
+# directory).
+if (
+   set X `ls -Lt $srcdir/configure conftest.file 2> /dev/null`
+   if test "$[*]" = "X"; then
+      # -L didn't work.
+      set X `ls -t $srcdir/configure conftest.file`
+   fi
+   rm -f conftest.file
+   if test "$[*]" != "X $srcdir/configure conftest.file" \
+      && test "$[*]" != "X conftest.file $srcdir/configure"; then
+
+      # If neither matched, then we have a broken ls.  This can happen
+      # if, for instance, CONFIG_SHELL is bash and it inherits a
+      # broken ls alias from the environment.  This has actually
+      # happened.  Such a system could not be considered "sane".
+      AC_MSG_ERROR([ls -t appears to fail.  Make sure there is not a broken
+alias in your environment])
+   fi
+
+   test "$[2]" = conftest.file
+   )
+then
+   # Ok.
+   :
+else
+   AC_MSG_ERROR([newly created file is older than distributed files!
+Check your system clock])
+fi
+AC_MSG_RESULT(yes)])
+
+# Copyright (C) 2001, 2003, 2005  Free Software Foundation, Inc.
+#
+# This file is free software; the Free Software Foundation
+# gives unlimited permission to copy and/or distribute it,
+# with or without modifications, as long as this notice is preserved.
+
+# AM_PROG_INSTALL_STRIP
+# ---------------------
+# One issue with vendor `install' (even GNU) is that you can't
+# specify the program used to strip binaries.  This is especially
+# annoying in cross-compiling environments, where the build's strip
+# is unlikely to handle the host's binaries.
+# Fortunately install-sh will honor a STRIPPROG variable, so we
+# always use install-sh in `make install-strip', and initialize
+# STRIPPROG with the value of the STRIP variable (set by the user).
+AC_DEFUN([AM_PROG_INSTALL_STRIP],
+[AC_REQUIRE([AM_PROG_INSTALL_SH])dnl
+# Installed binaries are usually stripped using `strip' when the user
+# run `make install-strip'.  However `strip' might not be the right
+# tool to use in cross-compilation environments, therefore Automake
+# will honor the `STRIP' environment variable to overrule this program.
+dnl Don't test for $cross_compiling = yes, because it might be `maybe'.
+if test "$cross_compiling" != no; then
+  AC_CHECK_TOOL([STRIP], [strip], :)
+fi
+INSTALL_STRIP_PROGRAM="\${SHELL} \$(install_sh) -c -s"
+AC_SUBST([INSTALL_STRIP_PROGRAM])])
+
+# Check how to create a tarball.                            -*- Autoconf -*-
+
+# Copyright (C) 2004, 2005  Free Software Foundation, Inc.
+#
+# This file is free software; the Free Software Foundation
+# gives unlimited permission to copy and/or distribute it,
+# with or without modifications, as long as this notice is preserved.
+
+# serial 2
+
+# _AM_PROG_TAR(FORMAT)
+# --------------------
+# Check how to create a tarball in format FORMAT.
+# FORMAT should be one of `v7', `ustar', or `pax'.
+#
+# Substitute a variable $(am__tar) that is a command
+# writing to stdout a FORMAT-tarball containing the directory
+# $tardir.
+#     tardir=directory && $(am__tar) > result.tar
+#
+# Substitute a variable $(am__untar) that extract such
+# a tarball read from stdin.
+#     $(am__untar) < result.tar
+AC_DEFUN([_AM_PROG_TAR],
+[# Always define AMTAR for backward compatibility.
+AM_MISSING_PROG([AMTAR], [tar])
+m4_if([$1], [v7],
+     [am__tar='${AMTAR} chof - "$$tardir"'; am__untar='${AMTAR} xf -'],
+     [m4_case([$1], [ustar],, [pax],,
+              [m4_fatal([Unknown tar format])])
+AC_MSG_CHECKING([how to create a $1 tar archive])
+# Loop over all known methods to create a tar archive until one works.
+_am_tools='gnutar m4_if([$1], [ustar], [plaintar]) pax cpio none'
+_am_tools=${am_cv_prog_tar_$1-$_am_tools}
+# Do not fold the above two line into one, because Tru64 sh and
+# Solaris sh will not grok spaces in the rhs of `-'.
+for _am_tool in $_am_tools
+do
+  case $_am_tool in
+  gnutar)
+    for _am_tar in tar gnutar gtar;
+    do
+      AM_RUN_LOG([$_am_tar --version]) && break
+    done
+    am__tar="$_am_tar --format=m4_if([$1], [pax], [posix], [$1]) -chf - "'"$$tardir"'
+    am__tar_="$_am_tar --format=m4_if([$1], [pax], [posix], [$1]) -chf - "'"$tardir"'
+    am__untar="$_am_tar -xf -"
+    ;;
+  plaintar)
+    # Must skip GNU tar: if it does not support --format= it doesn't create
+    # ustar tarball either.
+    (tar --version) >/dev/null 2>&1 && continue
+    am__tar='tar chf - "$$tardir"'
+    am__tar_='tar chf - "$tardir"'
+    am__untar='tar xf -'
+    ;;
+  pax)
+    am__tar='pax -L -x $1 -w "$$tardir"'
+    am__tar_='pax -L -x $1 -w "$tardir"'
+    am__untar='pax -r'
+    ;;
+  cpio)
+    am__tar='find "$$tardir" -print | cpio -o -H $1 -L'
+    am__tar_='find "$tardir" -print | cpio -o -H $1 -L'
+    am__untar='cpio -i -H $1 -d'
+    ;;
+  none)
+    am__tar=false
+    am__tar_=false
+    am__untar=false
+    ;;
+  esac
+
+  # If the value was cached, stop now.  We just wanted to have am__tar
+  # and am__untar set.
+  test -n "${am_cv_prog_tar_$1}" && break
+
+  # tar/untar a dummy directory, and stop if the command works
+  rm -rf conftest.dir
+  mkdir conftest.dir
+  echo GrepMe > conftest.dir/file
+  AM_RUN_LOG([tardir=conftest.dir && eval $am__tar_ >conftest.tar])
+  rm -rf conftest.dir
+  if test -s conftest.tar; then
+    AM_RUN_LOG([$am__untar <conftest.tar])
+    grep GrepMe conftest.dir/file >/dev/null 2>&1 && break
+  fi
+done
+rm -rf conftest.dir
+
+AC_CACHE_VAL([am_cv_prog_tar_$1], [am_cv_prog_tar_$1=$_am_tool])
+AC_MSG_RESULT([$am_cv_prog_tar_$1])])
+AC_SUBST([am__tar])
+AC_SUBST([am__untar])
+]) # _AM_PROG_TAR
+

Added: freeswitch/trunk/libs/spandsp/autogen.sh
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/autogen.sh	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,145 @@
+#!/usr/bin/env sh
+#
+# SpanDSP - a series of DSP components for telephony
+#
+# autogen script
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU Lesser General Public License version 2.1,
+# as published by the Free Software Foundation.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this program; if not, write to the Free Software
+# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+#
+# $Id: autogen.sh,v 1.6 2008/03/30 18:33:24 steveu Exp $
+#
+
+UNAME=`uname`
+
+if [ "x$UNAME" = "xFreeBSD" ]; then
+    echo ""
+    echo ""
+    echo "******************************************"
+    echo "***              NOTICE                ***"
+    echo "******************************************"
+    echo "                                          "
+    echo "FreeBSD is buggy. Please use this         "
+    echo "workaround if you  want to bootstrap      "
+    echo "on FreeBSD.                               "
+    echo "                                          "
+    echo "cd /usr/local/share/aclocal19             "
+    echo "ln -s ../aclocal/libtool15.m4 .           "
+    echo "ln -s ../aclocal/ltdl15.m4 .              "
+    echo "				      "
+    echo "******************************************"
+    echo ""
+fi
+
+debug ()
+{
+	# Outputs debug statments if DEBUG var is set
+	if [ ! -z "$DEBUG" ]; then
+		echo "DEBUG: $1"
+	fi
+}
+
+version_compare()
+{
+	# Checks a command is found and the version is high enough
+	PROGRAM=$1
+	MAJOR=$2
+	MINOR=$3
+	MICRO=$4
+	test -z "$MAJOR" && MAJOR=0
+	test -z "$MINOR" && MINOR=0
+	test -z "$MICRO" && MICRO=0
+
+	debug "Checking $PROGRAM >= $MAJOR.$MINOR.$MICRO"
+
+	WHICH_PATH=`whereis which | cut -f2 -d' '`
+	COMMAND=`$WHICH_PATH $PROGRAM`
+	if [ -z $COMMAND ]; then
+		echo "$PROGRAM-$MAJOR.$MINOR.$MICRO is required and was not found."
+		return 1
+	else
+		debug "Found $COMMAND"
+	fi
+
+	INS_VER=`$COMMAND --version | head -1 | sed 's/[^0-9]*//' | cut -d' ' -f1`
+	INS_MAJOR=`echo $INS_VER | cut -d. -f1 | sed s/[a-zA-Z\-].*//g`
+	INS_MINOR=`echo $INS_VER | cut -d. -f2 | sed s/[a-zA-Z\-].*//g`
+	INS_MICRO=`echo $INS_VER | cut -d. -f3 | sed s/[a-zA-Z\-].*//g`
+	test -z "$INS_MAJOR" && INS_MAJOR=0
+	test -z "$INS_MINOR" && INS_MINOR=0
+	test -z "$INS_MICRO" && INS_MICRO=0
+	debug "Installed version: $INS_VER"
+
+	if [ "$INS_MAJOR" -gt "$MAJOR" ]; then
+		debug "MAJOR: $INS_MAJOR > $MAJOR"
+		return 0
+	elif [ "$INS_MAJOR" -eq "$MAJOR" ]; then
+		debug "MAJOR: $INS_MAJOR = $MAJOR"
+		if [ "$INS_MINOR" -gt "$MINOR" ]; then
+			debug "MINOR: $INS_MINOR > $MINOR"
+			return 0
+		elif [ "$INS_MINOR" -eq "$MINOR" ]; then
+			if [ "$INS_MICRO" -ge "$MICRO" ]; then
+				debug "MICRO: $INS_MICRO >= $MICRO"
+				return 0
+			else
+				debug "MICRO: $INS_MICRO < $MICRO"
+			fi
+		else
+			debug "MINOR: $INS_MINOR < $MINOR"
+		fi
+	else
+		debug "MAJOR: $INS_MAJOR < $MAJOR"
+	fi
+
+	echo "You have the wrong version of $PROGRAM. The minimum required version is $MAJOR.$MINOR.$MICRO"
+	echo "    and the version installed is $INS_MAJOR.$INS_MINOR.$INS_MICRO ($COMMAND)."
+	return 1
+}
+
+# Check for required version and die if unhappy
+
+if [ "x$UNAME" = "xFreeBSD" ]; then
+version_compare libtoolize 1 5 16 || exit 1
+version_compare automake19 1 9 5 || exit 1
+version_compare autoconf259 2 59 || exit 1
+ACLOCAL=aclocal19
+AUTOHEADER=autoheader259
+AUTOMAKE=automake19
+AUTOCONF=autoconf259
+else
+version_compare libtoolize 1 5 16 || exit 1
+version_compare automake 1 9 5 || exit 1
+version_compare autoconf 2 59 || exit 1
+ACLOCAL=aclocal
+AUTOHEADER=autoheader
+AUTOMAKE=automake
+AUTOCONF=autoconf
+fi
+
+libtoolize --copy --force --ltdl
+#NetBSD seems to need this file writable
+chmod u+w libltdl/configure
+
+$ACLOCAL
+$AUTOHEADER --force
+$AUTOMAKE --copy --add-missing
+$AUTOCONF --force
+
+#chmod ug+x debian/rules
+
+if [ "x$UNAME" = "xNetBSD" ]; then
+echo ""
+echo "Please remember to run gmake instead of make on NetBSD"
+echo ""
+fi

Added: freeswitch/trunk/libs/spandsp/config-h.in
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/config-h.in	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,289 @@
+/* config-h.in.  Generated from configure.ac by autoheader.  */
+
+/* Define to 1 if you have the acosf() function. */
+#undef HAVE_ACOSF
+
+/* Define to 1 if you have the asinf() function. */
+#undef HAVE_ASINF
+
+/* Define to 1 if you have the atan2f() function. */
+#undef HAVE_ATAN2F
+
+/* Define to 1 if you have the atanf() function. */
+#undef HAVE_ATANF
+
+/* Define to 1 if you have the <audiofile.h> header file. */
+#undef HAVE_AUDIOFILE_H
+
+/* Define to 1 if you have the ceilf() function. */
+#undef HAVE_CEILF
+
+/* Define to 1 if you have the cosf() function. */
+#undef HAVE_COSF
+
+/* Define to 1 if you have the <dlfcn.h> header file. */
+#undef HAVE_DLFCN_H
+
+/* Define to 1 if you don't have `vprintf' but do have `_doprnt.' */
+#undef HAVE_DOPRNT
+
+/* Define to 1 if you have the expf() function. */
+#undef HAVE_EXPF
+
+/* Define to 1 if you have the <fcntl.h> header file. */
+#undef HAVE_FCNTL_H
+
+/* Define to 1 if you have the <fftw3.h> header file. */
+#undef HAVE_FFTW3_H
+
+/* Define to 1 if you have the <fftw.h> header file. */
+#undef HAVE_FFTW_H
+
+/* Define to 1 if you have the <float.h> header file. */
+#undef HAVE_FLOAT_H
+
+/* Define to 1 if you have the floorf() function. */
+#undef HAVE_FLOORF
+
+/* Define to 1 if you have the <FL/Fl_Audio_Meter.H> header file. */
+#undef HAVE_FL_FL_AUDIO_METER_H
+
+/* Define to 1 if you have the <FL/Fl_Cartesian.H> header file. */
+#undef HAVE_FL_FL_CARTESIAN_H
+
+/* Define to 1 if you have the <FL/fl_draw.H> header file. */
+#undef HAVE_FL_FL_DRAW_H
+
+/* Define to 1 if you have the <FL/Fl.H> header file. */
+#undef HAVE_FL_FL_H
+
+/* Define to 1 if you have the <FL/Fl_Light_Button.H> header file. */
+#undef HAVE_FL_FL_LIGHT_BUTTON_H
+
+/* Define to 1 if you have the <FL/Fl_Overlay_Window.H> header file. */
+#undef HAVE_FL_FL_OVERLAY_WINDOW_H
+
+/* Define to 1 if you have the `gettimeofday' function. */
+#undef HAVE_GETTIMEOFDAY
+
+/* Define to 1 if you have the <inttypes.h> header file. */
+#undef HAVE_INTTYPES_H
+
+/* Define to 1 if you have the `m' library (-lm). */
+#undef HAVE_LIBM
+
+/* Define to 1 if you have the `tiff' library (-ltiff). */
+#undef HAVE_LIBTIFF
+
+/* Define to 1 if you have the 'libxml2' library (-lxml2). */
+#undef HAVE_LIBXML2
+
+/* Define to 1 if you have the <libxml/parser.h> header file. */
+#undef HAVE_LIBXML_PARSER_H
+
+/* Define to 1 if you have the <libxml/xinclude.h> header file. */
+#undef HAVE_LIBXML_XINCLUDE_H
+
+/* Define to 1 if you have the <libxml/xmlmemory.h> header file. */
+#undef HAVE_LIBXML_XMLMEMORY_H
+
+/* Define if you have C99's llrint function. */
+#undef HAVE_LLRINT
+
+/* Define if you have C99's llrintf function. */
+#undef HAVE_LLRINTF
+
+/* Define to 1 if you have the log10f() function. */
+#undef HAVE_LOG10F
+
+/* Define to 1 if you have the logf() function. */
+#undef HAVE_LOGF
+
+/* Define to 1 if the system has the type `long double'. */
+#undef HAVE_LONG_DOUBLE
+
+/* Define to 1 if the system has the type `long long'. */
+#undef HAVE_LONG_LONG
+
+/* Define if you have C99's lrint function. */
+#undef HAVE_LRINT
+
+/* Define if you have C99's lrintf function. */
+#undef HAVE_LRINTF
+
+/* Define to 1 if your system has a GNU libc compatible `malloc' function, and
+   to 0 otherwise. */
+#undef HAVE_MALLOC
+
+/* Define to 1 if you have the <malloc.h> header file. */
+#undef HAVE_MALLOC_H
+
+/* Define to 1 if you have the <math.h> header file. */
+#undef HAVE_MATH_H
+
+/* Define to 1 if you have the `memmove' function. */
+#undef HAVE_MEMMOVE
+
+/* Define to 1 if you have the <memory.h> header file. */
+#undef HAVE_MEMORY_H
+
+/* Define to 1 if you have the `memset' function. */
+#undef HAVE_MEMSET
+
+/* Define to 1 if you have the powf() function. */
+#undef HAVE_POWF
+
+/* Define to 1 if you have the <pthread.h> header file. */
+#undef HAVE_PTHREAD_H
+
+/* Define to 1 if your system has a GNU libc compatible `realloc' function,
+   and to 0 otherwise. */
+#undef HAVE_REALLOC
+
+/* Define to 1 if you have the `select' function. */
+#undef HAVE_SELECT
+
+/* Define to 1 if you have the sinf() function. */
+#undef HAVE_SINF
+
+/* Define to 1 if you have the <socket.h> header file. */
+#undef HAVE_SOCKET_H
+
+/* Define to 1 if you have the <stdint.h> header file. */
+#undef HAVE_STDINT_H
+
+/* Define to 1 if you have the <stdlib.h> header file. */
+#undef HAVE_STDLIB_H
+
+/* Define to 1 if you have the `strcasecmp' function. */
+#undef HAVE_STRCASECMP
+
+/* Define to 1 if you have the `strchr' function. */
+#undef HAVE_STRCHR
+
+/* Define to 1 if you have the `strdup' function. */
+#undef HAVE_STRDUP
+
+/* Define to 1 if you have the `strerror' function. */
+#undef HAVE_STRERROR
+
+/* Define to 1 if you have the <strings.h> header file. */
+#undef HAVE_STRINGS_H
+
+/* Define to 1 if you have the <string.h> header file. */
+#undef HAVE_STRING_H
+
+/* Define to 1 if you have the `strstr' function. */
+#undef HAVE_STRSTR
+
+/* Define to 1 if you have the `strtol' function. */
+#undef HAVE_STRTOL
+
+/* Define to 1 if you have the <sys/fcntl.h> header file. */
+#undef HAVE_SYS_FCNTL_H
+
+/* Define to 1 if you have the <sys/ioctl.h> header file. */
+#undef HAVE_SYS_IOCTL_H
+
+/* Define to 1 if you have the <sys/select.h> header file. */
+#undef HAVE_SYS_SELECT_H
+
+/* Define to 1 if you have the <sys/socket.h> header file. */
+#undef HAVE_SYS_SOCKET_H
+
+/* Define to 1 if you have the <sys/stat.h> header file. */
+#undef HAVE_SYS_STAT_H
+
+/* Define to 1 if you have the <sys/time.h> header file. */
+#undef HAVE_SYS_TIME_H
+
+/* Define to 1 if you have the <sys/types.h> header file. */
+#undef HAVE_SYS_TYPES_H
+
+/* Define to 1 if you have <sys/wait.h> that is POSIX.1 compatible. */
+#undef HAVE_SYS_WAIT_H
+
+/* Define to 1 if you have the tanf() function. */
+#undef HAVE_TANF
+
+/* Define to 1 if you have the <tgmath.h> header file. */
+#undef HAVE_TGMATH_H
+
+/* Define to 1 if you have the <tiffio.h> header file. */
+#undef HAVE_TIFFIO_H
+
+/* Define to 1 if you have the <unicall.h> header file. */
+#undef HAVE_UNICALL_H
+
+/* Define to 1 if you have the <unistd.h> header file. */
+#undef HAVE_UNISTD_H
+
+/* Define to 1 if you have the `vprintf' function. */
+#undef HAVE_VPRINTF
+
+/* Define to 1 if you have the <X11/X.h> header file. */
+#undef HAVE_X11_X_H
+
+/* Name of package */
+#undef PACKAGE
+
+/* Define to the address where bug reports for this package should be sent. */
+#undef PACKAGE_BUGREPORT
+
+/* Define to the full name of this package. */
+#undef PACKAGE_NAME
+
+/* Define to the full name and version of this package. */
+#undef PACKAGE_STRING
+
+/* Define to the one symbol short name of this package. */
+#undef PACKAGE_TARNAME
+
+/* Define to the version of this package. */
+#undef PACKAGE_VERSION
+
+/* Define as the return type of signal handlers (`int' or `void'). */
+#undef RETSIGTYPE
+
+/* Define to the type of arg 1 for `select'. */
+#undef SELECT_TYPE_ARG1
+
+/* Define to the type of args 2, 3 and 4 for `select'. */
+#undef SELECT_TYPE_ARG234
+
+/* Define to the type of arg 5 for `select'. */
+#undef SELECT_TYPE_ARG5
+
+/* Do not expect a misaligned memory access to work correctly */
+#undef SPANDSP_MISALIGNED_ACCESS_FAILS
+
+/* Enable fixed point processing, where possible, instead of floating point */
+#undef SPANDSP_USE_FIXED_POINT
+
+/* Define to 1 if you have the ANSI C header files. */
+#undef STDC_HEADERS
+
+/* Define to 1 if you can safely include both <sys/time.h> and <time.h>. */
+#undef TIME_WITH_SYS_TIME
+
+/* Version number of package */
+#undef VERSION
+
+/* Define to empty if `const' does not conform to ANSI C. */
+#undef const
+
+/* Define to `__inline__' or `__inline' if that's what the C compiler
+   calls it, or to nothing if 'inline' is not supported under any name.  */
+#ifndef __cplusplus
+#undef inline
+#endif
+
+/* Define to rpl_malloc if the replacement function should be used. */
+#undef malloc
+
+/* Define to rpl_realloc if the replacement function should be used. */
+#undef realloc
+
+/* Define to empty if the keyword `volatile' does not work. Warning: valid
+   code using `volatile' can become incorrect without. Disable with care. */
+#undef volatile

Added: freeswitch/trunk/libs/spandsp/config/ax_c99_features.m4
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/config/ax_c99_features.m4	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,181 @@
+# @synopsis AX_C99_FLEXIBLE_ARRAY
+#
+# Does the compiler support the 1999 ISO C Standard "struct hack".
+# @version 1.1    Mar 15 2004
+# @author Erik de Castro Lopo <erikd AT mega-nerd DOT com>
+#
+# Permission to use, copy, modify, distribute, and sell this file for any 
+# purpose is hereby granted without fee, provided that the above copyright 
+# and this permission notice appear in all copies.  No representations are
+# made about the suitability of this software for any purpose.  It is 
+# provided "as is" without express or implied warranty.
+
+AC_DEFUN([AX_C99_FLEXIBLE_ARRAY],
+[AC_CACHE_CHECK(C99 struct flexible array support, 
+    ac_cv_c99_flexible_array,
+
+# Initialize to unknown
+ac_cv_c99_flexible_array=no
+
+AC_TRY_LINK([[
+    #include <stdlib.h>
+
+    typedef struct {
+    int k;
+    char buffer [] ;
+    } MY_STRUCT ;
+    ]], 
+    [  MY_STRUCT *p = calloc (1, sizeof (MY_STRUCT) + 42); ],
+    ac_cv_c99_flexible_array=yes,
+    ac_cv_c99_flexible_array=no
+    ))]
+) # AX_C99_FLEXIBLE_ARRAY
+
+# @synopsis AX_C99_FUNC_LRINT
+#
+# Check whether C99's lrint function is available.
+# @version 1.3    Feb 12 2002
+# @author Erik de Castro Lopo <erikd AT mega-nerd DOT com>
+#
+# Permission to use, copy, modify, distribute, and sell this file for any 
+# purpose is hereby granted without fee, provided that the above copyright 
+# and this permission notice appear in all copies.  No representations are
+# made about the suitability of this software for any purpose.  It is 
+# provided "as is" without express or implied warranty.
+#
+AC_DEFUN([AX_C99_FUNC_LRINT],
+[AC_CACHE_CHECK(for lrint,
+  ac_cv_c99_lrint,
+[
+lrint_save_CFLAGS=$CFLAGS
+CFLAGS="-lm"
+AC_TRY_LINK([
+#define _ISOC9X_SOURCE  1
+#define _ISOC99_SOURCE  1
+#define __USE_ISOC99    1
+#define __USE_ISOC9X    1
+
+#include <math.h>
+], if (!lrint(3.14159)) lrint(2.7183);, ac_cv_c99_lrint=yes, ac_cv_c99_lrint=no)
+
+CFLAGS=$lrint_save_CFLAGS
+
+])
+
+if test "$ac_cv_c99_lrint" = yes; then
+  AC_DEFINE(HAVE_LRINT, 1,
+            [Define if you have C99's lrint function.])
+fi
+])# AX_C99_FUNC_LRINT
+
+# @synopsis AX_C99_FUNC_LRINTF
+#
+# Check whether C99's lrintf function is available.
+# @version 1.3    Feb 12 2002
+# @author Erik de Castro Lopo <erikd AT mega-nerd DOT com>
+#
+# Permission to use, copy, modify, distribute, and sell this file for any 
+# purpose is hereby granted without fee, provided that the above copyright 
+# and this permission notice appear in all copies.  No representations are
+# made about the suitability of this software for any purpose.  It is 
+# provided "as is" without express or implied warranty.
+#
+AC_DEFUN([AX_C99_FUNC_LRINTF],
+[AC_CACHE_CHECK(for lrintf,
+  ac_cv_c99_lrintf,
+[
+lrintf_save_CFLAGS=$CFLAGS
+CFLAGS="-lm"
+AC_TRY_LINK([
+#define _ISOC9X_SOURCE  1
+#define _ISOC99_SOURCE  1
+#define __USE_ISOC99    1
+#define __USE_ISOC9X    1
+
+#include <math.h>
+], if (!lrintf(3.14159)) lrintf(2.7183);, ac_cv_c99_lrintf=yes, ac_cv_c99_lrintf=no)
+
+CFLAGS=$lrintf_save_CFLAGS
+
+])
+
+if test "$ac_cv_c99_lrintf" = yes; then
+  AC_DEFINE(HAVE_LRINTF, 1,
+            [Define if you have C99's lrintf function.])
+fi
+])# AX_C99_FUNC_LRINTF
+
+# @synopsis AX_C99_FUNC_LLRINT
+#
+# Check whether C99's llrint function is available.
+# @version 1.1    Sep 30 2002
+# @author Erik de Castro Lopo <erikd AT mega-nerd DOT com>
+#
+# Permission to use, copy, modify, distribute, and sell this file for any 
+# purpose is hereby granted without fee, provided that the above copyright 
+# and this permission notice appear in all copies.  No representations are
+# made about the suitability of this software for any purpose.  It is 
+# provided "as is" without express or implied warranty.
+#
+AC_DEFUN([AX_C99_FUNC_LLRINT],
+[AC_CACHE_CHECK(for llrint,
+  ac_cv_c99_llrint,
+[
+llrint_save_CFLAGS=$CFLAGS
+CFLAGS="-lm"
+AC_TRY_LINK([
+#define ISOC9X_SOURCE   1
+#define _ISOC99_SOURCE  1
+#define __USE_ISOC99    1
+#define __USE_ISOC9X    1
+
+#include <math.h>
+], long long int x ; x = llrint(3.14159) ;, ac_cv_c99_llrint=yes, ac_cv_c99_llrint=no)
+
+CFLAGS=$llrint_save_CFLAGS
+
+])
+
+if test "$ac_cv_c99_llrint" = yes; then
+  AC_DEFINE(HAVE_LLRINT, 1,
+            [Define if you have C99's llrint function.])
+fi
+])# AX_C99_FUNC_LLRINT
+
+
+# @synopsis AX_C99_FUNC_LLRINTF
+#
+# Check whether C99's llrintf function is available.
+# @version 1.1    Sep 30 2002
+# @author Erik de Castro Lopo <erikd AT mega-nerd DOT com>
+#
+# Permission to use, copy, modify, distribute, and sell this file for any 
+# purpose is hereby granted without fee, provided that the above copyright 
+# and this permission notice appear in all copies.  No representations are
+# made about the suitability of this software for any purpose.  It is 
+# provided "as is" without express or implied warranty.
+#
+AC_DEFUN([AX_C99_FUNC_LLRINTF],
+[AC_CACHE_CHECK(for llrintf,
+  ac_cv_c99_llrintf,
+[
+llrintf_save_CFLAGS=$CFLAGS
+CFLAGS="-lm"
+AC_TRY_LINK([
+#define _ISOC9X_SOURCE  1
+#define _ISOC99_SOURCE  1
+#define __USE_ISOC99    1
+#define __USE_ISOC9X    1
+
+#include <math.h>
+], long long int x ; x = llrintf(3.14159) ;, ac_cv_c99_llrintf=yes, ac_cv_c99_llrintf=no)
+
+CFLAGS=$llrintf_save_CFLAGS
+
+])
+
+if test "$ac_cv_c99_llrintf" = yes; then
+  AC_DEFINE(HAVE_LLRINTF, 1,
+            [Define if you have C99's llrintf function.])
+fi
+])# AX_C99_FUNC_LLRINTF

Added: freeswitch/trunk/libs/spandsp/config/ax_check_real_file.m4
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/config/ax_check_real_file.m4	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,27 @@
+# AX_CHECK_REAL_FILE(FILE, [ACTION-IF-FOUND], [ACTION-IF-NOT-FOUND])
+# ------------------------------------------------------------------
+#
+# Check for the existence of FILE, and make sure it is a real file or
+# directory, and not a symbolic link.
+#
+AC_DEFUN([AX_CHECK_REAL_FILE],
+[AC_DIAGNOSE([cross],
+	     [cannot check for file existence when cross compiling])dnl
+AS_VAR_PUSHDEF([ac_RealFile], [ac_cv_real_file_$1])dnl
+AC_CACHE_CHECK([for $1], ac_RealFile,
+[test "$cross_compiling" = yes &&
+  AC_MSG_ERROR([cannot check for file existence when cross compiling])
+if test -r "$1"
+then
+  if test -h "$1"
+  then
+    AS_VAR_SET(ac_RealFile, no)
+  else
+    AS_VAR_SET(ac_RealFile, yes)
+  fi
+else
+  AS_VAR_SET(ac_RealFile, no)
+fi])
+AS_IF([test AS_VAR_GET(ac_RealFile) = yes], [$2], [$3])[]dnl
+AS_VAR_POPDEF([ac_RealFile])dnl
+])# AX_CHECK_REAL_FILE

Added: freeswitch/trunk/libs/spandsp/config/ax_compiler_vendor.m4
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/config/ax_compiler_vendor.m4	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,65 @@
+# ===========================================================================
+#           http://autoconf-archive.cryp.to/ax_compiler_vendor.html
+# ===========================================================================
+#
+# SYNOPSIS
+#
+#   AX_COMPILER_VENDOR
+#
+# DESCRIPTION
+#
+#   Determine the vendor of the C/C++ compiler, e.g., gnu, intel, ibm, sun,
+#   hp, borland, comeau, dec, cray, kai, lcc, metrowerks, sgi, microsoft,
+#   watcom, etc. The vendor is returned in the cache variable
+#   $ax_cv_c_compiler_vendor for C and $ax_cv_cxx_compiler_vendor for C++.
+#
+# LAST MODIFICATION
+#
+#   2008-04-12
+#
+# COPYLEFT
+#
+#   Copyright (c) 2008 Steven G. Johnson <stevenj at alum.mit.edu>
+#   Copyright (c) 2008 Matteo Frigo
+#
+#   This program is free software: you can redistribute it and/or modify it
+#   under the terms of the GNU General Public License as published by the
+#   Free Software Foundation, either version 3 of the License, or (at your
+#   option) any later version.
+#
+#   This program is distributed in the hope that it will be useful, but
+#   WITHOUT ANY WARRANTY; without even the implied warranty of
+#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
+#   Public License for more details.
+#
+#   You should have received a copy of the GNU General Public License along
+#   with this program. If not, see <http://www.gnu.org/licenses/>.
+#
+#   As a special exception, the respective Autoconf Macro's copyright owner
+#   gives unlimited permission to copy, distribute and modify the configure
+#   scripts that are the output of Autoconf when processing the Macro. You
+#   need not follow the terms of the GNU General Public License when using
+#   or distributing such scripts, even though portions of the text of the
+#   Macro appear in them. The GNU General Public License (GPL) does govern
+#   all other use of the material that constitutes the Autoconf Macro.
+#
+#   This special exception to the GPL applies to versions of the Autoconf
+#   Macro released by the Autoconf Macro Archive. When you make and
+#   distribute a modified version of the Autoconf Macro, you may extend this
+#   special exception to the GPL to apply to your modified version as well.
+
+AC_DEFUN([AX_COMPILER_VENDOR],
+[
+AC_CACHE_CHECK([for _AC_LANG compiler vendor], ax_cv_[]_AC_LANG_ABBREV[]_compiler_vendor,
+ [ax_cv_[]_AC_LANG_ABBREV[]_compiler_vendor=unknown
+  # note: don't check for gcc first since some other compilers define __GNUC__
+  for ventest in intel:__ICC,__ECC,__INTEL_COMPILER ibm:__xlc__,__xlC__,__IBMC__,__IBMCPP__ pathscale:__PATHCC__,__PATHSCALE__ gnu:__GNUC__ sun:__SUNPRO_C,__SUNPRO_CC hp:__HP_cc,__HP_aCC dec:__DECC,__DECCXX,__DECC_VER,__DECCXX_VER borland:__BORLANDC__,__TURBOC__ comeau:__COMO__ cray:_CRAYC kai:__KCC lcc:__LCC__ metrowerks:__MWERKS__ sgi:__sgi,sgi microsoft:_MSC_VER watcom:__WATCOMC__ portland:__PGI; do
+    vencpp="defined("`echo $ventest | cut -d: -f2 | sed 's/,/) || defined(/g'`")"
+    AC_COMPILE_IFELSE([AC_LANG_PROGRAM(,[
+#if !($vencpp)
+      thisisanerror;
+#endif
+])], [ax_cv_]_AC_LANG_ABBREV[_compiler_vendor=`echo $ventest | cut -d: -f1`; break])
+  done
+ ])
+])

Added: freeswitch/trunk/libs/spandsp/config/ax_fixed_point_machine.m4
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/config/ax_fixed_point_machine.m4	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,35 @@
+# AX_FIXED_POINT_MACHINE(MACHINE, [ACTION-IF-FIXED-POINT], [ACTION-IF-NOT-FIXED-POINT])
+# -------------------------------------------------------------------------------------
+#
+# Check if a specified machine type is a fixed point only machine. That is, if it lacks
+# fast floating point support.
+#
+# This is a simple lookup amongst machines known to the current autotools. So far we deal
+# with the embedded ARM, Blackfin, MIPS, TI DSP and XScale processors as things which lack
+# fast hardware floating point.
+#
+# Other candidates would be the small embedded Power PCs.
+#
+AC_DEFUN([AX_FIXED_POINT_MACHINE],
+[AS_VAR_PUSHDEF([ac_FixedPoint], [ac_cv_fixed_point_machine_$1])dnl
+AC_CACHE_CHECK([if $1 is fixed point only], ac_FixedPoint,
+[case $1 in
+      arc | arm | arm[bl]e | arme[lb] | armv[2345] | armv[345][lb] \
+    | bfin \
+    | mips | mipsbe | mipseb | mipsel | mipsle \
+    | tic54x | c54x* | tic55x | c55x* | tic6x | c6x* \
+    | xscale | xscalee[bl] \
+    | arm-*  | armbe-* | armle-* | armeb-* | armv*-* \
+    | bfin-* \
+    | mips-* | mipsbe-* | mipseb-* | mipsel-* | mipsle-* \
+    | tic30-* | tic4x-* | tic54x-* | tic55x-* | tic6x-* | tic80-* \
+    | xscale-* | xscalee[bl]-* )
+        AS_VAR_SET(ac_FixedPoint, yes)
+        ;;
+    *)
+        AS_VAR_SET(ac_FixedPoint, no)
+        ;;
+esac])
+AS_IF([test AS_VAR_GET(ac_FixedPoint) = yes], [$2], [$3])[]dnl
+AS_VAR_POPDEF([ac_FixedPoint])dnl
+])# AX_FIXED_POINT_MACHINE

Added: freeswitch/trunk/libs/spandsp/config/ax_misaligned_access_fails.m4
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/config/ax_misaligned_access_fails.m4	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,33 @@
+# AX_MISALIGNED_ACCESS_FAILS(MACHINE, [ACTION-IF-MISALIGNED-FAILS], [ACTION-IF-MISALIGNED-OK])
+# -------------------------------------------------------------------------------------
+#
+# Check if a specified machine type cannot handle misaligned data. That is, multi-byte data
+# types which are not properly aligned in memory fail. Many machines are happy to work with
+# misaligned data, but slowing down a bit. Other machines just won't tolerate such data.
+#
+# This is a simple lookup amongst machines known to the current autotools. So far we only deal
+# with the ARM and sparc.
+# A lookup is used, as many of the devices which cannot handled misaligned access are embedded
+# processors, for which the code normally be cross-compiled. 
+#
+AC_DEFUN([AX_MISALIGNED_ACCESS_FAILS],
+[AS_VAR_PUSHDEF([ac_MisalignedAccessFails], [ac_cv_misaligned_access_fails_$1])dnl
+AC_CACHE_CHECK([if $1 fails on misaligned memory access], ac_MisalignedAccessFails,
+[case $1 in
+      arc | arm | arm[bl]e | arme[lb] | armv[2345] | armv[345][lb] \
+    | bfin \
+    | sparc \
+    | xscale | xscalee[bl] \
+    | arm-*  | armbe-* | armle-* | armeb-* | armv*-* \
+    | bfin-* \
+    | sparc-* \
+    | xscale-* | xscalee[bl]-* )
+        AS_VAR_SET(ac_MisalignedAccessFails, yes)
+        ;;
+    *)
+        AS_VAR_SET(ac_MisalignedAccessFails, no)
+        ;;
+esac])
+AS_IF([test AS_VAR_GET(ac_MisalignedAccessFails) = yes], [$2], [$3])[]dnl
+AS_VAR_POPDEF([ac_MisalignedAccessFails])dnl
+])# MISALIGNED_ACCESS_FAILS

Added: freeswitch/trunk/libs/spandsp/configure.ac
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/configure.ac	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,371 @@
+#
+# SpanDSP - a series of DSP components for telephony
+#
+# configure.ac - Process this file with autoconf to produce configure
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU Lesser General Public License version 2.1,
+# as published by the Free Software Foundation.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this program; if not, write to the Free Software
+# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+#
+# $Id: configure.ac,v 1.56 2008/07/10 12:44:54 steveu Exp $
+
+# @start 1
+
+AC_INIT
+
+m4_include(config/ax_compiler_vendor.m4)
+m4_include(config/ax_check_real_file.m4)
+m4_include(config/ax_fixed_point_machine.m4)
+m4_include(config/ax_misaligned_access_fails.m4)
+m4_include(config/ax_c99_features.m4)
+
+SPANDSP_MAJOR_VERSION=0
+SPANDSP_MINOR_VERSION=0
+SPANDSP_MICRO_VERSION=5
+
+SPANDSP_LT_CURRENT=1
+SPANDSP_LT_REVISION=0
+SPANDSP_LT_AGE=0
+
+VERSION=$SPANDSP_MAJOR_VERSION.$SPANDSP_MINOR_VERSION.$SPANDSP_MICRO_VERSION
+PACKAGE=spandsp
+
+AC_SUBST(SPANDSP_LT_CURRENT)
+AC_SUBST(SPANDSP_LT_REVISION)
+AC_SUBST(SPANDSP_LT_AGE)
+
+AC_CONFIG_SRCDIR([src/tone_generate.c])
+AC_CONFIG_AUX_DIR(config)
+AC_CONFIG_HEADERS([src/config.h:config-h.in])
+AM_INIT_AUTOMAKE($PACKAGE, $VERSION)
+
+AC_CANONICAL_HOST
+AC_CANONICAL_BUILD
+AC_PROG_CC
+AC_PROG_CXX
+AC_PROG_GCC_TRADITIONAL
+AC_PROG_LIBTOOL
+AC_LANG([C])
+
+AX_COMPILER_VENDOR
+
+if test "${build}" != "${host}"
+then
+  # If we are doing a Canadian Cross, in which the host and build systems
+  # are not the same, we set reasonable default values for the tools.
+
+  CC_FOR_BUILD=${CC_FOR_BUILD-gcc}
+  CPPFLAGS_FOR_BUILD="\$(CPPFLAGS)"
+  CC=${CC-${host_alias}-gcc}
+  CFLAGS=${CFLAGS-"-g -O2"}
+  CXX=${CXX-${host_alias}-c++}
+  CXXFLAGS=${CXXFLAGS-"-g -O2"}
+else
+  # Set reasonable default values for some tools even if not Canadian.
+  # Of course, these are different reasonable default values, originally
+  # specified directly in the Makefile.
+  # We don't export, so that autoconf can do its job.
+  # Note that all these settings are above the fragment inclusion point
+  # in Makefile.in, so can still be overridden by fragments.
+  # This is all going to change when we autoconfiscate...
+  CC_FOR_BUILD="\$(CC)"
+  CPPFLAGS_FOR_BUILD="\$(CPPFLAGS)"
+  AC_PROG_CC
+
+  # We must set the default linker to the linker used by gcc for the correct
+  # operation of libtool.  If LD is not defined and we are using gcc, try to
+  # set the LD default to the ld used by gcc.
+  if test -z "$LD"
+  then
+    if test "$GCC" = yes
+    then
+      case $build in
+      *-*-mingw*)
+        gcc_prog_ld=`$CC -print-prog-name=ld 2>&1 | tr -d '\015'` ;;
+      *)
+        gcc_prog_ld=`$CC -print-prog-name=ld 2>&1` ;;
+      esac
+      case $gcc_prog_ld in
+      # Accept absolute paths.
+      [[\\/]* | [A-Za-z]:[\\/]*)]
+        LD="$gcc_prog_ld" ;;
+      esac
+    fi
+  fi
+
+  CXX=${CXX-"c++"}
+  CFLAGS=${CFLAGS-"-g -O2"}
+  CXXFLAGS=${CXXFLAGS-"-g -O2"}
+fi
+
+AC_C_CONST
+AC_C_INLINE
+AC_C_VOLATILE
+
+AC_CHECK_TYPES(long long)
+AC_CHECK_TYPES(long double)
+
+AC_TYPE_SIGNAL
+
+AC_ARG_ENABLE(doc,          [  --enable-doc         Build the documentation])
+AC_ARG_ENABLE(tests,        [  --enable-tests       Build the test programs])
+AC_ARG_ENABLE(test_data,    [  --enable-test-data   Build TIFF test files for some ITU test images])
+AC_ARG_ENABLE(mmx,          [  --enable-mmx         Enable MMX support])
+AC_ARG_ENABLE(sse,          [  --enable-sse         Enable SSE support])
+AC_ARG_ENABLE(fixed_point,  [  --enable-fixed-point Enable fixed point support])
+
+AC_FUNC_ERROR_AT_LINE
+AC_FUNC_VPRINTF
+AC_FUNC_MALLOC
+AC_FUNC_MEMCMP
+AC_FUNC_REALLOC
+AC_FUNC_SELECT_ARGTYPES
+
+AX_C99_FUNC_LRINT
+AX_C99_FUNC_LRINTF
+AX_C99_FUNC_LLRINT
+AX_C99_FUNC_LLRINTF
+
+if test "x$ac_cv_c99_lrint" = "xno" ; then
+    if test "x$ac_cv_c99_lrintf" = "xno" ; then
+        AC_MSG_WARN([[*** Missing C99 standard functions lrint() and lrintf().]])
+        AC_MSG_WARN([[*** This may cause benign compiler warnings on some systems (ie Solaris).]])
+    fi
+fi
+
+AX_C99_FLEXIBLE_ARRAY
+
+AC_CHECK_FUNCS([memmove])
+AC_CHECK_FUNCS([memset])
+AC_CHECK_FUNCS([select])
+AC_CHECK_FUNCS([strcasecmp])
+AC_CHECK_FUNCS([strchr])
+AC_CHECK_FUNCS([strdup])
+AC_CHECK_FUNCS([strerror])
+AC_CHECK_FUNCS([strstr])
+AC_CHECK_FUNCS([strtol])
+AC_CHECK_FUNCS([gettimeofday])
+
+AC_HEADER_STDC
+AC_HEADER_SYS_WAIT
+AC_HEADER_TIME
+
+# Check for header files.
+AC_CHECK_HEADERS([socket.h])
+AC_CHECK_HEADERS([inttypes.h], [INSERT_INTTYPES_HEADER="#include <inttypes.h>"])
+AC_CHECK_HEADERS([stdint.h], [INSERT_STDINT_HEADER="#include <stdint.h>"])
+AC_CHECK_HEADERS([unistd.h])
+AC_CHECK_HEADERS([stdlib.h])
+AC_CHECK_HEADERS([string.h])
+AC_CHECK_HEADERS([strings.h])
+AC_CHECK_HEADERS([malloc.h])
+AC_CHECK_HEADERS([tgmath.h], [INSERT_TGMATH_HEADER="#include <tgmath.h>"])
+AC_CHECK_HEADERS([math.h], [INSERT_MATH_HEADER="#include <math.h>"])
+AC_CHECK_HEADERS([float.h])
+AC_CHECK_HEADERS([fcntl.h])
+AC_CHECK_HEADERS([sys/time.h])
+AC_CHECK_HEADERS([sys/select.h])
+AC_CHECK_HEADERS([sys/ioctl.h])
+AC_CHECK_HEADERS([sys/fcntl.h])
+AC_CHECK_HEADERS([audiofile.h])
+AC_CHECK_HEADERS([fftw3.h], , [AC_CHECK_HEADERS([fftw.h])])
+AC_CHECK_HEADERS([tiffio.h])
+AC_CHECK_HEADERS([pthread.h])
+AC_CHECK_HEADERS([unicall.h])
+if test "${build}" == "${host}"
+then
+  AC_CHECK_HEADERS([X11/X.h])
+fi
+
+# Determine XML2 include path
+AC_MSG_CHECKING(for libxml/xmlmemory.h)
+
+# Can we include headers using system include dirs?
+AC_TRY_COMPILE([#include <libxml/xmlmemory.h>], [int a = 1;],
+    XML2_INCLUDE=" ",
+    XML2_INCLUDE=
+)
+
+# Hunt through several possible directories to find the includes for libxml2
+if test "x$XML2_INCLUDE" = "x"; then
+    old_CPPFLAGS="$CPPFLAGS"
+    for i in $xml2_include_dir /usr/include /usr/local/include /usr/include/libxml2 /usr/local/include/libxml2 ; do
+        CPPFLAGS="$old_CPPFLAGS -I$i"
+        AC_TRY_COMPILE([#include <libxml/xmlmemory.h>], [int a = 1;],
+            XML2_INCLUDE="-I$i",
+            XML2_INCLUDE=
+        )
+        if test "x$XML2_INCLUDE" != "x"; then
+            break;
+        fi
+    done
+    CPPFLAGS="$old_CPPFLAGS $XML2_INCLUDE"
+fi
+
+AC_CHECK_HEADERS([libxml/xmlmemory.h])
+AC_CHECK_HEADERS([libxml/parser.h])
+AC_CHECK_HEADERS([libxml/xinclude.h])
+
+AC_LANG([C++])
+AC_CHECK_HEADERS([FL/Fl.H])
+AC_CHECK_HEADERS([FL/Fl_Overlay_Window.H])
+AC_CHECK_HEADERS([FL/Fl_Light_Button.H])
+AC_CHECK_HEADERS([FL/fl_draw.H])
+AC_CHECK_HEADERS([FL/Fl_Cartesian.H], [], [], [],[[#include <FL/Fl.H>
+]])
+AC_CHECK_HEADERS([FL/Fl_Audio_Meter.H], [], [], [],[[#include <FL/Fl.H>
+]])
+
+AC_LANG([C])
+
+if test "${build}" == "${host}"
+then
+    case "${host}" in
+    x86_64-*)
+        AX_CHECK_REAL_FILE([${prefix}/lib64], libdir='$(exec_prefix)/lib64')
+        ;;
+    esac
+fi
+
+AC_CHECK_LIB([m], [cos])
+# Some platforms still seem to lack the basic single precision trig and power related function.
+AC_SEARCH_LIBS([sinf], [m], AC_DEFINE([HAVE_SINF], [1], [Define to 1 if you have the sinf() function.]))
+AC_SEARCH_LIBS([cosf], [m], AC_DEFINE([HAVE_COSF], [1], [Define to 1 if you have the cosf() function.]))
+AC_SEARCH_LIBS([tanf], [m], AC_DEFINE([HAVE_TANF], [1], [Define to 1 if you have the tanf() function.]))
+AC_SEARCH_LIBS([asinf], [m], AC_DEFINE([HAVE_ASINF], [1], [Define to 1 if you have the asinf() function.]))
+AC_SEARCH_LIBS([acosf], [m], AC_DEFINE([HAVE_ACOSF], [1], [Define to 1 if you have the acosf() function.]))
+AC_SEARCH_LIBS([atanf], [m], AC_DEFINE([HAVE_ATANF], [1], [Define to 1 if you have the atanf() function.]))
+AC_SEARCH_LIBS([atan2f], [m], AC_DEFINE([HAVE_ATAN2F], [1], [Define to 1 if you have the atan2f() function.]))
+AC_SEARCH_LIBS([ceilf], [m], AC_DEFINE([HAVE_CEILF], [1], [Define to 1 if you have the ceilf() function.]))
+AC_SEARCH_LIBS([floorf], [m], AC_DEFINE([HAVE_FLOORF], [1], [Define to 1 if you have the floorf() function.]))
+AC_SEARCH_LIBS([powf], [m], AC_DEFINE([HAVE_POWF], [1], [Define to 1 if you have the powf() function.]))
+AC_SEARCH_LIBS([expf], [m], AC_DEFINE([HAVE_EXPF], [1], [Define to 1 if you have the expf() function.]))
+AC_SEARCH_LIBS([logf], [m], AC_DEFINE([HAVE_LOGF], [1], [Define to 1 if you have the logf() function.]))
+AC_SEARCH_LIBS([log10f], [m], AC_DEFINE([HAVE_LOG10F], [1], [Define to 1 if you have the log10f() function.]))
+
+if test "${build}" == "${host}"
+then
+    case "${host}" in
+    x86_64-*)
+        # X86_64 Linux machines may have both 64 bit and 32 bit libraries. We need to choose the right set
+        AX_CHECK_REAL_FILE([/usr/X11R6/lib64], [TESTLIBS="$TESTLIBS -L/usr/X11R6/lib64"], AC_CHECK_FILE([/usr/X11R6/lib], [TESTLIBS="$TESTLIBS -L/usr/X11R6/lib"]))
+        ;;
+    esac
+fi
+
+# Checks for libraries.
+AC_CHECK_LIB([tiff], [TIFFOpen], , AC_MSG_ERROR("Can't build without libtiff (does your system require a libtiff-devel package?)"), -lm)
+
+AC_CHECK_LIB([xml2], [xmlParseFile], [AC_DEFINE([HAVE_LIBXML2], [1], [Define to 1 if you have the 'libxml2' library (-lxml2).]) TESTLIBS="$TESTLIBS -lxml2"])
+
+if test -n "$enable_tests" ; then
+    AC_LANG([C])
+    AC_CHECK_LIB([audiofile], [afOpenFile], TESTLIBS="$TESTLIBS -laudiofile", AC_MSG_ERROR("Can't make tests without libaudiofile (does your system require a libaudiofile-devel package?)"))
+    AC_CHECK_LIB([fftw3], [fftw_plan_dft_1d], TESTLIBS="$TESTLIBS -lfftw3", [AC_CHECK_LIB([fftw], [fftw_create_plan], TESTLIBS="$TESTLIBS -lfftw")])
+    AC_CHECK_LIB([pthread], [pthread_attr_init], TESTLIBS="$TESTLIBS -lpthread")
+    AC_CHECK_LIB([dl], [dlopen], TESTLIBS="$TESTLIBS -ldl")
+    AC_CHECK_LIB([unicall], [uc_start], TESTLIBS="$TESTLIBS -lunicall",, -ltiff -ldl)
+    AC_CHECK_LIB([Xft], [XftFontOpen], TESTLIBS="$TESTLIBS -lXft",, $TESTLIBS)
+    AC_CHECK_LIB([Xext], [XextCreateExtension], TESTLIBS="$TESTLIBS -lXext",, $TESTLIBS)
+    AC_CHECK_LIB([X11], [XOpenDisplay], TESTLIBS="$TESTLIBS -lX11",, $TESTLIBS)
+    AC_LANG([C++])
+    AC_CHECK_LIB([fltk], [main], TESTLIBS="$TESTLIBS -lfltk -lsupc++",, $TESTLIBS)
+    AC_CHECK_LIB([fltk_cartesian], [main], TESTLIBS="-lfltk_cartesian $TESTLIBS",, $TESTLIBS)
+    AC_CHECK_LIB([fltk_audio_meter], [main], TESTLIBS="-lfltk_audio_meter $TESTLIBS",, $TESTLIBS)
+    AC_LANG([C])
+fi
+
+case "${ax_cv_c_compiler_vendor}" in
+gnu)
+    COMP_VENDOR_CFLAGS="-std=gnu99 -ffast-math -Wall -Wunused-variable -Wwrite-strings -Wstrict-prototypes -Wmissing-prototypes"
+    if test "$enable_sse" = "yes" ; then
+        COMP_VENDOR_CFLAGS="-msse $COMP_VENDOR_CFLAGS"
+    fi
+    if test "$enable_mmx" = "yes" ; then
+        COMP_VENDOR_CFLAGS="-mmmx $COMP_VENDOR_CFLAGS"
+    fi
+    case $host_os in
+    mingw* | cygwin*)
+        COMP_VENDOR_LDFLAGS="-no-undefined"
+        ;;
+    *)
+        COMP_VENDOR_LDFLAGS=
+        ;;
+    esac
+    ;;
+sun)
+    COMP_VENDOR_CFLAGS="-xc99=all -mt -xCC -errwarn=%all -xvpara"
+    if test "$enable_sse" = "yes" ; then
+        COMP_VENDOR_CFLAGS="-native -fast $COMP_VENDOR_CFLAGS"
+    fi
+    if test "$enable_mmx" = "yes" ; then
+        COMP_VENDOR_CFLAGS="-native -fast $COMP_VENDOR_CFLAGS"
+    fi
+    COMP_VENDOR_LDFLAGS=
+    ;;
+*)
+    COMP_VENDOR_CFLAGS="-std=c99 -Wall -Wunused-variable -Wwrite-strings -Wstrict-prototypes -Wmissing-prototypes"
+    COMP_VENDOR_LDFLAGS=
+    ;;
+esac
+
+COMP_VENDOR_CFLAGS="-DNDEBUG $COMP_VENDOR_CFLAGS"
+
+AM_CONDITIONAL([COND_DOC], [test "$enable_doc" = yes])
+AM_CONDITIONAL([COND_TESTS], [test "$enable_tests" = yes])
+AM_CONDITIONAL([COND_TESTDATA], [test "$enable_test_data" = yes])
+AM_CONDITIONAL([COND_MMX], [test "$enable_mmx" = yes])
+AM_CONDITIONAL([COND_SSE], [test "$enable_sse" = yes])
+if test "$enable_fixed_point" = "yes" ; then
+    AC_DEFINE([SPANDSP_USE_FIXED_POINT], [1], [Enable fixed point processing, where possible, instead of floating point])
+    SPANDSP_USE_FIXED_POINT="#define SPANDSP_USE_FIXED_POINT 1"
+else
+    AX_FIXED_POINT_MACHINE([$host],
+        [AC_DEFINE([SPANDSP_USE_FIXED_POINT], [1], [Enable fixed point processing, where possible, instead of floating point])
+        SPANDSP_USE_FIXED_POINT="#define SPANDSP_USE_FIXED_POINT 1"],
+        [SPANDSP_USE_FIXED_POINT="#undef SPANDSP_USE_FIXED_POINT"])
+fi
+AX_MISALIGNED_ACCESS_FAILS([$host],
+    [AC_DEFINE([SPANDSP_MISALIGNED_ACCESS_FAILS], [1], [Do not expect a misaligned memory access to work correctly])
+    SPANDSP_MISALIGNED_ACCESS_FAILS="#define SPANDSP_MISALIGNED_ACCESS_FAILS 1"],
+    [SPANDSP_MISALIGNED_ACCESS_FAILS="#undef SPANDSP_MISALIGNED_ACCESS_FAILS"])
+
+AC_SUBST(CC_FOR_BUILD)
+AC_SUBST(CPPFLAGS_FOR_BUILD)
+AC_SUBST(COMP_VENDOR_CFLAGS)
+AC_SUBST(COMP_VENDOR_LDFLAGS)
+AC_SUBST(TESTLIBS)
+AC_SUBST(SPANDSP_USE_FIXED_POINT)
+AC_SUBST(SPANDSP_MISALIGNED_ACCESS_FAILS)
+AC_SUBST(INSERT_INTTYPES_HEADER)
+AC_SUBST(INSERT_STDINT_HEADER)
+AC_SUBST(INSERT_TGMATH_HEADER)
+AC_SUBST(INSERT_MATH_HEADER)
+
+AC_CONFIG_FILES([Makefile
+                 doc/Makefile
+                 doc/doxygen
+                 src/Makefile
+                 src/spandsp.h
+                 spandsp-sim/Makefile
+                 test-data/Makefile
+                 test-data/etsi/Makefile
+                 test-data/etsi/fax/Makefile
+                 test-data/itu/Makefile
+                 test-data/itu/fax/Makefile
+                 test-data/local/Makefile
+                 tests/Makefile
+                 spandsp.spec])
+
+AC_OUTPUT
+
+# @end 1

Added: freeswitch/trunk/libs/spandsp/debian/changelog
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/debian/changelog	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,109 @@
+spandsp (0.0.5~pre2-1) unstable; urgency=low
+
+  [ Massimo Cetra ]
+  * New upstream release.
+  * Fixed upstream file name for get-orig-source.
+  * Updates to 64bit archs support
+  * Removal of obsoleted files
+
+ -- Massimo Cetra <devel at navynet.it>  Mon, 5 May 2008 09:23:36 +0100
+
+spandsp (0.0.4~pre3-1) unstable; urgency=low
+
+  [ Santiago Ruano Rincón ]
+  * Added Conflicts: libspandsp2
+  * Fixed URL in debian/rules
+
+  [ Tzafrir Cohen ]
+  * New upstream release.
+  * Fixed upstream file name for get-orig-source.
+  * standards version updated to 3.7.2 .
+
+ -- Tzafrir Cohen <tzafrir.cohen at xorcom.com>  Mon, 18 Jun 2007 09:53:06 +0300
+
+spandsp (0.0.4~pre1-1) experimental; urgency=low
+
+  * New upstream release.
+
+ -- Santiago Ruano Rincón <santiago at debian.org>  Fri, 11 May 2007 15:24:41 -0500
+
+spandsp (0.0.3pre27-2) experimental; urgency=low
+
+  * Conflict with libspandsp1 due to /usr/share/spandsp/global-tones.xml
+
+ -- Kilian Krause <kilian at debian.org>  Thu,  4 Jan 2007 00:29:10 +0100
+
+spandsp (0.0.3pre27-1) experimental; urgency=low
+
+  * New upstream release.
+  * Bump library name due to API incompatibility with 0.0.2 versions.
+
+ -- Kilian Krause <kilian at debian.org>  Wed,  3 Jan 2007 21:31:17 +0100
+
+spandsp (0.0.3pre26-1) experimental; urgency=low
+
+  * New upstream release
+    - New version of spandsp available (Closes: #339293)
+    - Remove src/spandsp/mmx.h is not GPL and has no license attached (Closes:
+    #357813)
+  * Lintian: outdated-autotools-helper-file - Build-Depends: autotools-dev
+  * Add debian/watch
+
+ -- Mark Purcell <msp at debian.org>  Sat,  9 Dec 2006 14:00:57 +0000
+
+spandsp (0.0.2pre26-1) unstable; urgency=low
+
+  * New upstream release.
+  * Added get-orig-source target.
+  * Removed unneeded and buggy nommx.dpatch (Closes: #376249, #377374)
+
+ -- Tzafrir Cohen <tzafrir.cohen at xorcom.com>  Tue,  1 Aug 2006 06:27:47 +0100
+
+spandsp (0.0.2pre25-1) unstable; urgency=low
+
+  * New upstream version.
+  * debian/copyright: Source's URL in copyright file updated to the new author' site.
+  * Added -doc package, with the API documentation.
+  * Fixed package name to follow new upstream's version.
+
+ -- Kilian Krause <kilian at debian.org>  Sat, 18 Feb 2006 22:10:21 +0100
+
+spandsp (0.0.2pre17-1) unstable; urgency=low
+
+  * New upstream version.
+
+ -- Kilian Krause <kk at verfaction.de>  Mon,  9 May 2005 21:56:53 +0200
+
+spandsp (0.0.2pre10-3) unstable; urgency=low
+
+  * debian/control: fixed overrides. again. (Back to the old optional)
+
+ -- Kilian Krause <kk at verfaction.de>  Mon, 21 Mar 2005 10:39:30 +0100
+
+spandsp (0.0.2pre10-2) unstable; urgency=low
+
+  * debian/control: fixed overrides.
+  * debian/patches/mmx.dpatch: Added compilation on non-i386 and non-amd64.
+
+ -- Kilian Krause <kk at verfaction.de>  Sun, 20 Mar 2005 23:32:48 +0100
+
+spandsp (0.0.2pre10-1) unstable; urgency=low
+
+  * New upstream release.
+  * Debian VoIP Team takes this package from Simon. Thanks!
+  * debian/rules: fixed how config.[sub,guess] files are linked from
+    autotools-dev and cleaned.
+
+ -- Kilian Krause <kk at verfaction.de>  Sat, 22 Jan 2005 13:44:28 +0100
+
+spandsp (0.0.2-2) unstable; urgency=low
+
+  * Corrected build dependencies.
+
+ -- Simon Richter <sjr at debian.org>  Mon,  3 Jan 2005 13:33:33 +0100
+
+spandsp (0.0.2-1) unstable; urgency=low
+
+  * Initial Release (Closes: #262032).
+
+ -- Simon Richter <sjr at debian.org>  Sun,  2 Jan 2005 15:22:58 +0100

Added: freeswitch/trunk/libs/spandsp/debian/compat
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/debian/compat	Wed Sep  3 15:02:00 2008
@@ -0,0 +1 @@
+4

Added: freeswitch/trunk/libs/spandsp/debian/control
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/debian/control	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,42 @@
+Source: spandsp
+Section: libs
+Priority: optional
+Maintainer: Debian VoIP Team <pkg-voip-maintainers at lists.alioth.debian.org>
+Uploaders: Jose Carlos Garcia Sogo <jsogo at debian.org>, Kilian Krause <kilian at debian.org>, Santiago Garcia Mantinan <manty at debian.org>, Mark Purcell <msp at debian.org>, Tzafrir Cohen <tzafrir.cohen at xorcom.com>, Santiago Ruano Rincón <santiago at debian.org>
+Build-Depends: debhelper (>= 4.0.0), libtiff4-dev, libjpeg62-dev, dpatch, doxygen, autotools-dev, xsltproc
+Standards-Version: 3.7.2
+XS-Vcs-Svn: svn://svn.debian.org/pkg-voip/
+XS-Vcs-Browser: http://svn.debian.org/wsvn/pkg-voip/
+
+Package: libspandsp5
+Architecture: any
+Depends: ${shlibs:Depends}
+Conflicts: libspandsp0, libspandsp1, libspandsp2
+Description: Telephony signal processing library
+ This is a low-level signal processing library that modulate and demodulate
+ signals commonly used in telephony, such as the "noise" generated by a
+ fax modem or DTMF touchpad.
+ .
+ This package contains the shared library.
+
+Package: libspandsp-dev
+Section: libdevel
+Architecture: any
+Depends: libspandsp5 (= ${Source-Version}), libtiff4-dev, libjpeg62-dev
+Description: Telephony signal processing library
+ This is a low-level signal processing library that modulate and demodulate
+ signals commonly used in telephony, such as the "noise" generated by a
+ fax modem or DTMF touchpad.
+ .
+ This package contains the static library and development headers.
+ .
+ Homepage: http://www.soft-switch.org/
+
+Package: libspandsp-doc
+Section: doc
+Architecture: all
+Description: Documentation for the spandsp signal processing library
+ This package contains the online API in HTML for the libspandsp, a low
+ level signal processing library that modulate and demodulate siignals
+ commonly used in telephony, such as the "noise" generated by a fax
+ modem or DTMF touchpad.

Added: freeswitch/trunk/libs/spandsp/debian/copyright
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/debian/copyright	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,25 @@
+This package was debianized by Simon Richter <sjr at debian.org> on
+Sun,  2 Jan 2005 15:22:58 +0100.
+
+It was downloaded from http://soft-switch.org/downloads/spandsp/
+
+Copyright: Steve Underwood <steveu at coppice.org>
+
+License:
+
+   This package is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; version 2 dated June, 1991.
+
+   This package is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License along with
+   this package; if not, write to the Free Software Foundation, Inc., 
+   51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
+
+
+On Debian systems, the complete text of the GNU General
+Public License can be found in `/usr/share/common-licenses/GPL'.

Added: freeswitch/trunk/libs/spandsp/debian/libspandsp-dev.install
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/debian/libspandsp-dev.install	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,4 @@
+debian/tmp/usr/include
+debian/tmp/usr/lib/libspandsp.so
+debian/tmp/usr/lib/libspandsp.la
+debian/tmp/usr/lib/libspandsp.a

Added: freeswitch/trunk/libs/spandsp/debian/libspandsp-doc.install
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/debian/libspandsp-doc.install	Wed Sep  3 15:02:00 2008
@@ -0,0 +1 @@
+doc/api/html usr/share/doc/libspandsp-doc/api/

Added: freeswitch/trunk/libs/spandsp/debian/libspandsp5.install
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/debian/libspandsp5.install	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,2 @@
+debian/tmp/usr/lib*/libspandsp.so.1.*
+debian/tmp/usr/lib*/libspandsp.so.1

Added: freeswitch/trunk/libs/spandsp/debian/rules
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/debian/rules	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,106 @@
+#!/usr/bin/make -f
+
+# Uncomment this to turn on verbose mode.
+#export DH_VERBOSE=1
+
+DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
+DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
+
+DEBVERSION:=$(shell head -n 1 debian/changelog \
+		    | sed -e 's/^[^(]*(\([^)]*\)).*/\1/')
+ORIGTARVER:=$(shell echo $(DEBVERSION) | sed -e 's/^.*://' -e 's/-[0-9.]*$$//')# -e 's/.dfsg$$//' -e 's/~//')
+
+UPVERSION:=$(shell echo $(ORIGTARVER) | tr -d '~')
+
+FILENAME := spandsp_$(ORIGTARVER).orig.tar.gz
+FULLNAME := spandsp-$(UPVERSION)
+URL := http://soft-switch.org/downloads/spandsp/spandsp-$(UPVERSION).tgz
+
+CFLAGS = -Wall -g
+
+ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
+	CFLAGS += -O0
+else
+	CFLAGS += -O2
+endif
+
+include /usr/share/dpatch/dpatch.make
+
+
+autotools: patch-stamp
+	ln -s /usr/share/misc/config.sub config.sub
+	ln -s /usr/share/misc/config.guess config.guess
+	touch autotools
+
+config.status: autotools configure
+	dh_testdir
+	CFLAGS="$(CFLAGS)" ./configure \
+	  --host=$(DEB_HOST_GNU_TYPE) \
+	  --build=$(DEB_BUILD_GNU_TYPE) \
+	  --prefix=/usr \
+	  --mandir=\$${prefix}/share/man \
+	  --infodir=\$${prefix}/share/info \
+	  --enable-doc
+
+build: build-stamp
+
+build-stamp:  config.status
+	dh_testdir
+	$(MAKE)
+	touch build-stamp
+
+clean: clean-patched unpatch
+clean-patched:
+	dh_testdir
+	dh_testroot
+	rm -f build-stamp autotools
+	-$(MAKE) distclean
+
+	-$(RM) -f config.sub
+	-$(RM) -f config.guess
+
+	dh_clean 
+
+install: build-stamp
+	dh_testdir
+	dh_testroot
+	dh_clean -k 
+	$(MAKE) install DESTDIR=$(CURDIR)/debian/tmp
+
+binary-indep: build-stamp install
+	dh_testdir -i
+	dh_testroot -i
+	dh_installchangelogs -i ChangeLog
+	dh_installdocs -i DueDiligence
+	dh_install -i
+	dh_compress -i
+	dh_fixperms -i
+	dh_installdeb -i
+	dh_gencontrol -i
+	dh_md5sums -i
+	dh_builddeb -i
+
+binary-arch: build-stamp install
+	dh_testdir -a
+	dh_testroot -a
+	dh_installchangelogs -a ChangeLog
+	dh_installdocs -a DueDiligence
+	dh_install -a
+	dh_strip -a
+	dh_compress -a
+	dh_fixperms -a
+	dh_makeshlibs -a
+	dh_installdeb -a
+	dh_shlibdeps -a
+	dh_gencontrol -a
+	dh_md5sums -a
+	dh_builddeb -a
+
+get-orig-source:
+	-@@dh_testdir
+	@@[ -d ../tarballs/. ]||mkdir -p ../tarballs
+	@@echo Downloading $(FILENAME) from $(URL) ...
+	@@wget -N -nv -T10 -t3 -O ../tarballs/$(FILENAME) $(URL)
+
+binary: binary-indep binary-arch
+.PHONY: build clean binary-indep binary-arch binary install patch unpatch

Added: freeswitch/trunk/libs/spandsp/debian/watch
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/debian/watch	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,7 @@
+# See uscan(1) for format
+
+# Compulsory line, this is a version 3 file
+version=3
+
+# <Webpage URL> <string match>
+http://soft-switch.org/downloads/spandsp/ spandsp-(.*)\.tgz debian svn-upgrade

Added: freeswitch/trunk/libs/spandsp/doc/Makefile.am
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/doc/Makefile.am	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,39 @@
+##
+## SpanDSP - a series of DSP components for telephony
+##
+## Makefile.am - Process this file with automake to produce Makefile.in
+##
+## This program is free software; you can redistribute it and/or modify
+## it under the terms of the GNU Lesser General Public License version 2.1,
+## as published by the Free Software Foundation.
+##
+## This program is distributed in the hope that it will be useful,
+## but WITHOUT ANY WARRANTY; without even the implied warranty of
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+## GNU Lesser General Public License for more details.
+##
+## You should have received a copy of the GNU Lesser General Public
+## License along with this program; if not, write to the Free Software
+## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+##
+## $Id: Makefile.am,v 1.11 2008/06/21 10:28:34 steveu Exp $
+
+MAINTAINERCLEANFILES = Makefile.in
+
+EXTRA_DIST = css.css \
+             t38-gateway.dia \
+             t38-terminal.dia \
+             t38_manual.xml \
+             wrapper.xsl \
+             t38_manual/css.css
+
+all: doxydocs t38_manual/index.html
+
+doxydocs:
+	doxygen doxygen
+
+t38_manual/index.html: t38_manual.xml
+	cd t38_manual ; xsltproc ../wrapper.xsl ../t38_manual.xml
+
+clean:
+	cd t38_manual ; rm *.html ; cd ../api ; rm -rf html

Added: freeswitch/trunk/libs/spandsp/doc/css.css
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/doc/css.css	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,564 @@
+body {
+	background-image: url("../images/weave.jpg");
+	font-family: Verdana, Arial, Helvetica, Sans-serif;
+	color: black;
+	margin-right: 20px;
+	margin-left: 20px;
+}
+
+h1 {
+	text-align: center;
+}
+
+h2 {
+	font-family: Verdana, Arial, Helvetica, Sans-serif;
+	border-color: #c00000;
+	color : black;
+	margin-top: 0.8em;
+	border-style: solid;
+	border-width: 0px 0px 3px 0.5em;
+	line-height : 130%;
+}
+
+h3 {
+	font-family: Verdana, Arial, Helvetica, Sans-serif;
+	border-color: #f02020;
+	color : black;
+	border-width: 0px 0px 2px 0.5em;
+	border-style: solid;
+	margin-right: 20%;
+	line-height : 130%;
+}
+caption {
+	font-weight: bold
+}
+a.qindex {}
+a.qindexRef {}
+a.el {
+	text-decoration: none;
+	font-weight: bold
+}
+a.elRef {
+	font-weight: bold
+}
+a.code {
+	text-decoration: none;
+	font-weight: normal;
+	color: #4444ee
+}
+a.codeRef {
+	font-weight: normal;
+	color: #4444ee
+}
+a:hover {
+	text-decoration: none;
+	background-color: #f2f2ff
+}
+dl.el {
+	margin-left: -1cm
+}
+div.fragment {
+	width: 100%;
+	border: none;
+	background-color: #eeeeee
+}
+div.ah {
+	background-color: black;
+	font-weight: bold;
+	color: #ffffff;
+	margin-bottom: 3px;
+	margin-top: 3px
+}
+
+td {
+	font-family: Verdana, Arial, Helvetica, Sans-serif;
+	font-weight: bold;
+}
+
+.navheader {
+	font-family: Verdana, Arial, Helvetica, Sans-serif;
+	background-color: #B2B2ff;
+	font-weight: bold;
+}
+
+.navfooter {
+	font-family: Verdana, Arial, Helvetica, Sans-serif;
+	background-color: #B2B2ff;
+	font-weight: bold;
+}
+
+table.menu {
+	background-color: #000066;
+	font-weight: bold;
+	text-align: center;
+	width: 100%; 
+}
+
+tr.menu {
+	background-color: #ccffff;
+	font-weight: bold;
+	text-align: center;
+}
+td.menu {
+	background-color: #f2e0d0;
+	font-weight: bold;
+	text-align: center;
+}
+
+td.md {
+	background-color: #f2f2ff;
+	font-weight: bold;
+}
+td.mdname1 {
+	background-color: #f2f2ff;
+	font-weight: bold;
+	color: #602020;
+}
+td.mdname {
+	background-color: #f2f2ff;
+	font-weight: bold;
+	color: #602020;
+	width: 600px;
+}
+div.groupHeader {
+	margin-left: 16px;
+	margin-top: 12px;
+	margin-bottom: 6px;
+	font-weight: bold
+}
+div.groupText {
+	margin-left: 16px;
+	font-style: italic;
+	font-size: smaller
+}
+td.indexkey { 
+	font-family: Verdana, Arial, Helvetica, Sans-serif;
+	background-color: #eeeeff; 
+	font-weight: bold; 
+	padding-right  : 10px; 
+	padding-top    : 2px; 
+	padding-left   : 10px; 
+	padding-bottom : 2px; 
+	margin-left    : 0px; 
+	margin-right   : 0px; 
+	margin-top     : 2px; 
+	margin-bottom  : 2px  
+}
+td.indexvalue { 
+	font-family: Verdana, Arial, Helvetica, Sans-serif;
+	background-color: #eeeeff; 
+	font-style: italic; 
+	padding-right  : 10px; 
+	padding-top    : 2px; 
+	padding-left   : 10px; 
+	padding-bottom : 2px; 
+	margin-left    : 0px; 
+	margin-right   : 0px; 
+	margin-top     : 2px; 
+	margin-bottom  : 2px  
+}
+span.keyword {
+	color: #008000
+}
+span.keywordtype {
+	color: #604020
+}
+span.keywordflow {
+	color: #e08000
+}
+span.comment {
+	color: #800000
+}
+span.preprocessor {
+	color: #806020
+}
+span.stringliteral {
+	color: #002080
+}
+span.charliteral {
+	color: #008080
+}
+em {
+	color: #990000;
+	background-color: transparent;
+}
+h1,h2,h3,h4,h5,h6,p,center,td,th,ul,dl,div {
+	font-family: Geneva, Arial, Helvetica, sans-serif;
+}
+body,td {
+	font-size: 90%;
+}
+h1 {
+	text-align: center;
+	font-size: 160%;
+}
+h2 {
+	font-size: 120%;
+}
+h3 {
+	font-size: 100%;
+}
+caption {
+	font-weight: bold
+}
+div.qindex {
+	width: 100%;
+	background-color: #eeeeff;
+	border: 1px solid #b0b0b0;
+	text-align: center;
+	margin: 2px;
+	padding: 2px;
+	line-height: 140%;
+}
+div.nav {
+	width: 100%;
+	background-color: #eeeeff;
+	border: 1px solid #b0b0b0;
+	text-align: center;
+	margin: 2px;
+	padding: 2px;
+	line-height: 140%;
+}
+div.navtab {
+	background-color: #eeeeff;
+	border: 1px solid #b0b0b0;
+	text-align: center;
+	margin: 2px;
+	margin-right: 15px;
+	padding: 2px;
+}
+td.navtab {
+	font-size: 70%;
+}
+a.qindex {
+	text-decoration: none;
+	font-weight: bold;
+	color: #1a419d;
+}
+a.qindex:visited {
+	text-decoration: none;
+	font-weight: bold;
+	color: #1a419d
+}
+a.qindex:hover {
+	text-decoration: none;
+	background-color: #ddddff;
+}
+a.qindexHL {
+	text-decoration: none;
+	font-weight: bold;
+	background-color: #6666cc;
+	color: #ffffff;
+	border: 1px double #9295C2;
+}
+a.qindexHL:hover {
+	text-decoration: none;
+	background-color: #6666cc;
+	color: #ffffff;
+}
+a.qindexHL:visited {
+	text-decoration: none;
+	background-color: #6666cc;
+	color: #ffffff
+}
+a.el {
+	text-decoration: none;
+	font-weight: bold
+}
+a.elRef {
+	font-weight: bold
+}
+a.code:link {
+	text-decoration: none;
+	font-weight: normal;
+	color: #0000FF
+}
+a.code:visited {
+	text-decoration: none;
+	font-weight: normal;
+	color: #0000FF
+}
+a.codeRef:link {
+	font-weight: normal;
+	color: #0000FF
+}
+a.codeRef:visited {
+	font-weight: normal;
+	color: #0000FF
+}
+a:hover {
+	text-decoration: none;
+	background-color: #f2f2ff
+}
+dl.el {
+	margin-left: -1cm
+}
+.fragment {
+	font-family: Fixed, monospace;
+	font-size: 95%;
+}
+pre.fragment {
+	border: 1px solid #CCCCCC;
+	background-color: #f5f5f5;
+	margin-top: 4px;
+	margin-bottom: 4px;
+	margin-left: 2px;
+	margin-right: 8px;
+	padding-left: 6px;
+	padding-right: 6px;
+	padding-top: 4px;
+	padding-bottom: 4px;
+}
+div.ah {
+	background-color: black;
+	font-weight: bold;
+	color: #ffffff;
+	margin-bottom: 3px;
+	margin-top: 3px
+}
+td.md {
+	background-color: #F4F4FB;
+	font-weight: bold;
+}
+td.mdPrefix {
+	background-color: #F4F4FB;
+	color: #606060;
+	font-size: 80%;
+}
+td.mdname1 {
+	background-color: #F4F4FB;
+	font-weight: bold;
+	color: #602020;
+}
+td.mdname {
+	background-color: #F4F4FB;
+	font-weight: bold;
+	color: #602020;
+	width: 600px;
+}
+div.groupHeader {
+	margin-left: 16px;
+	margin-top: 12px;
+	margin-bottom: 6px;
+	font-weight: bold;
+}
+div.groupText {
+	margin-left: 16px;
+	font-style: italic;
+	font-size: 90%
+}
+td.indexkey {
+	background-color: #eeeeff;
+	font-weight: bold;
+	padding-right  : 10px;
+	padding-top    : 2px;
+	padding-left   : 10px;
+	padding-bottom : 2px;
+	margin-left    : 0px;
+	margin-right   : 0px;
+	margin-top     : 2px;
+	margin-bottom  : 2px;
+	border: 1px solid #CCCCCC;
+}
+td.indexvalue {
+	background-color: #eeeeff;
+	font-style: italic;
+	padding-right  : 10px;
+	padding-top    : 2px;
+	padding-left   : 10px;
+	padding-bottom : 2px;
+	margin-left    : 0px;
+	margin-right   : 0px;
+	margin-top     : 2px;
+	margin-bottom  : 2px;
+	border: 1px solid #CCCCCC;
+}
+tr.memlist {
+	background-color: #f0f0f0; 
+}
+p.formulaDsp {
+	text-align: center;
+}
+img.formulaDsp {
+}
+img.formulaInl {
+	vertical-align: middle; 
+}
+span.keyword {
+	color: #008000
+}
+span.keywordtype {
+	color: #604020
+}
+span.keywordflow {
+	color: #e08000
+}
+span.comment {
+	color: #800000
+}
+span.preprocessor  {
+	color: #806020
+}
+span.stringliteral {
+	color: #002080
+}
+span.charliteral {
+	color: #008080
+}
+.mdTable {
+	border: 1px solid #868686;
+	background-color: #F4F4FB;
+}
+.mdRow {
+	padding: 8px 10px;
+}
+.mdescLeft {
+	padding: 0px 8px 4px 8px;
+	font-size: 80%;
+	font-style: italic;
+	background-color: #FAFAFA;
+	border-top: 1px none #E0E0E0;
+	border-right: 1px none #E0E0E0;
+	border-bottom: 1px none #E0E0E0;
+	border-left: 1px none #E0E0E0;
+	margin: 0px;
+}
+.mdescRight {
+	padding: 0px 8px 4px 8px;
+	font-size: 80%;
+	font-style: italic;
+	background-color: #FAFAFA;
+	border-top: 1px none #E0E0E0;
+	border-right: 1px none #E0E0E0;
+	border-bottom: 1px none #E0E0E0;
+	border-left: 1px none #E0E0E0;
+	margin: 0px;
+}
+.memItemLeft {
+	padding: 1px 0px 0px 8px;
+	margin: 4px;
+	border-top-width: 1px;
+	border-right-width: 1px;
+	border-bottom-width: 1px;
+	border-left-width: 1px;
+	border-top-color: #E0E0E0;
+	border-right-color: #E0E0E0;
+	border-bottom-color: #E0E0E0;
+	border-left-color: #E0E0E0;
+	border-top-style: solid;
+	border-right-style: none;
+	border-bottom-style: none;
+	border-left-style: none;
+	background-color: #FAFAFA;
+	font-size: 80%;
+}
+.memItemRight {
+	padding: 1px 8px 0px 8px;
+	margin: 4px;
+	border-top-width: 1px;
+	border-right-width: 1px;
+	border-bottom-width: 1px;
+	border-left-width: 1px;
+	border-top-color: #E0E0E0;
+	border-right-color: #E0E0E0;
+	border-bottom-color: #E0E0E0;
+	border-left-color: #E0E0E0;
+	border-top-style: solid;
+	border-right-style: none;
+	border-bottom-style: none;
+	border-left-style: none;
+	background-color: #FAFAFA;
+	font-size: 80%;
+}
+.memTemplItemLeft {
+	padding: 1px 0px 0px 8px;
+	margin: 4px;
+	border-top-width: 1px;
+	border-right-width: 1px;
+	border-bottom-width: 1px;
+	border-left-width: 1px;
+	border-top-color: #E0E0E0;
+	border-right-color: #E0E0E0;
+	border-bottom-color: #E0E0E0;
+	border-left-color: #E0E0E0;
+	border-top-style: none;
+	border-right-style: none;
+	border-bottom-style: none;
+	border-left-style: none;
+	background-color: #FAFAFA;
+	font-size: 80%;
+}
+.memTemplItemRight {
+	padding: 1px 8px 0px 8px;
+	margin: 4px;
+	border-top-width: 1px;
+	border-right-width: 1px;
+	border-bottom-width: 1px;
+	border-left-width: 1px;
+	border-top-color: #E0E0E0;
+	border-right-color: #E0E0E0;
+	border-bottom-color: #E0E0E0;
+	border-left-color: #E0E0E0;
+	border-top-style: none;
+	border-right-style: none;
+	border-bottom-style: none;
+	border-left-style: none;
+	background-color: #FAFAFA;
+	font-size: 80%;
+}
+.memTemplParams {
+	padding: 1px 0px 0px 8px;
+	margin: 4px;
+	border-top-width: 1px;
+	border-right-width: 1px;
+	border-bottom-width: 1px;
+	border-left-width: 1px;
+	border-top-color: #E0E0E0;
+	border-right-color: #E0E0E0;
+	border-bottom-color: #E0E0E0;
+	border-left-color: #E0E0E0;
+	border-top-style: solid;
+	border-right-style: none;
+	border-bottom-style: none;
+	border-left-style: none;
+	color: #606060;
+	background-color: #FAFAFA;
+	font-size: 80%;
+}
+.search {
+	color: #003399;
+	font-weight: bold;
+}
+form.search {
+	margin-bottom: 0px;
+	margin-top: 0px;
+}
+input.search {
+	font-size: 75%;
+	color: #000080;
+	font-weight: normal;
+	background-color: #eeeeff;
+}
+td.tiny {
+	font-size: 75%;
+}
+a {
+	color: #252e78;
+}
+a:visited {
+	color: #3d2185;
+}
+.dirtab {
+	padding: 4px;
+	border-collapse: collapse;
+	border: 1px solid #b0b0b0;
+}
+th.dirtab {
+	background: #eeeeff;
+	font-weight: bold;
+}
+hr {
+	height: 1px;
+	border: none;
+	border-top: 1px solid black;
+}

Added: freeswitch/trunk/libs/spandsp/doc/doxygen.in
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/doc/doxygen.in	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,1229 @@
+# Doxyfile 1.4.4
+
+# This file describes the settings to be used by the documentation system
+# doxygen (www.doxygen.org) for a project
+#
+# All text after a hash (#) is considered a comment and will be ignored
+# The format is:
+#       TAG = value [value, ...]
+# For lists items can also be appended using:
+#       TAG += value [value, ...]
+# Values that contain spaces should be placed between quotes (" ")
+
+#---------------------------------------------------------------------------
+# Project related configuration options
+#---------------------------------------------------------------------------
+
+# The PROJECT_NAME tag is a single word (or a sequence of words surrounded 
+# by quotes) that should identify the project.
+
+PROJECT_NAME           = @PACKAGE@
+
+# The PROJECT_NUMBER tag can be used to enter a project or revision number. 
+# This could be handy for archiving the generated documentation or 
+# if some version control system is used.
+
+PROJECT_NUMBER         = @VERSION@
+
+# The OUTPUT_DIRECTORY tag is used to specify the (relative or absolute) 
+# base path where the generated documentation will be put. 
+# If a relative path is entered, it will be relative to the location 
+# where doxygen was started. If left blank the current directory will be used.
+
+OUTPUT_DIRECTORY       = api
+
+# If the CREATE_SUBDIRS tag is set to YES, then doxygen will create 
+# 4096 sub-directories (in 2 levels) under the output directory of each output 
+# format and will distribute the generated files over these directories. 
+# Enabling this option can be useful when feeding doxygen a huge amount of 
+# source files, where putting all generated files in the same directory would 
+# otherwise cause performance problems for the file system.
+
+CREATE_SUBDIRS         = NO
+
+# The OUTPUT_LANGUAGE tag is used to specify the language in which all 
+# documentation generated by doxygen is written. Doxygen will use this 
+# information to generate all constant output in the proper language. 
+# The default language is English, other supported languages are: 
+# Brazilian, Catalan, Chinese, Chinese-Traditional, Croatian, Czech, Danish, 
+# Dutch, Finnish, French, German, Greek, Hungarian, Italian, Japanese, 
+# Japanese-en (Japanese with English messages), Korean, Korean-en, Norwegian, 
+# Polish, Portuguese, Romanian, Russian, Serbian, Slovak, Slovene, Spanish, 
+# Swedish, and Ukrainian.
+
+OUTPUT_LANGUAGE        = English
+
+# This tag can be used to specify the encoding used in the generated output. 
+# The encoding is not always determined by the language that is chosen, 
+# but also whether or not the output is meant for Windows or non-Windows users. 
+# In case there is a difference, setting the USE_WINDOWS_ENCODING tag to YES 
+# forces the Windows encoding (this is the default for the Windows binary), 
+# whereas setting the tag to NO uses a Unix-style encoding (the default for 
+# all platforms other than Windows).
+
+USE_WINDOWS_ENCODING   = YES
+
+# If the BRIEF_MEMBER_DESC tag is set to YES (the default) Doxygen will 
+# include brief member descriptions after the members that are listed in 
+# the file and class documentation (similar to JavaDoc). 
+# Set to NO to disable this.
+
+BRIEF_MEMBER_DESC      = YES
+
+# If the REPEAT_BRIEF tag is set to YES (the default) Doxygen will prepend 
+# the brief description of a member or function before the detailed description. 
+# Note: if both HIDE_UNDOC_MEMBERS and BRIEF_MEMBER_DESC are set to NO, the 
+# brief descriptions will be completely suppressed.
+
+REPEAT_BRIEF           = YES
+
+# This tag implements a quasi-intelligent brief description abbreviator 
+# that is used to form the text in various listings. Each string 
+# in this list, if found as the leading text of the brief description, will be 
+# stripped from the text and the result after processing the whole list, is 
+# used as the annotated text. Otherwise, the brief description is used as-is. 
+# If left blank, the following values are used ("$name" is automatically 
+# replaced with the name of the entity): "The $name class" "The $name widget" 
+# "The $name file" "is" "provides" "specifies" "contains" 
+# "represents" "a" "an" "the"
+
+ABBREVIATE_BRIEF       = 
+
+# If the ALWAYS_DETAILED_SEC and REPEAT_BRIEF tags are both set to YES then 
+# Doxygen will generate a detailed section even if there is only a brief 
+# description.
+
+ALWAYS_DETAILED_SEC    = NO
+
+# If the INLINE_INHERITED_MEMB tag is set to YES, doxygen will show all 
+# inherited members of a class in the documentation of that class as if those 
+# members were ordinary class members. Constructors, destructors and assignment 
+# operators of the base classes will not be shown.
+
+INLINE_INHERITED_MEMB  = NO
+
+# If the FULL_PATH_NAMES tag is set to YES then Doxygen will prepend the full 
+# path before files name in the file list and in the header files. If set 
+# to NO the shortest path that makes the file name unique will be used.
+
+FULL_PATH_NAMES        = NO
+
+# If the FULL_PATH_NAMES tag is set to YES then the STRIP_FROM_PATH tag 
+# can be used to strip a user-defined part of the path. Stripping is 
+# only done if one of the specified strings matches the left-hand part of 
+# the path. The tag can be used to show relative paths in the file list. 
+# If left blank the directory from which doxygen is run is used as the 
+# path to strip.
+
+STRIP_FROM_PATH        = 
+
+# The STRIP_FROM_INC_PATH tag can be used to strip a user-defined part of 
+# the path mentioned in the documentation of a class, which tells 
+# the reader which header file to include in order to use a class. 
+# If left blank only the name of the header file containing the class 
+# definition is used. Otherwise one should specify the include paths that 
+# are normally passed to the compiler using the -I flag.
+
+STRIP_FROM_INC_PATH    = 
+
+# If the SHORT_NAMES tag is set to YES, doxygen will generate much shorter 
+# (but less readable) file names. This can be useful is your file systems 
+# doesn't support long names like on DOS, Mac, or CD-ROM.
+
+SHORT_NAMES            = NO
+
+# If the JAVADOC_AUTOBRIEF tag is set to YES then Doxygen 
+# will interpret the first line (until the first dot) of a JavaDoc-style 
+# comment as the brief description. If set to NO, the JavaDoc 
+# comments will behave just like the Qt-style comments (thus requiring an 
+# explicit @brief command for a brief description.
+
+JAVADOC_AUTOBRIEF      = NO
+
+# The MULTILINE_CPP_IS_BRIEF tag can be set to YES to make Doxygen 
+# treat a multi-line C++ special comment block (i.e. a block of //! or /// 
+# comments) as a brief description. This used to be the default behaviour. 
+# The new default is to treat a multi-line C++ comment block as a detailed 
+# description. Set this tag to YES if you prefer the old behaviour instead.
+
+MULTILINE_CPP_IS_BRIEF = NO
+
+# If the DETAILS_AT_TOP tag is set to YES then Doxygen 
+# will output the detailed description near the top, like JavaDoc.
+# If set to NO, the detailed description appears after the member 
+# documentation.
+
+DETAILS_AT_TOP         = NO
+
+# If the INHERIT_DOCS tag is set to YES (the default) then an undocumented 
+# member inherits the documentation from any documented member that it 
+# re-implements.
+
+INHERIT_DOCS           = YES
+
+# If member grouping is used in the documentation and the DISTRIBUTE_GROUP_DOC 
+# tag is set to YES, then doxygen will reuse the documentation of the first 
+# member in the group (if any) for the other members of the group. By default 
+# all members of a group must be documented explicitly.
+
+DISTRIBUTE_GROUP_DOC   = NO
+
+# If the SEPARATE_MEMBER_PAGES tag is set to YES, then doxygen will produce 
+# a new page for each member. If set to NO, the documentation of a member will 
+# be part of the file/class/namespace that contains it.
+
+SEPARATE_MEMBER_PAGES  = NO
+
+# The TAB_SIZE tag can be used to set the number of spaces in a tab. 
+# Doxygen uses this value to replace tabs by spaces in code fragments.
+
+TAB_SIZE               = 8
+
+# This tag can be used to specify a number of aliases that acts 
+# as commands in the documentation. An alias has the form "name=value". 
+# For example adding "sideeffect=\par Side Effects:\n" will allow you to 
+# put the command \sideeffect (or @sideeffect) in the documentation, which 
+# will result in a user-defined paragraph with heading "Side Effects:". 
+# You can put \n's in the value part of an alias to insert newlines.
+
+ALIASES                = 
+
+# Set the OPTIMIZE_OUTPUT_FOR_C tag to YES if your project consists of C 
+# sources only. Doxygen will then generate output that is more tailored for C. 
+# For instance, some of the names that are used will be different. The list 
+# of all members will be omitted, etc.
+
+OPTIMIZE_OUTPUT_FOR_C  = NO
+
+# Set the OPTIMIZE_OUTPUT_JAVA tag to YES if your project consists of Java sources 
+# only. Doxygen will then generate output that is more tailored for Java. 
+# For instance, namespaces will be presented as packages, qualified scopes 
+# will look different, etc.
+
+OPTIMIZE_OUTPUT_JAVA   = NO
+
+# Set the SUBGROUPING tag to YES (the default) to allow class member groups of 
+# the same type (for instance a group of public functions) to be put as a 
+# subgroup of that type (e.g. under the Public Functions section). Set it to 
+# NO to prevent subgrouping. Alternatively, this can be done per class using 
+# the \nosubgrouping command.
+
+SUBGROUPING            = YES
+
+#---------------------------------------------------------------------------
+# Build related configuration options
+#---------------------------------------------------------------------------
+
+# If the EXTRACT_ALL tag is set to YES doxygen will assume all entities in 
+# documentation are documented, even if no documentation was available. 
+# Private class members and static file members will be hidden unless 
+# the EXTRACT_PRIVATE and EXTRACT_STATIC tags are set to YES
+
+EXTRACT_ALL            = NO
+
+# If the EXTRACT_PRIVATE tag is set to YES all private members of a class 
+# will be included in the documentation.
+
+EXTRACT_PRIVATE        = NO
+
+# If the EXTRACT_STATIC tag is set to YES all static members of a file 
+# will be included in the documentation.
+
+EXTRACT_STATIC         = NO
+
+# If the EXTRACT_LOCAL_CLASSES tag is set to YES classes (and structs) 
+# defined locally in source files will be included in the documentation. 
+# If set to NO only classes defined in header files are included.
+
+EXTRACT_LOCAL_CLASSES  = YES
+
+# This flag is only useful for Objective-C code. When set to YES local 
+# methods, which are defined in the implementation section but not in 
+# the interface are included in the documentation. 
+# If set to NO (the default) only methods in the interface are included.
+
+EXTRACT_LOCAL_METHODS  = NO
+
+# If the HIDE_UNDOC_MEMBERS tag is set to YES, Doxygen will hide all 
+# undocumented members of documented classes, files or namespaces. 
+# If set to NO (the default) these members will be included in the 
+# various overviews, but no documentation section is generated. 
+# This option has no effect if EXTRACT_ALL is enabled.
+
+HIDE_UNDOC_MEMBERS     = NO
+
+# If the HIDE_UNDOC_CLASSES tag is set to YES, Doxygen will hide all 
+# undocumented classes that are normally visible in the class hierarchy. 
+# If set to NO (the default) these classes will be included in the various 
+# overviews. This option has no effect if EXTRACT_ALL is enabled.
+
+HIDE_UNDOC_CLASSES     = NO
+
+# If the HIDE_FRIEND_COMPOUNDS tag is set to YES, Doxygen will hide all 
+# friend (class|struct|union) declarations. 
+# If set to NO (the default) these declarations will be included in the 
+# documentation.
+
+HIDE_FRIEND_COMPOUNDS  = NO
+
+# If the HIDE_IN_BODY_DOCS tag is set to YES, Doxygen will hide any 
+# documentation blocks found inside the body of a function. 
+# If set to NO (the default) these blocks will be appended to the 
+# function's detailed documentation block.
+
+HIDE_IN_BODY_DOCS      = NO
+
+# The INTERNAL_DOCS tag determines if documentation 
+# that is typed after a \internal command is included. If the tag is set 
+# to NO (the default) then the documentation will be excluded. 
+# Set it to YES to include the internal documentation.
+
+INTERNAL_DOCS          = NO
+
+# If the CASE_SENSE_NAMES tag is set to NO then Doxygen will only generate 
+# file names in lower-case letters. If set to YES upper-case letters are also 
+# allowed. This is useful if you have classes or files whose names only differ 
+# in case and if your file system supports case sensitive file names. Windows 
+# and Mac users are advised to set this option to NO.
+
+CASE_SENSE_NAMES       = YES
+
+# If the HIDE_SCOPE_NAMES tag is set to NO (the default) then Doxygen 
+# will show members with their full class and namespace scopes in the 
+# documentation. If set to YES the scope will be hidden.
+
+HIDE_SCOPE_NAMES       = NO
+
+# If the SHOW_INCLUDE_FILES tag is set to YES (the default) then Doxygen 
+# will put a list of the files that are included by a file in the documentation 
+# of that file.
+
+SHOW_INCLUDE_FILES     = YES
+
+# If the INLINE_INFO tag is set to YES (the default) then a tag [inline] 
+# is inserted in the documentation for inline members.
+
+INLINE_INFO            = YES
+
+# If the SORT_MEMBER_DOCS tag is set to YES (the default) then doxygen 
+# will sort the (detailed) documentation of file and class members 
+# alphabetically by member name. If set to NO the members will appear in 
+# declaration order.
+
+SORT_MEMBER_DOCS       = YES
+
+# If the SORT_BRIEF_DOCS tag is set to YES then doxygen will sort the 
+# brief documentation of file, namespace and class members alphabetically 
+# by member name. If set to NO (the default) the members will appear in 
+# declaration order.
+
+SORT_BRIEF_DOCS        = NO
+
+# If the SORT_BY_SCOPE_NAME tag is set to YES, the class list will be 
+# sorted by fully-qualified names, including namespaces. If set to 
+# NO (the default), the class list will be sorted only by class name, 
+# not including the namespace part. 
+# Note: This option is not very useful if HIDE_SCOPE_NAMES is set to YES.
+# Note: This option applies only to the class list, not to the 
+# alphabetical list.
+
+SORT_BY_SCOPE_NAME     = NO
+
+# The GENERATE_TODOLIST tag can be used to enable (YES) or 
+# disable (NO) the todo list. This list is created by putting \todo 
+# commands in the documentation.
+
+GENERATE_TODOLIST      = YES
+
+# The GENERATE_TESTLIST tag can be used to enable (YES) or 
+# disable (NO) the test list. This list is created by putting \test 
+# commands in the documentation.
+
+GENERATE_TESTLIST      = YES
+
+# The GENERATE_BUGLIST tag can be used to enable (YES) or 
+# disable (NO) the bug list. This list is created by putting \bug 
+# commands in the documentation.
+
+GENERATE_BUGLIST       = YES
+
+# The GENERATE_DEPRECATEDLIST tag can be used to enable (YES) or 
+# disable (NO) the deprecated list. This list is created by putting 
+# \deprecated commands in the documentation.
+
+GENERATE_DEPRECATEDLIST= YES
+
+# The ENABLED_SECTIONS tag can be used to enable conditional 
+# documentation sections, marked by \if sectionname ... \endif.
+
+ENABLED_SECTIONS       = 
+
+# The MAX_INITIALIZER_LINES tag determines the maximum number of lines 
+# the initial value of a variable or define consists of for it to appear in 
+# the documentation. If the initializer consists of more lines than specified 
+# here it will be hidden. Use a value of 0 to hide initializers completely. 
+# The appearance of the initializer of individual variables and defines in the 
+# documentation can be controlled using \showinitializer or \hideinitializer 
+# command in the documentation regardless of this setting.
+
+MAX_INITIALIZER_LINES  = 30
+
+# Set the SHOW_USED_FILES tag to NO to disable the list of files generated 
+# at the bottom of the documentation of classes and structs. If set to YES the 
+# list will mention the files that were used to generate the documentation.
+
+SHOW_USED_FILES        = YES
+
+# If the sources in your project are distributed over multiple directories 
+# then setting the SHOW_DIRECTORIES tag to YES will show the directory hierarchy 
+# in the documentation. The default is YES.
+
+SHOW_DIRECTORIES       = YES
+
+# The FILE_VERSION_FILTER tag can be used to specify a program or script that 
+# doxygen should invoke to get the current version for each file (typically from the 
+# version control system). Doxygen will invoke the program by executing (via 
+# popen()) the command <command> <input-file>, where <command> is the value of 
+# the FILE_VERSION_FILTER tag, and <input-file> is the name of an input file 
+# provided by doxygen. Whatever the progam writes to standard output 
+# is used as the file version. See the manual for examples.
+
+FILE_VERSION_FILTER    = 
+
+#---------------------------------------------------------------------------
+# configuration options related to warning and progress messages
+#---------------------------------------------------------------------------
+
+# The QUIET tag can be used to turn on/off the messages that are generated 
+# by doxygen. Possible values are YES and NO. If left blank NO is used.
+
+QUIET                  = NO
+
+# The WARNINGS tag can be used to turn on/off the warning messages that are 
+# generated by doxygen. Possible values are YES and NO. If left blank 
+# NO is used.
+
+WARNINGS               = YES
+
+# If WARN_IF_UNDOCUMENTED is set to YES, then doxygen will generate warnings 
+# for undocumented members. If EXTRACT_ALL is set to YES then this flag will 
+# automatically be disabled.
+
+WARN_IF_UNDOCUMENTED   = YES
+
+# If WARN_IF_DOC_ERROR is set to YES, doxygen will generate warnings for 
+# potential errors in the documentation, such as not documenting some 
+# parameters in a documented function, or documenting parameters that 
+# don't exist or using markup commands wrongly.
+
+WARN_IF_DOC_ERROR      = YES
+
+# This WARN_NO_PARAMDOC option can be abled to get warnings for 
+# functions that are documented, but have no documentation for their parameters 
+# or return value. If set to NO (the default) doxygen will only warn about 
+# wrong or incomplete parameter documentation, but not about the absence of 
+# documentation.
+
+WARN_NO_PARAMDOC       = NO
+
+# The WARN_FORMAT tag determines the format of the warning messages that 
+# doxygen can produce. The string should contain the $file, $line, and $text 
+# tags, which will be replaced by the file and line number from which the 
+# warning originated and the warning text. Optionally the format may contain 
+# $version, which will be replaced by the version of the file (if it could 
+# be obtained via FILE_VERSION_FILTER)
+
+WARN_FORMAT            = 
+
+# The WARN_LOGFILE tag can be used to specify a file to which warning 
+# and error messages should be written. If left blank the output is written 
+# to stderr.
+
+WARN_LOGFILE           = 
+
+#---------------------------------------------------------------------------
+# configuration options related to the input files
+#---------------------------------------------------------------------------
+
+# The INPUT tag can be used to specify the files and/or directories that contain 
+# documented source files. You may enter file names like "myfile.cpp" or 
+# directories like "/usr/src/myproject". Separate the files or directories 
+# with spaces.
+
+INPUT                  = ..
+
+# If the value of the INPUT tag contains directories, you can use the 
+# FILE_PATTERNS tag to specify one or more wildcard pattern (like *.cpp 
+# and *.h) to filter out the source-files in the directories. If left 
+# blank the following patterns are tested: 
+# *.c *.cc *.cxx *.cpp *.c++ *.java *.ii *.ixx *.ipp *.i++ *.inl *.h *.hh *.hxx 
+# *.hpp *.h++ *.idl *.odl *.cs *.php *.php3 *.inc *.m *.mm
+
+FILE_PATTERNS          = *.c \
+                         *.h
+
+# The RECURSIVE tag can be used to turn specify whether or not subdirectories 
+# should be searched for input files as well. Possible values are YES and NO. 
+# If left blank NO is used.
+
+RECURSIVE              = YES
+
+# The EXCLUDE tag can be used to specify files and/or directories that should 
+# excluded from the INPUT source files. This way you can easily exclude a 
+# subdirectory from a directory tree whose root is specified with the INPUT tag.
+
+EXCLUDE                = 
+
+# The EXCLUDE_SYMLINKS tag can be used select whether or not files or 
+# directories that are symbolic links (a Unix filesystem feature) are excluded 
+# from the input.
+
+EXCLUDE_SYMLINKS       = NO
+
+# If the value of the INPUT tag contains directories, you can use the 
+# EXCLUDE_PATTERNS tag to specify one or more wildcard patterns to exclude 
+# certain files from those directories. Note that the wildcards are matched 
+# against the file with absolute path, so to exclude all test directories 
+# for example use the pattern */test/*
+
+EXCLUDE_PATTERNS       = 
+
+# The EXAMPLE_PATH tag can be used to specify one or more files or 
+# directories that contain example code fragments that are included (see 
+# the \include command).
+
+EXAMPLE_PATH           = 
+
+# If the value of the EXAMPLE_PATH tag contains directories, you can use the 
+# EXAMPLE_PATTERNS tag to specify one or more wildcard pattern (like *.cpp 
+# and *.h) to filter out the source-files in the directories. If left 
+# blank all files are included.
+
+EXAMPLE_PATTERNS       = 
+
+# If the EXAMPLE_RECURSIVE tag is set to YES then subdirectories will be 
+# searched for input files to be used with the \include or \dontinclude 
+# commands irrespective of the value of the RECURSIVE tag. 
+# Possible values are YES and NO. If left blank NO is used.
+
+EXAMPLE_RECURSIVE      = NO
+
+# The IMAGE_PATH tag can be used to specify one or more files or 
+# directories that contain image that are included in the documentation (see 
+# the \image command).
+
+IMAGE_PATH             = 
+
+# The INPUT_FILTER tag can be used to specify a program that doxygen should 
+# invoke to filter for each input file. Doxygen will invoke the filter program 
+# by executing (via popen()) the command <filter> <input-file>, where <filter> 
+# is the value of the INPUT_FILTER tag, and <input-file> is the name of an 
+# input file. Doxygen will then use the output that the filter program writes 
+# to standard output.  If FILTER_PATTERNS is specified, this tag will be 
+# ignored.
+
+INPUT_FILTER           = 
+
+# The FILTER_PATTERNS tag can be used to specify filters on a per file pattern 
+# basis.  Doxygen will compare the file name with each pattern and apply the 
+# filter if there is a match.  The filters are a list of the form: 
+# pattern=filter (like *.cpp=my_cpp_filter). See INPUT_FILTER for further 
+# info on how filters are used. If FILTER_PATTERNS is empty, INPUT_FILTER 
+# is applied to all files.
+
+FILTER_PATTERNS        = 
+
+# If the FILTER_SOURCE_FILES tag is set to YES, the input filter (if set using 
+# INPUT_FILTER) will be used to filter the input files when producing source 
+# files to browse (i.e. when SOURCE_BROWSER is set to YES).
+
+FILTER_SOURCE_FILES    = NO
+
+#---------------------------------------------------------------------------
+# configuration options related to source browsing
+#---------------------------------------------------------------------------
+
+# If the SOURCE_BROWSER tag is set to YES then a list of source files will 
+# be generated. Documented entities will be cross-referenced with these sources. 
+# Note: To get rid of all source code in the generated output, make sure also 
+# VERBATIM_HEADERS is set to NO.
+
+SOURCE_BROWSER         = NO
+
+# Setting the INLINE_SOURCES tag to YES will include the body 
+# of functions and classes directly in the documentation.
+
+INLINE_SOURCES         = NO
+
+# Setting the STRIP_CODE_COMMENTS tag to YES (the default) will instruct 
+# doxygen to hide any special comment blocks from generated source code 
+# fragments. Normal C and C++ comments will always remain visible.
+
+STRIP_CODE_COMMENTS    = NO
+
+# If the REFERENCED_BY_RELATION tag is set to YES (the default) 
+# then for each documented function all documented 
+# functions referencing it will be listed.
+
+REFERENCED_BY_RELATION = YES
+
+# If the REFERENCES_RELATION tag is set to YES (the default) 
+# then for each documented function all documented entities 
+# called/used by that function will be listed.
+
+REFERENCES_RELATION    = YES
+
+# If the USE_HTAGS tag is set to YES then the references to source code 
+# will point to the HTML generated by the htags(1) tool instead of doxygen 
+# built-in source browser. The htags tool is part of GNU's global source 
+# tagging system (see http://www.gnu.org/software/global/global.html). You 
+# will need version 4.8.6 or higher.
+
+USE_HTAGS              = NO
+
+# If the VERBATIM_HEADERS tag is set to YES (the default) then Doxygen 
+# will generate a verbatim copy of the header file for each class for 
+# which an include is specified. Set to NO to disable this.
+
+VERBATIM_HEADERS       = YES
+
+#---------------------------------------------------------------------------
+# configuration options related to the alphabetical class index
+#---------------------------------------------------------------------------
+
+# If the ALPHABETICAL_INDEX tag is set to YES, an alphabetical index 
+# of all compounds will be generated. Enable this if the project 
+# contains a lot of classes, structs, unions or interfaces.
+
+ALPHABETICAL_INDEX     = YES
+
+# If the alphabetical index is enabled (see ALPHABETICAL_INDEX) then 
+# the COLS_IN_ALPHA_INDEX tag can be used to specify the number of columns 
+# in which this list will be split (can be a number in the range [1..20])
+
+COLS_IN_ALPHA_INDEX    = 2
+
+# In case all classes in a project start with a common prefix, all 
+# classes will be put under the same header in the alphabetical index. 
+# The IGNORE_PREFIX tag can be used to specify one or more prefixes that 
+# should be ignored while generating the index headers.
+
+IGNORE_PREFIX          = 
+
+#---------------------------------------------------------------------------
+# configuration options related to the HTML output
+#---------------------------------------------------------------------------
+
+# If the GENERATE_HTML tag is set to YES (the default) Doxygen will 
+# generate HTML output.
+
+GENERATE_HTML          = YES
+
+# The HTML_OUTPUT tag is used to specify where the HTML docs will be put. 
+# If a relative path is entered the value of OUTPUT_DIRECTORY will be 
+# put in front of it. If left blank `html' will be used as the default path.
+
+HTML_OUTPUT            = 
+
+# The HTML_FILE_EXTENSION tag can be used to specify the file extension for 
+# each generated HTML page (for example: .htm,.php,.asp). If it is left blank 
+# doxygen will generate files with .html extension.
+
+HTML_FILE_EXTENSION    = .html
+
+# The HTML_HEADER tag can be used to specify a personal HTML header for 
+# each generated HTML page. If it is left blank doxygen will generate a 
+# standard header.
+
+HTML_HEADER            = 
+
+# The HTML_FOOTER tag can be used to specify a personal HTML footer for 
+# each generated HTML page. If it is left blank doxygen will generate a 
+# standard footer.
+
+HTML_FOOTER            = 
+
+# The HTML_STYLESHEET tag can be used to specify a user-defined cascading 
+# style sheet that is used by each HTML page. It can be used to 
+# fine-tune the look of the HTML output. If the tag is left blank doxygen 
+# will generate a default style sheet. Note that doxygen will try to copy 
+# the style sheet file to the HTML output directory, so don't put your own 
+# stylesheet in the HTML output directory as well, or it will be erased!
+
+HTML_STYLESHEET        = css.css
+
+# If the HTML_ALIGN_MEMBERS tag is set to YES, the members of classes, 
+# files or namespaces will be aligned in HTML using tables. If set to 
+# NO a bullet list will be used.
+
+HTML_ALIGN_MEMBERS     = YES
+
+# If the GENERATE_HTMLHELP tag is set to YES, additional index files 
+# will be generated that can be used as input for tools like the 
+# Microsoft HTML help workshop to generate a compressed HTML help file (.chm) 
+# of the generated HTML documentation.
+
+GENERATE_HTMLHELP      = NO
+
+# If the GENERATE_HTMLHELP tag is set to YES, the CHM_FILE tag can 
+# be used to specify the file name of the resulting .chm file. You 
+# can add a path in front of the file if the result should not be 
+# written to the html output directory.
+
+CHM_FILE               = 
+
+# If the GENERATE_HTMLHELP tag is set to YES, the HHC_LOCATION tag can 
+# be used to specify the location (absolute path including file name) of 
+# the HTML help compiler (hhc.exe). If non-empty doxygen will try to run 
+# the HTML help compiler on the generated index.hhp.
+
+HHC_LOCATION           = 
+
+# If the GENERATE_HTMLHELP tag is set to YES, the GENERATE_CHI flag 
+# controls if a separate .chi index file is generated (YES) or that 
+# it should be included in the master .chm file (NO).
+
+GENERATE_CHI           = NO
+
+# If the GENERATE_HTMLHELP tag is set to YES, the BINARY_TOC flag 
+# controls whether a binary table of contents is generated (YES) or a 
+# normal table of contents (NO) in the .chm file.
+
+BINARY_TOC             = NO
+
+# The TOC_EXPAND flag can be set to YES to add extra items for group members 
+# to the contents of the HTML help documentation and to the tree view.
+
+TOC_EXPAND             = NO
+
+# The DISABLE_INDEX tag can be used to turn on/off the condensed index at 
+# top of each HTML page. The value NO (the default) enables the index and 
+# the value YES disables it.
+
+DISABLE_INDEX          = NO
+
+# This tag can be used to set the number of enum values (range [1..20]) 
+# that doxygen will group on one line in the generated HTML documentation.
+
+ENUM_VALUES_PER_LINE   = 4
+
+# If the GENERATE_TREEVIEW tag is set to YES, a side panel will be
+# generated containing a tree-like index structure (just like the one that 
+# is generated for HTML Help). For this to work a browser that supports 
+# JavaScript, DHTML, CSS and frames is required (for instance Mozilla 1.0+, 
+# Netscape 6.0+, Internet explorer 5.0+, or Konqueror). Windows users are 
+# probably better off using the HTML help feature.
+
+GENERATE_TREEVIEW      = YES
+
+# If the treeview is enabled (see GENERATE_TREEVIEW) then this tag can be 
+# used to set the initial width (in pixels) of the frame in which the tree 
+# is shown.
+
+TREEVIEW_WIDTH         = 250
+
+#---------------------------------------------------------------------------
+# configuration options related to the LaTeX output
+#---------------------------------------------------------------------------
+
+# If the GENERATE_LATEX tag is set to YES (the default) Doxygen will 
+# generate Latex output.
+
+GENERATE_LATEX         = NO
+
+# The LATEX_OUTPUT tag is used to specify where the LaTeX docs will be put. 
+# If a relative path is entered the value of OUTPUT_DIRECTORY will be 
+# put in front of it. If left blank `latex' will be used as the default path.
+
+LATEX_OUTPUT           = 
+
+# The LATEX_CMD_NAME tag can be used to specify the LaTeX command name to be 
+# invoked. If left blank `latex' will be used as the default command name.
+
+LATEX_CMD_NAME         = latex
+
+# The MAKEINDEX_CMD_NAME tag can be used to specify the command name to 
+# generate index for LaTeX. If left blank `makeindex' will be used as the 
+# default command name.
+
+MAKEINDEX_CMD_NAME     = makeindex
+
+# If the COMPACT_LATEX tag is set to YES Doxygen generates more compact 
+# LaTeX documents. This may be useful for small projects and may help to 
+# save some trees in general.
+
+COMPACT_LATEX          = NO
+
+# The PAPER_TYPE tag can be used to set the paper type that is used 
+# by the printer. Possible values are: a4, a4wide, letter, legal and 
+# executive. If left blank a4wide will be used.
+
+PAPER_TYPE             = a4wide
+
+# The EXTRA_PACKAGES tag can be to specify one or more names of LaTeX 
+# packages that should be included in the LaTeX output.
+
+EXTRA_PACKAGES         = 
+
+# The LATEX_HEADER tag can be used to specify a personal LaTeX header for 
+# the generated latex document. The header should contain everything until 
+# the first chapter. If it is left blank doxygen will generate a 
+# standard header. Notice: only use this tag if you know what you are doing!
+
+LATEX_HEADER           = 
+
+# If the PDF_HYPERLINKS tag is set to YES, the LaTeX that is generated 
+# is prepared for conversion to pdf (using ps2pdf). The pdf file will 
+# contain links (just like the HTML output) instead of page references 
+# This makes the output suitable for online browsing using a pdf viewer.
+
+PDF_HYPERLINKS         = NO
+
+# If the USE_PDFLATEX tag is set to YES, pdflatex will be used instead of 
+# plain latex in the generated Makefile. Set this option to YES to get a 
+# higher quality PDF documentation.
+
+USE_PDFLATEX           = NO
+
+# If the LATEX_BATCHMODE tag is set to YES, doxygen will add the \\batchmode. 
+# command to the generated LaTeX files. This will instruct LaTeX to keep 
+# running if errors occur, instead of asking the user for help. 
+# This option is also used when generating formulas in HTML.
+
+LATEX_BATCHMODE        = NO
+
+# If LATEX_HIDE_INDICES is set to YES then doxygen will not 
+# include the index chapters (such as File Index, Compound Index, etc.) 
+# in the output.
+
+LATEX_HIDE_INDICES     = NO
+
+#---------------------------------------------------------------------------
+# configuration options related to the RTF output
+#---------------------------------------------------------------------------
+
+# If the GENERATE_RTF tag is set to YES Doxygen will generate RTF output 
+# The RTF output is optimized for Word 97 and may not look very pretty with 
+# other RTF readers or editors.
+
+GENERATE_RTF           = NO
+
+# The RTF_OUTPUT tag is used to specify where the RTF docs will be put. 
+# If a relative path is entered the value of OUTPUT_DIRECTORY will be 
+# put in front of it. If left blank `rtf' will be used as the default path.
+
+RTF_OUTPUT             = 
+
+# If the COMPACT_RTF tag is set to YES Doxygen generates more compact 
+# RTF documents. This may be useful for small projects and may help to 
+# save some trees in general.
+
+COMPACT_RTF            = NO
+
+# If the RTF_HYPERLINKS tag is set to YES, the RTF that is generated 
+# will contain hyperlink fields. The RTF file will 
+# contain links (just like the HTML output) instead of page references. 
+# This makes the output suitable for online browsing using WORD or other 
+# programs which support those fields. 
+# Note: wordpad (write) and others do not support links.
+
+RTF_HYPERLINKS         = NO
+
+# Load stylesheet definitions from file. Syntax is similar to doxygen's 
+# config file, i.e. a series of assignments. You only have to provide 
+# replacements, missing definitions are set to their default value.
+
+RTF_STYLESHEET_FILE    = 
+
+# Set optional variables used in the generation of an rtf document. 
+# Syntax is similar to doxygen's config file.
+
+RTF_EXTENSIONS_FILE    = 
+
+#---------------------------------------------------------------------------
+# configuration options related to the man page output
+#---------------------------------------------------------------------------
+
+# If the GENERATE_MAN tag is set to YES (the default) Doxygen will 
+# generate man pages
+
+GENERATE_MAN           = NO
+
+# The MAN_OUTPUT tag is used to specify where the man pages will be put. 
+# If a relative path is entered the value of OUTPUT_DIRECTORY will be 
+# put in front of it. If left blank `man' will be used as the default path.
+
+MAN_OUTPUT             = 
+
+# The MAN_EXTENSION tag determines the extension that is added to 
+# the generated man pages (default is the subroutine's section .3)
+
+MAN_EXTENSION          = 
+
+# If the MAN_LINKS tag is set to YES and Doxygen generates man output, 
+# then it will generate one additional man file for each entity 
+# documented in the real man page(s). These additional files 
+# only source the real man page, but without them the man command 
+# would be unable to find the correct page. The default is NO.
+
+MAN_LINKS              = NO
+
+#---------------------------------------------------------------------------
+# configuration options related to the XML output
+#---------------------------------------------------------------------------
+
+# If the GENERATE_XML tag is set to YES Doxygen will 
+# generate an XML file that captures the structure of 
+# the code including all documentation.
+
+GENERATE_XML           = NO
+
+# The XML_OUTPUT tag is used to specify where the XML pages will be put. 
+# If a relative path is entered the value of OUTPUT_DIRECTORY will be 
+# put in front of it. If left blank `xml' will be used as the default path.
+
+XML_OUTPUT             = xml
+
+# The XML_SCHEMA tag can be used to specify an XML schema, 
+# which can be used by a validating XML parser to check the 
+# syntax of the XML files.
+
+XML_SCHEMA             = 
+
+# The XML_DTD tag can be used to specify an XML DTD, 
+# which can be used by a validating XML parser to check the 
+# syntax of the XML files.
+
+XML_DTD                = 
+
+# If the XML_PROGRAMLISTING tag is set to YES Doxygen will 
+# dump the program listings (including syntax highlighting 
+# and cross-referencing information) to the XML output. Note that 
+# enabling this will significantly increase the size of the XML output.
+
+XML_PROGRAMLISTING     = YES
+
+#---------------------------------------------------------------------------
+# configuration options for the AutoGen Definitions output
+#---------------------------------------------------------------------------
+
+# If the GENERATE_AUTOGEN_DEF tag is set to YES Doxygen will 
+# generate an AutoGen Definitions (see autogen.sf.net) file 
+# that captures the structure of the code including all 
+# documentation. Note that this feature is still experimental 
+# and incomplete at the moment.
+
+GENERATE_AUTOGEN_DEF   = NO
+
+#---------------------------------------------------------------------------
+# configuration options related to the Perl module output
+#---------------------------------------------------------------------------
+
+# If the GENERATE_PERLMOD tag is set to YES Doxygen will 
+# generate a Perl module file that captures the structure of 
+# the code including all documentation. Note that this 
+# feature is still experimental and incomplete at the 
+# moment.
+
+GENERATE_PERLMOD       = NO
+
+# If the PERLMOD_LATEX tag is set to YES Doxygen will generate 
+# the necessary Makefile rules, Perl scripts and LaTeX code to be able 
+# to generate PDF and DVI output from the Perl module output.
+
+PERLMOD_LATEX          = NO
+
+# If the PERLMOD_PRETTY tag is set to YES the Perl module output will be 
+# nicely formatted so it can be parsed by a human reader.  This is useful 
+# if you want to understand what is going on.  On the other hand, if this 
+# tag is set to NO the size of the Perl module output will be much smaller 
+# and Perl will parse it just the same.
+
+PERLMOD_PRETTY         = YES
+
+# The names of the make variables in the generated doxyrules.make file 
+# are prefixed with the string contained in PERLMOD_MAKEVAR_PREFIX. 
+# This is useful so different doxyrules.make files included by the same 
+# Makefile don't overwrite each other's variables.
+
+PERLMOD_MAKEVAR_PREFIX = 
+
+#---------------------------------------------------------------------------
+# Configuration options related to the preprocessor   
+#---------------------------------------------------------------------------
+
+# If the ENABLE_PREPROCESSING tag is set to YES (the default) Doxygen will 
+# evaluate all C-preprocessor directives found in the sources and include 
+# files.
+
+ENABLE_PREPROCESSING   = YES
+
+# If the MACRO_EXPANSION tag is set to YES Doxygen will expand all macro 
+# names in the source code. If set to NO (the default) only conditional 
+# compilation will be performed. Macro expansion can be done in a controlled 
+# way by setting EXPAND_ONLY_PREDEF to YES.
+
+MACRO_EXPANSION        = NO
+
+# If the EXPAND_ONLY_PREDEF and MACRO_EXPANSION tags are both set to YES 
+# then the macro expansion is limited to the macros specified with the 
+# PREDEFINED and EXPAND_AS_PREDEFINED tags.
+
+EXPAND_ONLY_PREDEF     = NO
+
+# If the SEARCH_INCLUDES tag is set to YES (the default) the includes files 
+# in the INCLUDE_PATH (see below) will be search if a #include is found.
+
+SEARCH_INCLUDES        = YES
+
+# The INCLUDE_PATH tag can be used to specify one or more directories that 
+# contain include files that are not input files but should be processed by 
+# the preprocessor.
+
+INCLUDE_PATH           = 
+
+# You can use the INCLUDE_FILE_PATTERNS tag to specify one or more wildcard 
+# patterns (like *.h and *.hpp) to filter out the header-files in the 
+# directories. If left blank, the patterns specified with FILE_PATTERNS will 
+# be used.
+
+INCLUDE_FILE_PATTERNS  = 
+
+# The PREDEFINED tag can be used to specify one or more macro names that 
+# are defined before the preprocessor is started (similar to the -D option of 
+# gcc). The argument of the tag is a list of macros of the form: name 
+# or name=definition (no spaces). If the definition and the = are 
+# omitted =1 is assumed. To prevent a macro definition from being 
+# undefined via #undef or recursively expanded use the := operator 
+# instead of the = operator.
+
+PREDEFINED             = 
+
+# If the MACRO_EXPANSION and EXPAND_ONLY_PREDEF tags are set to YES then 
+# this tag can be used to specify a list of macro names that should be expanded. 
+# The macro definition that is found in the sources will be used. 
+# Use the PREDEFINED tag if you want to use a different macro definition.
+
+EXPAND_AS_DEFINED      = 
+
+# If the SKIP_FUNCTION_MACROS tag is set to YES (the default) then 
+# doxygen's preprocessor will remove all function-like macros that are alone 
+# on a line, have an all uppercase name, and do not end with a semicolon. Such 
+# function macros are typically used for boiler-plate code, and will confuse 
+# the parser if not removed.
+
+SKIP_FUNCTION_MACROS   = YES
+
+#---------------------------------------------------------------------------
+# Configuration::additions related to external references   
+#---------------------------------------------------------------------------
+
+# The TAGFILES option can be used to specify one or more tagfiles. 
+# Optionally an initial location of the external documentation 
+# can be added for each tagfile. The format of a tag file without 
+# this location is as follows: 
+#   TAGFILES = file1 file2 ... 
+# Adding location for the tag files is done as follows: 
+#   TAGFILES = file1=loc1 "file2 = loc2" ... 
+# where "loc1" and "loc2" can be relative or absolute paths or 
+# URLs. If a location is present for each tag, the installdox tool 
+# does not have to be run to correct the links.
+# Note that each tag file must have a unique name
+# (where the name does NOT include the path)
+# If a tag file is not located in the directory in which doxygen 
+# is run, you must also specify the path to the tagfile here.
+
+TAGFILES               = 
+
+# When a file name is specified after GENERATE_TAGFILE, doxygen will create 
+# a tag file that is based on the input files it reads.
+
+GENERATE_TAGFILE       = 
+
+# If the ALLEXTERNALS tag is set to YES all external classes will be listed 
+# in the class index. If set to NO only the inherited external classes 
+# will be listed.
+
+ALLEXTERNALS           = NO
+
+# If the EXTERNAL_GROUPS tag is set to YES all external groups will be listed 
+# in the modules index. If set to NO, only the current project's groups will 
+# be listed.
+
+EXTERNAL_GROUPS        = YES
+
+# The PERL_PATH should be the absolute path and name of the perl script 
+# interpreter (i.e. the result of `which perl').
+
+PERL_PATH              = /usr/bin/perl
+
+#---------------------------------------------------------------------------
+# Configuration options related to the dot tool   
+#---------------------------------------------------------------------------
+
+# If the CLASS_DIAGRAMS tag is set to YES (the default) Doxygen will 
+# generate a inheritance diagram (in HTML, RTF and LaTeX) for classes with base 
+# or super classes. Setting the tag to NO turns the diagrams off. Note that 
+# this option is superseded by the HAVE_DOT option below. This is only a 
+# fallback. It is recommended to install and use dot, since it yields more 
+# powerful graphs.
+
+CLASS_DIAGRAMS         = YES
+
+# If set to YES, the inheritance and collaboration graphs will hide 
+# inheritance and usage relations if the target is undocumented 
+# or is not a class.
+
+HIDE_UNDOC_RELATIONS   = YES
+
+# If you set the HAVE_DOT tag to YES then doxygen will assume the dot tool is 
+# available from the path. This tool is part of Graphviz, a graph visualization 
+# toolkit from AT&T and Lucent Bell Labs. The other options in this section 
+# have no effect if this option is set to NO (the default)
+
+HAVE_DOT               = NO
+
+# If the CLASS_GRAPH and HAVE_DOT tags are set to YES then doxygen 
+# will generate a graph for each documented class showing the direct and 
+# indirect inheritance relations. Setting this tag to YES will force the 
+# the CLASS_DIAGRAMS tag to NO.
+
+CLASS_GRAPH            = YES
+
+# If the COLLABORATION_GRAPH and HAVE_DOT tags are set to YES then doxygen 
+# will generate a graph for each documented class showing the direct and 
+# indirect implementation dependencies (inheritance, containment, and 
+# class references variables) of the class with other documented classes.
+
+COLLABORATION_GRAPH    = YES
+
+# If the GROUP_GRAPHS and HAVE_DOT tags are set to YES then doxygen 
+# will generate a graph for groups, showing the direct groups dependencies
+
+GROUP_GRAPHS           = YES
+
+# If the UML_LOOK tag is set to YES doxygen will generate inheritance and 
+# collaboration diagrams in a style similar to the OMG's Unified Modeling 
+# Language.
+
+UML_LOOK               = NO
+
+# If set to YES, the inheritance and collaboration graphs will show the 
+# relations between templates and their instances.
+
+TEMPLATE_RELATIONS     = YES
+
+# If the ENABLE_PREPROCESSING, SEARCH_INCLUDES, INCLUDE_GRAPH, and HAVE_DOT 
+# tags are set to YES then doxygen will generate a graph for each documented 
+# file showing the direct and indirect include dependencies of the file with 
+# other documented files.
+
+INCLUDE_GRAPH          = YES
+
+# If the ENABLE_PREPROCESSING, SEARCH_INCLUDES, INCLUDED_BY_GRAPH, and 
+# HAVE_DOT tags are set to YES then doxygen will generate a graph for each 
+# documented header file showing the documented files that directly or 
+# indirectly include this file.
+
+INCLUDED_BY_GRAPH      = YES
+
+# If the CALL_GRAPH and HAVE_DOT tags are set to YES then doxygen will 
+# generate a call dependency graph for every global function or class method. 
+# Note that enabling this option will significantly increase the time of a run. 
+# So in most cases it will be better to enable call graphs for selected 
+# functions only using the \callgraph command.
+
+CALL_GRAPH             = NO
+
+# If the GRAPHICAL_HIERARCHY and HAVE_DOT tags are set to YES then doxygen 
+# will graphical hierarchy of all classes instead of a textual one.
+
+GRAPHICAL_HIERARCHY    = YES
+
+# If the DIRECTORY_GRAPH, SHOW_DIRECTORIES and HAVE_DOT tags are set to YES 
+# then doxygen will show the dependencies a directory has on other directories 
+# in a graphical way. The dependency relations are determined by the #include
+# relations between the files in the directories.
+
+DIRECTORY_GRAPH        = YES
+
+# The DOT_IMAGE_FORMAT tag can be used to set the image format of the images 
+# generated by dot. Possible values are png, jpg, or gif
+# If left blank png will be used.
+
+DOT_IMAGE_FORMAT       = png
+
+# The tag DOT_PATH can be used to specify the path where the dot tool can be 
+# found. If left blank, it is assumed the dot tool can be found in the path.
+
+DOT_PATH               = 
+
+# The DOTFILE_DIRS tag can be used to specify one or more directories that 
+# contain dot files that are included in the documentation (see the 
+# \dotfile command).
+
+DOTFILE_DIRS           = 
+
+# The MAX_DOT_GRAPH_WIDTH tag can be used to set the maximum allowed width 
+# (in pixels) of the graphs generated by dot. If a graph becomes larger than 
+# this value, doxygen will try to truncate the graph, so that it fits within 
+# the specified constraint. Beware that most browsers cannot cope with very 
+# large images.
+
+MAX_DOT_GRAPH_WIDTH    = 1024
+
+# The MAX_DOT_GRAPH_HEIGHT tag can be used to set the maximum allows height 
+# (in pixels) of the graphs generated by dot. If a graph becomes larger than 
+# this value, doxygen will try to truncate the graph, so that it fits within 
+# the specified constraint. Beware that most browsers cannot cope with very 
+# large images.
+
+MAX_DOT_GRAPH_HEIGHT   = 1024
+
+# The MAX_DOT_GRAPH_DEPTH tag can be used to set the maximum depth of the 
+# graphs generated by dot. A depth value of 3 means that only nodes reachable 
+# from the root by following a path via at most 3 edges will be shown. Nodes 
+# that lay further from the root node will be omitted. Note that setting this 
+# option to 1 or 2 may greatly reduce the computation time needed for large 
+# code bases. Also note that a graph may be further truncated if the graph's 
+# image dimensions are not sufficient to fit the graph (see MAX_DOT_GRAPH_WIDTH 
+# and MAX_DOT_GRAPH_HEIGHT). If 0 is used for the depth value (the default), 
+# the graph is not depth-constrained.
+
+MAX_DOT_GRAPH_DEPTH    = 0
+
+# Set the DOT_TRANSPARENT tag to YES to generate images with a transparent 
+# background. This is disabled by default, which results in a white background. 
+# Warning: Depending on the platform used, enabling this option may lead to 
+# badly anti-aliased labels on the edges of a graph (i.e. they become hard to 
+# read).
+
+DOT_TRANSPARENT        = NO
+
+# Set the DOT_MULTI_TARGETS tag to YES allow dot to generate multiple output 
+# files in one run (i.e. multiple -o and -T options on the command line). This 
+# makes dot run faster, but since only newer versions of dot (>1.8.10) 
+# support this, this feature is disabled by default.
+
+DOT_MULTI_TARGETS      = NO
+
+# If the GENERATE_LEGEND tag is set to YES (the default) Doxygen will 
+# generate a legend page explaining the meaning of the various boxes and 
+# arrows in the dot generated graphs.
+
+GENERATE_LEGEND        = YES
+
+# If the DOT_CLEANUP tag is set to YES (the default) Doxygen will 
+# remove the intermediate dot files that are used to generate 
+# the various graphs.
+
+DOT_CLEANUP            = YES
+
+#---------------------------------------------------------------------------
+# Configuration::additions related to the search engine   
+#---------------------------------------------------------------------------
+
+# The SEARCHENGINE tag specifies whether or not a search engine should be 
+# used. If set to NO the values of all tags below this one will be ignored.
+
+SEARCHENGINE           = NO

Added: freeswitch/trunk/libs/spandsp/doc/t38-gateway.dia
==============================================================================
Binary file. No diff available.

Added: freeswitch/trunk/libs/spandsp/doc/t38-terminal.dia
==============================================================================
Binary file. No diff available.

Added: freeswitch/trunk/libs/spandsp/doc/t38_manual.xml
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/doc/t38_manual.xml	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,291 @@
+<?xml version="1.0" encoding="iso8859-1"?>
+<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.3//EN"
+               "http://www.oasis-open.org/docbook/xml/4.3/docbookx.dtd">
+<!-- $Id: t38_manual.xml,v 1.5 2007/12/08 16:25:17 steveu Exp $ -->
+<book>
+  <bookinfo>
+    <date>2007-11-14</date>
+    <title>T.38 protocol (FoIP) support</title>
+    <subtitle>User manual</subtitle>
+    <releaseinfo>documentation in progress</releaseinfo>
+    <authorgroup>
+      <author>
+        <firstname>Steve</firstname>
+        <surname>Underwood</surname>
+      </author>
+    </authorgroup>
+    <address>
+      <email>steveu at coppice.org</email>
+    </address>
+    <copyright>
+      <year>2007</year>
+      <holder>Steve Underwood</holder>
+    </copyright>
+    <legalnotice>
+      <para>
+        This document can be freely redistributed according to the
+        terms of the GNU General Public License.
+      </para>
+    </legalnotice>
+  </bookinfo>
+
+  <toc></toc>
+
+<chapter id="what-is-t38">
+<title>The T.38 real-time FAX over IP (FoIP) protocol</title>
+
+<para>There are two ITU recommendations which address sending FAXes over IP networks. T.37 specifies a method of encapsulating FAX images in e-mails, and transporting them to the recipient (an e-mail box, or another FAX machine) in a store-and-forward manner. T.38 specifies a protocol for transmitting a FAX across an IP network in real time. This document discusses the implementation of the T.38, real time, FAX over IP (FoIP) protocol.</para>
+
+<para>FAXing between two PSTN FAX machines provides the illusion of real-time communication, with confirmed delivery. This is, of course, no more than an illusion. The sender has no idea what is receiving the FAX. It might be a FAX mailbox, where the FAX might only be retrieved much later, or might never be retrieved at all. It might be a FAX machine which is out of paper, and is storing the FAX in memory. If nobody adds paper before the FAX machine is switched off, the FAX might never be printed. With increasing amounts of FAX spam, many people don't ever bother to collect FAXes from the FAX machine in their office. Still, with all these reasons why the real-time confirmed delivery nature of FAXing is an illusion, large numbers of people still insist on using direct FAXing.</para>
+
+<para>The FAX protocols - in particular T.30 - were designed for the PSTN. The PSTN is very different from a packet network like the Internet. It offers very strict timing; latency is rock steady throughout a call; and latency is seldom very high. The lack of these features in packet networks tends to spoil the quality of voice over IP, compared to voice over the PSTN. However, it can totally destroy modem data, like that used for FAX. Jitter and packet loss can cause modem reception to fail, and excessive delays can cause timers designed for a low latency environment to expire. T.38 tries to mitigate these factors, and can greatly improve the reliability of FAXing across the internet. It can also send FAXes using less bandwidth than using VoIP protocols. There are limits to what can be achieved on a congested network, though, and T.38 can never offer the reliability of a store and forward protocol, like T.37. Sadly, the average office worker's love affair with real-time FAX ensures a bright future for the T.38 protocol, even though the T.37 protocol makes far more sense.</para>
+
+<para>The T.38 protocol primarily operates between:
+<itemizedlist>
+<listitem><para>Internet-aware FAX terminals, which connect directly to an IP network.</para></listitem>
+<listitem><para>FAX gateways, which allow traditional PSTN FAX terminals to communicate via the Internet.</para></listitem>
+<listitem><para>A combination of these.</para></listitem>
+</itemizedlist>
+T.38 is the only standardised protocol which exists for real-time FoIP. Reliably transporting a FAX between PSTN FAX terminals, through an IP network, requires use of the T.38 protocol at FAX gateways. VoIP connections are not robust for modem use, including FAX modem use. Most use low bit rate codecs, which cannot convey the modem signals accurately. Even when high bit rate codecs, such as G.711, are used, VoIP connections suffer dropouts and timing adjustments, which modems cannot tolerate. In a LAN environment the dropout rate may be very low, but the timing adjustments which occur in VoIP connections still make modem operation unreliable. T.38 FAX gateways deal with the delays, timing jitter, and packet loss experienced in packet networks, and isolate the PSTN FAX terminals from these as far as possible. In addition, by sending FAXes as image data, rather than digitised audio, the required bandwidth of the IP network might be reduced. However, the redundant transmission needed to make T.38 work acceptably over an unreliable network tends to offset much of the potential bandwidth gain.</para>
+
+<para>The original T.38 specification provides for operation up to 14,400bps, using a V.17 modem. The latest version of the T.38 specification adds features for FAXing at up to 33,600bps, using a V.34 modem. However, it appears most current T.38 implementations only support operation up to 14,400bps.</para>
+</chapter>
+
+<chapter id="the-basics-of-a-t38-entity">
+<title>The basics of a T.38 entity</title>
+
+<para>The T.38 protocol may be used to build an Internet-aware FAX terminal, for direct connection to the Internet. It may also be used to build gateways. A T.38 FAX gateway might be a gateway between the PSTN and the Internet. It might just be an a ATA box acting as a gateway between a directly connected traditional FAX machine and the Internet. The T.38 protocol merely defines what passes between two T.38 entities. Creating a robust entity, able to tolerate the widest possible variation in network delays, jitter and packet loss, requires considerably more than just implementing what is contained in the T.38 spec. Also, the protocol definition is somewhat loose, resulting is considerable variability in the way the protocol is implemented. Considerable flexibility is required in a T.38 entity's design, to tolerate these variations.</para>
+
+<para>T.38 currently works over one of the following transports:
+<itemizedlist>
+<listitem><para>TCP, with TPKT message framing - the use of TPKT was originally specified in a vague way, and implementations without TPKT framing apparently exist.</para></listitem>
+<listitem><para>UDPTL - A UDP based protocol used nowhere else. This is the most common transport for T.38.</para></listitem>
+<listitem><para>RTP - Added quite late to the T.38 specification, and still not widely supported.</para></listitem>
+</itemizedlist>
+TCP is the ideal way to communicate between two entities on an IP network, which do not have real-time constraints. The entities can talk as fast as they and the medium between them permit, with full error control. Internet-aware FAX devices would, therefore, usually use TCP as the transport for T.38. Gateways have only limited control of the timing of the FAX data passing through them. They have to operate in real-time, at a rate outside their control. Gateways, therefore, usually use UDPTL. The RTP option was only added to the T.38 specification in 2004, and is not yet widely supported. Over time it may become the preferred replacement for UDPTL, since most entities handle more than just FAX, and need to implement RTP anyway.</para>
+
+<para>A TCP stream is fully corrected by the TCP protocol itself. However, in the UDPTL or RTP modes of operation, T.38 is subject to possible packet loss. A high level of missing data would defeat the protocol, so some measure of FEC (forward error correction) must be used. The UDPTL specification defines two optional forms of FEC. Both consist of adding information from previous packets to new packets. In one form this repeated data is send as a direct copy of the original. In the other it is sent as parity information, which requires encoding and decoding. The specifications for RTP include definitions of suitable FEC and redundancy protocols (RFC2198 and RFC2733). The T.38 specification says these should be used for T.38 over RTP.</para>
+
+<para>Interestingly, even the latest revision of the T.38 specification does not provide properly for security in FAX transmission. SRTP is a standard way to achieve secure multi-media communication, and can be applied to T.38 over RTP without any specific wording on the subject in the T.38 specification. UDPTL might be seen as obsolete in the long term, and not worthy of enhancements to encrypt the data. However, no secure option for T.38 over TCP is defined. TPKT could be sent over TLS/TCP. TLS also has message framing features which would allow IFP packets to be sent directly over a TLS protected connection. However, there is no specified way to do this.</para>
+
+<para>Although redundant information in future packets is an important part of a solid T.38 implementation, it is not a complete solution to problem of lost packets. Sometimes the next packet occurs after a considerable delay (e.g. when allowing time for a fast modem to train). If a "start training" message is only received through the redundant information in a following packet, it usually arrives too late to be useful (at least for a gateway). Most T.38 implementations now follow a practice of sending several exact copies of key packets - generally the ones which start or end the stages of the T.30 protocol. Typically up to 4 identical copies of these packets are sent down the wire. The may be sent in a burst, as fast as possible, or they may be spaced in time by 10ms or 20ms. IP network congestion, and the resulting packet loss, can be very bursty. If all copies are sent together, they might all be lost. Even a small amount of spreading in time may significantly increase the likelihood of at least one copy reaching its destination. The price is some delay in delivery of the message, which might be problematic. Multiple copies of these packets add little to the overall volume of data transmitted, as only a small percentage of packets fall in this "key packet" category. Some T.38 implementations follow a less effective practice of sending multiple key packets, which have separate sequence numbers and are separately bundled with redundant information for transmission. They may also be spaced in time. Although this seems a less effective strategy, a T.38 entity must expect to receive packets streams of this kind, and tolerate them.</para>
+
+<para>Between the high priority key packets, and the low priority packets for the image data (the loss of which might just cause a streak on an image, or be corrected by ECM FAXing), lies the V.21 HDLC control messages. Some T.38 implementations dynamically adjust the amount of redundant information in packets, so these control messages are repeated through several packets, but the large image data packets are repeated less, or not at all. Used with care, this dynamic redundancy strategy can nicely balance data volume and reliability.</para>
+
+<para>A T.38 terminal has a lot more flexibility in how it can deal with problem data than a T.38 gateway. The terminal has no tight timing constraints governing how it behaves with regard to received data. It can wait a considerable period for delayed packets to arrive. The gateway is a man-in-the-middle, and must live with the timing constraints that imposes. This means a T.38 gateway has a rather more difficult design than a terminal.</para>
+</chapter>
+
+<chapter id="the-core-elements-of-a-t38-implementation">
+<title>The core elements of a T.38 implementation</title>
+
+<para>There are many differences between the behaviour of a T.38 terminal and a T.38 gateway. However, some functions are common to both types of T.38 entity, particular in the IP network interface.</para>
+
+<sect1>
+<title>The T.38 Internet Fascimile Protocol (IFP) packetiser</title>
+
+<para>The T.38 specification defines an ASN.1 schema for the messages which pass between T.38 entities. These messages are called IFP (Internet Fascimile Protocol) messages. Their format is independent of the transport used to carry them. However, there are currently two slightly different versions of the ASN.1 schema. This is due to a typo in the original version of the T.38 specification. The protocol negotiation which occurs just before T.38 communication resolves which version will be used. Although the typo was corrected several years ago, it is still very more common to find implementations which only support the original buggy version.</para>
+</sect1>
+
+<sect1>
+<title>The UDPTL, RTP, TPKT packetiser</title>
+
+<para>A second level packetiser bundles the IFP packets for transmission. This functions in different ways, depending on the type of transport in use. For the unreliable transports (UDPTL and RTP) the packetiser adds forward error correction (FEC) information to the packets. This can greatly inprove the reliability of the T.38 protocol, at the expense of higher bandwidth. The amount of error correction information added to the packets is implementation dependant.</para>
+
+<para>For UDPTL, two forms of FEC are defined. One simply repeats older T.38 packets as FEC information in the current UDPTL packet. The other generates parity packets over a series of T.38 packets, and includes that parity information in the current UDPTL packet. The type of FEC to be used is negotiated  just before T.38 communication begins.</para>
+
+<para>The RTP specifications (RFC3550, RFC3551, RFC2198, RFC2733) define common redundancy and FEC formats, to be used for any payload. Where T.38 packets are carried by RTP, the standard RTP FEC mechanisms are used.</para>
+
+<para>TPKT (RFC1006, RFC2126) encapsulation works over a TCP transport. TCP provides full error correction, though retries may slow it considerably. TPKT encapsulation merely provides the ability to delineate the start and end of the IFP packets in the structureless TCP stream.</para>
+</sect1>
+
+</chapter>
+
+<chapter id="the-elements-of-a-t38-terminal">
+<title>The elements of a T.38 terminal</title>
+
+<graphic fileref="t38-terminal.png"/>
+
+</chapter>
+
+<chapter id="the-elements-of-a-t38-gateway">
+<title>The elements of a T.38 gateway</title>
+
+<graphic fileref="t38-gateway.png"/>
+
+<sect1>
+<title>The HDLC decoder</title>
+<para>If the HDLC decoder in a T.38 gateway worked in whole frames, it would introduce unacceptable latency. Instead, the HDLC decoder must work progressively through the HDLC frames, outputing its results octet by octet. The T.38 message stream does not contain HDLC preamble, though it usually contains an indication that preamble is in progress. It does not contain the CRC octets, but simply an indication of whether the CRC is good or bad. The HDLC decoder provides an indication when preamble is being received, and checks the CRC octets at the end of frames to provide the good or bad indication. The decoder makes the frames available, octet by octet, to the T.38 engine for status tracking and possible modification. Tracking and modification imposes a few octets delay, but the goal is to keep this to the minimum possible. Whether the frames are modified or unmodified, good or bad they are always passed on, to maintain the appropriate timing flow for the T.30 protocol.</para>
+
+<para>An interesting aspect of the timing flow of V.21 HDLC messages on the T.38 path relates to the size of the CRC. The usual practice for sending these frames is to send one octet in each message, with the messages spaced by the duration of one octet. They will normally be sent with the minimum possible delay. The CRC at the end of each frame is two octets long, and only an indication of good or bad is sent in the T.38 messages. It takes two octet times to know if the CRC is OK or not, typically causing the flow of T.38 messages to stutter by the duration of an octet. Since flow control within a frame is not possible when frames are replayed by a remote T.38 gateway, that gateway must allow for this inevitable stutter as it prepares to begin playout of the frame.</para>
+</sect1>
+
+<sect1>
+<title>T.30 message analysis and manipulation</title>
+<para>The T.30 message analyser in a T.38 gateway performs the following functions:
+<itemizedlist>
+<listitem><para>It passes NSS, NSF and NSC frames, but modifies their contents. A receiving FAX machine must be prevented from acting upon these messages in a manufacturer specific manner. The T.38 protocol is incapable of handling such manufacturer specific things. However, simply dropping these packets would upset the timing of the T.30 protocol. Instead, most T.38 implementations modify the country and manufacturer codes nesr the start of these messages, so the messages will be ignored by any receiver. They are typically set to 0x00, 0x55, 0xAA or 0xFF. Some implementations set them to the country and manufacturer code of the T.38 implementor. As long as they are set to something the receiver will not recognise, problems are avoided. To avoid removing useful information, the original country and manufacturer code bytes might be reinserted in a different part of the modified NSF message, provided space permits.</para></listitem>
+<listitem><para>It tracks the contents of the DCS messages. During flow control of non-ECM data, the minimum row length must be preserved, so the current minimum must be found from the DCS messages. Similarly, the current encoding must be known to apply non-ECM flow control correctly. ECM data must be flow controlled in a completely different way than non-ECM data, so ECM mode information must be found from the DCS messages.</para></listitem>
+<listitem><para>It may optionally modify the minimum row length in the DIS messages. Because flow control will be applied to non-ECM data, it can make sense to tell the sending FAX machine it has no need to impose a minimum row length, and let the emitting T.38 entity impose the minimum as part of its flow control operation.</para></listitem>
+<listitem><para>It passes on the stripped and modified HDLC frames, with the minimum possible delay. This implies that is must analyse and modify the messages octet by octet.</para></listitem>
+</itemizedlist>
+</para>
+</sect1>
+
+<sect1>
+<title>The HDLC rate adapting encoder</title>
+<para>The HDLC data rate adapting encoder accepts a T.30 message stream as input. The V.21 HDLC messages generally arriving byte by byte, in separate T.38 messages. ECM HDLC messages usually arrive in larger chunks, but they are still generally fragmented. The rate adapter dynamically buffers the T.30 data, generates preamble, and adjusts its length if the incoming message data is falling behind. It may insert additional flag octets between frames, as a flow control mechanism. However, the HDLC protocol is synchronous, so the adapter cannot perform any flow control within a frame. It must, therefore, buffer enough octets of a frame to provide reasonable jitter tolerance, before it emits the first octet of that frame. If the arriving messages fall too far behind mid-frame, there will be corruption of the outgoing stream. In the case of ECM image data frames, the frames are fast enough, that buffering whole frames is quite proactical within the T.30 timing constraints. This ensures mid-frame underflow can never occur.</para>
+</sect1>
+
+<sect1>
+<title>TCF (training confirmation) and non-ECM image data rate adaption</title>
+<para>Non-ECM image data rate adaption deals with exchanging TCF data, and non-ECM image data using T.4 1-D or 2-D compression. Non-ECM image data is typically padded with zero bits at the end of each pixel row. This is used to avoid rows arriving at the receiving FAX machine faster than the mechanical paper handling can process them. It is also used by sending FAX machines for flow control, when their paper handling falls behind. The minimum duration of a row is negotiated between the FAX machines, before image transfer commences.</para>
+
+<para>TCF data is a continuous stream of zero bits. It may, optionally, be preceeded by a continuous block of one bits. So, when transferring TCF data between the T.38 entities, flow control is simply a matter of repeating the last received bit when the late arrival of TCF data packets causes underflow at an emitting gateway.</para>
+
+<para>T.38 entities may negotiate to generate and check TCF data locally, and not exchange it. Generally, this is only used when TCP/TPKT is used as the transport for the T.38 messages. A TCF checker and a TCF generator are needed by a T.38 gateway supporting this option.</para>
+
+<para>Some T.38 entities are capable of negotiating the removal of end of row padding bits in the data passing between the T.38 entities. This may reduce the amount of data significantly, for some types of image. If negotiated, most or all surplus zero bits are stripped from the image bit stream, at the sending T.38 entity. If a T.38 gateway is receiving such stripped data, it may need to reimpose a minimum row time, by inserting zeros, as part of its non-ECM rate adaption process. By checking the contents of the DCS messages, it can determine what this minimum should be.</para>
+
+<para>Only two types of image compression are used for non-ECM transmission - T.4 1-D and 2-D. The end of line (EOL) marker for both these is 11 zero bits, followed by a one bit. In 2-D mode, an additional one or zero bit follows, which defines the mode of the next row. It turns out that the maximum number of valid consecutive zeros which could preceed the 11 zeros of an EOL is 3. In a valid image there are no longer runs of zeros than at an EOL, and the maximum there is 14. Therefore, if the padding stripper reduces all runs of zeros longer than 14 to just 14, it will strip most of the padding with very simple logic. Simple logic is a good thing in this case. It not only avoids the need to fully analyse the image data, it also minimises problems when there are bit errors in the image data stream.</para>
+
+<para>For image data sent without error correction (T.4 1-D or 2-D) there may be underflow at the outgoing gateway, if image data packets arrive too late. To deal with slow mechanical paper handling in FAX machines, the T.30 specification includes an end of pixel row padding scheme for non-error corrected image data. The receiving machine may request a minimum row duration. The sending machine is required to impose that minimum, but can arbitrarily make it much longer to meet its own paper handling needs, sending padding bits as appropriate. This mechanism provides a robust basis for flow control in a T.38 gateway. If the outgoing gateway only starts sending a pixel row when it has the entire row in its buffer, it can safely idle, sending padding bits, at any row end it needs to. Only the 6 EOL markers, which terminate an image, must be protected from this kind of flow control. Those markers are defined as being consecutive, with no zero bit padding separating them.</para>
+
+<para>When ECM is used (usually with T.6 image data) the image is packetised in HDLC frames. In this case rate adaption is handled in a similar manner to the V.21 HDLC packets.</para>
+</sect1>
+
+</chapter>
+
+<chapter id="testing-an-implementation-of-t38">
+<title>Testing an implementation of T.38</title>
+<para>The T.38 specification does not define any specific compliance tests which an implementation must pass. It is not supplied with any test vectors. Commetrex is a supplier of T.38 implementations, who have taken it upon themselves to define a set of tests, and create a lab for T.38 interoperability testing. This seems the closest thing to an industry standard for T.38 testing which exists at this time, and is much to their credit.</para>
+
+<para>Commetrex have defined 16 tests which an implementation T.38 undergoes in their lab. These are described on the Commetrex web site as:
+
+<table frame="all">
+<title>Commetrex T.38 tests</title>
+<tgroup cols="3" align="center" colsep="1" rowsep="1">
+<colspec colname="Test #" />
+<colspec colname="Direction" />
+<colspec colname="Transport" />
+<colspec colname="Image file" />
+<colspec colname="Error correction" />
+<colspec colname="Data rate mgt" />
+<colspec colname="Image encoding" />
+<colspec colname="Polling" />
+<thead>
+<row>
+<entry align="centre">Test #</entry>
+<entry align="centre">Direction</entry>
+<entry align="centre">Transport</entry>
+<entry align="centre">Image file</entry>
+<entry align="centre">Error correction</entry>
+<entry align="centre">Data rate mgt</entry>
+<entry align="centre">Image encoding</entry>
+<entry align="centre">Polling</entry>
+</row>
+</thead>
+<tbody>
+<row>
+<entry>1</entry><entry>originate</entry><entry>UDP</entry><entry>ccitt2p.tif</entry><entry>redundancy 0</entry><entry>method 2</entry><entry>MR</entry><entry>no</entry>
+</row>
+<row>
+<entry>2</entry><entry>originate</entry><entry>UDP</entry><entry>100page.tif</entry><entry>redundancy 0</entry><entry>method 2</entry><entry>MR</entry><entry>no</entry>
+</row>
+<row>
+<entry>3</entry><entry>originate</entry><entry>TCP</entry><entry>ccitt2p.tif</entry><entry>redundancy 0</entry><entry>method 1</entry><entry>MR</entry><entry>no</entry>
+</row>
+<row>
+<entry>4</entry><entry>originate</entry><entry>UDP</entry><entry>ccitt2p.tif</entry><entry>redundancy 3</entry><entry>method 2</entry><entry>MR</entry><entry>no</entry>
+</row>
+<row>
+<entry>5</entry><entry>originate</entry><entry>TCP</entry><entry>ccitt2p.tif</entry><entry>FEC 2 from 3 span</entry><entry>method 2</entry><entry>MR</entry><entry>no</entry>
+</row>
+<row>
+<entry>6</entry><entry>originate</entry><entry>UDP</entry><entry>dither1d.tif</entry><entry>redundancy 3</entry><entry>method 2</entry><entry>MR</entry><entry>no</entry>
+</row>
+<row>
+<entry>7</entry><entry>originate</entry><entry>UDP</entry><entry>ccitt2p.tif</entry><entry>redundancy 3</entry><entry>method 2</entry><entry>ECM</entry><entry>no</entry>
+</row>
+<row>
+<entry>8</entry><entry>originate &amp; poll to rx</entry><entry>UDP</entry><entry>ccitt2p.tif</entry><entry>redundancy 3</entry><entry>method 2</entry><entry>MR</entry><entry>polled rx</entry>
+</row>
+<row>
+<entry>9</entry><entry>answer</entry><entry>UDP</entry><entry>ccitt2p.tif</entry><entry>redundancy 0</entry><entry>method 2</entry><entry>MR</entry><entry>no</entry>
+</row>
+<row>
+<entry>10</entry><entry>answer</entry><entry>UDP</entry><entry>100page.tif</entry><entry>redundancy 0</entry><entry>method 2</entry><entry>MR</entry><entry>no</entry>
+</row>
+<row>
+<entry>11</entry><entry>answer</entry><entry>TCP</entry><entry>ccitt2p.tif</entry><entry>redundancy 0</entry><entry>method 1</entry><entry>MR</entry><entry>no</entry>
+</row>
+<row>
+<entry>12</entry><entry>answer</entry><entry>UDP</entry><entry>ccitt2p.tif</entry><entry>redundancy 3</entry><entry>method 2</entry><entry>MR</entry><entry>no</entry>
+</row>
+<row>
+<entry>13</entry><entry>answer</entry><entry>TCP</entry><entry>ccitt2p.tif</entry><entry>FEC 2 from 3 span</entry><entry>method 2</entry><entry>MR</entry><entry>no</entry>
+</row>
+<row>
+<entry>14</entry><entry>answer</entry><entry>UDP</entry><entry>dither1d.tif</entry><entry>redundancy 3</entry><entry>method 2</entry><entry>MR</entry><entry>no</entry>
+</row>
+<row>
+<entry>15</entry><entry>answer</entry><entry>UDP</entry><entry>ccitt2p.tif</entry><entry>redundancy 3</entry><entry>method 2</entry><entry>ECM</entry><entry>no</entry>
+</row>
+<row>
+<entry>16</entry><entry>answer &amp; polled to tx</entry><entry>UDP</entry><entry>ccitt2p.tif</entry><entry>redundancy 3</entry><entry>method 2</entry><entry>MR</entry><entry>polled tx</entry>
+</row>
+</tbody>
+</tgroup>
+</table>
+The file dither1d.tif is a whole page of dense checkerboard pattern, which does not compress, and produces a kind of torture test page. It is about 2M bytes. 100page.tif is exactly what it says - a file with 100 pages of FAX images. The content of the pages does not seem to be specified by Commetrex. The ITU T.30 test images, repeated sufficiently, seems a good basis for this test. ccitt2p.tif appears to be page 2 of the ITU test images. The tests are heavily biased towards non-ECM operation with MR coding. No other coding appears to be used, and only 2 tests use error corrected (ECM) FAXing.
+</para>
+
+<para>Some of the Commetrex tests seem strange. Why is TCP transmission tested with FEC or redundancy? These things are only needed to overcome to lack of reliability in a UDP path.</para>
+</chapter>
+
+<chapter id="living-with-real-world-t38-entities">
+<title>Living with real world T.38 entities.</title>
+<para> The T.38 specification leaves a number of grey areas, and many things can be implemented in several ways. It might, therefore, be expected that interoperability will not always run smoothly. Whilst some design decisions are reasonable subjects for discussion, it is quite common to find T.38 implementations doing things which are simply wrong.</para>
+
+<sect1>
+<title>Non-ECM data ending immediately after the six EOLs denoting the end of a page causes corruption</title>
+<para>From the T.38 specification, a T.38 entity sending non-ECM image data should be able to end the image data, with <emphasis>t4-non-ecm-sig-end</emphasis>, at the last bit of the six consecutive EOLs which denote the end of a page. However, this offers poor compatibility with some implementations of T.38. They will drop their carrier signal prematurely, and the last few rows of the image will be corrupted at the receiver.  This can happen regardless of whether the <emphasis>t4-non-ecm-sig-end</emphasis> is followed by a <emphasis>no-signal</emphasis> message, and regardless of the timing of that <emphasis>no-signal</emphasis> message. Padding with zero bytes for 40ms or more after the six EOLs seems to prevent this, and results in an image which exactly matches the one at the source.</para>
+</sect1>
+
+<sect1>
+<title>Expect mixups between non-ECM and ECM image modes.</title>
+<para>The TCF data should always be sent as non-ECM image data, whether the images which follow it are sent as non-ECM or ECM data. However, some broken implementations of T.38 (e.g. Mediatrix) may send an <emphasis>hdlc-sig-end</emphasis> message to terminate the TCF data. To be as tolerant as possible, it is probably wise to accept <emphasis>hdlc-sig-end</emphasis> or <emphasis>t4-non-ecm-sig-end</emphasis>, regardless of which kind of data it is really terminating.</para>
+</sect1>
+
+<sect1>
+<title>Expect some very poor timing decisions in various designs.</title>
+<para>The preamble for V.21 HDLC data is specified as being 1s ±15%. This should mean 850ms of preamble is within spec, and many modern FAX machines only send that much. A number of T.38 implementations choke on this. They require at least 1s between the <emphasis>v21-preamble</emphasis> message and the first HDLC data, for reliable operation. This is very poor design. Even if there is 1s between these events at the source, jitter on the network can make them arrive less than 1s apart. It appears the best a tolerant implementation can really do is impose a 1s minimum between sending these T.38 messages, and accept that things might still go wrong when there is some jitter.</para>
+
+<para>There is similar intolerance with the timing of the start of training messages for the fast image data modems. Most implementations do not impose a minimum which is above that permitted by the T.30 specification. Some do, however, make no allowance for network jitter reducing the interval at the receiver. You should expect the far end to be using TEP, when sizing the required delay between the start of training, and the first data. Also, for ECM data, make sure the delay allows for the specified minimum 200ms of preamble.</para>
+</sect1>
+
+<sect1>
+<title>Which kinds of error correction to use.</title>
+<para>T.38 defines two kinds of error correction for UDPTL streams. One inserts redundant copies of older IFP packets in each UDPTL packet. The other uses a more complex parity FEC scheme, based on XOR'ed data inserted in each UDPTL packet. Although each UDPTL packet identifies the type of error correction it contains, the original T.38 spec. did not offer any form of negotiation about the type to be used. The SIP and MGCP SDP for T.38 now includes preferred error correction type information, so some measure of negotiation is possible before the flow of UDPTL packets begins.</para>
+
+<para>It appears all T.38 implementations are happy to receive packets with redundant secondary content. However, it seems not all implementations will actually recover anything when there are missing UDPTL packets. It appears not all T.38 implementations are happy to receive packets with parity FEC data. Unless the SDP data from the far end explicitly says they support the parity FEC scheme, it is better to send only redundant information. A good implementation should be prepared to accept either kind of data, even intermixed, regardless of anything in the SDP negotiation. In the real world, whatever you ask for in your SDP data, you will probably receive secondary IFP packet redundancy from the far end. It is the only thing most implementations support, and with real world packet loss it may perform just as well as the more complex parity FEC scheme.</para>
+
+<para>When the first few UDPTL packets are sent, there will be no historical information to send as redundant secondary IFP packets, or parity FEC data. When using the redundant secondary packet scheme this is handled transparently. Each UDPTL packet says the number of redundant secondary IFP packets it contains. This can simply be set to zero for the first UDPTL packet, one for the second, up to the amount of redundancy which has been configured. All implementations seem to work in this way. When parity FEC is being used, the startup arrangement is less clear. While some implementations simply insert zeros for the non-existant packets before the start, others send a few packets with redundant secondary IFP packets, until they are able to insert the required amount of FEC data. Dealing with intermixed types of error correction makes a receiving implementation of UDPTL messier and slower, but is necessary for compatibility.</para>
+</sect1>
+
+<sect1>
+<title>What really ends the data?</title>
+<para>It should be correct to send some image data, and end with the image data signal end message. However, some T.38 implementations misbehave when this is all that is sent. Immediately following the signal end message with a <emphasis>no-signal</emphasis> indicator message seems to greatly improve compatibility with some T.38 implementations.</para>
+
+<para>T.38 defines <emphasis>hdlc-fcs-OK</emphasis>, <emphasis>hdlc-fcs-OK-sig-end</emphasis>, <emphasis>hdlc-sig-end</emphasis>, and <emphasis>no-signal</emphasis> messages. Always end periods of HDLC transmission with  either a <emphasis>hdlc-fcs-OK</emphasis>, <emphasis>hdlc-sig-end</emphasis>, <emphasis>no-signal</emphasis> sequence or a <emphasis>hdlc-fcs-OK-sig-end</emphasis>, <emphasis>no-signal</emphasis> sequence. Do not send <emphasis>hdlc-sig-end</emphasis> after <emphasis>hdlc-fcs-OK-sig-end</emphasis>. A number of T.38 implementations choke on this sequence, even though it looks fairly harmless.</para>
+
+</sect1>
+
+<sect1>
+<title>Normal use versus testing</title>
+<para>The design of some T.38 implementations precludes some reasonable tests from succeeding, due to design features which may have been implemented for sane reasons. This means each test failure needs to be investigated in detail, to see if it represents something genuinely bad in an implementation of T.38. For example, the 2M byte torture test page in the Commetrex test suite will not successfully pass through some T.38 implementations, because of a timeout they impose. For example, some ATAs based on Audiocodes silicon have been seen to stop any continuous period of modem transmission after about 90s. This appears to be a backstop timeout, to prevent troublesome implementations jamming the gateway. Legitimate pages just don't take that long, and the timeout is reasonable. However, the 2M byte test page takes up to 20 minutes to transmit, and will always fail on these boxes.</para>
+
+</sect1>
+
+</chapter>
+
+</book>

Added: freeswitch/trunk/libs/spandsp/doc/t38_manual/css.css
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/doc/t38_manual/css.css	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,564 @@
+body {
+	background-image: url("../images/weave.jpg");
+	font-family: Verdana, Arial, Helvetica, Sans-serif;
+	color: black;
+	margin-right: 20px;
+	margin-left: 20px;
+}
+
+h1 {
+	text-align: center;
+}
+
+h2 {
+	font-family: Verdana, Arial, Helvetica, Sans-serif;
+	border-color: #c00000;
+	color : black;
+	margin-top: 0.8em;
+	border-style: solid;
+	border-width: 0px 0px 3px 0.5em;
+	line-height : 130%;
+}
+
+h3 {
+	font-family: Verdana, Arial, Helvetica, Sans-serif;
+	border-color: #f02020;
+	color : black;
+	border-width: 0px 0px 2px 0.5em;
+	border-style: solid;
+	margin-right: 20%;
+	line-height : 130%;
+}
+caption {
+	font-weight: bold
+}
+a.qindex {}
+a.qindexRef {}
+a.el {
+	text-decoration: none;
+	font-weight: bold
+}
+a.elRef {
+	font-weight: bold
+}
+a.code {
+	text-decoration: none;
+	font-weight: normal;
+	color: #4444ee
+}
+a.codeRef {
+	font-weight: normal;
+	color: #4444ee
+}
+a:hover {
+	text-decoration: none;
+	background-color: #f2f2ff
+}
+dl.el {
+	margin-left: -1cm
+}
+div.fragment {
+	width: 100%;
+	border: none;
+	background-color: #eeeeee
+}
+div.ah {
+	background-color: black;
+	font-weight: bold;
+	color: #ffffff;
+	margin-bottom: 3px;
+	margin-top: 3px
+}
+
+td {
+	font-family: Verdana, Arial, Helvetica, Sans-serif;
+	font-weight: bold;
+}
+
+.navheader {
+	font-family: Verdana, Arial, Helvetica, Sans-serif;
+	background-color: #B2B2ff;
+	font-weight: bold;
+}
+
+.navfooter {
+	font-family: Verdana, Arial, Helvetica, Sans-serif;
+	background-color: #B2B2ff;
+	font-weight: bold;
+}
+
+table.menu {
+	background-color: #000066;
+	font-weight: bold;
+	text-align: center;
+	width: 100%; 
+}
+
+tr.menu {
+	background-color: #ccffff;
+	font-weight: bold;
+	text-align: center;
+}
+td.menu {
+	background-color: #f2e0d0;
+	font-weight: bold;
+	text-align: center;
+}
+
+td.md {
+	background-color: #f2f2ff;
+	font-weight: bold;
+}
+td.mdname1 {
+	background-color: #f2f2ff;
+	font-weight: bold;
+	color: #602020;
+}
+td.mdname {
+	background-color: #f2f2ff;
+	font-weight: bold;
+	color: #602020;
+	width: 600px;
+}
+div.groupHeader {
+	margin-left: 16px;
+	margin-top: 12px;
+	margin-bottom: 6px;
+	font-weight: bold
+}
+div.groupText {
+	margin-left: 16px;
+	font-style: italic;
+	font-size: smaller
+}
+td.indexkey { 
+	font-family: Verdana, Arial, Helvetica, Sans-serif;
+	background-color: #eeeeff; 
+	font-weight: bold; 
+	padding-right  : 10px; 
+	padding-top    : 2px; 
+	padding-left   : 10px; 
+	padding-bottom : 2px; 
+	margin-left    : 0px; 
+	margin-right   : 0px; 
+	margin-top     : 2px; 
+	margin-bottom  : 2px  
+}
+td.indexvalue { 
+	font-family: Verdana, Arial, Helvetica, Sans-serif;
+	background-color: #eeeeff; 
+	font-style: italic; 
+	padding-right  : 10px; 
+	padding-top    : 2px; 
+	padding-left   : 10px; 
+	padding-bottom : 2px; 
+	margin-left    : 0px; 
+	margin-right   : 0px; 
+	margin-top     : 2px; 
+	margin-bottom  : 2px  
+}
+span.keyword {
+	color: #008000
+}
+span.keywordtype {
+	color: #604020
+}
+span.keywordflow {
+	color: #e08000
+}
+span.comment {
+	color: #800000
+}
+span.preprocessor {
+	color: #806020
+}
+span.stringliteral {
+	color: #002080
+}
+span.charliteral {
+	color: #008080
+}
+em {
+	color: #990000;
+	background-color: transparent;
+}
+h1,h2,h3,h4,h5,h6,p,center,td,th,ul,dl,div {
+	font-family: Geneva, Arial, Helvetica, sans-serif;
+}
+body,td {
+	font-size: 90%;
+}
+h1 {
+	text-align: center;
+	font-size: 160%;
+}
+h2 {
+	font-size: 120%;
+}
+h3 {
+	font-size: 100%;
+}
+caption {
+	font-weight: bold
+}
+div.qindex {
+	width: 100%;
+	background-color: #eeeeff;
+	border: 1px solid #b0b0b0;
+	text-align: center;
+	margin: 2px;
+	padding: 2px;
+	line-height: 140%;
+}
+div.nav {
+	width: 100%;
+	background-color: #eeeeff;
+	border: 1px solid #b0b0b0;
+	text-align: center;
+	margin: 2px;
+	padding: 2px;
+	line-height: 140%;
+}
+div.navtab {
+	background-color: #eeeeff;
+	border: 1px solid #b0b0b0;
+	text-align: center;
+	margin: 2px;
+	margin-right: 15px;
+	padding: 2px;
+}
+td.navtab {
+	font-size: 70%;
+}
+a.qindex {
+	text-decoration: none;
+	font-weight: bold;
+	color: #1a419d;
+}
+a.qindex:visited {
+	text-decoration: none;
+	font-weight: bold;
+	color: #1a419d
+}
+a.qindex:hover {
+	text-decoration: none;
+	background-color: #ddddff;
+}
+a.qindexHL {
+	text-decoration: none;
+	font-weight: bold;
+	background-color: #6666cc;
+	color: #ffffff;
+	border: 1px double #9295C2;
+}
+a.qindexHL:hover {
+	text-decoration: none;
+	background-color: #6666cc;
+	color: #ffffff;
+}
+a.qindexHL:visited {
+	text-decoration: none;
+	background-color: #6666cc;
+	color: #ffffff
+}
+a.el {
+	text-decoration: none;
+	font-weight: bold
+}
+a.elRef {
+	font-weight: bold
+}
+a.code:link {
+	text-decoration: none;
+	font-weight: normal;
+	color: #0000FF
+}
+a.code:visited {
+	text-decoration: none;
+	font-weight: normal;
+	color: #0000FF
+}
+a.codeRef:link {
+	font-weight: normal;
+	color: #0000FF
+}
+a.codeRef:visited {
+	font-weight: normal;
+	color: #0000FF
+}
+a:hover {
+	text-decoration: none;
+	background-color: #f2f2ff
+}
+dl.el {
+	margin-left: -1cm
+}
+.fragment {
+	font-family: Fixed, monospace;
+	font-size: 95%;
+}
+pre.fragment {
+	border: 1px solid #CCCCCC;
+	background-color: #f5f5f5;
+	margin-top: 4px;
+	margin-bottom: 4px;
+	margin-left: 2px;
+	margin-right: 8px;
+	padding-left: 6px;
+	padding-right: 6px;
+	padding-top: 4px;
+	padding-bottom: 4px;
+}
+div.ah {
+	background-color: black;
+	font-weight: bold;
+	color: #ffffff;
+	margin-bottom: 3px;
+	margin-top: 3px
+}
+td.md {
+	background-color: #F4F4FB;
+	font-weight: bold;
+}
+td.mdPrefix {
+	background-color: #F4F4FB;
+	color: #606060;
+	font-size: 80%;
+}
+td.mdname1 {
+	background-color: #F4F4FB;
+	font-weight: bold;
+	color: #602020;
+}
+td.mdname {
+	background-color: #F4F4FB;
+	font-weight: bold;
+	color: #602020;
+	width: 600px;
+}
+div.groupHeader {
+	margin-left: 16px;
+	margin-top: 12px;
+	margin-bottom: 6px;
+	font-weight: bold;
+}
+div.groupText {
+	margin-left: 16px;
+	font-style: italic;
+	font-size: 90%
+}
+td.indexkey {
+	background-color: #eeeeff;
+	font-weight: bold;
+	padding-right  : 10px;
+	padding-top    : 2px;
+	padding-left   : 10px;
+	padding-bottom : 2px;
+	margin-left    : 0px;
+	margin-right   : 0px;
+	margin-top     : 2px;
+	margin-bottom  : 2px;
+	border: 1px solid #CCCCCC;
+}
+td.indexvalue {
+	background-color: #eeeeff;
+	font-style: italic;
+	padding-right  : 10px;
+	padding-top    : 2px;
+	padding-left   : 10px;
+	padding-bottom : 2px;
+	margin-left    : 0px;
+	margin-right   : 0px;
+	margin-top     : 2px;
+	margin-bottom  : 2px;
+	border: 1px solid #CCCCCC;
+}
+tr.memlist {
+	background-color: #f0f0f0; 
+}
+p.formulaDsp {
+	text-align: center;
+}
+img.formulaDsp {
+}
+img.formulaInl {
+	vertical-align: middle; 
+}
+span.keyword {
+	color: #008000
+}
+span.keywordtype {
+	color: #604020
+}
+span.keywordflow {
+	color: #e08000
+}
+span.comment {
+	color: #800000
+}
+span.preprocessor  {
+	color: #806020
+}
+span.stringliteral {
+	color: #002080
+}
+span.charliteral {
+	color: #008080
+}
+.mdTable {
+	border: 1px solid #868686;
+	background-color: #F4F4FB;
+}
+.mdRow {
+	padding: 8px 10px;
+}
+.mdescLeft {
+	padding: 0px 8px 4px 8px;
+	font-size: 80%;
+	font-style: italic;
+	background-color: #FAFAFA;
+	border-top: 1px none #E0E0E0;
+	border-right: 1px none #E0E0E0;
+	border-bottom: 1px none #E0E0E0;
+	border-left: 1px none #E0E0E0;
+	margin: 0px;
+}
+.mdescRight {
+	padding: 0px 8px 4px 8px;
+	font-size: 80%;
+	font-style: italic;
+	background-color: #FAFAFA;
+	border-top: 1px none #E0E0E0;
+	border-right: 1px none #E0E0E0;
+	border-bottom: 1px none #E0E0E0;
+	border-left: 1px none #E0E0E0;
+	margin: 0px;
+}
+.memItemLeft {
+	padding: 1px 0px 0px 8px;
+	margin: 4px;
+	border-top-width: 1px;
+	border-right-width: 1px;
+	border-bottom-width: 1px;
+	border-left-width: 1px;
+	border-top-color: #E0E0E0;
+	border-right-color: #E0E0E0;
+	border-bottom-color: #E0E0E0;
+	border-left-color: #E0E0E0;
+	border-top-style: solid;
+	border-right-style: none;
+	border-bottom-style: none;
+	border-left-style: none;
+	background-color: #FAFAFA;
+	font-size: 80%;
+}
+.memItemRight {
+	padding: 1px 8px 0px 8px;
+	margin: 4px;
+	border-top-width: 1px;
+	border-right-width: 1px;
+	border-bottom-width: 1px;
+	border-left-width: 1px;
+	border-top-color: #E0E0E0;
+	border-right-color: #E0E0E0;
+	border-bottom-color: #E0E0E0;
+	border-left-color: #E0E0E0;
+	border-top-style: solid;
+	border-right-style: none;
+	border-bottom-style: none;
+	border-left-style: none;
+	background-color: #FAFAFA;
+	font-size: 80%;
+}
+.memTemplItemLeft {
+	padding: 1px 0px 0px 8px;
+	margin: 4px;
+	border-top-width: 1px;
+	border-right-width: 1px;
+	border-bottom-width: 1px;
+	border-left-width: 1px;
+	border-top-color: #E0E0E0;
+	border-right-color: #E0E0E0;
+	border-bottom-color: #E0E0E0;
+	border-left-color: #E0E0E0;
+	border-top-style: none;
+	border-right-style: none;
+	border-bottom-style: none;
+	border-left-style: none;
+	background-color: #FAFAFA;
+	font-size: 80%;
+}
+.memTemplItemRight {
+	padding: 1px 8px 0px 8px;
+	margin: 4px;
+	border-top-width: 1px;
+	border-right-width: 1px;
+	border-bottom-width: 1px;
+	border-left-width: 1px;
+	border-top-color: #E0E0E0;
+	border-right-color: #E0E0E0;
+	border-bottom-color: #E0E0E0;
+	border-left-color: #E0E0E0;
+	border-top-style: none;
+	border-right-style: none;
+	border-bottom-style: none;
+	border-left-style: none;
+	background-color: #FAFAFA;
+	font-size: 80%;
+}
+.memTemplParams {
+	padding: 1px 0px 0px 8px;
+	margin: 4px;
+	border-top-width: 1px;
+	border-right-width: 1px;
+	border-bottom-width: 1px;
+	border-left-width: 1px;
+	border-top-color: #E0E0E0;
+	border-right-color: #E0E0E0;
+	border-bottom-color: #E0E0E0;
+	border-left-color: #E0E0E0;
+	border-top-style: solid;
+	border-right-style: none;
+	border-bottom-style: none;
+	border-left-style: none;
+	color: #606060;
+	background-color: #FAFAFA;
+	font-size: 80%;
+}
+.search {
+	color: #003399;
+	font-weight: bold;
+}
+form.search {
+	margin-bottom: 0px;
+	margin-top: 0px;
+}
+input.search {
+	font-size: 75%;
+	color: #000080;
+	font-weight: normal;
+	background-color: #eeeeff;
+}
+td.tiny {
+	font-size: 75%;
+}
+a {
+	color: #252e78;
+}
+a:visited {
+	color: #3d2185;
+}
+.dirtab {
+	padding: 4px;
+	border-collapse: collapse;
+	border: 1px solid #b0b0b0;
+}
+th.dirtab {
+	background: #eeeeff;
+	font-weight: bold;
+}
+hr {
+	height: 1px;
+	border: none;
+	border-top: 1px solid black;
+}

Added: freeswitch/trunk/libs/spandsp/doc/wrapper.xsl
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/doc/wrapper.xsl	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,5 @@
+<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
+                version='1.0'>
+  <xsl:import href="http://docbook.sourceforge.net/release/xsl/current/xhtml/chunk.xsl"/>
+  <xsl:param name="html.stylesheet">css.css</xsl:param>
+</xsl:stylesheet>
\ No newline at end of file

Added: freeswitch/trunk/libs/spandsp/libspandsp.vcproj
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/libspandsp.vcproj	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,793 @@
+<?xml version="1.0" encoding="Windows-1252"?>
+<VisualStudioProject
+	ProjectType="Visual C++"
+	Version="8.00"
+	Name="libspandsp"
+	ProjectGUID="{CF70F278-3364-4395-A2E1-23501C9B8AD2}"
+	RootNamespace="libspandsp"
+	Keyword="Win32Proj"
+	>
+	<Platforms>
+		<Platform
+			Name="Win32"
+		/>
+	</Platforms>
+	<ToolFiles>
+	</ToolFiles>
+	<Configurations>
+		<Configuration
+			Name="Debug|Win32"
+			OutputDirectory="$(SolutionDir)$(ConfigurationName)"
+			IntermediateDirectory="$(ConfigurationName)"
+			ConfigurationType="2"
+			CharacterSet="1"
+			>
+			<Tool
+				Name="VCPreBuildEventTool"
+			/>
+			<Tool
+				Name="VCCustomBuildTool"
+			/>
+			<Tool
+				Name="VCXMLDataGeneratorTool"
+			/>
+			<Tool
+				Name="VCWebServiceProxyGeneratorTool"
+			/>
+			<Tool
+				Name="VCMIDLTool"
+			/>
+			<Tool
+				Name="VCCLCompilerTool"
+				Optimization="0"
+				AdditionalIncludeDirectories="src; src\spandsp; src\msvc"
+				PreprocessorDefinitions="WIN32;_DEBUG;_WINDOWS;_USRDLL;LIBSPANDSP_EXPORTS;_CRT_SECURE_NO_DEPRECATE;_CRT_NONSTDC_NO_DEPRECATE"
+				MinimalRebuild="true"
+				BasicRuntimeChecks="3"
+				RuntimeLibrary="3"
+				UsePrecompiledHeader="0"
+				WarningLevel="4"
+				Detect64BitPortabilityProblems="true"
+				DebugInformationFormat="4"
+			/>
+			<Tool
+				Name="VCManagedResourceCompilerTool"
+			/>
+			<Tool
+				Name="VCResourceCompilerTool"
+			/>
+			<Tool
+				Name="VCPreLinkEventTool"
+			/>
+			<Tool
+				Name="VCLinkerTool"
+				LinkIncremental="2"
+				ModuleDefinitionFile="src/msvc/spandsp.def"
+				GenerateDebugInformation="true"
+				SubSystem="2"
+				TargetMachine="1"
+			/>
+			<Tool
+				Name="VCALinkTool"
+			/>
+			<Tool
+				Name="VCManifestTool"
+			/>
+			<Tool
+				Name="VCXDCMakeTool"
+			/>
+			<Tool
+				Name="VCBscMakeTool"
+			/>
+			<Tool
+				Name="VCFxCopTool"
+			/>
+			<Tool
+				Name="VCAppVerifierTool"
+			/>
+			<Tool
+				Name="VCWebDeploymentTool"
+			/>
+			<Tool
+				Name="VCPostBuildEventTool"
+			/>
+		</Configuration>
+		<Configuration
+			Name="Release|Win32"
+			OutputDirectory="$(SolutionDir)$(ConfigurationName)"
+			IntermediateDirectory="$(ConfigurationName)"
+			ConfigurationType="2"
+			CharacterSet="1"
+			WholeProgramOptimization="1"
+			>
+			<Tool
+				Name="VCPreBuildEventTool"
+			/>
+			<Tool
+				Name="VCCustomBuildTool"
+			/>
+			<Tool
+				Name="VCXMLDataGeneratorTool"
+			/>
+			<Tool
+				Name="VCWebServiceProxyGeneratorTool"
+			/>
+			<Tool
+				Name="VCMIDLTool"
+			/>
+			<Tool
+				Name="VCCLCompilerTool"
+				AdditionalIncludeDirectories="src; src\spandsp; src\msvc"
+				PreprocessorDefinitions="WIN32;NDEBUG;_WINDOWS;_USRDLL;LIBSPANDSP_EXPORTS;_CRT_SECURE_NO_DEPRECATE;_CRT_NONSTDC_NO_DEPRECATE"
+				RuntimeLibrary="2"
+				UsePrecompiledHeader="0"
+				WarningLevel="4"
+				Detect64BitPortabilityProblems="true"
+				DebugInformationFormat="3"
+			/>
+			<Tool
+				Name="VCManagedResourceCompilerTool"
+			/>
+			<Tool
+				Name="VCResourceCompilerTool"
+			/>
+			<Tool
+				Name="VCPreLinkEventTool"
+			/>
+			<Tool
+				Name="VCLinkerTool"
+				LinkIncremental="1"
+				ModuleDefinitionFile="src/msvc/spandsp.def"
+				GenerateDebugInformation="true"
+				SubSystem="2"
+				OptimizeReferences="2"
+				EnableCOMDATFolding="2"
+				TargetMachine="1"
+			/>
+			<Tool
+				Name="VCALinkTool"
+			/>
+			<Tool
+				Name="VCManifestTool"
+			/>
+			<Tool
+				Name="VCXDCMakeTool"
+			/>
+			<Tool
+				Name="VCBscMakeTool"
+			/>
+			<Tool
+				Name="VCFxCopTool"
+			/>
+			<Tool
+				Name="VCAppVerifierTool"
+			/>
+			<Tool
+				Name="VCWebDeploymentTool"
+			/>
+			<Tool
+				Name="VCPostBuildEventTool"
+			/>
+		</Configuration>
+	</Configurations>
+	<References>
+	</References>
+	<Files>
+		<Filter
+			Name="Source Files"
+			Filter="cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx"
+			UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
+			>
+			<File
+				RelativePath=".\src\adsi.c"
+				>
+			</File>
+			<File
+				RelativePath=".\src\async.c"
+				>
+			</File>
+			<File
+				RelativePath=".\src\at_interpreter.c"
+				>
+			</File>
+			<File
+				RelativePath=".\src\awgn.c"
+				>
+			</File>
+			<File
+				RelativePath=".\src\bell_r2_mf.c"
+				>
+			</File>
+			<File
+				RelativePath=".\src\bert.c"
+				>
+			</File>
+			<File
+				RelativePath=".\src\complex_filters.c"
+				>
+			</File>
+			<File
+				RelativePath=".\src\complex_vector_float.c"
+				>
+			</File>
+			<File
+				RelativePath=".\src\crc.c"
+				>
+			</File>
+			<File
+				RelativePath=".\src\dds_float.c"
+				>
+			</File>
+			<File
+				RelativePath=".\src\dds_int.c"
+				>
+			</File>
+			<File
+				RelativePath=".\src\dtmf.c"
+				>
+			</File>
+			<File
+				RelativePath=".\src\echo.c"
+				>
+			</File>
+			<File
+				RelativePath=".\src\fax.c"
+				>
+			</File>
+			<File
+				RelativePath=".\src\fsk.c"
+				>
+			</File>
+			<File
+				RelativePath=".\src\g722_decode.c"
+				>
+			</File>
+			<File
+				RelativePath=".\src\g722_encode.c"
+				>
+			</File>
+			<File
+				RelativePath=".\src\g726.c"
+				>
+			</File>
+			<File
+				RelativePath=".\src\msvc\gettimeofday.c"
+				>
+			</File>
+			<File
+				RelativePath=".\src\gsm0610_decode.c"
+				>
+			</File>
+			<File
+				RelativePath=".\src\gsm0610_encode.c"
+				>
+			</File>
+			<File
+				RelativePath=".\src\gsm0610_long_term.c"
+				>
+			</File>
+			<File
+				RelativePath=".\src\gsm0610_lpc.c"
+				>
+			</File>
+			<File
+				RelativePath=".\src\gsm0610_preprocess.c"
+				>
+			</File>
+			<File
+				RelativePath=".\src\gsm0610_rpe.c"
+				>
+			</File>
+			<File
+				RelativePath=".\src\gsm0610_short_term.c"
+				>
+			</File>
+			<File
+				RelativePath=".\src\hdlc.c"
+				>
+			</File>
+			<File
+				RelativePath=".\src\ima_adpcm.c"
+				>
+			</File>
+			<File
+				RelativePath=".\src\logging.c"
+				>
+			</File>
+			<File
+				RelativePath=".\src\lpc10_analyse.c"
+				>
+			</File>
+			<File
+				RelativePath=".\src\lpc10_decode.c"
+				>
+			</File>
+			<File
+				RelativePath=".\src\lpc10_encode.c"
+				>
+			</File>
+			<File
+				RelativePath=".\src\lpc10_placev.c"
+				>
+			</File>
+			<File
+				RelativePath=".\src\lpc10_voicing.c"
+				>
+			</File>
+			<File
+				RelativePath=".\src\modem_connect_tones.c"
+				>
+			</File>
+			<File
+				RelativePath=".\src\modem_echo.c"
+				>
+			</File>
+			<File
+				RelativePath=".\src\noise.c"
+				>
+			</File>
+			<File
+				RelativePath=".\src\oki_adpcm.c"
+				>
+			</File>
+			<File
+				RelativePath=".\src\playout.c"
+				>
+			</File>
+			<File
+				RelativePath=".\src\plc.c"
+				>
+			</File>
+			<File
+				RelativePath=".\src\power_meter.c"
+				>
+			</File>
+			<File
+				RelativePath=".\src\queue.c"
+				>
+			</File>
+			<File
+				RelativePath=".\src\schedule.c"
+				>
+			</File>
+			<File
+				RelativePath=".\src\sig_tone.c"
+				>
+			</File>
+			<File
+				RelativePath=".\src\super_tone_rx.c"
+				>
+			</File>
+			<File
+				RelativePath=".\src\super_tone_tx.c"
+				>
+			</File>
+			<File
+				RelativePath=".\src\t30_api.c"
+				>
+			</File>
+			<File
+				RelativePath=".\src\t30.c"
+				>
+			</File>
+			<File
+				RelativePath=".\src\t30_logging.c"
+				>
+			</File>
+			<File
+				RelativePath=".\src\t31.c"
+				>
+			</File>
+			<File
+				RelativePath=".\src\t35.c"
+				>
+			</File>
+			<File
+				RelativePath=".\src\t38_core.c"
+				>
+			</File>
+			<File
+				RelativePath=".\src\t38_gateway.c"
+				>
+			</File>
+			<File
+				RelativePath=".\src\t38_terminal.c"
+				>
+			</File>
+			<File
+				RelativePath=".\src\t4.c"
+				>
+			</File>
+			<File
+				RelativePath=".\src\testcpuid.c"
+				>
+			</File>
+			<File
+				RelativePath=".\src\time_scale.c"
+				>
+			</File>
+			<File
+				RelativePath=".\src\tone_detect.c"
+				>
+			</File>
+			<File
+				RelativePath=".\src\tone_generate.c"
+				>
+			</File>
+			<File
+				RelativePath=".\src\v17rx.c"
+				>
+			</File>
+			<File
+				RelativePath=".\src\v17tx.c"
+				>
+			</File>
+			<File
+				RelativePath=".\src\v22bis_rx.c"
+				>
+			</File>
+			<File
+				RelativePath=".\src\v22bis_tx.c"
+				>
+			</File>
+			<File
+				RelativePath=".\src\v27ter_rx.c"
+				>
+			</File>
+			<File
+				RelativePath=".\src\v27ter_tx.c"
+				>
+			</File>
+			<File
+				RelativePath=".\src\v29rx.c"
+				>
+			</File>
+			<File
+				RelativePath=".\src\v29tx.c"
+				>
+			</File>
+			<File
+				RelativePath=".\src\v42bis.c"
+				>
+			</File>
+			<File
+				RelativePath=".\src\v42.c"
+				>
+			</File>
+			<File
+				RelativePath=".\src\v8.c"
+				>
+			</File>
+			<File
+				RelativePath=".\src\vector_float.c"
+				>
+			</File>
+			<File
+				RelativePath=".\src\vector_int.c"
+				>
+			</File>
+		</Filter>
+		<Filter
+			Name="Header Files"
+			Filter="h;hpp;hxx;hm;inl;inc;xsd"
+			UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"
+			>
+			<File
+				RelativePath=".\src\spandsp\adsi.h"
+				>
+			</File>
+			<File
+				RelativePath=".\src\spandsp\arctan2.h"
+				>
+			</File>
+			<File
+				RelativePath=".\src\spandsp\async.h"
+				>
+			</File>
+			<File
+				RelativePath=".\src\spandsp\at_interpreter.h"
+				>
+			</File>
+			<File
+				RelativePath=".\src\spandsp\awgn.h"
+				>
+			</File>
+			<File
+				RelativePath=".\src\spandsp\bell_r2_mf.h"
+				>
+			</File>
+			<File
+				RelativePath=".\src\spandsp\bert.h"
+				>
+			</File>
+			<File
+				RelativePath=".\src\spandsp\biquad.h"
+				>
+			</File>
+			<File
+				RelativePath=".\src\spandsp\bit_operations.h"
+				>
+			</File>
+			<File
+				RelativePath=".\src\spandsp\bitstream.h"
+				>
+			</File>
+			<File
+				RelativePath=".\src\spandsp\complex_filters.h"
+				>
+			</File>
+			<File
+				RelativePath=".\src\spandsp\complex.h"
+				>
+			</File>
+			<File
+				RelativePath=".\src\spandsp\complex_vector_float.h"
+				>
+			</File>
+			<File
+				RelativePath=".\src\spandsp\crc.h"
+				>
+			</File>
+			<File
+				RelativePath=".\src\spandsp\dc_restore.h"
+				>
+			</File>
+			<File
+				RelativePath=".\src\spandsp\dds.h"
+				>
+			</File>
+			<File
+				RelativePath=".\src\spandsp\dtmf.h"
+				>
+			</File>
+			<File
+				RelativePath=".\src\spandsp\echo.h"
+				>
+			</File>
+			<File
+				RelativePath=".\src\spandsp\fax.h"
+				>
+			</File>
+			<File
+				RelativePath=".\src\faxfont.h"
+				>
+			</File>
+			<File
+				RelativePath=".\src\spandsp\fir.h"
+				>
+			</File>
+			<File
+				RelativePath=".\src\spandsp\fsk.h"
+				>
+			</File>
+			<File
+				RelativePath=".\src\spandsp\g168models.h"
+				>
+			</File>
+			<File
+				RelativePath=".\src\spandsp\g711.h"
+				>
+			</File>
+			<File
+				RelativePath=".\src\spandsp\g722.h"
+				>
+			</File>
+			<File
+				RelativePath=".\src\spandsp\g726.h"
+				>
+			</File>
+			<File
+				RelativePath=".\src\spandsp\hdlc.h"
+				>
+			</File>
+			<File
+				RelativePath=".\src\spandsp\ima_adpcm.h"
+				>
+			</File>
+			<File
+				RelativePath=".\src\msvc\inttypes.h"
+				>
+			</File>
+			<File
+				RelativePath=".\src\spandsp\logging.h"
+				>
+			</File>
+			<File
+				RelativePath=".\src\spandsp\lpc10.h"
+				>
+			</File>
+			<File
+				RelativePath=".\src\spandsp\modem_connect_tones.h"
+				>
+			</File>
+			<File
+				RelativePath=".\src\spandsp\modem_echo.h"
+				>
+			</File>
+			<File
+				RelativePath=".\src\spandsp\noise.h"
+				>
+			</File>
+			<File
+				RelativePath=".\src\spandsp\oki_adpcm.h"
+				>
+			</File>
+			<File
+				RelativePath=".\src\spandsp\playout.h"
+				>
+			</File>
+			<File
+				RelativePath=".\src\spandsp\plc.h"
+				>
+			</File>
+			<File
+				RelativePath=".\src\spandsp\power_meter.h"
+				>
+			</File>
+			<File
+				RelativePath=".\src\spandsp\queue.h"
+				>
+			</File>
+			<File
+				RelativePath=".\src\spandsp\schedule.h"
+				>
+			</File>
+			<File
+				RelativePath=".\src\spandsp\sig_tone.h"
+				>
+			</File>
+			<File
+				RelativePath=".\src\spandsp\silence_gen.h"
+				>
+			</File>
+			<File
+				RelativePath=".\src\spandsp.h"
+				>
+			</File>
+			<File
+				RelativePath=".\src\spandsp\super_tone_rx.h"
+				>
+			</File>
+			<File
+				RelativePath=".\src\spandsp\super_tone_tx.h"
+				>
+			</File>
+			<File
+				RelativePath=".\src\spandsp\t30_api.h"
+				>
+			</File>
+			<File
+				RelativePath=".\src\spandsp\t30_fcf.h"
+				>
+			</File>
+			<File
+				RelativePath=".\src\spandsp\t30.h"
+				>
+			</File>
+			<File
+				RelativePath=".\src\spandsp\t30_logging.h"
+				>
+			</File>
+			<File
+				RelativePath=".\src\spandsp\t31.h"
+				>
+			</File>
+			<File
+				RelativePath=".\src\spandsp\t35.h"
+				>
+			</File>
+			<File
+				RelativePath=".\src\spandsp\t38_core.h"
+				>
+			</File>
+			<File
+				RelativePath=".\src\spandsp\t38_gateway.h"
+				>
+			</File>
+			<File
+				RelativePath=".\src\spandsp\t38_terminal.h"
+				>
+			</File>
+			<File
+				RelativePath=".\src\spandsp\t4.h"
+				>
+			</File>
+			<File
+				RelativePath=".\src\t4states.h"
+				>
+			</File>
+			<File
+				RelativePath=".\src\spandsp\telephony.h"
+				>
+			</File>
+			<File
+				RelativePath=".\src\msvc\tgmath.h"
+				>
+			</File>
+			<File
+				RelativePath=".\src\msvc\sys\time.h"
+				>
+			</File>
+			<File
+				RelativePath=".\src\spandsp\time_scale.h"
+				>
+			</File>
+			<File
+				RelativePath=".\src\spandsp\timing.h"
+				>
+			</File>
+			<File
+				RelativePath=".\src\spandsp\tone_detect.h"
+				>
+			</File>
+			<File
+				RelativePath=".\src\spandsp\tone_generate.h"
+				>
+			</File>
+			<File
+				RelativePath=".\src\msvc\unistd.h"
+				>
+			</File>
+			<File
+				RelativePath=".\src\spandsp\v17rx.h"
+				>
+			</File>
+			<File
+				RelativePath=".\src\spandsp\v17tx.h"
+				>
+			</File>
+			<File
+				RelativePath=".\src\spandsp\v22bis.h"
+				>
+			</File>
+			<File
+				RelativePath=".\src\spandsp\v27ter_rx.h"
+				>
+			</File>
+			<File
+				RelativePath=".\src\spandsp\v27ter_tx.h"
+				>
+			</File>
+			<File
+				RelativePath=".\src\spandsp\v29rx.h"
+				>
+			</File>
+			<File
+				RelativePath=".\src\spandsp\v29tx.h"
+				>
+			</File>
+			<File
+				RelativePath=".\src\spandsp\v42bis.h"
+				>
+			</File>
+			<File
+				RelativePath=".\src\spandsp\v42.h"
+				>
+			</File>
+			<File
+				RelativePath=".\src\spandsp\v8.h"
+				>
+			</File>
+			<File
+				RelativePath=".\src\spandsp\vector_float.h"
+				>
+			</File>
+			<File
+				RelativePath=".\src\spandsp\vector_int.h"
+				>
+			</File>
+			<File
+				RelativePath=".\src\spandsp\version.h"
+				>
+			</File>
+		</Filter>
+		<Filter
+			Name="Resource Files"
+			Filter="rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav"
+			UniqueIdentifier="{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}"
+			>
+		</Filter>
+	</Files>
+	<Globals>
+	</Globals>
+</VisualStudioProject>

Added: freeswitch/trunk/libs/spandsp/spandsp-sim/Makefile.am
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/spandsp-sim/Makefile.am	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,103 @@
+##
+## SpanDSP - a series of DSP components for telephony
+##
+## Makefile.am - Process this file with automake to produce Makefile.in
+##
+## This program is free software; you can redistribute it and/or modify
+## it under the terms of the GNU General Public License version 2, as
+## published by the Free Software Foundation.
+##
+## This program is distributed in the hope that it will be useful,
+## but WITHOUT ANY WARRANTY; without even the implied warranty of
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+## GNU General Public License for more details.
+##
+## You should have received a copy of the GNU General Public License
+## along with this program; if not, write to the Free Software
+## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+##
+## $Id: Makefile.am,v 1.7 2008/05/03 13:05:04 steveu Exp $
+
+AM_CFLAGS = $(COMP_VENDOR_CFLAGS)
+AM_LDFLAGS = $(COMP_VENDOR_LDFLAGS)
+
+LIBS += $(TESTLIBS)
+
+MAINTAINERCLEANFILES = Makefile.in
+
+INCLUDES = -I$(top_builddir) -I$(top_builddir)/src -DDATADIR="\"$(pkgdatadir)\""
+
+noinst_PROGRAMS = make_line_models
+
+lib_LTLIBRARIES = libspandsp-sim.la
+
+libspandsp_sim_la_SOURCES = g1050.c \
+                            line_model.c \
+                            rfc2198_sim.c \
+                            test_utils.c
+
+nodist_libspandsp_sim_la_SOURCES = line_models.c
+
+libspandsp_sim_la_LDFLAGS = -version-info @SPANDSP_LT_CURRENT@:@SPANDSP_LT_REVISION@:@SPANDSP_LT_AGE@
+
+nobase_include_HEADERS = spandsp/g1050.h \
+                         spandsp/line_model.h \
+                         spandsp/line_models.h \
+                         spandsp/rfc2198_sim.h \
+                         spandsp/test_utils.h \
+                         spandsp-sim.h
+
+make_line_models_SOURCES = make_line_models.c
+make_line_models_LDADD = -L$(top_builddir)/src -lspandsp
+
+# We need to run make_line_models, so it generates the line_models.h file
+# used by several of the test programs.
+
+line_models.lo: make_line_models$(EXEEXT) line_models.c
+
+line_models.$(OBJEXT): make_line_models$(EXEEXT) line_models.c
+
+line_models.c: make_line_models$(EXEEXT)
+	./make_line_models$(EXEEXT)
+    
+DSP = libspandsp-sim.dsp
+VCPROJ = libspandsp-sim.vcproj
+
+WIN32SOURCES = $(libspandsp_sim_la_SOURCES) msvc/gettimeofday.c
+WIN32HEADERS = $(nobase_include_HEADERS) spandsp-sim.h
+
+DSPOUT = | awk '{printf("%s\r\n", $$0)}' >> $(DSP)
+VCPROJOUT = | awk '{printf("%s\r\n", $$0)}' >> $(VCPROJ)
+
+$(DSP): msvc/msvcproj.head msvc/msvcproj.foot Makefile.am
+	echo "creating $(DSP)"
+	@(cp $(srcdir)/msvc/msvcproj.head $(DSP); \
+	echo "# Begin Group \"Source Files\"" $(DSPOUT); \
+	for file in $(WIN32SOURCES); do \
+	echo "# Begin Source File" $(DSPOUT); \
+	echo "" $(DSPOUT); \
+	echo "SOURCE=.\\"$$file $(DSPOUT); \
+	echo "# End Source File" $(DSPOUT); \
+	done; \
+	echo "# End Group" $(DSPOUT); \
+	echo "# Begin Group \"Header Files\"" $(DSPOUT); \
+	for file in $(WIN32HEADERS); do \
+	echo "# Begin Source File" $(DSPOUT); \
+	echo "" $(DSPOUT); \
+	echo "SOURCE=.\\"$$file $(DSPOUT); \
+	echo "# End Source File" $(DSPOUT); \
+	done; \
+	echo "# End Group" $(DSPOUT); \
+	cat $(srcdir)/msvc/msvcproj.foot $(DSPOUT) )
+
+$(VCPROJ): msvc/vc8proj.head msvc/vc8proj.foot Makefile.am
+	echo "creating $(VCPROJ)"
+	@(cp $(srcdir)/msvc/vc8proj.head $(VCPROJ); \
+	for file in $(WIN32SOURCES); do \
+		echo "<File RelativePath=\""$$file"\"></File>" $(VCPROJOUT); \
+	done; \
+	echo "</Filter><Filter  Name=\"Header Files\">" $(VCPROJOUT); \
+	for file in $(WIN32HEADERS); do \
+		echo "<File RelativePath=\""$$file"\"></File>" $(VCPROJOUT); \
+	done; \
+	cat $(srcdir)/msvc/vc8proj.foot $(VCPROJOUT) )

Added: freeswitch/trunk/libs/spandsp/spandsp-sim/g1050.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/spandsp-sim/g1050.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,1275 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * g1050.c - IP network modeling, as per G.1050/TIA-921.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2007 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: g1050.c,v 1.10 2008/07/02 14:48:25 steveu Exp $
+ */
+
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <unistd.h>
+#include <inttypes.h>
+#include <string.h>
+#include <time.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <audiofile.h>
+#include "floating_fudge.h"
+#if defined(HAVE_TGMATH_H)
+#include <tgmath.h>
+#endif
+#if defined(HAVE_MATH_H)
+#define GEN_CONST
+#include <math.h>
+#endif
+
+#include "spandsp.h"
+#include "spandsp/g1050.h"
+
+#define PACKET_LOSS_TIME    -1
+
+#define FALSE 0
+#define TRUE (!FALSE)
+
+g1050_constants_t g1050_constants[1] =
+{
+    {
+        {
+            {   /* Side A LAN */
+                {
+                    0.004,          /*! Probability of loss rate change low->high */
+                    0.1             /*! Probability of loss rate change high->low */
+                },
+                {
+                    {
+                        0.0,        /*! Probability of an impulse */
+                        0.0,
+                    },
+                    {
+                        0.5,
+                        0.0
+                    }
+                },
+                1.0,                /*! Impulse height, based on MTU and bit rate */
+                0.0,                /*! Impulse decay coefficient */
+                0.001,              /*! Probability of packet loss due to occupancy. */
+                0.15                /*! Probability of packet loss due to a multiple access collision. */
+            },
+            {   /* Side A access link */
+                {
+                    0.0002,         /*! Probability of loss rate change low->high */
+                    0.2             /*! Probability of loss rate change high->low */
+                },
+                {
+                    {
+                        0.001,      /*! Probability of an impulse */
+                        0.0,
+                    },
+                    {
+                        0.3,
+                        0.4
+                    }
+                },
+                40.0,               /*! Impulse height, based on MTU and bit rate */
+                0.75,               /*! Impulse decay coefficient */
+                0.0005,             /*! Probability of packet loss due to occupancy. */
+                0.0                 /*! Probability of packet loss due to a multiple access collision. */
+            },
+            {   /* Side B access link */
+                {
+                    0.0002,         /*! Probability of loss rate change low->high */
+                    0.2             /*! Probability of loss rate change high->low */
+                },
+                {
+                    {
+                        0.001,      /*! Probability of an impulse */
+                        0.0,
+                    },
+                    {
+                        0.3,
+                        0.4
+                    }
+                },
+                40.0,               /*! Impulse height, based on MTU and bit rate */
+                0.75,               /*! Impulse decay coefficient */
+                0.0005,             /*! Probability of packet loss due to occupancy. */
+                0.0                 /*! Probability of packet loss due to a multiple access collision. */
+            },
+            {   /* Side B LAN */
+                {
+                    0.004,          /*! Probability of loss rate change low->high */
+                    0.1             /*! Probability of loss rate change high->low */
+                },
+                {
+                    {
+                        0.0,        /*! Probability of an impulse */
+                        0.0,
+                    },
+                    {
+                        0.5,
+                        0.0
+                    }
+                },
+                1.0,                /*! Impulse height, based on MTU and bit rate */
+                0.0,                /*! Impulse decay coefficient */
+                0.001,              /*! Probability of packet loss due to occupancy. */
+                0.15                /*! Probability of packet loss due to a multiple access collision. */
+            }
+        }
+    }
+};
+
+g1050_channel_speeds_t g1050_speed_patterns[133] =
+{
+    {  4000000, 0,    128000,   768000, 0,     4000000, 0,    128000,   768000, 0,  2.2500},
+    {  4000000, 0,    128000,   768000, 0,    20000000, 0,    128000,   768000, 0,  1.5000},
+    { 20000000, 0,    128000,   768000, 0,    20000000, 0,    128000,   768000, 0,  0.2500},
+    {  4000000, 0,    128000,  1536000, 0,     4000000, 0,    384000,   768000, 0,  3.4125},
+    {  4000000, 0,    128000,  1536000, 0,    20000000, 0,    384000,   768000, 0,  2.6750},
+    { 20000000, 0,    128000,  1536000, 0,    20000000, 0,    384000,   768000, 0,  0.5125},
+    {  4000000, 0,    128000,  3000000, 0,     4000000, 0,    384000,   768000, 0,  0.7875},
+    {  4000000, 0,    128000,  3000000, 0,    20000000, 0,    384000,   768000, 0,  0.6750},
+    { 20000000, 0,    128000,  3000000, 0,    20000000, 0,    384000,   768000, 0,  0.1375},
+    {  4000000, 0,    384000,   768000, 0,     4000000, 0,    128000,  1536000, 0,  3.4125},
+    {  4000000, 0,    384000,   768000, 0,    20000000, 0,    128000,  1536000, 0,  2.6750},
+    { 20000000, 0,    384000,   768000, 0,    20000000, 0,    128000,  1536000, 0,  0.5125},
+    {  4000000, 0,    384000,  1536000, 0,     4000000, 0,    384000,  1536000, 0,  5.1756},
+    {  4000000, 0,    384000,  1536000, 0,    20000000, 0,    384000,  1536000, 0,  4.6638},
+    { 20000000, 0,    384000,  1536000, 0,    20000000, 0,    384000,  1536000, 0,  1.0506},
+    {  4000000, 0,    384000,  3000000, 0,     4000000, 0,    384000,  1536000, 0,  1.1944},
+    {  4000000, 0,    384000,  3000000, 0,    20000000, 0,    384000,  1536000, 0,  1.1638},
+    { 20000000, 0,    384000,  3000000, 0,    20000000, 0,    384000,  1536000, 0,  0.2819},
+    {  4000000, 0,    384000,   768000, 0,     4000000, 0,    128000,  3000000, 0,  0.7875},
+    {  4000000, 0,    384000,   768000, 0,    20000000, 0,    128000,  3000000, 0,  0.6750},
+    { 20000000, 0,    384000,   768000, 0,    20000000, 0,    128000,  3000000, 0,  0.1375},
+    {  4000000, 0,    384000,  1536000, 0,     4000000, 0,    384000,  3000000, 0,  1.1944},
+    {  4000000, 0,    384000,  1536000, 0,    20000000, 0,    384000,  3000000, 0,  1.1638},
+    { 20000000, 0,    384000,  1536000, 0,    20000000, 0,    384000,  3000000, 0,  0.2819},
+    {  4000000, 0,    384000,  3000000, 0,     4000000, 0,    384000,  3000000, 0,  0.2756},
+    {  4000000, 0,    384000,  3000000, 0,    20000000, 0,    384000,  3000000, 0,  0.2888},
+    { 20000000, 0,    384000,  3000000, 0,    20000000, 0,    384000,  3000000, 0,  0.0756},
+    {  4000000, 0,    128000,  1536000, 0,   100000000, 0,    384000,   768000, 0,  1.8000},
+    { 20000000, 0,    128000,  1536000, 0,   100000000, 0,    384000,   768000, 0,  0.6000},
+    {  4000000, 0,    128000,  3000000, 0,   100000000, 0,    384000,   768000, 0,  0.6750},
+    { 20000000, 0,    128000,  3000000, 0,   100000000, 0,    384000,   768000, 0,  0.2250},
+    {  4000000, 0,    128000,  1536000, 0,     4000000, 0,    768000,  1536000, 0,  0.6000},
+    {  4000000, 0,    128000,  1536000, 0,    20000000, 0,    768000,  1536000, 0,  0.8000},
+    {  4000000, 0,    128000,  1536000, 0,   100000000, 0,    768000,  1536000, 0,  1.8000},
+    { 20000000, 0,    128000,  1536000, 0,    20000000, 0,    768000,  1536000, 0,  0.2000},
+    { 20000000, 0,    128000,  1536000, 0,   100000000, 0,    768000,  1536000, 0,  0.6000},
+    {  4000000, 0,    128000, 43000000, 0,     4000000, 0,    768000, 43000000, 0,  0.0750},
+    {  4000000, 0,    128000, 43000000, 0,    20000000, 0,    768000, 43000000, 0,  0.1000},
+    {  4000000, 0,    128000, 43000000, 0,   100000000, 0,    768000, 43000000, 0,  0.2250},
+    { 20000000, 0,    128000, 43000000, 0,    20000000, 0,    768000, 43000000, 0,  0.0250},
+    { 20000000, 0,    128000, 43000000, 0,   100000000, 0,    768000, 43000000, 0,  0.0750},
+    {  4000000, 0,    384000,  1536000, 0,   100000000, 0,    384000,  1536000, 0,  5.4600},
+    { 20000000, 0,    384000,  1536000, 0,   100000000, 0,    384000,  1536000, 0,  2.4600},
+    {  4000000, 0,    384000,  3000000, 0,   100000000, 0,    384000,  1536000, 0,  1.6538},
+    { 20000000, 0,    384000,  3000000, 0,   100000000, 0,    384000,  1536000, 0,  0.7913},
+    {  4000000, 0,    384000,  1536000, 0,     4000000, 0,   1536000,  1536000, 0,  0.9100},
+    {  4000000, 0,    384000,  1536000, 0,    20000000, 0,   1536000,  1536000, 0,  1.3200},
+    {  4000000, 0,    384000,  1536000, 0,   100000000, 0,   1536000,  1536000, 0,  3.2100},
+    { 20000000, 0,    384000,  1536000, 0,    20000000, 0,   1536000,  1536000, 0,  0.4100},
+    { 20000000, 0,    384000,  1536000, 0,   100000000, 0,   1536000,  1536000, 0,  1.7100},
+    {  4000000, 0,    384000, 43000000, 0,     4000000, 0,   1536000, 43000000, 0,  0.1138},
+    {  4000000, 0,    384000, 43000000, 0,    20000000, 0,   1536000, 43000000, 0,  0.1650},
+    {  4000000, 0,    384000, 43000000, 0,   100000000, 0,   1536000, 43000000, 0,  0.4013},
+    { 20000000, 0,    384000, 43000000, 0,    20000000, 0,   1536000, 43000000, 0,  0.0513},
+    { 20000000, 0,    384000, 43000000, 0,   100000000, 0,   1536000, 43000000, 0,  0.2138},
+    {  4000000, 0,    384000,  1536000, 0,   100000000, 0,    384000,  3000000, 0,  1.6538},
+    { 20000000, 0,    384000,  1536000, 0,   100000000, 0,    384000,  3000000, 0,  0.7913},
+    {  4000000, 0,    384000,  3000000, 0,   100000000, 0,    384000,  3000000, 0,  0.4725},
+    { 20000000, 0,    384000,  3000000, 0,   100000000, 0,    384000,  3000000, 0,  0.2475},
+    {  4000000, 0,    384000,  1536000, 0,     4000000, 0,   1536000,  3000000, 0,  0.2100},
+    {  4000000, 0,    384000,  1536000, 0,    20000000, 0,   1536000,  3000000, 0,  0.3200},
+    {  4000000, 0,    384000,  1536000, 0,   100000000, 0,   1536000,  3000000, 0,  0.8100},
+    { 20000000, 0,    384000,  1536000, 0,    20000000, 0,   1536000,  3000000, 0,  0.1100},
+    { 20000000, 0,    384000,  1536000, 0,   100000000, 0,   1536000,  3000000, 0,  0.5100},
+    {  4000000, 0,    384000, 43000000, 0,     4000000, 0,   3000000, 43000000, 0,  0.0263},
+    {  4000000, 0,    384000, 43000000, 0,    20000000, 0,   3000000, 43000000, 0,  0.0400},
+    {  4000000, 0,    384000, 43000000, 0,   100000000, 0,   3000000, 43000000, 0,  0.1013},
+    { 20000000, 0,    384000, 43000000, 0,    20000000, 0,   3000000, 43000000, 0,  0.0138},
+    { 20000000, 0,    384000, 43000000, 0,   100000000, 0,   3000000, 43000000, 0,  0.0638},
+    {  4000000, 0,    384000,   768000, 0,   100000000, 0,    128000,  1536000, 0,  1.8000},
+    { 20000000, 0,    384000,   768000, 0,   100000000, 0,    128000,  1536000, 0,  0.6000},
+    {  4000000, 0,    384000,   768000, 0,   100000000, 0,    128000,  3000000, 0,  0.6750},
+    { 20000000, 0,    384000,   768000, 0,   100000000, 0,    128000,  3000000, 0,  0.2250},
+    {  4000000, 0,    768000,  1536000, 0,     4000000, 0,    128000,  1536000, 0,  0.6000},
+    {  4000000, 0,    768000,  1536000, 0,    20000000, 0,    128000,  1536000, 0,  0.8000},
+    { 20000000, 0,    768000,  1536000, 0,    20000000, 0,    128000,  1536000, 0,  0.2000},
+    {  4000000, 0,    768000,  1536000, 0,   100000000, 0,    128000,  1536000, 0,  1.8000},
+    { 20000000, 0,    768000,  1536000, 0,   100000000, 0,    128000,  1536000, 0,  0.6000},
+    {  4000000, 0,   1536000,  1536000, 0,     4000000, 0,    384000,  1536000, 0,  0.9100},
+    {  4000000, 0,   1536000,  1536000, 0,    20000000, 0,    384000,  1536000, 0,  1.3200},
+    { 20000000, 0,   1536000,  1536000, 0,    20000000, 0,    384000,  1536000, 0,  0.4100},
+    {  4000000, 0,   1536000,  1536000, 0,   100000000, 0,    384000,  1536000, 0,  3.2100},
+    { 20000000, 0,   1536000,  1536000, 0,   100000000, 0,    384000,  1536000, 0,  1.7100},
+    {  4000000, 0,   1536000,  3000000, 0,     4000000, 0,    384000,  1536000, 0,  0.2100},
+    {  4000000, 0,   1536000,  3000000, 0,    20000000, 0,    384000,  1536000, 0,  0.3200},
+    { 20000000, 0,   1536000,  3000000, 0,    20000000, 0,    384000,  1536000, 0,  0.1100},
+    {  4000000, 0,   1536000,  3000000, 0,   100000000, 0,    384000,  1536000, 0,  0.8100},
+    { 20000000, 0,   1536000,  3000000, 0,   100000000, 0,    384000,  1536000, 0,  0.5100},
+    {  4000000, 0,    768000, 43000000, 0,     4000000, 0,    128000, 43000000, 0,  0.0750},
+    {  4000000, 0,    768000, 43000000, 0,    20000000, 0,    128000, 43000000, 0,  0.1000},
+    { 20000000, 0,    768000, 43000000, 0,    20000000, 0,    128000, 43000000, 0,  0.0250},
+    {  4000000, 0,    768000, 43000000, 0,   100000000, 0,    128000, 43000000, 0,  0.2250},
+    { 20000000, 0,    768000, 43000000, 0,   100000000, 0,    128000, 43000000, 0,  0.0750},
+    {  4000000, 0,   1536000, 43000000, 0,     4000000, 0,    384000, 43000000, 0,  0.1138},
+    {  4000000, 0,   1536000, 43000000, 0,    20000000, 0,    384000, 43000000, 0,  0.1650},
+    { 20000000, 0,   1536000, 43000000, 0,    20000000, 0,    384000, 43000000, 0,  0.0513},
+    {  4000000, 0,   1536000, 43000000, 0,   100000000, 0,    384000, 43000000, 0,  0.4013},
+    { 20000000, 0,   1536000, 43000000, 0,   100000000, 0,    384000, 43000000, 0,  0.2138},
+    {  4000000, 0,   3000000, 43000000, 0,     4000000, 0,    384000, 43000000, 0,  0.0263},
+    {  4000000, 0,   3000000, 43000000, 0,    20000000, 0,    384000, 43000000, 0,  0.0400},
+    { 20000000, 0,   3000000, 43000000, 0,    20000000, 0,    384000, 43000000, 0,  0.0138},
+    {  4000000, 0,   3000000, 43000000, 0,   100000000, 0,    384000, 43000000, 0,  0.1013},
+    { 20000000, 0,   3000000, 43000000, 0,   100000000, 0,    384000, 43000000, 0,  0.0638},
+    {100000000, 0,    384000,  1536000, 0,   100000000, 0,    384000,  1536000, 0,  1.4400},
+    {100000000, 0,    384000,  3000000, 0,   100000000, 0,    384000,  1536000, 0,  0.5400},
+    {100000000, 0,    384000,  1536000, 0,   100000000, 0,   1536000,  1536000, 0,  1.4400},
+    {100000000, 0,    384000, 43000000, 0,   100000000, 0,   1536000, 43000000, 0,  0.1800},
+    {100000000, 0,    384000,  1536000, 0,   100000000, 0,    384000,  3000000, 0,  0.5400},
+    {100000000, 0,    384000,  3000000, 0,   100000000, 0,    384000,  3000000, 0,  0.2025},
+    {100000000, 0,    384000,  1536000, 0,   100000000, 0,   1536000,  3000000, 0,  0.5400},
+    {100000000, 0,    384000, 43000000, 0,   100000000, 0,   3000000, 43000000, 0,  0.0675},
+    {100000000, 0,   1536000,  1536000, 0,   100000000, 0,    384000,  1536000, 0,  1.4400},
+    {100000000, 0,   1536000,  3000000, 0,   100000000, 0,    384000,  1536000, 0,  0.5400},
+    {  4000000, 0,   1536000,  1536000, 0,     4000000, 0,   1536000,  1536000, 0,  0.1600},
+    {  4000000, 0,   1536000,  1536000, 0,    20000000, 0,   1536000,  1536000, 0,  0.3200},
+    {  4000000, 0,   1536000,  1536000, 0,   100000000, 0,   1536000,  1536000, 0,  0.9600},
+    { 20000000, 0,   1536000,  1536000, 0,    20000000, 0,   1536000,  1536000, 0,  0.1600},
+    { 20000000, 0,   1536000,  1536000, 0,   100000000, 0,   1536000,  1536000, 0,  0.9600},
+    {100000000, 0,   1536000,  1536000, 0,   100000000, 0,   1536000,  1536000, 0,  1.4400},
+    {  4000000, 0,   1536000, 43000000, 0,     4000000, 0,   1536000, 43000000, 0,  0.0400},
+    {  4000000, 0,   1536000, 43000000, 0,    20000000, 0,   1536000, 43000000, 0,  0.0800},
+    {  4000000, 0,   1536000, 43000000, 0,   100000000, 0,   1536000, 43000000, 0,  0.2400},
+    { 20000000, 0,   1536000, 43000000, 0,    20000000, 0,   1536000, 43000000, 0,  0.0400},
+    { 20000000, 0,   1536000, 43000000, 0,   100000000, 0,   1536000, 43000000, 0,  0.2400},
+    {100000000, 0,   1536000, 43000000, 0,   100000000, 0,   1536000, 43000000, 0,  0.3600},
+    {100000000, 0,   1536000, 43000000, 0,   100000000, 0,    384000, 43000000, 0,  0.1800},
+    {100000000, 0,   3000000, 43000000, 0,   100000000, 0,    384000, 43000000, 0,  0.0675},
+    {  4000000, 0,  43000000, 43000000, 0,     4000000, 0,  43000000, 43000000, 0,  0.0025},
+    {  4000000, 0,  43000000, 43000000, 0,    20000000, 0,  43000000, 43000000, 0,  0.0050},
+    {  4000000, 0,  43000000, 43000000, 0,   100000000, 0,  43000000, 43000000, 0,  0.0150},
+    { 20000000, 0,  43000000, 43000000, 0,    20000000, 0,  43000000, 43000000, 0,  0.0025},
+    { 20000000, 0,  43000000, 43000000, 0,   100000000, 0,  43000000, 43000000, 0,  0.0150},
+    {100000000, 0,  43000000, 43000000, 0,   100000000, 0,  43000000, 43000000, 0,  0.0225}
+};
+
+g1050_model_t g1050_standard_models[9] =
+{
+    {   /* Severity 0 - no impairment */
+        {
+            0,          /*! Percentage likelihood of occurance in scenario A */
+            0,          /*! Percentage likelihood of occurance in scenario B */
+            0,          /*! Percentage likelihood of occurance in scenario C */
+        },
+        {
+            0.0,        /*! Percentage occupancy */
+            1508,       /*! MTU */
+            0.0         /*! Peak jitter */
+        },
+        {
+            0.0,        /*! Percentage occupancy */
+            512,        /*! MTU */
+            0.0         /*! Peak jitter */
+        },
+        {
+            0.0,        /*! Basic delay of the regional backbone, in seconds */
+            0.0,        /*! Basic delay of the intercontinental backbone, in seconds */
+            0.0,        /*! Percentage packet loss of the backbone */
+            0.0,        /*! Maximum jitter of the backbone, in seconds */
+            0.0,        /*! Interval between the backbone route flapping between two paths, in seconds */
+            0.0,        /*! The difference in backbone delay between the two routes we flap between, in seconds */
+            0.0,        /*! The interval between link failures, in seconds */
+            0.0,        /*! The duration of link failures, in seconds */
+            0.0,        /*! Probability of packet loss in the backbone, in percent */
+            0.0         /*! Probability of a packet going out of sequence in the backbone. */
+        },
+        {
+            0.0,        /*! Percentage occupancy */
+            512,        /*! MTU */
+            0.0         /*! Peak jitter */
+        },
+        {
+            0.0,        /*! Percentage occupancy */
+            1508,       /*! MTU */
+            0.0         /*! Peak jitter */
+        }
+    },
+    {   /* Severity A */
+        {
+            50,         /*! Percentage likelihood of occurance in scenario A */
+            5,          /*! Percentage likelihood of occurance in scenario B */
+            5,          /*! Percentage likelihood of occurance in scenario C */
+        },
+        {
+            1.0,        /*! Percentage occupancy */
+            1508,       /*! MTU */
+            0.0015      /*! Peak jitter */
+        },
+        {
+            0.0,        /*! Percentage occupancy */
+            512,        /*! MTU */
+            0.0         /*! Peak jitter */
+        },
+        {
+            0.004,      /*! Basic delay of the regional backbone, in seconds */
+            0.016,      /*! Basic delay of the intercontinental backbone, in seconds */
+            0.0,        /*! Percentage packet loss of the backbone */
+            0.005,      /*! Maximum jitter of the backbone, in seconds */
+            0.0,        /*! Interval between the backbone route flapping between two paths, in seconds */
+            0.0,        /*! The difference in backbone delay between the two routes we flap between, in seconds */
+            0.0,        /*! The interval between link failures, in seconds */
+            0.0,        /*! The duration of link failures, in seconds */
+            0.0,        /*! Probability of packet loss in the backbone, in percent */
+            0.0         /*! Probability of a packet going out of sequence in the backbone. */
+        },
+        {
+            0.0,        /*! Percentage occupancy */
+            512,        /*! MTU */
+            0.0         /*! Peak jitter */
+        },
+        {
+            1.0,        /*! Percentage occupancy */
+            1508,       /*! MTU */
+            0.0015      /*! Peak jitter */
+        }
+    },
+    {   /* Severity B */
+        {
+            30,         /*! Percentage likelihood of occurance in scenario A */
+            25,         /*! Percentage likelihood of occurance in scenario B */
+            5,          /*! Percentage likelihood of occurance in scenario C */
+        },
+        {
+            2.0,        /*! Percentage occupancy */
+            1508,       /*! MTU */
+            0.0015      /*! Peak jitter */
+        },
+        {
+            1.0,        /*! Percentage occupancy */
+            512,        /*! MTU */
+            0.0         /*! Peak jitter */
+        },
+        {
+            0.008,      /*! Basic delay of the regional backbone, in seconds */
+            0.032,      /*! Basic delay of the intercontinental backbone, in seconds */
+            0.01,       /*! Percentage packet loss of the backbone */
+            0.01,       /*! Maximum jitter of the backbone, in seconds */
+            3600.0,     /*! Interval between the backbone route flapping between two paths, in seconds */
+            0.002,      /*! The difference in backbone delay between the two routes we flap between, in seconds */
+            3600.0,     /*! The interval between link failures, in seconds */
+            0.064,      /*! The duration of link failures, in seconds */
+            0.0,        /*! Probability of packet loss in the backbone, in percent */
+            0.0         /*! Probability of a packet going out of sequence in the backbone. */
+        },
+        {
+            1.0,        /*! Percentage occupancy */
+            512,        /*! MTU */
+            0.0         /*! Peak jitter */
+        },
+        {
+            2.0,        /*! Percentage occupancy */
+            1508,       /*! MTU */
+            0.0015      /*! Peak jitter */
+        }
+    },
+    {   /* Severity C */
+        {
+            15,         /*! Percentage likelihood of occurance in scenario A */
+            30,         /*! Percentage likelihood of occurance in scenario B */
+            10,         /*! Percentage likelihood of occurance in scenario C */
+        },
+        {
+            3.0,        /*! Percentage occupancy */
+            1508,       /*! MTU */
+            0.0015      /*! Peak jitter */
+        },
+        {
+            2.0,        /*! Percentage occupancy */
+            1508,       /*! MTU */
+            0.0         /*! Peak jitter */
+        },
+        {
+            0.016,      /*! Basic delay of the regional backbone, in seconds */
+            0.064,      /*! Basic delay of the intercontinental backbone, in seconds */
+            0.02,       /*! Percentage packet loss of the backbone */
+            0.016,      /*! Maximum jitter of the backbone, in seconds */
+            1800.0,     /*! Interval between the backbone route flapping between two paths, in seconds */
+            0.004,      /*! The difference in backbone delay between the two routes we flap between, in seconds */
+            1800.0,     /*! The interval between link failures, in seconds */
+            0.128,      /*! The duration of link failures, in seconds */
+            0.0,        /*! Probability of packet loss in the backbone, in percent */
+            0.0         /*! Probability of a packet going out of sequence in the backbone. */
+        },
+        {
+            2.0,        /*! Percentage occupancy */
+            1508,       /*! MTU */
+            0.0         /*! Peak jitter */
+        },
+        {
+            3.0,        /*! Percentage occupancy */
+            1508,       /*! MTU */
+            0.0015      /*! Peak jitter */
+        }
+    },
+    {   /* Severity D */
+        {
+            5,          /*! Percentage likelihood of occurance in scenario A */
+            25,         /*! Percentage likelihood of occurance in scenario B */
+            15,         /*! Percentage likelihood of occurance in scenario C */
+        },
+        {
+            5.0,         /*! Percentage occupancy */
+            1508,       /*! MTU */
+            0.0015      /*! Peak jitter */
+        },
+        {
+            4.0,        /*! Percentage occupancy */
+            1508,       /*! MTU */
+            0.0         /*! Peak jitter */
+        },
+        {
+            0.032,      /*! Basic delay of the regional backbone, in seconds */
+            0.128,      /*! Basic delay of the intercontinental backbone, in seconds */
+            0.04,       /*! Percentage packet loss of the backbone */
+            0.04,       /*! Maximum jitter of the backbone, in seconds */
+            900.0,      /*! Interval between the backbone route flapping between two paths, in seconds */
+            0.008,      /*! The difference in backbone delay between the two routes we flap between, in seconds */
+            900.0,      /*! The interval between link failures, in seconds */
+            0.256,      /*! The duration of link failures, in seconds */
+            0.0,        /*! Probability of packet loss in the backbone, in percent */
+            0.0         /*! Probability of a packet going out of sequence in the backbone. */
+        },
+        {
+            4.0,        /*! Percentage occupancy */
+            1508,       /*! MTU */
+            0.0         /*! Peak jitter */
+        },
+        {
+            5.0,        /*! Percentage occupancy */
+            1508,       /*! MTU */
+            0.0015      /*! Peak jitter */
+        }
+    },
+    {   /* Severity E */
+        {
+            0,          /*! Percentage likelihood of occurance in scenario A */
+            10,         /*! Percentage likelihood of occurance in scenario B */
+            20,         /*! Percentage likelihood of occurance in scenario C */
+        },
+        {
+            8.0,        /*! Percentage occupancy */
+            1508,       /*! MTU */
+            0.0015      /*! Peak jitter */
+        },
+        {
+            8.0,        /*! Percentage occupancy */
+            1508,       /*! MTU */
+            0.0         /*! Peak jitter */
+        },
+        {
+            0.064,      /*! Basic delay of the regional backbone, in seconds */
+            0.196,      /*! Basic delay of the intercontinental backbone, in seconds */
+            0.1,        /*! Percentage packet loss of the backbone */
+            0.07,       /*! Maximum jitter of the backbone, in seconds */
+            480.0,      /*! Interval between the backbone route flapping between two paths, in seconds */
+            0.016,      /*! The difference in backbone delay between the two routes we flap between, in seconds */
+            480.0,      /*! The interval between link failures, in seconds */
+            0.4,        /*! The duration of link failures, in seconds */
+            0.0,        /*! Probability of packet loss in the backbone, in percent */
+            0.0         /*! Probability of a packet going out of sequence in the backbone. */
+        },
+        {
+            8.0,        /*! Percentage occupancy */
+            1508,       /*! MTU */
+            0.0         /*! Peak jitter */
+        },
+        {
+            8.0,        /*! Percentage occupancy */
+            1508,       /*! MTU */
+            0.0015      /*! Peak jitter */
+        }
+    },
+    {   /* Severity F */
+        {
+            0,          /*! Percentage likelihood of occurance in scenario A */
+            0,          /*! Percentage likelihood of occurance in scenario B */
+            25,         /*! Percentage likelihood of occurance in scenario C */
+        },
+        {
+            12.0,       /*! Percentage occupancy */
+            1508,       /*! MTU */
+            0.0015      /*! Peak jitter */
+        },
+        {
+            15.0,       /*! Percentage occupancy */
+            1508,       /*! MTU */
+            0.0         /*! Peak jitter */
+        },
+        {
+            0.128,      /*! Basic delay of the regional backbone, in seconds */
+            0.256,      /*! Basic delay of the intercontinental backbone, in seconds */
+            0.2,        /*! Percentage packet loss of the backbone */
+            0.1,        /*! Maximum jitter of the backbone, in seconds */
+            240.0,      /*! Interval between the backbone route flapping between two paths, in seconds */
+            0.032,      /*! The difference in backbone delay between the two routes we flap between, in seconds */
+            240.0,      /*! The interval between link failures, in seconds */
+            0.8,        /*! The duration of link failures, in seconds */
+            0.0,        /*! Probability of packet loss in the backbone, in percent */
+            0.0         /*! Probability of a packet going out of sequence in the backbone. */
+        },
+        {
+            15.0,       /*! Percentage occupancy */
+            1508,       /*! MTU */
+            0.0         /*! Peak jitter */
+        },
+        {
+            12.0,       /*! Percentage occupancy */
+            1508,       /*! MTU */
+            0.0015      /*! Peak jitter */
+        }
+    },
+    {   /* Severity G */
+        {
+            0,          /*! Percentage likelihood of occurance in scenario A */
+            0,          /*! Percentage likelihood of occurance in scenario B */
+            15,         /*! Percentage likelihood of occurance in scenario C */
+        },
+        {
+            16.0,       /*! Percentage occupancy */
+            1508,       /*! MTU */
+            0.0015      /*! Peak jitter */
+        },
+        {
+            30.0,       /*! Percentage occupancy */
+            1508,       /*! MTU */
+            0.0         /*! Peak jitter */
+        },
+        {
+            0.256,      /*! Basic delay of the regional backbone, in seconds */
+            0.512,      /*! Basic delay of the intercontinental backbone, in seconds */
+            0.5,        /*! Percentage packet loss of the backbone */
+            0.15,       /*! Maximum jitter of the backbone, in seconds */
+            120.0,      /*! Interval between the backbone route flapping between two paths, in seconds */
+            0.064,      /*! The difference in backbone delay between the two routes we flap between, in seconds */
+            120.0,      /*! The interval between link failures, in seconds */
+            1.6,        /*! The duration of link failures, in seconds */
+            0.0,        /*! Probability of packet loss in the backbone, in percent */
+            0.0         /*! Probability of a packet going out of sequence in the backbone. */
+        },
+        {
+            30.0,       /*! Percentage occupancy */
+            1508,       /*! MTU */
+            0.0         /*! Peak jitter */
+        },
+        {
+            16.0,       /*! Percentage occupancy */
+            1508,       /*! MTU */
+            0.0015      /*! Peak jitter */
+        }
+    },
+    {   /* Severity H */
+        {
+            0,          /*! Percentage likelihood of occurance in scenario A */
+            0,          /*! Percentage likelihood of occurance in scenario B */
+            5,          /*! Percentage likelihood of occurance in scenario C */
+        },
+        {
+            20.0,       /*! Percentage occupancy */
+            1508,       /*! MTU */
+            0.0015      /*! Peak jitter */
+        },
+        {
+            50.0,       /*! Percentage occupancy */
+            1508,       /*! MTU */
+            0.0         /*! Peak jitter */
+        },
+        {
+            0.512,      /*! Basic delay of the regional backbone, in seconds */
+            0.768,      /*! Basic delay of the intercontinental backbone, in seconds */
+            1.0,        /*! Percentage packet loss of the backbone */
+            0.5,        /*! Maximum jitter of the backbone, in seconds */
+            60.0,       /*! Interval between the backbone route flapping between two paths, in seconds */
+            0.128,      /*! The difference in backbone delay between the two routes we flap between, in seconds */
+            60.0,       /*! The interval between link failures, in seconds */
+            3.0,        /*! The duration of link failures, in seconds */
+            1.0,        /*! Probability of packet loss in the backbone, in percent */
+            1.0         /*! Probability of a packet going out of sequence in the backbone. */
+        },
+        {
+            50.0,       /*! Percentage occupancy */
+            1508,       /*! MTU */
+            0.0         /*! Peak jitter */
+        },
+        {
+            20.0,       /*! Percentage occupancy */
+            1508,       /*! MTU */
+            0.0015      /*! Peak jitter */
+        }
+    }
+};
+
+static __inline__ double q1050_rand(void)
+{
+    return drand48();
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ double scale_probability(double prob, double scale)
+{
+    /* Re-calculate probability based on a different time interval */
+    return 1.0 - pow(1.0 - prob, scale);
+}
+/*- End of function --------------------------------------------------------*/
+
+static void g1050_segment_init(g1050_segment_state_t *s,
+                               int link_type,
+                               g1050_segment_constants_t *constants,
+                               g1050_segment_model_t *parms,
+                               int bit_rate,
+                               int multiple_access,
+                               int qos_enabled,
+                               int packet_size,
+                               int packet_rate)
+{
+    double x;
+    double packet_interval;
+
+    memset(s, 0, sizeof(*s));
+
+    packet_interval = 1000.0/packet_rate;
+    /* Some calculatons are common to both LAN and access links, and those that are not. */
+    s->link_type = link_type;
+    s->prob_loss_rate_change[0] = scale_probability(constants->prob_loss_rate_change[0]*parms->percentage_occupancy, 1.0/packet_interval);
+
+    s->serial_delay = packet_size*8.0/bit_rate;
+    if (link_type == G1050_LAN_LINK)
+    {
+        s->prob_loss_rate_change[1] = scale_probability(constants->prob_loss_rate_change[1], 1.0/packet_interval);
+        s->prob_impulse[0] = constants->prob_impulse[0][0];
+        s->prob_impulse[1] = constants->prob_impulse[1][0];
+        s->impulse_coeff = constants->impulse_coeff;
+        s->impulse_height = parms->mtu*(8.0/bit_rate)*(1.0 + parms->percentage_occupancy/constants->impulse_height);
+    }
+    else if (link_type == G1050_ACCESS_LINK)
+    {
+        s->prob_loss_rate_change[1] = scale_probability(constants->prob_loss_rate_change[1]/(1.0 + parms->percentage_occupancy), 1.0/packet_interval);
+        s->prob_impulse[0] = scale_probability(constants->prob_impulse[0][0] + (parms->percentage_occupancy/2000.0), 1.0/packet_interval);
+        s->prob_impulse[1] = scale_probability(constants->prob_impulse[1][0] + (constants->prob_impulse[1][1]*parms->percentage_occupancy/100.0), 1.0/packet_interval);
+        s->impulse_coeff = 1.0 - scale_probability(1.0 - constants->impulse_coeff, 1.0/packet_interval);
+        x = (1.0 - constants->impulse_coeff)/(1.0 - s->impulse_coeff);
+        s->impulse_height = x*parms->mtu*(8.0/bit_rate)*(1.0 + parms->percentage_occupancy/constants->impulse_height);
+    }
+
+    /* The following are calculated the same way for LAN and access links */
+    s->prob_packet_loss = constants->prob_packet_loss*parms->percentage_occupancy;
+    s->qos_enabled = qos_enabled;
+    s->multiple_access = multiple_access;
+    s->prob_packet_collision_loss = constants->prob_packet_collision_loss;
+    s->max_jitter = parms->max_jitter;
+
+    /* The following is common state information to all links. */
+    s->high_loss = FALSE;
+    s->congestion_delay = 0.0;
+    s->last_arrival_time = 0.0;
+
+    /* Count of packets lost in this segment. */
+    s->lost_packets = 0;
+    s->lost_packets_2 = 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void g1050_core_init(g1050_core_state_t *s, g1050_core_model_t *parms, int packet_rate)
+{
+    memset(s, 0, sizeof(*s));
+
+    /* Set up route flapping. */
+    /* This is the length of the period of both the delayed duration and the non-delayed. */
+    s->route_flap_interval = parms->route_flap_interval*G1050_TICKS_PER_SEC;
+
+    /* How much additional delay is added or subtracted during route flaps. */
+    s->route_flap_delta = parms->route_flap_delay;
+
+    /* Current tick count. This is initialized so that we are part way into the first
+       CLEAN interval before the first change occurs. This is a random portion of the
+       period. When we reach the first flap, the flapping in both directions becomes
+       periodic. */
+    s->route_flap_counter = s->route_flap_interval - 99 - floor(s->route_flap_interval*q1050_rand());
+    s->link_failure_interval_ticks = parms->link_failure_interval*G1050_TICKS_PER_SEC;
+
+    /* Link failures occur when the count reaches this number of ticks. */
+    /* Duration of a failure. */
+    s->link_failure_duration_ticks = floor((G1050_TICKS_PER_SEC*parms->link_failure_duration));
+    /* How far into the first CLEAN interval we are. This is like the route flap initialzation. */
+    s->link_failure_counter = s->link_failure_interval_ticks - 99 - floor(s->link_failure_interval_ticks*q1050_rand());
+    s->link_recovery_counter = s->link_failure_duration_ticks;
+    
+    s->base_delay = parms->base_regional_delay;
+    s->max_jitter = parms->max_jitter;
+    s->prob_packet_loss = parms->prob_packet_loss/100.0;
+    s->prob_oos = parms->prob_oos/100.0;
+    s->last_arrival_time = 0.0;
+    s->delay_delta = 0;
+
+    /* Count of packets lost in this segment. */
+    s->lost_packets = 0;
+    s->lost_packets_2 = 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void g1050_segment_model(g1050_segment_state_t *s, double delays[], int len)
+{
+    int i;
+    int lose;
+    int was_high_loss;
+    double impulse;
+    double slice_delay;
+
+    /* Compute delay and loss value for each time slice. */
+    for (i = 0;  i < len;  i++)
+    {
+        lose = FALSE;
+        /* Initialize delay to the serial delay plus some jitter. */
+        slice_delay = s->serial_delay + s->max_jitter*q1050_rand();
+        /* If no QoS, do congestion delay and packet loss analysis. */
+        if (!s->qos_enabled)
+        {
+            /* To match the logic in G.1050 we need to record the current loss state, before
+               checking if we should change. */
+            was_high_loss = s->high_loss;
+            /* Toggle between the low-loss and high-loss states, based on the transition probability. */
+            if (q1050_rand() < s->prob_loss_rate_change[was_high_loss])
+                s->high_loss = !s->high_loss;
+            impulse = 0.0;
+            if (q1050_rand() < s->prob_impulse[was_high_loss])
+            {
+                impulse = s->impulse_height;
+                if (!was_high_loss  ||  s->link_type == G1050_LAN_LINK)
+                    impulse *= q1050_rand();
+            }
+
+            if (was_high_loss  &&  q1050_rand() < s->prob_packet_loss)
+                lose = TRUE;
+            /* Single pole LPF for the congestion delay impulses. */
+            s->congestion_delay = s->congestion_delay*s->impulse_coeff + impulse*(1.0 - s->impulse_coeff);
+            slice_delay += s->congestion_delay;
+        }
+        /* If duplex mismatch on LAN, packet loss based on loss probability. */
+        if (s->multiple_access  &&  (q1050_rand() < s->prob_packet_collision_loss))
+            lose = TRUE;
+        /* Put computed delay into time slice array. */
+        if (lose)
+        {
+            delays[i] = PACKET_LOSS_TIME;
+            s->lost_packets++;
+        }
+        else
+        {
+            delays[i] = slice_delay;
+        }
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static void g1050_core_model(g1050_core_state_t *s, double delays[], int len)
+{
+    int32_t i;
+    int lose;
+    double jitter_delay;
+
+    for (i = 0;  i < len;  i++)
+    {
+        lose = FALSE;
+        jitter_delay = s->base_delay + s->max_jitter*q1050_rand();
+        /* Route flapping */
+        if (--s->route_flap_counter <= 0)
+        {
+            /* Route changed */
+            s->delay_delta = s->route_flap_delta - s->delay_delta;
+            s->route_flap_counter = s->route_flap_interval;
+        }
+        if (q1050_rand() < s->prob_packet_loss)
+            lose = TRUE;
+        /* Link failures */
+        if (--s->link_failure_counter <= 0)
+        {
+            /* We are in a link failure */
+            lose = TRUE;
+            if (--s->link_recovery_counter <= 0)
+            {
+                /* Leave failure state. */
+                s->link_failure_counter = s->link_failure_interval_ticks;
+                s->link_recovery_counter = s->link_failure_duration_ticks;
+                lose = FALSE;
+            }
+        }
+        if (lose)
+        {
+            delays[i] = PACKET_LOSS_TIME;
+            s->lost_packets++;
+        }
+        else
+        {
+            delays[i] = jitter_delay + s->delay_delta;
+        }
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static int g1050_segment_delay(g1050_segment_state_t *s,
+                               double base_time,
+                               double arrival_times[],
+                               double delays[],
+                               int num_packets)
+{
+    int i;
+    int32_t departure_time;
+    int lost_packets;
+
+    /* Add appropriate delays to the packets for the segments before the core. */
+    lost_packets = 0;
+    for (i = 0;  i < num_packets;  i++)
+    {
+        /* Apply half a millisecond of rounding, as we working in millisecond steps. */
+        departure_time = (arrival_times[i] + 0.0005 - base_time)*G1050_TICKS_PER_SEC;
+        if (arrival_times[i] == PACKET_LOSS_TIME)
+        {
+            /* Lost already */
+        }
+        else if (delays[departure_time] == PACKET_LOSS_TIME)
+        {
+            arrival_times[i] = PACKET_LOSS_TIME;
+            lost_packets++;
+        }
+        else
+        {
+            arrival_times[i] += delays[departure_time];
+            if (arrival_times[i] < s->last_arrival_time)
+                arrival_times[i] = s->last_arrival_time;
+            else
+                s->last_arrival_time = arrival_times[i];
+        }
+    }
+    return lost_packets;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int g1050_segment_delay_preserve_order(g1050_segment_state_t *s,
+                                              double base_time,
+                                              double arrival_times_a[],
+                                              double arrival_times_b[],
+                                              double delays[],
+                                              int num_packets)
+{
+    int i;
+    int j;
+    int departure_time;
+    double last_arrival_time;
+    double last_arrival_time_temp;
+    int lost_packets;
+
+    /* Add appropriate delays to the packets for the segments after the core. */
+    last_arrival_time = 0.0;
+    last_arrival_time_temp = 0.0;
+    lost_packets = 0;
+    for (i = 0;  i < num_packets;  i++)
+    {
+        /* We need to preserve the order that came out of the core, so we
+           use an alternate array for the results.  */
+        /* Apply half a millisecond of rounding, as we working in millisecond steps. */
+        departure_time = (arrival_times_a[i] + 0.0005 - base_time)*G1050_TICKS_PER_SEC;
+        if (arrival_times_a[i] == PACKET_LOSS_TIME)
+        {
+            /* Lost already */
+            arrival_times_b[i] = PACKET_LOSS_TIME;
+        }
+        else if (delays[departure_time] == PACKET_LOSS_TIME)
+        {
+            arrival_times_b[i] = PACKET_LOSS_TIME;
+            lost_packets++;
+        }
+        else
+        {
+            arrival_times_b[i] = arrival_times_a[i] + delays[departure_time];
+            if (arrival_times_a[i] < last_arrival_time)
+            {
+                /* If a legitimate out of sequence packet is detected, search
+                   back a fixed amount of time to preserve order. */
+                for (j = i - 1;  j >= 0;  j--)
+                {
+                    if ((arrival_times_a[j] != PACKET_LOSS_TIME)
+                        &&
+                        (arrival_times_b[j] != PACKET_LOSS_TIME))
+                    {
+                        if ((arrival_times_a[i] - arrival_times_a[j]) > SEARCHBACK_PERIOD)
+                            break;
+                        if ((arrival_times_a[j] > arrival_times_a[i])
+                            &&
+                            (arrival_times_b[j] < arrival_times_b[i]))
+                        {
+                            arrival_times_b[j] = arrival_times_b[i];
+                        }
+                    }
+                }
+            }
+            else
+            {
+                last_arrival_time = arrival_times_a[i];
+                if (arrival_times_b[i] < last_arrival_time_temp)
+                    arrival_times_b[i] = last_arrival_time_temp;
+                else
+                    last_arrival_time_temp = arrival_times_b[i];
+            }
+        }
+    }
+    return lost_packets;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int g1050_core_delay(g1050_core_state_t *s,
+                            double base_time,
+                            double arrival_times[],
+                            double delays[],
+                            int num_packets)
+{
+    int i;
+    int departure_time;
+    int lost_packets;
+
+    /* This element does NOT preserve packet order. */
+    lost_packets = 0;
+    for (i = 0;  i < num_packets;  i++)
+    {
+        /* Apply half a millisecond of rounding, as we working in millisecond steps. */
+        departure_time = (arrival_times[i] + 0.0005 - base_time)*G1050_TICKS_PER_SEC;
+        if (arrival_times[i] == PACKET_LOSS_TIME)
+        {
+            /* Lost already */
+        }
+        else if (delays[departure_time] == PACKET_LOSS_TIME)
+        {
+            arrival_times[i] = PACKET_LOSS_TIME;
+            lost_packets++;
+        }
+        else
+        {
+            /* Not lost. Compute arrival time. */
+            arrival_times[i] += delays[departure_time];
+            if (arrival_times[i] < s->last_arrival_time)
+            {
+                /* This packet is EARLIER than the last one. It is out of order! */
+                /* Do we allow it to stay out of order? */
+                if (q1050_rand() >= s->prob_oos)
+                    arrival_times[i] = s->last_arrival_time;
+            }
+            else
+            {
+                /* Packet is in the correct order, relative to the last one. */
+                s->last_arrival_time = arrival_times[i];
+            }
+        }
+    }
+    return lost_packets;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void g1050_simulate_chunk(g1050_state_t *s)
+{
+    int i;
+    
+    s->base_time += 1.0;
+
+    memcpy(&s->segment[0].delays[0], &s->segment[0].delays[G1050_TICKS_PER_SEC], 2*G1050_TICKS_PER_SEC*sizeof(s->segment[0].delays[0]));
+    g1050_segment_model(&s->segment[0], &s->segment[0].delays[2*G1050_TICKS_PER_SEC], G1050_TICKS_PER_SEC);
+
+    memcpy(&s->segment[1].delays[0], &s->segment[1].delays[G1050_TICKS_PER_SEC], 2*G1050_TICKS_PER_SEC*sizeof(s->segment[1].delays[0]));
+    g1050_segment_model(&s->segment[1], &s->segment[1].delays[2*G1050_TICKS_PER_SEC], G1050_TICKS_PER_SEC);
+
+    memcpy(&s->core.delays[0], &s->core.delays[G1050_TICKS_PER_SEC], 2*G1050_TICKS_PER_SEC*sizeof(s->core.delays[0]));
+    g1050_core_model(&s->core, &s->core.delays[2*G1050_TICKS_PER_SEC], G1050_TICKS_PER_SEC);
+
+    memcpy(&s->segment[2].delays[0], &s->segment[2].delays[G1050_TICKS_PER_SEC], 2*G1050_TICKS_PER_SEC*sizeof(s->segment[2].delays[0]));
+    g1050_segment_model(&s->segment[2], &s->segment[2].delays[2*G1050_TICKS_PER_SEC], G1050_TICKS_PER_SEC);
+
+    memcpy(&s->segment[3].delays[0], &s->segment[3].delays[G1050_TICKS_PER_SEC], 2*G1050_TICKS_PER_SEC*sizeof(s->segment[3].delays[0]));
+    g1050_segment_model(&s->segment[3], &s->segment[3].delays[2*G1050_TICKS_PER_SEC], G1050_TICKS_PER_SEC);
+
+    memcpy(&s->arrival_times_1[0], &s->arrival_times_1[s->packet_rate], 2*s->packet_rate*sizeof(s->arrival_times_1[0]));
+    memcpy(&s->arrival_times_2[0], &s->arrival_times_2[s->packet_rate], 2*s->packet_rate*sizeof(s->arrival_times_2[0]));
+    for (i = 0;  i < s->packet_rate;  i++)
+    {
+        s->arrival_times_1[2*s->packet_rate + i] = s->base_time + 2.0 + (double) i/(double) s->packet_rate;
+        s->arrival_times_2[2*s->packet_rate + i] = 0.0;
+    }
+
+    s->segment[0].lost_packets_2 += g1050_segment_delay(&s->segment[0], s->base_time, s->arrival_times_1, s->segment[0].delays, s->packet_rate);
+    s->segment[1].lost_packets_2 += g1050_segment_delay(&s->segment[1], s->base_time, s->arrival_times_1, s->segment[1].delays, s->packet_rate);
+    s->core.lost_packets_2 += g1050_core_delay(&s->core, s->base_time, s->arrival_times_1, s->core.delays, s->packet_rate);
+    s->segment[2].lost_packets_2 += g1050_segment_delay_preserve_order(&s->segment[2], s->base_time, s->arrival_times_1, s->arrival_times_2, s->segment[2].delays, s->packet_rate);
+    s->segment[3].lost_packets_2 += g1050_segment_delay_preserve_order(&s->segment[3], s->base_time, s->arrival_times_2, s->arrival_times_1, s->segment[3].delays, s->packet_rate);
+}
+/*- End of function --------------------------------------------------------*/
+
+g1050_state_t *g1050_init(int model,
+                          int speed_pattern,
+                          int packet_size,
+                          int packet_rate)
+{
+    g1050_state_t *s;
+    g1050_constants_t *constants;
+    g1050_channel_speeds_t *sp;
+    g1050_model_t *mo;
+    int i;
+
+    /* If the random generator has not been seeded it might give endless
+       zeroes - it depends on the platform. */
+    for (i = 0;  i < 10;  i++)
+    {
+        if (q1050_rand() != 0.0)
+            break;
+    }
+    if (i >= 10)
+        srand48(time(NULL));
+    if ((s = (g1050_state_t *) malloc(sizeof(*s))) == NULL)
+        return NULL;
+    memset(s, 0, sizeof(*s));
+
+    constants = &g1050_constants[0];
+    sp = &g1050_speed_patterns[speed_pattern - 1];
+    mo = &g1050_standard_models[model];
+
+    memset(s, 0, sizeof(*s));
+    
+    s->packet_rate = packet_rate;
+    s->packet_size = packet_size;
+
+    g1050_segment_init(&s->segment[0],
+                       G1050_LAN_LINK,
+                       &constants->segment[0],
+                       &mo->sidea_lan,
+                       sp->sidea_lan_bit_rate,
+                       sp->sidea_lan_multiple_access,
+                       FALSE,
+                       packet_size,
+                       packet_rate);
+    g1050_segment_init(&s->segment[1],
+                       G1050_ACCESS_LINK,
+                       &constants->segment[1],
+                       &mo->sidea_access_link,
+                       sp->sidea_access_link_bit_rate_ab,
+                       FALSE,
+                       sp->sidea_access_link_qos_enabled,
+                       packet_size,
+                       packet_rate);
+    g1050_core_init(&s->core, &mo->core, packet_rate);
+    g1050_segment_init(&s->segment[2],
+                       G1050_ACCESS_LINK,
+                       &constants->segment[2],
+                       &mo->sideb_access_link,
+                       sp->sideb_access_link_bit_rate_ba,
+                       FALSE,
+                       sp->sideb_access_link_qos_enabled,
+                       packet_size,
+                       packet_rate);
+    g1050_segment_init(&s->segment[3],
+                       G1050_LAN_LINK,
+                       &constants->segment[3],
+                       &mo->sideb_lan,
+                       sp->sideb_lan_bit_rate,
+                       sp->sideb_lan_multiple_access,
+                       FALSE,
+                       packet_size,
+                       packet_rate);
+
+    s->base_time = 0.0;
+    /* Start with enough of the future modelled to allow for the worst jitter.
+       After this we will always keep at least 2 seconds of the future modelled. */
+    g1050_segment_model(&s->segment[0], s->segment[0].delays, 3*G1050_TICKS_PER_SEC);
+    g1050_segment_model(&s->segment[1], s->segment[1].delays, 3*G1050_TICKS_PER_SEC);
+    g1050_core_model(&s->core, s->core.delays, 3*G1050_TICKS_PER_SEC);
+    g1050_segment_model(&s->segment[2], s->segment[2].delays, 3*G1050_TICKS_PER_SEC);
+    g1050_segment_model(&s->segment[3], s->segment[3].delays, 3*G1050_TICKS_PER_SEC);
+
+    /* Initialise the arrival times to the departure times */
+    for (i = 0;  i < 3*s->packet_rate;  i++)
+    {
+        s->arrival_times_1[i] = s->base_time + (double) i/(double)s->packet_rate;
+        s->arrival_times_2[i] = 0.0;
+    }
+
+    s->segment[0].lost_packets_2 += g1050_segment_delay(&s->segment[0], s->base_time, s->arrival_times_1, s->segment[0].delays, s->packet_rate);
+    s->segment[1].lost_packets_2 += g1050_segment_delay(&s->segment[1], s->base_time, s->arrival_times_1, s->segment[1].delays, s->packet_rate);
+    s->core.lost_packets_2 += g1050_core_delay(&s->core, s->base_time, s->arrival_times_1, s->core.delays, s->packet_rate);
+    s->segment[2].lost_packets_2 += g1050_segment_delay_preserve_order(&s->segment[2], s->base_time, s->arrival_times_1, s->arrival_times_2, s->segment[2].delays, s->packet_rate);
+    s->segment[3].lost_packets_2 += g1050_segment_delay_preserve_order(&s->segment[3], s->base_time, s->arrival_times_2, s->arrival_times_1, s->segment[3].delays, s->packet_rate);
+
+    s->first = NULL;
+    s->last = NULL;
+    return s;
+}
+/*- End of function --------------------------------------------------------*/
+
+void g1050_dump_parms(int model, int speed_pattern)
+{
+    g1050_channel_speeds_t *sp;
+    g1050_model_t *mo;
+    
+    sp = &g1050_speed_patterns[speed_pattern - 1];
+    mo = &g1050_standard_models[model];
+
+    printf("Model %d%c\n", speed_pattern, 'A' + model - 1);
+    printf("LOO %.6f%% %.6f%% %.6f%%\n", mo->loo[0]*sp->loo/100.0, mo->loo[1]*sp->loo/100.0, mo->loo[2]*sp->loo/100.0);
+    printf("Side A LAN %dbps, %.3f%% occupancy, MTU %d, %s MA\n", sp->sidea_lan_bit_rate, mo->sidea_lan.percentage_occupancy, mo->sidea_lan.mtu, (sp->sidea_lan_multiple_access)  ?  ""  :  "no");
+    printf("Side A access %dbps, %.3f%% occupancy, MTU %d, %s QoS\n", sp->sidea_access_link_bit_rate_ab, mo->sidea_access_link.percentage_occupancy, mo->sidea_access_link.mtu, (sp->sidea_access_link_qos_enabled)  ?  ""  :  "no");
+    printf("Core delay %.4fs (%.4fs), peak jitter %.4fs, prob loss %.4f%%, prob OOS %.4f%%\n", mo->core.base_regional_delay, mo->core.base_intercontinental_delay, mo->core.max_jitter, mo->core.prob_packet_loss, mo->core.prob_oos);
+    printf("     Route flap interval %.4fs, delay change %.4fs\n", mo->core.route_flap_interval, mo->core.route_flap_delay);
+    printf("     Link failure interval %.4fs, duration %.4fs\n", mo->core.link_failure_interval, mo->core.link_failure_duration);
+    printf("Side B access %dbps, %.3f%% occupancy, MTU %d, %s QoS\n", sp->sideb_access_link_bit_rate_ba, mo->sideb_access_link.percentage_occupancy, mo->sideb_access_link.mtu, (sp->sideb_access_link_qos_enabled)  ?  ""  :  "no");
+    printf("Side B LAN %dbps, %.3f%% occupancy, MTU %d, %s MA\n", sp->sideb_lan_bit_rate, mo->sideb_lan.percentage_occupancy, mo->sideb_lan.mtu, (sp->sideb_lan_multiple_access)  ?  ""  :  "no");
+}
+/*- End of function --------------------------------------------------------*/
+
+int g1050_put(g1050_state_t *s, const uint8_t buf[], int len, int seq_no, double departure_time)
+{
+    g1050_queue_element_t *element;
+    g1050_queue_element_t *e;
+    double arrival_time;
+
+    while (departure_time >= s->base_time + 1.0)
+        g1050_simulate_chunk(s);
+    arrival_time = s->arrival_times_1[(int) ((departure_time - s->base_time)*(double) s->packet_rate + 0.5)];
+    if (arrival_time < 0)
+    {
+        /* This packet is lost */
+        return 0;
+    }
+    if ((element = (g1050_queue_element_t *) malloc(sizeof(*element) + len)) == NULL)
+        return -1;
+    element->next = NULL;
+    element->prev = NULL;
+    element->seq_no = seq_no;
+    element->departure_time = departure_time;
+    element->arrival_time = arrival_time;
+    element->len = len;
+    memcpy(element->pkt, buf, len);
+    /* Add it to the queue, in order */
+    if (s->last == NULL)
+    {
+        /* The queue is empty */
+        s->first =
+        s->last = element;
+    }
+    else
+    {
+        for (e = s->last;  e;  e = e->prev)
+        {
+            if (e->arrival_time <= arrival_time)
+                break;
+        }
+        if (e)
+        {
+            element->next = e->next; 
+            element->prev = e; 
+            e->next = element;
+        }
+        else
+        {
+            element->next = s->first;
+            s->first = element;
+        }
+        if (element->next)
+            element->next->prev = element;
+        else
+            s->last = element;
+    }
+    //printf(">> Seq %d, departs %f, arrives %f\n", seq_no, departure_time, arrival_time);
+    return len;
+}
+/*- End of function --------------------------------------------------------*/
+
+int g1050_get(g1050_state_t *s, uint8_t buf[], int max_len, double current_time, int *seq_no, double *departure_time, double *arrival_time)
+{
+    int len;
+    g1050_queue_element_t *element;
+
+    element = s->first;
+    if (element == NULL)
+    {
+        if (seq_no)
+            *seq_no = -1;
+        if (departure_time)
+            *departure_time = -1;
+        if (arrival_time)
+            *arrival_time = -1;
+        return -1;
+    }
+    if (element->arrival_time > current_time)
+    {
+        if (seq_no)
+            *seq_no = element->seq_no;
+        if (departure_time)
+            *departure_time = element->departure_time;
+        if (arrival_time)
+            *arrival_time = element->arrival_time;
+        return -1;
+    }
+    /* Return the first packet in the queue */
+    len = element->len;
+    memcpy(buf, element->pkt, len);
+    if (seq_no)
+        *seq_no = element->seq_no;
+    if (departure_time)
+        *departure_time = element->departure_time;
+    if (arrival_time)
+        *arrival_time = element->arrival_time;
+    //printf("<< Seq %d, arrives %f (%f)\n", element->seq_no, element->arrival_time, current_time);
+
+    /* Remove it from the queue */
+    if (s->first == s->last)
+        s->last = NULL;
+    s->first = element->next;
+    if (element->next)
+        element->next->prev = NULL;
+    free(element);
+    return len;
+}
+/*- End of function --------------------------------------------------------*/
+
+void g1050_queue_dump(g1050_state_t *s)
+{
+    g1050_queue_element_t *e;
+
+    printf("Queue scanned forewards\n");
+    for (e = s->first;  e;  e = e->next)
+        printf("Seq %5d, arrival %10.4f, len %3d\n", e->seq_no, e->arrival_time, e->len);
+    printf("Queue scanned backwards\n");
+    for (e = s->last;  e;  e = e->prev)
+        printf("Seq %5d, arrival %10.4f, len %3d\n", e->seq_no, e->arrival_time, e->len);
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/spandsp-sim/line_model.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/spandsp-sim/line_model.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,566 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * line_model.c - Model a telephone line.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2004 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: line_model.c,v 1.6 2008/07/02 14:48:25 steveu Exp $
+ */
+
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <unistd.h>
+#include <inttypes.h>
+#include <string.h>
+#include <time.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <audiofile.h>
+#include "floating_fudge.h"
+#if defined(HAVE_TGMATH_H)
+#include <tgmath.h>
+#endif
+#if defined(HAVE_MATH_H)
+#define GEN_CONST
+#include <math.h>
+#endif
+
+#include "spandsp.h"
+#include "spandsp-sim.h"
+#include "spandsp/g168models.h"
+
+#if !defined(NULL)
+#define NULL (void *) 0
+#endif
+
+float null_line_model[] =
+{
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        0.0,
+        1.0
+};
+
+static float *models[] =
+{
+    null_line_model,        /* 0 */
+    proakis_line_model,
+    ad_1_edd_1_model,
+    ad_1_edd_2_model,
+    ad_1_edd_3_model,
+    ad_5_edd_1_model,       /* 5 */
+    ad_5_edd_2_model,
+    ad_5_edd_3_model,
+    ad_6_edd_1_model,
+    ad_6_edd_2_model,
+    ad_6_edd_3_model,       /* 10 */
+    ad_7_edd_1_model,
+    ad_7_edd_2_model,
+    ad_7_edd_3_model,
+    ad_8_edd_1_model,
+    ad_8_edd_2_model,       /* 15 */
+    ad_8_edd_3_model,
+    ad_9_edd_1_model,
+    ad_9_edd_2_model,
+    ad_9_edd_3_model
+};
+
+static float calc_near_line_filter(one_way_line_model_state_t *s, float v)
+{
+    float sum;
+    int j;
+    int p;
+
+    /* Add the sample in the filter buffer */
+    p = s->near_buf_ptr;
+    s->near_buf[p] = v;
+    if (++p == s->near_filter_len)
+        p = 0;
+    s->near_buf_ptr = p;
+    
+    /* Apply the filter */
+    sum = 0;
+    for (j = 0;  j < s->near_filter_len;  j++)
+    {
+        sum += s->near_filter[j]*s->near_buf[p];
+        if (++p >= s->near_filter_len)
+            p = 0;
+    }
+    
+    /* Add noise */
+    sum += awgn(&s->near_noise);
+    
+    return sum;
+}
+/*- End of function --------------------------------------------------------*/
+
+static float calc_far_line_filter(one_way_line_model_state_t *s, float v)
+{
+    float sum;
+    int j;
+    int p;
+
+    /* Add the sample in the filter buffer */
+    p = s->far_buf_ptr;
+    s->far_buf[p] = v;
+    if (++p == s->far_filter_len)
+        p = 0;
+    s->far_buf_ptr = p;
+    
+    /* Apply the filter */
+    sum = 0;
+    for (j = 0;  j < s->far_filter_len;  j++)
+    {
+        sum += s->far_filter[j]*s->far_buf[p];
+        if (++p >= s->far_filter_len)
+            p = 0;
+    }
+    
+    /* Add noise */
+    sum += awgn(&s->far_noise);
+
+    return sum;
+}
+/*- End of function --------------------------------------------------------*/
+
+void one_way_line_model(one_way_line_model_state_t *s, 
+                        int16_t output[],
+                        const int16_t input[],
+                        int samples)
+{
+    int i;
+    float in;
+    float out;
+    float out1;
+    int16_t amp[1];
+
+    /* The path being modelled is:
+        terminal
+          | < hybrid
+          |
+          | < noise and filtering
+          |
+          | < hybrid
+         CO
+          |
+          | < A-law distortion + bulk delay
+          |
+         CO
+          | < hybrid
+          |
+          | < noise and filtering
+          |
+          | < hybrid
+        terminal
+     */
+    for (i = 0;  i < samples;  i++)
+    {
+        in = input[i];
+
+        /* Near end analogue section */
+        
+        /* Line model filters & noise */
+        out = calc_near_line_filter(s, in);
+    
+        /* Long distance digital section */
+
+        amp[0] = out;
+        codec_munge(s->munge, amp, 1);
+        out = amp[0];
+        /* Introduce the bulk delay of the long distance link. */
+        out1 = s->bulk_delay_buf[s->bulk_delay_ptr];
+        s->bulk_delay_buf[s->bulk_delay_ptr] = out;
+        out = out1;
+        if (++s->bulk_delay_ptr >= s->bulk_delay)
+            s->bulk_delay_ptr = 0;
+
+        /* Far end analogue section */
+        
+        /* Line model filters & noise */
+        out = calc_far_line_filter(s, out);
+    
+        if (s->mains_interference)
+        {
+            tone_gen(&s->mains_tone, amp, 1);
+            out += amp[0];
+        }
+        output[i] = out + s->dc_offset;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+void one_way_line_model_set_dc(one_way_line_model_state_t *s, float dc)
+{
+    s->dc_offset = dc;
+}
+/*- End of function --------------------------------------------------------*/
+
+void one_way_line_model_set_mains_pickup(one_way_line_model_state_t *s, int f, float level)
+{
+    tone_gen_descriptor_t mains_tone_desc;
+
+    if (f)
+    {
+        make_tone_gen_descriptor(&mains_tone_desc, f, (int) (level - 10.0f), f*3, (int) level, 1, 0, 0, 0, TRUE);
+        tone_gen_init(&s->mains_tone, &mains_tone_desc);
+    }
+    s->mains_interference = f;
+}
+/*- End of function --------------------------------------------------------*/
+
+void both_ways_line_model(both_ways_line_model_state_t *s, 
+                          int16_t output1[],
+                          const int16_t input1[],
+                          int16_t output2[],
+                          const int16_t input2[],
+                          int samples)
+{
+    int i;
+    float in1;
+    float in2;
+    float out1;
+    float out2;
+    float tmp1;
+    float tmp2;
+    int16_t amp[1];
+
+    /* The path being modelled is:
+        terminal
+          | < hybrid echo
+          |
+          | < noise and filtering
+          |
+          | < hybrid echo
+         CO
+          |
+          | < A-law distortion + bulk delay
+          |
+         CO
+          | < hybrid echo
+          |
+          | < noise and filtering
+          |
+          | < hybrid echo
+        terminal
+     */
+    for (i = 0;  i < samples;  i++)
+    {
+        in1 = input1[i];
+        in2 = input2[i];
+
+        /* Near end analogue sections */
+        /* Echo from each terminal's CO hybrid */
+        tmp1 = in1 + s->fout2*s->line1.near_co_hybrid_echo;
+        tmp2 = in2 + s->fout1*s->line2.near_co_hybrid_echo;
+
+        /* Line model filters & noise */
+        s->fout1 = calc_near_line_filter(&s->line1, tmp1);
+        s->fout2 = calc_near_line_filter(&s->line2, tmp2);
+
+        /* Long distance digital section */
+
+        /* Introduce distortion due to A-law or u-law munging. */
+        amp[0] = s->fout1;
+        codec_munge(s->line1.munge, amp, 1);
+        s->fout1 = amp[0];
+
+        amp[0] = s->fout2;
+        codec_munge(s->line2.munge, amp, 1);
+        s->fout2 = amp[0];
+
+        /* Introduce the bulk delay of the long distance digital link. */
+        out1 = s->line1.bulk_delay_buf[s->line1.bulk_delay_ptr];
+        s->line1.bulk_delay_buf[s->line1.bulk_delay_ptr] = s->fout1;
+        s->fout1 = out1;
+        if (++s->line1.bulk_delay_ptr >= s->line1.bulk_delay)
+            s->line1.bulk_delay_ptr = 0;
+
+        out2 = s->line2.bulk_delay_buf[s->line2.bulk_delay_ptr];
+        s->line2.bulk_delay_buf[s->line2.bulk_delay_ptr] = s->fout2;
+        s->fout2 = out2;
+        if (++s->line2.bulk_delay_ptr >= s->line2.bulk_delay)
+            s->line2.bulk_delay_ptr = 0;
+
+        /* Far end analogue sections */
+
+        /* Echo from each terminal's own hybrid */
+        out1 += in2*s->line1.far_cpe_hybrid_echo;
+        out2 += in1*s->line2.far_cpe_hybrid_echo;
+
+        /* Line model filters & noise */
+        out1 = calc_far_line_filter(&s->line1, out1);
+        out2 = calc_far_line_filter(&s->line2, out2);
+
+        output1[i] = fsaturate(out1 + s->line1.dc_offset);
+        output2[i] = fsaturate(out2 + s->line2.dc_offset);
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+void both_ways_line_model_set_dc(both_ways_line_model_state_t *s, float dc1, float dc2)
+{
+    s->line1.dc_offset = dc1;
+    s->line2.dc_offset = dc2;
+}
+/*- End of function --------------------------------------------------------*/
+
+void both_ways_line_model_set_mains_pickup(both_ways_line_model_state_t *s, int f, float level1, float level2)
+{
+    tone_gen_descriptor_t mains_tone_desc;
+
+    if (f)
+    {
+        make_tone_gen_descriptor(&mains_tone_desc, f, (int) (level1 - 10.0f), f*3, (int) level1, 1, 0, 0, 0, TRUE);
+        tone_gen_init(&s->line1.mains_tone, &mains_tone_desc);
+        make_tone_gen_descriptor(&mains_tone_desc, f, (int) (level2 - 10.0f), f*3, (int) level2, 1, 0, 0, 0, TRUE);
+        tone_gen_init(&s->line2.mains_tone, &mains_tone_desc);
+    }
+    s->line1.mains_interference = f;
+    s->line2.mains_interference = f;
+}
+/*- End of function --------------------------------------------------------*/
+
+one_way_line_model_state_t *one_way_line_model_init(int model, float noise, int codec, int rbs_pattern)
+{
+    one_way_line_model_state_t *s;
+
+    if ((s = (one_way_line_model_state_t *) malloc(sizeof(*s))) == NULL)
+        return NULL;
+    memset(s, 0, sizeof(*s));
+
+    s->bulk_delay = 8;
+    s->bulk_delay_ptr = 0;
+
+    s->munge = codec_munge_init(codec, rbs_pattern);
+
+    s->near_filter = models[model];
+    s->near_filter_len = 129;
+
+    s->far_filter = models[model];
+    s->far_filter_len = 129;
+
+    awgn_init_dbm0(&s->near_noise, 1234567, noise);
+    awgn_init_dbm0(&s->far_noise, 1234567, noise);
+    
+    s->dc_offset = 0.0f;
+    s->mains_interference = 0;
+
+    return s;
+}
+/*- End of function --------------------------------------------------------*/
+
+int one_way_line_model_release(one_way_line_model_state_t *s)
+{
+    free(s);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+both_ways_line_model_state_t *both_ways_line_model_init(int model1,
+                                                        float noise1,
+                                                        int model2,
+                                                        float noise2,
+                                                        int codec,
+                                                        int rbs_pattern)
+{
+    float echo_level;
+    both_ways_line_model_state_t *s;
+
+    if ((s = (both_ways_line_model_state_t *) malloc(sizeof(*s))) == NULL)
+        return NULL;
+    memset(s, 0, sizeof(*s));
+
+    s->line1.munge = codec_munge_init(codec, rbs_pattern);
+    s->line2.munge = codec_munge_init(codec, rbs_pattern);
+
+    s->line1.bulk_delay = 8;
+    s->line2.bulk_delay = 8;
+
+    s->line1.bulk_delay_ptr = 0;
+    s->line2.bulk_delay_ptr = 0;
+
+    s->line1.near_filter = models[model1];
+    s->line1.near_filter_len = 129;
+    s->line2.near_filter = models[model2];
+    s->line2.near_filter_len = 129;
+
+    s->line1.far_filter = models[model1];
+    s->line1.far_filter_len = 129;
+    s->line2.far_filter = models[model2];
+    s->line2.far_filter_len = 129;
+
+    awgn_init_dbm0(&s->line1.near_noise, 1234567, noise1);
+    awgn_init_dbm0(&s->line2.near_noise, 7654321, noise2);
+
+    awgn_init_dbm0(&s->line1.far_noise, 1234567, noise1);
+    awgn_init_dbm0(&s->line2.far_noise, 7654321, noise2);
+
+    s->line1.dc_offset = 0.0f;
+    s->line2.dc_offset = 0.0f;
+    s->line1.mains_interference = 0;
+    s->line2.mains_interference = 0;
+
+    /* Echos */
+    echo_level = -15; /* in dB */
+    s->line1.near_co_hybrid_echo = pow(10, echo_level/20.0);
+    s->line2.near_co_hybrid_echo = pow(10, echo_level/20.0);
+    s->line1.near_cpe_hybrid_echo = pow(10, echo_level/20.0);
+    s->line2.near_cpe_hybrid_echo = pow(10, echo_level/20.0);
+    
+    return s;
+}
+/*- End of function --------------------------------------------------------*/
+
+int both_ways_line_model_release(both_ways_line_model_state_t *s)
+{
+    free(s);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/spandsp-sim/make_line_models.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/spandsp-sim/make_line_models.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,1041 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * make_line_models.c
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2004 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: make_line_models.c,v 1.7 2008/07/25 13:56:54 steveu Exp $
+ */
+
+/*! \page make_line_models_page Telephony line model construction
+\section make_line_models_page_sec_1 What does it do?
+???.
+
+\section make_line_models_page_sec_2 How does it work?
+???.
+*/
+
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
+
+#include <inttypes.h>
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include "floating_fudge.h"
+#if defined(HAVE_FFTW3_H)
+#include <fftw3.h>
+#else
+#include <fftw.h>
+#endif
+#if defined(HAVE_TGMATH_H)
+#include <tgmath.h>
+#endif
+#if defined(HAVE_MATH_H)
+#include <math.h>
+#endif
+
+#include "spandsp.h"
+
+#if !defined(M_PI)
+# define M_PI           3.14159265358979323846  /* pi */
+#endif
+
+#define LINE_MODEL_FILE_NAME    "line_models.c"
+
+#define SAMPLE_RATE             8000
+
+#define LINE_FILTER_SIZE        129
+#define FFT_SIZE                1024
+
+/* Tabulated medium range telephone line response
+   (from p 537, Digital Communication, John G. Proakis */
+/*
+    amp 1.0 -> 2.15, freq = 3000 Hz -> 3.2, by 0.2 increments 
+    delay = 4 ms -> 2.2
+ */
+
+struct
+{
+    int frequency;
+    float amp;
+    float delay;
+} proakis[] =
+{
+    {   0, 0.00,  4.80},
+    { 200, 0.90,  3.50},
+    { 400, 1.40,  2.20},
+    { 600, 1.80,  0.90},
+    { 800, 2.00,  0.50},
+    {1000, 2.10,  0.25},
+    {1200, 2.30,  0.10},
+    {1400, 2.30,  0.05},
+    {1600, 2.20,  0.00},
+    {1800, 2.10,  0.00},
+    {2000, 2.00,  0.00},
+    {2200, 1.85,  0.05},
+    {2400, 1.75,  0.10},
+    {2600, 1.55,  0.20},
+    {2800, 1.30,  0.40},
+    {3000, 1.10,  0.50},
+    {3200, 0.80,  0.90},
+    {3400, 0.55,  1.20},
+    {3600, 0.25,  2.20},
+    {3800, 0.05,  3.20},
+    {4000, 0.05,  4.20},
+    {4200, 0.05,  5.20}
+};
+
+#define CPE_TO_CO_ATTENUATION       0       /* In dB */
+#define CPE_TO_CO_DELAY             1       /* In us */
+#define CPE_TO_CO_IMPEDANCE         2       /* In ohms */
+#define CPE_TO_CO_PHASE             3       /* In degrees */
+#define CO_TO_CPE_IMPEDANCE         4       /* In ohms */
+#define CO_TO_CPE_PHASE             5       /* In degrees */
+#define CO_TO_CPE_ATTENUATION       6       /* In dB */
+#define CO_TO_CPE_DELAY             7       /* In us */
+
+/* Terms used, for V.56bis:
+    AD = attenuation distortion
+    EDD = envelope delay distortion */
+
+/* V.56bis EIA LL-1, non-loaded loop */
+
+struct
+{
+    int freq;
+    float ad[8];
+} eia_ll1[] =
+{
+    { 200,    {0.0,    0.4,    767,     -1.4,    767,     -1.4,    0.0,    0.4}},
+    { 300,    {0.0,    0.7,    766,     -2.0,    766,     -2.0,    0.0,    0.7}},
+    { 400,    {0.0,    0.5,    763,     -2.8,    763,     -2.8,    0.0,    0.5}},
+    { 500,    {0.0,    0.6,    765,     -3.4,    765,     -3.4,    0.0,    0.6}},
+    { 600,    {0.0,    0.2,    764,     -4.1,    764,     -4.1,    0.0,    0.2}},
+    { 700,    {0.0,    0.4,    764,     -4.7,    764,     -4.7,    0.0,    0.4}},
+    { 800,    {0.0,    0.4,    762,     -5.4,    762,     -5.4,    0.0,    0.4}},
+    { 900,    {0.0,    0.2,    762,     -6.0,    762,     -6.0,    0.0,    0.2}},
+    {1000,    {1.2,    0.5,    761,     -6.7,    761,     -6.7,    1.2,    0.5}},
+    {1100,    {0.0,    0.6,    759,     -7.4,    759,     -7.4,    0.0,    0.6}},
+    {1200,    {0.0,    0.4,    757,     -8.1,    757,     -8.1,    0.0,    0.4}},
+    {1300,    {0.0,    0.1,    757,     -8.6,    757,     -8.6,    0.0,    0.1}},
+    {1400,    {0.0,    0.3,    755,     -9.3,    755,     -9.3,    0.0,    0.3}},
+    {1500,    {0.0,    0.4,    753,    -10.0,    753,    -10.0,    0.0,    0.4}},
+    {1600,    {0.0,    0.3,    751,    -10.7,    751,    -10.7,    0.0,    0.3}},
+    {1700,    {0.0,    0.1,    748,    -11.3,    748,    -11.3,    0.0,    0.1}},
+    {1800,    {0.0,   11.0,    748,    -11.9,    748,    -11.9,    0.0,   11.0}},
+    {1900,    {0.1,    0.1,    745,    -12.5,    745,    -12.5,    0.1,    0.1}},
+    {2000,    {0.1,    0.3,    743,    -13.9,    743,    -13.9,    0.1,    0.3}},
+    {2100,    {0.1,    0.3,    740,    -13.9,    740,    -13.9,    0.1,    0.3}},
+    {2200,    {0.1,    0.3,    737,    -14.5,    737,    -14.5,    0.1,    0.3}},
+    {2300,    {0.1,    0.3,    734,    -15.2,    734,    -15.2,    0.1,    0.3}},
+    {2400,    {0.1,    0.2,    731,    -15.8,    731,    -15.8,    0.1,    0.2}},
+    {2500,    {0.1,    0.0,    728,    -16.4,    728,    -16.4,    0.1,    0.0}},
+    {2600,    {0.1,    0.0,    729,    -16.8,    729,    -16.8,    0.1,    0.0}},
+    {2700,    {0.2,    0.1,    726,    -17.4,    726,    -17.4,    0.2,    0.1}},
+    {2800,    {0.2,    0.2,    722,    -18.0,    722,    -18.0,    0.2,    0.2}},
+    {2900,    {0.2,    0.3,    719,    -18.6,    719,    -18.6,    0.2,    0.3}},
+    {3000,    {0.2,    0.4,    715,    -19.3,    715,    -19.3,    0.2,    0.4}},
+    {3100,    {0.2,    0.4,    712,    -19.9,    712,    -19.9,    0.2,    0.4}},
+    {3200,    {0.2,    0.5,    708,    -20.5,    708,    -20.5,    0.2,    0.5}},
+    {3300,    {0.2,    0.5,    704,    -21.1,    704,    -21.1,    0.2,    0.5}},
+    {3400,    {0.2,    0.5,    700,    -21.7,    700,    -21.7,    0.2,    0.5}},
+    {3500,    {0.2,    0.5,    696,    -22.3,    696,    -22.3,    0.2,    0.5}},
+    {3600,    {0.2,    0.4,    692,    -22.9,    692,    -22.9,    0.2,    0.4}},
+    {3700,    {0.2,    0.3,    688,    -23.5,    688,    -23.5,    0.2,    0.3}},
+    {3800,    {0.2,    0.2,    684,    -24.1,    684,    -24.1,    0.2,    0.2}},
+    {3900,    {0.2,    0.1,    680,    -24.7,    680,    -24.7,    0.2,    0.1}},
+    {4000,    {0.2,   -0.1,    676,    -25.2,    676,    -25.2,    0.2,   -0.1}}
+};
+
+/* V.56bis EIA LL-2, non-loaded loop */
+
+struct
+{
+    int freq;
+    float ad[8];
+} eia_ll2[] =
+{
+    { 200,   {-0.2,    6.6,   1086,     -4.9,   1085,     -5.6,   -0.2,    6.6}},
+    { 300,   {-0.2,    7.7,   1079,     -7.3,   1077,     -8.3,   -0.2,    7.7}},
+    { 400,   {-0.2,    6.7,   1062,     -9.9,   1058,    -11.2,   -0.2,    6.7}},
+    { 500,   {-0.2,    7.1,   1059,    -12.0,   1053,    -13.6,   -0.2,    7.1}},
+    { 600,   {-0.1,    5.2,   1041,    -14.4,   1034,    -16.3,   -0.1,    5.2}},
+    { 700,   {-0.1,    5.8,   1030,    -16.5,   1020,    -18.6,   -0.1,    5.8}},
+    { 800,   {-0.1,    5.4,   1010,    -18.7,    998,    -21.0,   -0.1,    5.4}},
+    { 900,   { 0.0,    4.5,    997,    -20.5,    982,    -23.1,    0.0,    4.5}},
+    {1000,   { 3.2,    5.1,    976,    -22.5,    959,    -25.3,    3.2,    5.1}},
+    {1100,   { 0.1,    5.0,    954,    -24.5,    934,    -27.4,    0.1,    5.0}},
+    {1200,   { 0.1,    4.0,    931,    -26.2,    909,    -29.4,    0.1,    4.0}},
+    {1300,   { 0.2,    2.7,    918,    -27.6,    894,    -30.9,    0.2,    2.7}},
+    {1400,   { 0.2,    2.8,    897,    -29.2,    871,    -32.6,    0.2,    2.8}},
+    {1500,   { 0.3,    2.6,    874,    -30.7,    847,    -34.3,    0.3,    2.6}},
+    {1600,   { 0.3,    2.0,    852,    -32.1,    823,    -35.8,    0.3,    2.0}},
+    {1700,   { 0.4,    0.9,    831,    -33.4,    800,    -37.2,    0.4,    0.9}},
+    {1800,   { 0.5,   40.8,    816,    -34.4,    783,    -38.4,    0.5,   40.8}},
+    {1900,   { 0.6,    0.0,    796,    -35.6,    762,    -39.6,    0.6,    0.0}},
+    {2000,   { 0.6,   -0.2,    776,    -36.6,    741,    -40.7,    0.6,   -0.2}},
+    {2100,   { 0.7,   -0.6,    756,    -37.6,    720,    -41.8,    0.7,   -0.6}},
+    {2200,   { 0.8,   -1.1,    737,    -38.6,    700,    -42.9,    0.8,   -1.1}},
+    {2300,   { 0.9,   -1.8,    719,    -39.4,    681,    -43.8,    0.9,   -1.8}},
+    {2400,   { 1.0,   -2.6,    701,    -40.2,    663,    -44.7,    1.0,   -2.6}},
+    {2500,   { 1.0,   -3.7,    684,    -41.0,    646,    -45.5,    1.0,   -3.7}},
+    {2600,   { 1.1,   -4.1,    678,    -41.3,    639,    -45.9,    1.1,   -4.1}},
+    {2700,   { 1.2,   -4.3,    663,    -42.0,    623,    -46.6,    1.2,   -4.3}},
+    {2800,   { 1.3,   -4.5,    647,    -42.6,    607,    -47.3,    1.3,   -4.5}},
+    {2900,   { 1.4,   -4.8,    632,    -43.1,    591,    -47.9,    1.4,   -4.8}},
+    {3000,   { 1.5,   -5.2,    617,    -43.6,    576,    -48.4,    1.5,   -5.2}},
+    {3100,   { 1.6,   -5.6,    603,    -44.1,    562,    -49.0,    1.6,   -5.6}},
+    {3200,   { 1.7,   -6.0,    589,    -44.5,    548,    -49.5,    1.7,   -6.0}},
+    {3300,   { 1.8,   -6.5,    576,    -44.9,    535,    -49.9,    1.8,   -6.5}},
+    {3400,   { 1.9,   -7.1,    563,    -45.3,    522,    -50.3,    1.9,   -7.1}},
+    {3500,   { 2.0,   -7.7,    551,    -45.6,    509,    -50.7,    2.0,   -7.7}},
+    {3600,   { 2.1,   -8.4,    539,    -45.9,    498,    -51.0,    2.1,   -8.4}},
+    {3700,   { 2.2,   -9.1,    528,    -46.2,    486,    -51.3,    2.2,   -9.1}},
+    {3800,   { 2.3,   -9.9,    518,    -46.4,    476,    -51.6,    2.3,   -9.9}},
+    {3900,   { 2.4,  -10.6,    507,    -46.6,    466,    -51.9,    2.4,  -10.6}},
+    {4000,   { 2.5,  -11.5,    498,    -46.8,    456,    -52.1,    2.5,  -11.5}}
+};
+
+/* V.56bis EIA LL-3, non-loaded loop */
+
+struct
+{
+    int freq;
+    float ad[8];
+} eia_ll3[] =
+{
+    { 200,   {-0.3,   10.5,   1176,     -5.9,   1173,     -7.4,   -0.3,   10.5}},
+    { 300,   {-0.3,   11.5,   1165,     -8.8,   1159,    -11.0,   -0.3,   11.5}},
+    { 400,   {-0.3,   10.6,   1140,    -11.8,   1130,    -14.7,   -0.3,   10.6}},
+    { 500,   {-0.3,   11.0,   1133,    -14.3,   1117,    -17.8,   -0.3,   11.0}},
+    { 600,   {-0.2,    8.5,   1108,    -17.1,   1086,    -21.2,   -0.2,    8.5}},
+    { 700,   {-0.2,    8.5,   1090,    -19.4,   1062,    -24.0,   -0.2,    8.5}},
+    { 800,   {-0.1,    8.4,   1062,    -21.9,   1029,    -27.0,   -0.1,    8.4}},
+    { 900,   { 0.0,    7.1,   1042,    -23.9,   1003,    -29.4,    0.0,    7.1}},
+    {1000,   { 3.8,    7.7,   1013,    -23.0,    969,    -31.9,    3.8,    7.7}},
+    {1100,   { 0.1,    7.4,    982,    -28.1,    934,    -34.3,    0.1,    7.4}},
+    {1200,   { 0.1,    6.0,    953,    -29.9,    900,    -36.5,    0.1,    6.0}},
+    {1300,   { 0.2,    4.2,    935,    -31.3,    878,    -38.1,    0.2,    4.2}},
+    {1400,   { 0.3,    4.2,    907,    -32.8,    847,    -40.0,    0.3,    4.2}},
+    {1500,   { 0.4,    3.7,    880,    -34.3,    817,    -41.7,    0.4,    3.7}},
+    {1600,   { 0.5,    2.7,    853,    -35.6,    787,    -43.2,    0.5,    2.7}},
+    {1700,   { 0.6,    1.2,    827,    -36.8,    760,    -44.6,    0.6,    1.2}},
+    {1800,   { 0.7,   48.7,    809,    -37.8,    739,    -45.8,    0.7,   48.7}},
+    {1900,   { 0.8,   -0.2,    785,    -38.8,    715,    -47.0,    0.8,   -0.2}},
+    {2000,   { 0.9,   -0.7,    763,    -39.7,    691,    -48.0,    0.9,   -0.7}},
+    {2100,   { 1.0,   -1.3,    741,    -40.5,    668,    -49.1,    1.0,   -1.3}},
+    {2200,   { 1.1,   -2.1,    719,    -41.3,    647,    -50.0,    1.1,   -2.1}},
+    {2300,   { 1.2,   -2.1,    699,    -42.0,    625,    -50.8,    1.2,   -2.1}},
+    {2400,   { 1.2,   -4.3,    680,    -42.6,    606,    -51.6,    1.2,   -4.3}},
+    {2500,   { 1.3,   -5.6,    663,    -43.2,    588,    -52.3,    1.3,   -5.6}},
+    {2600,   { 1.6,   -6.2,    656,    -43.4,    581,    -52.7,    1.6,   -6.2}},
+    {2700,   { 1.7,   -6.6,    640,    -43.9,    564,    -53.3,    1.7,   -6.6}},
+    {2800,   { 1.8,   -7.0,    624,    -44.3,    548,    -53.9,    1.8,   -7.0}},
+    {2900,   { 1.9,   -7.5,    609,    -44.7,    533,    -54.4,    1.9,   -7.5}},
+    {3000,   { 2.0,   -8.0,    594,    -45.0,    518,    -54.8,    2.0,   -8.0}},
+    {3100,   { 2.2,   -8.6,    580,    -45.3,    504,    -55.3,    2.2,   -8.6}},
+    {3200,   { 2.3,   -9.2,    566,    -45.6,    490,    -55.7,    2.3,   -9.2}},
+    {3300,   { 2.4,   -9.9,    553,    -45.8,    477,    -56.0,    2.4,   -9.9}},
+    {3400,   { 2.6,  -10.7,    540,    -46.0,    465,    -56.3,    2.6,  -10.7}},
+    {3500,   { 2.7,  -11.4,    529,    -46.2,    454,    -56.6,    2.7,  -11.4}},
+    {3600,   { 2.8,  -12.3,    517,    -46.3,    443,    -56.9,    2.8,  -12.3}},
+    {3700,   { 3.0,  -13.1,    507,    -46.4,    432,    -57.1,    3.0,  -13.1}},
+    {3800,   { 3.1,  -14.0,    497,    -46.5,    422,    -57.3,    3.1,  -14.0}},
+    {3900,   { 3.2,  -14.9,    487,    -46.6,    413,    -57.5,    3.2,  -14.9}},
+    {4000,   { 3.3,  -15.9,    478,    -46.6,    404,    -57.7,    3.3,  -15.9}}
+};
+
+
+/* V.56bis EIA LL-4, non-loaded loop */
+
+struct
+{
+    int freq;
+    float ad[8];
+} eia_ll4[] =
+{
+    { 200,   {-0.8,   31.0,   1564,    -10.7,   1564,    -10.7,   -0.8,   31.0}},
+    { 300,   {-0.8,   32.6,   1520,    -15.6,   1520,    -15.6,   -0.8,   32.6}},
+    { 400,   {-0.8,   29.8,   1447,    -20.5,   1447,    -20.5,   -0.8,   29.8}},
+    { 500,   {-0.6,   29.7,   1402,    -24.3,   1402,    -24.3,   -0.6,   29.7}},
+    { 600,   {-0.5,   24.9,   1328,    -28.1,   1328,    -28.1,   -0.5,   24.9}},
+    { 700,   {-0.4,   24.8,   1270,    -31.2,   1270,    -31.2,   -0.4,   24.8}},
+    { 800,   {-0.3,   22.7,   1200,    -34.0,   1200,    -34.0,   -0.3,   22.7}},
+    { 900,   {-0.1,   19.8,   1148,    -36.2,   1148,    -36.2,   -0.1,   19.8}},
+    {1000,   { 6.1,   19.3,   1086,    -38.3,   1086,    -38.3,    6.1,   19.3}},
+    {1100,   { 0.1,   17.5,   1027,    -40.1,   1027,    -40.1,    0.1,   17.5}},
+    {1200,   { 0.3,   14.3,    974,    -41.6,    974,    -41.6,    0.3,   14.3}},
+    {1300,   { 0.5,   10.9,    941,    -42.6,    941,    -42.6,    0.5,   10.9}},
+    {1400,   { 0.7,    9.6,    897,    -43.7,    897,    -43.7,    0.7,    9.6}},
+    {1500,   { 0.9,    7.7,    856,    -44.6,    856,    -44.6,    0.9,    7.7}},
+    {1600,   { 1.1,    5.3,    818,    -45.3,    818,    -45.3,    1.1,    5.3}},
+    {1700,   { 1.3,    2.4,    784,    -45.9,    784,    -45.9,    1.3,    2.4}},
+    {1800,   { 1.4,   69.1,    761,    -46.3,    761,    -46.3,    1.4,   69.1}},
+    {1900,   { 1.7,   -1.3,    732,    -46.6,    732,    -46.6,    1.7,   -1.3}},
+    {2000,   { 1.9,   -2.7,    706,    -46.9,    706,    -46.9,    1.9,   -2.7}},
+    {2100,   { 2.1,   -4.3,    682,    -47.1,    682,    -47.1,    2.1,   -4.3}},
+    {2200,   { 2.3,   -6.0,    659,    -47.3,    659,    -47.3,    2.3,   -6.0}},
+    {2300,   { 2.5,   -7.9,    638,    -47.4,    638,    -47.4,    2.5,   -7.9}},
+    {2400,   { 2.7,   -9.9,    619,    -47.4,    619,    -47.4,    2.7,   -9.9}},
+    {2500,   { 2.9,  -12.0,    602,    -47.5,    602,    -47.5,    2.9,  -12.0}},
+    {2600,   { 3.1,  -13.0,    596,    -47.4,    596,    -47.4,    3.1,  -13.0}},
+    {2700,   { 3.3,  -13.9,    580,    -47.4,    580,    -47.4,    3.3,  -13.9}},
+    {2800,   { 3.5,  -14.8,    566,    -47.3,    566,    -47.3,    3.5,  -14.8}},
+    {2900,   { 3.7,  -15.7,    552,    -47.2,    552,    -47.2,    3.7,  -15.7}},
+    {3000,   { 3.9,  -16.7,    539,    -47.1,    539,    -47.1,    3.9,  -16.7}},
+    {3100,   { 4.1,  -17.7,    526,    -47.0,    526,    -47.0,    4.1,  -17.7}},
+    {3200,   { 4.3,  -18.7,    515,    -46.8,    515,    -46.8,    4.3,  -18.7}},
+    {3300,   { 4.5,  -19.8,    504,    -46.7,    504,    -46.7,    4.5,  -19.8}},
+    {3400,   { 4.7,  -20.8,    493,    -46.5,    493,    -46.5,    4.7,  -20.8}},
+    {3500,   { 4.9,  -21.8,    484,    -46.4,    484,    -46.4,    4.9,  -21.8}},
+    {3600,   { 5.1,  -22.9,    475,    -46.2,    475,    -46.2,    5.1,  -22.9}},
+    {3700,   { 5.3,  -23.9,    466,    -46.0,    466,    -46.0,    5.3,  -23.9}},
+    {3800,   { 5.5,  -25.0,    458,    -45.9,    458,    -45.9,    5.5,  -25.0}},
+    {3900,   { 5.6,  -26.1,    451,    -45.7,    451,    -45.7,    5.6,  -26.1}},
+    {4000,   { 5.8,  -27.2,    444,    -45.5,    444,    -45.5,    5.8,  -27.2}}
+};
+
+
+/* V.56bis EIA LL-5, non-loaded loop */
+
+struct
+{
+    int freq;
+    float ad[8];
+} eia_ll5[] =
+{
+    { 200,   {-1.4,    55.8,   1607,   -12.7,   1574,    -17.4,   -1.4,   55.8}},
+    { 300,   {-1.3,    57.2,   1541,   -18.3,   1478,    -24.8,   -1.3,   57.2}},
+    { 400,   {-1.2,    52.2,   1443,   -23.6,   1350,    -31.5,   -1.2,   52.2}},
+    { 500,   {-1.0,    51.0,   1379,   -27.5,   1261,    -36.4,   -1.0,   51.0}},
+    { 600,   {-0.9,    43.2,   1287,   -31.2,   1150,    -40.7,   -0.9,   43.2}},
+    { 700,   {-0.7,    41.8,   1216,   -34.0,   1066,    -44.0,   -0.7,   41.8}},
+    { 800,   {-0.5,    37.4,   1137,   -36.5,    979,    -46.9,   -0.5,   37.4}},
+    { 900,   {-0.2,    32.4,   1080,   -38.3,    915,    -48.9,   -0.2,   32.4}},
+    {1000,   { 7.0,    30.5,   1015,   -39.8,    848,    -50.7,    7.0,   30.5}},
+    {1100,   { 0.3,    26.8,    956,   -41.1,    788,    -52.2,    0.3,   26.8}},
+    {1200,   { 0.5,    21.5,    904,   -42.1,    736,    -53.3,    0.5,   21.5}},
+    {1300,   { 0.8,    16.6,    873,   -42.7,    703,    -54.1,    0.8,   16.6}},
+    {1400,   { 1.0,    14.1,    832,   -43.2,    663,    -54.8,    1.0,   14.1}},
+    {1500,   { 1.3,    10.9,    795,   -43.7,    627,    -55.3,    1.3,   10.9}},
+    {1600,   { 1.6,     7.3,    762,   -44.0,    595,    -55.7,    1.6,    7.3}},
+    {1700,   { 1.9,     3.2,    733,   -44.2,    567,    -56.0,    1.9,    3.2}},
+    {1800,   { 2.2,    81.5,    713,   -44.3,    547,    -56.2,    2.2,   81.5}},
+    {1900,   { 2.4,    -1.9,    689,   -44.4,    524,    -56.4,    2.4,   -1.9}},
+    {2000,   { 2.7,    -3.9,    667,   -44.4,    503,    -56.5,    2.7,   -3.9}},
+    {2100,   { 3.0,    -6.1,    646,   -44.4,    485,    -56.5,    3.0,   -6.1}},
+    {2200,   { 3.3,    -8.3,    628,   -44.4,    466,    -56.5,    3.3,   -8.3}},
+    {2300,   { 3.6,   -10.7,    610,   -44.3,    450,    -56.5,    3.6,  -10.7}},
+    {2400,   { 3.8,   -13.1,    595,   -44.2,    436,    -56.4,    3.8,  -13.1}},
+    {2500,   { 4.1,   -15.5,    581,   -44.1,    422,    -56.3,    4.1,  -15.5}},
+    {2600,   { 4.3,   -16.7,    577,   -44.0,    417,    -56.2,    4.3,  -16.7}},
+    {2700,   { 4.6,   -17.7,    565,   -43.9,    406,    -56.1,    4.6,  -17.7}},
+    {2800,   { 4.8,   -18.8,    553,   -43.8,    395,    -56.0,    4.8,  -18.8}},
+    {2900,   { 5.1,   -19.9,    542,   -43.7,    395,    -55.9,    5.1,  -19.9}},
+    {3000,   { 5.4,   -21.0,    531,   -43.6,    375,    -55.7,    5.4,  -21.0}},
+    {3100,   { 5.6,   -22.1,    521,   -43.5,    366,    -55.6,    5.6,  -22.1}},
+    {3200,   { 5.9,   -23.2,    511,   -43.4,    357,    -55.4,    5.9,  -23.2}},
+    {3300,   { 6.1,   -24.3,    502,   -43.3,    349,    -55.3,    6.1,  -24.3}},
+    {3400,   { 6.4,   -25.4,    494,   -43.2,    341,    -55.1,    6.4,  -25.4}},
+    {3500,   { 6.6,   -26.5,    486,   -43.1,    334,    -55.0,    6.6,  -26.5}},
+    {3600,   { 6.9,   -27.6,    478,   -43.0,    327,    -54.8,    6.9,  -27.6}},
+    {3700,   { 7.1,   -28.7,    471,   -42.9,    321,    -54.7,    7.1,  -28.7}},
+    {3800,   { 7.3,   -29.9,    464,   -42.8,    315,    -54.6,    7.3,  -29.9}},
+    {3900,   { 7.5,   -31.0,    458,   -42.7,    310,    -54.4,    7.5,  -31.0}},
+    {4000,   { 7.8,   -32.1,    452,   -42.7,    304,    -54.3,    7.8,  -32.1}}
+};
+
+
+/* V.56bis EIA LL-6, non-loaded loop */
+
+struct
+{
+    int freq;
+    float ad[8];
+} eia_ll6[] =
+{
+    { 200,   {-0.2,  -39.3,   1756,    -12.0,   1748,    -19.8,   -0.2,  -39.3}},
+    { 300,   {-0.2,  -31.7,   1642,    -15.9,   1689,    -26.9,   -0.2,  -31.7}},
+    { 400,   {-0.2,  -37.5,   1506,    -18.4,   1427,    -33.4,   -0.2,  -37.5}},
+    { 500,   {-0.1,  -34.7,   1442,    -19.5,   1301,    -37.7,   -0.1,  -34.7}},
+    { 600,   {-0.1,  -46.0,   1363,    -20.1,   1153,    -40.7,   -0.1,  -46.0}},
+    { 700,   { 0.0,  -40.8,   1320,    -20.7,   1045,    -42.2,    0.0,  -40.8}},
+    { 800,   { 0.0,  -40.1,   1269,    -21.5,    943,    -42.3,    0.0,  -40.1}},
+    { 900,   { 0.0,  -40.6,   1227,    -22.5,    878,    -41.3,    0.0,  -40.6}},
+    {1000,   { 6.6,  -28.0,   1161,    -23.4,    825,    -39.3,    6.6,  -28.0}},
+    {1100,   { 0.0,  -16.5,   1082,    -23.5,    797,    -36.8,    0.0,  -16.5}},
+    {1200,   {-0.1,    0.3,   1000,    -22.2,    798,    -34.4,   -0.1,    0.3}},
+    {1300,   { 0.0,   -2.3,    943,    -19.3,    826,    -33.2,    0.0,   -2.3}},
+    {1400,   { 0.0,   13.5,    896,    -14.0,    870,    -33.8,    0.0,   13.5}},
+    {1500,   { 0.1,   22.6,    890,     -7.2,    916,    -36.8,    0.1,   22.6}},
+    {1600,   { 0.3,   30.3,    940,     -0.3,    938,    -42.0,    0.3,   30.3}},
+    {1700,   { 0.5,   12.5,   1052,      4.6,    929,    -48.0,    0.5,   12.5}},
+    {1800,   { 0.8,  458.6,   1212,      6.9,    880,    -52.8,    0.8,  458.6}},
+    {1900,   { 1.1,   -5.1,   1410,      3.5,    814,    -56.5,    1.1,   -5.1}},
+    {2000,   { 1.4,   -5.0,   1579,     -3.6,    747,    -58.5,    1.4,   -5.0}},
+    {2100,   { 1.5,    6.1,   1618,    -13.2,    688,    -58.8,    1.5,    6.1}},
+    {2200,   { 1.5,   33.5,   1491,    -21.5,    646,    -57.7,    1.5,   33.5}},
+    {2300,   { 1.4,   80.5,   1275,    -24.9,    625,    -55.6,    1.4,   80.5}},
+    {2400,   { 1.3,  142.3,   1078,    -20.8,    633,    -53.8,    1.3,  142.3}},
+    {2500,   { 1.4,  196.5,    985,     -9.3,    664,    -54.5,    1.4,  196.5}},
+    {2600,   { 1.6,  214.5,   1045,      2.4,    692,    -57.6,    1.6,  214.5}},
+    {2700,   { 2.4,  196.8,   1326,     13.7,    684,    -63.5,    2.4,  196.8}},
+    {2800,   { 3.4,  150.4,   1887,     14.7,    637,    -68.3,    3.4,  150.4}},
+    {2900,   { 4.3,  125.3,   2608,      1.3,    501,    -70.7,    4.3,  125.3}},
+    {3000,   { 4.9,  174.6,   2730,    -21.8,    533,    -70.6,    4.9,  174.6}},
+    {3100,   { 4.9,  380.0,   2094,    -33.7,    506,    -68.5,    4.9,  380.0}},
+    {3200,   { 5.2,  759.3,   1642,    -21.3,    522,    -67.0,    5.2,  759.3}},
+    {3300,   { 8.0,  680.1,   2348,      0.5,    531,    -72.9,    8.0,  680.1}},
+    {3400,   {13.1,  237.8,   4510,    -20.9,    482,    -77.3,   13.1,  237.8}},
+    {3500,   {18.2,  -18.8,   4116,    -59.6,    439,    -78.0,   18.2,  -18.8}},
+    {3600,   {22.7, -145.4,   3041,    -74.4,    487,    -77.7,   22.7, -145.4}},
+    {3700,   {26.8, -214.5,   2427,    -80.1,    383,    -77.1,   26.8, -214.5}},
+    {3800,   {30.4, -257.0,   2054,    -82.7,    364,    -76.4,   30.4, -257.0}},
+    {3900,   {33.7, -285.6,   1803,    -84.2,    348,    -75.0,   33.7, -285.6}},
+    {4000,   {36.8, -306.2,   1621,    -85.1,    334,    -75.7,   36.8, -306.2}}
+};
+
+
+/* V.56bis EIA LL-7, non-loaded loop */
+
+struct
+{
+    int freq;
+    float ad[8];
+} eia_ll7[] =
+{
+    { 200,   { 0.4,   -81.3,    1848,  -10.5,   1737,    -15.6,    0.4,  -81.3}},
+    { 300,   { 0.3,   -68.9,    1785,  -16.2,   1585,    -21.6,    0.3,  -68.9}},
+    { 400,   { 0.2,   -68.1,    1646,  -22.0,   1388,    -25.8,    0.2,  -68.1}},
+    { 500,   { 0.1,   -57.0,    1528,  -26.2,   1247,    -27.7,    0.1,  -57.0}},
+    { 600,   { 0.0,   -59.8,    1349,  -28.9,   1087,    -27.3,    0.0,  -59.8}},
+    { 700,   { 0.0,   -45.0,    1205,  -29.1,    975,    -24.8,    0.0,  -45.0}},
+    { 800,   {-0.1,   -36.9,    1064,  -26.8,    885,    -19.7,   -0.1,  -36.9}},
+    { 900,   {-0.1,   -37.1,     989,  -22.6,    846,    -13.5,   -0.1,  -37.1}},
+    {1000,   { 5.9,   -29.2,     944,  -16.6,    847,     -6.1,    5.9,  -29.2}},
+    {1100,   { 0.1,   -30.8,     951,  -10.5,    900,      0.3,    0.1,  -30.8}},
+    {1200,   { 0.2,   -40.7,    1008,   -5.9,    999,      4.9,    0.2,  -40.7}},
+    {1300,   { 0.4,   -53.3,    1897,   -4.0,   1122,      4.6,    0.4,  -53.3}},
+    {1400,   { 0.5,   -52.7,    1197,   -4.8,   1253,      1.9,    0.5,  -52.7}},
+    {1500,   { 0.6,   -48.3,    1269,   -8.4,   1339,     -3.8,    0.6,  -48.3}},
+    {1600,   { 0.6,   -38.0,    1274,  -13.2,   1337,    -10.4,    0.6,  -38.0}},
+    {1700,   { 0.5,   -21.6,    1208,  -16.9,   1250,    -15.2,    0.5,  -21.6}},
+    {1800,   { 0.4,   539.7,    1119,  -17.8,   1143,    -16.6,    0.4,  539.7}},
+    {1900,   { 0.3,    35.4,    1027,  -14.7,   1036,    -13.7,    0.3,   35.4}},
+    {2000,   { 0.3,    64.1,     989,   -7.9,    998,     -6.9,    0.3,   64.1}},
+    {2100,   { 0.4,    76.1,    1045,    0.1,   1040,      1.0,    0.4,   76.1}},
+    {2200,   { 0.6,    69.8,    1210,    5.3,   1197,      6.9,    0.6,   69.8}},
+    {2300,   { 1.0,    55.9,    1460,    4.6,   1430,      5.4,    1.0,   55.9}},
+    {2400,   { 1.2,    51.3,    1692,   -2.8,   1640,     -1.7,    1.2,   51.3}},
+    {2500,   { 1.3,    72.6,    1730,  -13.4,   1666,    -11.5,    1.3,   72.6}},
+    {2600,   { 1.3,   117.1,    1613,  -49.6,   1556,    -16.9,    1.3,  117.1}},
+    {2700,   { 1.1,   222.5,    1371,  -19.5,   1334,    -16.1,    1.1,  222.5}},
+    {2800,   { 1.1,   332.3,    1258,   -8.9,   1243,     -5.1,    1.1,  332.3}},
+    {2900,   { 1.7,   356.1,    1474,    4.8,   1480,      8.4,    1.7,  356.1}},
+    {3000,   { 2.8,   299.9,    2128,    6.6,   2143,      9.8,    2.8,  299.9}},
+    {3100,   { 3.9,   309.4,    2813,  -10.5,   2882,     -7.1,    3.9,  309.4}},
+    {3200,   { 4.4,   576.4,    2490,  -27.7,   2487,    -22.2,    4.4,  576.4}},
+    {3300,   { 5.6,  1030.6,    2237,  -17.4,   2385,     -9.0,    5.6, 1030.6}},
+    {3400,   {10.7,   570.2,    3882,  -19.2,   4855,    -14.9,   10.7,  570.2}},
+    {3500,   {17.3,    83.5,    4116,  -57.4,   4649,    -63.5,   17.3,   83.5}},
+    {3600,   {23.2,  -130.6,    3057,  -74.0,   3175,    -78.6,   23.2, -130.6}},
+    {3700,   {28.3,  -153.9,    2432,  -80.0,   2471,    -83.1,   28.3, -153.9}},
+    {3800,   {32.8,  -292.4,    2055,  -82.8,   2072,    -85.1,   32.8, -292.4}},
+    {3900,   {36.9,  -249.9,    1803,  -84.2,   1811,    -86.1,   36.9, -249.9}},
+    {4000,   {40.7,  -356.2,    1621,  -85.1,   1625,    -86.7,   40.7, -356.2}}
+};
+
+
+/* V.56bis ETSI LL-1, non-loaded loop */
+
+struct
+{
+    int freq;
+    float ad[8];
+} etsi_ll1[] =
+{
+    { 200,  {-0.78,   14.0, 1248.5,     -9.7, 1248.5,     -9.7,  -0.78,   14.0}},
+    { 300,  {-0.74,   10.0, 1220.9,    -14.3, 1220.9,    -14.3,  -0.74,   10.0}},
+    { 400,  {-0.68,    8.0, 1185.2,    -18.6, 1185.2,    -18.6,  -0.68,    8.0}},
+    { 500,  {-0.60,    7.0, 1143.9,    -22.6, 1143.9,    -22.6,  -0.60,    7.0}},
+    { 600,  {-0.51,    6.0, 1099.0,    -26.2, 1099.0,    -26.2,  -0.51,    6.0}},
+    { 700,  {-0.40,    5.6, 1052.5,    -29.5, 1052.5,    -29.5,  -0.40,    5.6}},
+    { 800,  {-0.28,    5.3, 1005.9,    -32.4, 1005.9,    -32.4,  -0.28,    5.3}},
+    { 900,  {-0.14,    5.0,  960.3,    -35.0,  960.3,    -35.0,  -0.14,    5.0}},
+    {1000,  { 4.7,     4.6,  916.4,    -37.3,  916.4,    -37.3,   4.7,     4.6}},
+    {1100,  { 0.16,    4.3,  874.6,    -39.3,  874.6,    -39.3,   0.16,    4.3}},
+    {1200,  { 0.33,    3.6,  835.3,    -41.1,  835.3,    -41.1,   0.33,    3.6}},
+    {1300,  { 0.49,    2.6,  798.5,    -42.6,  798.5,    -42.6,   0.49,    2.6}},
+    {1400,  { 0.67,    2.0,  764.2,    -43.9,  764.2,    -43.9,   0.67,    2.0}},
+    {1500,  { 0.85,    1.0,  732.3,    -45.1,  732.3,    -45.1,   0.85,    1.0}},
+    {1600,  { 1.04,    0.6,  702.7,    -46.1,  702.7,    -46.1,   1.04,    0.6}},
+    {1700,  { 1.23,    0.3,  675.3,    -47.0,  675.3,    -47.0,   1.23,    0.3}},
+    {1800,  { 1.43,   40.0,  649.8,    -47.7,  649.8,    -47.7,   1.43,   40.0}},
+    {1900,  { 1.63,   -1.0,  626.2,    -48.4,  626.2,    -48.4,   1.63,   -1.0}},
+    {2000,  { 1.83,   -2.0,  604.3,    -48.9,  604.3,    -48.9,   1.83,   -2.0}},
+    {2100,  { 2.03,   -3.3,  584.0,    -49.4,  584.0,    -49.4,   2.03,   -3.3}},
+    {2200,  { 2.23,   -3.6,  565.1,    -49.8,  565.1,    -49.8,   2.23,   -3.6}},
+    {2300,  { 2.44,   -4.3,  547.5,    -50.1,  547.5,    -50.1,   2.44,   -4.3}},
+    {2400,  { 2.64,   -5.0,  531.1,    -50.4,  531.1,    -50.4,   2.64,   -5.0}},
+    {2500,  { 2.84,   -6.1,  515.9,    -50.6,  515.9,    -50.6,   2.84,   -6.1}},
+    {2600,  { 3.05,   -6.6,  501.6,    -50.8,  501.6,    -50.8,   3.05,   -6.6}},
+    {2700,  { 3.25,   -7.3,  488.2,    -51.0,  488.2,    -51.0,   3.25,   -7.3}},
+    {2800,  { 3.45,   -7.6,  475.7,    -51.1,  475.7,    -51.1,   3.45,   -7.6}},
+    {2900,  { 3.65,   -8.3,  464.0,    -51.1,  464.0,    -51.1,   3.65,   -8.3}},
+    {3000,  { 3.85,   -8.6,  453.0,    -51.2,  453.0,    -51.2,   3.85,   -8.6}},
+    {3100,  { 4.04,   -9.3,  442.6,    -51.2,  442.6,    -51.2,   4.04,   -9.3}},
+    {3200,  { 4.24,  -10.3,  432.9,    -51.2,  432.9,    -51.2,   4.24,  -10.3}},
+    {3300,  { 4.43,  -10.6,  423.7,    -51.2,  423.7,    -51.2,   4.43,  -10.6}},
+    {3400,  { 4.62,  -11.3,  415.1,    -51.2,  415.1,    -51.2,   4.62,  -11.3}},
+    {3500,  { 4.81,  -11.6,  406.9,    -51.1,  406.9,    -51.1,   4.81,  -11.6}},
+    {3600,  { 5.00,  -12.3,  399.1,    -51.1,  399.1,    -51.1,   5.00,  -12.3}},
+    {3700,  { 5.19,  -13.0,  391.8,    -51.0,  391.8,    -51.0,   5.19,  -13.0}},
+    {3800,  { 5.37,  -13.4,  384.9,    -51.0,  384.9,    -51.0,   5.37,  -13.4}},
+    {3900,  { 5.56,  -13.8,  378.3,    -50.9,  378.3,    -50.9,   5.56,  -13.8}},
+    {4000,  { 5.74,  -14.4,  372.0,    -50.8,  372.0,    -50.8,   5.74,  -14.4}}
+};
+
+
+/* V.56bis ETSI LL-2, non-loaded loop */
+
+struct
+{
+    int freq;
+    float ad[8];
+} etsi_ll2[] =
+{
+    { 200,  {-0.10,   15.0,  850.3,     -3.4,  850.3,     -3.4,  -0.10,   15.0}},
+    { 300,  {-0.09,    8.0,  848.1,     -5.1,  848.1,     -5.1,  -0.09,    8.0}},
+    { 400,  {-0.09,    7.0,  845.1,     -6.7,  845.1,     -6.7,  -0.09,    7.0}},
+    { 500,  {-0.08,    5.0,  841.3,     -8.4,  841.3,     -8.4,  -0.08,    5.0}},
+    { 600,  {-0.07,    4.6,  836.7,    -10.0,  836.7,    -10.0,  -0.07,    4.6}},
+    { 700,  {-0.06,    4.3,  831.3,    -11.6,  831.3,    -11.6,  -0.06,    4.3}},
+    { 800,  {-0.04,    3.8,  825.3,    -13.2,  825.3,    -13.2,  -0.04,    3.8}},
+    { 900,  {-0.02,    3.4,  818.6,    -14.8,  818.6,    -14.8,  -0.02,    3.4}},
+    {1000,  { 1.80,    3.0,  811.4,    -16.3,  811.4,    -16.3,   1.8,     3.0}},
+    {1100,  { 0.02,    2.6,  803.6,    -17.8,  803.6,    -17.8,   0.02,    2.6}},
+    {1200,  { 0.04,    2.3,  795.3,    -19.3,  795.3,    -19.3,   0.04,    2.3}},
+    {1300,  { 0.06,    1.3,  786.6,    -20.7,  786.6,    -20.7,   0.06,    1.3}},
+    {1400,  { 0.09,    0.9,  777.5,    -22.1,  777.5,    -22.1,   0.09,    0.9}},
+    {1500,  { 0.12,    0.6,  768.1,    -23.5,  768.1,    -23.5,   0.12,    0.6}},
+    {1600,  { 0.15,    0.3,  758.4,    -24.8,  758.4,    -24.8,   0.15,    0.3}},
+    {1700,  { 0.18,    0.0,  748.4,    -26.1,  748.4,    -26.1,   0.18,    0.0}},
+    {1800,  { 0.21,     15,  738.4,    -27.3,  738.4,    -27.3,   0.21,   15.0}},
+    {1900,  { 0.24,   -1.0,  728.1,    -28.5,  728.1,    -28.5,   0.24,   -1.0}},
+    {2000,  { 0.28,   -2.3,  717.8,    -29.7,  717.8,    -29.7,   0.28,   -2.3}},
+    {2100,  { 0.32,   -2.6,  707.4,    -30.8,  707.4,    -30.8,   0.32,   -2.6}},
+    {2200,  { 0.36,   -3.0,  697.0,    -31.9,  697.0,    -31.9,   0.36,   -3.0}},
+    {2300,  { 0.40,   -3.3,  686.6,    -33.0,  686.6,    -33.0,   0.40,   -3.3}},
+    {2400,  { 0.44,   -3.6,  676.2,    -34.0,  676.2,    -34.0,   0.44,   -3.6}},
+    {2500,  { 0.48,   -4.5,  665.9,    -35.0,  665.9,    -35.0,   0.48,   -4.5}},
+    {2600,  { 0.53,   -5.4,  655.6,    -35.9,  655.6,    -35.9,   0.53,   -5.4}},
+    {2700,  { 0.57,   -6.3,  645.5,    -36.8,  645.5,    -36.8,   0.57,   -6.3}},
+    {2800,  { 0.62,   -6.6,  635.5,    -37.7,  635.5,    -37.7,   0.62,   -6.6}},
+    {2900,  { 0.67,   -6.9,  625.6,    -38.6,  625.6,    -38.6,   0.67,   -6.9}},
+    {3000,  { 0.72,   -7.5,  615.8,    -39.4,  615.8,    -39.4,   0.72,   -7.5}},
+    {3100,  { 0.77,   -8.3,  606.2,    -40.2,  606.2,    -40.2,   0.77,   -8.3}},
+    {3200,  { 0.82,   -8.6,  596.7,    -40.9,  596.7,    -40.9,   0.82,   -8.6}},
+    {3300,  { 0.87,   -9.3,  587.4,    -41.6,  587.4,    -41.6,   0.87,   -9.3}},
+    {3400,  { 0.92,   -9.6,  578.3,    -42.3,  578.3,    -42.3,   0.92,   -9.6}},
+    {3500,  { 0.98,  -10.3,  569.3,    -43.0,  569.3,    -43.0,   0.98,  -10.3}},
+    {3600,  { 1.03,  -10.6,  560.6,    -43.7,  560.6,    -43.7,   1.03,  -10.6}},
+    {3700,  { 1.09,  -11.3,  552.0,    -44.3,  552.0,    -44.3,   1.09,  -11.3}},
+    {3800,  { 1.14,  -11.6,  543.5,    -44.9,  543.5,    -44.9,   1.14,  -11.6}},
+    {3900,  { 1.20,  -12.3,  535.3,    -45.4,  535.3,    -45.4,   1.20,  -12.3}},
+    {4000,  { 1.26,  -13.3,  527.2,    -46.0,  527.2,    -46.0,   1.26,  -13.3}}
+};
+
+/* V.56bis AD-1 AD-5 AD-6 AD-7 AD-8 AD-9 */
+
+struct
+{
+    int freq;
+    float ad[6];
+} ad[] =
+{
+    {   0,  {90.0,   90.0,   90.0,   90.0,   90.0,   90.0}},
+    { 200,  { 6.0,    3.2,    3.0,    2.9,   11.6,   23.3}},
+    { 300,  { 1.3,    1.4,    1.2,    1.1,    6.9,   13.9}},
+    { 400,  { 0.0,    0.4,    0.3,    0.3,    4.0,    7.9}},
+    { 500,  { 0.0,   -0.1,    0.0,    0.1,    2.0,    4.1}},
+    { 600,  { 0.0,   -0.1,    0.0,    0.1,    1.2,    2.4}},
+    { 700,  { 0.0,    0.1,    0.0,    0.0,    0.8,    1.7}},
+    { 800,  { 0.0,    0.0,    0.0,   -0.1,    0.5,    1.1}},
+    { 900,  { 0.0,    0.0,    0.0,   -0.1,    0.2,    0.4}},
+    {1000,  { 0.0,    0.0,    0.0,    0.0,    0.0,    0.0}},
+    {1100,  { 0.0,    0.0,    0.1,    0.0,   -0.1,   -0.2}},
+    {1200,  { 0.0,    0.0,    0.1,    0.1,   -0.1,   -0.2}},
+    {1300,  { 0.0,    0.1,    0.2,    0.3,   -0.1,   -0.2}},
+    {1400,  { 0.0,    0.2,    0.3,    0.4,   -0.1,   -0.3}},
+    {1500,  { 0.0,    0.2,    0.3,    0.4,   -0.2,   -0.4}},
+    {1600,  { 0.0,    0.3,    0.5,    0.5,   -0.1,   -0.3}},
+    {1700,  { 0.0,    0.3,    0.5,    0.6,   -0.1,   -0.1}},
+    {1800,  { 0.0,    0.3,    0.5,    0.6,    0.0,    0.0}},
+    {1900,  { 0.0,    0.4,    0.7,    0.7,    0.1,    0.2}},
+    {2000,  { 0.0,    0.5,    0.8,    0.9,    0.2,    0.5}},
+    {2100,  { 0.1,    0.6,    1.0,    1.0,    0.5,    0.9}},
+    {2200,  { 0.2,    0.7,    1.1,    1.1,    0.6,    1.1}},
+    {2300,  { 0.3,    0.9,    1.2,    1.4,    0.8,    1.5}},
+    {2400,  { 0.4,    1.1,    1.5,    1.6,    0.9,    1.8}},
+    {2500,  { 0.5,    1.3,    1.8,    2.0,    1.1,    2.3}},
+    {2600,  { 0.6,    1.6,    2.4,    2.7,    1.4,    2.8}},
+    {2700,  { 0.7,    2.0,    3.0,    3.5,    1.7,    3.4}},
+    {2800,  { 0.7,    2.3,    3.5,    4.3,    2.0,    4.0}},
+    {2900,  { 0.9,    2.8,    4.2,    5.0,    2.4,    4.9}},
+    {3000,  { 1.1,    3.2,    4.9,    5.8,    3.0,    5.9}},
+    {3100,  { 1.2,    3.5,    5.6,    6.7,    3.4,    6.8}},
+    {3200,  { 1.3,    4.1,    6.7,    8.0,    3.9,    7.7}},
+    {3300,  { 1.6,    4.8,    8.0,    9.6,    4.6,    9.2}},
+    {3400,  { 1.8,    5.3,    9.1,   11.0,    5.4,   10.7}},
+    {3500,  { 2.4,    5.7,   10.3,   12.2,    6.3,   12.6}},
+    {3600,  { 3.0,    6.6,   12.1,   13.9,    7.8,   15.5}},
+    {3700,  { 5.7,    8.9,   15.8,   17.3,   10.3,   20.5}},
+    {3800,  {13.5,   15.7,   24.4,   25.7,   16.2,   32.4}},
+    {3900,  {31.2,   31.1,   42.2,   43.3,   29.9,   59.9}},
+    {4000,  {31.2,   31.1,   42.2,   43.3,   29.9,   59.9}}
+};
+
+/* V.56bis EDD-1 EDD-2 EDD-3 */
+
+struct
+{
+    int freq;
+    float edd[3];
+} edd[] =
+{
+    {   0,    {3.98,    3.76,    8.00}},
+    { 200,    {3.98,    3.76,    8.00}},
+    { 300,    {2.70,    3.76,    8.00}},
+    { 400,    {1.69,    2.20,    6.90}},
+    { 500,    {1.15,    1.36,    5.50}},
+    { 600,    {0.80,    0.91,    4.40}},
+    { 700,    {0.60,    0.64,    3.40}},
+    { 800,    {0.50,    0.46,    2.80}},
+    { 900,    {0.40,    0.34,    2.00}},
+    {1000,    {0.30,    0.24,    1.50}},
+    {1100,    {0.20,    0.16,    1.00}},
+    {1200,    {0.20,    0.11,    0.70}},
+    {1300,    {0.10,    0.07,    0.40}},
+    {1400,    {0.05,    0.05,    0.30}},
+    {1500,    {0.00,    0.03,    0.20}},
+    {1600,    {0.00,    0.01,    0.10}},
+    {1700,    {0.00,    0.00,    0.10}},
+    {1800,    {0.00,    0.00,    0.00}},
+    {1900,    {0.00,    0.02,    0.10}},
+    {2000,    {0.00,    0.04,    0.10}},
+    {2100,    {0.02,    0.08,    0.10}},
+    {2200,    {0.02,    0.12,    0.20}},
+    {2300,    {0.02,    0.16,    0.20}},
+    {2400,    {0.02,    0.20,    0.30}},
+    {2500,    {0.10,    0.27,    0.40}},
+    {2600,    {0.12,    0.36,    0.50}},
+    {2700,    {0.15,    0.47,    0.80}},
+    {2800,    {0.20,    0.60,    1.10}},
+    {2900,    {0.27,    0.77,    1.50}},
+    {3000,    {0.40,    1.01,    2.00}},
+    {3100,    {0.56,    1.32,    2.60}},
+    {3200,    {0.83,    1.78,    3.20}},
+    {3300,    {1.07,    1.78,    4.00}},
+    {3400,    {1.39,    1.78,    4.00}},
+    {3500,    {1.39,    1.78,    4.00}},
+    {3600,    {1.39,    1.78,    4.00}},
+    {3700,    {1.39,    1.78,    4.00}},
+    {3800,    {1.39,    1.78,    4.00}},
+    {3900,    {1.39,    1.78,    4.00}},
+    {4000,    {1.39,    1.78,    4.00}}
+};
+
+/* V.56bis PCM AD-1, AD-2, AD-3 */
+
+struct
+{
+    int freq;
+    float ad[3];
+} pcm_ad[] =
+{
+    {  50,    {41.4,    77.8,   114.2}},
+    { 100,    {15.5,    27.7,    39.9}},
+    { 150,    { 3.7,     6.1,     8.6}},
+    { 200,    { 0.5,     0.8,     1.0}},
+    { 250,    {-0.2,    -0.2,    -0.3}},
+    { 300,    {-0.2,    -0.3,    -0.4}},
+    { 400,    { 0.0,    -0.2,    -0.3}},
+    { 500,    {-0.2,    -0.4,    -0.5}},
+    { 600,    {-0.2,    -0.3,    -0.5}},
+    { 700,    {-0.2,    -0.3,    -0.5}},
+    { 800,    {-0.2,    -0.4,    -0.5}},
+    { 900,    {-0.2,    -0.3,    -0.4}},
+    {1000,    {-0.1,    -0.2,    -0.3}},
+    {1100,    {-0.2,    -0.3,    -0.3}},
+    {1200,    {-0.2,    -0.3,    -0.4}},
+    {1300,    {-0.2,    -0.3,    -0.5}},
+    {1400,    {-0.1,    -0.3,    -0.4}},
+    {1500,    {-0.1,    -0.3,    -0.4}},
+    {1600,    {-0.1,    -0.2,    -0.3}},
+    {1700,    {-0.1,    -0.3,    -0.4}},
+    {1800,    {-0.2,    -0.3,    -0.4}},
+    {1900,    {-0.2,    -0.3,    -0.3}},
+    {2000,    {-0.1,    -0.2,    -0.3}},
+    {2100,    {-0.1,    -0.2,    -0.3}},
+    {2200,    {-0.1,    -0.3,    -0.4}},
+    {2300,    {-0.1,    -0.1,    -0.2}},
+    {2400,    {-0.1,    -0.1,    -0.2}},
+    {2500,    { 0.0,    -0.1,    -0.1}},
+    {2600,    { 0.0,    -0.1,    -0.1}},
+    {2700,    { 0.0,     0.0,     0.1}},
+    {2800,    { 0.0,     0.0,     0.1}},
+    {2900,    { 0.1,     0.2,     0.2}},
+    {3000,    { 0.0,     0.0,     0.1}},
+    {3100,    { 0.0,     0.0,     0.0}},
+    {3200,    { 0.0,     0.0,     0.1}},
+    {3300,    { 0.3,     0.7,     1.0}},
+    {3400,    { 1.2,     2.4,     3.6}},
+    {3500,    { 3.2,     6.3,     9.5}},
+    {3550,    { 5.0,     9.6,    14.3}},
+    {3600,    { 7.0,    13.5,    19.9}},
+    {3650,    {10.0,    18.7,    27.5}},
+    {3700,    {13.4,    24.6,    35.8}},
+    {3750,    {18.1,    32.1,    46.2}},
+    {3800,    {24.3,    41.2,    58.2}},
+    {3850,    {32.5,    52.6,    72.7}},
+    {3900,    {43.4,    66.6,    89.8}},
+    {4000,    {43.4,    66.6,    89.8}}
+};
+
+/* V.56bis PCM EDD-1, EDD-2, EDD-3 */
+
+struct
+{
+    int freq;
+    float edd[3];
+} pcm_edd[] =
+{
+    { 150,   { 2.76,    5.5,    8.3}},
+    { 200,   { 1.70,    3.4,    5.1}},
+    { 250,   { 0.92,    1.8,    2.8}},
+    { 300,   { 0.55,    1.1,    1.7}},
+    { 400,   { 0.25,    0.5,    0.7}},
+    { 500,   { 0.12,    0.2,    0.4}},
+    { 600,   { 0.06,    0.1,    0.2}},
+    { 700,   { 0.03,    0.1,    0.1}},
+    { 800,   { 0.01,    0.0,    0.0}},
+    { 900,   { 0.00,    0.0,    0.0}},
+    {1000,   {-0.01,    0.0,    0.0}},
+    {1100,   {-0.01,    0.0,    0.0}},
+    {1200,   {-0.02,    0.0,   -0.1}},
+    {1300,   {-0.02,    0.0,   -0.1}},
+    {1400,   {-0.01,    0.0,    0.0}},
+    {1500,   {-0.01,    0.0,    0.0}},
+    {1600,   { 0.00,    0.0,    0.0}},
+    {1700,   { 0.00,    0.0,    0.0}},
+    {1800,   { 0.01,    0.0,    0.0}},
+    {1900,   { 0.02,    0.0,    0.0}},
+    {2000,   { 0.02,    0.0,    0.1}},
+    {2100,   { 0.04,    0.1,    0.1}},
+    {2200,   { 0.05,    0.1,    0.2}},
+    {2300,   { 0.06,    0.1,    0.2}},
+    {2400,   { 0.07,    0.1,    0.2}},
+    {2500,   { 0.10,    0.2,    0.3}},
+    {2600,   { 0.11,    0.2,    0.3}},
+    {2700,   { 0.14,    0.3,    0.4}},
+    {2800,   { 0.18,    0.4,    0.5}},
+    {2900,   { 0.22,    0.4,    0.6}},
+    {3000,   { 0.27,    0.5,    0.8}},
+    {3100,   { 0.34,    0.7,    1.0}},
+    {3200,   { 0.45,    0.9,    1.4}},
+    {3250,   { 0.52,    1.0,    1.6}},
+    {3300,   { 0.60,    1.2,    1.8}},
+    {3350,   { 0.66,    1.3,    2.0}},
+    {3400,   { 0.74,    1.5,    2.2}},
+    {3450,   { 0.79,    1.6,    2.4}},
+    {3500,   { 0.83,    1.7,    2.5}},
+    {3550,   { 0.84,    1.7,    2.5}},
+    {3600,   { 0.81,    1.6,    2.4}},
+    {3700,   { 0.81,    1.6,    2.4}},
+    {3800,   { 0.81,    1.6,    2.4}},
+    {3900,   { 0.81,    1.6,    2.4}},
+    {4000,   { 0.81,    1.6,    2.4}}
+};
+
+FILE *outfile;
+
+float impulse_responses[100][LINE_FILTER_SIZE];
+int filter_sets = 0;
+
+static void generate_ad_edd(void)
+{
+    float f;
+    float offset;
+    float amp;
+    float phase;
+    float delay;
+    float pw;
+#if defined(HAVE_FFTW3_H)
+    double in[FFT_SIZE][2];
+    double out[FFT_SIZE][2];
+#else
+    fftw_complex in[FFT_SIZE];
+    fftw_complex out[FFT_SIZE];
+#endif
+    fftw_plan p;
+    int i;
+    int j;
+    int k;
+    int l;
+
+#if defined(HAVE_FFTW3_H)
+    p = fftw_plan_dft_1d(FFT_SIZE, in, out, FFTW_BACKWARD, FFTW_ESTIMATE);
+#else
+    p = fftw_create_plan(FFT_SIZE, FFTW_BACKWARD, FFTW_ESTIMATE);
+#endif
+    for (j = 0;  j < 6;  j++)
+    {
+        for (k = 0;  k < 3;  k++)
+        {
+            for (i = 0;  i < FFT_SIZE;  i++)
+            {
+#if defined(HAVE_FFTW3_H)
+                in[i][0] =
+                in[i][1] = 0.0f;
+#else
+                in[i].re =
+                in[i].im = 0.0f;
+#endif
+            }
+            for (i = 1;  i < FFT_SIZE/2;  i++)
+            {
+                f = (float) i*SAMPLE_RATE/FFT_SIZE;
+                amp = 0.0f;
+                for (l = 0;  l < (int) (sizeof(ad)/sizeof(ad[0]));  l++)
+                {
+                    if (f < ad[l].freq)
+                        break;
+                }
+                if (l < (int) (sizeof(ad)/sizeof(ad[0])))
+                {
+                    offset = (f - ad[l - 1].freq)/(ad[l].freq - ad[l - 1].freq);
+                    amp = (1.0 - offset)*ad[l - 1].ad[j] + offset*ad[l].ad[j];
+                    amp = pow(10.0, -amp/20.0);
+                }
+                delay = 0.0f;
+                for (l = 0;  l < (int) (sizeof(edd)/sizeof(edd[0]));  l++)
+                {
+                    if (f < edd[l].freq)
+                        break;
+                }
+                if (l < (int) (sizeof(edd)/sizeof(edd[0])))
+                {
+                    offset = (f - edd[l - 1].freq)/(edd[l].freq - edd[l - 1].freq);
+                    delay = (1.0f - offset)*edd[l - 1].edd[k] + offset*edd[l].edd[k];
+                }
+                phase = 2.0f*M_PI*f*delay*0.001f;
+#if defined(HAVE_FFTW3_H)    
+                in[i][0] = amp*cosf(phase);
+                in[i][1] = amp*sinf(phase);
+                in[FFT_SIZE - i][0] = in[i][0];
+                in[FFT_SIZE - i][1] = -in[i][1];
+#else
+                in[i].re = amp*cosf(phase);
+                in[i].im = amp*sinf(phase);
+                in[FFT_SIZE - i].re = in[i].re;
+                in[FFT_SIZE - i].im = -in[i].im;
+#endif
+            }
+#if 0
+            for (i = 0;  i < FFT_SIZE;  i++)
+                fprintf(outfile, "%5d %15.5f,%15.5f\n", i, in[i].re, in[i].im);
+#endif
+#if defined(HAVE_FFTW3_H)    
+            fftw_execute(p);
+#else
+            fftw_one(p, in, out);
+#endif
+
+            fprintf(outfile, "/* V.56bis AD-%d, EDD%d */\n", (j == 0)  ?  1  :  j + 4, k + 1);
+
+            fprintf(outfile, "float ad_%d_edd_%d_model[] =\n", (j == 0)  ?  1  :  j + 4, k + 1);
+            fprintf(outfile, "{\n");
+            /* Normalise the filter's gain */
+            pw = 0.0f;
+            l = FFT_SIZE - (LINE_FILTER_SIZE - 1)/2;
+            for (i = 0;  i < LINE_FILTER_SIZE;  i++)
+            {
+#if defined(HAVE_FFTW3_H)
+                pw += out[l][0]*out[l][0];
+#else
+                pw += out[l].re*out[l].re;
+#endif
+                if (++l == FFT_SIZE)
+                    l = 0;
+            }
+            pw = sqrt(pw);
+            l = FFT_SIZE - (LINE_FILTER_SIZE - 1)/2;
+            for (i = 0;  i < LINE_FILTER_SIZE;  i++)
+            {
+                
+#if defined(HAVE_FFTW3_H)
+                impulse_responses[filter_sets][i] = out[l][0]/pw;
+#else
+                impulse_responses[filter_sets][i] = out[l].re/pw;
+#endif
+                fprintf(outfile, "%15.5f,\n", impulse_responses[filter_sets][i]);
+                if (++l == FFT_SIZE)
+                    l = 0;
+            }
+            fprintf(outfile, "};\n\n");
+            filter_sets++;
+        }
+    }
+}
+
+static void generate_proakis(void)
+{
+    float f;
+    float f1;
+    float offset;
+    float amp;
+    float phase;
+    float delay;
+    float pw;
+    int index;
+    int i;
+    int l;
+#if defined(HAVE_FFTW3_H)
+    double in[FFT_SIZE][2];
+    double out[FFT_SIZE][2];
+#else
+    fftw_complex in[FFT_SIZE];
+    fftw_complex out[FFT_SIZE];
+#endif
+    fftw_plan p;
+
+#if defined(HAVE_FFTW3_H)
+    p = fftw_plan_dft_1d(FFT_SIZE, in, out, FFTW_BACKWARD, FFTW_ESTIMATE);
+#else
+    p = fftw_create_plan(FFT_SIZE, FFTW_BACKWARD, FFTW_ESTIMATE);
+#endif
+    for (i = 0;  i < FFT_SIZE;  i++)
+    {
+#if defined(HAVE_FFTW3_H)
+        in[i][0] =
+        in[i][1] = 0.0f;
+#else
+        in[i].re =
+        in[i].im = 0.0f;
+#endif
+    }
+    for (i = 1;  i < FFT_SIZE/2;  i++)
+    {
+        f = (float) i*SAMPLE_RATE/FFT_SIZE;
+        f1 = f/200.0f;
+        offset = f1 - floor(f1);
+        index = (int) floor(f1);
+
+        /* Linear interpolation */
+        amp = ((1.0f - offset)*proakis[index].amp + offset*proakis[index + 1].amp)/2.3f;
+        delay = (1.0f - offset)*proakis[index].delay + offset*proakis[index + 1].delay;
+        phase = 2.0f*M_PI*f*delay*0.001f;
+#if defined(HAVE_FFTW3_H)
+        in[i][0] = amp*cosf(phase);
+        in[i][1] = amp*sinf(phase);
+        in[FFT_SIZE - i][0] = in[i][0];
+        in[FFT_SIZE - i][1] = -in[i][1];
+#else
+        in[i].re = amp*cosf(phase);
+        in[i].im = amp*sinf(phase);
+        in[FFT_SIZE - i].re = in[i].re;
+        in[FFT_SIZE - i].im = -in[i].im;
+#endif
+    }
+
+#if defined(HAVE_FFTW3_H)
+    fftw_execute(p);
+#else
+    fftw_one(p, in, out);
+#endif
+
+    fprintf(outfile, "/* Medium range telephone line response\n");
+    fprintf(outfile, "   (from p 537, Digital Communication, John G. Proakis */\n");
+
+    fprintf(outfile, "float proakis_line_model[] =\n");
+    fprintf(outfile, "{\n");
+    /* Normalise the filter's gain */
+    pw = 0.0f;
+    l = FFT_SIZE - (LINE_FILTER_SIZE - 1)/2;
+    for (i = 0;  i < LINE_FILTER_SIZE;  i++)
+    {
+#if defined(HAVE_FFTW3_H)
+        pw += out[l][0]*out[l][0];
+#else
+        pw += out[l].re*out[l].re;
+#endif
+        if (++l == FFT_SIZE)
+            l = 0;
+    }
+    pw = sqrt(pw);
+    l = FFT_SIZE - (LINE_FILTER_SIZE - 1)/2;
+    for (i = 0;  i < LINE_FILTER_SIZE;  i++)
+    {
+#if defined(HAVE_FFTW3_H)
+        impulse_responses[filter_sets][i] = out[l][0]/pw;
+#else
+        impulse_responses[filter_sets][i] = out[l].re/pw;
+#endif
+        fprintf(outfile, "%15.5f,\n", impulse_responses[filter_sets][i]);
+        if (++l == FFT_SIZE)
+            l = 0;
+    }
+    fprintf(outfile, "};\n\n");
+    filter_sets++;
+}
+
+int main(int argc, char *argv[])
+{
+    int i;
+    int j;
+
+    if ((outfile = fopen(LINE_MODEL_FILE_NAME, "w")) == NULL)
+    {
+        fprintf(stderr, "Failed to open %s\n", "line_model.txt");
+        exit(2);
+    }
+
+    generate_proakis();
+    generate_ad_edd();
+    
+    fclose(outfile);
+    
+    if (argc > 1)
+    {
+        for (i = 0;  i < LINE_FILTER_SIZE;  i++)
+        {
+            printf("%d, ", i);
+            for (j = 0;  j < filter_sets;  j++)
+            {
+                printf("%15.5f, ", impulse_responses[j][i]);
+            }
+            printf("\n");
+        }
+    }
+    return 0;
+}

Added: freeswitch/trunk/libs/spandsp/spandsp-sim/rfc2198_sim.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/spandsp-sim/rfc2198_sim.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,176 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * rfc2198_sim.c - Simulate the behaviour of RFC2198 (or UDPTL) redundancy.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2007 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: rfc2198_sim.c,v 1.6 2008/07/02 14:48:25 steveu Exp $
+ */
+
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <unistd.h>
+#include <inttypes.h>
+#include <string.h>
+#include <time.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <audiofile.h>
+#include "floating_fudge.h"
+#if defined(HAVE_TGMATH_H)
+#include <tgmath.h>
+#endif
+#if defined(HAVE_MATH_H)
+#define GEN_CONST
+#include <math.h>
+#endif
+
+#include "spandsp.h"
+#include "spandsp/g1050.h"
+#include "spandsp/rfc2198_sim.h"
+
+#define PACKET_LOSS_TIME    -1
+
+#define FALSE 0
+#define TRUE (!FALSE)
+
+rfc2198_sim_state_t *rfc2198_sim_init(int model,
+                                      int speed_pattern,
+                                      int packet_size,
+                                      int packet_rate,
+                                      int redundancy_depth)
+{
+    rfc2198_sim_state_t *s;
+
+    if ((s = (rfc2198_sim_state_t *) malloc(sizeof(*s))) == NULL)
+        return NULL;
+    memset(s, 0, sizeof(*s));
+
+    s->g1050 = g1050_init(model, speed_pattern, packet_size, packet_rate);
+    s->redundancy_depth = redundancy_depth;
+    return s;
+}
+/*- End of function --------------------------------------------------------*/
+
+int rfc2198_sim_put(rfc2198_sim_state_t *s,
+                    const uint8_t buf[],
+                    int len,
+                    int seq_no,
+                    double departure_time)
+{
+    uint8_t buf2[8192];
+    uint8_t *p;
+    uint16_t *q;
+    int slot;
+    int i;
+
+    /* Save the packet in the history buffer */
+    memcpy(s->tx_pkt[s->next_pkt], buf, len);
+    s->tx_pkt_len[s->next_pkt] = len;
+    s->tx_pkt_seq_no[s->next_pkt] = seq_no;
+    
+    /* Construct the redundant packet */
+    p = buf2;
+    slot = s->next_pkt;
+    q = (uint16_t *) p;
+    *q = s->redundancy_depth;
+    p += sizeof(uint16_t);
+    for (i = 0;  i < s->redundancy_depth;  i++)
+    {
+        q = (uint16_t *) p;
+        *q = s->tx_pkt_len[slot];
+        p += sizeof(uint16_t);
+        memcpy(p, s->tx_pkt[slot], s->tx_pkt_len[slot]);
+        p += s->tx_pkt_len[slot];
+        slot = (slot - 1) & 0x1F;
+    }
+    s->next_pkt = (s->next_pkt + 1) & 0x1F;
+    return g1050_put(s->g1050, buf2, p - buf2, seq_no, departure_time);
+}
+/*- End of function --------------------------------------------------------*/
+
+int rfc2198_sim_get(rfc2198_sim_state_t *s,
+                    uint8_t buf[],
+                    int max_len,
+                    double current_time,
+                    int *seq_no,
+                    double *departure_time,
+                    double *arrival_time)
+{
+    int len;
+    int lenx;
+    int seq_nox;
+    int i;
+    uint8_t bufx[s->redundancy_depth*1024];
+    uint8_t *p;
+    uint16_t *q;
+    int redundancy_depth;
+    
+    if (s->rx_queued_pkts)
+    {
+        /* We have some stuff from the last g1050_get() still to deliver */
+        s->rx_queued_pkts--;
+        memcpy(buf, s->rx_pkt[s->rx_queued_pkts], s->rx_pkt_len[s->rx_queued_pkts]);
+        *seq_no = s->rx_pkt_seq_no[s->rx_queued_pkts];
+        return s->rx_pkt_len[s->rx_queued_pkts];
+    }
+    len = g1050_get(s->g1050, bufx, s->redundancy_depth*1024, current_time, &seq_nox, departure_time, arrival_time);
+    if (len > 0)
+    {
+        p = bufx;
+        q = (uint16_t *) p;
+        redundancy_depth = *q;
+        p += sizeof(uint16_t);
+        i = 0;
+        if (seq_nox > s->next_seq_no)
+        {
+            /* Some stuff is missing. Try to fill it in. */
+            s->rx_queued_pkts = seq_nox - s->next_seq_no;
+            if (s->rx_queued_pkts >= redundancy_depth)
+                s->rx_queued_pkts = redundancy_depth - 1;
+            for (i = 0;  i < s->rx_queued_pkts;  i++)
+            {
+                q = (uint16_t *) p;
+                s->rx_pkt_len[i] = *q;
+                p += sizeof(uint16_t);
+                memcpy(s->rx_pkt[i], p, s->rx_pkt_len[i]);
+                s->rx_pkt_seq_no[i] = seq_nox - i;
+                p += s->rx_pkt_len[i];
+            }
+        }
+        *seq_no = seq_nox - i;
+        q = (uint16_t *) p;
+        lenx = *q;
+        p += sizeof(uint16_t);
+        memcpy(buf, p, lenx);
+        s->next_seq_no = seq_nox + 1;
+    }
+    else
+    {
+        lenx = len;
+    }
+    return lenx;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/spandsp-sim/spandsp-sim.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/spandsp-sim/spandsp-sim.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,45 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * spandsp-sim.h - The head guy amongst the simulator headers
+ *
+ * Written by Steve Underwood <spandsp/steveu at coppice.org>
+ *
+ * Copyright (C) 2007 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: spandsp-sim.h,v 1.5 2008/04/26 13:39:16 steveu Exp $
+ */
+
+/*! \file */
+
+#if !defined(_SPANDSP_SIM_H_)
+#define _SPANDSP_SIM_H_
+
+#include <inttypes.h>
+#include <limits.h>
+#include <time.h>
+#include <tiffio.h>
+
+#include <spandsp/g1050.h>
+#include <spandsp/rfc2198_sim.h>
+#include <spandsp/test_utils.h>
+#include <spandsp/line_model.h>
+#include <spandsp/line_models.h>
+
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/spandsp-sim/spandsp/g1050.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/spandsp-sim/spandsp/g1050.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,300 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * g1050.h - IP network modeling, as per G.1050/TIA-921.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2007 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: g1050.h,v 1.7 2008/04/17 18:03:23 steveu Exp $
+ */
+
+/*! \file */
+
+/*! \page g1050_ip_network_model_page G.1050/TIA-921 IP network path model
+\section g1050_ip_network_model_page_sec_1 What does it do?
+The ITU G.1050 specification defines a model of an IP network, appropriate
+for the testing of how streaming media woud behave across the internet. The
+model is based on a path having 5 segments:
+ - a local LAN (wired or wireless)
+ - an access link to the internet
+ - an internet of arbitrary complexity
+ - an access link from the internet
+ - a distant LAN (wired or wireless)
+The impairments typical of these segments at various service levels are modelled.
+8 standard service level behaviours are defined, covering lightly loaded to heavily
+congested levels. 133 standard sets of link speeds are defined, covering typical
+wired and wireless LAN, broadband access link, and backbone characteristics.
+
+The G.1050 model is suitable for testing the behaviour of RTP, UDPTL and other streaming
+protocols for packet loss and jitter behaviour.
+*/
+
+#if !defined(_G1050_H_)
+#define _G1050_H_
+
+/* This is the time slice at which delays, packet loss, etc. are calculated. */
+#define G1050_TICKS_PER_SEC         1000
+
+/* Search back 200 ms to preserve order of legitimately out of sequence packets. */
+#define SEARCHBACK_PERIOD           200
+
+#define G1050_LOW_LOSS      0
+#define G1050_HIGH_LOSS     1
+
+#define G1050_LAN_LINK      1
+#define G1050_ACCESS_LINK   2
+
+/*! Segment constants, as defined in G.1050. */
+typedef struct
+{
+    /*! Probability of changing from low to high and high to low loss states */
+    double prob_loss_rate_change[2];
+    /*! Probability of an impulse in the low and high loss states */
+    double prob_impulse[2][2];
+
+    /*! Impulse height, based on MTU and bit rate */
+    double impulse_height;
+    /*! Impulse decay coefficient for the single pole IIR filter. */
+    double impulse_coeff;
+
+    /*! Probability of packet loss due to occupancy. */
+    double prob_packet_loss;
+    /*! Probability of packet loss due to a multiple access collision. */
+    double prob_packet_collision_loss;
+} g1050_segment_constants_t;
+
+/*! End-to-end constants, as defined in G.1050. */
+typedef struct
+{
+    g1050_segment_constants_t segment[4];
+} g1050_constants_t;
+
+/*! The model definition for a LAN or access link segment */
+typedef struct
+{
+    /*! Percentage occupancy of the media */
+    double percentage_occupancy;
+    /*! MTU of the media */
+    int mtu;
+    /*! Maximum jitter in the segment. */
+    double max_jitter;
+} g1050_segment_model_t;
+
+/*! The model definition for the core network (backbone) segment */
+typedef struct
+{
+    /*! Basic delay of the backbone for regional paths */
+    double base_regional_delay;
+    /*! Basic delay of the backbone for intercontinental paths */
+    double base_intercontinental_delay;
+    /*! Percentage packet loss of the backbone */
+    /*! Percentage packet loss of the backbone. */
+    double percentage_packet_loss;
+    /*! Maximum jitter in the backbone. */
+    double max_jitter;
+    /*! Interval between the backbone route flapping between two paths, in seconds. */
+    double route_flap_interval;
+    /*! The difference in backbone delay between the two routes we flap between, in seconds. */
+    double route_flap_delay;
+    /*! The interval between link failures. */
+    double link_failure_interval;
+    /*! The duration of link failures. */
+    double link_failure_duration;
+    /*! Probability of packet loss in the backbone. */
+    double prob_packet_loss;
+    /*! Probability of a packet going out of sequence in the backbone. */
+    double prob_oos;
+} g1050_core_model_t;
+
+/*! The model definition for a complete end-to-end path */
+typedef struct
+{
+    /*! The likelyhood of occurance probabilities for the A, B and C scenarios defined in G.1050 */
+    int loo[3];
+    g1050_segment_model_t sidea_lan;
+    g1050_segment_model_t sidea_access_link;
+    g1050_core_model_t core;
+    g1050_segment_model_t sideb_access_link;
+    g1050_segment_model_t sideb_lan;
+} g1050_model_t;
+
+/*! The speed model for a complete end-to-end path */
+typedef struct
+{
+    int sidea_lan_bit_rate;
+    int sidea_lan_multiple_access;
+    int sidea_access_link_bit_rate_ab;
+    int sidea_access_link_bit_rate_ba;
+    int sidea_access_link_qos_enabled;
+    int sideb_lan_bit_rate;
+    int sideb_lan_multiple_access;
+    int sideb_access_link_bit_rate_ab;
+    int sideb_access_link_bit_rate_ba;
+    int sideb_access_link_qos_enabled;
+    double loo;
+} g1050_channel_speeds_t;
+
+/*! The model state for a LAN or access link segment */
+typedef struct
+{
+    /*! The type of link, G1050_LAN_LINK or G_1050_ACCESS_LINK */
+    int link_type;
+    /*! 1 if in the high loss state, or 0 if in the low loss state. */
+    int high_loss;
+
+    /*! The probability of a loss rate change, for both loss rate states. */
+    double prob_loss_rate_change[2];
+    /*! The probability of a impulse occuring, for both loss rate states. */
+    double prob_impulse[2];
+
+    /*! The maximum permitted height of impulses. */
+    double impulse_height;
+    /*! The impulse decay coefficient. */
+    double impulse_coeff;
+
+    /*! The basic serial delay due to the link. */
+    double serial_delay;
+    /*! Peak jitter in the segment. */
+    double max_jitter;
+    /*! The probability of packet loss. */
+    double prob_packet_loss;
+    /*! The probability of packet loss due to collision. */
+    double prob_packet_collision_loss;
+    /*! The maximum addition delay due to congestion. */
+    double congestion_delay;
+
+    /*! TRUE if QoS is enabled on the link. */
+    int qos_enabled;
+    /*! TRUE if the link is a multiple access type (e.g. an ethernet hub). */
+    int multiple_access;
+
+    /*! The latest packet arrival time seen on the link. */
+    double last_arrival_time;
+
+    /*! 3 seconds of predicted delays for the link */
+    double delays[3*G1050_TICKS_PER_SEC];
+    
+    /*! A count of packets lost on the link. */
+    uint32_t lost_packets;
+    /*! An extra debug count of packets lost on the link. */
+    uint32_t lost_packets_2;
+} g1050_segment_state_t;
+
+/*! The model state for the core network (backbone) segment */
+typedef struct
+{
+    /* Router model. */
+    int32_t route_flap_counter;
+    int32_t route_flap_interval;
+    double route_flap_delta;
+
+    /* Link failure model. */
+    int32_t link_failure_counter;
+    int32_t link_recovery_counter;
+
+    int32_t link_failure_interval_ticks;
+    int32_t link_failure_duration_ticks;
+
+    /*! Basic backbone delay */
+    double base_delay;
+    /*! Peak jitter in the backbone delay */
+    double max_jitter;
+    /*! Probability of packet loss in the backbone, in percent */
+    double prob_packet_loss;
+    /*! Probability of a packet going out of sequence in the backbone. */
+    double prob_oos;
+
+    /*! The latest packet arrival time seen on the link. */
+    double last_arrival_time;
+    double delay_delta;
+
+    /*! 3 seconds of predicted delays for the link */
+    double delays[3*G1050_TICKS_PER_SEC];
+
+    /*! A count of packets lost on the link. */
+    uint32_t lost_packets;
+    /*! An extra debug count of packets lost on the link. */
+    uint32_t lost_packets_2;
+} g1050_core_state_t;
+
+/*! The definition of an element in the packet queue */
+typedef struct g1050_queue_element_s
+{
+    struct g1050_queue_element_s *next;
+    struct g1050_queue_element_s *prev;
+    int seq_no;
+    double departure_time;
+    double arrival_time;
+    int len;
+    uint8_t pkt[0];
+} g1050_queue_element_t;
+
+/*! The model definition for a complete end-to-end path */
+typedef struct
+{
+    int packet_rate;
+    int packet_size;
+    float base_time;
+    g1050_segment_state_t segment[4];
+    g1050_core_state_t core;
+    double arrival_times_1[3*G1050_TICKS_PER_SEC];
+    double arrival_times_2[3*G1050_TICKS_PER_SEC];
+    g1050_queue_element_t *first;
+    g1050_queue_element_t *last;
+} g1050_state_t;
+
+extern g1050_constants_t g1050_constants[1];
+extern g1050_channel_speeds_t g1050_speed_patterns[133];
+extern g1050_model_t g1050_standard_models[9];
+
+#ifdef  __cplusplus
+extern "C"
+{
+#endif
+
+g1050_state_t *g1050_init(int model,
+                          int speed_pattern,
+                          int packet_size,
+                          int packet_rate);
+
+void g1050_dump_parms(int model, int speed_pattern);
+
+int g1050_put(g1050_state_t *s,
+              const uint8_t buf[],
+              int len,
+              int seq_no,
+              double departure_time);
+
+int g1050_get(g1050_state_t *s,
+              uint8_t buf[],
+              int max_len,
+              double current_time,
+              int *seq_no,
+              double *departure_time,
+              double *arrival_time);
+
+void g1050_queue_dump(g1050_state_t *s);
+
+#ifdef  __cplusplus
+}
+#endif
+
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/spandsp-sim/spandsp/line_model.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/spandsp-sim/spandsp/line_model.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,170 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * line_model.h - Model a telephone line.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2004 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: line_model.h,v 1.3 2008/04/17 18:03:23 steveu Exp $
+ */
+
+/*! \file */
+
+/*! \page line_model_page Telephone line model
+\section line_model_page_sec_1 What does it do?
+The telephone line modelling module provides simple modelling of one way and two
+way telephone lines.
+
+The path being modelled is:
+
+    -    terminal
+    -      | < hybrid echo (2-way models)
+    -      |
+    -      | < noise and filtering
+    -      |
+    -      | < hybrid echo (2-way models)
+    -     CO
+    -      |
+    -      | < A-law distortion + bulk delay
+    -      |
+    -     CO
+    -      | < hybrid echo (2-way models)
+    -      |
+    -      | < noise and filtering
+    -      |
+    -      | < hybrid echo (2-way models)
+    -    terminal
+*/
+
+#if !defined(_SPANDSP_LINE_MODEL_H_)
+#define _SPANDSP_LINE_MODEL_H_
+
+#define LINE_FILTER_SIZE 129
+
+/*!
+    One way line model descriptor. This holds the complete state of
+    a line model with transmission in only one direction.
+*/
+typedef struct
+{
+    codec_munge_state_t *munge;
+
+    /*! The coefficients for the near end analogue section simulation filter */
+    float *near_filter;
+    /*! The number of coefficients for the near end analogue section simulation filter */
+    int near_filter_len;
+    /*! Last transmitted samples (ring buffer, used by the line filter) */
+    float near_buf[LINE_FILTER_SIZE];
+    /*! Pointer of the last transmitted sample in buf */
+    int near_buf_ptr;
+    /*! The noise source for local analogue section of the line */
+    awgn_state_t near_noise;
+
+    /*! The bulk delay of the path, in samples */
+    int bulk_delay;
+    /*! A pointer to the current write position in the bulk delay store. */
+    int bulk_delay_ptr;
+    /*! The data store for simulating the bulk delay */
+    int16_t bulk_delay_buf[8000];
+
+    /*! The coefficients for the far end analogue section simulation filter */
+    float *far_filter;
+    /*! The number of coefficients for the far end analogue section simulation filter */
+    int far_filter_len;
+    /*! Last transmitted samples (ring buffer, used by the line filter) */
+    float far_buf[LINE_FILTER_SIZE];
+    /*! Pointer of the last transmitted sample in buf */
+    int far_buf_ptr;
+    /*! The noise source for distant analogue section of the line */
+    awgn_state_t far_noise;
+
+    /*! The scaling factor for the local CPE hybrid echo */
+    float near_cpe_hybrid_echo;
+    /*! The scaling factor for the local CO hybrid echo */
+    float near_co_hybrid_echo;
+
+    /*! The scaling factor for the far CPE hybrid echo */
+    float far_cpe_hybrid_echo;
+    /*! The scaling factor for the far CO hybrid echo */
+    float far_co_hybrid_echo;
+    /*! DC offset impairment */
+    float dc_offset;
+    
+    /*! Mains pickup impairment */
+    int mains_interference;
+    tone_gen_state_t mains_tone;
+} one_way_line_model_state_t;
+
+/*!
+    Two way line model descriptor. This holds the complete state of
+    a line model with transmission in both directions.
+*/
+typedef struct
+{
+    one_way_line_model_state_t line1;
+    one_way_line_model_state_t line2;
+    float fout1;
+    float fout2; 
+} both_ways_line_model_state_t;
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+void both_ways_line_model(both_ways_line_model_state_t *s, 
+                          int16_t output1[],
+                          const int16_t input1[],
+                          int16_t output2[],
+                          const int16_t input2[],
+                          int samples);
+
+void both_ways_line_model_set_dc(both_ways_line_model_state_t *s, float dc1, float dc2);
+
+void both_ways_line_model_set_mains_pickup(both_ways_line_model_state_t *s, int f, float level1, float level2);
+    
+both_ways_line_model_state_t *both_ways_line_model_init(int model1,
+                                                        float noise1,
+                                                        int model2,
+                                                        float noise2,
+                                                        int codec,
+                                                        int rbs_pattern);
+
+int both_ways_line_model_release(both_ways_line_model_state_t *s);
+
+void one_way_line_model(one_way_line_model_state_t *s, 
+                        int16_t output[],
+                        const int16_t input[],
+                        int samples);
+
+void one_way_line_model_set_dc(one_way_line_model_state_t *s, float dc);
+
+void one_way_line_model_set_mains_pickup(one_way_line_model_state_t *s, int f, float level);
+
+one_way_line_model_state_t *one_way_line_model_init(int model, float noise, int codec, int rbs_pattern);
+
+int one_way_line_model_release(one_way_line_model_state_t *s);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/spandsp-sim/spandsp/line_models.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/spandsp-sim/spandsp/line_models.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,63 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * line_model.h - Model a telephone line.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2004 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: line_models.h,v 1.3 2008/04/17 18:03:23 steveu Exp $
+ */
+
+/*! \file */
+
+#if !defined(_SPANDSP_LINE_MODELS_H_)
+#define _SPANDSP_LINE_MODELS_H_
+
+extern float proakis_line_model[];
+extern float ad_1_edd_1_model[];
+extern float ad_1_edd_2_model[];
+extern float ad_1_edd_3_model[];
+extern float ad_5_edd_1_model[];
+extern float ad_5_edd_2_model[];
+extern float ad_5_edd_3_model[];
+extern float ad_6_edd_1_model[];
+extern float ad_6_edd_2_model[];
+extern float ad_6_edd_3_model[];
+extern float ad_7_edd_1_model[];
+extern float ad_7_edd_2_model[];
+extern float ad_7_edd_3_model[];
+extern float ad_8_edd_1_model[];
+extern float ad_8_edd_2_model[];
+extern float ad_8_edd_3_model[];
+extern float ad_9_edd_1_model[];
+extern float ad_9_edd_2_model[];
+extern float ad_9_edd_3_model[];
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/spandsp-sim/spandsp/rfc2198_sim.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/spandsp-sim/spandsp/rfc2198_sim.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,97 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * rfc2198_sim.h - Simulate the behaviour of RFC2198 (or UDPTL) redundancy.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2007 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: rfc2198_sim.h,v 1.3 2008/04/17 18:03:23 steveu Exp $
+ */
+
+/*! \file */
+
+/*! \page rfc2198_model_page RFC2198 simulation
+\section rfc2198_model_page_sec_1 What does it do?
+*/
+
+#if !defined(_RFC2198_SIM_H_)
+#define _RFC2198_SIM_H_
+
+/*! The definition of an element in the packet queue */
+typedef struct rfc2198_sim_queue_element_s
+{
+    struct rfc2198_sim_queue_element_s *next;
+    struct rfc2198_sim_queue_element_s *prev;
+    int seq_no;
+    double departure_time;
+    double arrival_time;
+    int len;
+    uint8_t pkt[0];
+} rfc2198_sim_queue_element_t;
+
+/*! The model definition for a complete end-to-end path */
+typedef struct
+{
+    int redundancy_depth;
+    int next_seq_no;
+    g1050_state_t *g1050;
+    rfc2198_sim_queue_element_t *first;
+    rfc2198_sim_queue_element_t *last;
+    uint8_t tx_pkt[32][1024];
+    int tx_pkt_len[32];
+    int tx_pkt_seq_no[32];
+    int next_pkt;
+    uint8_t rx_pkt[32][1024];
+    int rx_pkt_len[32];
+    int rx_pkt_seq_no[32];
+    int rx_queued_pkts;
+} rfc2198_sim_state_t;
+
+#ifdef  __cplusplus
+extern "C"
+{
+#endif
+
+rfc2198_sim_state_t *rfc2198_sim_init(int model,
+                                      int speed_pattern,
+                                      int packet_size,
+                                      int packet_rate,
+                                      int redundancy_depth);
+
+int rfc2198_sim_put(rfc2198_sim_state_t *s,
+                    const uint8_t buf[],
+                    int len,
+                    int seq_no,
+                    double departure_time);
+
+int rfc2198_sim_get(rfc2198_sim_state_t *s,
+                    uint8_t buf[],
+                    int max_len,
+                    double current_time,
+                    int *seq_no,
+                    double *departure_time,
+                    double *arrival_time);
+
+#ifdef  __cplusplus
+}
+#endif
+
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/spandsp-sim/spandsp/test_utils.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/spandsp-sim/spandsp/test_utils.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,77 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * test_utils.h - Utility routines for module tests.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2006 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: test_utils.h,v 1.5 2008/08/29 09:28:13 steveu Exp $
+ */
+
+/*! \file */
+
+#if !defined(_TEST_UTILS_H_)
+#define _TEST_UTILS_H_
+
+enum
+{
+    MUNGE_CODEC_NONE = 0,
+    MUNGE_CODEC_ALAW,
+    MUNGE_CODEC_ULAW,
+    MUNGE_CODEC_G726_40K,
+    MUNGE_CODEC_G726_32K,
+    MUNGE_CODEC_G726_24K,
+    MUNGE_CODEC_G726_16K,
+};
+
+typedef struct codec_munge_state_s codec_munge_state_t;
+
+typedef struct complexify_state_s complexify_state_t;
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+complexify_state_t *complexify_init(void);
+
+void complexify_release(complexify_state_t *s);
+
+complexf_t complexify(complexify_state_t *s, int16_t amp);
+
+void fft(complex_t data[], int len);
+
+void ifft(complex_t data[], int len);
+
+codec_munge_state_t *codec_munge_init(int codec, int info);
+
+void codec_munge_release(codec_munge_state_t *s);
+
+void codec_munge(codec_munge_state_t *s, int16_t amp[], int len);
+
+AFfilehandle afOpenFile_telephony_read(const char *name, int channels);
+
+AFfilehandle afOpenFile_telephony_write(const char *name, int channels);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/spandsp-sim/test_utils.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/spandsp-sim/test_utils.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,408 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * test_utils.c - Utility routines for module tests.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2006 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: test_utils.c,v 1.8 2008/08/29 09:28:13 steveu Exp $
+ */
+
+/*! \file */
+
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <inttypes.h>
+#include <string.h>
+#include <stdio.h>
+#include "floating_fudge.h"
+#if defined(HAVE_TGMATH_H)
+#include <tgmath.h>
+#endif
+#if defined(HAVE_MATH_H)
+#include <math.h>
+#endif
+#include <time.h>
+#include <fcntl.h>
+#include <audiofile.h>
+
+#include "spandsp.h"
+#include "spandsp-sim.h"
+
+#define MAX_FFT_LEN     8192
+
+struct codec_munge_state_s
+{
+    int munging_codec;
+    g726_state_t g726_enc_state;
+    g726_state_t g726_dec_state;
+    int rbs_pattern;
+    int sequence;
+};
+
+struct complexify_state_s
+{
+    float history[128];
+    int ptr;
+};
+
+static complex_t circle[MAX_FFT_LEN/2];
+static int circle_init = FALSE;
+static complex_t icircle[MAX_FFT_LEN/2];
+static int icircle_init = FALSE;
+
+complexify_state_t *complexify_init(void)
+{
+    complexify_state_t *s;
+    int i;
+
+    if ((s = (complexify_state_t *) malloc(sizeof(*s))))
+    {
+        s->ptr = 0;
+        for (i = 0;  i < 128;  i++)
+            s->history[i] = 0.0f;
+    }
+    return s;
+}
+/*- End of function --------------------------------------------------------*/
+
+void complexify_release(complexify_state_t *s)
+{
+    free(s);
+}
+/*- End of function --------------------------------------------------------*/
+
+complexf_t complexify(complexify_state_t *s, int16_t amp)
+{
+#define HILBERT_GAIN    1.569546344
+    static const float hilbert_coeffs[] =
+    {
+        +0.0012698413f, +0.0013489483f,
+        +0.0015105196f, +0.0017620440f,
+        +0.0021112899f, +0.0025663788f,
+        +0.0031358856f, +0.0038289705f,
+        +0.0046555545f, +0.0056265487f,
+        +0.0067541562f, +0.0080522707f,
+        +0.0095370033f, +0.0112273888f,
+        +0.0131463382f, +0.0153219442f,
+        +0.0177892941f, +0.0205930381f,
+        +0.0237910974f, +0.0274601544f,
+        +0.0317040029f, +0.0366666667f,
+        +0.0425537942f, +0.0496691462f,
+        +0.0584802574f, +0.0697446887f,
+        +0.0847739823f, +0.1060495199f,
+        +0.1388940865f, +0.1971551103f,
+        +0.3316207267f, +0.9994281838f,
+    };
+    float famp;
+    int i;
+    int j;
+    int k;
+    complexf_t res;
+
+    s->history[s->ptr] = amp;
+    i = s->ptr - 63;
+    if (i < 0)
+        i += 128;
+    res.re = s->history[i];
+
+    famp = 0.0f;
+    j = s->ptr - 126;
+    if (j < 0)
+        j += 128;
+    for (i = 0, k = s->ptr;  i < 32;  i++)
+    {
+        famp += (s->history[k] - s->history[j])*hilbert_coeffs[i];
+        j += 2;
+        if (j >= 128)
+            j -= 128;
+        k -= 2;
+        if (k < 0)
+            k += 128;
+    }
+    res.im = famp/HILBERT_GAIN;
+
+    if (++s->ptr >= 128)
+        s->ptr = 0;
+    return res;
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ complex_t expj(double theta)
+{
+    return complex_set(cos(theta), sin(theta));
+}
+/*- End of function --------------------------------------------------------*/
+
+static void fftx(complex_t data[], complex_t temp[], int n)
+{
+    int i;
+    int h;
+    int p;
+    int t;
+    int i2;
+    complex_t wkt;
+
+    if (n > 1)
+    {
+        h = n/2;
+        for (i = 0;  i < h;  i++)
+        {
+            i2 = i*2;
+            temp[i] = data[i2];         /* Even */
+            temp[h + i] = data[i2 + 1]; /* Odd */
+        }
+        fftx(&temp[0], &data[0], h);
+        fftx(&temp[h], &data[h], h);
+        p = 0;
+        t = MAX_FFT_LEN/n;
+        for (i = 0;  i < h;  i++)
+        {
+            wkt = complex_mul(&circle[p], &temp[h + i]);
+            data[i] = complex_add(&temp[i], &wkt);
+            data[h + i] = complex_sub(&temp[i], &wkt);
+            p += t;
+        }
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static void ifftx(complex_t data[], complex_t temp[], int n)
+{
+    int i;
+    int h;
+    int p;
+    int t;
+    int i2;
+    complex_t wkt;
+
+    if (n > 1)
+    {
+        h = n/2;
+        for (i = 0;  i < h;  i++)
+        {
+            i2 = i*2;
+            temp[i] = data[i2];         /* Even */
+            temp[h + i] = data[i2 + 1]; /* Odd */
+        }
+        fftx(&temp[0], &data[0], h);
+        fftx(&temp[h], &data[h], h);
+        p = 0;
+        t = MAX_FFT_LEN/n;
+        for (i = 0;  i < h;  i++)
+        {
+            wkt = complex_mul(&icircle[p], &temp[h + i]);
+            data[i] = complex_add(&temp[i], &wkt);
+            data[h + i] = complex_sub(&temp[i], &wkt);
+            p += t;
+        }
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+void fft(complex_t data[], int len)
+{
+    int i;
+    double x;
+    complex_t temp[MAX_FFT_LEN];
+
+    /* A very slow and clunky FFT, that's just fine for tests. */
+    if (!circle_init)
+    {
+        for (i = 0;  i < MAX_FFT_LEN/2;  i++)
+        {
+            x = -(2.0*3.1415926535*i)/(double) MAX_FFT_LEN;
+            circle[i] = expj(x);
+        }
+        circle_init = TRUE;
+    }
+    fftx(data, temp, len);
+}
+/*- End of function --------------------------------------------------------*/
+
+void ifft(complex_t data[], int len)
+{
+    int i;
+    double x;
+    complex_t temp[MAX_FFT_LEN];
+
+    /* A very slow and clunky FFT, that's just fine for tests. */
+    if (!icircle_init)
+    {
+        for (i = 0;  i < MAX_FFT_LEN/2;  i++)
+        {
+            x = (2.0*3.1415926535*i)/(double) MAX_FFT_LEN;
+            icircle[i] = expj(x);
+        }
+        icircle_init = TRUE;
+    }
+    ifftx(data, temp, len);
+}
+/*- End of function --------------------------------------------------------*/
+
+codec_munge_state_t *codec_munge_init(int codec, int info)
+{
+    codec_munge_state_t *s;
+    
+    if ((s = (codec_munge_state_t *) malloc(sizeof(*s))))
+    {
+        switch (codec)
+        {
+        case MUNGE_CODEC_G726_40K:
+            g726_init(&s->g726_enc_state, 40000, G726_ENCODING_LINEAR, G726_PACKING_NONE);
+            g726_init(&s->g726_dec_state, 40000, G726_ENCODING_LINEAR, G726_PACKING_NONE);
+            s->munging_codec = MUNGE_CODEC_G726_32K;
+            break;
+        case MUNGE_CODEC_G726_32K:
+            g726_init(&s->g726_enc_state, 32000, G726_ENCODING_LINEAR, G726_PACKING_NONE);
+            g726_init(&s->g726_dec_state, 32000, G726_ENCODING_LINEAR, G726_PACKING_NONE);
+            s->munging_codec = MUNGE_CODEC_G726_32K;
+            break;
+        case MUNGE_CODEC_G726_24K:
+            g726_init(&s->g726_enc_state, 24000, G726_ENCODING_LINEAR, G726_PACKING_NONE);
+            g726_init(&s->g726_dec_state, 24000, G726_ENCODING_LINEAR, G726_PACKING_NONE);
+            s->munging_codec = MUNGE_CODEC_G726_32K;
+            break;
+        case MUNGE_CODEC_G726_16K:
+            g726_init(&s->g726_enc_state, 16000, G726_ENCODING_LINEAR, G726_PACKING_NONE);
+            g726_init(&s->g726_dec_state, 16000, G726_ENCODING_LINEAR, G726_PACKING_NONE);
+            s->munging_codec = MUNGE_CODEC_G726_32K;
+            break;
+        default:
+            s->munging_codec = codec;
+            break;
+        }
+        s->sequence = 0;
+        s->rbs_pattern = info;
+    }
+    return s;
+}
+/*- End of function --------------------------------------------------------*/
+
+void codec_munge_release(codec_munge_state_t *s)
+{
+    free(s);
+}
+/*- End of function --------------------------------------------------------*/
+
+void codec_munge(codec_munge_state_t *s, int16_t amp[], int len)
+{
+    uint8_t law;
+    uint8_t adpcmdata[160];
+    int i;
+    int adpcm;
+    int x;
+
+    switch (s->munging_codec)
+    {
+    case MUNGE_CODEC_NONE:
+        /* Do nothing */
+        break;
+    case MUNGE_CODEC_ALAW:
+        for (i = 0;  i < len;  i++)
+        {
+            law = linear_to_alaw(amp[i]);
+            amp[i] = alaw_to_linear(law);
+        }
+        break;
+    case MUNGE_CODEC_ULAW:
+        for (i = 0;  i < len;  i++)
+        {
+            law = linear_to_ulaw(amp[i]);
+            if (s->rbs_pattern & (1 << s->sequence))
+            {
+                /* Strip the bottom bit at the RBS rate */
+                law &= 0xFE;
+            }
+            amp[i] = ulaw_to_linear(law);
+        }
+        break;
+    case MUNGE_CODEC_G726_32K:
+        /* This could actually be any of the G.726 rates */
+        for (i = 0;  i < len;  i += x)
+        {
+            x = (len - i >= 160)  ?  160  :  (len - i);
+            adpcm = g726_encode(&s->g726_enc_state, adpcmdata, amp + i, x);
+            g726_decode(&s->g726_dec_state, amp + i, adpcmdata, adpcm);
+        }
+        break;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+AFfilehandle afOpenFile_telephony_read(const char *name, int channels)
+{
+    float x;
+    AFfilehandle handle;
+
+    if ((handle = afOpenFile(name, "r", 0)) == AF_NULL_FILEHANDLE)
+    {
+        fprintf(stderr, "    Cannot open wave file '%s'\n", name);
+        exit(2);
+    }
+    if ((x = afGetFrameSize(handle, AF_DEFAULT_TRACK, 1)) != 2.0)
+    {
+        fprintf(stderr, "    Unexpected frame size in wave file '%s'\n", name);
+        exit(2);
+    }
+    if ((x = afGetRate(handle, AF_DEFAULT_TRACK)) != (float) SAMPLE_RATE)
+    {
+        printf("    Unexpected sample rate in wave file '%s'\n", name);
+        exit(2);
+    }
+    if ((x = afGetChannels(handle, AF_DEFAULT_TRACK)) != (float) channels)
+    {
+        printf("    Unexpected number of channels in wave file '%s'\n", name);
+        exit(2);
+    }
+
+    return handle;
+}
+/*- End of function --------------------------------------------------------*/
+
+AFfilehandle afOpenFile_telephony_write(const char *name, int channels)
+{
+    AFfilesetup setup;
+    AFfilehandle handle;
+
+    if ((setup = afNewFileSetup()) == AF_NULL_FILESETUP)
+    {
+        fprintf(stderr, "    %s: Failed to create file setup\n", name);
+        exit(2);
+    }
+    afInitSampleFormat(setup, AF_DEFAULT_TRACK, AF_SAMPFMT_TWOSCOMP, 16);
+    afInitRate(setup, AF_DEFAULT_TRACK, (float) SAMPLE_RATE);
+    afInitFileFormat(setup, AF_FILE_WAVE);
+    afInitChannels(setup, AF_DEFAULT_TRACK, channels);
+
+    if ((handle = afOpenFile(name, "w", setup)) == AF_NULL_FILEHANDLE)
+    {
+        fprintf(stderr, "    Failed to open result file\n");
+        exit(2);
+    }
+    afFreeFileSetup(setup);
+
+    return handle;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/spandsp.spec
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/spandsp.spec	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,83 @@
+Summary:    A DSP library for telephony.
+Name:       spandsp
+Version:    0.0.5
+Release:    1
+License:    GPL
+Group:      System Environment/Libraries
+URL:        http://www.soft-switch.org/spandsp
+BuildRoot:  %{_tmppath}/%{name}-%{version}-root
+Source:     http://www.soft-switch.org/downloads/spandsp/spandsp-0.0.5.tar.gz
+BuildRoot:  %{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n)
+
+Docdir:     %{_prefix}/doc
+
+BuildRequires: libtiff-devel
+BuildRequires: audiofile-devel
+BuildRequires: doxygen
+
+%description
+SpanDSP is a library of DSP functions for telephony, in the 8000
+sample per second world of E1s, T1s, and higher order PCM channels. It
+contains low level functions, such as basic filters. It also contains
+higher level functions, such as cadenced supervisory tone detection,
+and a complete software FAX machine. The software has been designed to
+avoid intellectual property issues, using mature techniques where all
+relevant patents have expired. See the file DueDiligence for important
+information about these intellectual property issues.
+
+%package devel
+Summary:    SpanDSP development files
+Group:      Development/Libraries
+Requires:   spandsp = %{version}
+Requires:   libtiff-devel
+PreReq:     /sbin/install-info
+
+%description devel
+SpanDSP development files.
+
+%prep
+%setup -q
+
+%build
+%configure --enable-doc --disable-static --disable-rpath
+make
+
+%install
+rm -rf %{buildroot}
+make install DESTDIR=%{buildroot}
+rm %{buildroot}%{_libdir}/libspandsp.la
+
+%clean
+rm -rf %{buildroot}
+
+%files
+%defattr(-,root,root,-)
+%doc DueDiligence ChangeLog AUTHORS COPYING NEWS README 
+
+%{_libdir}/libspandsp.so.*
+
+%{_datadir}/spandsp
+
+%files devel
+%defattr(-,root,root,-)
+%doc doc/api
+%{_includedir}/spandsp.h
+%{_includedir}/spandsp
+%{_libdir}/libspandsp.so
+
+%post -p /sbin/ldconfig
+
+%postun -p /sbin/ldconfig
+
+%changelog
+* Mon 23 Jun 2008 Steve Underwood <steveu at coppice.org> 0.0.5-1
+- Cleared out the dependency on libxml2
+
+* Sun Dec 31 2006 Steve Underwood <steveu at coppice.org> 0.0.3-1
+- Preparing for 0.0.3 release
+
+* Sat Oct 16 2004 Steve Underwood <steveu at coppice.org> 0.0.2-1
+- Preparing for 0.0.2 release
+
+* Thu Apr 15 2004 Steve Underwood <steveu at coppice.org> 0.0.1-1
+- Initial version

Added: freeswitch/trunk/libs/spandsp/spandsp.spec.in
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/spandsp.spec.in	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,83 @@
+Summary:    A DSP library for telephony.
+Name:       @PACKAGE@
+Version:    @VERSION@
+Release:    1
+License:    GPL
+Group:      System Environment/Libraries
+URL:        http://www.soft-switch.org/spandsp
+BuildRoot:  %{_tmppath}/%{name}-%{version}-root
+Source:     http://www.soft-switch.org/downloads/spandsp/@PACKAGE@-@VERSION@.tar.gz
+BuildRoot:  %{_tmppath}/%{name}-%{version}-%{release}-root-%(%{__id_u} -n)
+
+Docdir:     %{_prefix}/doc
+
+BuildRequires: libtiff-devel
+BuildRequires: audiofile-devel
+BuildRequires: doxygen
+
+%description
+SpanDSP is a library of DSP functions for telephony, in the 8000
+sample per second world of E1s, T1s, and higher order PCM channels. It
+contains low level functions, such as basic filters. It also contains
+higher level functions, such as cadenced supervisory tone detection,
+and a complete software FAX machine. The software has been designed to
+avoid intellectual property issues, using mature techniques where all
+relevant patents have expired. See the file DueDiligence for important
+information about these intellectual property issues.
+
+%package devel
+Summary:    SpanDSP development files
+Group:      Development/Libraries
+Requires:   spandsp = %{version}
+Requires:   libtiff-devel
+PreReq:     /sbin/install-info
+
+%description devel
+SpanDSP development files.
+
+%prep
+%setup -q
+
+%build
+%configure --enable-doc --disable-static --disable-rpath
+make
+
+%install
+rm -rf %{buildroot}
+make install DESTDIR=%{buildroot}
+rm %{buildroot}%{_libdir}/libspandsp.la
+
+%clean
+rm -rf %{buildroot}
+
+%files
+%defattr(-,root,root,-)
+%doc DueDiligence ChangeLog AUTHORS COPYING NEWS README 
+
+%{_libdir}/libspandsp.so.*
+
+%{_datadir}/spandsp
+
+%files devel
+%defattr(-,root,root,-)
+%doc doc/api
+%{_includedir}/spandsp.h
+%{_includedir}/spandsp
+%{_libdir}/libspandsp.so
+
+%post -p /sbin/ldconfig
+
+%postun -p /sbin/ldconfig
+
+%changelog
+* Mon 23 Jun 2008 Steve Underwood <steveu at coppice.org> 0.0.5-1
+- Cleared out the dependency on libxml2
+
+* Sun Dec 31 2006 Steve Underwood <steveu at coppice.org> 0.0.3-1
+- Preparing for 0.0.3 release
+
+* Sat Oct 16 2004 Steve Underwood <steveu at coppice.org> 0.0.2-1
+- Preparing for 0.0.2 release
+
+* Thu Apr 15 2004 Steve Underwood <steveu at coppice.org> 0.0.1-1
+- Initial version

Added: freeswitch/trunk/libs/spandsp/spandsp/global-tones.xml
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/spandsp/global-tones.xml	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,7334 @@
+<?xml version="1.0"?>
+<!DOCTYPE global-tones SYSTEM "../tones.dtd">
+<global-tones>
+  <tone-set country="Albania" uncode="al">
+    <dial-tone>
+      <step freq="425"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="1.0"/>
+        <step length="4.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.3"/>
+        <step length="0.3"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.15"/>
+        <step length="0.15"/>
+      </step>
+    </congestion-tone>
+  </tone-set>
+  <tone-set country="Algeria" uncode="dz">
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="25 || 50" level="-12" length="1.5"/>
+        <step length="3.5"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="450" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+  </tone-set>
+  <tone-set country="Angola" uncode="ao">
+    <dial-tone>
+      <step freq="400" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="25" level="-12" length="1.0"/>
+        <step length="5.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </congestion-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="0.2"/>
+        <step length="0.2"/>
+      </step>
+    </number-unobtainable-tone>
+    <waiting-tone>
+      <step freq="400" level="-12" length="1.0"/>
+      <step length="5.0"/>
+    </waiting-tone>
+  </tone-set>
+  <tone-set country="Anguilla" uncode="ai">
+    <dial-tone>
+      <step freq="350+440" level="-12"/>
+    </dial-tone>
+    <dial-tone type="recall">
+      <step cycles="3">
+        <step freq="350+440" level="-12" length="0.1"/>
+        <step length="0.1"/>
+      </step>
+      <step freq="350+440" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="440+480" level="-12" length="2.0"/>
+        <step length="4.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="480+620" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="480+620" level="-12" length="0.25"/>
+        <step length="0.25"/>
+      </step>
+    </congestion-tone>
+    <special-information-tone>
+      <step freq="950" level="-12" length="0.333"/>
+      <step freq="1400" level="-12" length="0.333"/>
+      <step freq="1800" level="-12" length="0.333"/>
+      <step length="1.0"/>
+    </special-information-tone>
+    <warning-tone type="operator-intervening">
+      <step freq="450" level="-12" length="0.2"/>
+      <step length="0.2"/>
+      <step length="0.65"/>
+      <step length="0.2"/>
+    </warning-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step freq="480+620" level="-12" length="0.25"/>
+        <step length="0.25"/>
+      </step>
+    </number-unobtainable-tone>
+    <waiting-tone>
+      <step cycles="1">
+        <step freq="440" level="-12" length="0.5"/>
+        <step length="10.0"/>
+      </step>
+      <step length="0.5"/>
+    </waiting-tone>
+    <payphone-recognition-tone>
+      <step freq="1600" level="-12" length="0.2"/>
+      <step length="0.2"/>
+      <step freq="1143" level="-12" length="0.2"/>
+      <step length="2.0"/>
+    </payphone-recognition-tone>
+  </tone-set>
+  <tone-set country="Antigua and Barbuda" uncode="ag">
+    <dial-tone>
+      <step freq="350+440" level="-12"/>
+    </dial-tone>
+    <dial-tone type="second">
+      <step freq="350+440" level="-12"/>
+    </dial-tone>
+    <dial-tone type="recall">
+      <step freq="350+440" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="440+480" level="-12" length="2.0"/>
+        <step length="4.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="480+620" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="480+620" level="-12" length="0.25"/>
+        <step length="0.25"/>
+      </step>
+    </congestion-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step recorded-announcement="true"/>
+      </step>
+    </number-unobtainable-tone>
+    <waiting-tone>
+      <step cycles="1">
+        <step freq="480" level="-12" length="0.5"/>
+        <step length="10.0"/>
+        <step freq="480" level="-12" length="0.5"/>
+      </step>
+    </waiting-tone>
+    <test-number-tone>
+      <step freq="1004" level="-12"/>
+    </test-number-tone>
+  </tone-set>
+  <tone-set country="Argentina" uncode="ar">
+    <dial-tone>
+      <step freq="425" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="1.0"/>
+        <step length="4.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.3"/>
+        <step length="0.2"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.3"/>
+        <step length="0.4"/>
+      </step>
+    </congestion-tone>
+    <warning-tone type="operator-intervening">
+      <step cycles="5">
+        <step freq="1400" level="-12" length="0.1"/>
+        <step length="1.9"/>
+      </step>
+    </warning-tone>
+    <waiting-tone>
+      <step freq="425" level="-12" length="0.4"/>
+      <step length="0.2"/>
+      <step length="0.4"/>
+      <step length="4.0"/>
+    </waiting-tone>
+    <call-waiting-tone>
+      <step freq="425" level="-12" length="0.3"/>
+      <step length="10.0"/>
+    </call-waiting-tone>
+    <end-of-three-party-service-tone>
+      <step cycles="1">
+        <step freq="950" level="-12" length="0.333"/>
+        <step freq="1400" level="-12" length="0.333"/>
+        <step freq="1800" level="-12" length="0.333"/>
+      </step>
+    </end-of-three-party-service-tone>
+    <service-activated-tone>
+      <step freq="425" level="-12" length="1.0"/>
+      <step length="0.25"/>
+    </service-activated-tone>
+  </tone-set>
+  <tone-set country="Aruba" uncode="aw">
+    <dial-tone>
+      <step freq="425" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="1.0"/>
+        <step length="4.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.25"/>
+        <step length="0.25"/>
+      </step>
+    </congestion-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step freq="950" level="-12" length="0.333"/>
+        <step freq="1400" level="-12" length="0.333"/>
+        <step freq="1800" level="-12" length="0.333"/>
+        <step length="1.0"/>
+      </step>
+    </number-unobtainable-tone>
+    <special-information-tone>
+      <step freq="950" level="-12" length="0.333"/>
+      <step freq="1400" level="-12" length="0.333"/>
+      <step freq="1800" level="-12" length="0.333"/>
+      <step length="1.0"/>
+    </special-information-tone>
+    <waiting-tone>
+      <step freq="425" level="-12" length="0.2"/>
+      <step length="0.2"/>
+      <step freq="425" level="-12" length="0.2"/>
+      <step length="4.4"/>
+    </waiting-tone>
+  </tone-set>
+  <tone-set country="Ascension Islands" uncode="??">
+    <dial-tone>
+      <step freq="350+440" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="440+480" level="-12" length="1.0"/>
+        <step length="3.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="480+620" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <warning-tone type="operator-intervening">
+      <step freq="440" level="-12" length="0.8"/>
+      <step length="6.0"/>
+      <step freq="440" level="-12" length="0.2"/>
+      <step length="6.0"/>
+      <step freq="440" level="-12" length="0.2"/>
+    </warning-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step freq="480+620" level="-12" length="0.25"/>
+        <step length="0.25"/>
+      </step>
+    </number-unobtainable-tone>
+  </tone-set>
+  <tone-set country="Australia" uncode="au">
+    <dial-tone>
+      <step freq="400+450" level="-12"/>
+    </dial-tone>
+    <dial-tone domain="PABX">
+      <step freq="154" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="383+417" level="-12" length="0.4"/>
+        <step length="0.2"/>
+        <step freq="383+417" level="-12" length="0.4"/>
+        <step length="2.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="0.375"/>
+        <step length="0.375"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="0.375"/>
+        <step length="0.375"/>
+        <step freq="400" level="-22" length="0.375"/>
+        <step length="0.375"/>
+      </step>
+    </congestion-tone>
+    <warning-tone type="operator-intervening">
+      <step freq="425" level="-12" length="1.0"/>
+      <step length="15.0"/>
+      <step freq="525" level="-12" length="0.36"/>
+      <step length="15.0"/>
+    </warning-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="2.5"/>
+        <step length="0.5"/>
+      </step>
+    </number-unobtainable-tone>
+    <waiting-tone>
+      <step freq="425" level="-12" length="0.1"/>
+      <step length="0.1"/>
+      <step freq="525" level="-12" length="0.1"/>
+      <step length="4.7"/>
+    </waiting-tone>
+    <record-tone>
+      <step cycles="endless">
+        <step freq="1400" level="-12" length="0.425"/>
+        <step length="14.525"/>
+      </step>
+    </record-tone>
+    <payphone-recognition-tone>
+      <step freq="1100+1750" level="-12" length="0.075"/>
+      <step length="0.15"/>
+      <step freq="750+1450" level="-12" length="0.075"/>
+      <step length="2.7"/>
+    </payphone-recognition-tone>
+    <facilities-tone>
+      <step freq="425" level="-12"/>
+    </facilities-tone>
+    <offering-tone>
+      <step freq="525" level="-12" length="0.1"/>
+      <step length="0.1"/>
+    </offering-tone>
+  </tone-set>
+  <tone-set country="Austria" uncode="at">
+    <dial-tone>
+      <step freq="420 || 450" level="-12"/>
+    </dial-tone>
+    <dial-tone type="special">
+      <step freq="380+420" level="-12"/>
+    </dial-tone>
+    <dial-tone type="recall">
+      <step freq="420" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="420 || 450" level="-12" length="1.0"/>
+        <step length="5.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="420" level="-12" length="0.4"/>
+        <step length="0.4"/>
+      </step>
+    </busy-tone>
+    <busy-tone domain="analogue">
+      <step cycles="endless">
+        <step freq="450" level="-12" length="0.3"/>
+        <step length="0.3"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="420" level="-12" length="0.2"/>
+        <step length="0.2"/>
+      </step>
+    </congestion-tone>
+    <congestion-tone domain="analogue">
+      <step cycles="endless">
+        <step freq="450" level="-12" length="0.3"/>
+        <step length="0.3"/>
+      </step>
+    </congestion-tone>
+    <special-information-tone>
+      <step freq="950" level="-12" length="0.333"/>
+      <step freq="1400" level="-12" length="0.333"/>
+      <step freq="1800" level="-12" length="0.333"/>
+      <step length="1.0"/>
+    </special-information-tone>
+    <warning-tone type="operator-intervening">
+      <step freq="420 || 450" level="-12" length="0.15"/>
+      <step length="0.15"/>
+      <step freq="420 || 450" level="-12" length="0.15"/>
+      <step length="1.95"/>
+    </warning-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step freq="950" level="-12" length="0.333"/>
+        <step freq="1400" level="-12" length="0.333"/>
+        <step freq="1800" level="-12" length="0.333"/>
+        <step length="1.0"/>
+      </step>
+    </number-unobtainable-tone>
+    <waiting-tone>
+      <step freq="420" level="-12" length="0.04"/>
+      <step length="1.95"/>
+    </waiting-tone>
+    <payphone-recognition-tone>
+      <step freq="1336" level="-12" length="0.2"/>
+      <step length="0.2"/>
+      <step freq="1633" level="-12" length="0.2"/>
+      <step length="2.0"/>
+    </payphone-recognition-tone>
+    <positive-indication-tone>
+      <step freq="380+420" level="-12" length="1.0"/>
+      <step length="5.0"/>
+    </positive-indication-tone>
+    <negative-indication-tone>
+      <step freq="380+420" level="-12" length="0.4"/>
+      <step length="0.4"/>
+    </negative-indication-tone>
+  </tone-set>
+  <tone-set country="Bahamas" uncode="bs">
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="440+480 || 400+440 || 400" level="-12" length="2.0"/>
+        <step length="4.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="480 || 400" level="-12" length="0.5"/>
+        <step length="0.5"/>
+        <step freq="620 || 400" level="-12" length="0.5"/>
+        <step length="0.5"/>
+        <step freq="420 || 400" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+  </tone-set>
+  <tone-set country="Bahrain" uncode="bh">
+    <dial-tone>
+      <step freq="330+440" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="440 || 450" level="-12" length="0.4"/>
+        <step length="0.2"/>
+        <step freq="440 || 450" level="-12" length="0.4"/>
+        <step length="2.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="0.375"/>
+        <step length="0.375"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="0.4"/>
+        <step length="0.35"/>
+        <step freq="400" level="-12" length="0.225"/>
+        <step length="0.525"/>
+      </step>
+    </congestion-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="0.375"/>
+        <step length="0.375"/>
+      </step>
+    </congestion-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12"/>
+      </step>
+    </number-unobtainable-tone>
+    <intercept-tone>
+      <step freq="450 || 425+475" level="-12" length="0.05"/>
+      <step length="0.05"/>
+    </intercept-tone>
+    <route-tone>
+      <step freq="425" level="-12" length="0.06"/>
+      <step length="0.06"/>
+    </route-tone>
+    <payphone-recognition-tone>
+      <step freq="816+1209" level="-12" length="0.125"/>
+      <step length="1.5"/>
+    </payphone-recognition-tone>
+  </tone-set>
+  <tone-set country="Barbados" uncode="bb">
+    <dial-tone>
+      <step freq="350+440" level="-12"/>
+    </dial-tone>
+    <dial-tone type="second">
+      <step cycles="2">
+        <step freq="350+440" level="-12" length="1.0"/>
+        <step length="1.0"/>
+      </step>
+      <step freq="350+440" level="-12"/>
+    </dial-tone>
+    <dial-tone type="recall">
+      <step cycles="3">
+        <step freq="350+440" level="-12" length="0.1"/>
+        <step length="0.1"/>
+      </step>
+      <step freq="350+440" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="440+480" level="-12" length="2.0"/>
+        <step length="4.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="480+620" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="480+620" level="-12" length="0.25"/>
+        <step length="0.25"/>
+      </step>
+    </congestion-tone>
+  </tone-set>
+  <tone-set country="Belarus" uncode="by">
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.8"/>
+        <step length="3.2"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.4"/>
+        <step length="0.4"/>
+      </step>
+    </busy-tone>
+  </tone-set>
+  <tone-set country="Belgium" uncode="be">
+    <dial-tone>
+      <step freq="425 || 450" level="-12"/>
+    </dial-tone>
+    <dial-tone type="second" domain="analogue">
+      <step freq="900" level="-12" length="0.333"/>
+      <step freq="1020" level="-12" length="0.333"/>
+      <step freq="1140" level="-12" length="0.333"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425 || 450" level="-12" length="1.0"/>
+        <step length="3.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425 || 450" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="425 || 450" level="-12" length="0.167"/>
+        <step length="0.167"/>
+      </step>
+    </congestion-tone>
+    <special-information-tone>
+      <step freq="900" level="-12" length="0.333"/>
+      <step freq="1380" level="-12" length="0.333"/>
+      <step freq="1860" level="-12" length="0.333"/>
+    </special-information-tone>
+  </tone-set>
+  <tone-set country="Benin" uncode="bj">
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="440" level="-12" length="1.5"/>
+        <step length="3.5"/>
+      </step>
+    </ringback-tone>
+    <payphone-recognition-tone>
+      <step freq="1100+1750" level="-12" length="0.2"/>
+      <step length="0.2"/>
+      <step freq="750+1450" level="-12" length="0.2"/>
+      <step length="2.0"/>
+    </payphone-recognition-tone>
+  </tone-set>
+  <tone-set country="Bermuda" uncode="bm">
+    <dial-tone>
+      <step freq="350+440" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="440+480" level="-12" length="2.0"/>
+        <step length="4.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="480+620" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="480+620" level="-12" length="0.25"/>
+        <step length="0.25"/>
+      </step>
+    </congestion-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step cycles="6">
+          <step freq="200 || 400" level="-12" length="0.5"/>
+          <step length="0.5"/>
+        </step>
+      </step>
+    </number-unobtainable-tone>
+    <waiting-tone>
+      <step cycles="2">
+        <step freq="440" level="-12" length="1.0"/>
+        <step length="10.0"/>
+      </step>
+    </waiting-tone>
+  </tone-set>
+  <tone-set country="Bhutan" uncode="bt">
+    <dial-tone>
+      <step freq="400+25" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="400+25" level="-12" length="0.4"/>
+        <step length="0.2"/>
+        <step freq="400+25" level="-12" length="0.4"/>
+        <step length="2.6"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="0.6"/>
+        <step length="0.6"/>
+      </step>
+    </busy-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12"/>
+      </step>
+    </number-unobtainable-tone>
+    <waiting-tone>
+      <step freq="400" level="-12" length="0.5"/>
+      <step length="0.25"/>
+    </waiting-tone>
+    <offering-tone>
+      <step freq="450" level="-12" length="0.2"/>
+      <step length="0.2"/>
+      <step freq="450" level="-12" length="0.2"/>
+      <step length="0.6"/>
+    </offering-tone>
+  </tone-set>
+  <tone-set country="Botswana" uncode="bw">
+    <dial-tone>
+      <step freq="350+450" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="400 || 450" level="-12" length="0.4"/>
+        <step length="0.2"/>
+        <step freq="400 || 450" level="-12" length="0.4"/>
+        <step length="2.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="0.375"/>
+        <step length="0.375"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.25"/>
+        <step length="0.25"/>
+      </step>
+    </congestion-tone>
+    <warning-tone type="operator-intervening">
+      <step freq="425" level="-12" length="0.16"/>
+      <step length="2.0"/>
+    </warning-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12"/>
+      </step>
+    </number-unobtainable-tone>
+    <waiting-tone>
+      <step freq="425" level="-12" length="0.2"/>
+      <step length="1.0"/>
+    </waiting-tone>
+    <payphone-recognition-tone>
+      <step freq="1600" level="-12" length="0.07"/>
+      <step length="2.0"/>
+    </payphone-recognition-tone>
+  </tone-set>
+  <tone-set country="Brazil" uncode="br">
+    <dial-tone>
+      <step freq="425" level="-12"/>
+    </dial-tone>
+    <dial-tone domain="PABX">
+      <step freq="425" level="-12" length="0.975"/>
+      <step length="0.05"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="1.0"/>
+        <step length="4.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.25"/>
+        <step length="0.25"/>
+      </step>
+    </busy-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.75"/>
+        <step length="0.25"/>
+        <step freq="425" level="-12" length="0.25"/>
+        <step length="0.25"/>
+      </step>
+    </number-unobtainable-tone>
+    <waiting-tone>
+      <step freq="425" level="-12" length="0.05"/>
+      <step length="1.0"/>
+    </waiting-tone>
+    <executive-override-tone>
+      <step freq="750" level="-12" length="0.02"/>
+      <step length="1.0"/>
+    </executive-override-tone>
+    <function-acknowledge-tone>
+      <step freq="425" level="-12" length="0.1"/>
+      <step length="0.1"/>
+      <step length="0.1"/>
+      <step length="2.0"/>
+    </function-acknowledge-tone>
+    <pay-tone>
+      <step freq="300" level="-12" length="0.75"/>
+    </pay-tone>
+  </tone-set>
+  <tone-set country="British Virgin Islands" uncode="vg">
+    <dial-tone>
+      <step freq="350+440" level="-12"/>
+    </dial-tone>
+    <dial-tone type="recall">
+      <step cycles="3">
+        <step freq="350+440" level="-12" length="0.1"/>
+        <step length="0.1"/>
+      </step>
+      <step freq="350+440" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="440+480" level="-12" length="2.0"/>
+        <step length="4.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="480+620" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="480+620" level="-12" length="0.25"/>
+        <step length="0.25"/>
+      </step>
+    </congestion-tone>
+    <special-information-tone>
+      <step freq="950" level="-12" length="0.333"/>
+      <step freq="1400" level="-12" length="0.333"/>
+      <step freq="1800" level="-12" length="0.333"/>
+      <step length="1.0"/>
+    </special-information-tone>
+    <warning-tone type="operator-intervening">
+      <step freq="450" level="-12" length="0.2"/>
+      <step length="0.2"/>
+      <step length="0.65"/>
+      <step length="0.2"/>
+    </warning-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step freq="480+620" level="-12" length="0.25"/>
+        <step length="0.25"/>
+      </step>
+    </number-unobtainable-tone>
+    <waiting-tone>
+      <step cycles="1">
+        <step freq="440" level="-12" length="0.5"/>
+        <step length="10.0"/>
+        <step length="0.5"/>
+      </step>
+    </waiting-tone>
+    <payphone-recognition-tone>
+      <step freq="1600" level="-12" length="0.2"/>
+      <step length="0.2"/>
+      <step freq="1143" level="-12" length="0.2"/>
+      <step length="2.0"/>
+    </payphone-recognition-tone>
+  </tone-set>
+  <tone-set country="Brunei Darussalam" uncode="bn">
+    <dial-tone>
+      <step freq="350+450" level="-12"/>
+    </dial-tone>
+    <dial-tone type="transmission">
+      <step freq="1000" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="400+450" level="-12" length="0.4"/>
+        <step length="0.3"/>
+        <step freq="400+450" level="-12" length="0.4"/>
+        <step length="2.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="0.375"/>
+        <step length="0.375"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="0.375"/>
+        <step length="0.375"/>
+      </step>
+    </congestion-tone>
+    <warning-tone type="operator-intervening">
+      <step freq="400" level="-12" length="0.25"/>
+      <step length="2.0"/>
+    </warning-tone>
+    <warning-tone type="end-of-period">
+      <step freq="400" level="-12" length="0.624"/>
+      <step length="4.376"/>
+    </warning-tone>
+    <warning-tone type="pip-tone">
+      <step freq="900" level="-12" length="0.25"/>
+      <step length="0.25"/>
+    </warning-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12"/>
+      </step>
+    </number-unobtainable-tone>
+    <waiting-tone>
+      <step freq="376+424" level="-12" length="0.5"/>
+    </waiting-tone>
+    <confirmation-tone>
+      <step freq="376+424" level="-12" length="0.1"/>
+      <step length="0.1"/>
+      <step freq="376+424" level="-12" length="0.3"/>
+      <step length="0.3"/>
+    </confirmation-tone>
+    <holding-tone>
+      <step freq="376+424" level="-12" length="0.5"/>
+      <step length="2.5"/>
+    </holding-tone>
+  </tone-set>
+  <tone-set country="Bulgaria" uncode="bg">
+    <dial-tone>
+      <step freq="425" level="-12" length="0.25"/>
+      <step length="0.3"/>
+      <step freq="425" level="-12" length="0.7"/>
+      <step length="0.8"/>
+    </dial-tone>
+    <dial-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.25"/>
+        <step length="0.75"/>
+        <step freq="425" level="-12" length="0.75"/>
+        <step length="1.0"/>
+      </step>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="1.0"/>
+        <step length="9.0"/>
+      </step>
+    </ringback-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="1.0"/>
+        <step length="4.0"/>
+      </step>
+    </ringback-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="1.83"/>
+        <step length="3.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.25"/>
+        <step length="0.25"/>
+      </step>
+    </busy-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.2"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.25"/>
+        <step length="0.25"/>
+      </step>
+    </congestion-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </congestion-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.2"/>
+        <step length="0.5"/>
+      </step>
+    </congestion-tone>
+    <special-information-tone>
+      <step freq="950" level="-12" length="0.333"/>
+      <step freq="1400" level="-12" length="0.333"/>
+      <step freq="1800" level="-12" length="0.333"/>
+      <step length="1.0"/>
+    </special-information-tone>
+    <warning-tone type="operator-intervening">
+      <step freq="425" level="-12" length="0.15"/>
+      <step length="0.25"/>
+      <step length="0.15"/>
+      <step length="1.15"/>
+    </warning-tone>
+    <warning-tone type="operator-intervening">
+      <step freq="425" level="-12" length="0.2"/>
+      <step length="0.3"/>
+      <step length="0.2"/>
+      <step length="1.5"/>
+    </warning-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step freq="1800" level="-12" length="0.333"/>
+        <step freq="1400" level="-12" length="0.333"/>
+        <step freq="1800" level="-12" length="0.333"/>
+        <step length="1.0"/>
+      </step>
+    </number-unobtainable-tone>
+  </tone-set>
+  <tone-set country="Burkina Faso" uncode="bf">
+    <dial-tone>
+      <step freq="425" level="-12"/>
+    </dial-tone>
+    <dial-tone domain="analogue">
+      <step freq="350+440" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="1.0"/>
+        <step length="4.0"/>
+      </step>
+    </ringback-tone>
+    <ringback-tone domain="analogue">
+      <step cycles="endless">
+        <step freq="440+480" level="-12" length="0.2"/>
+        <step length="0.2"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <busy-tone domain="analogue">
+      <step cycles="endless">
+        <step freq="480+620" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <congestion-tone domain="analogue">
+      <step cycles="endless">
+        <step freq="480+620" level="-12" length="2.4"/>
+        <step length="2.4"/>
+      </step>
+    </congestion-tone>
+    <route-tone>
+      <step freq="425" level="-12" length="0.05"/>
+      <step length="0.05"/>
+    </route-tone>
+    <route-tone domain="analogue">
+      <step freq="425" level="-12" length="0.04"/>
+      <step length="0.04"/>
+    </route-tone>
+  </tone-set>
+  <tone-set country="Burundi" uncode="bi">
+    <dial-tone>
+      <step freq="425" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="1.7"/>
+        <step length="3.3"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="450" level="-12" length="0.25"/>
+        <step length="0.25"/>
+      </step>
+    </busy-tone>
+    <special-information-tone>
+      <step freq="150" level="-12" length="0.5"/>
+      <step length="0.5"/>
+      <step freq="450" level="-12" length="0.5"/>
+    </special-information-tone>
+    <route-tone>
+      <step freq="425" level="-12" length="0.5"/>
+      <step length="0.5"/>
+    </route-tone>
+  </tone-set>
+  <tone-set country="Cameroon" uncode="cm">
+    <dial-tone>
+      <step freq="440" level="-12"/>
+    </dial-tone>
+    <dial-tone type="second">
+      <step freq="330 || 440" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="440" level="-12" length="1.7"/>
+        <step length="3.3"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="440" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <route-tone>
+      <step freq="440" level="-12" length="0.05"/>
+      <step length="0.05"/>
+    </route-tone>
+  </tone-set>
+  <tone-set country="Canada" uncode="ca">
+    <dial-tone>
+      <step freq="480+720 || 350+440" level="-12"/>
+    </dial-tone>
+    <dial-tone domain="PABX">
+      <step freq="350+440" level="-12"/>
+    </dial-tone>
+    <dial-tone type="recall" domain="PABX">
+      <step freq="350+440" level="-12" length="0.1"/>
+      <step length="0.1"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="440+480" level="-12" length="2.0"/>
+        <step length="4.0"/>
+      </step>
+    </ringback-tone>
+    <ringback-tone domain="PABX">
+      <step cycles="endless">
+        <step freq="440+480" level="-12" length="1.0"/>
+        <step length="3.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="480+620" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="480+620" level="-12" length="0.25"/>
+        <step length="0.25"/>
+      </step>
+    </congestion-tone>
+    <executive-override-tone domain="PABX">
+      <step freq="440" level="-12" length="3.0"/>
+    </executive-override-tone>
+    <intercept-tone domain="PABX">
+      <step freq="440" level="-12" length="0.25"/>
+      <step length="0.25"/>
+      <step freq="620" level="-12" length="0.25"/>
+      <step length="0.25"/>
+    </intercept-tone>
+    <confirmation-tone domain="PABX">
+      <step cycles="2">
+        <step freq="350+440" level="-12" length="0.1"/>
+        <step length="0.1"/>
+      </step>
+      <step freq="350+440" level="-12" length="0.1"/>
+    </confirmation-tone>
+  </tone-set>
+  <tone-set country="Cayman Islands" uncode="ky">
+    <dial-tone>
+      <step freq="425" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="1.0"/>
+        <step length="4.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.25"/>
+        <step length="0.25"/>
+      </step>
+    </congestion-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.25"/>
+        <step length="0.25"/>
+      </step>
+    </number-unobtainable-tone>
+  </tone-set>
+  <tone-set country="Central African Republic" uncode="cf">
+    <dial-tone>
+      <step freq="425" level="-12"/>
+    </dial-tone>
+    <dial-tone type="second">
+      <step freq="330+425" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="1.7"/>
+        <step length="3.3"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </congestion-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </number-unobtainable-tone>
+    <route-tone>
+      <step freq="425" level="-12" length="0.05"/>
+      <step length="0.05"/>
+    </route-tone>
+    <pay-tone>
+      <step freq="12000" level="-12" length="0.15"/>
+      <step length="0.15"/>
+    </pay-tone>
+  </tone-set>
+  <tone-set country="Channel Islands: Jersey" uncode="??">
+    <dial-tone>
+      <step freq="350+450" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="400+450" level="-12" length="0.4"/>
+        <step length="0.2"/>
+        <step freq="400+450" level="-12" length="0.4"/>
+        <step length="2.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="0.375"/>
+        <step length="0.375"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="0.4"/>
+        <step length="0.35"/>
+        <step freq="400" level="-12" length="0.225"/>
+        <step length="0.525"/>
+      </step>
+    </congestion-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12"/>
+      </step>
+    </number-unobtainable-tone>
+    <waiting-tone>
+      <step freq="400" level="-12" length="0.1"/>
+      <step length="2.5"/>
+      <step freq="400" level="-12" length="0.1"/>
+    </waiting-tone>
+    <pay-tone>
+      <step freq="400" level="-12" length="0.125"/>
+      <step length="0.125"/>
+    </pay-tone>
+  </tone-set>
+  <tone-set country="Chile" uncode="cl">
+    <dial-tone>
+      <step freq="400" level="-12"/>
+    </dial-tone>
+    <dial-tone type="second">
+      <step freq="330+440" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="1.0"/>
+        <step length="3.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="0.2"/>
+        <step length="0.2"/>
+      </step>
+    </congestion-tone>
+    <special-information-tone>
+      <step freq="950" level="-12" length="0.333"/>
+      <step freq="1400" level="-12" length="0.333"/>
+      <step freq="1800" level="-12" length="0.333"/>
+      <step length="1.0"/>
+    </special-information-tone>
+    <warning-tone type="operator-intervening">
+      <step freq="1400" level="-12" length="0.5"/>
+      <step length="15.0"/>
+    </warning-tone>
+    <waiting-tone>
+      <step freq="900+1300" level="-12" length="0.5"/>
+      <step length="0.5"/>
+    </waiting-tone>
+    <payphone-recognition-tone>
+      <step freq="1100+1750" level="-12" length="0.2"/>
+      <step length="0.2"/>
+      <step freq="750+1450" level="-12" length="0.2"/>
+      <step length="2.0"/>
+    </payphone-recognition-tone>
+    <identification-tone>
+      <step freq="800" level="-12" length="0.6"/>
+      <step length="2.0"/>
+      <step freq="1200" level="-12" length="0.6"/>
+      <step length="2.0"/>
+    </identification-tone>
+    <warning-tone type="end-of-period">
+      <step freq="800" level="-12" length="2.0"/>
+    </warning-tone>
+  </tone-set>
+  <tone-set country="China" uncode="cn">
+    <dial-tone>
+      <step freq="450" level="-12"/>
+    </dial-tone>
+    <dial-tone type="second">
+      <step freq="450" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="450" level="-12" length="1.0"/>
+        <step length="4.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="450" level="-12" length="0.35"/>
+        <step length="0.35"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="450" level="-12" length="0.7"/>
+        <step length="0.7"/>
+      </step>
+    </congestion-tone>
+    <intrusion-tone>
+      <step freq="450" level="-12" length="0.2"/>
+      <step length="0.2"/>
+      <step freq="450" level="-12" length="0.2"/>
+      <step length="0.6"/>
+    </intrusion-tone>
+    <special-information-tone>
+      <step freq="450" level="-12" length="0.4"/>
+      <step length="0.04"/>
+    </special-information-tone>
+    <special-information-tone>
+      <step freq="950" level="-12" length="0.4"/>
+      <step length="10.0"/>
+    </special-information-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step cycles="3">
+          <step freq="450" level="-12" length="0.1"/>
+          <step length="0.1"/>
+        </step>
+        <step freq="450" level="-12" length="0.4"/>
+        <step length="0.4"/>
+      </step>
+    </number-unobtainable-tone>
+    <waiting-tone>
+      <step freq="450" level="-12" length="0.4"/>
+      <step length="4.0"/>
+    </waiting-tone>
+  </tone-set>
+  <tone-set country="Comoros" uncode="km">
+    <dial-tone>
+      <step freq="425" level="-12"/>
+    </dial-tone>
+    <dial-tone type="second">
+      <step freq="850" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="1.71"/>
+        <step length="3.31"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <route-tone>
+      <step freq="425" level="-12" length="0.05"/>
+      <step length="0.05"/>
+    </route-tone>
+  </tone-set>
+  <tone-set country="Costa Rica" uncode="cr">
+    <dial-tone>
+      <step freq="425" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="1.2"/>
+        <step length="4.6"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.3"/>
+        <step length="0.3"/>
+      </step>
+    </busy-tone>
+  </tone-set>
+  <tone-set country="Croatia" uncode="hr">
+    <dial-tone>
+      <step freq="425" level="-12" length="0.2"/>
+      <step length="0.3"/>
+      <step freq="425" level="-12" length="0.7"/>
+      <step length="0.8"/>
+    </dial-tone>
+    <dial-tone type="special">
+      <step freq="425" level="-12" length="0.4"/>
+      <step length="0.04"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="1.0"/>
+        <step length="4.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.25"/>
+        <step length="0.25"/>
+      </step>
+    </congestion-tone>
+    <special-information-tone>
+      <step freq="950" level="-12" length="0.333"/>
+      <step freq="1400" level="-12" length="0.333"/>
+      <step freq="1800" level="-12" length="0.333"/>
+      <step length="1.0"/>
+    </special-information-tone>
+    <warning-tone type="operator-intervening">
+      <step freq="425" level="-20" length="0.2"/>
+      <step length="0.3"/>
+      <step freq="425" level="-20" length="0.7"/>
+      <step length="0.8"/>
+    </warning-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step freq="950" level="-12" length="0.333"/>
+        <step freq="1400" level="-12" length="0.333"/>
+        <step freq="1800" level="-12" length="0.333"/>
+        <step length="1.0"/>
+      </step>
+    </number-unobtainable-tone>
+    <waiting-tone>
+      <step freq="425" level="-12" length="0.3"/>
+      <step length="8.0"/>
+    </waiting-tone>
+    <payphone-recognition-tone>
+      <step cycles="4">
+        <step freq="1206" level="-12" length="0.2"/>
+        <step length="0.2"/>
+        <step freq="850" level="-12" length="0.2"/>
+        <step length="2.0"/>
+      </step>
+    </payphone-recognition-tone>
+    <positive-indication-tone>
+      <step freq="425" level="-12"/>
+    </positive-indication-tone>
+    <negative-indication-tone>
+      <step freq="425" level="-12" length="0.25"/>
+      <step length="0.25"/>
+    </negative-indication-tone>
+  </tone-set>
+  <tone-set country="Cuba" uncode="cu">
+    <dial-tone>
+      <step freq="480+720 || 425" level="-12"/>
+    </dial-tone>
+    <dial-tone>
+      <step freq="425" level="-12" length="0.75"/>
+      <step length="0.75"/>
+      <step freq="425" level="-12" length="0.25"/>
+      <step length="0.25"/>
+    </dial-tone>
+    <dial-tone type="second">
+      <step freq="330+425" level="-12"/>
+    </dial-tone>
+    <dial-tone type="second">
+      <step freq="1050" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="440+480 || 440+620 || 480+620" level="-12" length="2.0"/>
+        <step length="4.0"/>
+      </step>
+    </ringback-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="1.0"/>
+        <step length="4.0"/>
+      </step>
+    </ringback-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="380+460" level="-12" length="1.0"/>
+        <step length="2.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="480+720 || 620+480 || 425" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="480+720" level="-12" length="0.25"/>
+        <step length="0.25"/>
+      </step>
+    </congestion-tone>
+    <special-information-tone>
+      <step freq="950" level="-12" length="0.333"/>
+      <step length="0.03"/>
+      <step freq="1400" level="-12" length="0.333"/>
+      <step length="0.03"/>
+      <step freq="1800" level="-12" length="0.333"/>
+      <step length="0.03"/>
+      <step length="1.0"/>
+    </special-information-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step freq="520" level="-12" length="0.4"/>
+        <step cycles="2">
+          <step length="0.12"/>
+          <step freq="520" level="-12" length="0.12"/>
+        </step>
+        <step length="0.12"/>
+      </step>
+    </number-unobtainable-tone>
+  </tone-set>
+  <tone-set country="Cyprus" uncode="cy">
+    <dial-tone>
+      <step freq="350+450" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="1.5"/>
+        <step length="3.0"/>
+      </step>
+    </ringback-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="375+425" level="-12" length="0.4"/>
+        <step length="0.2"/>
+        <step freq="375+425" level="-12" length="0.4"/>
+        <step length="2.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.25"/>
+        <step length="0.25"/>
+      </step>
+    </congestion-tone>
+    <warning-tone type="operator-intervening">
+      <step freq="1400" level="-12" length="0.5"/>
+      <step length="14.0"/>
+    </warning-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="2.5"/>
+        <step length="0.5"/>
+      </step>
+    </number-unobtainable-tone>
+    <waiting-tone>
+      <step freq="425" level="-12" length="0.2"/>
+      <step length="0.6"/>
+    </waiting-tone>
+    <payphone-recognition-tone>
+      <step freq="1638" level="-12" length="0.068"/>
+      <step length="0.068"/>
+      <step freq="1206" level="-12" length="0.068"/>
+      <step length="0.068"/>
+      <step freq="943" level="-12" length="0.068"/>
+      <step length="0.068"/>
+    </payphone-recognition-tone>
+  </tone-set>
+  <tone-set country="Czech Republic" uncode="cz">
+    <dial-tone>
+      <step freq="425" level="-12"/>
+    </dial-tone>
+    <dial-tone>
+      <step freq="425" level="-12" length="0.333"/>
+      <step length="0.333"/>
+      <step freq="425" level="-12" length="0.66"/>
+      <step length="0.66"/>
+    </dial-tone>
+    <dial-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.165"/>
+        <step length="0.165"/>
+        <step freq="425" level="-12" length="0.660"/>
+        <step length="0.660"/>
+      </step>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="1.0"/>
+        <step length="4.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.333"/>
+        <step length="0.333"/>
+      </step>
+    </busy-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.167"/>
+        <step length="0.167"/>
+      </step>
+    </busy-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.165"/>
+        <step length="0.165"/>
+        <step freq="425" level="-12" length="0.165"/>
+        <step length="0.330"/>
+      </step>
+    </busy-tone>
+    <special-information-tone>
+      <step freq="950" level="-12" length="0.333"/>
+      <step length="0.03"/>
+      <step freq="1400" level="-12" length="0.333"/>
+      <step length="0.03"/>
+      <step freq="1800" level="-12" length="0.333"/>
+      <step length="1.0"/>
+    </special-information-tone>
+    <waiting-tone>
+      <step freq="425" level="-12" length="0.333"/>
+      <step length="5.0"/>
+    </waiting-tone>
+    <connection-tone>
+      <step freq="425" level="-12" length="0.05"/>
+      <step length="0.05"/>
+    </connection-tone>
+    <offering-tone>
+      <step freq="425" level="-12" length="0.333"/>
+      <step length="0.333"/>
+      <step freq="425" level="-12" length="0.333"/>
+      <step length="1.5"/>
+    </offering-tone>
+  </tone-set>
+  <tone-set country="Denmark" uncode="dk">
+    <dial-tone>
+      <step freq="425" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="1.0"/>
+        <step length="4.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.25"/>
+        <step length="0.25"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.25"/>
+        <step length="0.25"/>
+      </step>
+    </congestion-tone>
+    <special-information-tone>
+      <step freq="950" level="-12" length="0.333"/>
+      <step freq="1400" level="-12" length="0.333"/>
+      <step freq="1800" level="-12" length="0.333"/>
+      <step length="1.0"/>
+    </special-information-tone>
+    <warning-tone type="operator-intervening">
+      <step freq="150" level="-12" length="0.05"/>
+      <step length="0.5"/>
+    </warning-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step freq="950" level="-12" length="0.333"/>
+        <step freq="1400" level="-12" length="0.333"/>
+        <step freq="1800" level="-12" length="0.333"/>
+        <step length="1.0"/>
+      </step>
+    </number-unobtainable-tone>
+    <pay-tone>
+      <step freq="950" level="-12" length="0.22"/>
+      <step freq="1400" level="-12" length="0.22"/>
+      <step freq="1800" level="-12" length="0.22"/>
+      <step length="1.0"/>
+    </pay-tone>
+    <payphone-recognition-tone>
+      <step freq="1477+941" level="-12" length="0.2"/>
+      <step length="0.2"/>
+      <step freq="1400+950" level="-12" length="0.2"/>
+      <step length="2.0"/>
+    </payphone-recognition-tone>
+  </tone-set>
+  <tone-set country="Diego Garcia" uncode="??">
+    <dial-tone>
+      <step freq="350+440" level="-12"/>
+    </dial-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="480+620" level="-12" length="interrupted at 60 ipm"/>
+      </step>
+    </busy-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="440+480" level="-12" length="1.0"/>
+        <step length="3.0"/>
+      </step>
+    </ringback-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step freq="480+620" level="-12" length="interrupted at 120 ipm"/>
+      </step>
+    </number-unobtainable-tone>
+  </tone-set>
+  <tone-set country="Djibouti" uncode="dj">
+    <dial-tone>
+      <step freq="425" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="1.5"/>
+        <step length="3.5"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <route-tone>
+      <step freq="425" level="-12" length="0.05"/>
+      <step length="0.05"/>
+    </route-tone>
+  </tone-set>
+  <tone-set country="Dominica (Commonwealth of)" uncode="dm">
+    <dial-tone>
+      <step freq="350+440" level="-12"/>
+    </dial-tone>
+    <dial-tone type="recall">
+      <step cycles="3">
+        <step freq="350+440" level="-12" length="0.1"/>
+        <step length="0.1"/>
+      </step>
+      <step freq="350+440" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="440+480" level="-12" length="2.0"/>
+        <step length="4.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="480+620" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="480+620" level="-12" length="0.25"/>
+        <step length="0.25"/>
+      </step>
+    </congestion-tone>
+    <special-information-tone>
+      <step freq="950" level="-12" length="0.333"/>
+      <step freq="1400" level="-12" length="0.333"/>
+      <step freq="1800" level="-12" length="0.333"/>
+    </special-information-tone>
+    <warning-tone type="operator-intervening">
+      <step freq="450" level="-12" length="0.2"/>
+      <step length="0.2"/>
+      <step freq="450" level="-12" length="0.65"/>
+      <step length="0.2"/>
+    </warning-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step freq="480+620" level="-12" length="0.25"/>
+        <step length="0.25"/>
+      </step>
+    </number-unobtainable-tone>
+    <waiting-tone>
+      <step cycles="1">
+        <step freq="440" level="-12" length="0.5"/>
+        <step length="10.0"/>
+        <step length="0.5"/>
+      </step>
+    </waiting-tone>
+    <payphone-recognition-tone>
+      <step freq="1600" level="-12" length="0.2"/>
+      <step length="0.2"/>
+      <step freq="1143" level="-12" length="0.2"/>
+      <step length="2.0"/>
+    </payphone-recognition-tone>
+  </tone-set>
+  <tone-set country="Dominican Republic" uncode="do">
+    <dial-tone>
+      <step freq="50" level="-12"/>
+    </dial-tone>
+    <dial-tone>
+      <step freq="33" level="-12" length="0.0166"/>
+      <step length="0.0166"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="400 || 450" level="-12" length="0.4"/>
+        <step length="0.2"/>
+        <step freq="400 || 450" level="-12" length="0.4"/>
+        <step length="2.2"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="0.8"/>
+        <step length="0.8"/>
+      </step>
+    </busy-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="0.375"/>
+        <step length="0.375"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="0.8"/>
+        <step length="0.8"/>
+      </step>
+    </congestion-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="0.375"/>
+        <step length="0.375"/>
+      </step>
+    </congestion-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12"/>
+      </step>
+    </number-unobtainable-tone>
+  </tone-set>
+  <tone-set country="Ecuador" uncode="ec">
+    <dial-tone>
+      <step freq="425" level="-12" length="15.0"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="1.2"/>
+        <step length="4.65"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.333"/>
+        <step length="0.333"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.333"/>
+        <step length="0.333"/>
+      </step>
+    </congestion-tone>
+    <waiting-tone>
+      <step freq="425" level="-12" length="0.2"/>
+      <step length="0.6"/>
+    </waiting-tone>
+  </tone-set>
+  <tone-set country="Egypt" uncode="eg">
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="375+475" level="-12" length="2.0"/>
+        <step length="1.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="375+475" level="-12" length="1.0"/>
+        <step length="4.0"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="450" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </congestion-tone>
+    <special-information-tone>
+      <step freq="950" level="-12" length="0.333"/>
+      <step freq="1400" level="-12" length="0.333"/>
+      <step freq="1800" level="-12" length="0.333"/>
+    </special-information-tone>
+    <intrusion-tone>
+      <step freq="450" level="-12" length="0.5"/>
+      <step length="0.5"/>
+    </intrusion-tone>
+    <payphone-recognition-tone>
+      <step freq="1100+1750" level="-12" length="0.2"/>
+      <step length="0.2"/>
+      <step freq="750+1450" level="-12" length="0.2"/>
+      <step length="2.0"/>
+    </payphone-recognition-tone>
+  </tone-set>
+  <tone-set country="El Salvador" uncode="sv">
+    <dial-tone>
+      <step freq="425" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="1.2"/>
+        <step length="5.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.333"/>
+        <step length="0.333"/>
+      </step>
+    </busy-tone>
+  </tone-set>
+  <tone-set country="Estonia" uncode="ee">
+    <dial-tone>
+      <step freq="425" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="1.0"/>
+        <step length="4.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.3"/>
+        <step length="0.3"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.2"/>
+        <step length="0.2"/>
+      </step>
+    </congestion-tone>
+    <special-information-tone>
+      <step freq="950" level="-12" length="0.333"/>
+      <step freq="1400" level="-12" length="0.333"/>
+      <step freq="1800" level="-12" length="0.333"/>
+    </special-information-tone>
+    <warning-tone type="operator-intervening">
+      <step freq="425" level="-12" length="0.2"/>
+      <step length="0.3"/>
+      <step freq="425" level="-12" length="0.2"/>
+      <step length="1.3"/>
+    </warning-tone>
+    <waiting-tone>
+      <step freq="950" level="-12" length="0.33"/>
+      <step freq="1400" level="-12" length="0.33"/>
+      <step freq="1800" level="-12" length="0.33"/>
+      <step length="0.3"/>
+    </waiting-tone>
+  </tone-set>
+  <tone-set country="Ethiopia" uncode="et">
+    <dial-tone>
+      <step freq="425" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="1.0"/>
+        <step length="4.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.4"/>
+        <step length="0.4"/>
+      </step>
+    </busy-tone>
+    <special-information-tone>
+      <step freq="950" level="-12" length="0.333"/>
+      <step freq="1400" level="-12" length="0.333"/>
+      <step freq="1800" level="-12" length="0.333"/>
+      <step length="1.0"/>
+    </special-information-tone>
+    <intrusion-tone>
+      <step freq="425" level="-12" length="0.5"/>
+      <step length="0.2"/>
+    </intrusion-tone>
+    <waiting-tone>
+      <step freq="425" level="-12" length="0.2"/>
+      <step length="0.6"/>
+    </waiting-tone>
+  </tone-set>
+  <tone-set country="Falkland Islands" uncode="fk">
+    <dial-tone>
+      <step freq="350+450" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="400+450" level="-12" length="0.4"/>
+        <step length="0.2"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="0.375"/>
+        <step length="0.375"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="0.4"/>
+        <step length="0.35"/>
+        <step freq="400" level="-12" length="0.255"/>
+        <step length="0.525"/>
+      </step>
+    </congestion-tone>
+    <special-information-tone>
+      <step freq="950" level="-12" length="0.333"/>
+      <step freq="1400" level="-12" length="0.333"/>
+      <step freq="1800" level="-12" length="0.333"/>
+    </special-information-tone>
+    <warning-tone type="operator-intervening">
+      <step freq="1000" level="-12" length="0.1"/>
+      <step length="0.1"/>
+    </warning-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12"/>
+      </step>
+    </number-unobtainable-tone>
+  </tone-set>
+  <tone-set country="Faroe Islands" uncode="fo">
+    <dial-tone>
+      <step freq="425" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="1.0"/>
+        <step length="4.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.25"/>
+        <step length="0.25"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.25"/>
+        <step length="0.25"/>
+      </step>
+    </congestion-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step freq="950" level="-12" length="0.333"/>
+        <step freq="1400" level="-12" length="0.333"/>
+        <step freq="1800" level="-12" length="0.333"/>
+      </step>
+    </number-unobtainable-tone>
+  </tone-set>
+  <tone-set country="Fiji" uncode="fj">
+    <dial-tone>
+      <step freq="33 || 50" level="-12"/>
+    </dial-tone>
+    <dial-tone domain="PABX">
+      <step freq="33.33" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="116.33+149.67 || 375+425" level="-12" length="0.4"/>
+        <step length="0.2"/>
+        <step freq="116.33+149.67 || 375+425" level="-12" length="0.4"/>
+        <step length="2.0"/>
+      </step>
+    </ringback-tone>
+    <ringback-tone domain="PABX">
+      <step cycles="endless">
+        <step freq="383.33+416.67" level="-12" length="0.4"/>
+        <step length="0.2"/>
+        <step freq="383.33+416.67" level="-12" length="0.4"/>
+        <step length="2.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="0.75"/>
+        <step length="0.75"/>
+      </step>
+    </busy-tone>
+    <busy-tone domain="PABX">
+      <step cycles="endless">
+        <step freq="400" level="-12" length="0.375"/>
+        <step length="0.375"/>
+      </step>
+    </busy-tone>
+    <warning-tone type="operator-intervening">
+      <step freq="400" level="-12" length="0.5"/>
+      <step length="0.17"/>
+      <step freq="400" level="-12" length="0.17"/>
+      <step length="0.17"/>
+    </warning-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="2.5"/>
+        <step length="0.5"/>
+      </step>
+    </number-unobtainable-tone>
+    <number-unobtainable-tone domain="PABX">
+      <step cycles="endless">
+        <step freq="400" level="-12" length="2.5"/>
+        <step length="0.5"/>
+      </step>
+    </number-unobtainable-tone>
+  </tone-set>
+  <tone-set country="Finland" uncode="fi">
+    <dial-tone>
+      <step freq="425" level="-12"/>
+    </dial-tone>
+    <dial-tone domain="PABX">
+      <step cycles="2">
+        <step freq="425" level="-12" length="0.2"/>
+        <step length="0.3"/>
+      </step>
+      <step length="0.2"/>
+      <step length="0.8"/>
+    </dial-tone>
+    <dial-tone type="special">
+      <step freq="425" level="-12" length="0.65"/>
+      <step length="0.025"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="1.0"/>
+        <step length="4.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.3"/>
+        <step length="0.3"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.2"/>
+        <step length="0.2"/>
+      </step>
+    </congestion-tone>
+    <special-information-tone>
+      <step freq="950" level="-12" length="0.333"/>
+      <step freq="1400" level="-12" length="0.333"/>
+      <step freq="1800" level="-12" length="0.333"/>
+      <step length="1.0"/>
+    </special-information-tone>
+    <warning-tone type="operator-intervening">
+      <step freq="425" level="-12" length="0.2"/>
+      <step length="0.3"/>
+      <step freq="425" level="-12" length="0.2"/>
+      <step length="1.3"/>
+    </warning-tone>
+    <queue-tone>
+      <step freq="950" level="-12" length="0.65"/>
+      <step length="0.325"/>
+      <step freq="950" level="-12" length="0.325"/>
+      <step length="1.3"/>
+      <step freq="1400" level="-12" length="2.6"/>
+    </queue-tone>
+    <waiting-tone>
+      <step freq="425" level="-12" length="0.15"/>
+      <step length="0.15"/>
+      <step freq="425" level="-12" length="0.15"/>
+      <step length="8.0"/>
+    </waiting-tone>
+  </tone-set>
+  <tone-set country="France" uncode="fr">
+    <dial-tone>
+      <step freq="330+440 || 440" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="440" level="-12" length="1.5"/>
+        <step length="3.5"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="440" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <special-information-tone>
+      <step freq="950" level="-12" length="0.3"/>
+      <step length="0.03"/>
+      <step freq="1400" level="-12" length="0.3"/>
+      <step length="0.03"/>
+      <step freq="1800" level="-12" length="0.3"/>
+      <step length="1.0"/>
+    </special-information-tone>
+    <route-tone>
+      <step freq="440" level="-12" length="0.05"/>
+      <step length="0.05"/>
+    </route-tone>
+  </tone-set>
+  <tone-set country="French Polynesia" uncode="pf">
+    <dial-tone>
+      <step freq="440 || 330+440" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="440" level="-12" length="1.5"/>
+        <step length="3.5"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="440" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <special-information-tone>
+      <step freq="950" level="-12" length="0.3"/>
+      <step length="0.03"/>
+      <step freq="1400" level="-12" length="0.3"/>
+      <step length="0.03"/>
+      <step freq="1800" level="-12" length="0.3"/>
+      <step length="1.0"/>
+    </special-information-tone>
+    <route-tone>
+      <step freq="440" level="-12" length="0.05"/>
+      <step length="0.05"/>
+    </route-tone>
+  </tone-set>
+  <tone-set country="Gabon" uncode="ga">
+    <dial-tone>
+      <step freq="440" level="-12" length="15.0"/>
+    </dial-tone>
+    <dial-tone>
+      <step freq="425" level="-12"/>
+    </dial-tone>
+    <dial-tone type="second">
+      <step freq="330+440" level="-12" length="7.0"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="440" level="-12" length="1.5"/>
+        <step length="3.0"/>
+      </step>
+    </ringback-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="1.5"/>
+        <step length="3.5"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="440" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <special-information-tone>
+      <step freq="900" level="-12" length="0.35"/>
+      <step freq="1380" level="-12" length="0.30"/>
+      <step freq="1860" level="-12" length="0.35"/>
+      <step length="1.0"/>
+    </special-information-tone>
+    <route-tone>
+      <step freq="440" level="-12" length="0.05"/>
+      <step length="0.05"/>
+    </route-tone>
+    <route-tone>
+      <step freq="425" level="-12" length="0.05"/>
+      <step length="0.05"/>
+    </route-tone>
+  </tone-set>
+  <tone-set country="Gambia" uncode="gm">
+    <dial-tone>
+      <step freq="425" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="1.0"/>
+        <step length="4.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+  </tone-set>
+  <tone-set country="Germany" uncode="de">
+    <dial-tone>
+      <step freq="425 || 450" level="-12"/>
+    </dial-tone>
+    <dial-tone type="special">
+      <step freq="400+425" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425 || 450" level="-12" length="0.25"/>
+        <step length="4.0"/>
+        <step freq="425 || 450" level="-12" length="1.0"/>
+        <step length="4.0"/>
+        <step freq="425 || 450" level="-12" length="1.0"/>
+        <step length="4.0"/>
+      </step>
+    </ringback-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425 || 450" level="-12" length="0.5"/>
+        <step length="4.0"/>
+        <step freq="425 || 450" level="-12" length="1.0"/>
+        <step length="4.0"/>
+        <step freq="425 || 450" level="-12" length="1.0"/>
+        <step length="4.0"/>
+      </step>
+    </ringback-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425 || 450" level="-12" length="1.0"/>
+        <step length="5.0"/>
+      </step>
+    </ringback-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="450" level="-12" length="1.0"/>
+        <step length="9.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.48"/>
+        <step length="0.48"/>
+      </step>
+    </busy-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425 || 450" level="-12" length="0.15"/>
+        <step length="0.475"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.24"/>
+        <step length="0.24"/>
+      </step>
+    </congestion-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="425 || 450" level="-12" length="0.15"/>
+        <step length="0.475"/>
+      </step>
+    </congestion-tone>
+    <special-information-tone>
+      <step freq="950" level="-12" length="0.333"/>
+      <step freq="1400" level="-12" length="0.333"/>
+      <step freq="1800" level="-12" length="0.333"/>
+      <step length="1.0"/>
+    </special-information-tone>
+    <warning-tone type="operator-intervening">
+      <step freq="425 || 450" level="-12" length="0.24"/>
+      <step length="0.24"/>
+      <step freq="425 || 450" level="-12" length="0.24"/>
+      <step length="1.28"/>
+    </warning-tone>
+    <waiting-tone>
+      <step freq="425" level="-12" length="0.2"/>
+      <step length="0.2"/>
+      <step freq="425" level="-12" length="0.2"/>
+      <step length="5.0"/>
+    </waiting-tone>
+    <payphone-recognition-tone>
+      <step cycles="5">
+        <step freq="1633" level="-12" length="0.2"/>
+        <step length="0.2"/>
+        <step freq="1336" level="-12" length="0.2"/>
+        <step length="2.0"/>
+      </step>
+    </payphone-recognition-tone>
+    <payphone-recognition-tone>
+      <step cycles="5">
+        <step freq="1366" level="-12" length="0.2"/>
+        <step length="0.2"/>
+        <step freq="1024" level="-12" length="0.2"/>
+        <step length="2.2"/>
+      </step>
+    </payphone-recognition-tone>
+    <payphone-recognition-tone>
+      <step cycles="6">
+        <step freq="1336" level="-12" length="2.2"/>
+        <step length="0.2"/>
+        <step freq="1024" level="-12" length="0.2"/>
+        <step length="0.2"/>
+      </step>
+    </payphone-recognition-tone>
+    <payphone-recognition-tone>
+      <step cycles="5">
+        <step freq="1645+857" level="-12" length="0.2"/>
+        <step length="0.2"/>
+        <step freq="1215+935" level="-12" length="0.2"/>
+        <step length="2.0"/>
+      </step>
+    </payphone-recognition-tone>
+  </tone-set>
+  <tone-set country="Ghana" uncode="gh">
+    <dial-tone>
+      <step freq="400" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="1.0"/>
+        <step length="4.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="0.25"/>
+        <step length="0.25"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </congestion-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="0.075"/>
+        <step length="0.1"/>
+        <step freq="400" level="-12" length="0.075"/>
+        <step length="0.1"/>
+        <step freq="400" level="-12" length="0.075"/>
+        <step length="0.4"/>
+      </step>
+    </number-unobtainable-tone>
+    <waiting-tone>
+      <step freq="400" level="-12" length="0.5"/>
+      <step length="0.3"/>
+      <step freq="400" level="-12" length="0.2"/>
+      <step length="0.3"/>
+      <step freq="400" level="-12" length="0.2"/>
+      <step length="3.0"/>
+    </waiting-tone>
+    <holding-tone>
+      <step freq="400" level="-12" length="0.5"/>
+      <step length="0.5"/>
+      <step freq="400" level="-12" length="0.5"/>
+      <step length="0.25"/>
+    </holding-tone>
+  </tone-set>
+  <tone-set country="Gibraltar" uncode="gi">
+    <dial-tone>
+      <step freq="350+400" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="400+450" level="-12" length="0.4"/>
+        <step length="0.2"/>
+        <step freq="400+450" level="-12" length="0.4"/>
+        <step length="2.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="0.375"/>
+        <step length="0.375"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="0.4"/>
+        <step length="0.35"/>
+        <step freq="400" level="-12" length="0.225"/>
+        <step length="0.525"/>
+      </step>
+    </congestion-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12"/>
+      </step>
+    </number-unobtainable-tone>
+    <waiting-tone>
+      <step freq="400" level="-12" length="0.1"/>
+      <step length="3.0"/>
+    </waiting-tone>
+  </tone-set>
+  <tone-set country="Greece" uncode="gr">
+    <dial-tone>
+      <step cycles="endless">
+        <step freq="425 || 450" level="-12" length="0.2"/>
+        <step length="0.3"/>
+        <step freq="425 || 450" level="-12" length="0.7"/>
+        <step length="0.8"/>
+      </step>
+    </dial-tone>
+    <dial-tone type="special">
+      <step cycles="endless">
+        <step freq="400 || 425" level="-12" length="0.2"/>
+        <step length="0.3"/>
+        <step freq="425 || 450" level="-12" length="0.7"/>
+        <step length="0.8"/>
+      </step>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425 || 450" level="-12" length="1.0"/>
+        <step length="4.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425 || 450" level="-12" length="0.3"/>
+        <step length="0.3"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.15"/>
+        <step length="0.15"/>
+      </step>
+    </congestion-tone>
+    <special-information-tone>
+      <step freq="950" level="-12" length="0.33"/>
+      <step freq="1450" level="-12" length="0.33"/>
+      <step freq="1800" level="-12" length="0.33"/>
+      <step length="1.0"/>
+    </special-information-tone>
+    <waiting-tone>
+      <step freq="425" level="-12" length="0.3"/>
+      <step length="10.0"/>
+      <step freq="425" level="-12" length="0.3"/>
+      <step length="10.0"/>
+    </waiting-tone>
+    <intrusion-tone>
+      <step freq="425 || 450" level="-12" length="0.15"/>
+      <step length="0.25"/>
+      <step freq="425 || 450" level="-12" length="0.15"/>
+      <step length="1.45"/>
+    </intrusion-tone>
+    <holding-tone>
+      <step freq="900" level="-12" length="0.5"/>
+      <step length="0.5"/>
+    </holding-tone>
+    <valid-tone-radio-paging>
+      <step freq="450" level="-12" length="0.1"/>
+      <step length="0.1"/>
+      <step freq="450" level="-12" length="0.1"/>
+      <step length="0.7"/>
+    </valid-tone-radio-paging>
+    <number-unobtainable-tone domain="radio-paging">
+      <step cycles="endless">
+        <step freq="450" level="-12"/>
+      </step>
+    </number-unobtainable-tone>
+  </tone-set>
+  <tone-set country="Greenland" uncode="gl">
+    <dial-tone>
+      <step freq="445 || 425" level="-12"/>
+    </dial-tone>
+    <dial-tone type="second">
+      <step freq="445 || 425" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="445 || 425" level="-12" length="1.0"/>
+        <step length="4.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="445 || 425" level="-12" length="0.25"/>
+        <step length="0.25"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="445 || 425" level="-12" length="0.25"/>
+        <step length="0.25"/>
+      </step>
+    </congestion-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step freq="950" level="-12" length="0.333"/>
+        <step freq="1400" level="-12" length="0.333"/>
+        <step freq="1800" level="-12" length="0.333"/>
+      </step>
+    </number-unobtainable-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.25"/>
+        <step length="0.25"/>
+      </step>
+    </number-unobtainable-tone>
+    <pay-tone>
+      <step freq="950" level="-12" length="0.22"/>
+      <step freq="1400" level="-12" length="0.22"/>
+      <step freq="1800" level="-12" length="0.22"/>
+      <step length="1.0"/>
+    </pay-tone>
+  </tone-set>
+  <tone-set country="Grenada" uncode="gd">
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="440+480" level="-12" length="2.0"/>
+        <step length="4.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="460+620" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step recorded-announcement="true"/>
+      </step>
+    </congestion-tone>
+  </tone-set>
+  <tone-set country="Guadeloupe (French Dep.)" uncode="gp">
+    <dial-tone>
+      <step freq="440 || 330+440" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="440" level="-12" length="1.5"/>
+        <step length="3.5"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="440" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <special-information-tone>
+      <step freq="950" level="-12" length="0.3"/>
+      <step length="0.03"/>
+      <step freq="1400" level="-12" length="0.3"/>
+      <step length="0.03"/>
+      <step freq="1800" level="-12" length="0.3"/>
+      <step length="1.0"/>
+    </special-information-tone>
+    <route-tone>
+      <step freq="440" level="-12" length="0.05"/>
+      <step length="0.05"/>
+    </route-tone>
+  </tone-set>
+  <tone-set country="Guiana (French Dep.)" uncode="gf">
+    <dial-tone>
+      <step freq="440 || 330+440" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="440" level="-12" length="1.5"/>
+        <step length="3.5"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="440" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <special-information-tone>
+      <step freq="950" level="-12" length="0.3"/>
+      <step length="0.03"/>
+      <step freq="1400" level="-12" length="0.3"/>
+      <step length="0.03"/>
+      <step freq="1800" level="-12" length="0.3"/>
+      <step length="1.0"/>
+    </special-information-tone>
+    <route-tone>
+      <step freq="440" level="-12" length="0.05"/>
+      <step length="0.05"/>
+    </route-tone>
+  </tone-set>
+  <tone-set country="Guinea" uncode="gn">
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="450" level="-12" length="0.4"/>
+        <step length="0.2"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="450" level="-12" length="0.2"/>
+        <step length="0.2"/>
+      </step>
+    </busy-tone>
+  </tone-set>
+  <tone-set country="Guyana" uncode="gy">
+    <dial-tone>
+      <step freq="360+400" level="-12" length="1.3"/>
+      <step length="0.3"/>
+      <step freq="360+400" level="-12" length="0.2"/>
+      <step length="0.5"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="1.75"/>
+        <step length="3.25"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </congestion-tone>
+    <waiting-tone>
+      <step freq="480" level="-12" length="0.5"/>
+      <step length="18.0"/>
+    </waiting-tone>
+  </tone-set>
+  <tone-set country="Honduras" uncode="hn">
+    <dial-tone>
+      <step freq="440" level="-12"/>
+    </dial-tone>
+    <dial-tone type="second">
+      <step freq="440" level="-12" length="0.135"/>
+      <step length="0.135"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="440" level="-12" length="0.5"/>
+        <step length="4.0"/>
+        <step freq="440" level="-12" length="2.0"/>
+        <step length="4.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="440" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="440" level="-12" length="0.25"/>
+        <step length="0.25"/>
+      </step>
+    </congestion-tone>
+    <waiting-tone>
+      <step freq="440" level="-12" length="0.5"/>
+      <step length="0.5"/>
+      <step freq="440" level="-12" length="0.2"/>
+      <step length="2.7"/>
+    </waiting-tone>
+  </tone-set>
+  <tone-set country="Hong Kong" uncode="hk">
+    <dial-tone>
+      <step freq="350+440 [1%]" level="-13 [-15 -5]" recognition-length="0.3"/>
+    </dial-tone>
+    <dial-tone type="recall">
+      <step cycles="3">
+        <step freq="350+440 [1%]" level="-13 [-15 -5]" length="0.1 [10%]"/>
+        <step length="0.1"/>
+      </step>
+      <step freq="350+440 [1%]" level="-13 [-15 -5]" recognition-length="0.3"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="440+480 [1%]" level="-13" length="0.4 [10%]"/>
+        <step length="0.2 [10%]"/>
+        <step freq="440+480 [1%]" level="-13" length="0.4 [10%]"/>
+        <step length="3.0 [10%]" recognition-length="0.6"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="480+620 [1%]" level="-13" length="0.5 [10%]"/>
+        <step length="0.5 [10%]"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="480+620 [1%]" level="-13" length="0.25 [10%]"/>
+        <step length="0.25"/>
+      </step>
+    </congestion-tone>
+    <number-unobtainable-tone>
+      <step freq="480+620 [1%]" level="-13" recognition-length="0.9"/>
+    </number-unobtainable-tone>
+    <waiting-tone>
+      <step cycles="endless">
+        <step cycles="3">
+          <step freq="440 [1%]" level="-10" length="0.5 [10%]"/>
+          <step length="0.5 [10%]"/>
+        </step>
+        <step length="8.0 [10%]" recognition-length="1.0"/>
+      </step>
+    </waiting-tone>
+  </tone-set>
+  <tone-set country="Hungary" uncode="hu">
+    <dial-tone>
+      <step freq="425" level="-12"/>
+    </dial-tone>
+    <dial-tone type="second">
+      <step freq="425" level="-12"/>
+    </dial-tone>
+    <dial-tone type="special">
+      <step freq="350+375+400" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="1.2"/>
+        <step length="3.7"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.3"/>
+        <step length="0.3"/>
+      </step>
+    </busy-tone>
+    <special-information-tone>
+      <step freq="950" level="-12" length="0.333"/>
+      <step freq="1400" level="-12" length="0.333"/>
+      <step freq="1800" level="-12" length="0.333"/>
+      <step length="1.0"/>
+    </special-information-tone>
+    <warning-tone type="operator-intervening">
+      <step freq="425" level="-12" length="0.3"/>
+      <step length="0.3"/>
+      <step freq="425" level="-12" length="0.3"/>
+      <step length="1.5"/>
+    </warning-tone>
+    <waiting-tone>
+      <step freq="425+450" level="-12" length="0.3"/>
+      <step length="0.3"/>
+    </waiting-tone>
+    <payphone-recognition-tone>
+      <step freq="1100" level="-12" length="0.2"/>
+      <step length="0.2"/>
+      <step freq="750" level="-12" length="0.2"/>
+      <step length="2.0"/>
+    </payphone-recognition-tone>
+    <positive-indication-tone>
+      <step freq="300+420" level="-12" length="1.0"/>
+      <step length="0.2"/>
+      <step recorded-announcement="true"/>
+    </positive-indication-tone>
+    <negative-indication-tone>
+      <step freq="300+420" level="-12" length="0.2"/>
+      <step length="0.2"/>
+      <step recorded-announcement="true"/>
+    </negative-indication-tone>
+    <offering-tone>
+      <step freq="425" level="-12" length="0.3"/>
+      <step length="0.3"/>
+      <step freq="425" level="-12" length="0.3"/>
+      <step length="1.5"/>
+    </offering-tone>
+  </tone-set>
+  <tone-set country="Iceland" uncode="is">
+    <dial-tone>
+      <step freq="425" level="-12"/>
+    </dial-tone>
+    <dial-tone type="special">
+      <step freq="425" level="-12" length="0.4"/>
+      <step length="0.04"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="1.2"/>
+        <step length="4.7"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.25"/>
+        <step length="0.25"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.25"/>
+        <step length="0.25"/>
+      </step>
+    </congestion-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step recorded-announcement="true"/>
+      </step>
+    </number-unobtainable-tone>
+    <waiting-tone>
+      <step freq="425" level="-12" length="0.2"/>
+      <step length="0.6"/>
+      <step freq="425" level="-12" length="0.2"/>
+      <step length="5.0"/>
+    </waiting-tone>
+    <payphone-recognition-tone>
+      <step freq="1633" level="-12" length="0.2"/>
+      <step length="0.2"/>
+      <step freq="1209" level="-12" length="0.2"/>
+      <step length="2.0"/>
+    </payphone-recognition-tone>
+  </tone-set>
+  <tone-set country="India" uncode="in">
+    <dial-tone>
+      <step freq="33 || 50 || 375+425" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="375+425" level="-12" length="0.4"/>
+        <step length="0.2"/>
+        <step freq="375+425" level="-12" length="0.4"/>
+        <step length="2.6"/>
+      </step>
+    </ringback-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="133 || 375+425" level="-12" length="0.4"/>
+        <step length="0.2"/>
+        <step freq="133 || 375+425" level="-12" length="0.4"/>
+        <step length="2.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="0.75"/>
+        <step length="0.75"/>
+      </step>
+    </busy-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="0.6"/>
+        <step length="0.6"/>
+      </step>
+    </busy-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12"/>
+      </step>
+    </number-unobtainable-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="2.5"/>
+        <step length="0.5"/>
+      </step>
+    </number-unobtainable-tone>
+  </tone-set>
+  <tone-set country="Indonesia" uncode="id">
+    <dial-tone>
+      <step freq="425" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="1.0"/>
+        <step length="4.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.25"/>
+        <step length="0.25"/>
+      </step>
+    </congestion-tone>
+    <special-information-tone>
+      <step freq="950" level="-12" length="0.33"/>
+      <step length="0.03"/>
+      <step freq="1400" level="-12" length="0.33"/>
+      <step length="0.03"/>
+      <step freq="1800" level="-12" length="0.33"/>
+      <step length="1.0"/>
+    </special-information-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="2.0"/>
+        <step length="0.5"/>
+      </step>
+    </number-unobtainable-tone>
+    <payphone-recognition-tone>
+      <step freq="1200" level="-12" length="0.2"/>
+      <step length="0.2"/>
+      <step freq="800" level="-12" length="0.2"/>
+      <step length="2.0"/>
+    </payphone-recognition-tone>
+    <offering-tone>
+      <step freq="425" level="-12" length="0.5"/>
+      <step length="0.5"/>
+      <step freq="425" level="-12" length="0.5"/>
+      <step length="1.0"/>
+    </offering-tone>
+  </tone-set>
+  <tone-set country="Iran" uncode="ir">
+    <dial-tone>
+      <step freq="425" level="-12"/>
+    </dial-tone>
+    <dial-tone type="second">
+      <step freq="400+425" level="-12"/>
+    </dial-tone>
+    <dial-tone type="recall">
+      <step freq="425" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="1.0"/>
+        <step length="4.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.25"/>
+        <step length="0.25"/>
+      </step>
+    </congestion-tone>
+    <special-information-tone>
+      <step freq="950" level="-12" length="0.333"/>
+      <step freq="1400" level="-12" length="0.333"/>
+      <step freq="1800" level="-12" length="0.333"/>
+    </special-information-tone>
+    <waiting-tone>
+      <step freq="425" level="-12" length="0.2"/>
+      <step length="0.2"/>
+      <step freq="425" level="-12" length="0.2"/>
+      <step length="10.0"/>
+    </waiting-tone>
+    <holding-tone>
+      <step freq="950" level="-12" length="0.65"/>
+      <step length="0.325"/>
+      <step freq="950" level="-12" length="0.325"/>
+      <step length="1.3"/>
+      <step freq="1400" level="-12" length="2.6"/>
+    </holding-tone>
+    <offering-tone>
+      <step freq="425" level="-12" length="0.15"/>
+      <step length="0.25"/>
+      <step freq="425" level="-12" length="0.15"/>
+      <step length="1.45"/>
+    </offering-tone>
+    <preemption-tone>
+      <step cycles="3">
+        <step freq="1400" level="-12" length="0.1"/>
+        <step length="0.1"/>
+      </step>
+    </preemption-tone>
+    <permanent-signal-tone>
+      <step freq="1400" level="-12"/>
+    </permanent-signal-tone>
+  </tone-set>
+  <tone-set country="Iraq" uncode="iq">
+    <dial-tone type="recall">
+      <step freq="133" level="-12" length="0.4"/>
+      <step length="0.2"/>
+      <step freq="133" level="-12" length="0.4"/>
+      <step length="1.5"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="1.0"/>
+        <step length="1.0"/>
+      </step>
+    </busy-tone>
+  </tone-set>
+  <tone-set country="Ireland" uncode="ie">
+    <dial-tone>
+      <step freq="400+425+450" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="400 || 450 || 400+450" level="-12" length="0.4"/>
+        <step length="0.2"/>
+        <step freq="400 || 450 || 400+450" level="-12" length="0.4"/>
+        <step length="2.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <special-information-tone>
+      <step freq="950" level="-12" length="0.333"/>
+      <step freq="1400" level="-12" length="0.333"/>
+      <step freq="1800" level="-12" length="0.333"/>
+      <step length="1.0"/>
+    </special-information-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step freq="400 || 425" level="-12" length="6.0"/>
+        <step length="1.0"/>
+      </step>
+    </number-unobtainable-tone>
+    <route-tone>
+      <step freq="425" level="-12" length="0.06"/>
+      <step length="0.06"/>
+    </route-tone>
+    <payphone-recognition-tone>
+      <step freq="1100+1750" level="-12" length="0.2"/>
+      <step length="0.2"/>
+      <step freq="750+1450" level="-12" length="0.2"/>
+      <step length="2.0"/>
+    </payphone-recognition-tone>
+  </tone-set>
+  <tone-set country="Israel" uncode="il">
+    <dial-tone>
+      <step freq="400 || 425 || 440" level="-12"/>
+    </dial-tone>
+    <dial-tone type="second">
+      <step cycles="3">
+        <step freq="400 || 440" level="-12" length="0.1"/>
+        <step length="0.1"/>
+      </step>
+      <step freq="400 || 440" level="-12"/>
+    </dial-tone>
+    <dial-tone type="recall">
+      <step cycles="3">
+        <step freq="400 || 440" level="-12" length="0.1"/>
+        <step length="0.1"/>
+      </step>
+      <step freq="400 || 440" level="-12"/>
+    </dial-tone>
+    <dial-tone type="special">
+      <step freq="400" level="-12" length="1.0"/>
+      <step length="0.25"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="400 || 425 || 440" level="-12" length="1.0"/>
+        <step length="3.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="400 || 425 || 440" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <special-information-tone>
+      <step freq="975" level="-12" length="0.333"/>
+      <step freq="1400" level="-12" length="0.333"/>
+      <step freq="1800" level="-12" length="0.333"/>
+      <step length="1.0"/>
+      <step recorded-announcement="true"/>
+    </special-information-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step freq="975" level="-12" length="0.333"/>
+        <step freq="1400" level="-12" length="0.333"/>
+        <step freq="1800" level="-12" length="0.333"/>
+        <step length="1.0"/>
+        <step recorded-announcement="true"/>
+      </step>
+    </number-unobtainable-tone>
+    <waiting-tone>
+      <step cycles="1">
+        <step freq="400 || 440" level="-12" length="0.15"/>
+        <step length="10.0"/>
+        <step freq="400 || 440" level="-12" length="0.15"/>
+      </step>
+    </waiting-tone>
+    <call-waiting-tone>
+      <step cycles="2">
+        <step freq="400 || 440" level="-12" length="0.1"/>
+        <step length="0.1"/>
+      </step>
+      <step freq="400 || 440" level="-12" length="0.6"/>
+      <step length="3.0"/>
+    </call-waiting-tone>
+    <confirmation-tone>
+      <step cycles="1">
+        <step freq="400 || 440" level="-12" length="0.15"/>
+        <step length="0.15"/>
+        <step freq="400 || 440" level="-12" length="0.3"/>
+      </step>
+    </confirmation-tone>
+    <confirmation-tone>
+      <step freq="400" level="-12" length="0.04"/>
+      <step length="0.04"/>
+    </confirmation-tone>
+    <payphone-recognition-tone>
+      <step freq="1209" level="-12" length="0.2"/>
+      <step length="0.2"/>
+      <step freq="452" level="-12" length="0.2"/>
+      <step length="2.0"/>
+    </payphone-recognition-tone>
+    <search-tone>
+      <step freq="1004" level="-12" length="0.01"/>
+      <step length="1.0"/>
+    </search-tone>
+    <holding-tone>
+      <step freq="400" level="-12" length="0.05"/>
+      <step length="2.0"/>
+    </holding-tone>
+  </tone-set>
+  <tone-set country="Italy" uncode="it">
+    <dial-tone>
+      <step freq="425" level="-12" length="0.6"/>
+      <step length="1.0"/>
+      <step freq="425" level="-12" length="0.2"/>
+      <step length="0.2"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="1.0"/>
+        <step length="4.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.2"/>
+        <step length="0.2"/>
+      </step>
+    </busy-tone>
+  </tone-set>
+  <tone-set country="Ivory Coast" uncode="ci">
+    <dial-tone>
+      <step freq="425" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="50 || 375+475" level="-12" length="1.66"/>
+        <step length="3.33"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <route-tone>
+      <step freq="425" level="-12" length="0.05"/>
+      <step length="0.05"/>
+    </route-tone>
+  </tone-set>
+  <tone-set country="Jamaica" uncode="jm">
+    <dial-tone>
+      <step freq="350+440" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="440+480" level="-12" length="2.0"/>
+        <step length="4.0"/>
+      </step>
+    </ringback-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="440+480" level="-12" length="1.5"/>
+        <step length="4.5"/>
+      </step>
+    </ringback-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="440+480" level="-12" length="1.0"/>
+        <step length="4.0"/>
+      </step>
+    </ringback-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="400 || 133" level="-12" length="0.4"/>
+        <step length="0.2"/>
+        <step freq="400 || 133" level="-12" length="0.4"/>
+        <step length="2.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="0.75"/>
+        <step length="0.75"/>
+      </step>
+    </busy-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="480+620" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="480+620" level="-12" length="0.3"/>
+        <step length="0.2"/>
+      </step>
+    </congestion-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="480+620" level="-12" length="0.2"/>
+        <step length="0.3"/>
+      </step>
+    </congestion-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="3.0"/>
+        <step length="0.5"/>
+      </step>
+    </number-unobtainable-tone>
+    <record-tone>
+      <step freq="480+620" level="-12" length="0.25"/>
+      <step length="0.25"/>
+    </record-tone>
+  </tone-set>
+  <tone-set country="Japan" uncode="jp">
+    <dial-tone>
+      <step freq="400" level="-12"/>
+    </dial-tone>
+    <dial-tone domain="PABX">
+      <step cycles="endless">
+        <step freq="400" level="-12" length="0.25"/>
+        <step length="0.25"/>
+      </step>
+    </dial-tone>
+    <dial-tone type="second">
+      <step cycles="endless">
+        <step freq="400" level="-12" length="0.125"/>
+        <step length="0.125"/>
+      </step>
+    </dial-tone>
+    <dial-tone type="second">
+      <step cycles="endless">
+        <step freq="440" level="-12" length="0.125"/>
+        <step length="0.125"/>
+      </step>
+    </dial-tone>
+    <dial-tone type="second">
+      <step cycles="endless">
+        <step freq="400" level="-12" length="0.15"/>
+        <step length="0.15"/>
+      </step>
+    </dial-tone>
+    <dial-tone type="recall">
+      <step freq="350+440" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="384+416" level="-12" length="1.0"/>
+        <step length="2.0"/>
+      </step>
+    </ringback-tone>
+    <ringback-tone domain="PABX">
+      <step cycles="endless">
+        <step freq="380+420" level="-12" length="1.0"/>
+        <step length="2.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </congestion-tone>
+    <special-information-tone>
+      <step freq="400" level="-12" length="0.1"/>
+      <step length="0.1"/>
+    </special-information-tone>
+    <waiting-tone>
+      <step freq="440" level="-12" length="0.1"/>
+      <step length="0.1"/>
+      <step freq="440" level="-12" length="0.1"/>
+      <step length="1.0"/>
+    </waiting-tone>
+    <waiting-tone>
+      <step freq="384+416" level="-12" length="0.5"/>
+      <step length="0.5"/>
+      <step freq="400" level="-12" length="0.5"/>
+      <step length="2.5"/>
+    </waiting-tone>
+    <call-waiting-tone>
+      <step freq="384+416" level="-12" length="0.5"/>
+      <step length="0.0~4.0"/>
+      <step freq="400" level="-12" length="0.05"/>
+      <step length="0.45"/>
+      <step freq="400" level="-12" length="0.05"/>
+      <step length="3.45"/>
+      <step freq="400" level="-12" length="0.05"/>
+      <step length="0.45"/>
+      <step freq="400" level="-12" length="0.05"/>
+      <step length="3.45"/>
+    </call-waiting-tone>
+    <pay-tone>
+      <step freq="250" level="-12" length="1.0"/>
+    </pay-tone>
+    <pay-tone>
+      <step freq="250" level="-12" length="0.5"/>
+    </pay-tone>
+    <positive-indication-tone>
+      <step freq="400" level="-12" length="0.125"/>
+      <step length="0.125"/>
+      <step freq="400" level="-12" length="0.125"/>
+      <step length="0.625"/>
+    </positive-indication-tone>
+  </tone-set>
+  <tone-set country="Jordan" uncode="jo">
+    <dial-tone>
+      <step freq="380+460 || 400+440" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="380+460 || 400+440" level="-12" length="1.2"/>
+        <step length="4.8"/>
+      </step>
+    </ringback-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="383.33+416.67" level="-12" length="0.4"/>
+        <step length="0.2"/>
+        <step freq="383.33+416.67" level="-12" length="0.4"/>
+        <step length="2.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="0.75"/>
+        <step length="0.75"/>
+      </step>
+    </busy-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="380+460 || 400+440" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="380+460" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </congestion-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="400+440" level="-12" length="0.25"/>
+        <step length="0.25"/>
+      </step>
+    </congestion-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step freq="480+720 || 540+660" level="-12" length="2.5"/>
+        <step length="0.5"/>
+      </step>
+    </number-unobtainable-tone>
+    <waiting-tone>
+      <step freq="380+460 || 400+440" level="-12" length="0.5"/>
+      <step cycles="2">
+        <step length="0.3"/>
+        <step freq="380+460 || 400+440" level="-12" length="0.2"/>
+      </step>
+      <step length="3.0"/>
+    </waiting-tone>
+    <confirmation-tone>
+      <step cycles="20">
+        <step freq="400" level="-12" length="0.125"/>
+        <step length="0.125"/>
+      </step>
+      <step freq="400" level="-12"/>
+    </confirmation-tone>
+    <holding-tone>
+      <step freq="380+460 || 400+440" level="-12" length="0.5"/>
+      <step length="0.5"/>
+      <step freq="380+460 || 400+440" level="-12" length="0.5"/>
+      <step length="0.25"/>
+    </holding-tone>
+  </tone-set>
+  <tone-set country="Kenya" uncode="ke">
+    <dial-tone>
+      <step freq="425 || 50" level="-12"/>
+    </dial-tone>
+    <dial-tone type="second">
+      <step freq="425" level="-12" length="1.0"/>
+      <step length="0.2"/>
+      <step freq="425" level="-12" length="0.75"/>
+      <step length="0.75"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="1.0"/>
+        <step length="4.0"/>
+        <step freq="425" level="-12" length="1.0"/>
+        <step length="2.0"/>
+        <step freq="425" level="-12" length="1.0"/>
+        <step length="5.0"/>
+        <step freq="425" level="-12" length="0.4"/>
+        <step length="0.2"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.5"/>
+        <step length="0.5"/>
+        <step freq="425" level="-12" length="0.4"/>
+        <step length="0.2"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.2"/>
+        <step length="0.2"/>
+        <step freq="425" level="-12" length="0.4"/>
+        <step length="0.35"/>
+        <step freq="425" level="-12" length="0.225"/>
+        <step length="0.525"/>
+      </step>
+    </congestion-tone>
+    <warning-tone type="operator-intervening">
+      <step freq="425" level="-12" length="0.2"/>
+      <step length="0.2"/>
+      <step freq="425" level="-12" length="0.2"/>
+      <step length="1.4"/>
+    </warning-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step freq="950" level="-12" length="0.333"/>
+        <step freq="1400" level="-12" length="0.333"/>
+        <step freq="1800" level="-12" length="0.333"/>
+        <step length="1.0"/>
+      </step>
+    </number-unobtainable-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12"/>
+      </step>
+    </number-unobtainable-tone>
+    <waiting-tone>
+      <step freq="425" level="-12"/>
+    </waiting-tone>
+  </tone-set>
+  <tone-set country="Kiribati Republic" uncode="kg">
+    <dial-tone>
+      <step freq="425" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.4"/>
+        <step length="0.2"/>
+        <step freq="425" level="-12" length="0.4"/>
+        <step length="2.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </congestion-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.25"/>
+        <step length="0.25"/>
+      </step>
+    </number-unobtainable-tone>
+    <waiting-tone>
+      <step freq="425" level="-12" length="0.4"/>
+      <step length="0.2"/>
+      <step freq="425" level="-12" length="0.4"/>
+      <step length="2.0"/>
+    </waiting-tone>
+  </tone-set>
+  <tone-set country="Korea (Rep. of)" uncode="kr">
+    <dial-tone>
+      <step freq="350+440" level="-12"/>
+    </dial-tone>
+    <dial-tone type="recall">
+      <step freq="400 || 450" level="-12" length="1.0"/>
+      <step length="4.0"/>
+    </dial-tone>
+    <dial-tone type="recall">
+      <step freq="400 || 450" level="-12" length="1.0"/>
+      <step length="2.0"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="440+480" level="-12" length="1.0"/>
+        <step length="2.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="480+620" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="480+620" level="-12" length="0.3"/>
+        <step length="0.2"/>
+      </step>
+    </congestion-tone>
+    <warning-tone type="operator-intervening">
+      <step cycles="2">
+        <step freq="392" level="-12" length="0.5"/>
+        <step freq="494" level="-12" length="0.5"/>
+        <step freq="587" level="-12" length="1.5"/>
+      </step>
+    </warning-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step freq="450" level="-12" length="0.2"/>
+        <step length="0.1"/>
+        <step freq="450" level="-12" length="0.2"/>
+        <step length="1.5"/>
+      </step>
+    </number-unobtainable-tone>
+    <waiting-tone>
+      <step freq="350+440" level="-12" length="0.25"/>
+      <step length="0.25"/>
+      <step freq="350+440" level="-12" length="0.25"/>
+      <step length="3.25"/>
+    </waiting-tone>
+    <intercept-tone>
+      <step freq="350+440" level="-12" length="0.125"/>
+      <step length="0.25"/>
+      <step freq="350+440" level="-12" length="0.125"/>
+      <step length="1.5"/>
+    </intercept-tone>
+  </tone-set>
+  <tone-set country="Kuwait" uncode="kw">
+    <dial-tone>
+      <step freq="425" level="-12"/>
+    </dial-tone>
+    <dial-tone type="special">
+      <step freq="425" level="-12" length="0.4"/>
+      <step length="0.04"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="1.0"/>
+        <step length="4.0"/>
+      </step>
+    </ringback-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="1.0"/>
+        <step length="3.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </congestion-tone>
+    <warning-tone type="operator-intervening">
+      <step freq="425" level="-12" length="1.0"/>
+      <step length="1.5"/>
+      <step freq="425" level="-12" length="0.4"/>
+      <step length="1.5"/>
+    </warning-tone>
+    <intrusion-tone>
+      <step freq="425" level="-12" length="0.5"/>
+      <step length="0.2"/>
+      <step freq="425" level="-12" length="0.2"/>
+      <step length="0.2"/>
+    </intrusion-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12"/>
+      </step>
+    </number-unobtainable-tone>
+    <intercept-tone>
+      <step freq="950" level="-12" length="0.333"/>
+      <step freq="1400" level="-12" length="0.333"/>
+      <step freq="1800" level="-12" length="0.333"/>
+      <step length="1.0"/>
+    </intercept-tone>
+    <line-lockout-tone>
+      <step freq="425" level="-12" length="0.5"/>
+      <step length="0.5"/>
+    </line-lockout-tone>
+    <refusal-tone>
+      <step freq="425" level="-12" length="0.6"/>
+      <step length="0.6"/>
+    </refusal-tone>
+    <acceptance-tone>
+      <step freq="425" level="-12"/>
+    </acceptance-tone>
+  </tone-set>
+  <tone-set country="Kyrgyzstan" uncode="kg">
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.8"/>
+        <step length="3.2"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.4"/>
+        <step length="0.4"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.4"/>
+        <step length="0.4"/>
+      </step>
+    </congestion-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.4"/>
+        <step length="0.4"/>
+      </step>
+    </number-unobtainable-tone>
+  </tone-set>
+  <tone-set country="Lao P.D.R." uncode="la">
+    <dial-tone>
+      <step freq="425" level="-12"/>
+    </dial-tone>
+    <dial-tone type="special">
+      <step freq="425" level="-12" length="0.4"/>
+      <step length="0.04"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="1.0"/>
+        <step length="4.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="1.0"/>
+        <step length="1.0"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.375"/>
+        <step length="0.375"/>
+      </step>
+    </congestion-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="2.5"/>
+        <step length="0.5"/>
+      </step>
+    </number-unobtainable-tone>
+    <waiting-tone>
+      <step freq="425" level="-12" length="0.4"/>
+      <step length="0.4"/>
+    </waiting-tone>
+    <confirmation-tone>
+      <step freq="950" level="-12"/>
+    </confirmation-tone>
+  </tone-set>
+  <tone-set country="Lebanon" uncode="lb">
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="435" level="-12" length="1.2"/>
+        <step length="4.4"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="435" level="-12" length="0.4"/>
+        <step length="0.2"/>
+      </step>
+    </busy-tone>
+  </tone-set>
+  <tone-set country="Liberia" uncode="lr">
+    <dial-tone>
+      <step freq="425" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425 || 450" level="-12" length="1.0"/>
+        <step length="4.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.6"/>
+        <step length="0.2"/>
+        <step cycles="3">
+          <step freq="425" level="-12" length="0.2"/>
+          <step length="0.2"/>
+        </step>
+      </step>
+    </number-unobtainable-tone>
+  </tone-set>
+  <tone-set country="Lithuania" uncode="lt">
+    <dial-tone>
+      <step freq="425" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.8"/>
+        <step length="3.2"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.4"/>
+        <step length="0.4"/>
+      </step>
+    </busy-tone>
+    <waiting-tone>
+      <step freq="950" level="-12" length="0.333"/>
+      <step freq="1400" level="-12" length="0.333"/>
+      <step freq="1800" level="-12" length="0.333"/>
+      <step length="1.0"/>
+    </waiting-tone>
+  </tone-set>
+  <tone-set country="Luxembourg" uncode="lu">
+    <dial-tone>
+      <step freq="425 || 450" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="1.0"/>
+        <step length="4.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425 || 450" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="425 || 450" level="-12" length="0.25"/>
+        <step length="0.25"/>
+      </step>
+    </congestion-tone>
+    <special-information-tone>
+      <step freq="950" level="-12" length="0.333"/>
+      <step freq="1400" level="-12" length="0.333"/>
+      <step freq="1800" level="-12" length="0.333"/>
+      <step length="1.0"/>
+    </special-information-tone>
+    <warning-tone type="operator-intervening">
+      <step freq="425 || 450" level="-12" length="0.25"/>
+      <step length="0.25"/>
+      <step length="0.25"/>
+      <step length="1.25"/>
+    </warning-tone>
+    <intercept-tone>
+      <step freq="1190" level="-12" length="0.25"/>
+      <step freq="1280" level="-12" length="0.25"/>
+    </intercept-tone>
+    <payphone-recognition-tone>
+      <step freq="1100+1750" level="-12" length="0.2"/>
+      <step length="0.2"/>
+      <step freq="750+1450" level="-12" length="0.2"/>
+      <step length="2.0"/>
+    </payphone-recognition-tone>
+  </tone-set>
+  <tone-set country="Macau" uncode="mo">
+    <dial-tone>
+      <step freq="425" level="-12"/>
+    </dial-tone>
+    <dial-tone type="special">
+      <step freq="425" level="-12" length="1.8"/>
+      <step length="0.2"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="1.0"/>
+        <step length="4.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.25"/>
+        <step length="0.25"/>
+      </step>
+    </congestion-tone>
+    <waiting-tone>
+      <step freq="425" level="-12" length="0.2"/>
+      <step length="0.6"/>
+    </waiting-tone>
+    <intercept-tone>
+      <step freq="950" level="-12" length="0.333"/>
+      <step freq="1400" level="-12" length="0.333"/>
+      <step freq="1800" level="-12" length="0.333"/>
+      <step length="1.0"/>
+    </intercept-tone>
+    <payphone-recognition-tone>
+      <step freq="1100+1750" level="-12" length="0.2"/>
+      <step length="0.2"/>
+      <step freq="750+1450" level="-12" length="0.2"/>
+      <step length="2.0"/>
+    </payphone-recognition-tone>
+  </tone-set>
+  <tone-set country="Madagascar" uncode="mg">
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="25" level="-12" length="1.66"/>
+        <step length="3.33"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="450" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <waiting-tone>
+      <step freq="450" level="-12" length="0.125"/>
+      <step length="0.125"/>
+    </waiting-tone>
+  </tone-set>
+  <tone-set country="Malawi" uncode="mw">
+    <dial-tone>
+      <step freq="425" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="1.0"/>
+        <step length="4.0"/>
+      </step>
+    </ringback-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="400 || 133" level="-12" length="0.4"/>
+        <step length="0.2"/>
+        <step freq="400 || 133" level="-12" length="0.4"/>
+        <step length="2.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="0.75"/>
+        <step length="0.75"/>
+      </step>
+    </busy-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="525" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="0.4"/>
+        <step length="0.333"/>
+      </step>
+    </busy-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="2.5"/>
+        <step length="0.5"/>
+      </step>
+    </number-unobtainable-tone>
+  </tone-set>
+  <tone-set country="Malaysia" uncode="my">
+    <dial-tone>
+      <step freq="425" level="-12"/>
+    </dial-tone>
+    <dial-tone type="second">
+      <step freq="425" level="-12"/>
+    </dial-tone>
+    <dial-tone type="special">
+      <step freq="425" level="-12" length="0.25"/>
+      <step length="0.25"/>
+      <step freq="425" level="-12" length="0.25"/>
+      <step length="0.25"/>
+      <step freq="425" level="-12" length="2.0"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.4"/>
+        <step length="0.2"/>
+        <step freq="425" level="-12" length="0.4"/>
+        <step length="2.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.25"/>
+        <step length="0.25"/>
+      </step>
+    </congestion-tone>
+    <special-information-tone>
+      <step freq="900" level="-12" length="1.0"/>
+      <step freq="1400" level="-12" length="1.0"/>
+      <step freq="1800" level="-12" length="1.0"/>
+      <step length="1.0"/>
+    </special-information-tone>
+    <warning-tone type="operator-intervening">
+      <step freq="425" level="-12" length="0.5"/>
+      <step length="0.2"/>
+      <step freq="425" level="-12" length="0.2"/>
+      <step length="0.2"/>
+    </warning-tone>
+    <warning-tone type="end-of-period">
+      <step freq="900" level="-12" length="0.1"/>
+      <step length="0.1"/>
+      <step freq="900" level="-12" length="0.1"/>
+      <step length="0.1"/>
+      <step freq="900" level="-12" length="0.1"/>
+    </warning-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="2.5"/>
+        <step length="0.5"/>
+      </step>
+    </number-unobtainable-tone>
+    <waiting-tone>
+      <step freq="425" level="-12" length="1.0"/>
+      <step length="10.0"/>
+      <step freq="425" level="-12" length="0.5"/>
+      <step length="0.25"/>
+      <step freq="425" level="-12" length="0.5"/>
+      <step length="10.0"/>
+      <step freq="425" level="-12" length="0.5"/>
+      <step length="0.25"/>
+    </waiting-tone>
+    <line-lockout-tone>
+      <step freq="425" level="-12" length="0.5"/>
+      <step length="0.5"/>
+    </line-lockout-tone>
+  </tone-set>
+  <tone-set country="Maldives" uncode="mv">
+    <dial-tone>
+      <step freq="350+450" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="0.4"/>
+        <step length="0.2"/>
+        <step freq="400" level="-12" length="0.4"/>
+        <step length="2.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="0.25"/>
+        <step length="0.25"/>
+      </step>
+    </congestion-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12"/>
+      </step>
+    </number-unobtainable-tone>
+    <waiting-tone>
+      <step freq="400" level="-12" length="1.0"/>
+      <step length="10.0"/>
+    </waiting-tone>
+  </tone-set>
+  <tone-set country="Mali" uncode="ml">
+    <dial-tone>
+      <step freq="425" level="-12"/>
+    </dial-tone>
+    <dial-tone type="second">
+      <step freq="330+425" level="-12"/>
+    </dial-tone>
+    <dial-tone type="special">
+      <step freq="425" level="-12" length="0.40"/>
+      <step length="0.04"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="1.7"/>
+        <step length="3.3"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <special-information-tone>
+      <step freq="900" level="-12" length="0.33"/>
+      <step freq="1400" level="-12" length="0.33"/>
+      <step freq="1800" level="-12" length="0.33"/>
+      <step length="1.0"/>
+    </special-information-tone>
+    <route-tone>
+      <step freq="425" level="-12" length="0.05"/>
+      <step length="0.04"/>
+    </route-tone>
+    <acceptance-tone>
+      <step freq="950" level="-12"/>
+    </acceptance-tone>
+  </tone-set>
+  <tone-set country="Malta" uncode="mt">
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="400+450 || 375+425 || 383.33+416.67" level="-12" length="0.4"/>
+        <step length="0.2"/>
+        <step freq="400+450 || 375+425 || 383.33+416.67" level="-12" length="0.4"/>
+        <step length="2.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="0.375"/>
+        <step length="0.375"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="0.4"/>
+        <step length="0.35"/>
+        <step freq="400" level="-12" length="0.225"/>
+        <step length="0.525"/>
+      </step>
+    </congestion-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12"/>
+      </step>
+    </number-unobtainable-tone>
+    <pay-tone>
+      <step freq="400" level="-12" length="0.15"/>
+      <step length="0.15"/>
+    </pay-tone>
+  </tone-set>
+  <tone-set country="Martinique (French Dep.)" uncode="mq">
+    <dial-tone>
+      <step freq="440 || 330+440" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="440" level="-12" length="1.5"/>
+        <step length="3.5"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="440" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <special-information-tone>
+      <step freq="950" level="-12" length="0.3"/>
+      <step length="0.03"/>
+      <step freq="1400" level="-12" length="0.3"/>
+      <step length="0.03"/>
+      <step freq="1800" level="-12" length="0.3"/>
+      <step length="1.0"/>
+    </special-information-tone>
+    <route-tone>
+      <step freq="440" level="-12" length="0.05"/>
+      <step length="0.05"/>
+    </route-tone>
+  </tone-set>
+  <tone-set country="Mauritania" uncode="mr">
+    <dial-tone>
+      <step freq="425" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="1.5"/>
+        <step length="3.5"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <route-tone>
+      <step freq="425" level="-12" length="0.05"/>
+      <step length="0.05"/>
+    </route-tone>
+  </tone-set>
+  <tone-set country="Mauritius" uncode="mu">
+    <dial-tone>
+      <step freq="425" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="1.0"/>
+        <step length="4.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.75"/>
+        <step length="0.75"/>
+      </step>
+    </busy-tone>
+    <route-tone>
+      <step freq="425" level="-12" length="0.05"/>
+      <step length="0.05"/>
+    </route-tone>
+  </tone-set>
+  <tone-set country="Mayotte" uncode="yt">
+    <dial-tone>
+      <step freq="440 || 330+440" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="440" level="-12" length="1.5"/>
+        <step length="3.5"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="440" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <special-information-tone>
+      <step freq="950" level="-12" length="0.3"/>
+      <step length="0.03"/>
+      <step freq="1400" level="-12" length="0.3"/>
+      <step length="0.03"/>
+      <step freq="1800" level="-12" length="0.3"/>
+      <step length="1.0"/>
+    </special-information-tone>
+    <route-tone>
+      <step freq="440" level="-12" length="0.05"/>
+      <step length="0.05"/>
+    </route-tone>
+  </tone-set>
+  <tone-set country="Mexico" uncode="mx">
+    <dial-tone>
+      <step freq="425" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="1.0"/>
+        <step length="4.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.25"/>
+        <step length="0.25"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.25"/>
+        <step length="0.25"/>
+      </step>
+    </congestion-tone>
+    <warning-tone type="operator-intervening">
+      <step freq="425" level="-12" length="0.5"/>
+      <step length="0.17"/>
+      <step freq="425" level="-12" length="0.17"/>
+      <step length="0.17"/>
+    </warning-tone>
+  </tone-set>
+  <tone-set country="Moldova" uncode="md">
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.8"/>
+        <step length="3.2"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.4"/>
+        <step length="0.4"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.4"/>
+        <step length="0.4"/>
+      </step>
+    </congestion-tone>
+  </tone-set>
+  <tone-set country="Montserrat" uncode="ms">
+    <dial-tone>
+      <step freq="350+440" level="-12"/>
+    </dial-tone>
+    <dial-tone type="recall">
+      <step cycles="3">
+        <step freq="350+440" level="-12" length="0.1"/>
+        <step length="0.1"/>
+      </step>
+      <step freq="350+440" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="440+480" level="-12" length="2.0"/>
+        <step length="4.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="480+620" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="480+620" level="-12" length="0.25"/>
+        <step length="0.25"/>
+      </step>
+    </congestion-tone>
+    <special-information-tone>
+      <step freq="950" level="-12" length="0.333"/>
+      <step freq="1400" level="-12" length="0.333"/>
+      <step freq="1800" level="-12" length="0.333"/>
+    </special-information-tone>
+    <warning-tone type="operator-intervening">
+      <step freq="450" level="-12" length="0.2"/>
+      <step length="0.2"/>
+      <step freq="450" level="-12" length="0.65"/>
+      <step length="0.2"/>
+    </warning-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step freq="480+620" level="-12" length="0.25"/>
+        <step length="0.25"/>
+      </step>
+    </number-unobtainable-tone>
+    <waiting-tone>
+      <step cycles="1">
+        <step freq="440" level="-12" length="0.5"/>
+        <step length="10.0"/>
+        <step freq="440" level="-12" length="0.5"/>
+      </step>
+    </waiting-tone>
+    <payphone-recognition-tone>
+      <step freq="1600" level="-12" length="0.2"/>
+      <step length="0.2"/>
+      <step freq="1143" level="-12" length="0.2"/>
+      <step length="2.0"/>
+    </payphone-recognition-tone>
+  </tone-set>
+  <tone-set country="Morocco" uncode="ma">
+    <dial-tone>
+      <step freq="425" level="-12"/>
+    </dial-tone>
+    <dial-tone type="second">
+      <step freq="660" level="-12" length="0.5"/>
+      <step freq="440" level="-12" length="0.5"/>
+      <step freq="590" level="-12" length="0.5"/>
+      <step freq="740" level="-12" length="0.3"/>
+      <step length="0.2"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="1.66"/>
+        <step length="3.33"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <route-tone>
+      <step freq="425" level="-12" length="0.05"/>
+      <step length="0.05"/>
+    </route-tone>
+  </tone-set>
+  <tone-set country="Mozambique" uncode="mz">
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="1.0"/>
+        <step length="5.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="0.2"/>
+        <step length="0.2"/>
+      </step>
+    </number-unobtainable-tone>
+  </tone-set>
+  <tone-set country="Namibia" uncode="na">
+    <dial-tone>
+      <step freq="407+473" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="407+473" level="-12" length="0.4"/>
+        <step length="0.2"/>
+        <step freq="407+473" level="-12" length="0.4"/>
+        <step length="2.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="0.75"/>
+        <step length="0.75"/>
+      </step>
+    </busy-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="0.25"/>
+        <step length="0.25"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="0.25"/>
+        <step length="0.25"/>
+      </step>
+    </congestion-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="2.5"/>
+        <step length="0.5"/>
+      </step>
+    </number-unobtainable-tone>
+    <payphone-recognition-tone>
+      <step freq="770" level="-12" length="0.125"/>
+      <step freq="1209" level="-12" length="1.5"/>
+    </payphone-recognition-tone>
+    <pay-tone>
+      <step freq="400" level="-12" length="0.125"/>
+      <step length="0.125"/>
+    </pay-tone>
+  </tone-set>
+  <tone-set country="Nauru" uncode="nr">
+    <dial-tone>
+      <step freq="400" level="-12"/>
+    </dial-tone>
+    <dial-tone domain="PABX">
+      <step freq="33" level="-12"/>
+    </dial-tone>
+    <dial-tone type="recall" domain="PABX">
+      <step freq="33" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="1.0"/>
+        <step length="4.0"/>
+      </step>
+    </ringback-tone>
+    <ringback-tone domain="PABX">
+      <step cycles="endless">
+        <step freq="400+450" level="-12" length="0.4"/>
+        <step length="0.2"/>
+        <step freq="400+450" level="-12" length="0.4"/>
+        <step length="2.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <busy-tone domain="PABX">
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.375"/>
+        <step length="0.375"/>
+      </step>
+    </busy-tone>
+    <congestion-tone domain="PABX">
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.375"/>
+        <step length="0.375"/>
+      </step>
+    </congestion-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="0.75"/>
+        <step length="0.25"/>
+        <step freq="400" level="-12" length="0.25"/>
+        <step length="0.25"/>
+      </step>
+    </number-unobtainable-tone>
+  </tone-set>
+  <tone-set country="Nepal" uncode="np">
+    <dial-tone>
+      <step freq="425" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="1.1"/>
+        <step length="3.1"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.25"/>
+        <step length="0.25"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.175"/>
+        <step length="0.175"/>
+      </step>
+    </congestion-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.25"/>
+        <step length="0.5"/>
+      </step>
+    </number-unobtainable-tone>
+  </tone-set>
+  <tone-set country="Netherlands" uncode="nl">
+    <dial-tone>
+      <step freq="425 || 450" level="-12"/>
+    </dial-tone>
+    <dial-tone type="special">
+      <step freq="425 || 450" level="-12" length="0.5"/>
+      <step length="0.05"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425 || 450" level="-12" length="1.0"/>
+        <step length="4.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425 || 450" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425 || 450" level="-12" length="0.25"/>
+        <step length="0.25"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="425 || 450" level="-12" length="0.25"/>
+        <step length="0.25"/>
+      </step>
+    </congestion-tone>
+    <special-information-tone>
+      <step freq="950" level="-12" length="0.333"/>
+      <step freq="1400" level="-12" length="0.333"/>
+      <step freq="1800" level="-12" length="0.333"/>
+      <step length="1.0"/>
+    </special-information-tone>
+    <waiting-tone>
+      <step freq="425 || 450" level="-12" length="0.5"/>
+      <step length="9.5"/>
+    </waiting-tone>
+    <pay-tone>
+      <step freq="770 || 850 || 950" level="-12"/>
+    </pay-tone>
+    <positive-indication-tone>
+      <step freq="425 || 450" level="-12"/>
+    </positive-indication-tone>
+    <negative-indication-tone>
+      <step freq="425 || 450" level="-12" length="0.0625"/>
+      <step length="0.0625"/>
+    </negative-indication-tone>
+  </tone-set>
+  <tone-set country="New Caledonia" uncode="nc">
+    <dial-tone>
+      <step freq="440 || 330+440" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="440" level="-12" length="1.5"/>
+        <step length="3.5"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="440" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <special-information-tone>
+      <step freq="950" level="-12" length="0.3"/>
+      <step length="0.03"/>
+      <step freq="1400" level="-12" length="0.3"/>
+      <step length="0.03"/>
+      <step freq="1800" level="-12" length="0.3"/>
+      <step length="1.0"/>
+    </special-information-tone>
+    <route-tone>
+      <step freq="440" level="-12" length="0.05"/>
+      <step length="0.05"/>
+    </route-tone>
+  </tone-set>
+  <tone-set country="New Zealand" uncode="nz">
+    <dial-tone>
+      <step freq="400" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="400+450" level="-12" length="0.4"/>
+        <step length="0.2"/>
+        <step freq="400+450" level="-12" length="0.4"/>
+        <step length="2.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="900" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </congestion-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="0.25"/>
+        <step length="0.25"/>
+      </step>
+    </congestion-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step cycles="3">
+          <step freq="400" level="-12" length="0.75"/>
+          <step length="0.1"/>
+        </step>
+        <step freq="400" level="-12" length="0.75"/>
+        <step length="0.4"/>
+      </step>
+    </number-unobtainable-tone>
+    <waiting-tone>
+      <step freq="400+450" level="-12" length="0.5"/>
+    </waiting-tone>
+    <waiting-tone>
+      <step freq="400" level="-12" length="0.25"/>
+      <step length="0.25"/>
+      <step freq="400" level="-12" length="0.25"/>
+      <step length="3.25"/>
+    </waiting-tone>
+    <waiting-tone>
+      <step cycles="3">
+        <step freq="523" level="-12" length="0.2"/>
+        <step length="3.0"/>
+      </step>
+      <step freq="659" level="-12" length="0.2"/>
+    </waiting-tone>
+    <function-acknowledge-tone>
+      <step freq="440+450" level="-12" length="2.0"/>
+      <step length="0.4"/>
+      <step freq="440+450" level="-12" length="0.2"/>
+      <step length="0.4"/>
+    </function-acknowledge-tone>
+    <payphone-recognition-tone>
+      <step freq="1100+1750" level="-12" length="0.2"/>
+      <step length="0.2"/>
+      <step freq="750+1450" level="-12" length="0.2"/>
+      <step length="2.0"/>
+    </payphone-recognition-tone>
+    <holding-tone>
+      <step freq="400" level="-12" length="0.5"/>
+      <step length="0.5"/>
+      <step freq="400+450" level="-12" length="0.5"/>
+      <step length="0.25"/>
+    </holding-tone>
+  </tone-set>
+  <tone-set country="Niger" uncode="ne">
+    <dial-tone>
+      <step freq="440" level="-12"/>
+    </dial-tone>
+    <dial-tone type="second">
+      <step freq="330 || 440" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="440" level="-12" length="1.7"/>
+        <step length="3.3"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="440" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="440" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </congestion-tone>
+    <route-tone>
+      <step freq="440" level="-12" length="0.05"/>
+      <step length="0.05"/>
+    </route-tone>
+  </tone-set>
+  <tone-set country="Nigeria" uncode="ng">
+    <dial-tone>
+      <step freq="400 || 425 || 450" level="-12"/>
+    </dial-tone>
+    <dial-tone type="second">
+      <step freq="425" level="-12"/>
+    </dial-tone>
+    <dial-tone type="recall">
+      <step freq="400 || 425" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="400 || 425 || 450" level="-12" length="2.0"/>
+        <step length="4.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="0.25"/>
+        <step length="0.25"/>
+      </step>
+    </congestion-tone>
+    <special-information-tone>
+      <step freq="400 || 33" level="-12" length="0.2"/>
+      <step length="1.5"/>
+    </special-information-tone>
+    <warning-tone type="operator-intervening">
+      <step freq="400" level="-12" length="0.15"/>
+      <step length="0.25"/>
+      <step freq="400" level="-12" length="0.15"/>
+      <step length="1.45"/>
+    </warning-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step freq="450" level="-12" length="5.5"/>
+        <step length="0.5"/>
+      </step>
+    </number-unobtainable-tone>
+    <waiting-tone>
+      <step freq="425" level="-12" length="2.0"/>
+      <step length="4.0"/>
+    </waiting-tone>
+    <holding-tone>
+      <step freq="950" level="-12" length="0.65"/>
+      <step length="0.325"/>
+      <step freq="950" level="-12" length="0.325"/>
+      <step length="1.3"/>
+      <step freq="1400" level="-12" length="2.6"/>
+    </holding-tone>
+  </tone-set>
+  <tone-set country="Norway" uncode="no">
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="1.0"/>
+        <step length="4.0"/>
+      </step>
+    </ringback-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="400 || 450" level="-12" length="1.0"/>
+        <step length="3.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="400 || 450" level="-12" length="0.2"/>
+        <step length="0.4"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.25"/>
+        <step length="0.25"/>
+      </step>
+    </congestion-tone>
+    <special-information-tone>
+      <step freq="950" level="-12" length="0.333"/>
+      <step freq="1400" level="-12" length="0.333"/>
+      <step freq="1800" level="-12" length="0.333"/>
+      <step length="1.0"/>
+    </special-information-tone>
+    <payphone-recognition-tone>
+      <step cycles="4">
+        <step freq="1477" level="-12" length="0.2"/>
+        <step length="0.2"/>
+        <step freq="941" level="-12" length="0.2"/>
+        <step length="2.0"/>
+      </step>
+    </payphone-recognition-tone>
+  </tone-set>
+  <tone-set country="Oman" uncode="om">
+    <dial-tone>
+      <step freq="425" level="-12"/>
+    </dial-tone>
+    <dial-tone type="second">
+      <step freq="425" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.4"/>
+        <step length="0.2"/>
+        <step freq="425" level="-12" length="0.4"/>
+        <step length="2.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </congestion-tone>
+    <warning-tone type="operator-intervening">
+      <step freq="425" level="-12" length="0.5"/>
+      <step length="0.17"/>
+      <step freq="425" level="-12" length="0.17"/>
+      <step length="0.17"/>
+    </warning-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.2"/>
+        <step length="0.3"/>
+      </step>
+    </number-unobtainable-tone>
+    <waiting-tone>
+      <step freq="425" level="-12" length="0.3"/>
+      <step length="1.0"/>
+    </waiting-tone>
+    <line-lockout-tone>
+      <step freq="425" level="-12" length="0.48"/>
+      <step length="0.48"/>
+    </line-lockout-tone>
+  </tone-set>
+  <tone-set country="Pakistan" uncode="pk">
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="450" level="-12" length="1.0"/>
+        <step length="4.0"/>
+      </step>
+    </ringback-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="383.33+416.67" level="-12" length="0.4"/>
+        <step length="0.2"/>
+        <step freq="383.33+416.67" level="-12" length="0.4"/>
+        <step length="2.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="0.75"/>
+        <step length="0.75"/>
+      </step>
+    </busy-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="450" level="-12" length="0.4"/>
+        <step length="0.675"/>
+        <step freq="450" level="-12" length="0.13"/>
+        <step length="0.17"/>
+      </step>
+    </busy-tone>
+  </tone-set>
+  <tone-set country="Panama" uncode="pa">
+    <dial-tone>
+      <step freq="425" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="1.20"/>
+        <step length="4.65"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.32"/>
+        <step length="4.65"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.32"/>
+        <step length="4.65"/>
+      </step>
+    </congestion-tone>
+    <special-information-tone>
+      <step freq="425" level="-12" length="0.4"/>
+      <step length="0.04"/>
+    </special-information-tone>
+    <warning-tone type="operator-intervening">
+      <step freq="425" level="-12" length="0.18"/>
+      <step length="0.18"/>
+      <step freq="425" level="-12" length="0.5"/>
+      <step length="0.18"/>
+    </warning-tone>
+    <number-unobtainable-tone domain="digital">
+      <step cycles="endless">
+        <step recorded-announcement="true"/>
+      </step>
+    </number-unobtainable-tone>
+  </tone-set>
+  <tone-set country="Papua New Guinea" uncode="pg">
+    <dial-tone>
+      <step freq="425 || 375+425 || 350+440" level="-12"/>
+    </dial-tone>
+    <dial-tone type="special">
+      <step freq="425" level="-12" length="0.4"/>
+      <step length="0.04"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425 || 375+425 || 383.33+416.67 || 400+450 || 390+430" level="-12" length="0.4"/>
+        <step length="0.2"/>
+        <step freq="425 || 375+425 || 383.33+416.67 || 400+450 || 390+430" level="-12" length="0.4"/>
+        <step length="2.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425 || 400" level="-12" length="0.375"/>
+        <step length="0.375"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="425 || 400" level="-12" length="0.375"/>
+        <step length="0.375"/>
+        <step freq="425 || 400" level="-22" length="0.375"/>
+        <step length="0.375"/>
+      </step>
+    </congestion-tone>
+    <special-information-tone>
+      <step freq="950" level="-12" length="0.33"/>
+      <step freq="1400" level="-12" length="0.33"/>
+      <step freq="1800" level="-12" length="0.33"/>
+      <step length="1.0"/>
+    </special-information-tone>
+    <warning-tone type="operator-intervening">
+      <step freq="1400" level="-12" length="0.5"/>
+      <step length="11.5"/>
+    </warning-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step freq="425 || 400" level="-12" length="2.5"/>
+        <step length="0.5"/>
+      </step>
+    </number-unobtainable-tone>
+    <waiting-tone>
+      <step freq="425" level="-12" length="0.04"/>
+      <step length="10.0"/>
+      <step freq="425" level="-12" length="0.04"/>
+      <step length="20.0"/>
+      <step freq="425" level="-12" length="0.04"/>
+      <step length="20.0"/>
+    </waiting-tone>
+    <payphone-recognition-tone>
+      <step cycles="4">
+        <step freq="1209" level="-12" length="0.2"/>
+        <step length="0.2"/>
+        <step freq="852" level="-12" length="0.2"/>
+        <step length="2.0"/>
+      </step>
+    </payphone-recognition-tone>
+    <payphone-recognition-tone>
+      <step cycles="2">
+        <step freq="1336" level="-12" length="0.2"/>
+        <step length="0.2"/>
+        <step freq="941" level="-12" length="0.2"/>
+        <step length="2.0"/>
+      </step>
+    </payphone-recognition-tone>
+    <payphone-recognition-tone>
+      <step cycles="2">
+        <step freq="900" level="-12" length="0.075"/>
+        <step length="0.15"/>
+        <step freq="900" level="-12" length="0.075"/>
+        <step length="2.7"/>
+      </step>
+    </payphone-recognition-tone>
+  </tone-set>
+  <tone-set country="Paraguay" uncode="py">
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="1.0"/>
+        <step length="4.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="1.0"/>
+        <step length="1.0"/>
+      </step>
+    </busy-tone>
+    <warning-tone type="operator-intervening">
+      <step freq="425" level="-12" length="0.15"/>
+      <step length="0.25"/>
+      <step freq="425" level="-12" length="0.15"/>
+      <step length="1.45"/>
+    </warning-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="2.0"/>
+        <step length="0.5"/>
+      </step>
+    </number-unobtainable-tone>
+    <waiting-tone>
+      <step freq="950" level="-12" length="0.65"/>
+      <step length="0.325"/>
+      <step freq="950" level="-12" length="0.125"/>
+      <step length="1.3"/>
+      <step freq="1400" level="-12" length="2.6"/>
+    </waiting-tone>
+    <positive-indication-tone>
+      <step freq="950" level="-12" length="0.333"/>
+      <step freq="1400" level="-12" length="0.333"/>
+      <step freq="1800" level="-12" length="0.333"/>
+      <step length="1.0"/>
+    </positive-indication-tone>
+  </tone-set>
+  <tone-set country="Philippines" uncode="ph">
+    <dial-tone>
+      <step freq="480+720 || 425" level="-12"/>
+    </dial-tone>
+    <dial-tone>
+      <step freq="400" level="-12" length="0.2"/>
+      <step length="0.2"/>
+    </dial-tone>
+    <dial-tone type="special">
+      <step freq="400+425" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="450" level="-12" length="1.0"/>
+        <step length="10.0"/>
+      </step>
+    </ringback-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425+480" level="-12" length="1.0"/>
+        <step length="4.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="480+720 || 425" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="450" level="-12" length="0.4"/>
+        <step length="0.2"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="480+720 || 480+620" level="-12" length="0.25"/>
+        <step length="0.25"/>
+      </step>
+    </congestion-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step freq="480+720" level="-12" length="0.25"/>
+        <step length="0.25"/>
+      </step>
+    </number-unobtainable-tone>
+  </tone-set>
+  <tone-set country="Poland" uncode="pl">
+    <dial-tone>
+      <step freq="425" level="-12"/>
+    </dial-tone>
+    <dial-tone type="second">
+      <step freq="350+425" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="1.0"/>
+        <step length="4.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </congestion-tone>
+    <special-information-tone>
+      <step freq="950" level="-12" length="0.333"/>
+      <step freq="1400" level="-12" length="0.333"/>
+      <step freq="1800" level="-12" length="0.333"/>
+      <step length="1.0"/>
+    </special-information-tone>
+    <waiting-tone>
+      <step freq="425" level="-12" length="0.15"/>
+      <step length="0.15"/>
+      <step freq="425" level="-12" length="0.15"/>
+      <step length="4.0"/>
+    </waiting-tone>
+    <route-tone>
+      <step freq="425" level="-12" length="0.05"/>
+      <step length="0.05"/>
+    </route-tone>
+  </tone-set>
+  <tone-set country="Portugal" uncode="pt">
+    <dial-tone>
+      <step freq="425" level="-12"/>
+    </dial-tone>
+    <dial-tone domain="analogue">
+      <step freq="400" level="-12"/>
+    </dial-tone>
+    <dial-tone type="special">
+      <step freq="425" level="-12" length="1.0"/>
+      <step length="0.2"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="1.0"/>
+        <step length="5.0"/>
+      </step>
+    </ringback-tone>
+    <ringback-tone domain="analogue">
+      <step cycles="endless">
+        <step freq="400" level="-12" length="1.0"/>
+        <step length="5.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <busy-tone domain="analogue">
+      <step cycles="endless">
+        <step freq="400" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.2"/>
+        <step length="0.2"/>
+      </step>
+    </congestion-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.2"/>
+        <step length="0.2"/>
+      </step>
+    </number-unobtainable-tone>
+    <number-unobtainable-tone domain="analogue">
+      <step cycles="endless">
+        <step freq="400" level="-12" length="0.2"/>
+        <step length="0.2"/>
+      </step>
+    </number-unobtainable-tone>
+    <waiting-tone>
+      <step freq="425" level="-12" length="0.2"/>
+      <step length="0.2"/>
+      <step freq="425" level="-12" length="0.2"/>
+      <step length="5.0"/>
+    </waiting-tone>
+    <payphone-recognition-tone>
+      <step freq="1477" level="-12" length="0.2"/>
+      <step length="0.2"/>
+      <step freq="941" level="-12" length="0.2"/>
+      <step length="2.0"/>
+    </payphone-recognition-tone>
+  </tone-set>
+  <tone-set country="Puerto Rico" uncode="pr">
+    <payphone-recognition-tone>
+      <step freq="1100+1750" level="-12" length="0.2"/>
+      <step length="0.2"/>
+      <step freq="750+1450" level="-12" length="0.2"/>
+      <step length="2.0"/>
+    </payphone-recognition-tone>
+  </tone-set>
+  <tone-set country="Qatar" uncode="qa">
+    <dial-tone>
+      <step freq="350+440" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="400+450" level="-12" length="0.4"/>
+        <step length="0.2"/>
+        <step freq="400+450" level="-12" length="0.4"/>
+        <step length="2.2"/>
+      </step>
+    </ringback-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="400+450" level="-12" length="0.4"/>
+        <step length="0.2"/>
+        <step freq="400+450" level="-12" length="0.4"/>
+        <step length="2.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="400 || 450" level="-12" length="0.8"/>
+        <step length="0.8"/>
+      </step>
+    </busy-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="0.375"/>
+        <step length="0.375"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="0.4"/>
+        <step length="0.35"/>
+        <step freq="400" level="-12" length="0.225"/>
+        <step length="0.525"/>
+      </step>
+    </congestion-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12"/>
+      </step>
+    </number-unobtainable-tone>
+  </tone-set>
+  <tone-set country="Reunion (French Dep.)" uncode="re">
+    <dial-tone>
+      <step freq="440 || 330+440" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="440" level="-12" length="1.5"/>
+        <step length="3.5"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="440" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <special-information-tone>
+      <step freq="950" level="-12" length="0.3"/>
+      <step length="0.03"/>
+      <step freq="1400" level="-12" length="0.3"/>
+      <step length="0.03"/>
+      <step freq="1800" level="-12" length="0.3"/>
+      <step length="1.0"/>
+    </special-information-tone>
+    <route-tone>
+      <step freq="440" level="-12" length="0.05"/>
+      <step length="0.05"/>
+    </route-tone>
+  </tone-set>
+  <tone-set country="Romania" uncode="ro">
+    <dial-tone>
+      <step freq="400 || 450" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="384+416 || 425+475" level="-12" length="2.0"/>
+        <step length="4.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="133 || 450" level="-12" length="0.15"/>
+        <step length="0.20"/>
+      </step>
+    </busy-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step cycles="6">
+          <step freq="400 || 450" level="-12" length="0.033"/>
+          <step length="0.03"/>
+        </step>
+        <step length="0.1"/>
+        <step freq="400 || 450" level="-12" length="0.03"/>
+      </step>
+    </number-unobtainable-tone>
+  </tone-set>
+  <tone-set country="Russia" uncode="ru">
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.8"/>
+        <step length="3.2"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.4"/>
+        <step length="0.4"/>
+      </step>
+    </busy-tone>
+    <waiting-tone>
+      <step cycles="1">
+        <step freq="950" level="-12" length="0.333"/>
+        <step freq="1400" level="-12" length="0.333"/>
+        <step freq="1800" level="-12" length="0.333"/>
+      </step>
+      <step length="1.0"/>
+    </waiting-tone>
+  </tone-set>
+  <tone-set country="Rwandese Republic" uncode="rw">
+    <dial-tone>
+      <step freq="440" level="-12"/>
+    </dial-tone>
+    <dial-tone type="second">
+      <step freq="340" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="440" level="-12" length="1.7"/>
+        <step length="3.3"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="440" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <special-information-tone>
+      <step cycles="1">
+        <step freq="950" level="-12" length="0.333"/>
+        <step freq="1400" level="-12" length="0.333"/>
+        <step freq="1800" level="-12" length="0.333"/>
+      </step>
+      <step length="1.0"/>
+    </special-information-tone>
+    <route-tone>
+      <step freq="440" level="-12" length="0.05"/>
+      <step length="0.05"/>
+    </route-tone>
+  </tone-set>
+  <tone-set country="St. Helena" uncode="sh">
+    <dial-tone>
+      <step freq="350+400" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="400+450" level="-12" length="0.4"/>
+        <step length="0.2"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="0.375"/>
+        <step length="0.375"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="0.4"/>
+        <step length="0.35"/>
+      </step>
+    </congestion-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12"/>
+      </step>
+    </number-unobtainable-tone>
+    <pay-tone>
+      <step freq="400" level="-12" length="0.125"/>
+      <step length="0.125"/>
+    </pay-tone>
+  </tone-set>
+  <tone-set country="St. Kitts and Nevis" uncode="kn">
+    <dial-tone>
+      <step freq="350+440" level="-12"/>
+    </dial-tone>
+    <dial-tone type="recall">
+      <step cycles="1">
+        <step freq="350+440" level="-12" length="0.1"/>
+        <step length="0.1"/>
+      </step>
+      <step freq="350+440" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="440+480" level="-12" length="2.0"/>
+        <step length="4.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="480+620" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="480+620" level="-12" length="0.25"/>
+        <step length="0.25"/>
+      </step>
+    </congestion-tone>
+    <special-information-tone>
+      <step freq="950" level="-12" length="0.333"/>
+      <step freq="1400" level="-12" length="0.333"/>
+      <step freq="1800" level="-12" length="0.333"/>
+    </special-information-tone>
+    <warning-tone type="operator-intervening">
+      <step freq="450" level="-12" length="0.2"/>
+      <step length="0.2"/>
+      <step length="0.65"/>
+      <step length="0.2"/>
+    </warning-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step freq="480+620" level="-12" length="0.25"/>
+        <step length="0.25"/>
+      </step>
+    </number-unobtainable-tone>
+    <waiting-tone>
+      <step freq="440" level="-12" length="0.5"/>
+      <step length="10.0"/>
+      <step freq="440" level="-12" length="0.5"/>
+    </waiting-tone>
+    <payphone-recognition-tone>
+      <step freq="1600" level="-12" length="0.2"/>
+      <step length="0.2"/>
+      <step freq="1143" level="-12" length="0.2"/>
+      <step length="2.0"/>
+    </payphone-recognition-tone>
+  </tone-set>
+  <tone-set country="St. Lucia" uncode="lc">
+    <dial-tone>
+      <step freq="425" level="-12"/>
+    </dial-tone>
+    <dial-tone type="second">
+      <step freq="425" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.375"/>
+        <step length="0.25"/>
+        <step freq="425" level="-12" length="0.375"/>
+        <step length="2.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.25"/>
+        <step length="0.25"/>
+      </step>
+    </congestion-tone>
+    <special-information-tone>
+      <step freq="425+330" level="-12"/>
+    </special-information-tone>
+    <warning-tone type="operator-intervening">
+      <step freq="425" level="-12" length="0.1"/>
+      <step length="4.9"/>
+    </warning-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step recorded-announcement="true"/>
+      </step>
+    </number-unobtainable-tone>
+    <waiting-tone>
+      <step freq="425" level="-12" length="0.2"/>
+      <step length="0.2"/>
+    </waiting-tone>
+    <route-tone>
+      <step freq="425" level="-12" length="0.05"/>
+      <step length="0.05"/>
+    </route-tone>
+  </tone-set>
+  <tone-set country="St. Pierre and Miquelon" uncode="pm">
+    <dial-tone>
+      <step freq="440 || 330+440" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="440" level="-12" length="1.5"/>
+        <step length="3.5"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="440" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <special-information-tone>
+      <step freq="950" level="-12" length="0.3"/>
+      <step length="0.03"/>
+      <step freq="1400" level="-12" length="0.3"/>
+      <step length="0.03"/>
+      <step freq="1800" level="-12" length="0.3"/>
+      <step length="1.0"/>
+    </special-information-tone>
+    <route-tone>
+      <step freq="440" level="-12" length="0.05"/>
+      <step length="0.05"/>
+    </route-tone>
+  </tone-set>
+  <tone-set country="St. Vincent and the Grenadines" uncode="vc">
+    <dial-tone>
+      <step freq="350+450" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="400+450" level="-12" length="0.2"/>
+        <step length="0.4"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="0.375"/>
+        <step length="0.375"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="0.4"/>
+        <step length="0.35"/>
+        <step freq="400" level="-12" length="0.255"/>
+        <step length="0.525"/>
+      </step>
+    </congestion-tone>
+  </tone-set>
+  <tone-set country="San Marino" uncode="sm">
+    <dial-tone>
+      <step freq="425" level="-12" length="0.6"/>
+      <step length="1.0"/>
+      <step freq="425" level="-12" length="0.2"/>
+      <step length="0.2"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="1.0"/>
+        <step length="4.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.2"/>
+        <step length="0.2"/>
+      </step>
+    </busy-tone>
+  </tone-set>
+  <tone-set country="Sao Tome e Principe" uncode="st">
+    <dial-tone>
+      <step freq="425" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="1.0"/>
+        <step length="5.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.2"/>
+        <step length="0.2"/>
+      </step>
+    </number-unobtainable-tone>
+    <pay-tone>
+      <step freq="12000" level="-12"/>
+    </pay-tone>
+  </tone-set>
+  <tone-set country="Saudi Arabia" uncode="sa">
+    <dial-tone>
+      <step freq="425" level="-12"/>
+    </dial-tone>
+    <dial-tone type="recall">
+      <step freq="425" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="1.2"/>
+        <step length="4.6"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <intrusion-tone>
+      <step freq="425" level="-12" length="0.5"/>
+      <step length="0.2"/>
+      <step freq="425" level="-12" length="0.2"/>
+      <step length="0.2"/>
+    </intrusion-tone>
+    <waiting-tone>
+      <step freq="425" level="-12" length="0.2"/>
+      <step length="0.6"/>
+      <step freq="425" level="-12" length="0.2"/>
+      <step length="4.0"/>
+    </waiting-tone>
+    <payphone-recognition-tone>
+      <step freq="1600" level="-12" length="0.2"/>
+      <step length="0.2"/>
+      <step freq="1200" level="-12" length="0.2"/>
+      <step length="2.0"/>
+    </payphone-recognition-tone>
+    <refusal-tone>
+      <step freq="425" level="-12" length="0.6"/>
+      <step length="0.6"/>
+    </refusal-tone>
+    <acceptance-tone>
+      <step freq="425" level="-12"/>
+    </acceptance-tone>
+  </tone-set>
+  <tone-set country="Senegal" uncode="sn">
+    <dial-tone>
+      <step freq="440" level="-12"/>
+    </dial-tone>
+    <dial-tone type="second">
+      <step freq="330+440" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="1.7"/>
+        <step length="3.3"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="440" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <route-tone>
+      <step freq="440" level="-12" length="0.05"/>
+      <step length="0.05"/>
+    </route-tone>
+  </tone-set>
+  <tone-set country="Seychelles" uncode="sc">
+    <dial-tone>
+      <step freq="350+440" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.4"/>
+        <step length="0.2"/>
+        <step freq="425" level="-12" length="0.4"/>
+        <step length="2.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.375"/>
+        <step length="0.375"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.4"/>
+        <step length="0.35"/>
+        <step freq="425" level="-12" length="0.225"/>
+        <step length="0.525"/>
+      </step>
+    </congestion-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12"/>
+      </step>
+    </number-unobtainable-tone>
+  </tone-set>
+  <tone-set country="Sierra Leone" uncode="sl">
+    <dial-tone>
+      <step freq="425 || 450" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425 || 450" level="-12" length="1.0"/>
+        <step length="4.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425 || 450" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.2"/>
+        <step length="0.2"/>
+      </step>
+    </congestion-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step freq="950 || 450" level="-12" length="0.333"/>
+        <step length="0.03"/>
+      </step>
+    </number-unobtainable-tone>
+    <waiting-tone>
+      <step cycles="1" freq="425" level="-12" length="1.0"/>
+    </waiting-tone>
+  </tone-set>
+  <tone-set country="Singapore" uncode="sg">
+    <dial-tone>
+      <step freq="425" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="401+449" level="-12" length="0.4"/>
+        <step length="0.2"/>
+        <step freq="401+449" level="-12" length="0.4"/>
+        <step length="2.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.75"/>
+        <step length="0.75"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.25"/>
+        <step length="0.25"/>
+      </step>
+    </congestion-tone>
+    <warning-tone type="operator-intervening">
+      <step freq="425" level="-12" length="0.125"/>
+      <step length="0.125"/>
+    </warning-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="2.5"/>
+        <step length="0.5"/>
+      </step>
+    </number-unobtainable-tone>
+    <waiting-tone>
+      <step freq="425" level="-12" length="0.3"/>
+      <step length="0.2"/>
+      <step freq="425" level="-12" length="0.3"/>
+      <step length="3.2"/>
+    </waiting-tone>
+    <intercept-tone>
+      <step freq="425" level="-12" length="0.25"/>
+      <step length="2.0"/>
+    </intercept-tone>
+    <payphone-recognition-tone>
+      <step freq="1206" level="-12" length="0.2"/>
+      <step length="0.2"/>
+      <step freq="832" level="-12" length="0.2"/>
+      <step length="2.0"/>
+    </payphone-recognition-tone>
+  </tone-set>
+  <tone-set country="Slovakia" uncode="sk">
+    <dial-tone>
+      <step freq="425" level="-12" length="0.333"/>
+      <step length="0.333"/>
+      <step freq="425" level="-12" length="0.66"/>
+      <step length="0.66"/>
+    </dial-tone>
+    <dial-tone domain="centrex">
+      <step freq="425" level="-12"/>
+    </dial-tone>
+    <dial-tone type="special">
+      <step cycles="3">
+        <step freq="425" level="-12" length="0.165"/>
+        <step length="0.165"/>
+      </step>
+      <step freq="425" level="-12" length="0.660"/>
+      <step length="0.660"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="1.0"/>
+        <step length="4.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.333"/>
+        <step length="0.333"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.165"/>
+        <step length="0.165"/>
+      </step>
+    </congestion-tone>
+    <special-information-tone>
+      <step freq="950" level="-12" length="0.33"/>
+      <step length="0.03"/>
+      <step freq="1400" level="-12" length="0.33"/>
+      <step length="0.03"/>
+      <step freq="1800" level="-12" length="0.33"/>
+      <step length="1.0"/>
+    </special-information-tone>
+    <waiting-tone>
+      <step freq="425" level="-12" length="0.333"/>
+      <step length="9.0"/>
+    </waiting-tone>
+    <call-waiting-tone>
+      <step freq="425" level="-12" length="1.0"/>
+      <step length="0.17"/>
+      <step freq="425" level="-12" length="0.333"/>
+      <step length="3.5"/>
+    </call-waiting-tone>
+    <connection-tone>
+      <step freq="1400" level="-12" length="0.05"/>
+      <step length="0.05"/>
+    </connection-tone>
+    <offering-tone>
+      <step freq="425" level="-12" length="0.333"/>
+      <step length="0.333"/>
+      <step freq="425" level="-12" length="0.333"/>
+      <step length="1.5"/>
+    </offering-tone>
+    <warning-tone type="conference">
+      <step freq="425" level="-12" length="0.66"/>
+    </warning-tone>
+  </tone-set>
+  <tone-set country="Slovenia" uncode="si">
+    <dial-tone>
+      <step freq="425" level="-12" length="0.7"/>
+      <step length="0.8"/>
+      <step freq="425" level="-12" length="0.2"/>
+      <step length="0.3"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="1.0"/>
+        <step length="4.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.2"/>
+        <step length="0.2"/>
+      </step>
+    </congestion-tone>
+    <special-information-tone>
+      <step freq="950" level="-12" length="0.333"/>
+      <step freq="1400" level="-12" length="0.333"/>
+      <step freq="1600" level="-12" length="0.333"/>
+      <step length="1.0"/>
+    </special-information-tone>
+  </tone-set>
+  <tone-set country="Solomon Islands" uncode="sb">
+    <dial-tone>
+      <step freq="400" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="400+450" level="-12" length="0.4"/>
+        <step length="0.2"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="0.1"/>
+        <step length="0.1"/>
+      </step>
+    </congestion-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="0.075"/>
+        <step length="0.1"/>
+      </step>
+    </number-unobtainable-tone>
+    <waiting-tone>
+      <step freq="400+450" level="-12" length="0.5"/>
+      <step freq="400" level="-12" length="0.5"/>
+    </waiting-tone>
+    <pay-tone>
+      <step freq="852" level="-12" length="0.2"/>
+      <step length="2.0"/>
+    </pay-tone>
+  </tone-set>
+  <tone-set country="South Africa" uncode="za">
+    <dial-tone>
+      <step freq="367+433" level="-12"/>
+    </dial-tone>
+    <dial-tone type="special">
+      <step cycles="4">
+        <step freq="367+433" level="-12" length="0.25"/>
+        <step length="0.25"/>
+      </step>
+      <step freq="367+433" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="367+433" level="-12" length="0.4"/>
+        <step length="0.2"/>
+        <step freq="367+433" level="-12" length="0.4"/>
+        <step length="2.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="0.25"/>
+        <step length="0.25"/>
+      </step>
+    </congestion-tone>
+    <special-information-tone>
+      <step freq="950" level="-12" length="0.333"/>
+      <step freq="1400" level="-12" length="0.333"/>
+      <step freq="1800" level="-12" length="0.333"/>
+      <step length="1.0"/>
+    </special-information-tone>
+    <warning-tone type="operator-intervening">
+      <step freq="400" level="-12" length="0.15"/>
+      <step length="0.25"/>
+      <step freq="400" level="-12" length="0.15"/>
+      <step length="1.45"/>
+    </warning-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="2.5"/>
+        <step length="0.5"/>
+      </step>
+    </number-unobtainable-tone>
+    <waiting-tone>
+      <step cycles="3">
+        <step freq="367+433" level="-12" length="0.25"/>
+        <step length="0.25"/>
+      </step>
+      <step freq="367+433" level="-12" length="0.25"/>
+      <step length="7.25"/>
+    </waiting-tone>
+    <pay-tone>
+      <step freq="900" level="-12" length="0.2"/>
+      <step length="1.8"/>
+    </pay-tone>
+    <payphone-recognition-tone>
+      <step freq="770" level="-12" length="0.125"/>
+      <step freq="1209" level="-12" length="1.5"/>
+    </payphone-recognition-tone>
+    <positive-indication-tone>
+      <step freq="700" level="-12" length="0.25"/>
+      <step length="0.25"/>
+      <step freq="1100" level="-12" length="0.25"/>
+      <step length="0.25"/>
+    </positive-indication-tone>
+    <comfort-tone>
+      <step freq="950" level="-12" length="0.65"/>
+      <step length="0.325"/>
+      <step freq="950" level="-12" length="0.325"/>
+      <step length="1.3"/>
+      <step freq="1400" level="-12" length="2.6"/>
+    </comfort-tone>
+    <notify-tone>
+      <step cycles="3">
+        <step freq="900" level="-12" length="0.2"/>
+        <step length="0.2"/>
+      </step>
+    </notify-tone>
+  </tone-set>
+  <tone-set country="Spain" uncode="es">
+    <dial-tone>
+      <step freq="425" level="-12"/>
+    </dial-tone>
+    <dial-tone>
+      <step freq="600" level="-12"/>
+    </dial-tone>
+    <dial-tone type="special">
+      <step freq="425" level="-12" length="1.0"/>
+      <step length="0.1"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="1.5"/>
+        <step length="3.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.2"/>
+        <step length="0.2"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step cycles="2">
+          <step freq="425" level="-12" length="0.2"/>
+          <step length="0.2"/>
+        </step>
+        <step freq="425" level="-12" length="0.2"/>
+        <step length="0.6"/>
+      </step>
+    </congestion-tone>
+    <special-information-tone>
+      <step freq="950" level="-12" length="0.333"/>
+      <step freq="1400" level="-12" length="0.333"/>
+      <step freq="1800" level="-12" length="0.333"/>
+      <step length="1.0"/>
+    </special-information-tone>
+    <special-information-tone>
+      <step freq="950" level="-12" length="0.33"/>
+      <step length="0.03"/>
+      <step freq="1400" level="-12" length="0.33"/>
+      <step length="0.03"/>
+      <step freq="1800" level="-12" length="0.33"/>
+      <step length="1.0"/>
+    </special-information-tone>
+    <warning-tone type="operator-intervening">
+      <step freq="1400" level="-12" length="0.4"/>
+      <step length="5.0"/>
+    </warning-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.2"/>
+        <step length="0.2"/>
+        <step freq="425" level="-12" length="0.2"/>
+        <step length="0.6"/>
+      </step>
+    </number-unobtainable-tone>
+    <waiting-tone>
+      <step freq="425" level="-12" length="0.2"/>
+      <step length="0.6"/>
+    </waiting-tone>
+    <waiting-tone>
+      <step freq="425" level="-12" length="0.175"/>
+      <step length="0.175"/>
+      <step freq="425" level="-12" length="0.175"/>
+      <step length="3.5"/>
+    </waiting-tone>
+    <payphone-recognition-tone>
+      <step freq="1600" level="-12" length="0.05"/>
+      <step length="1.28"/>
+    </payphone-recognition-tone>
+    <payphone-recognition-tone>
+      <step freq="1600" level="-12" length="0.05"/>
+      <step length="0.05"/>
+      <step freq="1600" level="-12" length="0.05"/>
+      <step length="1.55"/>
+    </payphone-recognition-tone>
+    <payphone-recognition-tone>
+      <step freq="1600" level="-12" length="0.05"/>
+      <step length="0.05"/>
+      <step freq="1600" level="-12" length="0.05"/>
+      <step length="0.05"/>
+      <step freq="1600" level="-12" length="0.05"/>
+      <step length="1.5"/>
+    </payphone-recognition-tone>
+  </tone-set>
+  <tone-set country="Sri Lanka" uncode="lk">
+    <dial-tone>
+      <step freq="425" level="-12"/>
+    </dial-tone>
+    <dial-tone type="second">
+      <step freq="425" level="-12" length="0.65"/>
+      <step length="0.03"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.4"/>
+        <step length="0.2"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.75"/>
+        <step length="0.75"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.25"/>
+        <step length="0.25"/>
+      </step>
+    </congestion-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12"/>
+      </step>
+    </number-unobtainable-tone>
+    <waiting-tone>
+      <step freq="425" level="-12" length="0.2"/>
+      <step length="0.2"/>
+      <step freq="425" level="-12" length="0.2"/>
+      <step length="4.5"/>
+    </waiting-tone>
+    <route-tone>
+      <step freq="425" level="-12" length="0.05"/>
+      <step length="0.05"/>
+    </route-tone>
+  </tone-set>
+  <tone-set country="Suriname" uncode="sr">
+    <dial-tone>
+      <step freq="425" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="15.0"/>
+        <step length="0.5"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.25"/>
+        <step length="0.25"/>
+      </step>
+    </congestion-tone>
+    <special-information-tone>
+      <step freq="950" level="-12" length="0.333"/>
+      <step freq="1400" level="-12" length="0.333"/>
+      <step freq="1800" level="-12" length="0.333"/>
+    </special-information-tone>
+  </tone-set>
+  <tone-set country="Swaziland" uncode="sz">
+    <dial-tone>
+      <step freq="50" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="0.4"/>
+        <step length="0.2"/>
+        <step freq="400" level="-12" length="0.4"/>
+        <step length="4.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="0.4"/>
+        <step length="0.4"/>
+      </step>
+    </busy-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="4.8"/>
+        <step length="0.2"/>
+      </step>
+    </number-unobtainable-tone>
+    <payphone-recognition-tone>
+      <step freq="1430" level="-12" length="2.0"/>
+      <step length="2.0"/>
+      <step freq="943" level="-12" length="2.0"/>
+    </payphone-recognition-tone>
+    <payphone-recognition-tone>
+      <step freq="1100+1750" level="-12" length="0.2"/>
+      <step length="0.2"/>
+      <step freq="750+1450" level="-12" length="0.2"/>
+      <step length="2.0"/>
+    </payphone-recognition-tone>
+    <pay-tone>
+      <step freq="900" level="-12" length="0.25"/>
+      <step length="0.25"/>
+    </pay-tone>
+  </tone-set>
+  <tone-set country="Sweden" uncode="se">
+    <dial-tone>
+      <step freq="425" level="-12"/>
+    </dial-tone>
+    <dial-tone type="second">
+      <step freq="425" level="-12"/>
+    </dial-tone>
+    <dial-tone type="special">
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.32"/>
+        <step length="0.01"/>
+      </step>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="1.0"/>
+        <step length="5.0"/>
+      </step>
+    </ringback-tone>
+    <ringback-tone domain="analogue">
+      <step cycles="endless">
+        <step freq="400" level="-12" length="1.0"/>
+        <step length="9.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.25"/>
+        <step length="0.25"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.25"/>
+        <step length="0.75"/>
+      </step>
+    </congestion-tone>
+    <special-information-tone>
+      <step freq="950" level="-12" length="0.333"/>
+      <step freq="1400" level="-12" length="0.333"/>
+      <step freq="1800" level="-12" length="0.333"/>
+      <step length="1.0"/>
+      <step recorded-announcement="true"/>
+    </special-information-tone>
+    <special-information-tone domain="analogue">
+      <step cycles="3">
+        <step freq="400" level="-12" length="0.05"/>
+        <step length="0.2"/>
+      </step>
+      <step freq="400" level="-12" length="0.6"/>
+    </special-information-tone>
+    <warning-tone type="operator-intervening">
+      <step cycles="endless">
+        <step freq="1400" level="-12" length="0.1"/>
+        <step length="1.5"/>
+      </step>
+    </warning-tone>
+    <warning-tone type="three-party-conference">
+      <step cycles="endless">
+        <step freq="1400" level="-12" length="0.35"/>
+        <step length="15.0"/>
+      </step>
+    </warning-tone>
+    <warning-tone type="time-limit">
+      <step freq="425" level="-12" length="2.0"/>
+      <step length="5.0"/>
+      <step freq="425" level="-12"/>
+    </warning-tone>
+    <number-unobtainable-tone>
+      <step freq="950" level="-12" length="0.333"/>
+      <step freq="1400" level="-12" length="0.333"/>
+      <step freq="1800" level="-12" length="0.333"/>
+      <step length="1.0"/>
+      <step recorded-announcement="true"/>
+    </number-unobtainable-tone>
+    <waiting-tone>
+      <step freq="425" level="-12" length="0.2"/>
+      <step length="0.5"/>
+      <step freq="425" level="-12" length="0.2"/>
+    </waiting-tone>
+    <payphone-recognition-tone>
+      <step cycles="7">
+        <step freq="1633" level="-12" length="0.2"/>
+        <step length="0.2"/>
+        <step freq="1336" level="-12" length="0.2"/>
+        <step length="4.0"/>
+      </step>
+    </payphone-recognition-tone>
+    <pay-tone>
+      <step cycles="2">
+        <step freq="941" level="-12" length="0.2"/>
+        <step length="0.2"/>
+        <step freq="941" level="-12" length="0.2"/>
+        <step length="3.6"/>
+      </step>
+    </pay-tone>
+  </tone-set>
+  <tone-set country="Switzerland" uncode="ch">
+    <dial-tone>
+      <step freq="425" level="-12"/>
+    </dial-tone>
+    <dial-tone domain="PABX">
+      <step freq="500" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425 || 500" level="-12" length="1.0"/>
+        <step length="4.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.25"/>
+        <step length="0.25"/>
+      </step>
+    </busy-tone>
+    <busy-tone domain="PABX">
+      <step cycles="endless">
+        <step freq="500" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.2"/>
+        <step length="0.2"/>
+      </step>
+    </congestion-tone>
+    <special-information-tone>
+      <step freq="950" level="-12" length="0.333"/>
+      <step freq="1400" level="-12" length="0.333"/>
+      <step freq="1800" level="-12" length="0.333"/>
+      <step length="1.0"/>
+    </special-information-tone>
+    <record-tone>
+      <step freq="1400" level="-12" length="0.45"/>
+      <step length="15.0"/>
+    </record-tone>
+    <payphone-recognition-tone>
+      <step freq="800" level="-12" length="0.2"/>
+      <step length="0.2"/>
+      <step freq="1200" level="-12" length="0.2"/>
+      <step length="2.0"/>
+    </payphone-recognition-tone>
+    <payphone-recognition-tone>
+      <step freq="1100+1750" level="-12" length="0.2"/>
+      <step length="0.2"/>
+      <step freq="750+1450" level="-12" length="0.2"/>
+      <step length="2.0"/>
+    </payphone-recognition-tone>
+  </tone-set>
+  <tone-set country="Syria" uncode="sy">
+    <dial-tone>
+      <step freq="450" level="-12"/>
+    </dial-tone>
+    <dial-tone type="recall">
+      <step freq="450" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425 || 475" level="-12" length="1.0"/>
+        <step length="4.0"/>
+      </step>
+    </ringback-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="450 || 50" level="-12" length="1.0"/>
+        <step length="3.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="450" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="450" level="-12" length="0.44"/>
+        <step length="0.49"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step cycles="2">
+          <step freq="450" level="-12" length="0.2"/>
+          <step length="0.23"/>
+        </step>
+        <step freq="450" level="-12" length="0.2"/>
+        <step length="0.92"/>
+      </step>
+    </congestion-tone>
+    <special-information-tone>
+      <step freq="950" level="-12" length="0.33"/>
+      <step length="0.03"/>
+      <step freq="1400" level="-12" length="0.33"/>
+      <step length="0.03"/>
+      <step freq="1800" level="-12" length="0.33"/>
+      <step length="1.0"/>
+    </special-information-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step freq="450" level="-12" length="0.9"/>
+        <step length="0.2"/>
+        <step cycles="2">
+          <step freq="450" level="-12" length="0.25"/>
+          <step length="0.2"/>
+        </step>
+      </step>
+    </number-unobtainable-tone>
+  </tone-set>
+  <tone-set country="Tajikistan" uncode="tj">
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.8"/>
+        <step length="3.2"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.4"/>
+        <step length="0.4"/>
+      </step>
+    </busy-tone>
+    <waiting-tone>
+      <step freq="950" level="-12" length="0.333"/>
+      <step freq="1400" level="-12" length="0.333"/>
+      <step freq="1800" level="-12" length="0.333"/>
+      <step length="1.0"/>
+    </waiting-tone>
+  </tone-set>
+  <tone-set country="Tanzania" uncode="tz">
+    <dial-tone>
+      <step freq="50" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step freq="400" level="-12"/>
+    </ringback-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="1.0"/>
+        <step length="2.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="0.75"/>
+        <step length="0.75"/>
+      </step>
+    </busy-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="0.375"/>
+        <step length="0.375"/>
+      </step>
+    </busy-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="2.5"/>
+        <step length="0.5"/>
+      </step>
+    </number-unobtainable-tone>
+  </tone-set>
+  <tone-set country="Thailand" uncode="th">
+    <dial-tone>
+      <step freq="350+450 || 400" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="1.0"/>
+        <step length="4.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="450" level="-12" length="0.333"/>
+        <step length="0.333"/>
+      </step>
+    </busy-tone>
+    <warning-tone type="operator-intervening">
+      <step freq="800" level="-12" length="1.0"/>
+    </warning-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step cycles="6">
+          <step freq="400" level="-12" length="0.1"/>
+          <step length="0.9"/>
+        </step>
+        <step freq="400" level="-12" length="0.3"/>
+        <step length="0.7"/>
+      </step>
+    </number-unobtainable-tone>
+    <offering-tone>
+      <step cycles="2">
+        <step freq="400" level="-12" length="0.17"/>
+        <step length="0.83"/>
+      </step>
+      <step freq="400" level="-12" length="0.51"/>
+      <step length="0.49"/>
+    </offering-tone>
+  </tone-set>
+  <tone-set country="Trinidad and Tobago" uncode="tt">
+    <dial-tone>
+      <step freq="350+440" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="440+480" level="-12" length="2.0"/>
+        <step length="4.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="480+620" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <waiting-tone>
+      <step freq="440" level="-12" length="0.3"/>
+      <step length="10.0"/>
+    </waiting-tone>
+    <re-order-tone>
+      <step freq="480+620" level="-12" length="0.3"/>
+      <step length="0.2"/>
+    </re-order-tone>
+    <re-order-tone>
+      <step freq="480+620" level="-12" length="0.2"/>
+      <step length="0.3"/>
+    </re-order-tone>
+  </tone-set>
+  <tone-set country="Tunisia" uncode="tn">
+    <dial-tone>
+      <step freq="425" level="-12"/>
+    </dial-tone>
+    <dial-tone>
+      <step freq="425" level="-12" length="0.4"/>
+      <step length="0.04"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="1.2"/>
+        <step length="4.6"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.25"/>
+        <step length="0.25"/>
+      </step>
+    </busy-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="950" level="-12"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.167"/>
+        <step length="0.167"/>
+      </step>
+    </congestion-tone>
+  </tone-set>
+  <tone-set country="Turkey" uncode="tr">
+    <dial-tone>
+      <step freq="450" level="-12"/>
+    </dial-tone>
+    <dial-tone type="second">
+      <step freq="350+450" level="-12"/>
+    </dial-tone>
+    <dial-tone type="special">
+      <step freq="450" level="-12" length="1.0"/>
+      <step length="0.25"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="450" level="-12" length="2.0"/>
+        <step length="4.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="450" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step cycles="3">
+          <step freq="450" level="-12" length="0.2"/>
+          <step length="0.2"/>
+        </step>
+        <step freq="450" level="-12" length="0.6"/>
+        <step length="0.2"/>
+      </step>
+    </congestion-tone>
+    <special-information-tone>
+      <step freq="950" level="-12" length="0.3"/>
+      <step freq="1400" level="-12" length="0.3"/>
+      <step freq="1800" level="-12" length="0.3"/>
+      <step length="1.0"/>
+    </special-information-tone>
+    <warning-tone type="operator-intervening">
+      <step freq="450" level="-12" length="0.2"/>
+      <step length="0.2"/>
+      <step freq="450" level="-12" length="0.6"/>
+      <step length="0.2"/>
+    </warning-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step freq="450" level="-12" length="0.2"/>
+        <step length="0.2"/>
+      </step>
+    </number-unobtainable-tone>
+    <waiting-tone>
+      <step freq="350+450 || 450" level="-12" length="0.5"/>
+      <step length="0.5"/>
+      <step freq="350+450 || 450" level="-12" length="2.5"/>
+    </waiting-tone>
+    <payphone-recognition-tone>
+      <step freq="1000" level="-12" length="0.25"/>
+      <step length="0.25"/>
+      <step freq="1200 || 1330" level="-12" length="0.25"/>
+      <step length="2.0"/>
+    </payphone-recognition-tone>
+    <confirmation-tone>
+      <step freq="450" level="-12" length="0.04"/>
+      <step length="0.04"/>
+    </confirmation-tone>
+  </tone-set>
+  <tone-set country="Turkmenistan" uncode="tm">
+    <dial-tone>
+      <step freq="425" level="-12"/>
+    </dial-tone>
+    <dial-tone type="second">
+      <step freq="425" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="25" level="-12" length="0.4"/>
+        <step length="4.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.4"/>
+        <step length="0.4"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.4"/>
+        <step length="0.4"/>
+      </step>
+    </congestion-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.8"/>
+        <step length="4.0"/>
+      </step>
+    </number-unobtainable-tone>
+    <route-tone>
+      <step freq="25" level="-12" length="0.4"/>
+      <step length="0.4"/>
+    </route-tone>
+  </tone-set>
+  <tone-set country="Turks and Caicos Islands" uncode="tc">
+    <dial-tone>
+      <step freq="350+440" level="-12"/>
+    </dial-tone>
+    <dial-tone type="recall">
+      <step cycles="3">
+        <step freq="350+440" level="-12" length="0.1"/>
+        <step length="0.1"/>
+      </step>
+      <step freq="350+440" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="440+480" level="-12" length="2.0"/>
+        <step length="4.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="480+620" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="480+620" level="-12" length="0.25"/>
+        <step length="0.25"/>
+      </step>
+    </congestion-tone>
+    <special-information-tone>
+      <step freq="950" level="-12" length="0.333"/>
+      <step freq="1400" level="-12" length="0.333"/>
+      <step freq="1800" level="-12" length="0.333"/>
+    </special-information-tone>
+    <warning-tone type="operator-intervening">
+      <step freq="450" level="-12" length="0.2"/>
+      <step length="0.2"/>
+      <step freq="450" level="-12" length="0.65"/>
+      <step length="0.2"/>
+    </warning-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step freq="480+620" level="-12" length="0.25"/>
+        <step length="0.25"/>
+      </step>
+    </number-unobtainable-tone>
+    <waiting-tone>
+      <step cycles="1">
+        <step freq="440" level="-12" length="0.5"/>
+        <step length="10.0"/>
+        <step length="0.5"/>
+      </step>
+    </waiting-tone>
+    <payphone-recognition-tone>
+      <step freq="1600" level="-12" length="0.2"/>
+      <step length="0.2"/>
+      <step freq="1143" level="-12" length="0.2"/>
+      <step length="2.0"/>
+    </payphone-recognition-tone>
+  </tone-set>
+  <tone-set country="Uganda" uncode="ug">
+    <dial-tone>
+      <step freq="33 || 50" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="133+17 || 400+17 || 400+450" level="-12" length="0.25"/>
+        <step length="0.5"/>
+        <step freq="133+17 || 400+17 || 400+450" level="-12" length="0.25"/>
+        <step length="2.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="0.75"/>
+        <step length="0.75"/>
+      </step>
+    </busy-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="0.375"/>
+        <step length="0.375"/>
+      </step>
+    </busy-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12"/>
+      </step>
+    </number-unobtainable-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="2.5"/>
+        <step length="0.5"/>
+      </step>
+    </number-unobtainable-tone>
+  </tone-set>
+  <tone-set country="United Arab Emirates" uncode="ae">
+    <dial-tone>
+      <step freq="350+440" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="400+450 || 425" level="-12" length="0.4"/>
+        <step length="0.2"/>
+        <step freq="400+450 || 425" level="-12" length="0.4"/>
+        <step length="2.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="400 || 425" level="-12" length="0.375"/>
+        <step length="0.375"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="400 || 425" level="-12" length="0.4"/>
+        <step length="0.35"/>
+        <step freq="400 || 425" level="-12" length="0.225"/>
+        <step length="0.525"/>
+      </step>
+    </congestion-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step freq="400 || 425" level="-12"/>
+      </step>
+    </number-unobtainable-tone>
+  </tone-set>
+  <tone-set country="United Kingdom" uncode="uk">
+    <dial-tone>
+      <step freq="350+440 || 50" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="400+450 || 375+425 || 383.33+416.67" level="-12" length="0.4"/>
+        <step length="0.2"/>
+        <step freq="400+450 || 375+425 || 383.33+416.67" level="-12" length="0.4"/>
+        <step length="2.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="0.375"/>
+        <step length="0.375"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="0.4"/>
+        <step length="0.35"/>
+        <step freq="400" level="-12" length="0.225"/>
+        <step length="0.525"/>
+      </step>
+    </congestion-tone>
+    <number-unobtainable-tone>
+      <step freq="400" level="-12"/>
+    </number-unobtainable-tone>
+    <payphone-recognition-tone>
+      <step freq="1200" level="-12" length="0.2"/>
+      <step length="0.2"/>
+      <step freq="800" level="-12" length="0.2"/>
+      <step length="2.0"/>
+    </payphone-recognition-tone>
+    <pay-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="0.125"/>
+        <step length="0.125"/>
+      </step>
+    </pay-tone>
+  </tone-set>
+  <tone-set country="United States" uncode="us">
+    <dial-tone>
+      <step freq="350+440 || 480+720" level="-13 [1.5dB]"/>
+    </dial-tone>
+    <dial-tone domain="PABX">
+      <step freq="350+440 || 480+720" level="-16 [0.75dB]"/>
+    </dial-tone>
+    <dial-tone type="recall">
+      <step cycles="3">
+        <step freq="350+440" level="-13 [1.5dB]" length="0.1"/>
+        <step length="0.1"/>
+      </step>
+      <step freq="350+440" level="-13 [1.5dB]"/>
+    </dial-tone>
+    <dial-tone domain="PABX" type="recall">
+      <step cycles="3">
+        <step freq="350+440" level="-16 [0.75dB]" length="0.1"/>
+        <step length="0.1"/>
+      </step>
+      <step freq="350+440" level="-16 [0.75dB]"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="440+480 || 380+460" level="-19 [1.5dB]" length="2.0"/>
+        <step length="4.0"/>
+      </step>
+    </ringback-tone>
+    <ringback-tone domain="PABX">
+      <step cycles="endless">
+        <step freq="440+480" level="-16 [1.5dB]" length="1.0"/>
+        <step length="3.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="480+620 || 480+720" level="-24 [1.5dB]" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <busy-tone domain="PABX">
+      <step cycles="endless">
+        <step freq="480+620 || 480+720" level="-21 [1.5dB]" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="480+620 || 480+720" level="-24 [1.5dB]" length="0.25"/>
+        <step length="0.25"/>
+      </step>
+    </congestion-tone>
+    <congestion-tone domain="PABX">
+      <step cycles="endless">
+        <step freq="480+620 || 480+720" level="-21 [1.5dB]" length="0.25"/>
+        <step length="0.25"/>
+      </step>
+    </congestion-tone>
+    <call-waiting-tone>
+      <step cycles="endless">
+        <step freq="480+620" level="-13 [1.5dB]" length="0.3"/>
+        <step length="10.0"/>
+      </step>
+    </call-waiting-tone>
+    <call-waiting-tone domain="PABX" type="station-call">
+      <step freq="480+620" level="-16 [1.5dB]" length="0.3"/>
+    </call-waiting-tone>
+    <call-waiting-tone domain="PABX" type="outside-call">
+      <step cycles="2">
+        <step freq="480+620" level="-16 [1.5dB]" length="0.1"/>
+        <step length="0.1"/>
+      </step>
+    </call-waiting-tone>
+    <call-waiting-tone domain="PABX" type="urgent-call">
+      <step cycles="3">
+        <step freq="480+620" level="-16 [1.5dB]" length="0.1"/>
+        <step length="0.1"/>
+      </step>
+    </call-waiting-tone>
+    <special-information-tone>
+      <step freq="950" level="-13 [1.5dB]" length="0.33"/>
+      <step freq="1400" level="-13 [1.5dB]" length="0.33"/>
+      <step freq="1800" level="-13 [1.5dB]" length="0.33"/>
+    </special-information-tone>
+    <warning-tone type="operator-intervening">
+      <step freq="440" level="-13 [1.5dB]" length="2.0"/>
+      <step length="10.0"/>
+      <step freq="440" level="-13 [1.5dB]" length="0.5"/>
+      <step length="10.0"/>
+    </warning-tone>
+    <warning-tone type="operator-intervening" domain="PABX">
+      <step freq="440" level="-13 [1.5dB]" length="1.5"/>
+      <step length="8.0"/>
+      <step freq="440" level="-13 [1.5dB]" length="0.5"/>
+      <step length="8.0"/>
+    </warning-tone>
+    <waiting-tone domain="PABX">
+      <step freq="440" level="-13 [1.5dB]" length="0.3"/>
+      <step length="10.0"/>
+    </waiting-tone>
+    <record-tone>
+      <step freq="1400" level="-13 [1.5dB]" length="0.5"/>
+      <step length="15.0"/>
+    </record-tone>
+    <executive-override-tone domain="PABX">
+      <step freq="440" level="-14 [1.5dB]" length="3.0"/>
+    </executive-override-tone>
+    <intercept-tone domain="PABX">
+      <step freq="440" level="-13 [1.5dB]" length="0.25"/>
+      <step freq="620" level="-13 [1.5dB]" length="0.25"/>
+    </intercept-tone>
+    <confirmation-tone>
+      <step freq="350+440" level="-13 [1.5dB]" length="0.1"/>
+      <step length="0.1"/>
+      <step freq="350+440" level="-13 [1.5dB]" length="0.3"/>
+    </confirmation-tone>
+    <confirmation-tone domain="PABX">
+      <step cycles="2">
+        <step freq="350+440" level="-16 [1.5dB]" length="0.1"/>
+        <step length="0.1"/>
+      </step>
+      <step freq="350+440" level="-16 [1.5dB]" length="0.1"/>
+    </confirmation-tone>
+  </tone-set>
+  <tone-set country="Uruguay" uncode="uy">
+    <dial-tone>
+      <step freq="425" level="-12"/>
+    </dial-tone>
+    <dial-tone>
+      <step freq="450" level="-12" length="0.7"/>
+      <step length="0.8"/>
+      <step freq="450" level="-12" length="0.2"/>
+      <step length="0.3"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="1.0"/>
+        <step length="4.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.25"/>
+        <step length="0.25"/>
+      </step>
+    </congestion-tone>
+    <special-information-tone>
+      <step freq="950" level="-12" length="0.333"/>
+      <step freq="1400" level="-12" length="0.333"/>
+      <step freq="1800" level="-12" length="0.333"/>
+      <step length="1.0"/>
+    </special-information-tone>
+    <warning-tone type="operator-intervening">
+      <step freq="425" level="-12" length="0.15"/>
+      <step length="0.25"/>
+      <step freq="425" level="-12" length="0.15"/>
+      <step length="1.45"/>
+    </warning-tone>
+    <waiting-tone>
+      <step freq="425" level="-12" length="0.2"/>
+      <step length="0.2"/>
+      <step freq="425" level="-12" length="0.2"/>
+      <step length="4.4"/>
+    </waiting-tone>
+    <positive-indication-tone>
+      <step cycles="2">
+        <step freq="425" level="-12" length="0.1"/>
+        <step length="0.2"/>
+      </step>
+      <step freq="425" level="-12" length="0.1"/>
+      <step length="1.3"/>
+    </positive-indication-tone>
+    <negative-indication-tone>
+      <step freq="425" level="-12" length="0.5"/>
+      <step length="0.5"/>
+    </negative-indication-tone>
+  </tone-set>
+  <tone-set country="Vanuatu" uncode="vu">
+    <dial-tone>
+      <step freq="425" level="-12"/>
+    </dial-tone>
+    <dial-tone type="special">
+      <step freq="425" level="-12" length="0.4"/>
+      <step length="0.04"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="1.0"/>
+        <step length="4.0"/>
+      </step>
+    </ringback-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="400+450 || 116.33+149.67" level="-12" length="0.4"/>
+        <step length="0.2"/>
+        <step freq="400+450 || 116.33+149.67" level="-12" length="0.4"/>
+        <step length="2.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="0.75"/>
+        <step length="0.75"/>
+      </step>
+    </busy-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="0.375"/>
+        <step length="0.375"/>
+      </step>
+    </busy-tone>
+    <special-information-tone>
+      <step freq="950" level="-12" length="0.33"/>
+      <step length="0.03"/>
+      <step freq="1400" level="-12" length="0.33"/>
+      <step length="0.03"/>
+      <step freq="1800" level="-12" length="0.33"/>
+      <step length="0.03"/>
+    </special-information-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12"/>
+      </step>
+    </number-unobtainable-tone>
+    <waiting-tone>
+      <step freq="425" level="-12" length="0.3"/>
+      <step length="10.0"/>
+    </waiting-tone>
+    <route-tone>
+      <step freq="425" level="-12" length="0.05"/>
+      <step length="0.05"/>
+    </route-tone>
+    <acceptance-tone>
+      <step freq="950" level="-12"/>
+    </acceptance-tone>
+  </tone-set>
+  <tone-set country="Wallis and Futuna Islands" uncode="wf">
+    <dial-tone>
+      <step freq="440 || 330+440" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="440" level="-12" length="1.5"/>
+        <step length="3.5"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="440" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <special-information-tone>
+      <step freq="950" level="-12" length="0.3"/>
+      <step length="0.03"/>
+      <step freq="1400" level="-12" length="0.3"/>
+      <step length="0.03"/>
+      <step freq="1800" level="-12" length="0.3"/>
+      <step length="1.0"/>
+    </special-information-tone>
+    <route-tone>
+      <step freq="440" level="-12" length="0.05"/>
+      <step length="0.05"/>
+    </route-tone>
+  </tone-set>
+  <tone-set country="Western Samoa" uncode="ws">
+    <dial-tone>
+      <step freq="425" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="400+450" level="-12" length="0.375"/>
+        <step length="0.25"/>
+        <step freq="400+450" level="-12" length="0.375"/>
+        <step length="2.0"/>
+      </step>
+    </ringback-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step recorded-announcement="true"/>
+      </step>
+    </congestion-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step recorded-announcement="true"/>
+      </step>
+    </number-unobtainable-tone>
+  </tone-set>
+  <tone-set country="Yugoslavia" uncode="yu">
+    <dial-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.7"/>
+        <step length="0.8"/>
+        <step freq="425" level="-12" length="0.2"/>
+        <step length="0.3"/>
+      </step>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425+475" level="-12" length="1.0"/>
+        <step length="9.0"/>
+      </step>
+    </ringback-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="1.0"/>
+        <step length="4.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.2"/>
+        <step length="0.4"/>
+      </step>
+    </busy-tone>
+    <special-information-tone>
+      <step freq="950" level="-12" length="0.333"/>
+      <step freq="1400" level="-12" length="0.333"/>
+      <step freq="1800" level="-12" length="0.333"/>
+      <step length="1.0"/>
+    </special-information-tone>
+  </tone-set>
+  <tone-set country="Zambia" uncode="zm">
+    <dial-tone>
+      <step freq="425 || 50" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="1.0"/>
+        <step length="4.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.5"/>
+        <step length="0.5"/>
+      </step>
+    </busy-tone>
+    <congestion-tone>
+      <step cycles="endless">
+        <step freq="425" level="-12" length="0.25"/>
+        <step length="0.25"/>
+      </step>
+    </congestion-tone>
+    <special-information-tone>
+      <step freq="950" level="-12" length="0.333"/>
+      <step freq="1400" level="-12" length="0.333"/>
+      <step freq="1800" level="-12" length="0.333"/>
+      <step length="1.0"/>
+    </special-information-tone>
+    <intrusion-tone>
+      <step freq="425" level="-12" length="0.1"/>
+      <step length="4.9"/>
+    </intrusion-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step freq="950" level="-12" length="0.333"/>
+        <step freq="1400" level="-12" length="0.333"/>
+        <step freq="1800" level="-12" length="0.333"/>
+      </step>
+    </number-unobtainable-tone>
+    <payphone-recognition-tone>
+      <step freq="1100+1750" level="-12" length="0.2"/>
+      <step length="0.2"/>
+      <step freq="750+1450" level="-12" length="0.2"/>
+      <step length="2.0"/>
+    </payphone-recognition-tone>
+  </tone-set>
+  <tone-set country="Zimbabwe" uncode="zw">
+    <dial-tone>
+      <step freq="400" level="-12"/>
+    </dial-tone>
+    <dial-tone type="second">
+      <step freq="400+450 || 367+433" level="-12"/>
+    </dial-tone>
+    <ringback-tone>
+      <step cycles="endless">
+        <step freq="400+450 || 367+433" level="-12" length="0.4"/>
+        <step length="0.2"/>
+        <step freq="400+450 || 367+433" level="-12" length="0.4"/>
+        <step length="2.0"/>
+      </step>
+    </ringback-tone>
+    <busy-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="0.375"/>
+        <step length="0.375"/>
+      </step>
+    </busy-tone>
+    <number-unobtainable-tone>
+      <step cycles="endless">
+        <step freq="400" level="-12" length="2.5"/>
+        <step length="0.5"/>
+      </step>
+    </number-unobtainable-tone>
+    <waiting-tone>
+      <step freq="523+659" level="-12" length="1.5"/>
+      <step length="1.5"/>
+    </waiting-tone>
+    <payphone-recognition-tone>
+      <step freq="1100+1750" level="-12" length="0.2"/>
+      <step length="0.2"/>
+      <step freq="750+1450" level="-12" length="0.2"/>
+      <step length="2.0"/>
+    </payphone-recognition-tone>
+  </tone-set>
+</global-tones>

Added: freeswitch/trunk/libs/spandsp/spandsp/tones.dtd
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/spandsp/tones.dtd	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,193 @@
+<!-- Global PSTN supervisory tones -->
+<!ELEMENT global-tones (tone-set)* >
+<!ELEMENT tone-set (dial-tone|ringback-tone|busy-tone|congestion-tone|number-unobtainable-tone|special-information-tone|call-waiting-tone|waiting-tone|payphone-recognition-tone|test-number-tone|end-of-three-party-service-tone|service-activated-tone|record-tone|facilities-tone|offering-tone|positive-indication-tone|negative-indication-tone|route-tone|pay-tone|confirmation-tone|intrusion-tone|intercept-tone|warning-tone|holding-tone|search-tone|acceptance-tone|executive-override-tone|function-acknowledge-tone|queue-tone|valid-tone-radio-paging|notify-tone|re-order-tone|preemption-tone|comfort-tone|connection-tone|refusal-tone|line-lockout-tone|permanent-signal-tone|identification-tone|reorder-tone|recall-dial-tone|special-ringback-tone|busy-verification-tone)* >
+
+<!ATTLIST tone-set
+  country   CDATA   #REQUIRED
+  uncode    CDATA   #REQUIRED
+  >
+
+<!ELEMENT dial-tone (step)* >
+<!ATTLIST dial-tone
+  domain    CDATA   #IMPLIED
+  type      CDATA   #IMPLIED
+  >
+<!ELEMENT ringback-tone (step)* >
+<!ATTLIST ringback-tone
+  domain    CDATA   #IMPLIED
+  >
+<!ELEMENT busy-tone (step)* >
+<!ATTLIST busy-tone
+  domain    CDATA   #IMPLIED
+  >
+<!ELEMENT congestion-tone (step)* >
+<!ATTLIST congestion-tone
+  domain    CDATA   #IMPLIED
+  >
+<!ELEMENT number-unobtainable-tone (step)* >
+<!ATTLIST number-unobtainable-tone
+  domain    CDATA   #IMPLIED
+  >
+<!ELEMENT special-information-tone (step)* >
+<!ATTLIST special-information-tone
+  domain    CDATA   #IMPLIED
+  >
+<!ELEMENT call-waiting-tone (step)* >
+<!ATTLIST call-waiting-tone
+  domain    CDATA   #IMPLIED
+  >
+<!ELEMENT pay-tone (step)* >
+<!ATTLIST pay-tone
+  domain    CDATA   #IMPLIED
+  >
+<!ELEMENT payphone-recognition-tone (step)* >
+<!ATTLIST payphone-recognition-tone
+  domain    CDATA   #IMPLIED
+  >
+<!ELEMENT warning-tone (step)* >
+<!ATTLIST warning-tone
+  type      CDATA   #IMPLIED
+  domain    CDATA   #IMPLIED
+  >
+<!ELEMENT reorder-tone (step)* >
+<!ATTLIST reorder-tone
+  domain    CDATA   #IMPLIED
+  >
+<!ELEMENT recall-dial-tone (step)* >
+<!ATTLIST recall-dial-tone
+  domain    CDATA   #IMPLIED
+  >
+<!ELEMENT special-ringback-tone (step)* >
+<!ATTLIST special-ringback-tone
+  domain    CDATA   #IMPLIED
+  >
+<!ELEMENT busy-verification-tone (step)* >
+<!ATTLIST busy-verification-tone
+  domain    CDATA   #IMPLIED
+  >
+
+<!ELEMENT waiting-tone (step)* >
+<!ATTLIST waiting-tone
+  domain    CDATA   #IMPLIED
+  >
+<!ELEMENT test-number-tone (step)* >
+<!ATTLIST test-number-tone
+  domain    CDATA   #IMPLIED
+  >
+<!ELEMENT end-of-three-party-service-tone (step)* >
+<!ATTLIST end-of-three-party-service-tone
+  domain    CDATA   #IMPLIED
+  >
+<!ELEMENT service-activated-tone (step)* >
+<!ATTLIST service-activated-tone
+  domain    CDATA   #IMPLIED
+  >
+<!ELEMENT record-tone (step)* >
+<!ATTLIST record-tone
+  domain    CDATA   #IMPLIED
+  >
+<!ELEMENT facilities-tone (step)* >
+<!ATTLIST facilities-tone
+  domain    CDATA   #IMPLIED
+  >
+<!ELEMENT offering-tone (step)* >
+<!ATTLIST offering-tone
+  domain    CDATA   #IMPLIED
+  >
+<!ELEMENT positive-indication-tone (step)* >
+<!ATTLIST positive-indication-tone
+  domain    CDATA   #IMPLIED
+  >
+<!ELEMENT negative-indication-tone (step)* >
+<!ATTLIST negative-indication-tone
+  domain    CDATA   #IMPLIED
+  >
+<!ELEMENT route-tone (step)* >
+<!ATTLIST route-tone
+  domain    CDATA   #IMPLIED
+  >
+<!ELEMENT confirmation-tone (step)* >
+<!ATTLIST confirmation-tone
+  domain    CDATA   #IMPLIED
+  >
+<!ELEMENT intrusion-tone (step)* >
+<!ATTLIST intrusion-tone
+  domain    CDATA   #IMPLIED
+  >
+<!ELEMENT intercept-tone (step)* >
+<!ATTLIST intercept-tone
+  domain    CDATA   #IMPLIED
+  >
+<!ELEMENT holding-tone (step)* >
+<!ATTLIST holding-tone
+  domain    CDATA   #IMPLIED
+  >
+<!ELEMENT search-tone (step)* >
+<!ATTLIST search-tone
+  domain    CDATA   #IMPLIED
+  >
+<!ELEMENT acceptance-tone (step)* >
+<!ATTLIST acceptance-tone
+  domain    CDATA   #IMPLIED
+  >
+<!ELEMENT executive-override-tone (step)* >
+<!ATTLIST executive-override-tone
+  domain    CDATA   #IMPLIED
+  >
+<!ELEMENT function-acknowledge-tone (step)* >
+<!ATTLIST function-acknowledge-tone
+  domain    CDATA   #IMPLIED
+  >
+<!ELEMENT queue-tone (step)* >
+<!ATTLIST queue-tone
+  domain    CDATA   #IMPLIED
+  >
+<!ELEMENT valid-tone-radio-paging (step)* >
+<!ATTLIST valid-tone-radio-paging
+  domain    CDATA   #IMPLIED
+  >
+<!ELEMENT notify-tone (step)* >
+<!ATTLIST notify-tone
+  domain    CDATA   #IMPLIED
+  >
+<!ELEMENT re-order-tone (step)* >
+<!ATTLIST re-order-tone
+  domain    CDATA   #IMPLIED
+  >
+<!ELEMENT preemption-tone (step)* >
+<!ATTLIST preemption-tone
+  domain    CDATA   #IMPLIED
+  >
+<!ELEMENT comfort-tone (step)* >
+<!ATTLIST comfort-tone
+  domain    CDATA   #IMPLIED
+  >
+<!ELEMENT connection-tone (step)* >
+<!ATTLIST connection-tone
+  domain    CDATA   #IMPLIED
+  >
+<!ELEMENT refusal-tone (step)* >
+<!ATTLIST refusal-tone
+  domain    CDATA   #IMPLIED
+  >
+<!ELEMENT line-lockout-tone (step)* >
+<!ATTLIST line-lockout-tone
+  domain    CDATA   #IMPLIED
+  >
+<!ELEMENT permanent-signal-tone (step)* >
+<!ATTLIST permanent-signal-tone
+  domain    CDATA   #IMPLIED
+  >
+<!ELEMENT identification-tone (step)* >
+<!ATTLIST identification-tone
+  domain    CDATA   #IMPLIED
+  >
+
+<!ELEMENT step (step|EMPTY)* >
+<!ATTLIST step
+  cycles                CDATA   #IMPLIED
+  freq                  CDATA   #IMPLIED
+  level                 CDATA   #IMPLIED
+  length                CDATA   #IMPLIED
+  recorded-announcement CDATA   #IMPLIED
+  >

Added: freeswitch/trunk/libs/spandsp/spandsp/tsb85.xml
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/spandsp/tsb85.xml	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,5881 @@
+<?xml version="1.0"?>
+<fax-tests>
+<!-- $Id: tsb85.xml,v 1.15 2008/08/05 16:01:13 steveu Exp $ -->
+<messages>
+    <!-- TCF = 2700 bytes at 14400, 2250 at 12000, 1800 at 9600, 1350 at 7200, 900 at 4800 or 450 at 2400 -->
+    <!-- Bad TCF == 10101010.... -->
+    <!-- slow HDLC preamble == 37 flag bytes -->
+    <!-- slow HDLC inter-frame flag sequence == 1 flag byte -->
+    <!-- slow HDLC end flag sequence == 5 flag bytes -->
+    <!-- synchronisation sequence == 250ms of zeros. = 450 bytes at 14400, 375 at 12000, 300 at 9600, 225 at 7200, 150 at 4800 or 75 at 2400 -->
+    <!-- fast HDLC inter-frame flag sequence == 1 flag byte -->
+    <!-- fast HDLC end flag sequence == 10 flag bytes -->
+    <message type="CSI" fif="30 31 32 33 34 35 36 37 38 39 2B 20 20 20 20 20 20 20 20 20"/>
+    <message type="TSI" fif="30 31 32 33 34 35 36 37 38 39 2B 20 20 20 20 20 20 20 20 20"/>
+    <message type="CIG" fif="30 31 32 33 34 35 36 37 38 39 2B 20 20 20 20 20 20 20 20 20"/>
+    <message type="PWD" fif="30 31 32 33 34 35 36 37 38 39 2B 20 20 20 20 20 20 20 20 20"/>
+    <message type="SUB" fif="30 31 32 33 34 35 36 37 38 39 2B 20 20 20 20 20 20 20 20 20"/>
+    <message type="SEP" fif="30 31 32 33 34 35 36 37 38 39 2B 20 20 20 20 20 20 20 20 20"/>
+    <message type="NSF" fif="B5 FF FF"/>
+    <message type="NSF" fif="B5 FF FF"/>
+    <message type="NSF" fif="B5 FF FF"/>
+</messages>
+<test-group name="Annex A - Receive test procedures">
+    <test name="MRGN01">
+        <step type="CALL"/>
+
+        <!--<step dir="T" type="CNG"/>-->
+
+        <step dir="R" type="CED">
+            <check name="MRGN01M01" desc="receipt of CED"/>
+            <check name="MRGN01M02" desc="CED is 2.6s to 4s"/>
+        </step>
+        <!--<step dir="R" type="NSF/CSI/DIS">-->
+        <step dir="R" type="HDLC" modem="V.21" tag="DIS" value="FF C8 01 ..." timeout="60000">
+            <check name="MRGN01M03" desc="receipt of DIS"/>
+            <check name="MRGN01M04" desc="CED to DIS silence is 55ms to 95ms"/>
+            <check name="MRGN01M05" desc="preamble is 850ms to 1150ms"/>
+            <check name="MRGN01M06" desc="CSI received"/>
+            <check name="MRGN01M07" desc="NSF and CSI framing"/>
+            <check name="MRGN01M08" desc="CSI FIF content"/>
+            <check name="MRGN01M09" desc="DIS framing"/>
+            <check name="MRGN01M10" desc="DIS FIF content"/>
+        </step>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="TCF" modem="V.27ter/4800" value="900"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="CFR" value="FF C8 21">
+            <check name="MRGN01M11" desc="Respond CFR to DCS TCF"/>
+            <check name="MRGN01M12" desc="CFR preamble"/>
+            <check name="MRGN01M13" desc="CFR framing"/>
+        </step>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="MSG" modem="V.27ter/4800" value="a4-white.tif"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MPS" value="FF C8 72"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="MCF" value="FF C8 31">
+            <check name="MRGN01M14" desc="Respond MCF to MPS"/>
+        </step>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="MSG" modem="V.27ter/4800" value="a4-white.tif"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="EOP" value="FF C8 74"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="MCF" value="FF C8 31">
+            <check name="MRGN01M15" desc="MCF preamble"/>
+            <check name="MRGN01M16" desc="MCF framing"/>
+            <check name="MRGN01M17" desc="Respond MCF to EOP"/>
+        </step>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCN" value="FF C8 5F">
+            <check name="MRGN01M18" desc="Disconnect after receiving DCN"/>
+        </step>
+        <step dir="T" type="POSTAMBLE"/>
+    </test>
+    <test name="MRGN02">
+        <step type="CALL"/>
+
+        <!--<step dir="T" type="CNG"/>-->
+
+        <step dir="R" type="CED"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="DIS" value="FF C8 01 ..." timeout="60000"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="TCF" modem="V.27ter/4800" value="900"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="CFR" value="FF C8 21"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="MSG" modem="V.27ter/4800" value="a4-diago1.tif">
+            <check name="MRGN02M01" desc="decoding of all A4 one dimensional encoding codes"/>
+        </step>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MPS" value="FF C8 72"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="MCF" value="FF C8 31"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="MSG" modem="V.27ter/4800" value="a4-diago2.tif">
+            <check name="MRGN02M01" desc="decoding of all A4 one dimensional encoding codes"/>
+        </step>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="EOP" value="FF C8 74"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="MCF" value="FF C8 31"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCN" value="FF C8 5F"/>
+        <step dir="T" type="POSTAMBLE"/>
+    </test>
+    <test name="MRGN03">
+        <step type="CALL"/>
+
+        <!--<step dir="T" type="CNG"/>-->
+
+        <step dir="R" type="CED"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="DIS" value="FF C8 01 ..." timeout="60000"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="TCF" modem="V.27ter/4800" value="900"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="CFR" value="FF C8 21"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="MSG" modem="V.27ter/4800" value="a4-duration1.tif">
+            <check name="MRGN02M01" desc="Accept 4.9s long scan line"/>
+        </step>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MPS" value="FF C8 72"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="MCF" value="FF C8 31"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="MSG" modem="V.27ter/4800" value="a4-duration2.tif">
+            <check name="MRGN02M02" desc="Reject 5.1s long scan line"/>
+        </step>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="EOP" value="FF C8 74"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step type="WAIT" value="6000"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="EOP" value="FF C8 74"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step type="WAIT" value="6000"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="EOP" value="FF C8 74"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step type="WAIT" value="6000"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCN" value="FF C8 5F"/>
+        <step dir="T" type="POSTAMBLE"/>
+    </test>
+    <test name="MRGN04">
+        <step type="CALL"/>
+
+        <!--<step dir="T" type="CNG"/>-->
+
+        <step dir="R" type="CED"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="DIS" value="FF C8 01 ..." timeout="60000"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="TCF" modem="V.27ter/4800" value="900"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="CFR" value="FF C8 21"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="MSG" modem="V.27ter/4800" value="a4-error.tif" bad_rows="
+ 79,  85, 105, 111, 133, 137, 157, 163, 183, 189, 209, 215, 235, 248, 261, 267,
+287, 293, 313"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MPS" value="FF C8 72"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="MCF or RTP" value="FF C8 31" xxx="FF C8 33">
+            <check name="MRGN02M01" desc="Accept 4.9% of scan lines in error"/>
+        </step>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="MSG" modem="V.27ter/4800" value="a4-error.tif" bad_rows="
+ 79,  85, 105, 111, 133, 137, 157, 163, 183, 189, 209, 215, 235, 248, 261, 267,
+287, 293, 313,  73,  76,  88,  91,  94,  99, 102, 117, 120, 125, 128, 143, 146,
+151, 154, 169, 172, 177, 180, 195, 198, 203, 206, 221, 224, 229, 232, 247, 250,
+255, 258, 273, 276, 281, 284, 299, 302, 307, 310, 319, 325, 328"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="EOP" value="FF C8 74"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="RTN" value="FF C8 32">
+            <check name="MRGN02M01" desc="Reject 15.1% of scan lines in error"/>
+        </step>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCN" value="FF C8 5F"/>
+        <step dir="T" type="POSTAMBLE"/>
+    </test>
+    <test name="MRGN05">
+        <step type="CALL"/>
+
+        <!--<step dir="T" type="CNG"/>-->
+
+        <step dir="R" type="CED"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="DIS" value="FF C8 01 ..." timeout="60000"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="TCF" modem="V.27ter/4800" value="900"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="CFR" value="FF C8 21"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="MSG" modem="V.27ter/4800" value="a4-impress.tif"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="EOP" value="FF C8 74"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="MCF" value="FF C8 31">
+            <check name="MRGN05M01" desc="Check IMPRESS picture orientation"/>
+            <check name="MRGN05M02" desc="Check IMPRESS area 9 - horizontal intensity"/>
+            <check name="MRGN05M03" desc="Check IMPRESS area 2 - horizontal alignment"/>
+            <check name="MRGN05M04" desc="Check IMPRESS area 1 - vertical alignment"/>
+            <check name="MRGN05M05" desc="Check IMPRESS area 16 - vertical density"/>
+            <check name="MRGN05M06" desc="Check IMPRESS area 4 - odd alternate pixels black"/>
+            <check name="MRGN05M07" desc="Check IMPRESS area 5 - even alternate pixels black"/>
+            <check name="MRGN05M08" desc="Check IMPRESS area 7 - alternate lines black"/>
+            <check name="MRGN05M09" desc="Check IMPRESS area 11 - isolated black pixels"/>
+            <check name="MRGN05M10" desc="Check IMPRESS area 13 - white cross on black"/>
+            <check name="MRGN05M11" desc="Check IMPRESS area 14 - black cross on white"/>
+            <check name="MRGN05M12" desc="Check IMPRESS area 15 - vertical one pixel wide line"/>
+        </step>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCN" value="FF C8 5F"/>
+        <step dir="T" type="POSTAMBLE"/>
+    </test>
+    <test name="MRGN06a">
+        <step type="CALL"/>
+
+        <!--<step dir="T" type="CNG"/>-->
+
+        <step dir="R" type="CED"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="DIS" value="FF C8 01 ..." timeout="60000"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="55"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCS" value="FF C8 41 00 50 00">
+            <check name="MRGN06M01" desc="Low side of acceptable DIS to DCS limit as receiver (55ms)"/>
+            <check name="MRGN06M03" desc="Accept DCS with 8 byte FIF"/>
+        </step>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="TCF" modem="V.27ter/4800" value="810"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="CFR" value="FF C8 21"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="55"/>
+        <step dir="T" type="MSG" modem="V.27ter/4800" value="a4-white.tif"/>
+        <step type="WAIT" value="55"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MPS" value="FF C8 72"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="MCF" value="FF C8 31"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="55"/>
+        <step dir="T" type="MSG" modem="V.27ter/4800" value="a4-white.tif"/>
+        <step type="WAIT" value="55"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="EOP" value="FF C8 74"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="MCF" value="FF C8 31"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCN" value="FF C8 5F"/>
+        <step dir="T" type="POSTAMBLE"/>
+    </test>
+    <test name="MRGN06b">
+        <step type="CALL"/>
+
+        <!--<step dir="T" type="CNG"/>-->
+
+        <step dir="R" type="CED"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="DIS" value="FF C8 01 ..." timeout="60000"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="2550"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCS" value="FF C8 41 00 50 00">
+            <check name="MRGN06M02" desc="High side of acceptable DIS to DCS limit as receiver (2550ms)"/>
+            <check name="MRGN06M03" desc="Accept DCS with 8 byte FIF"/>
+        </step>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="95"/>
+        <step dir="T" type="TCF" modem="V.27ter/4800" value="990"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="CFR" value="FF C8 21"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="5000"/>
+        <step dir="T" type="MSG" modem="V.27ter/4800" value="a4-white.tif"/>
+        <step type="WAIT" value="5000"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MPS" value="FF C8 72"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="MCF" value="FF C8 31"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="5000"/>
+        <step dir="T" type="MSG" modem="V.27ter/4800" value="a4-white.tif"/>
+        <step type="WAIT" value="5000"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="EOP" value="FF C8 74"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="MCF" value="FF C8 31"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCN" value="FF C8 5F"/>
+        <step dir="T" type="POSTAMBLE"/>
+    </test>
+    <test name="MRGN07">
+        <step type="CALL"/>
+
+        <!--<step dir="T" type="CNG"/>-->
+
+        <step dir="R" type="CED"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="DIS" value="FF C8 01 ..." timeout="60000"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="TCF" modem="V.27ter/4800" value="900" pattern="900"/> <!-- 01010 for the whole TCF -->
+
+        <step dir="R" type="HDLC" modem="V.21" tag="FTT" value="FF C8 22">
+            <check name="MRGN07M01" desc="FTT returned for corrupted TCF pattern"/>
+        </step>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="TCF" modem="V.27ter/4800" value="900" pattern="7"/> <!-- 01010 for 50 bits, and then zero for the test of the TCF -->
+
+        <step dir="R" type="HDLC" modem="V.21" tag="CFR" value="FF C8 21"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="MSG" modem="V.27ter/4800" value="a4-white.tif"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="EOP" value="FF C8 74"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="MCF" value="FF C8 31"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCN" value="FF C8 5F"/>
+        <step dir="T" type="POSTAMBLE"/>
+    </test>
+    <test name="MRGN08">
+        <step type="CALL"/>
+
+        <!--<step dir="T" type="CNG"/>-->
+
+        <step dir="R" type="CED"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="DIS" value="FF C8 01 ..." timeout="60000"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="TCF" modem="V.27ter/2400" value="450"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="FTT" value="FF C8 22">
+            <check name="MRGN07M01" desc="FTT returned for corrupted TCF pattern"/>
+            <check name="MRGN07M02" desc="FTT preamble between 850ms and 1150ms"/>
+            <check name="MRGN07M03" desc="FTT is 0xFF, 0xC8, FCF with MSB of zero, and no FIF bytes"/>
+        </step>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="TCF" modem="V.27ter/4800" value="900"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="CFR" value="FF C8 21">
+            <check name="MRGN07M04" desc="Accept TCF"/>
+        </step>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="MSG" modem="V.27ter/4800" value="a4-white.tif"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="EOP" value="FF C8 74"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="MCF" value="FF C8 31"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCN" value="FF C8 5F"/>
+        <step dir="T" type="POSTAMBLE"/>
+    </test>
+    <test name="MRGN09">
+        <step type="CALL" value="a4-white.tif"/>
+
+        <step dir="R" type="CED"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="DIS" value="FF C8 01 ..." timeout="60000"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="TCF" modem="V.27ter/4800" value="900"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="CFR" value="FF C8 21"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="MSG" modem="V.27ter/4800" value="a4-white.tif"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="EOM" value="FF C8 71"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="MCF" value="FF C8 31"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DIS" value="FF C8 01 ..." timeout="60000">
+            <check name="MRGN09M01" desc="Device goes to phase B after receiving EOM"/>
+            <check name="MRGN09M02" desc="T2 elapses after MCF response to EOM. MCF to DIS interval must be between 5.0s and 7.0s"/>
+        </step>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="TCF" modem="V.27ter/4800" value="900"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="CFR" value="FF C8 21"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="MSG" modem="V.27ter/4800" value="a4-white.tif"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="EOP" value="FF C8 74"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="MCF" value="FF C8 31"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCN" value="FF C8 5F"/>
+        <step dir="T" type="POSTAMBLE"/>
+    </test>
+    <test name="MRGN10">
+        <step type="CALL"/>
+
+        <!--<step dir="T" type="CNG"/>-->
+
+        <step dir="R" type="CED"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="DIS" value="FF C8 01 ..."/>
+        <step dir="R" type="HDLC" modem="V.21" tag="DIS" value="FF C8 01 ..."/>
+        <step dir="R" type="HDLC" modem="V.21" tag="DIS" value="FF C8 01 ...">
+            <check name="MRGN10M01" desc="Two non-responses to DIS"/>
+        </step>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="TCF" modem="V.27ter/4800" value="900"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="CFR" value="FF C8 21"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="MSG" modem="V.27ter/4800" value="a4-white.tif"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="EOP" value="FF C8 74"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="MCF" value="FF C8 31"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCN" value="FF C8 5F"/>
+        <step dir="T" type="POSTAMBLE"/>
+    </test>
+    <test name="MRGN11">
+        <step type="CALL"/>
+
+        <!--<step dir="T" type="CNG"/>-->
+
+        <step dir="R" type="CED"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="DIS" value="FF C8 01 ..." timeout="60000"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCS" value="FF C8 41 00 50 00" crc_error="0"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="TCF" modem="V.27ter/4800" value="900"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DIS" value="FF C8 01 ..."/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCS" value="FF C8 41 00 50 00" crc_error="0"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="TCF" modem="V.27ter/4800" value="900"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DIS" value="FF C8 01 ...">
+            <check name="MRGN11M01" desc="Two bad FCS responses to first DIS"/>
+        </step>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="TCF" modem="V.27ter/4800" value="900"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="CFR" value="FF C8 21"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="MSG" modem="V.27ter/4800" value="a4-white.tif"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="EOP" value="FF C8 74"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="MCF" value="FF C8 31"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCN" value="FF C8 5F"/>
+        <step dir="T" type="POSTAMBLE"/>
+    </test>
+    <test name="MRGN12">
+        <step type="CALL"/>
+
+        <!--<step dir="T" type="CNG"/>-->
+
+        <step dir="R" type="CED"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="DIS" value="FF C8 01 ..." timeout="60000"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="CRP" value="FF C8 58"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DIS" value="FF C8 01 ..." timeout="60000"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="CRP" value="FF C8 58">
+            <check name="MRGN12M01" desc="Two CRP responses to first DIS"/>
+        </step>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DIS" value="FF C8 01 ..." timeout="60000"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="TCF" modem="V.27ter/4800" value="900"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="CFR" value="FF C8 21"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="MSG" modem="V.27ter/4800" value="a4-white.tif"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="EOP" value="FF C8 74"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="MCF" value="FF C8 31"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCN" value="FF C8 5F"/>
+        <step dir="T" type="POSTAMBLE"/>
+    </test>
+    <test name="MRGN13">
+        <step type="CALL"/>
+
+        <!--<step dir="T" type="CNG"/>-->
+
+        <step dir="R" type="CED"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="DIS" value="FF C8 01 ..." timeout="60000"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="FTT" value="FF C8 22" timeout="9000"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="FTT" value="FF C8 22" timeout="9000">
+            <check name="MRGN13M01" desc="Two DCS lacking TCF responses to first DIS"/>
+        </step>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="TCF" modem="V.27ter/4800" value="900"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="CFR" value="FF C8 21"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="MSG" modem="V.27ter/4800" value="a4-white.tif"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="EOP" value="FF C8 74"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="MCF" value="FF C8 31"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCN" value="FF C8 5F"/>
+        <step dir="T" type="POSTAMBLE"/>
+    </test>
+    <test name="MRGN14">
+        <step type="CALL"/>
+
+        <!--<step dir="T" type="CNG"/>-->
+
+        <step dir="R" type="CED"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="DIS" value="FF C8 01 ..." timeout="60000"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="TCF" modem="V.27ter/4800" value="900"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="CFR" value="FF C8 21"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCS" value="FF C8 41 00 50 00" crc_error="0"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="TCF" modem="V.27ter/4800" value="900"/>
+
+        <!-- dir="R" type="HDLC" modem="V.21" tag="CRP" value="FF C8 58 | nothing"> -->
+        <step type="WAIT" value="5000">
+            <check name="MRGN14M01" desc="One bad FCS DCS command after CFR"/>
+        </step>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="TCF" modem="V.27ter/4800" value="900"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="CFR" value="FF C8 21"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="MSG" modem="V.27ter/4800" value="a4-white.tif"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="EOP" value="FF C8 74"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="MCF" value="FF C8 31"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCN" value="FF C8 5F"/>
+        <step dir="T" type="POSTAMBLE"/>
+    </test>
+    <test name="MRGN15">
+        <step type="CALL"/>
+
+        <!--<step dir="T" type="CNG"/>-->
+
+        <step dir="R" type="CED"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="DIS" value="FF C8 01 ..." timeout="60000"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="TCF" modem="V.27ter/4800" value="900"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="CFR" value="FF C8 21"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="FTT" value="FF C8 22" timeout="10000">
+            <check name="MRGN15M01" desc="One DCS lacking TCF command after CFR"/>
+        </step>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="TCF" modem="V.27ter/4800" value="900"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="CFR" value="FF C8 21"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="MSG" modem="V.27ter/4800" value="a4-white.tif"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="EOP" value="FF C8 74"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="MCF" value="FF C8 31"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCN" value="FF C8 5F"/>
+        <step dir="T" type="POSTAMBLE"/>
+    </test>
+    <test name="MRGN16">
+        <step type="CALL"/>
+
+        <!--<step dir="T" type="CNG"/>-->
+
+        <step dir="R" type="CED"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="DIS" value="FF C8 01 ..." timeout="60000"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="TCF" modem="V.27ter/4800" value="900"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="CFR" value="FF C8 21"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="MSG" modem="V.27ter/4800" value="a4-white.tif"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="EOM" value="FF C8 71"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="MCF" value="FF C8 31"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="EOM" value="FF C8 71"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="MCF" value="FF C8 31"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="EOM" value="FF C8 71">
+            <check name="MRGN16M01" desc="Repeated EOM commands"/>
+        </step>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="MCF" value="FF C8 31"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DIS" value="FF C8 01 ..." timeout="60000"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="TCF" modem="V.27ter/4800" value="900"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="CFR" value="FF C8 21"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="MSG" modem="V.27ter/4800" value="a4-white.tif"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MPS" value="FF C8 72"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="MCF" value="FF C8 31"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MPS" value="FF C8 72"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="MCF" value="FF C8 31"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MPS" value="FF C8 72">
+            <check name="MRGN16M02" desc="Repeated MPS commands"/>
+        </step>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="MCF" value="FF C8 31"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="MSG" modem="V.27ter/4800" value="a4-white.tif"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="EOP" value="FF C8 74"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="MCF" value="FF C8 31"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="EOP" value="FF C8 74">
+            <check name="MRGN16M03" desc="Repeated EOP commands"/>
+        </step>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="MCF" value="FF C8 31"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCN" value="FF C8 5F"/>
+        <step dir="T" type="POSTAMBLE"/>
+    </test>
+    <test name="MRGN17">
+        <step type="CALL"/>
+
+        <!--<step dir="T" type="CNG"/>-->
+
+        <step dir="R" type="CED"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="DIS" value="FF C8 01 ..." timeout="60000"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="TCF" modem="V.27ter/4800" value="900"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="CFR" value="FF C8 21"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="MSG" modem="V.27ter/4800" value="a4-white.tif"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="EOM" value="FF C8 71" crc_error="0"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <!-- <step dir="R" type="HDLC" modem="V.21" tag="CRP" value="FF C8 58 | nothing" timeout="3450"/> -->
+        <step type="WAIT" value="3450"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="EOM" value="FF C8 71" crc_error="0"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <!-- <step dir="R" type="HDLC" modem="V.21" tag="CRP" value="FF C8 58 | nothing" timeout="3450"/> -->
+        <step type="WAIT" value="3450"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="EOM" value="FF C8 71"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="MCF" value="FF C8 31">
+            <check name="MRGN17M01" desc="Do not respond to bad EOM commands"/>
+        </step>
+        <step dir="R" type="SILENCE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DIS" value="FF C8 01 ..." timeout="10000"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="TCF" modem="V.27ter/4800" value="900"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="CFR" value="FF C8 21"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="MSG" modem="V.27ter/4800" value="a4-white.tif"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MPS" value="FF C8 72" crc_error="0"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <!-- <step dir="R" type="HDLC" modem="V.21" tag="CRP" value="FF C8 58 | nothing" timeout="6000"/> -->
+        <step type="WAIT" value="6000"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MPS" value="FF C8 72" crc_error="0"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <!-- <step dir="R" type="HDLC" modem="V.21" tag="CRP" value="FF C8 58 | nothing" timeout="6000"/> -->
+        <step type="WAIT" value="6000"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MPS" value="FF C8 72"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="MCF" value="FF C8 31">
+            <check name="MRGN17M02" desc="Do not respond to bad MPS commands"/>
+        </step>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="MSG" modem="V.27ter/4800" value="a4-white.tif"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="EOP" value="FF C8 74" crc_error="0"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <!-- <step dir="R" type="HDLC" modem="V.21" tag="CRP" value="FF C8 58 | nothing" timeout="3450"/> -->
+        <step type="WAIT" value="3450"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="EOP" value="FF C8 74" crc_error="0"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <!-- <step dir="R" type="HDLC" modem="V.21" tag="CRP" value="FF C8 58 | nothing" timeout="3450"/> -->
+        <step type="WAIT" value="3450"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="EOP" value="FF C8 74"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="MCF" value="FF C8 31">
+            <check name="MRGN17M03" desc="Do not respond to bad EOP commands"/>
+        </step>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCN" value="FF C8 5F"/>
+        <step dir="T" type="POSTAMBLE"/>
+    </test>
+</test-group>
+<test-group name="Annex B - Capability receive test procedures">
+    <test name="ORGC01">
+        <step type="CALL"/>
+
+        <!--<step dir="T" type="CNG"/>-->
+
+        <step dir="R" type="CED"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="DIS" value="FF C8 01 ..." timeout="60000"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="TCF" modem="V.27ter/4800" value="900"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="CFR" value="FF C8 21"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="MSG" modem="V.27ter/4800" value="a4-diago1.tif"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MPS" value="FF C8 72"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="MCF" value="FF C8 31"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="MSG" modem="V.27ter/4800" value="a4-diago2.tif"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="EOP" value="FF C8 74"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="MCF" value="FF C8 31"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCN" value="FF C8 5F"/>
+        <step dir="T" type="POSTAMBLE"/>
+    </test>
+    <test name="ORGC02">
+        <step type="CALL"/>
+
+        <!--<step dir="T" type="CNG"/>-->
+
+        <step dir="R" type="CED"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="DIS" value="FF C8 01 ..." timeout="60000"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="TCF" modem="V.27ter/4800" value="900"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="CFR" value="FF C8 21"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="MSG" modem="V.27ter/4800" value="a4-white.tif"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MPS" value="FF C8 72"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="MCF" value="FF C8 31"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="MSG" modem="V.27ter/4800" value="a4-white.tif"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="EOP" value="FF C8 74"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="MCF" value="FF C8 31"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCN" value="FF C8 5F"/>
+        <step dir="T" type="POSTAMBLE"/>
+    </test>
+    <test name="ORGC03">
+        <step type="CALL"/>
+
+        <!--<step dir="T" type="CNG"/>-->
+
+        <step dir="R" type="CED"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="DIS" value="FF C8 01 ..." timeout="60000"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="TCF" modem="V.27ter/4800" value="900"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="CFR" value="FF C8 21"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="MSG" modem="V.27ter/4800" value="a4-impress.tif"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="EOP" value="FF C8 74"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="MCF" value="FF C8 31"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCN" value="FF C8 5F"/>
+        <step dir="T" type="POSTAMBLE"/>
+    </test>
+</test-group>
+<test-group name="Annex C - ECM receive test procedures">
+    <test name="OREN01">
+        <step type="CALL"/>
+
+        <!--<step dir="T" type="CNG"/>-->
+
+        <step dir="R" type="CED"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="DIS" value="FF C8 01 ..." timeout="60000"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCS" value="FF C8 41 00 50 1F 30"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="TCF" modem="V.27ter/4800" value="900"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="CFR" value="FF C8 21"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.27ter/4800"/>
+        <step dir="T" type="PP" value="a4-white.tif"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="PPS-NULL" value="FF C8 7D 00 00 00 08"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="MCF" value="FF C8 31"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.27ter/4800"/>
+        <step dir="T" type="PP" value="a4-white.tif"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="PPS-MPS" value="FF C8 7D 72 00 00 08"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <possible-step>
+            <step dir="R" type="HDLC" modem="V.21" tag="RNR" value="FF C8 37"/>
+            <step dir="R" type="SILENCE"/>
+
+            <step type="WAIT" value="75"/>
+            <step dir="T" type="PREAMBLE" modem="V.21"/>
+            <step dir="T" type="HDLC" tag="RR" value="FF C8 76"/>
+            <step dir="T" type="POSTAMBLE"/>
+        </possible-step>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="MCF" value="FF C8 31"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.27ter/4800"/>
+        <step dir="T" type="PP" value="a4-white.tif"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="PPS-MPS" value="FF C8 7D 72 00 00 08"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <possible-step>
+            <step dir="R" type="HDLC" modem="V.21" tag="RNR" value="FF C8 37"/>
+            <step dir="R" type="SILENCE"/>
+
+            <step type="WAIT" value="75"/>
+            <step dir="T" type="PREAMBLE" modem="V.21"/>
+            <step dir="T" type="HDLC" tag="RR" value="FF C8 76"/>
+            <step dir="T" type="POSTAMBLE"/>
+        </possible-step>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="MCF" value="FF C8 31"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCN" value="FF C8 5F"/>
+        <step dir="T" type="POSTAMBLE"/>
+    </test>
+    <test name="OREN02">
+        <step type="CALL"/>
+
+        <!--<step dir="T" type="CNG"/>-->
+
+        <step dir="R" type="CED"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="DIS" value="FF C8 01 ..." timeout="60000"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCS" value="FF C8 41 00 44 0F 30"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="TCF" modem="V.17/14400" value="2700"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="CFR" value="FF C8 21"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.17/14400"/>
+        <step dir="T" type="PP" value="a4-stairstep.tif" crc_error="0"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="PPS-NULL" value="FF C8 7D 00 00 00 6B"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="PPR" value="FF C8 3D 80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 FF FF FF FF FF"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.17/14400"/>
+        <step dir="T" type="PP" value="a4-stairstep.tif"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="PPS-NULL" value="FF C8 7D 00 00 00 6B"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="MCF" value="FF C8 31"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.17/14400"/>
+        <step dir="T" type="PP" value="a4-stairstep.tif" crc_error="100"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="PPS-MPS" value="FF C8 7D 72 00 80 6B"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="PPR" value="FF C8 3D 00 00 00 00 00 00 00 00 00 00 00 00 08 00 00 00 00 00 00 00 00 00 00 00 00 00 01 FF FF FF FF FF"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.17/14400"/>
+        <step dir="T" type="PP" value="a4-stairstep.tif"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="PPS-MPS" value="FF C8 7D 72 00 80 6B"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="MCF" value="FF C8 31"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.17/14400"/>
+        <step dir="T" type="PP" value="a4-stairstep.tif" crc_error="214"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="PPS-MPS" value="FF C8 7D 72 80 00 6B"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="PPR" value="FF C8 3D 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 03 FF FF FF FF FF"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.17/14400"/>
+        <step dir="T" type="PP" value="a4-stairstep.tif"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="PPS-MPS" value="FF C8 7D 72 80 00 6B"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <possible-step>
+            <step dir="R" type="HDLC" modem="V.21" tag="RNR" value="FF C8 37"/>
+            <step dir="R" type="SILENCE"/>
+
+            <step type="WAIT" value="75"/>
+            <step dir="T" type="PREAMBLE" modem="V.21"/>
+            <step dir="T" type="HDLC" tag="RR" value="FF C8 76"/>
+            <step dir="T" type="POSTAMBLE"/>
+        </possible-step>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="MCF" value="FF C8 31"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.17/14400"/>
+        <step dir="T" type="PP" value="a4-stairstep.tif" crc_error="0"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="PPS-EOP" value="FF C8 7D 74 40 00 6B"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="PPR" value="FF C8 3D 80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 FF FF FF FF FF"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.17/14400"/>
+        <step dir="T" type="PP" value="a4-stairstep.tif"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="PPS-EOP" value="FF C8 7D 74 40 00 6B"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <possible-step>
+            <step dir="R" type="HDLC" modem="V.21" tag="RNR" value="FF C8 37"/>
+            <step dir="R" type="SILENCE"/>
+
+            <step type="WAIT" value="75"/>
+            <step dir="T" type="PREAMBLE" modem="V.21"/>
+            <step dir="T" type="HDLC" tag="RR" value="FF C8 76"/>
+            <step dir="T" type="POSTAMBLE"/>
+        </possible-step>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="MCF" value="FF C8 31"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCN" value="FF C8 5F"/>
+        <step dir="T" type="POSTAMBLE"/>
+    </test>
+    <test name="OREN03">
+        <step type="CALL"/>
+
+        <!--<step dir="T" type="CNG"/>-->
+
+        <step dir="R" type="CED"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="DIS" value="FF C8 01 ..." timeout="60000"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCS" value="FF C8 41 00 50 1F 30"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="TCF" modem="V.27ter/4800" value="900"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="CFR" value="FF C8 21"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.27ter/4800"/>
+        <step dir="T" type="PP" value="a4-stairstep.tif" crc_error="0"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="PPS-EOP" value="FF C8 7D 74 00 00 10"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <possible-step>
+            <step dir="R" type="HDLC" modem="V.21" tag="RNR" value="FF C8 37"/>
+            <step dir="R" type="SILENCE"/>
+
+            <step type="WAIT" value="75"/>
+            <step dir="T" type="PREAMBLE" modem="V.21"/>
+            <step dir="T" type="HDLC" tag="RR" value="FF C8 76"/>
+            <step dir="T" type="POSTAMBLE"/>
+        </possible-step>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="PPR" value="FF C8 3D 80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 FF FF FF FF FF"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.27ter/4800"/>
+        <step dir="T" type="PP" value="a4-stairstep.tif" crc_error="0"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="PPS-EOP" value="FF C8 7D 74 00 00 10"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <possible-step>
+            <step dir="R" type="HDLC" modem="V.21" tag="RNR" value="FF C8 37"/>
+            <step dir="R" type="SILENCE"/>
+
+            <step type="WAIT" value="75"/>
+            <step dir="T" type="PREAMBLE" modem="V.21"/>
+            <step dir="T" type="HDLC" tag="RR" value="FF C8 76"/>
+            <step dir="T" type="POSTAMBLE"/>
+        </possible-step>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="PPR" value="FF C8 3D 80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 FF FF FF FF FF"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.27ter/4800"/>
+        <step dir="T" type="PP" value="a4-stairstep.tif" crc_error="0"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="PPS-EOP" value="FF C8 7D 74 00 00 10"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <possible-step>
+            <step dir="R" type="HDLC" modem="V.21" tag="RNR" value="FF C8 37"/>
+            <step dir="R" type="SILENCE"/>
+
+            <step type="WAIT" value="75"/>
+            <step dir="T" type="PREAMBLE" modem="V.21"/>
+            <step dir="T" type="HDLC" tag="RR" value="FF C8 76"/>
+            <step dir="T" type="POSTAMBLE"/>
+        </possible-step>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="PPR" value="FF C8 3D 80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 FF FF FF FF FF"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.27ter/4800"/>
+        <step dir="T" type="PP" value="a4-stairstep.tif" crc_error="0"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="PPS-EOP" value="FF C8 7D 74 00 00 10"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <possible-step>
+            <step dir="R" type="HDLC" modem="V.21" tag="RNR" value="FF C8 37"/>
+            <step dir="R" type="SILENCE"/>
+
+            <step type="WAIT" value="75"/>
+            <step dir="T" type="PREAMBLE" modem="V.21"/>
+            <step dir="T" type="HDLC" tag="RR" value="FF C8 76"/>
+            <step dir="T" type="POSTAMBLE"/>
+        </possible-step>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="PPR" value="FF C8 3D 80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 FF FF FF FF FF"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="CTC" value="FF C8 48"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="CTR" value="FF C8 23"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.27ter/4800"/>
+        <step dir="T" type="PP" value="a4-stairstep.tif"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="PPS-EOP" value="FF C8 7D 74 00 00 6B"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <possible-step>
+            <step dir="R" type="HDLC" modem="V.21" tag="RNR" value="FF C8 37"/>
+            <step dir="R" type="SILENCE"/>
+
+            <step type="WAIT" value="75"/>
+            <step dir="T" type="PREAMBLE" modem="V.21"/>
+            <step dir="T" type="HDLC" tag="RR" value="FF C8 76"/>
+            <step dir="T" type="POSTAMBLE"/>
+        </possible-step>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="MCF" value="FF C8 31"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCN" value="FF C8 5F"/>
+        <step dir="T" type="POSTAMBLE"/>
+    </test>
+    <test name="OREN04">
+        <step type="CALL"/>
+
+        <!--<step dir="T" type="CNG"/>-->
+
+        <step dir="R" type="CED"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="DIS" value="FF C8 01 ..." timeout="60000"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCS" value="FF C8 41 00 50 1F 30"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="TCF" modem="V.27ter/4800" value="900"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="CFR" value="FF C8 21"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.27ter/4800"/>
+        <step dir="T" type="PP" value="a4-stairstep.tif" crc_error="0"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="PPS-NULL" value="FF C8 7D 00 00 00 6B"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <possible-step>
+            <step dir="R" type="HDLC" modem="V.21" tag="RNR" value="FF C8 37"/>
+            <step dir="R" type="SILENCE"/>
+
+            <step type="WAIT" value="75"/>
+            <step dir="T" type="PREAMBLE" modem="V.21"/>
+            <step dir="T" type="HDLC" tag="RR" value="FF C8 76"/>
+            <step dir="T" type="POSTAMBLE"/>
+        </possible-step>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="PPR" value="FF C8 3D 80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 FF FF FF FF FF"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.27ter/4800"/>
+        <step dir="T" type="PP" value="a4-stairstep.tif" crc_error="0"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="PPS-NULL" value="FF C8 7D 00 00 00 6B"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <possible-step>
+            <step dir="R" type="HDLC" modem="V.21" tag="RNR" value="FF C8 37"/>
+            <step dir="R" type="SILENCE"/>
+
+            <step type="WAIT" value="75"/>
+            <step dir="T" type="PREAMBLE" modem="V.21"/>
+            <step dir="T" type="HDLC" tag="RR" value="FF C8 76"/>
+            <step dir="T" type="POSTAMBLE"/>
+        </possible-step>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="PPR" value="FF C8 3D 80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 FF FF FF FF FF"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.27ter/4800"/>
+        <step dir="T" type="PP" value="a4-stairstep.tif" crc_error="0"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="PPS-NULL" value="FF C8 7D 00 00 00 6B"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <possible-step>
+            <step dir="R" type="HDLC" modem="V.21" tag="RNR" value="FF C8 37"/>
+            <step dir="R" type="SILENCE"/>
+
+            <step type="WAIT" value="75"/>
+            <step dir="T" type="PREAMBLE" modem="V.21"/>
+            <step dir="T" type="HDLC" tag="RR" value="FF C8 76"/>
+            <step dir="T" type="POSTAMBLE"/>
+        </possible-step>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="PPR" value="FF C8 3D 80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 FF FF FF FF FF"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.27ter/4800"/>
+        <step dir="T" type="PP" value="a4-stairstep.tif" crc_error="0"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="PPS-NULL" value="FF C8 7D 00 00 00 6B"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <possible-step>
+            <step dir="R" type="HDLC" modem="V.21" tag="RNR" value="FF C8 37"/>
+            <step dir="R" type="SILENCE"/>
+
+            <step type="WAIT" value="75"/>
+            <step dir="T" type="PREAMBLE" modem="V.21"/>
+            <step dir="T" type="HDLC" tag="RR" value="FF C8 76"/>
+            <step dir="T" type="POSTAMBLE"/>
+        </possible-step>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="PPR" value="FF C8 3D 80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 FF FF FF FF FF"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="EOR-NULL" value="FF C8 73 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="ERR" value="FF C8 38"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.27ter/4800"/>
+        <step dir="T" type="PP" value="a4-stairstep.tif"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="PPS-EOP" value="FF C8 7D 74 00 00 6B"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <possible-step>
+            <step dir="R" type="HDLC" modem="V.21" tag="RNR" value="FF C8 37"/>
+            <step dir="R" type="SILENCE"/>
+
+            <step type="WAIT" value="75"/>
+            <step dir="T" type="PREAMBLE" modem="V.21"/>
+            <step dir="T" type="HDLC" tag="RR" value="FF C8 76"/>
+            <step dir="T" type="POSTAMBLE"/>
+        </possible-step>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="MCF" value="FF C8 31"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCN" value="FF C8 5F"/>
+        <step dir="T" type="POSTAMBLE"/>
+    </test>
+    <test name="OREN05">
+        <step type="CALL"/>
+
+        <!--<step dir="T" type="CNG"/>-->
+
+        <step dir="R" type="CED"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="DIS" value="FF C8 01 ..." timeout="60000"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCS" value="FF C8 41 00 50 1F 30"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="TCF" modem="V.27ter/4800" value="900"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="CFR" value="FF C8 21"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.27ter/4800"/>
+        <step dir="T" type="PP" value="a4-stairstep.tif"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="PPS-NULL" value="FF C8 7D 00 00 00 6B"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <possible-step>
+            <step dir="R" type="HDLC" modem="V.21" tag="RNR" value="FF C8 37"/>
+            <step dir="R" type="SILENCE"/>
+
+            <step type="WAIT" value="75"/>
+            <step dir="T" type="PREAMBLE" modem="V.21"/>
+            <step dir="T" type="HDLC" tag="RR" value="FF C8 76"/>
+            <step dir="T" type="POSTAMBLE"/>
+        </possible-step>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="MCF" value="FF C8 31"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.27ter/4800"/>
+        <step dir="T" type="PP" value="a4-stairstep.tif" crc_error="0"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="PPS-NULL" value="FF C8 7D 00 00 00 6B"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <possible-step>
+            <step dir="R" type="HDLC" modem="V.21" tag="RNR" value="FF C8 37"/>
+            <step dir="R" type="SILENCE"/>
+
+            <step type="WAIT" value="75"/>
+            <step dir="T" type="PREAMBLE" modem="V.21"/>
+            <step dir="T" type="HDLC" tag="RR" value="FF C8 76"/>
+            <step dir="T" type="POSTAMBLE"/>
+        </possible-step>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="PPR" value="FF C8 3D 80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 FF FF FF FF FF"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.27ter/4800"/>
+        <step dir="T" type="PP" value="a4-stairstep.tif" crc_error="0"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="PPS-NULL" value="FF C8 7D 00 00 00 6B"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <possible-step>
+            <step dir="R" type="HDLC" modem="V.21" tag="RNR" value="FF C8 37"/>
+            <step dir="R" type="SILENCE"/>
+
+            <step type="WAIT" value="75"/>
+            <step dir="T" type="PREAMBLE" modem="V.21"/>
+            <step dir="T" type="HDLC" tag="RR" value="FF C8 76"/>
+            <step dir="T" type="POSTAMBLE"/>
+        </possible-step>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="PPR" value="FF C8 3D 80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 FF FF FF FF FF"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.27ter/4800"/>
+        <step dir="T" type="PP" value="a4-stairstep.tif" crc_error="0"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="PPS-NULL" value="FF C8 7D 00 00 00 6B"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <possible-step>
+            <step dir="R" type="HDLC" modem="V.21" tag="RNR" value="FF C8 37"/>
+            <step dir="R" type="SILENCE"/>
+
+            <step type="WAIT" value="75"/>
+            <step dir="T" type="PREAMBLE" modem="V.21"/>
+            <step dir="T" type="HDLC" tag="RR" value="FF C8 76"/>
+            <step dir="T" type="POSTAMBLE"/>
+        </possible-step>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="PPR" value="FF C8 3D 80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 FF FF FF FF FF"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.27ter/4800"/>
+        <step dir="T" type="PP" value="a4-stairstep.tif" crc_error="0"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="PPS-NULL" value="FF C8 7D 00 00 00 6B"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <possible-step>
+            <step dir="R" type="HDLC" modem="V.21" tag="RNR" value="FF C8 37"/>
+            <step dir="R" type="SILENCE"/>
+
+            <step type="WAIT" value="75"/>
+            <step dir="T" type="PREAMBLE" modem="V.21"/>
+            <step dir="T" type="HDLC" tag="RR" value="FF C8 76"/>
+            <step dir="T" type="POSTAMBLE"/>
+        </possible-step>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="PPR" value="FF C8 3D 80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 FF FF FF FF FF"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="EOR-EOP" value="FF C8 73 74"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="ERR" value="FF C8 38"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCN" value="FF C8 5F"/>
+        <step dir="T" type="POSTAMBLE"/>
+    </test>
+    <test name="OREN06">
+        <step type="CALL"/>
+
+        <!--<step dir="T" type="CNG"/>-->
+
+        <step dir="R" type="CED"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="DIS" value="FF C8 01 ..." timeout="60000"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCS" value="FF C8 41 00 50 1F 30"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="TCF" modem="V.27ter/4800" value="900"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="CFR" value="FF C8 21"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.27ter/4800"/>
+        <step dir="T" type="PP" value="a4-stairstep.tif" crc_error="0"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="PPS-MPS" value="FF C8 7D 72 00 00 6B"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <possible-step>
+            <step dir="R" type="HDLC" modem="V.21" tag="RNR" value="FF C8 37"/>
+            <step dir="R" type="SILENCE"/>
+
+            <step type="WAIT" value="75"/>
+            <step dir="T" type="PREAMBLE" modem="V.21"/>
+            <step dir="T" type="HDLC" tag="RR" value="FF C8 76"/>
+            <step dir="T" type="POSTAMBLE"/>
+        </possible-step>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="PPR" value="FF C8 3D 80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 FF FF FF FF FF"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.27ter/4800"/>
+        <step dir="T" type="PP" value="a4-stairstep.tif" crc_error="0"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="PPS-MPS" value="FF C8 7D 72 00 00 6B"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <possible-step>
+            <step dir="R" type="HDLC" modem="V.21" tag="RNR" value="FF C8 37"/>
+            <step dir="R" type="SILENCE"/>
+
+            <step type="WAIT" value="75"/>
+            <step dir="T" type="PREAMBLE" modem="V.21"/>
+            <step dir="T" type="HDLC" tag="RR" value="FF C8 76"/>
+            <step dir="T" type="POSTAMBLE"/>
+        </possible-step>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="PPR" value="FF C8 3D 80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 FF FF FF FF FF"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.27ter/4800"/>
+        <step dir="T" type="PP" value="a4-stairstep.tif" crc_error="0"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="PPS-MPS" value="FF C8 7D 72 00 00 6B"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <possible-step>
+            <step dir="R" type="HDLC" modem="V.21" tag="RNR" value="FF C8 37"/>
+            <step dir="R" type="SILENCE"/>
+
+            <step type="WAIT" value="75"/>
+            <step dir="T" type="PREAMBLE" modem="V.21"/>
+            <step dir="T" type="HDLC" tag="RR" value="FF C8 76"/>
+            <step dir="T" type="POSTAMBLE"/>
+        </possible-step>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="PPR" value="FF C8 3D 80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 FF FF FF FF FF"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.27ter/4800"/>
+        <step dir="T" type="PP" value="a4-stairstep.tif" crc_error="0"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="PPS-MPS" value="FF C8 7D 72 00 00 6B"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <possible-step>
+            <step dir="R" type="HDLC" modem="V.21" tag="RNR" value="FF C8 37"/>
+            <step dir="R" type="SILENCE"/>
+
+            <step type="WAIT" value="75"/>
+            <step dir="T" type="PREAMBLE" modem="V.21"/>
+            <step dir="T" type="HDLC" tag="RR" value="FF C8 76"/>
+            <step dir="T" type="POSTAMBLE"/>
+        </possible-step>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="PPR" value="FF C8 3D 80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 FF FF FF FF FF"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="EOR-MPS" value="FF C8 73 72"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="ERR" value="FF C8 38"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.27ter/4800"/>
+        <step dir="T" type="PP" value="a4-stairstep.tif"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="PPS-EOP" value="FF C8 7D 74 00 00 6B"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <possible-step>
+            <step dir="R" type="HDLC" modem="V.21" tag="RNR" value="FF C8 37"/>
+            <step dir="R" type="SILENCE"/>
+
+            <step type="WAIT" value="75"/>
+            <step dir="T" type="PREAMBLE" modem="V.21"/>
+            <step dir="T" type="HDLC" tag="RR" value="FF C8 76"/>
+            <step dir="T" type="POSTAMBLE"/>
+        </possible-step>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="MCF" value="FF C8 31"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCN" value="FF C8 5F"/>
+        <step dir="T" type="POSTAMBLE"/>
+    </test>
+    <test name="OREN07">
+        <step type="CALL"/>
+
+        <!--<step dir="T" type="CNG"/>-->
+
+        <step dir="R" type="CED"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="DIS" value="FF C8 01 ..." timeout="60000"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCS" value="FF C8 41 00 50 1F 30"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="TCF" modem="V.27ter/4800" value="900"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="CFR" value="FF C8 21"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.27ter/4800"/>
+        <step dir="T" type="PP" value="a4-stairstep.tif" crc_error="0"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="PPS-EOP" value="FF C8 7D 74 00 00 6B"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <possible-step>
+            <step dir="R" type="HDLC" modem="V.21" tag="RNR" value="FF C8 37"/>
+            <step dir="R" type="SILENCE"/>
+
+            <step type="WAIT" value="75"/>
+            <step dir="T" type="PREAMBLE" modem="V.21"/>
+            <step dir="T" type="HDLC" tag="RR" value="FF C8 76"/>
+            <step dir="T" type="POSTAMBLE"/>
+        </possible-step>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="PPR" value="FF C8 3D 80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 FF FF FF FF FF"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.27ter/4800"/>
+        <step dir="T" type="PP" value="a4-stairstep.tif" crc_error="0"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="PPS-EOP" value="FF C8 7D 74 00 00 6B"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <possible-step>
+            <step dir="R" type="HDLC" modem="V.21" tag="RNR" value="FF C8 37"/>
+            <step dir="R" type="SILENCE"/>
+
+            <step type="WAIT" value="75"/>
+            <step dir="T" type="PREAMBLE" modem="V.21"/>
+            <step dir="T" type="HDLC" tag="RR" value="FF C8 76"/>
+            <step dir="T" type="POSTAMBLE"/>
+        </possible-step>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="PPR" value="FF C8 3D 80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 FF FF FF FF FF"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.27ter/4800"/>
+        <step dir="T" type="PP" value="a4-stairstep.tif" crc_error="0"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="PPS-EOP" value="FF C8 7D 74 00 00 6B"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <possible-step>
+            <step dir="R" type="HDLC" modem="V.21" tag="RNR" value="FF C8 37"/>
+            <step dir="R" type="SILENCE"/>
+
+            <step type="WAIT" value="75"/>
+            <step dir="T" type="PREAMBLE" modem="V.21"/>
+            <step dir="T" type="HDLC" tag="RR" value="FF C8 76"/>
+            <step dir="T" type="POSTAMBLE"/>
+        </possible-step>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="PPR" value="FF C8 3D 80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 FF FF FF FF FF"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.27ter/4800"/>
+        <step dir="T" type="PP" value="a4-stairstep.tif" crc_error="0"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="PPS-EOP" value="FF C8 7D 74 00 00 6B"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <possible-step>
+            <step dir="R" type="HDLC" modem="V.21" tag="RNR" value="FF C8 37"/>
+            <step dir="R" type="SILENCE"/>
+
+            <step type="WAIT" value="75"/>
+            <step dir="T" type="PREAMBLE" modem="V.21"/>
+            <step dir="T" type="HDLC" tag="RR" value="FF C8 76"/>
+            <step dir="T" type="POSTAMBLE"/>
+        </possible-step>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="PPR" value="FF C8 3D 80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 FF FF FF FF FF"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="EOR-EOP" value="FF C8 73 74"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="ERR" value="FF C8 38"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCN" value="FF C8 5F"/>
+        <step dir="T" type="POSTAMBLE"/>
+    </test>
+    <test name="OREN08">
+        <step type="CALL"/>
+
+        <!--<step dir="T" type="CNG"/>-->
+
+        <step dir="R" type="CED"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="DIS" value="FF C8 01 ..." timeout="60000"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCS" value="FF C8 41 00 50 1F 30"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="TCF" modem="V.27ter/4800" value="900"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="CFR" value="FF C8 21"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.27ter/4800"/>
+        <step dir="T" type="PP" value="a4-stairstep.tif"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="PPS-MPS" value="FF C8 7D 72 00 00 6B"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <possible-step>
+            <step dir="R" type="HDLC" modem="V.21" tag="RNR" value="FF C8 37"/>
+            <step dir="R" type="SILENCE"/>
+
+            <step type="WAIT" value="75"/>
+            <step dir="T" type="PREAMBLE" modem="V.21"/>
+            <step dir="T" type="HDLC" tag="RR" value="FF C8 76"/>
+            <step dir="T" type="POSTAMBLE"/>
+        </possible-step>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="MCF" value="FF C8 31"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.27ter/4800"/>
+        <step dir="T" type="PP" value="a4-stairstep.tif"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="PPS-EOP" value="FF C8 7D 74 80 00 6B"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <possible-step>
+            <step dir="R" type="HDLC" modem="V.21" tag="RNR" value="FF C8 37"/>
+            <step dir="R" type="SILENCE"/>
+
+            <step type="WAIT" value="75"/>
+            <step dir="T" type="PREAMBLE" modem="V.21"/>
+            <step dir="T" type="HDLC" tag="RR" value="FF C8 76"/>
+            <step dir="T" type="POSTAMBLE"/>
+        </possible-step>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="MCF" value="FF C8 31"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCN" value="FF C8 5F"/>
+        <step dir="T" type="POSTAMBLE"/>
+    </test>
+    <test name="OREN09">
+        <step type="CALL"/>
+
+        <!--<step dir="T" type="CNG"/>-->
+
+        <step dir="R" type="CED"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="DIS" value="FF C8 01 ..." timeout="60000"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCS" value="FF C8 41 00 50 1F 30"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="TCF" modem="V.27ter/4800" value="900"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="CFR" value="FF C8 21"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.27ter/4800"/>
+        <step dir="T" type="PP" value="a4-impress.tif"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="PPS-MPS" value="FF C8 7D 72 00 00 6B"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="MCF" value="FF C8 31"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.27ter/4800"/>
+        <step dir="T" type="PP" value="a4-stairstep.tif"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="PPS-EOP" value="FF C8 7D 74 00 00 6B"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="MCF" value="FF C8 31"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCN" value="FF C8 5F"/>
+        <step dir="T" type="POSTAMBLE"/>
+    </test>
+    <test name="OREN10">
+        <step type="CALL"/>
+
+        <!--<step dir="T" type="CNG"/>-->
+
+        <step dir="R" type="CED"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="DIS" value="FF C8 01 ..." timeout="60000"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCS" value="FF C8 41 00 50 1F 30"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="TCF" modem="V.27ter/4800" value="900"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="CFR" value="FF C8 21"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.27ter/4800"/>
+        <step dir="T" type="PP" value="a4-impress.tif"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="PPS-EOP" value="FF C8 7D 74 00 00 6B"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="MCF" value="FF C8 31"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCN" value="FF C8 5F"/>
+        <step dir="T" type="POSTAMBLE"/>
+    </test>
+</test-group>
+<test-group name="Annex D - Exception receive test procedures">
+    <test name="MRGX01">
+        <step type="CALL"/>
+
+        <step dir="R" type="CED"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="DIS" value="FF C8 01 ..." timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="DIS" value="FF C8 01 ..." timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="DIS" value="FF C8 01 ..." timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="DCN" value="FF C8 5F"/>
+    </test>
+    <test name="MRGX02">
+        <step type="CALL"/>
+
+        <step dir="R" type="CED"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="DIS" value="FF C8 01 ..." timeout="60000"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCN" value="FF C8 5F"/>
+        <step dir="T" type="POSTAMBLE"/>
+    </test>
+    <test name="MRGX03">
+        <step type="CALL"/>
+
+        <step dir="R" type="CED"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="DIS" value="FF C8 01 ..." timeout="60000"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="TCF" modem="V.27ter/4800" value="900"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="CFR" value="FF C8 21"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="TCF" modem="V.27ter/4800" value="900"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="CFR" value="FF C8 21"/>
+        <step dir="R" type="SILENCE"/>
+    </test>
+    <test name="MRGX04">
+        <step type="CALL"/>
+
+        <step dir="R" type="CED"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="DIS" value="FF C8 01 ..." timeout="60000"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="TCF" modem="V.27ter/4800" value="900"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="CFR" value="FF C8 21"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="TCF" modem="V.27ter/4800" value="900"/>
+    </test>
+    <test name="MRGX05">
+        <step type="CALL"/>
+
+        <step dir="R" type="CED"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="DIS" value="FF C8 01 ..." timeout="60000"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="TCF" modem="V.27ter/4800" value="900"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="CFR" value="FF C8 21"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="MSG" modem="V.27ter/4800" value="a4-white.tif"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MPS" value="FF C8 72"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="MCF" value="FF C8 31"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MPS" value="FF C8 72"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="MCF" value="FF C8 31"/>
+        <step dir="R" type="SILENCE"/>
+    </test>
+    <test name="MRGX06">
+        <step type="CALL"/>
+
+        <step dir="R" type="CED"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="DIS" value="FF C8 01 ..." timeout="60000"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="TCF" modem="V.27ter/4800" value="900"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="CFR" value="FF C8 21"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="MSG" modem="V.27ter/4800" value="a4-white.tif"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MPS" value="FF C8 72"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="MCF" value="FF C8 31"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MPS" value="FF C8 72"/>
+        <step dir="T" type="POSTAMBLE"/>
+    </test>
+    <test name="MRGX07">
+        <step type="CALL"/>
+
+        <step dir="R" type="CED"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="DIS" value="FF C8 01 ..." timeout="60000"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="TCF" modem="V.27ter/4800" value="900"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="CFR" value="FF C8 21"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="MSG" modem="V.27ter/4800" value="a4-white.tif"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="EOP" value="FF C8 74"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="MCF" value="FF C8 31"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="EOP" value="FF C8 74"/>
+        <step dir="T" type="POSTAMBLE"/>
+    </test>
+    <test name="MRGX08">
+        <step type="CALL"/>
+
+        <step dir="R" type="CED"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="DIS" value="FF C8 01 ..." timeout="60000"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="TCF" modem="V.27ter/4800" value="900"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="CFR" value="FF C8 21"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="MSG" modem="V.27ter/4800" value="a4-white.tif"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="EOP" value="FF C8 74"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="MCF" value="FF C8 31"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="EOP" value="FF C8 74"/>
+        <step dir="T" type="POSTAMBLE"/>
+    </test>
+    <test name="MRGX09">
+        <step type="CALL"/>
+
+        <step dir="R" type="CED"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="DIS" value="FF C8 01 ..." timeout="60000"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <!-- UNKNOWN_BCS_2 -->
+        <step dir="T" type="PREAMBLE" modem="V.21" value="37"/>
+        <step dir="T" type="HDLC" tag="GARBAGE" value="FF C8 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="TCF" modem="V.27ter/4800" value="900"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="TCF" modem="V.27ter/4800" value="900"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="TCF" modem="V.27ter/4800" value="900"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCN" value="FF C8 5F"/>
+        <step dir="T" type="POSTAMBLE"/>
+    </test>
+    <test name="MRGX10">
+        <step type="CALL"/>
+
+        <step dir="R" type="CED"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="DIS" value="FF C8 01 ..." timeout="60000"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <!-- UNKNOWN_BCS_3 -->
+        <step dir="T" type="PREAMBLE" modem="V.21" value="37"/>
+        <step dir="T" type="HDLC" tag="GARBAGE" value="A2 02 B5 C9 7E 67"/>
+ 
+        <step dir="R" type="HDLC" modem="V.21" tag="DIS" value="FF C8 01 ..." timeout="60000"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <!-- UNKNOWN_BCS_1 -->
+        <step dir="T" type="HDLC" tag="GARBAGE" value="FF C0 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step dir="T" type="HDLC" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="TCF" modem="V.27ter/4800" value="900"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="CFR" value="FF C8 21"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="MSG" modem="V.27ter/4800" value="a4-white.tif"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="EOP" value="FF C8 74"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="MCF" value="FF C8 31"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCN" value="FF C8 5F"/>
+        <step dir="T" type="POSTAMBLE"/>
+    </test>
+    <test name="MRGX11">
+        <step type="CALL"/>
+
+        <step dir="R" type="CED"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="DIS" value="FF C8 01 ..." timeout="60000"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <!-- UNKNOWN_BCS_4 -->
+        <step dir="T" type="PREAMBLE" modem="V.21" value="43"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DIS" value="FF C8 01 ..." timeout="60000"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="TCF" modem="V.27ter/4800" value="900"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="CFR" value="FF C8 21"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="MSG" modem="V.27ter/4800" value="a4-white.tif"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="EOP" value="FF C8 74"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="MCF" value="FF C8 31"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCN" value="FF C8 5F"/>
+        <step dir="T" type="POSTAMBLE"/>
+    </test>
+    <test name="MRGX12">
+        <step type="CALL"/>
+
+        <step dir="R" type="CED"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="DIS" value="FF C8 01 ..." timeout="60000"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="TCF" modem="V.27ter/4800" value="900"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="CFR" value="FF C8 21"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="MSG" modem="V.27ter/4800" value="a4-white.tif"/>
+        <step type="WAIT" value="75"/>
+        <!-- UNKNOWN_BCS_2 -->
+        <step dir="T" type="PREAMBLE" modem="V.21" value="37"/>
+        <step dir="T" type="HDLC" tag="GARBAGE" value="FF C8 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="EOP" value="FF C8 74"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="EOP" value="FF C8 74"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="EOP" value="FF C8 74"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCN" value="FF C8 5F"/>
+        <step dir="T" type="POSTAMBLE"/>
+    </test>
+    <test name="MRGX13">
+        <step type="CALL"/>
+
+        <step dir="R" type="CED"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="DIS" value="FF C8 01 ..." timeout="60000"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="TCF" modem="V.27ter/4800" value="900"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="CFR" value="FF C8 21"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="MSG" modem="V.27ter/4800" value="a4-white.tif"/>
+        <step type="WAIT" value="75"/>
+        <!-- UNKNOWN_BCS_3 -->
+        <step dir="T" type="PREAMBLE" modem="V.21" value="37"/>
+        <step dir="T" type="HDLC" tag="GARBAGE" value="A2 02 B5 C9 7E 67"/>
+        <step dir="T" type="HDLC" tag="EOP" value="FF C8 74"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="MCF" value="FF C8 31"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCN" value="FF C8 5F"/>
+        <step dir="T" type="POSTAMBLE"/>
+    </test>
+    <test name="MRGX14">
+        <step type="CALL"/>
+
+        <step dir="R" type="CED"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="DIS" value="FF C8 01 ..." timeout="60000"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="TCF" modem="V.27ter/4800" value="900"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="CFR" value="FF C8 21"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="MSG" modem="V.27ter/4800" value="a4-white.tif"/>
+        <step type="WAIT" value="75"/>
+        <!-- UNKNOWN_BCS_4 -->
+        <step dir="T" type="PREAMBLE" modem="V.21" value="43"/>
+        <step dir="T" type="HDLC" tag="EOP" value="FF C8 74"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="MCF" value="FF C8 31"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCN" value="FF C8 5F"/>
+        <step dir="T" type="POSTAMBLE"/>
+    </test>
+    <test name="MRGX15">
+        <step type="CALL"/>
+
+        <step dir="R" type="CED"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="DIS" value="FF C8 01 ..." timeout="60000"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="CALLER_SET_GROUP"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="TCF" modem="V.27ter/4800" value="900"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="CFR" value="FF C8 21"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="MSG" modem="V.27ter/4800" value="a4-white.tif"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="EOP" value="FF C8 74"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="MCF" value="FF C8 31"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCN" value="FF C8 5F"/>
+        <step dir="T" type="POSTAMBLE"/>
+    </test>
+</test-group>
+<test-group name="Annex E - Polling receive test procedures">
+    <test name="MTGP01">
+        <step type="ANSWER"/>
+
+        <step dir="R" type="CNG"/>
+
+        <step dir="T" type="CED"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DIS" value="FF C8 01 00 40 10"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCN" value="FF C8 5F"/>
+        <step dir="R" type="SILENCE"/>
+    </test>
+    <test name="MTGP02">
+        <step type="ANSWER"/>
+
+        <step dir="R" type="CNG"/>
+
+        <step dir="T" type="CED"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DIS" value="FF C8 01 00 80 10"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="CIG" fif="30 31 32 33 34 35 36 37 38 39 2B 20 20 20 20 20 20 20 20 20"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="DTC" value="FF C8 81 ..."/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCS" value="FF C8 41 00 00 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="TCF" modem="V.27ter/4800" value="900"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="CFR" value="FF C8 21"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="MSG" modem="V.27ter/4800" value="a4-white.tif"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MPS" value="FF C8 72"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="MCF" value="FF C8 31"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="MSG" modem="V.27ter/4800" value="a4-white.tif"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="EOP" value="FF C8 74"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="MCF" value="FF C8 31"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCN" value="FF C8 5F"/>
+        <step dir="T" type="POSTAMBLE"/>
+    </test>
+    <test name="OTGP03">
+        <step type="ANSWER"/>
+
+        <step dir="R" type="CNG"/>
+
+        <step dir="T" type="CED"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DIS" value="FF C8 01 00 80 11 01 01 01 40"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="PWD" value="FF C0 83 ..."/>
+        <step dir="R" type="HDLC" tag="DTC" value="FF C8 81 ..."/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCS" value="FF C8 41 00 00 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="TCF" modem="V.27ter/4800" value="900"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="CFR" value="FF C8 21"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="MSG" modem="V.27ter/4800" value="a4-white.tif"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MPS" value="FF C8 72"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="MCF" value="FF C8 31"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="MSG" modem="V.27ter/4800" value="a4-white.tif"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="EOP" value="FF C8 74"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="MCF" value="FF C8 31"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCN" value="FF C8 5F"/>
+        <step dir="T" type="POSTAMBLE"/>
+    </test>
+</test-group>
+<test-group name="Annex F - Transmit test procedures">
+    <test name="MTGN01">
+        <step type="ANSWER"/>
+
+        <step dir="R" type="CNG"/>
+
+        <step dir="T" type="CED"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DIS" value="FF C8 01 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" tag="TSI" value="FF C8 42 ..."/>
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 ..."/>
+        <step dir="R" type="TCF" modem="V.27ter/4800" timeout="60000"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="CFR" value="FF C8 21"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="MSG" modem="V.27ter/4800" value="a4-impress.tif" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="MPS" value="FF C8 72"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MCF" value="FF C8 31"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="MSG" modem="V.27ter/4800"  timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="EOP" value="FF C8 74"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MCF" value="FF C8 31"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCN" value="FF C8 5F"/>
+    </test>
+    <test name="MTGN02">
+        <step type="ANSWER"/>
+
+        <step dir="R" type="CNG"/>
+
+        <step dir="T" type="CED"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DIS" value="FF C8 01 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" tag="TSI" value="FF C8 42 ..."/>
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800" timeout="60000"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="CFR" value="FF C8 21"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="MSG" modem="V.27ter/4800" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="EOP" value="FF C8 74"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MCF" value="FF C8 31"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCN" value="FF C8 5F"/>
+    </test>
+    <test name="MTGN03">
+        <step type="ANSWER"/>
+
+        <step dir="R" type="CNG"/>
+
+        <step dir="T" type="CED"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DIS" value="FF C8 01 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="CFR" value="FF C8 21"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="MSG" modem="V.27ter/4800" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="EOP" value="FF C8 74"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MCF" value="FF C8 31"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCN" value="FF C8 5F"/>
+    </test>
+    <test name="MTGN04">
+        <step type="ANSWER"/>
+
+        <step dir="R" type="CNG"/>
+
+        <step dir="T" type="CED"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DIS" value="FF C8 01 00 40 20"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800" timeout="60000"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="CFR" value="FF C8 21"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="MSG" modem="V.27ter/4800" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="EOP" value="FF C8 74"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MCF" value="FF C8 31"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCN" value="FF C8 5F"/>
+    </test>
+    <test name="MTGN05">
+        <step type="ANSWER"/>
+
+        <step dir="R" type="CNG"/>
+
+        <step dir="T" type="CED"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DIS" value="FF C8 01 00 40 02"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800" timeout="60000"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="CFR" value="FF C8 21"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="MSG" modem="V.27ter/4800" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="EOP" value="FF C8 74"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MCF" value="FF C8 31"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCN" value="FF C8 5F"/>
+    </test>
+    <test name="MTGN06">
+        <step type="ANSWER"/>
+
+        <step dir="R" type="CNG"/>
+
+        <step dir="T" type="CED"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DIS" value="FF C8 01 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800" timeout="60000"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="CFR" value="FF C8 21"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="MSG" modem="V.27ter/4800" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="EOP" value="FF C8 74"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MCF" value="FF C8 31"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCN" value="FF C8 5F"/>
+    </test>
+    <test name="MTGN07">
+        <step type="ANSWER"/>
+
+        <step dir="R" type="CNG"/>
+
+        <step dir="T" type="CED"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DIS" value="FF C8 01 00 00 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCN" value="FF C8 5F"/>
+    </test>
+    <test name="MTGN08">
+        <step type="ANSWER"/>
+
+        <step dir="R" type="CNG"/>
+
+        <step dir="T" type="CED"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DIS" value="FF C8 01 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800" timeout="60000"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="CFR" value="FF C8 21"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="MSG" modem="V.27ter/4800" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="MPS" value="FF C8 72"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MCF" value="FF C8 31"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="MSG" modem="V.27ter/4800" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="EOP" value="FF C8 74"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MCF" value="FF C8 31"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCN" value="FF C8 5F"/>
+    </test>
+    <test name="MTGN09">
+        <step type="ANSWER"/>
+
+        <step dir="R" type="CNG"/>
+
+        <step dir="T" type="CED"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DIS" value="FF C8 01 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800" timeout="60000"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DIS" value="FF C8 01 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800" timeout="60000"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="CFR" value="FF C8 21"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="MSG" modem="V.27ter/4800" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="EOP" value="FF C8 74"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MCF" value="FF C8 31"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCN" value="FF C8 5F"/>
+    </test>
+    <test name="MTGN10">
+        <step type="ANSWER"/>
+
+        <step dir="R" type="CNG"/>
+
+        <step dir="T" type="CED"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DIS" value="FF C8 01 00 50 00" crc_error="0"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="CRP" value="FF C8 58 | nothing"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DIS" value="FF C8 01 00 50 00" crc_error="0"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="CRP" value="FF C8 58 | nothing"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DIS" value="FF C8 01 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800" timeout="60000"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="CFR" value="FF C8 21"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="MSG" modem="V.27ter/4800" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="EOP" value="FF C8 74"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MCF" value="FF C8 31"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCN" value="FF C8 5F"/>
+    </test>
+    <test name="MTGN11">
+        <step type="ANSWER"/>
+
+        <step dir="R" type="CNG"/>
+
+        <step dir="T" type="CED"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DIS" value="FF C8 01 00 72 20"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800" timeout="60000"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="CFR" value="FF C8 21"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="MSG" modem="V.27ter/4800" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="EOP" value="FF C8 74"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MCF" value="FF C8 31"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCN" value="FF C8 5F"/>
+    </test>
+    <test name="MTGN12">
+        <step type="ANSWER"/>
+
+        <step dir="R" type="CNG"/>
+
+        <step dir="T" type="CED"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DIS" value="FF C8 01 00 72 20"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800" timeout="60000"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="CFR" value="FF C8 21" crc_error="0"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800" timeout="60000"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="CFR" value="FF C8 21"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="MSG" modem="V.27ter/4800" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="EOP" value="FF C8 74"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MCF" value="FF C8 31"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCN" value="FF C8 5F"/>
+    </test>
+    <test name="MTGN13">
+        <step type="ANSWER"/>
+
+        <step dir="R" type="CNG"/>
+
+        <step dir="T" type="CED"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DIS" value="FF C8 01 00 72 20"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800" timeout="60000"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" modem="V.21" tag="CRP" value="FF C8 58"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800" timeout="60000"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="CFR" value="FF C8 21"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="MSG" modem="V.27ter/4800" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="EOP" value="FF C8 74"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MCF" value="FF C8 31"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCN" value="FF C8 5F"/>
+    </test>
+    <test name="MTGN14">
+        <step type="ANSWER"/>
+
+        <step dir="R" type="CNG"/>
+
+        <step dir="T" type="CED"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DIS" value="FF C8 01 00 72 20"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800" timeout="60000"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="CFR" value="FF C8 21"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="MSG" modem="V.27ter/4800" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="EOM" value="FF C8 71"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="EOM" value="FF C8 71"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MCF" value="FF C8 31"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DIS" value="FF C8 01 00 72 20"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800" timeout="60000"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="CFR" value="FF C8 21"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="MSG" modem="V.27ter/4800" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="EOP" value="FF C8 74"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MCF" value="FF C8 31"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCN" value="FF C8 5F"/>
+    </test>
+    <test name="MTGN15">
+        <step type="ANSWER"/>
+
+        <step dir="R" type="CNG"/>
+
+        <step dir="T" type="CED"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DIS" value="FF C8 01 00 72 20"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800" timeout="60000"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="CFR" value="FF C8 21"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="MSG" modem="V.27ter/4800" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="EOM" value="FF C8 71"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MCF" value="FF C8 31" crc_error="0"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="EOM" value="FF C8 71"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MCF" value="FF C8 31" crc_error="0"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="EOM" value="FF C8 71"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MCF" value="FF C8 31"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DIS" value="FF C8 01 00 72 20"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800" timeout="60000"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="CFR" value="FF C8 21"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="MSG" modem="V.27ter/4800" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="EOP" value="FF C8 74"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MCF" value="FF C8 31"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCN" value="FF C8 5F"/>
+    </test>
+    <test name="MTGN16">
+        <step type="ANSWER"/>
+
+        <step dir="R" type="CNG"/>
+
+        <step dir="T" type="CED"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DIS" value="FF C8 01 00 72 20"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00" timeout="60000"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="CFR" value="FF C8 21"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="MSG" modem="V.27ter/4800" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="EOM" value="FF C8 71"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" modem="V.21" tag="CRP" value="FF C8 58"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="EOM" value="FF C8 71"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" modem="V.21" tag="CRP" value="FF C8 58"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="EOM" value="FF C8 71"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MCF" value="FF C8 31"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DIS" value="FF C8 01 00 72 20"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800" timeout="60000"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="CFR" value="FF C8 21"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="MSG" modem="V.27ter/4800" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="EOP" value="FF C8 74"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MCF" value="FF C8 31"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCN" value="FF C8 5F"/>
+    </test>
+    <test name="MTGN17">
+        <step type="ANSWER"/>
+
+        <step dir="R" type="CNG"/>
+
+        <step dir="T" type="CED"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DIS" value="FF C8 01 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800" timeout="60000"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="CFR" value="FF C8 21"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="MSG" modem="V.27ter/4800" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="MPS" value="FF C8 72"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="MPS" value="FF C8 72"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="MPS" value="FF C8 72"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MCF" value="FF C8 31"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="MSG" modem="V.27ter/4800" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="EOP" value="FF C8 74"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MCF" value="FF C8 31"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCN" value="FF C8 5F"/>
+    </test>
+    <test name="MTGN18">
+        <step type="ANSWER"/>
+
+        <step dir="R" type="CNG"/>
+
+        <step dir="T" type="CED"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DIS" value="FF C8 01 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800" timeout="60000"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="CFR" value="FF C8 21"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="MSG" modem="V.27ter/4800" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="MPS" value="FF C8 72"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MCF" value="FF C8 31" crc_error="0"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="MPS" value="FF C8 72"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MCF" value="FF C8 31" crc_error="0"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="MPS" value="FF C8 72"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MCF" value="FF C8 31"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="MSG" modem="V.27ter/4800" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="EOP" value="FF C8 74"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MCF" value="FF C8 31"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCN" value="FF C8 5F"/>
+    </test>
+    <test name="MTGN19">
+        <step type="ANSWER"/>
+
+        <step dir="R" type="CNG"/>
+
+        <step dir="T" type="CED"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DIS" value="FF C8 01 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800" timeout="60000"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="CFR" value="FF C8 21"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="MSG" modem="V.27ter/4800" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="MPS" value="FF C8 72"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" modem="V.21" tag="CRP" value="FF C8 58"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="MPS" value="FF C8 72"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" modem="V.21" tag="CRP" value="FF C8 58"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="MPS" value="FF C8 72"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MCF" value="FF C8 31"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="MSG" modem="V.27ter/4800" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="EOP" value="FF C8 74"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step dir="T" type="HDLC" tag="MCF" value="FF C8 31"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCN" value="FF C8 5F"/>
+    </test>
+    <test name="MTGN20">
+        <step type="ANSWER"/>
+
+        <step dir="R" type="CNG"/>
+
+        <step dir="T" type="CED"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DIS" value="FF C8 01 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800" timeout="60000"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="CFR" value="FF C8 21"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="MSG" modem="V.27ter/4800" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="EOP" value="FF C8 74"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="EOP" value="FF C8 74"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="EOP" value="FF C8 74"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MCF" value="FF C8 31"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCN" value="FF C8 5F"/>
+    </test>
+    <test name="MTGN21">
+        <step type="ANSWER"/>
+
+        <step dir="R" type="CNG"/>
+
+        <step dir="T" type="CED"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DIS" value="FF C8 01 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800" timeout="60000"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="CFR" value="FF C8 21"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="MSG" modem="V.27ter/4800" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="EOP" value="FF C8 74"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MCF" value="FF C8 31" crc_error="0"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="EOP" value="FF C8 74"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MCF" value="FF C8 31" crc_error="0"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="EOP" value="FF C8 74"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MCF" value="FF C8 31"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCN" value="FF C8 5F"/>
+    </test>
+    <test name="MTGN22">
+        <step type="ANSWER"/>
+
+        <step dir="R" type="CNG"/>
+
+        <step dir="T" type="CED"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DIS" value="FF C8 01 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800" timeout="60000"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="CFR" value="FF C8 21"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="MSG" modem="V.27ter/4800" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="EOP" value="FF C8 74"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" modem="V.21" tag="CRP" value="FF C8 58"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="EOP" value="FF C8 74"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" modem="V.21" tag="CRP" value="FF C8 58"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="EOP" value="FF C8 74"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MCF" value="FF C8 31"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCN" value="FF C8 5F"/>
+    </test>
+    <test name="MTGN23">
+        <step type="ANSWER"/>
+
+        <step dir="R" type="CNG"/>
+
+        <step dir="T" type="CED"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DIS" value="FF C8 01 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800" timeout="60000"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="CFR" value="FF C8 21"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="MSG" modem="V.27ter/4800" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="EOM" value="FF C8 71"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="RTP"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step type="WAIT" value="2000"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DIS" value="FF C8 01 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800" timeout="60000"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="CFR" value="FF C8 21"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="MSG" modem="V.27ter/4800" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="EOP" value="FF C8 74"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MCF" value="FF C8 31"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCN" value="FF C8 5F"/>
+    </test>
+    <test name="MTGN24">
+        <step type="ANSWER"/>
+
+        <step dir="R" type="CNG"/>
+
+        <step dir="T" type="CED"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DIS" value="FF C8 01 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800" timeout="60000"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="CFR" value="FF C8 21"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="MSG" modem="V.27ter/4800" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="MPS" value="FF C8 72"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="RTP"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800" timeout="60000"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="CFR" value="FF C8 21"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="MSG" modem="V.27ter/4800" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="EOP" value="FF C8 74"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MCF" value="FF C8 31"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCN" value="FF C8 5F"/>
+    </test>
+    <test name="MTGN25">
+        <step type="ANSWER"/>
+
+        <step dir="R" type="CNG"/>
+
+        <step dir="T" type="CED"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DIS" value="FF C8 01 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800" timeout="60000"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="CFR" value="FF C8 21"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="MSG" modem="V.27ter/4800" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="EOP" value="FF C8 74"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="RTP"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MCF" value="FF C8 31"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCN" value="FF C8 5F"/>
+    </test>
+    <test name="MTGN26">
+        <step type="ANSWER"/>
+
+        <step dir="R" type="CNG"/>
+
+        <step dir="T" type="CED"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DIS" value="FF C8 01 00 72 20"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800" timeout="60000"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="CFR" value="FF C8 21"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="MSG" modem="V.27ter/4800" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="EOM" value="FF C8 71"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="RTN"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step type="WAIT" value="2000"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DIS" value="FF C8 01 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800" timeout="60000"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="CFR" value="FF C8 21"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="MSG" modem="V.27ter/4800" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="EOP" value="FF C8 74"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MCF" value="FF C8 31"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCN" value="FF C8 5F"/>
+    </test>
+    <test name="MTGN27">
+        <step type="ANSWER"/>
+
+        <step dir="R" type="CNG"/>
+
+        <step dir="T" type="CED"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DIS" value="FF C8 01 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800" timeout="60000"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="CFR" value="FF C8 21"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="MSG" modem="V.27ter/4800" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="MPS" value="FF C8 72"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="RTN"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800" timeout="60000"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="CFR" value="FF C8 21"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="MSG" modem="V.27ter/4800" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="EOP" value="FF C8 74"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MCF" value="FF C8 31"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCN" value="FF C8 5F"/>
+    </test>
+    <test name="MTGN28">
+        <step type="ANSWER"/>
+
+        <step dir="R" type="CNG"/>
+
+        <step dir="T" type="CED"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DIS" value="FF C8 01 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800" timeout="60000"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="CFR" value="FF C8 21"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="MSG" modem="V.27ter/4800" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="EOP" value="FF C8 74"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="RTN"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCN" value="FF C8 5F"/>
+    </test>
+</test-group>
+<test-group name="Annex G - Capability transmit tests">
+    <test name="OTGC01">
+        <step type="ANSWER"/>
+
+        <step dir="R" type="CNG"/>
+
+        <step dir="T" type="CED"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DIS" value="FF C8 01 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800" timeout="60000"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="CFR" value="FF C8 21"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="MSG" modem="V.27ter/4800" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="MPS" value="FF C8 72"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MCF" value="FF C8 31"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="MSG" modem="V.27ter/4800" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="EOP" value="FF C8 74"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MCF" value="FF C8 31"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCN" value="FF C8 5F"/>
+    </test>
+    <test name="OTGC02">
+        <step type="ANSWER"/>
+
+        <step dir="R" type="CNG"/>
+
+        <step dir="T" type="CED"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DIS" value="FF C8 01 00 40 10"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800" timeout="60000"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="CFR" value="FF C8 21"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="MSG" modem="V.27ter/4800" value="a4-impress.tif" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="MPS" value="FF C8 72"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MCF" value="FF C8 31"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="MSG" modem="V.27ter/4800" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="EOP" value="FF C8 74"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MCF" value="FF C8 31"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCN" value="FF C8 5F"/>
+    </test>
+    <test name="OTGC03">
+        <step type="ANSWER"/>
+
+        <step dir="R" type="CNG"/>
+
+        <step dir="T" type="CED"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DIS" value="FF C8 01 00 72 20"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800" timeout="60000"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="CFR" value="FF C8 21"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="MSG" modem="V.27ter/4800" value="a4-impress.tif" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="MPS" value="FF C8 72"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MCF" value="FF C8 31"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="MSG" modem="V.27ter/4800" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="EOP" value="FF C8 74"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MCF" value="FF C8 31"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCN" value="FF C8 5F"/>
+    </test>
+    <test name="OTGC04">
+        <step type="ANSWER"/>
+
+        <step dir="R" type="CNG"/>
+
+        <step dir="T" type="CED"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DIS" value="FF C8 01 00 72 20"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800" timeout="60000"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="FTT" value="FF C8 22"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800" timeout="60000"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="CFR" value="FF C8 21"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="MSG" modem="V.27ter/4800" value="a4-impress.tif" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="MPS" value="FF C8 72"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MCF" value="FF C8 31"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="MSG" modem="V.27ter/4800" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="EOP" value="FF C8 74"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MCF" value="FF C8 31"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCN" value="FF C8 5F"/>
+    </test>
+    <test name="OTGC05">
+        <step type="ANSWER"/>
+
+        <step dir="R" type="CNG"/>
+
+        <step dir="T" type="CED"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DIS" value="FF C8 01 00 76 20"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800" timeout="60000"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="CFR" value="FF C8 21"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="MSG" modem="V.27ter/4800" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="MPS" value="FF C8 72"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MCF" value="FF C8 31"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="MSG" modem="V.27ter/4800" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="EOP" value="FF C8 74"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MCF" value="FF C8 31"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCN" value="FF C8 5F"/>
+    </test>
+    <test name="OTGC06">
+        <step type="ANSWER"/>
+
+        <step dir="R" type="CNG"/>
+
+        <step dir="T" type="CED"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DIS" value="FF C8 01 00 76 20"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800" timeout="60000"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="CFR" value="FF C8 21"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="MSG" modem="V.27ter/4800" value="a4-impress.tif" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="MPS" value="FF C8 72"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MCF" value="FF C8 31"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="MSG" modem="V.27ter/4800" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="EOP" value="FF C8 74"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MCF" value="FF C8 31"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCN" value="FF C8 5F"/>
+    </test>
+    <test name="OTGC07">
+        <step type="ANSWER"/>
+
+        <step dir="R" type="CNG"/>
+
+        <step dir="T" type="CED"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DIS" value="FF C8 01 00 76 20"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800" timeout="60000"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="FTT" value="FF C8 22"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800" timeout="60000"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="FTT" value="FF C8 22"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800" timeout="60000"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="CFR" value="FF C8 21"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="MSG" modem="V.27ter/4800" value="a4-impress.tif" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="MPS" value="FF C8 72"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MCF" value="FF C8 31"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="MSG" modem="V.27ter/4800" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="EOP" value="FF C8 74"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MCF" value="FF C8 31"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCN" value="FF C8 5F"/>
+    </test>
+    <test name="OTGC08">
+        <step type="ANSWER"/>
+
+        <step dir="R" type="CNG"/>
+
+        <step dir="T" type="CED"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DIS" value="FF C8 01 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800" timeout="60000"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="FTT" value="FF C8 22"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800" timeout="60000"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="FTT" value="FF C8 22"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800" timeout="60000"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="FTT" value="FF C8 22"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800" timeout="60000"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="CFR" value="FF C8 21"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="MSG" modem="V.27ter/4800" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="MPS" value="FF C8 72"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MCF" value="FF C8 31"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="MSG" modem="V.27ter/4800" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="EOP" value="FF C8 74"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MCF" value="FF C8 31"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCN" value="FF C8 5F"/>
+    </test>
+    <test name="OTGC09">
+        <step type="ANSWER"/>
+
+        <step dir="R" type="CNG"/>
+
+        <step dir="T" type="CED"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DIS" value="FF C8 01 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800" timeout="60000"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="CFR" value="FF C8 21"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="MSG" modem="V.27ter/4800" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="MPS" value="FF C8 72"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MCF" value="FF C8 31"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="MSG" modem="V.27ter/4800" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="EOP" value="FF C8 74"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MCF" value="FF C8 31"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCN" value="FF C8 5F"/>
+    </test>
+    <test name="OTGC10">
+        <step type="ANSWER"/>
+
+        <step dir="R" type="CNG"/>
+
+        <step dir="T" type="CED"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DIS" value="FF C8 01 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800" timeout="60000"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800" timeout="60000"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="FTT" value="FF C8 22"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800" timeout="60000"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="CFR" value="FF C8 21"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="MSG" modem="V.27ter/4800" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="MPS" value="FF C8 72"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="RTP"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800" timeout="60000"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" modem="V.21" tag="CRP" value="FF C8 58"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800" timeout="60000"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="CFR" value="FF C8 21"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="MSG" modem="V.27ter/4800" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="EOP" value="FF C8 74"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MCF" value="FF C8 31"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCN" value="FF C8 5F"/>
+    </test>
+    <test name="OTGC11">
+        <step type="ANSWER"/>
+
+        <step dir="R" type="CNG"/>
+
+        <step dir="T" type="CED"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DIS" value="FF C8 01 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800" timeout="60000"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="CFR" value="FF C8 21"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="MSG" modem="V.27ter/4800" value="a4-impress.tif" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="EOP" value="FF C8 74"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MCF" value="FF C8 31"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCN" value="FF C8 5F"/>
+    </test>
+</test-group>
+<test-group name="Annex H - ECM transmit test procedures">
+    <test name="OTEN01">
+        <step type="ANSWER"/>
+
+        <step dir="R" type="CNG"/>
+
+        <step dir="T" type="CED"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DIS" value="FF C8 01 00 50 1F 20"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800" timeout="60000"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="CFR" value="FF C8 21"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="PP" modem="V.27ter/4800" value="a4-white.tif" timeout="60000"/>
+        <step dir="R" type="HDLC" mode="V.21" tag="PPS-NULL" value="FF C8 7D 00"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MCF" value="FF C8 31"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="PP" modem="V.27ter/4800" value="a4-white.tif" timeout="60000"/>
+        <step dir="R" type="HDLC" mode="V.21" tag="PPS-MPS" value="FF C8 7D 72"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MCF" value="FF C8 31"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="PP" modem="V.27ter/4800" value="a4-white.tif" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="PPS-EOP" value="FF C8 7D 74"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MCF" value="FF C8 31"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCN" value="FF C8 5F"/>
+    </test>
+    <test name="OTEN02">
+        <step type="ANSWER"/>
+
+        <step dir="R" type="CNG"/>
+
+        <step dir="T" type="CED"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DIS" value="FF C8 01 00 50 1F 20"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800" timeout="60000"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="CFR" value="FF C8 21"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="PP" modem="V.27ter/4800" value="a4-impress.tif" timeout="60000"/>
+        <step dir="R" type="HDLC" mode="V.21" tag="PPS-NULL" value="FF C8 7D 00"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" modem="V.21" tag="PPR" value="FF C8 3D 00 00 00 00 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="PP" modem="V.27ter/4800" value="a4-impress.tif" timeout="60000"/>
+        <step dir="R" type="HDLC" mode="V.21" tag="PPS-NULL" value="FF C8 7D 00"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MCF" value="FF C8 31"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="PP" modem="V.27ter/4800" value="a4-impress.tif" timeout="60000"/>
+        <step dir="R" type="HDLC" mode="V.21" tag="PPS-MPS" value="FF C8 7D 72"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" modem="V.21" tag="PPR" value="FF C8 3D 00 00 00 00 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="PP" modem="V.27ter/4800" value="a4-white.tif" timeout="60000"/>
+        <step dir="R" type="HDLC" mode="V.21" tag="PPS-MPS" value="FF C8 7D 72"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MCF" value="FF C8 31"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="PP" modem="V.27ter/4800" value="a4-white.tif" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="PPS-EOP" value="FF C8 7D 74"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" modem="V.21" tag="PPR" value="FF C8 3D 00 00 00 00 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="PP" modem="V.27ter/4800" value="a4-white.tif" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="PPS-EOP" value="FF C8 7D 74"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MCF" value="FF C8 31"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCN" value="FF C8 5F"/>
+    </test>
+    <test name="OTEN03">
+        <step type="ANSWER"/>
+
+        <step dir="R" type="CNG"/>
+
+        <step dir="T" type="CED"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DIS" value="FF C8 01 00 50 1F 20"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800" timeout="60000"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="CFR" value="FF C8 21"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="PP" modem="V.27ter/4800" value="a4-white.tif" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="PPS-EOP" value="FF C8 7D 74"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="RNR" value="FF C8 37"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="RR" value="FF C8 76"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="RNR" value="FF C8 37"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCN" value="FF C8 5F"/>
+    </test>
+    <test name="OTEN04">
+        <step type="ANSWER"/>
+
+        <step dir="R" type="CNG"/>
+
+        <step dir="T" type="CED"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DIS" value="FF C8 01 00 50 1F 20"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800" timeout="60000"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="CFR" value="FF C8 21"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="PP" modem="V.27ter/4800" value="a4-white.tif" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="PPS-EOP" value="FF C8 7D 74"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" modem="V.21" tag="PPR" value="FF C8 3D 00 00 00 00 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="PP" modem="V.27ter/4800" value="a4-white.tif" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="PPS-EOP" value="FF C8 7D 74"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" modem="V.21" tag="PPR" value="FF C8 3D 00 00 00 00 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="PP" modem="V.27ter/4800" value="a4-white.tif" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="PPS-EOP" value="FF C8 7D 74"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" modem="V.21" tag="PPR" value="FF C8 3D 00 00 00 00 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="PP" modem="V.27ter/4800" value="a4-white.tif" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="PPS-EOP" value="FF C8 7D 74"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" modem="V.21" tag="PPR" value="FF C8 3D 00 00 00 00 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="EOR-EOP" value="FF C8 73 74"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="ERR" value="FF C8 38"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCN" value="FF C8 5F"/>
+        <step dir="R" type="SILENCE"/>
+    </test>
+    <test name="OTEN05">
+        <step type="ANSWER"/>
+
+        <step dir="R" type="CNG"/>
+
+        <step dir="T" type="CED"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DIS" value="FF C8 01 00 50 1F 20"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800" timeout="60000"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="CFR" value="FF C8 21"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="PP" modem="V.27ter/4800" value="a4-white.tif" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="PPS-EOP" value="FF C8 7D 74"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" modem="V.21" tag="PPR" value="FF C8 3D 00 00 00 00 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="PP" modem="V.27ter/4800" value="a4-white.tif" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="PPS-EOP" value="FF C8 7D 74"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" modem="V.21" tag="PPR" value="FF C8 3D 00 00 00 00 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="PP" modem="V.27ter/4800" value="a4-white.tif" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="PPS-EOP" value="FF C8 7D 74"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" modem="V.21" tag="PPR" value="FF C8 3D 00 00 00 00 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="PP" modem="V.27ter/4800" value="a4-white.tif" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="PPS-EOP" value="FF C8 7D 74"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" modem="V.21" tag="PPR" value="FF C8 3D 00 00 00 00 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="CTC" value="FF C8 48"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="CTR" value="FF C8 23"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="PP" modem="V.27ter/4800" value="a4-white.tif" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="PPS-EOP" value="FF C8 7D 74"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MCF" value="FF C8 31"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCN" value="FF C8 5F"/>
+    </test>
+    <test name="OTEN06">
+        <step type="ANSWER"/>
+
+        <step dir="R" type="CNG"/>
+
+        <step dir="T" type="CED"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DIS" value="FF C8 01 00 50 1F 20"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800" timeout="60000"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="CFR" value="FF C8 21"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="PP" modem="V.27ter/4800" value="a4-impress.tif" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="PPS-EOP" value="FF C8 7D 74"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MCF" value="FF C8 31"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCN" value="FF C8 5F"/>
+    </test>
+</test-group>
+<test-group name="Annex I - Exception transmit test procedures">
+    <test name="MTGX01">
+        <step type="ANSWER"/>
+
+        <step dir="R" type="CNG"/>
+
+        <step dir="T" type="CED"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DIS" value="FF C8 01 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800" timeout="60000"/>
+    </test>
+    <test name="MTGX02">
+        <step type="ANSWER"/>
+
+        <step dir="R" type="CNG"/>
+
+        <step dir="T" type="CED"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DIS" value="FF C8 01 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCN" value="FF C8 5F"/>
+        <step dir="T" type="POSTAMBLE"/>
+    </test>
+    <test name="MTGX03">
+        <step type="ANSWER"/>
+
+        <step dir="R" type="CNG"/>
+
+        <step dir="T" type="CED"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DIS" value="FF C8 01 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800" timeout="60000"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCN" value="FF C8 5F"/>
+        <step dir="T" type="POSTAMBLE"/>
+    </test>
+    <test name="MTGX04">
+        <step type="ANSWER"/>
+
+        <step dir="R" type="CNG"/>
+
+        <step dir="T" type="CED"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DIS" value="FF C8 01 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800" timeout="60000"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="CFR" value="FF C8 21"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="MSG" modem="V.27ter/4800" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="EOM" value="FF C8 71"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCN" value="FF C8 5F"/>
+        <step dir="T" type="POSTAMBLE"/>
+    </test>
+    <test name="MTGX05">
+        <step type="ANSWER"/>
+
+        <step dir="R" type="CNG"/>
+
+        <step dir="T" type="CED"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DIS" value="FF C8 01 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800" timeout="60000"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="CFR" value="FF C8 21"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="MSG" modem="V.27ter/4800" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="MPS" value="FF C8 72"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCN" value="FF C8 5F"/>
+        <step dir="T" type="POSTAMBLE"/>
+    </test>
+    <test name="MTGX06">
+        <step type="ANSWER"/>
+
+        <step dir="R" type="CNG"/>
+
+        <step dir="T" type="CED"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DIS" value="FF C8 01 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800" timeout="60000"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="CFR" value="FF C8 21"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="MSG" modem="V.27ter/4800" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="EOP" value="FF C8 74"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCN" value="FF C8 5F"/>
+        <step dir="T" type="POSTAMBLE"/>
+    </test>
+    <test name="MTGX07">
+        <step type="ANSWER"/>
+
+        <step dir="R" type="CNG"/>
+
+        <step dir="T" type="CED"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DIS" value="FF C8 01 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="DCN" value="FF C8 5F"/>
+    </test>
+    <test name="MTGX08">
+        <step type="ANSWER"/>
+
+        <step dir="R" type="CNG"/>
+
+        <step dir="T" type="CED"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DIS" value="FF C8 01 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="CFR" value="FF C8 21"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="MSG" modem="V.27ter/4800" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="EOM" value="FF C8 71"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="EOM" value="FF C8 71"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="EOM" value="FF C8 71"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="DCN" value="FF C8 5F"/>
+    </test>
+    <test name="MTGX09">
+        <step type="ANSWER"/>
+
+        <step dir="R" type="CNG"/>
+
+        <step dir="T" type="CED"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DIS" value="FF C8 01 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="CFR" value="FF C8 21"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="MSG" modem="V.27ter/4800" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="MPS" value="FF C8 72"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="MPS" value="FF C8 72"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="MPS" value="FF C8 72"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="DCN" value="FF C8 5F"/>
+    </test>
+    <test name="MTGX10">
+        <step type="ANSWER"/>
+
+        <step dir="R" type="CNG"/>
+
+        <step dir="T" type="CED"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DIS" value="FF C8 01 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="CFR" value="FF C8 21"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="MSG" modem="V.27ter/4800" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="EOP" value="FF C8 74"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="EOP" value="FF C8 74"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="EOP" value="FF C8 74"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="DCN" value="FF C8 5F"/>
+    </test>
+    <test name="MTGX11">
+        <step type="ANSWER"/>
+
+        <step dir="R" type="CNG"/>
+
+        <step dir="T" type="CED"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DIS" value="FF C8 01 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="FTT" value="FF C8 22"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="FTT" value="FF C8 22"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="FTT" value="FF C8 22"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCN" value="FF C8 5F"/>
+    </test>
+    <test name="MTGX12">
+        <step type="ANSWER"/>
+
+        <step dir="R" type="CNG"/>
+
+        <step dir="T" type="CED"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DIS" value="FF C8 01 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DIS" value="FF C8 01 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DIS" value="FF C8 01 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DIS" value="FF C8 01 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCN" value="FF C8 5F"/>
+    </test>
+    <test name="MTGX13">
+        <step type="ANSWER"/>
+
+        <step dir="R" type="CNG"/>
+
+        <step dir="T" type="CED"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DIS" value="FF C8 01 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" modem="V.21" tag="CRP" value="FF C8 58"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" modem="V.21" tag="CRP" value="FF C8 58"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800" timeout="60000"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" modem="V.21" tag="CRP" value="FF C8 58"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCN" value="FF C8 5F"/>
+    </test>
+    <test name="MTGX14">
+        <step type="ANSWER"/>
+
+        <step dir="R" type="CNG"/>
+
+        <step dir="T" type="CED"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DIS" value="FF C8 01 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="CFR" value="FF C8 21"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="MSG" modem="V.27ter/4800" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="EOM" value="FF C8 71"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" modem="V.21" tag="CRP" value="FF C8 58"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="EOM" value="FF C8 71"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" modem="V.21" tag="CRP" value="FF C8 58"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="EOM" value="FF C8 71"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" modem="V.21" tag="CRP" value="FF C8 58"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCN" value="FF C8 5F"/>
+    </test>
+    <test name="MTGX15">
+        <step type="ANSWER"/>
+
+        <step dir="R" type="CNG"/>
+
+        <step dir="T" type="CED"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DIS" value="FF C8 01 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="CFR" value="FF C8 21"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="MSG" modem="V.27ter/4800" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="MPS" value="FF C8 72"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" modem="V.21" tag="CRP" value="FF C8 58"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="MPS" value="FF C8 72"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" modem="V.21" tag="CRP" value="FF C8 58"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="MPS" value="FF C8 72"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" modem="V.21" tag="CRP" value="FF C8 58"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCN" value="FF C8 5F"/>
+    </test>
+    <test name="MTGX16">
+        <step type="ANSWER"/>
+
+        <step dir="R" type="CNG"/>
+
+        <step dir="T" type="CED"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DIS" value="FF C8 01 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800" timeout="60000"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="CFR" value="FF C8 21"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="MSG" modem="V.27ter/4800" value="a4-white.tif" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="EOP" value="FF C8 74"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" modem="V.21" tag="CRP" value="FF C8 58"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="EOP" value="FF C8 74"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" modem="V.21" tag="CRP" value="FF C8 58"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="EOP" value="FF C8 74"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" modem="V.21" tag="CRP" value="FF C8 58"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCN" value="FF C8 5F"/>
+    </test>
+    <test name="MTGX17">
+        <step type="ANSWER"/>
+
+        <step dir="R" type="CNG"/>
+
+        <step dir="T" type="CED"/>
+        <step type="WAIT" value="75"/>
+        <!-- UNKNOWN_BCS_2 -->
+        <step dir="T" type="PREAMBLE" modem="V.21" value="37"/>
+        <step dir="T" type="HDLC" tag="GARBAGE" value="FF C8 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step type="WAIT" value="75"/>
+        <!-- UNKNOWN_BCS_2 -->
+        <step dir="T" type="PREAMBLE" modem="V.21" value="37"/>
+        <step dir="T" type="HDLC" tag="GARBAGE" value="FF C8 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DIS" value="FF C8 01 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DIS" value="FF C8 01 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCN" value="FF C8 5F"/>
+        <step dir="T" type="POSTAMBLE"/>
+    </test>
+    <test name="MTGX18">
+        <step type="ANSWER"/>
+
+        <step dir="R" type="CNG"/>
+
+        <step dir="T" type="CED"/>
+        <step type="WAIT" value="75"/>
+        <!-- UNKNOWN_BCS_3 -->
+        <step dir="T" type="PREAMBLE" modem="V.21" value="37"/>
+        <step dir="T" type="HDLC" tag="GARBAGE" value="A2 02 B5 C9 7E 67"/>
+
+        <step type="WAIT" value="75"/>
+        <!-- UNKNOWN_BCS_1 -->
+        <step dir="T" type="HDLC" tag="GARBAGE" value="FF C0 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DIS" value="FF C8 01 00 50 00">
+            <check name="MTGX18M01" desc="Unknown frame for command received"/>
+            <check name="MTGX18M02" desc="Unknown frame for command received"/>
+        </step>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800" timeout="60000"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="CFR" value="FF C8 21"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="MSG" modem="V.27ter/4800" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="EOP" value="FF C8 74"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MCF" value="FF C8 31"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCN" value="FF C8 5F"/>
+    </test>
+    <test name="MTGX19">
+        <step type="ANSWER"/>
+
+        <step dir="R" type="CNG"/>
+
+        <step dir="T" type="CED"/>
+        <step type="WAIT" value="75"/>
+        <!-- UNKNOWN_BCS_4 -->
+        <step dir="T" type="PREAMBLE" modem="V.21" value="43"/>
+
+        <step type="WAIT" value="6000"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DIS" value="FF C8 01 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00">
+            <check name="MTGX19M01" desc="Unknown frame for command received"/>
+        </step>
+        <step dir="R" type="TCF" modem="V.27ter/4800" timeout="60000"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="CFR" value="FF C8 21"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="MSG" modem="V.27ter/4800"  timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="EOP" value="FF C8 74"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MCF" value="FF C8 31"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCN" value="FF C8 5F"/>
+    </test>
+    <test name="MTGX20">
+        <step type="ANSWER"/>
+
+        <step dir="R" type="CNG"/>
+ 
+        <step dir="T" type="CED"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DIS" value="FF C8 01 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800" timeout="60000"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="CFR" value="FF C8 21"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="MSG" modem="V.27ter/4800" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="EOP" value="FF C8 74"/>
+
+        <step type="WAIT" value="75"/>
+        <!-- UNKNOWN_BCS_2 -->
+        <step dir="T" type="PREAMBLE" modem="V.21" value="37"/>
+        <step dir="T" type="HDLC" tag="GARBAGE" value="FF C8 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCN" value="FF C8 5F">
+            <check name="MTGX20M01" desc="Unknown frame for response received"/>
+        </step>
+    </test>
+    <test name="MTGX21">
+        <step type="ANSWER"/>
+
+        <step dir="R" type="CNG"/>
+
+        <step dir="T" type="CED"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DIS" value="FF C8 01 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800" timeout="60000"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="CFR" value="FF C8 21"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="MSG" modem="V.27ter/4800" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="MPS" value="FF C8 72"/>
+
+        <step type="WAIT" value="75"/>
+        <!-- UNKNOWN_BCS_3 -->
+        <step dir="T" type="PREAMBLE" modem="V.21" value="37"/>
+        <step dir="T" type="HDLC" tag="GARBAGE" value="A2 02 B5 C9 7E 67"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="MPS" value="FF C8 72">
+            <check name="MTGX21M01" desc="Unknown frame for response received"/>
+        </step>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MCF" value="FF C8 31"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="MSG" modem="V.27ter/4800"  timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="EOP" value="FF C8 74"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MCF" value="FF C8 31"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCN" value="FF C8 5F"/>
+    </test>
+    <test name="MTGX22">
+        <step type="ANSWER" value="a4-white.tif"/>
+
+        <step dir="R" type="CNG"/>
+
+        <step dir="T" type="CED"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DIS" value="FF C8 01 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS+" value="FF C8 C1 ..."/>
+        <step dir="R" type="TCF" modem="V.27ter/4800" timeout="60000"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="CFR" value="FF C8 21"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="MSG" modem="V.27ter/4800" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="MPS" value="FF C8 72"/>
+
+        <step type="WAIT" value="75"/>
+        <!-- UNKNOWN_BCS_4 -->
+        <step dir="T" type="PREAMBLE" modem="V.21" value="43"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="MPS" value="FF C8 72">
+            <check name="MTGX22M01" desc="Unknown frame for response received"/>
+        </step>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MCF" value="FF C8 31"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="MSG" modem="V.27ter/4800" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="EOP" value="FF C8 74"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MCF" value="FF C8 31"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCN" value="FF C8 5F"/>
+    </test>
+    <test name="MTGX23">
+        <step type="ANSWER" value="a4-white.tif"/>
+
+        <step dir="R" type="CNG"/>
+
+        <step dir="T" type="CED"/>
+        <step type="WAIT" value="75"/>
+        <!-- CALLED-CAP-GROUP -->
+        <step dir="T" type="PREAMBLE" modem="V.21" value="43"/>
+        <step dir="T" type="HDLC" tag="NSF" value="FF C0 04 B5 FF FF 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step dir="T" type="HDLC" tag="CSI" value="FF C0 02 0C 8C 4C CC 2C AC 6C EC 1C 9C D4 04 04 04 04 04 04 04 04 04"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step dir="T" type="HDLC" tag="DIS" value="FF C8 01 00 50 10 01 01 09 01 01 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS+" value="FF C8 C1 ...">
+            <check name="MTGX23M01" desc="DIS exhibits T.4 transmitter ability without password"/>
+        </step>
+        <step dir="R" type="TCF" modem="V.27ter/4800" timeout="60000"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="CFR" value="FF C8 21"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="MSG" modem="V.27ter/4800" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="EOP+" value="FF C8 74"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MCF" value="FF C8 31"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCN" value="FF C8 5F"/>
+    </test>
+</test-group>
+<test-group name="Annex J - Polling transmit test procedures">
+    <test name="MRGP01">
+        <step type="CALL"/>
+
+        <!--<step dir="T" type="CNG"/>-->
+
+        <step dir="R" type="CED"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="DIS" value="FF C8 01 ..." timeout="60000">
+            <check name="MRGP01M01" desc="DIS exhibits T.4 transmitter ability without password"/>
+        </step>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DTC" value="FF C8 81 00 50 10"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" tag="TSI" value="FF C8 42 ...">
+            <check name="MRGP01M04" desc="Polled, TSI prescence"/>
+            <check name="MRGP01M05" desc="Polled, TSI framing"/>
+            <check name="MRGP01M06" desc="Polled, TSI FIF content"/>
+        </step>
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 00 50 00">
+            <check name="MRGP01M02" desc="Polled, trasnmit DCS"/>
+            <check name="MRGP01M03" desc="Polled, DCS preamble"/>
+            <check name="MRGP01M07" desc="Polled, DCS framing"/>
+            <check name="MRGP01M08" desc="Polled, DCS FIF content"/>
+        </step>
+        <step dir="R" type="TCF">
+            <check name="MRGP01M09" desc="Polled, DCS TCF"/>
+        </step>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="CFR" value="FF C8 21"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="MSG" modem="V.27ter/4800" value="a4-white.tif" timeout="60000">
+            <check name="MRGP01M10" desc="Polled, MSG1"/>
+        </step>
+        <step dir="R" type="HDLC" modem="V.21" tag="MPS" value="FF C8 72">
+            <check name="MRGP01M11" desc="Polled, MPS"/>
+            <check name="MRGP01M12" desc="Polled, MPS preamble"/>
+            <check name="MRGP01M13" desc="Polled, MPS framing"/>
+        </step>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MCF" value="FF C8 31"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="MSG" modem="V.27ter/4800" timeout="60000">
+            <check name="MRGP01M14" desc="Polled, MSG1"/>
+        </step>
+        <step dir="R" type="HDLC" modem="V.21" tag="EOP" value="FF C8 74">
+            <check name="MRGP01M15" desc="Polled, EOP"/>
+            <check name="MRGP01M16" desc="Polled, EOP preamble"/>
+            <check name="MRGP01M17" desc="Polled, EOP framing"/>
+        </step>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MCF" value="FF C8 31"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCN" value="FF C8 5F">
+            <check name="MRGP01M18" desc="Polled, DCN"/>
+            <check name="MRGP01M19" desc="Polled, DCN preamble"/>
+            <check name="MRGP01M20" desc="Polled, DCN framing"/>
+            <check name="MRGP01M21" desc="Polled, disconnect after DCN"/>
+        </step>
+        <step dir="R" type="SILENCE"/>
+    </test>
+    <test name="MRGP02">
+        <step type="CALL" value="a4-white.tif"/>
+
+        <!--<step dir="T" type="CNG"/>-->
+
+        <step dir="R" type="CED"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="DIS" value="FF C8 01 ..." timeout="60000">
+            <check name="MRGP02M01" desc="DIS exhibits T.4 transmitter ability with password"/>
+        </step>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="PWD" value="FF C0 83 0C 8C 4C CC 2C AC 6C EC 1C 9C D4 04 04 04 04 04 04 04 04 04"/>
+        <step dir="T" type="HDLC" tag="DTC" value="FF C8 81 00 50 11 01 01 01 40"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="TSI" value="FF C8 42 ..."/>
+        <step dir="R" type="HDLC" tag="DCS" value="FF C8 41 ...">
+            <check name="MRGP02M02" desc="Polled, DUT accepts tester's password"/>
+        </step>
+        <step dir="R" type="SILENCE"/>
+        <step dir="R" type="TCF" modem="V.27ter/4800"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="CFR" value="FF C8 21"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="MSG" modem="V.27ter/4800" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="MPS" value="FF C8 72"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MCF" value="FF C8 31"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="MSG" modem="V.27ter/4800" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="EOP" value="FF C8 74"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MCF" value="FF C8 31"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCN" value="FF C8 5F"/>
+        <step dir="R" type="SILENCE"/>
+    </test>
+    <test name="MRGP03">
+        <step type="CALL"/>
+
+        <!--<step dir="T" type="CNG"/>-->
+
+        <step dir="R" type="CED"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="DIS" value="FF C8 01 ..." timeout="60000"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="PWD" value="FF C0 83 0C 8C 4C CC 2C AC 6C EC 1C 9C D4 04 04 04 04 04 04 04 04 04"/>
+        <step dir="T" type="HDLC" tag="DTC" value="FF C8 81 00 50 11 01 01 01 40"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCN" value="FF C8 5F">
+            <check name="MRGP03M01" desc="Polled, DUT rejects tester's password"/>
+        </step>
+        <step dir="R" type="SILENCE"/>
+    </test>
+    <test name="MRGP04">
+        <step type="CALL"/>
+
+        <!--<step dir="T" type="CNG"/>-->
+
+        <step dir="R" type="CED"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="DIS" value="FF C8 01 ..." timeout="60000"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DTC" value="FF C8 81 00 74 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCN" value="FF C8 5F">
+            <check name="MRGP04M01" desc="Polled, DUT rejects DTC for lack of password"/>
+        </step>
+        <step dir="R" type="SILENCE"/>
+    </test>
+    <test name="MRGP05">
+        <step type="CALL"/>
+
+        <!--<step dir="T" type="CNG"/>-->
+
+        <step dir="R" type="CED"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="DIS" value="FF C8 01 ..." timeout="60000">
+            <check name="MRGP05M01" desc="DIS not exhibit T.4 transmit capability"/>
+        </step>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DTC" value="FF C8 81 00 74 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCN" value="FF C8 5F">
+            <check name="MRGP05M02" desc="Polled, DUT rejects DTC for no document available"/>
+        </step>
+        <step dir="R" type="SILENCE"/>
+    </test>
+    <test name="MRGP06">
+        <step type="CALL" value="a4-white.tif"/>
+
+        <!--<step dir="T" type="CNG"/>-->
+
+        <step dir="R" type="CED"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="DIS" value="FF C8 01 ..." timeout="60000"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DIS" value="FF C8 01 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS+" value="FF C8 C1 ...">
+            <check name="MRGP06M01" desc="Polled, DUT accepts DIS in place of DTC"/>
+        </step>
+        <step dir="R" type="TCF" modem="V.27ter/4800" timeout="60000"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="CFR" value="FF C8 21"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="MSG" modem="V.27ter/4800" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="MPS" value="FF C8 72"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MCF" value="FF C8 31"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="MSG" modem="V.27ter/4800" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="EOP" value="FF C8 74"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MCF" value="FF C8 31"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCN" value="FF C8 5F"/>
+        <step dir="R" type="SILENCE"/>
+    </test>
+    <test name="MRGP07">
+        <step type="CALL"/>
+
+        <!--<step dir="T" type="CNG"/>-->
+
+        <step dir="R" type="CED"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="DIS" value="FF C8 01 ..." timeout="60000"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DIS" value="FF C8 01 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCN" value="FF C8 5F">
+            <check name="MRGP07M01" desc="Polled, DUT rejects DIS in place of DTC for lackof password"/>
+        </step>
+        <step dir="R" type="SILENCE"/>
+    </test>
+    <test name="MRGP08">
+        <step type="CALL"/>
+
+        <!--<step dir="T" type="CNG"/>-->
+
+        <step dir="R" type="CED"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="DIS" value="FF C8 01 ..." timeout="60000"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DIS" value="FF C8 01 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCN" value="FF C8 5F">
+            <check name="MRGP08M01" desc="Polled, DUT rejects DIS in place of DTC for no document available"/>
+        </step>
+        <step dir="R" type="SILENCE"/>
+    </test>
+    <test name="ORGP09">
+        <step type="CALL" value="a4-white.tif"/>
+
+        <!--<step dir="T" type="CNG"/>-->
+
+        <step dir="R" type="CED"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="DIS" value="FF C8 01 ..." timeout="60000"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="TCF" modem="V.27ter/4800" value="900"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="CFR" value="FF C8 21"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="MSG" modem="V.27ter/4800" value="a4-white.tif"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="EOM" value="FF C8 71"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="MCF" value="FF C8 31"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="DIS" value="FF C8 01 ..." timeout="60000"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DTC" value="FF C8 81 00 74 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 ...">
+            <check name="ORGP09M01" desc="Reception followed by invited polling transmission"/>
+        </step>
+        <step dir="R" type="TCF" modem="V.17/14400"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="CFR" value="FF C8 21"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="MSG" modem="V.17/14400" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="EOP" value="FF C8 74"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MCF" value="FF C8 31"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCN" value="FF C8 5F"/>
+        <step dir="R" type="SILENCE"/>
+    </test>
+    <test name="ORGP10">
+        <step type="CALL" value="a4-white.tif"/>
+
+        <!--<step dir="T" type="CNG"/>-->
+
+        <step dir="R" type="CED"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="DIS" value="FF C8 01 ..." timeout="60000"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DCS" value="FF C8 41 00 50 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="TCF" modem="V.27ter/4800" value="900"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="CFR" value="FF C8 21"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="MSG" modem="V.27ter/4800" value="a4-white.tif"/>
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="EOM" value="FF C8 71"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="MCF" value="FF C8 31"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="2000"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="DTC" value="FF C8 81 00 74 00"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCS" value="FF C8 41 ...">
+            <check name="ORGP10M01" desc="Reception followed by immediate polling transmission"/>
+        </step>
+        <step dir="R" type="TCF" modem="V.17/14400"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="CFR" value="FF C8 21"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="MSG" modem="V.17/14400" timeout="60000"/>
+        <step dir="R" type="HDLC" modem="V.21" tag="EOP" value="FF C8 74"/>
+        <step dir="R" type="SILENCE"/>
+
+        <step type="WAIT" value="75"/>
+        <step dir="T" type="PREAMBLE" modem="V.21"/>
+        <step dir="T" type="HDLC" tag="MCF" value="FF C8 31"/>
+        <step dir="T" type="POSTAMBLE"/>
+
+        <step dir="R" type="HDLC" modem="V.21" tag="DCN" value="FF C8 5F"/>
+        <step dir="R" type="SILENCE"/>
+    </test>
+</test-group>
+</fax-tests>

Added: freeswitch/trunk/libs/spandsp/src/Makefile.am
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/Makefile.am	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,379 @@
+##
+## SpanDSP - a series of DSP components for telephony
+##
+## Makefile.am - Process this file with automake to produce Makefile.in
+##
+## This program is free software; you can redistribute it and/or modify
+## it under the terms of the GNU Lesser General Public License version 2.1,
+## as published by the Free Software Foundation.
+##
+## This program is distributed in the hope that it will be useful,
+## but WITHOUT ANY WARRANTY; without even the implied warranty of
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+## GNU Lesser General Public License for more details.
+##
+## You should have received a copy of the GNU Lesser General Public
+## License along with this program; if not, write to the Free Software
+## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+##
+## $Id: Makefile.am,v 1.100 2008/08/14 14:06:05 steveu Exp $
+
+AM_CFLAGS = $(COMP_VENDOR_CFLAGS)
+AM_LDFLAGS = $(COMP_VENDOR_LDFLAGS)
+
+MAINTAINERCLEANFILES = Makefile.in
+
+INCLUDES = -I$(top_builddir)
+
+lib_LTLIBRARIES = libspandsp.la
+
+libspandsp_la_SOURCES = adsi.c \
+                        async.c \
+                        at_interpreter.c \
+                        awgn.c \
+                        bell_r2_mf.c \
+                        bert.c \
+                        bit_operations.c \
+                        bitstream.c \
+                        complex_filters.c \
+                        complex_vector_float.c \
+                        crc.c \
+                        dds_float.c \
+                        dds_int.c \
+                        dtmf.c \
+                        echo.c \
+                        fax.c \
+                        fsk.c \
+                        g711.c \
+                        g722_encode.c \
+                        g722_decode.c \
+                        g726.c \
+                        gsm0610_decode.c \
+                        gsm0610_encode.c \
+                        gsm0610_long_term.c \
+                        gsm0610_lpc.c \
+                        gsm0610_preprocess.c \
+                        gsm0610_rpe.c \
+                        gsm0610_short_term.c \
+                        hdlc.c \
+                        ima_adpcm.c \
+                        logging.c \
+                        lpc10_analyse.c \
+                        lpc10_decode.c \
+                        lpc10_encode.c \
+                        lpc10_placev.c \
+                        lpc10_voicing.c \
+                        modem_echo.c \
+                        modem_connect_tones.c \
+                        noise.c \
+                        oki_adpcm.c \
+                        playout.c \
+                        plc.c \
+                        power_meter.c \
+                        queue.c \
+                        schedule.c \
+                        sig_tone.c \
+                        silence_gen.c \
+                        super_tone_rx.c \
+                        super_tone_tx.c \
+                        t4.c \
+                        t30.c \
+                        t30_api.c \
+                        t30_logging.c \
+                        t31.c \
+                        t35.c \
+                        t38_core.c \
+                        t38_gateway.c \
+                        t38_non_ecm_buffer.c \
+                        t38_terminal.c \
+                        testcpuid.c \
+                        time_scale.c \
+                        tone_detect.c \
+                        tone_generate.c \
+                        v17rx.c \
+                        v17tx.c \
+                        v22bis_rx.c \
+                        v22bis_tx.c \
+                        v27ter_rx.c \
+                        v27ter_tx.c \
+                        v29rx.c \
+                        v29tx.c \
+                        v42.c \
+                        v42bis.c \
+                        v8.c \
+                        vector_float.c \
+                        vector_int.c
+
+libspandsp_la_LDFLAGS = -version-info @SPANDSP_LT_CURRENT@:@SPANDSP_LT_REVISION@:@SPANDSP_LT_AGE@
+
+nobase_include_HEADERS = spandsp/adsi.h \
+                         spandsp/async.h \
+                         spandsp/arctan2.h \
+                         spandsp/at_interpreter.h \
+                         spandsp/awgn.h \
+                         spandsp/bell_r2_mf.h \
+                         spandsp/bert.h \
+                         spandsp/biquad.h \
+                         spandsp/bit_operations.h \
+                         spandsp/bitstream.h \
+                         spandsp/crc.h \
+                         spandsp/complex.h \
+                         spandsp/complex_filters.h \
+                         spandsp/complex_vector_float.h \
+                         spandsp/dc_restore.h \
+                         spandsp/dds.h \
+                         spandsp/dtmf.h \
+                         spandsp/echo.h \
+                         spandsp/fax.h \
+                         spandsp/fax_modems.h \
+                         spandsp/fir.h \
+                         spandsp/fsk.h \
+                         spandsp/g168models.h \
+                         spandsp/g711.h \
+                         spandsp/g722.h \
+                         spandsp/g726.h \
+                         spandsp/gsm0610.h \
+                         spandsp/hdlc.h \
+                         spandsp/ima_adpcm.h \
+                         spandsp/logging.h \
+                         spandsp/lpc10.h \
+                         spandsp/modem_echo.h \
+                         spandsp/modem_connect_tones.h \
+                         spandsp/noise.h \
+                         spandsp/oki_adpcm.h \
+                         spandsp/playout.h \
+                         spandsp/plc.h \
+                         spandsp/power_meter.h \
+                         spandsp/queue.h \
+                         spandsp/schedule.h \
+                         spandsp/sig_tone.h \
+                         spandsp/silence_gen.h \
+                         spandsp/super_tone_rx.h \
+                         spandsp/super_tone_tx.h \
+                         spandsp/t4.h \
+                         spandsp/t30.h \
+                         spandsp/t30_api.h \
+                         spandsp/t30_fcf.h \
+                         spandsp/t30_logging.h \
+                         spandsp/t31.h \
+                         spandsp/t35.h \
+                         spandsp/t38_core.h \
+                         spandsp/t38_gateway.h \
+                         spandsp/t38_non_ecm_buffer.h \
+                         spandsp/t38_terminal.h \
+                         spandsp/telephony.h \
+                         spandsp/time_scale.h \
+                         spandsp/timing.h \
+                         spandsp/tone_detect.h \
+                         spandsp/tone_generate.h \
+                         spandsp/v17rx.h \
+                         spandsp/v17tx.h \
+                         spandsp/v22bis.h \
+                         spandsp/v27ter_rx.h \
+                         spandsp/v27ter_tx.h \
+                         spandsp/v29rx.h \
+                         spandsp/v29tx.h \
+                         spandsp/v42.h \
+                         spandsp/v42bis.h \
+                         spandsp/v8.h \
+                         spandsp/vector_float.h \
+                         spandsp/vector_int.h \
+                         spandsp/version.h
+
+nodist_include_HEADERS = spandsp.h
+
+noinst_HEADERS =        faxfont.h \
+                        filter_tools.h \
+                        gsm0610_local.h \
+                        lpc10_encdecs.h \
+                        t30_local.h \
+                        t4_states.h \
+                        v17rx_constellation_maps.h \
+                        v17tx_constellation_maps.h \
+                        v29tx_constellation_maps.h
+
+noinst_PROGRAMS = make_at_dictionary \
+                  make_modem_filter
+
+make_modem_filter_SOURCES = make_modem_filter.c \
+                            filter_tools.c
+
+# We need to run make_at_dictionary, so it generates the
+# at_interpreter_dictionary.h file
+
+at_interpreter.$(OBJEXT): at_interpreter_dictionary.h
+
+at_interpreter.lo: at_interpreter_dictionary.h
+
+at_interpreter_dictionary.h: make_at_dictionary$(EXEEXT)
+	./make_at_dictionary$(EXEEXT) >at_interpreter_dictionary.h
+
+v17rx.$(OBJEXT): v17rx_fixed_rrc.h v17rx_floating_rrc.h
+
+v17rx.lo: v17rx_fixed_rrc.h v17rx_floating_rrc.h
+
+v17rx_fixed_rrc.h: make_modem_filter$(EXEEXT)
+	./make_modem_filter$(EXEEXT) -m V.17 -i -r >v17rx_fixed_rrc.h
+
+v17rx_floating_rrc.h: make_modem_filter$(EXEEXT)
+	./make_modem_filter$(EXEEXT) -m V.17 -r >v17rx_floating_rrc.h
+
+v17tx.$(OBJEXT): v17tx_fixed_rrc.h v17tx_floating_rrc.h
+
+v17tx.lo: v17tx_fixed_rrc.h v17tx_floating_rrc.h
+
+v17tx_fixed_rrc.h: make_modem_filter$(EXEEXT)
+	./make_modem_filter$(EXEEXT) -m V.17 -i -t >v17tx_fixed_rrc.h
+
+v17tx_floating_rrc.h: make_modem_filter$(EXEEXT)
+	./make_modem_filter$(EXEEXT) -m V.17 -t >v17tx_floating_rrc.h
+
+v22bis_rx.$(OBJEXT): v22bis_rx_1200_fixed_rrc.h \
+                     v22bis_rx_2400_fixed_rrc.h \
+                     v22bis_rx_1200_floating_rrc.h \
+                     v22bis_rx_2400_floating_rrc.h
+
+v22bis_rx.lo: v22bis_rx_1200_fixed_rrc.h \
+              v22bis_rx_2400_fixed_rrc.h \
+              v22bis_rx_1200_floating_rrc.h \
+              v22bis_rx_2400_floating_rrc.h
+
+v22bis_rx_1200_fixed_rrc.h: make_modem_filter$(EXEEXT)
+	./make_modem_filter$(EXEEXT) -m V.22bis1200 -i -r >v22bis_rx_1200_fixed_rrc.h
+
+v22bis_rx_2400_fixed_rrc.h: make_modem_filter$(EXEEXT)
+	./make_modem_filter$(EXEEXT) -m V.22bis2400 -i -r >v22bis_rx_2400_fixed_rrc.h
+
+v22bis_rx_1200_floating_rrc.h: make_modem_filter$(EXEEXT)
+	./make_modem_filter$(EXEEXT) -m V.22bis1200 -r >v22bis_rx_1200_floating_rrc.h
+
+v22bis_rx_2400_floating_rrc.h: make_modem_filter$(EXEEXT)
+	./make_modem_filter$(EXEEXT) -m V.22bis2400 -r >v22bis_rx_2400_floating_rrc.h
+
+v22bis_tx.$(OBJEXT): v22bis_tx_fixed_rrc.h v22bis_tx_floating_rrc.h
+
+v22bis_tx.lo: v22bis_tx_fixed_rrc.h v22bis_tx_floating_rrc.h
+
+v22bis_tx_fixed_rrc.h: make_modem_filter$(EXEEXT)
+	./make_modem_filter$(EXEEXT) -m V.22bis -i -t >v22bis_tx_fixed_rrc.h
+
+v22bis_tx_floating_rrc.h: make_modem_filter$(EXEEXT)
+	./make_modem_filter$(EXEEXT) -m V.22bis -t >v22bis_tx_floating_rrc.h
+
+v27ter_rx_.$(OBJEXT): v27ter_rx_2400_fixed_rrc.h \
+                      v27ter_rx_4800_fixed_rrc.h \
+                      v27ter_rx_2400_floating_rrc.h \
+                      v27ter_rx_4800_floating_rrc.h
+
+v27ter_rx.lo: v27ter_rx_2400_fixed_rrc.h \
+              v27ter_rx_4800_fixed_rrc.h \
+              v27ter_rx_2400_floating_rrc.h \
+              v27ter_rx_4800_floating_rrc.h
+
+v27ter_rx_2400_fixed_rrc.h: make_modem_filter$(EXEEXT)
+	./make_modem_filter$(EXEEXT) -m V.27ter2400 -i -r >v27ter_rx_2400_fixed_rrc.h
+
+v27ter_rx_4800_fixed_rrc.h: make_modem_filter$(EXEEXT)
+	./make_modem_filter$(EXEEXT) -m V.27ter4800 -i -r >v27ter_rx_4800_fixed_rrc.h
+
+v27ter_rx_2400_floating_rrc.h: make_modem_filter$(EXEEXT)
+	./make_modem_filter$(EXEEXT) -m V.27ter2400 -r >v27ter_rx_2400_floating_rrc.h
+
+v27ter_rx_4800_floating_rrc.h: make_modem_filter$(EXEEXT)
+	./make_modem_filter$(EXEEXT) -m V.27ter4800 -r >v27ter_rx_4800_floating_rrc.h
+
+v27ter_tx_.$(OBJEXT): v27ter_tx_2400_fixed_rrc.h \
+                      v27ter_tx_4800_fixed_rrc.h \
+                      v27ter_tx_2400_floating_rrc.h \
+                      v27ter_tx_4800_floating_rrc.h
+
+v27ter_tx.lo: v27ter_tx_2400_fixed_rrc.h \
+              v27ter_tx_4800_fixed_rrc.h \
+              v27ter_tx_2400_floating_rrc.h \
+              v27ter_tx_4800_floating_rrc.h
+
+v27ter_tx_2400_fixed_rrc.h: make_modem_filter$(EXEEXT)
+	./make_modem_filter$(EXEEXT) -m V.27ter2400 -i -t >v27ter_tx_2400_fixed_rrc.h
+
+v27ter_tx_4800_fixed_rrc.h: make_modem_filter$(EXEEXT)
+	./make_modem_filter$(EXEEXT) -m V.27ter4800 -i -t >v27ter_tx_4800_fixed_rrc.h
+
+v27ter_tx_2400_floating_rrc.h: make_modem_filter$(EXEEXT)
+	./make_modem_filter$(EXEEXT) -m V.27ter2400 -t >v27ter_tx_2400_floating_rrc.h
+
+v27ter_tx_4800_floating_rrc.h: make_modem_filter$(EXEEXT)
+	./make_modem_filter$(EXEEXT) -m V.27ter4800 -t >v27ter_tx_4800_floating_rrc.h
+
+v29rx.$(OBJEXT): v29rx_fixed_rrc.h v29rx_floating_rrc.h
+
+v29rx.lo: v29rx_fixed_rrc.h v29rx_floating_rrc.h
+
+v29rx_fixed_rrc.h: make_modem_filter$(EXEEXT)
+	./make_modem_filter$(EXEEXT) -m V.29 -i -r >v29rx_fixed_rrc.h
+
+v29rx_floating_rrc.h: make_modem_filter$(EXEEXT)
+	./make_modem_filter$(EXEEXT) -m V.29 -r >v29rx_floating_rrc.h
+
+v29tx.$(OBJEXT): v29tx_fixed_rrc.h v29tx_floating_rrc.h
+
+v29tx.lo: v29tx_fixed_rrc.h v29tx_floating_rrc.h
+
+v29tx_fixed_rrc.h: make_modem_filter$(EXEEXT)
+	./make_modem_filter$(EXEEXT) -m V.29 -i -t >v29tx_fixed_rrc.h
+
+v29tx_floating_rrc.h: make_modem_filter$(EXEEXT)
+	./make_modem_filter$(EXEEXT) -m V.29 -t >v29tx_floating_rrc.h
+
+DSP = libspandsp.dsp
+VCPROJ = libspandsp.vcproj
+
+WIN32SOURCES = $(libspandsp_la_SOURCES) msvc/gettimeofday.c
+WIN32HEADERS = $(nobase_include_HEADERS) spandsp.h
+
+DSPOUT = | awk '{printf("%s\r\n", $$0)}' >> $(DSP)
+VCPROJOUT = | awk '{printf("%s\r\n", $$0)}' >> $(VCPROJ)
+
+$(DSP): msvc/msvcproj.head msvc/msvcproj.foot Makefile.am
+	echo "creating $(DSP)"
+	@(cp $(srcdir)/msvc/msvcproj.head $(DSP); \
+	echo "# Begin Group \"Source Files\"" $(DSPOUT); \
+	for file in $(WIN32SOURCES); do \
+	echo "# Begin Source File" $(DSPOUT); \
+	echo "" $(DSPOUT); \
+	echo "SOURCE=.\\"$$file $(DSPOUT); \
+	echo "# End Source File" $(DSPOUT); \
+	done; \
+	echo "# End Group" $(DSPOUT); \
+	echo "# Begin Group \"Header Files\"" $(DSPOUT); \
+	for file in $(WIN32HEADERS); do \
+	echo "# Begin Source File" $(DSPOUT); \
+	echo "" $(DSPOUT); \
+	echo "SOURCE=.\\"$$file $(DSPOUT); \
+	echo "# End Source File" $(DSPOUT); \
+	done; \
+	echo "# End Group" $(DSPOUT); \
+	cat $(srcdir)/msvc/msvcproj.foot $(DSPOUT) )
+
+$(VCPROJ): msvc/vc8proj.head msvc/vc8proj.foot Makefile.am
+	echo "creating $(VCPROJ)"
+	@(cp $(srcdir)/msvc/vc8proj.head $(VCPROJ); \
+	for file in $(WIN32SOURCES); do \
+		echo "<File RelativePath=\""$$file"\"></File>" $(VCPROJOUT); \
+	done; \
+	echo "</Filter><Filter  Name=\"Header Files\">" $(VCPROJOUT); \
+	for file in $(WIN32HEADERS); do \
+		echo "<File RelativePath=\""$$file"\"></File>" $(VCPROJOUT); \
+	done; \
+	cat $(srcdir)/msvc/vc8proj.foot $(VCPROJOUT) )
+
+spandsp/version.h:
+	NOWDATE=`date --utc +"%Y%m%d"` ; \
+	NOWTIME=`date --utc +"%H%M%S"` ; \
+	sed 's/$$SPANDSP_RELEASE_DATE/'$$NOWDATE'/;s/$$SPANDSP_RELEASE_TIME/'$$NOWTIME'/' \
+		<spandsp/version.h.in >spandsp/version.h
+
+dist-hook:
+	NOWDATE=`date --utc +"%Y%m%d"` ; \
+	NOWTIME=`date --utc +"%H%M%S"` ; \
+	sed 's/$$SPANDSP_RELEASE_DATE/'$$NOWDATE'/;s/$$SPANDSP_RELEASE_TIME/'$$NOWTIME'/' \
+		<spandsp/version.h.in >spandsp/version.h

Added: freeswitch/trunk/libs/spandsp/src/adsi.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/adsi.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,1070 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * adsi.c - Analogue display service interfaces of various types, including
+ *          ADSI, TDD and most caller ID formats.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2003 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: adsi.c,v 1.59 2008/07/02 14:48:25 steveu Exp $
+ */
+
+/*! \file */
+
+#if defined(HAVE_CONFIG_H)
+#include <config.h>
+#endif
+
+#include <inttypes.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include "floating_fudge.h"
+#if defined(HAVE_TGMATH_H)
+#include <tgmath.h>
+#endif
+#if defined(HAVE_MATH_H)
+#include <math.h>
+#endif
+#include <assert.h>
+
+#include "spandsp/telephony.h"
+#include "spandsp/logging.h"
+#include "spandsp/queue.h"
+#include "spandsp/complex.h"
+#include "spandsp/dds.h"
+#include "spandsp/power_meter.h"
+#include "spandsp/async.h"
+#include "spandsp/crc.h"
+#include "spandsp/fsk.h"
+#include "spandsp/tone_detect.h"
+#include "spandsp/tone_generate.h"
+#include "spandsp/super_tone_rx.h"
+#include "spandsp/dtmf.h"
+#include "spandsp/adsi.h"
+
+#define BAUDOT_FIGURE_SHIFT     0x1B
+#define BAUDOT_LETTER_SHIFT     0x1F
+
+#define SOH 0x01
+#define STX 0x02
+#define ETX 0x03
+#define DLE 0x10
+#define SUB 0x1A
+
+static uint16_t adsi_encode_baudot(adsi_tx_state_t *s, uint8_t ch);
+static uint8_t adsi_decode_baudot(adsi_rx_state_t *s, uint8_t ch);
+
+static int adsi_tx_get_bit(void *user_data)
+{
+    int bit;
+    adsi_tx_state_t *s;
+    
+    s = (adsi_tx_state_t *) user_data;
+    /* This is similar to the async. handling code in fsk.c, but a few special
+       things are needed in the preamble, and postamble of an ADSI message. */
+    if (s->bit_no < s->preamble_len)
+    {
+        /* Alternating bit preamble */
+        bit = s->bit_no & 1;
+        s->bit_no++;
+    }
+    else if (s->bit_no < s->preamble_len + s->preamble_ones_len)
+    {
+        /* All 1s for receiver conditioning */
+        /* NB: The receiver is an async one. It needs a rest after the
+               alternating 1/0 sequence so it can reliably pick up on
+               the next start bit, and sync to the byte stream. */
+        /* The length of this period varies with the circumstance */
+        bit = 1;
+        s->bit_no++;
+    }
+    else if (s->bit_no <= s->preamble_len + s->preamble_ones_len)
+    {
+        /* Push out the 8 bit async. chars, with an appropriate number of stop bits */
+        if (s->bit_pos == 0)
+        {
+            /* Start bit */
+            bit = 0;
+            s->bit_pos++;
+        }
+        else if (s->bit_pos < 1 + 8)
+        {
+            bit = (s->msg[s->byte_no] >> (s->bit_pos - 1)) & 1;
+            s->bit_pos++;
+        }
+        else if (s->bit_pos < 1 + 8 + s->stop_bits - 1)
+        {
+            /* Stop bit */
+            bit = 1;
+            s->bit_pos++;
+        }
+        else
+        {
+            /* Stop bit */
+            bit = 1;
+            s->bit_pos = 0;
+            if (++s->byte_no >= s->msg_len)
+                s->bit_no++;
+        }
+    }
+    else if (s->bit_no <= s->preamble_len + s->preamble_ones_len + s->postamble_ones_len)
+    {
+        /* Extra stop bits beyond the last character, to meet the specs., and ensure
+           all bits are out of the DSP before we shut off the FSK modem. */
+        bit = 1;
+        s->bit_no++;
+    }
+    else
+    {
+        bit = PUTBIT_END_OF_DATA;
+        if (s->tx_signal_on)
+        {
+            /* The FSK should now be switched off. */
+            s->tx_signal_on = FALSE;
+            s->msg_len = 0;
+        }
+    }
+    //printf("Tx bit %d\n", bit);
+    return bit;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int adsi_tdd_get_async_byte(void *user_data)
+{
+    adsi_tx_state_t *s;
+    
+    s = (adsi_tx_state_t *) user_data;
+    if (s->byte_no < s->msg_len)
+        return s->msg[s->byte_no++];
+    if (s->tx_signal_on)
+    {
+        /* The FSK should now be switched off. */
+        s->tx_signal_on = FALSE;
+        s->msg_len = 0;
+    }
+    return 0x1F;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void adsi_rx_put_bit(void *user_data, int bit)
+{
+    adsi_rx_state_t *s;
+    int i;
+    int sum;
+
+    s = (adsi_rx_state_t *) user_data;
+    if (bit < 0)
+    {
+        /* Special conditions */
+        switch (bit)
+        {
+        case PUTBIT_CARRIER_UP:
+            span_log(&s->logging, SPAN_LOG_FLOW, "Carrier up.\n");
+            s->consecutive_ones = 0;
+            s->bit_pos = 0;
+            s->in_progress = 0;
+            s->msg_len = 0;
+            s->baudot_shift = 0;
+            break;
+        case PUTBIT_CARRIER_DOWN:
+            span_log(&s->logging, SPAN_LOG_FLOW, "Carrier down.\n");
+            break;
+        default:
+            span_log(&s->logging, SPAN_LOG_WARNING, "Unexpected special put bit value - %d!\n", bit);
+            break;
+        }
+        return;
+    }
+    bit &= 1;
+    if (s->bit_pos == 0)
+    {
+        if (bit == 0)
+        {
+            /* Start bit */
+            s->bit_pos++;
+            if (s->consecutive_ones > 10)
+            {
+                /* This is a line idle condition, which means we should
+                   restart message acquisition */
+                s->msg_len = 0;
+            }
+            s->consecutive_ones = 0;
+        }
+        else
+        {
+            s->consecutive_ones++;
+        }
+    }
+    else if (s->bit_pos <= 8)
+    {
+        s->in_progress >>= 1;
+        if (bit)
+            s->in_progress |= 0x80;
+        s->bit_pos++;
+    }
+    else
+    {
+        /* Stop bit */
+        if (bit)
+        {
+            if (s->msg_len < 256)
+            {
+                if (s->standard == ADSI_STANDARD_JCLIP)
+                {
+                    if (s->msg_len == 0)
+                    {
+                        /* A message should start DLE SOH, but let's just check 
+                           we are starting with a DLE for now */
+                        if (s->in_progress == (0x80 | DLE))
+                            s->msg[s->msg_len++] = (uint8_t) s->in_progress;
+                    }
+                    else
+                    {
+                        s->msg[s->msg_len++] = (uint8_t) s->in_progress;
+                    }
+                    if (s->msg_len >= 11  &&  s->msg_len == ((s->msg[6] & 0x7F) + 11))
+                    {
+                        /* Test the CRC-16 */
+                        if (crc_itu16_calc(s->msg + 2, s->msg_len - 2, 0) == 0)
+                        {
+                            /* Strip off the parity bits. It doesn't seem
+                               worthwhile actually checking the parity if a
+                               CRC check has succeeded. */
+                            for (i = 0;  i < s->msg_len - 2;  i++)
+                                s->msg[i] &= 0x7F;
+                            /* Put everything, except the CRC octets */
+                            s->put_msg(s->user_data, s->msg, s->msg_len - 2);
+                        }
+                        else
+                        {
+                            span_log(&s->logging, SPAN_LOG_WARNING, "CRC failed\n");
+                        }
+                        s->msg_len = 0;
+                    }
+                }
+                else
+                {
+                    s->msg[s->msg_len++] = (uint8_t) s->in_progress;
+                    if (s->msg_len >= 3  &&  s->msg_len == (s->msg[1] + 3))
+                    {
+                        /* Test the checksum */
+                        sum = 0;
+                        for (i = 0;  i < s->msg_len - 1;  i++)
+                            sum += s->msg[i];
+                        if ((-sum & 0xFF) == s->msg[i])
+                            s->put_msg(s->user_data, s->msg, s->msg_len - 1);
+                        else
+                            span_log(&s->logging, SPAN_LOG_WARNING, "Sumcheck failed\n");
+                        s->msg_len = 0;
+                    }
+                }
+            }
+        }
+        else
+        {
+            s->framing_errors++;
+        }
+        s->bit_pos = 0;
+        s->in_progress = 0;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static void adsi_tdd_put_async_byte(void *user_data, int byte)
+{
+    adsi_rx_state_t *s;
+    uint8_t octet;
+    
+    s = (adsi_rx_state_t *) user_data;
+    if (byte < 0)
+    {
+        /* Special conditions */
+        switch (byte)
+        {
+        case PUTBIT_CARRIER_UP:
+            span_log(&s->logging, SPAN_LOG_FLOW, "Carrier up.\n");
+            s->consecutive_ones = 0;
+            s->bit_pos = 0;
+            s->in_progress = 0;
+            s->msg_len = 0;
+            s->baudot_shift = 0;
+            break;
+        case PUTBIT_CARRIER_DOWN:
+            span_log(&s->logging, SPAN_LOG_FLOW, "Carrier down.\n");
+            if (s->msg_len > 0)
+            {
+                /* Whatever we have to date constitutes the message */
+                s->put_msg(s->user_data, s->msg, s->msg_len);
+                s->msg_len = 0;
+            }
+            break;
+        default:
+            span_log(&s->logging, SPAN_LOG_WARNING, "Unexpected special put byte value - %d!\n", byte);
+            break;
+        }
+        return;
+    }
+    if ((octet = adsi_decode_baudot(s, (uint8_t) byte)))
+        s->msg[s->msg_len++] = octet;
+    if (s->msg_len >= 256)
+    {
+        s->put_msg(s->user_data, s->msg, s->msg_len);
+        s->msg_len = 0;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static void adsi_rx_dtmf(void *user_data, const char *digits, int len)
+{
+    adsi_rx_state_t *s;
+
+    s = (adsi_rx_state_t *) user_data;
+    if (s->msg_len == 0)
+    {
+        /* Message starting. Start a 10s timeout, to make things more noise
+           tolerant for a detector running continuously when on hook. */
+        s->in_progress = 80000;
+    }
+    /* It seems all the DTMF variants are a string of digits and letters,
+       terminated by a "#", or a "C". It appears these are unambiguous, and
+       non-conflicting. */
+    for (  ;  len  &&  s->msg_len < 256;  len--)
+    {
+        s->msg[s->msg_len++] = *digits;
+        if (*digits == '#'  ||  *digits == 'C')
+        {
+            s->put_msg(s->user_data, s->msg, s->msg_len);
+            s->msg_len = 0;
+        }
+        digits++;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static void start_tx(adsi_tx_state_t *s)
+{
+    switch (s->standard)
+    {
+    case ADSI_STANDARD_CLASS:
+        fsk_tx_init(&(s->fsktx), &preset_fsk_specs[FSK_BELL202], adsi_tx_get_bit, s);
+        break;
+    case ADSI_STANDARD_CLIP:
+    case ADSI_STANDARD_ACLIP:
+    case ADSI_STANDARD_JCLIP:
+        fsk_tx_init(&(s->fsktx), &preset_fsk_specs[FSK_V23CH1], adsi_tx_get_bit, s);
+        break;
+    case ADSI_STANDARD_CLIP_DTMF:
+        dtmf_tx_init(&(s->dtmftx));
+        break;
+    case ADSI_STANDARD_TDD:
+        fsk_tx_init(&(s->fsktx), &preset_fsk_specs[FSK_WEITBRECHT], async_tx_get_bit, &(s->asynctx));
+        async_tx_init(&(s->asynctx), 5, ASYNC_PARITY_NONE, 2, FALSE, adsi_tdd_get_async_byte, s);
+        /* Schedule an explicit shift at the start of baudot transmission */
+        s->baudot_shift = 2;
+        break;
+    }
+    s->tx_signal_on = TRUE;
+}
+/*- End of function --------------------------------------------------------*/
+
+int adsi_rx(adsi_rx_state_t *s, const int16_t *amp, int len)
+{
+    switch (s->standard)
+    {
+    case ADSI_STANDARD_CLIP_DTMF:
+        /* Apply a message timeout. */
+        s->in_progress -= len;
+        if (s->in_progress <= 0)
+            s->msg_len = 0;
+        dtmf_rx(&(s->dtmfrx), amp, len);
+        break;
+    default:
+        fsk_rx(&(s->fskrx), amp, len);
+        break;
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+adsi_rx_state_t *adsi_rx_init(adsi_rx_state_t *s,
+                              int standard,
+                              put_msg_func_t put_msg,
+                              void *user_data)
+{
+    if (s == NULL)
+    {
+        if ((s = (adsi_rx_state_t *) malloc(sizeof(*s))) == NULL)
+            return NULL;
+    }
+    memset(s, 0, sizeof(*s));
+    s->put_msg = put_msg;
+    s->user_data = user_data;
+    switch (standard)
+    {
+    case ADSI_STANDARD_CLASS:
+        fsk_rx_init(&(s->fskrx), &preset_fsk_specs[FSK_BELL202], FALSE, adsi_rx_put_bit, s);
+        break;
+    case ADSI_STANDARD_CLIP:
+    case ADSI_STANDARD_ACLIP:
+    case ADSI_STANDARD_JCLIP:
+        fsk_rx_init(&(s->fskrx), &preset_fsk_specs[FSK_V23CH1], FALSE, adsi_rx_put_bit, s);
+        break;
+    case ADSI_STANDARD_CLIP_DTMF:
+        dtmf_rx_init(&(s->dtmfrx), adsi_rx_dtmf, s);
+        break;
+    case ADSI_STANDARD_TDD:
+        fsk_rx_init(&(s->fskrx), &preset_fsk_specs[FSK_WEITBRECHT], FALSE, async_rx_put_bit, &(s->asyncrx));
+        async_rx_init(&(s->asyncrx), 5, ASYNC_PARITY_NONE, 2, TRUE, adsi_tdd_put_async_byte, s);
+        break;
+    }
+    s->standard = standard;
+    span_log_init(&s->logging, SPAN_LOG_NONE, NULL);
+    return s;
+}
+/*- End of function --------------------------------------------------------*/
+
+int adsi_tx(adsi_tx_state_t *s, int16_t *amp, int max_len)
+{
+    int len;
+    int lenx;
+
+    len = tone_gen(&(s->alert_tone_gen), amp, max_len);
+    if (s->tx_signal_on)
+    {
+        switch (s->standard)
+        {
+        case ADSI_STANDARD_CLIP_DTMF:
+            if (len < max_len)
+                len += dtmf_tx(&(s->dtmftx), amp, max_len - len);
+            break;
+        default:
+            if (len < max_len)
+            {
+                if ((lenx = fsk_tx(&(s->fsktx), amp + len, max_len - len)) <= 0)
+                    s->tx_signal_on = FALSE;
+                len += lenx;
+            }
+            break;
+        }
+    }
+    return len;
+}
+/*- End of function --------------------------------------------------------*/
+
+void adsi_tx_send_alert_tone(adsi_tx_state_t *s)
+{
+    tone_gen_init(&(s->alert_tone_gen), &(s->alert_tone_desc));
+}
+/*- End of function --------------------------------------------------------*/
+
+void adsi_tx_set_preamble(adsi_tx_state_t *s,
+                          int preamble_len,
+                          int preamble_ones_len,
+                          int postamble_ones_len,
+                          int stop_bits)
+{
+    if (preamble_len < 0)
+    {
+        if (s->standard == ADSI_STANDARD_JCLIP)
+            s->preamble_len = 0;
+        else
+            s->preamble_len = 300;
+    }
+    else
+    {
+        s->preamble_len = preamble_len;
+    }
+    if (preamble_ones_len < 0)
+    {
+        if (s->standard == ADSI_STANDARD_JCLIP)
+            s->preamble_ones_len = 75;
+        else
+            s->preamble_ones_len = 80;
+    }
+    else
+    {
+        s->preamble_ones_len = preamble_ones_len;
+    }
+    if (postamble_ones_len < 0)
+    {
+        if (s->standard == ADSI_STANDARD_JCLIP)
+            s->postamble_ones_len = 5;
+        else
+            s->postamble_ones_len = 5;
+    }
+    else
+    {
+        s->postamble_ones_len = postamble_ones_len;
+    }
+    if (stop_bits < 0)
+    {
+        if (s->standard == ADSI_STANDARD_JCLIP)
+            s->stop_bits = 4;
+        else
+            s->stop_bits = 1;
+    }
+    else
+    {
+        s->stop_bits = stop_bits;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+int adsi_tx_put_message(adsi_tx_state_t *s, const uint8_t *msg, int len)
+{
+    int i;
+    int j;
+    int k;
+    int byte;
+    int parity;
+    int sum;
+    size_t ii;
+    uint16_t crc_value;
+
+    /* Don't inject a new message when a previous one is still in progress */
+    if (s->msg_len > 0)
+        return 0;
+    if (!s->tx_signal_on)
+    {
+        /* We need to restart the modem */
+        start_tx(s);
+    }
+    switch (s->standard)
+    {
+    case ADSI_STANDARD_CLIP_DTMF:
+        if (len >= 128)
+            return -1;
+        len -= (int) dtmf_tx_put(&(s->dtmftx), (char *) msg, len);
+        break;
+    case ADSI_STANDARD_JCLIP:
+        if (len > 128 - 9)
+            return -1;
+        i = 0;
+        s->msg[i++] = DLE;
+        s->msg[i++] = SOH;
+        s->msg[i++] = 0x07; //header
+        s->msg[i++] = DLE;
+        s->msg[i++] = STX;
+        s->msg[i++] = msg[0];
+        s->msg[i++] = (uint8_t) (len - 2);
+        /* We might need to byte stuff the overall length, but the rest of the
+           message should already be stuffed. */
+        if (len - 2 == DLE)
+            s->msg[i++] = DLE;
+        memcpy(&s->msg[i], &msg[2], len - 2);
+        i += len - 2;
+        s->msg[i++] = DLE;
+        s->msg[i++] = ETX;
+        
+        /* Set the parity bits */
+        for (j = 0;  j < i;  j++)
+        {
+            byte = s->msg[j];
+            parity = 0;
+            for (k = 1;  k <= 7;  k++)
+                parity ^= (byte << k);
+            s->msg[j] = (s->msg[j] & 0x7F) | ((uint8_t) parity & 0x80);
+        }
+
+        crc_value = crc_itu16_calc(s->msg + 2, i - 2, 0);
+        s->msg[i++] = (uint8_t) (crc_value & 0xFF);
+        s->msg[i++] = (uint8_t) ((crc_value >> 8) & 0xFF);
+        s->msg_len = i;
+        break;
+    case ADSI_STANDARD_TDD:
+        if (len > 255)
+            return -1;
+        memcpy(s->msg, msg, len);
+        s->msg_len = len;
+        break;
+    default:
+        if (len > 255)
+            return -1;
+        memcpy(s->msg, msg, len);
+        /* Force the length in case it is wrong */
+        s->msg[1] = (uint8_t) (len - 2);
+        /* Add the sumcheck */
+        sum = 0;
+        for (ii = 0;  ii < (size_t) len;  ii++)
+            sum += s->msg[ii];
+        s->msg[len] = (uint8_t) ((-sum) & 0xFF);
+        s->msg_len = len + 1;
+        break;
+    }    
+    /* Prepare the bit sequencing */
+    s->byte_no = 0;
+    s->bit_pos = 0;
+    s->bit_no = 0;
+    return len;
+}
+/*- End of function --------------------------------------------------------*/
+
+adsi_tx_state_t *adsi_tx_init(adsi_tx_state_t *s, int standard)
+{
+    if (s == NULL)
+    {
+        if ((s = (adsi_tx_state_t *) malloc(sizeof(*s))) == NULL)
+            return NULL;
+    }
+    memset(s, 0, sizeof(*s));
+    make_tone_gen_descriptor(&(s->alert_tone_desc),
+                             2130,
+                             -13,
+                             2750,
+                             -13,
+                             110,
+                             60,
+                             0,
+                             0,
+                             FALSE);
+    s->standard = standard;
+    adsi_tx_set_preamble(s, -1, -1, -1, -1);
+    span_log_init(&s->logging, SPAN_LOG_NONE, NULL);
+    start_tx(s);
+    return s;
+}
+/*- End of function --------------------------------------------------------*/
+
+static uint16_t adsi_encode_baudot(adsi_tx_state_t *s, uint8_t ch)
+{
+    static const uint8_t conv[128] =
+    {
+        0x00, /* NUL */
+        0xFF, /*   */
+        0xFF, /*   */
+        0xFF, /*   */
+        0xFF, /*   */
+        0xFF, /*   */
+        0xFF, /*   */
+        0xFF, /*   */
+        0xFF, /*   */
+        0xFF, /*   */
+        0x42, /* LF */
+        0xFF, /*   */
+        0xFF, /*   */
+        0x48, /* CR */
+        0xFF, /*   */
+        0xFF, /*   */
+        0xFF, /*   */
+        0xFF, /*   */
+        0xFF, /*   */
+        0xFF, /*   */
+        0xFF, /*   */
+        0xFF, /*   */
+        0xFF, /*   */
+        0xFF, /*   */
+        0xFF, /*   */
+        0xFF, /*   */
+        0xFF, /*   */
+        0xFF, /*   */
+        0xFF, /*   */
+        0xFF, /*   */
+        0xFF, /*   */
+        0xFF, /*   */
+        0x44, /*   */
+        0xFF, /* ! */
+        0xFF, /* " */
+        0x94, /* # */
+        0x89, /* $ */
+        0xFF, /* % */
+        0xFF, /* & */
+        0x85, /* ' */
+        0x8F, /* ( */
+        0x92, /* ) */
+        0x8B, /* * */
+        0x91, /* + */
+        0x8C, /* , */
+        0x83, /* - */
+        0x9C, /* . */
+        0x9D, /* / */
+        0x96, /* 0 */
+        0x97, /* 1 */
+        0x93, /* 2 */
+        0x81, /* 3 */
+        0x8A, /* 4 */
+        0x90, /* 5 */
+        0x95, /* 6 */
+        0x87, /* 7 */
+        0x86, /* 8 */
+        0x98, /* 9 */
+        0x8E, /* : */
+        0xFF, /* ; */
+        0xFF, /* < */
+        0x9E, /* = */
+        0xFF, /* > */
+        0x99, /* ? */
+        0xFF, /* @ */
+        0x03, /* A */
+        0x19, /* B */
+        0x0E, /* C */
+        0x09, /* D */
+        0x01, /* E */
+        0x0D, /* F */
+        0x1A, /* G */
+        0x14, /* H */
+        0x06, /* I */
+        0x0B, /* J */
+        0x0F, /* K */
+        0x12, /* L */
+        0x1C, /* M */
+        0x0C, /* N */
+        0x18, /* O */
+        0x16, /* P */
+        0x17, /* Q */
+        0x0A, /* R */
+        0x05, /* S */
+        0x10, /* T */
+        0x07, /* U */
+        0x1E, /* V */
+        0x13, /* W */
+        0x1D, /* X */
+        0x15, /* Y */
+        0x11, /* Z */
+        0xFF, /* [ */
+        0xFF, /* \ */
+        0xFF, /* ] */
+        0x9B, /* ^ */
+        0xFF, /* _ */
+        0xFF, /* ` */
+        0x03, /* a */
+        0x19, /* b */
+        0x0E, /* c */
+        0x09, /* d */
+        0x01, /* e */
+        0x0D, /* f */
+        0x1A, /* g */
+        0x14, /* h */
+        0x06, /* i */
+        0x0B, /* j */
+        0x0F, /* k */
+        0x12, /* l */
+        0x1C, /* m */
+        0x0C, /* n */
+        0x18, /* o */
+        0x16, /* p */
+        0x17, /* q */
+        0x0A, /* r */
+        0x05, /* s */
+        0x10, /* t */
+        0x07, /* u */
+        0x1E, /* v */
+        0x13, /* w */
+        0x1D, /* x */
+        0x15, /* y */
+        0x11, /* z */
+        0xFF, /* { */
+        0xFF, /* | */
+        0xFF, /* } */
+        0xFF, /* ~ */
+        0xFF, /* DEL */
+    };
+    uint16_t shift;
+
+    ch = conv[ch];
+    if (ch == 0xFF)
+        return 0;
+    if ((ch & 0x40))
+        return ch & 0x1F;
+    if ((ch & 0x80))
+    {
+        if (s->baudot_shift == 1)
+            return ch & 0x1F;
+        s->baudot_shift = 1;
+        shift = BAUDOT_FIGURE_SHIFT;
+    }
+    else
+    {
+        if (s->baudot_shift == 0)
+            return ch & 0x1F;
+        s->baudot_shift = 0;
+        shift = BAUDOT_LETTER_SHIFT;
+    }
+    return (shift << 5) | (ch & 0x1F);
+}
+/*- End of function --------------------------------------------------------*/
+
+static uint8_t adsi_decode_baudot(adsi_rx_state_t *s, uint8_t ch)
+{
+    static const uint8_t conv[2][32] =
+    {
+        {"\000E\nA SIU\rDRJNFCKTZLWHYPQOBG^MXV^"},
+        {"\0003\n- '87\r$4*,*:(5+)2#6019?*^./=^"}
+    };
+
+    switch (ch)
+    {
+    case BAUDOT_FIGURE_SHIFT:
+        s->baudot_shift = 1;
+        break;
+    case BAUDOT_LETTER_SHIFT:
+        s->baudot_shift = 0;
+        break;
+    default:
+        return conv[s->baudot_shift][ch];
+    }
+    /* return 0 if we did not produce a character */
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int adsi_next_field(adsi_rx_state_t *s, const uint8_t *msg, int msg_len, int pos, uint8_t *field_type, uint8_t const **field_body, int *field_len)
+{
+    int i;
+
+    /* Return -1 for no more fields. Return -2 for message structure corrupt. */
+    switch (s->standard)
+    {
+    case ADSI_STANDARD_CLASS:
+    case ADSI_STANDARD_CLIP:
+    case ADSI_STANDARD_ACLIP:
+        if (pos >= msg_len)
+            return -1;
+        /* For MDMF type messages, these standards all use "IE" type fields - type,
+           length, contents - and similar headers */
+        if (pos <= 0)
+        {
+            /* Return the message type */
+            *field_type = msg[0];
+            *field_len = 0;
+            *field_body = NULL;
+            pos = 2;
+        }
+        else
+        {
+            if ((msg[0] & 0x80))
+            {
+                /* MDMF messages seem to always have a message type with the MSB set. Is that
+                   guaranteed? */
+                *field_type = msg[pos++];
+                *field_len = msg[pos++];
+                *field_body = msg + pos;
+            }
+            else
+            {
+                /* SDMF */
+                *field_type = 0;
+                *field_len = msg_len - pos;
+                *field_body = msg + pos;
+            }
+            pos += *field_len;
+        }
+        if (pos > msg_len)
+            return -2;
+        break;
+    case ADSI_STANDARD_JCLIP:
+        if (pos >= msg_len - 2)
+            return -1;
+        if (pos <= 0)
+        {
+            /* Return the message type */
+            pos = 5;
+            *field_type = msg[pos++];
+            if (*field_type == DLE)
+                pos++;
+            if (msg[pos++] == DLE)
+                pos++;
+            *field_len = 0;
+            *field_body = NULL;
+        }
+        else
+        {
+            *field_type = msg[pos++];
+            if (*field_type == DLE)
+                pos++;
+            *field_len = msg[pos++];
+            if (*field_len == DLE)
+                pos++;
+            /* TODO: we assume here that the body contains no DLE's that would have been stuffed */
+            *field_body = msg + pos;
+            pos += *field_len;
+        }
+        if (pos > msg_len - 2)
+            return -2;
+        break;
+    case ADSI_STANDARD_CLIP_DTMF:
+        if (pos > msg_len)
+            return -1;
+        if (pos <= 0)
+        {
+            pos = 1;
+            *field_type = msg[msg_len - 1];
+            *field_len = 0;
+            *field_body = NULL;
+        }
+        else
+        {
+            /* Remove bias on the pos value */
+            pos--;
+            if (msg[pos] >= '0'  &&  msg[pos] <= '9')
+                *field_type = CLIP_DTMF_HASH_UNSPECIFIED;
+            else
+                *field_type = msg[pos++];
+            *field_body = msg + pos;
+            i = pos;
+            while (i < msg_len  &&  msg[i] >= '0'  &&  msg[i] <= '9')
+                i++;
+            *field_len = i - pos;
+            pos = i;
+            /* Check if we have reached the end of message marker. */
+            if (msg[pos] == '#'  ||  msg[pos] == 'C')
+                pos++;
+            if (pos > msg_len)
+                return -2;
+            /* Bias the pos value, so we don't return 0 inappropriately */
+            pos++;
+        }
+        break;
+    case ADSI_STANDARD_TDD:
+        if (pos >= msg_len)
+            return -1;
+        *field_type = 0;
+        *field_body = msg;
+        *field_len = msg_len;
+        pos = msg_len;
+        break;
+    }
+    return pos;
+}
+/*- End of function --------------------------------------------------------*/
+
+int adsi_add_field(adsi_tx_state_t *s, uint8_t *msg, int len, uint8_t field_type, uint8_t const *field_body, int field_len)
+{
+    int i;
+    int x;
+
+    switch (s->standard)
+    {
+    case ADSI_STANDARD_CLASS:
+    case ADSI_STANDARD_CLIP:
+    case ADSI_STANDARD_ACLIP:
+        /* These standards all use "IE" type fields - type, length, value - and similar headers */
+        if (len <= 0)
+        {
+            /* Initialise a new message. The field type is actually the message type. */
+            msg[0] = field_type;
+            msg[1] = 0;
+            len = 2;
+        }
+        else
+        {
+            /* Add to a message in progress. */
+            if (field_type)
+            {
+                msg[len++] = field_type;
+                msg[len++] = (uint8_t) field_len;
+                if (field_len == DLE)
+                    msg[len++] = (uint8_t) field_len;
+                memcpy(msg + len, field_body, field_len);
+                len += field_len;
+            }
+            else
+            {
+                /* No field type or length, for restricted single message formats */
+                memcpy(msg + len, field_body, field_len);
+                len += field_len;
+            }
+        }
+        break;
+    case ADSI_STANDARD_JCLIP:
+        /* This standard uses "IE" type fields - type, length, value - but escapes DLE characters,
+           to prevent immitation of a control octet. */
+        if (len <= 0)
+        {
+            /* Initialise a new message. The field type is actually the message type. */
+            msg[0] = field_type;
+            msg[1] = 0;
+            len = 2;
+        }
+        else
+        {
+            /* Add to a message in progress. */
+            msg[len++] = field_type;
+            if (field_type == DLE)
+                msg[len++] = field_type;
+            msg[len++] = (uint8_t) field_len;
+            if (field_len == DLE)
+                msg[len++] = (uint8_t) field_len;
+            for (i = 0;  i < field_len;  i++)
+            {
+                msg[len++] = field_body[i];
+                if (field_body[i] == DLE)
+                    msg[len++] = field_body[i];
+            }
+        }
+        break;
+    case ADSI_STANDARD_CLIP_DTMF:
+        if (len <= 0)
+        {
+            /* Initialise a new message. The field type is actually the message type. */
+            msg[0] = field_type;
+            len = 1;
+        }
+        else
+        {
+            /* Save and reuse the terminator/message type */
+            x = msg[--len];
+            if (field_type != CLIP_DTMF_HASH_UNSPECIFIED)
+                msg[len++] = field_type;
+            memcpy(msg + len, field_body, field_len);
+            msg[len + field_len] = x;
+            len += (field_len + 1);
+        }
+        break;
+    case ADSI_STANDARD_TDD:
+        if (len < 0)
+            len = 0;
+        for (i = 0;  i < field_len;  i++)
+        {
+            if ((x = adsi_encode_baudot(s, field_body[i])))
+            {
+                if ((x & 0x3E0))
+                    msg[len++] = (uint8_t) ((x >> 5) & 0x1F);
+                msg[len++] = (uint8_t) (x & 0x1F);
+            }
+        }
+        break;
+    }
+
+    return len;
+}
+/*- End of function --------------------------------------------------------*/
+
+const char *adsi_standard_to_str(int standard)
+{
+    switch (standard)
+    {
+    case ADSI_STANDARD_CLASS:
+        return "CLASS";
+    case ADSI_STANDARD_CLIP:
+        return "CLIP";
+    case ADSI_STANDARD_ACLIP:
+        return "A-CLIP";
+    case ADSI_STANDARD_JCLIP:
+        return "J-CLIP";
+    case ADSI_STANDARD_CLIP_DTMF:
+        return "CLIP-DTMF";
+    case ADSI_STANDARD_TDD:
+        return "TDD";
+    }
+    return "???";
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/async.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/async.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,231 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * async.c - Asynchronous serial bit stream encoding and decoding
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2003 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: async.c,v 1.11 2008/05/13 13:17:21 steveu Exp $
+ */
+
+/*! \file */
+
+#if defined(HAVE_CONFIG_H)
+#include <config.h>
+#endif
+
+#include <inttypes.h>
+#include <stdlib.h>
+#include <string.h>
+#include <assert.h>
+
+#include "spandsp/telephony.h"
+#include "spandsp/async.h"
+
+async_rx_state_t *async_rx_init(async_rx_state_t *s,
+                                int data_bits,
+                                int parity,
+                                int stop_bits,
+                                int use_v14,
+                                put_byte_func_t put_byte,
+                                void *user_data)
+{
+    if (s == NULL)
+    {
+        if ((s = (async_rx_state_t *) malloc(sizeof(*s))) == NULL)
+            return NULL;
+    }
+    s->data_bits = data_bits;
+    s->parity = parity;
+    s->stop_bits = stop_bits;
+    s->use_v14 = use_v14;
+
+    s->put_byte = put_byte;
+    s->user_data = user_data;
+
+    s->byte_in_progress = 0;
+    s->bitpos = 0;
+    s->parity_bit = 0;
+
+    s->parity_errors = 0;
+    s->framing_errors = 0;
+    return s;
+}
+/*- End of function --------------------------------------------------------*/
+
+void async_rx_put_bit(void *user_data, int bit)
+{
+    async_rx_state_t *s;
+
+    s = (async_rx_state_t *) user_data;
+    if (bit < 0)
+    {
+        /* Special conditions */
+        switch (bit)
+        {
+        case PUTBIT_CARRIER_UP:
+        case PUTBIT_CARRIER_DOWN:
+        case PUTBIT_TRAINING_IN_PROGRESS:
+        case PUTBIT_TRAINING_SUCCEEDED:
+        case PUTBIT_TRAINING_FAILED:
+        case PUTBIT_END_OF_DATA:
+            s->put_byte(s->user_data, bit);
+            s->bitpos = 0;
+            s->byte_in_progress = 0;
+            break;
+        default:
+            //printf("Eh!\n");
+            break;
+        }
+        return;
+    }
+    if (s->bitpos == 0)
+    {
+        /* Search for the start bit */
+        s->bitpos += (bit ^ 1);
+        s->parity_bit = 0;
+        s->byte_in_progress = 0;
+    }
+    else if (s->bitpos <= s->data_bits)
+    {
+        s->byte_in_progress = (s->byte_in_progress >> 1) | (bit << 7);
+        s->parity_bit ^= bit;
+        s->bitpos++;
+    }
+    else if (s->parity  &&  s->bitpos == s->data_bits + 1)
+    {
+        if (s->parity == ASYNC_PARITY_ODD)
+            s->parity_bit ^= 1;
+
+        if (s->parity_bit != bit)
+            s->parity_errors++;
+        s->bitpos++;
+    }
+    else
+    {
+        /* Stop bit */
+        if (bit == 1)
+        {
+            /* Align the received value */
+            if (s->data_bits < 8)
+                s->byte_in_progress >>= (8 - s->data_bits);
+            s->put_byte(s->user_data, s->byte_in_progress);
+            s->bitpos = 0;
+        }
+        else if (s->use_v14)
+        {
+            /* This is actually the start bit for the next character, and
+               the stop bit has been dropped from the stream. This is the
+               rate adaption specified in V.14 */
+            /* Align the received value */
+            if (s->data_bits < 8)
+                s->byte_in_progress >>= (8 - s->data_bits);
+            s->put_byte(s->user_data, s->byte_in_progress);
+            s->bitpos = 1;
+            s->parity_bit = 0;
+            s->byte_in_progress = 0;
+        }
+        else
+        {
+            s->framing_errors++;
+            s->bitpos = 0;
+        }
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+async_tx_state_t *async_tx_init(async_tx_state_t *s,
+                                int data_bits,
+                                int parity,
+                                int stop_bits,
+                                int use_v14,
+                                get_byte_func_t get_byte,
+                                void *user_data)
+{
+    if (s == NULL)
+    {
+        if ((s = (async_tx_state_t *) malloc(sizeof(*s))) == NULL)
+            return NULL;
+    }
+    /* We have a use_v14 parameter for completeness, but right now V.14 only
+       applies to the receive side. We are unlikely to have an application where
+       flow control does not exist, so V.14 stuffing is not needed. */
+    s->data_bits = data_bits;
+    s->parity = parity;
+    s->stop_bits = stop_bits;
+    if (parity != ASYNC_PARITY_NONE)
+        s->stop_bits++;
+        
+    s->get_byte = get_byte;
+    s->user_data = user_data;
+
+    s->byte_in_progress = 0;
+    s->bitpos = 0;
+    s->parity_bit = 0;
+    return s;
+}
+/*- End of function --------------------------------------------------------*/
+
+int async_tx_get_bit(void *user_data)
+{
+    async_tx_state_t *s;
+    int bit;
+    
+    s = (async_tx_state_t *) user_data;
+    if (s->bitpos == 0)
+    {
+        if ((s->byte_in_progress = s->get_byte(s->user_data)) < 0)
+        {
+            /* No more data */
+            bit = PUTBIT_END_OF_DATA;
+        }
+        else
+        {
+            /* Start bit */
+            bit = 0;
+            s->parity_bit = 0;
+            s->bitpos++;
+        }
+    }
+    else if (s->bitpos <= s->data_bits)
+    {
+        bit = s->byte_in_progress & 1;
+        s->byte_in_progress >>= 1;
+        s->parity_bit ^= bit;
+        s->bitpos++;
+    }
+    else if (s->parity  &&  s->bitpos == s->data_bits + 1)
+    {
+        if (s->parity == ASYNC_PARITY_ODD)
+            s->parity_bit ^= 1;
+        bit = s->parity_bit;
+        s->bitpos++;
+    }
+    else
+    {
+        /* Stop bit(s) */
+        bit = 1;
+        if (++s->bitpos > s->data_bits + s->stop_bits)
+            s->bitpos = 0;
+    }
+    return bit;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/at_interpreter.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/at_interpreter.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,5324 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * at_interpreter.c - AT command interpreter to V.251, V.252, V.253, T.31 and the 3GPP specs.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Special thanks to Lee Howard <faxguy at howardsilvan.com>
+ * for his great work debugging and polishing this code.
+ *
+ * Copyright (C) 2004, 2005, 2006 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: at_interpreter.c,v 1.30 2008/07/24 13:55:23 steveu Exp $
+ */
+
+/*! \file */
+
+#if defined(HAVE_CONFIG_H)
+#include <config.h>
+#endif
+
+#include <inttypes.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <memory.h>
+#include <string.h>
+#include <ctype.h>
+#include <assert.h>
+
+#include "spandsp/telephony.h"
+#include "spandsp/logging.h"
+#include "spandsp/queue.h"
+#include "spandsp/power_meter.h"
+#include "spandsp/complex.h"
+#include "spandsp/tone_generate.h"
+#include "spandsp/async.h"
+#include "spandsp/hdlc.h"
+#include "spandsp/fsk.h"
+
+#include "spandsp/at_interpreter.h"
+
+#define ms_to_samples(t)        (((t)*SAMPLE_RATE)/1000)
+
+#define MANUFACTURER            "www.soft-switch.org"
+#define SERIAL_NUMBER           "42"
+#define GLOBAL_OBJECT_IDENTITY  "42"
+
+enum
+{
+    ASCII_RESULT_CODES = 1,
+    NUMERIC_RESULT_CODES,
+    NO_RESULT_CODES
+};
+
+static at_profile_t profiles[3] =
+{
+    {
+#if defined(_MSC_VER)
+        /*.echo =*/ TRUE,
+        /*.verbose =*/ TRUE,
+        /*.result_code_format =*/ ASCII_RESULT_CODES,
+        /*.pulse_dial =*/ FALSE,
+        /*.double_escape =*/ FALSE,
+        /*.adaptive_receive =*/ FALSE,
+        /*.s_regs[100] =*/ {0, 0, 0, '\r', '\n', '\b', 1, 60, 5, 0, 0}
+#else
+        .echo = TRUE,
+        .verbose = TRUE,
+        .result_code_format = ASCII_RESULT_CODES,
+        .pulse_dial = FALSE,
+        .double_escape = FALSE,
+        .adaptive_receive = FALSE,
+        .s_regs[0] = 0,
+        .s_regs[3] = '\r',
+        .s_regs[4] = '\n',
+        .s_regs[5] = '\b',
+        .s_regs[6] = 1,
+        .s_regs[7] = 60,
+        .s_regs[8] = 5,
+        .s_regs[10] = 0
+#endif
+    }
+};
+
+typedef const char *(*at_cmd_service_t)(at_state_t *s, const char *cmd);
+
+static const char *manufacturer = MANUFACTURER;
+static const char *model = PACKAGE;
+static const char *revision = VERSION;
+
+#define ETX 0x03
+#define DLE 0x10
+#define SUB 0x1A
+
+/* BEWARE: right now this must match up with a list in the T.31 code. */
+enum
+{
+    T31_NONE = -1,
+    T31_FLUSH = 0,
+    T31_SILENCE_TX,
+    T31_SILENCE_RX,
+    T31_CED_TONE,
+    T31_CNG_TONE,
+    T31_NOCNG_TONE,
+};
+
+static const char *at_response_codes[] =
+{
+    "OK",
+    "CONNECT",
+    "RING",
+    "NO CARRIER",
+    "ERROR",
+    "???",
+    "NO DIALTONE",
+    "BUSY",
+    "NO ANSWER",
+    "+FCERROR",
+    "+FRH:3"
+};
+
+void at_set_at_rx_mode(at_state_t *s, int new_mode)
+{
+    /* The use of a DTE timeout is mode dependent. Set the timeout appropriately in
+       the modem. */
+    switch (new_mode)
+    {
+    case AT_MODE_HDLC:
+    case AT_MODE_STUFFED:
+        at_modem_control(s, s->dte_inactivity_timeout*1000, (void *) (intptr_t) s->dte_inactivity_timeout);
+        break;
+    default:
+        at_modem_control(s, AT_MODEM_CONTROL_DTE_TIMEOUT, NULL);
+        break;
+    }
+    s->at_rx_mode = new_mode;
+}
+/*- End of function --------------------------------------------------------*/
+
+void at_put_response(at_state_t *s, const char *t)
+{
+    uint8_t buf[3];
+    
+    buf[0] = s->p.s_regs[3];
+    buf[1] = s->p.s_regs[4];
+    buf[2] = '\0';
+    if (s->p.result_code_format == ASCII_RESULT_CODES)
+        s->at_tx_handler(s, s->at_tx_user_data, buf, 2);
+    s->at_tx_handler(s, s->at_tx_user_data, (uint8_t *) t, strlen(t));
+    s->at_tx_handler(s, s->at_tx_user_data, buf, 2);
+}
+/*- End of function --------------------------------------------------------*/
+
+void at_put_numeric_response(at_state_t *s, int val)
+{
+    char buf[20];
+
+    snprintf(buf, sizeof(buf), "%d", val);
+    at_put_response(s, buf);
+}
+/*- End of function --------------------------------------------------------*/
+
+void at_put_response_code(at_state_t *s, int code)
+{
+    uint8_t buf[20];
+
+    switch (s->p.result_code_format)
+    {
+    case ASCII_RESULT_CODES:
+        at_put_response(s, at_response_codes[code]);
+        break;
+    case NUMERIC_RESULT_CODES:
+        snprintf((char *) buf, sizeof(buf), "%d%c", code, s->p.s_regs[3]);
+        s->at_tx_handler(s, s->at_tx_user_data, buf, strlen((char *) buf));
+        break;
+    default:
+        /* No result codes */
+        break;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static int answer_call(at_state_t *s)
+{
+    if (at_modem_control(s, AT_MODEM_CONTROL_ANSWER, NULL) < 0)
+        return FALSE;
+    /* Answering should now be in progress. No AT response should be
+       issued at this point. */
+    s->do_hangup = FALSE;
+    return TRUE;
+}
+/*- End of function --------------------------------------------------------*/
+
+void at_call_event(at_state_t *s, int event)
+{
+    span_log(&s->logging, SPAN_LOG_FLOW, "Call event %d received\n", event);
+    switch (event)
+    {
+    case AT_CALL_EVENT_ALERTING:
+        at_modem_control(s, AT_MODEM_CONTROL_RNG, (void *) 1);
+        if (s->display_call_info  &&  !s->call_info_displayed)
+            at_display_call_info(s);
+        at_put_response_code(s, AT_RESPONSE_CODE_RING);
+        if ((++s->rings_indicated) >= s->p.s_regs[0]  &&  s->p.s_regs[0])
+        {
+            /* The modem is set to auto-answer now */
+            answer_call(s);
+        }
+        break;
+    case AT_CALL_EVENT_ANSWERED:
+        at_modem_control(s, AT_MODEM_CONTROL_RNG, (void *) 0);
+        if (s->fclass_mode == 0)
+        {
+            /* Normal data modem connection */
+            at_set_at_rx_mode(s, AT_MODE_CONNECTED);
+            /* TODO: */
+        }
+        else
+        {
+            /* FAX modem connection */
+            at_set_at_rx_mode(s, AT_MODE_DELIVERY);
+            at_modem_control(s, AT_MODEM_CONTROL_RESTART, (void *) T31_CED_TONE);
+        }
+        break;
+    case AT_CALL_EVENT_CONNECTED:
+        span_log(&s->logging, SPAN_LOG_FLOW, "Dial call - connected. fclass=%d\n", s->fclass_mode);
+        at_modem_control(s, AT_MODEM_CONTROL_RNG, (void *) 0);
+        if (s->fclass_mode == 0)
+        {
+            /* Normal data modem connection */
+            at_set_at_rx_mode(s, AT_MODE_CONNECTED);
+            /* TODO: */
+        }
+        else
+        {
+            /* FAX modem connection */
+            at_set_at_rx_mode(s, AT_MODE_DELIVERY);
+            if (s->silent_dial)
+                at_modem_control(s, AT_MODEM_CONTROL_RESTART, (void *) T31_NOCNG_TONE);
+            else
+                at_modem_control(s, AT_MODEM_CONTROL_RESTART, (void *) T31_CNG_TONE);
+            s->dte_is_waiting = TRUE;
+        }
+        break;
+    case AT_CALL_EVENT_BUSY:
+        at_set_at_rx_mode(s, AT_MODE_ONHOOK_COMMAND);
+        at_put_response_code(s, AT_RESPONSE_CODE_BUSY);
+        break;
+    case AT_CALL_EVENT_NO_DIALTONE:
+        at_set_at_rx_mode(s, AT_MODE_ONHOOK_COMMAND);
+        at_put_response_code(s, AT_RESPONSE_CODE_NO_DIALTONE);
+        break;
+    case AT_CALL_EVENT_NO_ANSWER:
+        at_set_at_rx_mode(s, AT_MODE_ONHOOK_COMMAND);
+        at_put_response_code(s, AT_RESPONSE_CODE_NO_ANSWER);
+        break;
+    case AT_CALL_EVENT_HANGUP:
+        span_log(&s->logging, SPAN_LOG_FLOW, "Hangup... at_rx_mode %d\n", s->at_rx_mode);
+        at_modem_control(s, AT_MODEM_CONTROL_ONHOOK, NULL);
+        if (s->dte_is_waiting)
+        {
+            if (s->ok_is_pending)
+            {
+                at_put_response_code(s, AT_RESPONSE_CODE_OK);
+                s->ok_is_pending = FALSE;
+            }
+            else
+            {
+                at_put_response_code(s, AT_RESPONSE_CODE_NO_CARRIER);
+            }
+            s->dte_is_waiting = FALSE;
+            at_set_at_rx_mode(s, AT_MODE_ONHOOK_COMMAND);
+        }
+        else if (s->fclass_mode  &&  s->rx_signal_present)
+        {
+            s->rx_data[s->rx_data_bytes++] = DLE;
+            s->rx_data[s->rx_data_bytes++] = ETX;
+            s->at_tx_handler(s, s->at_tx_user_data, s->rx_data, s->rx_data_bytes);
+            s->rx_data_bytes = 0;
+        }
+        if (s->at_rx_mode != AT_MODE_OFFHOOK_COMMAND  &&  s->at_rx_mode != AT_MODE_ONHOOK_COMMAND)
+            at_put_response_code(s, AT_RESPONSE_CODE_NO_CARRIER);
+        s->rx_signal_present = FALSE;
+        at_modem_control(s, AT_MODEM_CONTROL_RNG, (void *) 0);
+        at_set_at_rx_mode(s, AT_MODE_ONHOOK_COMMAND);
+        break;
+    default:
+        span_log(&s->logging, SPAN_LOG_WARNING, "Invalid call event %d received.\n", event);
+        break;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+void at_reset_call_info(at_state_t *s)
+{
+    struct at_call_id *call_id;
+    struct at_call_id *next;
+ 
+    for (call_id = s->call_id;  call_id;  call_id = next)
+    {
+        next = call_id->next;
+        free(call_id);
+    }
+    s->call_id = NULL;
+    s->rings_indicated = 0;
+    s->call_info_displayed = FALSE;
+}
+/*- End of function --------------------------------------------------------*/
+
+void at_set_call_info(at_state_t *s, char const *id, char const *value)
+{
+    struct at_call_id *new_call_id;
+    struct at_call_id *call_id;
+
+    /* TODO: We should really not merely ignore a failure to malloc */
+    if ((new_call_id = (struct at_call_id *) malloc(sizeof(*new_call_id))) == NULL)
+        return;
+    call_id = s->call_id;
+    /* If these strdups fail its pretty harmless. We just appear to not
+       have the relevant field. */
+    new_call_id->id = (id)  ?  strdup(id)  :  NULL;
+    new_call_id->value = (value)  ?  strdup(value)  :  NULL;
+    new_call_id->next = NULL;
+
+    if (call_id)
+    {
+        while (call_id->next)
+            call_id = call_id->next;
+        call_id->next = new_call_id;
+    }
+    else
+    {
+        s->call_id = new_call_id;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+void at_display_call_info(at_state_t *s)
+{
+    char buf[132 + 1];
+    struct at_call_id *call_id = s->call_id;
+
+    while (call_id)
+    {
+        snprintf(buf, sizeof(buf), "%s=%s", 
+                 call_id->id ? call_id->id : "NULL", call_id->value ? call_id->value : "<NONE>");
+        at_put_response(s, buf);
+        call_id = call_id->next;
+    }
+    s->call_info_displayed = TRUE;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int parse_num(const char **s, int max_value)
+{
+    int i;
+    
+    /* The spec. says no digits is valid, and should be treated as zero. */
+    i = 0;
+    while (isdigit(**s))
+    {
+        i = i*10 + ((**s) - '0');
+        (*s)++;
+    }
+    if (i > max_value)
+        i = -1;
+    return i;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int parse_hex_num(const char **s, int max_value)
+{
+    int i;
+    
+    /* The spec. says a hex value is always 2 digits, and the alpha digits are
+       upper case. */
+    i = 0;
+    if (isdigit(**s))
+        i = **s - '0';
+    else if (**s >= 'A'  &&  **s <= 'F')
+        i = **s - 'A';
+    else
+        return -1;
+    (*s)++;
+
+    if (isdigit(**s))
+        i = (i << 4)  | (**s - '0');
+    else if (**s >= 'A'  &&  **s <= 'F')
+        i = (i << 4)  | (**s - 'A');
+    else
+        return -1;
+    (*s)++;
+    if (i > max_value)
+        i = -1;
+    return i;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int match_element(const char **variant, const char *variants)
+{
+    int i;
+    size_t len;
+    char const *s;
+    char const *t;
+
+    s = variants;
+    for (i = 0;  *s;  i++)
+    {
+        if ((t = strchr(s, ',')))
+            len = t - s;
+        else
+            len = strlen(s);
+        if (len == (int) strlen(*variant)  &&  memcmp(*variant, s, len) == 0)
+        {
+            *variant += len;
+            return  i;
+        }
+        s += len;
+        if (*s == ',')
+            s++;
+    }
+    return  -1;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int parse_out(at_state_t *s, const char **t, int *target, int max_value, const char *prefix, const char *def)
+{
+    char buf[100];
+    int val;
+
+    switch (*(*t)++)
+    {
+    case '=':
+        switch (**t)
+        {
+        case '?':
+            /* Show possible values */
+            (*t)++;
+            snprintf(buf, sizeof(buf), "%s%s", (prefix)  ?  prefix  :  "", def);
+            at_put_response(s, buf);
+            break;
+        default:
+            /* Set value */
+            if ((val = parse_num(t, max_value)) < 0)
+                return FALSE;
+            if (target)
+                *target = val;
+            break;
+        }
+        break;
+    case '?':
+        /* Show current value */
+        val = (target)  ?  *target  :  0;
+        snprintf(buf, sizeof(buf), "%s%d", (prefix)  ?  prefix  :  "", val);
+        at_put_response(s, buf);
+        break;
+    default:
+        return FALSE;
+    }
+    return TRUE;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int parse_2_out(at_state_t *s, const char **t, int *target1, int max_value1, int *target2, int max_value2, const char *prefix, const char *def)
+{
+    char buf[100];
+    int val1;
+    int val2;
+
+    switch (*(*t)++)
+    {
+    case '=':
+        switch (**t)
+        {
+        case '?':
+            /* Show possible values */
+            (*t)++;
+            snprintf(buf, sizeof(buf), "%s%s", (prefix)  ?  prefix  :  "", def);
+            at_put_response(s, buf);
+            break;
+        default:
+            /* Set value */
+            if ((val1 = parse_num(t, max_value1)) < 0)
+                return FALSE;
+            if (target1)
+                *target1 = val1;
+            if (**t == ',')
+            {
+                (*t)++;
+                if ((val2 = parse_num(t, max_value2)) < 0)
+                    return FALSE;
+                if (target2)
+                    *target2 = val2;
+            }
+            break;
+        }
+        break;
+    case '?':
+        /* Show current value */
+        val1 = (target1)  ?  *target1  :  0;
+        val2 = (target2)  ?  *target2  :  0;
+        snprintf(buf, sizeof(buf), "%s%d,%d", (prefix)  ?  prefix  :  "", val1, val2);
+        at_put_response(s, buf);
+        break;
+    default:
+        return FALSE;
+    }
+    return TRUE;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int parse_hex_out(at_state_t *s, const char **t, int *target, int max_value, const char *prefix, const char *def)
+{
+    char buf[100];
+    int val;
+
+    switch (*(*t)++)
+    {
+    case '=':
+        switch (**t)
+        {
+        case '?':
+            /* Show possible values */
+            (*t)++;
+            snprintf(buf, sizeof(buf), "%s%s", (prefix)  ?  prefix  :  "", def);
+            at_put_response(s, buf);
+            break;
+        default:
+            /* Set value */
+            if ((val = parse_hex_num(t, max_value)) < 0)
+                return FALSE;
+            if (target)
+                *target = val;
+            break;
+        }
+        break;
+    case '?':
+        /* Show current value */
+        val = (target)  ?  *target  :  0;
+        snprintf(buf, sizeof(buf), "%s%02X", (prefix)  ?  prefix  :  "", val);
+        at_put_response(s, buf);
+        break;
+    default:
+        return FALSE;
+    }
+    return TRUE;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int parse_string_list_out(at_state_t *s, const char **t, int *target, int max_value, const char *prefix, const char *def)
+{
+    char buf[100];
+    int val;
+    size_t len;
+    char *tmp;
+
+    switch (*(*t)++)
+    {
+    case '=':
+        switch (**t)
+        {
+        case '?':
+            /* Show possible values */
+            (*t)++;
+            snprintf(buf, sizeof(buf), "%s%s", (prefix)  ?  prefix  :  "", def);
+            at_put_response(s, buf);
+            break;
+        default:
+            /* Set value */
+            if ((val = match_element(t, def)) < 0)
+                return FALSE;
+            if (target)
+                *target = val;
+            break;
+        }
+        break;
+    case '?':
+        /* Show current index value from def */
+        val = (target)  ?  *target  :  0;
+        while (val--  &&  (def = strchr(def, ',')))
+            def++;
+        if ((tmp = strchr(def, ',')))
+            len = tmp - def;
+        else
+            len = strlen(def);
+        snprintf(buf, sizeof(buf), "%s%.*s", (prefix)  ?  prefix  :  "", (int) len, def);
+        at_put_response(s, buf);
+        break;
+    default:
+        return FALSE;
+    }
+    return TRUE;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int parse_string_out(at_state_t *s, const char **t, char **target, const char *prefix)
+{
+    char buf[100];
+
+    switch (*(*t)++)
+    {
+    case '=':
+        switch (**t)
+        {
+        case '?':
+            /* Show possible values */
+            (*t)++;
+            snprintf(buf, sizeof(buf), "%s", (prefix)  ?  prefix  :  "");
+            at_put_response(s, buf);
+            break;
+        default:
+            /* Set value */
+            if (*target)
+                free(*target);
+            /* If this strdup fails, it should be harmless */
+            *target = strdup(*t);
+            break;
+        }
+        break;
+    case '?':
+        /* Show current index value */
+        at_put_response(s, (*target)  ?  *target  :  "");
+        break;
+    default:
+        return FALSE;
+    }
+    while (*t)
+        t++;
+    return TRUE;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *s_reg_handler(at_state_t *s, const char *t, int reg)
+{
+    int val;
+    int b;
+    char buf[4];
+
+    /* Set or get an S register */
+    switch (*t++)
+    {
+    case '=':
+        switch (*t)
+        {
+        case '?':
+            t++;
+            snprintf(buf, sizeof(buf), "%3.3d", 0);
+            at_put_response(s, buf);
+            break;
+        default:
+            if ((val = parse_num(&t, 255)) < 0)
+                return NULL;
+            s->p.s_regs[reg] = (uint8_t) val;
+            break;
+        }
+        break;
+    case '?':
+        snprintf(buf, sizeof(buf), "%3.3d", s->p.s_regs[reg]);
+        at_put_response(s, buf);
+        break;
+    case '.':
+        if ((b = parse_num(&t, 7)) < 0)
+            return NULL;
+        switch (*t++)
+        {
+        case '=':
+            switch (*t)
+            {
+            case '?':
+                t++;
+                at_put_numeric_response(s, 0);
+                break;
+            default:
+                if ((val = parse_num(&t, 1)) < 0)
+                    return NULL;
+                if (val)
+                    s->p.s_regs[reg] |= (1 << b);
+                else
+                    s->p.s_regs[reg] &= ~(1 << b);
+                break;
+            }
+            break;
+        case '?':
+            at_put_numeric_response(s, (int) ((s->p.s_regs[reg] >> b) & 1));
+            break;
+        default:
+            return NULL;
+        }
+        break;
+    default:
+        return NULL;
+    }
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int process_class1_cmd(at_state_t *s, const char **t)
+{
+    int val;
+    int operation;
+    int direction;
+    int result;
+    const char *allowed;
+
+    direction = (*(*t + 2) == 'T');
+    operation = *(*t + 3);
+    /* Step past the "+Fxx" */
+    *t += 4;
+    switch (operation)
+    {
+    case 'S':
+        allowed = "0-255";
+        break;
+    case 'H':
+        allowed = "3";
+        break;
+    default:
+        allowed = "24,48,72,73,74,96,97,98,121,122,145,146";
+        break;
+    }
+    
+    val = -1;
+    if (!parse_out(s, t, &val, 255, NULL, allowed))
+        return TRUE;
+    if (val < 0)
+    {
+        /* It was just a query */
+        return  TRUE;
+    }
+    /* All class 1 FAX commands are supposed to give an ERROR response, if the phone
+       is on-hook. */
+    if (s->at_rx_mode == AT_MODE_ONHOOK_COMMAND)
+        return FALSE;
+
+    result = TRUE;
+    if (s->class1_handler)
+        result = s->class1_handler(s, s->class1_user_data, direction, operation, val);
+    switch (result)
+    {
+    case 0:
+        /* Inhibit an immediate response.  (These commands should not be part of a multi-command entry.) */
+        *t = (const char *) -1;
+        return TRUE;
+    case -1:
+        return FALSE;
+    }
+    return TRUE;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_dummy(at_state_t *s, const char *t)
+{
+    /* Dummy routine to absorb delimiting characters from a command string */
+    return t + 1;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_A(at_state_t *s, const char *t)
+{
+    /* V.250 6.3.5 - Answer (abortable) */ 
+    t += 1;
+    if (!answer_call(s))
+        return NULL;
+    return (const char *) -1;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_D(at_state_t *s, const char *t)
+{
+    int ok;
+    char *u;
+    const char *w;
+    char num[100 + 1];
+    char ch;
+
+    /* V.250 6.3.1 - Dial (abortable) */ 
+    at_reset_call_info(s);
+    s->do_hangup = FALSE;
+    s->silent_dial = FALSE;
+    t += 1;
+    ok = FALSE;
+    /* There are a numbers of options in a dial command string.
+       Many are completely irrelevant in this application. */
+    w = t;
+    u = num;
+    for (  ;  (ch = *t);  t++)
+    {
+        if (isdigit(ch))
+        {
+            /* V.250 6.3.1.1 Basic digit set */
+            *u++ = ch;
+        }
+        else
+        {
+            switch (ch)
+            {
+            case 'A':
+            case 'B':
+            case 'C':
+            case 'D':
+            case '*':
+            case '#':
+                /* V.250 6.3.1.1 Full DTMF repertoire */
+                if (!s->p.pulse_dial)
+                    *u++ = ch;
+                break;
+            case ' ':
+            case '-':
+                /* Ignore spaces and dashes */
+                /* This is not a standards based thing. It just improves
+                   compatibility with some other modems. */
+                break;
+            case '+':
+                /* V.250 6.3.1.1 International access code */
+                /* TODO: */
+                break;
+            case ',':
+                /* V.250 6.3.1.2 Pause */
+                /* Pass these through to the application to handle. */
+                *u++ = ch;
+                break;
+            case 'T':
+                /* V.250 6.3.1.3 Tone dial */
+                s->p.pulse_dial = FALSE;
+                break;
+            case 'P':
+                /* V.250 6.3.1.4 Pulse dial */
+                s->p.pulse_dial = TRUE;
+                break;
+            case '!':
+                /* V.250 6.3.1.5 Hook flash, register recall */
+                /* TODO: */
+                break;
+            case 'W':
+                /* V.250 6.3.1.6 Wait for dial tone */
+                /* TODO: */
+                break;
+            case '@':
+                /* V.250 6.3.1.7 Wait for quiet answer */
+                s->silent_dial = TRUE;
+                break;
+            case 'S':
+                /* V.250 6.3.1.8 Invoke stored string */
+                /* S=<location> */
+                /* TODO: */
+                break;
+            case 'G':
+            case 'g':
+                /* GSM07.07 6.2 - Control the CUG supplementary service for this call */
+                /* TODO: */
+                break;
+            case 'I':
+            case 'i':
+                /* GSM07.07 6.2 - Override Calling Line Identification Restriction (CLIR) */
+                /* TODO: */
+                break;
+            case ';':
+                /* V.250 6.3.1 - Dial string terminator - make voice call and remain in command mode */
+                /* TODO: */
+                break;
+            case '>':
+                /* GSM07.07 6.2 - Direct dialling from phone book supplementary service subscription
+                                  default value for this call */
+                /* TODO: */
+                break;
+            default:
+                return NULL;
+            }
+        }
+    }
+    *u = '\0';
+    if ((ok = at_modem_control(s, AT_MODEM_CONTROL_CALL, num)) < 0)
+        return NULL;
+    /* Dialing should now be in progress. No AT response should be
+       issued at this point. */
+    return (const char *) -1;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_E(at_state_t *s, const char *t)
+{
+    int val;
+
+    /* V.250 6.2.4 - Command echo */ 
+    t += 1;
+    if ((val = parse_num(&t, 1)) < 0)
+        return NULL;
+    s->p.echo = val;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_H(at_state_t *s, const char *t)
+{
+    int val;
+
+    /* V.250 6.3.6 - Hook control */ 
+    t += 1;
+    if ((val = parse_num(&t, 1)) < 0)
+        return NULL;
+    if (val)
+    {
+        /* Take the receiver off-hook, effectively busying-out the modem. */
+        if (s->at_rx_mode != AT_MODE_ONHOOK_COMMAND  &&  s->at_rx_mode != AT_MODE_OFFHOOK_COMMAND)
+            return NULL;
+        at_modem_control(s, AT_MODEM_CONTROL_OFFHOOK, NULL);
+        at_set_at_rx_mode(s, AT_MODE_OFFHOOK_COMMAND);
+        return t;
+    }
+    at_reset_call_info(s);
+    if (s->at_rx_mode != AT_MODE_ONHOOK_COMMAND  &&  s->at_rx_mode != AT_MODE_OFFHOOK_COMMAND)
+    {
+        /* Push out the last of the audio (probably by sending a short silence). */
+        at_modem_control(s, AT_MODEM_CONTROL_RESTART, (void *) T31_FLUSH);
+        s->do_hangup = TRUE;
+        at_set_at_rx_mode(s, AT_MODE_CONNECTED);
+        return (const char *) -1;
+    }
+    at_modem_control(s, AT_MODEM_CONTROL_HANGUP, NULL);
+    at_set_at_rx_mode(s, AT_MODE_ONHOOK_COMMAND);
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_I(at_state_t *s, const char *t)
+{
+    int val;
+
+    /* V.250 6.1.3 - Request identification information */ 
+    /* N.B. The information supplied in response to an ATIx command is very
+       variable. It was widely used in different ways before the AT command
+       set was standardised by the ITU. */
+    t += 1;
+    switch (val = parse_num(&t, 255))
+    {
+    case 0:
+        at_put_response(s, model);
+        break;
+    case 3:
+        at_put_response(s, manufacturer);
+        break;
+    default:
+        return NULL;
+    }
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_L(at_state_t *s, const char *t)
+{
+    int val;
+
+    /* V.250 6.3.13 - Monitor speaker loudness */
+    /* Just absorb this command, as we have no speaker */
+    t += 1;
+    if ((val = parse_num(&t, 255)) < 0)
+        return NULL;
+    s->speaker_volume = val;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_M(at_state_t *s, const char *t)
+{
+    int val;
+
+    /* V.250 6.3.14 - Monitor speaker mode */ 
+    /* Just absorb this command, as we have no speaker */
+    t += 1;
+    if ((val = parse_num(&t, 255)) < 0)
+        return NULL;
+    s->speaker_mode = val;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_O(at_state_t *s, const char *t)
+{
+    int val;
+
+    /* V.250 6.3.7 - Return to online data state */ 
+    t += 1;
+    if ((val = parse_num(&t, 1)) < 0)
+        return NULL;
+    if (val == 0)
+    {
+        at_set_at_rx_mode(s, AT_MODE_CONNECTED);
+        at_put_response_code(s, AT_RESPONSE_CODE_CONNECT);
+    }
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_P(at_state_t *s, const char *t)
+{
+    /* V.250 6.3.3 - Select pulse dialling (command) */ 
+    t += 1;
+    s->p.pulse_dial = TRUE;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_Q(at_state_t *s, const char *t)
+{
+    int val;
+
+    /* V.250 6.2.5 - Result code suppression */ 
+    t += 1;
+    if ((val = parse_num(&t, 1)) < 0)
+        return NULL;
+    switch (val)
+    {
+    case 0:
+        s->p.result_code_format = (s->p.verbose)  ?  ASCII_RESULT_CODES  :  NUMERIC_RESULT_CODES;
+        break;
+    case 1:
+        s->p.result_code_format = NO_RESULT_CODES;
+        break;
+    }
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_S0(at_state_t *s, const char *t)
+{
+    /* V.250 6.3.8 - Automatic answer */ 
+    t += 2;
+    return s_reg_handler(s, t, 0);
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_S10(at_state_t *s, const char *t)
+{
+    /* V.250 6.3.12 - Automatic disconnect delay */ 
+    t += 3;
+    return s_reg_handler(s, t, 10);
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_S3(at_state_t *s, const char *t)
+{
+    /* V.250 6.2.1 - Command line termination character */ 
+    t += 2;
+    return s_reg_handler(s, t, 3);
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_S4(at_state_t *s, const char *t)
+{
+    /* V.250 6.2.2 - Response formatting character */ 
+    t += 2;
+    return s_reg_handler(s, t, 4);
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_S5(at_state_t *s, const char *t)
+{
+    /* V.250 6.2.3 - Command line editing character */ 
+    t += 2;
+    return s_reg_handler(s, t, 5);
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_S6(at_state_t *s, const char *t)
+{
+    /* V.250 6.3.9 - Pause before blind dialling */ 
+    t += 2;
+    return s_reg_handler(s, t, 6);
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_S7(at_state_t *s, const char *t)
+{
+    /* V.250 6.3.10 - Connection completion timeout */ 
+    t += 2;
+    return s_reg_handler(s, t, 7);
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_S8(at_state_t *s, const char *t)
+{
+    /* V.250 6.3.11 - Comma dial modifier time */ 
+    t += 2;
+    return s_reg_handler(s, t, 8);
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_T(at_state_t *s, const char *t)
+{
+    /* V.250 6.3.2 - Select tone dialling (command) */ 
+    t += 1;
+    s->p.pulse_dial = FALSE;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_V(at_state_t *s, const char *t)
+{
+    int val;
+
+    /* V.250 6.2.6 - DCE response format */ 
+    t += 1;
+    if ((val = parse_num(&t, 1)) < 0)
+        return NULL;
+    s->p.verbose = val;
+    if (s->p.result_code_format != NO_RESULT_CODES)
+        s->p.result_code_format = (s->p.verbose)  ?  ASCII_RESULT_CODES  :  NUMERIC_RESULT_CODES;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_X(at_state_t *s, const char *t)
+{
+    int val;
+
+    /* V.250 6.2.7 - Result code selection and call progress monitoring control */
+    /* 0    CONNECT result code is given upon entering online data state.
+            Dial tone and busy detection are disabled.
+       1    CONNECT <text> result code is given upon entering online data state.
+            Dial tone and busy detection are disabled.
+       2    CONNECT <text> result code is given upon entering online data state.
+            Dial tone detection is enabled, and busy detection is disabled.
+       3    CONNECT <text> result code is given upon entering online data state.
+            Dial tone detection is disabled, and busy detection is enabled.
+       4    CONNECT <text> result code is given upon entering online data state.
+            Dial tone and busy detection are both enabled. */
+    t += 1;
+    if ((val = parse_num(&t, 4)) < 0)
+        return NULL;
+    s->result_code_mode = val;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_Z(at_state_t *s, const char *t)
+{
+    int val;
+
+    /* V.250 6.1.1 - Reset to default configuration */ 
+    t += 1;
+    if ((val = parse_num(&t, sizeof(profiles)/sizeof(profiles[0]) - 1)) < 0)
+        return NULL;
+    /* Just make sure we are on hook */
+    at_modem_control(s, AT_MODEM_CONTROL_HANGUP, NULL);
+    at_set_at_rx_mode(s, AT_MODE_ONHOOK_COMMAND);
+    s->p = profiles[val];
+    at_reset_call_info(s);
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_amp_C(at_state_t *s, const char *t)
+{
+    int val;
+
+    /* V.250 6.2.8 - Circuit 109 (received line signal detector) behaviour */ 
+    /* We have no RLSD pin, so just absorb this. */
+    t += 2;
+    if ((val = parse_num(&t, 1)) < 0)
+        return NULL;
+    s->rlsd_behaviour = val;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_amp_D(at_state_t *s, const char *t)
+{
+    int val;
+
+    /* V.250 6.2.9 - Circuit 108 (data terminal ready) behaviour */ 
+    t += 2;
+    if ((val = parse_num(&t, 2)) < 0)
+        return NULL;
+    /* TODO: We have no DTR pin, but we need this to get into online
+             command state. */
+    s->dtr_behaviour = val;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_amp_F(at_state_t *s, const char *t)
+{
+    t += 2;
+
+    /* V.250 6.1.2 - Set to factory-defined configuration */ 
+    /* Just make sure we are on hook */
+    at_modem_control(s, AT_MODEM_CONTROL_HANGUP, NULL);
+    at_set_at_rx_mode(s, AT_MODE_ONHOOK_COMMAND);
+    s->p = profiles[0];
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_A8A(at_state_t *s, const char *t)
+{
+    /* V.251 6.3 - V.8 calling tone indication */
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_A8C(at_state_t *s, const char *t)
+{
+    /* V.251 6.2 - V.8 answer signal indication */
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_A8E(at_state_t *s, const char *t)
+{
+    int val;
+
+    /* V.251 5.1 - V.8 and V.8bis operation controls */
+    /* Syntax: +A8E=<v8o>,<v8a>,<v8cf>[,<v8b>][,<cfrange>][,<protrange>] */
+    /* <v8o>=0  Disable V.8 origination negotiation
+       <v8o>=1  Enable DCE-controlled V.8 origination negotiation
+       <v8o>=2  Enable DTE-controlled V.8 origination negotiation, send V.8 CI only
+       <v8o>=3  Enable DTE-controlled V.8 origination negotiation, send 1100Hz CNG only
+       <v8o>=4  Enable DTE-controlled V.8 origination negotiation, send 1300Hz CT only
+       <v8o>=5  Enable DTE-controlled V.8 origination negotiation, send no tones
+       <v8o>=6  Enable DCE-controlled V.8 origination negotiation, issue +A8x indications
+       <v8a>=0  Disable V.8 answer negotiation
+       <v8a>=1  Enable DCE-controlled V.8 answer negotiation
+       <v8a>=2  Enable DTE-controlled V.8 answer negotiation, send ANSam
+       <v8a>=3  Enable DTE-controlled V.8 answer negotiation, send no signal
+       <v8a>=4  Disable DTE-controlled V.8 answer negotiation, send ANS
+       <v8a>=5  Enable DCE-controlled V.8 answer negotiation, issue +A8x indications
+       <v8cf>=X..Y Set the V.8 CI signal call function to the hexadecimal octet value X..Y
+       <v8b>=0  Disable V.8bis negotiation
+       <v8b>=1  Enable DCE-controlled V.8bis negotiation
+       <v8b>=2  Enable DTE-controlled V.8bis negotiation
+       <cfrange>="<string of values>"   Set to alternative list of call function "option bit"
+                                        values that the answering DCE shall accept from the caller
+       <protrange>="<string of values>" Set to alternative list of protocol "option bit" values that
+                                        the answering DCE shall accept from the caller
+    */
+    /* TODO: */
+    t += 4;
+    if (!parse_out(s, &t, &val, 6, "+A8E:", "(0-6),(0-5),(00-FF)"))
+        return NULL;
+    if (*t != ',')
+        return t;
+    if ((val = parse_num(&t, 5)) < 0)
+        return NULL;
+    if (*t != ',')
+        return t;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_A8I(at_state_t *s, const char *t)
+{
+    /* V.251 6.1 - V.8 CI signal indication */
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_A8J(at_state_t *s, const char *t)
+{
+    /* V.251 6.4 - V.8 negotiation complete */
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_A8M(at_state_t *s, const char *t)
+{
+    /* V.251 5.2 - Send V.8 menu signals */
+    /* Syntax: +A8M=<hexadecimal coded CM or JM octet string>  */
+    /* TODO: */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_A8R(at_state_t *s, const char *t)
+{
+    /* V.251 6.6 - V.8bis signal and message reporting */
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_A8T(at_state_t *s, const char *t)
+{
+    int val;
+
+    /* V.251 5.3 - Send V.8bis signal and/or message(s) */
+    /* Syntax: +A8T=<signal>[,<1st message>][,<2nd message>][,<sig_en>][,<msg_en>][,<supp_delay>] */
+    /*  <signal>=0  None
+        <signal>=1  Initiating Mre
+        <signal>=2  Initiating MRd
+        <signal>=3  Initiating CRe, low power
+        <signal>=4  Initiating CRe, high power
+        <signal>=5  Initiating CRd
+        <signal>=6  Initiating Esi
+        <signal>=7  Responding MRd, low power
+        <signal>=8  Responding MRd, high power
+        <signal>=9  Responding CRd
+        <signal>=10 Responding Esr
+    */
+    /* TODO: */
+    t += 4;
+    if (!parse_out(s, &t, &val, 10, "+A8T:", "(0-10)"))
+        return NULL;
+    s->v8bis_signal = val;
+    if (*t != ',')
+        return t;
+    if ((val = parse_num(&t, 255)) < 0)
+        return NULL;
+    s->v8bis_1st_message = val;
+    if (*t != ',')
+        return t;
+    if ((val = parse_num(&t, 255)) < 0)
+        return NULL;
+    s->v8bis_2nd_message = val;
+    if (*t != ',')
+        return t;
+    if ((val = parse_num(&t, 255)) < 0)
+        return NULL;
+    s->v8bis_sig_en = val;
+    if (*t != ',')
+        return t;
+    if ((val = parse_num(&t, 255)) < 0)
+        return NULL;
+    s->v8bis_msg_en = val;
+    if (*t != ',')
+        return t;
+    if ((val = parse_num(&t, 255)) < 0)
+        return NULL;
+    s->v8bis_supp_delay = val;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_ASTO(at_state_t *s, const char *t)
+{
+    /* V.250 6.3.15 - Store telephone number */ 
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+ASTO:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CAAP(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 7.25 - Automatic answer for eMLPP Service */
+    /* TODO: */
+    t += 5;
+    if (!parse_2_out(s, &t, NULL, 65535, NULL, 65535, "+CAAP:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CACM(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 8.25 - Accumulated call meter */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+CACM:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CACSP(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 11.1.7 - Voice Group or Voice Broadcast Call State Attribute Presentation */
+    /* TODO: */
+    t += 6;
+    if (!parse_out(s, &t, NULL, 1, "+CACSP:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CAD(at_state_t *s, const char *t)
+{
+    /* IS-99 5.6.3 - Query analogue or digital service */
+    /* TODO: */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CAEMLPP(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 7.22 - eMLPP Priority Registration and Interrogation */
+    /* TODO: */
+    t += 8;
+    if (!parse_out(s, &t, NULL, 1, "+CAEMLPP:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CAHLD(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 11.1.3 - Leave an ongoing Voice Group or Voice Broadcast Call */
+    /* TODO: */
+    t += 6;
+    if (!parse_out(s, &t, NULL, 1, "+CAHLD:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CAJOIN(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 11.1.1 - Accept an incoming Voice Group or Voice Broadcast Call */
+    /* TODO: */
+    t += 7;
+    if (!parse_out(s, &t, NULL, 1, "+CAJOIN:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CALA(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 8.16 - Alarm */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+CALA:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CALCC(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 11.1.6 - List current Voice Group and Voice Broadcast Calls */
+    /* TODO: */
+    t += 6;
+    if (!parse_out(s, &t, NULL, 1, "+CALCC:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CALD(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 8.38 - Delete alarm */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+CALD:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CALM(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 8.20 - Alert sound mode */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+CALM:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CAMM(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 8.26 - Accumulated call meter maximum */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+CAMM:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CANCHEV(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 11.1.8 - NCH Support Indication */
+    /* TODO: */
+    t += 8;
+    if (!parse_out(s, &t, NULL, 1, "+CANCHEV:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CAOC(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 7.16 - Advice of Charge */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+CAOC:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CAPD(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 8.39 - Postpone or dismiss an alarm */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+CAPD:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CAPTT(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 11.1.4 - Talker Access for Voice Group Call */
+    /* TODO: */
+    t += 6;
+    if (!parse_out(s, &t, NULL, 1, "+CAPTT:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CAREJ(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 11.1.2 - Reject an incoming Voice Group or Voice Broadcast Call */
+    /* TODO: */
+    t += 6;
+    if (!parse_out(s, &t, NULL, 1, "+CAREJ:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CAULEV(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 11.1.5 - Voice Group Call Uplink Status Presentation */
+    /* TODO: */
+    t += 7;
+    if (!parse_out(s, &t, NULL, 1, "+CAULEV:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CBC(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 8.4 - Battery charge */
+    /* TODO: */
+    t += 4;
+    if (!parse_out(s, &t, NULL, 1, "+CBC:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CBCS(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 11.3.2 - VBS subscriptions and GId status */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+CBCS:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CBIP(at_state_t *s, const char *t)
+{
+    /* IS-99 5.6 - Base station IP address */
+    /* TODO: */
+    t += 5;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CBST(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 6.7 - Select bearer service type */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+CBST:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CCFC(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 7.11 - Call forwarding number and conditions */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+CCFC:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CCLK(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 8.15 - Clock */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+CCLK:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CCS(at_state_t *s, const char *t)
+{
+    /* IS-135 4.1.22 - Compression status */
+    /* TODO: */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CCUG(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 7.10 - Closed user group */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+CCUG:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CCWA(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 7.12 - Call waiting */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+CCWA:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CCWE(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 8.28 - Call Meter maximum event */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+CCWE:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CDIP(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 7.9 - Called line identification presentation */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+CDIP:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CDIS(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 8.8 - Display control */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+CDIS:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CDV(at_state_t *s, const char *t)
+{
+    /* IS-99 5.6 - Dial command for voice call */
+    /* TODO: */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CEER(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 6.10 - Extended error report */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+CEER:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CESP(at_state_t *s, const char *t)
+{
+    /* GSM07.05  3.2.4 - Enter SMS block mode protocol */
+    /* TODO: */
+    t += 5;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CFCS(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 7.24 - Fast call setup conditions */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+CFCS:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CFG(at_state_t *s, const char *t)
+{
+    /* IS-99 5.6 - Configuration string */
+    /* TODO: */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CFUN(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 8.2 - Set phone functionality */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+CFUN:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CGACT(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 10.1.10 - PDP context activate or deactivate */
+    /* TODO: */
+    t += 6;
+    if (!parse_out(s, &t, NULL, 1, "+CGACT:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CGANS(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 10.1.16 - Manual response to a network request for PDP context activation */
+    /* TODO: */
+    t += 6;
+    if (!parse_out(s, &t, NULL, 1, "+CGANS:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CGATT(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 10.1.9 - PS attach or detach */
+    /* TODO: */
+    t += 6;
+    if (!parse_out(s, &t, NULL, 1, "+CGATT:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CGAUTO(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 10.1.15 - Automatic response to a network request for PDP context activation */
+    /* TODO: */
+    t += 7;
+    if (!parse_out(s, &t, NULL, 1, "+CGAUTO:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CGCAP(at_state_t *s, const char *t)
+{
+    /* IS-99 5.6 - Request complete capabilities list */
+    /* TODO: */
+    t += 6;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CGCLASS(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 10.1.17 - GPRS mobile station class (GPRS only) */
+    /* TODO: */
+    t += 8;
+    if (!parse_out(s, &t, NULL, 1, "+CGCLASS:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CGCLOSP(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 10.1.13 - Configure local Octet Stream PAD parameters (Obsolete) */
+    /* TODO: */
+    t += 8;
+    if (!parse_out(s, &t, NULL, 1, "+CGCLOSP:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CGCLPAD(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 10.1.12 - Configure local triple-X PAD parameters (GPRS only) (Obsolete) */
+    /* TODO: */
+    t += 8;
+    if (!parse_out(s, &t, NULL, 1, "+CGCLPAD:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CGCMOD(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 10.1.11 - PDP Context Modify */
+    /* TODO: */
+    t += 7;
+    if (!parse_out(s, &t, NULL, 1, "+CGCMOD:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CGCS(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 11.3.1 - VGCS subscriptions and GId status */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+CGCS:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CGDATA(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 10.1.12 - Enter data state */
+    /* TODO: */
+    t += 7;
+    if (!parse_out(s, &t, NULL, 1, "+CGDATA:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CGDCONT(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 10.1.1 - Define PDP Context */
+    /* TODO: */
+    t += 8;
+    if (!parse_out(s, &t, NULL, 1, "+CGDCONT:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CGDSCONT(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 10.1.2 - Define Secondary PDP Context */
+    /* TODO: */
+    t += 9;
+    if (!parse_out(s, &t, NULL, 1, "+CGDSCONT:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CGEQMIN(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 10.1.7 - 3G Quality of Service Profile (Minimum acceptable) */
+    /* TODO: */
+    t += 8;
+    if (!parse_out(s, &t, NULL, 1, "+CGEQMIN:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CGEQNEG(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 10.1.8 - 3G Quality of Service Profile (Negotiated) */
+    /* TODO: */
+    t += 8;
+    if (!parse_out(s, &t, NULL, 1, "+CGEQNEG:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CGEQREQ(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 10.1.6 - 3G Quality of Service Profile (Requested) */
+    /* TODO: */
+    t += 8;
+    if (!parse_out(s, &t, NULL, 1, "+CGEQREQ:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CGEREP(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 10.1.18 - Packet Domain event reporting */
+    /* TODO: */
+    t += 7;
+    if (!parse_out(s, &t, NULL, 1, "+CGEREP:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CGMI(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 5.1 - Request manufacturer identification */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+CGMI:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CGMM(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 5.2 - Request model identification */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+CGMM:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CGMR(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 5.3 - Request revision identification */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+CGMR:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CGOI(at_state_t *s, const char *t)
+{
+    /* IS-99 5.6 - Request global object identification */
+    /* TODO: */
+    t += 5;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CGPADDR(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 10.1.14 - Show PDP address */
+    /* TODO: */
+    t += 8;
+    if (!parse_out(s, &t, NULL, 1, "+CGPADDR:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CGQMIN(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 10.1.5 - Quality of Service Profile (Minimum acceptable) */
+    /* TODO: */
+    t += 7;
+    if (!parse_out(s, &t, NULL, 1, "+CGQMIN:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CGQREQ(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 10.1.4 - Quality of Service Profile (Requested) */
+    /* TODO: */
+    t += 7;
+    if (!parse_out(s, &t, NULL, 1, "+CGQREQ:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CGREG(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 10.1.19 - GPRS network registration status */
+    /* TODO: */
+    t += 6;
+    if (!parse_out(s, &t, NULL, 1, "+CGREG:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CGSMS(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 10.1.20 - Select service for MO SMS messages */
+    /* TODO: */
+    t += 6;
+    if (!parse_out(s, &t, NULL, 1, "+CGSMS:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CGSN(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 5.4 - Request product serial number identification */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+CGSN:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CGTFT(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 10.1.3 - Traffic Flow Template */
+    /* TODO: */
+    t += 6;
+    if (!parse_out(s, &t, NULL, 1, "+CGTFT:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CHLD(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 7.13 - Call related supplementary services */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+CHLD:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CHSA(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 6.18 - HSCSD non-transparent asymmetry configuration */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+CHSA:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CHSC(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 6.15 - HSCSD current call parameters */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+CHSC:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CHSD(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 6.12 - HSCSD device parameters */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+CHSD:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CHSN(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 6.14 - HSCSD non-transparent call configuration */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+CHSN:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CHSR(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 6.16 - HSCSD parameters report */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+CHSR:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CHST(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 6.13 - HSCSD transparent call configuration */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+CHST:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CHSU(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 6.17 - HSCSD automatic user initiated upgrading */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+CHSU:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CHUP(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 6.5 - Hangup call */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+CHUP:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CHV(at_state_t *s, const char *t)
+{
+    /* IS-99 5.6 - Hang-up voice */
+    /* TODO: */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CIMI(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 5.6 - Request international mobile subscriber identity */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+CIMI:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CIND(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 8.9 - Indicator control */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+CIND:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CIT(at_state_t *s, const char *t)
+{
+    /* IS-99 5.6 - Command state inactivity timer */
+    /* TODO: */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CKPD(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 8.7 - Keypad control */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+CKPD:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CLAC(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 8.37 - List all available AT commands */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+CLAC:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CLAE(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 8.31 - Language Event */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+CLAE:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CLAN(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 8.30 - Set Language */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+CLAN:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CLCC(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 7.18 - List current calls */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+CLCC:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CLCK(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 7.4 - Facility lock */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+CLCK:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CLIP(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 7.6 - Calling line identification presentation */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+CLIP:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CLIR(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 7.7 - Calling line identification restriction */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+CLIR:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CLVL(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 8.23 - Loudspeaker volume level */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+CLVL:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CMAR(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 8.36 - Master Reset */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+CMAR:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CMEC(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 8.6 - Mobile Termination control mode */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+CMEC:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CMEE(at_state_t *s, const char *t)
+{
+    /* GSM07.07 9.1 - Report mobile equipment error */
+    /* TODO: */
+    t += 5;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CMER(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 8.10 - Mobile Termination event reporting */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+CMER:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CMGC(at_state_t *s, const char *t)
+{
+    /* GSM07.05 3.5.5/4.5 - Send command */
+    /* TODO: */
+    t += 5;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CMGD(at_state_t *s, const char *t)
+{
+    /* GSM07.05 3.5.4 - Delete message */
+    /* TODO: */
+    t += 5;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CMGF(at_state_t *s, const char *t)
+{
+    /* GSM07.05 3.2.3 - Message Format */
+    /* TODO: */
+    t += 5;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CMGL(at_state_t *s, const char *t)
+{
+    /* GSM07.05 3.4.2/4.1 -  List messages */
+    /* TODO: */
+    t += 5;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CMGR(at_state_t *s, const char *t)
+{
+    /* GSM07.05 3.4.3/4.2 - Read message */
+    /* TODO: */
+    t += 5;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CMGS(at_state_t *s, const char *t)
+{
+    /* GSM07.05 3.5.1/4.3 - Send message */
+    /* TODO: */
+    t += 5;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CMGW(at_state_t *s, const char *t)
+{
+    /* GSM07.05 3.5.3/4.4 - Write message to memory */
+    /* TODO: */
+    t += 5;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CMIP(at_state_t *s, const char *t)
+{
+    /* IS-99 5.6 - Mobile station IP address */
+    /* TODO: */
+    t += 5;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CMM(at_state_t *s, const char *t)
+{
+    /* IS-135 4.1.23 - Menu map */
+    /* TODO: */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CMMS(at_state_t *s, const char *t)
+{
+    /* GSM07.05 3.5.6 - More messages to send */
+    /* TODO: */
+    t += 5;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CMOD(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 6.4 - Call mode */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+CMOD:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CMSS(at_state_t *s, const char *t)
+{
+    /* GSM07.05 3.5.2/4.7 - Send message from storage */
+    /* TODO: */
+    t += 5;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CMUT(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 8.24 - Mute control */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+CMUT:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CNMA(at_state_t *s, const char *t)
+{
+    /* GSM07.05 3.4.4/4.6 - New message acknowledgement to terminal adapter */
+    /* TODO: */
+    t += 5;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CNMI(at_state_t *s, const char *t)
+{
+    /* GSM07.05 3.4.1 - New message indications to terminal equipment */
+    /* TODO: */
+    t += 5;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CMUX(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 5.7 - Multiplexing mode */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+CMUX:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CNUM(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 7.1 - Subscriber number */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+CNUM:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_COLP(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 7.8 - Connected line identification presentation */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+COLP:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_COPN(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 7.21 - Read operator names */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+COPN:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_COPS(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 7.3 - PLMN selection */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+COPS:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_COS(at_state_t *s, const char *t)
+{
+    /* IS-135 4.1.24 - Originating service */
+    /* TODO: */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_COTDI(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 11.1.9 - Originator to Dispatcher Information */
+    /* TODO: */
+    t += 6;
+    if (!parse_out(s, &t, NULL, 1, "+COTDI:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CPAS(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 8.1 - Phone activity status */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+CPAS:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CPBF(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 8.13 - Find phonebook entries */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+CPBF:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CPBR(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 8.12 - Read phonebook entries */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+CPBR:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CPBS(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 8.11 - Select phonebook memory storage */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+CPBS:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CPBW(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 8.14 - Write phonebook entry */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+CPBW:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CPIN(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 8.3 - Enter PIN */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+CPIN:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CPLS(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 7.20 - Selection of preferred PLMN list */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+CPLS:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CPMS(at_state_t *s, const char *t)
+{
+    /* GSM07.05 3.2.2 - Preferred message storage */
+    /* TODO: */
+    t += 5;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CPOL(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 7.19 - Preferred PLMN list */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+CPOL:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CPPS(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 7.23 - eMLPP subscriptions */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+CPPS:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CPROT(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 8.42 - Enter protocol mode */
+    /* TODO: */
+    t += 6;
+    if (!parse_out(s, &t, NULL, 1, "+CPROT:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CPUC(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 8.27 - Price per unit and currency table */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+CPUC:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CPWC(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 8.29 - Power class */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+CPWC:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CPWD(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 7.5 - Change password */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+CPWD:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CQD(at_state_t *s, const char *t)
+{
+    /* IS-135 4.1.25 - Query disconnect timer */
+    /* TODO: */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CR(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 6.9 - Service reporting control */
+    /* TODO: */
+    t += 3;
+    if (!parse_out(s, &t, NULL, 1, "+CR:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CRC(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 6.11 - Cellular result codes */
+    /* TODO: */
+    t += 4;
+    if (!parse_out(s, &t, NULL, 1, "+CRC:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CREG(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 7.2 - Network registration */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+CREG:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CRES(at_state_t *s, const char *t)
+{
+    /* GSM07.05 3.3.6 - Restore Settings */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+CRLP:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CRLP(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 6.8 - Radio link protocol */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+CRLP:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CRM(at_state_t *s, const char *t)
+{
+    /* IS-99 5.6 - Set rm interface protocol */
+    /* TODO: */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CRMC(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 8.34 - Ring Melody Control */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+CRMC:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CRMP(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 8.35 - Ring Melody Playback */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+CRMP:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CRSL(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 8.21 - Ringer sound level */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+CRSL:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CRSM(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 8.18 - Restricted SIM access */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+CRSM:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CSAS(at_state_t *s, const char *t)
+{
+    /* GSM07.05 3.3.5 - Save settings */
+    /* TODO: */
+    t += 5;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CSCA(at_state_t *s, const char *t)
+{
+    /* GSM07.05 3.3.1 - Service centre address */
+    /* TODO: */
+    t += 5;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CSCB(at_state_t *s, const char *t)
+{
+    /* GSM07.05 3.3.4 - Select cell broadcast message types */
+    /* TODO: */
+    t += 5;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CSCC(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 8.19 - Secure control command */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+CSCC:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CSCS(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 5.5 - Select TE character set */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+CSCS:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CSDF(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 6.22 - Settings date format */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+CSDF:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CSDH(at_state_t *s, const char *t)
+{
+    /* GSM07.05 3.3.3 - Show text mode parameters */
+    /* TODO: */
+    t += 5;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CSGT(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 8.32 - Set Greeting Text */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+CSGT:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CSIL(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 6.23 - Silence Command */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+CSIL:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CSIM(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 8.17 - Generic SIM access */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+CSIM:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CSMP(at_state_t *s, const char *t)
+{
+    /* GSM07.05 3.3.2 - Set text mode parameters */
+    /* TODO: */
+    t += 5;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CSMS(at_state_t *s, const char *t)
+{
+    /* GSM07.05 3.2.1 - Select Message Service */
+    /* TODO: */
+    t += 5;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CSNS(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 6.19 - Single numbering scheme */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+CSNS:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CSQ(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 8.5 - Signal quality */
+    /* TODO: */
+    t += 4;
+    if (!parse_out(s, &t, NULL, 1, "+CSQ:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CSS(at_state_t *s, const char *t)
+{
+    /* IS-135 4.1.28 - Serving system identification */
+    /* TODO: */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CSSN(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 7.17 - Supplementary service notifications */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+CSSN:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CSTA(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 6.1 - Select type of address */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+CSTA:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CSTF(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 6.24 - Settings time format */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+CSTF:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CSVM(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 8.33 - Set Voice Mail Number */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+CSVM:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CTA(at_state_t *s, const char *t)
+{
+    /* IS-135 4.1.29 - MT-Terminated async. Data calls */
+    /* TODO: */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CTF(at_state_t *s, const char *t)
+{
+    /* IS-135 4.1.30 - MT-Terminated FAX calls */
+    /* TODO: */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CTFR(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 7.14 - Call deflection */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+CTFR:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CTZR(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 8.41 - Time Zone Reporting */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+CTZR:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CTZU(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 8.40 - Automatic Time Zone Update */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+CTZU:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CUSD(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 7.15 - Unstructured supplementary service data */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+CUSD:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CUUS1(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 7.26 - User to User Signalling Service 1 */
+    /* TODO: */
+    t += 6;
+    if (!parse_out(s, &t, NULL, 1, "+CUUS1:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CV120(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 6.21 - V.120 rate adaption protocol */
+    /* TODO: */
+    t += 6;
+    if (!parse_out(s, &t, NULL, 1, "+CV120:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CVHU(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 6.20 - Voice Hangup Control */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+CVHU:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CVIB(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 8.22 - Vibrator mode */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+CVIB:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_CXT(at_state_t *s, const char *t)
+{
+    /* IS-99 5.6 - Cellular extension */
+    /* TODO: */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_DR(at_state_t *s, const char *t)
+{
+    /* V.250 6.6.2 - Data compression reporting */ 
+    /* TODO: */
+    t += 3;
+    if (!parse_out(s, &t, NULL, 1, "+DR:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_DS(at_state_t *s, const char *t)
+{
+    /* V.250 6.6.1 - Data compression */ 
+    /* TODO: */
+    t += 3;
+    if (!parse_out(s, &t, NULL, 1, "+DS:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_DS44(at_state_t *s, const char *t)
+{
+    /* V.250 6.6.2 - V.44 data compression */
+    /* TODO: */
+    t += 5;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_EB(at_state_t *s, const char *t)
+{
+    /* V.250 6.5.2 - Break handling in error control operation */ 
+    /* TODO: */
+    t += 3;
+    if (!parse_out(s, &t, NULL, 1, "+EB:", ""))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_EFCS(at_state_t *s, const char *t)
+{
+    /* V.250 6.5.4 - 32-bit frame check sequence */ 
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 2, "+EFCS:", "(0-2)"))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_EFRAM(at_state_t *s, const char *t)
+{
+    /* V.250 6.5.8 - Frame length */ 
+    /* TODO: */
+    t += 6;
+    if (!parse_2_out(s, &t, NULL, 65535, NULL, 65535, "+EFRAM:", "(1-65535),(1-65535)"))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_ER(at_state_t *s, const char *t)
+{
+    /* V.250 6.5.5 - Error control reporting */ 
+    /*  0   Error control reporting disabled (no +ER intermediate result code transmitted)
+        1   Error control reporting enabled (+ER intermediate result code transmitted) */
+    /* TODO: */
+    t += 3;
+    if (!parse_out(s, &t, NULL, 1, "+ER:", "(0,1)"))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_ES(at_state_t *s, const char *t)
+{
+    /* V.250 6.5.1 - Error control selection */ 
+    /* TODO: */
+    t += 3;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_ESR(at_state_t *s, const char *t)
+{
+    /* V.250 6.5.3 - Selective repeat */ 
+    /* TODO: */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_ETBM(at_state_t *s, const char *t)
+{
+    /* V.250 6.5.6 - Call termination buffer management */ 
+    /* TODO: */
+    t += 5;
+    if (!parse_2_out(s, &t, NULL, 2, NULL, 2, "+ETBM:", "(0-2),(0-2),(0-30)"))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_EWIND(at_state_t *s, const char *t)
+{
+    /* V.250 6.5.7 - Window size */ 
+    /* TODO: */
+    t += 6;
+    if (!parse_2_out(s, &t, &s->rx_window, 127, &s->tx_window, 127, "+EWIND:", "(1-127),(1-127)"))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_FAA(at_state_t *s, const char *t)
+{
+    /* T.32 8.5.2.5 - Adaptive Answer parameter */
+    /* TODO */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_FAP(at_state_t *s, const char *t)
+{
+    /* T.32 8.5.1.12 - Addressing and polling capabilities parameter */
+    /* TODO */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_FAR(at_state_t *s, const char *t)
+{
+    /* T.31 8.5.1 - Adaptive reception control */ 
+    t += 4;
+    if (!parse_out(s, &t, &s->p.adaptive_receive, 1, NULL, "0,1"))
+         return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_FBO(at_state_t *s, const char *t)
+{
+    /* T.32 8.5.3.4 - Phase C data bit order */
+    /* TODO */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_FBS(at_state_t *s, const char *t)
+{
+    /* T.32 8.5.3.2 - Buffer Size, read only parameter */
+    /* TODO */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_FBU(at_state_t *s, const char *t)
+{
+    /* T.32 8.5.1.10 - HDLC Frame Reporting parameter */
+    /* TODO */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_FCC(at_state_t *s, const char *t)
+{
+    /* T.32 8.5.1.1 - DCE capabilities parameters */
+    /* TODO */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_FCL(at_state_t *s, const char *t)
+{
+    /* T.31 8.5.2 - Carrier loss timeout */ 
+    t += 4;
+    if (!parse_out(s, &t, &s->carrier_loss_timeout, 255, NULL, "(0-255)"))
+         return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_FCLASS(at_state_t *s, const char *t)
+{
+    /* T.31 8.2 - Capabilities identification and control */ 
+    t += 7;
+    /* T.31 says the reply string should be "0,1.0", however making
+       it "0,1,1.0" makes things compatible with a lot more software
+       that may be expecting a pre-T.31 modem. */
+    if (!parse_string_list_out(s, &t, &s->fclass_mode, 1, NULL, "0,1,1.0"))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_FCQ(at_state_t *s, const char *t)
+{
+    /* T.32 8.5.2.3 -  Copy quality checking parameter */
+    /* TODO */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_FCR(at_state_t *s, const char *t)
+{
+    /* T.32 8.5.1.9 - Capability to receive parameter */
+    /* TODO */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_FCS(at_state_t *s, const char *t)
+{
+    /* T.32 8.5.1.3 - Current Session Results parameters */
+    /* TODO */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_FCT(at_state_t *s, const char *t)
+{
+    /* T.32 8.5.2.6 - DTE phase C timeout parameter */
+    /* TODO */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_FDD(at_state_t *s, const char *t)
+{
+    /* T.31 8.5.3 - Double escape character replacement */ 
+    t += 4;
+    if (!parse_out(s, &t, &s->p.double_escape, 1, NULL, "(0,1)"))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_FDR(at_state_t *s, const char *t)
+{
+    /* T.32 8.3.4 - Data reception command */
+    /* TODO */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_FDT(at_state_t *s, const char *t)
+{
+    /* T.32 8.3.3 - Data transmission command */
+    /* TODO */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_FEA(at_state_t *s, const char *t)
+{
+    /* T.32 8.5.3.5 - Phase C received EOL alignment parameter */
+    /* TODO */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_FFC(at_state_t *s, const char *t)
+{
+    /* T.32 8.5.3.6 - Format conversion parameter */
+    /* TODO */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_FFD(at_state_t *s, const char *t)
+{
+    /* T.32 8.5.1.14 - File diagnostic message parameter */
+    /* TODO */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_FHS(at_state_t *s, const char *t)
+{
+    /* T.32 8.5.2.7 - Call termination status parameter */
+    /* TODO */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_FIE(at_state_t *s, const char *t)
+{
+    /* T.32 8.5.2.1 - Procedure interrupt enable parameter */
+    /* TODO */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_FIP(at_state_t *s, const char *t)
+{
+    /* T.32 8.3.6 - Initialize Facsimile Parameters */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_FIS(at_state_t *s, const char *t)
+{
+    /* T.32 8.5.1.2 -  Current session parameters */
+    /* TODO */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_FIT(at_state_t *s, const char *t)
+{
+    /* T.31 8.5.4 - DTE inactivity timeout */ 
+    t += 4;
+    if (!parse_2_out(s, &t, &s->dte_inactivity_timeout, 255, &s->dte_inactivity_action, 1, "+FIT:", "(0-255),(0-1)"))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_FKS(at_state_t *s, const char *t)
+{
+    /* T.32 8.3.5 - Session termination command */
+    /* TODO: */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_FLI(at_state_t *s, const char *t)
+{
+    /* T.32 8.5.1.5 - Local ID string parameter, TSI or CSI */
+    /* TODO: */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_FLO(at_state_t *s, const char *t)
+{
+    /* T.31 Annex A */ 
+    /* Implement something similar to the V.250 +IFC command */ 
+    /* TODO: */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_FLP(at_state_t *s, const char *t)
+{
+    /* T.32 8.5.1.7 - Indicate document to poll parameter */
+    /* TODO: */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_FMI(at_state_t *s, const char *t)
+{
+    /* T.31 says to duplicate +GMI */ 
+    t += 4;
+    if (t[0] == '?')
+    {
+        at_put_response(s, manufacturer);
+        t += 1;
+    }
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_FMM(at_state_t *s, const char *t)
+{
+    /* T.31 says to duplicate +GMM */ 
+    t += 4;
+    if (t[0] == '?')
+    {
+        at_put_response(s, model);
+        t += 1;
+    }
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_FMR(at_state_t *s, const char *t)
+{
+    /* T.31 says to duplicate +GMR */ 
+    t += 4;
+    if (t[0] == '?')
+    {
+        at_put_response(s, revision);
+        t += 1;
+    }
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_FMS(at_state_t *s, const char *t)
+{
+    /* T.32 8.5.2.9 - Minimum phase C speed parameter */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_FND(at_state_t *s, const char *t)
+{
+    /* T.32 8.5.2.10 - Non-Standard Message Data Indication parameter */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_FNR(at_state_t *s, const char *t)
+{
+    /* T.32 8.5.1.11 - Negotiation message reporting control parameters */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_FNS(at_state_t *s, const char *t)
+{
+    /* T.32 8.5.1.6 - Non-Standard Frame FIF parameter */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_FPA(at_state_t *s, const char *t)
+{
+    /* T.32 8.5.1.13 - Selective polling address parameter */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_FPI(at_state_t *s, const char *t)
+{
+    /* T.32 8.5.1.5 - Local Polling ID String parameter */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_FPP(at_state_t *s, const char *t)
+{
+    /* T.32 8.5.3 - Facsimile packet protocol */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_FPR(at_state_t *s, const char *t)
+{
+    /* T.31 Annex A */ 
+    /* Implement something similar to the V.250 +IPR command */ 
+    t += 4;
+    if (!parse_out(s, &t, &s->dte_rate, 115200, NULL, "115200"))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_FPS(at_state_t *s, const char *t)
+{
+    /* T.32 8.5.2.2 - Page Status parameter */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_FPW(at_state_t *s, const char *t)
+{
+    /* T.32 8.5.1.13 - PassWord parameter (Sending or Polling) */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_FRH(at_state_t *s, const char *t)
+{
+    /* T.31 8.3.6 - HDLC receive */ 
+    if (!process_class1_cmd(s, &t))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_FRM(at_state_t *s, const char *t)
+{
+    /* T.31 8.3.4 - Facsimile receive */ 
+    if (!process_class1_cmd(s, &t))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_FRQ(at_state_t *s, const char *t)
+{
+    /* T.32 8.5.2.4 - Receive Quality Thresholds parameters */
+    /* TODO */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_FRS(at_state_t *s, const char *t)
+{
+    /* T.31 8.3.2 - Receive silence */ 
+    if (!process_class1_cmd(s, &t))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_FRY(at_state_t *s, const char *t)
+{
+    /* T.32 8.5.2.8 - ECM Retry Value parameter */
+    /* TODO */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_FSA(at_state_t *s, const char *t)
+{
+    /* T.32 8.5.1.13 - Subaddress parameter */
+    /* TODO */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_FSP(at_state_t *s, const char *t)
+{
+    /* T.32 8.5.1.8 - Request to poll parameter */
+    /* TODO */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_FTH(at_state_t *s, const char *t)
+{
+    /* T.31 8.3.5 - HDLC transmit */ 
+    if (!process_class1_cmd(s, &t))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_FTM(at_state_t *s, const char *t)
+{
+    /* T.31 8.3.3 - Facsimile transmit */ 
+    if (!process_class1_cmd(s, &t))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_FTS(at_state_t *s, const char *t)
+{
+    /* T.31 8.3.1 - Transmit silence */ 
+    if (!process_class1_cmd(s, &t))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_GCAP(at_state_t *s, const char *t)
+{
+    /* V.250 6.1.9 - Request complete capabilities list */
+    t += 5;
+    /* Response elements
+       +FCLASS     +F (FAX) commands
+       +MS         +M (modulation control) commands +MS and +MR
+       +MV18S      +M (modulation control) commands +MV18S and +MV18R
+       +ES         +E (error control) commands +ES, +EB, +ER, +EFCS, and +ETBM
+       +DS         +D (data compression) commands +DS and +DR */
+    /* TODO: make this adapt to the configuration we really have. */
+    if (t[0] == '?')
+    {
+        at_put_response(s, "+GCAP:+FCLASS");
+        t += 1;
+    }
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_GCI(at_state_t *s, const char *t)
+{
+    /* V.250 6.1.10 - Country of installation, */ 
+    t += 4;
+    if (!parse_hex_out(s, &t, &s->country_of_installation, 255, "+GCI:", "(00-FF)"))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_GMI(at_state_t *s, const char *t)
+{
+    /* V.250 6.1.4 - Request manufacturer identification */ 
+    t += 4;
+    if (t[0] == '?')
+    {
+        at_put_response(s, manufacturer);
+        t += 1;
+    }
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_GMM(at_state_t *s, const char *t)
+{
+    /* V.250 6.1.5 - Request model identification */ 
+    t += 4;
+    if (t[0] == '?')
+    {
+        at_put_response(s, model);
+        t += 1;
+    }
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_GMR(at_state_t *s, const char *t)
+{
+    /* V.250 6.1.6 - Request revision identification */ 
+    t += 4;
+    if (t[0] == '?')
+    {
+        at_put_response(s, revision);
+        t += 1;
+    }
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_GOI(at_state_t *s, const char *t)
+{
+    /* V.250 6.1.8 - Request global object identification */ 
+    /* TODO: */
+    t += 4;
+    if (t[0] == '?')
+    {
+        at_put_response(s, GLOBAL_OBJECT_IDENTITY);
+        t += 1;
+    }
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_GSN(at_state_t *s, const char *t)
+{
+    /* V.250 6.1.7 - Request product serial number identification */ 
+    /* TODO: */
+    t += 4;
+    if (t[0] == '?')
+    {
+        at_put_response(s, SERIAL_NUMBER);
+        t += 1;
+    }
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_IBC(at_state_t *s, const char *t)
+{
+    /* TIA-617 8.3 - Control of in-band control */
+    /* TODO: */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_IBM(at_state_t *s, const char *t)
+{
+    /* TIA-617 8.4 - In-Band MARK idle reporting control */
+    /* TODO: */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_ICF(at_state_t *s, const char *t)
+{
+    /* V.250 6.2.11 - DTE-DCE character framing */ 
+    t += 4;
+    /* Character format
+        0    auto detect
+        1    8 data 2 stop
+        2    8 data 1 parity 1 stop
+        3    8 data 1 stop
+        4    7 data 2 stop
+        5    7 data 1 parity 1 stop
+        6    7 data 1 stop
+    
+       parity
+        0    Odd
+        1    Even
+        2    Mark
+        3    Space */
+    if (!parse_2_out(s, &t, &s->dte_char_format, 6, &s->dte_parity, 3, "+ICF:", "(0-6),(0-3)"))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_ICLOK(at_state_t *s, const char *t)
+{
+    /* V.250 6.2.14 - Select sync transmit clock source */ 
+    t += 6;
+    if (!parse_out(s, &t, &s->sync_tx_clock_source, 2, "+ICLOK:", "(0-2)"))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_IDSR(at_state_t *s, const char *t)
+{
+    /* V.250 6.2.16 - Select data set ready option */ 
+    t += 5;
+    if (!parse_out(s, &t, &s->dsr_option, 2, "+IDSR:", "(0-2)"))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_IFC(at_state_t *s, const char *t)
+{
+    /* V.250 6.2.12 - DTE-DCE local flow control */ 
+    /* TODO: */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_ILRR(at_state_t *s, const char *t)
+{
+    /* V.250 6.2.13 - DTE-DCE local rate reporting */ 
+    /* TODO: */
+    t += 5;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_ILSD(at_state_t *s, const char *t)
+{
+    /* V.250 6.2.15 - Select long space disconnect option */ 
+    t += 5;
+    if (!parse_out(s, &t, &s->long_space_disconnect_option, 2, "+ILSD:", "(0,1)"))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_IPR(at_state_t *s, const char *t)
+{
+    /* V.250 6.2.10 - Fixed DTE rate */ 
+    /* TODO: */
+    t += 4;
+    if (!parse_out(s, &t, &s->dte_rate, 115200, "+IPR:", "(115200),(115200)"))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_IRTS(at_state_t *s, const char *t)
+{
+    /* V.250 6.2.17 - Select synchronous mode RTS option */ 
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+IRTS:", "(0,1)"))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_MA(at_state_t *s, const char *t)
+{
+    /* V.250 6.4.2 - Modulation automode control */ 
+    /* TODO: */
+    t += 3;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_MR(at_state_t *s, const char *t)
+{
+    /* V.250 6.4.3 - Modulation reporting control */ 
+    /*  0    Disables reporting of modulation connection (+MCR: and +MRR: are not transmitted)
+        1    Enables reporting of modulation connection (+MCR: and +MRR: are transmitted) */
+    /* TODO: */
+    t += 3;
+    if (!parse_out(s, &t, NULL, 1, "+MR:", "(0,1)"))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_MS(at_state_t *s, const char *t)
+{
+    /* V.250 6.4.1 - Modulation selection */ 
+    /* TODO: */
+    t += 3;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_MSC(at_state_t *s, const char *t)
+{
+    /* V.250 6.4.8 - Seamless rate change enable */ 
+    /*  0   Disables V.34 seamless rate change 
+        1   Enables V.34 seamless rate change */
+    /* TODO: */
+    t += 4;
+    if (!parse_out(s, &t, NULL, 1, "+MSC:", "(0,1)"))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_MV18AM(at_state_t *s, const char *t)
+{
+    /* V.250 6.4.6 - V.18 answering message editing */ 
+    /* TODO: */
+    t += 7;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_MV18P(at_state_t *s, const char *t)
+{
+    /* V.250 6.4.7 - Order of probes */ 
+    /*  2    Send probe message in 5-bit (Baudot) mode
+        3    Send probe message in DTMF mode
+        4    Send probe message in EDT mode
+        5    Send Rec. V.21 carrier as a probe
+        6    Send Rec. V.23 carrier as a probe
+        7    Send Bell 103 carrier as a probe */
+    /* TODO: */
+    t += 6;
+    if (!parse_out(s, &t, NULL, 7, "+MV18P:", "(2-7)"))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_MV18R(at_state_t *s, const char *t)
+{
+    /* V.250 6.4.5 - V.18 reporting control */ 
+    /* TODO: */
+    t += 6;
+    if (!parse_out(s, &t, NULL, 1, "+MV18R:", "(0,1)"))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_MV18S(at_state_t *s, const char *t)
+{
+    /* V.250 6.4.4 - V.18 selection */ 
+    /*  mode:
+        0    Disables V.18 operation
+        1    V.18 operation, auto detect mode
+        2    V.18 operation, connect in 5-bit (Baudot) mode
+        3    V.18 operation, connect in DTMF mode
+        4    V.18 operation, connect in EDT mode
+        5    V.18 operation, connect in V.21 mode
+        6    V.18 operation, connect in V.23 mode
+        7    V.18 operation, connect in Bell 103-type mode
+
+        dflt_ans_mode:
+        0    Disables V.18 answer operation
+        1    No default specified (auto detect)
+        2    V.18 operation connect in 5-bit (Baudot) mode
+        3    V.18 operation connect in DTMF mode
+        4    V.18 operation connect in EDT mode
+
+        fbk_time_enable:
+        0    Disable
+        1    Enable
+
+        ans_msg_enable
+        0    Disable
+        1    Enable
+
+        probing_en
+        0    Disable probing
+        1    Enable probing
+        2    Initiate probing */
+    /* TODO: */
+    t += 6;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_PCW(at_state_t *s, const char *t)
+{
+    /* V.250 6.8.1 - Call waiting enable (V.92 DCE) */
+    /* TODO: */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_PIG(at_state_t *s, const char *t)
+{
+    /* V.250 6.8.5 - PCM upstream ignore */
+    /* TODO: */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_PMH(at_state_t *s, const char *t)
+{
+    /* V.250 6.8.2 - Modem on hold enable */
+    /* TODO: */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_PMHF(at_state_t *s, const char *t)
+{
+    /* V.250 6.8.6 - V.92 Modem on hold hook flash */
+    /* TODO: */
+    t += 5;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_PMHR(at_state_t *s, const char *t)
+{
+    /* V.250 6.8.4 - Initiate modem on hold */
+    /* TODO: */
+    t += 5;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_PMHT(at_state_t *s, const char *t)
+{
+    /* V.250 6.8.3 - Modem on hold timer */
+    /* TODO: */
+    t += 5;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_PQC(at_state_t *s, const char *t)
+{
+    /* V.250 6.8.7 - V.92 Phase 1 and Phase 2 Control */
+    /* TODO: */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_PSS(at_state_t *s, const char *t)
+{
+    /* V.250 6.8.8 - V.92 Use Short Sequence */
+    /* TODO: */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_SAC(at_state_t *s, const char *t)
+{
+    /* V.252 3.4 - Audio transmit configuration */
+    /* TODO: */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_SAM(at_state_t *s, const char *t)
+{
+    /* V.252 3.5 - Audio receive mode */
+    /* TODO: */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_SAR(at_state_t *s, const char *t)
+{
+    /* V.252 5.3 - Audio receive channel indication */
+    /* TODO: */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_SARR(at_state_t *s, const char *t)
+{
+    /* V.252 3.9 - Audio indication reporting */
+    /* TODO: */
+    t += 5;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_SAT(at_state_t *s, const char *t)
+{
+    /* V.252 5.4 - Audio transmit channel indication */
+    /* TODO: */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_SCRR(at_state_t *s, const char *t)
+{
+    /* V.252 3.11 - Capabilities indication reporting */
+    /* TODO: */
+    t += 5;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_SDC(at_state_t *s, const char *t)
+{
+    /* V.252 3.3 - Data configuration */
+    /* TODO: */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_SDI(at_state_t *s, const char *t)
+{
+    /* V.252 5.2 - Data channel identification */
+    /* TODO: */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_SDR(at_state_t *s, const char *t)
+{
+    /* V.252 3.8 - Data indication reporting */
+    /* TODO: */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_SRSC(at_state_t *s, const char *t)
+{
+    /* V.252 5.1.2 - Remote terminal simultaneous capability indication */
+    /* TODO: */
+    t += 5;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_STC(at_state_t *s, const char *t)
+{
+    /* V.252 3.1 - Terminal configuration */
+    /* TODO: */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_STH(at_state_t *s, const char *t)
+{
+    /* V.252 3.2 - Close logical channel */
+    /* TODO: */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_SVC(at_state_t *s, const char *t)
+{
+    /* V.252 3.6 - Video transmit configuration */
+    /* TODO: */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_SVM(at_state_t *s, const char *t)
+{
+    /* V.252 3.7 - Video receive mode */
+    /* TODO: */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_SVR(at_state_t *s, const char *t)
+{
+    /* V.252 5.5 - Video receive channel indication */
+    /* TODO: */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_SVRR(at_state_t *s, const char *t)
+{
+    /* V.252 3.10 - Video indication reporting */
+    /* TODO: */
+    t += 5;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_SVT(at_state_t *s, const char *t)
+{
+    /* V.252 5.6 - Video transmit channel indication */
+    /* TODO: */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_TADR(at_state_t *s, const char *t)
+{
+    /* V.250 6.7.2.9 - Local V.54 address */ 
+    /* TODO: */
+    t += 5;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_TAL(at_state_t *s, const char *t)
+{
+    /* V.250 6.7.2.15 - Local analogue loop */ 
+    /* Action
+        0   Disable analogue loop
+        1   Enable analogue loop
+       Band
+        0   Low frequency band
+        1   High frequency band */
+    /* TODO: */
+    t += 4;
+    if (!parse_2_out(s, &t, NULL, 1, NULL, 1, "+TAL:", "(0,1),(0,1)"))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_TALS(at_state_t *s, const char *t)
+{
+    /* V.250 6.7.2.6 - Analogue loop status */ 
+    /*  0   Inactive
+        1   V.24 circuit 141 invoked
+        2   Front panel invoked
+        3   Network management system invoked */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 3, "+TALS:", "(0-3)"))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_TDLS(at_state_t *s, const char *t)
+{
+    /* V.250 6.7.2.7 - Local digital loop status */ 
+    /*  0   Disabled
+        1   Enabled, inactive
+        2   Front panel invoked
+        3   Network management system invoked
+        4   Remote invoked */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 3, "+TDLS:", "(0-4)"))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_TE140(at_state_t *s, const char *t)
+{
+    /* V.250 6.7.2.1 - Enable ckt 140 */ 
+    /*  0   Disabled
+        1   Enabled */
+    /* TODO: */
+    t += 6;
+    if (!parse_out(s, &t, NULL, 1, "+TE140:", "(0,1)"))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_TE141(at_state_t *s, const char *t)
+{
+    /* V.250 6.7.2.2 - Enable ckt 141 */ 
+    /*  0   Response is disabled
+        1   Response is enabled */
+    /* TODO: */
+    t += 6;
+    if (!parse_out(s, &t, NULL, 1, "+TE141:", "(0,1)"))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_TEPAL(at_state_t *s, const char *t)
+{
+    /* V.250 6.7.2.5 - Enable front panel analogue loop */ 
+    /*  0   Disabled
+        1   Enabled */
+    /* TODO: */
+    t += 6;
+    if (!parse_out(s, &t, NULL, 1, "+TEPAL:", "(0,1)"))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_TEPDL(at_state_t *s, const char *t)
+{
+    /* V.250 6.7.2.4 - Enable front panel RDL */ 
+    /*  0   Disabled
+        1   Enabled */
+    /* TODO: */
+    t += 6;
+    if (!parse_out(s, &t, NULL, 1, "+TEPDL:", "(0,1)"))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_TERDL(at_state_t *s, const char *t)
+{
+    /* V.250 6.7.2.3 - Enable RDL from remote */ 
+    /*  0   Local DCE will ignore command from remote
+        1   Local DCE will obey command from remote */
+    /* TODO: */
+    t += 6;
+    if (!parse_out(s, &t, NULL, 1, "+TERDL:", "(0,1)"))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_TLDL(at_state_t *s, const char *t)
+{
+    /* V.250 6.7.2.13 - Local digital loop */
+    /*  0   Stop test
+        1   Start test */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+TLDL:", "(0,1)"))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_TMO(at_state_t *s, const char *t)
+{
+    /* V.250 6.9 - V.59 command */
+    /* TODO: */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_TMODE(at_state_t *s, const char *t)
+{
+    /* V.250 6.7.2.10 - Set V.54 mode */ 
+    /* TODO: */
+    t += 6;
+    if (!parse_out(s, &t, NULL, 1, "+TMODE:", "(0,1)"))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_TNUM(at_state_t *s, const char *t)
+{
+    /* V.250 6.7.2.12 - Errored bit and block counts */ 
+    /* TODO: */
+    t += 5;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_TRDL(at_state_t *s, const char *t)
+{
+    /* V.250 6.7.2.14 - Request remote digital loop */ 
+    /*  0   Stop RDL
+        1   Start RDL */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+TRDL:", "(0,1)"))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_TRDLS(at_state_t *s, const char *t)
+{
+    /* V.250 6.7.2.8 - Remote digital loop status */ 
+    /* TODO: */
+    t += 6;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_TRES(at_state_t *s, const char *t)
+{
+    /* V.250 6.7.2.17 - Self test result */ 
+    /*  0   No test
+        1   Pass
+        2   Fail */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, NULL, 1, "+TRES:", "(0-2)"))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_TSELF(at_state_t *s, const char *t)
+{
+    /* V.250 6.7.2.16 - Self test */ 
+    /*  0   Intrusive full test
+        1   Safe partial test */
+    /* TODO: */
+    t += 6;
+    if (!parse_out(s, &t, NULL, 1, "+TSELF:", "(0,1)"))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_TTER(at_state_t *s, const char *t)
+{
+    /* V.250 6.7.2.11 - Test error rate */ 
+    /* TODO: */
+    t += 5;
+    if (!parse_2_out(s, &t, NULL, 65535, NULL, 65535, "+TTER:", "(0-65535),(0-65535)"))
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_VAC(at_state_t *s, const char *t)
+{
+    /* V.252 4.1 - Set audio code */
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_VACR(at_state_t *s, const char *t)
+{
+    /* V.252 6.1 - Audio code report */
+    /* TODO: */
+    t += 5;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_VBT(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 C.2.2 - Buffer threshold setting */
+    /* TODO: */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_VCID(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 C.2.3 - Calling number ID presentation */
+    /* TODO: */
+    t += 5;
+    if (!parse_out(s, &t, &s->display_call_info, 1, NULL, "0,1"))
+         return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_VCIDR(at_state_t *s, const char *t)
+{
+    /* V.252 6.2 - Caller ID report */
+    /* TODO: */
+    t += 6;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_VDID(at_state_t *s, const char *t)
+{
+    /* V.253 9.2.4 - DID service */
+    /* TODO: */
+    t += 5;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_VDIDR(at_state_t *s, const char *t)
+{
+    /* V.252 6.2 - DID report */
+    /* TODO: */
+    t += 6;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_VDR(at_state_t *s, const char *t)
+{
+    /* V.253 10.3.1 - Distinctive ring (ring cadence reporting) */
+    /* TODO: */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_VDT(at_state_t *s, const char *t)
+{
+    /* V.253 10.3.2 - Control tone cadence reporting */
+    /* TODO: */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_VDX(at_state_t *s, const char *t)
+{
+    /* V.253 10.5.6 - Speakerphone duplex mode */
+    /* TODO: */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_VEM(at_state_t *s, const char *t)
+{
+    /* V.253 10.5.7 - Deliver event reports */
+    /* TODO: */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_VGM(at_state_t *s, const char *t)
+{
+    /* V.253 10.5.2 - Microphone gain */
+    /* TODO: */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_VGR(at_state_t *s, const char *t)
+{
+    /* V.253 10.2.1 - Receive gain selection */
+    /* TODO: */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_VGS(at_state_t *s, const char *t)
+{
+    /* V.253 10.5.3 - Speaker gain */
+    /* TODO: */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_VGT(at_state_t *s, const char *t)
+{
+    /* V.253 10.2.2 - Volume selection */
+    /* TODO: */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_VHC(at_state_t *s, const char *t)
+{
+    /* V.252 4.12 - Telephony port hook control */
+    /* TODO: */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_VIP(at_state_t *s, const char *t)
+{
+    /* V.253 10.1.1 - Initialize voice parameters */
+    /* TODO: */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_VIT(at_state_t *s, const char *t)
+{
+    /* V.253 10.2.3 - DTE/DCE inactivity timer */
+    /* TODO: */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_VLS(at_state_t *s, const char *t)
+{
+    /* V.253 10.2.4 - Analogue source/destination selection */
+    /* TODO: */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_VNH(at_state_t *s, const char *t)
+{
+    /* V.253 9.2.5 - Automatic hangup control */
+    /* TODO: */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_VPH(at_state_t *s, const char *t)
+{
+    /* V.252 4.11 - Phone hookswitch status */
+    /* TODO: */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_VPP(at_state_t *s, const char *t)
+{
+    /* V.253 10.4.2 - Voice packet protocol */
+    /* TODO: */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_VPR(at_state_t *s, const char *t)
+{
+    /* IS-101 10.4.3 - Select DTE/DCE interface rate */
+    /* TODO: */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_VRA(at_state_t *s, const char *t)
+{
+    /* V.253 10.2.5 - Ringing tone goes away timer */
+    /* TODO: */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_VRID(at_state_t *s, const char *t)
+{
+    int val;
+
+    /* Extension of V.253 +VCID, Calling number ID report/repeat */
+    t += 5;
+    val = 0;
+    if (!parse_out(s, &t, &val, 1, NULL, "0,1"))
+         return NULL;
+    if (val == 1)
+        at_display_call_info(s);
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_VRL(at_state_t *s, const char *t)
+{
+    /* V.253 10.1.2 - Ring local phone */
+    /* TODO: */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_VRN(at_state_t *s, const char *t)
+{
+    /* V.253 10.2.6 - Ringing tone never appeared timer */
+    /* TODO: */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_VRX(at_state_t *s, const char *t)
+{
+    /* V.253 10.1.3 - Voice receive state */
+    /* TODO: */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_VSD(at_state_t *s, const char *t)
+{
+    /* V.253 10.2.7 - Silence detection (QUIET and SILENCE) */
+    /* TODO: */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_VSID(at_state_t *s, const char *t)
+{
+    /* Extension of V.253 +VCID, Set calling number ID */
+    t += 5;
+    if (!parse_string_out(s, &t, &s->local_id, NULL))
+        return NULL;
+    if (at_modem_control(s, AT_MODEM_CONTROL_SETID, s->local_id) < 0)
+        return NULL;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_VSM(at_state_t *s, const char *t)
+{
+    /* V.253 10.2.8 - Compression method selection */
+    /* TODO: */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_VSP(at_state_t *s, const char *t)
+{
+    /* V.253 10.5.1 - Voice speakerphone state */
+    /* TODO: */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_VTA(at_state_t *s, const char *t)
+{
+    /* V.253 10.5.4 - Train acoustic echo-canceller */ 
+    /* TODO: */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_VTD(at_state_t *s, const char *t)
+{
+    /* V.253 10.2.9 - Beep tone duration timer */
+    /* TODO: */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_VTER(at_state_t *s, const char *t)
+{
+    /* V.252 6.4 - Simple telephony event report */
+    /* TODO: */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_VTH(at_state_t *s, const char *t)
+{
+    /* V.253 10.5.5 - Train line echo-canceller */ 
+    /* TODO: */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_VTR(at_state_t *s, const char *t)
+{
+    /* V.253 10.1.4 - Voice duplex state */
+    /* TODO: */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_VTS(at_state_t *s, const char *t)
+{
+    /* V.253 10.1.5 - DTMF and tone generation in voice */
+    /* TODO: */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_VTX(at_state_t *s, const char *t)
+{
+    /* V.253 10.1.6 - Transmit data state */
+    /* TODO: */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_VXT(at_state_t *s, const char *t)
+{
+    /* IS-101 10.1.5 - Translate voice data */
+    /* TODO: */
+    t += 4;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_W(at_state_t *s, const char *t)
+{
+    /* TIA-678 5.2.4.1 - Compliance indication */
+    /* TODO: */
+    t += 2;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_WBAG(at_state_t *s, const char *t)
+{
+    /* TIA-678 C.5.6 Bias Modem Audio Gain */
+    /* TODO: */
+    t += 5;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_WCDA(at_state_t *s, const char *t)
+{
+    /* TIA-678 B.3.2.5 Display Data Link Address */
+    /* TODO: */
+    t += 5;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_WCHG(at_state_t *s, const char *t)
+{
+    /* TIA-678 B.3.2.4 Display Battery Charging Status */
+    /* TODO: */
+    t += 5;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_WCID(at_state_t *s, const char *t)
+{
+    /* TIA-678 B.3.2.1 Display System ID (operator) */
+    /* TODO: */
+    t += 5;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_WCLK(at_state_t *s, const char *t)
+{
+    /* TIA-678 B.3.2.3 Lock/Unlock DCE */
+    /* TODO: */
+    t += 5;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_WCPN(at_state_t *s, const char *t)
+{
+    /* TIA-678 B.3.2.2 Set Personal Identification Number */
+    /* TODO: */
+    t += 5;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_WCXF(at_state_t *s, const char *t)
+{
+    /* TIA-678 B.3.2.6 Display Supported Annex B commands */
+    /* TODO: */
+    t += 5;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_WDAC(at_state_t *s, const char *t)
+{
+    /* TIA-678 C.5.1 Data over Analogue Cellular Command Query */
+    /* TODO: */
+    t += 5;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_WDIR(at_state_t *s, const char *t)
+{
+    /* TIA-678 C.5.8 Phone Number Directory Selection */
+    /* TODO: */
+    t += 5;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_WECR(at_state_t *s, const char *t)
+{
+    /* TIA-678 C.5.3 Enable Cellular Result Codes */
+    /* TODO: */
+    t += 5;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_WFON(at_state_t *s, const char *t)
+{
+    /* TIA-678 C.5.5 Phone Specification */
+    /* TODO: */
+    t += 5;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_WKPD(at_state_t *s, const char *t)
+{
+    /* TIA-678 C.5.7 Keypad Emulation */
+    /* TODO: */
+    t += 5;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_WPBA(at_state_t *s, const char *t)
+{
+    /* TIA-678 C.5.9 Phone Battery Query */
+    /* TODO: */
+    t += 5;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_WPTH(at_state_t *s, const char *t)
+{
+    /* TIA-678 C.5.10 Call Path */
+    /* TODO: */
+    t += 5;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_WRLK(at_state_t *s, const char *t)
+{
+    /* TIA-678 C.5.4 Roam Lockout */
+    /* TODO: */
+    t += 5;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_WS45(at_state_t *s, const char *t)
+{
+    /* TIA-678 5.2.4.2 DTE-side stack selection */
+    /* TODO: */
+    t += 5;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_WS46(at_state_t *s, const char *t)
+{
+    /* 3GPP TS 27.007 5.9 - PCCA STD-101 [17] select wireless network */
+    /* TODO: */
+    t += 5;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_WS50(at_state_t *s, const char *t)
+{
+    /* TIA-678 B.3.1.1 Normalized Signal Strength */
+    /* TODO: */
+    t += 5;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_WS51(at_state_t *s, const char *t)
+{
+    /* TIA-678 B.3.1.2 Carrier Detect Signal Threshold */
+    /* TODO: */
+    t += 5;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_WS52(at_state_t *s, const char *t)
+{
+    /* TIA-678 B.3.1.3 Normalized Battery Level */
+    /* TODO: */
+    t += 5;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_WS53(at_state_t *s, const char *t)
+{
+    /* TIA-678 B.3.1.4 Normalized Channel Quality */
+    /* TODO: */
+    t += 5;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_WS54(at_state_t *s, const char *t)
+{
+    /* TIA-678 B.3.1.5 Carrier Detect Channel Quality Threshold */
+    /* TODO: */
+    t += 5;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_WS57(at_state_t *s, const char *t)
+{
+    /* TIA-678 B.3.1.7 Antenna Preference */
+    /* TODO: */
+    t += 5;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_WS58(at_state_t *s, const char *t)
+{
+    /* TIA-678 B.3.1.8 Idle Time-out Value */
+    /* TODO: */
+    t += 5;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const char *at_cmd_plus_WSTL(at_state_t *s, const char *t)
+{
+    /* TIA-678 C.5.2 Call Session Time Limit */
+    /* TODO: */
+    t += 5;
+    return t;
+}
+/*- End of function --------------------------------------------------------*/
+
+/*
+    AT command group prefixes:
+
+    +A    Call control (network addressing) issues, common, PSTN, ISDN, Rec. X.25, switched digital
+    +C    Digital cellular extensions
+    +D    Data compression, Rec. V.42bis
+    +E    Error control, Rec. V.42
+    +F    Facsimile, Rec. T.30, etc.
+    +G    Generic issues such as identity and capabilities
+    +I    DTE-DCE interface issues, Rec. V.24, etc.
+    +M    Modulation, Rec. V.32bis, etc.
+    +S    Switched or simultaneous data types
+    +T    Test issues
+    +V    Voice extensions
+    +W    Wireless extensions
+*/
+
+#include "at_interpreter_dictionary.h"
+
+static int command_search(const char *u, int len, int *matched)
+{
+    int i;
+    int index;
+    int first;
+    int last;
+    int entry;
+    int ptr;
+
+    entry = 0;
+    /* Loop over the length of the string to search the trie... */
+    for (i = 0, ptr = 0;  ptr < COMMAND_TRIE_LEN;  i++)
+    {
+        /* The character in u we are processing... */
+        /* V.250 5.4.1 says upper and lower case are equivalent in commands */
+        index = (unsigned char) toupper(u[i]);
+        /* Is there a child node for this character? */
+        /* Note: First and last could have been packed into one uint16_t,
+           but space is not that critical, so the other packing is good
+           enough to make the table reasonable. */
+        first = command_trie[ptr++];
+        last = command_trie[ptr++];
+        entry = command_trie[ptr++];
+        if (index < first  ||  index > last)
+            break;
+        if ((ptr = command_trie[ptr + index - first]) == 0)
+            break;
+        ptr--;
+    }
+    *matched = i;
+    return entry;
+}
+/*- End of function --------------------------------------------------------*/
+
+int at_modem_control(at_state_t *s, int op, const char *num)
+{
+    switch (op)
+    {
+    case AT_MODEM_CONTROL_ANSWER:
+        break;
+    case AT_MODEM_CONTROL_CALL:
+        break;
+    case AT_MODEM_CONTROL_HANGUP:
+        break;
+    case AT_MODEM_CONTROL_OFFHOOK:
+        break;
+    case AT_MODEM_CONTROL_DTR:
+        break;
+    case AT_MODEM_CONTROL_RTS:
+        break;
+    case AT_MODEM_CONTROL_CTS:
+        break;
+    case AT_MODEM_CONTROL_CAR:
+        break;
+    case AT_MODEM_CONTROL_RNG:
+        break;
+    case AT_MODEM_CONTROL_DSR:
+        break;
+    case AT_MODEM_CONTROL_RESTART:
+        break;
+    default:
+        break;
+    }
+    /*endswitch*/
+    return s->modem_control_handler(s, s->modem_control_user_data, op, num);
+}
+/*- End of function --------------------------------------------------------*/
+
+void at_interpreter(at_state_t *s, const char *cmd, int len)
+{
+    int i;
+    int c;
+    int entry;
+    int matched;
+    const char *t;
+
+    if (s->p.echo)
+        s->at_tx_handler(s, s->at_tx_user_data, (uint8_t *) cmd, len);
+
+    for (i = 0;  i < len;  i++)
+    {
+        /* The spec says the top bit should be ignored */
+        c = *cmd++ & 0x7F;
+        /* Handle incoming character */
+        if (s->line_ptr < 2)
+        {
+            /* Look for the initial "at", "AT", "a/" or "A/", and ignore anything before it */
+            /* V.250 5.2.1 only shows "at" and "AT" as command prefixes. "At" and "aT" are
+               not specified, despite 5.4.1 saying upper and lower case are equivalent in
+               commands. Let's be tolerant and accept them. */
+            if (tolower(c) == 'a')
+            {
+                s->line_ptr = 0;
+                s->line[s->line_ptr++] = (char) toupper(c);
+            }
+            else if (s->line_ptr == 1)
+            {
+                if (tolower(c) == 't')
+                {
+                    /* We have an "AT" command */
+                    s->line[s->line_ptr++] = (char) toupper(c);
+                }
+                else if (c == '/')
+                {
+                    /* We have an "A/" command */
+                    /* TODO: implement "A/" command repeat */
+                    s->line[s->line_ptr++] = (char) c;
+                }
+                else
+                {
+                    s->line_ptr = 0;
+                }
+            }
+        }
+        else
+        {
+            /* We are beyond the initial AT */
+            if (c >= 0x20  &&  c <= 0x7E)
+            {
+                /* Add a new char */
+                if (s->line_ptr < (int) (sizeof(s->line) - 1))
+                    s->line[s->line_ptr++] = (char) toupper(c);
+            }
+            else if (c == s->p.s_regs[3])
+            {
+                /* End of command line. Do line validation */
+                s->line[s->line_ptr] = '\0';
+                if (s->line_ptr > 2)
+                {
+                    /* The spec says the commands within a command line are executed in order, until
+                       an error is found, or the end of the command line is reached. */
+                    t = s->line + 2;
+                    while (t  &&  *t)
+                    {
+                        if ((entry = command_search(t, 15, &matched)) <= 0)
+                            break;
+                        if ((t = at_commands[entry - 1](s, t)) == NULL)
+                            break;
+                        if (t == (const char *) -1)
+                            break;
+                    }
+                    if (t != (const char *) -1)
+                    {
+                        if (t == NULL)
+                            at_put_response_code(s, AT_RESPONSE_CODE_ERROR);
+                        else
+                            at_put_response_code(s, AT_RESPONSE_CODE_OK);
+                    }
+                }
+                else if (s->line_ptr == 2)
+                {
+                    /* It's just an empty "AT" command, return OK. */
+                    at_put_response_code(s, AT_RESPONSE_CODE_OK);
+                }
+                s->line_ptr = 0;
+            }
+            else if (c == s->p.s_regs[5])
+            {
+                /* Command line editing character (backspace) */
+                if (s->line_ptr > 0)
+                    s->line_ptr--;
+            }
+            else
+            {
+                /* The spec says control characters, other than those
+                   explicitly handled, should be ignored, and so this
+                   invalid character causes everything buffered
+                   before it to also be ignored. */
+                s->line_ptr = 0;
+            }
+        }
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+void at_set_class1_handler(at_state_t *s, at_class1_handler_t handler, void *user_data)
+{
+    s->class1_handler = handler;
+    s->class1_user_data = user_data;
+}
+/*- End of function --------------------------------------------------------*/
+
+at_state_t *at_init(at_state_t *s,
+                    at_tx_handler_t *at_tx_handler,
+                    void *at_tx_user_data,
+                    at_modem_control_handler_t *modem_control_handler,
+                    void *modem_control_user_data)
+{
+    if (s == NULL)
+    {
+        if ((s = (at_state_t *) malloc(sizeof(*s))) == NULL)
+            return NULL;
+    }
+    memset(s, '\0', sizeof(*s));
+    s->modem_control_handler = modem_control_handler;
+    s->modem_control_user_data = modem_control_user_data;
+    s->at_tx_handler = at_tx_handler;
+    s->at_tx_user_data = at_tx_user_data;
+    s->call_id = NULL;
+    s->local_id = NULL;
+    s->display_call_info = 0;
+    at_set_at_rx_mode(s, AT_MODE_ONHOOK_COMMAND);
+    s->p = profiles[0];
+    return s;
+}
+/*- End of function --------------------------------------------------------*/
+
+int at_free(at_state_t *s)
+{
+    at_reset_call_info(s);
+    if (s->local_id)
+        free(s->local_id);
+    free(s);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/awgn.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/awgn.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,164 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * awgn.c - An additive Gaussian white noise generator
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2001 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: awgn.c,v 1.16 2008/07/02 14:48:25 steveu Exp $
+ */
+
+/*! \file */
+
+/* This code is based on some demonstration code in a research
+   paper somewhere. I can't track down where I got the original from,
+   so that due recognition can be given. The original had no explicit
+   copyright notice, and I hope nobody objects to its use here.
+   
+   Having a reasonable Gaussian noise generator is pretty important for
+   telephony testing (in fact, pretty much any DSP testing), and this
+   one seems to have served me OK. Since the generation of Gaussian
+   noise is only for test purposes, and not a core system component,
+   I don't intend to worry excessively about copyright issues, unless
+   someone worries me.
+        
+   The non-core nature of this code also explains why it is unlikely
+   to ever be optimised. */
+
+#if defined(HAVE_CONFIG_H)
+#include <config.h>
+#endif
+
+#include <stdlib.h>
+#include <inttypes.h>
+#include "floating_fudge.h"
+#if defined(HAVE_TGMATH_H)
+#include <tgmath.h>
+#endif
+#if defined(HAVE_MATH_H)
+#include <math.h>
+#endif
+
+#include "spandsp/telephony.h"
+#include "spandsp/dc_restore.h"
+#include "spandsp/awgn.h"
+
+/* Gaussian noise generator constants */
+#define M1 259200
+#define IA1 7141
+#define IC1 54773
+#define RM1 (1.0/M1)
+#define M2 134456
+#define IA2 8121
+#define IC2 28411
+#define RM2 (1.0/M2)
+#define M3 243000
+#define IA3 4561
+#define IC3 51349
+
+static double ran1(awgn_state_t *s)
+{
+    double temp;
+    int j;
+
+    s->ix1 = (IA1*s->ix1 + IC1)%M1;
+    s->ix2 = (IA2*s->ix2 + IC2)%M2;
+    s->ix3 = (IA3*s->ix3 + IC3)%M3;
+    j = 1 + ((97*s->ix3)/M3);
+    if (j > 97  ||  j < 1)
+    {
+        /* Error */
+        return -1;
+    }
+    temp = s->r[j];
+    s->r[j] = (s->ix1 + s->ix2*RM2)*RM1;
+    return temp;
+}
+/*- End of function --------------------------------------------------------*/
+
+awgn_state_t *awgn_init_dbm0(awgn_state_t *s, int idum, float level)
+{
+    return awgn_init_dbov(s, idum, level - DBM0_MAX_POWER);
+}
+/*- End of function --------------------------------------------------------*/
+
+awgn_state_t *awgn_init_dbov(awgn_state_t *s, int idum, float level)
+{
+    int j;
+
+    if (s == NULL)
+    {
+        if ((s = (awgn_state_t *) malloc(sizeof(*s))) == NULL)
+            return  NULL;
+    }
+    if (idum < 0)
+        idum = -idum;
+
+    s->rms = pow(10.0, level/20.0)*32768.0;
+
+    s->ix1 = (IC1 + idum)%M1;
+    s->ix1 = (IA1*s->ix1 + IC1)%M1;
+    s->ix2 = s->ix1%M2;
+    s->ix1 = (IA1*s->ix1 + IC1)%M1;
+    s->ix3 = s->ix1%M3;
+    s->r[0] = 0.0;
+    for (j = 1;  j <= 97;  j++)
+    {
+        s->ix1 = (IA1*s->ix1 + IC1)%M1;
+        s->ix2 = (IA2*s->ix2 + IC2)%M2;
+        s->r[j] = (s->ix1 + s->ix2*RM2)*RM1;
+    }
+    s->gset = 0.0;
+    s->iset = 0;
+    return s;
+}
+/*- End of function --------------------------------------------------------*/
+
+int16_t awgn(awgn_state_t *s)
+{
+    double fac;
+    double r;
+    double v1;
+    double v2;
+    double amp;
+
+    if (s->iset == 0)
+    {
+        do
+        {
+            v1 = 2.0*ran1(s) - 1.0;
+            v2 = 2.0*ran1(s) - 1.0;
+            r = v1*v1 + v2*v2;
+        }
+        while (r >= 1.0);
+        fac = sqrt(-2.0*log(r)/r);
+        s->gset = v1*fac;
+        s->iset = 1;
+        amp = v2*fac*s->rms;
+    }
+    else
+    {
+        s->iset = 0;
+        amp = s->gset*s->rms;
+    }
+    return fsaturate(amp);
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/bell_r2_mf.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/bell_r2_mf.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,833 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * bell_r2_mf.c - Bell MF and MFC/R2 tone generation and detection.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2001 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: bell_r2_mf.c,v 1.29 2008/07/02 14:48:25 steveu Exp $
+ */
+
+/*! \file */
+
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <inttypes.h>
+#include <string.h>
+#include <time.h>
+#include <fcntl.h>
+#include "floating_fudge.h"
+#if defined(HAVE_TGMATH_H)
+#include <tgmath.h>
+#endif
+#if defined(HAVE_MATH_H)
+#include <math.h>
+#endif
+
+#include "spandsp/telephony.h"
+#include "spandsp/queue.h"
+#include "spandsp/dc_restore.h"
+#include "spandsp/complex.h"
+#include "spandsp/dds.h"
+#include "spandsp/tone_detect.h"
+#include "spandsp/tone_generate.h"
+#include "spandsp/super_tone_rx.h"
+#include "spandsp/dtmf.h"
+#include "spandsp/bell_r2_mf.h"
+
+#if !defined(M_PI)
+/* C99 systems may not define M_PI */
+#define M_PI 3.14159265358979323846264338327
+#endif
+
+#define ms_to_samples(t)            (((t)*SAMPLE_RATE)/1000)
+
+typedef struct
+{
+    int         f1;         /* First freq */
+    int         f2;         /* Second freq */
+    int8_t      level1;     /* Level of the first freq (dB) */
+    int8_t      level2;     /* Level of the second freq (dB) */
+    uint8_t     on_time;    /* Tone on time (ms) */
+    uint8_t     off_time;   /* Minimum post tone silence (ms) */
+} mf_digit_tones_t;
+
+int bell_mf_gen_inited = FALSE;
+tone_gen_descriptor_t bell_mf_digit_tones[15];
+
+int r2_mf_gen_inited = FALSE;
+tone_gen_descriptor_t r2_mf_fwd_digit_tones[15];
+tone_gen_descriptor_t r2_mf_back_digit_tones[15];
+
+#if 0
+tone_gen_descriptor_t socotel_mf_digit_tones[18];
+#endif
+
+/* Bell R1 tone generation specs.
+ *  Power: -7dBm +- 1dB
+ *  Frequency: within +-1.5%
+ *  Mismatch between the start time of a pair of tones: <=6ms.
+ *  Mismatch between the end time of a pair of tones: <=6ms.
+ *  Tone duration: 68+-7ms, except KP which is 100+-7ms.
+ *  Inter-tone gap: 68+-7ms.
+ */
+static const mf_digit_tones_t bell_mf_tones[] =
+{
+    { 700,  900, -7, -7,  68, 68},
+    { 700, 1100, -7, -7,  68, 68},
+    { 900, 1100, -7, -7,  68, 68},
+    { 700, 1300, -7, -7,  68, 68},
+    { 900, 1300, -7, -7,  68, 68},
+    {1100, 1300, -7, -7,  68, 68},
+    { 700, 1500, -7, -7,  68, 68},
+    { 900, 1500, -7, -7,  68, 68},
+    {1100, 1500, -7, -7,  68, 68},
+    {1300, 1500, -7, -7,  68, 68},
+    { 700, 1700, -7, -7,  68, 68}, /* ST''' - use 'C' */
+    { 900, 1700, -7, -7,  68, 68}, /* ST'   - use 'A' */
+    {1100, 1700, -7, -7, 100, 68}, /* KP    - use '*' */
+    {1300, 1700, -7, -7,  68, 68}, /* ST''  - use 'B' */
+    {1500, 1700, -7, -7,  68, 68}, /* ST    - use '#' */
+    {0, 0, 0, 0, 0, 0}
+};
+
+/* The order of the digits here must match the list above */
+static const char bell_mf_tone_codes[] = "1234567890CA*B#";
+
+/* R2 tone generation specs.
+ *  Power: -11.5dBm +- 1dB
+ *  Frequency: within +-4Hz
+ *  Mismatch between the start time of a pair of tones: <=1ms.
+ *  Mismatch between the end time of a pair of tones: <=1ms.
+ */
+static const mf_digit_tones_t r2_mf_fwd_tones[] =
+{
+    {1380, 1500, -11, -11, 1, 0},
+    {1380, 1620, -11, -11, 1, 0},
+    {1500, 1620, -11, -11, 1, 0},
+    {1380, 1740, -11, -11, 1, 0},
+    {1500, 1740, -11, -11, 1, 0},
+    {1620, 1740, -11, -11, 1, 0},
+    {1380, 1860, -11, -11, 1, 0},
+    {1500, 1860, -11, -11, 1, 0},
+    {1620, 1860, -11, -11, 1, 0},
+    {1740, 1860, -11, -11, 1, 0},
+    {1380, 1980, -11, -11, 1, 0},
+    {1500, 1980, -11, -11, 1, 0},
+    {1620, 1980, -11, -11, 1, 0},
+    {1740, 1980, -11, -11, 1, 0},
+    {1860, 1980, -11, -11, 1, 0},
+    {0, 0, 0, 0, 0, 0}
+};
+
+static const mf_digit_tones_t r2_mf_back_tones[] =
+{
+    {1140, 1020, -11, -11, 1, 0},
+    {1140,  900, -11, -11, 1, 0},
+    {1020,  900, -11, -11, 1, 0},
+    {1140,  780, -11, -11, 1, 0},
+    {1020,  780, -11, -11, 1, 0},
+    { 900,  780, -11, -11, 1, 0},
+    {1140,  660, -11, -11, 1, 0},
+    {1020,  660, -11, -11, 1, 0},
+    { 900,  660, -11, -11, 1, 0},
+    { 780,  660, -11, -11, 1, 0},
+    {1140,  540, -11, -11, 1, 0},
+    {1020,  540, -11, -11, 1, 0},
+    { 900,  540, -11, -11, 1, 0},
+    { 780,  540, -11, -11, 1, 0},
+    { 660,  540, -11, -11, 1, 0},
+    {0, 0, 0, 0, 0, 0}
+};
+
+/* The order of the digits here must match the lists above */
+static const char r2_mf_tone_codes[] = "1234567890BCDEF";
+
+#if 0
+static const mf_digit_tones_t socotel_tones[] =
+{
+    { 700,  900, -11, -11, 1, 0},
+    { 700, 1100, -11, -11, 1, 0},
+    { 900, 1100, -11, -11, 1, 0},
+    { 700, 1300, -11, -11, 1, 0},
+    { 900, 1300, -11, -11, 1, 0},
+    {1100, 1300, -11, -11, 1, 0},
+    { 700, 1500, -11, -11, 1, 0},
+    { 900, 1500, -11, -11, 1, 0},
+    {1100, 1500, -11, -11, 1, 0},
+    {1300, 1500, -11, -11, 1, 0},
+    {1500, 1700, -11, -11, 1, 0},
+    { 700, 1700, -11, -11, 1, 0},
+    { 900, 1700, -11, -11, 1, 0},
+    {1300, 1700, -11, -11, 1, 0},
+    {1100, 1700, -11, -11, 1, 0},
+    {1700,    0, -11, -11, 1, 0},   /* Use 'F' */
+    {1900,    0, -11, -11, 1, 0},   /* Use 'G' */
+    {0, 0, 0, 0, 0, 0}
+};
+
+/* The order of the digits here must match the list above */
+static char socotel_mf_tone_codes[] = "1234567890ABCDEFG";
+#endif
+
+#if defined(SPANDSP_USE_FIXED_POINT)
+#define BELL_MF_THRESHOLD           204089              /* -30.5dBm0 */
+#define BELL_MF_TWIST               3.981f              /* 6dB */
+#define BELL_MF_RELATIVE_PEAK       12.589f             /* 11dB */
+#define BELL_MF_SAMPLES_PER_BLOCK   120
+
+#define R2_MF_THRESHOLD             62974               /* -36.5dBm0 */
+#define R2_MF_TWIST                 5.012f              /* 7dB */
+#define R2_MF_RELATIVE_PEAK         12.589f             /* 11dB */
+#define R2_MF_SAMPLES_PER_BLOCK     133
+#else
+#define BELL_MF_THRESHOLD           3343803100.0f       /* -30.5dBm0 [((120.0*32768.0/1.4142)*10^((-30.5 - DBM0_MAX_SINE_POWER)/20.0))^2 => 3343803100.0] */
+#define BELL_MF_TWIST               3.981f              /* 6dB [10^(6/10) => 3.981] */
+#define BELL_MF_RELATIVE_PEAK       12.589f             /* 11dB */
+#define BELL_MF_SAMPLES_PER_BLOCK   120
+
+#define R2_MF_THRESHOLD             1031766650.0f       /* -36.5dBm0 [((133.0*32768.0/1.4142)*10^((-36.5 - DBM0_MAX_SINE_POWER)/20.0))^2 => 1031766650.0] */
+#define R2_MF_TWIST                 5.012f              /* 7dB */
+#define R2_MF_RELATIVE_PEAK         12.589f             /* 11dB */
+#define R2_MF_SAMPLES_PER_BLOCK     133
+#endif
+
+static goertzel_descriptor_t bell_mf_detect_desc[6];
+
+static goertzel_descriptor_t mf_fwd_detect_desc[6];
+static goertzel_descriptor_t mf_back_detect_desc[6];
+
+static const int bell_mf_frequencies[] =
+{
+     700,  900, 1100, 1300, 1500, 1700
+};
+
+/* Use the follow characters for the Bell MF special signals:
+    KP    - use '*'
+    ST    - use '#'
+    ST'   - use 'A'
+    ST''  - use 'B'
+    ST''' - use 'C' */
+static const char bell_mf_positions[] = "1247C-358A--69*---0B----#";
+
+static const int r2_mf_fwd_frequencies[] =
+{
+    1380, 1500, 1620, 1740, 1860, 1980
+};
+
+static const int r2_mf_back_frequencies[] =
+{
+    1140, 1020,  900,  780,  660,  540
+};
+
+/* Use codes '1' to 'F' for the R2 signals 1 to 15, except for signal 'A'.
+   Use '0' for this, so the codes match the digits 0-9. */
+static const char r2_mf_positions[] = "1247B-358C--69D---0E----F";
+
+static void bell_mf_gen_init(void)
+{
+    int i;
+    const mf_digit_tones_t *tones;
+
+    if (bell_mf_gen_inited)
+        return;
+    i = 0;
+    tones = bell_mf_tones;
+    while (tones->on_time)
+    {
+        /* Note: The duration of KP is longer than the other signals. */
+        make_tone_gen_descriptor(&bell_mf_digit_tones[i++],
+                                 tones->f1,
+                                 tones->level1,
+                                 tones->f2,
+                                 tones->level2,
+                                 tones->on_time,
+                                 tones->off_time,
+                                 0,
+                                 0,
+                                 FALSE);
+        tones++;
+    }
+    bell_mf_gen_inited = TRUE;
+}
+/*- End of function --------------------------------------------------------*/
+
+int bell_mf_tx(bell_mf_tx_state_t *s, int16_t amp[], int max_samples)
+{
+    int len;
+    const char *cp;
+    int digit;
+
+    len = 0;
+    if (s->tones.current_section >= 0)
+    {
+        /* Deal with the fragment left over from last time */
+        len = tone_gen(&(s->tones), amp, max_samples);
+    }
+    while (len < max_samples  &&  (digit = queue_read_byte(&s->queue.queue)) >= 0)
+    {
+        /* Step to the next digit */
+        if ((cp = strchr(bell_mf_tone_codes, digit)) == NULL)
+            continue;
+        tone_gen_init(&(s->tones), &bell_mf_digit_tones[cp - bell_mf_tone_codes]);
+        len += tone_gen(&(s->tones), amp + len, max_samples - len);
+    }
+    return len;
+}
+/*- End of function --------------------------------------------------------*/
+
+size_t bell_mf_tx_put(bell_mf_tx_state_t *s, const char *digits, int len)
+{
+    size_t space;
+
+    /* This returns the number of characters that would not fit in the buffer.
+       The buffer will only be loaded if the whole string of digits will fit,
+       in which case zero is returned. */
+    if (len < 0)
+    {
+        if ((len = strlen(digits)) == 0)
+            return 0;
+    }
+    if ((space = queue_free_space(&s->queue.queue)) < len)
+        return len - space;
+    if (queue_write(&s->queue.queue, (const uint8_t *) digits, len) >= 0)
+        return 0;
+    return -1;
+}
+/*- End of function --------------------------------------------------------*/
+
+bell_mf_tx_state_t *bell_mf_tx_init(bell_mf_tx_state_t *s)
+{
+    if (s == NULL)
+    {
+        if ((s = (bell_mf_tx_state_t *) malloc(sizeof(*s))) == NULL)
+            return NULL;
+    }
+    memset(s, 0, sizeof(*s));
+
+    if (!bell_mf_gen_inited)
+        bell_mf_gen_init();
+    tone_gen_init(&(s->tones), &bell_mf_digit_tones[0]);
+    s->current_sample = 0;
+    queue_init(&s->queue.queue, MAX_BELL_MF_DIGITS, QUEUE_READ_ATOMIC | QUEUE_WRITE_ATOMIC);
+    s->tones.current_section = -1;
+    return s;
+}
+/*- End of function --------------------------------------------------------*/
+
+int bell_mf_tx_free(bell_mf_tx_state_t *s)
+{
+    free(s);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int r2_mf_tx(r2_mf_tx_state_t *s, int16_t amp[], int samples)
+{
+    int len;
+
+    if (s->digit == 0)
+    {
+        len = samples;
+        memset(amp, 0, len*sizeof(int16_t));
+    }
+    else
+    {
+        len = tone_gen(&s->tone, amp, samples);
+    }
+    return len;
+}
+/*- End of function --------------------------------------------------------*/
+
+int r2_mf_tx_put(r2_mf_tx_state_t *s, char digit)
+{
+    char *cp;
+
+    if (digit  &&  (cp = strchr(r2_mf_tone_codes, digit)))
+    {
+        if (s->fwd)
+            tone_gen_init(&s->tone, &r2_mf_fwd_digit_tones[cp - r2_mf_tone_codes]);
+        else
+            tone_gen_init(&s->tone, &r2_mf_back_digit_tones[cp - r2_mf_tone_codes]);
+        s->digit = digit;
+    }
+    else
+    {
+        s->digit = 0;
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+r2_mf_tx_state_t *r2_mf_tx_init(r2_mf_tx_state_t *s, int fwd)
+{
+    int i;
+    const mf_digit_tones_t *tones;
+
+    if (s == NULL)
+    {
+        if ((s = (r2_mf_tx_state_t *) malloc(sizeof(*s))) == NULL)
+            return NULL;
+    }
+    memset(s, 0, sizeof(*s));
+
+    if (!r2_mf_gen_inited)
+    {
+        i = 0;
+        tones = r2_mf_fwd_tones;
+        while (tones->on_time)
+        {
+            make_tone_gen_descriptor(&r2_mf_fwd_digit_tones[i++],
+                                     tones->f1,
+                                     tones->level1,
+                                     tones->f2,
+                                     tones->level2,
+                                     tones->on_time,
+                                     tones->off_time,
+                                     0,
+                                     0,
+                                     (tones->off_time == 0));
+            tones++;
+        }
+        i = 0;
+        tones = r2_mf_back_tones;
+        while (tones->on_time)
+        {
+            make_tone_gen_descriptor(&r2_mf_back_digit_tones[i++],
+                                     tones->f1,
+                                     tones->level1,
+                                     tones->f2,
+                                     tones->level2,
+                                     tones->on_time,
+                                     tones->off_time,
+                                     0,
+                                     0,
+                                     (tones->off_time == 0));
+            tones++;
+        }
+        r2_mf_gen_inited = TRUE;
+    }
+    s->fwd = fwd;
+    return s;
+}
+/*- End of function --------------------------------------------------------*/
+
+int r2_mf_tx_free(r2_mf_tx_state_t *s)
+{
+    free(s);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int bell_mf_rx(bell_mf_rx_state_t *s, const int16_t amp[], int samples)
+{
+#if defined(SPANDSP_USE_FIXED_POINT)
+    int32_t energy[6];
+    int16_t xamp;
+#else
+    float energy[6];
+    float xamp;
+#endif
+    int i;
+    int j;
+    int sample;
+    int best;
+    int second_best;
+    int limit;
+    uint8_t hit;
+
+    hit = 0;
+    for (sample = 0;  sample < samples;  sample = limit)
+    {
+        if ((samples - sample) >= (BELL_MF_SAMPLES_PER_BLOCK - s->current_sample))
+            limit = sample + (BELL_MF_SAMPLES_PER_BLOCK - s->current_sample);
+        else
+            limit = samples;
+        for (j = sample;  j < limit;  j++)
+        {
+            xamp = goertzel_preadjust_amp(amp[j]);
+            goertzel_samplex(&s->out[0], xamp);
+            goertzel_samplex(&s->out[1], xamp);
+            goertzel_samplex(&s->out[2], xamp);
+            goertzel_samplex(&s->out[3], xamp);
+            goertzel_samplex(&s->out[4], xamp);
+            goertzel_samplex(&s->out[5], xamp);
+        }
+        s->current_sample += (limit - sample);
+        if (s->current_sample < BELL_MF_SAMPLES_PER_BLOCK)
+            continue;
+
+        /* We are at the end of an MF detection block */
+        /* Find the two highest energies. The spec says to look for
+           two tones and two tones only. Taking this literally -ie
+           only two tones pass the minimum threshold - doesn't work
+           well. The sinc function mess, due to rectangular windowing
+           ensure that! Find the two highest energies and ensure they
+           are considerably stronger than any of the others. */
+        energy[0] = goertzel_result(&s->out[0]);
+        energy[1] = goertzel_result(&s->out[1]);
+        if (energy[0] > energy[1])
+        {
+            best = 0;
+            second_best = 1;
+        }
+        else
+        {
+            best = 1;
+            second_best = 0;
+        }
+        for (i = 2;  i < 6;  i++)
+        {
+            energy[i] = goertzel_result(&s->out[i]);
+            if (energy[i] >= energy[best])
+            {
+                second_best = best;
+                best = i;
+            }
+            else if (energy[i] >= energy[second_best])
+            {
+                second_best = i;
+            }
+        }
+        /* Basic signal level and twist tests */
+        hit = 0;
+        if (energy[best] >= BELL_MF_THRESHOLD
+            &&
+            energy[second_best] >= BELL_MF_THRESHOLD
+            &&
+            energy[best] < energy[second_best]*BELL_MF_TWIST
+            &&
+            energy[best]*BELL_MF_TWIST > energy[second_best])
+        {
+            /* Relative peak test */
+            hit = 'X';
+            for (i = 0;  i < 6;  i++)
+            {
+                if (i != best  &&  i != second_best)
+                {
+                    if (energy[i]*BELL_MF_RELATIVE_PEAK >= energy[second_best])
+                    {
+                        /* The best two are not clearly the best */
+                        hit = 0;
+                        break;
+                    }
+                }
+            }
+        }
+        if (hit)
+        {
+            /* Get the values into ascending order */
+            if (second_best < best)
+            {
+                i = best;
+                best = second_best;
+                second_best = i;
+            }
+            best = best*5 + second_best - 1;
+            hit = bell_mf_positions[best];
+            /* Look for two successive similar results */
+            /* The logic in the next test is:
+               For KP we need 4 successive identical clean detects, with
+               two blocks of something different preceeding it. For anything
+               else we need two successive identical clean detects, with
+               two blocks of something different preceeding it. */
+            if (hit == s->hits[4]
+                &&
+                hit == s->hits[3]
+                &&
+                   ((hit != '*'  &&  hit != s->hits[2]  &&  hit != s->hits[1])
+                    ||
+                    (hit == '*'  &&  hit == s->hits[2]  &&  hit != s->hits[1]  &&  hit != s->hits[0])))
+            {
+                if (s->current_digits < MAX_BELL_MF_DIGITS)
+                {
+                    s->digits[s->current_digits++] = (char) hit;
+                    s->digits[s->current_digits] = '\0';
+                    if (s->digits_callback)
+                    {
+                        s->digits_callback(s->digits_callback_data, s->digits, s->current_digits);
+                        s->current_digits = 0;
+                    }
+                }
+                else
+                {
+                    s->lost_digits++;
+                }
+            }
+        }
+        s->hits[0] = s->hits[1];
+        s->hits[1] = s->hits[2];
+        s->hits[2] = s->hits[3];
+        s->hits[3] = s->hits[4];
+        s->hits[4] = hit;
+        s->current_sample = 0;
+    }
+    if (s->current_digits  &&  s->digits_callback)
+    {
+        s->digits_callback(s->digits_callback_data, s->digits, s->current_digits);
+        s->digits[0] = '\0';
+        s->current_digits = 0;
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+size_t bell_mf_rx_get(bell_mf_rx_state_t *s, char *buf, int max)
+{
+    if (max > s->current_digits)
+        max = s->current_digits;
+    if (max > 0)
+    {
+        memcpy(buf, s->digits, max);
+        memmove(s->digits, s->digits + max, s->current_digits - max);
+        s->current_digits -= max;
+    }
+    buf[max] = '\0';
+    return  max;
+}
+/*- End of function --------------------------------------------------------*/
+
+bell_mf_rx_state_t *bell_mf_rx_init(bell_mf_rx_state_t *s,
+                                    digits_rx_callback_t callback,
+                                    void *user_data)
+{
+    int i;
+    static int initialised = FALSE;
+
+    if (s == NULL)
+    {
+        if ((s = (bell_mf_rx_state_t *) malloc(sizeof(*s))) == NULL)
+            return NULL;
+    }
+    memset(s, 0, sizeof(*s));
+
+    if (!initialised)
+    {
+        for (i = 0;  i < 6;  i++)
+            make_goertzel_descriptor(&bell_mf_detect_desc[i], bell_mf_frequencies[i], BELL_MF_SAMPLES_PER_BLOCK);
+        initialised = TRUE;
+    }
+    s->digits_callback = callback;
+    s->digits_callback_data = user_data;
+
+    s->hits[0] = 
+    s->hits[1] =
+    s->hits[2] =
+    s->hits[3] = 
+    s->hits[4] = 0;
+
+    for (i = 0;  i < 6;  i++)
+        goertzel_init(&s->out[i], &bell_mf_detect_desc[i]);
+    s->current_sample = 0;
+    s->lost_digits = 0;
+    s->current_digits = 0;
+    s->digits[0] = '\0';
+    return s;
+}
+/*- End of function --------------------------------------------------------*/
+
+int bell_mf_rx_free(bell_mf_rx_state_t *s)
+{
+    free(s);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int r2_mf_rx(r2_mf_rx_state_t *s, const int16_t amp[], int samples)
+{
+#if defined(SPANDSP_USE_FIXED_POINT)
+    int32_t energy[6];
+    int16_t xamp;
+#else
+    float energy[6];
+    float xamp;
+#endif
+    int i;
+    int j;
+    int sample;
+    int best;
+    int second_best;
+    int hit;
+    int hit_digit;
+    int limit;
+
+    hit = 0;
+    hit_digit = 0;
+    for (sample = 0;  sample < samples;  sample = limit)
+    {
+        if ((samples - sample) >= (R2_MF_SAMPLES_PER_BLOCK - s->current_sample))
+            limit = sample + (R2_MF_SAMPLES_PER_BLOCK - s->current_sample);
+        else
+            limit = samples;
+        for (j = sample;  j < limit;  j++)
+        {
+            xamp = goertzel_preadjust_amp(amp[j]);
+            goertzel_samplex(&s->out[0], xamp);
+            goertzel_samplex(&s->out[1], xamp);
+            goertzel_samplex(&s->out[2], xamp);
+            goertzel_samplex(&s->out[3], xamp);
+            goertzel_samplex(&s->out[4], xamp);
+            goertzel_samplex(&s->out[5], xamp);
+        }
+        s->current_sample += (limit - sample);
+        if (s->current_sample < R2_MF_SAMPLES_PER_BLOCK)
+            continue;
+
+        /* We are at the end of an MF detection block */
+        /* Find the two highest energies */
+        energy[0] = goertzel_result(&s->out[0]);
+        energy[1] = goertzel_result(&s->out[1]);
+        if (energy[0] > energy[1])
+        {
+            best = 0;
+            second_best = 1;
+        }
+        else
+        {
+            best = 1;
+            second_best = 0;
+        }
+        
+        for (i = 2;  i < 6;  i++)
+        {
+            energy[i] = goertzel_result(&s->out[i]);
+            if (energy[i] >= energy[best])
+            {
+                second_best = best;
+                best = i;
+            }
+            else if (energy[i] >= energy[second_best])
+            {
+                second_best = i;
+            }
+        }
+        /* Basic signal level and twist tests */
+        hit = FALSE;
+        if (energy[best] >= R2_MF_THRESHOLD
+            &&
+            energy[second_best] >= R2_MF_THRESHOLD
+            &&
+            energy[best] < energy[second_best]*R2_MF_TWIST
+            &&
+            energy[best]*R2_MF_TWIST > energy[second_best])
+        {
+            /* Relative peak test */
+            hit = TRUE;
+            for (i = 0;  i < 6;  i++)
+            {
+                if (i != best  &&  i != second_best)
+                {
+                    if (energy[i]*R2_MF_RELATIVE_PEAK >= energy[second_best])
+                    {
+                        /* The best two are not clearly the best */
+                        hit = FALSE;
+                        break;
+                    }
+                }
+            }
+        }
+        if (hit)
+        {
+            /* Get the values into ascending order */
+            if (second_best < best)
+            {
+                i = best;
+                best = second_best;
+                second_best = i;
+            }
+            best = best*5 + second_best - 1;
+            hit_digit = r2_mf_positions[best];
+        }
+        else
+        {
+            hit_digit = 0;
+        }
+        if (s->current_digit != hit_digit  &&  s->callback)
+        {
+            i = (hit_digit)  ?  -99  :  -10;
+            s->callback(s->callback_data, hit_digit, i, 0);
+        }
+        s->current_digit = hit_digit;
+        s->current_sample = 0;
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int r2_mf_rx_get(r2_mf_rx_state_t *s)
+{
+    return s->current_digit;
+}
+/*- End of function --------------------------------------------------------*/
+
+r2_mf_rx_state_t *r2_mf_rx_init(r2_mf_rx_state_t *s,
+                                int fwd,
+                                tone_report_func_t callback,
+                                void *user_data)
+{
+    int i;
+    static int initialised = FALSE;
+
+    if (s == NULL)
+    {
+        if ((s = (r2_mf_rx_state_t *) malloc(sizeof(*s))) == NULL)
+            return NULL;
+    }
+    memset(s, 0, sizeof(*s));
+
+    s->fwd = fwd;
+
+    if (!initialised)
+    {
+        for (i = 0;  i < 6;  i++)
+        {
+            make_goertzel_descriptor(&mf_fwd_detect_desc[i], r2_mf_fwd_frequencies[i], R2_MF_SAMPLES_PER_BLOCK);
+            make_goertzel_descriptor(&mf_back_detect_desc[i], r2_mf_back_frequencies[i], R2_MF_SAMPLES_PER_BLOCK);
+        }
+        initialised = TRUE;
+    }
+    if (fwd)
+    {
+        for (i = 0;  i < 6;  i++)
+            goertzel_init(&s->out[i], &mf_fwd_detect_desc[i]);
+    }
+    else
+    {
+        for (i = 0;  i < 6;  i++)
+            goertzel_init(&s->out[i], &mf_back_detect_desc[i]);
+    }
+    s->callback = callback;
+    s->callback_data = user_data;
+    s->current_digit = 0;
+    s->current_sample = 0;
+    return s;
+}
+/*- End of function --------------------------------------------------------*/
+
+int r2_mf_rx_free(r2_mf_rx_state_t *s)
+{
+    free(s);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/bert.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/bert.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,513 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * bert.c - Bit error rate tests.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2004 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: bert.c,v 1.27 2008/05/13 13:17:22 steveu Exp $
+ */
+
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
+
+#include <inttypes.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <assert.h>
+#include <time.h>
+
+#include "spandsp/telephony.h"
+#include "spandsp/logging.h"
+#include "spandsp/async.h"
+#include "spandsp/bert.h"
+
+static const char *qbf = "VoyeZ Le BricK GeanT QuE J'ExaminE PreS Du WharF 123 456 7890 + - * : = $ % ( )"
+                         "ThE QuicK BrowN FoX JumpS OveR ThE LazY DoG 123 456 7890 + - * : = $ % ( )";
+
+const char *bert_event_to_str(int event)
+{
+    switch (event)
+    {
+    case BERT_REPORT_SYNCED:
+        return "synced";
+    case BERT_REPORT_UNSYNCED:
+        return "unsync'ed";
+    case BERT_REPORT_REGULAR:
+        return "regular";
+    case BERT_REPORT_GT_10_2:
+        return "error rate > 1 in 10^2";
+    case BERT_REPORT_LT_10_2:
+        return "error rate < 1 in 10^2";
+    case BERT_REPORT_LT_10_3:
+        return "error rate < 1 in 10^3";
+    case BERT_REPORT_LT_10_4:
+        return "error rate < 1 in 10^4";
+    case BERT_REPORT_LT_10_5:
+        return "error rate < 1 in 10^5";
+    case BERT_REPORT_LT_10_6:
+        return "error rate < 1 in 10^6";
+    case BERT_REPORT_LT_10_7:
+        return "error rate < 1 in 10^7";
+    }
+    return "???";
+}
+/*- End of function --------------------------------------------------------*/
+
+int bert_get_bit(bert_state_t *s)
+{
+    int bit;
+
+    if (s->limit  &&  s->tx_bits >= s->limit)
+        return PUTBIT_END_OF_DATA;
+    bit = 0;
+    switch (s->pattern_class)
+    {
+    case 0:
+        bit = s->tx_reg & 1;
+        s->tx_reg = (s->tx_reg >> 1) | ((s->tx_reg & 1) << s->shift2);
+        break;
+    case 1:
+        bit = s->tx_reg & 1;
+        s->tx_reg = (s->tx_reg >> 1) | (((s->tx_reg ^ (s->tx_reg >> s->shift)) & 1) << s->shift2);
+        if (s->max_zeros)
+        {
+            /* This generator suppresses runs >s->max_zeros */
+            if (bit)
+            {
+                if (++s->tx_zeros > s->max_zeros)
+                {
+                    s->tx_zeros = 0;
+                    bit ^= 1;
+                }
+            }
+            else
+            {
+                s->tx_zeros = 0;
+            }
+        }
+        bit ^= s->invert;
+        break;
+    case 2:
+        if (s->tx_step_bit == 0)
+        {
+            s->tx_step_bit = 7;
+            s->tx_reg = qbf[s->tx_step++];
+            if (s->tx_reg == 0)
+            {
+                s->tx_reg = 'V';
+                s->tx_step = 1;
+            }
+        }
+        bit = s->tx_reg & 1;
+        s->tx_reg >>= 1;
+        s->tx_step_bit--;
+        break;
+    }
+    s->tx_bits++;
+    return bit;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void assess_error_rate(bert_state_t *s)
+{
+    int i;
+    int j;
+    int sum;
+    int test;
+
+    /* We assess the error rate in decadic steps. For each decade we assess the error over 10 times
+       the number of bits, to smooth the result. This means we assess the 1 in 100 rate based on 1000 bits
+       (i.e. we look for >=10 errors in 1000 bits). We make an assessment every 100 bits, using a sliding
+       window over the last 1000 bits. We assess the 1 in 1000 rate over 10000 bits in a similar way, and
+       so on for the lower error rates. */
+    test = TRUE;
+    for (i = 2;  i <= 7;  i++)
+    {
+        if (++s->decade_ptr[i] < 10)
+            break;
+        /* This decade has reached 10 snapshots, so we need to touch the next decade */
+        s->decade_ptr[i] = 0;
+        /* Sum the last 10 snapshots from this decade, to see if we overflow into the next decade */
+        for (sum = 0, j = 0;  j < 10;  j++)
+            sum += s->decade_bad[i][j];
+        if (test  &&  sum > 10)
+        {
+            /* We overflow into the next decade */
+            test = FALSE;
+            if (s->error_rate != i  &&  s->reporter)
+                s->reporter(s->user_data, BERT_REPORT_GT_10_2 + i - 2, &s->results);
+            s->error_rate = i;
+        }
+        s->decade_bad[i][0] = 0;
+        if (i < 7)
+            s->decade_bad[i + 1][s->decade_ptr[i + 1]] = sum;
+    }
+    if (i > 7)
+    {
+        if (s->decade_ptr[i] >= 10)
+            s->decade_ptr[i] = 0;
+        if (test)
+        {
+            if (s->error_rate != i  &&  s->reporter)
+                s->reporter(s->user_data, BERT_REPORT_GT_10_2 + i - 2, &s->results);
+            s->error_rate = i;
+        }
+    }
+    else
+    {
+        s->decade_bad[i][s->decade_ptr[i]] = 0;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+void bert_put_bit(bert_state_t *s, int bit)
+{
+    if (bit < 0)
+    {
+        /* Special conditions */
+        switch (bit)
+        {
+        case PUTBIT_TRAINING_IN_PROGRESS:
+            span_log(&s->logging, SPAN_LOG_FLOW, "Training in progress\n");
+            break;
+        case PUTBIT_TRAINING_FAILED:
+            span_log(&s->logging, SPAN_LOG_FLOW, "Training failed\n");
+            break;
+        case PUTBIT_TRAINING_SUCCEEDED:
+            span_log(&s->logging, SPAN_LOG_FLOW, "Training succeeded\n");
+            break;
+        case PUTBIT_CARRIER_UP:
+            span_log(&s->logging, SPAN_LOG_FLOW, "Carrier up\n");
+            break;
+        case PUTBIT_CARRIER_DOWN:
+            span_log(&s->logging, SPAN_LOG_FLOW, "Carrier down\n");
+            break;
+        default:
+            span_log(&s->logging, SPAN_LOG_FLOW, "Eh!\n");
+            break;
+        }
+        return;
+    }
+    bit = (bit & 1) ^ s->invert;
+    s->rx_bits++;
+    switch (s->pattern_class)
+    {
+    case 0:
+        if (s->resync)
+        {
+            s->rx_reg = (s->rx_reg >> 1) | (bit << s->shift2);
+            s->ref_reg = (s->ref_reg >> 1) | ((s->ref_reg & 1) << s->shift2);
+            if (s->rx_reg == s->ref_reg)
+            {
+                if (++s->resync > s->resync_time)
+                {
+                    s->resync = 0;
+                    if (s->reporter)
+                        s->reporter(s->user_data, BERT_REPORT_SYNCED, &s->results);
+                }
+            }
+            else
+            {
+                s->resync = 2;
+                s->ref_reg = s->master_reg;
+            }
+        }
+        else
+        {
+            s->results.total_bits++;
+            if ((bit ^ s->ref_reg) & 1)
+                s->results.bad_bits++;
+            s->ref_reg = (s->ref_reg >> 1) | ((s->ref_reg & 1) << s->shift2);
+        }
+        break;
+    case 1:
+        if (s->resync)
+        {
+            /* If we get a reasonable period for which we correctly predict the
+               next bit, we must be in sync. */
+            /* Don't worry about max. zeros tests when resyncing.
+               It might just extend the resync time a little. Trying
+               to include the test might affect robustness. */
+            if (bit == (int) ((s->rx_reg >> s->shift) & 1))
+            {
+                if (++s->resync > s->resync_time)
+                {
+                    s->resync = 0;
+                    if (s->reporter)
+                        s->reporter(s->user_data, BERT_REPORT_SYNCED, &s->results);
+                }
+            }
+            else
+            {
+                s->resync = 2;
+                s->rx_reg ^= s->mask;
+            }
+        }
+        else
+        {
+            s->results.total_bits++;
+            if (s->max_zeros)
+            {
+                /* This generator suppresses runs >s->max_zeros */
+                if ((s->rx_reg & s->mask))
+                {
+                    if (++s->rx_zeros > s->max_zeros)
+                    {
+                        s->rx_zeros = 0;
+                        bit ^= 1;
+                    }
+                }
+                else
+                {
+                    s->rx_zeros = 0;
+                }
+            }
+            if (bit != (int) ((s->rx_reg >> s->shift) & 1))
+            {
+                s->results.bad_bits++;
+                s->resync_bad_bits++;
+                s->decade_bad[2][s->decade_ptr[2]]++;
+            }
+            if (--s->step <= 0)
+            {
+                /* Every hundred bits we need to do the error rate measurement */
+                s->step = 100;
+                assess_error_rate(s);
+            }
+            if (--s->resync_cnt <= 0)
+            {
+                /* Check if there were enough bad bits during this period to
+                   justify a resync. */
+                if (s->resync_bad_bits >= (s->resync_len*s->resync_percent)/100)
+                {
+                    s->resync = 1;
+                    s->results.resyncs++;
+                    if (s->reporter)
+                        s->reporter(s->user_data, BERT_REPORT_UNSYNCED, &s->results);
+                }
+                s->resync_cnt = s->resync_len;
+                s->resync_bad_bits = 0;
+            }
+        }
+        s->rx_reg = (s->rx_reg >> 1) | (((s->rx_reg ^ (s->rx_reg >> s->shift)) & 1) << s->shift2);
+        break;
+    case 2:
+        s->rx_reg = (s->rx_reg >> 1) | (bit << 6);
+        /* TODO: There is no mechanism for synching yet. This only works if things start in sync. */
+        if (++s->rx_step_bit == 7)
+        {
+            s->rx_step_bit = 0;
+            if ((int) s->rx_reg != qbf[s->rx_step])
+            {
+                /* We need to work out the number of actual bad bits here. We need to look at the
+                   error rate, and see it a resync is needed. etc. */
+                s->results.bad_bits++;
+            }
+            if (qbf[++s->rx_step] == '\0')
+                s->rx_step = 0;
+        }
+        s->results.total_bits++;
+        break;
+    }
+    if (s->report_frequency > 0)
+    {
+        if (--s->report_countdown <= 0)
+        {
+            if (s->reporter)
+                s->reporter(s->user_data, BERT_REPORT_REGULAR, &s->results);
+            s->report_countdown = s->report_frequency;
+        }
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+int bert_result(bert_state_t *s, bert_results_t *results)
+{
+    results->total_bits = s->results.total_bits;
+    results->bad_bits = s->results.bad_bits;
+    results->resyncs = s->results.resyncs;
+    return sizeof(*results);
+}
+/*- End of function --------------------------------------------------------*/
+
+void bert_set_report(bert_state_t *s, int freq, bert_report_func_t reporter, void *user_data)
+{
+    s->report_frequency = freq;
+    s->reporter = reporter;
+    s->user_data = user_data;
+    
+    s->report_countdown = s->report_frequency;
+}
+/*- End of function --------------------------------------------------------*/
+
+bert_state_t *bert_init(bert_state_t *s, int limit, int pattern, int resync_len, int resync_percent)
+{
+    int i;
+    int j;
+
+    if (s == NULL)
+    {
+        if ((s = (bert_state_t *) malloc(sizeof(*s))) == NULL)
+            return  NULL;
+    }
+    memset(s, 0, sizeof(*s));
+
+    s->pattern = pattern;
+    s->limit = limit;
+    s->reporter = NULL;
+    s->user_data = NULL;
+    s->report_frequency = 0;
+
+    s->resync_time = 72;
+    s->invert = 0;
+    switch (s->pattern)
+    {
+    case BERT_PATTERN_ZEROS:
+        s->tx_reg = 0;
+        s->shift2 = 31;
+        s->pattern_class = 0;
+        break;
+    case BERT_PATTERN_ONES:
+        s->tx_reg = ~((uint32_t) 0);
+        s->shift2 = 31;
+        s->pattern_class = 0;
+        break;
+    case BERT_PATTERN_7_TO_1:
+        s->tx_reg = 0xFEFEFEFE;
+        s->shift2 = 31;
+        s->pattern_class = 0;
+        break;
+    case BERT_PATTERN_3_TO_1:
+        s->tx_reg = 0xEEEEEEEE;
+        s->shift2 = 31;
+        s->pattern_class = 0;
+        break;
+    case BERT_PATTERN_1_TO_1:
+        s->tx_reg = 0xAAAAAAAA;
+        s->shift2 = 31;
+        s->pattern_class = 0;
+        break;
+    case BERT_PATTERN_1_TO_3:
+        s->tx_reg = 0x11111111;
+        s->shift2 = 31;
+        s->pattern_class = 0;
+        break;
+    case BERT_PATTERN_1_TO_7:
+        s->tx_reg = 0x01010101;
+        s->shift2 = 31;
+        s->pattern_class = 0;
+        break;
+    case BERT_PATTERN_QBF:
+        s->tx_reg = 0;
+        s->pattern_class = 2;
+        break;
+    case BERT_PATTERN_ITU_O151_23:
+        s->pattern_class = 1;
+        s->tx_reg = 0x7FFFFF;
+        s->mask = 0x20;
+        s->shift = 5;
+        s->shift2 = 22;
+        s->invert = 1;
+        s->resync_time = 56;
+        s->max_zeros = 0;
+        break;
+    case BERT_PATTERN_ITU_O151_20:
+        s->pattern_class = 1;
+        s->tx_reg = 0xFFFFF;
+        s->mask = 0x8;
+        s->shift = 3;
+        s->shift2 = 19;
+        s->invert = 1;
+        s->resync_time = 50;
+        s->max_zeros = 14;
+        break;
+    case BERT_PATTERN_ITU_O151_15:
+        s->pattern_class = 1;
+        s->tx_reg = 0x7FFF;
+        s->mask = 0x2;
+        s->shift = 1;
+        s->shift2 = 14;
+        s->invert = 1;
+        s->resync_time = 40;
+        s->max_zeros = 0;
+        break;
+    case BERT_PATTERN_ITU_O152_11:
+        s->pattern_class = 1;
+        s->tx_reg = 0x7FF;
+        s->mask = 0x4;
+        s->shift = 2;
+        s->shift2 = 10;
+        s->invert = 0;
+        s->resync_time = 32;
+        s->max_zeros = 0;
+        break;
+    case BERT_PATTERN_ITU_O153_9:
+        s->pattern_class = 1;
+        s->tx_reg = 0x1FF;
+        s->mask = 0x10;
+        s->shift = 4;
+        s->shift2 = 8;
+        s->invert = 0;
+        s->resync_time = 28;
+        s->max_zeros = 0;
+        break;
+    }
+    s->tx_bits = 0;
+    s->tx_step = 0;
+    s->tx_step_bit = 0;
+    s->tx_zeros = 0;
+
+    s->rx_reg = s->tx_reg;
+    s->ref_reg = s->rx_reg;
+    s->master_reg = s->ref_reg;
+    s->rx_bits = 0;
+    s->rx_step = 0;
+    s->rx_step_bit = 0;
+
+    s->resync = 1;
+    s->resync_cnt = resync_len;
+    s->resync_bad_bits = 0;
+    s->resync_len = resync_len;
+    s->resync_percent = resync_percent;
+    s->results.total_bits = 0;
+    s->results.bad_bits = 0;
+    s->results.resyncs = 0;
+
+    s->report_countdown = 0;
+
+    for (i = 0;  i < 8;  i++)
+    {
+        for (j = 0;  j < 10;  j++)
+            s->decade_bad[i][j] = 0;
+        s->decade_ptr[i] = 0;
+    }
+    s->error_rate = 8;
+    s->step = 100;
+    
+    span_log_init(&s->logging, SPAN_LOG_NONE, NULL);
+    span_log_set_protocol(&s->logging, "BERT");
+
+    return  s;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/bit_operations.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/bit_operations.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,181 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * bit_operations.c - Various bit level operations, such as bit reversal
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2006 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: bit_operations.c,v 1.15 2008/06/28 01:13:08 steveu Exp $
+ */
+
+/*! \file */
+
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
+
+#include <inttypes.h>
+#include <stdlib.h>
+#include <fcntl.h>
+#include <string.h>
+#include <assert.h>
+#include <memory.h>
+
+#include "spandsp/telephony.h"
+#include "spandsp/bit_operations.h"
+
+uint16_t bit_reverse16(uint16_t x)
+{
+    x = (x >> 8) | (x << 8);
+    x = ((x & 0xF0F0) >> 4) | ((x & 0x0F0F) << 4);
+    x = ((x & 0xCCCC) >> 2) | ((x & 0x3333) << 2);
+    return ((x & 0xAAAA) >> 1) | ((x & 0x5555) << 1);
+}
+/*- End of function --------------------------------------------------------*/
+
+uint32_t bit_reverse32(uint32_t x)
+{
+    x = (x >> 16) | (x << 16);
+    x = ((x & 0xFF00FF00) >> 8) | ((x & 0x00FF00FF) << 8);
+    x = ((x & 0xF0F0F0F0) >> 4) | ((x & 0x0F0F0F0F) << 4);
+    x = ((x & 0xCCCCCCCC) >> 2) | ((x & 0x33333333) << 2);
+    return ((x & 0xAAAAAAAA) >> 1) | ((x & 0x55555555) << 1);
+}
+/*- End of function --------------------------------------------------------*/
+
+uint32_t bit_reverse_4bytes(uint32_t x)
+{
+    x = ((x & 0xF0F0F0F0) >> 4) | ((x & 0x0F0F0F0F) << 4);
+    x = ((x & 0xCCCCCCCC) >> 2) | ((x & 0x33333333) << 2);
+    return ((x & 0xAAAAAAAA) >> 1) | ((x & 0x55555555) << 1);
+}
+/*- End of function --------------------------------------------------------*/
+
+#if defined(__x86_64__)
+uint64_t bit_reverse_8bytes(uint64_t x)
+{
+    x = ((x & 0xF0F0F0F0F0F0F0F0LLU) >> 4) | ((x & 0x0F0F0F0F0F0F0F0FLLU) << 4);
+    x = ((x & 0xCCCCCCCCCCCCCCCCLLU) >> 2) | ((x & 0x3333333333333333LLU) << 2);
+    return ((x & 0xAAAAAAAAAAAAAAAALLU) >> 1) | ((x & 0x5555555555555555LLU) << 1);
+}
+/*- End of function --------------------------------------------------------*/
+#endif
+
+void bit_reverse(uint8_t to[], const uint8_t from[], int len)
+{
+#if defined(SPANDSP_MISALIGNED_ACCESS_FAILS)
+    int i;
+#else
+    const uint8_t *y1;
+    uint8_t *z1;
+    const uint32_t *y4;
+    uint32_t *z4;
+    uint32_t x4;
+#if defined(__x86_64__)
+    const uint64_t *y8;
+    uint64_t *z8;
+    uint64_t x8;
+#endif
+#endif
+
+#if defined(SPANDSP_MISALIGNED_ACCESS_FAILS)
+    /* This code works byte by byte, so it works on machines where misalignment
+       is either desperately slow (its a bit slow on practically any machine, but
+       some machines make it desparately slow) or fails. */
+    for (i = 0;  i < len;  i++)
+        to[i] = bit_reverse8(from[i]);
+#else
+    /* This code is this is based on the woolly assumption that the start of the buffers
+       is memory aligned. If it isn't, the routine will be less efficient on some machines,
+       but might not work at all on others. */
+#if defined(__x86_64__)
+    y8 = (const uint64_t *) from;
+    z8 = (uint64_t *) to;
+    while (len >= sizeof(uint64_t))
+    {
+        x8 = *y8++;
+        x8 = ((x8 & 0xF0F0F0F0F0F0F0F0LLU) >> 4) | ((x8 & 0x0F0F0F0F0F0F0F0FLLU) << 4);
+        x8 = ((x8 & 0xCCCCCCCCCCCCCCCCLLU) >> 2) | ((x8 & 0x3333333333333333LLU) << 2);
+        *z8++ = ((x8 & 0xAAAAAAAAAAAAAAAALLU) >> 1) | ((x8 & 0x5555555555555555LLU) << 1);
+        len -= sizeof(uint64_t);
+    }
+    y4 = (const uint32_t *) y8;
+    z4 = (uint32_t *) z8;
+#else
+    y4 = (const uint32_t *) from;
+    z4 = (uint32_t *) to;
+#endif
+    while (len >= sizeof(uint32_t))
+    {
+        x4 = *y4++;
+        x4 = ((x4 & 0xF0F0F0F0) >> 4) | ((x4 & 0x0F0F0F0F) << 4);
+        x4 = ((x4 & 0xCCCCCCCC) >> 2) | ((x4 & 0x33333333) << 2);
+        *z4++ = ((x4 & 0xAAAAAAAA) >> 1) | ((x4 & 0x55555555) << 1);
+        len -= sizeof(uint32_t);
+    }
+    y1 = (const uint8_t *) y4;
+    z1 = (uint8_t *) z4;
+    while (len-- > 0)
+        *z1++ = bit_reverse8(*y1++);
+#endif
+}
+/*- End of function --------------------------------------------------------*/
+
+int one_bits32(uint32_t x)
+{
+    x = x - ((x >> 1) & 0x55555555);
+    /* We now have 16 2-bit counts */
+    x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
+    /* We now have 8 4-bit counts */
+    x = (x + (x >> 4)) & 0x0F0F0F0F;
+    /* We now have 4 8-bit counts */
+#if defined(__i386__)  ||  defined(__x86_64__)  ||  defined(__ppc__)  ||  defined(__powerpc__)
+    /* If multiply is fast */
+    return (x*0x01010101) >> 24;
+#else
+    /* If multiply is slow */
+    x += (x >> 8);
+    x += (x >> 16);
+    return (x & 0x0000003F);
+#endif
+}
+/*- End of function --------------------------------------------------------*/
+
+uint32_t make_mask32(uint32_t x)
+{
+    x |= (x >> 1);
+    x |= (x >> 2);
+    x |= (x >> 4);
+    x |= (x >> 8);
+    x |= (x >> 16);
+    return x;
+}
+/*- End of function --------------------------------------------------------*/
+
+uint16_t make_mask16(uint16_t x)
+{
+    x |= (x >> 1);
+    x |= (x >> 2);
+    x |= (x >> 4);
+    x |= (x >> 8);
+    return x;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/bitstream.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/bitstream.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,137 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * bitstream.c - Bitstream composition and decomposition routines.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2006 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: bitstream.c,v 1.13 2008/05/13 13:17:22 steveu Exp $
+ */
+
+/*! \file */
+
+#if defined(HAVE_CONFIG_H)
+#include <config.h>
+#endif
+
+#include <inttypes.h>
+#include <stdlib.h>
+#include <string.h>
+#include <assert.h>
+
+#include "spandsp/telephony.h"
+#include "spandsp/bitstream.h"
+
+void bitstream_put(bitstream_state_t *s, uint8_t **c, unsigned int value, int bits)
+{
+    value &= ((1 << bits) - 1);
+    if (s->residue + bits <= 32)
+    {
+        s->bitstream |= (value << s->residue);
+        s->residue += bits;
+    }
+    while (s->residue >= 8)
+    {
+        s->residue -= 8;
+        *(*c)++ = (uint8_t) (s->bitstream & 0xFF);
+        s->bitstream >>= 8;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+void bitstream_put2(bitstream_state_t *s, uint8_t **c, unsigned int value, int bits)
+{
+    value &= ((1 << bits) - 1);
+    if (s->residue + bits <= 32)
+    {
+        s->bitstream = (s->bitstream << bits) | value;
+        s->residue += bits;
+    }
+    while (s->residue >= 8)
+    {
+        s->residue -= 8;
+        *(*c)++ = (uint8_t) ((s->bitstream >> s->residue) & 0xFF);
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+unsigned int bitstream_get(bitstream_state_t *s, const uint8_t **c, int bits)
+{
+    unsigned int x;
+
+    while (s->residue < (unsigned int) bits)
+    {
+        x = (unsigned int) *(*c)++;
+        s->bitstream |= (x << s->residue);
+        s->residue += 8;
+    }
+    s->residue -= bits;
+    x = s->bitstream & ((1 << bits) - 1);
+    s->bitstream >>= bits;
+    return x;
+}
+/*- End of function --------------------------------------------------------*/
+
+unsigned int bitstream_get2(bitstream_state_t *s, const uint8_t **c, int bits)
+{
+    unsigned int x;
+
+    while (s->residue < (unsigned int) bits)
+    {
+        x = (unsigned int) *(*c)++;
+        s->bitstream = (s->bitstream << 8) | x;
+        s->residue += 8;
+    }
+    s->residue -= bits;
+    x = (s->bitstream >> s->residue) & ((1 << bits) - 1);
+    return x;
+}
+/*- End of function --------------------------------------------------------*/
+
+void bitstream_flush(bitstream_state_t *s, uint8_t **c)
+{
+    if (s->residue > 0)
+    {
+        *(*c)++ = (uint8_t) ((s->bitstream << (8 - s->residue)) & 0xFF);
+        s->residue = 0;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+void bitstream_flush2(bitstream_state_t *s, uint8_t **c)
+{
+    if (s->residue > 0)
+    {
+        *(*c)++ = (uint8_t) ((s->bitstream << (8 - s->residue)) & 0xFF);
+        s->residue = 0;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+bitstream_state_t *bitstream_init(bitstream_state_t *s)
+{
+    if (s == NULL)
+        return NULL;
+    s->bitstream = 0;
+    s->residue = 0;
+    return s;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/complex_filters.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/complex_filters.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,104 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * complex_filters.c
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2003 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: complex_filters.c,v 1.13 2008/05/13 13:17:22 steveu Exp $
+ */
+
+#if defined(HAVE_CONFIG_H)
+#include <config.h>
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <inttypes.h>
+
+#include "spandsp/complex.h"
+#include "spandsp/complex_filters.h"
+
+filter_t *filter_create(fspec_t *fs)
+{
+    int i;
+    filter_t *fi;
+
+    if ((fi = (filter_t *) malloc(sizeof(*fi) + sizeof(float)*(fs->np + 1))))
+    {
+        fi->fs = fs;
+        fi->sum = 0.0;
+        /* Moving average filters only */
+        fi->ptr = 0;
+        for (i = 0;  i <= fi->fs->np;  i++)
+            fi->v[i] = 0.0;
+    }
+    return fi;
+}
+
+void filter_delete(filter_t *fi)
+{
+    if (fi)
+        free(fi);
+}
+
+float filter_step(filter_t *fi, float x)
+{
+    return fi->fs->fsf(fi, x);
+}
+
+cfilter_t *cfilter_create(fspec_t *fs)
+{
+    cfilter_t *cfi;
+
+    if ((cfi = (cfilter_t *) malloc(sizeof(*cfi))))
+    {
+        if ((cfi->ref = filter_create(fs)) == NULL)
+        {
+            free(cfi);
+            return NULL;
+        }
+        if ((cfi->imf = filter_create(fs)) == NULL)
+        {
+            free(cfi->ref);
+            free(cfi);
+            return NULL;
+        }
+    }
+    return cfi;
+}
+
+void cfilter_delete(cfilter_t *cfi)
+{
+    if (cfi)
+    {
+        filter_delete(cfi->ref);
+        filter_delete(cfi->imf);
+    }
+}
+
+complexf_t cfilter_step(cfilter_t *cfi, const complexf_t *z)
+{
+    complexf_t cc;
+    
+    cc.re = filter_step(cfi->ref, z->re);
+    cc.im = filter_step(cfi->imf, z->im);
+    return cc;
+}

Added: freeswitch/trunk/libs/spandsp/src/complex_vector_float.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/complex_vector_float.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,53 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * vector_float.c - Floating vector arithmetic routines.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2006 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: complex_vector_float.c,v 1.8 2008/07/02 14:48:25 steveu Exp $
+ */
+
+/*! \file */
+
+#if defined(HAVE_CONFIG_H)
+#include <config.h>
+#endif
+
+#include <inttypes.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include "floating_fudge.h"
+#if defined(HAVE_TGMATH_H)
+#include <tgmath.h>
+#endif
+#if defined(HAVE_MATH_H)
+#include <math.h>
+#endif
+#include <assert.h>
+
+#include "spandsp/telephony.h"
+#include "spandsp/logging.h"
+#include "spandsp/complex.h"
+#include "spandsp/vector_float.h"
+#include "spandsp/complex_vector_float.h"
+
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/crc.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/crc.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,225 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * crc.c
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2003 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: crc.c,v 1.4 2008/05/13 13:17:22 steveu Exp $
+ */
+
+/*! \file */
+
+#if defined(HAVE_CONFIG_H)
+#include <config.h>
+#endif
+
+#include <inttypes.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "spandsp/telephony.h"
+#include "spandsp/crc.h"
+#include "spandsp/bit_operations.h"
+
+static const uint32_t crc_itu32_table[] =
+{
+    0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA, 
+    0x076DC419, 0x706AF48F, 0xE963A535, 0x9E6495A3, 
+    0x0EDB8832, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988, 
+    0x09B64C2B, 0x7EB17CBD, 0xE7B82D07, 0x90BF1D91, 
+    0x1DB71064, 0x6AB020F2, 0xF3B97148, 0x84BE41DE, 
+    0x1ADAD47D, 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7, 
+    0x136C9856, 0x646BA8C0, 0xFD62F97A, 0x8A65C9EC, 
+    0x14015C4F, 0x63066CD9, 0xFA0F3D63, 0x8D080DF5, 
+    0x3B6E20C8, 0x4C69105E, 0xD56041E4, 0xA2677172, 
+    0x3C03E4D1, 0x4B04D447, 0xD20D85FD, 0xA50AB56B, 
+    0x35B5A8FA, 0x42B2986C, 0xDBBBC9D6, 0xACBCF940, 
+    0x32D86CE3, 0x45DF5C75, 0xDCD60DCF, 0xABD13D59, 
+    0x26D930AC, 0x51DE003A, 0xC8D75180, 0xBFD06116, 
+    0x21B4F4B5, 0x56B3C423, 0xCFBA9599, 0xB8BDA50F, 
+    0x2802B89E, 0x5F058808, 0xC60CD9B2, 0xB10BE924, 
+    0x2F6F7C87, 0x58684C11, 0xC1611DAB, 0xB6662D3D, 
+    0x76DC4190, 0x01DB7106, 0x98D220BC, 0xEFD5102A, 
+    0x71B18589, 0x06B6B51F, 0x9FBFE4A5, 0xE8B8D433, 
+    0x7807C9A2, 0x0F00F934, 0x9609A88E, 0xE10E9818, 
+    0x7F6A0DBB, 0x086D3D2D, 0x91646C97, 0xE6635C01, 
+    0x6B6B51F4, 0x1C6C6162, 0x856530D8, 0xF262004E, 
+    0x6C0695ED, 0x1B01A57B, 0x8208F4C1, 0xF50FC457, 
+    0x65B0D9C6, 0x12B7E950, 0x8BBEB8EA, 0xFCB9887C, 
+    0x62DD1DDF, 0x15DA2D49, 0x8CD37CF3, 0xFBD44C65, 
+    0x4DB26158, 0x3AB551CE, 0xA3BC0074, 0xD4BB30E2, 
+    0x4ADFA541, 0x3DD895D7, 0xA4D1C46D, 0xD3D6F4FB, 
+    0x4369E96A, 0x346ED9FC, 0xAD678846, 0xDA60B8D0, 
+    0x44042D73, 0x33031DE5, 0xAA0A4C5F, 0xDD0D7CC9, 
+    0x5005713C, 0x270241AA, 0xBE0B1010, 0xC90C2086, 
+    0x5768B525, 0x206F85B3, 0xB966D409, 0xCE61E49F, 
+    0x5EDEF90E, 0x29D9C998, 0xB0D09822, 0xC7D7A8B4, 
+    0x59B33D17, 0x2EB40D81, 0xB7BD5C3B, 0xC0BA6CAD, 
+    0xEDB88320, 0x9ABFB3B6, 0x03B6E20C, 0x74B1D29A, 
+    0xEAD54739, 0x9DD277AF, 0x04DB2615, 0x73DC1683, 
+    0xE3630B12, 0x94643B84, 0x0D6D6A3E, 0x7A6A5AA8, 
+    0xE40ECF0B, 0x9309FF9D, 0x0A00AE27, 0x7D079EB1, 
+    0xF00F9344, 0x8708A3D2, 0x1E01F268, 0x6906C2FE, 
+    0xF762575D, 0x806567CB, 0x196C3671, 0x6E6B06E7, 
+    0xFED41B76, 0x89D32BE0, 0x10DA7A5A, 0x67DD4ACC, 
+    0xF9B9DF6F, 0x8EBEEFF9, 0x17B7BE43, 0x60B08ED5, 
+    0xD6D6A3E8, 0xA1D1937E, 0x38D8C2C4, 0x4FDFF252, 
+    0xD1BB67F1, 0xA6BC5767, 0x3FB506DD, 0x48B2364B, 
+    0xD80D2BDA, 0xAF0A1B4C, 0x36034AF6, 0x41047A60, 
+    0xDF60EFC3, 0xA867DF55, 0x316E8EEF, 0x4669BE79, 
+    0xCB61B38C, 0xBC66831A, 0x256FD2A0, 0x5268E236, 
+    0xCC0C7795, 0xBB0B4703, 0x220216B9, 0x5505262F, 
+    0xC5BA3BBE, 0xB2BD0B28, 0x2BB45A92, 0x5CB36A04, 
+    0xC2D7FFA7, 0xB5D0CF31, 0x2CD99E8B, 0x5BDEAE1D, 
+    0x9B64C2B0, 0xEC63F226, 0x756AA39C, 0x026D930A, 
+    0x9C0906A9, 0xEB0E363F, 0x72076785, 0x05005713, 
+    0x95BF4A82, 0xE2B87A14, 0x7BB12BAE, 0x0CB61B38, 
+    0x92D28E9B, 0xE5D5BE0D, 0x7CDCEFB7, 0x0BDBDF21, 
+    0x86D3D2D4, 0xF1D4E242, 0x68DDB3F8, 0x1FDA836E, 
+    0x81BE16CD, 0xF6B9265B, 0x6FB077E1, 0x18B74777, 
+    0x88085AE6, 0xFF0F6A70, 0x66063BCA, 0x11010B5C, 
+    0x8F659EFF, 0xF862AE69, 0x616BFFD3, 0x166CCF45, 
+    0xA00AE278, 0xD70DD2EE, 0x4E048354, 0x3903B3C2, 
+    0xA7672661, 0xD06016F7, 0x4969474D, 0x3E6E77DB, 
+    0xAED16A4A, 0xD9D65ADC, 0x40DF0B66, 0x37D83BF0, 
+    0xA9BCAE53, 0xDEBB9EC5, 0x47B2CF7F, 0x30B5FFE9, 
+    0xBDBDF21C, 0xCABAC28A, 0x53B39330, 0x24B4A3A6, 
+    0xBAD03605, 0xCDD70693, 0x54DE5729, 0x23D967BF, 
+    0xB3667A2E, 0xC4614AB8, 0x5D681B02, 0x2A6F2B94, 
+    0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D
+};
+
+uint32_t crc_itu32_calc(const uint8_t *buf, int len, uint32_t crc)
+{
+    int i;
+
+    for (i = 0;  i < len;  i++)
+        crc = ((crc >> 8) & 0x00FFFFFF) ^ crc_itu32_table[(crc ^ buf[i]) & 0xFF];
+    return crc;
+}
+/*- End of function --------------------------------------------------------*/
+
+int crc_itu32_append(uint8_t *buf, int len)
+{
+    uint32_t crc;
+    int new_len;
+    int i;
+
+    crc = 0xFFFFFFFF;
+    new_len = len + 4;
+    for (i = 0;  i < len;  i++)
+        crc = ((crc >> 8) & 0x00FFFFFF) ^ crc_itu32_table[(crc ^ buf[i]) & 0xFF];
+    crc ^= 0xFFFFFFFF;
+    buf[i++] = (uint8_t) crc;
+    buf[i++] = (uint8_t) (crc >> 8);
+    buf[i++] = (uint8_t) (crc >> 16);
+    buf[i++] = (uint8_t) (crc >> 24);
+    return new_len;
+}
+/*- End of function --------------------------------------------------------*/
+
+int crc_itu32_check(const uint8_t *buf, int len)
+{
+    uint32_t crc;
+    int i;
+
+    crc = 0xFFFFFFFF;
+    for (i = 0;  i < len;  i++)
+        crc = ((crc >> 8) & 0x00FFFFFF) ^ crc_itu32_table[(crc ^ buf[i]) & 0xFF];
+    return (crc == 0xDEBB20E3);
+}
+/*- End of function --------------------------------------------------------*/
+
+static const uint16_t crc_itu16_table[] =
+{
+    0x0000, 0x1189, 0x2312, 0x329B, 0x4624, 0x57AD, 0x6536, 0x74BF,
+    0x8C48, 0x9DC1, 0xAF5A, 0xBED3, 0xCA6C, 0xDBE5, 0xE97E, 0xF8F7,
+    0x1081, 0x0108, 0x3393, 0x221A, 0x56A5, 0x472C, 0x75B7, 0x643E,
+    0x9CC9, 0x8D40, 0xBFDB, 0xAE52, 0xDAED, 0xCB64, 0xF9FF, 0xE876,
+    0x2102, 0x308B, 0x0210, 0x1399, 0x6726, 0x76AF, 0x4434, 0x55BD,
+    0xAD4A, 0xBCC3, 0x8E58, 0x9FD1, 0xEB6E, 0xFAE7, 0xC87C, 0xD9F5,
+    0x3183, 0x200A, 0x1291, 0x0318, 0x77A7, 0x662E, 0x54B5, 0x453C,
+    0xBDCB, 0xAC42, 0x9ED9, 0x8F50, 0xFBEF, 0xEA66, 0xD8FD, 0xC974,
+    0x4204, 0x538D, 0x6116, 0x709F, 0x0420, 0x15A9, 0x2732, 0x36BB,
+    0xCE4C, 0xDFC5, 0xED5E, 0xFCD7, 0x8868, 0x99E1, 0xAB7A, 0xBAF3,
+    0x5285, 0x430C, 0x7197, 0x601E, 0x14A1, 0x0528, 0x37B3, 0x263A,
+    0xDECD, 0xCF44, 0xFDDF, 0xEC56, 0x98E9, 0x8960, 0xBBFB, 0xAA72,
+    0x6306, 0x728F, 0x4014, 0x519D, 0x2522, 0x34AB, 0x0630, 0x17B9,
+    0xEF4E, 0xFEC7, 0xCC5C, 0xDDD5, 0xA96A, 0xB8E3, 0x8A78, 0x9BF1,
+    0x7387, 0x620E, 0x5095, 0x411C, 0x35A3, 0x242A, 0x16B1, 0x0738,
+    0xFFCF, 0xEE46, 0xDCDD, 0xCD54, 0xB9EB, 0xA862, 0x9AF9, 0x8B70,
+    0x8408, 0x9581, 0xA71A, 0xB693, 0xC22C, 0xD3A5, 0xE13E, 0xF0B7,
+    0x0840, 0x19C9, 0x2B52, 0x3ADB, 0x4E64, 0x5FED, 0x6D76, 0x7CFF,
+    0x9489, 0x8500, 0xB79B, 0xA612, 0xD2AD, 0xC324, 0xF1BF, 0xE036,
+    0x18C1, 0x0948, 0x3BD3, 0x2A5A, 0x5EE5, 0x4F6C, 0x7DF7, 0x6C7E,
+    0xA50A, 0xB483, 0x8618, 0x9791, 0xE32E, 0xF2A7, 0xC03C, 0xD1B5,
+    0x2942, 0x38CB, 0x0A50, 0x1BD9, 0x6F66, 0x7EEF, 0x4C74, 0x5DFD,
+    0xB58B, 0xA402, 0x9699, 0x8710, 0xF3AF, 0xE226, 0xD0BD, 0xC134,
+    0x39C3, 0x284A, 0x1AD1, 0x0B58, 0x7FE7, 0x6E6E, 0x5CF5, 0x4D7C,
+    0xC60C, 0xD785, 0xE51E, 0xF497, 0x8028, 0x91A1, 0xA33A, 0xB2B3,
+    0x4A44, 0x5BCD, 0x6956, 0x78DF, 0x0C60, 0x1DE9, 0x2F72, 0x3EFB,
+    0xD68D, 0xC704, 0xF59F, 0xE416, 0x90A9, 0x8120, 0xB3BB, 0xA232,
+    0x5AC5, 0x4B4C, 0x79D7, 0x685E, 0x1CE1, 0x0D68, 0x3FF3, 0x2E7A,
+    0xE70E, 0xF687, 0xC41C, 0xD595, 0xA12A, 0xB0A3, 0x8238, 0x93B1,
+    0x6B46, 0x7ACF, 0x4854, 0x59DD, 0x2D62, 0x3CEB, 0x0E70, 0x1FF9,
+    0xF78F, 0xE606, 0xD49D, 0xC514, 0xB1AB, 0xA022, 0x92B9, 0x8330,
+    0x7BC7, 0x6A4E, 0x58D5, 0x495C, 0x3DE3, 0x2C6A, 0x1EF1, 0x0F78
+};
+
+uint16_t crc_itu16_calc(const uint8_t *buf, int len, uint16_t crc)
+{
+    int i;
+
+    for (i = 0;  i < len;  i++)
+        crc = (crc >> 8) ^ crc_itu16_table[(crc ^ buf[i]) & 0xFF];
+    return crc;
+}
+/*- End of function --------------------------------------------------------*/
+
+int crc_itu16_append(uint8_t *buf, int len)
+{
+    uint16_t crc;
+    int new_len;
+    int i;
+
+    crc = 0xFFFF;
+    new_len = len + 2;
+    for (i = 0;  i < len;  i++)
+        crc = (crc >> 8) ^ crc_itu16_table[(crc ^ buf[i]) & 0xFF];
+    crc ^= 0xFFFF;
+    buf[i++] = (uint8_t) crc;
+    buf[i++] = (uint8_t) (crc >> 8);
+    return new_len;
+}
+/*- End of function --------------------------------------------------------*/
+
+int crc_itu16_check(const uint8_t *buf, int len)
+{
+    uint16_t crc;
+    int i;
+
+    crc = 0xFFFF;
+    for (i = 0;  i < len;  i++)
+        crc = (crc >> 8) ^ crc_itu16_table[(crc ^ buf[i]) & 0xFF];
+    return (crc & 0xFFFF) == 0xF0B8;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/dds_float.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/dds_float.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,2189 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * complex_dds.c
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2003 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: dds_float.c,v 1.8 2008/07/02 14:48:25 steveu Exp $
+ */
+
+/*! \file */
+
+#if defined(HAVE_CONFIG_H)
+#include <config.h>
+#endif
+
+#include <stdio.h>
+#include <inttypes.h>
+#include "floating_fudge.h"
+#if defined(HAVE_TGMATH_H)
+#include <tgmath.h>
+#endif
+#if defined(HAVE_MATH_H)
+#include <math.h>
+#endif
+
+#include "spandsp/telephony.h"
+#include "spandsp/complex.h"
+#include "spandsp/dds.h"
+
+#define SLENK       11
+#define SINELEN     (1 << SLENK)
+
+/* Precreating this table allows it to be in const memory, which might
+   have some performance advantage. */
+static const float sine_table[SINELEN] =
+{
+     0.00000000f,
+     0.00306796f,
+     0.00613588f,
+     0.00920375f,
+     0.01227154f,
+     0.01533921f,
+     0.01840673f,
+     0.02147408f,
+     0.02454123f,
+     0.02760815f,
+     0.03067480f,
+     0.03374117f,
+     0.03680722f,
+     0.03987293f,
+     0.04293826f,
+     0.04600318f,
+     0.04906767f,
+     0.05213170f,
+     0.05519524f,
+     0.05825826f,
+     0.06132074f,
+     0.06438263f,
+     0.06744392f,
+     0.07050457f,
+     0.07356456f,
+     0.07662386f,
+     0.07968244f,
+     0.08274026f,
+     0.08579731f,
+     0.08885355f,
+     0.09190896f,
+     0.09496350f,
+     0.09801714f,
+     0.10106986f,
+     0.10412163f,
+     0.10717242f,
+     0.11022221f,
+     0.11327095f,
+     0.11631863f,
+     0.11936521f,
+     0.12241068f,
+     0.12545498f,
+     0.12849811f,
+     0.13154003f,
+     0.13458071f,
+     0.13762012f,
+     0.14065824f,
+     0.14369503f,
+     0.14673047f,
+     0.14976453f,
+     0.15279719f,
+     0.15582840f,
+     0.15885814f,
+     0.16188639f,
+     0.16491312f,
+     0.16793829f,
+     0.17096189f,
+     0.17398387f,
+     0.17700422f,
+     0.18002290f,
+     0.18303989f,
+     0.18605515f,
+     0.18906866f,
+     0.19208040f,
+     0.19509032f,
+     0.19809841f,
+     0.20110463f,
+     0.20410897f,
+     0.20711138f,
+     0.21011184f,
+     0.21311032f,
+     0.21610680f,
+     0.21910124f,
+     0.22209362f,
+     0.22508391f,
+     0.22807208f,
+     0.23105811f,
+     0.23404196f,
+     0.23702361f,
+     0.24000302f,
+     0.24298018f,
+     0.24595505f,
+     0.24892761f,
+     0.25189782f,
+     0.25486566f,
+     0.25783110f,
+     0.26079412f,
+     0.26375468f,
+     0.26671276f,
+     0.26966833f,
+     0.27262136f,
+     0.27557182f,
+     0.27851969f,
+     0.28146494f,
+     0.28440754f,
+     0.28734746f,
+     0.29028468f,
+     0.29321916f,
+     0.29615089f,
+     0.29907983f,
+     0.30200595f,
+     0.30492923f,
+     0.30784964f,
+     0.31076715f,
+     0.31368174f,
+     0.31659338f,
+     0.31950203f,
+     0.32240768f,
+     0.32531029f,
+     0.32820984f,
+     0.33110631f,
+     0.33399965f,
+     0.33688985f,
+     0.33977688f,
+     0.34266072f,
+     0.34554132f,
+     0.34841868f,
+     0.35129276f,
+     0.35416353f,
+     0.35703096f,
+     0.35989504f,
+     0.36275572f,
+     0.36561300f,
+     0.36846683f,
+     0.37131719f,
+     0.37416406f,
+     0.37700741f,
+     0.37984721f,
+     0.38268343f,
+     0.38551605f,
+     0.38834505f,
+     0.39117038f,
+     0.39399204f,
+     0.39680999f,
+     0.39962420f,
+     0.40243465f,
+     0.40524131f,
+     0.40804416f,
+     0.41084317f,
+     0.41363831f,
+     0.41642956f,
+     0.41921689f,
+     0.42200027f,
+     0.42477968f,
+     0.42755509f,
+     0.43032648f,
+     0.43309382f,
+     0.43585708f,
+     0.43861624f,
+     0.44137127f,
+     0.44412214f,
+     0.44686884f,
+     0.44961133f,
+     0.45234959f,
+     0.45508359f,
+     0.45781330f,
+     0.46053871f,
+     0.46325978f,
+     0.46597650f,
+     0.46868882f,
+     0.47139674f,
+     0.47410021f,
+     0.47679923f,
+     0.47949376f,
+     0.48218377f,
+     0.48486925f,
+     0.48755016f,
+     0.49022648f,
+     0.49289819f,
+     0.49556526f,
+     0.49822767f,
+     0.50088538f,
+     0.50353838f,
+     0.50618665f,
+     0.50883014f,
+     0.51146885f,
+     0.51410274f,
+     0.51673180f,
+     0.51935599f,
+     0.52197529f,
+     0.52458968f,
+     0.52719913f,
+     0.52980362f,
+     0.53240313f,
+     0.53499762f,
+     0.53758708f,
+     0.54017147f,
+     0.54275078f,
+     0.54532499f,
+     0.54789406f,
+     0.55045797f,
+     0.55301671f,
+     0.55557023f,
+     0.55811853f,
+     0.56066158f,
+     0.56319934f,
+     0.56573181f,
+     0.56825895f,
+     0.57078075f,
+     0.57329717f,
+     0.57580819f,
+     0.57831380f,
+     0.58081396f,
+     0.58330865f,
+     0.58579786f,
+     0.58828155f,
+     0.59075970f,
+     0.59323230f,
+     0.59569930f,
+     0.59816071f,
+     0.60061648f,
+     0.60306660f,
+     0.60551104f,
+     0.60794978f,
+     0.61038281f,
+     0.61281008f,
+     0.61523159f,
+     0.61764731f,
+     0.62005721f,
+     0.62246128f,
+     0.62485949f,
+     0.62725182f,
+     0.62963824f,
+     0.63201874f,
+     0.63439328f,
+     0.63676186f,
+     0.63912444f,
+     0.64148101f,
+     0.64383154f,
+     0.64617601f,
+     0.64851440f,
+     0.65084668f,
+     0.65317284f,
+     0.65549285f,
+     0.65780669f,
+     0.66011434f,
+     0.66241578f,
+     0.66471098f,
+     0.66699992f,
+     0.66928259f,
+     0.67155895f,
+     0.67382900f,
+     0.67609270f,
+     0.67835004f,
+     0.68060100f,
+     0.68284555f,
+     0.68508367f,
+     0.68731534f,
+     0.68954054f,
+     0.69175926f,
+     0.69397146f,
+     0.69617713f,
+     0.69837625f,
+     0.70056879f,
+     0.70275474f,
+     0.70493408f,
+     0.70710678f,
+     0.70927283f,
+     0.71143220f,
+     0.71358487f,
+     0.71573083f,
+     0.71787005f,
+     0.72000251f,
+     0.72212819f,
+     0.72424708f,
+     0.72635916f,
+     0.72846439f,
+     0.73056277f,
+     0.73265427f,
+     0.73473888f,
+     0.73681657f,
+     0.73888732f,
+     0.74095113f,
+     0.74300795f,
+     0.74505779f,
+     0.74710061f,
+     0.74913639f,
+     0.75116513f,
+     0.75318680f,
+     0.75520138f,
+     0.75720885f,
+     0.75920919f,
+     0.76120239f,
+     0.76318842f,
+     0.76516727f,
+     0.76713891f,
+     0.76910334f,
+     0.77106052f,
+     0.77301045f,
+     0.77495311f,
+     0.77688847f,
+     0.77881651f,
+     0.78073723f,
+     0.78265060f,
+     0.78455660f,
+     0.78645521f,
+     0.78834643f,
+     0.79023022f,
+     0.79210658f,
+     0.79397548f,
+     0.79583690f,
+     0.79769084f,
+     0.79953727f,
+     0.80137617f,
+     0.80320753f,
+     0.80503133f,
+     0.80684755f,
+     0.80865618f,
+     0.81045720f,
+     0.81225059f,
+     0.81403633f,
+     0.81581441f,
+     0.81758481f,
+     0.81934752f,
+     0.82110251f,
+     0.82284978f,
+     0.82458930f,
+     0.82632106f,
+     0.82804505f,
+     0.82976123f,
+     0.83146961f,
+     0.83317016f,
+     0.83486287f,
+     0.83654773f,
+     0.83822471f,
+     0.83989379f,
+     0.84155498f,
+     0.84320824f,
+     0.84485357f,
+     0.84649094f,
+     0.84812034f,
+     0.84974177f,
+     0.85135519f,
+     0.85296060f,
+     0.85455799f,
+     0.85614733f,
+     0.85772861f,
+     0.85930182f,
+     0.86086694f,
+     0.86242396f,
+     0.86397286f,
+     0.86551362f,
+     0.86704625f,
+     0.86857071f,
+     0.87008699f,
+     0.87159509f,
+     0.87309498f,
+     0.87458665f,
+     0.87607009f,
+     0.87754529f,
+     0.87901223f,
+     0.88047089f,
+     0.88192126f,
+     0.88336334f,
+     0.88479710f,
+     0.88622253f,
+     0.88763962f,
+     0.88904836f,
+     0.89044872f,
+     0.89184071f,
+     0.89322430f,
+     0.89459949f,
+     0.89596625f,
+     0.89732458f,
+     0.89867447f,
+     0.90001589f,
+     0.90134885f,
+     0.90267332f,
+     0.90398929f,
+     0.90529676f,
+     0.90659570f,
+     0.90788612f,
+     0.90916798f,
+     0.91044129f,
+     0.91170603f,
+     0.91296219f,
+     0.91420976f,
+     0.91544872f,
+     0.91667906f,
+     0.91790078f,
+     0.91911385f,
+     0.92031828f,
+     0.92151404f,
+     0.92270113f,
+     0.92387953f,
+     0.92504924f,
+     0.92621024f,
+     0.92736253f,
+     0.92850608f,
+     0.92964090f,
+     0.93076696f,
+     0.93188427f,
+     0.93299280f,
+     0.93409255f,
+     0.93518351f,
+     0.93626567f,
+     0.93733901f,
+     0.93840353f,
+     0.93945922f,
+     0.94050607f,
+     0.94154407f,
+     0.94257320f,
+     0.94359346f,
+     0.94460484f,
+     0.94560733f,
+     0.94660091f,
+     0.94758559f,
+     0.94856135f,
+     0.94952818f,
+     0.95048607f,
+     0.95143502f,
+     0.95237501f,
+     0.95330604f,
+     0.95422810f,
+     0.95514117f,
+     0.95604525f,
+     0.95694034f,
+     0.95782641f,
+     0.95870347f,
+     0.95957151f,
+     0.96043052f,
+     0.96128049f,
+     0.96212140f,
+     0.96295327f,
+     0.96377607f,
+     0.96458979f,
+     0.96539444f,
+     0.96619000f,
+     0.96697647f,
+     0.96775384f,
+     0.96852209f,
+     0.96928124f,
+     0.97003125f,
+     0.97077214f,
+     0.97150389f,
+     0.97222650f,
+     0.97293995f,
+     0.97364425f,
+     0.97433938f,
+     0.97502535f,
+     0.97570213f,
+     0.97636973f,
+     0.97702814f,
+     0.97767736f,
+     0.97831737f,
+     0.97894818f,
+     0.97956977f,
+     0.98018214f,
+     0.98078528f,
+     0.98137919f,
+     0.98196387f,
+     0.98253930f,
+     0.98310549f,
+     0.98366242f,
+     0.98421009f,
+     0.98474850f,
+     0.98527764f,
+     0.98579751f,
+     0.98630810f,
+     0.98680940f,
+     0.98730142f,
+     0.98778414f,
+     0.98825757f,
+     0.98872169f,
+     0.98917651f,
+     0.98962202f,
+     0.99005821f,
+     0.99048508f,
+     0.99090264f,
+     0.99131086f,
+     0.99170975f,
+     0.99209931f,
+     0.99247953f,
+     0.99285041f,
+     0.99321195f,
+     0.99356414f,
+     0.99390697f,
+     0.99424045f,
+     0.99456457f,
+     0.99487933f,
+     0.99518473f,
+     0.99548076f,
+     0.99576741f,
+     0.99604470f,
+     0.99631261f,
+     0.99657115f,
+     0.99682030f,
+     0.99706007f,
+     0.99729046f,
+     0.99751146f,
+     0.99772307f,
+     0.99792529f,
+     0.99811811f,
+     0.99830154f,
+     0.99847558f,
+     0.99864022f,
+     0.99879546f,
+     0.99894129f,
+     0.99907773f,
+     0.99920476f,
+     0.99932238f,
+     0.99943060f,
+     0.99952942f,
+     0.99961882f,
+     0.99969882f,
+     0.99976941f,
+     0.99983058f,
+     0.99988235f,
+     0.99992470f,
+     0.99995764f,
+     0.99998118f,
+     0.99999529f,
+     1.00000000f,
+     0.99999529f,
+     0.99998118f,
+     0.99995764f,
+     0.99992470f,
+     0.99988235f,
+     0.99983058f,
+     0.99976941f,
+     0.99969882f,
+     0.99961882f,
+     0.99952942f,
+     0.99943060f,
+     0.99932238f,
+     0.99920476f,
+     0.99907773f,
+     0.99894129f,
+     0.99879546f,
+     0.99864022f,
+     0.99847558f,
+     0.99830154f,
+     0.99811811f,
+     0.99792529f,
+     0.99772307f,
+     0.99751146f,
+     0.99729046f,
+     0.99706007f,
+     0.99682030f,
+     0.99657115f,
+     0.99631261f,
+     0.99604470f,
+     0.99576741f,
+     0.99548076f,
+     0.99518473f,
+     0.99487933f,
+     0.99456457f,
+     0.99424045f,
+     0.99390697f,
+     0.99356414f,
+     0.99321195f,
+     0.99285041f,
+     0.99247953f,
+     0.99209931f,
+     0.99170975f,
+     0.99131086f,
+     0.99090264f,
+     0.99048508f,
+     0.99005821f,
+     0.98962202f,
+     0.98917651f,
+     0.98872169f,
+     0.98825757f,
+     0.98778414f,
+     0.98730142f,
+     0.98680940f,
+     0.98630810f,
+     0.98579751f,
+     0.98527764f,
+     0.98474850f,
+     0.98421009f,
+     0.98366242f,
+     0.98310549f,
+     0.98253930f,
+     0.98196387f,
+     0.98137919f,
+     0.98078528f,
+     0.98018214f,
+     0.97956977f,
+     0.97894818f,
+     0.97831737f,
+     0.97767736f,
+     0.97702814f,
+     0.97636973f,
+     0.97570213f,
+     0.97502535f,
+     0.97433938f,
+     0.97364425f,
+     0.97293995f,
+     0.97222650f,
+     0.97150389f,
+     0.97077214f,
+     0.97003125f,
+     0.96928124f,
+     0.96852209f,
+     0.96775384f,
+     0.96697647f,
+     0.96619000f,
+     0.96539444f,
+     0.96458979f,
+     0.96377607f,
+     0.96295327f,
+     0.96212140f,
+     0.96128049f,
+     0.96043052f,
+     0.95957151f,
+     0.95870347f,
+     0.95782641f,
+     0.95694034f,
+     0.95604525f,
+     0.95514117f,
+     0.95422810f,
+     0.95330604f,
+     0.95237501f,
+     0.95143502f,
+     0.95048607f,
+     0.94952818f,
+     0.94856135f,
+     0.94758559f,
+     0.94660091f,
+     0.94560733f,
+     0.94460484f,
+     0.94359346f,
+     0.94257320f,
+     0.94154407f,
+     0.94050607f,
+     0.93945922f,
+     0.93840353f,
+     0.93733901f,
+     0.93626567f,
+     0.93518351f,
+     0.93409255f,
+     0.93299280f,
+     0.93188427f,
+     0.93076696f,
+     0.92964090f,
+     0.92850608f,
+     0.92736253f,
+     0.92621024f,
+     0.92504924f,
+     0.92387953f,
+     0.92270113f,
+     0.92151404f,
+     0.92031828f,
+     0.91911385f,
+     0.91790078f,
+     0.91667906f,
+     0.91544872f,
+     0.91420976f,
+     0.91296219f,
+     0.91170603f,
+     0.91044129f,
+     0.90916798f,
+     0.90788612f,
+     0.90659570f,
+     0.90529676f,
+     0.90398929f,
+     0.90267332f,
+     0.90134885f,
+     0.90001589f,
+     0.89867447f,
+     0.89732458f,
+     0.89596625f,
+     0.89459949f,
+     0.89322430f,
+     0.89184071f,
+     0.89044872f,
+     0.88904836f,
+     0.88763962f,
+     0.88622253f,
+     0.88479710f,
+     0.88336334f,
+     0.88192126f,
+     0.88047089f,
+     0.87901223f,
+     0.87754529f,
+     0.87607009f,
+     0.87458665f,
+     0.87309498f,
+     0.87159509f,
+     0.87008699f,
+     0.86857071f,
+     0.86704625f,
+     0.86551362f,
+     0.86397286f,
+     0.86242396f,
+     0.86086694f,
+     0.85930182f,
+     0.85772861f,
+     0.85614733f,
+     0.85455799f,
+     0.85296060f,
+     0.85135519f,
+     0.84974177f,
+     0.84812034f,
+     0.84649094f,
+     0.84485357f,
+     0.84320824f,
+     0.84155498f,
+     0.83989379f,
+     0.83822471f,
+     0.83654773f,
+     0.83486287f,
+     0.83317016f,
+     0.83146961f,
+     0.82976123f,
+     0.82804505f,
+     0.82632106f,
+     0.82458930f,
+     0.82284978f,
+     0.82110251f,
+     0.81934752f,
+     0.81758481f,
+     0.81581441f,
+     0.81403633f,
+     0.81225059f,
+     0.81045720f,
+     0.80865618f,
+     0.80684755f,
+     0.80503133f,
+     0.80320753f,
+     0.80137617f,
+     0.79953727f,
+     0.79769084f,
+     0.79583690f,
+     0.79397548f,
+     0.79210658f,
+     0.79023022f,
+     0.78834643f,
+     0.78645521f,
+     0.78455660f,
+     0.78265060f,
+     0.78073723f,
+     0.77881651f,
+     0.77688847f,
+     0.77495311f,
+     0.77301045f,
+     0.77106052f,
+     0.76910334f,
+     0.76713891f,
+     0.76516727f,
+     0.76318842f,
+     0.76120239f,
+     0.75920919f,
+     0.75720885f,
+     0.75520138f,
+     0.75318680f,
+     0.75116513f,
+     0.74913639f,
+     0.74710061f,
+     0.74505779f,
+     0.74300795f,
+     0.74095113f,
+     0.73888732f,
+     0.73681657f,
+     0.73473888f,
+     0.73265427f,
+     0.73056277f,
+     0.72846439f,
+     0.72635916f,
+     0.72424708f,
+     0.72212819f,
+     0.72000251f,
+     0.71787005f,
+     0.71573083f,
+     0.71358487f,
+     0.71143220f,
+     0.70927283f,
+     0.70710678f,
+     0.70493408f,
+     0.70275474f,
+     0.70056879f,
+     0.69837625f,
+     0.69617713f,
+     0.69397146f,
+     0.69175926f,
+     0.68954054f,
+     0.68731534f,
+     0.68508367f,
+     0.68284555f,
+     0.68060100f,
+     0.67835004f,
+     0.67609270f,
+     0.67382900f,
+     0.67155895f,
+     0.66928259f,
+     0.66699992f,
+     0.66471098f,
+     0.66241578f,
+     0.66011434f,
+     0.65780669f,
+     0.65549285f,
+     0.65317284f,
+     0.65084668f,
+     0.64851440f,
+     0.64617601f,
+     0.64383154f,
+     0.64148101f,
+     0.63912444f,
+     0.63676186f,
+     0.63439328f,
+     0.63201874f,
+     0.62963824f,
+     0.62725182f,
+     0.62485949f,
+     0.62246128f,
+     0.62005721f,
+     0.61764731f,
+     0.61523159f,
+     0.61281008f,
+     0.61038281f,
+     0.60794978f,
+     0.60551104f,
+     0.60306660f,
+     0.60061648f,
+     0.59816071f,
+     0.59569930f,
+     0.59323230f,
+     0.59075970f,
+     0.58828155f,
+     0.58579786f,
+     0.58330865f,
+     0.58081396f,
+     0.57831380f,
+     0.57580819f,
+     0.57329717f,
+     0.57078075f,
+     0.56825895f,
+     0.56573181f,
+     0.56319934f,
+     0.56066158f,
+     0.55811853f,
+     0.55557023f,
+     0.55301671f,
+     0.55045797f,
+     0.54789406f,
+     0.54532499f,
+     0.54275078f,
+     0.54017147f,
+     0.53758708f,
+     0.53499762f,
+     0.53240313f,
+     0.52980362f,
+     0.52719913f,
+     0.52458968f,
+     0.52197529f,
+     0.51935599f,
+     0.51673180f,
+     0.51410274f,
+     0.51146885f,
+     0.50883014f,
+     0.50618665f,
+     0.50353838f,
+     0.50088538f,
+     0.49822767f,
+     0.49556526f,
+     0.49289819f,
+     0.49022648f,
+     0.48755016f,
+     0.48486925f,
+     0.48218377f,
+     0.47949376f,
+     0.47679923f,
+     0.47410021f,
+     0.47139674f,
+     0.46868882f,
+     0.46597650f,
+     0.46325978f,
+     0.46053871f,
+     0.45781330f,
+     0.45508359f,
+     0.45234959f,
+     0.44961133f,
+     0.44686884f,
+     0.44412214f,
+     0.44137127f,
+     0.43861624f,
+     0.43585708f,
+     0.43309382f,
+     0.43032648f,
+     0.42755509f,
+     0.42477968f,
+     0.42200027f,
+     0.41921689f,
+     0.41642956f,
+     0.41363831f,
+     0.41084317f,
+     0.40804416f,
+     0.40524131f,
+     0.40243465f,
+     0.39962420f,
+     0.39680999f,
+     0.39399204f,
+     0.39117038f,
+     0.38834505f,
+     0.38551605f,
+     0.38268343f,
+     0.37984721f,
+     0.37700741f,
+     0.37416406f,
+     0.37131719f,
+     0.36846683f,
+     0.36561300f,
+     0.36275572f,
+     0.35989504f,
+     0.35703096f,
+     0.35416353f,
+     0.35129276f,
+     0.34841868f,
+     0.34554132f,
+     0.34266072f,
+     0.33977688f,
+     0.33688985f,
+     0.33399965f,
+     0.33110631f,
+     0.32820984f,
+     0.32531029f,
+     0.32240768f,
+     0.31950203f,
+     0.31659338f,
+     0.31368174f,
+     0.31076715f,
+     0.30784964f,
+     0.30492923f,
+     0.30200595f,
+     0.29907983f,
+     0.29615089f,
+     0.29321916f,
+     0.29028468f,
+     0.28734746f,
+     0.28440754f,
+     0.28146494f,
+     0.27851969f,
+     0.27557182f,
+     0.27262136f,
+     0.26966833f,
+     0.26671276f,
+     0.26375468f,
+     0.26079412f,
+     0.25783110f,
+     0.25486566f,
+     0.25189782f,
+     0.24892761f,
+     0.24595505f,
+     0.24298018f,
+     0.24000302f,
+     0.23702361f,
+     0.23404196f,
+     0.23105811f,
+     0.22807208f,
+     0.22508391f,
+     0.22209362f,
+     0.21910124f,
+     0.21610680f,
+     0.21311032f,
+     0.21011184f,
+     0.20711138f,
+     0.20410897f,
+     0.20110463f,
+     0.19809841f,
+     0.19509032f,
+     0.19208040f,
+     0.18906866f,
+     0.18605515f,
+     0.18303989f,
+     0.18002290f,
+     0.17700422f,
+     0.17398387f,
+     0.17096189f,
+     0.16793829f,
+     0.16491312f,
+     0.16188639f,
+     0.15885814f,
+     0.15582840f,
+     0.15279719f,
+     0.14976453f,
+     0.14673047f,
+     0.14369503f,
+     0.14065824f,
+     0.13762012f,
+     0.13458071f,
+     0.13154003f,
+     0.12849811f,
+     0.12545498f,
+     0.12241068f,
+     0.11936521f,
+     0.11631863f,
+     0.11327095f,
+     0.11022221f,
+     0.10717242f,
+     0.10412163f,
+     0.10106986f,
+     0.09801714f,
+     0.09496350f,
+     0.09190896f,
+     0.08885355f,
+     0.08579731f,
+     0.08274026f,
+     0.07968244f,
+     0.07662386f,
+     0.07356456f,
+     0.07050457f,
+     0.06744392f,
+     0.06438263f,
+     0.06132074f,
+     0.05825826f,
+     0.05519524f,
+     0.05213170f,
+     0.04906767f,
+     0.04600318f,
+     0.04293826f,
+     0.03987293f,
+     0.03680722f,
+     0.03374117f,
+     0.03067480f,
+     0.02760815f,
+     0.02454123f,
+     0.02147408f,
+     0.01840673f,
+     0.01533921f,
+     0.01227154f,
+     0.00920375f,
+     0.00613588f,
+     0.00306796f,
+     0.00000000f,
+    -0.00306796f,
+    -0.00613588f,
+    -0.00920375f,
+    -0.01227154f,
+    -0.01533921f,
+    -0.01840673f,
+    -0.02147408f,
+    -0.02454123f,
+    -0.02760815f,
+    -0.03067480f,
+    -0.03374117f,
+    -0.03680722f,
+    -0.03987293f,
+    -0.04293826f,
+    -0.04600318f,
+    -0.04906767f,
+    -0.05213170f,
+    -0.05519524f,
+    -0.05825826f,
+    -0.06132074f,
+    -0.06438263f,
+    -0.06744392f,
+    -0.07050457f,
+    -0.07356456f,
+    -0.07662386f,
+    -0.07968244f,
+    -0.08274026f,
+    -0.08579731f,
+    -0.08885355f,
+    -0.09190896f,
+    -0.09496350f,
+    -0.09801714f,
+    -0.10106986f,
+    -0.10412163f,
+    -0.10717242f,
+    -0.11022221f,
+    -0.11327095f,
+    -0.11631863f,
+    -0.11936521f,
+    -0.12241068f,
+    -0.12545498f,
+    -0.12849811f,
+    -0.13154003f,
+    -0.13458071f,
+    -0.13762012f,
+    -0.14065824f,
+    -0.14369503f,
+    -0.14673047f,
+    -0.14976453f,
+    -0.15279719f,
+    -0.15582840f,
+    -0.15885814f,
+    -0.16188639f,
+    -0.16491312f,
+    -0.16793829f,
+    -0.17096189f,
+    -0.17398387f,
+    -0.17700422f,
+    -0.18002290f,
+    -0.18303989f,
+    -0.18605515f,
+    -0.18906866f,
+    -0.19208040f,
+    -0.19509032f,
+    -0.19809841f,
+    -0.20110463f,
+    -0.20410897f,
+    -0.20711138f,
+    -0.21011184f,
+    -0.21311032f,
+    -0.21610680f,
+    -0.21910124f,
+    -0.22209362f,
+    -0.22508391f,
+    -0.22807208f,
+    -0.23105811f,
+    -0.23404196f,
+    -0.23702361f,
+    -0.24000302f,
+    -0.24298018f,
+    -0.24595505f,
+    -0.24892761f,
+    -0.25189782f,
+    -0.25486566f,
+    -0.25783110f,
+    -0.26079412f,
+    -0.26375468f,
+    -0.26671276f,
+    -0.26966833f,
+    -0.27262136f,
+    -0.27557182f,
+    -0.27851969f,
+    -0.28146494f,
+    -0.28440754f,
+    -0.28734746f,
+    -0.29028468f,
+    -0.29321916f,
+    -0.29615089f,
+    -0.29907983f,
+    -0.30200595f,
+    -0.30492923f,
+    -0.30784964f,
+    -0.31076715f,
+    -0.31368174f,
+    -0.31659338f,
+    -0.31950203f,
+    -0.32240768f,
+    -0.32531029f,
+    -0.32820984f,
+    -0.33110631f,
+    -0.33399965f,
+    -0.33688985f,
+    -0.33977688f,
+    -0.34266072f,
+    -0.34554132f,
+    -0.34841868f,
+    -0.35129276f,
+    -0.35416353f,
+    -0.35703096f,
+    -0.35989504f,
+    -0.36275572f,
+    -0.36561300f,
+    -0.36846683f,
+    -0.37131719f,
+    -0.37416406f,
+    -0.37700741f,
+    -0.37984721f,
+    -0.38268343f,
+    -0.38551605f,
+    -0.38834505f,
+    -0.39117038f,
+    -0.39399204f,
+    -0.39680999f,
+    -0.39962420f,
+    -0.40243465f,
+    -0.40524131f,
+    -0.40804416f,
+    -0.41084317f,
+    -0.41363831f,
+    -0.41642956f,
+    -0.41921689f,
+    -0.42200027f,
+    -0.42477968f,
+    -0.42755509f,
+    -0.43032648f,
+    -0.43309382f,
+    -0.43585708f,
+    -0.43861624f,
+    -0.44137127f,
+    -0.44412214f,
+    -0.44686884f,
+    -0.44961133f,
+    -0.45234959f,
+    -0.45508359f,
+    -0.45781330f,
+    -0.46053871f,
+    -0.46325978f,
+    -0.46597650f,
+    -0.46868882f,
+    -0.47139674f,
+    -0.47410021f,
+    -0.47679923f,
+    -0.47949376f,
+    -0.48218377f,
+    -0.48486925f,
+    -0.48755016f,
+    -0.49022648f,
+    -0.49289819f,
+    -0.49556526f,
+    -0.49822767f,
+    -0.50088538f,
+    -0.50353838f,
+    -0.50618665f,
+    -0.50883014f,
+    -0.51146885f,
+    -0.51410274f,
+    -0.51673180f,
+    -0.51935599f,
+    -0.52197529f,
+    -0.52458968f,
+    -0.52719913f,
+    -0.52980362f,
+    -0.53240313f,
+    -0.53499762f,
+    -0.53758708f,
+    -0.54017147f,
+    -0.54275078f,
+    -0.54532499f,
+    -0.54789406f,
+    -0.55045797f,
+    -0.55301671f,
+    -0.55557023f,
+    -0.55811853f,
+    -0.56066158f,
+    -0.56319934f,
+    -0.56573181f,
+    -0.56825895f,
+    -0.57078075f,
+    -0.57329717f,
+    -0.57580819f,
+    -0.57831380f,
+    -0.58081396f,
+    -0.58330865f,
+    -0.58579786f,
+    -0.58828155f,
+    -0.59075970f,
+    -0.59323230f,
+    -0.59569930f,
+    -0.59816071f,
+    -0.60061648f,
+    -0.60306660f,
+    -0.60551104f,
+    -0.60794978f,
+    -0.61038281f,
+    -0.61281008f,
+    -0.61523159f,
+    -0.61764731f,
+    -0.62005721f,
+    -0.62246128f,
+    -0.62485949f,
+    -0.62725182f,
+    -0.62963824f,
+    -0.63201874f,
+    -0.63439328f,
+    -0.63676186f,
+    -0.63912444f,
+    -0.64148101f,
+    -0.64383154f,
+    -0.64617601f,
+    -0.64851440f,
+    -0.65084668f,
+    -0.65317284f,
+    -0.65549285f,
+    -0.65780669f,
+    -0.66011434f,
+    -0.66241578f,
+    -0.66471098f,
+    -0.66699992f,
+    -0.66928259f,
+    -0.67155895f,
+    -0.67382900f,
+    -0.67609270f,
+    -0.67835004f,
+    -0.68060100f,
+    -0.68284555f,
+    -0.68508367f,
+    -0.68731534f,
+    -0.68954054f,
+    -0.69175926f,
+    -0.69397146f,
+    -0.69617713f,
+    -0.69837625f,
+    -0.70056879f,
+    -0.70275474f,
+    -0.70493408f,
+    -0.70710678f,
+    -0.70927283f,
+    -0.71143220f,
+    -0.71358487f,
+    -0.71573083f,
+    -0.71787005f,
+    -0.72000251f,
+    -0.72212819f,
+    -0.72424708f,
+    -0.72635916f,
+    -0.72846439f,
+    -0.73056277f,
+    -0.73265427f,
+    -0.73473888f,
+    -0.73681657f,
+    -0.73888732f,
+    -0.74095113f,
+    -0.74300795f,
+    -0.74505779f,
+    -0.74710061f,
+    -0.74913639f,
+    -0.75116513f,
+    -0.75318680f,
+    -0.75520138f,
+    -0.75720885f,
+    -0.75920919f,
+    -0.76120239f,
+    -0.76318842f,
+    -0.76516727f,
+    -0.76713891f,
+    -0.76910334f,
+    -0.77106052f,
+    -0.77301045f,
+    -0.77495311f,
+    -0.77688847f,
+    -0.77881651f,
+    -0.78073723f,
+    -0.78265060f,
+    -0.78455660f,
+    -0.78645521f,
+    -0.78834643f,
+    -0.79023022f,
+    -0.79210658f,
+    -0.79397548f,
+    -0.79583690f,
+    -0.79769084f,
+    -0.79953727f,
+    -0.80137617f,
+    -0.80320753f,
+    -0.80503133f,
+    -0.80684755f,
+    -0.80865618f,
+    -0.81045720f,
+    -0.81225059f,
+    -0.81403633f,
+    -0.81581441f,
+    -0.81758481f,
+    -0.81934752f,
+    -0.82110251f,
+    -0.82284978f,
+    -0.82458930f,
+    -0.82632106f,
+    -0.82804505f,
+    -0.82976123f,
+    -0.83146961f,
+    -0.83317016f,
+    -0.83486287f,
+    -0.83654773f,
+    -0.83822471f,
+    -0.83989379f,
+    -0.84155498f,
+    -0.84320824f,
+    -0.84485357f,
+    -0.84649094f,
+    -0.84812034f,
+    -0.84974177f,
+    -0.85135519f,
+    -0.85296060f,
+    -0.85455799f,
+    -0.85614733f,
+    -0.85772861f,
+    -0.85930182f,
+    -0.86086694f,
+    -0.86242396f,
+    -0.86397286f,
+    -0.86551362f,
+    -0.86704625f,
+    -0.86857071f,
+    -0.87008699f,
+    -0.87159509f,
+    -0.87309498f,
+    -0.87458665f,
+    -0.87607009f,
+    -0.87754529f,
+    -0.87901223f,
+    -0.88047089f,
+    -0.88192126f,
+    -0.88336334f,
+    -0.88479710f,
+    -0.88622253f,
+    -0.88763962f,
+    -0.88904836f,
+    -0.89044872f,
+    -0.89184071f,
+    -0.89322430f,
+    -0.89459949f,
+    -0.89596625f,
+    -0.89732458f,
+    -0.89867447f,
+    -0.90001589f,
+    -0.90134885f,
+    -0.90267332f,
+    -0.90398929f,
+    -0.90529676f,
+    -0.90659570f,
+    -0.90788612f,
+    -0.90916798f,
+    -0.91044129f,
+    -0.91170603f,
+    -0.91296219f,
+    -0.91420976f,
+    -0.91544872f,
+    -0.91667906f,
+    -0.91790078f,
+    -0.91911385f,
+    -0.92031828f,
+    -0.92151404f,
+    -0.92270113f,
+    -0.92387953f,
+    -0.92504924f,
+    -0.92621024f,
+    -0.92736253f,
+    -0.92850608f,
+    -0.92964090f,
+    -0.93076696f,
+    -0.93188427f,
+    -0.93299280f,
+    -0.93409255f,
+    -0.93518351f,
+    -0.93626567f,
+    -0.93733901f,
+    -0.93840353f,
+    -0.93945922f,
+    -0.94050607f,
+    -0.94154407f,
+    -0.94257320f,
+    -0.94359346f,
+    -0.94460484f,
+    -0.94560733f,
+    -0.94660091f,
+    -0.94758559f,
+    -0.94856135f,
+    -0.94952818f,
+    -0.95048607f,
+    -0.95143502f,
+    -0.95237501f,
+    -0.95330604f,
+    -0.95422810f,
+    -0.95514117f,
+    -0.95604525f,
+    -0.95694034f,
+    -0.95782641f,
+    -0.95870347f,
+    -0.95957151f,
+    -0.96043052f,
+    -0.96128049f,
+    -0.96212140f,
+    -0.96295327f,
+    -0.96377607f,
+    -0.96458979f,
+    -0.96539444f,
+    -0.96619000f,
+    -0.96697647f,
+    -0.96775384f,
+    -0.96852209f,
+    -0.96928124f,
+    -0.97003125f,
+    -0.97077214f,
+    -0.97150389f,
+    -0.97222650f,
+    -0.97293995f,
+    -0.97364425f,
+    -0.97433938f,
+    -0.97502535f,
+    -0.97570213f,
+    -0.97636973f,
+    -0.97702814f,
+    -0.97767736f,
+    -0.97831737f,
+    -0.97894818f,
+    -0.97956977f,
+    -0.98018214f,
+    -0.98078528f,
+    -0.98137919f,
+    -0.98196387f,
+    -0.98253930f,
+    -0.98310549f,
+    -0.98366242f,
+    -0.98421009f,
+    -0.98474850f,
+    -0.98527764f,
+    -0.98579751f,
+    -0.98630810f,
+    -0.98680940f,
+    -0.98730142f,
+    -0.98778414f,
+    -0.98825757f,
+    -0.98872169f,
+    -0.98917651f,
+    -0.98962202f,
+    -0.99005821f,
+    -0.99048508f,
+    -0.99090264f,
+    -0.99131086f,
+    -0.99170975f,
+    -0.99209931f,
+    -0.99247953f,
+    -0.99285041f,
+    -0.99321195f,
+    -0.99356414f,
+    -0.99390697f,
+    -0.99424045f,
+    -0.99456457f,
+    -0.99487933f,
+    -0.99518473f,
+    -0.99548076f,
+    -0.99576741f,
+    -0.99604470f,
+    -0.99631261f,
+    -0.99657115f,
+    -0.99682030f,
+    -0.99706007f,
+    -0.99729046f,
+    -0.99751146f,
+    -0.99772307f,
+    -0.99792529f,
+    -0.99811811f,
+    -0.99830154f,
+    -0.99847558f,
+    -0.99864022f,
+    -0.99879546f,
+    -0.99894129f,
+    -0.99907773f,
+    -0.99920476f,
+    -0.99932238f,
+    -0.99943060f,
+    -0.99952942f,
+    -0.99961882f,
+    -0.99969882f,
+    -0.99976941f,
+    -0.99983058f,
+    -0.99988235f,
+    -0.99992470f,
+    -0.99995764f,
+    -0.99998118f,
+    -0.99999529f,
+    -1.00000000f,
+    -0.99999529f,
+    -0.99998118f,
+    -0.99995764f,
+    -0.99992470f,
+    -0.99988235f,
+    -0.99983058f,
+    -0.99976941f,
+    -0.99969882f,
+    -0.99961882f,
+    -0.99952942f,
+    -0.99943060f,
+    -0.99932238f,
+    -0.99920476f,
+    -0.99907773f,
+    -0.99894129f,
+    -0.99879546f,
+    -0.99864022f,
+    -0.99847558f,
+    -0.99830154f,
+    -0.99811811f,
+    -0.99792529f,
+    -0.99772307f,
+    -0.99751146f,
+    -0.99729046f,
+    -0.99706007f,
+    -0.99682030f,
+    -0.99657115f,
+    -0.99631261f,
+    -0.99604470f,
+    -0.99576741f,
+    -0.99548076f,
+    -0.99518473f,
+    -0.99487933f,
+    -0.99456457f,
+    -0.99424045f,
+    -0.99390697f,
+    -0.99356414f,
+    -0.99321195f,
+    -0.99285041f,
+    -0.99247953f,
+    -0.99209931f,
+    -0.99170975f,
+    -0.99131086f,
+    -0.99090264f,
+    -0.99048508f,
+    -0.99005821f,
+    -0.98962202f,
+    -0.98917651f,
+    -0.98872169f,
+    -0.98825757f,
+    -0.98778414f,
+    -0.98730142f,
+    -0.98680940f,
+    -0.98630810f,
+    -0.98579751f,
+    -0.98527764f,
+    -0.98474850f,
+    -0.98421009f,
+    -0.98366242f,
+    -0.98310549f,
+    -0.98253930f,
+    -0.98196387f,
+    -0.98137919f,
+    -0.98078528f,
+    -0.98018214f,
+    -0.97956977f,
+    -0.97894818f,
+    -0.97831737f,
+    -0.97767736f,
+    -0.97702814f,
+    -0.97636973f,
+    -0.97570213f,
+    -0.97502535f,
+    -0.97433938f,
+    -0.97364425f,
+    -0.97293995f,
+    -0.97222650f,
+    -0.97150389f,
+    -0.97077214f,
+    -0.97003125f,
+    -0.96928124f,
+    -0.96852209f,
+    -0.96775384f,
+    -0.96697647f,
+    -0.96619000f,
+    -0.96539444f,
+    -0.96458979f,
+    -0.96377607f,
+    -0.96295327f,
+    -0.96212140f,
+    -0.96128049f,
+    -0.96043052f,
+    -0.95957151f,
+    -0.95870347f,
+    -0.95782641f,
+    -0.95694034f,
+    -0.95604525f,
+    -0.95514117f,
+    -0.95422810f,
+    -0.95330604f,
+    -0.95237501f,
+    -0.95143502f,
+    -0.95048607f,
+    -0.94952818f,
+    -0.94856135f,
+    -0.94758559f,
+    -0.94660091f,
+    -0.94560733f,
+    -0.94460484f,
+    -0.94359346f,
+    -0.94257320f,
+    -0.94154407f,
+    -0.94050607f,
+    -0.93945922f,
+    -0.93840353f,
+    -0.93733901f,
+    -0.93626567f,
+    -0.93518351f,
+    -0.93409255f,
+    -0.93299280f,
+    -0.93188427f,
+    -0.93076696f,
+    -0.92964090f,
+    -0.92850608f,
+    -0.92736253f,
+    -0.92621024f,
+    -0.92504924f,
+    -0.92387953f,
+    -0.92270113f,
+    -0.92151404f,
+    -0.92031828f,
+    -0.91911385f,
+    -0.91790078f,
+    -0.91667906f,
+    -0.91544872f,
+    -0.91420976f,
+    -0.91296219f,
+    -0.91170603f,
+    -0.91044129f,
+    -0.90916798f,
+    -0.90788612f,
+    -0.90659570f,
+    -0.90529676f,
+    -0.90398929f,
+    -0.90267332f,
+    -0.90134885f,
+    -0.90001589f,
+    -0.89867447f,
+    -0.89732458f,
+    -0.89596625f,
+    -0.89459949f,
+    -0.89322430f,
+    -0.89184071f,
+    -0.89044872f,
+    -0.88904836f,
+    -0.88763962f,
+    -0.88622253f,
+    -0.88479710f,
+    -0.88336334f,
+    -0.88192126f,
+    -0.88047089f,
+    -0.87901223f,
+    -0.87754529f,
+    -0.87607009f,
+    -0.87458665f,
+    -0.87309498f,
+    -0.87159509f,
+    -0.87008699f,
+    -0.86857071f,
+    -0.86704625f,
+    -0.86551362f,
+    -0.86397286f,
+    -0.86242396f,
+    -0.86086694f,
+    -0.85930182f,
+    -0.85772861f,
+    -0.85614733f,
+    -0.85455799f,
+    -0.85296060f,
+    -0.85135519f,
+    -0.84974177f,
+    -0.84812034f,
+    -0.84649094f,
+    -0.84485357f,
+    -0.84320824f,
+    -0.84155498f,
+    -0.83989379f,
+    -0.83822471f,
+    -0.83654773f,
+    -0.83486287f,
+    -0.83317016f,
+    -0.83146961f,
+    -0.82976123f,
+    -0.82804505f,
+    -0.82632106f,
+    -0.82458930f,
+    -0.82284978f,
+    -0.82110251f,
+    -0.81934752f,
+    -0.81758481f,
+    -0.81581441f,
+    -0.81403633f,
+    -0.81225059f,
+    -0.81045720f,
+    -0.80865618f,
+    -0.80684755f,
+    -0.80503133f,
+    -0.80320753f,
+    -0.80137617f,
+    -0.79953727f,
+    -0.79769084f,
+    -0.79583690f,
+    -0.79397548f,
+    -0.79210658f,
+    -0.79023022f,
+    -0.78834643f,
+    -0.78645521f,
+    -0.78455660f,
+    -0.78265060f,
+    -0.78073723f,
+    -0.77881651f,
+    -0.77688847f,
+    -0.77495311f,
+    -0.77301045f,
+    -0.77106052f,
+    -0.76910334f,
+    -0.76713891f,
+    -0.76516727f,
+    -0.76318842f,
+    -0.76120239f,
+    -0.75920919f,
+    -0.75720885f,
+    -0.75520138f,
+    -0.75318680f,
+    -0.75116513f,
+    -0.74913639f,
+    -0.74710061f,
+    -0.74505779f,
+    -0.74300795f,
+    -0.74095113f,
+    -0.73888732f,
+    -0.73681657f,
+    -0.73473888f,
+    -0.73265427f,
+    -0.73056277f,
+    -0.72846439f,
+    -0.72635916f,
+    -0.72424708f,
+    -0.72212819f,
+    -0.72000251f,
+    -0.71787005f,
+    -0.71573083f,
+    -0.71358487f,
+    -0.71143220f,
+    -0.70927283f,
+    -0.70710678f,
+    -0.70493408f,
+    -0.70275474f,
+    -0.70056879f,
+    -0.69837625f,
+    -0.69617713f,
+    -0.69397146f,
+    -0.69175926f,
+    -0.68954054f,
+    -0.68731534f,
+    -0.68508367f,
+    -0.68284555f,
+    -0.68060100f,
+    -0.67835004f,
+    -0.67609270f,
+    -0.67382900f,
+    -0.67155895f,
+    -0.66928259f,
+    -0.66699992f,
+    -0.66471098f,
+    -0.66241578f,
+    -0.66011434f,
+    -0.65780669f,
+    -0.65549285f,
+    -0.65317284f,
+    -0.65084668f,
+    -0.64851440f,
+    -0.64617601f,
+    -0.64383154f,
+    -0.64148101f,
+    -0.63912444f,
+    -0.63676186f,
+    -0.63439328f,
+    -0.63201874f,
+    -0.62963824f,
+    -0.62725182f,
+    -0.62485949f,
+    -0.62246128f,
+    -0.62005721f,
+    -0.61764731f,
+    -0.61523159f,
+    -0.61281008f,
+    -0.61038281f,
+    -0.60794978f,
+    -0.60551104f,
+    -0.60306660f,
+    -0.60061648f,
+    -0.59816071f,
+    -0.59569930f,
+    -0.59323230f,
+    -0.59075970f,
+    -0.58828155f,
+    -0.58579786f,
+    -0.58330865f,
+    -0.58081396f,
+    -0.57831380f,
+    -0.57580819f,
+    -0.57329717f,
+    -0.57078075f,
+    -0.56825895f,
+    -0.56573181f,
+    -0.56319934f,
+    -0.56066158f,
+    -0.55811853f,
+    -0.55557023f,
+    -0.55301671f,
+    -0.55045797f,
+    -0.54789406f,
+    -0.54532499f,
+    -0.54275078f,
+    -0.54017147f,
+    -0.53758708f,
+    -0.53499762f,
+    -0.53240313f,
+    -0.52980362f,
+    -0.52719913f,
+    -0.52458968f,
+    -0.52197529f,
+    -0.51935599f,
+    -0.51673180f,
+    -0.51410274f,
+    -0.51146885f,
+    -0.50883014f,
+    -0.50618665f,
+    -0.50353838f,
+    -0.50088538f,
+    -0.49822767f,
+    -0.49556526f,
+    -0.49289819f,
+    -0.49022648f,
+    -0.48755016f,
+    -0.48486925f,
+    -0.48218377f,
+    -0.47949376f,
+    -0.47679923f,
+    -0.47410021f,
+    -0.47139674f,
+    -0.46868882f,
+    -0.46597650f,
+    -0.46325978f,
+    -0.46053871f,
+    -0.45781330f,
+    -0.45508359f,
+    -0.45234959f,
+    -0.44961133f,
+    -0.44686884f,
+    -0.44412214f,
+    -0.44137127f,
+    -0.43861624f,
+    -0.43585708f,
+    -0.43309382f,
+    -0.43032648f,
+    -0.42755509f,
+    -0.42477968f,
+    -0.42200027f,
+    -0.41921689f,
+    -0.41642956f,
+    -0.41363831f,
+    -0.41084317f,
+    -0.40804416f,
+    -0.40524131f,
+    -0.40243465f,
+    -0.39962420f,
+    -0.39680999f,
+    -0.39399204f,
+    -0.39117038f,
+    -0.38834505f,
+    -0.38551605f,
+    -0.38268343f,
+    -0.37984721f,
+    -0.37700741f,
+    -0.37416406f,
+    -0.37131719f,
+    -0.36846683f,
+    -0.36561300f,
+    -0.36275572f,
+    -0.35989504f,
+    -0.35703096f,
+    -0.35416353f,
+    -0.35129276f,
+    -0.34841868f,
+    -0.34554132f,
+    -0.34266072f,
+    -0.33977688f,
+    -0.33688985f,
+    -0.33399965f,
+    -0.33110631f,
+    -0.32820984f,
+    -0.32531029f,
+    -0.32240768f,
+    -0.31950203f,
+    -0.31659338f,
+    -0.31368174f,
+    -0.31076715f,
+    -0.30784964f,
+    -0.30492923f,
+    -0.30200595f,
+    -0.29907983f,
+    -0.29615089f,
+    -0.29321916f,
+    -0.29028468f,
+    -0.28734746f,
+    -0.28440754f,
+    -0.28146494f,
+    -0.27851969f,
+    -0.27557182f,
+    -0.27262136f,
+    -0.26966833f,
+    -0.26671276f,
+    -0.26375468f,
+    -0.26079412f,
+    -0.25783110f,
+    -0.25486566f,
+    -0.25189782f,
+    -0.24892761f,
+    -0.24595505f,
+    -0.24298018f,
+    -0.24000302f,
+    -0.23702361f,
+    -0.23404196f,
+    -0.23105811f,
+    -0.22807208f,
+    -0.22508391f,
+    -0.22209362f,
+    -0.21910124f,
+    -0.21610680f,
+    -0.21311032f,
+    -0.21011184f,
+    -0.20711138f,
+    -0.20410897f,
+    -0.20110463f,
+    -0.19809841f,
+    -0.19509032f,
+    -0.19208040f,
+    -0.18906866f,
+    -0.18605515f,
+    -0.18303989f,
+    -0.18002290f,
+    -0.17700422f,
+    -0.17398387f,
+    -0.17096189f,
+    -0.16793829f,
+    -0.16491312f,
+    -0.16188639f,
+    -0.15885814f,
+    -0.15582840f,
+    -0.15279719f,
+    -0.14976453f,
+    -0.14673047f,
+    -0.14369503f,
+    -0.14065824f,
+    -0.13762012f,
+    -0.13458071f,
+    -0.13154003f,
+    -0.12849811f,
+    -0.12545498f,
+    -0.12241068f,
+    -0.11936521f,
+    -0.11631863f,
+    -0.11327095f,
+    -0.11022221f,
+    -0.10717242f,
+    -0.10412163f,
+    -0.10106986f,
+    -0.09801714f,
+    -0.09496350f,
+    -0.09190896f,
+    -0.08885355f,
+    -0.08579731f,
+    -0.08274026f,
+    -0.07968244f,
+    -0.07662386f,
+    -0.07356456f,
+    -0.07050457f,
+    -0.06744392f,
+    -0.06438263f,
+    -0.06132074f,
+    -0.05825826f,
+    -0.05519524f,
+    -0.05213170f,
+    -0.04906767f,
+    -0.04600318f,
+    -0.04293826f,
+    -0.03987293f,
+    -0.03680722f,
+    -0.03374117f,
+    -0.03067480f,
+    -0.02760815f,
+    -0.02454123f,
+    -0.02147408f,
+    -0.01840673f,
+    -0.01533921f,
+    -0.01227154f,
+    -0.00920375f,
+    -0.00613588f,
+    -0.00306796f
+};
+
+int32_t dds_phase_ratef(float frequency)
+{
+    return (int32_t) (frequency*65536.0f*65536.0f/SAMPLE_RATE);
+}
+/*- End of function --------------------------------------------------------*/
+
+float dds_frequencyf(int32_t phase_rate)
+{
+    return (float) phase_rate*(float) SAMPLE_RATE/(65536.0f*65536.0f);
+}
+/*- End of function --------------------------------------------------------*/
+
+float dds_scaling_dbm0f(float level)
+{
+    return powf(10.0f, (level - DBM0_MAX_SINE_POWER)/20.0f)*32767.0f;
+}
+/*- End of function --------------------------------------------------------*/
+
+float dds_scaling_dbovf(float level)
+{
+    return powf(10.0f, (level - DBOV_MAX_SINE_POWER)/20.0f)*32767.0f;
+}
+/*- End of function --------------------------------------------------------*/
+
+void dds_advancef(uint32_t *phase_acc, int32_t phase_rate)
+{
+    *phase_acc += phase_rate;
+}
+/*- End of function --------------------------------------------------------*/
+
+float ddsf(uint32_t *phase_acc, int32_t phase_rate)
+{
+    float amp;
+
+    amp = sine_table[*phase_acc >> (32 - SLENK)];
+    *phase_acc += phase_rate;
+    return amp;
+}
+/*- End of function --------------------------------------------------------*/
+
+float dds_lookupf(uint32_t phase)
+{
+    return sine_table[phase >> (32 - SLENK)];
+}
+/*- End of function --------------------------------------------------------*/
+
+float dds_modf(uint32_t *phase_acc, int32_t phase_rate, float scale, int32_t phase)
+{
+    float amp;
+
+    amp = sine_table[*(phase_acc + phase) >> (32 - SLENK)]*scale;
+    *phase_acc += phase_rate;
+    return amp;
+}
+/*- End of function --------------------------------------------------------*/
+
+complexf_t dds_complexf(uint32_t *phase_acc, int32_t phase_rate)
+{
+    complexf_t amp;
+
+    amp = complex_setf(sine_table[(*phase_acc + (1 << 30)) >> (32 - SLENK)],
+                       sine_table[*phase_acc >> (32 - SLENK)]);
+    *phase_acc += phase_rate;
+    return amp;
+}
+/*- End of function --------------------------------------------------------*/
+
+complexf_t dds_lookup_complexf(uint32_t phase)
+{
+    return complex_setf(sine_table[(phase + (1 << 30)) >> (32 - SLENK)],
+                        sine_table[phase >> (32 - SLENK)]);
+}
+/*- End of function --------------------------------------------------------*/
+
+complexf_t dds_complex_modf(uint32_t *phase_acc, int32_t phase_rate, float scale, int32_t phase)
+{
+    complexf_t amp;
+
+    amp = complex_setf(sine_table[(*phase_acc + phase + (1 << 30)) >> (32 - SLENK)]*scale,
+                       sine_table[(*phase_acc + phase) >> (32 - SLENK)]*scale);
+    *phase_acc += phase_rate;
+    return amp;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/dds_int.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/dds_int.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,291 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * dds.c
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2003 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: dds_int.c,v 1.9 2008/07/02 14:48:25 steveu Exp $
+ */
+
+/*! \file */
+
+#if defined(HAVE_CONFIG_H)
+#include <config.h>
+#endif
+
+#include <inttypes.h>
+#include "floating_fudge.h"
+#if defined(HAVE_TGMATH_H)
+#include <tgmath.h>
+#endif
+#if defined(HAVE_MATH_H)
+#include <math.h>
+#endif
+
+#include "spandsp/telephony.h"
+#include "spandsp/complex.h"
+#include "spandsp/dds.h"
+
+#if !defined(M_PI)
+# define M_PI           3.14159265358979323846  /* pi */
+#endif
+
+/* In a A-law or u-law channel, a 128 step sine table is adequate to keep the spectral
+   mess due to the DDS at a similar level to the spectral mess due to the A-law or u-law
+   compression. */
+#define SLENK       7
+#define DDS_STEPS   (1 << SLENK)
+#define DDS_SHIFT   (32 - 2 - SLENK)
+
+/* This is a simple set of direct digital synthesis (DDS) functions to generate sine
+   waves. This version uses a 128 entry sin/cos table to cover one quadrant. */
+
+static const int16_t sine_table[DDS_STEPS] =
+{
+       201,
+       603,
+      1005,
+      1407,
+      1809,
+      2210,
+      2611,
+      3012,
+      3412,
+      3812,
+      4211,
+      4609,
+      5007,
+      5404,
+      5800,
+      6195,
+      6590,
+      6983,
+      7376,
+      7767,
+      8157,
+      8546,
+      8933,
+      9319,
+      9704,
+     10088,
+     10469,
+     10850,
+     11228,
+     11605,
+     11980,
+     12354,
+     12725,
+     13095,
+     13463,
+     13828,
+     14192,
+     14553,
+     14912,
+     15269,
+     15624,
+     15976,
+     16326,
+     16673,
+     17018,
+     17361,
+     17700,
+     18037,
+     18372,
+     18703,
+     19032,
+     19358,
+     19681,
+     20001,
+     20318,
+     20632,
+     20943,
+     21251,
+     21555,
+     21856,
+     22154,
+     22449,
+     22740,
+     23028,
+     23312,
+     23593,
+     23870,
+     24144,
+     24414,
+     24680,
+     24943,
+     25202,
+     25457,
+     25708,
+     25956,
+     26199,
+     26439,
+     26674,
+     26906,
+     27133,
+     27357,
+     27576,
+     27791,
+     28002,
+     28209,
+     28411,
+     28610,
+     28803,
+     28993,
+     29178,
+     29359,
+     29535,
+     29707,
+     29875,
+     30038,
+     30196,
+     30350,
+     30499,
+     30644,
+     30784,
+     30920,
+     31050,
+     31177,
+     31298,
+     31415,
+     31527,
+     31634,
+     31737,
+     31834,
+     31927,
+     32015,
+     32099,
+     32177,
+     32251,
+     32319,
+     32383,
+     32442,
+     32496,
+     32546,
+     32590,
+     32629,
+     32664,
+     32693,
+     32718,
+     32738,
+     32753,
+     32762,
+     32767,
+};
+
+int32_t dds_phase_rate(float frequency)
+{
+    return (int32_t) (frequency*65536.0f*65536.0f/SAMPLE_RATE);
+}
+/*- End of function --------------------------------------------------------*/
+
+float dds_frequency(int32_t phase_rate)
+{
+    return (float) phase_rate*(float) SAMPLE_RATE/(65536.0f*65536.0f);
+}
+/*- End of function --------------------------------------------------------*/
+
+int dds_scaling_dbm0(float level)
+{
+    return (int) (powf(10.0f, (level - DBM0_MAX_SINE_POWER)/20.0f)*32767.0f);
+}
+/*- End of function --------------------------------------------------------*/
+
+int dds_scaling_dbov(float level)
+{
+    return (int) (powf(10.0f, (level - DBOV_MAX_SINE_POWER)/20.0f)*32767.0f);
+}
+/*- End of function --------------------------------------------------------*/
+
+int16_t dds_lookup(uint32_t phase)
+{
+    uint32_t step;
+    int16_t amp;
+
+    phase >>= DDS_SHIFT;
+    step = phase & (DDS_STEPS - 1);
+    if ((phase & DDS_STEPS))
+        step = (DDS_STEPS - 1) - step;
+    amp = sine_table[step];
+    if ((phase & (2*DDS_STEPS)))
+    	amp = -amp;
+    return amp;
+}
+/*- End of function --------------------------------------------------------*/
+
+int16_t dds_offset(uint32_t phase_acc, int32_t phase_offset)
+{
+    return dds_lookup(phase_acc + phase_offset);
+}
+/*- End of function --------------------------------------------------------*/
+
+void dds_advance(uint32_t *phase_acc, int32_t phase_rate)
+{
+    *phase_acc += phase_rate;
+}
+/*- End of function --------------------------------------------------------*/
+
+int16_t dds(uint32_t *phase_acc, int32_t phase_rate)
+{
+    int16_t amp;
+
+    amp = dds_lookup(*phase_acc);
+    *phase_acc += phase_rate;
+    return amp;
+}
+/*- End of function --------------------------------------------------------*/
+
+int16_t dds_mod(uint32_t *phase_acc, int32_t phase_rate, int scale, int32_t phase)
+{
+    int16_t amp;
+
+    amp = (int16_t) ((dds_lookup(*phase_acc + phase)*scale) >> 15);
+    *phase_acc += phase_rate;
+    return amp;
+}
+/*- End of function --------------------------------------------------------*/
+
+complexi_t dds_lookup_complexi(uint32_t phase)
+{
+    return complex_seti(dds_lookup(phase + (1 << 30)), dds_lookup(phase));
+}
+/*- End of function --------------------------------------------------------*/
+
+complexi_t dds_complexi(uint32_t *phase_acc, int32_t phase_rate)
+{
+    complexi_t amp;
+
+    amp = complex_seti(dds_lookup(*phase_acc + (1 << 30)), dds_lookup(*phase_acc));
+    *phase_acc += phase_rate;
+    return amp;
+}
+/*- End of function --------------------------------------------------------*/
+
+complexi_t dds_complexi_mod(uint32_t *phase_acc, int32_t phase_rate, int scale, int32_t phase)
+{
+    complexi_t amp;
+
+    amp = complex_seti((dds_lookup(*phase_acc + phase + (1 << 30))*scale) >> 15,
+                       (dds_lookup(*phase_acc + phase)*scale) >> 15);
+    *phase_acc += phase_rate;
+    return amp;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/dtmf.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/dtmf.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,530 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * dtmf.c - DTMF generation and detection.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2001-2003, 2005, 2006 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: dtmf.c,v 1.43 2008/07/02 14:48:25 steveu Exp $
+ */
+ 
+/*! \file dtmf.h */
+
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
+
+#include <inttypes.h>
+#include <stdlib.h>
+#include "floating_fudge.h"
+#if defined(HAVE_TGMATH_H)
+#include <tgmath.h>
+#endif
+#if defined(HAVE_MATH_H)
+#include <math.h>
+#endif
+#include <string.h>
+#include <stdio.h>
+#include <time.h>
+#include <fcntl.h>
+
+#include "spandsp/telephony.h"
+#include "spandsp/queue.h"
+#include "spandsp/complex.h"
+#include "spandsp/dds.h"
+#include "spandsp/tone_detect.h"
+#include "spandsp/tone_generate.h"
+#include "spandsp/super_tone_rx.h"
+#include "spandsp/dtmf.h"
+
+#define DEFAULT_DTMF_TX_LEVEL       -10
+#define DEFAULT_DTMF_TX_ON_TIME     50
+#define DEFAULT_DTMF_TX_OFF_TIME    55
+
+#if defined(SPANDSP_USE_FIXED_POINT)
+#define DTMF_THRESHOLD              10438           /* -42dBm0 */
+#define DTMF_NORMAL_TWIST           6.309f          /* 8dB */
+#define DTMF_REVERSE_TWIST          2.512f          /* 4dB */
+#define DTMF_RELATIVE_PEAK_ROW      6.309f          /* 8dB */
+#define DTMF_RELATIVE_PEAK_COL      6.309f          /* 8dB */
+#define DTMF_TO_TOTAL_ENERGY        83.868f         /* -0.85dB */
+#define DTMF_POWER_OFFSET           68.251f         /* 10*log(256.0*256.0*DTMF_SAMPLES_PER_BLOCK) */
+#define DTMF_SAMPLES_PER_BLOCK      102
+#else
+#define DTMF_THRESHOLD              171032462.0f    /* -42dBm0 [((DTMF_SAMPLES_PER_BLOCK*32768.0/1.4142)*10^((-42 - DBM0_MAX_SINE_POWER)/20.0))^2 => 171032462.0] */
+#define DTMF_NORMAL_TWIST           6.309f          /* 8dB [10^(8/10) => 6.309] */
+#define DTMF_REVERSE_TWIST          2.512f          /* 4dB */
+#define DTMF_RELATIVE_PEAK_ROW      6.309f          /* 8dB */
+#define DTMF_RELATIVE_PEAK_COL      6.309f          /* 8dB */
+#define DTMF_TO_TOTAL_ENERGY        83.868f         /* -0.85dB [DTMF_SAMPLES_PER_BLOCK*10^(-0.85/10.0)] */
+#define DTMF_POWER_OFFSET           110.395f        /* 10*log(32768.0*32768.0*DTMF_SAMPLES_PER_BLOCK) */
+#define DTMF_SAMPLES_PER_BLOCK      102
+#endif
+
+static const float dtmf_row[] =
+{
+     697.0f,  770.0f,  852.0f,  941.0f
+};
+static const float dtmf_col[] =
+{
+    1209.0f, 1336.0f, 1477.0f, 1633.0f
+};
+
+static const char dtmf_positions[] = "123A" "456B" "789C" "*0#D";
+
+static goertzel_descriptor_t dtmf_detect_row[4];
+static goertzel_descriptor_t dtmf_detect_col[4];
+
+static int dtmf_tx_inited = FALSE;
+static tone_gen_descriptor_t dtmf_digit_tones[16];
+
+int dtmf_rx(dtmf_rx_state_t *s, const int16_t amp[], int samples)
+{
+#if defined(SPANDSP_USE_FIXED_POINT)
+    int32_t row_energy[4];
+    int32_t col_energy[4];
+    int16_t xamp;
+    float famp;
+#else
+    float row_energy[4];
+    float col_energy[4];
+    float xamp;
+    float famp;
+#endif
+    float v1;
+    int i;
+    int j;
+    int sample;
+    int best_row;
+    int best_col;
+    int limit;
+    uint8_t hit;
+
+    hit = 0;
+    for (sample = 0;  sample < samples;  sample = limit)
+    {
+        /* The block length is optimised to meet the DTMF specs. */
+        if ((samples - sample) >= (DTMF_SAMPLES_PER_BLOCK - s->current_sample))
+            limit = sample + (DTMF_SAMPLES_PER_BLOCK - s->current_sample);
+        else
+            limit = samples;
+        /* The following unrolled loop takes only 35% (rough estimate) of the 
+           time of a rolled loop on the machine on which it was developed */
+        for (j = sample;  j < limit;  j++)
+        {
+            xamp = amp[j];
+            if (s->filter_dialtone)
+            {
+                famp = xamp;
+                /* Sharp notches applied at 350Hz and 440Hz - the two common dialtone frequencies.
+                   These are rather high Q, to achieve the required narrowness, without using lots of
+                   sections. */
+                v1 = 0.98356f*famp + 1.8954426f*s->z350[0] - 0.9691396f*s->z350[1];
+                famp = v1 - 1.9251480f*s->z350[0] + s->z350[1];
+                s->z350[1] = s->z350[0];
+                s->z350[0] = v1;
+
+                v1 = 0.98456f*famp + 1.8529543f*s->z440[0] - 0.9691396f*s->z440[1];
+                famp = v1 - 1.8819938f*s->z440[0] + s->z440[1];
+                s->z440[1] = s->z440[0];
+                s->z440[0] = v1;
+                xamp = famp;
+            }
+            xamp = goertzel_preadjust_amp(xamp);
+#if defined(SPANDSP_USE_FIXED_POINT)
+            s->energy += ((int32_t) xamp*xamp);
+#else
+            s->energy += xamp*xamp;
+#endif
+            goertzel_samplex(&s->row_out[0], xamp);
+            goertzel_samplex(&s->col_out[0], xamp);
+            goertzel_samplex(&s->row_out[1], xamp);
+            goertzel_samplex(&s->col_out[1], xamp);
+            goertzel_samplex(&s->row_out[2], xamp);
+            goertzel_samplex(&s->col_out[2], xamp);
+            goertzel_samplex(&s->row_out[3], xamp);
+            goertzel_samplex(&s->col_out[3], xamp);
+        }
+        s->current_sample += (limit - sample);
+        if (s->current_sample < DTMF_SAMPLES_PER_BLOCK)
+            continue;
+
+        /* We are at the end of a DTMF detection block */
+        /* Find the peak row and the peak column */
+        row_energy[0] = goertzel_result(&s->row_out[0]);
+        best_row = 0;
+        col_energy[0] = goertzel_result(&s->col_out[0]);
+        best_col = 0;
+        for (i = 1;  i < 4;  i++)
+        {
+            row_energy[i] = goertzel_result(&s->row_out[i]);
+            if (row_energy[i] > row_energy[best_row])
+                best_row = i;
+            col_energy[i] = goertzel_result(&s->col_out[i]);
+            if (col_energy[i] > col_energy[best_col])
+                best_col = i;
+        }
+        hit = 0;
+        /* Basic signal level test and the twist test */
+        if (row_energy[best_row] >= s->threshold
+            &&
+            col_energy[best_col] >= s->threshold
+            &&
+            col_energy[best_col] < row_energy[best_row]*s->reverse_twist
+            &&
+            col_energy[best_col]*s->normal_twist > row_energy[best_row])
+        {
+            /* Relative peak test ... */
+            for (i = 0;  i < 4;  i++)
+            {
+                if ((i != best_col  &&  col_energy[i]*DTMF_RELATIVE_PEAK_COL > col_energy[best_col])
+                    ||
+                    (i != best_row  &&  row_energy[i]*DTMF_RELATIVE_PEAK_ROW > row_energy[best_row]))
+                {
+                    break;
+                }
+            }
+            /* ... and fraction of total energy test */
+            if (i >= 4
+                &&
+                (row_energy[best_row] + col_energy[best_col]) > DTMF_TO_TOTAL_ENERGY*s->energy)
+            {
+                /* Got a hit */
+                hit = dtmf_positions[(best_row << 2) + best_col];
+            }
+        }
+        /* The logic in the next test should ensure the following for different successive hit patterns:
+                -----ABB = start of digit B.
+                ----B-BB = start of digit B
+                ----A-BB = start of digit B
+                BBBBBABB = still in digit B.
+                BBBBBB-- = end of digit B
+                BBBBBBC- = end of digit B
+                BBBBACBB = B ends, then B starts again.
+                BBBBBBCC = B ends, then C starts.
+                BBBBBCDD = B ends, then D starts.
+           This can work with:
+                - Back to back differing digits. Back-to-back digits should
+                  not happen. The spec. says there should be a gap between digits.
+                  However, many real phones do not impose a gap, and rolling across
+                  the keypad can produce little or no gap.
+                - It tolerates nasty phones that give a very wobbly start to a digit.
+                - VoIP can give sample slips. The phase jumps that produces will cause
+                  the block it is in to give no detection. This logic will ride over a
+                  single missed block, and not falsely declare a second digit. If the
+                  hiccup happens in the wrong place on a minimum length digit, however
+                  we would still fail to detect that digit. Could anything be done to
+                  deal with that? Packet loss is clearly a no-go zone.
+                  Note this is only relevant to VoIP using A-law, u-law or similar.
+                  Low bit rate codecs scramble DTMF too much for it to be recognised,
+                  and often slip in units larger than a sample. */
+        if (hit != s->in_digit)
+        {
+            if (s->last_hit != s->in_digit)
+            {
+                /* We have two successive indications that something has changed. */
+                /* To declare digit on, the hits must agree. Otherwise we declare tone off. */
+                hit = (hit  &&  hit == s->last_hit)  ?  hit   :  0;
+                if (s->realtime_callback)
+                {
+                    /* Avoid reporting multiple no digit conditions on flaky hits */
+                    if (s->in_digit  ||  hit)
+                    {
+                        i = (s->in_digit  &&  !hit)  ?  -99  :  lrintf(log10f(s->energy)*10.0f - DTMF_POWER_OFFSET + DBM0_MAX_POWER);
+                        s->realtime_callback(s->realtime_callback_data, hit, i, 0);
+                    }
+                }
+                else
+                {
+                    if (hit)
+                    {
+                        if (s->current_digits < MAX_DTMF_DIGITS)
+                        {
+                            s->digits[s->current_digits++] = (char) hit;
+                            s->digits[s->current_digits] = '\0';
+                            if (s->digits_callback)
+                            {
+                                s->digits_callback(s->digits_callback_data, s->digits, s->current_digits);
+                                s->current_digits = 0;
+                            }
+                        }
+                        else
+                        {
+                            s->lost_digits++;
+                        }
+                    }
+                }
+                s->in_digit = hit;
+            }
+        }
+        s->last_hit = hit;
+#if defined(SPANDSP_USE_FIXED_POINT)
+        s->energy = 0;
+#else
+        s->energy = 0.0f;
+#endif
+        s->current_sample = 0;
+    }
+    if (s->current_digits  &&  s->digits_callback)
+    {
+        s->digits_callback(s->digits_callback_data, s->digits, s->current_digits);
+        s->digits[0] = '\0';
+        s->current_digits = 0;
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int dtmf_rx_status(dtmf_rx_state_t *s)
+{
+    if (s->in_digit)
+        return s->in_digit;
+    if (s->last_hit)
+        return 'x';
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+size_t dtmf_rx_get(dtmf_rx_state_t *s, char *buf, int max)
+{
+    if (max > s->current_digits)
+        max = s->current_digits;
+    if (max > 0)
+    {
+        memcpy(buf, s->digits, max);
+        memmove(s->digits, s->digits + max, s->current_digits - max);
+        s->current_digits -= max;
+    }
+    buf[max] = '\0';
+    return  max;
+}
+/*- End of function --------------------------------------------------------*/
+
+void dtmf_rx_set_realtime_callback(dtmf_rx_state_t *s,
+                                   tone_report_func_t callback,
+                                   void *user_data)
+{
+    s->realtime_callback = callback;
+    s->realtime_callback_data = user_data;
+}
+/*- End of function --------------------------------------------------------*/
+
+void dtmf_rx_parms(dtmf_rx_state_t *s,
+                   int filter_dialtone,
+                   int twist,
+                   int reverse_twist,
+                   int threshold)
+{
+    float x;
+
+    if (filter_dialtone >= 0)
+    {
+        s->z350[0] = 0.0f;
+        s->z350[1] = 0.0f;
+        s->z440[0] = 0.0f;
+        s->z440[1] = 0.0f;
+        s->filter_dialtone = filter_dialtone;
+    }
+    if (twist >= 0)
+        s->normal_twist = powf(10.0f, twist/10.0f);
+    if (reverse_twist >= 0)
+        s->reverse_twist = powf(10.0f, reverse_twist/10.0f);
+    if (threshold > -99)
+    {
+        x = (DTMF_SAMPLES_PER_BLOCK*32768.0f/1.4142f)*powf(10.0f, (threshold - DBM0_MAX_SINE_POWER)/20.0f);
+        s->threshold = x*x;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+dtmf_rx_state_t *dtmf_rx_init(dtmf_rx_state_t *s,
+                              digits_rx_callback_t callback,
+                              void *user_data)
+{
+    int i;
+    static int initialised = FALSE;
+
+    if (s == NULL)
+    {
+        if ((s = (dtmf_rx_state_t *) malloc(sizeof (*s))) == NULL)
+            return  NULL;
+    }
+    s->digits_callback = callback;
+    s->digits_callback_data = user_data;
+    s->realtime_callback = NULL;
+    s->realtime_callback_data = NULL;
+    s->filter_dialtone = FALSE;
+    s->normal_twist = DTMF_NORMAL_TWIST;
+    s->reverse_twist = DTMF_REVERSE_TWIST;
+    s->threshold = DTMF_THRESHOLD;
+
+    s->in_digit = 0;
+    s->last_hit = 0;
+
+    if (!initialised)
+    {
+        for (i = 0;  i < 4;  i++)
+        {
+            make_goertzel_descriptor(&dtmf_detect_row[i], dtmf_row[i], DTMF_SAMPLES_PER_BLOCK);
+            make_goertzel_descriptor(&dtmf_detect_col[i], dtmf_col[i], DTMF_SAMPLES_PER_BLOCK);
+        }
+        initialised = TRUE;
+    }
+    for (i = 0;  i < 4;  i++)
+    {
+        goertzel_init(&s->row_out[i], &dtmf_detect_row[i]);
+        goertzel_init(&s->col_out[i], &dtmf_detect_col[i]);
+    }
+#if defined(SPANDSP_USE_FIXED_POINT)
+    s->energy = 0;
+#else
+    s->energy = 0.0f;
+#endif
+    s->current_sample = 0;
+    s->lost_digits = 0;
+    s->current_digits = 0;
+    s->digits[0] = '\0';
+    return s;
+}
+/*- End of function --------------------------------------------------------*/
+
+int dtmf_rx_free(dtmf_rx_state_t *s)
+{
+    free(s);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void dtmf_tx_initialise(void)
+{
+    int row;
+    int col;
+
+    if (dtmf_tx_inited)
+        return;
+    for (row = 0;  row < 4;  row++)
+    {
+        for (col = 0;  col < 4;  col++)
+        {
+            make_tone_gen_descriptor(&dtmf_digit_tones[row*4 + col],
+                                     (int) dtmf_row[row],
+                                     DEFAULT_DTMF_TX_LEVEL,
+                                     (int) dtmf_col[col],
+                                     DEFAULT_DTMF_TX_LEVEL,
+                                     DEFAULT_DTMF_TX_ON_TIME,
+                                     DEFAULT_DTMF_TX_OFF_TIME,
+                                     0,
+                                     0,
+                                     FALSE);
+        }
+    }
+    dtmf_tx_inited = TRUE;
+}
+/*- End of function --------------------------------------------------------*/
+
+int dtmf_tx(dtmf_tx_state_t *s, int16_t amp[], int max_samples)
+{
+    int len;
+    const char *cp;
+    int digit;
+
+    len = 0;
+    if (s->tones.current_section >= 0)
+    {
+        /* Deal with the fragment left over from last time */
+        len = tone_gen(&(s->tones), amp, max_samples);
+    }
+    while (len < max_samples  &&  (digit = queue_read_byte(&s->queue.queue)) >= 0)
+    {
+        /* Step to the next digit */
+        if (digit == 0)
+            continue;
+        if ((cp = strchr(dtmf_positions, digit)) == NULL)
+            continue;
+        tone_gen_init(&(s->tones), &dtmf_digit_tones[cp - dtmf_positions]);
+        s->tones.tone[0].gain = s->low_level;
+        s->tones.tone[1].gain = s->high_level;
+        s->tones.duration[0] = s->on_time;
+        s->tones.duration[1] = s->off_time;
+        len += tone_gen(&(s->tones), amp + len, max_samples - len);
+    }
+    return len;
+}
+/*- End of function --------------------------------------------------------*/
+
+size_t dtmf_tx_put(dtmf_tx_state_t *s, const char *digits, int len)
+{
+    size_t space;
+
+    /* This returns the number of characters that would not fit in the buffer.
+       The buffer will only be loaded if the whole string of digits will fit,
+       in which case zero is returned. */
+    if (len < 0)
+    {
+        if ((len = strlen(digits)) == 0)
+            return 0;
+    }
+    if ((space = queue_free_space(&s->queue.queue)) < len)
+        return len - space;
+    if (queue_write(&s->queue.queue, (const uint8_t *) digits, len) >= 0)
+        return 0;
+    return -1;
+}
+/*- End of function --------------------------------------------------------*/
+
+void dtmf_tx_set_level(dtmf_tx_state_t *s, int level, int twist)
+{
+    s->low_level = dds_scaling_dbm0f((float) level);
+    s->high_level = dds_scaling_dbm0f((float) (level + twist));
+}
+/*- End of function --------------------------------------------------------*/
+
+void dtmf_tx_set_timing(dtmf_tx_state_t *s, int on_time, int off_time)
+{
+    s->on_time = ((on_time >= 0)  ?  on_time  :  DEFAULT_DTMF_TX_ON_TIME)*SAMPLE_RATE/1000;
+    s->off_time = ((off_time >= 0)  ?  off_time  :  DEFAULT_DTMF_TX_OFF_TIME)*SAMPLE_RATE/1000;
+}
+/*- End of function --------------------------------------------------------*/
+
+dtmf_tx_state_t *dtmf_tx_init(dtmf_tx_state_t *s)
+{
+    if (s == NULL)
+    {
+        if ((s = (dtmf_tx_state_t *) malloc(sizeof (*s))) == NULL)
+            return  NULL;
+    }
+    if (!dtmf_tx_inited)
+        dtmf_tx_initialise();
+    tone_gen_init(&(s->tones), &dtmf_digit_tones[0]);
+    dtmf_tx_set_level(s, DEFAULT_DTMF_TX_LEVEL, 0);
+    dtmf_tx_set_timing(s, -1, -1);
+    queue_init(&s->queue.queue, MAX_DTMF_DIGITS, QUEUE_READ_ATOMIC | QUEUE_WRITE_ATOMIC);
+    s->tones.current_section = -1;
+    return s;
+}
+/*- End of function --------------------------------------------------------*/
+
+int dtmf_tx_free(dtmf_tx_state_t *s)
+{
+    free(s);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/echo.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/echo.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,612 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * echo.c - An echo cancellor, suitable for electrical and acoustic
+ *          cancellation. This code does not currently comply with
+ *          any relevant standards (e.g. G.164/5/7/8). One day....
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2001, 2003 Steve Underwood
+ *
+ * Based on a bit from here, a bit from there, eye of toad,
+ * ear of bat, etc - plus, of course, my own 2 cents.
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: echo.c,v 1.27 2008/08/29 09:28:13 steveu Exp $
+ */
+
+/*! \file */
+
+/* TODO:
+   Finish the echo suppressor option, however nasty suppression may be.
+   Add an option to reintroduce side tone at -24dB under appropriate conditions.
+   Improve double talk detector (iterative!)
+*/
+
+/* We need to differentiate between transmitted energy which will train the echo
+   canceller well (voice, white noise, and other broadband sources) and energy
+   which will train it badly (supervisory tones, DTMF, whistles, and other
+   narrowband sources). There are many ways this might be done. This canceller uses
+   a method based on the autocorrelation qualities of the transmitted signal. A rather
+   peaky autocorrelation function is a clear sign of a narrowband signal. We only need
+   perform the autocorrelation at well spaced intervals, so the compute load is not too
+   great. Multiple successive autocorrelation functions with a similar peaky shape are a
+   clear indication of a stationary narrowband signal. Using TKEO, it should be possible to
+   greatly reduce the compute requirement for narrowband detection. */
+
+/* The FIR taps must be adapted as 32 bit values, to get the necessary finesse
+   in the adaption process. However, they are applied as 16 bit values (bits 30-15
+   of the 32 bit values) in the FIR. For the working 16 bit values, we need 4 sets.
+   
+   3 of the 16 bit sets are used on a rotating basis. Normally the canceller steps
+   round these 3 sets at regular intervals. Any time we detect double talk, we can go
+   back to the set from two steps ago with reasonable assurance it is a well adapted
+   set. We cannot just go back one step, as we may have rotated the sets just before
+   double talk or tone was detected, and that set may already be somewhat corrupted.
+   
+   When narrowband energy is detected we need to continue adapting to it, to echo
+   cancel it. However, the adaption will almost certainly be going astray. Broadband
+   (or even complex sequences of narrowband) energy will normally lead to a well
+   trained cancellor, with taps matching the impulse response of the channel.
+   For stationary narrowband energy, there is usually has an infinite number of
+   alternative tap sets which will cancel it well. A previously well trained set of
+   taps will tend to drift amongst the alternatives. When broadband energy resumes, the
+   taps may be a total mismatch for the signal, and could even amplify rather than
+   attenuate the echo. The solution is to use a fourth set of 16 bit taps. When we first
+   detect the narrowband energy we save the oldest of the group of three sets, but do
+   not change back to an older set. We let the canceller cancel, and it adaption drift
+   while the narrowband energy is present. When we detect the narrowband energy has ceased,
+   we switch to using the fourth set of taps which was saved.
+
+   When we revert to an older set of taps, we must replace both the 16 bit and 32 bit
+   working tap sets. The saved 16 bit values are good enough to also be used as a replacement
+   for the 32 bit values. We loose the fractions, but they should soon settle down in a
+   reasonable way. */
+
+#if defined(HAVE_CONFIG_H)
+#include <config.h>
+#endif
+
+#include <inttypes.h>
+#include <stdlib.h>
+#include "floating_fudge.h"
+#if defined(HAVE_TGMATH_H)
+#include <tgmath.h>
+#endif
+#if defined(HAVE_MATH_H)
+#include <math.h>
+#endif
+#include <string.h>
+#include <stdio.h>
+
+#include "spandsp/telephony.h"
+#include "spandsp/logging.h"
+#include "spandsp/dc_restore.h"
+#include "spandsp/bit_operations.h"
+#include "spandsp/echo.h"
+
+#if !defined(NULL)
+#define NULL (void *) 0
+#endif
+#if !defined(FALSE)
+#define FALSE 0
+#endif
+#if !defined(TRUE)
+#define TRUE (!FALSE)
+#endif
+
+#define NONUPDATE_DWELL_TIME        600     /* 600 samples, or 75ms */
+
+#define MIN_TX_POWER_FOR_ADAPTION   64*64
+#define MIN_RX_POWER_FOR_ADAPTION   64*64
+
+static int narrowband_detect(echo_can_state_t *ec)
+{
+    int k;
+    int i;
+    float temp;
+    float scale;
+    float sf[128];
+    float f_acf[128];
+    int32_t acf[28];
+    int score;
+    int len = 32;
+    int alen = 9;
+    
+    k = ec->curr_pos;
+    for (i = 0;  i < len;  i++)
+    {
+        sf[i] = ec->fir_state.history[k++];
+        if (k >= 256)
+            k = 0;
+    }
+    for (k = 0;  k < alen;  k++)
+    {
+        temp = 0;
+        for (i = k;  i < len;  i++)
+            temp += sf[i]*sf[i - k];
+        f_acf[k] = temp;
+    }
+    scale = 0x1FFFFFFF/f_acf[0];
+    for (k = 0;  k < alen;  k++)
+        acf[k] = (int32_t) (f_acf[k]*scale);
+    score = 0;
+    for (i = 0;  i < 9;  i++)
+    {
+        if (ec->last_acf[i] >= 0  &&  acf[i] >= 0)
+        {
+            if ((ec->last_acf[i] >> 1) < acf[i]  &&  acf[i] < (ec->last_acf[i] << 1))
+                score++;
+        }
+        else if (ec->last_acf[i] < 0  &&  acf[i] < 0)
+        {
+            if ((ec->last_acf[i] >> 1) > acf[i]  &&  acf[i] > (ec->last_acf[i] << 1))
+                score++;
+        }
+    }
+    memcpy(ec->last_acf, acf, alen*sizeof(ec->last_acf[0]));
+    return score;
+}
+
+static __inline__ void lms_adapt(echo_can_state_t *ec, int factor)
+{
+    int i;
+
+#if 0
+    mmx_t *mmx_taps;
+    mmx_t *mmx_coeffs;
+    mmx_t *mmx_hist;
+    mmx_t mmx;
+
+    mmx.w[0] =
+    mmx.w[1] =
+    mmx.w[2] =
+    mmx.w[3] = factor;
+    mmx_hist = (mmx_t *) &fir->history[fir->curr_pos];
+    mmx_taps = (mmx_t *) &fir->taps;
+    mmx_coeffs = (mmx_t *) fir->coeffs;
+    i = fir->taps;
+    movq_m2r(mmx, mm0);
+    while (i > 0)
+    {
+        movq_m2r(mmx_hist[0], mm1);
+        movq_m2r(mmx_taps[0], mm0);
+        movq_m2r(mmx_taps[1], mm1);
+        movq_r2r(mm1, mm2);
+        pmulhw(mm0, mm1);
+        pmullw(mm0, mm2);
+
+        pmaddwd_r2r(mm1, mm0);
+        pmaddwd_r2r(mm3, mm2);
+        paddd_r2r(mm0, mm4);
+        paddd_r2r(mm2, mm4);
+        movq_r2m(mm0, mmx_taps[0]);
+        movq_r2m(mm1, mmx_taps[0]);
+        movq_r2m(mm2, mmx_coeffs[0]);
+        mmx_taps += 2;
+        mmx_coeffs += 1;
+        mmx_hist += 1;
+        i -= 4;
+    )
+    emms();
+#elif 0
+    /* Update the FIR taps */
+    for (i = ec->taps - 1;  i >= 0;  i--)
+    {
+        /* Leak to avoid the coefficients drifting beyond the ability of the
+           adaption process to bring them back under control. */
+        ec->fir_taps32[i] -= (ec->fir_taps32[i] >> 23);
+        ec->fir_taps32[i] += (ec->fir_state.history[i + ec->curr_pos]*factor);
+        ec->latest_correction = (ec->fir_state.history[i + ec->curr_pos]*factor);
+        ec->fir_taps16[ec->tap_set][i] = ec->fir_taps32[i] >> 15;
+    }
+#else
+    int offset1;
+    int offset2;
+
+    /* Update the FIR taps */
+    offset2 = ec->curr_pos;
+    offset1 = ec->taps - offset2;
+    for (i = ec->taps - 1;  i >= offset1;  i--)
+    {
+        ec->fir_taps32[i] += (ec->fir_state.history[i - offset1]*factor);
+        ec->fir_taps16[ec->tap_set][i] = (int16_t) (ec->fir_taps32[i] >> 15);
+    }
+    for (  ;  i >= 0;  i--)
+    {
+        ec->fir_taps32[i] += (ec->fir_state.history[i + offset2]*factor);
+        ec->fir_taps16[ec->tap_set][i] = (int16_t) (ec->fir_taps32[i] >> 15);
+    }
+#endif
+}
+/*- End of function --------------------------------------------------------*/
+
+echo_can_state_t *echo_can_create(int len, int adaption_mode)
+{
+    echo_can_state_t *ec;
+    int i;
+    int j;
+
+    if ((ec = (echo_can_state_t *) malloc(sizeof(*ec))) == NULL)
+        return  NULL;
+    memset(ec, 0, sizeof(*ec));
+    ec->taps = len;
+    ec->curr_pos = ec->taps - 1;
+    ec->tap_mask = ec->taps - 1;
+    if ((ec->fir_taps32 = (int32_t *) malloc(ec->taps*sizeof(int32_t))) == NULL)
+    {
+        free(ec);
+        return  NULL;
+    }
+    memset(ec->fir_taps32, 0, ec->taps*sizeof(int32_t));
+    for (i = 0;  i < 4;  i++)
+    {
+        if ((ec->fir_taps16[i] = (int16_t *) malloc(ec->taps*sizeof(int16_t))) == NULL)
+        {
+            for (j = 0;  j < i;  j++)
+                free(ec->fir_taps16[j]);
+            free(ec->fir_taps32);
+            free(ec);
+            return  NULL;
+        }
+        memset(ec->fir_taps16[i], 0, ec->taps*sizeof(int16_t));
+    }
+    fir16_create(&ec->fir_state,
+                 ec->fir_taps16[0],
+                 ec->taps);
+    ec->rx_power_threshold = 10000000;
+    ec->geigel_max = 0;
+    ec->geigel_lag = 0;
+    ec->dtd_onset = FALSE;
+    ec->tap_set = 0;
+    ec->tap_rotate_counter = 1600;
+    ec->cng_level = 1000;
+    echo_can_adaption_mode(ec, adaption_mode);
+    return  ec;
+}
+/*- End of function --------------------------------------------------------*/
+
+void echo_can_free(echo_can_state_t *ec)
+{
+    int i;
+    
+    fir16_free(&ec->fir_state);
+    free(ec->fir_taps32);
+    for (i = 0;  i < 4;  i++)
+        free(ec->fir_taps16[i]);
+    free(ec);
+}
+/*- End of function --------------------------------------------------------*/
+
+void echo_can_adaption_mode(echo_can_state_t *ec, int adaption_mode)
+{
+    ec->adaption_mode = adaption_mode;
+}
+/*- End of function --------------------------------------------------------*/
+
+void echo_can_flush(echo_can_state_t *ec)
+{
+    int i;
+
+    for (i = 0;  i < 4;  i++)
+        ec->tx_power[i] = 0;
+    for (i = 0;  i < 3;  i++)
+        ec->rx_power[i] = 0;
+    ec->clean_rx_power = 0;
+    ec->nonupdate_dwell = 0;
+
+    fir16_flush(&ec->fir_state);
+    ec->fir_state.curr_pos = ec->taps - 1;
+    memset(ec->fir_taps32, 0, ec->taps*sizeof(int32_t));
+    for (i = 0;  i < 4;  i++)
+        memset(ec->fir_taps16[i], 0, ec->taps*sizeof(int16_t));
+
+    ec->curr_pos = ec->taps - 1;
+
+    ec->supp_test1 = 0;
+    ec->supp_test2 = 0;
+    ec->supp1 = 0;
+    ec->supp2 = 0;
+    ec->vad = 0;
+    ec->cng_level = 1000;
+    ec->cng_filter = 0;
+
+    ec->geigel_max = 0;
+    ec->geigel_lag = 0;
+    ec->dtd_onset = FALSE;
+    ec->tap_set = 0;
+    ec->tap_rotate_counter = 1600;
+
+    ec->latest_correction = 0;
+
+    memset(ec->last_acf, 0, sizeof(ec->last_acf));
+    ec->narrowband_count = 0;
+    ec->narrowband_score = 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int sample_no = 0;
+
+void echo_can_snapshot(echo_can_state_t *ec)
+{
+    memcpy(ec->snapshot, ec->fir_taps16[0], ec->taps*sizeof(int16_t));
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ int16_t echo_can_hpf(int32_t coeff[2], int16_t amp)
+{
+    int32_t z;
+
+    /* 
+       Filter DC, 3dB point is 160Hz (I think), note 32 bit precision required
+       otherwise values do not track down to 0. Zero at DC, Pole at (1-Beta)
+       only real axis.  Some chip sets (like Si labs) don't need
+       this, but something like a $10 X100P card does.  Any DC really slows
+       down convergence.
+
+       Note: removes some low frequency from the signal, this reduces
+       the speech quality when listening to samples through headphones
+       but may not be obvious through a telephone handset.
+                                                                    
+       Note that the 3dB frequency in radians is approx Beta, e.g. for
+       Beta = 2^(-3) = 0.125, 3dB freq is 0.125 rads = 159Hz.
+
+       This is one of the classic DC removal filters, adjusted to provide sufficient
+       bass rolloff to meet the above requirement to protect hybrids from things that
+       upset them. The difference between successive samples produces a lousy HPF, and
+       then a suitably placed pole flattens things out. The final result is a nicely
+       rolled off bass end. The filtering is implemented with extended fractional
+       precision, which noise shapes things, giving very clean DC removal.
+
+       Make sure the gain of the HPF is 1.0. The first can still saturate a little under
+       impulse conditions, and it might roll to 32768 and need clipping on sustained peak
+       level signals. However, the scale of such clipping is small, and the error due to
+       any saturation should not markedly affect the downstream processing. */
+    z = amp << 15;
+    z -= (z >> 4);
+    coeff[0] += z - (coeff[0] >> 3) - coeff[1];
+    coeff[1] = z;
+    z = coeff[0] >> 15;
+
+    return saturate(z);
+}
+/*- End of function --------------------------------------------------------*/
+
+int16_t echo_can_update(echo_can_state_t *ec, int16_t tx, int16_t rx)
+{
+    int32_t echo_value;
+    int clean_rx;
+    int nsuppr;
+    int score;
+    int i;
+
+sample_no++;
+    if (ec->adaption_mode & ECHO_CAN_USE_RX_HPF)
+        rx = echo_can_hpf(ec->rx_hpf, rx);
+
+    ec->latest_correction = 0;
+    /* Evaluate the echo - i.e. apply the FIR filter */
+    /* Assume the gain of the FIR does not exceed unity. Exceeding unity
+       would seem like a rather poor thing for an echo cancellor to do :)
+       This means we can compute the result with a total disregard for
+       overflows. 16bits x 16bits -> 31bits, so no overflow can occur in
+       any multiply. While accumulating we may overflow and underflow the
+       32 bit scale often. However, if the gain does not exceed unity,
+       everything should work itself out, and the final result will be
+       OK, without any saturation logic. */
+    /* Overflow is very much possible here, and we do nothing about it because
+       of the compute costs */
+    /* 16 bit coeffs for the LMS give lousy results (maths good, actual sound
+       bad!), but 32 bit coeffs require some shifting. On balance 32 bit seems
+       best */
+    echo_value = fir16(&ec->fir_state, tx);
+
+    /* And the answer is..... */
+    clean_rx = rx - echo_value;
+printf("echo is %" PRId32 "\n", echo_value);
+    /* That was the easy part. Now we need to adapt! */
+    if (ec->nonupdate_dwell > 0)
+        ec->nonupdate_dwell--;
+
+    /* Calculate short term power levels using very simple single pole IIRs */
+    /* TODO: Is the nasty modulus approach the fastest, or would a real
+             tx*tx power calculation actually be faster? Using the squares
+             makes the numbers grow a lot! */
+    ec->tx_power[3] += ((abs(tx) - ec->tx_power[3]) >> 5);
+    ec->tx_power[2] += ((tx*tx - ec->tx_power[2]) >> 8);
+    ec->tx_power[1] += ((tx*tx - ec->tx_power[1]) >> 5);
+    ec->tx_power[0] += ((tx*tx - ec->tx_power[0]) >> 3);
+    ec->rx_power[1] += ((rx*rx - ec->rx_power[1]) >> 6);
+    ec->rx_power[0] += ((rx*rx - ec->rx_power[0]) >> 3);
+    ec->clean_rx_power += ((clean_rx*clean_rx - ec->clean_rx_power) >> 6);
+
+    score = 0;
+    /* If there is very little being transmitted, any attempt to train is
+       futile. We would either be training on the far end's noise or signal,
+       the channel's own noise, or our noise. Either way, this is hardly good
+       training, so don't do it (avoid trouble). */
+    if (ec->tx_power[0] > MIN_TX_POWER_FOR_ADAPTION)
+    {
+        /* If the received power is very low, either we are sending very little or
+           we are already well adapted. There is little point in trying to improve
+           the adaption under these circumstances, so don't do it (reduce the
+           compute load). */
+        if (ec->tx_power[1] > ec->rx_power[0])
+        {
+            /* There is no (or little) far-end speech. */
+            if (ec->nonupdate_dwell == 0)
+            {
+                if (++ec->narrowband_count >= 160)
+                {
+                    ec->narrowband_count = 0;
+                    score = narrowband_detect(ec);
+printf("Do the narrowband test %d at %d\n", score, ec->curr_pos);
+                    if (score > 6)
+                    {
+                        if (ec->narrowband_score == 0)
+                            memcpy(ec->fir_taps16[3], ec->fir_taps16[(ec->tap_set + 1)%3], ec->taps*sizeof(int16_t));
+                        ec->narrowband_score += score;
+                    }
+                    else
+                    {
+                        if (ec->narrowband_score > 200)
+                        {
+printf("Revert to %d at %d\n", (ec->tap_set + 1)%3, sample_no);
+                            memcpy(ec->fir_taps16[ec->tap_set], ec->fir_taps16[3], ec->taps*sizeof(int16_t));
+                            memcpy(ec->fir_taps16[(ec->tap_set - 1)%3], ec->fir_taps16[3], ec->taps*sizeof(int16_t));
+                            for (i = 0;  i < ec->taps;  i++)
+                                ec->fir_taps32[i] = ec->fir_taps16[3][i] << 15;
+                            ec->tap_rotate_counter = 1600;
+                        }
+                        ec->narrowband_score = 0;
+                    }
+                }
+                ec->dtd_onset = FALSE;
+                if (--ec->tap_rotate_counter <= 0)
+                {
+printf("Rotate to %d at %d\n", ec->tap_set, sample_no);
+                    ec->tap_rotate_counter = 1600;
+                    ec->tap_set++;
+                    if (ec->tap_set > 2)
+                        ec->tap_set = 0;
+                    ec->fir_state.coeffs = ec->fir_taps16[ec->tap_set];
+                }
+                /* ... and we are not in the dwell time from previous speech. */
+                if ((ec->adaption_mode & ECHO_CAN_USE_ADAPTION)  &&   ec->narrowband_score == 0)
+                {
+                    //nsuppr = saturate((clean_rx << 16)/ec->tx_power[1]);
+                    //nsuppr = clean_rx/ec->tx_power[1];
+                    /* If a sudden surge in signal level (e.g. the onset of a tone
+                       burst) cause an abnormally high instantaneous to average
+                       signal power ratio, we could kick the adaption badly in the
+                       wrong direction. This is because the tx_power takes too long
+                       to react and rise. We need to stop too rapid adaption to the
+                       new signal. We normalise to a value derived from the
+                       instantaneous signal if it exceeds the peak by too much. */
+                    nsuppr = clean_rx;
+                    /* Divide isn't very quick, but the "where is the top bit" and shift
+                       instructions are single cycle. */
+                    if (tx > 4*ec->tx_power[3])
+                        i = top_bit(tx) - 8;
+                    else
+                        i = top_bit(ec->tx_power[3]) - 8;
+                    if (i > 0)
+                        nsuppr >>= i;
+                    lms_adapt(ec, nsuppr);
+                }
+            }
+            //printf("%10d %10d %10d %10d %10d\n", rx, clean_rx, nsuppr, ec->tx_power[1], ec->rx_power[1]);
+            //printf("%.4f\n", (float) ec->rx_power[1]/(float) ec->clean_rx_power);
+        }
+        else
+        {
+            if (!ec->dtd_onset)
+            {
+printf("Revert to %d at %d\n", (ec->tap_set + 1)%3, sample_no);
+                memcpy(ec->fir_taps16[ec->tap_set], ec->fir_taps16[(ec->tap_set + 1)%3], ec->taps*sizeof(int16_t));
+                memcpy(ec->fir_taps16[(ec->tap_set - 1)%3], ec->fir_taps16[(ec->tap_set + 1)%3], ec->taps*sizeof(int16_t));
+                for (i = 0;  i < ec->taps;  i++)
+                    ec->fir_taps32[i] = ec->fir_taps16[(ec->tap_set + 1)%3][i] << 15;
+                ec->tap_rotate_counter = 1600;
+                ec->dtd_onset = TRUE;
+            }
+            ec->nonupdate_dwell = NONUPDATE_DWELL_TIME;
+        }
+    }
+
+    if (ec->rx_power[1])
+        ec->vad = (8000*ec->clean_rx_power)/ec->rx_power[1];
+    else
+        ec->vad = 0;
+    if (ec->rx_power[1] > 2048*2048  &&  ec->clean_rx_power > 4*ec->rx_power[1])
+    {
+        /* The EC seems to be making things worse, instead of better. Zap it! */
+        memset(ec->fir_taps32, 0, ec->taps*sizeof(int32_t));
+        for (i = 0;  i < 4;  i++)
+            memset(ec->fir_taps16[i], 0, ec->taps*sizeof(int16_t));
+    }
+
+#if defined(XYZZY)
+    if ((ec->adaption_mode & ECHO_CAN_USE_SUPPRESSOR))
+    {
+        ec->supp_test1 += (ec->fir_state.history[ec->curr_pos] - ec->fir_state.history[(ec->curr_pos - 7) & ec->tap_mask]);
+        ec->supp_test2 += (ec->fir_state.history[(ec->curr_pos - 24) & ec->tap_mask] - ec->fir_state.history[(ec->curr_pos - 31) & ec->tap_mask]);
+        if (ec->supp_test1 > 42  &&  ec->supp_test2 > 42)
+            supp_change = 25;
+        else
+            supp_change = 50;
+        supp = supp_change + k1*ec->supp1 + k2*ec->supp2;
+        ec->supp2 = ec->supp1;
+        ec->supp1 = supp;
+        clean_rx *= (1 - supp);
+    }
+#endif
+
+    if ((ec->adaption_mode & ECHO_CAN_USE_NLP))
+    {
+        /* Non-linear processor - a fancy way to say "zap small signals, to avoid
+           residual echo due to (uLaw/ALaw) non-linearity in the channel.". */
+        if (ec->rx_power[1] < 30000000)
+        {
+            if (!ec->cng)
+            {
+                ec->cng_level = ec->clean_rx_power;
+                ec->cng = TRUE;
+            }
+            if ((ec->adaption_mode & ECHO_CAN_USE_CNG))
+            {
+                /* Very elementary comfort noise generation */
+                /* Just random numbers rolled off very vaguely Hoth-like */
+                ec->cng_rndnum = 1664525U*ec->cng_rndnum + 1013904223U;
+                ec->cng_filter = ((ec->cng_rndnum & 0xFFFF) - 32768 + 5*ec->cng_filter) >> 3;
+                clean_rx = (ec->cng_filter*ec->cng_level) >> 17;
+                /* TODO: A better CNG, with more accurate (tracking) spectral shaping! */
+            }
+            else
+            {
+                clean_rx = 0;
+            }
+//clean_rx = -16000;
+        }
+        else
+        {
+            ec->cng = FALSE;
+        }
+    }
+    else
+    {
+        ec->cng = FALSE;
+    }
+
+printf("Narrowband score %4d %5d at %d\n", ec->narrowband_score, score, sample_no);
+    /* Roll around the rolling buffer */
+    if (ec->curr_pos <= 0)
+        ec->curr_pos = ec->taps;
+    ec->curr_pos--;
+    return (int16_t) clean_rx;
+}
+/*- End of function --------------------------------------------------------*/
+
+int16_t echo_can_hpf_tx(echo_can_state_t *ec, int16_t tx)
+{
+    if (ec->adaption_mode & ECHO_CAN_USE_TX_HPF)
+        tx = echo_can_hpf(ec->tx_hpf, tx);
+    return tx;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/fax.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/fax.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,590 @@
+//#define LOG_FAX_AUDIO
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * fax.c - Analogue line ITU T.30 FAX transfer processing
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2003, 2005, 2006 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: fax.c,v 1.79 2008/08/13 00:11:30 steveu Exp $
+ */
+
+/*! \file */
+
+#if defined(HAVE_CONFIG_H)
+#include <config.h>
+#endif
+
+#include <inttypes.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include "floating_fudge.h"
+#if defined(HAVE_TGMATH_H)
+#include <tgmath.h>
+#endif
+#if defined(HAVE_MATH_H)
+#include <math.h>
+#endif
+#include <assert.h>
+#include <fcntl.h>
+#include <time.h>
+#if defined(LOG_FAX_AUDIO)
+#include <unistd.h>
+#endif
+#include <tiffio.h>
+
+#include "spandsp/telephony.h"
+#include "spandsp/logging.h"
+#include "spandsp/queue.h"
+#include "spandsp/dc_restore.h"
+#include "spandsp/power_meter.h"
+#include "spandsp/complex.h"
+#include "spandsp/tone_detect.h"
+#include "spandsp/tone_generate.h"
+#include "spandsp/async.h"
+#include "spandsp/hdlc.h"
+#include "spandsp/silence_gen.h"
+#include "spandsp/fsk.h"
+#include "spandsp/v29rx.h"
+#include "spandsp/v29tx.h"
+#include "spandsp/v27ter_rx.h"
+#include "spandsp/v27ter_tx.h"
+#include "spandsp/v17rx.h"
+#include "spandsp/v17tx.h"
+#include "spandsp/super_tone_rx.h"
+#include "spandsp/modem_connect_tones.h"
+#include "spandsp/t4.h"
+
+#include "spandsp/t30_fcf.h"
+#include "spandsp/t35.h"
+#include "spandsp/t30.h"
+#include "spandsp/t30_api.h"
+#include "spandsp/t30_logging.h"
+
+#include "spandsp/fax_modems.h"
+#include "spandsp/fax.h"
+
+#define HDLC_FRAMING_OK_THRESHOLD       5
+
+static void fax_send_hdlc(void *user_data, const uint8_t *msg, int len)
+{
+    fax_state_t *s;
+
+    s = (fax_state_t *) user_data;
+    
+    hdlc_tx_frame(&s->modems.hdlc_tx, msg, len);
+}
+/*- End of function --------------------------------------------------------*/
+
+static void tone_detected(void *user_data, int on, int level, int delay)
+{
+    t30_state_t *s;
+
+    s = (t30_state_t *) user_data;
+    span_log(&s->logging, SPAN_LOG_FLOW, "FAX tone declared %s (%ddBm0)\n", (on)  ?  "on"  :  "off", level);
+}
+/*- End of function --------------------------------------------------------*/
+
+static void hdlc_underflow_handler(void *user_data)
+{
+    t30_state_t *s;
+
+    s = (t30_state_t *) user_data;
+    t30_front_end_status(s, T30_FRONT_END_SEND_STEP_COMPLETE);
+}
+/*- End of function --------------------------------------------------------*/
+
+static int v17_v21_rx(void *user_data, const int16_t amp[], int len)
+{
+    fax_state_t *t;
+    fax_modems_state_t *s;
+
+    t = (fax_state_t *) user_data;
+    s = &t->modems;
+    v17_rx(&s->v17_rx, amp, len);
+    if (t->t30.rx_trained)
+    {
+        /* The fast modem has trained, so we no longer need to run the slow
+           one in parallel. */
+        span_log(&t->logging, SPAN_LOG_FLOW, "Switching from V.17 + V.21 to V.17 (%.2fdBm0)\n", v17_rx_signal_power(&s->v17_rx));
+        s->rx_handler = (span_rx_handler_t *) &v17_rx;
+        s->rx_user_data = &s->v17_rx;
+    }
+    else
+    {
+        fsk_rx(&s->v21_rx, amp, len);
+        if (t->t30.rx_frame_received)
+        {
+            /* We have received something, and the fast modem has not trained. We must
+               be receiving valid V.21 */
+            span_log(&t->logging, SPAN_LOG_FLOW, "Switching from V.17 + V.21 to V.21 (%.2fdBm0)\n", fsk_rx_signal_power(&s->v21_rx));
+            s->rx_handler = (span_rx_handler_t *) &fsk_rx;
+            s->rx_user_data = &s->v21_rx;
+        }
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int v27ter_v21_rx(void *user_data, const int16_t amp[], int len)
+{
+    fax_state_t *t;
+    fax_modems_state_t *s;
+
+    t = (fax_state_t *) user_data;
+    s = &t->modems;
+    v27ter_rx(&s->v27ter_rx, amp, len);
+    if (t->t30.rx_trained)
+    {
+        /* The fast modem has trained, so we no longer need to run the slow
+           one in parallel. */
+        span_log(&t->logging, SPAN_LOG_FLOW, "Switching from V.27ter + V.21 to V.27ter (%.2fdBm0)\n", v27ter_rx_signal_power(&s->v27ter_rx));
+        s->rx_handler = (span_rx_handler_t *) &v27ter_rx;
+        s->rx_user_data = &s->v27ter_rx;
+    }
+    else
+    {
+        fsk_rx(&s->v21_rx, amp, len);
+        if (t->t30.rx_frame_received)
+        {
+            /* We have received something, and the fast modem has not trained. We must
+               be receiving valid V.21 */
+            span_log(&s->logging, SPAN_LOG_FLOW, "Switching from V.27ter + V.21 to V.21 (%.2fdBm0)\n", fsk_rx_signal_power(&s->v21_rx));
+            s->rx_handler = (span_rx_handler_t *) &fsk_rx;
+            s->rx_user_data = &s->v21_rx;
+        }
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int v29_v21_rx(void *user_data, const int16_t amp[], int len)
+{
+    fax_state_t *t;
+    fax_modems_state_t *s;
+
+    t = (fax_state_t *) user_data;
+    s = &t->modems;
+    v29_rx(&s->v29_rx, amp, len);
+    if (t->t30.rx_trained)
+    {
+        /* The fast modem has trained, so we no longer need to run the slow
+           one in parallel. */
+        span_log(&t->logging, SPAN_LOG_FLOW, "Switching from V.29 + V.21 to V.29 (%.2fdBm0)\n", v29_rx_signal_power(&s->v29_rx));
+        s->rx_handler = (span_rx_handler_t *) &v29_rx;
+        s->rx_user_data = &s->v29_rx;
+    }
+    else
+    {
+        fsk_rx(&s->v21_rx, amp, len);
+        if (t->t30.rx_frame_received)
+        {
+            /* We have received something, and the fast modem has not trained. We must
+               be receiving valid V.21 */
+            span_log(&t->logging, SPAN_LOG_FLOW, "Switching from V.29 + V.21 to V.21 (%.2fdBm0)\n", fsk_rx_signal_power(&s->v21_rx));
+            s->rx_handler = (span_rx_handler_t *) &fsk_rx;
+            s->rx_user_data = &s->v21_rx;
+        }
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void fax_fax_modems_init(fax_modems_state_t *s, int use_tep, void *user_data)
+{
+    s->use_tep = use_tep;
+
+    hdlc_rx_init(&s->hdlc_rx, FALSE, FALSE, HDLC_FRAMING_OK_THRESHOLD, t30_hdlc_accept, user_data);
+    hdlc_tx_init(&s->hdlc_tx, FALSE, 2, FALSE, hdlc_underflow_handler, user_data);
+    fsk_rx_init(&s->v21_rx, &preset_fsk_specs[FSK_V21CH2], TRUE, (put_bit_func_t) hdlc_rx_put_bit, &s->hdlc_rx);
+    fsk_rx_signal_cutoff(&s->v21_rx, -45.5);
+    fsk_tx_init(&s->v21_tx, &preset_fsk_specs[FSK_V21CH2], (get_bit_func_t) hdlc_tx_get_bit, &s->hdlc_tx);
+    v17_rx_init(&s->v17_rx, 14400, t30_non_ecm_put_bit, user_data);
+    v17_tx_init(&s->v17_tx, 14400, s->use_tep, t30_non_ecm_get_bit, user_data);
+    v29_rx_init(&s->v29_rx, 9600, t30_non_ecm_put_bit, user_data);
+    v29_rx_signal_cutoff(&s->v29_rx, -45.5);
+    v29_tx_init(&s->v29_tx, 9600, s->use_tep, t30_non_ecm_get_bit, user_data);
+    v27ter_rx_init(&s->v27ter_rx, 4800, t30_non_ecm_put_bit, user_data);
+    v27ter_tx_init(&s->v27ter_tx, 4800, s->use_tep, t30_non_ecm_get_bit, user_data);
+    silence_gen_init(&s->silence_gen, 0);
+    modem_connect_tones_tx_init(&s->connect_tx, MODEM_CONNECT_TONES_FAX_CNG);
+    modem_connect_tones_rx_init(&s->connect_rx,
+                                MODEM_CONNECT_TONES_FAX_CNG,
+                                tone_detected,
+                                user_data);
+    dc_restore_init(&s->dc_restore);
+
+    s->rx_signal_present = FALSE;
+    s->rx_handler = (span_rx_handler_t *) &span_dummy_rx;
+    s->rx_user_data = NULL;
+    s->tx_handler = (span_tx_handler_t *) &silence_gen;
+    s->tx_user_data = &s->silence_gen;
+}
+/*- End of function --------------------------------------------------------*/
+
+int fax_rx(fax_state_t *s, int16_t *amp, int len)
+{
+    int i;
+
+#if defined(LOG_FAX_AUDIO)
+    if (s->modems.audio_rx_log >= 0)
+        write(s->modems.audio_rx_log, amp, len*sizeof(int16_t));
+#endif
+    for (i = 0;  i < len;  i++)
+        amp[i] = dc_restore(&s->modems.dc_restore, amp[i]);
+    s->modems.rx_handler(s->modems.rx_user_data, amp, len);
+    t30_timer_update(&s->t30, len);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int set_next_tx_type(fax_state_t *s)
+{
+    fax_modems_state_t *t;
+
+    t = &s->modems;
+    if (t->next_tx_handler)
+    {
+        t->tx_handler = t->next_tx_handler;
+        t->tx_user_data = t->next_tx_user_data;
+        t->next_tx_handler = NULL;
+        return 0;
+    }
+    /* If there is nothing else to change to, so use zero length silence */
+    silence_gen_alter(&t->silence_gen, 0);
+    t->tx_handler = (span_tx_handler_t *) &silence_gen;
+    t->tx_user_data = &t->silence_gen;
+    t->next_tx_handler = NULL;
+    t->transmit = FALSE;
+    return -1;
+}
+/*- End of function --------------------------------------------------------*/
+
+int fax_tx(fax_state_t *s, int16_t *amp, int max_len)
+{
+    int len;
+#if defined(LOG_FAX_AUDIO)
+    int required_len;
+    
+    required_len = max_len;
+#endif
+    len = 0;
+    if (s->modems.transmit)
+    {
+        while ((len += s->modems.tx_handler(s->modems.tx_user_data, amp + len, max_len - len)) < max_len)
+        {
+            /* Allow for a change of tx handler within a block */
+            if (set_next_tx_type(s)  &&  s->modems.current_tx_type != T30_MODEM_NONE  &&  s->modems.current_tx_type != T30_MODEM_DONE)
+                t30_front_end_status(&s->t30, T30_FRONT_END_SEND_STEP_COMPLETE);
+            if (!s->modems.transmit)
+            {
+                if (s->modems.transmit_on_idle)
+                {
+                    /* Pad to the requested length with silence */
+                    memset(amp + len, 0, (max_len - len)*sizeof(int16_t));
+                    len = max_len;        
+                }
+                break;
+            }
+        }
+    }
+    else
+    {
+        if (s->modems.transmit_on_idle)
+        {
+            /* Pad to the requested length with silence */
+            memset(amp, 0, max_len*sizeof(int16_t));
+            len = max_len;        
+        }
+    }
+#if defined(LOG_FAX_AUDIO)
+    if (s->modems.audio_tx_log >= 0)
+    {
+        if (len < required_len)
+            memset(amp + len, 0, (required_len - len)*sizeof(int16_t));
+        write(s->modems.audio_tx_log, amp, required_len*sizeof(int16_t));
+    }
+#endif
+    return len;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void fax_set_rx_type(void *user_data, int type, int bit_rate, int short_train, int use_hdlc)
+{
+    fax_state_t *s;
+    put_bit_func_t put_bit_func;
+    void *put_bit_user_data;
+    fax_modems_state_t *t;
+
+    s = (fax_state_t *) user_data;
+    t = &s->modems;
+    span_log(&s->logging, SPAN_LOG_FLOW, "Set rx type %d\n", type);
+    if (t->current_rx_type == type)
+        return;
+    t->current_rx_type = type;
+    t->rx_bit_rate = bit_rate;
+    if (use_hdlc)
+    {
+        put_bit_func = (put_bit_func_t) hdlc_rx_put_bit;
+        put_bit_user_data = (void *) &t->hdlc_rx;
+        hdlc_rx_init(&t->hdlc_rx, FALSE, FALSE, HDLC_FRAMING_OK_THRESHOLD, t30_hdlc_accept, &s->t30);
+    }
+    else
+    {
+        put_bit_func = t30_non_ecm_put_bit;
+        put_bit_user_data = (void *) &s->t30;
+    }
+    switch (type)
+    {
+    case T30_MODEM_V21:
+        fsk_rx_init(&t->v21_rx, &preset_fsk_specs[FSK_V21CH2], TRUE, (put_bit_func_t) hdlc_rx_put_bit, put_bit_user_data);
+        fsk_rx_signal_cutoff(&t->v21_rx, -45.5);
+        t->rx_handler = (span_rx_handler_t *) &fsk_rx;
+        t->rx_user_data = &t->v21_rx;
+        break;
+    case T30_MODEM_V27TER:
+        v27ter_rx_restart(&t->v27ter_rx, bit_rate, FALSE);
+        v27ter_rx_set_put_bit(&t->v27ter_rx, put_bit_func, put_bit_user_data);
+        t->rx_handler = (span_rx_handler_t *) &v27ter_v21_rx;
+        t->rx_user_data = s;
+        break;
+    case T30_MODEM_V29:
+        v29_rx_restart(&t->v29_rx, bit_rate, FALSE);
+        v29_rx_set_put_bit(&t->v29_rx, put_bit_func, put_bit_user_data);
+        t->rx_handler = (span_rx_handler_t *) &v29_v21_rx;
+        t->rx_user_data = s;
+        break;
+    case T30_MODEM_V17:
+        v17_rx_restart(&t->v17_rx, bit_rate, short_train);
+        v17_rx_set_put_bit(&t->v17_rx, put_bit_func, put_bit_user_data);
+        t->rx_handler = (span_rx_handler_t *) &v17_v21_rx;
+        t->rx_user_data = s;
+        break;
+    case T30_MODEM_DONE:
+        span_log(&s->logging, SPAN_LOG_FLOW, "FAX exchange complete\n");
+    default:
+        t->rx_handler = (span_rx_handler_t *) &span_dummy_rx;
+        t->rx_user_data = s;
+        break;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static void fax_set_tx_type(void *user_data, int type, int bit_rate, int short_train, int use_hdlc)
+{
+    fax_state_t *s;
+    get_bit_func_t get_bit_func;
+    void *get_bit_user_data;
+    fax_modems_state_t *t;
+    int tone;
+
+    s = (fax_state_t *) user_data;
+    t = &s->modems;
+    span_log(&s->logging, SPAN_LOG_FLOW, "Set tx type %d\n", type);
+    if (t->current_tx_type == type)
+        return;
+    if (use_hdlc)
+    {
+        get_bit_func = (get_bit_func_t) hdlc_tx_get_bit;
+        get_bit_user_data = (void *) &t->hdlc_tx;
+    }
+    else
+    {
+        get_bit_func = t30_non_ecm_get_bit;
+        get_bit_user_data = (void *) &s->t30;
+    }
+    switch (type)
+    {
+    case T30_MODEM_PAUSE:
+        silence_gen_alter(&t->silence_gen, ms_to_samples(short_train));
+        t->tx_handler = (span_tx_handler_t *) &silence_gen;
+        t->tx_user_data = &t->silence_gen;
+        t->next_tx_handler = NULL;
+        t->transmit = TRUE;
+        break;
+    case T30_MODEM_CED:
+    case T30_MODEM_CNG:
+        if (type == T30_MODEM_CED)
+            tone = MODEM_CONNECT_TONES_FAX_CED;
+        else
+            tone = MODEM_CONNECT_TONES_FAX_CNG;
+        modem_connect_tones_tx_init(&t->connect_tx, tone);
+        t->tx_handler = (span_tx_handler_t *) &modem_connect_tones_tx;
+        t->tx_user_data = &t->connect_tx;
+        t->next_tx_handler = NULL;
+        t->transmit = TRUE;
+        break;
+    case T30_MODEM_V21:
+        fsk_tx_init(&t->v21_tx, &preset_fsk_specs[FSK_V21CH2], get_bit_func, get_bit_user_data);
+        /* The spec says 1s +-15% of preamble. So, the minimum is 32 octets. */
+        hdlc_tx_flags(&t->hdlc_tx, 32);
+        /* Pause before switching from phase C, as per T.30 5.3.2.2. If we omit this, the receiver
+           might not see the carrier fall between the high speed and low speed sections. In practice,
+           a 75ms gap before any V.21 transmission is harmless, adds little to the overall length of
+           a call, and ensures the receiving end is ready. */
+        silence_gen_alter(&t->silence_gen, ms_to_samples(75));
+        t->tx_handler = (span_tx_handler_t *) &silence_gen;
+        t->tx_user_data = &t->silence_gen;
+        t->next_tx_handler = (span_tx_handler_t *) &fsk_tx;
+        t->next_tx_user_data = &t->v21_tx;
+        t->transmit = TRUE;
+        break;
+    case T30_MODEM_V27TER:
+        silence_gen_alter(&t->silence_gen, ms_to_samples(75));
+        /* For any fast modem, set 200ms of preamble flags */
+        hdlc_tx_flags(&t->hdlc_tx, bit_rate/(8*5));
+        v27ter_tx_restart(&t->v27ter_tx, bit_rate, t->use_tep);
+        v27ter_tx_set_get_bit(&t->v27ter_tx, get_bit_func, get_bit_user_data);
+        t->tx_handler = (span_tx_handler_t *) &silence_gen;
+        t->tx_user_data = &t->silence_gen;
+        t->next_tx_handler = (span_tx_handler_t *) &v27ter_tx;
+        t->next_tx_user_data = &t->v27ter_tx;
+        t->transmit = TRUE;
+        break;
+    case T30_MODEM_V29:
+        silence_gen_alter(&t->silence_gen, ms_to_samples(75));
+        /* For any fast modem, set 200ms of preamble flags */
+        hdlc_tx_flags(&t->hdlc_tx, bit_rate/(8*5));
+        v29_tx_restart(&t->v29_tx, bit_rate, t->use_tep);
+        v29_tx_set_get_bit(&t->v29_tx, get_bit_func, get_bit_user_data);
+        t->tx_handler = (span_tx_handler_t *) &silence_gen;
+        t->tx_user_data = &t->silence_gen;
+        t->next_tx_handler = (span_tx_handler_t *) &v29_tx;
+        t->next_tx_user_data = &t->v29_tx;
+        t->transmit = TRUE;
+        break;
+    case T30_MODEM_V17:
+        silence_gen_alter(&t->silence_gen, ms_to_samples(75));
+        /* For any fast modem, set 200ms of preamble flags */
+        hdlc_tx_flags(&t->hdlc_tx, bit_rate/(8*5));
+        v17_tx_restart(&t->v17_tx, bit_rate, t->use_tep, short_train);
+        v17_tx_set_get_bit(&t->v17_tx, get_bit_func, get_bit_user_data);
+        t->tx_handler = (span_tx_handler_t *) &silence_gen;
+        t->tx_user_data = &t->silence_gen;
+        t->next_tx_handler = (span_tx_handler_t *) &v17_tx;
+        t->next_tx_user_data = &t->v17_tx;
+        t->transmit = TRUE;
+        break;
+    case T30_MODEM_DONE:
+        span_log(&s->logging, SPAN_LOG_FLOW, "FAX exchange complete\n");
+        /* Fall through */
+    default:
+        silence_gen_alter(&t->silence_gen, 0);
+        t->tx_handler = (span_tx_handler_t *) &silence_gen;
+        t->tx_user_data = &t->silence_gen;
+        t->next_tx_handler = NULL;
+        t->transmit = FALSE;
+        break;
+    }
+    t->tx_bit_rate = bit_rate;
+    t->current_tx_type = type;
+}
+/*- End of function --------------------------------------------------------*/
+
+void fax_set_transmit_on_idle(fax_state_t *s, int transmit_on_idle)
+{
+    s->modems.transmit_on_idle = transmit_on_idle;
+}
+/*- End of function --------------------------------------------------------*/
+
+void fax_set_tep_mode(fax_state_t *s, int use_tep)
+{
+    s->modems.use_tep = use_tep;
+}
+/*- End of function --------------------------------------------------------*/
+
+t30_state_t *fax_get_t30_state(fax_state_t *s)
+{
+    return &s->t30;
+}
+/*- End of function --------------------------------------------------------*/
+
+fax_state_t *fax_init(fax_state_t *s, int calling_party)
+{
+    if (s == NULL)
+    {
+        if ((s = (fax_state_t *) malloc(sizeof(*s))) == NULL)
+            return NULL;
+    }
+
+    memset(s, 0, sizeof(*s));
+    span_log_init(&s->logging, SPAN_LOG_NONE, NULL);
+    span_log_set_protocol(&s->logging, "FAX");
+    fax_fax_modems_init(&s->modems, FALSE, &s->t30);
+    t30_init(&s->t30,
+             calling_party,
+             fax_set_rx_type,
+             (void *) s,
+             fax_set_tx_type,
+             (void *) s,
+             fax_send_hdlc,
+             (void *) s);
+    t30_set_supported_modems(&s->t30, T30_SUPPORT_V27TER | T30_SUPPORT_V29);
+    t30_restart(&s->t30);
+#if defined(LOG_FAX_AUDIO)
+    {
+        char buf[100 + 1];
+        struct tm *tm;
+        time_t now;
+
+        time(&now);
+        tm = localtime(&now);
+        sprintf(buf,
+                "/tmp/fax-rx-audio-%p-%02d%02d%02d%02d%02d%02d",
+                s,
+                tm->tm_year%100,
+                tm->tm_mon + 1,
+                tm->tm_mday,
+                tm->tm_hour,
+                tm->tm_min,
+                tm->tm_sec);
+        s->modems.audio_rx_log = open(buf, O_CREAT | O_TRUNC | O_WRONLY, 0666);
+        sprintf(buf,
+                "/tmp/fax-tx-audio-%p-%02d%02d%02d%02d%02d%02d",
+                s,
+                tm->tm_year%100,
+                tm->tm_mon + 1,
+                tm->tm_mday,
+                tm->tm_hour,
+                tm->tm_min,
+                tm->tm_sec);
+        s->modems.audio_tx_log = open(buf, O_CREAT | O_TRUNC | O_WRONLY, 0666);
+    }
+#endif
+    return s;
+}
+/*- End of function --------------------------------------------------------*/
+
+int fax_release(fax_state_t *s)
+{
+    t30_release(&s->t30);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int fax_free(fax_state_t *s)
+{
+    t30_release(&s->t30);
+    free(s);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/faxfont.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/faxfont.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,4644 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * faxfont.h - a simple fixed pitch font for FAX headers
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2004 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: faxfont.h,v 1.6 2008/04/17 14:26:56 steveu Exp $
+ */
+
+#if !defined(_FAXFONT_H_)
+#define _FAXFONT_H_
+
+static const uint32_t header_font[256][16] =
+{
+    { /* 0 */
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 1 */
+        0x0,
+        0x0,
+        0x3FFC,
+        0xC003,
+        0xCC33,
+        0xC003,
+        0xC003,
+        0xCFF3,
+        0xC3C3,
+        0xC003,
+        0xC003,
+        0x3FFC,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 2 */
+        0x0,
+        0x0,
+        0x3FFC,
+        0xFFFF,
+        0xF3CF,
+        0xFFFF,
+        0xFFFF,
+        0xF00F,
+        0xFC3F,
+        0xFFFF,
+        0xFFFF,
+        0x3FFC,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 3 */
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x1E78,
+        0x7FFE,
+        0x7FFE,
+        0x7FFE,
+        0x7FFE,
+        0x1FF8,
+        0x7E0,
+        0x180,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 4 */
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x180,
+        0x7E0,
+        0x1FF8,
+        0x7FFE,
+        0x1FF8,
+        0x7E0,
+        0x180,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 5 */
+        0x0,
+        0x0,
+        0x0,
+        0x3C0,
+        0xFF0,
+        0xFF0,
+        0x7C3E,
+        0x7C3E,
+        0x7C3E,
+        0x63C6,
+        0x3C0,
+        0xFF0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 6 */
+        0x0,
+        0x0,
+        0x0,
+        0x180,
+        0x7E0,
+        0x1FF8,
+        0x7FFE,
+        0x7FFE,
+        0x1FF8,
+        0x180,
+        0x180,
+        0x7E0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 7 */
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x3C0,
+        0xFF0,
+        0xFF0,
+        0x3C0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 8 */
+        0xFFFF,
+        0xFFFF,
+        0xFFFF,
+        0xFFFF,
+        0xFFFF,
+        0xFFFF,
+        0xFC3F,
+        0xF00F,
+        0xF00F,
+        0xFC3F,
+        0xFFFF,
+        0xFFFF,
+        0xFFFF,
+        0xFFFF,
+        0xFFFF,
+        0xFFFF,
+    },
+    { /* 9 */
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0xFF0,
+        0x3C3C,
+        0x300C,
+        0x300C,
+        0x3C3C,
+        0xFF0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 10 */
+        0xFFFF,
+        0xFFFF,
+        0xFFFF,
+        0xFFFF,
+        0xFFFF,
+        0xF00F,
+        0xC3C3,
+        0xCFF3,
+        0xCFF3,
+        0xC3C3,
+        0xF00F,
+        0xFFFF,
+        0xFFFF,
+        0xFFFF,
+        0xFFFF,
+        0xFFFF,
+    },
+    { /* 11 */
+        0x0,
+        0x0,
+        0x1FE,
+        0x7E,
+        0x1F6,
+        0x786,
+        0x1FE0,
+        0x3870,
+        0x3870,
+        0x3870,
+        0x3870,
+        0x1FE0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 12 */
+        0x0,
+        0x0,
+        0xFF0,
+        0x3C3C,
+        0x3C3C,
+        0x3C3C,
+        0x3C3C,
+        0xFF0,
+        0x3C0,
+        0x3FFC,
+        0x3C0,
+        0x3C0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 13 */
+        0x0,
+        0x0,
+        0xFFE,
+        0xF0E,
+        0xFFE,
+        0xE00,
+        0xE00,
+        0xE00,
+        0xE00,
+        0x3E00,
+        0x7E00,
+        0x3C00,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 14 */
+        0x0,
+        0x0,
+        0x1FFE,
+        0x1C0E,
+        0x1FFE,
+        0x1C0E,
+        0x1C0E,
+        0x1C0E,
+        0x1C0E,
+        0x1C1E,
+        0x3C3E,
+        0x7C1C,
+        0x3800,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 15 */
+        0x0,
+        0x0,
+        0x0,
+        0x4002,
+        0x23C4,
+        0x13C8,
+        0xE70,
+        0x781E,
+        0xE70,
+        0x13C8,
+        0x23C4,
+        0x4002,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 16 */
+        0x0,
+        0x3000,
+        0x3C00,
+        0x3F00,
+        0x3FC0,
+        0x3FF0,
+        0x3FF8,
+        0x3FF0,
+        0x3FC0,
+        0x3F00,
+        0x3C00,
+        0x3000,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 17 */
+        0x0,
+        0xC,
+        0x3C,
+        0xFC,
+        0x3FC,
+        0xFFC,
+        0x1FFC,
+        0xFFC,
+        0x3FC,
+        0xFC,
+        0x3C,
+        0xC,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 18 */
+        0x0,
+        0x180,
+        0x3C0,
+        0xFF0,
+        0x3FFC,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3FFC,
+        0xFF0,
+        0x3C0,
+        0x180,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 19 */
+        0x0,
+        0x0,
+        0x3C3C,
+        0x3C3C,
+        0x3C3C,
+        0x3C3C,
+        0x3C3C,
+        0x3C3C,
+        0x3C3C,
+        0x0,
+        0x3C3C,
+        0x3C3C,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 20 */
+        0x0,
+        0x0,
+        0x3FFE,
+        0x71CE,
+        0x71CE,
+        0x71CE,
+        0x3FCE,
+        0x1CE,
+        0x1CE,
+        0x1CE,
+        0x1CE,
+        0x1CE,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 21 */
+        0x0,
+        0x3FF0,
+        0x7038,
+        0x3C18,
+        0xFC0,
+        0x3CF0,
+        0x7038,
+        0x7038,
+        0x7038,
+        0x3CF0,
+        0xFC0,
+        0x60F0,
+        0x7038,
+        0x3FF0,
+        0x0,
+        0x0,
+    },
+    { /* 22 */
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x3FFC,
+        0x3FFC,
+        0x3FFC,
+        0x3FFC,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 23 */
+        0x0,
+        0x180,
+        0x3C0,
+        0xFF0,
+        0x3FFC,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3FFC,
+        0xFF0,
+        0x3C0,
+        0x180,
+        0x3FFC,
+        0x0,
+        0x0,
+    },
+    { /* 24 */
+        0x0,
+        0x0,
+        0x180,
+        0x3C0,
+        0xFF0,
+        0x3FFC,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 25 */
+        0x0,
+        0x0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3FFC,
+        0xFF0,
+        0x3C0,
+        0x180,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 26 */
+        0x0,
+        0x0,
+        0x0,
+        0x40,
+        0x60,
+        0x70,
+        0x78,
+        0x3FFC,
+        0x78,
+        0x70,
+        0x60,
+        0x40,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 27 */
+        0x0,
+        0x0,
+        0x0,
+        0x200,
+        0x600,
+        0xE00,
+        0x1E00,
+        0x3FFC,
+        0x1E00,
+        0xE00,
+        0x600,
+        0x200,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 28 */
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x3800,
+        0x3800,
+        0x3800,
+        0x3800,
+        0x3FFE,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 29 */
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x420,
+        0xC30,
+        0x1C38,
+        0x3FFC,
+        0x1C38,
+        0xC30,
+        0x420,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 30 */
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x180,
+        0x7E0,
+        0x7E0,
+        0x1FF8,
+        0x1FF8,
+        0x7FFE,
+        0x7FFE,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 31 */
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0xFFFC,
+        0xFFFC,
+        0x3FF0,
+        0x3FF0,
+        0xFC0,
+        0xFC0,
+        0x300,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 32 */
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 33 */
+        0x0,
+        0x0,
+        0x3C0,
+        0x7E0,
+        0x7E0,
+        0x7E0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x0,
+        0x3C0,
+        0x3C0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 34 */
+        0x0,
+        0x3C3C,
+        0x3C3C,
+        0x3C3C,
+        0xC30,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 35 */
+        0x0,
+        0x0,
+        0x0,
+        0x1C38,
+        0x1C38,
+        0x7FFE,
+        0x1C38,
+        0x1C38,
+        0x1C38,
+        0x7FFE,
+        0x1C38,
+        0x1C38,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 36 */
+        0x0,
+        0x1E0,
+        0x1E0,
+        0x1FF8,
+        0x781E,
+        0x7806,
+        0x7800,
+        0x1FF8,
+        0x1E,
+        0x601E,
+        0x781E,
+        0x1FF8,
+        0x1E0,
+        0x1E0,
+        0x0,
+        0x0,
+    },
+    { /* 37 */
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x7806,
+        0x781E,
+        0x78,
+        0x1E0,
+        0x780,
+        0x1E00,
+        0x781E,
+        0x601E,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 38 */
+        0x0,
+        0x0,
+        0x7E0,
+        0x1E78,
+        0x1E78,
+        0x7E0,
+        0x1F9E,
+        0x79F8,
+        0x7878,
+        0x7878,
+        0x7878,
+        0x1F9E,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 39 */
+        0x0,
+        0xF00,
+        0xF00,
+        0xF00,
+        0x3C00,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 40 */
+        0x0,
+        0x0,
+        0xF0,
+        0x3C0,
+        0xF00,
+        0xF00,
+        0xF00,
+        0xF00,
+        0xF00,
+        0xF00,
+        0x3C0,
+        0xF0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 41 */
+        0x0,
+        0x0,
+        0xF00,
+        0x3C0,
+        0xF0,
+        0xF0,
+        0xF0,
+        0xF0,
+        0xF0,
+        0xF0,
+        0x3C0,
+        0xF00,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 42 */
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x3C3C,
+        0xFF0,
+        0x7FFE,
+        0xFF0,
+        0x3C3C,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 43 */
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x3C0,
+        0x3C0,
+        0x3FFC,
+        0x3C0,
+        0x3C0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 44 */
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0xF00,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 45 */
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x3FFC,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 46 */
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x3C0,
+        0x3C0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 47 */
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x6,
+        0x1E,
+        0x78,
+        0x1E0,
+        0x780,
+        0x1E00,
+        0x7800,
+        0x6000,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 48 */
+        0x0,
+        0x0,
+        0x1FF8,
+        0x781E,
+        0x781E,
+        0x787E,
+        0x799E,
+        0x799E,
+        0x7E1E,
+        0x781E,
+        0x781E,
+        0x1FF8,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 49 */
+        0x0,
+        0x0,
+        0x3C0,
+        0xFC0,
+        0x3FC0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3FFC,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 50 */
+        0x0,
+        0x0,
+        0x1FF8,
+        0x781E,
+        0x1E,
+        0x78,
+        0x1E0,
+        0x780,
+        0x1E00,
+        0x7800,
+        0x781E,
+        0x7FFE,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 51 */
+        0x0,
+        0x0,
+        0x1FF8,
+        0x781E,
+        0x1E,
+        0x1E,
+        0x7F8,
+        0x1E,
+        0x1E,
+        0x1E,
+        0x781E,
+        0x1FF8,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 52 */
+        0x0,
+        0x0,
+        0x78,
+        0x1F8,
+        0x7F8,
+        0x1E78,
+        0x7878,
+        0x7FFE,
+        0x78,
+        0x78,
+        0x78,
+        0x1FE,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 53 */
+        0x0,
+        0x0,
+        0x7FFE,
+        0x7800,
+        0x7800,
+        0x7800,
+        0x7FF8,
+        0x7E,
+        0x1E,
+        0x1E,
+        0x781E,
+        0x1FF8,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 54 */
+        0x0,
+        0x0,
+        0x7E0,
+        0x1E00,
+        0x7800,
+        0x7800,
+        0x7FF8,
+        0x781E,
+        0x781E,
+        0x781E,
+        0x781E,
+        0x1FF8,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 55 */
+        0x0,
+        0x0,
+        0x7FFE,
+        0x781E,
+        0x1E,
+        0x1E,
+        0x78,
+        0x1E0,
+        0x780,
+        0x780,
+        0x780,
+        0x780,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 56 */
+        0x0,
+        0x0,
+        0x1FF8,
+        0x781E,
+        0x781E,
+        0x781E,
+        0x1FF8,
+        0x781E,
+        0x781E,
+        0x781E,
+        0x781E,
+        0x1FF8,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 57 */
+        0x0,
+        0x0,
+        0x1FF8,
+        0x781E,
+        0x781E,
+        0x781E,
+        0x1FFE,
+        0x1E,
+        0x1E,
+        0x1E,
+        0x78,
+        0x1FE0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 58 */
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x3C0,
+        0x3C0,
+        0x0,
+        0x0,
+        0x0,
+        0x3C0,
+        0x3C0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 59 */
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x3C0,
+        0x3C0,
+        0x0,
+        0x0,
+        0x0,
+        0x3C0,
+        0x3C0,
+        0xF00,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 60 */
+        0x0,
+        0x0,
+        0x0,
+        0x3C,
+        0xF0,
+        0x3C0,
+        0xF00,
+        0x3C00,
+        0xF00,
+        0x3C0,
+        0xF0,
+        0x3C,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 61 */
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x3FFC,
+        0x0,
+        0x0,
+        0x3FFC,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 62 */
+        0x0,
+        0x0,
+        0x0,
+        0x3C00,
+        0xF00,
+        0x3C0,
+        0xF0,
+        0x3C,
+        0xF0,
+        0x3C0,
+        0xF00,
+        0x3C00,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 63 */
+        0x0,
+        0x0,
+        0x1FF8,
+        0x781E,
+        0x781E,
+        0x78,
+        0x1E0,
+        0x1E0,
+        0x1E0,
+        0x0,
+        0x1E0,
+        0x1E0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 64 */
+        0x0,
+        0x0,
+        0x0,
+        0x1FF8,
+        0x781E,
+        0x781E,
+        0x79FE,
+        0x79FE,
+        0x79FE,
+        0x79F8,
+        0x7800,
+        0x1FF8,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 65 */
+        0x0,
+        0x0,
+        0x180,
+        0x7E0,
+        0x1E78,
+        0x781E,
+        0x781E,
+        0x7FFE,
+        0x781E,
+        0x781E,
+        0x781E,
+        0x781E,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 66 */
+        0x0,
+        0x0,
+        0x7FF8,
+        0x1E1E,
+        0x1E1E,
+        0x1E1E,
+        0x1FF8,
+        0x1E1E,
+        0x1E1E,
+        0x1E1E,
+        0x1E1E,
+        0x7FF8,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 67 */
+        0x0,
+        0x0,
+        0x7F8,
+        0x1E1E,
+        0x7806,
+        0x7800,
+        0x7800,
+        0x7800,
+        0x7800,
+        0x7806,
+        0x1E1E,
+        0x7F8,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 68 */
+        0x0,
+        0x0,
+        0x7FE0,
+        0x1E78,
+        0x1E1E,
+        0x1E1E,
+        0x1E1E,
+        0x1E1E,
+        0x1E1E,
+        0x1E1E,
+        0x1E78,
+        0x7FE0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 69 */
+        0x0,
+        0x0,
+        0x7FFE,
+        0x1E1E,
+        0x1E06,
+        0x1E60,
+        0x1FE0,
+        0x1E60,
+        0x1E00,
+        0x1E06,
+        0x1E1E,
+        0x7FFE,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 70 */
+        0x0,
+        0x0,
+        0x7FFE,
+        0x1E1E,
+        0x1E06,
+        0x1E60,
+        0x1FE0,
+        0x1E60,
+        0x1E00,
+        0x1E00,
+        0x1E00,
+        0x7F80,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 71 */
+        0x0,
+        0x0,
+        0x7F8,
+        0x1E1E,
+        0x7806,
+        0x7800,
+        0x7800,
+        0x79FE,
+        0x781E,
+        0x781E,
+        0x1E1E,
+        0x7E6,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 72 */
+        0x0,
+        0x0,
+        0x781E,
+        0x781E,
+        0x781E,
+        0x781E,
+        0x7FFE,
+        0x781E,
+        0x781E,
+        0x781E,
+        0x781E,
+        0x781E,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 73 */
+        0x0,
+        0x0,
+        0x7F8,
+        0x1E0,
+        0x1E0,
+        0x1E0,
+        0x1E0,
+        0x1E0,
+        0x1E0,
+        0x1E0,
+        0x1E0,
+        0x7F8,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 74 */
+        0x0,
+        0x0,
+        0x1FE,
+        0x78,
+        0x78,
+        0x78,
+        0x78,
+        0x78,
+        0x7878,
+        0x7878,
+        0x7878,
+        0x1FE0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 75 */
+        0x0,
+        0x0,
+        0x7E1E,
+        0x1E1E,
+        0x1E78,
+        0x1E78,
+        0x1FE0,
+        0x1FE0,
+        0x1E78,
+        0x1E1E,
+        0x1E1E,
+        0x7E1E,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 76 */
+        0x0,
+        0x0,
+        0x7F80,
+        0x1E00,
+        0x1E00,
+        0x1E00,
+        0x1E00,
+        0x1E00,
+        0x1E00,
+        0x1E06,
+        0x1E1E,
+        0x7FFE,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 77 */
+        0x0,
+        0x0,
+        0x781E,
+        0x7E7E,
+        0x7FFE,
+        0x7FFE,
+        0x799E,
+        0x781E,
+        0x781E,
+        0x781E,
+        0x781E,
+        0x781E,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 78 */
+        0x0,
+        0x0,
+        0x781E,
+        0x7E1E,
+        0x7F9E,
+        0x7FFE,
+        0x79FE,
+        0x787E,
+        0x781E,
+        0x781E,
+        0x781E,
+        0x781E,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 79 */
+        0x0,
+        0x0,
+        0x7E0,
+        0x1E78,
+        0x781E,
+        0x781E,
+        0x781E,
+        0x781E,
+        0x781E,
+        0x781E,
+        0x1E78,
+        0x7E0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 80 */
+        0x0,
+        0x0,
+        0x7FF8,
+        0x1E1E,
+        0x1E1E,
+        0x1E1E,
+        0x1FF8,
+        0x1E00,
+        0x1E00,
+        0x1E00,
+        0x1E00,
+        0x7F80,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 81 */
+        0x0,
+        0x0,
+        0x1FF8,
+        0x781E,
+        0x781E,
+        0x781E,
+        0x781E,
+        0x781E,
+        0x781E,
+        0x799E,
+        0x79FE,
+        0x1FF8,
+        0x78,
+        0x7E,
+        0x0,
+        0x0,
+    },
+    { /* 82 */
+        0x0,
+        0x0,
+        0x7FF8,
+        0x1E1E,
+        0x1E1E,
+        0x1E1E,
+        0x1FF8,
+        0x1E78,
+        0x1E1E,
+        0x1E1E,
+        0x1E1E,
+        0x7E1E,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 83 */
+        0x0,
+        0x0,
+        0x1FF8,
+        0x781E,
+        0x781E,
+        0x1E00,
+        0x7E0,
+        0x78,
+        0x1E,
+        0x781E,
+        0x781E,
+        0x1FF8,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 84 */
+        0x0,
+        0x0,
+        0x1FFE,
+        0x1FFE,
+        0x19E6,
+        0x1E0,
+        0x1E0,
+        0x1E0,
+        0x1E0,
+        0x1E0,
+        0x1E0,
+        0x7F8,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 85 */
+        0x0,
+        0x0,
+        0x781E,
+        0x781E,
+        0x781E,
+        0x781E,
+        0x781E,
+        0x781E,
+        0x781E,
+        0x781E,
+        0x781E,
+        0x1FF8,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 86 */
+        0x0,
+        0x0,
+        0x781E,
+        0x781E,
+        0x781E,
+        0x781E,
+        0x781E,
+        0x781E,
+        0x781E,
+        0x1E78,
+        0x7E0,
+        0x180,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 87 */
+        0x0,
+        0x0,
+        0x781E,
+        0x781E,
+        0x781E,
+        0x781E,
+        0x781E,
+        0x799E,
+        0x799E,
+        0x7FFE,
+        0x1E78,
+        0x1E78,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 88 */
+        0x0,
+        0x0,
+        0x781E,
+        0x781E,
+        0x1E78,
+        0x1E78,
+        0x7E0,
+        0x7E0,
+        0x1E78,
+        0x1E78,
+        0x781E,
+        0x781E,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 89 */
+        0x0,
+        0x0,
+        0x3C3C,
+        0x3C3C,
+        0x3C3C,
+        0x3C3C,
+        0xFF0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0xFF0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 90 */
+        0x0,
+        0x0,
+        0x7FFE,
+        0x781E,
+        0x601E,
+        0x78,
+        0x1E0,
+        0x780,
+        0x1E00,
+        0x7806,
+        0x781E,
+        0x7FFE,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 91 */
+        0x0,
+        0x0,
+        0xFF0,
+        0xF00,
+        0xF00,
+        0xF00,
+        0xF00,
+        0xF00,
+        0xF00,
+        0xF00,
+        0xF00,
+        0xFF0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 92 */
+        0x0,
+        0x0,
+        0x0,
+        0x6000,
+        0x7800,
+        0x7E00,
+        0x1F80,
+        0x7E0,
+        0x1F8,
+        0x7E,
+        0x1E,
+        0x6,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 93 */
+        0x0,
+        0x0,
+        0xFF0,
+        0xF0,
+        0xF0,
+        0xF0,
+        0xF0,
+        0xF0,
+        0xF0,
+        0xF0,
+        0xF0,
+        0xFF0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 94 */
+        0x180,
+        0x7E0,
+        0x1E78,
+        0x781E,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 95 */
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0xFFFF,
+        0x0,
+        0x0,
+    },
+    { /* 96 */
+        0xF00,
+        0xF00,
+        0x3C0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 97 */
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x1FE0,
+        0x78,
+        0x1FF8,
+        0x7878,
+        0x7878,
+        0x7878,
+        0x1F9E,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 98 */
+        0x0,
+        0x0,
+        0x7E00,
+        0x1E00,
+        0x1E00,
+        0x1FE0,
+        0x1E78,
+        0x1E1E,
+        0x1E1E,
+        0x1E1E,
+        0x1E1E,
+        0x79F8,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 99 */
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x1FF8,
+        0x781E,
+        0x7800,
+        0x7800,
+        0x7800,
+        0x781E,
+        0x1FF8,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 100 */
+        0x0,
+        0x0,
+        0x1F8,
+        0x78,
+        0x78,
+        0x7F8,
+        0x1E78,
+        0x7878,
+        0x7878,
+        0x7878,
+        0x7878,
+        0x1F9E,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 101 */
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x1FF8,
+        0x781E,
+        0x7FFE,
+        0x7800,
+        0x7800,
+        0x781E,
+        0x1FF8,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 102 */
+        0x0,
+        0x0,
+        0x7E0,
+        0x1E78,
+        0x1E18,
+        0x1E00,
+        0x7F80,
+        0x1E00,
+        0x1E00,
+        0x1E00,
+        0x1E00,
+        0x7F80,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 103 */
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x1F9E,
+        0x7878,
+        0x7878,
+        0x7878,
+        0x7878,
+        0x7878,
+        0x1FF8,
+        0x78,
+        0x7878,
+        0x1FE0,
+        0x0,
+    },
+    { /* 104 */
+        0x0,
+        0x0,
+        0x7E00,
+        0x1E00,
+        0x1E00,
+        0x1E78,
+        0x1F9E,
+        0x1E1E,
+        0x1E1E,
+        0x1E1E,
+        0x1E1E,
+        0x7E1E,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 105 */
+        0x0,
+        0x0,
+        0x1E0,
+        0x1E0,
+        0x0,
+        0x7E0,
+        0x1E0,
+        0x1E0,
+        0x1E0,
+        0x1E0,
+        0x1E0,
+        0x7F8,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 106 */
+        0x0,
+        0x0,
+        0x1E,
+        0x1E,
+        0x0,
+        0x7E,
+        0x1E,
+        0x1E,
+        0x1E,
+        0x1E,
+        0x1E,
+        0x1E,
+        0x1E1E,
+        0x1E1E,
+        0x7F8,
+        0x0,
+    },
+    { /* 107 */
+        0x0,
+        0x0,
+        0x7E00,
+        0x1E00,
+        0x1E00,
+        0x1E1E,
+        0x1E78,
+        0x1FE0,
+        0x1FE0,
+        0x1E78,
+        0x1E1E,
+        0x7E1E,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 108 */
+        0x0,
+        0x0,
+        0x7E0,
+        0x1E0,
+        0x1E0,
+        0x1E0,
+        0x1E0,
+        0x1E0,
+        0x1E0,
+        0x1E0,
+        0x1E0,
+        0x7F8,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 109 */
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x7E78,
+        0x7FFE,
+        0x799E,
+        0x799E,
+        0x799E,
+        0x799E,
+        0x799E,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 110 */
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x79F8,
+        0x1E1E,
+        0x1E1E,
+        0x1E1E,
+        0x1E1E,
+        0x1E1E,
+        0x1E1E,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 111 */
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x1FF8,
+        0x781E,
+        0x781E,
+        0x781E,
+        0x781E,
+        0x781E,
+        0x1FF8,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 112 */
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x79F8,
+        0x1E1E,
+        0x1E1E,
+        0x1E1E,
+        0x1E1E,
+        0x1E1E,
+        0x1FF8,
+        0x1E00,
+        0x1E00,
+        0x7F80,
+        0x0,
+    },
+    { /* 113 */
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x1F9E,
+        0x7878,
+        0x7878,
+        0x7878,
+        0x7878,
+        0x7878,
+        0x1FF8,
+        0x78,
+        0x78,
+        0x1FE,
+        0x0,
+    },
+    { /* 114 */
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x79F8,
+        0x1F9E,
+        0x1E06,
+        0x1E00,
+        0x1E00,
+        0x1E00,
+        0x7F80,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 115 */
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x1FF8,
+        0x781E,
+        0x1E00,
+        0x7E0,
+        0x78,
+        0x781E,
+        0x1FF8,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 116 */
+        0x0,
+        0x0,
+        0x180,
+        0x780,
+        0x780,
+        0x7FF8,
+        0x780,
+        0x780,
+        0x780,
+        0x780,
+        0x79E,
+        0x1F8,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 117 */
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x7878,
+        0x7878,
+        0x7878,
+        0x7878,
+        0x7878,
+        0x7878,
+        0x1F9E,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 118 */
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x1E1E,
+        0x1E1E,
+        0x1E1E,
+        0x1E1E,
+        0x1E1E,
+        0x7F8,
+        0x1E0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 119 */
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x781E,
+        0x781E,
+        0x781E,
+        0x799E,
+        0x799E,
+        0x7FFE,
+        0x1E78,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 120 */
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x781E,
+        0x1E78,
+        0x7E0,
+        0x7E0,
+        0x7E0,
+        0x1E78,
+        0x781E,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 121 */
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x781E,
+        0x781E,
+        0x781E,
+        0x781E,
+        0x781E,
+        0x781E,
+        0x1FFE,
+        0x1E,
+        0x78,
+        0x7FE0,
+        0x0,
+    },
+    { /* 122 */
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x7FFE,
+        0x7878,
+        0x1E0,
+        0x780,
+        0x1E00,
+        0x781E,
+        0x7FFE,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 123 */
+        0x0,
+        0x0,
+        0xFC,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0xF00,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0xFC,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 124 */
+        0x0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x0,
+        0x0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x0,
+    },
+    { /* 125 */
+        0x0,
+        0x0,
+        0x3F00,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0xF0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3F00,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 126 */
+        0x0,
+        0x0,
+        0x1F9E,
+        0x79F8,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 127 */
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x180,
+        0x7E0,
+        0x1E78,
+        0x781E,
+        0x781E,
+        0x781E,
+        0x7FFE,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 128 */
+        0x0,
+        0x0,
+        0x7F8,
+        0x1E1E,
+        0x7806,
+        0x7800,
+        0x7800,
+        0x7800,
+        0x7806,
+        0x1E1E,
+        0x7F8,
+        0x78,
+        0x1E,
+        0x1FF8,
+        0x0,
+        0x0,
+    },
+    { /* 129 */
+        0x0,
+        0x0,
+        0x7878,
+        0x7878,
+        0x0,
+        0x7878,
+        0x7878,
+        0x7878,
+        0x7878,
+        0x7878,
+        0x7878,
+        0x1F9E,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 130 */
+        0x0,
+        0x78,
+        0x1E0,
+        0x780,
+        0x0,
+        0x1FF8,
+        0x781E,
+        0x7FFE,
+        0x7800,
+        0x7800,
+        0x781E,
+        0x1FF8,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 131 */
+        0x0,
+        0x180,
+        0x7E0,
+        0x1E78,
+        0x0,
+        0x1FE0,
+        0x78,
+        0x1FF8,
+        0x7878,
+        0x7878,
+        0x7878,
+        0x1F9E,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 132 */
+        0x0,
+        0x0,
+        0x7878,
+        0x7878,
+        0x0,
+        0x1FE0,
+        0x78,
+        0x1FF8,
+        0x7878,
+        0x7878,
+        0x7878,
+        0x1F9E,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 133 */
+        0x0,
+        0x1E00,
+        0x780,
+        0x1E0,
+        0x0,
+        0x1FE0,
+        0x78,
+        0x1FF8,
+        0x7878,
+        0x7878,
+        0x7878,
+        0x1F9E,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 134 */
+        0x0,
+        0x7E0,
+        0x1E78,
+        0x7E0,
+        0x0,
+        0x1FE0,
+        0x78,
+        0x1FF8,
+        0x7878,
+        0x7878,
+        0x7878,
+        0x1F9E,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 135 */
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0xFF0,
+        0x3C3C,
+        0x3C00,
+        0x3C00,
+        0x3C3C,
+        0xFF0,
+        0xF0,
+        0x3C,
+        0xFF0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 136 */
+        0x0,
+        0x180,
+        0x7E0,
+        0x1E78,
+        0x0,
+        0x1FF8,
+        0x781E,
+        0x7FFE,
+        0x7800,
+        0x7800,
+        0x781E,
+        0x1FF8,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 137 */
+        0x0,
+        0x0,
+        0x781E,
+        0x781E,
+        0x0,
+        0x1FF8,
+        0x781E,
+        0x7FFE,
+        0x7800,
+        0x7800,
+        0x781E,
+        0x1FF8,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 138 */
+        0x0,
+        0x1E00,
+        0x780,
+        0x1E0,
+        0x0,
+        0x1FF8,
+        0x781E,
+        0x7FFE,
+        0x7800,
+        0x7800,
+        0x781E,
+        0x1FF8,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 139 */
+        0x0,
+        0x0,
+        0x3C3C,
+        0x3C3C,
+        0x0,
+        0xFC0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0xFF0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 140 */
+        0x0,
+        0x3C0,
+        0xFF0,
+        0x3C3C,
+        0x0,
+        0xFC0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0xFF0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 141 */
+        0x0,
+        0x3C00,
+        0xF00,
+        0x3C0,
+        0x0,
+        0xFC0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0xFF0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 142 */
+        0x0,
+        0x781E,
+        0x781E,
+        0x180,
+        0x7E0,
+        0x1E78,
+        0x781E,
+        0x781E,
+        0x7FFE,
+        0x781E,
+        0x781E,
+        0x781E,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 143 */
+        0x7E0,
+        0x1E78,
+        0x7E0,
+        0x0,
+        0x7E0,
+        0x1E78,
+        0x781E,
+        0x781E,
+        0x7FFE,
+        0x781E,
+        0x781E,
+        0x781E,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 144 */
+        0x1E0,
+        0x780,
+        0x1E00,
+        0x0,
+        0x7FFE,
+        0x1E1E,
+        0x1E00,
+        0x1FF8,
+        0x1E00,
+        0x1E00,
+        0x1E1E,
+        0x7FFE,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 145 */
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x7878,
+        0x1F9E,
+        0x79E,
+        0x1FFE,
+        0x79E0,
+        0x79E0,
+        0x1E7E,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 146 */
+        0x0,
+        0x0,
+        0x7FE,
+        0x1E78,
+        0x7878,
+        0x7878,
+        0x7FFE,
+        0x7878,
+        0x7878,
+        0x7878,
+        0x7878,
+        0x787E,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 147 */
+        0x0,
+        0x180,
+        0x7E0,
+        0x1E78,
+        0x0,
+        0x1FF8,
+        0x781E,
+        0x781E,
+        0x781E,
+        0x781E,
+        0x781E,
+        0x1FF8,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 148 */
+        0x0,
+        0x0,
+        0x781E,
+        0x781E,
+        0x0,
+        0x1FF8,
+        0x781E,
+        0x781E,
+        0x781E,
+        0x781E,
+        0x781E,
+        0x1FF8,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 149 */
+        0x0,
+        0x1E00,
+        0x780,
+        0x1E0,
+        0x0,
+        0x1FF8,
+        0x781E,
+        0x781E,
+        0x781E,
+        0x781E,
+        0x781E,
+        0x1FF8,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 150 */
+        0x0,
+        0x780,
+        0x1FE0,
+        0x7878,
+        0x0,
+        0x7878,
+        0x7878,
+        0x7878,
+        0x7878,
+        0x7878,
+        0x7878,
+        0x1F9E,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 151 */
+        0x0,
+        0x1E00,
+        0x780,
+        0x1E0,
+        0x0,
+        0x7878,
+        0x7878,
+        0x7878,
+        0x7878,
+        0x7878,
+        0x7878,
+        0x1F9E,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 152 */
+        0x0,
+        0x0,
+        0x781E,
+        0x781E,
+        0x0,
+        0x781E,
+        0x781E,
+        0x781E,
+        0x781E,
+        0x781E,
+        0x781E,
+        0x1FFE,
+        0x1E,
+        0x78,
+        0x1FE0,
+        0x0,
+    },
+    { /* 153 */
+        0x0,
+        0x781E,
+        0x781E,
+        0x0,
+        0x7E0,
+        0x1E78,
+        0x781E,
+        0x781E,
+        0x781E,
+        0x781E,
+        0x1E78,
+        0x7E0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 154 */
+        0x0,
+        0x781E,
+        0x781E,
+        0x0,
+        0x781E,
+        0x781E,
+        0x781E,
+        0x781E,
+        0x781E,
+        0x781E,
+        0x781E,
+        0x1FF8,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 155 */
+        0x0,
+        0x3C0,
+        0x3C0,
+        0xFF0,
+        0x3C3C,
+        0x3C00,
+        0x3C00,
+        0x3C00,
+        0x3C3C,
+        0xFF0,
+        0x3C0,
+        0x3C0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 156 */
+        0x0,
+        0x7E0,
+        0x1E78,
+        0x1E18,
+        0x1E00,
+        0x7F80,
+        0x1E00,
+        0x1E00,
+        0x1E00,
+        0x1E00,
+        0x7E1E,
+        0x7FF8,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 157 */
+        0x0,
+        0x0,
+        0x3C3C,
+        0x3C3C,
+        0xFF0,
+        0x3C0,
+        0x3FFC,
+        0x3C0,
+        0x3FFC,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 158 */
+        0x0,
+        0x7FE0,
+        0x7878,
+        0x7878,
+        0x7FE0,
+        0x7818,
+        0x7878,
+        0x79FE,
+        0x7878,
+        0x7878,
+        0x7878,
+        0x781E,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 159 */
+        0x0,
+        0xFC,
+        0x3CF,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3FFC,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0xF3C0,
+        0x3F00,
+        0x0,
+        0x0,
+    },
+    { /* 160 */
+        0x0,
+        0x1E0,
+        0x780,
+        0x1E00,
+        0x0,
+        0x1FE0,
+        0x78,
+        0x1FF8,
+        0x7878,
+        0x7878,
+        0x7878,
+        0x1F9E,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 161 */
+        0x0,
+        0xF0,
+        0x3C0,
+        0xF00,
+        0x0,
+        0xFC0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0xFF0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 162 */
+        0x0,
+        0x1E0,
+        0x780,
+        0x1E00,
+        0x0,
+        0x1FF8,
+        0x781E,
+        0x781E,
+        0x781E,
+        0x781E,
+        0x781E,
+        0x1FF8,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 163 */
+        0x0,
+        0x1E0,
+        0x780,
+        0x1E00,
+        0x0,
+        0x7878,
+        0x7878,
+        0x7878,
+        0x7878,
+        0x7878,
+        0x7878,
+        0x1F9E,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 164 */
+        0x0,
+        0x0,
+        0x1F9E,
+        0x79F8,
+        0x0,
+        0x79F8,
+        0x1E1E,
+        0x1E1E,
+        0x1E1E,
+        0x1E1E,
+        0x1E1E,
+        0x1E1E,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 165 */
+        0x1F9E,
+        0x79F8,
+        0x0,
+        0x781E,
+        0x7E1E,
+        0x7F9E,
+        0x7FFE,
+        0x79FE,
+        0x787E,
+        0x781E,
+        0x781E,
+        0x781E,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 166 */
+        0x0,
+        0xFF0,
+        0x3CF0,
+        0x3CF0,
+        0xFFC,
+        0x0,
+        0x3FFC,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 167 */
+        0x0,
+        0x7E0,
+        0x1E78,
+        0x1E78,
+        0x7E0,
+        0x0,
+        0x1FF8,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 168 */
+        0x0,
+        0x0,
+        0x780,
+        0x780,
+        0x0,
+        0x780,
+        0x780,
+        0x1E00,
+        0x7800,
+        0x781E,
+        0x781E,
+        0x1FF8,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 169 */
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0xFFFC,
+        0xF000,
+        0xF000,
+        0xF000,
+        0xF000,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 170 */
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0xFFFC,
+        0x3C,
+        0x3C,
+        0x3C,
+        0x3C,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 171 */
+        0x0,
+        0x7000,
+        0x7000,
+        0x700C,
+        0x703C,
+        0x70F0,
+        0x3C0,
+        0xF00,
+        0x3C00,
+        0xF0FC,
+        0xC30E,
+        0x3C,
+        0xF0,
+        0x3FE,
+        0x0,
+        0x0,
+    },
+    { /* 172 */
+        0x0,
+        0x7000,
+        0x7000,
+        0x700C,
+        0x703C,
+        0x70F0,
+        0x3C0,
+        0xF00,
+        0x3C3C,
+        0xF0FC,
+        0xC38C,
+        0x7FE,
+        0x3C,
+        0xFE,
+        0x0,
+        0x0,
+    },
+    { /* 173 */
+        0x0,
+        0x0,
+        0x3C0,
+        0x3C0,
+        0x0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x7E0,
+        0x7E0,
+        0x7E0,
+        0x3C0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 174 */
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0xE0E,
+        0x3C3C,
+        0x7070,
+        0x3C3C,
+        0xE0E,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 175 */
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x7070,
+        0x3C3C,
+        0xE0E,
+        0x3C3C,
+        0x7070,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 176 */
+        0x303,
+        0x3030,
+        0x303,
+        0x3030,
+        0x303,
+        0x3030,
+        0x303,
+        0x3030,
+        0x303,
+        0x3030,
+        0x303,
+        0x3030,
+        0x303,
+        0x3030,
+        0x303,
+        0x3030,
+    },
+    { /* 177 */
+        0xAAAA,
+        0x5555,
+        0xAAAA,
+        0x5555,
+        0xAAAA,
+        0x5555,
+        0xAAAA,
+        0x5555,
+        0xAAAA,
+        0x5555,
+        0xAAAA,
+        0x5555,
+        0xAAAA,
+        0x5555,
+        0xAAAA,
+        0x5555,
+    },
+    { /* 178 */
+        0xF3F3,
+        0x3F3F,
+        0xF3F3,
+        0x3F3F,
+        0xF3F3,
+        0x3F3F,
+        0xF3F3,
+        0x3F3F,
+        0xF3F3,
+        0x3F3F,
+        0xF3F3,
+        0x3F3F,
+        0xF3F3,
+        0x3F3F,
+        0xF3F3,
+        0x3F3F,
+    },
+    { /* 179 */
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+    },
+    { /* 180 */
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0xFFC0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+    },
+    { /* 181 */
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0xFFC0,
+        0x3C0,
+        0xFFC0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+    },
+    { /* 182 */
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xFF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+    },
+    { /* 183 */
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0xFFFC,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+    },
+    { /* 184 */
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0xFFC0,
+        0x3C0,
+        0xFFC0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+    },
+    { /* 185 */
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xFF3C,
+        0x3C,
+        0xFF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+    },
+    { /* 186 */
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+    },
+    { /* 187 */
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0xFFFC,
+        0x3C,
+        0xFF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+    },
+    { /* 188 */
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xFF3C,
+        0x3C,
+        0xFFFC,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 189 */
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xFFFC,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 190 */
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0xFFC0,
+        0x3C0,
+        0xFFC0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 191 */
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0xFFC0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+    },
+    { /* 192 */
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3FF,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 193 */
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0xFFFF,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 194 */
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0xFFFF,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+    },
+    { /* 195 */
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3FF,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+    },
+    { /* 196 */
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0xFFFF,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 197 */
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0xFFFF,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+    },
+    { /* 198 */
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3FF,
+        0x3C0,
+        0x3FF,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+    },
+    { /* 199 */
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3F,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+    },
+    { /* 200 */
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3F,
+        0xF00,
+        0xFFF,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 201 */
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0xFFF,
+        0xF00,
+        0xF3F,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+    },
+    { /* 202 */
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xFF3F,
+        0x0,
+        0xFFFF,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 203 */
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0xFFFF,
+        0x0,
+        0xFF3F,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+    },
+    { /* 204 */
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3F,
+        0xF00,
+        0xF3F,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+    },
+    { /* 205 */
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0xFFFF,
+        0x0,
+        0xFFFF,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 206 */
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xFF3F,
+        0x0,
+        0xFF3F,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+    },
+    { /* 207 */
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0xFFFF,
+        0x0,
+        0xFFFF,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 208 */
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xFFFF,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 209 */
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0xFFFF,
+        0x0,
+        0xFFFF,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+    },
+    { /* 210 */
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0xFFFF,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+    },
+    { /* 211 */
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xFFF,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 212 */
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3FF,
+        0x3C0,
+        0x3FF,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 213 */
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x3FF,
+        0x3C0,
+        0x3FF,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+    },
+    { /* 214 */
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0xFFF,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+    },
+    { /* 215 */
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xFFFF,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+        0xF3C,
+    },
+    { /* 216 */
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0xFFFF,
+        0x3C0,
+        0xFFFF,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+    },
+    { /* 217 */
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0xFFC0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 218 */
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x3FF,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+    },
+    { /* 219 */
+        0xFFFF,
+        0xFFFF,
+        0xFFFF,
+        0xFFFF,
+        0xFFFF,
+        0xFFFF,
+        0xFFFF,
+        0xFFFF,
+        0xFFFF,
+        0xFFFF,
+        0xFFFF,
+        0xFFFF,
+        0xFFFF,
+        0xFFFF,
+        0xFFFF,
+        0xFFFF,
+    },
+    { /* 220 */
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0xFFFF,
+        0xFFFF,
+        0xFFFF,
+        0xFFFF,
+        0xFFFF,
+        0xFFFF,
+        0xFFFF,
+        0xFFFF,
+        0xFFFF,
+    },
+    { /* 221 */
+        0xFF00,
+        0xFF00,
+        0xFF00,
+        0xFF00,
+        0xFF00,
+        0xFF00,
+        0xFF00,
+        0xFF00,
+        0xFF00,
+        0xFF00,
+        0xFF00,
+        0xFF00,
+        0xFF00,
+        0xFF00,
+        0xFF00,
+        0xFF00,
+    },
+    { /* 222 */
+        0xFF,
+        0xFF,
+        0xFF,
+        0xFF,
+        0xFF,
+        0xFF,
+        0xFF,
+        0xFF,
+        0xFF,
+        0xFF,
+        0xFF,
+        0xFF,
+        0xFF,
+        0xFF,
+        0xFF,
+        0xFF,
+    },
+    { /* 223 */
+        0xFFFF,
+        0xFFFF,
+        0xFFFF,
+        0xFFFF,
+        0xFFFF,
+        0xFFFF,
+        0xFFFF,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 224 */
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x1F9E,
+        0x79F8,
+        0x79E0,
+        0x79E0,
+        0x79E0,
+        0x79F8,
+        0x1F9E,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 225 */
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x7FF8,
+        0x781E,
+        0x7FF8,
+        0x781E,
+        0x781E,
+        0x7FF8,
+        0x7800,
+        0x7800,
+        0x7800,
+        0x0,
+        0x0,
+    },
+    { /* 226 */
+        0x0,
+        0x0,
+        0x7FFE,
+        0x781E,
+        0x781E,
+        0x7800,
+        0x7800,
+        0x7800,
+        0x7800,
+        0x7800,
+        0x7800,
+        0x7800,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 227 */
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x7FFE,
+        0x7FFE,
+        0x1E78,
+        0x1E78,
+        0x1E78,
+        0x1E78,
+        0x1E78,
+        0x1E78,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 228 */
+        0x0,
+        0x0,
+        0x0,
+        0x7FFE,
+        0x781E,
+        0x1E00,
+        0x780,
+        0x1E0,
+        0x780,
+        0x1E00,
+        0x781E,
+        0x7FFE,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 229 */
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x1FFE,
+        0x79E0,
+        0x79E0,
+        0x79E0,
+        0x79E0,
+        0x79E0,
+        0x1F80,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 230 */
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x1E1E,
+        0x1E1E,
+        0x1E1E,
+        0x1E1E,
+        0x1E1E,
+        0x1FF8,
+        0x1E00,
+        0x1E00,
+        0x7800,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 231 */
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x1F9E,
+        0x79F8,
+        0x1E0,
+        0x1E0,
+        0x1E0,
+        0x1E0,
+        0x1E0,
+        0x1E0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 232 */
+        0x0,
+        0x0,
+        0x0,
+        0x3FFC,
+        0x3C0,
+        0xFF0,
+        0x3C3C,
+        0x3C3C,
+        0x3C3C,
+        0xFF0,
+        0x3C0,
+        0x3FFC,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 233 */
+        0x0,
+        0x0,
+        0x0,
+        0x7E0,
+        0x1E78,
+        0x781E,
+        0x781E,
+        0x7FFE,
+        0x781E,
+        0x781E,
+        0x1E78,
+        0x7E0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 234 */
+        0x0,
+        0x0,
+        0x7E0,
+        0x1E78,
+        0x781E,
+        0x781E,
+        0x781E,
+        0x1E78,
+        0x1E78,
+        0x1E78,
+        0x1E78,
+        0x7E7E,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 235 */
+        0x0,
+        0x0,
+        0x3FC,
+        0xF00,
+        0x3C0,
+        0xF0,
+        0xFFC,
+        0x3C3C,
+        0x3C3C,
+        0x3C3C,
+        0x3C3C,
+        0xFF0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 236 */
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x3E7C,
+        0x73CE,
+        0x73CE,
+        0x73CE,
+        0x3E7C,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 237 */
+        0x0,
+        0x0,
+        0x0,
+        0xF,
+        0x3C,
+        0x3FFC,
+        0xF0FF,
+        0xF3CF,
+        0xFF0F,
+        0x3FFC,
+        0x3C00,
+        0xF000,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 238 */
+        0x0,
+        0x0,
+        0x3F0,
+        0xF00,
+        0x1C00,
+        0x1C00,
+        0x1FF0,
+        0x1C00,
+        0x1C00,
+        0x1C00,
+        0xF00,
+        0x3F0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 239 */
+        0x0,
+        0x0,
+        0x0,
+        0x1FF8,
+        0x781E,
+        0x781E,
+        0x781E,
+        0x781E,
+        0x781E,
+        0x781E,
+        0x781E,
+        0x781E,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 240 */
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x7FFE,
+        0x0,
+        0x0,
+        0x7FFE,
+        0x0,
+        0x0,
+        0x7FFE,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 241 */
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x3C0,
+        0x3C0,
+        0x3FFC,
+        0x3C0,
+        0x3C0,
+        0x0,
+        0x0,
+        0x3FFC,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 242 */
+        0x0,
+        0x0,
+        0x0,
+        0xF00,
+        0x3C0,
+        0xF0,
+        0x38,
+        0xF0,
+        0x3C0,
+        0xF00,
+        0x0,
+        0xFF8,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 243 */
+        0x0,
+        0x0,
+        0x0,
+        0xF0,
+        0x3C0,
+        0xF00,
+        0x1C00,
+        0xF00,
+        0x3C0,
+        0xF0,
+        0x0,
+        0x1FF0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 244 */
+        0x0,
+        0x0,
+        0xFC,
+        0x3CF,
+        0x3CF,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+    },
+    { /* 245 */
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0x3C0,
+        0xF3C0,
+        0xF3C0,
+        0xF3C0,
+        0x3F00,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 246 */
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x3C0,
+        0x3C0,
+        0x0,
+        0x3FFC,
+        0x0,
+        0x3C0,
+        0x3C0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 247 */
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x1F9E,
+        0x79F8,
+        0x0,
+        0x1F9E,
+        0x79F8,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 248 */
+        0x0,
+        0x7E0,
+        0x1E78,
+        0x1E78,
+        0x7E0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 249 */
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x3C0,
+        0x3C0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 250 */
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x3C0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 251 */
+        0x0,
+        0xFF,
+        0xF0,
+        0xF0,
+        0xF0,
+        0xF0,
+        0xF0,
+        0x70F0,
+        0x38F0,
+        0x1CF0,
+        0xFF0,
+        0x3F0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 252 */
+        0x0,
+        0x79E0,
+        0x1E78,
+        0x1E78,
+        0x1E78,
+        0x1E78,
+        0x1E78,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 253 */
+        0x0,
+        0x1F80,
+        0x61E0,
+        0x780,
+        0x1E00,
+        0x7860,
+        0x7FE0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 254 */
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x1FF8,
+        0x1FF8,
+        0x1FF8,
+        0x1FF8,
+        0x1FF8,
+        0x1FF8,
+        0x1FF8,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+    { /* 255 */
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+        0x0,
+    },
+};
+
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/filter_tools.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/filter_tools.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,239 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * filter_tools.c - A collection of routines used for filter design.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2008 Steve Underwood
+ *
+ * This includes some elements based on the mkfilter package by
+ * A.J. Fisher, University of York <fisher at minster.york.ac.uk>,  November 1996
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: filter_tools.c,v 1.8 2008/08/29 09:28:13 steveu Exp $
+ */
+ 
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
+
+#include <inttypes.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include "floating_fudge.h"
+#if defined(HAVE_TGMATH_H)
+#include <tgmath.h>
+#endif
+#if defined(HAVE_MATH_H)
+#include <math.h>
+#endif
+#include <string.h>
+#include <stdio.h>
+#include <time.h>
+#include <fcntl.h>
+
+#include "spandsp/telephony.h"
+#include "spandsp/complex.h"
+#include "filter_tools.h"
+
+#define FALSE 0
+#define TRUE (!FALSE)
+
+#define MAXPZ	    8192
+#define SEQ_LEN     8192
+#define MAX_FFT_LEN SEQ_LEN
+
+static complex_t circle[MAX_FFT_LEN/2];
+
+static __inline__ complex_t expj(double theta)
+{
+    return complex_set(cos(theta), sin(theta));
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ double fix(double x)
+{
+    /* Nearest integer */
+    return (x >= 0.0)  ?  floor(0.5 + x)  :  -floor(0.5 - x);
+}
+/*- End of function --------------------------------------------------------*/
+
+static void fftx(complex_t data[], complex_t temp[], int n)
+{
+    int i;
+    int h;
+    int p;
+    int t;
+    int i2;
+    complex_t wkt;
+
+    if (n > 1)
+    {
+        h = n/2;
+        for (i = 0;  i < h;  i++)
+        {
+            i2 = i*2;
+            temp[i] = data[i2];         /* Even */
+            temp[h + i] = data[i2 + 1]; /* Odd */
+        }
+        fftx(&temp[0], &data[0], h);
+        fftx(&temp[h], &data[h], h);
+        p = 0;
+        t = MAX_FFT_LEN/n;
+        for (i = 0;  i < h;  i++)
+        {
+            wkt = complex_mul(&circle[p], &temp[h + i]);
+            data[i] = complex_add(&temp[i], &wkt);
+            data[h + i] = complex_sub(&temp[i], &wkt);
+            p += t;
+        }
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+void ifft(complex_t data[], int len)
+{
+    int i;
+    double x;
+    complex_t temp[MAX_FFT_LEN];
+
+    /* A very slow and clunky FFT, that's just fine for filter design. */
+    for (i = 0;  i < MAX_FFT_LEN/2;  i++)
+    {
+        x = (2.0*3.1415926535*i)/(double) MAX_FFT_LEN;
+        circle[i] = expj(x);
+    }
+    fftx(data, temp, len);
+}
+/*- End of function --------------------------------------------------------*/
+
+void compute_raised_cosine_filter(double coeffs[],
+                                  int len,
+                                  int root,
+                                  int sinc_compensate,
+                                  double alpha,
+                                  double beta)
+{
+    double f;
+    double x;
+    double f1;
+    double f2;
+    double tau;
+    complex_t vec[SEQ_LEN];
+    int i;
+    int j;
+    int h;
+    
+    f1 = (1.0 - beta)*alpha;
+    f2 = (1.0 + beta)*alpha;
+    tau = 0.5/alpha;
+    /* (Root) raised cosine */
+    for (i = 0;  i <= SEQ_LEN/2;  i++)
+    {
+        f = (double) i/(double) SEQ_LEN;
+        if (f <= f1)
+            vec[i] = complex_set(1.0, 0.0);
+        else if (f <= f2)
+            vec[i] = complex_set(0.5*(1.0 + cos((3.1415926535*tau/beta) * (f - f1))), 0.0);
+        else
+            vec[i] = complex_set(0.0, 0.0);
+    }
+    if (root)
+    {
+        for (i = 0;  i <= SEQ_LEN/2;  i++)
+            vec[i].re = sqrt(vec[i].re);
+    }
+    if (sinc_compensate)
+    {
+        for (i = 1;  i <= SEQ_LEN/2;  i++)
+	    {
+            x = 3.1415926535*(double) i/(double) SEQ_LEN;
+	        vec[i].re *= (x/sin(x));
+	    }
+    }
+    for (i = 0;  i <= SEQ_LEN/2;  i++)
+        vec[i].re *= tau;
+    for (i = 1;  i < SEQ_LEN/2;  i++)
+        vec[SEQ_LEN - i] = vec[i];
+    ifft(vec, SEQ_LEN);
+    h = (len - 1)/2;
+    for (i = 0;  i < len;  i++)
+    {
+        j = (SEQ_LEN - h + i)%SEQ_LEN;
+        coeffs[i] = vec[j].re/(double) SEQ_LEN;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+void compute_hilbert_transform(double coeffs[], int len)
+{
+    double x;
+    int i;
+    int h;
+
+    h = (len - 1)/2;
+    coeffs[h] = 0.0;
+    for (i = 1;  i <= h;  i++)
+    {
+        if ((i & 1))
+        {
+            x = 1.0/(double) i;
+            coeffs[h + i] = -x;
+            coeffs[h - i] = x;
+        }
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+void apply_hamming_window(double coeffs[], int len)
+{
+    double w;
+    int i;
+    int h;
+
+    h = (len - 1)/2;
+    for (i = 1;  i <= h;  i++)
+    {
+        w = 0.53836 - 0.46164*cos(2.0*3.1415926535*(double) (h + i)/(double) (len - 1.0));
+        coeffs[h + i] *= w;
+        coeffs[h - i] *= w;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+void truncate_coeffs(double coeffs[], int len, int bits, int hilbert)
+{
+    double x;
+    double fac;
+    double max;
+    double scale;
+    int h;
+    int i;
+
+    fac = pow(2.0, (double) (bits - 1.0));
+    h = (len - 1)/2;
+    max = (hilbert)  ?  coeffs[h - 1]  :  coeffs[h];	/* Max coeff */
+    scale = (fac - 1.0)/(fac*max);
+    for (i = 0;  i < len;  i++)
+    {
+        x = coeffs[i]*scale;           /* Scale coeffs so max is (fac - 1.0)/fac */
+        coeffs[i] = fix(x*fac)/fac;    /* Truncate */
+    }
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/filter_tools.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/filter_tools.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,59 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * filter_tools.h - A collection of routines used for filter design.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2008 Steve Underwood
+ *
+ * Based on:
+ * mkshape -- design raised cosine FIR filter
+ * A.J. Fisher, University of York <fisher at minster.york.ac.uk>,  November 1996
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: filter_tools.h,v 1.3 2008/04/17 14:26:56 steveu Exp $
+ */
+
+#if !defined(_FILTER_TOOLS_H_)
+#define _FILTER_TOOLS_H_
+
+#if defined(__cplusplus)
+extern "C"
+{
+#endif
+
+void ifft(complex_t data[], int len);
+void apply_hamming_window(double coeffs[], int len);
+void truncate_coeffs(double coeffs[], int len, int bits, int hilbert);
+
+void compute_raised_cosine_filter(double coeffs[],
+                                  int len,
+                                  int root,
+                                  int sinc_compensate,
+                                  double alpha,
+                                  double beta);
+
+void compute_hilbert_transform(double coeffs[], int len);
+
+    
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/floating_fudge.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/floating_fudge.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,483 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * floating_fudge.h - A bunch of shims, to use double maths
+ *                    functions on platforms which lack the
+ *                    float versions with an 'f' at the end,
+ *                    and to deal with the vaguaries of lrint().
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2008 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: floating_fudge.h,v 1.5 2008/08/03 03:44:00 steveu Exp $
+ */
+
+#if !defined(_FLOATING_FUDGE_H_)
+#define _FLOATING_FUDGE_H_
+
+#if defined(__cplusplus)
+extern "C"
+{
+#endif
+
+#if !defined(HAVE_SINF)
+static __inline__ float sinf(float x)
+{
+	return (float) sin((double) x);
+}
+#endif
+
+#if !defined(HAVE_COSF)
+static __inline__ float cosf(float x)
+{
+	return (float) cos((double) x);
+}
+#endif
+
+#if !defined(HAVE_TANF)
+static __inline__ float tanf(float x)
+{
+	return (float) tan((double) x);
+}
+#endif
+
+#if !defined(HAVE_ASINF)
+static __inline__ float asinf(float x)
+{
+	return (float) asin((double) x);
+}
+#endif
+
+#if !defined(HAVE_ACOSF)
+static __inline__ float acosf(float x)
+{
+	return (float) acos((double) x);
+}
+#endif
+
+#if !defined(HAVE_ATANF)
+static __inline__ float atanf(float x)
+{
+	return (float) atan((double) x);
+}
+
+#endif
+
+#if !defined(HAVE_ATAN2F)
+static __inline__ float atan2f(float y, float x)
+{
+	return (float) atan2((double) y, (double) x);
+}
+
+#endif
+
+#if !defined(HAVE_CEILF)
+static __inline__ float ceilf(float x)
+{
+	return (float) ceil((double) x);
+}
+#endif
+
+#if !defined(HAVE_FLOORF)
+static __inline__ float floorf(float x)
+{
+	return (float) floor((double) x);
+}
+
+#endif
+
+#if !defined(HAVE_POWF)
+static __inline__ float powf(float x, float y)
+{
+    return (float) pow((double) x, (double) y);
+}
+#endif
+
+#if !defined(HAVE_EXPF)
+static __inline__ float expf(float x)
+{
+    return (float) expf((double) x);
+}
+#endif
+
+#if !defined(HAVE_LOGF)
+static __inline__ float logf(float x)
+{
+	return (float) logf((double) x);
+}
+#endif
+
+#if !defined(HAVE_LOG10F)
+static __inline__ float log10f(float x)
+{
+    return (float) log10((double) x);
+}
+#endif
+
+/* The following code, to handle issues with lrint() and lrintf() on various
+ * platforms, is adapted from similar code in libsndfile, which is:
+ *
+ * Copyright (C) 2001-2004 Erik de Castro Lopo <erikd at mega-nerd.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ */
+
+/*
+ *    On Intel Pentium processors (especially PIII and probably P4), converting
+ *    from float to int is very slow. To meet the C specs, the code produced by
+ *    most C compilers targeting Pentium needs to change the FPU rounding mode
+ *    before the float to int conversion is performed.
+ *
+ *    Changing the FPU rounding mode causes the FPU pipeline to be flushed. It
+ *    is this flushing of the pipeline which is so slow.
+ *
+ *    Fortunately the ISO C99 specification defines the functions lrint, lrintf,
+ *    llrint and llrintf which fix this problem as a side effect.
+ *
+ *    On Unix-like systems, the configure process should have detected the
+ *    presence of these functions. If they weren't found we have to replace them
+ *    here with a standard C cast.
+ */
+
+/*
+ *    The C99 prototypes for these functions are as follows:
+ *
+ *        int rintf(float x);
+ *        int rint(double x);
+ *        long int lrintf(float x);
+ *        long int lrint(double x);
+ *        long long int llrintf(float x);
+ *        long long int llrint(double x);
+ *
+ *    The presence of the required functions are detected during the configure
+ *    process and the values HAVE_LRINT and HAVE_LRINTF are set accordingly in
+ *    the config file.
+ */
+
+#if defined(__CYGWIN__)
+    /*
+     *    CYGWIN has lrint and lrintf functions, but they are slow and buggy:
+     *        http://sourceware.org/ml/cygwin/2005-06/msg00153.html
+     *        http://sourceware.org/ml/cygwin/2005-09/msg00047.html
+     *    The latest version of cygwin seems to have made no effort to fix this.
+     *    These replacement functions (pulled from the Public Domain MinGW
+     *    math.h header) replace the native versions.
+     */
+    static __inline__ long int lrint(double x)
+    {
+        long int retval;
+
+        __asm__ __volatile__
+        (
+            "fistpl %0"
+            : "=m" (retval)
+            : "t" (x)
+            : "st"
+        );
+
+        return retval;
+    }
+
+    static __inline__ long int lrintf(float x)
+    {
+        long int retval;
+
+        __asm__ __volatile__
+        (
+            "fistpl %0"
+            : "=m" (retval)
+            : "t" (x)
+            : "st"
+        );
+        return retval;
+    }
+
+    static __inline__ long int lfastrint(double x)
+    {
+        long int retval;
+
+        __asm__ __volatile__
+        (
+            "fistpl %0"
+            : "=m" (retval)
+            : "t" (x)
+            : "st"
+        );
+
+        return retval;
+    }
+
+    static __inline__ long int lfastrintf(float x)
+    {
+        long int retval;
+
+        __asm__ __volatile__
+        (
+            "fistpl %0"
+            : "=m" (retval)
+            : "t" (x)
+            : "st"
+        );
+        return retval;
+    }
+#elif defined(HAVE_LRINT)  &&  defined(HAVE_LRINTF)
+
+#if defined(__i386__)
+    /* These routines are guaranteed fast on an i386 machine. Using the built in
+       lrint() and lrintf() should be similar, but they may not always be enabled.
+       Sometimes, especially with "-O0", you might get slow calls to routines. */
+    static __inline__ long int lfastrint(double x)
+    {
+        long int retval;
+
+        __asm__ __volatile__
+        (
+            "fistpl %0"
+            : "=m" (retval)
+            : "t" (x)
+            : "st"
+        );
+
+        return retval;
+    }
+
+    static __inline__ long int lfastrintf(float x)
+    {
+        long int retval;
+
+        __asm__ __volatile__
+        (
+            "fistpl %0"
+            : "=m" (retval)
+            : "t" (x)
+            : "st"
+        );
+        return retval;
+    }
+#elif defined(__x86_64__)
+    /* On an x86_64 machine, the fastest thing seems to be a pure assignment from a
+       double or float to an int. It looks like the design on the x86_64 took account
+       of the default behaviour specified for C. */
+    static __inline__ long int lfastrint(double x)
+    {
+        return (long int) (x);
+    }
+
+    static __inline__ long int lfastrintf(float x)
+    {
+        return (long int) (x);
+    }
+#elif defined(__ppc__)  ||   defined(__powerpc__)
+    static __inline__ long int lfastrint(register double x)
+    {
+        int res[2];
+
+        __asm__ __volatile__
+        (
+            "fctiw %1, %1\n\t"
+            "stfd %1, %0"
+            : "=m" (res)    /* Output */
+            : "f" (x)       /* Input */
+            : "memory"
+        );
+
+        return res[1];
+    }
+
+    static __inline__ long int lfastrintf(register float x)
+    {
+        int res[2];
+
+        __asm__ __volatile__
+        (
+            "fctiw %1, %1\n\t"
+            "stfd %1, %0"
+            : "=m" (res)    /* Output */
+            : "f" (x)       /* Input */
+            : "memory"
+        );
+
+        return res[1];
+    }
+#endif
+
+#elif defined(WIN32)  ||  defined(_WIN32)
+    /*
+     *    Win32 doesn't seem to have the lrint() and lrintf() functions.
+     *    Therefore implement inline versions of these functions here.
+     */
+    __inline long int lrint(double x)
+    {
+        long int i;
+
+        _asm
+        {
+            fld x
+            fistp i
+        };
+        return i;
+    }
+
+    __inline long int lrintf(float x)
+    {
+        long int i;
+
+        _asm
+        {
+            fld x
+            fistp i
+        };
+        return i;
+    }
+
+    __inline long int lfastrint(double x)
+    {
+        long int i;
+
+        _asm
+        {
+            fld x
+            fistp i
+        };
+        return i;
+    }
+
+    __inline long int lfastrintf(float x)
+    {
+        long int i;
+
+        _asm
+        {
+            fld x
+            fistp i
+        };
+        return i;
+    }
+#elif defined(WIN64)  ||  defined(_WIN64)
+    /*
+     *    Win64 machines will do best with a simple assignment.
+     */
+    __inline long int lfastrint(double x)
+    {
+        return (long int) (x);
+    }
+
+    __inline long int lfastrintf(float x)
+    {
+        return (long int) (x);
+    }
+#elif defined(__MWERKS__)  &&  defined(macintosh)
+    /* This MacOS 9 solution was provided by Stephane Letz */
+
+    long int __inline__ lfastrint(register double x)
+    {
+        long int res[2];
+
+        asm
+        {
+            fctiw x, x
+            stfd x, res
+        }
+        return res[1];
+    }
+
+    long int __inline__ lfastrintf(register float x)
+    {
+        long int res[2];
+
+        asm
+        {
+            fctiw x, x
+            stfd x, res
+        }
+        return res[1];
+    }
+
+#elif defined(__MACH__)  &&  defined(__APPLE__)  &&  (defined(__ppc__)  ||  defined(__powerpc__))
+    /* For Apple Mac OS/X - do recent versions still need this? */
+
+    static __inline__ long int lfastrint(register double x)
+    {
+        int res[2];
+
+        __asm__ __volatile__
+        (
+            "fctiw %1, %1\n\t"
+            "stfd %1, %0"
+            : "=m" (res)    /* Output */
+            : "f" (x)       /* Input */
+            : "memory"
+        );
+
+        return res[1];
+    }
+
+    static __inline__ long int lfastrintf(register float x)
+    {
+        int res[2];
+
+        __asm__ __volatile__
+        (
+            "fctiw %1, %1\n\t"
+            "stfd %1, %0"
+            : "=m" (res)    /* Output */
+            : "f" (x)       /* Input */
+            : "memory"
+        );
+
+        return res[1];
+    }
+#else
+    /* There is nothing else to do, but use a simple casting operation, instead of a real
+       rint() type function. Since we are only trying to use rint() to speed up conversions,
+       the accuracy issues related to changing the rounding scheme are of little concern
+       to us. */
+
+    #if !defined(__sgi)
+        #warning "No usable lrint() and lrintf() functions available."
+        #warning "Replacing these functions with a simple C cast."
+    #endif
+
+    static __inline__ long int lfastrint(double x)
+    {
+        return (long int) (x);
+    }
+
+    static __inline__ long int lfastrintf(float x)
+    {
+        return (long int) (x);
+    }
+
+#endif
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
+
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/fsk.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/fsk.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,409 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * fsk.c - FSK modem transmit and receive parts
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2003 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: fsk.c,v 1.44 2008/07/16 17:01:49 steveu Exp $
+ */
+
+/*! \file */
+
+#if defined(HAVE_CONFIG_H)
+#include <config.h>
+#endif
+
+#include <stdlib.h>
+#include <inttypes.h>
+#include <string.h>
+#include "floating_fudge.h"
+#if defined(HAVE_TGMATH_H)
+#include <tgmath.h>
+#endif
+#if defined(HAVE_MATH_H)
+#include <math.h>
+#endif
+#include <assert.h>
+
+#include "spandsp/telephony.h"
+#include "spandsp/complex.h"
+#include "spandsp/dds.h"
+#include "spandsp/power_meter.h"
+#include "spandsp/async.h"
+#include "spandsp/fsk.h"
+
+const fsk_spec_t preset_fsk_specs[] =
+{
+    {
+        "V21 ch 1",
+        1080 + 100,
+        1080 - 100,
+        -14,
+        -30,
+        300*100
+    },
+    {
+        "V21 ch 2",
+        1750 + 100,
+        1750 - 100,
+        -14,
+        -30,
+        300*100
+    },
+    {
+        "V23 ch 1",
+        2100,
+        1300,
+        -14,
+        -30,
+        1200*100
+    },
+    {
+        "V23 ch 2",
+        450,
+        390,
+        -14,
+        -30,
+        75*100
+    },
+    {
+        "Bell103 ch 1",
+        2125 - 100,
+        2125 + 100,
+        -14,
+        -30,
+        300*100
+    },
+    {
+        "Bell103 ch 2",
+        1170 - 100,
+        1170 + 100,
+        -14,
+        -30,
+        300*100
+    },
+    {
+        "Bell202",
+        2200,
+        1200,
+        -14,
+        -30,
+        1200*100
+    },
+    {
+        "Weitbrecht",   /* Used for TDD (Telecoms Device for the Deaf) */
+        1800,
+        1400,
+        -14,
+        -30,
+         4545
+    }
+};
+
+fsk_tx_state_t *fsk_tx_init(fsk_tx_state_t *s,
+                            const fsk_spec_t *spec,
+                            get_bit_func_t get_bit,
+                            void *user_data)
+{
+    if (s == NULL)
+    {
+        if ((s = (fsk_tx_state_t *) malloc(sizeof(*s))) == NULL)
+            return NULL;
+    }
+
+    s->baud_rate = spec->baud_rate;
+    s->get_bit = get_bit;
+    s->get_bit_user_data = user_data;
+
+    s->phase_rates[0] = dds_phase_rate((float) spec->freq_zero);
+    s->phase_rates[1] = dds_phase_rate((float) spec->freq_one);
+    s->scaling = dds_scaling_dbm0((float) spec->tx_level);
+    /* Initialise fractional sample baud generation. */
+    s->phase_acc = 0;
+    s->baud_inc = s->baud_rate;
+    s->baud_frac = 0;
+    s->current_phase_rate = s->phase_rates[1];
+    
+    s->shutdown = FALSE;
+    return s;
+}
+/*- End of function --------------------------------------------------------*/
+
+int fsk_tx(fsk_tx_state_t *s, int16_t *amp, int len)
+{
+    int sample;
+    int bit;
+
+    if (s->shutdown)
+        return 0;
+    /* Make the transitions between 0 and 1 phase coherent, but instantaneous
+       jumps. There is currently no interpolation for bauds that end mid-sample.
+       Mainstream users will not care. Some specialist users might have a problem
+       with them, if they care about accurate transition timing. */
+    for (sample = 0;  sample < len;  sample++)
+    {
+        if ((s->baud_frac += s->baud_inc) >= SAMPLE_RATE*100)
+        {
+            s->baud_frac -= SAMPLE_RATE*100;
+            if ((bit = s->get_bit(s->get_bit_user_data)) == PUTBIT_END_OF_DATA)
+            {
+                if (s->status_handler)
+                    s->status_handler(s->status_user_data, MODEM_TX_STATUS_DATA_EXHAUSTED);
+                if (s->status_handler)
+                    s->status_handler(s->status_user_data, MODEM_TX_STATUS_SHUTDOWN_COMPLETE);
+                s->shutdown = TRUE;
+                break;
+            }
+            s->current_phase_rate = s->phase_rates[bit & 1];
+        }
+        amp[sample] = dds_mod(&(s->phase_acc), s->current_phase_rate, s->scaling, 0);
+    }
+    return sample;
+}
+/*- End of function --------------------------------------------------------*/
+
+void fsk_tx_power(fsk_tx_state_t *s, float power)
+{
+    s->scaling = dds_scaling_dbm0(power);
+}
+/*- End of function --------------------------------------------------------*/
+
+void fsk_tx_set_get_bit(fsk_tx_state_t *s, get_bit_func_t get_bit, void *user_data)
+{
+    s->get_bit = get_bit;
+    s->get_bit_user_data = user_data;
+}
+/*- End of function --------------------------------------------------------*/
+
+void fsk_tx_set_modem_status_handler(fsk_tx_state_t *s, modem_tx_status_func_t handler, void *user_data)
+{
+    s->status_handler = handler;
+    s->status_user_data = user_data;
+}
+/*- End of function --------------------------------------------------------*/
+
+void fsk_rx_signal_cutoff(fsk_rx_state_t *s, float cutoff)
+{
+    /* The 6.04 allows for the gain of the DC blocker */
+    s->carrier_on_power = (int32_t) (power_meter_level_dbm0(cutoff + 2.5f - 6.04f));
+    s->carrier_off_power = (int32_t) (power_meter_level_dbm0(cutoff - 2.5f - 6.04f));
+}
+/*- End of function --------------------------------------------------------*/
+
+float fsk_rx_signal_power(fsk_rx_state_t *s)
+{
+    return power_meter_current_dbm0(&s->power);
+}
+/*- End of function --------------------------------------------------------*/
+
+void fsk_rx_set_put_bit(fsk_rx_state_t *s, put_bit_func_t put_bit, void *user_data)
+{
+    s->put_bit = put_bit;
+    s->put_bit_user_data = user_data;
+}
+/*- End of function --------------------------------------------------------*/
+
+void fsk_rx_set_modem_status_handler(fsk_rx_state_t *s, modem_tx_status_func_t handler, void *user_data)
+{
+    s->status_handler = handler;
+    s->status_user_data = user_data;
+}
+/*- End of function --------------------------------------------------------*/
+
+fsk_rx_state_t *fsk_rx_init(fsk_rx_state_t *s,
+                            const fsk_spec_t *spec,
+                            int sync_mode,
+                            put_bit_func_t put_bit,
+                            void *user_data)
+{
+    int chop;
+
+    if (s == NULL)
+    {
+        if ((s = (fsk_rx_state_t *) malloc(sizeof(*s))) == NULL)
+            return NULL;
+    }
+
+    memset(s, 0, sizeof(*s));
+    s->baud_rate = spec->baud_rate;
+    s->sync_mode = sync_mode;
+    fsk_rx_signal_cutoff(s, (float) spec->min_level);
+    s->put_bit = put_bit;
+    s->put_bit_user_data = user_data;
+
+    /* Detect by correlating against the tones we want, over a period
+       of one baud. The correlation must be quadrature. */
+    
+    /* First we need the quadrature tone generators to correlate
+       against. */
+    s->phase_rate[0] = dds_phase_rate((float) spec->freq_zero);
+    s->phase_rate[1] = dds_phase_rate((float) spec->freq_one);
+    s->phase_acc[0] = 0;
+    s->phase_acc[1] = 0;
+    s->last_sample = 0;
+
+    /* The correlation should be over one baud. */
+    s->correlation_span = SAMPLE_RATE*100/spec->baud_rate;
+    /* But limit it for very slow baud rates, so we do not overflow our
+       buffer. */
+    if (s->correlation_span > FSK_MAX_WINDOW_LEN)
+        s->correlation_span = FSK_MAX_WINDOW_LEN;
+
+    /* We need to scale, to avoid overflow in the correlation. */
+    s->scaling_shift = 0;
+    chop = s->correlation_span;
+    while (chop != 0)
+    {
+        s->scaling_shift++;
+        chop >>= 1;
+    }
+
+    /* Initialise the baud/bit rate tracking. */
+    s->baud_inc = s->baud_rate;
+    s->baud_pll = 0;
+    
+    /* Initialise a power detector, so sense when a signal is present. */
+    power_meter_init(&(s->power), 4);
+    s->signal_present = 0;
+    return s;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void report_status_change(fsk_rx_state_t *s, int status)
+{
+    if (s->status_handler)
+        s->status_handler(s->status_user_data, status);
+    else if (s->put_bit)
+        s->put_bit(s->put_bit_user_data, status);
+}
+/*- End of function --------------------------------------------------------*/
+
+int fsk_rx(fsk_rx_state_t *s, const int16_t *amp, int len)
+{
+    int buf_ptr;
+    int baudstate;
+    int i;
+    int j;
+    int16_t x;
+    int32_t dot;
+    int32_t sum[2];
+    int32_t power;
+    complexi_t ph;
+
+    buf_ptr = s->buf_ptr;
+
+    for (i = 0;  i < len;  i++)
+    {
+        /* If there isn't much signal, don't demodulate - it will only produce
+           useless junk results. */
+        /* There should be no DC in the signal, but sometimes there is.
+           We need to measure the power with the DC blocked, but not using
+           a slow to respond DC blocker. Use the most elementary HPF. */
+        x = amp[i] >> 1;
+        power = power_meter_update(&(s->power), x - s->last_sample);
+        s->last_sample = x;
+        if (s->signal_present)
+        {
+            /* Look for power below turn-off threshold to turn the carrier off */
+            if (power < s->carrier_off_power)
+            {
+                if (--s->signal_present <= 0)
+                {
+                    /* Count down a short delay, to ensure we push the last
+                       few bits through the filters before stopping. */
+                    report_status_change(s, PUTBIT_CARRIER_DOWN);
+                    continue;
+                }
+            }
+        }
+        else
+        {
+            /* Look for power exceeding turn-on threshold to turn the carrier on */
+            if (power < s->carrier_on_power)
+                continue;
+            s->signal_present = 1;
+            report_status_change(s, PUTBIT_CARRIER_UP);
+        }
+        /* Non-coherent FSK demodulation by correlation with the target tones
+           over a one baud interval. The slow V.xx specs. are too open ended
+           to allow anything fancier to be used. The dot products are calculated
+           using a sliding window approach, so the compute load is not that great. */
+        /* The *totally* asynchronous character to character behaviour of these
+           modems, when carrying async. data, seems to force a sample by sample
+           approach. */
+        for (j = 0;  j < 2;  j++)
+        {
+            s->dot[j].re -= s->window[j][buf_ptr].re;
+            s->dot[j].im -= s->window[j][buf_ptr].im;
+
+            ph = dds_complexi(&(s->phase_acc[j]), s->phase_rate[j]);
+            s->window[j][buf_ptr].re = (ph.re*amp[i]) >> s->scaling_shift;
+            s->window[j][buf_ptr].im = (ph.im*amp[i]) >> s->scaling_shift;
+
+            s->dot[j].re += s->window[j][buf_ptr].re;
+            s->dot[j].im += s->window[j][buf_ptr].im;
+
+            dot = s->dot[j].re >> 15;
+            sum[j] = dot*dot;
+            dot = s->dot[j].im >> 15;
+            sum[j] += dot*dot;
+        }
+        baudstate = (sum[0] < sum[1]);
+
+        if (s->lastbit != baudstate)
+        {
+            s->lastbit = baudstate;
+            if (s->sync_mode)
+            {
+                /* For synchronous use (e.g. HDLC channels in FAX modems), nudge
+                   the baud phase gently, trying to keep it centred on the bauds. */
+                if (s->baud_pll < (SAMPLE_RATE*50))
+                    s->baud_pll += (s->baud_inc >> 3);
+                else
+                    s->baud_pll -= (s->baud_inc >> 3);
+            }
+            else
+            {
+                /* For async. operation, believe transitions completely, and
+                   sample appropriately. This allows instant start on the first
+                   transition. */
+                /* We must now be about half way to a sampling point. We do not do
+                   any fractional sample estimation of the transitions, so this is
+                   the most accurate baud alignment we can do. */
+                s->baud_pll = SAMPLE_RATE*50;
+            }
+
+        }
+        if ((s->baud_pll += s->baud_inc) >= (SAMPLE_RATE*100))
+        {
+            /* We should be in the middle of a baud now, so report the current
+               state as the next bit */
+            s->baud_pll -= (SAMPLE_RATE*100);
+            s->put_bit(s->put_bit_user_data, baudstate);
+        }
+        if (++buf_ptr >= s->correlation_span)
+            buf_ptr = 0;
+    }
+    s->buf_ptr = buf_ptr;
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/g711.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/g711.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,196 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * g711.c - A-law and u-law transcoding routines
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2006 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: g711.c,v 1.11 2008/07/02 14:48:25 steveu Exp $
+ */
+
+/*! \file */
+
+#if defined(HAVE_CONFIG_H)
+#include <config.h>
+#endif
+
+#include <inttypes.h>
+#include <stdlib.h>
+#include <string.h>
+#include "floating_fudge.h"
+#if defined(HAVE_TGMATH_H)
+#include <tgmath.h>
+#endif
+#if defined(HAVE_MATH_H)
+#include <math.h>
+#endif
+#include <assert.h>
+
+#include "spandsp/telephony.h"
+#include "spandsp/bit_operations.h"
+#include "spandsp/g711.h"
+
+/* Copied from the CCITT G.711 specification */
+static const uint8_t ulaw_to_alaw_table[256] =
+{
+     42,  43,  40,  41,  46,  47,  44,  45,  34,  35,  32,  33,  38,  39,  36,  37,
+     58,  59,  56,  57,  62,  63,  60,  61,  50,  51,  48,  49,  54,  55,  52,  53,
+     10,  11,   8,   9,  14,  15,  12,  13,   2,   3,   0,   1,   6,   7,   4,  26,
+     27,  24,  25,  30,  31,  28,  29,  18,  19,  16,  17,  22,  23,  20,  21, 106,
+    104, 105, 110, 111, 108, 109,  98,  99,  96,  97, 102, 103, 100, 101, 122, 120,
+    126, 127, 124, 125, 114, 115, 112, 113, 118, 119, 116, 117,  75,  73,  79,  77,
+     66,  67,  64,  65,  70,  71,  68,  69,  90,  91,  88,  89,  94,  95,  92,  93,
+     82,  82,  83,  83,  80,  80,  81,  81,  86,  86,  87,  87,  84,  84,  85,  85,
+    170, 171, 168, 169, 174, 175, 172, 173, 162, 163, 160, 161, 166, 167, 164, 165,
+    186, 187, 184, 185, 190, 191, 188, 189, 178, 179, 176, 177, 182, 183, 180, 181,
+    138, 139, 136, 137, 142, 143, 140, 141, 130, 131, 128, 129, 134, 135, 132, 154,
+    155, 152, 153, 158, 159, 156, 157, 146, 147, 144, 145, 150, 151, 148, 149, 234,
+    232, 233, 238, 239, 236, 237, 226, 227, 224, 225, 230, 231, 228, 229, 250, 248,
+    254, 255, 252, 253, 242, 243, 240, 241, 246, 247, 244, 245, 203, 201, 207, 205,
+    194, 195, 192, 193, 198, 199, 196, 197, 218, 219, 216, 217, 222, 223, 220, 221,
+    210, 210, 211, 211, 208, 208, 209, 209, 214, 214, 215, 215, 212, 212, 213, 213
+};
+
+/* These transcoding tables are copied from the CCITT G.711 specification. To achieve
+   optimal results, do not change them. */
+
+static const uint8_t alaw_to_ulaw_table[256] =
+{
+     42,  43,  40,  41,  46,  47,  44,  45,  34,  35,  32,  33,  38,  39,  36,  37,
+     57,  58,  55,  56,  61,  62,  59,  60,  49,  50,  47,  48,  53,  54,  51,  52,
+     10,  11,   8,   9,  14,  15,  12,  13,   2,   3,   0,   1,   6,   7,   4,   5,
+     26,  27,  24,  25,  30,  31,  28,  29,  18,  19,  16,  17,  22,  23,  20,  21,
+     98,  99,  96,  97, 102, 103, 100, 101,  93,  93,  92,  92,  95,  95,  94,  94,
+    116, 118, 112, 114, 124, 126, 120, 122, 106, 107, 104, 105, 110, 111, 108, 109,
+     72,  73,  70,  71,  76,  77,  74,  75,  64,  65,  63,  63,  68,  69,  66,  67,
+     86,  87,  84,  85,  90,  91,  88,  89,  79,  79,  78,  78,  82,  83,  80,  81,
+    170, 171, 168, 169, 174, 175, 172, 173, 162, 163, 160, 161, 166, 167, 164, 165,
+    185, 186, 183, 184, 189, 190, 187, 188, 177, 178, 175, 176, 181, 182, 179, 180,
+    138, 139, 136, 137, 142, 143, 140, 141, 130, 131, 128, 129, 134, 135, 132, 133,
+    154, 155, 152, 153, 158, 159, 156, 157, 146, 147, 144, 145, 150, 151, 148, 149,
+    226, 227, 224, 225, 230, 231, 228, 229, 221, 221, 220, 220, 223, 223, 222, 222,
+    244, 246, 240, 242, 252, 254, 248, 250, 234, 235, 232, 233, 238, 239, 236, 237,
+    200, 201, 198, 199, 204, 205, 202, 203, 192, 193, 191, 191, 196, 197, 194, 195,
+    214, 215, 212, 213, 218, 219, 216, 217, 207, 207, 206, 206, 210, 211, 208, 209
+};
+
+uint8_t alaw_to_ulaw(uint8_t alaw)
+{
+    return alaw_to_ulaw_table[alaw];
+}
+/*- End of function --------------------------------------------------------*/
+
+uint8_t ulaw_to_alaw(uint8_t ulaw)
+{
+    return ulaw_to_alaw_table[ulaw];
+}
+/*- End of function --------------------------------------------------------*/
+
+int g711_decode(g711_state_t *s,
+                int16_t amp[],
+                const uint8_t g711_data[],
+                int g711_bytes)
+{
+    int i;
+
+    if (s->mode == G711_ALAW)
+    {
+        for (i = 0;  i < g711_bytes;  i++)
+            amp[i] = alaw_to_linear(g711_data[i]);
+        /*endfor*/
+    }
+    else
+    {
+        for (i = 0;  i < g711_bytes;  i++)
+            amp[i] = ulaw_to_linear(g711_data[i]);
+        /*endfor*/
+    }
+    /*endif*/
+    return g711_bytes;
+}
+/*- End of function --------------------------------------------------------*/
+
+int g711_encode(g711_state_t *s,
+                uint8_t g711_data[],
+                const int16_t amp[],
+                int len)
+{
+    int i;
+
+    if (s->mode == G711_ALAW)
+    {
+        for (i = 0;  i < len;  i++)
+            g711_data[i] = linear_to_alaw(amp[i]);
+        /*endfor*/
+    }
+    else
+    {
+        for (i = 0;  i < len;  i++)
+            g711_data[i] = linear_to_ulaw(amp[i]);
+        /*endfor*/
+    }
+    /*endif*/
+    return len;
+}
+/*- End of function --------------------------------------------------------*/
+
+int g711_transcode(g711_state_t *s,
+                   uint8_t g711_out[],
+                   const uint8_t g711_in[],
+                   int g711_bytes)
+{
+    int i;
+
+    if (s->mode == G711_ALAW)
+    {
+        for (i = 0;  i < g711_bytes;  i++)
+            g711_out[i] = alaw_to_ulaw_table[g711_in[i]];
+        /*endfor*/
+    }
+    else
+    {
+        for (i = 0;  i < g711_bytes;  i++)
+            g711_out[i] = ulaw_to_alaw_table[g711_in[i]];
+        /*endfor*/
+    }
+    /*endif*/
+    return g711_bytes;
+}
+/*- End of function --------------------------------------------------------*/
+
+g711_state_t *g711_init(g711_state_t *s, int mode)
+{
+    if (s == NULL)
+    {
+        if ((s = (g711_state_t *) malloc(sizeof(*s))) == NULL)
+            return  NULL;
+    }
+    s->mode = mode;
+    return s;
+}
+/*- End of function --------------------------------------------------------*/
+
+int g711_release(g711_state_t *s)
+{
+    free(s);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/g722_decode.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/g722_decode.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,405 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * g722_decode.c - The ITU G.722 codec, decode part.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2005 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * Based in part on a single channel G.722 codec which is:
+ *
+ * Copyright (c) CMU 1993
+ * Computer Science, Speech Group
+ * Chengxiang Lu and Alex Hauptmann
+ *
+ * $Id: g722_decode.c,v 1.24 2008/07/02 14:48:25 steveu Exp $
+ */
+
+/*! \file */
+
+#if defined(HAVE_CONFIG_H)
+#include <config.h>
+#endif
+
+#include <inttypes.h>
+#include <memory.h>
+#include <stdlib.h>
+#include "floating_fudge.h"
+#if defined(HAVE_TGMATH_H)
+#include <tgmath.h>
+#endif
+#if defined(HAVE_MATH_H)
+#include <math.h>
+#endif
+
+#include "spandsp/telephony.h"
+#include "spandsp/dc_restore.h"
+#include "spandsp/g722.h"
+
+static void block4(g722_decode_state_t *s, int band, int d);
+
+static void block4(g722_decode_state_t *s, int band, int d)
+{
+    int wd1;
+    int wd2;
+    int wd3;
+    int i;
+
+    /* Block 4, RECONS */
+    s->band[band].d[0] = d;
+    s->band[band].r[0] = saturate(s->band[band].s + d);
+
+    /* Block 4, PARREC */
+    s->band[band].p[0] = saturate(s->band[band].sz + d);
+
+    /* Block 4, UPPOL2 */
+    for (i = 0;  i < 3;  i++)
+        s->band[band].sg[i] = s->band[band].p[i] >> 15;
+    wd1 = saturate(s->band[band].a[1] << 2);
+
+    wd2 = (s->band[band].sg[0] == s->band[band].sg[1])  ?  -wd1  :  wd1;
+    if (wd2 > 32767)
+        wd2 = 32767;
+    wd3 = (s->band[band].sg[0] == s->band[band].sg[2])  ?  128  :  -128;
+    wd3 += (wd2 >> 7);
+    wd3 += (s->band[band].a[2]*32512) >> 15;
+    if (wd3 > 12288)
+        wd3 = 12288;
+    else if (wd3 < -12288)
+        wd3 = -12288;
+    s->band[band].ap[2] = wd3;
+
+    /* Block 4, UPPOL1 */
+    s->band[band].sg[0] = s->band[band].p[0] >> 15;
+    s->band[band].sg[1] = s->band[band].p[1] >> 15;
+    wd1 = (s->band[band].sg[0] == s->band[band].sg[1])  ?  192  :  -192;
+    wd2 = (s->band[band].a[1]*32640) >> 15;
+
+    s->band[band].ap[1] = saturate(wd1 + wd2);
+    wd3 = saturate(15360 - s->band[band].ap[2]);
+    if (s->band[band].ap[1] > wd3)
+        s->band[band].ap[1] = wd3;
+    else if (s->band[band].ap[1] < -wd3)
+        s->band[band].ap[1] = -wd3;
+
+    /* Block 4, UPZERO */
+    wd1 = (d == 0)  ?  0  :  128;
+    s->band[band].sg[0] = d >> 15;
+    for (i = 1;  i < 7;  i++)
+    {
+        s->band[band].sg[i] = s->band[band].d[i] >> 15;
+        wd2 = (s->band[band].sg[i] == s->band[band].sg[0])  ?  wd1  :  -wd1;
+        wd3 = (s->band[band].b[i]*32640) >> 15;
+        s->band[band].bp[i] = saturate(wd2 + wd3);
+    }
+
+    /* Block 4, DELAYA */
+    for (i = 6;  i > 0;  i--)
+    {
+        s->band[band].d[i] = s->band[band].d[i - 1];
+        s->band[band].b[i] = s->band[band].bp[i];
+    }
+    
+    for (i = 2;  i > 0;  i--)
+    {
+        s->band[band].r[i] = s->band[band].r[i - 1];
+        s->band[band].p[i] = s->band[band].p[i - 1];
+        s->band[band].a[i] = s->band[band].ap[i];
+    }
+
+    /* Block 4, FILTEP */
+    wd1 = saturate(s->band[band].r[1] + s->band[band].r[1]);
+    wd1 = (s->band[band].a[1]*wd1) >> 15;
+    wd2 = saturate(s->band[band].r[2] + s->band[band].r[2]);
+    wd2 = (s->band[band].a[2]*wd2) >> 15;
+    s->band[band].sp = saturate(wd1 + wd2);
+
+    /* Block 4, FILTEZ */
+    s->band[band].sz = 0;
+    for (i = 6;  i > 0;  i--)
+    {
+        wd1 = saturate(s->band[band].d[i] + s->band[band].d[i]);
+        s->band[band].sz += (s->band[band].b[i]*wd1) >> 15;
+    }
+    s->band[band].sz = saturate(s->band[band].sz);
+
+    /* Block 4, PREDIC */
+    s->band[band].s = saturate(s->band[band].sp + s->band[band].sz);
+}
+/*- End of function --------------------------------------------------------*/
+
+g722_decode_state_t *g722_decode_init(g722_decode_state_t *s, int rate, int options)
+{
+    if (s == NULL)
+    {
+        if ((s = (g722_decode_state_t *) malloc(sizeof(*s))) == NULL)
+            return NULL;
+    }
+    memset(s, 0, sizeof(*s));
+    if (rate == 48000)
+        s->bits_per_sample = 6;
+    else if (rate == 56000)
+        s->bits_per_sample = 7;
+    else
+        s->bits_per_sample = 8;
+    if ((options & G722_SAMPLE_RATE_8000))
+        s->eight_k = TRUE;
+    if ((options & G722_PACKED)  &&  s->bits_per_sample != 8)
+        s->packed = TRUE;
+    else
+        s->packed = FALSE;
+    s->band[0].det = 32;
+    s->band[1].det = 8;
+    return s;
+}
+/*- End of function --------------------------------------------------------*/
+
+int g722_decode_release(g722_decode_state_t *s)
+{
+    free(s);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int g722_decode(g722_decode_state_t *s, int16_t amp[], const uint8_t g722_data[], int len)
+{
+    static const int wl[8] =
+    {
+        -60, -30, 58, 172, 334, 538, 1198, 3042
+    };
+    static const int rl42[16] =
+    {
+        0, 7, 6, 5, 4, 3, 2, 1, 7, 6, 5, 4, 3,  2, 1, 0
+    };
+    static const int ilb[32] =
+    {
+        2048, 2093, 2139, 2186, 2233, 2282, 2332,
+        2383, 2435, 2489, 2543, 2599, 2656, 2714,
+        2774, 2834, 2896, 2960, 3025, 3091, 3158,
+        3228, 3298, 3371, 3444, 3520, 3597, 3676,
+        3756, 3838, 3922, 4008
+    };
+    static const int wh[3] =
+    {
+        0, -214, 798
+    };
+    static const int rh2[4] =
+    {
+        2, 1, 2, 1
+    };
+    static const int qm2[4] =
+    {
+        -7408, -1616,  7408,   1616
+    };
+    static const int qm4[16] = 
+    {
+              0, -20456, -12896,  -8968, 
+          -6288,  -4240,  -2584,  -1200,
+          20456,  12896,   8968,   6288,
+           4240,   2584,   1200,      0
+    };
+    static const int qm5[32] =
+    {
+           -280,   -280, -23352, -17560,
+         -14120, -11664,  -9752,  -8184,
+          -6864,  -5712,  -4696,  -3784,
+          -2960,  -2208,  -1520,   -880,
+          23352,  17560,  14120,  11664,
+           9752,   8184,   6864,   5712,
+           4696,   3784,   2960,   2208,
+           1520,    880,    280,   -280
+    };
+    static const int qm6[64] =
+    {
+           -136,   -136,   -136,   -136,
+         -24808, -21904, -19008, -16704,
+         -14984, -13512, -12280, -11192,
+         -10232,  -9360,  -8576,  -7856,
+          -7192,  -6576,  -6000,  -5456,
+          -4944,  -4464,  -4008,  -3576,
+          -3168,  -2776,  -2400,  -2032,
+          -1688,  -1360,  -1040,   -728,
+          24808,  21904,  19008,  16704,
+          14984,  13512,  12280,  11192,
+          10232,   9360,   8576,   7856,
+           7192,   6576,   6000,   5456,
+           4944,   4464,   4008,   3576,
+           3168,   2776,   2400,   2032,
+           1688,   1360,   1040,    728,
+            432,    136,   -432,   -136
+    };
+    static const int qmf_coeffs[12] =
+    {
+           3,  -11,   12,   32, -210,  951, 3876, -805,  362, -156,   53,  -11,
+    };
+    int dlowt;
+    int rlow;
+    int ihigh;
+    int dhigh;
+    int rhigh;
+    int xout1;
+    int xout2;
+    int wd1;
+    int wd2;
+    int wd3;
+    int code;
+    int outlen;
+    int i;
+    int j;
+
+    outlen = 0;
+    rhigh = 0;
+    for (j = 0;  j < len;  )
+    {
+        if (s->packed)
+        {
+            /* Unpack the code bits */
+            if (s->in_bits < s->bits_per_sample)
+            {
+                s->in_buffer |= (g722_data[j++] << s->in_bits);
+                s->in_bits += 8;
+            }
+            code = s->in_buffer & ((1 << s->bits_per_sample) - 1);
+            s->in_buffer >>= s->bits_per_sample;
+            s->in_bits -= s->bits_per_sample;
+        }
+        else
+        {
+            code = g722_data[j++];
+        }
+
+        switch (s->bits_per_sample)
+        {
+        default:
+        case 8:
+            wd1 = code & 0x3F;
+            ihigh = (code >> 6) & 0x03;
+            wd2 = qm6[wd1];
+            wd1 >>= 2;
+            break;
+        case 7:
+            wd1 = code & 0x1F;
+            ihigh = (code >> 5) & 0x03;
+            wd2 = qm5[wd1];
+            wd1 >>= 1;
+            break;
+        case 6:
+            wd1 = code & 0x0F;
+            ihigh = (code >> 4) & 0x03;
+            wd2 = qm4[wd1];
+            break;
+        }
+        /* Block 5L, LOW BAND INVQBL */
+        wd2 = (s->band[0].det*wd2) >> 15;
+        /* Block 5L, RECONS */
+        rlow = s->band[0].s + wd2;
+        /* Block 6L, LIMIT */
+        if (rlow > 16383)
+            rlow = 16383;
+        else if (rlow < -16384)
+            rlow = -16384;
+
+        /* Block 2L, INVQAL */
+        wd2 = qm4[wd1];
+        dlowt = (s->band[0].det*wd2) >> 15;
+
+        /* Block 3L, LOGSCL */
+        wd2 = rl42[wd1];
+        wd1 = (s->band[0].nb*127) >> 7;
+        wd1 += wl[wd2];
+        if (wd1 < 0)
+            wd1 = 0;
+        else if (wd1 > 18432)
+            wd1 = 18432;
+        s->band[0].nb = wd1;
+            
+        /* Block 3L, SCALEL */
+        wd1 = (s->band[0].nb >> 6) & 31;
+        wd2 = 8 - (s->band[0].nb >> 11);
+        wd3 = (wd2 < 0)  ?  (ilb[wd1] << -wd2)  :  (ilb[wd1] >> wd2);
+        s->band[0].det = wd3 << 2;
+
+        block4(s, 0, dlowt);
+        
+        if (!s->eight_k)
+        {
+            /* Block 2H, INVQAH */
+            wd2 = qm2[ihigh];
+            dhigh = (s->band[1].det*wd2) >> 15;
+            /* Block 5H, RECONS */
+            rhigh = dhigh + s->band[1].s;
+            /* Block 6H, LIMIT */
+            if (rhigh > 16383)
+                rhigh = 16383;
+            else if (rhigh < -16384)
+                rhigh = -16384;
+
+            /* Block 2H, INVQAH */
+            wd2 = rh2[ihigh];
+            wd1 = (s->band[1].nb*127) >> 7;
+            wd1 += wh[wd2];
+            if (wd1 < 0)
+                wd1 = 0;
+            else if (wd1 > 22528)
+                wd1 = 22528;
+            s->band[1].nb = wd1;
+            
+            /* Block 3H, SCALEH */
+            wd1 = (s->band[1].nb >> 6) & 31;
+            wd2 = 10 - (s->band[1].nb >> 11);
+            wd3 = (wd2 < 0)  ?  (ilb[wd1] << -wd2)  :  (ilb[wd1] >> wd2);
+            s->band[1].det = wd3 << 2;
+
+            block4(s, 1, dhigh);
+        }
+
+        if (s->itu_test_mode)
+        {
+            amp[outlen++] = (int16_t) (rlow << 1);
+            amp[outlen++] = (int16_t) (rhigh << 1);
+        }
+        else
+        {
+            if (s->eight_k)
+            {
+                amp[outlen++] = (int16_t) rlow;
+            }
+            else
+            {
+                /* Apply the receive QMF */
+                memcpy(s->x, &s->x[2], 22*sizeof(s->x[0]));
+                s->x[22] = rlow + rhigh;
+                s->x[23] = rlow - rhigh;
+
+                xout1 = 0;
+                xout2 = 0;
+                for (i = 0;  i < 12;  i++)
+                {
+                    xout2 += s->x[2*i]*qmf_coeffs[i];
+                    xout1 += s->x[2*i + 1]*qmf_coeffs[11 - i];
+                }
+                amp[outlen++] = (int16_t) (xout1 >> 12);
+                amp[outlen++] = (int16_t) (xout2 >> 12);
+            }
+        }
+    }
+    return outlen;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/g722_encode.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/g722_encode.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,391 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * g722_encode.c - The ITU G.722 codec, encode part.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2005 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * Based on a single channel 64kbps only G.722 codec which is:
+ *
+ *****    Copyright (c) CMU    1993      *****
+ * Computer Science, Speech Group
+ * Chengxiang Lu and Alex Hauptmann
+ *
+ * $Id: g722_encode.c,v 1.22 2008/07/02 14:48:25 steveu Exp $
+ */
+
+/*! \file */
+
+#if defined(HAVE_CONFIG_H)
+#include <config.h>
+#endif
+
+#include <inttypes.h>
+#include <memory.h>
+#include <stdlib.h>
+#include "floating_fudge.h"
+#if defined(HAVE_TGMATH_H)
+#include <tgmath.h>
+#endif
+#if defined(HAVE_MATH_H)
+#include <math.h>
+#endif
+
+#include "spandsp/telephony.h"
+#include "spandsp/dc_restore.h"
+#include "spandsp/g722.h"
+
+static void block4(g722_encode_state_t *s, int band, int d)
+{
+    int wd1;
+    int wd2;
+    int wd3;
+    int i;
+
+    /* Block 4, RECONS */
+    s->band[band].d[0] = d;
+    s->band[band].r[0] = saturate(s->band[band].s + d);
+
+    /* Block 4, PARREC */
+    s->band[band].p[0] = saturate(s->band[band].sz + d);
+
+    /* Block 4, UPPOL2 */
+    for (i = 0;  i < 3;  i++)
+        s->band[band].sg[i] = s->band[band].p[i] >> 15;
+    wd1 = saturate(s->band[band].a[1] << 2);
+
+    wd2 = (s->band[band].sg[0] == s->band[band].sg[1])  ?  -wd1  :  wd1;
+    if (wd2 > 32767)
+        wd2 = 32767;
+    wd3 = (wd2 >> 7) + ((s->band[band].sg[0] == s->band[band].sg[2])  ?  128  :  -128);
+    wd3 += (s->band[band].a[2]*32512) >> 15;
+    if (wd3 > 12288)
+        wd3 = 12288;
+    else if (wd3 < -12288)
+        wd3 = -12288;
+    s->band[band].ap[2] = wd3;
+
+    /* Block 4, UPPOL1 */
+    s->band[band].sg[0] = s->band[band].p[0] >> 15;
+    s->band[band].sg[1] = s->band[band].p[1] >> 15;
+    wd1 = (s->band[band].sg[0] == s->band[band].sg[1])  ?  192  :  -192;
+    wd2 = (s->band[band].a[1]*32640) >> 15;
+
+    s->band[band].ap[1] = saturate(wd1 + wd2);
+    wd3 = saturate(15360 - s->band[band].ap[2]);
+    if (s->band[band].ap[1] > wd3)
+        s->band[band].ap[1] = wd3;
+    else if (s->band[band].ap[1] < -wd3)
+        s->band[band].ap[1] = -wd3;
+
+    /* Block 4, UPZERO */
+    wd1 = (d == 0)  ?  0  :  128;
+    s->band[band].sg[0] = d >> 15;
+    for (i = 1;  i < 7;  i++)
+    {
+        s->band[band].sg[i] = s->band[band].d[i] >> 15;
+        wd2 = (s->band[band].sg[i] == s->band[band].sg[0])  ?  wd1  :  -wd1;
+        wd3 = (s->band[band].b[i]*32640) >> 15;
+        s->band[band].bp[i] = saturate(wd2 + wd3);
+    }
+
+    /* Block 4, DELAYA */
+    for (i = 6;  i > 0;  i--)
+    {
+        s->band[band].d[i] = s->band[band].d[i - 1];
+        s->band[band].b[i] = s->band[band].bp[i];
+    }
+    
+    for (i = 2;  i > 0;  i--)
+    {
+        s->band[band].r[i] = s->band[band].r[i - 1];
+        s->band[band].p[i] = s->band[band].p[i - 1];
+        s->band[band].a[i] = s->band[band].ap[i];
+    }
+
+    /* Block 4, FILTEP */
+    wd1 = saturate(s->band[band].r[1] + s->band[band].r[1]);
+    wd1 = (s->band[band].a[1]*wd1) >> 15;
+    wd2 = saturate(s->band[band].r[2] + s->band[band].r[2]);
+    wd2 = (s->band[band].a[2]*wd2) >> 15;
+    s->band[band].sp = saturate(wd1 + wd2);
+
+    /* Block 4, FILTEZ */
+    s->band[band].sz = 0;
+    for (i = 6;  i > 0;  i--)
+    {
+        wd1 = saturate(s->band[band].d[i] + s->band[band].d[i]);
+        s->band[band].sz += (s->band[band].b[i]*wd1) >> 15;
+    }
+    s->band[band].sz = saturate(s->band[band].sz);
+
+    /* Block 4, PREDIC */
+    s->band[band].s = saturate(s->band[band].sp + s->band[band].sz);
+}
+/*- End of function --------------------------------------------------------*/
+
+g722_encode_state_t *g722_encode_init(g722_encode_state_t *s, int rate, int options)
+{
+    if (s == NULL)
+    {
+        if ((s = (g722_encode_state_t *) malloc(sizeof(*s))) == NULL)
+            return NULL;
+    }
+    memset(s, 0, sizeof(*s));
+    if (rate == 48000)
+        s->bits_per_sample = 6;
+    else if (rate == 56000)
+        s->bits_per_sample = 7;
+    else
+        s->bits_per_sample = 8;
+    if ((options & G722_SAMPLE_RATE_8000))
+        s->eight_k = TRUE;
+    if ((options & G722_PACKED)  &&  s->bits_per_sample != 8)
+        s->packed = TRUE;
+    else
+        s->packed = FALSE;
+    s->band[0].det = 32;
+    s->band[1].det = 8;
+    return s;
+}
+/*- End of function --------------------------------------------------------*/
+
+int g722_encode_release(g722_encode_state_t *s)
+{
+    free(s);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int g722_encode(g722_encode_state_t *s, uint8_t g722_data[], const int16_t amp[], int len)
+{
+    static const int q6[32] =
+    {
+           0,   35,   72,  110,  150,  190,  233,  276,
+         323,  370,  422,  473,  530,  587,  650,  714,
+         786,  858,  940, 1023, 1121, 1219, 1339, 1458,
+        1612, 1765, 1980, 2195, 2557, 2919,    0,    0
+    };
+    static const int iln[32] =
+    {
+         0, 63, 62, 31, 30, 29, 28, 27,
+        26, 25, 24, 23, 22, 21, 20, 19,
+        18, 17, 16, 15, 14, 13, 12, 11,
+        10,  9,  8,  7,  6,  5,  4,  0
+    };
+    static const int ilp[32] =
+    {
+         0, 61, 60, 59, 58, 57, 56, 55,
+        54, 53, 52, 51, 50, 49, 48, 47,
+        46, 45, 44, 43, 42, 41, 40, 39,
+        38, 37, 36, 35, 34, 33, 32,  0
+    };
+    static const int wl[8] =
+    {
+        -60, -30, 58, 172, 334, 538, 1198, 3042
+    };
+    static const int rl42[16] =
+    {
+        0, 7, 6, 5, 4, 3, 2, 1, 7, 6, 5, 4, 3, 2, 1, 0
+    };
+    static const int ilb[32] =
+    {
+        2048, 2093, 2139, 2186, 2233, 2282, 2332,
+        2383, 2435, 2489, 2543, 2599, 2656, 2714,
+        2774, 2834, 2896, 2960, 3025, 3091, 3158,
+        3228, 3298, 3371, 3444, 3520, 3597, 3676,
+        3756, 3838, 3922, 4008
+    };
+    static const int qm4[16] =
+    {
+             0, -20456, -12896, -8968,
+         -6288,  -4240,  -2584, -1200,
+         20456,  12896,   8968,  6288,
+          4240,   2584,   1200,     0
+    };
+    static const int qm2[4] =
+    {
+        -7408,  -1616,   7408,   1616
+    };
+    static const int qmf_coeffs[12] =
+    {
+           3,  -11,   12,   32, -210,  951, 3876, -805,  362, -156,   53,  -11,
+    };
+    static const int ihn[3] = {0, 1, 0};
+    static const int ihp[3] = {0, 3, 2};
+    static const int wh[3] = {0, -214, 798};
+    static const int rh2[4] = {2, 1, 2, 1};
+
+    int dlow;
+    int dhigh;
+    int el;
+    int wd;
+    int wd1;
+    int ril;
+    int wd2;
+    int il4;
+    int ih2;
+    int wd3;
+    int eh;
+    int mih;
+    int i;
+    int j;
+    /* Low and high band PCM from the QMF */
+    int xlow;
+    int xhigh;
+    int g722_bytes;
+    /* Even and odd tap accumulators */
+    int sumeven;
+    int sumodd;
+    int ihigh;
+    int ilow;
+    int code;
+
+    g722_bytes = 0;
+    xhigh = 0;
+    for (j = 0;  j < len;  )
+    {
+        if (s->itu_test_mode)
+        {
+            xlow =
+            xhigh = amp[j++] >> 1;
+        }
+        else
+        {
+            if (s->eight_k)
+            {
+                xlow = amp[j++];
+            }
+            else
+            {
+                /* Apply the transmit QMF */
+                /* Shuffle the buffer down */
+                memcpy(s->x, &s->x[2], 22*sizeof(s->x[0]));
+                s->x[22] = amp[j++];
+                s->x[23] = amp[j++];
+    
+                /* Discard every other QMF output */
+                sumeven = 0;
+                sumodd = 0;
+                for (i = 0;  i < 12;  i++)
+                {
+                    sumodd += s->x[2*i]*qmf_coeffs[i];
+                    sumeven += s->x[2*i + 1]*qmf_coeffs[11 - i];
+                }
+                xlow = (sumeven + sumodd) >> 13;
+                xhigh = (sumeven - sumodd) >> 13;
+            }
+        }
+        /* Block 1L, SUBTRA */
+        el = saturate(xlow - s->band[0].s);
+
+        /* Block 1L, QUANTL */
+        wd = (el >= 0)  ?  el  :  -(el + 1);
+
+        for (i = 1;  i < 30;  i++)
+        {
+            wd1 = (q6[i]*s->band[0].det) >> 12;
+            if (wd < wd1)
+                break;
+        }
+        ilow = (el < 0)  ?  iln[i]  :  ilp[i];
+
+        /* Block 2L, INVQAL */
+        ril = ilow >> 2;
+        wd2 = qm4[ril];
+        dlow = (s->band[0].det*wd2) >> 15;
+
+        /* Block 3L, LOGSCL */
+        il4 = rl42[ril];
+        wd = (s->band[0].nb*127) >> 7;
+        s->band[0].nb = wd + wl[il4];
+        if (s->band[0].nb < 0)
+            s->band[0].nb = 0;
+        else if (s->band[0].nb > 18432)
+            s->band[0].nb = 18432;
+
+        /* Block 3L, SCALEL */
+        wd1 = (s->band[0].nb >> 6) & 31;
+        wd2 = 8 - (s->band[0].nb >> 11);
+        wd3 = (wd2 < 0)  ?  (ilb[wd1] << -wd2)  :  (ilb[wd1] >> wd2);
+        s->band[0].det = wd3 << 2;
+
+        block4(s, 0, dlow);
+        
+        if (s->eight_k)
+        {
+            /* Just leave the high bits as zero */
+            code = (0xC0 | ilow) >> (8 - s->bits_per_sample);
+        }
+        else
+        {
+            /* Block 1H, SUBTRA */
+            eh = saturate(xhigh - s->band[1].s);
+
+            /* Block 1H, QUANTH */
+            wd = (eh >= 0)  ?  eh  :  -(eh + 1);
+            wd1 = (564*s->band[1].det) >> 12;
+            mih = (wd >= wd1)  ?  2  :  1;
+            ihigh = (eh < 0)  ?  ihn[mih]  :  ihp[mih];
+
+            /* Block 2H, INVQAH */
+            wd2 = qm2[ihigh];
+            dhigh = (s->band[1].det*wd2) >> 15;
+
+            /* Block 3H, LOGSCH */
+            ih2 = rh2[ihigh];
+            wd = (s->band[1].nb*127) >> 7;
+            s->band[1].nb = wd + wh[ih2];
+            if (s->band[1].nb < 0)
+                s->band[1].nb = 0;
+            else if (s->band[1].nb > 22528)
+                s->band[1].nb = 22528;
+
+            /* Block 3H, SCALEH */
+            wd1 = (s->band[1].nb >> 6) & 31;
+            wd2 = 10 - (s->band[1].nb >> 11);
+            wd3 = (wd2 < 0)  ?  (ilb[wd1] << -wd2)  :  (ilb[wd1] >> wd2);
+            s->band[1].det = wd3 << 2;
+
+            block4(s, 1, dhigh);
+            code = ((ihigh << 6) | ilow) >> (8 - s->bits_per_sample);
+        }
+
+        if (s->packed)
+        {
+            /* Pack the code bits */
+            s->out_buffer |= (code << s->out_bits);
+            s->out_bits += s->bits_per_sample;
+            if (s->out_bits >= 8)
+            {
+                g722_data[g722_bytes++] = (uint8_t) (s->out_buffer & 0xFF);
+                s->out_bits -= 8;
+                s->out_buffer >>= 8;
+            }
+        }
+        else
+        {
+            g722_data[g722_bytes++] = (uint8_t) code;
+        }
+    }
+    return g722_bytes;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/g726.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/g726.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,1179 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * g726.c - The ITU G.726 codec.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2006 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * Based on G.721/G.723 code which is:
+ *
+ * This source code is a product of Sun Microsystems, Inc. and is provided
+ * for unrestricted use.  Users may copy or modify this source code without
+ * charge.
+ *
+ * SUN SOURCE CODE IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING
+ * THE WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
+ *
+ * Sun source code is provided with no support and without any obligation on
+ * the part of Sun Microsystems, Inc. to assist in its use, correction,
+ * modification or enhancement.
+ *
+ * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
+ * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY THIS SOFTWARE
+ * OR ANY PART THEREOF.
+ *
+ * In no event will Sun Microsystems, Inc. be liable for any lost revenue
+ * or profits or other special, indirect and consequential damages, even if
+ * Sun has been advised of the possibility of such damages.
+ *
+ * Sun Microsystems, Inc.
+ * 2550 Garcia Avenue
+ * Mountain View, California  94043
+ *
+ * $Id: g726.c,v 1.22 2008/07/02 14:48:25 steveu Exp $
+ */
+
+/*! \file */
+
+#if defined(HAVE_CONFIG_H)
+#include <config.h>
+#endif
+
+#include <inttypes.h>
+#include <memory.h>
+#include <stdlib.h>
+#include "floating_fudge.h"
+#if defined(HAVE_TGMATH_H)
+#include <tgmath.h>
+#endif
+#if defined(HAVE_MATH_H)
+#include <math.h>
+#endif
+
+#include "spandsp/telephony.h"
+#include "spandsp/dc_restore.h"
+#include "spandsp/bitstream.h"
+#include "spandsp/bit_operations.h"
+#include "spandsp/g711.h"
+#include "spandsp/g726.h"
+
+/*
+ * Maps G.726_16 code word to reconstructed scale factor normalized log
+ * magnitude values.
+ */
+static const int g726_16_dqlntab[4] =
+{
+    116, 365, 365, 116
+};
+
+/* Maps G.726_16 code word to log of scale factor multiplier. */
+static const int g726_16_witab[4] =
+{
+    -704, 14048, 14048, -704
+};
+
+/*
+ * Maps G.726_16 code words to a set of values whose long and short
+ * term averages are computed and then compared to give an indication
+ * how stationary (steady state) the signal is.
+ */
+static const int g726_16_fitab[4] =
+{
+    0x000, 0xE00, 0xE00, 0x000
+};
+
+static const int qtab_726_16[1] =
+{
+    261
+};
+
+/*
+ * Maps G.726_24 code word to reconstructed scale factor normalized log
+ * magnitude values.
+ */
+static const int g726_24_dqlntab[8] =
+{
+    -2048, 135, 273, 373, 373, 273, 135, -2048
+};
+
+/* Maps G.726_24 code word to log of scale factor multiplier. */
+static const int g726_24_witab[8] =
+{
+    -128, 960, 4384, 18624, 18624, 4384, 960, -128
+};
+
+/*
+ * Maps G.726_24 code words to a set of values whose long and short
+ * term averages are computed and then compared to give an indication
+ * how stationary (steady state) the signal is.
+ */
+static const int g726_24_fitab[8] =
+{
+    0x000, 0x200, 0x400, 0xE00, 0xE00, 0x400, 0x200, 0x000
+};
+
+static const int qtab_726_24[3] =
+{
+    8, 218, 331
+};
+
+/*
+ * Maps G.726_32 code word to reconstructed scale factor normalized log
+ * magnitude values.
+ */
+static const int g726_32_dqlntab[16] =
+{
+    -2048,   4, 135, 213, 273, 323, 373,   425,
+      425, 373, 323, 273, 213, 135,   4, -2048
+};
+
+/* Maps G.726_32 code word to log of scale factor multiplier. */
+static const int g726_32_witab[16] =
+{
+     -384,   576,  1312,  2048,  3584,  6336, 11360, 35904,
+    35904, 11360,  6336,  3584,  2048,  1312,   576,  -384
+};
+
+/*
+ * Maps G.726_32 code words to a set of values whose long and short
+ * term averages are computed and then compared to give an indication
+ * how stationary (steady state) the signal is.
+ */
+static const int g726_32_fitab[16] =
+{
+    0x000, 0x000, 0x000, 0x200, 0x200, 0x200, 0x600, 0xE00,
+    0xE00, 0x600, 0x200, 0x200, 0x200, 0x000, 0x000, 0x000
+};
+
+static const int qtab_726_32[7] =
+{
+    -124, 80, 178, 246, 300, 349, 400
+};
+
+/*
+ * Maps G.726_40 code word to ructeconstructed scale factor normalized log
+ * magnitude values.
+ */
+static const int g726_40_dqlntab[32] =
+{
+    -2048, -66, 28, 104, 169, 224, 274, 318,
+      358, 395, 429, 459, 488, 514, 539, 566,
+      566, 539, 514, 488, 459, 429, 395, 358,
+      318, 274, 224, 169, 104, 28, -66, -2048
+};
+
+/* Maps G.726_40 code word to log of scale factor multiplier. */
+static const int g726_40_witab[32] =
+{
+      448,   448,   768,  1248,  1280,  1312,  1856,  3200,
+     4512,  5728,  7008,  8960, 11456, 14080, 16928, 22272,
+    22272, 16928, 14080, 11456,  8960,  7008,  5728,  4512,
+     3200,  1856,  1312,  1280,  1248,   768,   448,   448
+};
+
+/*
+ * Maps G.726_40 code words to a set of values whose long and short
+ * term averages are computed and then compared to give an indication
+ * how stationary (steady state) the signal is.
+ */
+static const int g726_40_fitab[32] =
+{
+    0x000, 0x000, 0x000, 0x000, 0x000, 0x200, 0x200, 0x200,
+    0x200, 0x200, 0x400, 0x600, 0x800, 0xA00, 0xC00, 0xC00,
+    0xC00, 0xC00, 0xA00, 0x800, 0x600, 0x400, 0x200, 0x200,
+    0x200, 0x200, 0x200, 0x000, 0x000, 0x000, 0x000, 0x000
+};
+
+static const int qtab_726_40[15] =
+{
+    -122, -16,  68, 139, 198, 250, 298, 339,
+     378, 413, 445, 475, 502, 528, 553
+};
+
+/*
+ * returns the integer product of the 14-bit integer "an" and
+ * "floating point" representation (4-bit exponent, 6-bit mantessa) "srn".
+ */
+static int16_t fmult(int16_t an, int16_t srn)
+{
+    int16_t anmag;
+    int16_t anexp;
+    int16_t anmant;
+    int16_t wanexp;
+    int16_t wanmant;
+    int16_t retval;
+
+    anmag = (an > 0)  ?  an  :  ((-an) & 0x1FFF);
+    anexp = (int16_t) (top_bit(anmag) - 5);
+    anmant = (anmag == 0)  ?  32  :  (anexp >= 0)  ?  (anmag >> anexp)  :  (anmag << -anexp);
+    wanexp = anexp + ((srn >> 6) & 0xF) - 13;
+
+    wanmant = (anmant*(srn & 0x3F) + 0x30) >> 4;
+    retval = (wanexp >= 0)  ?  ((wanmant << wanexp) & 0x7FFF)  :  (wanmant >> -wanexp);
+
+    return (((an ^ srn) < 0)  ?  -retval  :  retval);
+}
+/*- End of function --------------------------------------------------------*/
+
+/*
+ * Compute the estimated signal from the 6-zero predictor.
+ */
+static __inline__ int16_t predictor_zero(g726_state_t *s)
+{
+    int i;
+    int sezi;
+
+    sezi = fmult(s->b[0] >> 2, s->dq[0]);
+    /* ACCUM */
+    for (i = 1;  i < 6;  i++)
+        sezi += fmult(s->b[i] >> 2, s->dq[i]);
+    return (int16_t) sezi;
+}
+/*- End of function --------------------------------------------------------*/
+
+/*
+ * Computes the estimated signal from the 2-pole predictor.
+ */
+static __inline__ int16_t predictor_pole(g726_state_t *s)
+{
+    return (fmult(s->a[1] >> 2, s->sr[1]) + fmult(s->a[0] >> 2, s->sr[0]));
+}
+/*- End of function --------------------------------------------------------*/
+
+/*
+ * Computes the quantization step size of the adaptive quantizer.
+ */
+static int step_size(g726_state_t *s)
+{
+    int y;
+    int dif;
+    int al;
+
+    if (s->ap >= 256)
+        return s->yu;
+    y = s->yl >> 6;
+    dif = s->yu - y;
+    al = s->ap >> 2;
+    if (dif > 0)
+        y += (dif*al) >> 6;
+    else if (dif < 0)
+        y += (dif*al + 0x3F) >> 6;
+    return y;
+}
+/*- End of function --------------------------------------------------------*/
+
+/*
+ * Given a raw sample, 'd', of the difference signal and a
+ * quantization step size scale factor, 'y', this routine returns the
+ * ADPCM codeword to which that sample gets quantized.  The step
+ * size scale factor division operation is done in the log base 2 domain
+ * as a subtraction.
+ */
+static int16_t quantize(int d,                  /* Raw difference signal sample */
+                        int y,                  /* Step size multiplier */
+                        const int table[],     /* quantization table */
+                        int quantizer_states)   /* table size of int16_t integers */
+{
+    int16_t dqm;    /* Magnitude of 'd' */
+    int16_t exp;    /* Integer part of base 2 log of 'd' */
+    int16_t mant;   /* Fractional part of base 2 log */
+    int16_t dl;     /* Log of magnitude of 'd' */
+    int16_t dln;    /* Step size scale factor normalized log */
+    int i;
+    int size;
+
+    /*
+     * LOG
+     *
+     * Compute base 2 log of 'd', and store in 'dl'.
+     */
+    dqm = (int16_t) abs(d);
+    exp = (int16_t) (top_bit(dqm >> 1) + 1);
+    /* Fractional portion. */
+    mant = ((dqm << 7) >> exp) & 0x7F;
+    dl = (exp << 7) + mant;
+
+    /*
+     * SUBTB
+     *
+     * "Divide" by step size multiplier.
+     */
+    dln = dl - (int16_t) (y >> 2);
+
+    /*
+     * QUAN
+     *
+     * Search for codword i for 'dln'.
+     */
+    size = (quantizer_states - 1) >> 1;
+    for (i = 0;  i < size;  i++)
+    {
+        if (dln < table[i])
+            break;
+    }
+    if (d < 0)
+    {
+        /* Take 1's complement of i */
+        return (int16_t) ((size << 1) + 1 - i);
+    }
+    if (i == 0  &&  (quantizer_states & 1))
+    {
+        /* Zero is only valid if there are an even number of states, so
+           take the 1's complement if the code is zero. */
+        return (int16_t) quantizer_states;
+    }
+    return (int16_t) i;
+}
+/*- End of function --------------------------------------------------------*/
+
+/*
+ * Returns reconstructed difference signal 'dq' obtained from
+ * codeword 'i' and quantization step size scale factor 'y'.
+ * Multiplication is performed in log base 2 domain as addition.
+ */
+static int16_t reconstruct(int sign,    /* 0 for non-negative value */
+                           int dqln,    /* G.72x codeword */
+                           int y)       /* Step size multiplier */
+{
+    int16_t dql;    /* Log of 'dq' magnitude */
+    int16_t dex;    /* Integer part of log */
+    int16_t dqt;
+    int16_t dq;     /* Reconstructed difference signal sample */
+
+    dql = (int16_t) (dqln + (y >> 2));  /* ADDA */
+
+    if (dql < 0)
+        return ((sign)  ?  -0x8000  :  0);
+    /* ANTILOG */
+    dex = (dql >> 7) & 15;
+    dqt = 128 + (dql & 127);
+    dq = (dqt << 7) >> (14 - dex);
+    return ((sign)  ?  (dq - 0x8000)  :  dq);
+}
+/*- End of function --------------------------------------------------------*/
+
+/*
+ * updates the state variables for each output code
+ */
+static void update(g726_state_t *s,
+                   int y,       /* quantizer step size */
+                   int wi,      /* scale factor multiplier */
+                   int fi,      /* for long/short term energies */
+                   int dq,      /* quantized prediction difference */
+                   int sr,      /* reconstructed signal */
+                   int dqsez)   /* difference from 2-pole predictor */
+{
+    int16_t mag;
+    int16_t exp;
+    int16_t a2p;        /* LIMC */
+    int16_t a1ul;       /* UPA1 */
+    int16_t pks1;       /* UPA2 */
+    int16_t fa1;
+    int16_t ylint;
+    int16_t dqthr;
+    int16_t ylfrac;
+    int16_t thr;
+    int16_t pk0;
+    int i;
+    int tr;
+
+    a2p = 0;
+    /* Needed in updating predictor poles */
+    pk0 = (dqsez < 0)  ?  1  :  0;
+
+    /* prediction difference magnitude */
+    mag = (int16_t) (dq & 0x7FFF);
+    /* TRANS */
+    ylint = (int16_t) (s->yl >> 15);            /* exponent part of yl */
+    ylfrac = (int16_t) ((s->yl >> 10) & 0x1F);  /* fractional part of yl */
+    /* Limit threshold to 31 << 10 */
+    thr = (ylint > 9)  ?  (31 << 10)  :  ((32 + ylfrac) << ylint);
+    dqthr = (thr + (thr >> 1)) >> 1;            /* dqthr = 0.75 * thr */
+    if (!s->td)                                 /* signal supposed voice */
+        tr = FALSE;
+    else if (mag <= dqthr)                      /* supposed data, but small mag */
+        tr = FALSE;                             /* treated as voice */
+    else                                        /* signal is data (modem) */
+        tr = TRUE;
+
+    /*
+     * Quantizer scale factor adaptation.
+     */
+
+    /* FUNCTW & FILTD & DELAY */
+    /* update non-steady state step size multiplier */
+    s->yu = (int16_t) (y + ((wi - y) >> 5));
+
+    /* LIMB */
+    if (s->yu < 544)
+        s->yu = 544;
+    else if (s->yu > 5120)
+        s->yu = 5120;
+
+    /* FILTE & DELAY */
+    /* update steady state step size multiplier */
+    s->yl += s->yu + ((-s->yl) >> 6);
+
+    /*
+     * Adaptive predictor coefficients.
+     */
+    if (tr)
+    {
+        /* Reset the a's and b's for a modem signal */
+        s->a[0] = 0;
+        s->a[1] = 0;
+        s->b[0] = 0;
+        s->b[1] = 0;
+        s->b[2] = 0;
+        s->b[3] = 0;
+        s->b[4] = 0;
+        s->b[5] = 0;
+    }
+    else
+    {
+        /* Update the a's and b's */
+        /* UPA2 */
+        pks1 = pk0 ^ s->pk[0];
+
+        /* Update predictor pole a[1] */
+        a2p = s->a[1] - (s->a[1] >> 7);
+        if (dqsez != 0)
+        {
+            fa1 = (pks1)  ?  s->a[0]  :  -s->a[0];
+            /* a2p = function of fa1 */
+            if (fa1 < -8191)
+                a2p -= 0x100;
+            else if (fa1 > 8191)
+                a2p += 0xFF;
+            else
+                a2p += fa1 >> 5;
+
+            if (pk0 ^ s->pk[1])
+            {
+                /* LIMC */
+                if (a2p <= -12160)
+                    a2p = -12288;
+                else if (a2p >= 12416)
+                    a2p = 12288;
+                else
+                    a2p -= 0x80;
+            }
+            else if (a2p <= -12416)
+                a2p = -12288;
+            else if (a2p >= 12160)
+                a2p = 12288;
+            else
+                a2p += 0x80;
+        }
+
+        /* TRIGB & DELAY */
+        s->a[1] = a2p;
+
+        /* UPA1 */
+        /* Update predictor pole a[0] */
+        s->a[0] -= s->a[0] >> 8;
+        if (dqsez != 0)
+        {
+            if (pks1 == 0)
+                s->a[0] += 192;
+            else
+                s->a[0] -= 192;
+        }
+        /* LIMD */
+        a1ul = 15360 - a2p;
+        if (s->a[0] < -a1ul)
+            s->a[0] = -a1ul;
+        else if (s->a[0] > a1ul)
+            s->a[0] = a1ul;
+
+        /* UPB : update predictor zeros b[6] */
+        for (i = 0;  i < 6;  i++)
+        {
+            /* Distinguish 40Kbps mode from the others */
+            s->b[i] -= s->b[i] >> ((s->bits_per_sample == 5)  ?  9  :  8);
+            if (dq & 0x7FFF)
+            {
+                /* XOR */
+                if ((dq ^ s->dq[i]) >= 0)
+                    s->b[i] += 128;
+                else
+                    s->b[i] -= 128;
+            }
+        }
+    }
+
+    for (i = 5;  i > 0;  i--)
+        s->dq[i] = s->dq[i - 1];
+    /* FLOAT A : convert dq[0] to 4-bit exp, 6-bit mantissa f.p. */
+    if (mag == 0)
+    {
+        s->dq[0] = (dq >= 0)  ?  0x20  :  0xFC20;
+    }
+    else
+    {
+        exp = (int16_t) (top_bit(mag) + 1);
+        s->dq[0] = (dq >= 0)
+                 ?  ((exp << 6) + ((mag << 6) >> exp))
+                 :  ((exp << 6) + ((mag << 6) >> exp) - 0x400);
+    }
+
+    s->sr[1] = s->sr[0];
+    /* FLOAT B : convert sr to 4-bit exp., 6-bit mantissa f.p. */
+    if (sr == 0)
+    {
+        s->sr[0] = 0x20;
+    }
+    else if (sr > 0)
+    {
+        exp = (int16_t) (top_bit(sr) + 1);
+        s->sr[0] = (int16_t) ((exp << 6) + ((sr << 6) >> exp));
+    }
+    else if (sr > -32768)
+    {
+        mag = (int16_t) -sr;
+        exp = (int16_t) (top_bit(mag) + 1);
+        s->sr[0] =  (exp << 6) + ((mag << 6) >> exp) - 0x400;
+    }
+    else
+    {
+        s->sr[0] = (uint16_t) 0xFC20;
+    }
+
+    /* DELAY A */
+    s->pk[1] = s->pk[0];
+    s->pk[0] = pk0;
+
+    /* TONE */
+    if (tr)                 /* this sample has been treated as data */
+        s->td = FALSE;      /* next one will be treated as voice */
+    else if (a2p < -11776)  /* small sample-to-sample correlation */
+        s->td = TRUE;       /* signal may be data */
+    else                    /* signal is voice */
+        s->td = FALSE;
+
+    /* Adaptation speed control. */
+    /* FILTA */
+    s->dms += ((int16_t) fi - s->dms) >> 5;
+    /* FILTB */
+    s->dml += (((int16_t) (fi << 2) - s->dml) >> 7);
+
+    if (tr)
+        s->ap = 256;
+    else if (y < 1536)                      /* SUBTC */
+        s->ap += (0x200 - s->ap) >> 4;
+    else if (s->td)
+        s->ap += (0x200 - s->ap) >> 4;
+    else if (abs((s->dms << 2) - s->dml) >= (s->dml >> 3))
+        s->ap += (0x200 - s->ap) >> 4;
+    else
+        s->ap += (-s->ap) >> 4;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int16_t tandem_adjust_alaw(int16_t sr,   /* decoder output linear PCM sample */
+                                  int se,       /* predictor estimate sample */
+                                  int y,        /* quantizer step size */
+                                  int i,        /* decoder input code */
+                                  int sign,
+                                  const int qtab[],
+                                  int quantizer_states)
+{
+    uint8_t sp; /* A-law compressed 8-bit code */
+    int16_t dx; /* prediction error */
+    int id;     /* quantized prediction error */
+    int sd;     /* adjusted A-law decoded sample value */
+
+    if (sr <= -32768)
+        sr = -1;
+    sp = linear_to_alaw((sr >> 1) << 3);
+    /* 16-bit prediction error */
+    dx = (int16_t) ((alaw_to_linear(sp) >> 2) - se);
+    id = quantize(dx, y, qtab, quantizer_states);
+    if (id == i)
+    {
+        /* No adjustment of sp required */
+        return (int16_t) sp;
+    }
+    /* sp adjustment needed */
+    /* ADPCM codes : 8, 9, ... F, 0, 1, ... , 6, 7 */
+    /* 2's complement to biased unsigned */
+    if ((id ^ sign) > (i ^ sign))
+    {
+        /* sp adjusted to next lower value */
+        if (sp & 0x80)
+            sd = (sp == 0xD5)  ?  0x55  :  (((sp ^ 0x55) - 1) ^ 0x55);
+        else
+            sd = (sp == 0x2A)  ?  0x2A  :  (((sp ^ 0x55) + 1) ^ 0x55);
+    }
+    else
+    {
+        /* sp adjusted to next higher value */
+        if (sp & 0x80)
+            sd = (sp == 0xAA)  ?  0xAA  :  (((sp ^ 0x55) + 1) ^ 0x55);
+        else
+            sd = (sp == 0x55)  ?  0xD5  :  (((sp ^ 0x55) - 1) ^ 0x55);
+    }
+    return (int16_t) sd;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int16_t tandem_adjust_ulaw(int16_t sr,   /* decoder output linear PCM sample */
+                                  int se,       /* predictor estimate sample */
+                                  int y,        /* quantizer step size */
+                                  int i,        /* decoder input code */
+                                  int sign,
+                                  const int qtab[],
+                                  int quantizer_states)
+{
+    uint8_t sp; /* u-law compressed 8-bit code */
+    int16_t dx; /* prediction error */
+    int id;     /* quantized prediction error */
+    int sd;     /* adjusted u-law decoded sample value */
+
+    if (sr <= -32768)
+        sr = 0;
+    sp = linear_to_ulaw(sr << 2);
+    /* 16-bit prediction error */
+    dx = (int16_t) ((ulaw_to_linear(sp) >> 2) - se);
+    id = quantize(dx, y, qtab, quantizer_states);
+    if (id == i)
+    {
+        /* No adjustment of sp required. */
+        return (int16_t) sp;
+    }
+    /* ADPCM codes : 8, 9, ... F, 0, 1, ... , 6, 7 */
+    /* 2's complement to biased unsigned */
+    if ((id ^ sign) > (i ^ sign))
+    {
+        /* sp adjusted to next lower value */
+        if (sp & 0x80)
+            sd = (sp == 0xFF)  ?  0x7E  :  (sp + 1);
+        else
+            sd = (sp == 0x00)  ?  0x00  :  (sp - 1);
+    }
+    else
+    {
+        /* sp adjusted to next higher value */
+        if (sp & 0x80)
+            sd = (sp == 0x80)  ?  0x80  :  (sp - 1);
+        else
+            sd = (sp == 0x7F)  ?  0xFE  :  (sp + 1);
+    }
+    return (int16_t) sd;
+}
+/*- End of function --------------------------------------------------------*/
+
+/*
+ * Encodes a linear PCM, A-law or u-law input sample and returns its 3-bit code.
+ */
+static uint8_t g726_16_encoder(g726_state_t *s, int16_t amp)
+{
+    int y;
+    int16_t sei;
+    int16_t sezi;
+    int16_t se;
+    int16_t d;
+    int16_t sr;
+    int16_t dqsez;
+    int16_t dq;
+    int16_t i;
+    
+    sezi = predictor_zero(s);
+    sei = sezi + predictor_pole(s);
+    se = sei >> 1;
+    d = amp - se;
+
+    /* Quantize prediction difference */
+    y = step_size(s);
+    i = quantize(d, y, qtab_726_16, 4);
+    dq = reconstruct(i & 2, g726_16_dqlntab[i], y);
+
+    /* Reconstruct the signal */
+    sr = (dq < 0)  ?  (se - (dq & 0x3FFF))  :  (se + dq);
+
+    /* Pole prediction difference */
+    dqsez = sr + (sezi >> 1) - se;
+    
+    update(s, y, g726_16_witab[i], g726_16_fitab[i], dq, sr, dqsez);
+    return (uint8_t) i;
+}
+/*- End of function --------------------------------------------------------*/
+
+/*
+ * Decodes a 2-bit CCITT G.726_16 ADPCM code and returns
+ * the resulting 16-bit linear PCM, A-law or u-law sample value.
+ */
+static int16_t g726_16_decoder(g726_state_t *s, uint8_t code)
+{
+    int16_t sezi;
+    int16_t sei;
+    int16_t se;
+    int16_t sr;
+    int16_t dq;
+    int16_t dqsez;
+    int y;
+
+    /* Mask to get proper bits */
+    code &= 0x03;
+    sezi = predictor_zero(s);
+    sei = sezi + predictor_pole(s);
+
+    y = step_size(s);
+    dq = reconstruct(code & 2, g726_16_dqlntab[code], y);
+
+    /* Reconstruct the signal */
+    se = sei >> 1;
+    sr = (dq < 0)  ?  (se - (dq & 0x3FFF))  :  (se + dq);
+
+    /* Pole prediction difference */
+    dqsez = sr + (sezi >> 1) - se;
+
+    update(s, y, g726_16_witab[code], g726_16_fitab[code], dq, sr, dqsez);
+
+    switch (s->ext_coding)
+    {
+    case G726_ENCODING_ALAW:
+        return tandem_adjust_alaw(sr, se, y, code, 2, qtab_726_16, 4);
+    case G726_ENCODING_ULAW:
+        return tandem_adjust_ulaw(sr, se, y, code, 2, qtab_726_16, 4);
+    }
+    return (sr << 2);
+}
+/*- End of function --------------------------------------------------------*/
+
+/*
+ * Encodes a linear PCM, A-law or u-law input sample and returns its 3-bit code.
+ */
+static uint8_t g726_24_encoder(g726_state_t *s, int16_t amp)
+{
+    int16_t sei;
+    int16_t sezi;
+    int16_t se;
+    int16_t d;
+    int16_t sr;
+    int16_t dqsez;
+    int16_t dq;
+    int16_t i;
+    int y;
+    
+    sezi = predictor_zero(s);
+    sei = sezi + predictor_pole(s);
+    se = sei >> 1;
+    d = amp - se;
+
+    /* Quantize prediction difference */
+    y = step_size(s);
+    i = quantize(d, y, qtab_726_24, 7);
+    dq = reconstruct(i & 4, g726_24_dqlntab[i], y);
+
+    /* Reconstruct the signal */
+    sr = (dq < 0)  ?  (se - (dq & 0x3FFF))  :  (se + dq);
+
+    /* Pole prediction difference */
+    dqsez = sr + (sezi >> 1) - se;
+    
+    update(s, y, g726_24_witab[i], g726_24_fitab[i], dq, sr, dqsez);
+    return (uint8_t) i;
+}
+/*- End of function --------------------------------------------------------*/
+
+/*
+ * Decodes a 3-bit CCITT G.726_24 ADPCM code and returns
+ * the resulting 16-bit linear PCM, A-law or u-law sample value.
+ */
+static int16_t g726_24_decoder(g726_state_t *s, uint8_t code)
+{
+    int16_t sezi;
+    int16_t sei;
+    int16_t se;
+    int16_t sr;
+    int16_t dq;
+    int16_t dqsez;
+    int y;
+
+    /* Mask to get proper bits */
+    code &= 0x07;
+    sezi = predictor_zero(s);
+    sei = sezi + predictor_pole(s);
+
+    y = step_size(s);
+    dq = reconstruct(code & 4, g726_24_dqlntab[code], y);
+
+    /* Reconstruct the signal */
+    se = sei >> 1;
+    sr = (dq < 0)  ?  (se - (dq & 0x3FFF))  :  (se + dq);
+
+    /* Pole prediction difference */
+    dqsez = sr + (sezi >> 1) - se;
+
+    update(s, y, g726_24_witab[code], g726_24_fitab[code], dq, sr, dqsez);
+
+    switch (s->ext_coding)
+    {
+    case G726_ENCODING_ALAW:
+        return tandem_adjust_alaw(sr, se, y, code, 4, qtab_726_24, 7);
+    case G726_ENCODING_ULAW:
+        return tandem_adjust_ulaw(sr, se, y, code, 4, qtab_726_24, 7);
+    }
+    return (sr << 2);
+}
+/*- End of function --------------------------------------------------------*/
+
+/*
+ * Encodes a linear input sample and returns its 4-bit code.
+ */
+static uint8_t g726_32_encoder(g726_state_t *s, int16_t amp)
+{
+    int16_t sei;
+    int16_t sezi;
+    int16_t se;
+    int16_t d;
+    int16_t sr;
+    int16_t dqsez;
+    int16_t dq;
+    int16_t i;
+    int y;
+    
+    sezi = predictor_zero(s);
+    sei = sezi + predictor_pole(s);
+    se = sei >> 1;
+    d = amp - se;
+
+    /* Quantize the prediction difference */
+    y = step_size(s);
+    i = quantize(d, y, qtab_726_32, 15);
+    dq = reconstruct(i & 8, g726_32_dqlntab[i], y);
+
+    /* Reconstruct the signal */
+    sr = (dq < 0)  ?  (se - (dq & 0x3FFF))  :  (se + dq);
+
+    /* Pole prediction difference */
+    dqsez = sr + (sezi >> 1) - se;
+
+    update(s, y, g726_32_witab[i], g726_32_fitab[i], dq, sr, dqsez);
+    return (uint8_t) i;
+}
+/*- End of function --------------------------------------------------------*/
+
+/*
+ * Decodes a 4-bit CCITT G.726_32 ADPCM code and returns
+ * the resulting 16-bit linear PCM, A-law or u-law sample value.
+ */
+static int16_t g726_32_decoder(g726_state_t *s, uint8_t code)
+{
+    int16_t sezi;
+    int16_t sei;
+    int16_t se;
+    int16_t sr;
+    int16_t dq;
+    int16_t dqsez;
+    int y;
+
+    /* Mask to get proper bits */
+    code &= 0x0F;
+    sezi = predictor_zero(s);
+    sei = sezi + predictor_pole(s);
+
+    y = step_size(s);
+    dq = reconstruct(code & 8, g726_32_dqlntab[code], y);
+
+    /* Reconstruct the signal */
+    se = sei >> 1;
+    sr = (dq < 0)  ?  (se - (dq & 0x3FFF))  :  (se + dq);
+
+    /* Pole prediction difference */
+    dqsez = sr + (sezi >> 1) - se;
+
+    update(s, y, g726_32_witab[code], g726_32_fitab[code], dq, sr, dqsez);
+
+    switch (s->ext_coding)
+    {
+    case G726_ENCODING_ALAW:
+        return tandem_adjust_alaw(sr, se, y, code, 8, qtab_726_32, 15);
+    case G726_ENCODING_ULAW:
+        return tandem_adjust_ulaw(sr, se, y, code, 8, qtab_726_32, 15);
+    }
+    return (sr << 2);
+}
+/*- End of function --------------------------------------------------------*/
+
+/*
+ * Encodes a 16-bit linear PCM, A-law or u-law input sample and retuens
+ * the resulting 5-bit CCITT G.726 40Kbps code.
+ */
+static uint8_t g726_40_encoder(g726_state_t *s, int16_t amp)
+{
+    int16_t sei;
+    int16_t sezi;
+    int16_t se;
+    int16_t d;
+    int16_t sr;
+    int16_t dqsez;
+    int16_t dq;
+    int16_t i;
+    int y;
+    
+    sezi = predictor_zero(s);
+    sei = sezi + predictor_pole(s);
+    se = sei >> 1;
+    d = amp - se;
+
+    /* Quantize prediction difference */
+    y = step_size(s);
+    i = quantize(d, y, qtab_726_40, 31);
+    dq = reconstruct(i & 0x10, g726_40_dqlntab[i], y);
+
+    /* Reconstruct the signal */
+    sr = (dq < 0)  ?  (se - (dq & 0x7FFF))  :  (se + dq);
+
+    /* Pole prediction difference */
+    dqsez = sr + (sezi >> 1) - se;
+
+    update(s, y, g726_40_witab[i], g726_40_fitab[i], dq, sr, dqsez);
+    return (uint8_t) i;
+}
+/*- End of function --------------------------------------------------------*/
+
+/*
+ * Decodes a 5-bit CCITT G.726 40Kbps code and returns
+ * the resulting 16-bit linear PCM, A-law or u-law sample value.
+ */
+static int16_t g726_40_decoder(g726_state_t *s, uint8_t code)
+{
+    int16_t sezi;
+    int16_t sei;
+    int16_t se;
+    int16_t sr;
+    int16_t dq;
+    int16_t dqsez;
+    int y;
+
+    /* Mask to get proper bits */
+    code &= 0x1F;
+    sezi = predictor_zero(s);
+    sei = sezi + predictor_pole(s);
+        
+    y = step_size(s);
+    dq = reconstruct(code & 0x10, g726_40_dqlntab[code], y);
+
+    /* Reconstruct the signal */
+    se = sei >> 1;
+    sr = (dq < 0)  ?  (se - (dq & 0x7FFF))  :  (se + dq);
+
+    /* Pole prediction difference */
+    dqsez = sr + (sezi >> 1) - se;
+
+    update(s, y, g726_40_witab[code], g726_40_fitab[code], dq, sr, dqsez);
+
+    switch (s->ext_coding)
+    {
+    case G726_ENCODING_ALAW:
+        return tandem_adjust_alaw(sr, se, y, code, 0x10, qtab_726_40, 31);
+    case G726_ENCODING_ULAW:
+        return tandem_adjust_ulaw(sr, se, y, code, 0x10, qtab_726_40, 31);
+    }
+    return (sr << 2);
+}
+/*- End of function --------------------------------------------------------*/
+
+g726_state_t *g726_init(g726_state_t *s, int bit_rate, int ext_coding, int packing)
+{
+    int i;
+
+    if (bit_rate != 16000  &&  bit_rate != 24000  &&  bit_rate != 32000  &&  bit_rate != 40000)
+        return NULL;
+    if (s == NULL)
+    {
+        if ((s = (g726_state_t *) malloc(sizeof(*s))) == NULL)
+            return  NULL;
+    }
+    s->yl = 34816;
+    s->yu = 544;
+    s->dms = 0;
+    s->dml = 0;
+    s->ap = 0;
+    s->rate = bit_rate;
+    s->ext_coding = ext_coding;
+    s->packing = packing;
+    for (i = 0; i < 2; i++)
+    {
+        s->a[i] = 0;
+        s->pk[i] = 0;
+        s->sr[i] = 32;
+    }
+    for (i = 0; i < 6; i++)
+    {
+        s->b[i] = 0;
+        s->dq[i] = 32;
+    }
+    s->td = FALSE;
+    switch (bit_rate)
+    {
+    case 16000:
+        s->enc_func = g726_16_encoder;
+        s->dec_func = g726_16_decoder;
+        s->bits_per_sample = 2;
+        break;
+    case 24000:
+        s->enc_func = g726_24_encoder;
+        s->dec_func = g726_24_decoder;
+        s->bits_per_sample = 3;
+        break;
+    case 32000:
+    default:
+        s->enc_func = g726_32_encoder;
+        s->dec_func = g726_32_decoder;
+        s->bits_per_sample = 4;
+        break;
+    case 40000:
+        s->enc_func = g726_40_encoder;
+        s->dec_func = g726_40_decoder;
+        s->bits_per_sample = 5;
+        break;
+    }
+    bitstream_init(&s->bs);
+    return s;
+}
+/*- End of function --------------------------------------------------------*/
+
+int g726_release(g726_state_t *s)
+{
+    free(s);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int g726_decode(g726_state_t *s,
+                int16_t amp[],
+                const uint8_t g726_data[],
+                int g726_bytes)
+{
+    int i;
+    int samples;
+    uint8_t code;
+    int sl;
+
+    for (samples = i = 0;  ;  )
+    {
+        if (s->packing != G726_PACKING_NONE)
+        {
+            /* Unpack the code bits */
+            if (s->packing != G726_PACKING_LEFT)
+            {
+                if (s->bs.residue < s->bits_per_sample)
+                {
+                    if (i >= g726_bytes)
+                        break;
+                    s->bs.bitstream |= (g726_data[i++] << s->bs.residue);
+                    s->bs.residue += 8;
+                }
+                code = (uint8_t) (s->bs.bitstream & ((1 << s->bits_per_sample) - 1));
+                s->bs.bitstream >>= s->bits_per_sample;
+            }
+            else
+            {
+                if (s->bs.residue < s->bits_per_sample)
+                {
+                    if (i >= g726_bytes)
+                        break;
+                    s->bs.bitstream = (s->bs.bitstream << 8) | g726_data[i++];
+                    s->bs.residue += 8;
+                }
+                code = (uint8_t) ((s->bs.bitstream >> (s->bs.residue - s->bits_per_sample)) & ((1 << s->bits_per_sample) - 1));
+            }
+            s->bs.residue -= s->bits_per_sample;
+        }
+        else
+        {
+            if (i >= g726_bytes)
+                break;
+            code = g726_data[i++];
+        }
+        sl = s->dec_func(s, code);
+        if (s->ext_coding != G726_ENCODING_LINEAR)
+            ((uint8_t *) amp)[samples++] = (uint8_t) sl;
+        else
+            amp[samples++] = (int16_t) sl;
+    }
+    return samples;
+}
+/*- End of function --------------------------------------------------------*/
+
+int g726_encode(g726_state_t *s,
+                uint8_t g726_data[],
+                const int16_t amp[],
+                int len)
+{
+    int i;
+    int g726_bytes;
+    int16_t sl;
+    uint8_t code;
+
+    for (g726_bytes = i = 0;  i < len;  i++)
+    {
+        /* Linearize the input sample to 14-bit PCM */
+        switch (s->ext_coding)
+        {
+        case G726_ENCODING_ALAW:
+            sl = alaw_to_linear(((const uint8_t *) amp)[i]) >> 2;
+            break;
+        case G726_ENCODING_ULAW:
+            sl = ulaw_to_linear(((const uint8_t *) amp)[i]) >> 2;
+            break;
+        default:
+            sl = amp[i] >> 2;
+            break;
+        }
+        code = s->enc_func(s, sl);
+        if (s->packing != G726_PACKING_NONE)
+        {
+            /* Pack the code bits */
+            if (s->packing != G726_PACKING_LEFT)
+            {
+                s->bs.bitstream |= (code << s->bs.residue);
+                s->bs.residue += s->bits_per_sample;
+                if (s->bs.residue >= 8)
+                {
+                    g726_data[g726_bytes++] = (uint8_t) (s->bs.bitstream & 0xFF);
+                    s->bs.bitstream >>= 8;
+                    s->bs.residue -= 8;
+                }
+            }
+            else
+            {
+                s->bs.bitstream = (s->bs.bitstream << s->bits_per_sample) | code;
+                s->bs.residue += s->bits_per_sample;
+                if (s->bs.residue >= 8)
+                {
+                    g726_data[g726_bytes++] = (uint8_t) ((s->bs.bitstream >> (s->bs.residue - 8)) & 0xFF);
+                    s->bs.residue -= 8;
+                }
+            }
+        }
+        else
+        {
+            g726_data[g726_bytes++] = (uint8_t) code;
+        }
+    }
+    return g726_bytes;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/gsm0610_decode.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/gsm0610_decode.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,353 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * gsm0610_decode.c - GSM 06.10 full rate speech codec.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2006 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * This code is based on the widely used GSM 06.10 code available from
+ * http://kbs.cs.tu-berlin.de/~jutta/toast.html
+ *
+ * $Id: gsm0610_decode.c,v 1.21 2008/07/02 14:48:25 steveu Exp $
+ */
+
+/*! \file */
+
+#if defined(HAVE_CONFIG_H)
+#include <config.h>
+#endif
+
+#include <assert.h>
+#include <inttypes.h>
+#include "floating_fudge.h"
+#if defined(HAVE_TGMATH_H)
+#include <tgmath.h>
+#endif
+#if defined(HAVE_MATH_H)
+#include <math.h>
+#endif
+#include <stdlib.h>
+#include <memory.h>
+
+#include "spandsp/telephony.h"
+#include "spandsp/bitstream.h"
+#include "spandsp/dc_restore.h"
+#include "spandsp/gsm0610.h"
+
+#include "gsm0610_local.h"
+
+/* 4.3 FIXED POINT IMPLEMENTATION OF THE RPE-LTP DECODER */
+
+static void postprocessing(gsm0610_state_t *s, int16_t amp[])
+{
+    int k;
+    int16_t msr;
+    int16_t tmp;
+
+    msr = s->msr;
+    for (k = 0;  k < GSM0610_FRAME_LEN;  k++)
+    {
+        tmp = gsm_mult_r(msr, 28180);
+        /* De-emphasis */
+        msr = gsm_add(amp[k], tmp);
+        /* Truncation & upscaling */
+        amp[k] = (int16_t) (gsm_add(msr, msr) & 0xFFF8);
+    }
+    /*endfor*/
+    s->msr = msr;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void decode_a_frame(gsm0610_state_t *s,
+                           int16_t amp[GSM0610_FRAME_LEN],
+                           gsm0610_frame_t *f)
+{
+    int j;
+    int k;
+    int16_t erp[40];
+    int16_t wt[GSM0610_FRAME_LEN];
+    int16_t *drp;
+
+    drp = s->dp0 + 120;
+    for (j = 0;  j < 4;  j++)
+    {
+        gsm0610_rpe_decoding(s, f->xmaxc[j], f->Mc[j], f->xMc[j], erp);
+        gsm0610_long_term_synthesis_filtering(s, f->Nc[j], f->bc[j], erp, drp);
+        for (k = 0;  k < 40;  k++)
+            wt[j*40 + k] = drp[k];
+        /*endfor*/
+    }
+    /*endfor*/
+
+    gsm0610_short_term_synthesis_filter(s, f->LARc, wt, amp);
+    postprocessing(s, amp);
+}
+/*- End of function --------------------------------------------------------*/
+
+int gsm0610_unpack_none(gsm0610_frame_t *s, const uint8_t c[])
+{
+    int i;
+    int j;
+    int k;
+    
+    i = 0;
+    for (j = 0;  j < 8;  j++)
+        s->LARc[j] = c[i++];
+    for (j = 0;  j < 4;  j++)
+    {
+        s->Nc[j] = c[i++];
+        s->bc[j] = c[i++];
+        s->Mc[j] = c[i++];
+        s->xmaxc[j] = c[i++];
+        for (k = 0;  k < 13;  k++)
+            s->xMc[j][k] = c[i++];
+    }
+    return 76;
+}
+/*- End of function --------------------------------------------------------*/
+
+int gsm0610_unpack_wav49(gsm0610_frame_t *s, const uint8_t c[])
+{
+    uint16_t sr;
+    int i;
+
+    sr = *c++;
+    s->LARc[0] = sr & 0x3F;
+    sr >>= 6;
+    sr |= (uint16_t) *c++ << 2;
+    s->LARc[1] = sr & 0x3F;
+    sr >>= 6;
+    sr |= (uint16_t) *c++ << 4;
+    s->LARc[2] = sr & 0x1F;
+    sr >>= 5;
+    s->LARc[3] = sr & 0x1F;
+    sr >>= 5;
+    sr |= (uint16_t) *c++ << 2;
+    s->LARc[4] = sr & 0xF;
+    sr >>= 4;
+    s->LARc[5] = sr & 0xF;
+    sr >>= 4;
+    sr |= (uint16_t) *c++ << 2;
+    s->LARc[6] = sr & 0x7;
+    sr >>= 3;
+    s->LARc[7] = sr & 0x7;
+    sr >>= 3;
+
+    for (i = 0;  i < 4;  i++)
+    {
+        sr |= (uint16_t) *c++ << 4;
+        s->Nc[i] = sr & 0x7F;
+        sr >>= 7;
+        s->bc[i] = sr & 0x3;
+        sr >>= 2;
+        s->Mc[i] = sr & 0x3;
+        sr >>= 2;
+        sr |= (uint16_t) *c++ << 1;
+        s->xmaxc[i] = sr & 0x3F;
+        sr >>= 6;
+        s->xMc[i][0] = sr & 0x7;
+        sr >>= 3;
+        sr = *c++;
+        s->xMc[i][1] = sr & 0x7;
+        sr >>= 3;
+        s->xMc[i][2] = sr & 0x7;
+        sr >>= 3;
+        sr |= (uint16_t) *c++ << 2;
+        s->xMc[i][3] = sr & 0x7;
+        sr >>= 3;
+        s->xMc[i][4] = sr & 0x7;
+        sr >>= 3;
+        s->xMc[i][5] = sr & 0x7;
+        sr >>= 3;
+        sr |= (uint16_t) *c++ << 1;
+        s->xMc[i][6] = sr & 0x7;
+        sr >>= 3;
+        s->xMc[i][7] = sr & 0x7;
+        sr >>= 3;
+        s->xMc[i][8] = sr & 0x7;
+        sr >>= 3;
+        sr = *c++;
+        s->xMc[i][9] = sr & 0x7;
+        sr >>= 3;
+        s->xMc[i][10] = sr & 0x7;
+        sr >>= 3;
+        sr |= (uint16_t) *c++ << 2;
+        s->xMc[i][11] = sr & 0x7;
+        sr >>= 3;
+        s->xMc[i][12] = sr & 0x7;
+        sr >>= 3;
+    }
+
+    s++;
+    sr |= (uint16_t) *c++ << 4;
+    s->LARc[0] = sr & 0x3F;
+    sr >>= 6;
+    s->LARc[1] = sr & 0x3F;
+    sr >>= 6;
+    sr = *c++;
+    s->LARc[2] = sr & 0x1F;
+    sr >>= 5;
+    sr |= (uint16_t) *c++ << 3;
+    s->LARc[3] = sr & 0x1F;
+    sr >>= 5;
+    s->LARc[4] = sr & 0xF;
+    sr >>= 4;
+    sr |= (uint16_t) *c++ << 2;
+    s->LARc[5] = sr & 0xF;
+    sr >>= 4;
+    s->LARc[6] = sr & 0x7;
+    sr >>= 3;
+    s->LARc[7] = sr & 0x7;
+    sr >>= 3;
+
+    for (i = 0;  i < 4;  i++)
+    {
+        sr = *c++;
+        s->Nc[i] = sr & 0x7F;
+        sr >>= 7;
+        sr |= (uint16_t) *c++ << 1;
+        s->bc[i] = sr & 0x3;
+        sr >>= 2;
+        s->Mc[i] = sr & 0x3;
+        sr >>= 2;
+        sr |= (uint16_t) *c++ << 5;
+        s->xmaxc[i] = sr & 0x3F;
+        sr >>= 6;
+        s->xMc[i][0] = sr & 0x7;
+        sr >>= 3;
+        s->xMc[i][1] = sr & 0x7;
+        sr >>= 3;
+        sr |= (uint16_t) *c++ << 1;
+        s->xMc[i][2] = sr & 0x7;
+        sr >>= 3;
+        s->xMc[i][3] = sr & 0x7;
+        sr >>= 3;
+        s->xMc[i][4] = sr & 0x7;
+        sr >>= 3;
+        sr = *c++;
+        s->xMc[i][5] = sr & 0x7;
+        sr >>= 3;
+        s->xMc[i][6] = sr & 0x7;
+        sr >>= 3;
+        sr |= (uint16_t) *c++ << 2;
+        s->xMc[i][7] = sr & 0x7;
+        sr >>= 3;
+        s->xMc[i][8] = sr & 0x7;
+        sr >>= 3;
+        s->xMc[i][9] = sr & 0x7;
+        sr >>= 3;
+        sr |= (uint16_t) *c++ << 1;
+        s->xMc[i][10] = sr & 0x7;
+        sr >>= 3;
+        s->xMc[i][11] = sr & 0x7;
+        sr >>= 3;
+        s->xMc[i][12] = sr & 0x7;
+        sr >>= 3;
+    }
+    return 65;
+}
+/*- End of function --------------------------------------------------------*/
+
+int gsm0610_unpack_voip(gsm0610_frame_t *s, const uint8_t c[33])
+{
+    int i;
+
+    s->LARc[0]  = (*c++ & 0xF) << 2;
+    s->LARc[0] |= (*c >> 6) & 0x3;
+    s->LARc[1]  = *c++ & 0x3F;
+    s->LARc[2]  = (*c >> 3) & 0x1F;
+    s->LARc[3]  = (*c++ & 0x7) << 2;
+    s->LARc[3] |= (*c >> 6) & 0x3;
+    s->LARc[4]  = (*c >> 2) & 0xF;
+    s->LARc[5]  = (*c++ & 0x3) << 2;
+    s->LARc[5] |= (*c >> 6) & 0x3;
+    s->LARc[6]  = (*c >> 3) & 0x7;
+    s->LARc[7]  = *c++ & 0x7;
+
+    for (i = 0;  i < 4;  i++)
+    {
+        s->Nc[i]       = (*c >> 1) & 0x7F;
+        s->bc[i]       = (*c++ & 0x1) << 1;
+        s->bc[i]      |= (*c >> 7) & 0x1;
+        s->Mc[i]       = (*c >> 5) & 0x3;
+        s->xmaxc[i]    = (*c++ & 0x1F) << 1;
+        s->xmaxc[i]   |= (*c >> 7) & 0x1;
+        s->xMc[i][0]   = (*c >> 4) & 0x7;
+        s->xMc[i][1]   = (*c >> 1) & 0x7;
+        s->xMc[i][2]   = (*c++ & 0x1) << 2;
+        s->xMc[i][2]  |= (*c >> 6) & 0x3;
+        s->xMc[i][3]   = (*c >> 3) & 0x7;
+        s->xMc[i][4]   = *c++ & 0x7;
+        s->xMc[i][5]   = (*c >> 5) & 0x7;
+        s->xMc[i][6]   = (*c >> 2) & 0x7;
+        s->xMc[i][7]   = (*c++ & 0x3) << 1;
+        s->xMc[i][7]  |= (*c >> 7) & 0x1;
+        s->xMc[i][8]   = (*c >> 4) & 0x7;
+        s->xMc[i][9]   = (*c >> 1) & 0x7;
+        s->xMc[i][10]  = (*c++ & 0x1) << 2;
+        s->xMc[i][10] |= (*c >> 6) & 0x3;
+        s->xMc[i][11]  = (*c >> 3) & 0x7;
+        s->xMc[i][12]  = *c++ & 0x7;
+    }
+    return 33;
+}
+/*- End of function --------------------------------------------------------*/
+
+int gsm0610_decode(gsm0610_state_t *s, int16_t amp[], const uint8_t code[], int len)
+{
+    gsm0610_frame_t frame[2];
+    int bytes;
+    int samples;
+    int i;
+
+    samples = 0;
+    for (i = 0;  i < len;  i += bytes)
+    {
+        switch (s->packing)
+        {
+        default:
+        case GSM0610_PACKING_NONE:
+            if ((bytes = gsm0610_unpack_none(frame, &code[i])) < 0)
+                return 0;
+            decode_a_frame(s, &amp[samples], frame);
+            samples += GSM0610_FRAME_LEN;
+            break;
+        case GSM0610_PACKING_WAV49:
+            if ((bytes = gsm0610_unpack_wav49(frame, &code[i])) < 0)
+                return 0;
+            decode_a_frame(s, &amp[samples], frame);
+            samples += GSM0610_FRAME_LEN;
+            decode_a_frame(s, &amp[samples], frame + 1);
+            samples += GSM0610_FRAME_LEN;
+            break;
+        case GSM0610_PACKING_VOIP:
+            if ((bytes = gsm0610_unpack_voip(frame, &code[i])) < 0)
+                return 0;
+            decode_a_frame(s, &amp[samples], frame);
+            samples += GSM0610_FRAME_LEN;
+            break;
+        }
+        /*endswitch*/
+    }
+    /*endfor*/
+    return samples;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/gsm0610_encode.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/gsm0610_encode.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,335 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * gsm0610_encode.c - GSM 06.10 full rate speech codec.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2006 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * This code is based on the widely used GSM 06.10 code available from
+ * http://kbs.cs.tu-berlin.de/~jutta/toast.html
+ *
+ * $Id: gsm0610_encode.c,v 1.25 2008/07/02 14:48:25 steveu Exp $
+ */
+
+/*! \file */
+
+#if defined(HAVE_CONFIG_H)
+#include <config.h>
+#endif
+
+#include <assert.h>
+#include <inttypes.h>
+#include "floating_fudge.h"
+#if defined(HAVE_TGMATH_H)
+#include <tgmath.h>
+#endif
+#if defined(HAVE_MATH_H)
+#include <math.h>
+#endif
+#include <stdlib.h>
+#include <memory.h>
+
+#include "spandsp/telephony.h"
+#include "spandsp/bitstream.h"
+#include "spandsp/dc_restore.h"
+#include "spandsp/gsm0610.h"
+
+#include "gsm0610_local.h"
+
+/* 4.2 FIXED POINT IMPLEMENTATION OF THE RPE-LTP CODER */
+
+/* The RPE-LTD coder works on a frame by frame basis.  The length of
+   the frame is equal to 160 samples.  Some computations are done
+   once per frame to produce at the output of the coder the
+   LARc[1..8] parameters which are the coded LAR coefficients and 
+   also to realize the inverse filtering operation for the entire
+   frame (160 samples of signal d[0..159]).  These parts produce at
+   the output of the coder:
+
+   Procedure 4.2.11 to 4.2.18 are to be executed four times per
+   frame.  That means once for each sub-segment RPE-LTP analysis of
+   40 samples.  These parts produce at the output of the coder.
+*/
+static void encode_a_frame(gsm0610_state_t *s, gsm0610_frame_t *f, const int16_t amp[])
+{
+    int k;
+    int16_t *dp;
+    int16_t *dpp;
+    int16_t so[GSM0610_FRAME_LEN];
+    int i;
+
+    dp = s->dp0 + 120;
+    dpp = dp;
+    gsm0610_preprocess(s, amp, so);
+    gsm0610_lpc_analysis(s, so, f->LARc);
+    gsm0610_short_term_analysis_filter(s, f->LARc, so);
+
+    for (k = 0;  k < 4;  k++)
+    {
+        gsm0610_long_term_predictor(s,
+                                    so + k*40,
+                                    dp,
+                                    s->e + 5,
+                                    dpp,
+                                    &f->Nc[k],
+                                    &f->bc[k]);
+        gsm0610_rpe_encoding(s, s->e + 5, &f->xmaxc[k], &f->Mc[k], f->xMc[k]);
+
+        for (i = 0;  i < 40;  i++)
+            dp[i] = gsm_add(s->e[5 + i], dpp[i]);
+        /*endfor*/
+        dp += 40;
+        dpp += 40;
+    }
+    /*endfor*/
+    memcpy((char *) s->dp0,
+           (char *) (s->dp0 + GSM0610_FRAME_LEN),
+           120*sizeof(*s->dp0));
+}
+/*- End of function --------------------------------------------------------*/
+
+gsm0610_state_t *gsm0610_init(gsm0610_state_t *s, int packing)
+{
+    if (s == NULL)
+    {
+        if ((s = (gsm0610_state_t *) malloc(sizeof (*s))) == NULL)
+            return  NULL;
+        /*endif*/
+    }
+    /*endif*/
+    memset((char *) s, '\0', sizeof (gsm0610_state_t));
+    s->nrp = 40;
+    s->packing = packing;
+    return s;
+}
+/*- End of function --------------------------------------------------------*/
+
+int gsm0610_set_packing(gsm0610_state_t *s, int packing)
+{
+    s->packing = packing;
+    return 0;    
+}
+/*- End of function --------------------------------------------------------*/
+
+int gsm0610_release(gsm0610_state_t *s)
+{
+    if (s)
+        free(s);
+    /*endif*/
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int gsm0610_pack_none(uint8_t c[], const gsm0610_frame_t *s)
+{
+    int i;
+    int j;
+    int k;
+    
+    i = 0;
+    for (j = 0;  j < 8;  j++)
+        c[i++] = (uint8_t) s->LARc[j];
+    for (j = 0;  j < 4;  j++)
+    {
+        c[i++] = (uint8_t) s->Nc[j];
+        c[i++] = (uint8_t) s->bc[j];
+        c[i++] = (uint8_t) s->Mc[j];
+        c[i++] = (uint8_t) s->xmaxc[j];
+        for (k = 0;  k < 13;  k++)
+            c[i++] = (uint8_t) s->xMc[j][k];
+    }
+    /*endfor*/
+    return 76;
+}
+/*- End of function --------------------------------------------------------*/
+
+int gsm0610_pack_wav49(uint8_t c[], const gsm0610_frame_t *s)
+{
+    uint16_t sr;
+    int i;
+ 
+	sr = 0;
+	sr = (sr >> 6) | (s->LARc[0] << 10);
+	sr = (sr >> 6) | (s->LARc[1] << 10);
+	*c++ = (uint8_t) (sr >> 4);
+	sr = (sr >> 5) | (s->LARc[2] << 11);
+	*c++ = (uint8_t) (sr >> 7);
+	sr = (sr >> 5) | (s->LARc[3] << 11);
+	sr = (sr >> 4) | (s->LARc[4] << 12);
+	*c++ = (uint8_t) (sr >> 6);
+	sr = (sr >> 4) | (s->LARc[5] << 12);
+	sr = (sr >> 3) | (s->LARc[6] << 13);
+	*c++ = (uint8_t) (sr >> 7);
+	sr = (sr >> 3) | (s->LARc[7] << 13);
+
+    for (i = 0;  i < 4;  i++)
+    {
+    	sr = (sr >> 7) | (s->Nc[i] << 9);
+    	*c++ = (uint8_t) (sr >> 5);
+    	sr = (sr >> 2) | (s->bc[i] << 14);
+    	sr = (sr >> 2) | (s->Mc[i] << 14);
+    	sr = (sr >> 6) | (s->xmaxc[i] << 10);
+    	*c++ = (uint8_t) (sr >> 3);
+    	sr = (sr >> 3) | (s->xMc[i][0] << 13);
+    	*c++ = (uint8_t) (sr >> 8);
+    	sr = (sr >> 3) | (s->xMc[i][1] << 13);
+    	sr = (sr >> 3) | (s->xMc[i][2] << 13);
+        sr = (sr >> 3) | (s->xMc[i][3] << 13);
+        *c++ = (uint8_t) (sr >> 7);
+        sr = (sr >> 3) | (s->xMc[i][4] << 13);
+        sr = (sr >> 3) | (s->xMc[i][5] << 13);
+        sr = (sr >> 3) | (s->xMc[i][6] << 13);
+        *c++ = (uint8_t) (sr >> 6);
+        sr = (sr >> 3) | (s->xMc[i][7] << 13);
+        sr = (sr >> 3) | (s->xMc[i][8] << 13);
+        *c++ = (uint8_t) (sr >> 8);
+        sr = (sr >> 3) | (s->xMc[i][9] << 13);
+        sr = (sr >> 3) | (s->xMc[i][10] << 13);
+        sr = (sr >> 3) | (s->xMc[i][11] << 13);
+        *c++ = (uint8_t) (sr >> 7);
+        sr = (sr >> 3) | (s->xMc[i][12] << 13);
+    }
+    /*endfor*/
+
+    s++;
+    sr = (sr >> 6) | (s->LARc[0] << 10);
+    *c++ = (uint8_t) (sr >> 6);
+    sr = (sr >> 6) | (s->LARc[1] << 10);
+    *c++ = (uint8_t) (sr >> 8);
+    sr = (sr >> 5) | (s->LARc[2] << 11);
+    sr = (sr >> 5) | (s->LARc[3] << 11);
+    *c++ = (uint8_t) (sr >> 6);
+    sr = (sr >> 4) | (s->LARc[4] << 12);
+    sr = (sr >> 4) | (s->LARc[5] << 12);
+    *c++ = (uint8_t) (sr >> 6);
+    sr = (sr >> 3) | (s->LARc[6] << 13);
+    sr = (sr >> 3) | (s->LARc[7] << 13);
+    *c++ = (uint8_t) (sr >> 8);
+
+    for (i = 0;  i < 4;  i++)
+    {
+        sr = (sr >> 7) | (s->Nc[i] << 9);
+        sr = (sr >> 2) | (s->bc[i] << 14);
+        *c++ = (uint8_t) (sr >> 7);
+        sr = (sr >> 2) | (s->Mc[i] << 14);
+        sr = (sr >> 6) | (s->xmaxc[i] << 10);
+        *c++ = (uint8_t) (sr >> 7);
+        sr = (sr >> 3) | (s->xMc[i][0] << 13);
+        sr = (sr >> 3) | (s->xMc[i][1] << 13);
+        sr = (sr >> 3) | (s->xMc[i][2] << 13);
+        *c++ = (uint8_t) (sr >> 6);
+        sr = (sr >> 3) | (s->xMc[i][3] << 13);
+        sr = (sr >> 3) | (s->xMc[i][4] << 13);
+        *c++ = (uint8_t) (sr >> 8);
+        sr = (sr >> 3) | (s->xMc[i][5] << 13);
+        sr = (sr >> 3) | (s->xMc[i][6] << 13);
+        sr = (sr >> 3) | (s->xMc[i][7] << 13);
+        *c++ = (uint8_t) (sr >> 7);
+        sr = (sr >> 3) | (s->xMc[i][8] << 13);
+        sr = (sr >> 3) | (s->xMc[i][9] << 13);
+        sr = (sr >> 3) | (s->xMc[i][10] << 13);
+        *c++ = (uint8_t) (sr >> 6);
+        sr = (sr >> 3) | (s->xMc[i][11] << 13);
+        sr = (sr >> 3) | (s->xMc[i][12] << 13);
+        *c++ = (uint8_t) (sr >> 8);
+    }
+    /*endfor*/
+    return 65;
+}
+/*- End of function --------------------------------------------------------*/
+
+int gsm0610_pack_voip(uint8_t c[33], const gsm0610_frame_t *s)
+{
+    int i;
+
+    *c++ = (uint8_t) (((GSM0610_MAGIC & 0xF) << 4)
+                    | ((s->LARc[0] >> 2) & 0xF));
+    *c++ = (uint8_t) (((s->LARc[0] & 0x3) << 6)
+                    |  (s->LARc[1] & 0x3F));
+    *c++ = (uint8_t) (((s->LARc[2] & 0x1F) << 3)
+                    | ((s->LARc[3] >> 2) & 0x7));
+    *c++ = (uint8_t) (((s->LARc[3] & 0x3) << 6)
+                    | ((s->LARc[4] & 0xF) << 2)
+                    | ((s->LARc[5] >> 2) & 0x3));
+    *c++ = (uint8_t) (((s->LARc[5] & 0x3) << 6)
+                    | ((s->LARc[6] & 0x7) << 3)
+                    |  (s->LARc[7] & 0x7));
+
+    for (i = 0;  i < 4;  i++)
+    {
+        *c++ = (uint8_t) (((s->Nc[i] & 0x7F) << 1)
+                        | ((s->bc[i] >> 1) & 0x1));
+        *c++ = (uint8_t) (((s->bc[i] & 0x1) << 7)
+                        | ((s->Mc[i] & 0x3) << 5)
+                        | ((s->xmaxc[i] >> 1) & 0x1F));
+        *c++ = (uint8_t) (((s->xmaxc[i] & 0x1) << 7)
+                        | ((s->xMc[i][0] & 0x7) << 4)
+                        | ((s->xMc[i][1] & 0x7) << 1)
+                        | ((s->xMc[i][2] >> 2) & 0x1));
+        *c++ = (uint8_t) (((s->xMc[i][2] & 0x3) << 6)
+                        | ((s->xMc[i][3] & 0x7) << 3)
+                        |  (s->xMc[i][4] & 0x7));
+        *c++ = (uint8_t) (((s->xMc[i][5] & 0x7) << 5)
+                        | ((s->xMc[i][6] & 0x7) << 2)
+                        | ((s->xMc[i][7] >> 1) & 0x3));
+        *c++ = (uint8_t) (((s->xMc[i][7] & 0x1) << 7)
+                        | ((s->xMc[i][8] & 0x7) << 4)
+                        | ((s->xMc[i][9] & 0x7) << 1)
+                        | ((s->xMc[i][10] >> 2) & 0x1));
+        *c++ = (uint8_t) (((s->xMc[i][10] & 0x3) << 6)
+                        | ((s->xMc[i][11] & 0x7) << 3)
+                        |  (s->xMc[i][12] & 0x7));
+    }
+    /*endfor*/
+    return 33;
+}
+/*- End of function --------------------------------------------------------*/
+
+int gsm0610_encode(gsm0610_state_t *s, uint8_t code[], const int16_t amp[], int len)
+{
+    gsm0610_frame_t frame[2];
+    int bytes;
+    int i;
+
+    bytes = 0;
+    for (i = 0;  i < len;  i += GSM0610_FRAME_LEN)
+    {
+        encode_a_frame(s, frame, &amp[i]);
+        switch (s->packing)
+        {
+        case GSM0610_PACKING_WAV49:
+            i += GSM0610_FRAME_LEN;
+            encode_a_frame(s, frame + 1, &amp[i]);
+            bytes += gsm0610_pack_wav49(&code[bytes], frame);
+            break;
+        case GSM0610_PACKING_VOIP:
+            bytes += gsm0610_pack_voip(&code[bytes], frame);
+            break;
+        default:
+            bytes += gsm0610_pack_none(&code[bytes], frame);
+            break;
+        }
+        /*endswitch*/
+    }
+    /*endfor*/
+    return bytes;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/gsm0610_local.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/gsm0610_local.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,229 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * gsm0610_local.h - GSM 06.10 full rate speech codec.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2006 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * This code is based on the widely used GSM 06.10 code available from
+ * http://kbs.cs.tu-berlin.de/~jutta/toast.html
+ *
+ * $Id: gsm0610_local.h,v 1.10 2008/04/17 14:26:56 steveu Exp $
+ */
+
+#if !defined(_GSM0610_LOCAL_H_)
+#define _GSM0610_LOCAL_H_
+
+#define GSM0610_FRAME_LEN               160
+
+#define	GSM0610_MAGIC                   0xD
+
+static __inline__ int16_t gsm_add(int16_t a, int16_t b)
+{
+#if defined(__GNUC__)  &&  defined(__i386__)
+    __asm__ __volatile__(
+        " addw %2,%0;\n"
+        " jno 0f;\n"
+        " movw $0x7fff,%0;\n"
+        " adcw $0,%0;\n"
+        "0:"
+        : "=r" (a)
+        : "0" (a), "ir" (b)
+        : "cc"
+    );
+    return a;
+#else
+    int32_t sum;
+
+    sum = (int32_t) a + (int32_t) b;
+    return  saturate(sum);
+#endif
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ int32_t gsm_l_add(int32_t a, int32_t b)
+{
+#if defined(__i386__)
+    __asm__ __volatile__(
+        " addl %2,%0;\n"
+        " jno 0f;\n"
+        " movl $0x7fffffff,%0;\n"
+        " adcl $0,%0;\n"
+        "0:"
+        : "=r" (a)
+        : "0" (a), "ir" (b)
+        : "cc"
+    );
+    return a;
+#else
+    uint32_t A;
+
+    if (a < 0)
+    {
+        if (b >= 0)
+            return  a + b;
+        /*endif*/
+        A = (uint32_t) -(a + 1) + (uint32_t) -(b + 1);
+        return  (A >= INT32_MAX)  ?  INT32_MIN  :  -(int32_t) A - 2;
+    }
+    /*endif*/
+    if (b <= 0)
+        return  a + b;
+    /*endif*/
+    A = (uint32_t) a + (uint32_t) b;
+    return  (A > INT32_MAX)  ?  INT32_MAX  :  A;
+#endif
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ int16_t gsm_sub(int16_t a, int16_t b)
+{
+    int32_t diff;
+
+    diff = (int32_t) a - (int32_t) b;
+    return  saturate(diff);
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ int16_t gsm_mult(int16_t a, int16_t b)
+{
+    if (a == INT16_MIN  &&  b == INT16_MIN)
+        return  INT16_MAX;
+    /*endif*/
+    return  (int16_t) (((int32_t) a * (int32_t) b) >> 15);
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ int32_t gsm_l_mult(int16_t a, int16_t b)
+{
+    assert (a != INT16_MIN  ||  b != INT16_MIN);
+    return  ((int32_t) a * (int32_t) b) << 1;
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ int16_t gsm_mult_r(int16_t a, int16_t b)
+{
+    int32_t prod;
+
+    if (b == INT16_MIN  &&  a == INT16_MIN)
+        return  INT16_MAX;
+    /*endif*/
+    prod = (int32_t) a * (int32_t) b + 16384;
+    prod >>= 15;
+    return  (int16_t) (prod & 0xFFFF);
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ int16_t gsm_abs(int16_t a)
+{
+    return  (a == INT16_MIN)  ?  INT16_MAX  :  (int16_t) abs(a);
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ int16_t gsm_asr(int16_t a, int n)
+{
+    if (n >= 16)
+        return  (int16_t) (-(a < 0));
+    /*endif*/
+    if (n <= -16)
+        return  0;
+    /*endif*/
+    if (n < 0)
+        return (int16_t) (a << -n);
+    /*endif*/
+    return  (int16_t) (a >> n);
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ int16_t gsm_asl(int16_t a, int n)
+{
+    if (n >= 16)
+        return  0;
+    /*endif*/
+    if (n <= -16)
+        return  (int16_t) (-(a < 0));
+    /*endif*/
+    if (n < 0)
+        return  gsm_asr(a, -n);
+    /*endif*/
+    return  (int16_t) (a << n);
+}
+/*- End of function --------------------------------------------------------*/
+
+extern void gsm0610_long_term_predictor(gsm0610_state_t *s,
+                                        int16_t d[40],
+                                        int16_t *dp,        /* [-120..-1] d'        IN  */
+                                        int16_t e[40],
+                                        int16_t dpp[40],
+                                        int16_t *Nc,
+                                        int16_t *bc);
+
+extern void gsm0610_lpc_analysis(gsm0610_state_t *s,
+                                 int16_t amp[160],
+                                 int16_t LARc[8]);
+
+extern void gsm0610_preprocess(gsm0610_state_t *s,
+                               const int16_t amp[],
+                               int16_t so[]);
+
+extern void gsm0610_short_term_analysis_filter(gsm0610_state_t *s,
+                                               int16_t LARc[8],
+                                               int16_t amp[160]);
+
+extern void gsm0610_long_term_synthesis_filtering(gsm0610_state_t *s,
+                                                  int16_t Ncr,
+                                                  int16_t bcr,
+                                                  int16_t erp[40],
+                                                  int16_t *drp);             /* [-120..-1] IN, [0..40] OUT */
+
+extern void gsm0610_rpe_decoding(gsm0610_state_t *s,
+                                 int16_t xmaxcr,
+                                 int16_t Mcr,
+                                 int16_t *xMcr,             /* [0..12], 3 bits             IN      */
+                                 int16_t erp[40]);
+
+extern void gsm0610_rpe_encoding(gsm0610_state_t *s,
+                                 int16_t *e,                /* [-5..-1][0..39][40..44]     IN/OUT  */
+                                 int16_t *xmaxc,
+                                 int16_t *Mc,
+                                 int16_t xMc[13]);
+
+extern void gsm0610_short_term_synthesis_filter(gsm0610_state_t *s,
+                                                int16_t LARcr[8],
+                                                int16_t drp[40],
+                                                int16_t amp[160]);
+
+extern int16_t gsm0610_norm(int32_t a);
+
+#if defined(__GNUC__)  &&  defined(__i386__)
+
+void gsm0610_vec_vsraw(const int16_t *p, int n, int bits);
+
+int32_t gsm0610_vec_iprod(const int16_t *p, const int16_t *q, int n);
+
+int32_t gsm0610_vec_maxmin(const int16_t *p, int n, int16_t *out);
+
+int32_t gsm0610_max_cross_corr(const int16_t *wt, const int16_t *dp, int16_t *Nc_out);
+
+#endif
+
+#endif
+
+/*- End of include ---------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/gsm0610_long_term.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/gsm0610_long_term.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,411 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * gsm0610_long_term.c - GSM 06.10 full rate speech codec.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2006 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * This code is based on the widely used GSM 06.10 code available from
+ * http://kbs.cs.tu-berlin.de/~jutta/toast.html
+ *
+ * $Id: gsm0610_long_term.c,v 1.16 2008/07/02 14:48:25 steveu Exp $
+ */
+
+/*! \file */
+
+#if defined(HAVE_CONFIG_H)
+#include <config.h>
+#endif
+
+#include <assert.h>
+#include <inttypes.h>
+#include "floating_fudge.h"
+#if defined(HAVE_TGMATH_H)
+#include <tgmath.h>
+#endif
+#if defined(HAVE_MATH_H)
+#include <math.h>
+#endif
+#include <stdlib.h>
+
+#include "spandsp/telephony.h"
+#include "spandsp/bitstream.h"
+#include "spandsp/dc_restore.h"
+#include "spandsp/gsm0610.h"
+
+#include "gsm0610_local.h"
+
+/* Table 4.3a  Decision level of the LTP gain quantizer */
+static const int16_t gsm_DLB[4] =
+{
+    6554,    16384,    26214,     32767
+};
+
+/* Table 4.3b   Quantization levels of the LTP gain quantizer */
+static const int16_t gsm_QLB[4] =
+{
+    3277,    11469,    21299,     32767
+};
+
+/* 4.2.11 .. 4.2.12 LONG TERM PREDICTOR (LTP) SECTION */
+
+#if defined(__GNUC__)  &&  defined(__i386__)
+int32_t gsm0610_max_cross_corr(const int16_t *wt, const int16_t *dp, int16_t *Nc_out)
+{
+    int32_t lmax;
+    int32_t out;
+
+    __asm__ __volatile__(
+        " emms;\n"
+        " pushl %%ebx;\n"
+        " movl $0,%%edx;\n"             /* Will be maximum inner-product */
+        " movl $40,%%ebx;\n"
+        " movl %%ebx,%%ecx;\n"          /* Will be index of max inner-product */
+        " subl $80,%%esi;\n"
+        " .p2align 2;\n"
+        "1:\n"
+        " movq (%%edi),%%mm0;\n"
+        " movq (%%esi),%%mm2;\n"
+        " pmaddwd %%mm2,%%mm0;\n"
+        " movq 8(%%edi),%%mm1;\n"
+        " movq 8(%%esi),%%mm2;\n"
+        " pmaddwd %%mm2,%%mm1;\n"
+        " paddd %%mm1,%%mm0;\n"
+        " movq 16(%%edi),%%mm1;\n"
+        " movq 16(%%esi),%%mm2;\n"
+        " pmaddwd %%mm2,%%mm1;\n"
+        " paddd %%mm1,%%mm0;\n"
+        " movq 24(%%edi),%%mm1;\n"
+        " movq 24(%%esi),%%mm2;\n"
+        " pmaddwd %%mm2,%%mm1;\n"
+        " paddd %%mm1,%%mm0;\n"
+        " movq 32(%%edi),%%mm1;\n"
+        " movq 32(%%esi),%%mm2;\n"
+        " pmaddwd %%mm2,%%mm1;\n"
+        " paddd %%mm1,%%mm0;\n"
+        " movq 40(%%edi),%%mm1;\n"
+        " movq 40(%%esi),%%mm2;\n"
+        " pmaddwd %%mm2,%%mm1;\n"
+        " paddd %%mm1,%%mm0;\n"
+        " movq 48(%%edi),%%mm1;\n"
+        " movq 48(%%esi),%%mm2;\n"
+        " pmaddwd %%mm2,%%mm1;\n"
+        " paddd %%mm1,%%mm0;\n"
+        " movq 56(%%edi),%%mm1;\n"
+        " movq 56(%%esi),%%mm2;\n"
+        " pmaddwd %%mm2,%%mm1;\n"
+        " paddd %%mm1,%%mm0;\n"
+        " movq 64(%%edi),%%mm1;\n"
+        " movq 64(%%esi),%%mm2;\n"
+        " pmaddwd %%mm2,%%mm1;\n"
+        " paddd %%mm1,%%mm0;\n"
+        " movq 72(%%edi),%%mm1;\n"
+        " movq 72(%%esi),%%mm2;\n"
+        " pmaddwd %%mm2,%%mm1;\n"
+        " paddd %%mm1,%%mm0;\n"
+        " movq %%mm0,%%mm1;\n"
+        " punpckhdq %%mm0,%%mm1;\n"        /* mm1 has high int32 of mm0 dup'd */
+        " paddd %%mm1,%%mm0;\n"
+        " movd %%mm0,%%eax;\n"                /* eax has result */
+        " cmpl %%edx,%%eax;\n"
+        " jle 2f;\n"
+        " movl %%eax,%%edx;\n"
+        " movl %%ebx,%%ecx;\n"
+        " .p2align 2;\n"
+        "2:\n"
+        " subl $2,%%esi;\n"
+        " incl %%ebx;\n"
+        " cmpl $120,%%ebx;\n"
+        " jle 1b;\n"
+        " popl %%ebx;\n"
+        " emms;\n"
+        : "=d" (lmax), "=c" (out)
+        : "D" (wt), "S" (dp)
+        : "eax"
+    );
+    *Nc_out = out;
+    return  lmax;
+}
+/*- End of function --------------------------------------------------------*/
+#endif
+
+/* This procedure computes the LTP gain (bc) and the LTP lag (Nc)
+   for the long term analysis filter.   This is done by calculating a
+   maximum of the cross-correlation function between the current
+   sub-segment short term residual signal d[0..39] (output of
+   the short term analysis filter; for simplification the index
+   of this array begins at 0 and ends at 39 for each sub-segment of the
+   RPE-LTP analysis) and the previous reconstructed short term
+   residual signal dp[ -120 .. -1 ].  A dynamic scaling must be
+   performed to avoid overflow. */
+
+/* This procedure exists in three versions.  First, the integer
+   version; then, the two floating point versions (as another
+   function), with or without scaling. */
+
+static int16_t evaluate_ltp_parameters(int16_t d[40],
+                                       int16_t *dp,   // [-120..-1]   IN
+                                       int16_t *Nc_out)
+{
+    int k;
+    int16_t Nc;
+    int16_t bc;
+    int16_t wt[40];
+    int32_t L_max;
+    int32_t L_power;
+    int16_t R;
+    int16_t S;
+    int16_t dmax;
+    int16_t scale;
+    int16_t temp;
+    int32_t L_temp;
+#if !(defined(__GNUC__)  &&  defined(__i386__))
+    int16_t lambda;
+#endif
+
+    /* Search of the optimum scaling of d[0..39]. */
+    dmax = 0;
+    for (k = 0;  k < 40;  k++)
+    {
+        temp = d[k];
+        temp = gsm_abs(temp);
+        if (temp > dmax)
+            dmax = temp;
+        /*endif*/
+    }
+    /*endfor*/
+
+    if (dmax == 0)
+    {
+        temp = 0;
+    }
+    else
+    {
+        assert(dmax > 0);
+        temp = gsm0610_norm((int32_t) dmax << 16);
+    }
+    /*endif*/
+
+    if (temp > 6)
+        scale = 0;
+    else
+        scale = (int16_t) (6 - temp);
+    /*endif*/
+    assert(scale >= 0);
+
+    /* Initialization of a working array wt */
+    for (k = 0;  k < 40;  k++)
+        wt[k] = d[k] >> scale;
+    /*endfor*/
+
+    /* Search for the maximum cross-correlation and coding of the LTP lag */
+#if defined(__GNUC__)  &&  defined(__i386__)
+    L_max = gsm0610_max_cross_corr(wt, dp, &Nc);
+#else
+    L_max = 0;
+    Nc = 40; /* index for the maximum cross-correlation */
+
+    for (lambda = 40;  lambda <= 120;  lambda++)
+    {
+        int32_t L_result;
+
+        L_result  = (wt[0]*dp[0 - lambda])
+                  + (wt[1]*dp[1 - lambda])
+                  + (wt[2]*dp[2 - lambda])
+                  + (wt[3]*dp[3 - lambda])
+                  + (wt[4]*dp[4 - lambda])
+                  + (wt[5]*dp[5 - lambda])
+                  + (wt[6]*dp[6 - lambda])
+                  + (wt[7]*dp[7 - lambda])
+                  + (wt[8]*dp[8 - lambda])
+                  + (wt[9]*dp[9 - lambda])
+                  + (wt[10]*dp[10 - lambda])
+                  + (wt[11]*dp[11 - lambda])
+                  + (wt[12]*dp[12 - lambda])
+                  + (wt[13]*dp[13 - lambda])
+                  + (wt[14]*dp[14 - lambda])
+                  + (wt[15]*dp[15 - lambda])
+                  + (wt[16]*dp[16 - lambda])
+                  + (wt[17]*dp[17 - lambda])
+                  + (wt[18]*dp[18 - lambda])
+                  + (wt[19]*dp[19 - lambda])
+                  + (wt[20]*dp[20 - lambda])
+                  + (wt[21]*dp[21 - lambda])
+                  + (wt[22]*dp[22 - lambda])
+                  + (wt[23]*dp[23 - lambda])
+                  + (wt[24]*dp[24 - lambda])
+                  + (wt[25]*dp[25 - lambda])
+                  + (wt[26]*dp[26 - lambda])
+                  + (wt[27]*dp[27 - lambda])
+                  + (wt[28]*dp[28 - lambda])
+                  + (wt[29]*dp[29 - lambda])
+                  + (wt[30]*dp[30 - lambda])
+                  + (wt[31]*dp[31 - lambda])
+                  + (wt[32]*dp[32 - lambda])
+                  + (wt[33]*dp[33 - lambda])
+                  + (wt[34]*dp[34 - lambda])
+                  + (wt[35]*dp[35 - lambda])
+                  + (wt[36]*dp[36 - lambda])
+                  + (wt[37]*dp[37 - lambda])
+                  + (wt[38]*dp[38 - lambda])
+                  + (wt[39]*dp[39 - lambda]);
+
+        if (L_result > L_max)
+        {
+            Nc = lambda;
+            L_max = L_result;
+        }
+        /*endif*/
+    }
+    /*endfor*/
+#endif
+    *Nc_out = Nc;
+
+    L_max <<= 1;
+
+    /* Rescaling of L_max */
+    assert(scale <= 100  &&  scale >=  -100);
+    L_max = L_max >> (6 - scale);
+
+    assert(Nc <= 120  &&  Nc >= 40);
+
+    /* Compute the power of the reconstructed short term residual signal dp[..] */
+    L_power = 0;
+    for (k = 0;  k < 40;  k++)
+    {
+        L_temp = dp[k - Nc] >> 3;
+        L_power += L_temp*L_temp;
+    }
+    /*endfor*/
+    L_power <<= 1;  /* from L_MULT */
+
+    /* Normalization of L_max and L_power */
+    if (L_max <= 0)
+        return  0;
+    /*endif*/
+    if (L_max >= L_power)
+        return  3;
+    /*endif*/
+    temp = gsm0610_norm(L_power);
+
+    R = (int16_t) ((L_max << temp) >> 16);
+    S = (int16_t) ((L_power << temp) >> 16);
+
+    /* Coding of the LTP gain */
+
+    /* Table 4.3a must be used to obtain the level DLB[i] for the
+       quantization of the LTP gain b to get the coded version bc. */
+    for (bc = 0;  bc <= 2;  bc++)
+    {
+        if (R <= gsm_mult(S, gsm_DLB[bc]))
+            break;
+        /*endif*/
+    }
+    /*endfor*/
+    return  bc;
+}
+/*- End of function --------------------------------------------------------*/
+
+/* 4.2.12 */
+static void long_term_analysis_filtering(int16_t bc,
+                                         int16_t Nc,
+                                         int16_t *dp,    // previous d   [-120..-1]      IN
+                                         int16_t d[40],
+                                         int16_t dpp[40],
+                                         int16_t e[40])
+{
+    int k;
+
+    /* In this part, we have to decode the bc parameter to compute
+       the samples of the estimate dpp[0..39].  The decoding of bc needs the
+       use of table 4.3b.  The long term residual signal e[0..39]
+       is then calculated to be fed to the RPE encoding section. */
+    for (k = 0;  k < 40;  k++)
+    {
+        dpp[k] = gsm_mult_r(gsm_QLB[bc], dp[k - Nc]);
+        e[k] = gsm_sub(d[k], dpp[k]);
+    }
+    /*endfor*/
+}
+/*- End of function --------------------------------------------------------*/
+
+/* 4x for 160 samples */
+void gsm0610_long_term_predictor(gsm0610_state_t *s,
+                                 int16_t d[40],
+                                 int16_t *dp,        // [-120..-1] d'    IN
+                                 int16_t e[40],
+                                 int16_t dpp[40],
+                                 int16_t *Nc,
+                                 int16_t *bc)
+{
+#if 0
+    assert(d);
+    assert(dp);
+    assert(e);
+    assert(dpp);
+    assert(Nc);
+    assert(bc);
+#endif
+
+    *bc = evaluate_ltp_parameters(d, dp, Nc);
+    long_term_analysis_filtering(*bc, *Nc, dp, d, dpp, e);
+}
+/*- End of function --------------------------------------------------------*/
+
+/* 4.3.2 */
+void gsm0610_long_term_synthesis_filtering(gsm0610_state_t *s,
+                                           int16_t Ncr,
+                                           int16_t bcr,
+                                           int16_t erp[40],
+                                           int16_t *drp)    // [-120..-1] IN, [0..40] OUT
+{
+    int k;
+    int16_t brp;
+    int16_t drpp;
+    int16_t Nr;
+
+    /* This procedure uses the bcr and Ncr parameter to realize the
+       long term synthesis filter.  The decoding of bcr needs
+       table 4.3b. */
+
+    /* Check the limits of Nr. */
+    Nr = (Ncr < 40  ||  Ncr > 120)  ?  s->nrp  :  Ncr;
+    s->nrp = Nr;
+    assert (Nr >= 40  &&  Nr <= 120);
+
+    /* Decode the LTP gain, bcr */
+    brp = gsm_QLB[bcr];
+
+    /* Compute the reconstructed short term residual signal, drp[0..39] */
+    assert(brp != INT16_MIN);
+    for (k = 0;  k < 40;  k++)
+    {
+        drpp = gsm_mult_r(brp, drp[k - Nr]);
+        drp[k] = gsm_add(erp[k], drpp);
+    }
+    /*endfor*/
+
+    /* Update the reconstructed short term residual signal, drp[-1..-120] */
+    for (k = 0;  k < 120;  k++)
+        drp[k - 120] = drp[k - 80];
+    /*endfor*/
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/gsm0610_lpc.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/gsm0610_lpc.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,534 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * gsm0610_lpc.c - GSM 06.10 full rate speech codec.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2006 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * This code is based on the widely used GSM 06.10 code available from
+ * http://kbs.cs.tu-berlin.de/~jutta/toast.html
+ *
+ * $Id: gsm0610_lpc.c,v 1.20 2008/07/02 14:48:25 steveu Exp $
+ */
+
+/*! \file */
+
+#if defined(HAVE_CONFIG_H)
+#include <config.h>
+#endif
+
+#include <assert.h>
+#include <inttypes.h>
+#include "floating_fudge.h"
+#if defined(HAVE_TGMATH_H)
+#include <tgmath.h>
+#endif
+#if defined(HAVE_MATH_H)
+#include <math.h>
+#endif
+#include <stdlib.h>
+
+#include "spandsp/telephony.h"
+#include "spandsp/bitstream.h"
+#include "spandsp/bit_operations.h"
+#include "spandsp/dc_restore.h"
+#include "spandsp/vector_int.h"
+#include "spandsp/gsm0610.h"
+
+#include "gsm0610_local.h"
+
+/* 4.2.4 .. 4.2.7 LPC ANALYSIS SECTION */
+
+/* The number of left shifts needed to normalize the 32 bit
+   variable x for positive values on the interval
+   with minimum of
+   minimum of 1073741824  (01000000000000000000000000000000) and 
+   maximum of 2147483647  (01111111111111111111111111111111)
+   and for negative values on the interval with
+   minimum of -2147483648 (-10000000000000000000000000000000) and
+   maximum of -1073741824 ( -1000000000000000000000000000000).
+  
+   In order to normalize the result, the following
+   operation must be done: norm_var1 = x << gsm0610_norm(x);
+  
+   (That's 'ffs', only from the left, not the right..)
+*/
+
+int16_t gsm0610_norm(int32_t x)
+{
+    assert(x != 0);
+
+    if (x < 0)
+    {
+        if (x <= -1073741824)
+            return  0;
+        /*endif*/
+        x = ~x;
+    }
+    /*endif*/
+    return (int16_t) (30 - top_bit(x));
+}
+/*- End of function --------------------------------------------------------*/
+
+/*
+   (From p. 46, end of section 4.2.5)
+  
+   NOTE: The following lines gives [sic] one correct implementation
+         of the div(num, denum) arithmetic operation.  Compute div
+         which is the integer division of num by denom: with
+         denom >= num > 0
+*/
+static int16_t gsm_div(int16_t num, int16_t denom)
+{
+    int32_t num32;
+    int32_t denom32;
+    int16_t div;
+    int k;
+
+    /* The parameter num sometimes becomes zero.
+       Although this is explicitly guarded against in 4.2.5,
+       we assume that the result should then be zero as well. */
+
+    assert(num >= 0  &&  denom >= num);
+    if (num == 0)
+        return  0;
+    /*endif*/
+    num32 = num;
+    denom32 = denom;
+    div = 0;
+    k = 15;
+    while (k--)
+    {
+        div <<= 1;
+        num32 <<= 1;
+
+        if (num32 >= denom32)
+        {
+            num32 -= denom32;
+            div++;
+        }
+        /*endif*/
+    }
+    /*endwhile*/
+
+    return  div;
+}
+/*- End of function --------------------------------------------------------*/
+
+#if defined(__GNUC__)  &&  defined(__i386__)
+void gsm0610_vec_vsraw(const int16_t *p, int n, int bits)
+{
+    static const int64_t ones = 0x0001000100010001LL;
+
+    if (n == 0)
+        return;
+    /*endif*/
+    __asm__ __volatile__(
+        " leal -16(%%esi,%%eax,2),%%edx;\n"         /* edx = top - 16 */
+        " emms;\n"
+        " movd %%ecx,%%mm3;\n"
+        " movq %[ones],%%mm2;\n"
+        " psllw %%mm3,%%mm2;\n"
+        " psrlw $1,%%mm2;\n"
+        " cmpl %%edx,%%esi;"
+        " ja 4f;\n"
+
+         " .p2align 2;\n"
+        /* 8 words per iteration */
+        "6:\n"
+        " movq (%%esi),%%mm0;\n"
+        " movq 8(%%esi),%%mm1;\n"
+        " paddsw %%mm2,%%mm0;\n"
+        " psraw %%mm3,%%mm0;\n"
+        " paddsw %%mm2,%%mm1;\n"
+        " psraw %%mm3,%%mm1;\n"
+        " movq %%mm0,(%%esi);\n"
+        " movq %%mm1,8(%%esi);\n"
+        " addl $16,%%esi;\n"
+        " cmpl %%edx,%%esi;\n"
+        " jbe 6b;\n"
+
+        " .p2align 2;\n"
+        "4:\n"
+        " addl $12,%%edx;\n"                        /* now edx = top-4 */
+        " cmpl %%edx,%%esi;\n"
+        " ja 3f;\n"
+
+        " .p2align 2;\n"
+        /* do up to 6 words, two per iteration */
+        "5:\n"
+        " movd  (%%esi),%%mm0;\n"
+        " paddsw %%mm2,%%mm0;\n"
+        " psraw %%mm3,%%mm0;\n"
+        " movd %%mm0,(%%esi);\n"
+        " addl $4,%%esi;\n"
+        " cmpl %%edx,%%esi;\n"
+        " jbe 5b;\n"
+
+        " .p2align 2;\n"
+        "3:\n"
+        " addl $2,%%edx;\n"                        /* now edx = top-2 */
+        " cmpl %%edx,%%esi;\n"
+        " ja 2f;\n"
+        
+        " movzwl (%%esi),%%eax;\n"
+        " movd %%eax,%%mm0;\n"
+        " paddsw %%mm2,%%mm0;\n"
+        " psraw %%mm3,%%mm0;\n"
+        " movd %%mm0,%%eax;\n"
+        " movw %%ax,(%%esi);\n"
+
+        " .p2align 2;\n"
+        "2:\n"
+        " emms;\n"
+        :
+        : "S" (p), "a" (n), "c" (bits), [ones] "m" (ones)
+        : "edx"
+    );
+}
+/*- End of function --------------------------------------------------------*/
+#endif
+
+/* 4.2.4 */
+static void autocorrelation(int16_t amp[GSM0610_FRAME_LEN], int32_t L_ACF[9])
+{
+    int k;
+    int16_t smax;
+    int16_t scalauto;
+#if !(defined(__GNUC__)  &&  defined(__i386__))
+    int i;
+    int temp;
+    int16_t *sp;
+    int16_t sl;
+#endif
+    
+    /* The goal is to compute the array L_ACF[k].  The signal s[i] must
+       be scaled in order to avoid an overflow situation. */
+
+    /* Dynamic scaling of the array  s[0..159] */
+    /* Search for the maximum. */
+#if defined(__GNUC__)  &&  defined(__i386__)
+    smax = saturate(vec_min_maxi16(amp, GSM0610_FRAME_LEN, NULL));
+#else
+    for (smax = 0, k = 0;  k < GSM0610_FRAME_LEN;  k++)
+    {
+        temp = gsm_abs(amp[k]);
+        if (temp > smax)
+            smax = (int16_t) temp;
+        /*endif*/
+    }
+    /*endfor*/
+#endif
+
+    /* Computation of the scaling factor. */
+    if (smax == 0)
+    {
+        scalauto = 0;
+    }
+    else
+    {
+        assert(smax > 0);
+        scalauto = (int16_t) (4 - gsm0610_norm((int32_t) smax << 16));
+    }
+    /*endif*/
+
+    /* Scaling of the array s[0...159] */
+#if defined(__GNUC__)  &&  defined(__i386__)
+    if (scalauto > 0)
+        gsm0610_vec_vsraw(amp, GSM0610_FRAME_LEN, scalauto);
+    /*endif*/
+#else
+    if (scalauto > 0)
+    {
+        for (k = 0;  k < GSM0610_FRAME_LEN;  k++)
+            amp[k] = gsm_mult_r(amp[k], 16384 >> (scalauto - 1));
+        /*endfor*/
+    }
+    /*endif*/
+#endif
+
+    /* Compute the L_ACF[..]. */
+#if defined(__GNUC__)  &&  defined(__i386__)
+    for (k = 0;  k < 9;  k++)
+        L_ACF[k] = vec_dot_prodi16(amp, amp + k, GSM0610_FRAME_LEN - k) << 1;
+    /*endfor*/
+#else
+    sp = amp;
+    sl = *sp;
+    L_ACF[0] = ((int32_t) sl*sp[0]);
+    sl = *++sp;
+    L_ACF[0] += ((int32_t) sl*sp[0]);
+    L_ACF[1] = ((int32_t) sl*sp[-1]);
+    sl = *++sp;
+    L_ACF[0] += ((int32_t) sl*sp[0]);
+    L_ACF[1] += ((int32_t) sl*sp[-1]);
+    L_ACF[2] = ((int32_t) sl*sp[-2]);
+    sl = *++sp;
+    L_ACF[0] += ((int32_t) sl*sp[0]);
+    L_ACF[1] += ((int32_t) sl*sp[-1]);
+    L_ACF[2] += ((int32_t) sl*sp[-2]);
+    L_ACF[3] = ((int32_t) sl*sp[-3]);
+    sl = *++sp;
+    L_ACF[0] += ((int32_t) sl*sp[0]);
+    L_ACF[1] += ((int32_t) sl*sp[-1]);
+    L_ACF[2] += ((int32_t) sl*sp[-2]);
+    L_ACF[3] += ((int32_t) sl*sp[-3]);
+    L_ACF[4] = ((int32_t) sl*sp[-4]);
+    sl = *++sp;
+    L_ACF[0] += ((int32_t) sl*sp[0]);
+    L_ACF[1] += ((int32_t) sl*sp[-1]);
+    L_ACF[2] += ((int32_t) sl*sp[-2]);
+    L_ACF[3] += ((int32_t) sl*sp[-3]);
+    L_ACF[4] += ((int32_t) sl*sp[-4]);
+    L_ACF[5] = ((int32_t) sl*sp[-5]);
+    sl = *++sp;
+    L_ACF[0] += ((int32_t) sl*sp[0]);
+    L_ACF[1] += ((int32_t) sl*sp[-1]);
+    L_ACF[2] += ((int32_t) sl*sp[-2]);
+    L_ACF[3] += ((int32_t) sl*sp[-3]);
+    L_ACF[4] += ((int32_t) sl*sp[-4]);
+    L_ACF[5] += ((int32_t) sl*sp[-5]);
+    L_ACF[6] = ((int32_t) sl*sp[-6]);
+    sl = *++sp;
+    L_ACF[0] += ((int32_t) sl*sp[0]);
+    L_ACF[1] += ((int32_t) sl*sp[-1]);
+    L_ACF[2] += ((int32_t) sl*sp[-2]);
+    L_ACF[3] += ((int32_t) sl*sp[-3]);
+    L_ACF[4] += ((int32_t) sl*sp[-4]);
+    L_ACF[5] += ((int32_t) sl*sp[-5]);
+    L_ACF[6] += ((int32_t) sl*sp[-6]);
+    L_ACF[7] = ((int32_t) sl*sp[-7]);
+    sl = *++sp;
+    L_ACF[0] += ((int32_t) sl*sp[0]);
+    L_ACF[1] += ((int32_t) sl*sp[-1]);
+    L_ACF[2] += ((int32_t) sl*sp[-2]);
+    L_ACF[3] += ((int32_t) sl*sp[-3]);
+    L_ACF[4] += ((int32_t) sl*sp[-4]);
+    L_ACF[5] += ((int32_t) sl*sp[-5]);
+    L_ACF[6] += ((int32_t) sl*sp[-6]);
+    L_ACF[7] += ((int32_t) sl*sp[-7]);
+    L_ACF[8] = ((int32_t) sl*sp[-8]);
+    for (i = 9;  i < GSM0610_FRAME_LEN;  i++)
+    {
+        sl = *++sp;
+        L_ACF[0] += ((int32_t) sl*sp[0]);
+        L_ACF[1] += ((int32_t) sl*sp[-1]);
+        L_ACF[2] += ((int32_t) sl*sp[-2]);
+        L_ACF[3] += ((int32_t) sl*sp[-3]);
+        L_ACF[4] += ((int32_t) sl*sp[-4]);
+        L_ACF[5] += ((int32_t) sl*sp[-5]);
+        L_ACF[6] += ((int32_t) sl*sp[-6]);
+        L_ACF[7] += ((int32_t) sl*sp[-7]);
+        L_ACF[8] += ((int32_t) sl*sp[-8]);
+    }
+    /*endfor*/
+    for (k = 0;  k < 9;  k++)
+        L_ACF[k] <<= 1;
+    /*endfor*/
+#endif
+    /* Rescaling of the array s[0..159] */
+    if (scalauto > 0)
+    {
+        assert(scalauto <= 4); 
+        for (k = 0;  k < GSM0610_FRAME_LEN;  k++)
+            amp[k] <<= scalauto;
+        /*endfor*/
+    }
+    /*endif*/
+}
+/*- End of function --------------------------------------------------------*/
+
+/* 4.2.5 */
+static void reflection_coefficients(int32_t L_ACF[9], int16_t r[8])
+{
+    int i;
+    int m;
+    int n;
+    int16_t temp;
+    int16_t ACF[9];
+    int16_t P[9];
+    int16_t K[9];
+
+    /* Schur recursion with 16 bits arithmetic. */
+    if (L_ACF[0] == 0)
+    {
+        for (i = 8;  i--;  *r++ = 0)
+            ;
+        /*endfor*/
+        return;
+    }
+    /*endif*/
+
+    assert(L_ACF[0] != 0);
+    temp = gsm0610_norm(L_ACF[0]);
+
+    assert(temp >= 0  &&  temp < 32);
+
+    /* ? overflow ? */
+    for (i = 0;  i <= 8;  i++)
+        ACF[i] = (int16_t) ((L_ACF[i] << temp) >> 16);
+    /*endfor*/
+
+    /* Initialize array P[..] and K[..] for the recursion. */
+    for (i = 1;  i <= 7;  i++)
+        K[i] = ACF[i];
+    /*endfor*/
+    for (i = 0;  i <= 8;  i++)
+        P[i] = ACF[i];
+    /*endfor*/
+    /* Compute reflection coefficients */
+    for (n = 1;  n <= 8;  n++, r++)
+    {
+        temp = P[1];
+        temp = gsm_abs (temp);
+        if (P[0] < temp)
+        {
+            for (i = n;  i <= 8;  i++)
+                *r++ = 0;
+            /*endfor*/
+            return;
+        }
+        /*endif*/
+
+        *r = gsm_div(temp, P[0]);
+
+        assert(*r >= 0);
+        if (P[1] > 0)
+            *r = -*r;     /* r[n] = sub(0, r[n]) */
+        /*endif*/
+        assert(*r != INT16_MIN);
+        if (n == 8)
+            return; 
+        /*endif*/
+
+        /* Schur recursion */
+        temp = gsm_mult_r(P[1], *r);
+        P[0] = gsm_add(P[0], temp);
+
+        for (m = 1;  m <= 8 - n;  m++)
+        {
+            temp = gsm_mult_r(K[m], *r);
+            P[m] = gsm_add(P[m + 1], temp);
+
+            temp = gsm_mult_r(P[m + 1], *r);
+            K[m] = gsm_add(K[m], temp);
+        }
+        /*endfor*/
+    }
+    /*endfor*/
+}
+/*- End of function --------------------------------------------------------*/
+
+/* 4.2.6 */
+static void transform_to_log_area_ratios(int16_t r[8])
+{
+    int16_t temp;
+    int i;
+
+    /* The following scaling for r[..] and LAR[..] has been used:
+      
+       r[..]   = integer (real_r[..]*32768.); -1 <= real_r < 1.
+       LAR[..] = integer (real_LAR[..] * 16384);
+       with -1.625 <= real_LAR <= 1.625
+    */
+
+    /* Computation of the LAR[0..7] from the r[0..7] */
+    for (i = 1;  i <= 8;  i++, r++)
+    {
+        temp = *r;
+        temp = gsm_abs(temp);
+        assert(temp >= 0);
+
+        if (temp < 22118)
+        {
+            temp >>= 1;
+        }
+        else if (temp < 31130)
+        {
+            assert(temp >= 11059);
+            temp -= 11059;
+        }
+        else
+        {
+            assert(temp >= 26112);
+            temp -= 26112;
+            temp <<= 2;
+        }
+        /*endif*/
+
+        *r = (*r < 0)  ?  -temp  :  temp;
+        assert(*r != INT16_MIN);
+    }
+    /*endfor*/
+}
+/*- End of function --------------------------------------------------------*/
+
+/* 4.2.7 */
+static void quantization_and_coding(int16_t LAR[8])
+{
+    int16_t temp;
+
+    /* This procedure needs four tables; the following equations
+       give the optimum scaling for the constants:
+        
+       A[0..7] = integer(real_A[0..7] * 1024)
+       B[0..7] = integer(real_B[0..7] *  512)
+       MAC[0..7] = maximum of the LARc[0..7]
+       MIC[0..7] = minimum of the LARc[0..7] */
+
+#undef STEP
+#define STEP(A,B,MAC,MIC)                                       \
+        temp = gsm_mult(A, *LAR);                               \
+        temp = gsm_add(temp, B);                                \
+        temp = gsm_add(temp, 256);                              \
+        temp >>= 9;                                             \
+        *LAR  = (int16_t) ((temp > MAC)                         \
+                         ?                                      \
+                         MAC - MIC                              \
+                         :                                      \
+                         ((temp < MIC)  ?  0  :  temp - MIC));  \
+        LAR++;
+
+    STEP(20480,     0,  31, -32);
+    STEP(20480,     0,  31, -32);
+    STEP(20480,  2048,  15, -16);
+    STEP(20480, -2560,  15, -16);
+
+    STEP(13964,    94,   7,  -8);
+    STEP(15360, -1792,   7,  -8);
+    STEP( 8534,  -341,   3,  -4);
+    STEP( 9036, -1144,   3,  -4);
+#undef STEP
+}
+/*- End of function --------------------------------------------------------*/
+
+void gsm0610_lpc_analysis(gsm0610_state_t *s,
+                          int16_t amp[GSM0610_FRAME_LEN],
+                          int16_t LARc[8])
+{
+    int32_t L_ACF[9];
+
+    autocorrelation(amp, L_ACF);
+    reflection_coefficients(L_ACF, LARc);
+    transform_to_log_area_ratios(LARc);
+    quantization_and_coding(LARc);
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/gsm0610_preprocess.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/gsm0610_preprocess.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,150 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * gsm0610_preprocess.c - GSM 06.10 full rate speech codec.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2006 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * This code is based on the widely used GSM 06.10 code available from
+ * http://kbs.cs.tu-berlin.de/~jutta/toast.html
+ *
+ * $Id: gsm0610_preprocess.c,v 1.13 2008/07/02 14:48:25 steveu Exp $
+ */
+
+/*! \file */
+
+#if defined(HAVE_CONFIG_H)
+#include <config.h>
+#endif
+
+#include <assert.h>
+#include <inttypes.h>
+#include "floating_fudge.h"
+#if defined(HAVE_TGMATH_H)
+#include <tgmath.h>
+#endif
+#if defined(HAVE_MATH_H)
+#include <math.h>
+#endif
+#include <stdlib.h>
+
+#include "spandsp/telephony.h"
+#include "spandsp/bitstream.h"
+#include "spandsp/dc_restore.h"
+#include "spandsp/gsm0610.h"
+
+#include "gsm0610_local.h"
+
+/*
+    4.2.0 .. 4.2.3  PREPROCESSING SECTION
+    
+    After A-law to linear conversion (or directly from the
+    A to D converter) the following scaling is assumed for
+    input to the RPE-LTP algorithm:
+  
+    in:  0.1.....................12
+         S.v.v.v.v.v.v.v.v.v.v.v.v.*.*.*
+  
+    Where S is the sign bit, v a valid bit, and * a "don't care" bit.
+    The original signal is called sop[..]
+  
+    out:   0.1................... 12 
+         S.S.v.v.v.v.v.v.v.v.v.v.v.v.0.0
+*/
+
+void gsm0610_preprocess(gsm0610_state_t *s, const int16_t amp[GSM0610_FRAME_LEN], int16_t so[GSM0610_FRAME_LEN])
+{
+    int16_t z1;
+    int16_t mp;
+    int16_t s1;
+    int16_t msp;
+    int16_t SO;
+    int32_t L_z2;
+    int32_t L_s2;
+    int32_t L_temp;
+#if !defined(__GNUC__)
+    int16_t lsp;
+#endif
+    int k;
+
+    z1 = s->z1;
+    L_z2 = s->L_z2;
+    mp = s->mp;
+    for (k = 0;  k < GSM0610_FRAME_LEN;  k++)
+    {
+        /* 4.2.1   Downscaling of the input signal */
+        SO = (amp[k] >> 1) & ~3;
+
+        assert(SO >= -0x4000); // downscaled by
+        assert(SO <=  0x3FFC); // previous routine.
+
+        /* 4.2.2   Offset compensation */
+
+        /*  This part implements a high-pass filter and requires extended
+            arithmetic precision for the recursive part of this filter.
+            The input of this procedure is the array so[0...159] and the
+            output the array sof[ 0...159 ].
+        */
+        /* Compute the non-recursive part */
+        s1 = SO - z1;
+        z1 = SO;
+
+        assert(s1 != INT16_MIN);
+
+        /* Compute the recursive part */
+        L_s2 = s1;
+        L_s2 <<= 15;
+
+        /* Perform a 31 by 16 bits multiplication */
+#if defined(__GNUC__)
+        L_z2 = ((int64_t) L_z2*32735 + 0x4000) >> 15;
+        /* Alternate (ANSI) version of below line does slightly different rounding:
+         * L_temp = L_z2 >> 9;
+         * L_temp += L_temp >> 5;
+         * L_temp = (++L_temp) >> 1;
+         * L_z2 = L_z2 - L_temp;
+         */
+        L_z2 = gsm_l_add(L_z2, L_s2);
+#else
+        /* This does L_z2  = L_z2 * 0x7FD5/0x8000 + L_s2 */
+        msp = (int16_t) (L_z2 >> 15);
+        lsp = (int16_t) (L_z2 - ((int32_t) msp << 15));
+
+        L_s2 += gsm_mult_r(lsp, 32735);
+        L_temp = (int32_t) msp*32735;
+        L_z2 = gsm_l_add(L_temp, L_s2);
+#endif
+
+        /* Compute sof[k] with rounding */
+        L_temp = gsm_l_add(L_z2, 16384);
+
+        /* 4.2.3  Preemphasis */
+        msp = gsm_mult_r(mp, -28180);
+        mp = (int16_t) (L_temp >> 15);
+        so[k] = gsm_add(mp, msp);
+    }
+    /*endfor*/
+
+    s->z1 = z1;
+    s->L_z2 = L_z2;
+    s->mp = mp;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/gsm0610_rpe.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/gsm0610_rpe.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,541 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * gsm0610_rpe.c - GSM 06.10 full rate speech codec.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2006 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * This code is based on the widely used GSM 06.10 code available from
+ * http://kbs.cs.tu-berlin.de/~jutta/toast.html
+ *
+ * $Id: gsm0610_rpe.c,v 1.21 2008/07/02 14:48:25 steveu Exp $
+ */
+
+/*! \file */
+
+#if defined(HAVE_CONFIG_H)
+#include <config.h>
+#endif
+
+#include <assert.h>
+#include <inttypes.h>
+#include "floating_fudge.h"
+#if defined(HAVE_TGMATH_H)
+#include <tgmath.h>
+#endif
+#if defined(HAVE_MATH_H)
+#include <math.h>
+#endif
+#include <stdlib.h>
+
+#include "spandsp/telephony.h"
+#include "spandsp/bitstream.h"
+#include "spandsp/dc_restore.h"
+#include "spandsp/gsm0610.h"
+
+#include "gsm0610_local.h"
+
+/* 4.2.13 .. 4.2.17  RPE ENCODING SECTION */
+
+/* 4.2.13 */
+static void weighting_filter(const int16_t *e,     // signal [-5..0.39.44] IN
+                             int16_t x[40])
+{
+#if defined(__GNUC__)  &&  defined(__i386__)
+    /* Table 4.4   Coefficients of the weighting filter */
+    /* This must be padded to a multiple of 4 for MMX to work */
+    static const union
+    {
+        int16_t gsm_H[12];
+        uint64_t x[3];
+    } gsm_H =
+    {
+        {
+            -134, -374, 0, 2054, 5741, 8192, 5741, 2054, 0, -374, -134, 0
+        }
+
+    };
+
+    __asm__ __volatile__(
+        " emms;\n"
+        " addl $-10,%%ecx;\n"
+        " movl $0x1000,%%eax;\n"
+        " movd %%eax,%%mm5;\n"              /* for rounding */
+        " movq %[gsm_H],%%mm1;\n"
+        " movq %[gsm_H8],%%mm2;\n"
+        " movq %[gsm_H16],%%mm3;\n"
+        " xorl %%esi,%%esi;\n"
+        " .p2align 2;\n"
+        "1:;\n"
+        " movq (%%ecx,%%esi,2),%%mm0;\n"
+        " pmaddwd %%mm1,%%mm0;\n"
+ 
+        " movq 8(%%ecx,%%esi,2),%%mm4;\n"
+        " pmaddwd %%mm2,%%mm4;\n"
+        " paddd %%mm4,%%mm0;\n"
+
+        " movq 16(%%ecx,%%esi,2),%%mm4;\n"
+        " pmaddwd %%mm3,%%mm4;\n"
+        " paddd %%mm4,%%mm0;\n"
+
+        " movq %%mm0,%%mm4;\n"
+        " punpckhdq %%mm0,%%mm4;\n"         /* mm4 has high int32 of mm0 dup'd */
+        " paddd %%mm4,%%mm0;\n"
+
+        " paddd %%mm5,%%mm0;\n"             /* Add for roundoff */
+        " psrad $13,%%mm0;\n"
+        " packssdw %%mm0,%%mm0;\n"        
+        " movd %%mm0,%%eax;\n"              /* eax has result */
+        " movw %%ax,(%%edi,%%esi,2);\n"
+        " incl %%esi;\n"
+        " cmpl $39,%%esi;\n"
+        " jle 1b;\n"
+        " emms;\n"
+        :
+        : "c" (e), "D" (x), [gsm_H] "X" (gsm_H.x[0]), [gsm_H8] "X" (gsm_H.x[1]), [gsm_H16] "X" (gsm_H.x[2])
+        : "eax", "edx", "esi"
+    );
+#else
+    int32_t L_result;
+    int k;
+
+    /* The coefficients of the weighting filter are stored in a table
+       (see table 4.4).  The following scaling is used:
+      
+        H[0..10] = integer(real_H[0..10] * 8192); 
+    */
+    /* Initialization of a temporary working array wt[0...49] */
+
+    /* for (k =  0;  k <=  4;  k++) wt[k] = 0;
+     * for (k =  5;  k <= 44;  k++) wt[k] = *e++;
+     * for (k = 45;  k <= 49;  k++) wt[k] = 0;
+     *
+     *  (e[-5..-1] and e[40..44] are allocated by the caller,
+     *  are initially zero and are not written anywhere.)
+     */
+    e -= 5;
+
+    /* Compute the signal x[0..39] */
+    for (k = 0;  k < 40;  k++)
+    {
+        L_result = 8192 >> 1;
+
+        /* for (i = 0; i <= 10; i++)
+         * {
+         *      L_temp   = gsm_l_mult(wt[k + i], gsm_H[i]);
+         *      L_result = gsm_l_add(L_result, L_temp);
+         * }
+         */
+
+#undef STEP
+#define STEP(i,H) (e[k + i] * (int32_t) H)
+
+        /* Every one of these multiplications is done twice,
+           but I don't see an elegant way to optimize this. 
+           Do you?
+        */
+        L_result += STEP( 0,  -134);
+        L_result += STEP( 1,  -374);
+              // += STEP( 2,  0   );
+        L_result += STEP( 3,  2054);
+        L_result += STEP( 4,  5741);
+        L_result += STEP( 5,  8192);
+        L_result += STEP( 6,  5741);
+        L_result += STEP( 7,  2054);
+              // += STEP( 8,  0   );
+        L_result += STEP( 9,  -374);
+        L_result += STEP(10,  -134);
+
+        /* 2 adds vs. >> 16 => 14, minus one shift to compensate for
+           those we lost when replacing L_MULT by '*'. */
+        L_result >>= 13;
+        x[k] = saturate(L_result);
+    }
+    /*endfor*/
+#endif
+}
+/*- End of function --------------------------------------------------------*/
+
+/* 4.2.14 */
+static void rpe_grid_selection(int16_t x[40], int16_t xM[13], int16_t *Mc_out)
+{
+    int i;
+    int32_t L_result;
+    int32_t L_temp;
+    int32_t EM;     /* xxx should be L_EM? */
+    int16_t Mc;
+    int32_t L_common_0_3;
+
+    /* The signal x[0..39] is used to select the RPE grid which is
+       represented by Mc. */
+
+    EM = 0;
+    Mc = 0;
+
+#undef STEP
+#define STEP(m,i)                           \
+    L_temp = x[m + 3*i] >> 2;               \
+    L_result += L_temp*L_temp;
+
+    /* Common part of 0 and 3 */
+    L_result = 0;
+    STEP(0, 1);
+    STEP(0, 2);
+    STEP(0, 3);
+    STEP(0, 4);
+    STEP(0, 5);
+    STEP(0, 6);
+    STEP(0, 7);
+    STEP(0, 8);
+    STEP(0, 9);
+    STEP(0, 10);
+    STEP(0, 11);
+    STEP(0, 12);
+    L_common_0_3 = L_result;
+
+    /* i = 0 */
+
+    STEP(0, 0);
+    L_result <<= 1; /* implicit in L_MULT */
+    EM = L_result;
+
+    /* i = 1 */
+
+    L_result = 0;
+    STEP(1, 0);
+    STEP(1, 1);
+    STEP(1, 2);
+    STEP(1, 3);
+    STEP(1, 4);
+    STEP(1, 5);
+    STEP(1, 6);
+    STEP(1, 7);
+    STEP(1, 8);
+    STEP(1, 9);
+    STEP(1, 10);
+    STEP(1, 11);
+    STEP(1, 12);
+    L_result <<= 1;
+    if (L_result > EM)
+    {
+        Mc = 1;
+        EM = L_result;
+    }
+    /*endif*/
+
+    /* i = 2 */
+
+    L_result = 0;
+    STEP(2, 0);
+    STEP(2, 1);
+    STEP(2, 2);
+    STEP(2, 3);
+    STEP(2, 4);
+    STEP(2, 5);
+    STEP(2, 6);
+    STEP(2, 7);
+    STEP(2, 8);
+    STEP(2, 9);
+    STEP(2, 10);
+    STEP(2, 11);
+    STEP(2, 12);
+    L_result <<= 1;
+    if (L_result > EM)
+    {
+        Mc = 2;
+        EM = L_result;
+    }
+    /*endif*/
+
+    /* i = 3 */
+
+    L_result = L_common_0_3;
+    STEP(3, 12);
+    L_result <<= 1;
+    if (L_result > EM)
+    {
+        Mc = 3;
+        EM = L_result;
+    }
+    /*endif*/
+
+    /* Down-sampling by a factor 3 to get the selected xM[0..12]
+       RPE sequence. */
+    for (i = 0;  i < 13;  i++)
+        xM[i] = x[Mc + 3*i];
+    /*endfor*/
+    *Mc_out = Mc;
+}
+/*- End of function --------------------------------------------------------*/
+
+/* 4.12.15 */
+static void apcm_quantization_xmaxc_to_exp_mant(int16_t xmaxc,
+                                                int16_t *exp_out,
+                                                int16_t *mant_out)
+{
+    int16_t exp;
+    int16_t mant;
+
+    /* Compute exponent and mantissa of the decoded version of xmaxc */
+    exp = 0;
+    if (xmaxc > 15)
+        exp = (int16_t) ((xmaxc >> 3) - 1);
+    /*endif*/
+    mant = xmaxc - (exp << 3);
+
+    if (mant == 0)
+    {
+        exp = -4;
+        mant = 7;
+    }
+    else
+    {
+        while (mant <= 7)
+        {
+            mant = (int16_t) (mant << 1 | 1);
+            exp--;
+        }
+        /*endwhile*/
+        mant -= 8;
+    }
+    /*endif*/
+
+    assert(exp >= -4  &&  exp <= 6);
+    assert(mant >= 0  &&  mant <= 7);
+
+    *exp_out  = exp;
+    *mant_out = mant;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void apcm_quantization(int16_t xM[13],
+                              int16_t xMc[13],
+                              int16_t *mant_out,
+                              int16_t *exp_out,
+                              int16_t *xmaxc_out)
+{
+    /* Table 4.5   Normalized inverse mantissa used to compute xM/xmax */
+    static const int16_t gsm_NRFAC[8] =
+    {
+        29128, 26215, 23832, 21846, 20165, 18725, 17476, 16384
+    };
+    int i;
+    int itest;
+    int16_t xmax;
+    int16_t xmaxc;
+    int16_t temp;
+    int16_t temp1;
+    int16_t temp2;
+    int16_t exp;
+    int16_t mant;
+
+    /* Find the maximum absolute value xmax of xM[0..12]. */
+    xmax = 0;
+    for (i = 0;  i < 13;  i++)
+    {
+        temp = xM[i];
+        temp = gsm_abs(temp);
+        if (temp > xmax)
+            xmax = temp;
+        /*endif*/
+    }
+    /*endfor*/
+
+    /* Quantizing and coding of xmax to get xmaxc. */
+    exp = 0;
+    temp = xmax >> 9;
+    itest = 0;
+
+    for (i = 0;  i <= 5;  i++)
+    {
+        itest |= (temp <= 0);
+        temp >>= 1;
+
+        assert(exp <= 5);
+        if (itest == 0)
+            exp++;
+        /*endif*/
+    }
+    /*endfor*/
+
+    assert(exp <= 6  &&  exp >= 0);
+    temp = (int16_t) (exp + 5);
+
+    assert(temp <= 11  &&  temp >= 0);
+    xmaxc = gsm_add((xmax >> temp), exp << 3);
+
+    /* Quantizing and coding of the xM[0..12] RPE sequence
+       to get the xMc[0..12] */
+    apcm_quantization_xmaxc_to_exp_mant(xmaxc, &exp, &mant);
+
+    /* This computation uses the fact that the decoded version of xmaxc
+       can be calculated by using the exponent and the mantissa part of
+       xmaxc (logarithmic table).
+       So, this method avoids any division and uses only a scaling
+       of the RPE samples by a function of the exponent.  A direct 
+       multiplication by the inverse of the mantissa (NRFAC[0..7]
+       found in table 4.5) gives the 3 bit coded version xMc[0..12]
+       of the RPE samples.
+    */
+    /* Direct computation of xMc[0..12] using table 4.5 */
+    assert(exp <= 4096  &&  exp >= -4096);
+    assert(mant >= 0  &&  mant <= 7); 
+
+    temp1 = (int16_t) (6 - exp);    /* Normalization by the exponent */
+    temp2 = gsm_NRFAC[mant];        /* Inverse mantissa */
+
+    for (i = 0;  i < 13;  i++)
+    {
+        assert(temp1 >= 0  &&  temp1 < 16);
+
+        temp = xM[i] << temp1;
+        temp = gsm_mult(temp, temp2);
+        temp >>= 12;
+        xMc[i] = (int16_t) (temp + 4);      /* See note below */
+    }
+    /*endfor*/
+
+    /* NOTE: This equation is used to make all the xMc[i] positive. */
+    *mant_out  = mant;
+    *exp_out   = exp;
+    *xmaxc_out = xmaxc;
+}
+/*- End of function --------------------------------------------------------*/
+
+/* 4.2.16 */
+static void apcm_inverse_quantization(int16_t xMc[13],
+                                      int16_t mant,
+                                      int16_t exp,
+                                      int16_t xMp[13])
+{
+    /* Table 4.6   Normalized direct mantissa used to compute xM/xmax */
+    static const int16_t gsm_FAC[8] =
+    {
+        18431, 20479, 22527, 24575, 26623, 28671, 30719, 32767
+    };
+    int i;
+    int16_t temp;
+    int16_t temp1;
+    int16_t temp2;
+    int16_t temp3;
+
+    /* This part is for decoding the RPE sequence of coded xMc[0..12]
+       samples to obtain the xMp[0..12] array.  Table 4.6 is used to get
+       the mantissa of xmaxc (FAC[0..7]).
+    */
+#if 0
+    assert(mant >= 0  &&  mant <= 7);
+#endif
+
+    temp1 = gsm_FAC[mant];          /* See 4.2-15 for mant */
+    temp2 = gsm_sub(6, exp);        /* See 4.2-15 for exp */
+    temp3 = gsm_asl(1, gsm_sub (temp2, 1));
+
+    for (i = 0;  i < 13;  i++)
+    {
+        assert(xMc[i] >= 0  &&  xMc[i] <= 7);   /* 3 bit unsigned */
+
+        temp = (int16_t) ((xMc[i] << 1) - 7);   /* Restore sign */
+        assert(temp <= 7  &&  temp >= -7);      /* 4 bit signed */
+
+        temp <<= 12;                            /* 16 bit signed */
+        temp = gsm_mult_r(temp1, temp);
+        temp = gsm_add(temp, temp3);
+        xMp[i] = gsm_asr(temp, temp2);
+    }
+    /*endfor*/
+}
+/*- End of function --------------------------------------------------------*/
+
+/* 4.2.17 */
+static void rpe_grid_positioning(int16_t Mc,
+                                 int16_t xMp[13],
+                                 int16_t ep[40])
+{
+    int i = 13;
+
+    /* This procedure computes the reconstructed long term residual signal
+       ep[0..39] for the LTP analysis filter.  The inputs are the Mc
+       which is the grid position selection and the xMp[0..12] decoded
+       RPE samples which are upsampled by a factor of 3 by inserting zero
+       values.
+    */
+    assert(0 <= Mc  &&  Mc <= 3);
+
+    switch (Mc)
+    {
+    case 3:
+        *ep++ = 0;
+    case 2:
+        do
+        {
+            *ep++ = 0;
+    case 1:
+            *ep++ = 0;
+    case 0:
+            *ep++ = *xMp++;
+        }
+        while (--i);
+    }
+    /*endswitch*/
+    while (++Mc < 4)
+        *ep++ = 0;
+    /*endwhile*/
+}
+/*- End of function --------------------------------------------------------*/
+
+void gsm0610_rpe_encoding(gsm0610_state_t *s,
+                          int16_t *e,          // [-5..-1][0..39][40..44]
+                          int16_t *xmaxc,
+                          int16_t *Mc,
+                          int16_t xMc[13])
+{
+    int16_t x[40];
+    int16_t xM[13];
+    int16_t xMp[13];
+    int16_t mant;
+    int16_t exp;
+
+    weighting_filter(e, x);
+    rpe_grid_selection(x, xM, Mc);
+
+    apcm_quantization(xM, xMc, &mant, &exp, xmaxc);
+    apcm_inverse_quantization(xMc, mant, exp, xMp);
+
+    rpe_grid_positioning(*Mc, xMp, e);
+}
+/*- End of function --------------------------------------------------------*/
+
+void gsm0610_rpe_decoding(gsm0610_state_t *s,
+                          int16_t xmaxc,
+                          int16_t Mcr,
+                          int16_t xMcr[13],
+                          int16_t erp[40])
+{
+    int16_t exp;
+    int16_t mant;
+    int16_t xMp[13];
+
+    apcm_quantization_xmaxc_to_exp_mant(xmaxc, &exp, &mant);
+    apcm_inverse_quantization(xMcr, mant, exp, xMp);
+    rpe_grid_positioning(Mcr, xMp, erp);
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/gsm0610_short_term.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/gsm0610_short_term.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,362 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * gsm0610_short_term.c - GSM 06.10 full rate speech codec.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2006 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * This code is based on the widely used GSM 06.10 code available from
+ * http://kbs.cs.tu-berlin.de/~jutta/toast.html
+ *
+ * $Id: gsm0610_short_term.c,v 1.15 2008/07/02 14:48:25 steveu Exp $
+ */
+
+/*! \file */
+
+#if defined(HAVE_CONFIG_H)
+#include <config.h>
+#endif
+
+#include <assert.h>
+#include <inttypes.h>
+#include "floating_fudge.h"
+#if defined(HAVE_TGMATH_H)
+#include <tgmath.h>
+#endif
+#if defined(HAVE_MATH_H)
+#include <math.h>
+#endif
+#include <stdlib.h>
+
+#include "spandsp/telephony.h"
+#include "spandsp/bitstream.h"
+#include "spandsp/dc_restore.h"
+#include "spandsp/gsm0610.h"
+
+#include "gsm0610_local.h"
+
+/* SHORT TERM ANALYSIS FILTERING SECTION */
+
+/* 4.2.8 */
+static void decode_log_area_ratios(int16_t LARc[8], int16_t *LARpp)
+{
+    int16_t temp1;
+
+    /* This procedure requires for efficient implementation
+       two tables.
+       INVA[1..8] = integer((32768*8)/real_A[1..8])
+       MIC[1..8]  = minimum value of the LARc[1..8]
+    */
+
+    /* Compute the LARpp[1..8] */
+
+#undef STEP
+#define STEP(B,MIC,INVA)                        \
+    temp1 = gsm_add(*LARc++, MIC) << 10;       \
+    temp1 = gsm_sub(temp1, B << 1);            \
+    temp1 = gsm_mult_r (INVA, temp1);           \
+    *LARpp++ = gsm_add(temp1, temp1);
+
+    STEP(    0,  -32,  13107);
+    STEP(    0,  -32,  13107);
+    STEP( 2048,  -16,  13107);
+    STEP(-2560,  -16,  13107);
+
+    STEP(   94,   -8,  19223);
+    STEP(-1792,   -8,  17476);
+    STEP( -341,   -4,  31454);
+    STEP(-1144,   -4,  29708);
+
+    /* NOTE: the addition of *MIC is used to restore the sign of *LARc. */
+}
+/*- End of function --------------------------------------------------------*/
+
+/* 4.2.9 */
+
+/* Computation of the quantized reflection coefficients */
+
+/* 4.2.9.1  Interpolation of the LARpp[1..8] to get the LARp[1..8] */
+
+/* Within each frame of 160 analyzed speech samples the short term
+   analysis and synthesis filters operate with four different sets of
+   coefficients, derived from the previous set of decoded LARs(LARpp(j - 1))
+   and the actual set of decoded LARs (LARpp(j))
+  
+   (Initial value: LARpp(j - 1)[1..8] = 0.)
+*/
+
+static void coefficients_0_12(int16_t *LARpp_j_1,
+                              int16_t *LARpp_j,
+                              int16_t *LARp)
+{
+    int i;
+
+    for (i = 1;  i <= 8;  i++, LARp++, LARpp_j_1++, LARpp_j++)
+    {
+        *LARp = gsm_add(*LARpp_j_1 >> 2, *LARpp_j >> 2);
+        *LARp = gsm_add(*LARp, *LARpp_j_1 >> 1);
+    }
+    /*endfor*/
+}
+/*- End of function --------------------------------------------------------*/
+
+static void coefficients_13_26(int16_t *LARpp_j_1,
+                               int16_t *LARpp_j,
+                               int16_t *LARp)
+{
+    int i;
+
+    for (i = 1;  i <= 8;  i++, LARpp_j_1++, LARpp_j++, LARp++)
+        *LARp = gsm_add(*LARpp_j_1 >> 1, *LARpp_j >> 1);
+    /*endfor*/
+}
+/*- End of function --------------------------------------------------------*/
+
+static void coefficients_27_39(int16_t *LARpp_j_1,
+                               int16_t *LARpp_j,
+                               int16_t *LARp)
+{
+    int i;
+
+    for (i = 1;  i <= 8;  i++, LARpp_j_1++, LARpp_j++, LARp++)
+    {
+        *LARp = gsm_add(*LARpp_j_1 >> 2, *LARpp_j >> 2);
+        *LARp = gsm_add(*LARp, *LARpp_j >> 1);
+    }
+    /*endfor*/
+}
+/*- End of function --------------------------------------------------------*/
+
+static void coefficients_40_159(int16_t *LARpp_j, int16_t *LARp)
+{
+    int i;
+
+    for (i = 1;  i <= 8;  i++)
+        *LARp++ = *LARpp_j++;
+    /*endfor*/
+}
+/*- End of function --------------------------------------------------------*/
+
+/* 4.2.9.2 */
+static void larp_to_rp(int16_t LARp[8])
+{
+    int i;
+    int16_t *LARpx;
+    int16_t temp;
+
+    /* The input to this procedure is the interpolated LARp[0..7] array.
+       The reflection coefficients, rp[i], are used in the analysis
+       filter and in the synthesis filter.
+    */
+
+    LARpx = LARp;
+    for (i = 1;  i <= 8;  i++, LARpx++)
+    {
+        temp = *LARpx;
+        if (temp < 0)
+        {
+            if (temp == INT16_MIN)
+                temp = INT16_MAX;
+            else
+                temp = -temp;
+            /*endif*/
+            if (temp < 11059)
+                temp <<= 1;
+            else if (temp < 20070)
+                temp += 11059;
+            else
+                temp = gsm_add(temp >> 2, 26112);
+            /*endif*/
+            *LARpx = -temp;
+        }
+        else
+        {
+            if (temp < 11059)
+                temp <<= 1;
+            else if (temp < 20070)
+                temp += 11059;
+            else
+                temp = gsm_add(temp >> 2, 26112);
+            /*endif*/
+            *LARpx = temp;
+        }
+        /*endif*/
+    }
+    /*endfor*/
+}
+/*- End of function --------------------------------------------------------*/
+
+/* 4.2.10 */
+static void short_term_analysis_filtering(gsm0610_state_t *s,
+                                          int16_t rp[8],
+                                          int k_n,          // k_end - k_start
+                                          int16_t amp[])    // [0..n-1]     IN/OUT
+{
+    /* This procedure computes the short term residual signal d[..] to be fed
+       to the RPE-LTP loop from the s[..] signal and from the local rp[..]
+       array (quantized reflection coefficients).  As the call of this
+       procedure can be done in many ways (see the interpolation of the LAR
+       coefficient), it is assumed that the computation begins with index
+       k_start (for arrays d[..] and s[..]) and stops with index k_end
+       (k_start and k_end are defined in 4.2.9.1).  This procedure also
+       needs to keep the array u[0..7] in memory for each call.
+    */
+    int16_t *u0;
+    int16_t *u_top;
+    int i;
+    int16_t *u;
+    int16_t *rpx;
+    int32_t di;
+    int32_t u_out;
+
+    u0 = s->u;
+    u_top = u0 + 8;
+
+    for (i = 0;  i < k_n;  i++)
+    {
+        di =
+        u_out = amp[i];
+        for (rpx = rp, u = u0;  u < u_top;  )
+        {
+            int32_t ui;
+            int32_t rpi;
+
+            ui = *u;
+            *u++ = (int16_t) u_out;
+            rpi = *rpx++;
+            u_out = ui + (((rpi*di) + 0x4000) >> 15);
+            di = di + (((rpi*ui) + 0x4000) >> 15);
+            u_out = saturate(u_out);
+            di = saturate(di);
+        }
+        /*endfor*/
+        amp[i] = (int16_t) di;
+    }
+    /*endfor*/
+}
+/*- End of function --------------------------------------------------------*/
+
+static void short_term_synthesis_filtering(gsm0610_state_t *s,
+                                           int16_t rrp[8],
+                                           int k,          // k_end - k_start
+                                           int16_t *wt,    // [0..k - 1]
+                                           int16_t *sr)    // [0..k - 1]
+{
+    int16_t *v;
+    int i;
+    int16_t sri;
+    int16_t tmp1;
+    int16_t tmp2;
+
+    v = s->v;
+    while (k--)
+    {
+        sri = *wt++;
+        for (i = 8;  i--;  )
+        {
+            tmp1 = rrp[i];
+            tmp2 = v[i];
+            tmp2 = ((tmp1 == INT16_MIN  &&  tmp2 == INT16_MIN)
+                   ?
+                   INT16_MAX
+                   :
+                   (int16_t) (((int32_t) tmp1*(int32_t) tmp2 + 16384) >> 15) & 0xFFFF);
+
+            sri = gsm_sub(sri, tmp2);
+
+            tmp1 = ((tmp1 == INT16_MIN  &&  sri == INT16_MIN)
+                    ?
+                    INT16_MAX
+                    :
+                    (int16_t) (((int32_t) tmp1*(int32_t) sri + 16384) >> 15) & 0xFFFF);
+
+            v[i + 1] = gsm_add(v[i], tmp1);
+        }
+        /*endfor*/
+        *sr++ =
+        v[0] = sri;
+    }
+    /*endwhile*/
+}
+/*- End of function --------------------------------------------------------*/
+
+void gsm0610_short_term_analysis_filter(gsm0610_state_t *s,
+                                        int16_t LARc[8],
+                                        int16_t amp[GSM0610_FRAME_LEN])
+{
+    int16_t *LARpp_j;
+    int16_t *LARpp_j_1;
+    int16_t LARp[8];
+
+    LARpp_j = s->LARpp[s->j];
+    LARpp_j_1 = s->LARpp[s->j ^= 1];
+
+    decode_log_area_ratios(LARc, LARpp_j);
+
+    coefficients_0_12(LARpp_j_1, LARpp_j, LARp);
+    larp_to_rp(LARp);
+    short_term_analysis_filtering(s, LARp, 13, amp);
+
+    coefficients_13_26(LARpp_j_1, LARpp_j, LARp);
+    larp_to_rp(LARp);
+    short_term_analysis_filtering(s, LARp, 14, amp + 13);
+
+    coefficients_27_39(LARpp_j_1, LARpp_j, LARp);
+    larp_to_rp(LARp);
+    short_term_analysis_filtering(s, LARp, 13, amp + 27);
+
+    coefficients_40_159(LARpp_j, LARp);
+    larp_to_rp(LARp);
+    short_term_analysis_filtering(s, LARp, 120, amp + 40);
+}
+/*- End of function --------------------------------------------------------*/
+
+void gsm0610_short_term_synthesis_filter(gsm0610_state_t *s,
+                                         int16_t LARcr[8],
+                                         int16_t wt[GSM0610_FRAME_LEN],
+                                         int16_t amp[GSM0610_FRAME_LEN])
+{
+    int16_t *LARpp_j;
+    int16_t *LARpp_j_1;
+    int16_t LARp[8];
+
+    LARpp_j = s->LARpp[s->j];
+    LARpp_j_1 = s->LARpp[s->j ^= 1];
+
+    decode_log_area_ratios(LARcr, LARpp_j);
+
+    coefficients_0_12(LARpp_j_1, LARpp_j, LARp);
+    larp_to_rp(LARp);
+    short_term_synthesis_filtering(s, LARp, 13, wt, amp);
+
+    coefficients_13_26(LARpp_j_1, LARpp_j, LARp);
+    larp_to_rp(LARp);
+    short_term_synthesis_filtering(s, LARp, 14, wt + 13, amp + 13);
+
+    coefficients_27_39(LARpp_j_1, LARpp_j, LARp);
+    larp_to_rp(LARp);
+    short_term_synthesis_filtering(s, LARp, 13, wt + 27, amp + 27);
+
+    coefficients_40_159(LARpp_j, LARp);
+    larp_to_rp(LARp);
+    short_term_synthesis_filtering(s, LARp, 120, wt + 40, amp + 40);
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/hdlc.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/hdlc.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,571 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * hdlc.c
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2003 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: hdlc.c,v 1.60 2008/05/13 13:17:22 steveu Exp $
+ */
+
+/*! \file */
+
+#if defined(HAVE_CONFIG_H)
+#include <config.h>
+#endif
+
+#include <stdlib.h>
+#include <inttypes.h>
+#include <string.h>
+#include <stdio.h>
+
+#include "spandsp/telephony.h"
+#include "spandsp/async.h"
+#include "spandsp/crc.h"
+#include "spandsp/bit_operations.h"
+#include "spandsp/hdlc.h"
+
+static void rx_special_condition(hdlc_rx_state_t *s, int condition)
+{
+    /* Special conditions */
+    switch (condition)
+    {
+    case PUTBIT_CARRIER_UP:
+    case PUTBIT_TRAINING_SUCCEEDED:
+        /* Reset the HDLC receiver. */
+        s->raw_bit_stream = 0;
+        s->len = 0;
+        s->num_bits = 0;
+        s->flags_seen = 0;
+        s->framing_ok_announced = FALSE;
+        /* Fall through */
+    case PUTBIT_TRAINING_IN_PROGRESS:
+    case PUTBIT_TRAINING_FAILED:
+    case PUTBIT_CARRIER_DOWN:
+    case PUTBIT_END_OF_DATA:
+        s->frame_handler(s->user_data, NULL, condition, TRUE);
+        break;
+    default:
+        //printf("Eh!\n");
+        break;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ void octet_set_and_count(hdlc_rx_state_t *s)
+{
+    if (s->octet_count_report_interval == 0)
+        return;
+        
+    /* If we are not in octet counting mode, we start it.
+       If we are in octet counting mode, we update it. */
+    if (s->octet_counting_mode)
+    {
+        if (--s->octet_count <= 0)
+        {
+            s->octet_count = s->octet_count_report_interval;
+            s->frame_handler(s->user_data, NULL, PUTBIT_OCTET_REPORT, TRUE);
+        }
+    }
+    else
+    {
+        s->octet_counting_mode = TRUE;
+        s->octet_count = s->octet_count_report_interval;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ void octet_count(hdlc_rx_state_t *s)
+{
+    if (s->octet_count_report_interval == 0)
+        return;
+
+    /* If we are not in octet counting mode, we start it.
+       If we are in octet counting mode, we update it. */
+    if (s->octet_counting_mode)
+    {
+        if (--s->octet_count <= 0)
+        {
+            s->octet_count = s->octet_count_report_interval;
+            s->frame_handler(s->user_data, NULL, PUTBIT_OCTET_REPORT, TRUE);
+        }
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static void rx_flag_or_abort(hdlc_rx_state_t *s)
+{
+    if ((s->raw_bit_stream & 0x8000))
+    {
+        /* Hit HDLC abort */
+        s->rx_aborts++;
+        s->frame_handler(s->user_data, NULL, PUTBIT_ABORT, TRUE);
+        /* If we have not yet seen enough flags, restart the count. If we
+           are beyond that point, just back off one step, so we need to see
+           another flag before proceeding to collect frame octets. */
+        if (s->flags_seen < s->framing_ok_threshold)
+            s->flags_seen = 0;
+        else
+            s->flags_seen = s->framing_ok_threshold - 1;
+        /* An abort starts octet counting */
+        octet_set_and_count(s);
+    }
+    else
+    {
+        /* Hit HDLC flag */
+        /* A flag clears octet counting */
+        s->octet_counting_mode = FALSE;
+        if (s->flags_seen >= s->framing_ok_threshold)
+        {
+            /* We may have a frame, or we may have back to back flags */
+            if (s->len)
+            {
+                if (s->num_bits == 7  &&  s->len >= s->crc_bytes  &&  s->len <= s->max_frame_len)
+                {
+                    if ((s->crc_bytes == 2  &&  crc_itu16_check(s->buffer, s->len))
+                        ||
+                        (s->crc_bytes != 2  &&  crc_itu32_check(s->buffer, s->len)))
+                    {
+                        s->rx_frames++;
+                        s->rx_bytes += s->len - s->crc_bytes;
+                        s->len -= s->crc_bytes;
+                        s->frame_handler(s->user_data, s->buffer, s->len, TRUE);
+                    }
+                    else
+                    {
+                        s->rx_crc_errors++;
+                        if (s->report_bad_frames)
+                        {
+                            s->len -= s->crc_bytes;
+                            s->frame_handler(s->user_data, s->buffer, s->len, FALSE);
+                        }
+                    }
+                }
+                else
+                {
+                    /* Frame too short or too long, or the flag is misaligned with its octets. */
+                    if (s->report_bad_frames)
+                    {
+                        /* Don't let the length go below zero, or it will be confused
+                           with one of the special conditions. */
+                        if (s->len >= s->crc_bytes)
+                            s->len -= s->crc_bytes;
+                        else
+                            s->len = 0;
+                        s->frame_handler(s->user_data, s->buffer, s->len, FALSE);
+                    }
+                    s->rx_length_errors++;
+                }
+            }
+        }
+        else
+        {
+            /* Check the flags are back-to-back when testing for valid preamble. This
+               greatly reduces the chances of false preamble detection, and anything
+               which doesn't send them back-to-back is badly broken. */
+            if (s->num_bits != 7)
+                s->flags_seen = 0;
+            if (++s->flags_seen >= s->framing_ok_threshold  &&  !s->framing_ok_announced)
+            {
+                s->frame_handler(s->user_data, NULL, PUTBIT_FRAMING_OK, TRUE);
+                s->framing_ok_announced = TRUE;
+            }
+        }
+    }
+    s->len = 0;
+    s->num_bits = 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ void hdlc_rx_put_bit_core(hdlc_rx_state_t *s)
+{
+    if ((s->raw_bit_stream & 0x3F00) == 0x3E00)
+    {
+        /* Its time to either skip a bit, for stuffing, or process a
+           flag or abort */
+        if ((s->raw_bit_stream & 0x4000))
+            rx_flag_or_abort(s);
+        return;
+    }
+    s->num_bits++;
+    if (s->flags_seen < s->framing_ok_threshold)
+    {
+        if ((s->num_bits & 0x7) == 0)
+            octet_count(s);
+        return;
+    }
+    s->byte_in_progress = (s->byte_in_progress | (s->raw_bit_stream & 0x100)) >> 1;
+    if (s->num_bits == 8)
+    {
+        /* Ensure we do not accept an overlength frame, and especially that
+           we do not overflow our buffer */
+        if (s->len < s->max_frame_len)
+        {
+            s->buffer[s->len++] = (uint8_t) s->byte_in_progress;
+        }
+        else
+        {
+            /* This is too long. Abandon the frame, and wait for the next
+               flag octet. */
+            s->len = sizeof(s->buffer) + 1;
+            s->flags_seen = s->framing_ok_threshold - 1;
+            octet_set_and_count(s);
+        }
+        s->num_bits = 0;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+void hdlc_rx_put_bit(hdlc_rx_state_t *s, int new_bit)
+{
+    if (new_bit < 0)
+    {
+        rx_special_condition(s, new_bit);
+        return;
+    }
+    s->raw_bit_stream = (s->raw_bit_stream << 1) | ((new_bit << 8) & 0x100);
+    hdlc_rx_put_bit_core(s);
+}
+/*- End of function --------------------------------------------------------*/
+
+void hdlc_rx_put_byte(hdlc_rx_state_t *s, int new_byte)
+{
+    int i;
+
+    if (new_byte < 0)
+    {
+        rx_special_condition(s, new_byte);
+        return;
+    }
+    s->raw_bit_stream |= new_byte;
+    for (i = 0;  i < 8;  i++)
+    {
+        s->raw_bit_stream <<= 1;
+        hdlc_rx_put_bit_core(s);
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+void hdlc_rx_put(hdlc_rx_state_t *s, const uint8_t buf[], int len)
+{
+    int i;
+
+    for (i = 0;  i < len;  i++)
+        hdlc_rx_put_byte(s, buf[i]);
+}
+/*- End of function --------------------------------------------------------*/
+
+void hdlc_rx_set_max_frame_len(hdlc_rx_state_t *s, size_t max_len)
+{
+    max_len += s->crc_bytes;
+    s->max_frame_len = (max_len <= sizeof(s->buffer))  ?  max_len  :  sizeof(s->buffer);
+}
+/*- End of function --------------------------------------------------------*/
+
+void hdlc_rx_set_octet_counting_report_interval(hdlc_rx_state_t *s, int interval)
+{
+    s->octet_count_report_interval = interval;
+}
+/*- End of function --------------------------------------------------------*/
+
+hdlc_rx_state_t *hdlc_rx_init(hdlc_rx_state_t *s,
+                              int crc32,
+                              int report_bad_frames,
+                              int framing_ok_threshold,
+                              hdlc_frame_handler_t handler,
+                              void *user_data)
+{
+    if (s == NULL)
+    {
+        if ((s = (hdlc_rx_state_t *) malloc(sizeof(*s))) == NULL)
+            return NULL;
+    }
+    memset(s, 0, sizeof(*s));
+    s->frame_handler = handler;
+    s->user_data = user_data;
+    s->crc_bytes = (crc32)  ?  4  :  2;
+    s->report_bad_frames = report_bad_frames;
+    s->framing_ok_threshold = (framing_ok_threshold < 1)  ?  1  :  framing_ok_threshold;
+    s->max_frame_len = sizeof(s->buffer);
+    return s;
+}
+/*- End of function --------------------------------------------------------*/
+
+int hdlc_rx_get_stats(hdlc_rx_state_t *s,
+                      hdlc_rx_stats_t *t)
+{
+    t->bytes = s->rx_bytes;
+    t->good_frames = s->rx_frames;
+    t->crc_errors = s->rx_crc_errors;
+    t->length_errors = s->rx_length_errors;
+    t->aborts = s->rx_aborts;
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int hdlc_tx_frame(hdlc_tx_state_t *s, const uint8_t *frame, size_t len)
+{
+    if (len <= 0)
+    {
+        s->tx_end = TRUE;
+        return 0;
+    }
+    if (s->len + len > s->max_frame_len)
+        return -1;
+    if (s->progressive)
+    {
+        /* Only lock out if we are in the CRC section. */
+        if (s->pos >= HDLC_MAXFRAME_LEN)
+            return -1;
+    }
+    else
+    {
+        /* Lock out if there is anything in the buffer. */
+        if (s->len)
+            return -1;
+    }
+    memcpy(s->buffer + s->len, frame, len);
+    if (s->crc_bytes == 2)
+        s->crc = crc_itu16_calc(frame, len, (uint16_t) s->crc);
+    else
+        s->crc = crc_itu32_calc(frame, len, s->crc);
+    if (s->progressive)
+        s->len += len;
+    else
+        s->len = len;
+    s->tx_end = FALSE;
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int hdlc_tx_flags(hdlc_tx_state_t *s, int len)
+{
+    /* Some HDLC applications require the ability to force a period of HDLC
+       flag words. */
+    if (s->pos)
+        return -1;
+    if (len < 0)
+        s->flag_octets += -len;
+    else
+        s->flag_octets = len;
+    s->report_flag_underflow = TRUE;
+    s->tx_end = FALSE;
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int hdlc_tx_abort(hdlc_tx_state_t *s)
+{
+    /* TODO: This is a really crude way of just fudging an abort out for simple
+             test purposes. */
+    s->flag_octets++;
+    s->abort_octets++;
+    return -1;
+}
+/*- End of function --------------------------------------------------------*/
+
+int hdlc_tx_corrupt_frame(hdlc_tx_state_t *s)
+{
+    if (s->len <= 0)
+        return -1;
+    s->crc ^= 0xFFFF;
+    s->buffer[HDLC_MAXFRAME_LEN] ^= 0xFF;
+    s->buffer[HDLC_MAXFRAME_LEN + 1] ^= 0xFF;
+    s->buffer[HDLC_MAXFRAME_LEN + 2] ^= 0xFF;
+    s->buffer[HDLC_MAXFRAME_LEN + 3] ^= 0xFF;
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int hdlc_tx_get_byte(hdlc_tx_state_t *s)
+{
+    int i;
+    int byte_in_progress;
+    int txbyte;
+
+    if (s->flag_octets > 0)
+    {
+        /* We are in a timed flag section (preamble, inter frame gap, etc.) */
+        if (--s->flag_octets <= 0  &&  s->report_flag_underflow)
+        {
+            s->report_flag_underflow = FALSE;
+            if (s->len == 0)
+            {
+                /* The timed flags have finished, there is nothing else queued to go,
+                   and we have been told to report this underflow. */
+                if (s->underflow_handler)
+                    s->underflow_handler(s->user_data);
+            }
+        }
+        if (s->abort_octets)
+        {
+            s->abort_octets = 0;
+            return 0x7F;
+        }   
+        return s->idle_octet;
+    }
+    if (s->len)
+    {
+        if (s->num_bits >= 8)
+        {
+            s->num_bits -= 8;
+            return (s->octets_in_progress >> s->num_bits) & 0xFF;
+        }
+        if (s->pos >= s->len)
+        {
+            if (s->pos == s->len)
+            {
+                s->crc ^= 0xFFFFFFFF;
+                s->buffer[HDLC_MAXFRAME_LEN] = (uint8_t) s->crc;
+                s->buffer[HDLC_MAXFRAME_LEN + 1] = (uint8_t) (s->crc >> 8);
+                if (s->crc_bytes == 4)
+                {
+                    s->buffer[HDLC_MAXFRAME_LEN + 2] = (uint8_t) (s->crc >> 16);
+                    s->buffer[HDLC_MAXFRAME_LEN + 3] = (uint8_t) (s->crc >> 24);
+                }
+                s->pos = HDLC_MAXFRAME_LEN;
+            }
+            else if (s->pos == HDLC_MAXFRAME_LEN + s->crc_bytes)
+            {
+                /* Finish off the current byte with some flag bits. If we are at the
+                   start of a byte we need a at least one whole byte of flag to ensure
+                   we cannot end up with back to back frames, and no flag octet at all */
+                txbyte = (uint8_t) ((s->octets_in_progress << (8 - s->num_bits)) | (0x7E >> s->num_bits));
+                /* Create a rotated octet of flag for idling... */
+                s->idle_octet = (0x7E7E >> s->num_bits) & 0xFF;
+                /* ...and the partial flag octet needed to start off the next message. */
+                s->octets_in_progress = s->idle_octet >> (8 - s->num_bits);
+                s->flag_octets = s->inter_frame_flags - 1;
+                s->len = 0;
+                s->pos = 0;
+                if (s->crc_bytes == 2)
+                    s->crc = 0xFFFF;
+                else
+                    s->crc = 0xFFFFFFFF;
+                /* Report the underflow now. If there are timed flags still in progress, loading the
+                   next frame right now will be harmless. */
+                s->report_flag_underflow = FALSE;
+                if (s->underflow_handler)
+                    s->underflow_handler(s->user_data);
+                /* Make sure we finish off with at least one flag octet, if the underflow report did not result
+                   in a new frame being sent. */
+                if (s->len == 0  &&  s->flag_octets < 2)
+                    s->flag_octets = 2;
+                return txbyte;
+            }
+        }
+        byte_in_progress = s->buffer[s->pos++];
+        i = bottom_bit(byte_in_progress | 0x100);
+        s->octets_in_progress <<= i;
+        byte_in_progress >>= i;
+        for (  ;  i < 8;  i++)
+        {
+            s->octets_in_progress = (s->octets_in_progress << 1) | (byte_in_progress & 0x01);
+            byte_in_progress >>= 1;
+            if ((s->octets_in_progress & 0x1F) == 0x1F)
+            {
+                /* There are 5 ones - stuff */
+                s->octets_in_progress <<= 1;
+                s->num_bits++;
+            }
+        }
+        /* An input byte will generate between 8 and 10 output bits */
+        return (s->octets_in_progress >> s->num_bits) & 0xFF;
+    }
+    /* Untimed idling on flags */
+    if (s->tx_end)
+    {
+        s->tx_end = FALSE;
+        return PUTBIT_END_OF_DATA;
+    }
+    return s->idle_octet;
+}
+/*- End of function --------------------------------------------------------*/
+
+int hdlc_tx_get_bit(hdlc_tx_state_t *s)
+{
+    int txbit;
+
+    if (s->bits == 0)
+    {
+        if ((s->byte = hdlc_tx_get_byte(s)) < 0)
+            return s->byte;
+        s->bits = 8;
+    }
+    s->bits--;
+    txbit = (s->byte >> s->bits) & 0x01;
+    return  txbit;
+}
+/*- End of function --------------------------------------------------------*/
+
+int hdlc_tx_get(hdlc_tx_state_t *s, uint8_t buf[], size_t max_len)
+{
+    int i;
+    int x;
+
+    for (i = 0;  i < max_len;  i++)
+    {
+        if ((x = hdlc_tx_get_byte(s)) == PUTBIT_END_OF_DATA)
+            return i;
+        buf[i] = x;
+    }
+    return i;
+}
+/*- End of function --------------------------------------------------------*/
+
+void hdlc_tx_set_max_frame_len(hdlc_tx_state_t *s, size_t max_len)
+{
+    s->max_frame_len = (max_len <= HDLC_MAXFRAME_LEN)  ?  max_len  :  HDLC_MAXFRAME_LEN;
+}
+/*- End of function --------------------------------------------------------*/
+
+hdlc_tx_state_t *hdlc_tx_init(hdlc_tx_state_t *s,
+                              int crc32,
+                              int inter_frame_flags,
+                              int progressive,
+                              hdlc_underflow_handler_t handler,
+                              void *user_data)
+{
+    if (s == NULL)
+    {
+        if ((s = (hdlc_tx_state_t *) malloc(sizeof(*s))) == NULL)
+            return NULL;
+    }
+    memset(s, 0, sizeof(*s));
+    s->idle_octet = 0x7E;
+    s->underflow_handler = handler;
+    s->user_data = user_data;
+    s->inter_frame_flags = (inter_frame_flags < 1)  ?  1  :  inter_frame_flags;
+    if (crc32)
+    {
+        s->crc_bytes = 4;
+        s->crc = 0xFFFFFFFF;
+    }
+    else
+    {
+        s->crc_bytes = 2;
+        s->crc = 0xFFFF;
+    }
+    s->progressive = progressive;
+    s->max_frame_len = HDLC_MAXFRAME_LEN;
+    return s;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/ima_adpcm.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/ima_adpcm.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,497 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * ima_adpcm.c - Conversion routines between linear 16 bit PCM data and
+ *               IMA/DVI/Intel ADPCM format.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2001, 2004 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: ima_adpcm.c,v 1.28 2008/07/02 14:48:25 steveu Exp $
+ */
+
+/*! \file */
+
+#if defined(HAVE_CONFIG_H)
+#include <config.h>
+#endif
+
+#include <stdlib.h>
+#include <inttypes.h>
+#include <string.h>
+#include "floating_fudge.h"
+#if defined(HAVE_TGMATH_H)
+#include <tgmath.h>
+#endif
+#if defined(HAVE_MATH_H)
+#include <math.h>
+#endif
+
+#include "spandsp/telephony.h"
+#include "spandsp/dc_restore.h"
+#include "spandsp/ima_adpcm.h"
+
+/*
+ * Intel/DVI ADPCM coder/decoder.
+ *
+ * The algorithm for this coder was taken from the IMA Compatability Project
+ * proceedings, Vol 2, Number 2; May 1992.
+ *
+ * The RTP payload specs. reference a variant of DVI, called VDVI. This attempts to
+ * further compress, in a variable bit rate manner, by expressing the 4 bit codes
+ * from the DVI codec as:
+ *
+ *  0 00
+ *  1 010
+ *  2 1100
+ *  3 11100
+ *  4 111100
+ *  5 1111100
+ *  6 11111100
+ *  7 11111110
+ *  8 10
+ *  9 011
+ * 10 1101
+ * 11 11101
+ * 12 111101
+ * 13 1111101
+ * 14 11111101
+ * 15 11111111
+ *
+ * Any left over bits in the last octet of an encoded burst are set to one.
+ */
+
+/*
+   DVI4 uses an adaptive delta pulse code modulation (ADPCM) encoding
+   scheme that was specified by the Interactive Multimedia Association
+   (IMA) as the "IMA ADPCM wave type".  However, the encoding defined
+   here as DVI4 differs in three respects from the IMA specification:
+
+   o  The RTP DVI4 header contains the predicted value rather than the
+      first sample value contained the IMA ADPCM block header.
+
+   o  IMA ADPCM blocks contain an odd number of samples, since the first
+      sample of a block is contained just in the header (uncompressed),
+      followed by an even number of compressed samples.  DVI4 has an
+      even number of compressed samples only, using the `predict' word
+      from the header to decode the first sample.
+
+   o  For DVI4, the 4-bit samples are packed with the first sample in
+      the four most significant bits and the second sample in the four
+      least significant bits.  In the IMA ADPCM codec, the samples are
+      packed in the opposite order.
+
+   Each packet contains a single DVI block.  This profile only defines
+   the 4-bit-per-sample version, while IMA also specified a 3-bit-per-
+   sample encoding.
+
+   The "header" word for each channel has the following structure:
+
+      int16  predict;  // predicted value of first sample
+                       // from the previous block (L16 format)
+      u_int8 index;    // current index into stepsize table
+      u_int8 reserved; // set to zero by sender, ignored by receiver
+
+   Each octet following the header contains two 4-bit samples, thus the
+   number of samples per packet MUST be even because there is no means
+   to indicate a partially filled last octet.
+*/
+
+#define STEP_MAX 88
+
+/* Intel ADPCM step variation table */
+static const int step_size[STEP_MAX + 1] =
+{
+        7,     8,     9,   10,     11,    12,    13,    14,    16,    17,
+       19,    21,    23,   25,     28,    31,    34,    37,    41,    45,
+       50,    55,    60,   66,     73,    80,    88,    97,   107,   118,
+      130,   143,   157,   173,   190,   209,   230,   253,   279,   307,
+      337,   371,   408,   449,   494,   544,   598,   658,   724,   796,
+      876,   963,  1060,  1166,  1282,  1411,  1552,  1707,  1878,  2066,
+     2272,  2499,  2749,  3024,  3327,  3660,  4026,  4428,  4871,  5358,
+     5894,  6484,  7132,  7845,  8630,  9493, 10442, 11487, 12635, 13899,
+    15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
+};
+
+static const int step_adjustment[8] =
+{
+    -1, -1, -1, -1, 2, 4, 6, 8
+};
+
+static const struct
+{
+    uint8_t code;
+    uint8_t bits;
+} vdvi_encode[] =
+{
+    {0x00,       2},
+    {0x02,       3},
+    {0x0C,       4},
+    {0x1C,       5},
+    {0x3C,       6},
+    {0x7C,       7},
+    {0xFC,       8},
+    {0xFE,       8},
+    {0x02,       2},
+    {0x03,       3},
+    {0x0D,       4},
+    {0x1D,       5},
+    {0x3D,       6},
+    {0x7D,       7},
+    {0xFD,       8},
+    {0xFF,       8}
+};
+
+static const struct
+{
+    uint16_t code;
+    uint16_t mask;
+    uint8_t bits;
+} vdvi_decode[] =
+{
+    {0x0000,    0xC000,     2},
+    {0x4000,    0xE000,     3},
+    {0xC000,    0xF000,     4},
+    {0xE000,    0xF800,     5},
+    {0xF000,    0xFC00,     6},
+    {0xF800,    0xFE00,     7},
+    {0xFC00,    0xFF00,     8},
+    {0xFE00,    0xFF00,     8},
+    {0x8000,    0xC000,     2},
+    {0x6000,    0xE000,     3},
+    {0xD000,    0xF000,     4},
+    {0xE800,    0xF800,     5},
+    {0xF400,    0xFC00,     6},
+    {0xFA00,    0xFE00,     7},
+    {0xFD00,    0xFF00,     8},
+    {0xFF00,    0xFF00,     8}
+};
+
+static int16_t decode(ima_adpcm_state_t *s, uint8_t adpcm)
+{
+    int e;
+    int ss;
+    int16_t linear;
+
+    /* e = (adpcm+0.5)*step/4 */
+    ss = step_size[s->step_index];
+    e = ss >> 3;
+    if (adpcm & 0x01)
+        e += (ss >> 2);
+    /*endif*/
+    if (adpcm & 0x02)
+        e += (ss >> 1);
+    /*endif*/
+    if (adpcm & 0x04)
+        e += ss;
+    /*endif*/
+    if (adpcm & 0x08)
+        e = -e;
+    /*endif*/
+    linear = saturate(s->last + e);
+    s->last = linear;
+    s->step_index += step_adjustment[adpcm & 0x07];
+    if (s->step_index < 0)
+        s->step_index = 0;
+    else if (s->step_index > STEP_MAX)
+        s->step_index = STEP_MAX;
+    /*endif*/
+    return linear;
+}
+/*- End of function --------------------------------------------------------*/
+
+static uint8_t encode(ima_adpcm_state_t *s, int16_t linear)
+{
+    int e;
+    int ss;
+    int adpcm;
+    int diff;
+    int initial_e;
+
+    ss = step_size[s->step_index];
+    initial_e =
+    e = linear - s->last;
+    diff = ss >> 3;
+    adpcm = (uint8_t) 0x00;
+    if (e < 0)
+    {
+        adpcm = (uint8_t) 0x08;
+        e = -e;
+    }
+    /*endif*/
+    if (e >= ss)
+    {
+        adpcm |= (uint8_t) 0x04;
+        e -= ss;
+    }
+    /*endif*/
+    ss >>= 1;
+    if (e >= ss)
+    {
+        adpcm |= (uint8_t) 0x02;
+        e -= ss;
+    }
+    /*endif*/
+    ss >>= 1;
+    if (e >= ss)
+    {
+        adpcm |= (uint8_t) 0x01;
+        e -= ss;
+    }
+    /*endif*/
+
+    if (initial_e < 0)
+        diff = -(diff - initial_e - e);
+    else
+        diff = diff + initial_e - e;
+    /*endif*/
+    s->last = saturate(diff + s->last);
+    s->step_index += step_adjustment[adpcm & 0x07];
+    if (s->step_index < 0)
+        s->step_index = 0;
+    else if (s->step_index > STEP_MAX)
+        s->step_index = STEP_MAX;
+    /*endif*/
+    return (uint8_t) adpcm;
+}
+/*- End of function --------------------------------------------------------*/
+
+ima_adpcm_state_t *ima_adpcm_init(ima_adpcm_state_t *s, int variant, int chunk_size)
+{
+    if (s == NULL)
+    {
+        if ((s = (ima_adpcm_state_t *) malloc(sizeof(*s))) == NULL)
+            return  NULL;
+    }
+    /*endif*/
+    memset(s, 0, sizeof(*s));
+    s->variant = variant;
+    s->chunk_size = chunk_size;
+    return  s;
+}
+/*- End of function --------------------------------------------------------*/
+
+int ima_adpcm_release(ima_adpcm_state_t *s)
+{
+    free(s);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int ima_adpcm_decode(ima_adpcm_state_t *s,
+                     int16_t amp[],
+                     const uint8_t ima_data[],
+                     int ima_bytes)
+{
+    int i;
+    int j;
+    int samples;
+    uint16_t code;
+
+    samples = 0;
+    switch (s->variant)
+    {
+    case IMA_ADPCM_IMA4:
+        i = 0;
+        if (s->chunk_size == 0)
+        {
+            amp[samples++] = (ima_data[1] << 8) | ima_data[0];
+            s->step_index = ima_data[2];
+            s->last = amp[0];
+            i = 4;
+        }
+        /*endif*/
+        for (  ;  i < ima_bytes;  i++)
+        {
+            amp[samples++] = decode(s, ima_data[i] & 0xF);
+            amp[samples++] = decode(s, (ima_data[i] >> 4) & 0xF);
+        }
+        /*endfor*/
+        break;
+    case IMA_ADPCM_DVI4:
+        i = 0;
+        if (s->chunk_size == 0)
+        {
+            s->last = (int16_t) ((ima_data[0] << 8) | ima_data[1]);
+            s->step_index = ima_data[2];
+            i = 4;
+        }
+        /*endif*/
+        for (  ;  i < ima_bytes;  i++)
+        {
+            amp[samples++] = decode(s, (ima_data[i] >> 4) & 0xF);
+            amp[samples++] = decode(s, ima_data[i] & 0xF);
+        }
+        /*endfor*/
+        break;
+    case IMA_ADPCM_VDVI:
+        i = 0;
+        if (s->chunk_size == 0)
+        {
+            s->last = (int16_t) ((ima_data[0] << 8) | ima_data[1]);
+            s->step_index = ima_data[2];
+            i = 4;
+        }
+        /*endif*/
+        code = 0;
+        s->bits = 0;
+        for (;;)
+        {
+            if (s->bits <= 8)
+            {
+                if (i >= ima_bytes)
+                    break;
+                /*endif*/
+                code |= ((uint16_t) ima_data[i++] << (8 - s->bits));
+                s->bits += 8;
+            }
+            /*endif*/
+            for (j = 0;  j < 8;  j++)
+            {
+                if ((vdvi_decode[j].mask & code) == vdvi_decode[j].code)
+                    break;
+                if ((vdvi_decode[j + 8].mask & code) == vdvi_decode[j + 8].code)
+                {
+                    j += 8;
+                    break;
+                }
+                /*endif*/
+            }
+            /*endfor*/
+            amp[samples++] = decode(s, (uint8_t) j);
+            code <<= vdvi_decode[j].bits;
+            s->bits -= vdvi_decode[j].bits;
+        }
+        /*endfor*/
+        /* Use up the remanents of the last octet */
+        while (s->bits > 0)
+        {
+            for (j = 0;  j < 8;  j++)
+            {
+                if ((vdvi_decode[j].mask & code) == vdvi_decode[j].code)
+                    break;
+                /*endif*/
+                if ((vdvi_decode[j + 8].mask & code) == vdvi_decode[j + 8].code)
+                {
+                    j += 8;
+                    break;
+                }
+                /*endif*/
+            }
+            /*endfor*/
+            if (vdvi_decode[j].bits > s->bits)
+                break;
+            /*endif*/
+            amp[samples++] = decode(s, (uint8_t) j);
+            code <<= vdvi_decode[j].bits;
+            s->bits -= vdvi_decode[j].bits;
+        }
+        /*endwhile*/
+        break;
+    }
+    /*endswitch*/
+    return samples;
+}
+/*- End of function --------------------------------------------------------*/
+
+int ima_adpcm_encode(ima_adpcm_state_t *s,
+                     uint8_t ima_data[],
+                     const int16_t amp[],
+                     int len)
+{
+    int i;
+    int bytes;
+    uint8_t code;
+
+    bytes = 0;
+    switch (s->variant)
+    {
+    case IMA_ADPCM_IMA4:
+        i = 0;
+        if (s->chunk_size == 0)
+        {
+            ima_data[bytes++] = (uint8_t) amp[0];
+            ima_data[bytes++] = (uint8_t) (amp[0] >> 8);
+            ima_data[bytes++] = (uint8_t) s->step_index;
+            ima_data[bytes++] = 0;
+            s->last = amp[0];
+            s->bits = 0;
+            i = 1;
+        }
+        /*endif*/
+        for (  ;  i < len;  i++)
+        {
+            s->ima_byte = (uint8_t) ((s->ima_byte >> 4) | (encode(s, amp[i]) << 4));
+            if ((s->bits++ & 1))
+                ima_data[bytes++] = (uint8_t) s->ima_byte;
+            /*endif*/
+        }
+        /*endfor*/
+        break;
+    case IMA_ADPCM_DVI4:
+        if (s->chunk_size == 0)
+        {
+            ima_data[bytes++] = (uint8_t) (s->last >> 8);
+            ima_data[bytes++] = (uint8_t) s->last;
+            ima_data[bytes++] = (uint8_t) s->step_index;
+            ima_data[bytes++] = 0;
+        }
+        /*endif*/
+        for (i = 0;  i < len;  i++)
+        {
+            s->ima_byte = (uint8_t) ((s->ima_byte << 4) | encode(s, amp[i]));
+            if ((s->bits++ & 1))
+                ima_data[bytes++] = (uint8_t) s->ima_byte;
+            /*endif*/
+        }
+        /*endfor*/
+        break;
+    case IMA_ADPCM_VDVI:
+        if (s->chunk_size == 0)
+        {
+            ima_data[bytes++] = (uint8_t) (s->last >> 8);
+            ima_data[bytes++] = (uint8_t) s->last;
+            ima_data[bytes++] = (uint8_t) s->step_index;
+            ima_data[bytes++] = 0;
+        }
+        /*endif*/
+        s->bits = 0;
+        for (i = 0;  i < len;  i++)
+        {
+            code = encode(s, amp[i]);
+            s->ima_byte = (s->ima_byte << vdvi_encode[code].bits) | vdvi_encode[code].code;
+            s->bits += vdvi_encode[code].bits;
+            if (s->bits >= 8)
+            {
+                s->bits -= 8;
+                ima_data[bytes++] = (uint8_t) (s->ima_byte >> s->bits);
+            }
+            /*endif*/
+        }
+        /*endfor*/
+        if (s->bits)
+            ima_data[bytes++] = (uint8_t) (((s->ima_byte << 8) | 0xFF) >> s->bits);
+        /*endif*/
+        break;
+    }
+    /*endswitch*/
+    return  bytes;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/libspandsp.dsp
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/libspandsp.dsp	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,707 @@
+# Microsoft Developer Studio Project File - Name="spandsp" - Package Owner=<4>
+# Microsoft Developer Studio Generated Build File, Format Version 6.00
+# ** DO NOT EDIT **
+
+# TARGTYPE "Win32 (x86) Dynamic-Link Library" 0x0102
+
+CFG=spandsp - Win32 Debug
+!MESSAGE This is not a valid makefile. To build this project using NMAKE,
+!MESSAGE use the Export Makefile command and run
+!MESSAGE 
+!MESSAGE NMAKE /f "spandsp.mak".
+!MESSAGE 
+!MESSAGE You can specify a configuration when running NMAKE
+!MESSAGE by defining the macro CFG on the command line. For example:
+!MESSAGE 
+!MESSAGE NMAKE /f "spandsp.mak" CFG="spandsp - Win32 Debug"
+!MESSAGE 
+!MESSAGE Possible choices for configuration are:
+!MESSAGE 
+!MESSAGE "spandsp - Win32 Release" (based on "Win32 (x86) Dynamic-Link Library")
+!MESSAGE "spandsp - Win32 Debug" (based on "Win32 (x86) Dynamic-Link Library")
+!MESSAGE 
+
+# Begin Project
+# PROP AllowPerConfigDependencies 0
+# PROP Scc_ProjName ""
+# PROP Scc_LocalPath ""
+CPP=cl.exe
+MTL=midl.exe
+RSC=rc.exe
+
+!IF  "$(CFG)" == "spandsp - Win32 Release"
+
+# PROP BASE Use_MFC 0
+# PROP BASE Use_Debug_Libraries 0
+# PROP BASE Output_Dir "Release"
+# PROP BASE Intermediate_Dir "Release"
+# PROP BASE Target_Dir ""
+# PROP Use_MFC 0
+# PROP Use_Debug_Libraries 0
+# PROP Output_Dir "Release"
+# PROP Intermediate_Dir "Release"
+# PROP Ignore_Export_Lib 0
+# PROP Target_Dir ""
+# ADD BASE CPP /nologo /MT /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D HAVE_TGMATH_H /YX /FD /c
+# ADD CPP /nologo /MT /W3 /GX /Zi /O2 /I "." /I "..\include" /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D HAVE_TGMATH_H /D "_WINDLL" /FR /FD /c
+# SUBTRACT CPP /YX
+# ADD BASE MTL /nologo /D "NDEBUG" /mktyplib203 /win32
+# ADD MTL /nologo /D "NDEBUG" /mktyplib203 /win32
+# ADD BASE RSC /l 0x409 /d "NDEBUG"
+# ADD RSC /l 0x409 /d "NDEBUG"
+BSC32=bscmake.exe
+# ADD BASE BSC32 /nologo
+# ADD BSC32 /nologo
+LINK32=link.exe
+# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /dll /machine:I386
+# ADD LINK32 kernel32.lib ws2_32.lib winmm.lib /nologo /dll /map /debug /machine:I386 /out:"Release/libspandsp.dll"
+
+!ELSEIF  "$(CFG)" == "spandsp - Win32 Debug"
+
+# PROP BASE Use_MFC 0
+# PROP BASE Use_Debug_Libraries 1
+# PROP BASE Output_Dir "Debug"
+# PROP BASE Intermediate_Dir "Debug"
+# PROP BASE Target_Dir ""
+# PROP Use_MFC 0
+# PROP Use_Debug_Libraries 1
+# PROP Output_Dir "Debug"
+# PROP Intermediate_Dir "Debug"
+# PROP Ignore_Export_Lib 0
+# PROP Target_Dir ""
+# ADD BASE CPP /nologo /MTd /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D HAVE_TGMATH_H /YX /FD /GZ /c
+# ADD CPP /nologo /MTd /W3 /Gm /GX /Zi /Od /I "." /I "..\include" /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D HAVE_TGMATH_H /FR /FD /GZ /c
+# SUBTRACT CPP /WX /YX
+# ADD BASE MTL /nologo /D "_DEBUG" /mktyplib203 /win32
+# ADD MTL /nologo /D "_DEBUG" /mktyplib203 /win32
+# ADD BASE RSC /l 0x409 /d "_DEBUG"
+# ADD RSC /l 0x409 /d "_DEBUG"
+BSC32=bscmake.exe
+# ADD BASE BSC32 /nologo
+# ADD BSC32 /nologo
+LINK32=link.exe
+# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /dll /debug /machine:I386 /pdbtype:sept
+# ADD LINK32 kernel32.lib ws2_32.lib winmm.lib /nologo /dll /incremental:no /map /debug /machine:I386 /out:"Debug/libspandsp.dll" /pdbtype:sept
+# SUBTRACT LINK32 /nodefaultlib
+
+!ENDIF 
+
+# Begin Target
+
+# Name "spandsp - Win32 Release"
+# Name "spandsp - Win32 Debug"
+# Begin Group "Source Files"
+# Begin Source File
+
+SOURCE=.\adsi.c
+# End Source File
+# Begin Source File
+
+SOURCE=.\async.c
+# End Source File
+# Begin Source File
+
+SOURCE=.\at_interpreter.c
+# End Source File
+# Begin Source File
+
+SOURCE=.\awgn.c
+# End Source File
+# Begin Source File
+
+SOURCE=.\bell_r2_mf.c
+# End Source File
+# Begin Source File
+
+SOURCE=.\bert.c
+# End Source File
+# Begin Source File
+
+SOURCE=.\bit_operations.c
+# End Source File
+# Begin Source File
+
+SOURCE=.\bitstream.c
+# End Source File
+# Begin Source File
+
+SOURCE=.\complex_filters.c
+# End Source File
+# Begin Source File
+
+SOURCE=.\complex_vector_float.c
+# End Source File
+# Begin Source File
+
+SOURCE=.\crc.c
+# End Source File
+# Begin Source File
+
+SOURCE=.\dds_float.c
+# End Source File
+# Begin Source File
+
+SOURCE=.\dds_int.c
+# End Source File
+# Begin Source File
+
+SOURCE=.\dtmf.c
+# End Source File
+# Begin Source File
+
+SOURCE=.\echo.c
+# End Source File
+# Begin Source File
+
+SOURCE=.\fax.c
+# End Source File
+# Begin Source File
+
+SOURCE=.\fsk.c
+# End Source File
+# Begin Source File
+
+SOURCE=.\g711.c
+# End Source File
+# Begin Source File
+
+SOURCE=.\g722_encode.c
+# End Source File
+# Begin Source File
+
+SOURCE=.\g722_decode.c
+# End Source File
+# Begin Source File
+
+SOURCE=.\g726.c
+# End Source File
+# Begin Source File
+
+SOURCE=.\gsm0610_decode.c
+# End Source File
+# Begin Source File
+
+SOURCE=.\gsm0610_encode.c
+# End Source File
+# Begin Source File
+
+SOURCE=.\gsm0610_long_term.c
+# End Source File
+# Begin Source File
+
+SOURCE=.\gsm0610_lpc.c
+# End Source File
+# Begin Source File
+
+SOURCE=.\gsm0610_preprocess.c
+# End Source File
+# Begin Source File
+
+SOURCE=.\gsm0610_rpe.c
+# End Source File
+# Begin Source File
+
+SOURCE=.\gsm0610_short_term.c
+# End Source File
+# Begin Source File
+
+SOURCE=.\hdlc.c
+# End Source File
+# Begin Source File
+
+SOURCE=.\ima_adpcm.c
+# End Source File
+# Begin Source File
+
+SOURCE=.\logging.c
+# End Source File
+# Begin Source File
+
+SOURCE=.\lpc10_analyse.c
+# End Source File
+# Begin Source File
+
+SOURCE=.\lpc10_decode.c
+# End Source File
+# Begin Source File
+
+SOURCE=.\lpc10_encode.c
+# End Source File
+# Begin Source File
+
+SOURCE=.\lpc10_placev.c
+# End Source File
+# Begin Source File
+
+SOURCE=.\lpc10_voicing.c
+# End Source File
+# Begin Source File
+
+SOURCE=.\modem_echo.c
+# End Source File
+# Begin Source File
+
+SOURCE=.\modem_connect_tones.c
+# End Source File
+# Begin Source File
+
+SOURCE=.\noise.c
+# End Source File
+# Begin Source File
+
+SOURCE=.\oki_adpcm.c
+# End Source File
+# Begin Source File
+
+SOURCE=.\playout.c
+# End Source File
+# Begin Source File
+
+SOURCE=.\plc.c
+# End Source File
+# Begin Source File
+
+SOURCE=.\power_meter.c
+# End Source File
+# Begin Source File
+
+SOURCE=.\queue.c
+# End Source File
+# Begin Source File
+
+SOURCE=.\schedule.c
+# End Source File
+# Begin Source File
+
+SOURCE=.\sig_tone.c
+# End Source File
+# Begin Source File
+
+SOURCE=.\silence_gen.c
+# End Source File
+# Begin Source File
+
+SOURCE=.\super_tone_rx.c
+# End Source File
+# Begin Source File
+
+SOURCE=.\super_tone_tx.c
+# End Source File
+# Begin Source File
+
+SOURCE=.\t4.c
+# End Source File
+# Begin Source File
+
+SOURCE=.\t30.c
+# End Source File
+# Begin Source File
+
+SOURCE=.\t30_api.c
+# End Source File
+# Begin Source File
+
+SOURCE=.\t30_logging.c
+# End Source File
+# Begin Source File
+
+SOURCE=.\t31.c
+# End Source File
+# Begin Source File
+
+SOURCE=.\t35.c
+# End Source File
+# Begin Source File
+
+SOURCE=.\t38_core.c
+# End Source File
+# Begin Source File
+
+SOURCE=.\t38_gateway.c
+# End Source File
+# Begin Source File
+
+SOURCE=.\t38_non_ecm_buffer.c
+# End Source File
+# Begin Source File
+
+SOURCE=.\t38_terminal.c
+# End Source File
+# Begin Source File
+
+SOURCE=.\testcpuid.c
+# End Source File
+# Begin Source File
+
+SOURCE=.\time_scale.c
+# End Source File
+# Begin Source File
+
+SOURCE=.\tone_detect.c
+# End Source File
+# Begin Source File
+
+SOURCE=.\tone_generate.c
+# End Source File
+# Begin Source File
+
+SOURCE=.\v17rx.c
+# End Source File
+# Begin Source File
+
+SOURCE=.\v17tx.c
+# End Source File
+# Begin Source File
+
+SOURCE=.\v22bis_rx.c
+# End Source File
+# Begin Source File
+
+SOURCE=.\v22bis_tx.c
+# End Source File
+# Begin Source File
+
+SOURCE=.\v27ter_rx.c
+# End Source File
+# Begin Source File
+
+SOURCE=.\v27ter_tx.c
+# End Source File
+# Begin Source File
+
+SOURCE=.\v29rx.c
+# End Source File
+# Begin Source File
+
+SOURCE=.\v29tx.c
+# End Source File
+# Begin Source File
+
+SOURCE=.\v42.c
+# End Source File
+# Begin Source File
+
+SOURCE=.\v42bis.c
+# End Source File
+# Begin Source File
+
+SOURCE=.\v8.c
+# End Source File
+# Begin Source File
+
+SOURCE=.\vector_float.c
+# End Source File
+# Begin Source File
+
+SOURCE=.\vector_int.c
+# End Source File
+# Begin Source File
+
+SOURCE=.\msvc/gettimeofday.c
+# End Source File
+# End Group
+# Begin Group "Header Files"
+# Begin Source File
+
+SOURCE=.\spandsp/adsi.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\spandsp/async.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\spandsp/arctan2.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\spandsp/at_interpreter.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\spandsp/awgn.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\spandsp/bell_r2_mf.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\spandsp/bert.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\spandsp/biquad.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\spandsp/bit_operations.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\spandsp/bitstream.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\spandsp/crc.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\spandsp/complex.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\spandsp/complex_filters.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\spandsp/complex_vector_float.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\spandsp/dc_restore.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\spandsp/dds.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\spandsp/dtmf.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\spandsp/echo.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\spandsp/fax.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\spandsp/fax_modems.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\spandsp/fir.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\spandsp/fsk.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\spandsp/g168models.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\spandsp/g711.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\spandsp/g722.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\spandsp/g726.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\spandsp/gsm0610.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\spandsp/hdlc.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\spandsp/ima_adpcm.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\spandsp/logging.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\spandsp/lpc10.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\spandsp/modem_echo.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\spandsp/modem_connect_tones.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\spandsp/noise.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\spandsp/oki_adpcm.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\spandsp/playout.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\spandsp/plc.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\spandsp/power_meter.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\spandsp/queue.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\spandsp/schedule.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\spandsp/sig_tone.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\spandsp/silence_gen.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\spandsp/super_tone_rx.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\spandsp/super_tone_tx.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\spandsp/t4.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\spandsp/t30.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\spandsp/t30_api.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\spandsp/t30_fcf.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\spandsp/t30_logging.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\spandsp/t31.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\spandsp/t35.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\spandsp/t38_core.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\spandsp/t38_gateway.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\spandsp/t38_non_ecm_buffer.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\spandsp/t38_terminal.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\spandsp/telephony.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\spandsp/time_scale.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\spandsp/timing.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\spandsp/tone_detect.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\spandsp/tone_generate.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\spandsp/v17rx.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\spandsp/v17tx.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\spandsp/v22bis.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\spandsp/v27ter_rx.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\spandsp/v27ter_tx.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\spandsp/v29rx.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\spandsp/v29tx.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\spandsp/v42.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\spandsp/v42bis.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\spandsp/v8.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\spandsp/vector_float.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\spandsp/vector_int.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\spandsp/version.h
+# End Source File
+# Begin Source File
+
+SOURCE=.\spandsp.h
+# End Source File
+# End Group
+
+# Begin Group "Resource Files"
+
+# PROP Default_Filter "ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe"
+# End Group
+# End Target
+# End Project

Added: freeswitch/trunk/libs/spandsp/src/libspandsp.sln
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/libspandsp.sln	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,29 @@
+
+Microsoft Visual Studio Solution File, Format Version 9.00
+# Visual Studio 2005
+Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "libspandsp", "libspandsp.vcproj", "{CF70F278-3364-4395-A2E1-23501C9B8AD2}"
+	ProjectSection(ProjectDependencies) = postProject
+		{1CED5987-A529-46DC-B30F-870D85FF9C94} = {1CED5987-A529-46DC-B30F-870D85FF9C94}
+	EndProjectSection
+EndProject
+Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "at_dictionary_gen", "src\msvc\at_dictionary_gen.vcproj", "{1CED5987-A529-46DC-B30F-870D85FF9C94}"
+EndProject
+Global
+	GlobalSection(SolutionConfigurationPlatforms) = preSolution
+		Debug|Win32 = Debug|Win32
+		Release|Win32 = Release|Win32
+	EndGlobalSection
+	GlobalSection(ProjectConfigurationPlatforms) = postSolution
+		{CF70F278-3364-4395-A2E1-23501C9B8AD2}.Debug|Win32.ActiveCfg = Debug|Win32
+		{CF70F278-3364-4395-A2E1-23501C9B8AD2}.Debug|Win32.Build.0 = Debug|Win32
+		{CF70F278-3364-4395-A2E1-23501C9B8AD2}.Release|Win32.ActiveCfg = Release|Win32
+		{CF70F278-3364-4395-A2E1-23501C9B8AD2}.Release|Win32.Build.0 = Release|Win32
+		{1CED5987-A529-46DC-B30F-870D85FF9C94}.Debug|Win32.ActiveCfg = Debug|Win32
+		{1CED5987-A529-46DC-B30F-870D85FF9C94}.Debug|Win32.Build.0 = Debug|Win32
+		{1CED5987-A529-46DC-B30F-870D85FF9C94}.Release|Win32.ActiveCfg = Release|Win32
+		{1CED5987-A529-46DC-B30F-870D85FF9C94}.Release|Win32.Build.0 = Release|Win32
+	EndGlobalSection
+	GlobalSection(SolutionProperties) = preSolution
+		HideSolutionNode = FALSE
+	EndGlobalSection
+EndGlobal

Added: freeswitch/trunk/libs/spandsp/src/logging.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/logging.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,255 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * logging.c - error and debug logging.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2005 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: logging.c,v 1.26 2008/05/13 13:17:22 steveu Exp $
+ */
+
+/*! \file */
+
+#if defined(HAVE_CONFIG_H)
+#include <config.h>
+#endif
+
+#include <limits.h>
+#include <stdio.h>
+#include <stdarg.h>
+#include <fcntl.h>
+#include <stdlib.h>
+#include <inttypes.h>
+#include <string.h>
+#include <errno.h>
+#include <signal.h>
+#include <sys/time.h>
+#include <time.h>
+
+#include "spandsp/telephony.h"
+#include "spandsp/logging.h"
+
+static void default_message_handler(int level, const char *text);
+
+static message_handler_func_t __span_message = *default_message_handler;
+static error_handler_func_t __span_error = NULL;
+
+/* Note that this list *must* match the enum definition in logging.h */
+static const char *severities[] =
+{
+    "NONE",
+    "ERROR",
+    "WARNING",
+    "PROTOCOL_ERROR",
+    "PROTOCOL_WARNING",
+    "FLOW",
+    "FLOW 2",
+    "FLOW 3",
+    "DEBUG 1",
+    "DEBUG 2",
+    "DEBUG 3"
+};
+
+static void default_message_handler(int level, const char *text)
+{
+    fprintf(stderr, "%s", text);
+}
+/*- End of function --------------------------------------------------------*/
+
+int span_log_test(logging_state_t *s, int level)
+{
+    if (s  &&  (s->level & SPAN_LOG_SEVERITY_MASK) >= (level & SPAN_LOG_SEVERITY_MASK))
+        return TRUE;
+    return FALSE;
+}
+/*- End of function --------------------------------------------------------*/
+
+int span_log(logging_state_t *s, int level, const char *format, ...)
+{
+    char msg[1024 + 1];
+    va_list arg_ptr;
+    int len;
+    struct tm *tim;
+    struct timeval nowx;
+    time_t now;
+
+    if (span_log_test(s, level))
+    {
+        va_start(arg_ptr, format);
+        len = 0;
+        if ((level & SPAN_LOG_SUPPRESS_LABELLING) == 0)
+        {
+            if ((s->level & SPAN_LOG_SHOW_DATE))
+            {
+                gettimeofday(&nowx, NULL);
+                now = nowx.tv_sec;
+                tim = gmtime(&now);
+                len += snprintf(msg + len,
+                                1024 - len,
+                                "%04d/%02d/%02d %02d:%02d:%02d.%03d ",
+                                tim->tm_year + 1900,
+                                tim->tm_mon + 1,
+                                tim->tm_mday,
+                                tim->tm_hour,
+                                tim->tm_min,
+                                tim->tm_sec,
+                                (int) nowx.tv_usec/1000);
+            }
+            /*endif*/
+            if ((s->level & SPAN_LOG_SHOW_SAMPLE_TIME))
+            {
+                now = s->elapsed_samples/s->samples_per_second;
+                tim = gmtime(&now);
+                len += snprintf(msg + len,
+                                1024 - len,
+                                "%02d:%02d:%02d.%03d ",
+                                tim->tm_hour,
+                                tim->tm_min,
+                                tim->tm_sec,
+                                (int) (s->elapsed_samples%s->samples_per_second)*1000/s->samples_per_second);
+            }
+            /*endif*/
+            if ((s->level & SPAN_LOG_SHOW_SEVERITY)  &&  (level & SPAN_LOG_SEVERITY_MASK) <= SPAN_LOG_DEBUG_3)
+                len += snprintf(msg + len, 1024 - len, "%s ", severities[level & SPAN_LOG_SEVERITY_MASK]);
+            /*endif*/
+            if ((s->level & SPAN_LOG_SHOW_PROTOCOL)  &&  s->protocol)
+                len += snprintf(msg + len, 1024 - len, "%s ", s->protocol);
+            /*endif*/
+            if ((s->level & SPAN_LOG_SHOW_TAG)  &&  s->tag)
+                len += snprintf(msg + len, 1024 - len, "%s ", s->tag);
+            /*endif*/
+        }
+        /*endif*/
+        len += vsnprintf(msg + len, 1024 - len, format, arg_ptr);
+        if (s->span_error  &&  level == SPAN_LOG_ERROR)
+            s->span_error(msg);
+        else if (__span_error  &&  level == SPAN_LOG_ERROR)
+            __span_error(msg);
+        else if (s->span_message)
+            s->span_message(level, msg);
+        else if (__span_message)
+            __span_message(level, msg);
+        /*endif*/
+        va_end(arg_ptr);
+        return  1;
+    }
+    /*endif*/
+    return  0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int span_log_buf(logging_state_t *s, int level, const char *tag, const uint8_t *buf, int len)
+{
+    char msg[1024];
+    int i;
+    int msg_len;
+
+    if (span_log_test(s, level))
+    {
+        msg_len = 0;
+        if (tag)
+            msg_len += snprintf(msg + msg_len, 1024 - msg_len, "%s", tag);
+        for (i = 0;  i < len  &&  msg_len < 800;  i++)
+            msg_len += snprintf(msg + msg_len, 1024 - msg_len, " %02x", buf[i]);
+        msg_len += snprintf(msg + msg_len, 1024 - msg_len, "\n");
+        return span_log(s, level, msg);
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int span_log_init(logging_state_t *s, int level, const char *tag)
+{
+    s->span_error = __span_error;
+    s->span_message = __span_message;
+    s->level = level;
+    s->tag = tag;
+    s->protocol = NULL;
+    s->samples_per_second = SAMPLE_RATE;
+    s->elapsed_samples = 0;
+
+    return  0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int span_log_set_level(logging_state_t *s, int level)
+{
+    s->level = level;
+
+    return  0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int span_log_set_tag(logging_state_t *s, const char *tag)
+{
+    s->tag = tag;
+
+    return  0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int span_log_set_protocol(logging_state_t *s, const char *protocol)
+{
+    s->protocol = protocol;
+
+    return  0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int span_log_set_sample_rate(logging_state_t *s, int samples_per_second)
+{
+    s->samples_per_second = samples_per_second;
+
+    return  0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int span_log_bump_samples(logging_state_t *s, int samples)
+{
+    s->elapsed_samples += samples;
+
+    return  0;
+}
+/*- End of function --------------------------------------------------------*/
+
+void span_log_set_message_handler(logging_state_t *s, message_handler_func_t func)
+{
+    s->span_message = func;
+}
+/*- End of function --------------------------------------------------------*/
+
+void span_log_set_error_handler(logging_state_t *s, error_handler_func_t func)
+{
+    s->span_error = func;
+}
+/*- End of function --------------------------------------------------------*/
+
+void span_set_message_handler(message_handler_func_t func)
+{
+    __span_message = func;
+}
+/*- End of function --------------------------------------------------------*/
+
+void span_set_error_handler(error_handler_func_t func)
+{
+    __span_error = func;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/lpc10_analyse.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/lpc10_analyse.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,713 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * lpc10_analyse.c - LPC10 low bit rate speech codec.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2006 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * This code is based on the U.S. Department of Defense reference
+ * implementation of the LPC-10 2400 bps Voice Coder. They do not
+ * exert copyright claims on their code, and it may be freely used.
+ *
+ * $Id: lpc10_analyse.c,v 1.19 2008/07/02 14:48:25 steveu Exp $
+ */
+
+#if defined(HAVE_CONFIG_H)
+#include <config.h>
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <inttypes.h>
+#include <memory.h>
+#include "floating_fudge.h"
+#if defined(HAVE_TGMATH_H)
+#include <tgmath.h>
+#endif
+#if defined(HAVE_MATH_H)
+#include <math.h>
+#endif
+
+#include "spandsp/telephony.h"
+#include "spandsp/dc_restore.h"
+#include "spandsp/lpc10.h"
+
+#include "lpc10_encdecs.h"
+
+static __inline__ float energyf(float amp[], int len)
+{
+    int i;
+    float rms;
+
+    rms = 0.0f;
+    for (i = 0;  i < len;  i++)
+        rms += amp[i]*amp[i];
+    rms = sqrtf(rms/len);
+    return rms;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void remove_dc_bias(float speech[], int len, float sigout[])
+{
+    float bias;
+    int i;
+
+    bias = 0.0f;
+    for (i = 0;  i < len;  i++)
+        bias += speech[i];
+    bias /= len;
+    for (i = 0; i < len;  i++)
+        sigout[i] = speech[i] - bias;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void eval_amdf(float speech[],
+                      int32_t lpita,
+                      const int32_t tau[], 
+                      int32_t ltau,
+                      int32_t maxlag,
+                      float amdf[],
+                      int32_t *minptr,
+                      int32_t *maxptr)
+{
+    float sum;
+    int i;
+    int j;
+    int n1;
+    int n2;
+
+    *minptr = 0;
+    *maxptr = 0;
+    for (i = 0;  i < ltau;  i++)
+    {
+        n1 = (maxlag - tau[i])/2 + 1;
+        n2 = n1 + lpita - 1;
+        sum = 0.0f;
+        for (j = n1;  j <= n2;  j += 4)
+            sum += fabsf(speech[j - 1] - speech[j + tau[i] - 1]);
+        amdf[i] = sum;
+        if (amdf[i] < amdf[*minptr])
+            *minptr = i;
+        if (amdf[i] > amdf[*maxptr])
+            *maxptr = i;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static void eval_highres_amdf(float speech[],
+                              int32_t lpita,
+                              const int32_t tau[], 
+                              int32_t ltau,
+                              float amdf[],
+                              int32_t *minptr,
+                              int32_t *maxptr,
+                              int32_t *mintau)
+{
+    float amdf2[6];
+    int32_t tau2[6];
+    int32_t minp2;
+    int32_t ltau2;
+    int32_t maxp2;
+    int32_t minamd;
+    int i;
+    int i2;
+    int ptr;
+
+    /* Compute full AMDF using log spaced lags, find coarse minimum */
+    eval_amdf(speech, lpita, tau, ltau, tau[ltau - 1], amdf, minptr, maxptr);
+    *mintau = tau[*minptr];
+    minamd = (int32_t) amdf[*minptr];
+
+    /* Build table containing all lags within +/- 3 of the AMDF minimum,
+       excluding all that have already been computed */
+    ltau2 = 0;
+    ptr = *minptr - 2;
+    i2 = min(*mintau + 4, tau[ltau - 1]);
+    for (i = max(*mintau - 3, 41);  i < i2;  i++)
+    {
+        while (tau[ptr] < i)
+            ptr++;
+        if (tau[ptr] != i)
+            tau2[ltau2++] = i;
+    }
+    /* Compute AMDF of the new lags, if there are any, and choose one
+       if it is better than the coarse minimum */
+    if (ltau2 > 0)
+    {
+        eval_amdf(speech, lpita, tau2, ltau2, tau[ltau - 1], amdf2, &minp2, &maxp2);
+        if (amdf2[minp2] < (float) minamd)
+        {
+            *mintau = tau2[minp2];
+            minamd = (int32_t) amdf2[minp2];
+        }
+    }
+    /* Check one octave up, if there are any lags not yet computed */
+    if (*mintau >= 80)
+    {
+        i = *mintau/2;
+        if ((i & 1) == 0)
+        {
+            ltau2 = 2;
+            tau2[0] = i - 1;
+            tau2[1] = i + 1;
+        }
+        else
+        {
+            ltau2 = 1;
+            tau2[0] = i;
+        }
+        eval_amdf(speech, lpita, tau2, ltau2, tau[ltau - 1], amdf2, &minp2, &maxp2);
+        if (amdf2[minp2] < (float) minamd)
+        {
+            *mintau = tau2[minp2];
+            minamd = (int32_t) amdf2[minp2];
+            *minptr -= 20;
+        }
+    }
+    /* Force minimum of the AMDF array to the high resolution minimum */
+    amdf[*minptr] = (float) minamd;
+    /* Find maximum of AMDF within 1/2 octave of minimum */
+    *maxptr = max(*minptr - 5, 0);
+    i2 = min(*minptr + 6, ltau);
+    for (i = *maxptr;  i < i2;  i++)
+    {
+        if (amdf[i] > amdf[*maxptr])
+            *maxptr = i;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static void dynamic_pitch_tracking(lpc10_encode_state_t *s,
+                                   float amdf[],
+                                   int32_t ltau,
+                                   int32_t *minptr,
+                                   int32_t voice,
+                                   int32_t *pitch,
+                                   int32_t *midx)
+{
+    int32_t pbar;
+    float sbar;
+    int32_t path[2];
+    int32_t i;
+    int32_t j;
+    float alpha;
+    float minsc;
+    float maxsc;
+
+    /* Calculate the confidence factor ALPHA, used as a threshold slope in */
+    /* SEESAW.  If unvoiced, set high slope so that every point in P array */
+    /*is marked as a potential pitch frequency.  A scaled up version (ALPHAX )*/
+    /* is used to maintain arithmetic precision. */
+    if (voice == 1)
+        s->alphax = s->alphax*0.75f + amdf[*minptr - 1]*0.5f;
+    else
+        s->alphax *= 0.984375f;
+    alpha = s->alphax/16;
+    if (voice == 0  &&  s->alphax < 128.0f)
+        alpha = 8.0f;
+    /* SEESAW: Construct a pitch pointer array and intermediate winner function */
+    /* Left to right pass: */
+    s->p[s->ipoint][0] = 1;
+    pbar = 1;
+    sbar = s->s[0];
+    for (i = 0;  i < ltau;  i++)
+    {
+        sbar += alpha;
+        if (sbar < s->s[i])
+        {
+            s->s[i] = sbar;
+        }
+        else
+        {
+            pbar = i + 1;
+            sbar = s->s[i];
+        }
+        s->p[s->ipoint][i] = pbar;
+    }
+    /* Right to left pass: */
+    sbar = s->s[pbar - 1];
+    for (i = pbar - 2;  i >= 0;  i--)
+    {
+        sbar += alpha;
+        if (sbar < s->s[i])
+        {
+            s->s[i] = sbar;
+            s->p[s->ipoint][i] = pbar;
+        }
+        else
+        {
+            pbar = s->p[s->ipoint][i];
+            i = pbar - 1;
+            sbar = s->s[i];
+        }
+    }
+    /* Update S using AMDF */
+    /* Find maximum, minimum, and location of minimum */
+    s->s[0] += amdf[0]/2;
+    minsc = s->s[0];
+    maxsc = minsc;
+    *midx = 1;
+    for (i = 1;  i < ltau;  i++)
+    {
+        s->s[i] += amdf[i]/2;
+        if (s->s[i] > maxsc)
+            maxsc = s->s[i];
+        if (s->s[i] < minsc)
+        {
+            *midx = i + 1;
+            minsc = s->s[i];
+        }
+    }
+    /* Subtract MINSC from S to prevent overflow */
+    for (i = 0;  i < ltau;  i++)
+        s->s[i] -= minsc;
+    maxsc -= minsc;
+    /* Use higher octave pitch if significant null there */
+    j = 0;
+    for (i = 20;  i <= 40;  i += 10)
+    {
+        if (*midx > i)
+        {
+            if (s->s[*midx - i - 1] < maxsc / 4)
+                j = i;
+        }
+    }
+    *midx -= j;
+    /* TRACE: look back two frames to find minimum cost pitch estimate */
+    *pitch = *midx;
+    for (i = 0, j = s->ipoint;  i < 2;  i++, j++)
+    {
+        *pitch = s->p[j & 1][*pitch - 1];
+        path[i] = *pitch;
+    }
+
+    /* The following statement subtracts one from IPOINT, mod DEPTH.  I */
+    /* think the author chose to add DEPTH-1, instead of subtracting 1, */
+    /* because then it will work even if MOD doesn't work as desired on */
+    /* negative arguments. */
+    s->ipoint = (s->ipoint + 1) & 1;
+}
+/*- End of function --------------------------------------------------------*/
+
+/* Detection of onsets in (or slightly preceding) the futuremost frame of speech. */
+static void onset(lpc10_encode_state_t *s,
+                  float *pebuf,
+                  int32_t osbuf[],
+                  int32_t *osptr,
+                  int32_t oslen,
+                  int32_t sbufl,
+                  int32_t sbufh,
+                  int32_t lframe)
+{
+    int32_t i;
+    float r1;
+    float l2sum2;
+
+    pebuf -= sbufl;
+
+    if (s->hyst)
+        s->lasti -= lframe;
+    for (i = sbufh - lframe + 1;  i <= sbufh;  i++)
+    {
+        /* Compute FPC; Use old FPC on divide by zero; Clamp FPC to +/- 1. */
+        s->n = (pebuf[i]*pebuf[i - 1] + s->n*63.0f)/64.0f;
+        /* Computing 2nd power */
+        r1 = pebuf[i - 1];
+        s->d__ = (r1*r1 + s->d__*63.0f)/64.0f;
+        if (s->d__ != 0.0f)
+        {
+            if (fabsf(s->n) > s->d__)
+                s->fpc = r_sign(1.0f, s->n);
+            else
+                s->fpc = s->n/s->d__;
+        }
+        /* Filter FPC */
+        l2sum2 = s->l2buf[s->l2ptr1 - 1];
+        s->l2sum1 = s->l2sum1 - s->l2buf[s->l2ptr2 - 1] + s->fpc;
+        s->l2buf[s->l2ptr2 - 1] = s->l2sum1;
+        s->l2buf[s->l2ptr1 - 1] = s->fpc;
+        s->l2ptr1 = (s->l2ptr1 & 0xF) + 1;
+        s->l2ptr2 = (s->l2ptr2 & 0xF) + 1;
+        if (fabsf(s->l2sum1 - l2sum2) > 1.7f)
+        {
+            if (!s->hyst)
+            {
+                /* Ignore if buffer full */
+                if (*osptr <= oslen)
+                {
+                    osbuf[*osptr - 1] = i - 9;
+                    (*osptr)++;
+                }
+                s->hyst = TRUE;
+            }
+            s->lasti = i;
+            /* After one onset detection, at least OSHYST sample times must go */
+            /* by before another is allowed to occur. */
+        }
+        else if (s->hyst  &&  i - s->lasti >= 10)
+        {
+            s->hyst = FALSE;
+        }
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+/* Load a covariance matrix. */
+static void mload(int32_t order, int32_t awins, int32_t awinf, float speech[], float phi[], float psi[])
+{
+    int32_t start;
+    int i;
+    int r;
+
+    start = awins + order;
+    for (r = 1;  r <= order;  r++)
+    {
+        phi[r - 1] = 0.0f;
+        for (i = start;  i <= awinf;  i++)
+            phi[r - 1] += speech[i - 2]*speech[i - r - 1];
+    }
+
+    /* Load last element of vector PSI */
+    psi[order - 1] = 0.0f;
+    for (i = start - 1;  i < awinf;  i++)
+        psi[order - 1] += speech[i]*speech[i - order];
+    /* End correct to get additional columns of phi */
+    for (r = 1;  r < order;  r++)
+    {
+        for (i = 1;  i <= r;  i++)
+        {
+            phi[i*order + r] = phi[(i - 1)*order + r - 1]
+                             - speech[awinf - (r + 1)]*speech[awinf - (i + 1)]
+                             + speech[start - (r + 2)]*speech[start - (i + 2)];
+        }
+    }
+    /* End correct to get additional elements of PSI */
+    for (i = 0;  i < order - 1;  i++)
+    {
+        psi[i] = phi[i + 1]
+               - speech[start - 2]*speech[start - i - 3]
+               + speech[awinf - 1]*speech[awinf - i - 2];
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+/* Preemphasize speech with a single-zero filter. */
+/* (When coef = .9375, preemphasis is as in LPC43.) */
+static float preemp(float inbuf[], float pebuf[], int nsamp, float coeff, float z)
+{
+    float temp;
+    int i;
+
+    for (i = 0;  i < nsamp;  i++)
+    {
+        temp = inbuf[i] - coeff*z;
+        z = inbuf[i];
+        pebuf[i] = temp;
+    }
+    return z;
+}
+/*- End of function --------------------------------------------------------*/
+
+/* Invert a covariance matrix using Choleski decomposition method. */
+static void invert(int32_t order, float phi[], float psi[], float rc[])
+{
+    float r1;
+    int32_t i;
+    int32_t j;
+    int32_t k;
+    float v[10][10];
+
+    for (j = 0;  j < order;  j++)
+    {
+        for (i = j;  i < order;  i++)
+            v[j][i] = phi[i + j*order];
+        for (k = 0;  k < j;  k++)
+        {
+            r1 = v[k][j]*v[k][k];
+            for (i = j;  i <= order;  i++)
+                v[j][i] -= v[k][i]*r1;
+        }
+        /* Compute intermediate results, which are similar to RC's */
+        if (fabsf(v[j][j]) < 1.0e-10f)
+        {
+            for (i = j;  i < order;  i++)
+                rc[i] = 0.0f;
+            return;
+        }
+        rc[j] = psi[j];
+        for (k = 0;  k < j;  k++)
+            rc[j] -= rc[k]*v[k][j];
+        v[j][j] = 1.0f/v[j][j];
+        rc[j] *= v[j][j];
+        r1 = min(rc[j], 0.999f);
+        rc[j] = max(r1, -0.999f);
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+/* Check RC's, repeat previous frame's RC's if unstable */
+static int rcchk(int order, float rc1f[], float rc2f[])
+{
+    int i;
+
+    for (i = 0;  i < order;  i++)
+    {
+        if (fabsf(rc2f[i]) > 0.99f)
+        {
+            for (i = 0;  i < order;  i++)
+                rc2f[i] = rc1f[i];
+            break;
+        }
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void lpfilt(float inbuf[], float lpbuf[], int32_t len, int32_t nsamp)
+{
+    int32_t j;
+    float t;
+
+    /* 31 point equiripple FIR LPF */
+    /* Linear phase, delay = 15 samples */
+    /* Passband:  ripple = 0.25 dB, cutoff =  800 Hz */
+    /* Stopband:  atten. =  40. dB, cutoff = 1240 Hz */
+
+    for (j = len - nsamp;  j < len;  j++)
+    {
+        t = (inbuf[j] + inbuf[j - 30]) * -0.0097201988f;
+        t += (inbuf[j - 1] + inbuf[j - 29]) * -0.0105179986f;
+        t += (inbuf[j - 2] + inbuf[j - 28]) * -0.0083479648f;
+        t += (inbuf[j - 3] + inbuf[j - 27]) * 5.860774e-4f;
+        t += (inbuf[j - 4] + inbuf[j - 26]) * 0.0130892089f;
+        t += (inbuf[j - 5] + inbuf[j - 25]) * 0.0217052232f;
+        t += (inbuf[j - 6] + inbuf[j - 24]) * 0.0184161253f;
+        t += (inbuf[j - 7] + inbuf[j - 23]) * 3.39723e-4f;
+        t += (inbuf[j - 8] + inbuf[j - 22]) * -0.0260797087f;
+        t += (inbuf[j - 9] + inbuf[j - 21]) * -0.0455563702f;
+        t += (inbuf[j - 10] + inbuf[j - 20]) * -0.040306855f;
+        t += (inbuf[j - 11] + inbuf[j - 19]) * 5.029835e-4f;
+        t += (inbuf[j - 12] + inbuf[j - 18]) * 0.0729262903f;
+        t += (inbuf[j - 13] + inbuf[j - 17]) * 0.1572008878f;
+        t += (inbuf[j - 14] + inbuf[j - 16]) * 0.2247288674f;
+        t += inbuf[j - 15] * 0.250535965f;
+        lpbuf[j] = t;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+/* 2nd order inverse filter, speech is decimated 4:1 */
+static void ivfilt(float lpbuf[], float ivbuf[], int32_t len, int32_t nsamp, float ivrc[])
+{
+    int32_t i;
+    int32_t j;
+    int32_t k;
+    float r[3];
+    float pc1;
+    float pc2;
+
+    /* Calculate autocorrelations */
+    for (i = 1;  i <= 3;  i++)
+    {
+        r[i - 1] = 0.0f;
+        k = (i - 1) << 2;
+        for (j = (i << 2) + len - nsamp;  j <= len;  j += 2)
+            r[i - 1] += lpbuf[j - 1]*lpbuf[j - k - 1];
+    }
+    /* Calculate predictor coefficients */
+    pc1 = 0.0f;
+    pc2 = 0.0f;
+    ivrc[0] = 0.0f;
+    ivrc[1] = 0.0f;
+    if (r[0] > 1.0e-10f)
+    {
+        ivrc[0] = r[1]/r[0];
+        ivrc[1] = (r[2] - ivrc[0]*r[1])/(r[0] - ivrc[0]*r[1]);
+        pc1 = ivrc[0] - ivrc[0]*ivrc[1];
+        pc2 = ivrc[1];
+    }
+    /* Inverse filter LPBUF into IVBUF */
+    for (i = len - nsamp;  i < len;  i++)
+        ivbuf[i] = lpbuf[i] - pc1*lpbuf[i - 4] - pc2*lpbuf[i - 8];
+}
+/*- End of function --------------------------------------------------------*/
+
+void lpc10_analyse(lpc10_encode_state_t *s, float speech[], int32_t voice[], int32_t *pitch, float *rms, float rc[])
+{
+    static const int32_t tau[60] =
+    {
+        20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 
+        35, 36, 37, 38, 39, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58,
+        60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 84, 88, 92, 96,
+        100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140, 144,
+        148, 152, 156
+    };
+    static const int32_t buflim[4] =
+    {
+        181, 720, 25, 720
+    };
+    static const float precoef = 0.9375f;
+
+    float amdf[60];
+    float abuf[156];
+    float ivrc[2];
+    float temp;
+    float phi[100]    /* was [10][10] */;
+    float psi[10];
+    int32_t half;
+    int32_t midx;
+    int32_t ewin[3][2];
+    int32_t i;
+    int32_t j;
+    int32_t lanal;
+    int32_t ipitch;
+    int32_t mintau;
+    int32_t minptr;
+    int32_t maxptr;
+
+    /* Calculations are done on future frame due to requirements
+       of the pitch tracker.  Delay RMS and RC's 2 frames to give
+       current frame parameters on return. */
+
+    for (i = 0;  i <= 720 - LPC10_SAMPLES_PER_FRAME - 181;  i++)
+    {
+        s->inbuf[i] = s->inbuf[LPC10_SAMPLES_PER_FRAME + i];
+        s->pebuf[i] = s->pebuf[LPC10_SAMPLES_PER_FRAME + i];
+    }
+    for (i = 0;  i <= 540 - LPC10_SAMPLES_PER_FRAME - 229;  i++)
+        s->ivbuf[i] = s->ivbuf[LPC10_SAMPLES_PER_FRAME + i];
+    for (i = 0;  i <= 720 - LPC10_SAMPLES_PER_FRAME - 25;  i++)
+        s->lpbuf[i] = s->lpbuf[LPC10_SAMPLES_PER_FRAME + i];
+    for (i = 0, j = 0;  i < s->osptr - 1;  i++)
+    {
+        if (s->osbuf[i] > LPC10_SAMPLES_PER_FRAME)
+            s->osbuf[j++] = s->osbuf[i] - LPC10_SAMPLES_PER_FRAME;
+    }
+    s->osptr = j + 1;
+    s->voibuf[0][0] = s->voibuf[1][0];
+    s->voibuf[0][1] = s->voibuf[1][1];
+    for (i = 0;  i < 2;  i++)
+    {
+        s->vwin[i][0] = s->vwin[i + 1][0] - LPC10_SAMPLES_PER_FRAME;
+        s->vwin[i][1] = s->vwin[i + 1][1] - LPC10_SAMPLES_PER_FRAME;
+        s->awin[i][0] = s->awin[i + 1][0] - LPC10_SAMPLES_PER_FRAME;
+        s->awin[i][1] = s->awin[i + 1][1] - LPC10_SAMPLES_PER_FRAME;
+        s->obound[i] = s->obound[i + 1];
+        s->voibuf[i + 1][0] = s->voibuf[i + 2][0];
+        s->voibuf[i + 1][1] = s->voibuf[i + 2][1];
+        s->rmsbuf[i] = s->rmsbuf[i + 1];
+        for (j = 0;  j < LPC10_ORDER;  j++)
+            s->rcbuf[i][j] = s->rcbuf[i + 1][j];
+    }
+    /* If the average value in the frame was over 1/4096 (after current
+       BIAS correction), then subtract that much more from samples in the
+       next frame.  If the average value in the frame was under
+       -1/4096, add 1/4096 more to samples in next frame.  In all other
+       cases, keep BIAS the same. */
+    temp = 0.0f;
+    for (i = 0;  i < LPC10_SAMPLES_PER_FRAME;  i++)
+    {
+        s->inbuf[720 - 2*LPC10_SAMPLES_PER_FRAME + i] = speech[i]*4096.0f - s->bias;
+        temp += s->inbuf[720 - 2*LPC10_SAMPLES_PER_FRAME + i];
+    }
+    if (temp > (float) LPC10_SAMPLES_PER_FRAME)
+        s->bias++;
+    else if (temp < (float) (-LPC10_SAMPLES_PER_FRAME))
+        s->bias--;
+    /* Place voicing window */
+    i = 721 - LPC10_SAMPLES_PER_FRAME;
+    s->zpre = preemp(&s->inbuf[i - 181], &s->pebuf[i - 181], LPC10_SAMPLES_PER_FRAME, precoef, s->zpre);
+    onset(s, s->pebuf, s->osbuf, &s->osptr, 10, 181, 720, LPC10_SAMPLES_PER_FRAME);
+
+    lpc10_placev(s->osbuf, &s->osptr, 10, &s->obound[2], s->vwin, 3, LPC10_SAMPLES_PER_FRAME, 90, 156, 307, 462);
+    /* The Pitch Extraction algorithm estimates the pitch for a frame
+       of speech by locating the minimum of the average magnitude difference
+       function (AMDF).  The AMDF operates on low-pass, inverse filtered
+       speech.  (The low-pass filter is an 800 Hz, 19 tap, equiripple, FIR
+       filter and the inverse filter is a 2nd-order LPC filter.)  The pitch
+       estimate is later refined by dynamic tracking.  However, since some
+       of the tracking parameters are a function of the voicing decisions,
+       a voicing decision must precede the final pitch estimation. */
+    /* See subroutines LPFILT, IVFILT, and eval_highres_amdf. */
+    /* LPFILT reads indices LBUFH-LFRAME-29 = 511 through LBUFH = 720
+       of INBUF, and writes indices LBUFH+1-LFRAME = 541 through LBUFH
+       = 720 of LPBUF. */
+    lpfilt(&s->inbuf[228], &s->lpbuf[384], 312, LPC10_SAMPLES_PER_FRAME);
+    /* IVFILT reads indices (PWINH-LFRAME-7) = 353 through PWINH = 540
+       of LPBUF, and writes indices (PWINH-LFRAME+1) = 361 through
+       PWINH = 540 of IVBUF. */
+    ivfilt(&s->lpbuf[204], s->ivbuf, 312, LPC10_SAMPLES_PER_FRAME, ivrc);
+    /* eval_highres_amdf reads indices PWINL = 229 through
+       (PWINL-1)+MAXWIN+(TAU(LTAU)-TAU(1))/2 = 452 of IVBUF, and writes
+       indices 1 through LTAU = 60 of AMDF. */
+    eval_highres_amdf(s->ivbuf, 156, tau, 60, amdf, &minptr, &maxptr, &mintau);
+    /* Voicing decisions are made for each half frame of input speech.
+       An initial voicing classification is made for each half of the
+       analysis frame, and the voicing decisions for the present frame
+       are finalized.  See subroutine VOICIN. */
+    /*        The voicing detector (VOICIN) classifies the input signal as
+       unvoiced (including silence) or voiced using the AMDF windowed
+       maximum-to-minimum ratio, the zero crossing rate, energy measures,
+       reflection coefficients, and prediction gains. */
+    /*        The pitch and voicing rules apply smoothing and isolated
+       corrections to the pitch and voicing estimates and, in the process,
+       introduce two frames of delay into the corrected pitch estimates and
+       voicing decisions. */
+    for (half = 0;  half < 2;  half++)
+    {
+        lpc10_voicing(s,
+                      &s->vwin[2][0],
+                      s->inbuf,
+                      s->lpbuf,
+                      buflim,
+                      half,
+                      &amdf[minptr],
+                      &amdf[maxptr],
+                      &mintau,
+                      ivrc,
+                      s->obound);
+    }
+    /* Find the minimum cost pitch decision over several frames,
+       given the current voicing decision and the AMDF array */
+    minptr++;
+    dynamic_pitch_tracking(s, amdf, 60, &minptr, s->voibuf[3][1], pitch, &midx);
+    ipitch = tau[midx - 1];
+    /* Place spectrum analysis and energy windows */
+    lpc10_placea(&ipitch, s->voibuf, &s->obound[2], 3, s->vwin, s->awin, ewin, LPC10_SAMPLES_PER_FRAME, 156);
+    /* Remove short term DC bias over the analysis window. */
+    lanal = s->awin[2][1] + 1 - s->awin[2][0];
+    remove_dc_bias(&s->pebuf[s->awin[2][0] - 181], lanal, abuf);
+    /* Compute RMS over integer number of pitch periods within the analysis window. */
+    /* Note that in a hardware implementation this computation may be
+       simplified by using diagonal elements of phi computed by mload(). */
+    s->rmsbuf[2] = energyf(&abuf[ewin[2][0] - s->awin[2][0]], ewin[2][1] - ewin[2][0] + 1);
+    /* Matrix load and invert, check RC's for stability */
+    mload(LPC10_ORDER, 1, lanal, abuf, phi, psi);
+    invert(LPC10_ORDER, phi, psi, &s->rcbuf[2][0]);
+    rcchk(LPC10_ORDER, &s->rcbuf[1][0], &s->rcbuf[2][0]);
+    /* Set return parameters */
+    voice[0] = s->voibuf[1][0];
+    voice[1] = s->voibuf[1][1];
+    *rms = s->rmsbuf[0];
+    for (i = 0;  i < LPC10_ORDER;  i++)
+        rc[i] = s->rcbuf[0][i];
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/lpc10_decode.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/lpc10_decode.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,1110 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * lpc10_decode.c - LPC10 low bit rate speech codec.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2006 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * This code is based on the U.S. Department of Defense reference
+ * implementation of the LPC-10 2400 bps Voice Coder. They do not
+ * exert copyright claims on their code, and it may be freely used.
+ *
+ * $Id: lpc10_decode.c,v 1.22 2008/07/02 14:48:25 steveu Exp $
+ */
+
+#if defined(HAVE_CONFIG_H)
+#include <config.h>
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <inttypes.h>
+#include "floating_fudge.h"
+#if defined(HAVE_TGMATH_H)
+#include <tgmath.h>
+#endif
+#if defined(HAVE_MATH_H)
+#include <math.h>
+#endif
+#include <memory.h>
+
+#include "spandsp/telephony.h"
+#include "spandsp/dc_restore.h"
+#include "spandsp/lpc10.h"
+
+#define LPC10_ORDER     10
+
+#if !defined(min)
+#define min(a,b) ((a) <= (b) ? (a) : (b))
+#endif
+#if !defined(max)
+#define max(a,b) ((a) >= (b) ? (a) : (b))
+#endif
+
+/* Pseudo random number generator based on Knuth, Vol 2, p. 27. */
+/* lpc10_random - int32_t variable, uniformly distributed over -32768 to 32767 */
+static int32_t lpc10_random(lpc10_decode_state_t *s)
+{
+    int32_t ret_val;
+
+    /* The following is a 16 bit 2's complement addition,
+       with overflow checking disabled */
+    s->y[s->k] += s->y[s->j];
+    ret_val = s->y[s->k];
+    if (--s->k < 0)
+        s->k = 4;
+    if (--s->j < 0)
+        s->j = 4;
+    return ret_val;
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ int32_t pow_ii(int32_t x, int32_t n)
+{
+    int32_t pow;
+    uint32_t u;
+
+    if (n <= 0)
+    {
+        if (n == 0  ||  x == 1)
+            return 1;
+        if (x != -1)
+            return (x == 0)  ?  1/x  :  0;
+        n = -n;
+    }
+    u = n;
+    for (pow = 1;  ;  )
+    {
+        if ((u & 1))
+            pow *= x;
+        if ((u >>= 1) == 0)
+            break;
+        x *= x;
+    }
+    return pow;
+}
+/*- End of function --------------------------------------------------------*/
+
+/* Synthesize one pitch epoch */
+static void bsynz(lpc10_decode_state_t *s,
+                  float coef[],
+                  int32_t ip,
+                  int32_t *iv,
+                  float sout[],
+                  float rms,
+                  float ratio,
+                  float g2pass)
+{
+    static const int32_t kexc[25] =
+    {
+          8,  -16,   26, -48,  86, -162, 294, -502, 718, -728, 184, 
+        672, -610, -672, 184, 728,  718, 502,  294, 162,   86,  48, 26, 16, 8
+    };
+    int32_t i;
+    int32_t j;
+    int32_t k;
+    int32_t px;
+    float noise[166];
+    float pulse;
+    float r1;
+    float gain;
+    float xssq;
+    float sscale;
+    float xy;
+    float sum;
+    float ssq;
+    float lpi0;
+    float hpi0;
+
+    /* MAXPIT + MAXORD = 166 */
+    /* Calculate history scale factor XY and scale filter state */
+    /* Computing MIN */
+    r1 = s->rmso_bsynz/(rms + 1.0e-6f);
+    xy = min(r1, 8.0f);
+    s->rmso_bsynz = rms;
+    for (i = 0;  i < LPC10_ORDER;  i++)
+        s->exc2[i] = s->exc2[s->ipo + i]*xy;
+    s->ipo = ip;
+    if (*iv == 0)
+    {
+        /* Generate white noise for unvoiced */
+        for (i = 0;  i < ip;  i++)
+            s->exc[LPC10_ORDER + i] = (float) (lpc10_random(s)/64);
+        /* Impulse double excitation for plosives */
+        px = (lpc10_random(s) + 32768)*(ip - 1)/65536 + LPC10_ORDER + 1;
+        r1 = ratio/4.0f;
+        pulse = r1*342;
+        if (pulse > 2.0e3f)
+            pulse = 2.0e3f;
+        s->exc[px - 1] += pulse;
+        s->exc[px] -= pulse;
+    }
+    else
+    {
+        sscale = sqrtf((float) ip)/6.928f;
+        for (i = 0;  i < ip;  i++)
+        {
+            s->exc[LPC10_ORDER + i] = 0.0f;
+            if (i < 25)
+                s->exc[LPC10_ORDER + i] = sscale*kexc[i];
+            lpi0 = s->exc[LPC10_ORDER + i];
+            s->exc[LPC10_ORDER + i] = s->exc[LPC10_ORDER + i]*0.125f + s->lpi[0]*0.75f + s->lpi[1]*0.125f;
+            s->lpi[1] = s->lpi[0];
+            s->lpi[0] = lpi0;
+        }
+        for (i = 0;  i < ip;  i++)
+        {
+            noise[LPC10_ORDER + i] = lpc10_random(s)/64.0f;
+            hpi0 = noise[LPC10_ORDER + i];
+            noise[LPC10_ORDER + i] = noise[LPC10_ORDER + i]*-0.125f + s->hpi[0]*0.25f + s->hpi[1]*-0.125f;
+            s->hpi[1] = s->hpi[0];
+            s->hpi[0] = hpi0;
+        }
+        for (i = 0;  i < ip;  i++)
+            s->exc[LPC10_ORDER + i] += noise[LPC10_ORDER + i];
+    }
+    /* Synthesis filters: */
+    /* Modify the excitation with all-zero filter 1 + G*SUM */
+    xssq = 0.0f;
+    for (i = 0;  i < ip;  i++)
+    {
+        k = LPC10_ORDER + i;
+        sum = 0.0f;
+        for (j = 0;  j < LPC10_ORDER;  j++)
+            sum += coef[j]*s->exc[k - j - 1];
+        sum *= g2pass;
+        s->exc2[k] = sum + s->exc[k];
+    }
+    /* Synthesize using the all pole filter 1/(1 - SUM) */
+    for (i = 0;  i < ip;  i++)
+    {
+        k = LPC10_ORDER + i;
+        sum = 0.0f;
+        for (j = 0;  j < LPC10_ORDER;  j++)
+            sum += coef[j]*s->exc2[k - j - 1];
+        s->exc2[k] = sum + s->exc2[k];
+        xssq += s->exc2[k]*s->exc2[k];
+    }
+    /* Save filter history for next epoch */
+    for (i = 0;  i < LPC10_ORDER;  i++)
+    {
+        s->exc[i] = s->exc[ip + i];
+        s->exc2[i] = s->exc2[ip + i];
+    }
+    /* Apply gain to match RMS */
+    ssq = rms*rms*ip;
+    gain = sqrtf(ssq/xssq);
+    for (i = 0;  i < ip;  i++)
+        sout[i] = gain*s->exc2[LPC10_ORDER + i];
+}
+/*- End of function --------------------------------------------------------*/
+
+/* Synthesize a single pitch epoch */
+static int pitsyn(lpc10_decode_state_t *s,
+                  int voice[], 
+                  int32_t *pitch,
+                  float *rms,
+                  float *rc,
+                  int32_t ivuv[], 
+                  int32_t ipiti[],
+                  float *rmsi,
+                  float *rci,
+                  int32_t *nout,
+                  float *ratio)
+{
+    int32_t rci_dim1;
+    int32_t rci_offset;
+    int32_t i1;
+    int32_t i;
+    int32_t j;
+    int32_t vflag;
+    int32_t jused;
+    int32_t lsamp;
+    int32_t ip;
+    int32_t nl;
+    int32_t ivoice;
+    int32_t istart;
+    float r1;
+    float alrn;
+    float alro;
+    float yarc[10];
+    float prop;
+    float slope;
+    float uvpit;
+    float xxy;
+
+    rci_dim1 = LPC10_ORDER;
+    rci_offset = rci_dim1 + 1;
+    rci -= rci_offset;
+
+    if (*rms < 1.0f)
+        *rms = 1.0f;
+    if (s->rmso < 1.0f)
+        s->rmso = 1.0f;
+    uvpit = 0.0f;
+    *ratio = *rms/(s->rmso + 8.0f);
+    if (s->first_pitsyn)
+    {
+        lsamp = 0;
+        ivoice = voice[1];
+        if (ivoice == 0)
+            *pitch = LPC10_SAMPLES_PER_FRAME/4;
+        *nout = LPC10_SAMPLES_PER_FRAME / *pitch;
+        s->jsamp = LPC10_SAMPLES_PER_FRAME - *nout * *pitch;
+
+        i1 = *nout;
+        for (i = 0;  i < i1;  i++)
+        {
+            for (j = 0;  j < LPC10_ORDER;  j++)
+                rci[j + (i + 1)*rci_dim1 + 1] = rc[j];
+            ivuv[i] = ivoice;
+            ipiti[i] = *pitch;
+            rmsi[i] = *rms;
+        }
+        s->first_pitsyn = FALSE;
+    }
+    else
+    {
+        vflag = 0;
+        lsamp = LPC10_SAMPLES_PER_FRAME + s->jsamp;
+        slope = (*pitch - s->ipito)/(float) lsamp;
+        *nout = 0;
+        jused = 0;
+        istart = 1;
+        if (voice[0] == s->ivoico  &&  voice[1] == voice[0])
+        {
+            if (voice[1] == 0)
+            {
+                /* SSUV - -   0  ,  0  ,  0 */
+                *pitch = LPC10_SAMPLES_PER_FRAME/4;
+                s->ipito = *pitch;
+                if (*ratio > 8.0f)
+                    s->rmso = *rms;
+            }
+            /* SSVC - -   1  ,  1  ,  1 */
+            slope = (*pitch - s->ipito)/(float) lsamp;
+            ivoice = voice[1];
+        }
+        else
+        {
+            if (s->ivoico != 1)
+            {
+                if (s->ivoico == voice[0])
+                {
+                    /* UV2VC2 - -  0  ,  0  ,  1 */
+                    nl = lsamp - LPC10_SAMPLES_PER_FRAME/4;
+                }
+                else
+                {
+                    /* UV2VC1 - -  0  ,  1  ,  1 */
+                    nl = lsamp - LPC10_SAMPLES_PER_FRAME*3/4;
+                }
+                ipiti[0] = nl/2;
+                ipiti[1] = nl - ipiti[0];
+                ivuv[0] = 0;
+                ivuv[1] = 0;
+                rmsi[0] = s->rmso;
+                rmsi[1] = s->rmso;
+                for (i = 0;  i < LPC10_ORDER;  i++)
+                {
+                    rci[i + rci_dim1 + 1] = s->rco[i];
+                    rci[i + (rci_dim1 << 1) + 1] = s->rco[i];
+                    s->rco[i] = rc[i];
+                }
+                slope = 0.0f;
+                *nout = 2;
+                s->ipito = *pitch;
+                jused = nl;
+                istart = nl + 1;
+                ivoice = 1;
+            }
+            else
+            {
+                if (s->ivoico != voice[0])
+                {
+                    /* VC2UV1 - -   1  ,  0  ,  0 */
+                    lsamp = LPC10_SAMPLES_PER_FRAME/4 + s->jsamp;
+                }
+                else
+                {
+                    /* VC2UV2 - -   1  ,  1  ,  0 */
+                    lsamp = LPC10_SAMPLES_PER_FRAME*3/4 + s->jsamp;
+                }
+                for (i = 0;  i < LPC10_ORDER;  i++)
+                {
+                    yarc[i] = rc[i];
+                    rc[i] = s->rco[i];
+                }
+                ivoice = 1;
+                slope = 0.0f;
+                vflag = 1;
+            }
+        }
+        /* Here is the value of most variables that are used below, depending on */
+        /* the values of IVOICO, VOICE(1), and VOICE(2).  VOICE(1) and VOICE(2) */
+        /* are input arguments, and IVOICO is the value of VOICE(2) on the */
+        /* previous call (see notes for the IF (NOUT .NE. 0) statement near the */
+        /* end).  Each of these three values is either 0 or 1.  These three */
+        /* values below are given as 3-bit long strings, in the order IVOICO, */
+        /* VOICE(1), and VOICE(2).  It appears that the code above assumes that */
+        /* the bit sequences 010 and 101 never occur, but I wonder whether a */
+        /* large enough number of bit errors in the channel could cause such a */
+        /* thing to happen, and if so, could that cause NOUT to ever go over 11? */
+
+        /* Note that all of the 180 values in the table are floatly LFRAME, but */
+        /* 180 has fewer characters, and it makes the table a little more */
+        /* concrete.  If LFRAME is ever changed, keep this in mind.  Similarly, */
+        /* 135's are 3*LFRAME/4, and 45's are LFRAME/4.  If LFRAME is not a */
+        /* multiple of 4, then the 135 for NL-JSAMP is actually LFRAME-LFRAME/4, */
+        /* and the 45 for NL-JSAMP is actually LFRAME-3*LFRAME/4. */
+
+        /* Note that LSAMP-JSAMP is given as the variable.  This was just for */
+        /* brevity, to avoid adding "+JSAMP" to all of the column entries. */
+        /* Similarly for NL-JSAMP. */
+
+        /* Variable    | 000  001    011,010  111       110       100,101 */
+        /* ------------+-------------------------------------------------- */
+        /* ISTART      | 1    NL+1   NL+1     1         1         1 */
+        /* LSAMP-JSAMP | 180  180    180      180       135       45 */
+        /* IPITO       | 45   PITCH  PITCH    oldPITCH  oldPITCH  oldPITCH */
+        /* SLOPE       | 0    0      0        seebelow  0         0 */
+        /* JUSED       | 0    NL     NL       0         0         0 */
+        /* PITCH       | 45   PITCH  PITCH    PITCH     PITCH     PITCH */
+        /* NL-JSAMP    | --   135    45       --        --        -- */
+        /* VFLAG       | 0    0      0        0         1         1 */
+        /* NOUT        | 0    2      2        0         0         0 */
+        /* IVOICE      | 0    1      1        1         1         1 */
+
+        /* while_loop  | once once   once     once      twice     twice */
+
+        /* ISTART      | --   --     --       --        JUSED+1   JUSED+1 */
+        /* LSAMP-JSAMP | --   --     --       --        180       180 */
+        /* IPITO       | --   --     --       --        oldPITCH  oldPITCH */
+        /* SLOPE       | --   --     --       --        0         0 */
+        /* JUSED       | --   --     --       --        ??        ?? */
+        /* PITCH       | --   --     --       --        PITCH     PITCH */
+        /* NL-JSAMP    | --   --     --       --        --        -- */
+        /* VFLAG       | --   --     --       --        0         0 */
+        /* NOUT        | --   --     --       --        ??        ?? */
+        /* IVOICE      | --   --     --       --        0         0 */
+
+        /* UVPIT is always 0.0 on the first pass through the DO WHILE (TRUE)
+           loop below. */
+
+        /* The only possible non-0 value of SLOPE (in column 111) is
+           (PITCH-IPITO)/FLOAT(LSAMP) */
+
+        /* Column 101 is identical to 100.  Any good properties we can prove
+           for 100 will also hold for 101.  Similarly for 010 and 011. */
+
+        /* synths() calls this subroutine with PITCH restricted to the range 20 to
+           156.  IPITO is similarly restricted to this range, after the first
+           call.  IP below is also restricted to this range, given the
+           definitions of IPITO, SLOPE, UVPIT, and that I is in the range ISTART
+           to LSAMP. */
+
+        for (;;)
+        {
+            for (i = istart;  i <= lsamp;  i++)
+            {
+                r1 = s->ipito + slope*i;
+                ip = (int32_t) (r1 + 0.5f);
+                if (uvpit != 0.0f)
+                    ip = (int32_t) uvpit;
+                if (ip <= i - jused)
+                {
+                    ++(*nout);
+                    ipiti[*nout - 1] = ip;
+                    *pitch = ip;
+                    ivuv[*nout - 1] = ivoice;
+                    jused += ip;
+                    prop = (jused - ip/2)/(float) lsamp;
+                    for (j = 0;  j < LPC10_ORDER;  j++)
+                    {
+                        alro = logf((s->rco[j] + 1)/(1 - s->rco[j]));
+                        alrn = logf((rc[j] + 1)/(1 - rc[j]));
+                        xxy = alro + prop*(alrn - alro);
+                        xxy = expf(xxy);
+                        rci[j + *nout*rci_dim1 + 1] = (xxy - 1.0f)/(xxy + 1.0f);
+                    }
+                    rmsi[*nout - 1] = logf(s->rmso) + prop*(logf(*rms) - logf(s->rmso));
+                    rmsi[*nout - 1] = expf(rmsi[*nout - 1]);
+                }
+            }
+            if (vflag != 1)
+                break;
+
+            vflag = 0;
+            istart = jused + 1;
+            lsamp = LPC10_SAMPLES_PER_FRAME + s->jsamp;
+            slope = 0.0f;
+            ivoice = 0;
+            uvpit = (float) ((lsamp - istart)/2);
+            if (uvpit > 90.0f)
+                uvpit /= 2;
+            s->rmso = *rms;
+            for (i = 0;  i < LPC10_ORDER;  i++)
+            {
+                rc[i] = yarc[i];
+                s->rco[i] = yarc[i];
+            }
+        }
+        s->jsamp = lsamp - jused;
+    }
+    if (*nout != 0)
+    {
+        s->ivoico = voice[1];
+        s->ipito = *pitch;
+        s->rmso = *rms;
+        for (i = 0;  i < LPC10_ORDER;  i++)
+            s->rco[i] = rc[i];
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void deemp(lpc10_decode_state_t *s, float x[], int len)
+{
+    int i;
+    float r1;
+    float dei0;
+
+    for (i = 0;  i < len;  i++)
+    {
+        dei0 = x[i];
+        r1 = x[i] - s->dei[0]*1.9998f + s->dei[1];
+        x[i] = r1 + s->deo[0]*2.5f - s->deo[1]*2.0925f + s->deo[2]*0.585f;
+        s->dei[1] = s->dei[0];
+        s->dei[0] = dei0;
+        s->deo[2] = s->deo[1];
+        s->deo[1] = s->deo[0];
+        s->deo[0] = x[i];
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+/* Convert reflection coefficients to predictor coefficients */
+static float reflection_coeffs_to_predictor_coeffs(float rc[], float pc[], float gprime)
+{
+    float temp[10];
+    float g2pass;
+    int i;
+    int j;
+
+    g2pass = 1.0f;
+    for (i = 0;  i < LPC10_ORDER;  i++)
+        g2pass *= 1.0f - rc[i]*rc[i];
+    g2pass = gprime*sqrtf(g2pass);
+    pc[0] = rc[0];
+    for (i = 1;  i < LPC10_ORDER;  i++)
+    {
+        for (j = 0;  j < i;  j++)
+            temp[j] = pc[j] - rc[i]*pc[i - j - 1];
+        for (j = 0;  j < i;  j++)
+            pc[j] = temp[j];
+        pc[i] = rc[i];
+    }
+    return g2pass;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int synths(lpc10_decode_state_t *s,
+                  int voice[],
+                  int32_t *pitch,
+                  float *rms,
+                  float *rc,
+                  float speech[])
+{
+    int32_t i1;
+    int32_t ivuv[16];
+    int32_t ipiti[16];
+    int32_t nout;
+    int32_t i;
+    int32_t j;
+    float rmsi[16];
+    float ratio;
+    float g2pass;
+    float pc[10];
+    float rci[160];
+
+    i1 = min(*pitch, 156);
+    *pitch = max(i1, 20);
+    for (i = 0;  i < LPC10_ORDER;  i++)
+        rc[i] = max(min(rc[i], 0.99f), -0.99f);
+    pitsyn(s, voice, pitch, rms, rc, ivuv, ipiti, rmsi, rci, &nout, &ratio);
+    if (nout > 0)
+    {
+        for (j = 0;  j < nout;  j++)
+        {
+            /* Add synthesized speech for pitch period J to the end of s->buf. */
+            g2pass = reflection_coeffs_to_predictor_coeffs(&rci[j*10], pc, 0.7f);
+            bsynz(s, pc, ipiti[j], &ivuv[j], &s->buf[s->buflen], rmsi[j], ratio, g2pass);
+            deemp(s, &s->buf[s->buflen], ipiti[j]);
+            s->buflen += ipiti[j];
+        }
+        /* Copy first MAXFRM samples from BUF to output array speech (scaling them),
+           and then remove them from the beginning of s->buf. */
+
+        for (i = 0;  i < LPC10_SAMPLES_PER_FRAME;  i++)
+            speech[i] = s->buf[i]/4096.0f;
+        s->buflen -= LPC10_SAMPLES_PER_FRAME;
+        for (i = 0;  i < s->buflen;  i++)
+            s->buf[i] = s->buf[i + LPC10_SAMPLES_PER_FRAME];
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void lpc10_unpack(lpc10_frame_t *t, const uint8_t ibits[])
+{
+    static const int bit[10] =
+    {
+        2, 4, 8, 8, 8, 8, 16, 16, 16, 16
+    };
+    static const int iblist[53] =
+    {
+        13, 12, 11,  1,  2, 13, 12, 11,  1,  2,
+        13, 10, 11,  2,  1, 10, 13, 12, 11, 10,
+         2, 13, 12, 11, 10,  2,  1, 12,  7,  6,
+         1, 10,  9,  8,  7,  4,  6,  9,  8,  7,
+         5,  1,  9,  8,  4,  6,  1,  5,  9,  8,
+         7,  5,  6
+    };
+    int32_t itab[13];
+    int x;
+    int i;
+
+    /* ibits is 54 bits of LPC data ordered as follows: */
+    /*     R1-0, R2-0, R3-0,  P-0,  A-0, */
+    /*     R1-1, R2-1, R3-1,  P-1,  A-1, */
+    /*     R1-2, R4-0, R3-2,  A-2,  P-2, R4-1, */
+    /*     R1-3, R2-2, R3-3, R4-2,  A-3, */
+    /*     R1-4, R2-3, R3-4, R4-3,  A-4, */
+    /*      P-3, R2-4, R7-0, R8-0,  P-4, R4-4, */
+    /*     R5-0, R6-0, R7-1,R10-0, R8-1, */
+    /*     R5-1, R6-1, R7-2, R9-0,  P-5, */
+    /*     R5-2, R6-2,R10-1, R8-2,  P-6, R9-1, */
+    /*     R5-3, R6-3, R7-3, R9-2, R8-3, SYNC */
+
+    /* Reconstruct ITAB */
+    for (i = 0;  i < 13;  i++)
+        itab[i] = 0;
+    for (i = 0;  i < 53;  i++)
+    {
+        x = 52 - i;
+        x = (ibits[x >> 3] >> (7 - (x & 7))) & 1;
+        itab[iblist[52 - i] - 1] = (itab[iblist[52 - i] - 1] << 1) | x;
+    }
+    /* Sign extend the RC's */
+    for (i = 0;  i < LPC10_ORDER;  i++)
+    {
+        if ((itab[i + 3] & bit[i]))
+            itab[i + 3] -= (bit[i] << 1);
+    }
+    /* Restore variables */
+    t->ipitch = itab[0];
+    t->irms = itab[1];
+    for (i = 0;  i < LPC10_ORDER;  i++)
+        t->irc[i] = itab[LPC10_ORDER - 1 - i + 3];
+}
+/*- End of function --------------------------------------------------------*/
+
+/* Hamming 8, 4 decoder - can correct 1 out of seven bits
+   and can detect up to two errors. */
+
+/* This subroutine is entered with an eight bit word in INPUT.  The 8th */
+/* bit is parity and is stripped off.  The remaining 7 bits address the */
+/* hamming 8, 4 table and the output OUTPUT from the table gives the 4 */
+/* bits of corrected data.  If bit 4 is set,  no error was detected. */
+/* ERRCNT is the number of errors counted. */
+
+static int32_t hamming_84_decode(int32_t input, int *errcnt)
+{
+    static const uint8_t dactab[128] =
+    {
+        16,  0,  0,  3,  0,  5, 14,  7,  0,  9, 14, 11, 14, 13, 30, 14,
+         0,  9,  2,  7,  4,  7,  7, 23,  9, 25, 10,  9, 12,  9, 14,  7,
+         0,  5,  2, 11,  5, 21,  6,  5,  8, 11, 11, 27, 12,  5, 14, 11,
+         2,  1, 18,  2, 12,  5,  2,  7, 12,  9,  2, 11, 28, 12, 12, 15,
+         0,  3,  3, 19,  4, 13,  6,  3,  8, 13, 10,  3, 13, 29, 14, 13,
+         4,  1, 10,  3, 20,  4,  4,  7, 10,  9, 26, 10,  4, 13, 10, 15,
+         8,  1,  6,  3,  6,  5, 22,  6, 24,  8,  8, 11,  8, 13,  6, 15,
+         1, 17 , 2,  1,  4,  1,  6, 15,  8,  1, 10, 15, 12, 15, 15, 31
+    };
+    int i;
+    int parity;
+    int32_t output;
+
+    parity = input & 255;
+    parity ^= parity >> 4;
+    parity ^= parity >> 2;
+    parity ^= parity >> 1;
+    parity &= 1;
+    i = dactab[input & 127];
+    output = i & 15;
+    if ((i & 16))
+    {
+        /* No errors detected in seven bits */
+        if (parity)
+            (*errcnt)++;
+    }
+    else
+    {
+        /* One or two errors detected */
+        (*errcnt)++;
+        if (parity == 0)
+        {
+            /* Two errors detected */
+            (*errcnt)++;
+            output = -1;
+        }
+    }
+    return output;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int32_t median(int32_t d1, int32_t d2, int32_t d3)
+{
+    int32_t ret_val;
+
+    ret_val = d2;
+    if (d2 > d1  &&  d2 > d3)
+    {
+        ret_val = d1;
+        if (d3 > d1)
+            ret_val = d3;
+    }
+    else if (d2 < d1  &&  d2 < d3)
+    {
+        ret_val = d1;
+        if (d3 < d1)
+            ret_val = d3;
+    }
+    return ret_val;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void decode(lpc10_decode_state_t *s,
+                   lpc10_frame_t *t,
+                   int voice[],
+                   int32_t *pitch,
+                   float *rms,
+                   float rc[])
+{
+    static const int32_t ivtab[32] =
+    {
+        24960, 24960, 24960, 24960, 25480, 25480, 25483, 25480,
+        16640,  1560,  1560,  1560, 16640,  1816,  1563,  1560,
+        24960, 24960, 24859, 24856, 26001, 25881, 25915, 25913,
+         1560,  1560,  7800,  3640,  1561,  1561,  3643,  3641
+    };
+    static const float corth[32] =
+    {
+        32767.0f, 10.0f, 5.0f, 0.0f, 32767.0f,  8.0f, 4.0f, 0.0f,
+           32.0f,  6.4f, 3.2f, 0.0f,    32.0f,  6.4f, 3.2f, 0.0f,
+           32.0f, 11.2f, 6.4f, 0.0f,    32.0f, 11.2f, 6.4f, 0.0f,
+           16.0f,  5.6f, 3.2f, 0.0f,    16.0f,  5.6f, 3.2f, 0.0f
+    };
+    static const int32_t detau[128] =
+    {
+          0,   0,   0,   3,   0,   3,   3,  31,
+          0,   3,   3,  21,   3,   3,  29,  30,
+          0,   3,   3,  20,   3,  25,  27,  26,
+          3,  23,  58,  22,   3,  24,  28,   3,
+          0,   3,   3,   3,   3,  39,  33,  32,
+          3,  37,  35,  36,   3,  38,  34,   3,
+          3,  42,  46,  44,  50,  40,  48,   3,
+         54,   3,  56,   3,  52,   3,   3,   1,
+          0,   3,   3, 108,   3,  78, 100, 104,
+          3,  84,  92,  88, 156,  80,  96,   3,
+          3,  74,  70,  72,  66,  76,  68,   3,
+         62,   3,  60,   3,  64,   3,   3,   1,
+          3, 116, 132, 112, 148, 152,   3,   3,
+        140,   3, 136,   3, 144,   3,   3,   1,
+        124, 120, 128,   3,   3,   3,   3,   1,
+          3,   3,   3,   1,   3,   1,   1,   1
+    };
+    static const int32_t rmst[64] =
+    {
+        1024,  936,  856,  784,  718,  656,  600,  550,
+         502,  460,  420,  384,  352,  328,  294,  270,
+         246,  226,  206,  188,  172,  158,  144,  132,
+         120,  110,  102,   92,   84,   78,   70,   64,
+          60,   54,   50,   46,   42,   38,   34,   32,
+          30,   26,   24,   22,   20,   18,   17,   16,
+          15,   14,   13,   12,   11,   10,    9,    8,
+           7,    6,    5,    4,    3,    2,    1,    0
+    };
+    static const int32_t detab7[32] =
+    {
+          4,  11,  18,  25,  32,  39,  46,  53,
+         60,  66,  72,  77,  82,  87,  92,  96,
+        101, 104, 108, 111, 114, 115, 117, 119,
+        121, 122, 123, 124, 125, 126, 127, 127
+    };
+    static const float descl[8] =
+    {
+        0.6953f, 0.625f, 0.5781f, 0.5469f, 0.5312f, 0.5391f, 0.4688f, 0.3828f
+    };
+    static const int32_t deadd[8] =
+    {
+        1152, -2816, -1536, -3584, -1280, -2432, 768, -1920
+    };
+    static const int32_t qb[8] =
+    {
+        511, 511, 1023, 1023, 1023, 1023, 2047, 4095
+    };
+    static const int32_t nbit[10] =
+    {
+        8, 8, 5, 5, 4, 4, 4, 4, 3, 2
+    };
+    static const int32_t zrc[10] =
+    {
+        0, 0, 0, 0, 0, 3, 0, 2, 0, 0
+    };
+    static const int32_t bit[5] =
+    {
+        2, 4, 8, 16, 32
+    };
+    int32_t ipit;
+    int32_t iout;
+    int32_t i;
+    int32_t icorf;
+    int32_t index;
+    int32_t ivoic;
+    int32_t ixcor;
+    int32_t i1;
+    int32_t i2;
+    int32_t i4;
+    int32_t ishift;
+    int32_t lsb;
+    int errcnt;
+
+    /* If no error correction, do pitch and voicing then jump to decode */
+    i4 = detau[t->ipitch];
+    if (!s->error_correction)
+    {
+        voice[0] = 1;
+        voice[1] = 1;
+        if (t->ipitch <= 1)
+            voice[0] = 0;
+        if (t->ipitch == 0  ||  t->ipitch == 2)
+            voice[1] = 0;
+        if (i4 <= 4)
+            i4 = s->iptold;
+        *pitch = i4;
+        if (voice[0] == 1  &&  voice[1] == 1)
+            s->iptold = *pitch;
+        if (voice[0] != voice[1])
+            *pitch = s->iptold;
+    }
+    else
+    {
+        /* Do error correction pitch and voicing */
+        if (i4 > 4)
+        {
+            s->dpit[0] = i4;
+            ivoic = 2;
+            s->iavgp = (s->iavgp*15 + i4 + 8)/16;
+        }
+        else
+        {
+            s->dpit[0] = s->iavgp;
+            ivoic = i4;
+        }
+        s->drms[0] = t->irms;
+        for (i = 0;  i < LPC10_ORDER;  i++)
+            s->drc[i][0] = t->irc[i];
+        /* Determine index to IVTAB from V/UV decision */
+        /* If error rate is high then use alternate table */
+        index = (s->ivp2h << 4) + (s->iovoic << 2) + ivoic + 1;
+        i1 = ivtab[index - 1];
+        ipit = i1 & 3;
+        icorf = i1 >> 3;
+        if (s->erate < 2048)
+            icorf /= 64;
+        /* Determine error rate:  4=high    1=low */
+        ixcor = 4;
+        if (s->erate < 2048)
+            ixcor = 3;
+        if (s->erate < 1024)
+            ixcor = 2;
+        if (s->erate < 128)
+            ixcor = 1;
+        /* Voice/unvoice decision determined from bits 0 and 1 of IVTAB */
+        voice[0] = icorf/2 & 1;
+        voice[1] = icorf & 1;
+        /* Skip decoding on first frame because present data not yet available */
+        if (s->first)
+        {
+            s->first = FALSE;
+            /* Assign PITCH a "default" value on the first call, since */
+            /* otherwise it would be left uninitialized.  The two lines */
+            /* below were copied from above, since it seemed like a */
+            /* reasonable thing to do for the first call. */
+            if (i4 <= 4)
+                i4 = s->iptold;
+            *pitch = i4;
+        }
+        else
+        {
+            /* If bit 4 of ICORF is set then correct RMS and RC(1) - RC(4). */
+            /* Determine error rate and correct errors using a Hamming 8,4 code */
+            /* during transition of unvoiced frames.  If IOUT is negative, */
+            /* more than 1 error occurred, use previous frame's parameters. */
+            if ((icorf & bit[3]) != 0)
+            {
+                errcnt = 0;
+                lsb = s->drms[1] & 1;
+                index = (s->drc[7][1] << 4) + s->drms[1]/2;
+                iout = hamming_84_decode(index, &errcnt);
+                s->drms[1] = s->drms[2];
+                if (iout >= 0)
+                    s->drms[1] = (iout << 1) + lsb;
+                for (i = 1;  i <= 4;  i++)
+                {
+                    if (i == 1)
+                        i1 = ((s->drc[8][1] & 7) << 1) + (s->drc[9][1] & 1);
+                    else
+                        i1 = s->drc[8 - i][1] & 15;
+                    i2 = s->drc[4 - i][1] & 31;
+                    lsb = i2 & 1;
+                    index = (i1 << 4) + (i2 >> 1);
+                    iout = hamming_84_decode(index, &errcnt);
+                    if (iout >= 0)
+                    {
+                        iout = (iout << 1) + lsb;
+                        if ((iout & 16) == 16)
+                            iout -= 32;
+                    }
+                    else
+                    {
+                        iout = s->drc[4 - i][2];
+                    }
+                    s->drc[4 - i][1] = iout;
+                }
+                /* Determine error rate */
+                s->erate = (int32_t) (s->erate*0.96875f + errcnt*102.0f);
+            }
+            /* Get unsmoothed RMS, RC's, and PITCH */
+            t->irms = s->drms[1];
+            for (i = 0;  i < LPC10_ORDER;  i++)
+                t->irc[i] = s->drc[i][1];
+            if (ipit == 1)
+                s->dpit[1] = s->dpit[2];
+            if (ipit == 3)
+                s->dpit[1] = s->dpit[0];
+            *pitch = s->dpit[1];
+            /* If bit 2 of ICORF is set then smooth RMS and RC's, */
+            if ((icorf & bit[1]) != 0)
+            {
+                if ((float) abs(s->drms[1] - s->drms[0]) >= corth[ixcor + 3] 
+                    &&
+                    (float) abs(s->drms[1] - s->drms[2]) >= corth[ixcor + 3])
+                {
+                    t->irms = median(s->drms[2], s->drms[1], s->drms[0]);
+                }
+                for (i = 0;  i < 6;  i++)
+                {
+                    if ((float) abs(s->drc[i][1] - s->drc[i][0]) >= corth[ixcor + ((i + 3) << 2) - 5]
+                        &&
+                        (float) abs(s->drc[i][1] - s->drc[i][2]) >= corth[ixcor + ((i + 3) << 2) - 5])
+                    {
+                        t->irc[i] = median(s->drc[i][2], s->drc[i][1], s->drc[i][0]);
+                    }
+                }
+            }
+            /* If bit 3 of ICORF is set then smooth pitch */
+            if ((icorf & bit[2]) != 0)
+            {
+                if ((float) abs(s->dpit[1] - s->dpit[0]) >= corth[ixcor - 1] 
+                    &&
+                    (float) abs(s->dpit[1] - s->dpit[2]) >= corth[ixcor - 1])
+                {
+                    *pitch = median(s->dpit[2], s->dpit[1], s->dpit[0]);
+                }
+            }
+            /* If bit 5 of ICORF is set then RC(5) - RC(10) are loaded with
+               values so that after quantization bias is removed in decode
+               the values will be zero. */
+        }
+        if ((icorf & bit[4]) != 0)
+        {
+            for (i = 4;  i < LPC10_ORDER;  i++)
+                t->irc[i] = zrc[i];
+        }
+        /* Housekeeping  - one frame delay */
+        s->iovoic = ivoic;
+        s->ivp2h = voice[1];
+        s->dpit[2] = s->dpit[1];
+        s->dpit[1] = s->dpit[0];
+        s->drms[2] = s->drms[1];
+        s->drms[1] = s->drms[0];
+        for (i = 0;  i < LPC10_ORDER;  i++)
+        {
+            s->drc[i][2] = s->drc[i][1];
+            s->drc[i][1] = s->drc[i][0];
+        }
+    }
+    /* Decode RMS */
+    t->irms = rmst[(31 - t->irms)*2];
+    /* Decode RC(1) and RC(2) from log-area-ratios */
+    /* Protect from illegal coded value (-16) caused by bit errors */
+    for (i = 0;  i < 2;  i++)
+    {
+        i2 = t->irc[i];
+        i1 = 0;
+        if (i2 < 0)
+        {
+            i1 = 1;
+            i2 = -i2;
+            if (i2 > 15)
+                i2 = 0;
+        }
+        i2 = detab7[i2*2];
+        if (i1 == 1)
+            i2 = -i2;
+        ishift = 15 - nbit[i];
+        t->irc[i] = i2*pow_ii(2, ishift);
+    }
+    /* Decode RC(3)-RC(10) to sign plus 14 bits */
+    for (i = 2;  i < LPC10_ORDER;  i++)
+    {
+        ishift = 15 - nbit[i];
+        i2 = t->irc[i]*pow_ii(2, ishift) + qb[i - 2];
+        t->irc[i] = (int32_t) (i2*descl[i - 2] + deadd[i - 2]);
+    }
+    /* Scale RMS and RC's to floats */
+    *rms = (float) t->irms;
+    for (i = 0;  i < LPC10_ORDER;  i++)
+        rc[i] = t->irc[i]/16384.0f;
+}
+/*- End of function --------------------------------------------------------*/
+
+lpc10_decode_state_t *lpc10_decode_init(lpc10_decode_state_t *s, int error_correction)
+{
+    static const int16_t rand_init[] =
+    {
+        -21161,
+         -8478,
+         30892,
+        -10216,
+         16950
+    };
+    int i;
+    int j;
+
+    if (s == NULL)
+    {
+        if ((s = (lpc10_decode_state_t *) malloc(sizeof(*s))) == NULL)
+            return NULL;
+    }
+
+    s->error_correction = error_correction;
+
+    /* State used by function decode */
+    s->iptold = 60;
+    s->first = TRUE;
+    s->ivp2h = 0;
+    s->iovoic = 0;
+    s->iavgp = 60;
+    s->erate = 0;
+    for (i = 0;  i < 3;  i++)
+    {
+        for (j = 0;  j < 10;  j++)
+            s->drc[j][i] = 0;
+        s->dpit[i] = 0;
+        s->drms[i] = 0;
+    }
+
+    /* State used by function synths */
+    for (i = 0;  i < 360;  i++)
+        s->buf[i] = 0.0f;
+    s->buflen = LPC10_SAMPLES_PER_FRAME;
+
+    /* State used by function pitsyn */
+    s->rmso = 1.0f;
+    s->first_pitsyn = TRUE;
+
+    /* State used by function bsynz */
+    s->ipo = 0;
+    for (i = 0;  i < 166;  i++)
+    {
+        s->exc[i] = 0.0f;
+        s->exc2[i] = 0.0f;
+    }
+    for (i = 0;  i < 3;  i++)
+    {
+        s->lpi[i] = 0.0f;
+        s->hpi[i] = 0.0f;
+    }
+    s->rmso_bsynz = 0.0f;
+
+    /* State used by function lpc10_random */
+    s->j = 1;
+    s->k = 4;
+    for (i = 0;  i < 5;  i++)
+        s->y[i] = rand_init[i];
+
+    /* State used by function deemp */
+    for (i = 0;  i < 2;  i++)
+        s->dei[i] = 0.0f;
+    for (i = 0;  i < 3;  i++)
+        s->deo[i] = 0.0f;
+    
+    return s;
+}
+/*- End of function --------------------------------------------------------*/
+
+int lpc10_decode_release(lpc10_decode_state_t *s)
+{
+    free(s);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int lpc10_decode(lpc10_decode_state_t *s, int16_t amp[], const uint8_t code[], int len)
+{
+    int voice[2];
+    int32_t pitch;
+    float speech[LPC10_SAMPLES_PER_FRAME];
+    float rc[LPC10_ORDER];
+    lpc10_frame_t frame;
+    float rms;
+    int i;
+    int j;
+    int base;
+
+    /* Decode 54 bits in 7 bytes to LPC10_SAMPLES_PER_FRAME speech samples. */
+    len /= 7;
+    for (i = 0;  i < len;  i++)
+    {
+        lpc10_unpack(&frame, &code[i*7]);
+        decode(s, &frame, voice, &pitch, &rms, rc);
+        synths(s, voice, &pitch, &rms, rc, speech);
+        base = i*LPC10_SAMPLES_PER_FRAME;
+        for (j = 0;  j < LPC10_SAMPLES_PER_FRAME;  j++)
+            amp[base + j] = (int16_t) lrintf(32768.0f*speech[j]);
+    }
+
+    return len*LPC10_SAMPLES_PER_FRAME;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/lpc10_encdecs.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/lpc10_encdecs.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,107 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * lpc10_encdecs.h - LPC10 low bit rate speech codec.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2006 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: lpc10_encdecs.h,v 1.11 2008/04/17 14:26:56 steveu Exp $
+ */
+
+#define LPC10_ORDER     10
+
+#if !defined(min)
+#define min(a,b) ((a) <= (b) ? (a) : (b))
+#endif
+#if !defined(max)
+#define max(a,b) ((a) >= (b) ? (a) : (b))
+#endif
+
+void lpc10_placea(int32_t *ipitch,
+                  int32_t voibuf[4][2],
+                  int32_t *obound,
+                  int32_t af,
+                  int32_t vwin[3][2],
+                  int32_t awin[3][2],
+                  int32_t ewin[3][2], 
+                  int32_t lframe,
+                  int32_t maxwin);
+
+void lpc10_placev(int32_t *osbuf,
+                  int32_t *osptr,
+                  int32_t oslen, 
+                  int32_t *obound,
+                  int32_t vwin[3][2],
+                  int32_t af,
+                  int32_t lframe,
+                  int32_t minwin,
+                  int32_t maxwin,
+                  int32_t dvwinl,
+                  int32_t dvwinh);
+
+void lpc10_voicing(lpc10_encode_state_t *st,
+                   int32_t *vwin,
+                   float *inbuf,
+                   float *lpbuf,
+                   const int32_t buflim[],
+                   int32_t half,
+                   float *minamd,
+                   float *maxamd, 
+	               int32_t *mintau,
+                   float *ivrc,
+                   int32_t *obound);
+
+void lpc10_analyse(lpc10_encode_state_t *st, float *speech, int32_t *voice, int32_t *pitch, float *rms, float rc[]);
+
+static __inline__ int32_t pow_ii(int32_t x, int32_t n)
+{
+    int32_t pow;
+    uint32_t u;
+
+    if (n <= 0)
+    {
+        if (n == 0  ||  x == 1)
+            return 1;
+        if (x != -1)
+            return (x == 0)  ?  1/x  :  0;
+        n = -n;
+    }
+    u = n;
+    for (pow = 1;  ;  )
+    {
+        if ((u & 1))
+            pow *= x;
+        if ((u >>= 1) == 0)
+            break;
+        x *= x;
+    }
+    return pow;
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ float r_sign(float a, float b)
+{
+    float x;
+
+    x = fabsf(a);
+    return (b >= 0.0f)  ?  x  :  -x;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/lpc10_encode.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/lpc10_encode.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,396 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * lpc10_encode.c - LPC10 low bit rate speech codec.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2006 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * This code is based on the U.S. Department of Defense reference
+ * implementation of the LPC-10 2400 bps Voice Coder. They do not
+ * exert copyright claims on their code, and it may be freely used.
+ *
+ * $Id: lpc10_encode.c,v 1.23 2008/07/02 14:48:25 steveu Exp $
+ */
+
+#if defined(HAVE_CONFIG_H)
+#include <config.h>
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <inttypes.h>
+#include <memory.h>
+#include "floating_fudge.h"
+#if defined(HAVE_TGMATH_H)
+#include <tgmath.h>
+#endif
+#if defined(HAVE_MATH_H)
+#include <math.h>
+#endif
+
+#include "spandsp/telephony.h"
+#include "spandsp/dc_restore.h"
+#include "spandsp/lpc10.h"
+
+#include "lpc10_encdecs.h"
+
+static void lpc10_pack(lpc10_encode_state_t *s, uint8_t ibits[], lpc10_frame_t *t)
+{
+    static const int iblist[53] =
+    {
+        13, 12, 11,  1,  2, 13, 12, 11,  1,  2,
+        13, 10, 11,  2,  1, 10, 13, 12, 11, 10,
+         2, 13, 12, 11, 10,  2,  1, 12,  7,  6,
+         1, 10,  9,  8,  7,  4,  6,  9,  8,  7,
+         5,  1,  9,  8,  4,  6,  1,  5,  9,  8,
+         7,  5,  6
+    };
+    int32_t itab[13];
+    int x;
+    int i;
+
+    /* ibits is 54 bits of LPC data ordered as follows: */
+    /*     R1-0, R2-0, R3-0,  P-0,  A-0, */
+    /*     R1-1, R2-1, R3-1,  P-1,  A-1, */
+    /*     R1-2, R4-0, R3-2,  A-2,  P-2, R4-1, */
+    /*     R1-3, R2-2, R3-3, R4-2,  A-3, */
+    /*     R1-4, R2-3, R3-4, R4-3,  A-4, */
+    /*      P-3, R2-4, R7-0, R8-0,  P-4, R4-4, */
+    /*     R5-0, R6-0, R7-1,R10-0, R8-1, */
+    /*     R5-1, R6-1, R7-2, R9-0,  P-5, */
+    /*     R5-2, R6-2,R10-1, R8-2,  P-6, R9-1, */
+    /*     R5-3, R6-3, R7-3, R9-2, R8-3, SYNC */
+
+    itab[0] = t->ipitch;
+    itab[1] = t->irms;
+    itab[2] = 0;
+    for (i = 0;  i < LPC10_ORDER;  i++)
+        itab[i + 3] = t->irc[LPC10_ORDER - 1 - i] & 0x7FFF;
+    /* Put 54 bits into the output buffer */
+    x = 0;
+    for (i = 0;  i < 53;  i++)
+    {
+        x = (x << 1) | (itab[iblist[i] - 1] & 1);
+        if ((i & 7) == 7)
+            ibits[i >> 3] = (uint8_t) (x & 0xFF);
+        itab[iblist[i] - 1] >>= 1;
+    }
+    x = (x << 1) | (s->isync & 1);
+    s->isync ^= 1;
+    x <<= 2;
+    ibits[6] = (uint8_t) (x & 0xFF);
+}
+/*- End of function --------------------------------------------------------*/
+
+/* Quantize LPC parameters for transmission */
+static int encode(lpc10_encode_state_t *s,
+                  lpc10_frame_t *t,
+                  int32_t *voice,
+                  int32_t pitch,
+                  float rms,
+                  float *rc)
+{
+    static const int32_t enctab[16] =
+    {
+        0, 7, 11, 12, 13, 10, 6, 1, 14, 9, 5, 2, 3, 4, 8, 15
+    };
+    static const int32_t entau[60] =
+    {
+        19, 11, 27, 25, 29, 21, 23, 22, 30, 14, 15,  7, 39, 38, 46, 
+        42, 43, 41, 45, 37, 53, 49, 51, 50, 54, 52, 60, 56, 58, 26,
+        90, 88, 92, 84, 86, 82, 83, 81, 85, 69, 77, 73, 75, 74, 78,
+        70, 71, 67, 99, 97, 113, 112, 114, 98, 106, 104, 108, 100,
+        101, 76
+    };
+    static const int32_t enadd[8] =
+    {
+        1920, -768, 2432, 1280, 3584, 1536, 2816, -1152
+    };
+    static const float enscl[8] =
+    {
+        0.0204f, 0.0167f, 0.0145f, 0.0147f, 0.0143f, 0.0135f, 0.0125f, 0.0112f
+    };
+    static const int32_t enbits[8] =
+    {
+        6, 5, 4, 4, 4, 4, 3, 3
+    };
+    static const int32_t entab6[64] =
+    {
+        0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 3, 3,
+        3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6,
+        7, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 10, 10, 11, 11, 12, 13, 14, 15
+    };
+    static const int32_t rmst[64] =
+    {
+        1024, 936, 856, 784, 718, 656, 600, 550, 502,
+        460, 420, 384, 352, 328, 294, 270, 246, 226,
+        206, 188, 172, 158, 144, 132, 120, 110, 102, 
+        92, 84, 78, 70, 64, 60, 54, 50,
+        46, 42, 38, 34, 32, 30, 26, 24,
+        22, 20, 18, 17, 16, 15, 14, 13,
+        12, 11, 10, 9, 8, 7, 6, 5, 4,
+        3, 2, 1, 0
+    };
+
+    int32_t idel;
+    int32_t nbit;
+    int32_t i;
+    int32_t j;
+    int32_t i2;
+    int32_t i3;
+    int32_t mrk;
+
+    /* Scale RMS and RC's to int32_ts */
+    t->irms = (int32_t) rms;
+    for (i = 0;  i < LPC10_ORDER;  i++)
+        t->irc[i] = (int32_t) (rc[i]*32768.0f);
+    if (voice[0] != 0  &&  voice[1] != 0)
+    {
+        t->ipitch = entau[pitch - 1];
+    }
+    else
+    {
+        if (s->error_correction)
+        {
+            t->ipitch = 0;
+            if (voice[0] != voice[1])
+                t->ipitch = 127;
+        }
+        else
+        {
+            t->ipitch = (voice[0] << 1) + voice[1];
+        }
+    }
+    /* Encode RMS by binary table search */
+    j = 32;
+    idel = 16;
+    t->irms = min(t->irms, 1023);
+    while (idel > 0)
+    {
+        if (t->irms > rmst[j - 1])
+            j -= idel;
+        if (t->irms < rmst[j - 1])
+            j += idel;
+        idel /= 2;
+    }
+    if (t->irms > rmst[j - 1])
+        --j;
+    t->irms = 31 - j/2;
+    /* Encode RC(1) and (2) as log-area-ratios */
+    for (i = 0;  i < 2;  i++)
+    {
+        i2 = t->irc[i];
+        mrk = 0;
+        if (i2 < 0)
+        {
+            i2 = -i2;
+            mrk = 1;
+        }
+        i2 = min(i2/512, 63);
+        i2 = entab6[i2];
+        if (mrk != 0)
+            i2 = -i2;
+        t->irc[i] = i2;
+    }
+    /* Encode RC(3) - (10) linearly, remove bias then scale */
+    for (i = 2;  i < LPC10_ORDER;  i++)
+    {
+        i2 = (int32_t) ((t->irc[i]/2 + enadd[LPC10_ORDER - 1 - i])*enscl[LPC10_ORDER - 1 - i]);
+        i2 = max(i2, -127);
+        i2 = min(i2, 127);
+        nbit = enbits[LPC10_ORDER - 1 - i];
+        i3 = (i2 < 0);
+        i2 /= pow_ii(2, nbit);
+        if (i3)
+            i2--;
+        t->irc[i] = i2;
+    }
+    /* Protect the most significant bits of the most
+       important parameters during non-voiced frames.
+       RC(1) - RC(4) are protected using 20 parity bits
+       replacing RC(5) - RC(10). */
+    if (s->error_correction)
+    {
+        if (t->ipitch == 0  ||  t->ipitch == 127)
+        {
+            t->irc[4] = enctab[(t->irc[0] & 0x1E) >> 1];
+            t->irc[5] = enctab[(t->irc[1] & 0x1E) >> 1];
+            t->irc[6] = enctab[(t->irc[2] & 0x1E) >> 1];
+            t->irc[7] = enctab[(t->irms & 0x1E) >> 1];
+            t->irc[8] = enctab[(t->irc[3] & 0x1E) >> 1] >> 1;
+            t->irc[9] = enctab[(t->irc[3] & 0x1E) >> 1] & 1;
+        }
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void high_pass_100hz(lpc10_encode_state_t *s, float speech[], int start, int len)
+{
+    float si;
+    float err;
+    int i;
+
+    /* 100 Hz high pass filter */
+    for (i = start;  i < len;  i++)
+    {
+        si = speech[i];
+        err = si + s->z11*1.859076f - s->z21*0.8648249f;
+        si = err - s->z11*2.0f + s->z21;
+        s->z21 = s->z11;
+        s->z11 = err;
+        err = si + s->z12*1.935715f - s->z22*0.9417004f;
+        si = err - s->z12*2.0f + s->z22;
+        s->z22 = s->z12;
+        s->z12 = err;
+        speech[i] = si*0.902428f;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+lpc10_encode_state_t *lpc10_encode_init(lpc10_encode_state_t *s, int error_correction)
+{
+    int i;
+    int j;
+
+    if (s == NULL)
+    {
+        if ((s = (lpc10_encode_state_t *) malloc(sizeof(*s))) == NULL)
+            return NULL;
+    }
+
+    s->error_correction = error_correction;
+
+    /* State used only by function high_pass_100hz */
+    s->z11 = 0.0f;
+    s->z21 = 0.0f;
+    s->z12 = 0.0f;
+    s->z22 = 0.0f;
+    
+    /* State used by function lpc10_analyse */
+    for (i = 0;  i < 540;  i++)
+    {
+        s->inbuf[i] = 0.0f;
+        s->pebuf[i] = 0.0f;
+    }
+    for (i = 0;  i < 696;  i++)
+        s->lpbuf[i] = 0.0f;
+    for (i = 0;  i < 312;  i++)
+        s->ivbuf[i] = 0.0f;
+    s->bias = 0.0f;
+    s->osptr = 1;
+    for (i = 0;  i < 3;  i++)
+        s->obound[i] = 0;
+    s->vwin[2][0] = 307;
+    s->vwin[2][1] = 462;
+    s->awin[2][0] = 307;
+    s->awin[2][1] = 462;
+    for (i = 0;  i < 4;  i++)
+    {
+        s->voibuf[i][0] = 0;
+        s->voibuf[i][1] = 0;
+    }
+    for (i = 0;  i < 3;  i++)
+        s->rmsbuf[i] = 0.0f;
+    for (i = 0;  i < 3;  i++)
+    {
+        for (j = 0;  j < 10;  j++)
+            s->rcbuf[i][j] = 0.0f;
+    }
+    s->zpre = 0.0f;
+
+    /* State used by function onset */
+    s->n = 0.0f;
+    s->d__ = 1.0f;
+    for (i = 0;  i < 16;  i++)
+        s->l2buf[i] = 0.0f;
+    s->l2sum1 = 0.0f;
+    s->l2ptr1 = 1;
+    s->l2ptr2 = 9;
+    s->hyst = FALSE;
+
+    /* State used by function lpc10_voicing */
+    s->dither = 20.0f;
+    s->maxmin = 0.0f;
+    for (i = 0;  i < 3;  i++)
+    {
+        s->voice[i][0] = 0.0f;
+        s->voice[i][1] = 0.0f;
+    }
+    s->lbve = 3000;
+    s->fbve = 3000;
+    s->fbue = 187;
+    s->ofbue = 187;
+    s->sfbue = 187;
+    s->lbue = 93;
+    s->olbue = 93;
+    s->slbue = 93;
+    s->snr = (float) (s->fbve / s->fbue << 6);
+
+    /* State used by function dynamic_pitch_tracking */
+    for (i = 0;  i < 60;  i++)
+        s->s[i] = 0.0f;
+    for (i = 0;  i < 2;  i++)
+    {
+        for (j = 0;  j < 60;  j++)
+            s->p[i][j] = 0;
+    }
+    s->ipoint = 0;
+    s->alphax = 0.0f;
+
+    /* State used by function lpc10_pack */
+    s->isync = 0;
+    
+    return s;
+}
+/*- End of function --------------------------------------------------------*/
+
+int lpc10_encode_release(lpc10_encode_state_t *s)
+{
+    free(s);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int lpc10_encode(lpc10_encode_state_t *s, uint8_t code[], const int16_t amp[], int len)
+{
+    int32_t voice[2];
+    int32_t pitch;
+    float speech[LPC10_SAMPLES_PER_FRAME];
+    float rc[LPC10_ORDER];
+    float rms;
+    lpc10_frame_t frame;
+    int i;
+    int j;
+
+    len /= LPC10_SAMPLES_PER_FRAME;
+    for (i = 0;  i < len;  i++)
+    {
+        for (j = 0;  j < LPC10_SAMPLES_PER_FRAME;  j++)
+            speech[j] = (float) amp[i*LPC10_SAMPLES_PER_FRAME + j]/32768.0f;
+        high_pass_100hz(s, speech, 0, LPC10_SAMPLES_PER_FRAME);
+        lpc10_analyse(s, speech, voice, &pitch, &rms, rc);
+        encode(s, &frame, voice, pitch, rms, rc);
+        lpc10_pack(s, &code[7*i], &frame);
+    }
+    return len*7;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/lpc10_placev.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/lpc10_placev.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,336 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * lpc10_placev.c - LPC10 low bit rate speech codec.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2006 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * This code is based on the U.S. Department of Defense reference
+ * implementation of the LPC-10 2400 bps Voice Coder. They do not
+ * exert copyright claims on their code, and it may be freely used.
+ *
+ * $Id: lpc10_placev.c,v 1.17 2008/07/02 14:48:25 steveu Exp $
+ */
+
+#if defined(HAVE_CONFIG_H)
+#include <config.h>
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <inttypes.h>
+#include <memory.h>
+#include "floating_fudge.h"
+#if defined(HAVE_TGMATH_H)
+#include <tgmath.h>
+#endif
+#if defined(HAVE_MATH_H)
+#include <math.h>
+#endif
+
+#include "spandsp/telephony.h"
+#include "spandsp/dc_restore.h"
+#include "spandsp/lpc10.h"
+
+#include "lpc10_encdecs.h"
+
+#define subsc(x,y) (((x) << 1) + (y))
+
+void lpc10_placea(int32_t *ipitch,
+                  int32_t voibuf[3][2],
+                  int32_t *obound,
+                  int32_t af,
+                  int32_t vwin[3][2],
+                  int32_t awin[3][2],
+                  int32_t ewin[3][2], 
+                  int32_t lframe,
+                  int32_t maxwin)
+{
+    int allv;
+    int winv;
+    int32_t i;
+    int32_t j;
+    int32_t k;
+    int32_t l;
+    int32_t hrange;
+    int ephase;
+    int32_t lrange;
+    
+    lrange = (af - 2)*lframe + 1;
+    hrange = af*lframe;
+
+    /* Place the analysis window based on the voicing window placement,
+       onsets, tentative voicing decision, and pitch. */
+
+    /* Case 1:  Sustained voiced speech
+       If the five most recent voicing decisions are
+       voiced, then the window is placed phase-synchronously with the
+       previous window, as close to the present voicing window if possible.
+       If onsets bound the voicing window, then preference is given to
+       a phase-synchronous placement which does not overlap these onsets. */
+
+    /* Case 2:  Voiced transition
+       If at least one voicing decision in AF is voicied, and there are no
+       onsets, then the window is placed as in case 1. */
+
+    /* Case 3:  Unvoiced speech or onsets
+       If both voicing decisions in AF are unvoiced, or there are onsets
+       then the window is placed coincident with the voicing window. */
+
+    /* Note:  During phase-synchronous placement of windows, the length
+       is not altered from MAXWIN, since this would defeat the purpose
+       of phase-synchronous placement. */
+
+    /* Check for case 1 and case 2 */
+    allv = voibuf[af - 2][1] == 1
+           &&
+           voibuf[af - 1][0] == 1
+           &&
+           voibuf[af - 1][1] == 1
+           &&
+           voibuf[af][0] == 1
+           &&
+           voibuf[af][1] == 1;
+    winv = voibuf[af][0] == 1  ||  voibuf[af][1] == 1;
+    if (allv  ||  (winv  &&  *obound == 0))
+    {
+        /* APHASE:  Phase synchronous window placement. */
+        /* Get minimum lower index of the window. */
+        i = (lrange + *ipitch - 1 - awin[af - 2][0]) / *ipitch;
+        i *= *ipitch;
+        i += awin[af - 2][0];
+        /* l = the actual length of this frame's analysis window. */
+        l = maxwin;
+        /* Calculate the location where a perfectly centered window would start. */
+        k = (vwin[af - 1][0] + vwin[af - 1][1] + 1 - l)/2;
+        /* Choose the actual location to be the pitch multiple closest to this */
+        awin[af - 1][0] = i + ((int) floorf((float) (k - i)/(float) *ipitch + 0.5f))*(*ipitch);
+        awin[af - 1][1] = awin[af - 1][0] + l - 1;
+        /* If there is an onset bounding the right of the voicing window and the
+           analysis window overlaps that, then move the analysis window backward
+           to avoid this onset. */
+        if (*obound >= 2  &&  awin[af - 1][1] > vwin[af - 1][1])
+        {
+            awin[af - 1][0] -= *ipitch;
+            awin[af - 1][1] -= *ipitch;
+        }
+        /* Similarly for the left of the voicing window. */
+        if ((*obound == 1  ||  *obound == 3)  &&  awin[af - 1][0] < vwin[af - 1][0])
+        {
+            awin[af - 1][0] += *ipitch;
+            awin[af - 1][1] += *ipitch;
+        }
+        /* If this placement puts the analysis window above HRANGE, then
+           move it backward an integer number of pitch periods. */
+        while (awin[af - 1][1] > hrange)
+        {
+            awin[af - 1][0] -= *ipitch;
+            awin[af - 1][1] -= *ipitch;
+        }
+        /* Similarly if the placement puts the analysis window below LRANGE. */
+        while (awin[af - 1][0] < lrange)
+        {
+            awin[af - 1][0] += *ipitch;
+            awin[af - 1][1] += *ipitch;
+        }
+        /* Make energy window be phase-synchronous. */
+        ephase = TRUE;
+    }
+    else
+    {
+        /* Case 3 */
+        awin[af - 1][0] = vwin[af - 1][0];
+        awin[af - 1][1] = vwin[af - 1][1];
+        ephase = FALSE;
+    }
+    /* RMS is computed over an integer number of pitch periods in the analysis
+       window.  When it is not placed phase-synchronously, it is placed as close
+       as possible to onsets. */
+    j = (awin[af - 1][1] - awin[af - 1][0] + 1) / *ipitch * *ipitch;
+    if (j == 0  ||  !winv)
+    {
+        ewin[af - 1][0] = vwin[af - 1][0];
+        ewin[af - 1][1] = vwin[af - 1][1];
+    }
+    else if (!ephase  &&  *obound == 2)
+    {
+        ewin[af - 1][0] = awin[af - 1][1] - j + 1;
+        ewin[af - 1][1] = awin[af - 1][1];
+    }
+    else
+    {
+        ewin[af - 1][0] = awin[af - 1][0];
+        ewin[af - 1][1] = awin[af - 1][0] + j - 1;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+void lpc10_placev(int32_t *osbuf,
+                  int32_t *osptr,
+                  int32_t oslen, 
+                  int32_t *obound,
+                  int32_t vwin[3][2],
+                  int32_t af,
+                  int32_t lframe,
+                  int32_t minwin,
+                  int32_t maxwin,
+                  int32_t dvwinl,
+                  int32_t dvwinh)
+{
+    int32_t i1;
+    int32_t i2;
+    int crit;
+    int32_t q;
+    int32_t osptr1;
+    int32_t hrange;
+    int32_t lrange;
+    int i;
+
+    /* Voicing window placement */
+
+    /*         __________________ __________________ ______________ */
+    /*        |                  |                  |               */
+    /*        |        1F        |        2F        |        3F ... */
+    /*        |__________________|__________________|______________ */
+
+    /*    Previous | */
+    /*      Window | */
+    /*  ...________| */
+
+    /*             |                                | */
+    /*      ------>| This window's placement range  |<------ */
+    /*             |                                | */
+
+    /* There are three cases.  Note these are different from those
+       given in the LPC-10e phase 1 report. */
+
+    /* 1.  If there are no onsets in this range, then the voicing window
+       is centered in the pitch window.  If such a placement is not within
+       the window's placement range, then the window is placed in the left-most
+       portion of the placement range.  Its length is always MAXWIN. */
+
+    /* 2.  If the first onset is in 2F and there is sufficient room to place
+       the window immediately before this onset, then the window is placed
+       there, and its length is set to the maximum possible under these
+       constraints. */
+
+    /* "Critical Region Exception":  If there is another onset in 2F
+       such that a window can be placed between the two onsets, the
+       window is placed there (ie, as in case 3). */
+
+    /* 3.  Otherwise, the window is placed immediately after the onset.  The
+       window's length is the longest length that can fit in the range under these
+       constraints, except that the window may be shortened even further to avoid
+       overlapping other onsets in the placement range.  In any case, the window's
+       length is at least MINWIN. */
+
+    /* Note that the values of MINWIN and LFRAME must be chosen such
+       that case 2 = false implies case 3 = true.   This means that
+       MINWIN <= LFRAME/2.  If this were not the case, then a fourth case
+       would have to be added for when the window cannot fit either before
+       or after the onset. */
+
+    /* Note also that onsets which weren't in 2F last time may be in 1F this
+       time, due to the filter delays in computing onsets.  The result is that
+       occasionally a voicing window will overlap that onset.  The only way
+       to circumvent this problem is to add more delay in processing input
+       speech.  In the trade-off between delay and window-placement, window
+       placement lost. */
+
+    /* Compute the placement range */
+
+    /* Computing MAX */
+    i1 = vwin[af - 2][1] + 1;
+    i2 = (af - 2)*lframe + 1;
+    lrange = max(i1, i2);
+    hrange = af*lframe;
+    /* Compute OSPTR1, so the following code only looks at relevant onsets. */
+    for (osptr1 = *osptr - 1;  osptr1 >= 1;  osptr1--)
+    {
+        if (osbuf[osptr1 - 1] <= hrange)
+            break;
+    }
+    osptr1++;
+    /* Check for case 1 first (fast case) */
+    if (osptr1 <= 1  ||  osbuf[osptr1 - 2] < lrange)
+    {
+        /* Compute max */
+        i1 = vwin[af - 2][1] + 1;
+        vwin[af - 1][0] = max(i1, dvwinl);
+        vwin[af - 1][1] = vwin[af - 1][0] + maxwin - 1;
+        *obound = 0;
+    }
+    else
+    {
+        /* Search backward in OSBUF for first onset in range. */
+        /* This code relies on the above check being performed first. */
+        for (q = osptr1 - 1;  q >= 1;  q--)
+        {
+            if (osbuf[q - 1] < lrange)
+                break;
+        }
+        q++;
+        /* Check for case 2 (placement before onset): */
+        /* Check for critical region exception: */
+        crit = FALSE;
+        for (i = q + 1;  i < osptr1;  i++)
+        {
+            if (osbuf[i - 1] - osbuf[q - 1] >= minwin)
+            {
+                crit = TRUE;
+                break;
+            }
+        }
+        /* Compute max */
+        i1 = (af - 1)*lframe;
+        i2 = lrange + minwin - 1;
+        if (!crit  &&  osbuf[q - 1] > max(i1, i2))
+        {
+            vwin[af - 1][1] = osbuf[q - 1] - 1;
+            /* Compute max */
+            i2 = vwin[af - 1][1] - maxwin + 1;
+            vwin[af - 1][0] = max(lrange, i2);
+            *obound = 2;
+        }
+        else
+        {
+            /* Case 3 (placement after onset) */
+            vwin[af - 1][0] = osbuf[q - 1];
+            do
+            {
+                if (++q >= osptr1
+                    ||
+                    osbuf[q - 1] > vwin[af - 1][0] + maxwin)
+                {
+                    /* Compute min */
+                    i1 = vwin[af - 1][0] + maxwin - 1;
+                    vwin[af - 1][1] = min(i1, hrange);
+                    *obound = 1;
+                    return;
+                }
+            }
+            while (osbuf[q - 1] < vwin[af - 1][0] + minwin);
+            vwin[af - 1][1] = osbuf[q - 1] - 1;
+            *obound = 3;
+        }
+    }
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/lpc10_voicing.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/lpc10_voicing.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,491 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * lpc10_voicing.c - LPC10 low bit rate speech codec.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2006 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * This code is based on the U.S. Department of Defense reference
+ * implementation of the LPC-10 2400 bps Voice Coder. They do not
+ * exert copyright claims on their code, and it may be freely used.
+ *
+ * $Id: lpc10_voicing.c,v 1.14 2008/07/02 14:48:25 steveu Exp $
+ */
+
+#if defined(HAVE_CONFIG_H)
+#include <config.h>
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <inttypes.h>
+#include <memory.h>
+#include "floating_fudge.h"
+#if defined(HAVE_TGMATH_H)
+#include <tgmath.h>
+#endif
+#if defined(HAVE_MATH_H)
+#include <math.h>
+#endif
+
+#include "spandsp/telephony.h"
+#include "spandsp/dc_restore.h"
+#include "spandsp/lpc10.h"
+
+#include "lpc10_encdecs.h"
+
+static void vparms(int32_t vwin[],
+                   float *inbuf,
+                   float *lpbuf,
+                   const int32_t buflim[],
+                   int32_t half,
+                   float *dither,
+                   int32_t *mintau,
+                   int32_t *zc, 
+                   int32_t *lbe,
+                   int32_t *fbe,
+                   float *qs,
+                   float *rc1,
+                   float *ar_b,
+                   float *ar_f)
+{
+    int32_t inbuf_offset;
+    int32_t lpbuf_offset;
+    int32_t vlen;
+    int32_t stop;
+    int32_t i;
+    int32_t start;
+    float r1;
+    float r2;
+    float e_pre;
+    float ap_rms;
+    float e_0;
+    float oldsgn;
+    float lp_rms;
+    float e_b;
+    float e_f;
+    float r_b;
+    float r_f;
+    float e0ap;
+
+    /* Calculate zero crossings (ZC) and several energy and correlation */
+    /* measures on low band and full band speech.  Each measure is taken */
+    /* over either the first or the second half of the voicing window, */
+    /* depending on the variable HALF. */
+    lpbuf_offset = buflim[2];
+    lpbuf -= lpbuf_offset;
+    inbuf_offset = buflim[0];
+    inbuf -= inbuf_offset;
+
+    lp_rms = 0.0f;
+    ap_rms = 0.0f;
+    e_pre = 0.0f;
+    e0ap = 0.0f;
+    *rc1 = 0.0f;
+    e_0 = 0.0f;
+    e_b = 0.0f;
+    e_f = 0.0f;
+    r_f = 0.0f;
+    r_b = 0.0f;
+    *zc = 0;
+    vlen = vwin[1] - vwin[0] + 1;
+    start = vwin[0] + half*vlen/2 + 1;
+    stop = start + vlen/2 - 1;
+
+    /* I'll use the symbol HVL in the table below to represent the value */
+    /* VLEN/2.  Note that if VLEN is odd, then HVL should be rounded down, */
+    /* i.e., HVL = (VLEN-1)/2. */
+
+    /* HALF  START          STOP */
+
+    /* 1     VWIN(1)+1      VWIN(1)+HVL */
+    /* 2     VWIN(1)+HVL+1  VWIN(1)+2*HVL */
+    oldsgn = r_sign(1.0f, inbuf[start - 1] - *dither);
+    for (i = start;  i <= stop;  i++)
+    {
+        lp_rms += fabsf(lpbuf[i]);
+        ap_rms += fabsf(inbuf[i]);
+        e_pre += fabsf(inbuf[i] - inbuf[i - 1]);
+        r1 = inbuf[i];
+        e0ap += r1*r1;
+        *rc1 += inbuf[i]*inbuf[i - 1];
+        r1 = lpbuf[i];
+        e_0 += r1*r1;
+        r1 = lpbuf[i - *mintau];
+        e_b += r1*r1;
+        r1 = lpbuf[i + *mintau];
+        e_f += r1*r1;
+        r_f += lpbuf[i]*lpbuf[i + *mintau];
+        r_b += lpbuf[i]*lpbuf[i - *mintau];
+        r1 = inbuf[i] + *dither;
+        if (r_sign(1.0f, r1) != oldsgn)
+        {
+            ++(*zc);
+            oldsgn = -oldsgn;
+        }
+        *dither = -(*dither);
+    }
+    /* Normalized short-term autocovariance coefficient at unit sample delay */
+    *rc1 /= max(e0ap, 1.0f);
+    /* Ratio of the energy of the first difference signal (6 dB/oct preemphasis)*/
+    /* to the energy of the full band signal */
+    /* Computing MAX */
+    r1 = ap_rms*2.0f;
+    *qs = e_pre/max(r1, 1.0f);
+    /* aR_b is the product of the forward and reverse prediction gains, */
+    /* looking backward in time (the causal case). */
+    *ar_b = r_b/max(e_b, 1.0f)*(r_b/max(e_0, 1.0f));
+    /* aR_f is the same as aR_b, but looking forward in time (non causal case).*/
+    *ar_f = r_f/max(e_f, 1.0f)*(r_f/max(e_0, 1.0f));
+    /* Normalize ZC, LBE, and FBE to old fixed window length of 180. */
+    /* (The fraction 90/VLEN has a range of 0.58 to 1) */
+    r2 = (float) (*zc << 1);
+    *zc = lrintf(r2*(90.0f/vlen));
+    r1 = lp_rms/4*(90.0f/vlen);
+    *lbe = min(lrintf(r1), 32767);
+    r1 = ap_rms/4*(90.0f/vlen);
+    *fbe = min(lrintf(r1), 32767);
+}
+/*- End of function --------------------------------------------------------*/
+
+/* Voicing detection makes voicing decisions for each half */
+/* frame of input speech.  Tentative voicing decisions are made two frames*/
+/* in the future (2F) for each half frame.  These decisions are carried */
+/* through one frame in the future (1F) to the present (P) frame where */
+/* they are examined and smoothed, resulting in the final voicing */
+/* decisions for each half frame. */
+
+/* The voicing parameter (signal measurement) column vector (VALUE) */
+/* is based on a rectangular window of speech samples determined by the */
+/* window placement algorithm.  The voicing parameter vector contains the*/
+/* AMDF windowed maximum-to-minimum ratio, the zero crossing rate, energy*/
+/* measures, reflection coefficients, and prediction gains.  The voicing */
+/* window is placed to avoid contamination of the voicing parameter vector*/
+/* with speech onsets. */
+
+/* The input signal is then classified as unvoiced (including */
+/* silence) or voiced.  This decision is made by a linear discriminant */
+/* function consisting of a dot product of the voicing decision */
+/* coefficient (VDC) row vector with the measurement column vector */
+/* (VALUE).  The VDC vector is 2-dimensional, each row vector is optimized*/
+/* for a particular signal-to-noise ratio (SNR).  So, before the dot */
+/* product is performed, the SNR is estimated to select the appropriate */
+/* VDC vector. */
+
+/* The smoothing algorithm is a modified median smoother.  The */
+/* voicing discriminant function is used by the smoother to determine how*/
+/* strongly voiced or unvoiced a signal is.  The smoothing is further */
+/* modified if a speech onset and a voicing decision transition occur */
+/* within one half frame.  In this case, the voicing decision transition */
+/* is extended to the speech onset.  For transmission purposes, there are*/
+/* constraints on the duration and transition of voicing decisions.  The */
+/* smoother takes these constraints into account. */
+
+/* Finally, the energy estimates are updated along with the dither */
+/* threshold used to calculate the zero crossing rate (ZC). */
+
+void lpc10_voicing(lpc10_encode_state_t *s,
+                   int32_t vwin[],
+                   float *inbuf,
+                   float *lpbuf,
+                   const int32_t buflim[],
+                   int32_t half,
+                   float *minamd,
+                   float *maxamd, 
+                   int32_t *mintau,
+                   float ivrc[],
+                   int32_t obound[])
+{
+    static const float vdc[100] =
+    {
+        0.0f, 1714.0f, -110.0f, 334.0f, -4096.0f,  -654.0f, 3752.0f, 3769.0f, 0.0f,  1181.0f,
+        0.0f,  874.0f,  -97.0f, 300.0f, -4096.0f, -1021.0f, 2451.0f, 2527.0f, 0.0f,  -500.0f,
+        0.0f,  510.0f,  -70.0f, 250.0f, -4096.0f, -1270.0f, 2194.0f, 2491.0f, 0.0f, -1500.0f,
+        0.0f,  500.0f,  -10.0f, 200.0f, -4096.0f, -1300.0f,  2.0e3f,  2.0e3f, 0.0f,  -2.0e3f,
+        0.0f,  500.0f,    0.0f,   0.0f, -4096.0f, -1300.0f,  2.0e3f,  2.0e3f, 0.0f, -2500.0f,
+        0.0f,    0.0f,    0.0f,   0.0f,     0.0f,     0.0f,    0.0f,    0.0f, 0.0f,     0.0f,
+        0.0f,    0.0f,    0.0f,   0.0f,     0.0f,     0.0f,    0.0f,    0.0f, 0.0f,     0.0f,
+        0.0f,    0.0f,    0.0f,   0.0f,     0.0f,     0.0f,    0.0f,    0.0f, 0.0f,     0.0f,
+        0.0f,    0.0f,    0.0f,   0.0f,     0.0f,     0.0f,    0.0f,    0.0f, 0.0f,     0.0f,
+        0.0f,    0.0f,    0.0f,   0.0f,     0.0f,     0.0f,    0.0f,    0.0f, 0.0f,     0.0f
+    };
+    static const int nvdcl = 5;
+    static const float vdcl[10] =
+    {
+        600.0f, 450.0f, 300.0f, 200.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f
+    };
+
+    int32_t inbuf_offset;
+    int32_t lpbuf_offset;
+    int32_t i1;
+    float r1;
+    float r2;
+    float ar_b;
+    float ar_f;
+    int32_t snrl;
+    int32_t i;
+    float value[9];
+    int32_t zc;
+    int ot;
+    float qs;
+    int32_t vstate;
+    float rc1;
+    int32_t fbe;
+    int32_t lbe;
+    float snr2;
+
+#if (_MSC_VER >= 1400) 
+    __analysis_assume(half >= 0  &&  half < 2);
+#endif
+    inbuf_offset = 0;
+    lpbuf_offset = 0;
+    if (inbuf)
+    {
+        inbuf_offset = buflim[0];
+        inbuf -= inbuf_offset;
+    }
+    if (lpbuf)
+    {
+        lpbuf_offset = buflim[2];
+        lpbuf -= lpbuf_offset;
+    }
+
+    /* Voicing Decision Parameter vector (* denotes zero coefficient): */
+
+    /*     * MAXMIN */
+    /*       LBE/LBVE */
+    /*       ZC */
+    /*       RC1 */
+    /*       QS */
+    /*       IVRC2 */
+    /*       aR_B */
+    /*       aR_F */
+    /*     * LOG(LBE/LBVE) */
+    /* Define 2-D voicing decision coefficient vector according to the voicing */
+    /* parameter order above.  Each row (VDC vector) is optimized for a specific */
+    /*   SNR.  The last element of the vector is the constant. */
+    /*              E    ZC    RC1    Qs   IVRC2  aRb   aRf        c */
+
+    /* The VOICE array contains the result of the linear discriminant function*/
+    /* (analog values).  The VOIBUF array contains the hard-limited binary */
+    /* voicing decisions.  The VOICE and VOIBUF arrays, according to FORTRAN */
+    /* memory allocation, are addressed as: */
+
+    /*        (half-frame number, future-frame number) */
+
+    /*        |   Past    |  Present  |  Future1  |  Future2  | */
+    /*        | 1,0 | 2,0 | 1,1 | 2,1 | 1,2 | 2,2 | 1,3 | 2,3 |  --->  time */
+
+    /* Update linear discriminant function history each frame: */
+    if (half == 0)
+    {
+        s->voice[0][0] = s->voice[1][0];
+        s->voice[0][1] = s->voice[1][1];
+        s->voice[1][0] = s->voice[2][0];
+        s->voice[1][1] = s->voice[2][1];
+        s->maxmin = *maxamd / max(*minamd, 1.0f);
+    }
+    /* Calculate voicing parameters twice per frame */
+    vparms(vwin,
+           &inbuf[inbuf_offset],
+           &lpbuf[lpbuf_offset],
+           buflim, 
+           half,
+           &s->dither,
+           mintau,
+           &zc,
+           &lbe,
+           &fbe,
+           &qs,
+           &rc1,
+           &ar_b,
+           &ar_f);
+    /* Estimate signal-to-noise ratio to select the appropriate VDC vector. */
+    /* The SNR is estimated as the running average of the ratio of the */
+    /* running average full-band voiced energy to the running average */
+    /* full-band unvoiced energy. SNR filter has gain of 63. */
+    r1 = (s->snr + s->fbve/(float) max(s->fbue, 1))*63/64.0f;
+    s->snr = (float) lrintf(r1);
+    snr2 = s->snr*s->fbue/max(s->lbue, 1);
+    /* Quantize SNR to SNRL according to VDCL thresholds. */
+    i1 = nvdcl - 1;
+    for (snrl = 0;  snrl < i1;  snrl++)
+    {
+        if (snr2 > vdcl[snrl])
+            break;
+    }
+    /* (Note:  SNRL = NVDCL here) */
+    /* Linear discriminant voicing parameters: */
+    value[0] = s->maxmin;
+    value[1] = (float) lbe/max(s->lbve, 1);
+    value[2] = (float) zc;
+    value[3] = rc1;
+    value[4] = qs;
+    value[5] = ivrc[1];
+    value[6] = ar_b;
+    value[7] = ar_f;
+    /* Evaluation of linear discriminant function: */
+    s->voice[2][half] = vdc[snrl*10 + 9];
+    for (i = 0;  i < 8;  i++)
+        s->voice[2][half] += vdc[snrl*10 + i]*value[i];
+    /* Classify as voiced if discriminant > 0, otherwise unvoiced */
+    /* Voicing decision for current half-frame:  1 = Voiced; 0 = Unvoiced */
+    s->voibuf[3][half] = (s->voice[2][half] > 0.0f)  ?  1  :  0;
+    /* Skip voicing decision smoothing in first half-frame: */
+    /* Give a value to VSTATE, so that trace statements below will print */
+    /* a consistent value from one call to the next when HALF .EQ. 1. */
+    /* The value of VSTATE is not used for any other purpose when this is */
+    /* true. */
+    vstate = -1;
+    if (half != 0)
+    {
+        /* Voicing decision smoothing rules (override of linear combination): */
+
+        /*     Unvoiced half-frames:  At least two in a row. */
+        /*     -------------------- */
+
+        /*     Voiced half-frames:    At least two in a row in one frame. */
+        /*     -------------------    Otherwise at least three in a row. */
+        /*                    (Due to the way transition frames are encoded) */
+
+        /* In many cases, the discriminant function determines how to smooth. */
+        /* In the following chart, the decisions marked with a * may be overridden. */
+
+        /* Voicing override of transitions at onsets: */
+        /* If a V/UV or UV/V voicing decision transition occurs within one-half */
+        /* frame of an onset bounding a voicing window, then the transition is */
+        /* moved to occur at the onset. */
+
+        /*     P    1F */
+        /*     -----    ----- */
+        /*     0   0   0   0 */
+        /*     0   0   0*  1    (If there is an onset there) */
+        /*     0   0   1*  0*    (Based on 2F and discriminant distance) */
+        /*     0   0   1   1 */
+        /*     0   1*  0   0    (Always) */
+        /*     0   1*  0*  1    (Based on discriminant distance) */
+        /*     0*  1   1   0*    (Based on past, 2F, and discriminant distance) */
+        /*     0   1*  1   1    (If there is an onset there) */
+        /*     1   0*  0   0    (If there is an onset there) */
+        /*     1   0   0   1 */
+        /*     1   0*  1*  0    (Based on discriminant distance) */
+        /*     1   0*  1   1    (Always) */
+        /*     1   1   0   0 */
+        /*     1   1   0*  1*    (Based on 2F and discriminant distance) */
+        /*     1   1   1*  0    (If there is an onset there) */
+        /*     1   1   1   1 */
+
+        /* Determine if there is an onset transition between P and 1F. */
+        /* OT (Onset Transition) is true if there is an onset between */
+        /* P and 1F but not after 1F. */
+        ot = ((obound[0] & 2) != 0  ||  obound[1] == 1)  &&  (obound[2] & 1) == 0;
+        /* Multi-way dispatch on voicing decision history: */
+        vstate = (s->voibuf[1][0] << 3) + (s->voibuf[1][1] << 2) + (s->voibuf[2][0] << 1) + s->voibuf[2][1];
+        switch (vstate + 1)
+        {
+        case 2:
+            if (ot  &&  s->voibuf[3][0] == 1)
+                s->voibuf[2][0] = 1;
+            break;
+        case 3:
+            if (s->voibuf[3][0] == 0  ||  s->voice[1][0] < -s->voice[1][1])
+                s->voibuf[2][0] = 0;
+            else
+                s->voibuf[2][1] = 1;
+            break;
+        case 5:
+            s->voibuf[1][1] = 0;
+            break;
+        case 6:
+            if (s->voice[0][1] < -s->voice[1][0])
+                s->voibuf[1][1] = 0;
+            else
+                s->voibuf[2][0] = 1;
+            break;
+        case 7:
+            if (s->voibuf[0][0] == 1  ||  s->voibuf[3][0] == 1  ||  s->voice[1][1] > s->voice[0][0])
+                s->voibuf[2][1] = 1;
+            else
+                s->voibuf[1][0] = 1;
+            break;
+        case 8:
+            if (ot)
+                s->voibuf[1][1] = 0;
+            break;
+        case 9:
+            if (ot)
+                s->voibuf[1][1] = 1;
+            break;
+        case 11:
+            if (s->voice[1][9] < -s->voice[0][1])
+                s->voibuf[2][0] = 0;
+            else
+                s->voibuf[1][1] = 1;
+            break;
+        case 12:
+            s->voibuf[1][1] = 1;
+            break;
+        case 14:
+            if (s->voibuf[3][0] == 0  &&  s->voice[1][1] < -s->voice[1][0])
+                s->voibuf[2][1] = 0;
+            else
+                s->voibuf[2][0] = 1;
+            break;
+        case 15:
+            if (ot  &&  s->voibuf[3][0] == 0)
+                s->voibuf[2][0] = 0;
+            break;
+        }
+    }
+    /* During unvoiced half-frames, update the low band and full band unvoiced*/
+    /* energy estimates (LBUE and FBUE) and also the zero crossing */
+    /* threshold (DITHER).  (The input to the unvoiced energy filters is */
+    /* restricted to be less than 10dB above the previous inputs of the */
+    /* filters.) */
+    /* During voiced half-frames, update the low-pass (LBVE) and all-pass */
+    /* (FBVE) voiced energy estimates. */
+    if (s->voibuf[3][half] == 0)
+    {
+        r1 = (s->sfbue*63 + (min(fbe, s->ofbue*3) << 3))/64.0f;
+        s->sfbue = lrintf(r1);
+        s->fbue = s->sfbue/8;
+        s->ofbue = fbe;
+        r1 = (s->slbue*63 + (min(lbe, s->olbue*3) << 3))/64.0f;
+        s->slbue = lrintf(r1);
+        s->lbue = s->slbue/8;
+        s->olbue = lbe;
+    }
+    else
+    {
+        s->lbve = lrintf((s->lbve*63 + lbe)/64.0f);
+        s->fbve = lrintf((s->fbve*63 + fbe)/64.0f);
+    }
+    /* Set dither threshold to yield proper zero crossing rates in the */
+    /* presence of low frequency noise and low level signal input. */
+    /* NOTE: The divisor is a function of REF, the expected energies. */
+    /* Computing MIN */
+    /* Computing MAX */
+    r2 = sqrtf((float) (s->lbue*s->lbve))*64/3000;
+    r1 = max(r2, 1.0f);
+    s->dither = min(r1, 20.0f);
+    /* Voicing decisions are returned in VOIBUF. */
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/make_at_dictionary.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/make_at_dictionary.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,632 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * make_at_dictionary.c - Generate a trie based dictionary for the AT
+ *                        commands supported by the AT interpreter.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2006 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: make_at_dictionary.c,v 1.1 2008/07/01 03:27:51 steveu Exp $
+ */
+
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
+
+#include <inttypes.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <string.h>
+#include <assert.h>
+
+const char *wordlist[] =
+{
+    " ",        /* Dummy to absorb spaces in commands */
+    "&C",       /* V.250 6.2.8 - Circuit 109 (received line signal detector), behaviour */ 
+    "&D",       /* V.250 6.2.9 - Circuit 108 (data terminal ready) behaviour */ 
+    "&F",       /* V.250 6.1.2 - Set to factory-defined configuration */ 
+    "+A8A",     /* V.251 6.3 - V.8 calling tone indication */
+    "+A8C",     /* V.251 6.2 - V.8 answer signal indication */
+    "+A8E",     /* V.251 5.1 - V.8 and V.8bis operation controls */
+    "+A8I",     /* V.251 6.1 - V.8 CI signal indication */
+    "+A8J",     /* V.251 6.4 - V.8 negotiation complete */
+    "+A8M",     /* V.251 5.2 - Send V.8 menu signals */
+    "+A8R",     /* V.251 6.6 - V.8bis signal and message reporting */
+    "+A8T",     /* V.251 5.3 - Send V.8bis signal and/or message(s) */
+    "+ASTO",    /* V.250 6.3.15 - Store telephone number */ 
+    "+CAAP",    /* 3GPP TS 27.007 7.25 - Automatic answer for eMLPP Service */
+    "+CACM",    /* 3GPP TS 27.007 8.25 - Accumulated call meter */
+    "+CACSP",   /* 3GPP TS 27.007 11.1.7 - Voice Group or Voice Broadcast Call State Attribute Presentation */
+    "+CAD",     /* IS-99 5.6.3 - Query analogue or digital service */
+    "+CAEMLPP", /* 3GPP TS 27.007 7.22 - eMLPP Priority Registration and Interrogation */
+    "+CAHLD",   /* 3GPP TS 27.007 11.1.3 - Leave an ongoing Voice Group or Voice Broadcast Call */
+    "+CAJOIN",  /* 3GPP TS 27.007 11.1.1 - Accept an incoming Voice Group or Voice Broadcast Call */
+    "+CALA",    /* 3GPP TS 27.007 8.16 - Alarm */
+    "+CALCC",   /* 3GPP TS 27.007 11.1.6 - List current Voice Group and Voice Broadcast Calls */
+    "+CALD",    /* 3GPP TS 27.007 8.38 - Delete alar m */
+    "+CALM",    /* 3GPP TS 27.007 8.20 - Alert sound mode */
+    "+CAMM",    /* 3GPP TS 27.007 8.26 - Accumulated call meter maximum */
+    "+CANCHEV", /* 3GPP TS 27.007 11.1.8 - NCH Support Indication */
+    "+CAOC",    /* 3GPP TS 27.007 7.16 - Advice of Charge */
+    "+CAPD",    /* 3GPP TS 27.007 8.39 - Postpone or dismiss an alarm */
+    "+CAPTT",   /* 3GPP TS 27.007 11.1.4 - Talker Access for Voice Group Call */
+    "+CAREJ",   /* 3GPP TS 27.007 11.1.2 - Reject an incoming Voice Group or Voice Broadcast Call */
+    "+CAULEV",  /* 3GPP TS 27.007 11.1.5 - Voice Group Call Uplink Status Presentation */
+    "+CBC",     /* 3GPP TS 27.007 8.4 - Battery charge */
+    "+CBCS",    /* 3GPP TS 27.007 11.3.2 - VBS subscriptions and GId status */
+    "+CBIP",    /* IS-99 5.6 - Base station IP address */
+    "+CBST",    /* 3GPP TS 27.007 6.7 - Select bearer service type */
+    "+CCFC",    /* 3GPP TS 27.007 7.11 - Call forwarding number and conditions */
+    "+CCLK",    /* 3GPP TS 27.007 8.15 - Clock */
+    "+CCS",     /* IS-135 4.1.22 - Compression status */
+    "+CCUG",    /* 3GPP TS 27.007 7.10 - Closed user group */
+    "+CCWA",    /* 3GPP TS 27.007 7.12 - Call waiting */
+    "+CCWE",    /* 3GPP TS 27.007 8.28 - Call Meter maximum event */
+    "+CDIP",    /* 3GPP TS 27.007 7.9 - Called line identification presentation */
+    "+CDIS",    /* 3GPP TS 27.007 8.8 - Display control */
+    "+CDV",     /* IS-99 5.6 - Dial command for voice call */
+    "+CEER",    /* 3GPP TS 27.007 6.10 - Extended error report */
+    "+CESP",    /* GSM07.05  3.2.4 - Enter SMS block mode protocol */
+    "+CFCS",    /* 3GPP TS 27.007 7.24 - Fast call setup conditions */
+    "+CFG",     /* IS-99 5.6 - Configuration string */
+    "+CFUN",    /* 3GPP TS 27.007 8.2 - Set phone functionality */
+    "+CGACT",   /* 3GPP TS 27.007 10.1.10 - PDP context activate or deactivate */
+    "+CGANS",   /* 3GPP TS 27.007 10.1.16 - Manual response to a network request for PDP context activation */
+    "+CGATT",   /* 3GPP TS 27.007 10.1.9 - PS attach or detach */
+    "+CGAUTO",  /* 3GPP TS 27.007 10.1.15 - Automatic response to a network request for PDP context activation */
+    "+CGCAP",   /* IS-99 5.6 - Request complete capabilities list */
+    "+CGCLASS", /* 3GPP TS 27.007 10.1.17 - GPRS mobile station class (GPRS only) */
+    "+CGCLOSP", /* 3GPP TS 27.007 10.1.13 - Configure local octet stream PAD parameters (Obsolete) */
+    "+CGCLPAD", /* 3GPP TS 27.007 10.1.12 - Configure local triple-X PAD parameters (GPRS only) (Obsolete) */
+    "+CGCMOD",  /* 3GPP TS 27.007 10.1.11 - PDP Context Modify */
+    "+CGCS",    /* 3GPP TS 27.007 11.3.1 - VGCS subscriptions and GId status */
+    "+CGDATA",  /* 3GPP TS 27.007 10.1.12 - Enter data state */
+    "+CGDCONT", /* 3GPP TS 27.007 10.1.1 - Define PDP Context */
+    "+CGDSCONT",/* 3GPP TS 27.007 10.1.2 - Define Secondary PDP Context */
+    "+CGEQMIN", /* 3GPP TS 27.007 10.1.7 - 3G Quality of Service Profile (Minimum acceptable) */
+    "+CGEQNEG", /* 3GPP TS 27.007 10.1.8 - 3G Quality of Service Profile (Negotiated) */
+    "+CGEQREQ", /* 3GPP TS 27.007 10.1.6 - 3G Quality of Service Profile (Requested) */
+    "+CGEREP",  /* 3GPP TS 27.007 10.1.18 - Packet Domain event reporting */
+    "+CGMI",    /* 3GPP TS 27.007 5.1 - Request manufacturer identification */
+    "+CGMM",    /* 3GPP TS 27.007 5.2 - Request model identification */
+    "+CGMR",    /* 3GPP TS 27.007 5.3 - Request revision identification */
+    "+CGOI",    /* IS-99 5.6 - Request global object identification */
+    "+CGPADDR", /* 3GPP TS 27.007 10.1.14 - Show PDP address */
+    "+CGQMIN",  /* 3GPP TS 27.007 10.1.5 - Quality of Service Profile (Minimum acceptable) */
+    "+CGQREQ",  /* 3GPP TS 27.007 10.1.4 - Quality of Service Profile (Requested) */
+    "+CGREG",   /* 3GPP TS 27.007 10.1.19 - GPRS network registration status */
+    "+CGSMS",   /* 3GPP TS 27.007 10.1.20 - Select service for MO SMS messages */
+    "+CGSN",    /* 3GPP TS 27.007 5.4 - Request product serial number identification */
+    "+CGTFT",   /* 3GPP TS 27.007 10.1.3 - Traffic Flow Template */
+    "+CHLD",    /* 3GPP TS 27.007 7.13 - Call related supplementary services */
+    "+CHSA",    /* 3GPP TS 27.007 6.18 - HSCSD non-transparent asymmetry configuration */
+    "+CHSC",    /* 3GPP TS 27.007 6.15 - HSCSD current call parameters */
+    "+CHSD",    /* 3GPP TS 27.007 6.12 - HSCSD device parameters */
+    "+CHSN",    /* 3GPP TS 27.007 6.14 - HSCSD non-transparent call configuration */
+    "+CHSR",    /* 3GPP TS 27.007 6.16 - HSCSD parameters report */
+    "+CHST",    /* 3GPP TS 27.007 6.13 - HSCSD transparent call configuration */
+    "+CHSU",    /* 3GPP TS 27.007 6.17 - HSCSD automatic user initiated upgrading */
+    "+CHUP",    /* 3GPP TS 27.007 6.5 - Hangup call */
+    "+CHV",     /* IS-99 5.6 - Hang-up voice */
+    "+CIMI",    /* 3GPP TS 27.007 5.6 - Request international mobile subscriber identity */
+    "+CIND",    /* 3GPP TS 27.007 8.9 - Indicator control */
+    "+CIT",     /* IS-99 5.6 - Command state inactivity timer */
+    "+CKPD",    /* 3GPP TS 27.007 8.7 - Keypad control */
+    "+CLAC",    /* 3GPP TS 27.007 8.37 - List all available AT commands */
+    "+CLAE",    /* 3GPP TS 27.007 8.31 - Language Event */
+    "+CLAN",    /* 3GPP TS 27.007 8.30 - Set Language */
+    "+CLCC",    /* 3GPP TS 27.007 7.18 - List current calls */
+    "+CLCK",    /* 3GPP TS 27.007 7.4 - Facility lock */
+    "+CLIP",    /* 3GPP TS 27.007 7.6 - Calling line identification presentation */
+    "+CLIR",    /* 3GPP TS 27.007 7.7 - Calling line identification restriction */
+    "+CLVL",    /* 3GPP TS 27.007 8.23 - Loudspeaker volume level */
+    "+CMAR",    /* 3GPP TS 27.007 8.36 - Master reset */
+    "+CMEC",    /* 3GPP TS 27.007 8.6 - Mobile termination control mode */
+    "+CMEE",    /* GSM07.07 9.1 - Report mobile equipment error */
+    "+CMER",    /* 3GPP TS 27.007 8.10 - Mobile termination event reporting */
+    "+CMGC",    /* GSM07.05 3.5.5/4.5 - Send command */
+    "+CMGD",    /* GSM07.05 3.5.4 - Delete message */
+    "+CMGF",    /* GSM07.05 3.2.3 - Message Format */
+    "+CMGL",    /* GSM07.05 3.4.2/4.1 -  List messages */
+    "+CMGR",    /* GSM07.05 3.4.3/4.2 - Read message */
+    "+CMGS",    /* GSM07.05 3.5.1/4.3 - Send message */
+    "+CMGW",    /* GSM07.05 3.5.3/4.4 - Write message to memory */
+    "+CMIP",    /* IS-99 5.6 - Mobile station IP address */
+    "+CMM",     /* IS-135 4.1.23 - Menu map */
+    "+CMMS",    /* GSM07.05 3.5.6 - More messages to send */
+    "+CMOD",    /* 3GPP TS 27.007 6.4 - Call mode */
+    "+CMSS",    /* GSM07.05 3.5.2/4.7 - Send message from storage */
+    "+CMUT",    /* 3GPP TS 27.007 8.24 - Mute control */
+    "+CMUX",    /* 3GPP TS 27.007 5.7 - Multiplexing mode */
+    "+CNMA",    /* GSM07.05 3.4.4/4.6 - New message acknowledgement to terminal adapter */
+    "+CNMI",    /* GSM07.05 3.4.1 - New message indications to terminal equipment */
+    "+CNUM",    /* 3GPP TS 27.007 7.1 - Subscriber number */
+    "+COLP",    /* 3GPP TS 27.007 7.8 - Connected line identification presentation */
+    "+COPN",    /* 3GPP TS 27.007 7.21 - Read operator names */
+    "+COPS",    /* 3GPP TS 27.007 7.3 - PLMN selection */
+    "+COS",     /* IS-135 4.1.24 - Originating service */
+    "+COTDI",   /* 3GPP TS 27.007 11.1.9 - Originator to Dispatcher Information */
+    "+CPAS",    /* 3GPP TS 27.007 8.1 - Phone activity status */
+    "+CPBF",    /* 3GPP TS 27.007 8.13 - Find phonebook entries */
+    "+CPBR",    /* 3GPP TS 27.007 8.12 - Read phonebook entries */
+    "+CPBS",    /* 3GPP TS 27.007 8.11 - Select phonebook memory storage */
+    "+CPBW",    /* 3GPP TS 27.007 8.14 - Write phonebook entry */
+    "+CPIN",    /* 3GPP TS 27.007 8.3 - Enter PIN */
+    "+CPLS",    /* 3GPP TS 27.007 7.20 - Selection of preferred PLMN list */
+    "+CPMS",    /* GSM07.05 3.2.2 - Preferred message storage */
+    "+CPOL",    /* 3GPP TS 27.007 7.19 - Preferred PLMN list */
+    "+CPPS",    /* 3GPP TS 27.007 7.23 - eMLPP subscriptions */
+    "+CPROT",   /* 3GPP TS 27.007 8.42 - Enter protocol mode */
+    "+CPUC",    /* 3GPP TS 27.007 8.27 - Price per unit and currency table */
+    "+CPWC",    /* 3GPP TS 27.007 8.29 - Power class */
+    "+CPWD",    /* 3GPP TS 27.007 7.5 - Change password */
+    "+CQD",     /* IS-135 4.1.25 - Query disconnect timer */
+    "+CR",      /* 3GPP TS 27.007 6.9 - Service reporting control */
+    "+CRC",     /* 3GPP TS 27.007 6.11 - Cellular result codes */
+    "+CREG",    /* 3GPP TS 27.007 7.2 - Network registration */
+    "+CRES",    /* GSM07.05 3.3.6 - Restore Settings */
+    "+CRLP",    /* 3GPP TS 27.007 6.8 - Radio link protocol */
+    "+CRM",     /* IS-99 5.6 - Set rm interface protocol */
+    "+CRMC",    /* 3GPP TS 27.007 8.34 - Ring Melody Control */
+    "+CRMP",    /* 3GPP TS 27.007 8.35 - Ring Melody Playback */
+    "+CRSL",    /* 3GPP TS 27.007 8.21 - Ringer sound level */
+    "+CRSM",    /* 3GPP TS 27.007 8.18 - Restricted SIM access */
+    "+CSAS",    /* GSM07.05 3.3.5 - Save settings */
+    "+CSCA",    /* GSM07.05 3.3.1 - Service centre address */
+    "+CSCB",    /* GSM07.05 3.3.4 - Select cell broadcast message types */
+    "+CSCC",    /* 3GPP TS 27.007 8.19 - Secure control command */
+    "+CSCS",    /* 3GPP TS 27.007 5.5 - Select TE character set */
+    "+CSDF",    /* 3GPP TS 27.007 6.22 - Settings date format */
+    "+CSDH",    /* GSM07.05 3.3.3 - Show text mode parameters */
+    "+CSGT",    /* 3GPP TS 27.007 8.32 - Set Greeting Text */
+    "+CSIL",    /* 3GPP TS 27.007 6.23 - Silence Command */
+    "+CSIM",    /* 3GPP TS 27.007 8.17 - Generic SIM access */
+    "+CSMP",    /* GSM07.05 3.3.2 - Set text mode parameters */
+    "+CSMS",    /* GSM07.05 3.2.1 - Select Message Service */
+    "+CSNS",    /* 3GPP TS 27.007 6.19 - Single numbering scheme */
+    "+CSQ",     /* 3GPP TS 27.007 8.5 - Signal quality */
+    "+CSS",     /* IS-135 4.1.28 - Serving system identification */
+    "+CSSN",    /* 3GPP TS 27.007 7.17 - Supplementary service notifications */
+    "+CSTA",    /* 3GPP TS 27.007 6.1 - Select type of address */
+    "+CSTF",    /* 3GPP TS 27.007 6.24 - Settings time format */
+    "+CSVM",    /* 3GPP TS 27.007 8.33 - Set Voice Mail Number */
+    "+CTA",     /* IS-135 4.1.29 - MT-Terminated async. Data calls */
+    "+CTF",     /* IS-135 4.1.30 - MT-Terminated FAX calls */
+    "+CTFR",    /* 3GPP TS 27.007 7.14 - Call deflection */
+    "+CTZR",    /* 3GPP TS 27.007 8.41 - Time Zone Reporting */
+    "+CTZU",    /* 3GPP TS 27.007 8.40 - Automatic Time Zone Update */
+    "+CUSD",    /* 3GPP TS 27.007 7.15 - Unstructured supplementary service data */
+    "+CUUS1",   /* 3GPP TS 27.007 7.26 - User to User Signalling Service 1 */
+    "+CV120",   /* 3GPP TS 27.007 6.21 - V.120 rate adaption protocol */
+    "+CVHU",    /* 3GPP TS 27.007 6.20 - Voice Hangup Control */
+    "+CVIB",    /* 3GPP TS 27.007 8.22 - Vibrator mode */
+    "+CXT",     /* IS-99 5.6 - Cellular extension */
+    "+DR",      /* V.250 6.6.2 - Data compression reporting */ 
+    "+DS",      /* V.250 6.6.1 - Data compression */ 
+    "+DS44",    /* V.250 6.6.2 - V.44 data compression */
+    "+EB",      /* V.250 6.5.2 - Break handling in error control operation */ 
+    "+EFCS",    /* V.250 6.5.4 - 32-bit frame check sequence */ 
+    "+EFRAM",   /* V.250 6.5.8 - Frame length */ 
+    "+ER",      /* V.250 6.5.5 - Error control reporting */ 
+    "+ES",      /* V.250 6.5.1 - Error control selection */ 
+    "+ESR",     /* V.250 6.5.3 - Selective repeat */ 
+    "+ETBM",    /* V.250 6.5.6 - Call termination buffer management */ 
+    "+EWIND",   /* V.250 6.5.7 - Window size */ 
+    "+FAA",     /* T.32 8.5.2.5 - Adaptive Answer parameter */
+    "+FAP",     /* T.32 8.5.1.12 - Addressing and polling capabilities parameter */
+    "+FAR",     /* T.31 8.5.1 - Adaptive reception control */ 
+    "+FBO",     /* T.32 8.5.3.4 - Phase C data bit order */
+    "+FBS",     /* T.32 8.5.3.2 - Buffer Size, read only parameter */
+    "+FBU",     /* T.32 8.5.1.10 - HDLC Frame Reporting parameter */
+    "+FCC",     /* T.32 8.5.1.1 - DCE capabilities parameters */
+    "+FCL",     /* T.31 8.5.2 - Carrier loss timeout */ 
+    "+FCLASS",  /* T.31 8.2 - Capabilities identification and control */ 
+    "+FCQ",     /* T.32 8.5.2.3 -  Copy quality checking parameter */
+    "+FCR",     /* T.32 8.5.1.9 - Capability to receive parameter */
+    "+FCS",     /* T.32 8.5.1.3 - Current Session Results parameters */
+    "+FCT",     /* T.32 8.5.2.6 - DTE phase C timeout parameter */
+    "+FDD",     /* T.31 8.5.3 - Double escape character replacement */ 
+    "+FDR",     /* T.32 8.3.4 - Data reception command */
+    "+FDT",     /* T.32 8.3.3 - Data transmission command */
+    "+FEA",     /* T.32 8.5.3.5 - Phase C received EOL alignment parameter */
+    "+FFC",     /* T.32 8.5.3.6 - Format conversion parameter */
+    "+FFD",     /* T.32 8.5.1.14 - File diagnostic message parameter */
+    "+FHS",     /* T.32 8.5.2.7 - Call termination status parameter */
+    "+FIE",     /* T.32 8.5.2.1 - Procedure interrupt enable parameter */
+    "+FIP",     /* T.32 8.3.6 - Initialize facsimile parameters */
+    "+FIS",     /* T.32 8.5.1.2 -  Current session parameters */
+    "+FIT",     /* T.31 8.5.4 - DTE inactivity timeout */ 
+    "+FKS",     /* T.32 8.3.5 - Session termination command */
+    "+FLI",     /* T.32 8.5.1.5 - Local ID string parameter, TSI or CSI */
+    "+FLO",     /* T.31 says to implement something similar to +IFC */ 
+    "+FLP",     /* T.32 8.5.1.7 - Indicate document to poll parameter */
+    "+FMI",     /* T.31 says to duplicate +GMI */ 
+    "+FMM",     /* T.31 says to duplicate +GMM */ 
+    "+FMR",     /* T.31 says to duplicate +GMR */ 
+    "+FMS",     /* T.32 8.5.2.9 - Minimum phase C speed parameter */
+    "+FND",     /* T.32 8.5.2.10 - Non-Standard Message Data Indication parameter */
+    "+FNR",     /* T.32 8.5.1.11 - Negotiation message reporting control parameters */
+    "+FNS",     /* T.32 8.5.1.6 - Non-Standard Frame FIF parameter */
+    "+FPA",     /* T.32 8.5.1.13 - Selective polling address parameter */
+    "+FPI",     /* T.32 8.5.1.5 - Local Polling ID String parameter */
+    "+FPP",     /* T.32 8.5.3 - Facsimile packet protocol */
+    "+FPR",     /* T.31 says to implement something similar to +IPR */ 
+    "+FPS",     /* T.32 8.5.2.2 - Page Status parameter */
+    "+FPW",     /* T.32 8.5.1.13 - PassWord parameter (Sending or Polling) */
+    "+FRH",     /* T.31 8.3.6 - HDLC receive */ 
+    "+FRM",     /* T.31 8.3.4 - Facsimile receive */ 
+    "+FRQ",     /* T.32 8.5.2.4 - Receive Quality Thresholds parameters */
+    "+FRS",     /* T.31 8.3.2 - Receive silence */ 
+    "+FRY",     /* T.32 8.5.2.8 - ECM Retry Value parameter */
+    "+FSA",     /* T.32 8.5.1.13 - Subaddress parameter */
+    "+FSP",     /* T.32 8.5.1.8 - Request to poll parameter */
+    "+FTH",     /* T.31 8.3.5 - HDLC transmit */ 
+    "+FTM",     /* T.31 8.3.3 - Facsimile transmit */ 
+    "+FTS",     /* T.31 8.3.1 - Transmit silence */ 
+    "+GCAP",    /* V.250 6.1.9 - Request complete capabilities list */ 
+    "+GCI",     /* V.250 6.1.10 - Country of installation, */ 
+    "+GMI",     /* V.250 6.1.4 - Request manufacturer identification */ 
+    "+GMM",     /* V.250 6.1.5 - Request model identification */ 
+    "+GMR",     /* V.250 6.1.6 - Request revision identification */ 
+    "+GOI",     /* V.250 6.1.8 - Request global object identification */ 
+    "+GSN",     /* V.250 6.1.7 - Request product serial number identification */ 
+    "+IBC",     /* TIA-617 8.3 - Control of in-band control */
+    "+IBM",     /* TIA-617 8.4 - In-Band MARK idle reporting control */
+    "+ICF",     /* V.250 6.2.11 - DTE-DCE character framing */ 
+    "+ICLOK",   /* V.250 6.2.14 - Select sync transmit clock source */ 
+    "+IDSR",    /* V.250 6.2.16 - Select data set ready option */ 
+    "+IFC",     /* V.250 6.2.12 - DTE-DCE local flow control */ 
+    "+ILRR",    /* V.250 6.2.13 - DTE-DCE local rate reporting */ 
+    "+ILSD",    /* V.250 6.2.15 - Select long space disconnect option */ 
+    "+IPR",     /* V.250 6.2.10 - Fixed DTE rate */ 
+    "+IRTS",    /* V.250 6.2.17 - Select synchronous mode RTS option */ 
+    "+MA",      /* V.250 6.4.2 - Modulation automode control */ 
+    "+MR",      /* V.250 6.4.3 - Modulation reporting control */ 
+    "+MS",      /* V.250 6.4.1 - Modulation selection */ 
+    "+MSC",     /* V.250 6.4.8 - Seamless rate change enable */ 
+    "+MV18AM",  /* V.250 6.4.6 - V.18 answering message editing */ 
+    "+MV18P",   /* V.250 6.4.7 - Order of probes */ 
+    "+MV18R",   /* V.250 6.4.5 - V.18 reporting control */ 
+    "+MV18S",   /* V.250 6.4.4 - V.18 selection */
+    "+PCW",     /* V.250 6.8.1 - Call waiting enable (V.92 DCE) */
+    "+PIG",     /* V.250 6.8.5 - PCM upstream ignore */
+    "+PMH",     /* V.250 6.8.2 - Modem on hold enable */
+    "+PMHF",    /* V.250 6.8.6 - V.92 Modem on hold hook flash */
+    "+PMHR",    /* V.250 6.8.4 - Initiate modem on hold */
+    "+PMHT",    /* V.250 6.8.3 - Modem on hold timer */
+    "+PQC",     /* V.250 6.8.7 - V.92 Phase 1 and Phase 2 Control */
+    "+PSS",     /* V.250 6.8.8 - V.92 Use Short Sequence */
+    "+SAC",     /* V.252 3.4 - Audio transmit configuration */
+    "+SAM",     /* V.252 3.5 - Audio receive mode */
+    "+SAR",     /* V.252 5.3 - Audio receive channel indication */
+    "+SARR",    /* V.252 3.9 - Audio indication reporting */
+    "+SAT",     /* V.252 5.4 - Audio transmit channel indication */
+    "+SCRR",    /* V.252 3.11 - Capabilities indication reporting */
+    "+SDC",     /* V.252 3.3 - Data configuration */
+    "+SDI",     /* V.252 5.2 - Data channel identification */
+    "+SDR",     /* V.252 3.8 - Data indication reporting */
+    "+SRSC",    /* V.252 5.1.2 - Remote terminal simultaneous capability indication */
+    "+STC",     /* V.252 3.1 - Terminal configuration */
+    "+STH",     /* V.252 3.2 - Close logical channel */
+    "+SVC",     /* V.252 3.6 - Video transmit configuration */
+    "+SVM",     /* V.252 3.7 - Video receive mode */
+    "+SVR",     /* V.252 5.5 - Video receive channel indication */
+    "+SVRR",    /* V.252 3.10 - Video indication reporting */
+    "+SVT",     /* V.252 5.6 - Video transmit channel indication */
+    "+TADR",    /* V.250 6.7.2.9 - Local V.54 address */ 
+    "+TAL",     /* V.250 6.7.2.15 - Local analogue loop */ 
+    "+TALS",    /* V.250 6.7.2.6 - Analogue loop status */ 
+    "+TDLS",    /* V.250 6.7.2.7 - Local digital loop status */ 
+    "+TE140",   /* V.250 6.7.2.1 - Enable ckt 140 */ 
+    "+TE141",   /* V.250 6.7.2.2 - Enable ckt 141 */ 
+    "+TEPAL",   /* V.250 6.7.2.5 - Enable front panel analogue loop */ 
+    "+TEPDL",   /* V.250 6.7.2.4 - Enable front panel RDL */ 
+    "+TERDL",   /* V.250 6.7.2.3 - Enable RDL from remote */ 
+    "+TLDL",    /* V.250 6.7.2.13 - Local digital loop */ 
+    "+TMO",     /* V.250 6.9 - V.59 command */
+    "+TMODE",   /* V.250 6.7.2.10 - Set V.54 mode */ 
+    "+TNUM",    /* V.250 6.7.2.12 - Errored bit and block counts */ 
+    "+TRDL",    /* V.250 6.7.2.14 - Request remote digital loop */ 
+    "+TRDLS",   /* V.250 6.7.2.8 - Remote digital loop status */ 
+    "+TRES",    /* V.250 6.7.2.17 - Self test result */ 
+    "+TSELF",   /* V.250 6.7.2.16 - Self test */ 
+    "+TTER",    /* V.250 6.7.2.11 - Test error rate */ 
+    "+VAC",     /* V.252 4.1 - Set audio code */
+    "+VACR",    /* V.252 6.1 - Audio code report */
+    "+VBT",     /* 3GPP TS 27.007 C.2.2 - Buffer threshold setting */
+    "+VCID",    /* V.253 9.2.3 - Caller ID service */
+    "+VCIDR",   /* V.252 6.2 - Caller ID report */
+    "+VDID",    /* V.253 9.2.4 - DID service */
+    "+VDIDR",   /* V.252 6.2 - DID report */
+    "+VDR",     /* V.253 10.3.1 - Distinctive ring (ring cadence reporting) */
+    "+VDT",     /* V.253 10.3.2 - Control tone cadence reporting */
+    "+VDX",     /* V.253 10.5.6 - Speakerphone duplex mode */
+    "+VEM",     /* V.253 10.5.7 - Deliver event reports */
+    "+VGM",     /* V.253 10.5.2 - Microphone gain */
+    "+VGR",     /* V.253 10.2.1 - Receive gain selection */
+    "+VGS",     /* V.253 10.5.3 - Speaker gain */
+    "+VGT",     /* V.253 10.2.2 - Volume selection */
+    "+VHC",     /* V.252 4.12 - Telephony port hook control */
+    "+VIP",     /* V.253 10.1.1 - Initialize voice parameters */
+    "+VIT",     /* V.253 10.2.3 - DTE/DCE inactivity timer */
+    "+VLS",     /* V.253 10.2.4 - Analogue source/destination selection */
+    "+VNH",     /* V.253 9.2.5 - Automatic hangup control */
+    "+VPH",     /* V.252 4.11 - Phone hookswitch status */
+    "+VPP",     /* V.253 10.4.2 - Voice packet protocol */
+    "+VPR",     /* IS-101 10.4.3 - Select DTE/DCE interface rate */
+    "+VRA",     /* V.253 10.2.5 - Ringing tone goes away timer */
+    "+VRID",    /* Extension - Find the originating and destination numbers */
+    "+VRL",     /* V.253 10.1.2 - Ring local phone */
+    "+VRN",     /* V.253 10.2.6 - Ringing tone never appeared timer */
+    "+VRX",     /* V.253 10.1.3 - Voice receive state */
+    "+VSD",     /* V.253 10.2.7 - Silence detection (QUIET and SILENCE) */
+    "+VSID",    /* Extension - Set the originating number */
+    "+VSM",     /* V.253 10.2.8 - Compression method selection */
+    "+VSP",     /* V.253 10.5.1 - Voice speakerphone state */
+    "+VTA",     /* V.253 10.5.4 - Train acoustic echo-canceller */ 
+    "+VTD",     /* V.253 10.2.9 - Beep tone duration timer */
+    "+VTER",    /* V.252 6.4 - Simple telephony event report */
+    "+VTH",     /* V.253 10.5.5 - Train line echo-canceller */ 
+    "+VTR",     /* V.253 10.1.4 - Voice duplex state */
+    "+VTS",     /* V.253 10.1.5 - DTMF and tone generation in voice */
+    "+VTX",     /* V.253 10.1.6 - Transmit data state */
+    "+VXT",     /* IS-101 10.1.5 - Translate voice data */
+    "+W",       /* TIA-678 5.2.4.1 - Compliance indication */
+    "+WBAG",    /* TIA-678 C.5.6 Bias modem audio gain */
+    "+WCDA",    /* TIA-678 B.3.2.5 Display data link address */
+    "+WCHG",    /* TIA-678 B.3.2.4 Display battery charging status */
+    "+WCID",    /* TIA-678 B.3.2.1 Display system ID (operator) */
+    "+WCLK",    /* TIA-678 B.3.2.3 Lock/unlock DCE */
+    "+WCPN",    /* TIA-678 B.3.2.2 Set personal identification number */
+    "+WCXF",    /* TIA-678 B.3.2.6 Display supported annex B commands */
+    "+WDAC",    /* TIA-678 C.5.1 Data over analogue cellular command query */
+    "+WDIR",    /* TIA-678 C.5.8 Phone number directory selection */
+    "+WECR",    /* TIA-678 C.5.3 Enable cellular result codes */
+    "+WFON",    /* TIA-678 C.5.5 Phone specification */
+    "+WKPD",    /* TIA-678 C.5.7 Keypad emulation */
+    "+WPBA",    /* TIA-678 C.5.9 Phone battery query */
+    "+WPTH",    /* TIA-678 C.5.10 Call path */
+    "+WRLK",    /* TIA-678 C.5.4 Roam lockout */
+    "+WS45",    /* TIA-678 5.2.4.2 DTE-side stack selection */
+    "+WS46",    /* 3GPP TS 27.007 5.9 - PCCA STD-101 [17] select wireless network */
+    "+WS50",    /* TIA-678 B.3.1.1 Normalized signal strength */
+    "+WS51",    /* TIA-678 B.3.1.2 Carrier detect signal threshold */
+    "+WS52",    /* TIA-678 B.3.1.3 Normalized battery level */
+    "+WS53",    /* TIA-678 B.3.1.4 Normalized channel quality */
+    "+WS54",    /* TIA-678 B.3.1.5 Carrier detect channel quality threshold */
+    "+WS57",    /* TIA-678 B.3.1.7 Antenna preference */
+    "+WS58",    /* TIA-678 B.3.1.8 Idle time-out value */
+    "+WSTL",    /* TIA-678 C.5.2 Call session time limit */
+    ";",        /* Dummy to absorb semi-colon delimiters in commands */
+    "A",        /* V.250 6.3.5 - Answer */ 
+    "D",        /* V.250 6.3.1 - Dial */ 
+    "E",        /* V.250 6.2.4 - Command echo */ 
+    "H",        /* V.250 6.3.6 - Hook control */ 
+    "I",        /* V.250 6.1.3 - Request identification information */ 
+    "L",        /* V.250 6.3.13 - Monitor speaker loudness */ 
+    "M",        /* V.250 6.3.14 - Monitor speaker mode */ 
+    "O",        /* V.250 6.3.7 - Return to online data state */ 
+    "P",        /* V.250 6.3.3 - Select pulse dialling (command) */ 
+    "Q",        /* V.250 6.2.5 - Result code suppression */ 
+    "S0",       /* V.250 6.3.8 - Automatic answer */ 
+    "S10",      /* V.250 6.3.12 - Automatic disconnect delay */ 
+    "S3",       /* V.250 6.2.1 - Command line termination character */ 
+    "S4",       /* V.250 6.2.2 - Response formatting character */ 
+    "S5",       /* V.250 6.2.3 - Command line editing character */ 
+    "S6",       /* V.250 6.3.9 - Pause before blind dialling */ 
+    "S7",       /* V.250 6.3.10 - Connection completion timeout */ 
+    "S8",       /* V.250 6.3.11 - Comma dial modifier time */ 
+    "T",        /* V.250 6.3.2 - Select tone dialling (command) */ 
+    "V",        /* V.250 6.2.6 - DCE response format */ 
+    "X",        /* V.250 6.2.7 - Result code selection and call progress monitoring control */ 
+    "Z",        /* V.250 6.1.1 - Reset to default configuration */
+    NULL
+};
+
+int packed_ptr = 0;
+
+short int packed_trie[30000];
+
+#define ALPHABET_SIZE       128
+
+typedef struct trie_node_s
+{
+    int first;
+    int last;
+    int node_no;
+    int entry;
+    /* Array of pointers to children */
+    struct trie_node_s *child_list[ALPHABET_SIZE];
+} trie_node_t;
+
+typedef struct
+{
+    int entries;
+    /* The root of the trie */
+    trie_node_t *root;
+} trie_t;
+
+static trie_node_t *trie_node_create(void)
+{
+    trie_node_t *s;
+    
+    if ((s = (trie_node_t *) malloc(sizeof(*s))))
+    {
+        memset(s, 0, sizeof(*s));
+        s->first = ALPHABET_SIZE - 1;
+    }
+    return s;
+}
+/*- End of function --------------------------------------------------------*/
+
+static trie_t *trie_create(void)
+{
+    trie_t *s;
+    
+    if ((s = (trie_t *) malloc(sizeof(*s))))
+    {
+        memset(s, 0, sizeof(*s));
+        s->root = trie_node_create();
+    }
+    return s;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void trie_recursive_add_node_numbers(trie_node_t *t)
+{
+    int index;
+ 
+    if (t)
+    {
+        if (t->first <= t->last)
+        {
+            t->node_no = packed_ptr + 1;
+            packed_ptr += (t->last - t->first + 1 + 3);
+            for (index = 0;  index < ALPHABET_SIZE;  index++)
+                trie_recursive_add_node_numbers(t->child_list[index]);
+        }
+        else
+        {
+            t->node_no = packed_ptr + 1;
+            packed_ptr += 3;
+        }
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static void trie_recursive_build_packed_trie(trie_node_t *t)
+{
+    int i;
+ 
+    if (t)
+    {
+        if (t->first <= t->last)
+        {
+            packed_trie[packed_ptr++] = t->first;
+            packed_trie[packed_ptr++] = t->last;
+            packed_trie[packed_ptr++] = t->entry;
+            for (i = t->first;  i <= t->last;  i++)
+                packed_trie[packed_ptr++] = (t->child_list[i])  ?  t->child_list[i]->node_no  :  0;
+            for (i = t->first;  i <= t->last;  i++)
+                trie_recursive_build_packed_trie(t->child_list[i]);
+        }
+        else
+        {
+            packed_trie[packed_ptr++] = 1;
+            packed_trie[packed_ptr++] = 0;
+            packed_trie[packed_ptr++] = t->entry;
+        }
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static void trie_add(trie_t *s, const char *u, size_t len)
+{
+    int i;
+    int index;
+    trie_node_t *t;
+
+    s->entries++;
+    /* Loop over the length of the string to add and traverse the trie... */
+    for (t = s->root, i = 0;  i < len;  i++)
+    {
+        /* The character in u we are processing... */
+        index = (unsigned char) u[i];
+ 
+        /* Is there a child node for this character? */
+        if (t->child_list[index] == NULL)
+        {
+            t->child_list[index] = trie_node_create();
+            if (index < t->first)
+                t->first = index;
+            if (index > t->last)
+                t->last = index;
+        }
+ 
+        /* Move to the new node... and loop */
+        t = t->child_list[index];
+    }
+    t->entry = s->entries;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void dump_trie(void)
+{
+    int i;
+
+    printf("\nstatic const at_cmd_service_t at_commands[] =\n{\n");
+    for (i = 0;  wordlist[i];  i++)
+    {
+        switch (wordlist[i][0])
+        {
+        case ' ':
+        case ';':
+            printf("    at_cmd_dummy,\n");
+            break;
+        case '+':
+            printf("    at_cmd_plus_%s,\n", wordlist[i] + 1);
+            break;
+        case '&':
+            printf("    at_cmd_amp_%s,\n", wordlist[i] + 1);
+            break;
+        default:
+            printf("    at_cmd_%s,\n", wordlist[i]);
+            break;
+        }
+    }
+    printf("};\n");
+
+    printf("\nstatic const uint16_t command_trie[] =\n{");
+    for (i = 0;  i < packed_ptr;  i++)
+    {
+        if ((i & 7) == 0)
+            printf("\n    ");
+        printf("0x%04X, ", packed_trie[i]);
+    }
+    printf("\n};\n");
+    printf("\n#define COMMAND_TRIE_LEN %d\n", packed_ptr);
+
+}
+/*- End of function --------------------------------------------------------*/
+
+int main(int argc, char *argv[])
+{
+    trie_t *s;
+    int i;
+    
+    s = trie_create();
+
+    for (i = 0;  wordlist[i];  i++)
+        trie_add(s, wordlist[i], strlen(wordlist[i]));
+    printf("// The trie contains %d entries\n", i);
+
+    packed_ptr = 0;
+    trie_recursive_add_node_numbers(s->root);
+    packed_ptr = 0;
+    trie_recursive_build_packed_trie(s->root);
+
+    dump_trie();
+
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/make_modem_filter.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/make_modem_filter.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,412 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * make_modem_filter.c - Create coefficient sets for pulse shaping
+ *                       various modem rx and tx signals.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2008 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: make_modem_filter.c,v 1.11 2008/07/02 14:48:25 steveu Exp $
+ */
+
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
+
+#include <inttypes.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include "floating_fudge.h"
+#if defined(HAVE_TGMATH_H)
+#include <tgmath.h>
+#endif
+#if defined(HAVE_MATH_H)
+#include <math.h>
+#endif
+#include <string.h>
+#include <stdio.h>
+#include <time.h>
+#include <fcntl.h>
+
+#include "spandsp/telephony.h"
+#include "spandsp/complex.h"
+#include "filter_tools.h"
+
+#define FALSE 0
+#define TRUE (!FALSE)
+
+//#define SAMPLE_RATE         8000.0
+#define MAX_COEFFS_PER_FILTER   128
+#define MAX_COEFF_SETS          384
+
+static void make_tx_filter(int coeff_sets,
+                           int coeffs_per_filter,
+                           double carrier,
+                           double baud_rate,
+                           double excess_bandwidth,
+                           int fixed_point,
+                           const char *tag)
+{
+    int i;
+    int j;
+    int x;
+    int total_coeffs;
+    double alpha;
+    double beta;
+    double gain;
+    double scaling;
+    double peak;
+    double coeffs[MAX_COEFF_SETS*MAX_COEFFS_PER_FILTER + 1];
+
+    total_coeffs = coeff_sets*coeffs_per_filter + 1;
+    alpha = baud_rate/(2.0*(double) (coeff_sets*baud_rate));
+    beta = excess_bandwidth;
+
+    compute_raised_cosine_filter(coeffs, total_coeffs, TRUE, FALSE, alpha, beta);
+
+    /* Find the DC gain of the filter, and adjust the filter to unity gain. */
+    gain = 0.0;
+    for (i = coeff_sets/2;  i < total_coeffs;  i += coeff_sets)
+        gain += coeffs[i];
+    /* Normalise the gain to 1.0 */
+    for (i = 0;  i < total_coeffs;  i++)
+        coeffs[i] /= gain;
+    gain = 1.0;
+
+    if (fixed_point)
+    {
+        peak = -1.0;
+        for (i = 0;  i < total_coeffs;  i++)
+        {
+            if (fabs(coeffs[i]) > peak)
+                peak = fabs(coeffs[i]);
+        }
+        scaling = 32767.0;
+        if (peak >= 1.0)
+        {
+            scaling /= peak;
+            gain = 1.0/peak;
+        }
+        for (i = 0;  i < total_coeffs;  i++)
+            coeffs[i] *= scaling;
+    }
+    
+    /* Churn out the data as a C source code header file, which can be directly included by the
+       modem code. */
+    printf("#define TX_PULSESHAPER%s_GAIN        %ff\n", tag, gain);
+    printf("#define TX_PULSESHAPER%s_COEFF_SETS  %d\n", tag, coeff_sets);
+    printf("static const %s tx_pulseshaper%s[TX_PULSESHAPER%s_COEFF_SETS][%d] =\n",
+           (fixed_point)  ?  "int16_t"  :  "float",
+           tag,
+           tag,
+           coeffs_per_filter);
+    printf("{\n");
+    for (j = 0;  j < coeff_sets;  j++)
+    {
+        x = j;
+        printf("    {\n");
+        if (fixed_point)
+            printf("        %8d,     /* Filter %d */\n", (int) coeffs[x], j);
+        else
+            printf("        %15.10ff,     /* Filter %d */\n", coeffs[x], j);
+        for (i = 1;  i < coeffs_per_filter - 1;  i++)
+        {
+            x = i*coeff_sets + j;
+            if (fixed_point)
+                printf("        %8d,\n", (int) coeffs[x]);
+            else
+                printf("        %15.10ff,\n", coeffs[x]);
+        }
+        x = i*coeff_sets + j;
+        if (fixed_point)
+            printf("        %8d\n", (int) coeffs[x]);
+        else
+            printf("        %15.10ff\n", coeffs[x]);
+        if (j < coeff_sets - 1)
+            printf("    },\n");
+        else
+            printf("    }\n");
+    }
+    printf("};\n");
+}
+/*- End of function --------------------------------------------------------*/
+
+static void make_rx_filter(int coeff_sets,
+                           int coeffs_per_filter,
+                           double carrier,
+                           double baud_rate,
+                           double excess_bandwidth,
+                           int fixed_point,
+                           const char *tag)
+{
+    int i;
+    int j;
+    int m;
+    int x;
+    int total_coeffs;
+    double alpha;
+    double beta;
+    double gain;
+    double peak;
+    double coeffs[MAX_COEFF_SETS*MAX_COEFFS_PER_FILTER + 1];
+    complex_t co[MAX_COEFFS_PER_FILTER];
+
+    total_coeffs = coeff_sets*coeffs_per_filter + 1;
+    alpha = baud_rate/(2.0*(double) (coeff_sets*SAMPLE_RATE));
+    beta = excess_bandwidth;
+    carrier *= 2.0*3.1415926535/SAMPLE_RATE;
+
+    compute_raised_cosine_filter(coeffs, total_coeffs, TRUE, FALSE, alpha, beta);
+
+    /* Find the DC gain of the filter, and adjust the filter to unity gain. */
+    gain = 0.0;
+    for (i = coeff_sets/2;  i < total_coeffs;  i += coeff_sets)
+        gain += coeffs[i];
+    /* Normalise the gain to 1.0 */
+    for (i = 0;  i < total_coeffs;  i++)
+        coeffs[i] /= gain;
+    gain = 1.0;
+
+    if (fixed_point)
+    {
+        peak = -1.0;
+        for (i = 0;  i < total_coeffs;  i++)
+        {
+            if (fabs(coeffs[i]) > peak)
+                peak = fabs(coeffs[i]);
+        }
+        gain = 32767.0;
+        if (peak >= 1.0)
+            gain /= peak;
+        for (i = 0;  i < total_coeffs;  i++)
+            coeffs[i] *= gain;
+    }
+
+    /* Churn out the data as a C source code header file, which can be directly included by the
+       modem code. */
+    printf("#define RX_PULSESHAPER%s_GAIN        %ff\n", tag, gain);
+    printf("#define RX_PULSESHAPER%s_COEFF_SETS  %d\n", tag, coeff_sets);
+    printf("static const %s rx_pulseshaper%s[RX_PULSESHAPER%s_COEFF_SETS][%d] =\n",
+           (fixed_point)  ?  "complexi16_t"  :  "complexf_t",
+           tag,
+           tag,
+           coeffs_per_filter);
+    printf("{\n");
+    for (j = 0;  j < coeff_sets;  j++)
+    {
+        /* Complex modulate the filter, to make it a complex pulse shaping bandpass filter
+           centred at the nominal carrier frequency. Use the same phase for all the coefficient
+           sets. This means the modem can step the carrier in whole samples, and not worry about
+           the fractional sample shift caused by selecting amongst the various coefficient sets. */
+        for (i = 0;  i < coeffs_per_filter;  i++)
+        {
+            m = i - (coeffs_per_filter >> 1);
+            x = i*coeff_sets + j;
+            co[i].re = coeffs[x]*cos(carrier*m);
+            co[i].im = coeffs[x]*sin(carrier*m);
+        }
+        printf("    {\n");
+        if (fixed_point)
+            printf("        {%8d, %8d},     /* Filter %d */\n", (int) co[i].re, (int) co[i].im, j);
+        else
+            printf("        {%15.10ff, %15.10ff},     /* Filter %d */\n", co[0].re, co[0].im, j);
+        for (i = 1;  i < coeffs_per_filter - 1;  i++)
+        {
+            if (fixed_point)
+                printf("        {%8d, %8d},\n", (int) co[i].re, (int) co[i].im);
+            else
+                printf("        {%15.10ff, %15.10ff},\n", co[i].re, co[i].im);
+        }
+        if (fixed_point)
+            printf("        {%8d, %8d}\n", (int) co[i].re, (int) co[i].im);
+        else
+            printf("        {%15.10ff, %15.10ff}\n", co[i].re, co[i].im);
+        if (j < coeff_sets - 1)
+            printf("    },\n");
+        else
+            printf("    }\n");
+    }
+    printf("};\n");
+}
+/*- End of function --------------------------------------------------------*/
+
+static void usage(void)
+{
+    fprintf(stderr, "Usage: make_modem_rx_filter -m <V.17 | V.22bis | V.22bis1200 | V.22bis2400 | V.27ter2400 | V.27ter4800 | V.29> [-i] [-r] [-t]\n");
+}
+/*- End of function --------------------------------------------------------*/
+
+int main(int argc, char **argv)
+{
+    int rx_coeff_sets;
+    int rx_coeffs_per_filter;
+    int tx_coeff_sets;
+    int tx_coeffs_per_filter;
+    int opt;
+    int transmit_modem;
+    int fixed_point;
+    double carrier;
+    double baud_rate;
+    double rx_excess_bandwidth;
+    double tx_excess_bandwidth;
+    const char *tag;
+    const char *modem;
+
+    fixed_point = FALSE;
+    transmit_modem = FALSE;
+    modem = "";
+    while ((opt = getopt(argc, argv, "im:rt")) != -1)
+    {
+        switch (opt)
+        {
+        case 'i':
+            fixed_point = TRUE;
+            break;
+        case 'm':
+            modem = optarg;
+            break;
+        case 'r':
+            transmit_modem = FALSE;
+            break;
+        case 't':
+            transmit_modem = TRUE;
+            break;
+        default:
+            usage();
+            exit(2);
+            break;
+        }
+    }
+    if (strcmp(modem, "V.17") == 0)
+    {
+        rx_coeff_sets = 192;
+        rx_coeffs_per_filter = 27;
+        rx_excess_bandwidth = 0.5;
+        tx_coeff_sets = 10;
+        tx_coeffs_per_filter = 9;
+        tx_excess_bandwidth = 0.25;
+        carrier = 1800.0;
+        baud_rate = 2400.0;
+        tag = "";
+    }
+    else if (strcmp(modem, "V.22bis") == 0)
+    {
+        /* This is only intended to apply to transmit. */
+        rx_coeff_sets = 12;
+        rx_coeffs_per_filter = 37;
+        rx_excess_bandwidth = 0.75;
+        tx_coeff_sets = 40;
+        tx_coeffs_per_filter = 9;
+        tx_excess_bandwidth = 0.75;
+        carrier = 1200.0;
+        baud_rate = 600.0;
+        tag = "";
+    }
+    else if (strcmp(modem, "V.22bis1200") == 0)
+    {
+        /* This is only intended to apply to receive. */
+        rx_coeff_sets = 12;
+        rx_coeffs_per_filter = 37;
+        rx_excess_bandwidth = 0.75;
+        tx_coeff_sets = 40;
+        tx_coeffs_per_filter = 9;
+        tx_excess_bandwidth = 0.75;
+        carrier = 1200.0;
+        baud_rate = 600.0;
+        tag = "_1200";
+    }
+    else if (strcmp(modem, "V.22bis2400") == 0)
+    {
+        /* This is only intended to apply to receive. */
+        rx_coeff_sets = 12;
+        rx_coeffs_per_filter = 37;
+        rx_excess_bandwidth = 0.75;
+        tx_coeff_sets = 40;
+        tx_coeffs_per_filter = 9;
+        tx_excess_bandwidth = 0.75;
+        carrier = 2400.0;
+        baud_rate = 600.0;
+        tag = "_2400";
+    }
+    else if (strcmp(modem, "V.27ter4800") == 0)
+    {
+        rx_coeff_sets = 8;
+        rx_coeffs_per_filter = 27;
+        rx_excess_bandwidth = 0.5;
+        tx_coeff_sets = 5;
+        tx_coeffs_per_filter = 9;
+        tx_excess_bandwidth = 0.5;
+        carrier = 1800.0;
+        baud_rate = 1600.0;
+        tag = "_4800";
+    }
+    else if (strcmp(modem, "V.27ter2400") == 0)
+    {
+        rx_coeff_sets = 12;
+        rx_coeffs_per_filter = 27;
+        rx_excess_bandwidth = 0.5;
+        tx_coeff_sets = 20;
+        tx_coeffs_per_filter = 9;
+        tx_excess_bandwidth = 0.5;
+        carrier = 1800.0;
+        baud_rate = 1200.0;
+        tag = "_2400";
+    }
+    else if (strcmp(modem, "V.29") == 0)
+    {
+        rx_coeff_sets = 48;
+        rx_coeffs_per_filter = 27;
+        rx_excess_bandwidth = 0.5;
+        tx_coeff_sets = 10;
+        tx_coeffs_per_filter = 9;
+        tx_excess_bandwidth = 0.25;
+        carrier = 1700.0;
+        baud_rate = 2400.0;
+        tag = "";
+    }
+    else
+    {
+        usage();
+        exit(2);
+    }
+    if (transmit_modem)
+    {
+        make_tx_filter(tx_coeff_sets,
+                       tx_coeffs_per_filter,
+                       carrier,
+                       baud_rate,
+                       tx_excess_bandwidth,
+                       fixed_point,
+                       tag);
+    }
+    else
+    {
+        make_rx_filter(rx_coeff_sets,
+                       rx_coeffs_per_filter,
+                       carrier,
+                       baud_rate,
+                       rx_excess_bandwidth,
+                       fixed_point,
+                       tag);
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/modem_connect_tones.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/modem_connect_tones.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,549 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * modem_connect_tones.c - Generation and detection of tones
+ * associated with modems calling and answering calls.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2006 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: modem_connect_tones.c,v 1.27 2008/08/13 14:55:51 steveu Exp $
+ */
+ 
+/*! \file */
+
+#if defined(HAVE_CONFIG_H)
+#include <config.h>
+#endif
+
+#include <inttypes.h>
+#include <stdlib.h>
+#include <memory.h>
+#include "floating_fudge.h"
+#if defined(HAVE_TGMATH_H)
+#include <tgmath.h>
+#endif
+#if defined(HAVE_MATH_H)
+#include <math.h>
+#endif
+#include <stdio.h>
+
+#include "spandsp/telephony.h"
+#include "spandsp/logging.h"
+#include "spandsp/complex.h"
+#include "spandsp/dds.h"
+#include "spandsp/tone_detect.h"
+#include "spandsp/tone_generate.h"
+#include "spandsp/super_tone_rx.h"
+#include "spandsp/power_meter.h"
+#include "spandsp/async.h"
+#include "spandsp/fsk.h"
+#include "spandsp/modem_connect_tones.h"
+
+#define HDLC_FRAMING_OK_THRESHOLD       5
+
+const char *modem_connect_tone_to_str(int tone)
+{
+    switch (tone)
+    {
+    case MODEM_CONNECT_TONES_NONE:
+        return "No tone";
+    case MODEM_CONNECT_TONES_FAX_CNG:
+        return "FAX CNG";
+    case MODEM_CONNECT_TONES_ANS:
+        return "ANS or FAX CED";
+    case MODEM_CONNECT_TONES_ANS_PR:
+        return "ANS/";
+    case MODEM_CONNECT_TONES_ANSAM:
+        return "ANSam";
+    case MODEM_CONNECT_TONES_ANSAM_PR:
+        return "ANSam/";
+    case MODEM_CONNECT_TONES_FAX_PREAMBLE:
+        return "FAX preamble";
+    case MODEM_CONNECT_TONES_FAX_CED_OR_PREAMBLE:
+        return "FAX CED or preamble";
+    }
+    return "???";
+}
+/*- End of function --------------------------------------------------------*/
+
+int modem_connect_tones_tx(modem_connect_tones_tx_state_t *s,
+                           int16_t amp[],
+                           int len)
+{
+    int mod;
+    int i;
+    int xlen;
+
+    i = 0;
+    switch (s->tone_type)
+    {
+    case MODEM_CONNECT_TONES_FAX_CNG:
+        for (  ;  i < len;  i++)
+        {
+            if (s->duration_timer > ms_to_samples(3000))
+            {
+                if ((xlen = i + s->duration_timer - ms_to_samples(3000)) > len)
+                    xlen = len;
+                s->duration_timer -= (xlen - i);
+                for (  ;  i < xlen;  i++)
+                    amp[i] = dds_mod(&s->tone_phase, s->tone_phase_rate, s->level, 0);
+            }
+            if (s->duration_timer > 0)
+            {
+                if ((xlen = i + s->duration_timer) > len)
+                    xlen = len;
+                s->duration_timer -= (xlen - i);
+                memset(amp + i, 0, sizeof(int16_t)*(xlen - i));
+                i = xlen;
+            }
+            if (s->duration_timer == 0)
+                s->duration_timer = ms_to_samples(500 + 3000);
+        }
+        break;
+    case MODEM_CONNECT_TONES_ANS:
+        if (s->duration_timer < len)
+            len = s->duration_timer;
+        if (s->duration_timer > ms_to_samples(2600))
+        {
+            /* There is some initial silence to be generated. */
+            if ((i = s->duration_timer - ms_to_samples(2600)) > len)
+                i = len;
+            memset(amp, 0, sizeof(int16_t)*i);
+        }
+        for (  ;  i < len;  i++)
+            amp[i] = dds_mod(&s->tone_phase, s->tone_phase_rate, s->level, 0);
+        s->duration_timer -= len;
+        break;
+    case MODEM_CONNECT_TONES_ANS_PR:
+        if (s->duration_timer < len)
+            len = s->duration_timer;
+        if (s->duration_timer > ms_to_samples(3300))
+        {
+            if ((i = s->duration_timer - ms_to_samples(3300)) > len)
+                i = len;
+            memset(amp, 0, sizeof(int16_t)*i);
+        }
+        for (  ;  i < len;  i++)
+        {
+            if (--s->hop_timer <= 0)
+            {
+                s->hop_timer = ms_to_samples(450);
+                s->tone_phase += 0x80000000;
+            }
+            amp[i] = dds_mod(&s->tone_phase, s->tone_phase_rate, s->level, 0);
+        }
+        s->duration_timer -= len;
+        break;
+    case MODEM_CONNECT_TONES_ANSAM:
+        if (s->duration_timer < len)
+            len = s->duration_timer;
+        if (s->duration_timer > ms_to_samples(3300))
+        {
+            if ((i = s->duration_timer - ms_to_samples(3300)) > len)
+                i = len;
+            memset(amp, 0, sizeof(int16_t)*i);
+        }
+        for (  ;  i < len;  i++)
+        {
+            mod = s->level + dds_mod(&s->mod_phase, s->mod_phase_rate, s->mod_level, 0);
+            amp[i] = dds_mod(&s->tone_phase, s->tone_phase_rate, mod, 0);
+        }
+        s->duration_timer -= len;
+        break;
+    case MODEM_CONNECT_TONES_ANSAM_PR:
+        if (s->duration_timer < len)
+            len = s->duration_timer;
+        if (s->duration_timer > ms_to_samples(3300))
+        {
+            if ((i = s->duration_timer - ms_to_samples(3300)) > len)
+                i = len;
+            memset(amp, 0, sizeof(int16_t)*i);
+        }
+        for (  ;  i < len;  i++)
+        {
+            if (--s->hop_timer <= 0)
+            {
+                s->hop_timer = ms_to_samples(450);
+                s->tone_phase += 0x80000000;
+            }
+            mod = s->level + dds_mod(&s->mod_phase, s->mod_phase_rate, s->mod_level, 0);
+            amp[i] = dds_mod(&s->tone_phase, s->tone_phase_rate, mod, 0);
+        }
+        s->duration_timer -= len;
+        break;
+    }
+    return len;
+}
+/*- End of function --------------------------------------------------------*/
+
+modem_connect_tones_tx_state_t *modem_connect_tones_tx_init(modem_connect_tones_tx_state_t *s,
+                                                            int tone_type)
+{
+    int alloced;
+
+    alloced = FALSE;
+    if (s == NULL)
+    {
+        if ((s = (modem_connect_tones_tx_state_t *) malloc(sizeof(*s))) == NULL)
+            return NULL;
+        alloced = TRUE;
+    }
+    s->tone_type = tone_type;
+    switch (s->tone_type)
+    {
+    case MODEM_CONNECT_TONES_FAX_CNG:
+        /* 0.5s of 1100Hz+-38Hz + 3.0s of silence repeating. Timing +-15% */
+        s->tone_phase_rate = dds_phase_rate(1100.0);
+        s->level = dds_scaling_dbm0(-11);
+        s->duration_timer = ms_to_samples(500 + 3000);
+        s->mod_phase_rate = 0;
+        s->tone_phase = 0;
+        s->mod_phase = 0;
+        s->mod_level = 0;
+        break;
+    case MODEM_CONNECT_TONES_ANS:
+    case MODEM_CONNECT_TONES_ANSAM:
+        /* 0.2s of silence, then 2.6s to 4s of 2100Hz+-15Hz tone, then 75ms of silence. */
+        s->tone_phase_rate = dds_phase_rate(2100.0);
+        s->level = dds_scaling_dbm0(-11);
+        s->duration_timer = ms_to_samples(200 + 2600);
+        s->mod_phase_rate = dds_phase_rate(15.0);
+        s->tone_phase = 0;
+        s->mod_phase = 0;
+        s->mod_level = (s->tone_type == MODEM_CONNECT_TONES_ANSAM)  ?  s->level/5  :  0;
+        break;
+    case MODEM_CONNECT_TONES_ANS_PR:
+    case MODEM_CONNECT_TONES_ANSAM_PR:
+        s->tone_phase_rate = dds_phase_rate(2100.0);
+        s->level = dds_scaling_dbm0(-12);
+        s->duration_timer = ms_to_samples(200 + 3300);
+        s->mod_phase_rate = dds_phase_rate(15.0);
+        s->tone_phase = 0;
+        s->mod_phase = 0;
+        s->hop_timer = ms_to_samples(450);
+        s->mod_level = (s->tone_type == MODEM_CONNECT_TONES_ANSAM_PR)  ?  s->level/5  :  0;
+        break;
+    default:
+        if (alloced)
+            free(s);
+        return NULL;
+    }
+    return s;
+}
+/*- End of function --------------------------------------------------------*/
+
+int modem_connect_tones_tx_free(modem_connect_tones_tx_state_t *s)
+{
+    free(s);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void report_tone_state(modem_connect_tones_rx_state_t *s, int tone, int level)
+{
+    if (tone != s->tone_present)
+    {
+        if (s->tone_callback)
+        {
+            s->tone_callback(s->callback_data, tone, level, 0);
+        }
+        else
+        {
+            if (tone != MODEM_CONNECT_TONES_NONE)
+                s->hit = tone;
+        }
+        s->tone_present = tone;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static void v21_put_bit(void *user_data, int bit)
+{
+    modem_connect_tones_rx_state_t *s;
+
+    s = (modem_connect_tones_rx_state_t *) user_data;
+    if (bit < 0)
+    {
+        /* Special conditions. */
+        switch (bit)
+        {
+        case PUTBIT_CARRIER_DOWN:
+            /* Only declare tone off, if we were the one to declare tone on. */
+            if (s->tone_present == MODEM_CONNECT_TONES_FAX_PREAMBLE)
+                report_tone_state(s, MODEM_CONNECT_TONES_NONE, -99);
+            /* Fall through */
+        case PUTBIT_CARRIER_UP:
+            s->raw_bit_stream = 0;
+            s->num_bits = 0;
+            s->flags_seen = 0;
+            s->framing_ok_announced = FALSE;
+            break;
+        }
+        return;
+    }
+    /* Look for enough FAX V.21 message preamble (back to back HDLC flag octets) to be sure
+       we are really seeing preamble, and declare the signal to be present. Any change from
+       preamble declares the signal to not be present, though it will probably be the body
+       of the messages following the preamble. */
+    s->raw_bit_stream = (s->raw_bit_stream << 1) | ((bit << 8) & 0x100);
+    s->num_bits++;
+    if ((s->raw_bit_stream & 0x7F00) == 0x7E00)
+    {
+        if ((s->raw_bit_stream & 0x8000))
+        {
+            /* Hit HDLC abort */
+            s->flags_seen = 0;
+        }
+        else
+        {
+            /* Hit HDLC flag */
+            if (s->flags_seen < HDLC_FRAMING_OK_THRESHOLD)
+            {
+                /* Check the flags are back-to-back when testing for valid preamble. This
+                   greatly reduces the chances of false preamble detection, and anything
+                   which doesn't send them back-to-back is badly broken. */
+                if (s->num_bits != 8)
+                    s->flags_seen = 0;
+                if (++s->flags_seen >= HDLC_FRAMING_OK_THRESHOLD  &&  !s->framing_ok_announced)
+                {
+                    report_tone_state(s, MODEM_CONNECT_TONES_FAX_PREAMBLE, lrintf(fsk_rx_signal_power(&(s->v21rx))));
+                    s->framing_ok_announced = TRUE;
+                }
+            }
+        }
+        s->num_bits = 0;
+    }
+    else
+    {
+        if (s->flags_seen >= HDLC_FRAMING_OK_THRESHOLD)
+        {
+            if (s->num_bits == 8)
+            {
+                s->framing_ok_announced = FALSE;
+                s->flags_seen = 0;
+            }
+        }
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+int modem_connect_tones_rx(modem_connect_tones_rx_state_t *s, const int16_t amp[], int len)
+{
+    int i;
+    int16_t notched;
+    float v1;
+    float famp;
+    
+    switch (s->tone_type)
+    {
+    case MODEM_CONNECT_TONES_FAX_CNG:
+        for (i = 0;  i < len;  i++)
+        {
+            /* A Cauer notch at 1100Hz, spread just wide enough to meet our detection bandwidth
+               criteria. */
+            famp = amp[i];
+            v1 = 0.792928f*famp + 1.0018744927985f*s->z1 - 0.54196833412465f*s->z2;
+            famp = v1 - 1.2994747954630f*s->z1 + s->z2;
+            s->z2 = s->z1;
+            s->z1 = v1;
+            notched = (int16_t) lrintf(famp);
+
+            /* Estimate the overall energy in the channel, and the energy in
+               the notch (i.e. overall channel energy - tone energy => noise).
+               Use abs instead of multiply for speed (is it really faster?). */
+            s->channel_level += ((abs(amp[i]) - s->channel_level) >> 5);
+            s->notch_level += ((abs(notched) - s->notch_level) >> 5);
+            if (s->channel_level > 70  &&  s->notch_level*6 < s->channel_level)
+            {
+                /* There is adequate energy in the channel, and it is mostly at 1100Hz. */
+                if (s->tone_present != MODEM_CONNECT_TONES_FAX_CNG)
+                {
+                    if (++s->tone_cycle_duration >= ms_to_samples(415))
+                        report_tone_state(s, MODEM_CONNECT_TONES_FAX_CNG, lrintf(log10f(s->channel_level/32768.0f)*20.0f + DBM0_MAX_POWER + 0.8f));
+                }
+            }
+            else
+            {
+                /* If the signal looks wrong, even for a moment, we consider this the
+                   end of the tone. */
+                if (s->tone_present == MODEM_CONNECT_TONES_FAX_CNG)
+                    report_tone_state(s, MODEM_CONNECT_TONES_NONE, -99);
+                s->tone_cycle_duration = 0;
+            }
+        }
+        break;
+    case MODEM_CONNECT_TONES_FAX_CED_OR_PREAMBLE:
+        /* Also look for V.21 preamble. A lot of machines don't send the 2100Hz burst. It
+           might also not be seen all the way through the channel, due to switching delays. */
+        fsk_rx(&(s->v21rx), amp, len);
+        /* Now fall through and look for a 2100Hz tone */
+    case MODEM_CONNECT_TONES_ANS:
+        for (i = 0;  i < len;  i++)
+        {
+            /* A Cauer notch at 2100Hz, spread just wide enough to meet our detection bandwidth
+               criteria. */
+            /* This is actually centred at 2095Hz, but gets the balance we want, due
+               to the asymmetric walls of the notch */
+            famp = amp[i];
+            v1 = 0.76000f*famp - 0.1183852f*s->z1 - 0.5104039f*s->z2;
+            famp = v1 + 0.1567596f*s->z1 + s->z2;
+            s->z2 = s->z1;
+            s->z1 = v1;
+            notched = (int16_t) lrintf(famp);
+            /* Estimate the overall energy in the channel, and the energy in
+               the notch (i.e. overall channel energy - tone energy => noise).
+               Use abs instead of multiply for speed (is it really faster?).
+               Damp the overall energy a little more for a stable result.
+               Damp the notch energy a little less, so we don't damp out the
+               blip every time the phase reverses */
+            s->channel_level += ((abs(amp[i]) - s->channel_level) >> 5);
+            s->notch_level += ((abs(notched) - s->notch_level) >> 4);
+            /* This should cut off at about -43dBm0 */
+            if (s->channel_level <= 70)
+            {
+                /* If the energy level is low, even for a moment, we consider this the
+                   end of the tone. */
+                if (s->tone_present != MODEM_CONNECT_TONES_NONE)
+                    report_tone_state(s, MODEM_CONNECT_TONES_NONE, -99);
+                s->tone_cycle_duration = 0;
+                s->good_cycles = 0;
+                s->tone_on = FALSE;
+                continue;
+            }
+            /* There is adequate energy in the channel. Is it mostly at 2100Hz? */
+            s->tone_cycle_duration++;
+            if (s->notch_level*6 < s->channel_level)
+            {
+                /* The notch test says yes, so we have the tone. */
+                /* We should get a kick from the notch filter every 450+-25ms, as the phase reverses, for an
+                   EC disable tone. For a simple answer tone, the tone should persist unbroken for longer. */
+                if (!s->tone_on)
+                {
+                    if (s->tone_cycle_duration >= ms_to_samples(450 - 25))
+                    {
+                        if (++s->good_cycles == 3)
+                            report_tone_state(s, MODEM_CONNECT_TONES_ANS_PR, lrintf(log10f(s->channel_level/32768.0f)*20.0f + DBM0_MAX_POWER + 0.8f));
+                    }
+                    else
+                    {
+                        s->good_cycles = 0;
+                    }
+                    /* Cycles are timed from rising edge to rising edge */
+                    s->tone_cycle_duration = 0;
+                }
+                else
+                {
+                    if (s->tone_cycle_duration >= ms_to_samples(550))
+                    {
+                        if (s->tone_present == MODEM_CONNECT_TONES_NONE)
+                            report_tone_state(s, MODEM_CONNECT_TONES_ANS, lrintf(log10f(s->channel_level/32768.0f)*20.0f + DBM0_MAX_POWER + 0.8f));
+                        s->good_cycles = 0;
+                        s->tone_cycle_duration = ms_to_samples(550);
+                    }
+                }
+                s->tone_on = TRUE;
+            }
+            else
+            {
+                if (s->tone_present == MODEM_CONNECT_TONES_ANS)
+                {
+                    report_tone_state(s, MODEM_CONNECT_TONES_NONE, -99);
+                    s->good_cycles = 0;
+                }
+                else
+                {
+                    if (s->tone_cycle_duration >= ms_to_samples(450 + 25))
+                    {
+                        /* The change came too late for a cycle of ANS_PR tone */
+                        if (s->tone_present == MODEM_CONNECT_TONES_ANS_PR  ||  s->tone_present == MODEM_CONNECT_TONES_ANSAM_PR)
+                            report_tone_state(s, MODEM_CONNECT_TONES_NONE, -99);
+                        s->good_cycles = 0;
+                    }
+                }
+                s->tone_on = FALSE;
+            }
+        }
+        break;
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int modem_connect_tones_rx_get(modem_connect_tones_rx_state_t *s)
+{
+    int x;
+    
+    x = s->hit;
+    s->hit = MODEM_CONNECT_TONES_NONE;
+    return x;
+}
+/*- End of function --------------------------------------------------------*/
+
+modem_connect_tones_rx_state_t *modem_connect_tones_rx_init(modem_connect_tones_rx_state_t *s,
+                                                            int tone_type,
+                                                            tone_report_func_t tone_callback,
+                                                            void *user_data)
+{
+    if (s == NULL)
+    {
+        if ((s = (modem_connect_tones_rx_state_t *) malloc(sizeof(*s))) == NULL)
+            return NULL;
+    }
+
+    s->tone_type = tone_type;
+    switch (s->tone_type)
+    {
+    case MODEM_CONNECT_TONES_FAX_CED_OR_PREAMBLE:
+        fsk_rx_init(&(s->v21rx), &preset_fsk_specs[FSK_V21CH2], TRUE, v21_put_bit, s);
+        fsk_rx_signal_cutoff(&(s->v21rx), -45.5);
+        break;
+    case MODEM_CONNECT_TONES_ANS_PR:
+    case MODEM_CONNECT_TONES_ANSAM:
+    case MODEM_CONNECT_TONES_ANSAM_PR:
+        /* Treat these all the same for receive purposes */
+        s->tone_type = MODEM_CONNECT_TONES_ANS;
+        break;
+    }
+    s->channel_level = 0;
+    s->notch_level = 0;
+    s->tone_present = MODEM_CONNECT_TONES_NONE;
+    s->tone_cycle_duration = 0;
+    s->good_cycles = 0;
+    s->hit = MODEM_CONNECT_TONES_NONE;
+    s->tone_on = FALSE;
+    s->tone_callback = tone_callback;
+    s->callback_data = user_data;
+    s->z1 = 0.0f;
+    s->z2 = 0.0f;
+    s->num_bits = 0;
+    s->flags_seen = 0;
+    s->framing_ok_announced = FALSE;
+    s->raw_bit_stream = 0;
+    return s;
+}
+/*- End of function --------------------------------------------------------*/
+
+int modem_connect_tones_rx_free(modem_connect_tones_rx_state_t *s)
+{
+    free(s);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/modem_echo.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/modem_echo.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,176 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * modem_echo.c - An echo cancellor, suitable for electrical echos in GSTN modems
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2001, 2003, 2004 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: modem_echo.c,v 1.22 2008/07/02 14:48:25 steveu Exp $
+ */
+
+/*! \file */
+
+/* The FIR taps must be adapted as 32 bit values, to get the necessary finesse
+   in the adaption process. However, they are applied as 16 bit values (bits 30-15
+   of the 32 bit values) in the FIR. For the working 16 bit values, we need 4 sets.
+ */
+
+#if defined(HAVE_CONFIG_H)
+#include <config.h>
+#endif
+
+#include <stdlib.h>
+#include <inttypes.h>
+#include <string.h>
+#include <stdio.h>
+#include "floating_fudge.h"
+#if defined(HAVE_TGMATH_H)
+#include <tgmath.h>
+#endif
+#if defined(HAVE_MATH_H)
+#include <math.h>
+#endif
+
+#include "spandsp/telephony.h"
+#include "spandsp/bit_operations.h"
+#include "spandsp/dc_restore.h"
+#include "spandsp/modem_echo.h"
+
+modem_echo_can_state_t *modem_echo_can_create(int len)
+{
+    modem_echo_can_state_t *ec;
+
+    if ((ec = (modem_echo_can_state_t *) malloc(sizeof(*ec))) == NULL)
+        return  NULL;
+    memset(ec, 0, sizeof(*ec));
+    ec->taps = len;
+    ec->curr_pos = ec->taps - 1;
+    if ((ec->fir_taps32 = (int32_t *) malloc(ec->taps*sizeof(int32_t))) == NULL)
+    {
+        free(ec);
+        return  NULL;
+    }
+    memset(ec->fir_taps32, 0, ec->taps*sizeof(int32_t));
+    if ((ec->fir_taps16 = (int16_t *) malloc(ec->taps*sizeof(int16_t))) == NULL)
+    {
+        free(ec->fir_taps32);
+        free(ec);
+        return  NULL;
+    }
+    memset(ec->fir_taps16, 0, ec->taps*sizeof(int16_t));
+    if (fir16_create(&ec->fir_state, ec->fir_taps16, ec->taps) == NULL)
+    {
+        free(ec->fir_taps16);
+        free(ec->fir_taps32);
+        free(ec);
+        return  NULL;
+    }
+    return  ec;
+}
+/*- End of function --------------------------------------------------------*/
+
+void modem_echo_can_free(modem_echo_can_state_t *ec)
+{
+    fir16_free(&ec->fir_state);
+    free(ec->fir_taps32);
+    free(ec->fir_taps16);
+    free(ec);
+}
+/*- End of function --------------------------------------------------------*/
+
+void modem_echo_can_flush(modem_echo_can_state_t *ec)
+{
+    ec->tx_power = 0;
+
+    fir16_flush(&ec->fir_state);
+    ec->fir_state.curr_pos = ec->taps - 1;
+    memset(ec->fir_taps32, 0, ec->taps*sizeof(int32_t));
+    memset(ec->fir_taps16, 0, ec->taps*sizeof(int16_t));
+    ec->curr_pos = ec->taps - 1;
+}
+/*- End of function --------------------------------------------------------*/
+
+void modem_echo_can_adaption_mode(modem_echo_can_state_t *ec, int adapt)
+{
+    ec->adapt = adapt;
+}
+/*- End of function --------------------------------------------------------*/
+
+int16_t modem_echo_can_update(modem_echo_can_state_t *ec, int16_t tx, int16_t rx)
+{
+    int32_t echo_value;
+    int clean_rx;
+    int shift;
+    int i;
+    int offset1;
+    int offset2;
+
+    /* Evaluate the echo - i.e. apply the FIR filter */
+    /* Assume the gain of the FIR does not exceed unity. Exceeding unity
+       would seem like a rather poor thing for an echo cancellor to do :)
+       This means we can compute the result with a total disregard for
+       overflows. 16bits x 16bits -> 31bits, so no overflow can occur in
+       any multiply. While accumulating we may overflow and underflow the
+       32 bit scale often. However, if the gain does not exceed unity,
+       everything should work itself out, and the final result will be
+       OK, without any saturation logic. */
+    /* Overflow is very much possible here, and we do nothing about it because
+       of the compute costs */
+    echo_value = fir16(&ec->fir_state, tx);
+
+    /* And the answer is..... */
+    clean_rx = rx - echo_value;
+    //printf("%8d %8d %8d %8d\n", tx, rx, echo_value, clean_rx);
+    if (ec->adapt)
+    {
+        /* Calculate short term power levels using very simple single pole IIRs */
+        /* TODO: Is the nasty modulus approach the fastest, or would a real
+                 tx*tx power calculation actually be faster? Using the squares
+                 makes the numbers grow a lot! */
+        ec->tx_power += ((tx*tx - ec->tx_power) >> 5);
+
+        shift = 1;
+        /* Update the FIR taps */
+        offset2 = ec->curr_pos;
+        offset1 = ec->taps - offset2;
+        for (i = ec->taps - 1;  i >= offset1;  i--)
+        {
+            /* Leak to avoid the coefficients drifting beyond the ability of the
+               adaption process to bring them back under control. */
+            ec->fir_taps32[i] -= (ec->fir_taps32[i] >> 23);
+            ec->fir_taps32[i] += (ec->fir_state.history[i - offset1]*clean_rx) >> shift;
+            ec->fir_taps16[i] = (int16_t) (ec->fir_taps32[i] >> 15);
+        }
+        for (  ;  i >= 0;  i--)
+        {
+            ec->fir_taps32[i] -= (ec->fir_taps32[i] >> 23);
+            ec->fir_taps32[i] += (ec->fir_state.history[i + offset2]*clean_rx) >> shift;
+            ec->fir_taps16[i] = (int16_t) (ec->fir_taps32[i] >> 15);
+        }
+    }
+
+    /* Roll around the rolling buffer */
+    if (ec->curr_pos <= 0)
+        ec->curr_pos = ec->taps;
+    ec->curr_pos--;
+    return  (int16_t) clean_rx;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/msvc/gettimeofday.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/msvc/gettimeofday.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,14 @@
+#ifdef _MSC_VER
+#pragma warning(disable:4100)
+#endif
+
+#include "windows.h"
+
+void gettimeofday(struct timeval *tv, void *tz)
+{
+    long int l = GetTickCount();
+ 
+    tv->tv_sec = l / 1000;
+    tv->tv_usec = (l % 1000) * 1000;
+    return;
+} 

Added: freeswitch/trunk/libs/spandsp/src/msvc/inttypes.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/msvc/inttypes.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,87 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * inttypes.h - a fudge for MSVC, which lacks this header
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2006 Michael Jerris
+ *
+ *
+ * This file is released in the public domain.
+ *
+ */
+
+#if !defined(_INTTYPES_H_)
+#define _INTTYPES_H_
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifdef _MSC_VER
+#if (_MSC_VER >= 1400) // VC8+
+#ifndef _CRT_SECURE_NO_DEPRECATE
+#define _CRT_SECURE_NO_DEPRECATE
+#endif
+#ifndef _CRT_NONSTDC_NO_DEPRECATE
+#define _CRT_NONSTDC_NO_DEPRECATE
+#endif
+#endif // VC8+
+#include <windows.h>
+typedef unsigned __int8		uint8_t;
+typedef unsigned __int16	uint16_t;
+typedef unsigned __int32	uint32_t;
+typedef unsigned __int64    uint64_t;
+typedef __int8		int8_t;
+typedef __int16		int16_t;
+typedef __int32		int32_t;
+typedef __int64		int64_t;
+#define inline __inline
+#define __inline__ __inline
+#define  INT16_MAX   0x7fff 
+#define  INT16_MIN   (-INT16_MAX - 1) 
+#define _MMX_H_
+
+/* disable the following warnings 
+ * C4100: The formal parameter is not referenced in the body of the function. The unreferenced parameter is ignored. 
+ * C4200: Non standard extension C zero sized array
+ * C4706: assignment within conditional expression
+ * C4244: conversion from 'type1' to 'type2', possible loss of data
+ * C4295: array is too small to include a terminating null character
+ * C4125: decimal digit terminates octal escape sequence
+ */
+#pragma warning(disable:4100 4200 4706 4295 4125)
+
+#pragma comment(lib, "ws2_32.lib")
+
+#define strncasecmp _strnicmp
+#define strcasecmp _stricmp
+#define snprintf _snprintf
+
+#if !defined(INFINITY)
+#define INFINITY 0x7fffffff
+#endif
+#endif
+
+#define PACKAGE "spandsp"
+#define VERSION "0.0.3andabit"
+
+#define INT32_MAX	(2147483647)
+#define INT32_MIN	(-2147483647 - 1)
+
+#define PRId8 "d"
+#define PRId16 "d"
+#define PRId32 "ld"
+#define PRId64 "lld"
+
+#define PRIu8 "u"
+#define PRIu16 "u"
+#define PRIu32 "lu"
+#define PRIu64 "llu"
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif

Added: freeswitch/trunk/libs/spandsp/src/msvc/msvcproj.foot
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/msvc/msvcproj.foot	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,7 @@
+
+# Begin Group "Resource Files"
+
+# PROP Default_Filter "ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe"
+# End Group
+# End Target
+# End Project

Added: freeswitch/trunk/libs/spandsp/src/msvc/msvcproj.head
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/msvc/msvcproj.head	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,92 @@
+# Microsoft Developer Studio Project File - Name="spandsp" - Package Owner=<4>
+# Microsoft Developer Studio Generated Build File, Format Version 6.00
+# ** DO NOT EDIT **
+
+# TARGTYPE "Win32 (x86) Dynamic-Link Library" 0x0102
+
+CFG=spandsp - Win32 Debug
+!MESSAGE This is not a valid makefile. To build this project using NMAKE,
+!MESSAGE use the Export Makefile command and run
+!MESSAGE 
+!MESSAGE NMAKE /f "spandsp.mak".
+!MESSAGE 
+!MESSAGE You can specify a configuration when running NMAKE
+!MESSAGE by defining the macro CFG on the command line. For example:
+!MESSAGE 
+!MESSAGE NMAKE /f "spandsp.mak" CFG="spandsp - Win32 Debug"
+!MESSAGE 
+!MESSAGE Possible choices for configuration are:
+!MESSAGE 
+!MESSAGE "spandsp - Win32 Release" (based on "Win32 (x86) Dynamic-Link Library")
+!MESSAGE "spandsp - Win32 Debug" (based on "Win32 (x86) Dynamic-Link Library")
+!MESSAGE 
+
+# Begin Project
+# PROP AllowPerConfigDependencies 0
+# PROP Scc_ProjName ""
+# PROP Scc_LocalPath ""
+CPP=cl.exe
+MTL=midl.exe
+RSC=rc.exe
+
+!IF  "$(CFG)" == "spandsp - Win32 Release"
+
+# PROP BASE Use_MFC 0
+# PROP BASE Use_Debug_Libraries 0
+# PROP BASE Output_Dir "Release"
+# PROP BASE Intermediate_Dir "Release"
+# PROP BASE Target_Dir ""
+# PROP Use_MFC 0
+# PROP Use_Debug_Libraries 0
+# PROP Output_Dir "Release"
+# PROP Intermediate_Dir "Release"
+# PROP Ignore_Export_Lib 0
+# PROP Target_Dir ""
+# ADD BASE CPP /nologo /MT /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D HAVE_TGMATH_H /YX /FD /c
+# ADD CPP /nologo /MT /W3 /GX /Zi /O2 /I "." /I "..\include" /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D HAVE_TGMATH_H /D "_WINDLL" /FR /FD /c
+# SUBTRACT CPP /YX
+# ADD BASE MTL /nologo /D "NDEBUG" /mktyplib203 /win32
+# ADD MTL /nologo /D "NDEBUG" /mktyplib203 /win32
+# ADD BASE RSC /l 0x409 /d "NDEBUG"
+# ADD RSC /l 0x409 /d "NDEBUG"
+BSC32=bscmake.exe
+# ADD BASE BSC32 /nologo
+# ADD BSC32 /nologo
+LINK32=link.exe
+# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /dll /machine:I386
+# ADD LINK32 kernel32.lib ws2_32.lib winmm.lib /nologo /dll /map /debug /machine:I386 /out:"Release/libspandsp.dll"
+
+!ELSEIF  "$(CFG)" == "spandsp - Win32 Debug"
+
+# PROP BASE Use_MFC 0
+# PROP BASE Use_Debug_Libraries 1
+# PROP BASE Output_Dir "Debug"
+# PROP BASE Intermediate_Dir "Debug"
+# PROP BASE Target_Dir ""
+# PROP Use_MFC 0
+# PROP Use_Debug_Libraries 1
+# PROP Output_Dir "Debug"
+# PROP Intermediate_Dir "Debug"
+# PROP Ignore_Export_Lib 0
+# PROP Target_Dir ""
+# ADD BASE CPP /nologo /MTd /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D HAVE_TGMATH_H /YX /FD /GZ /c
+# ADD CPP /nologo /MTd /W3 /Gm /GX /Zi /Od /I "." /I "..\include" /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D HAVE_TGMATH_H /FR /FD /GZ /c
+# SUBTRACT CPP /WX /YX
+# ADD BASE MTL /nologo /D "_DEBUG" /mktyplib203 /win32
+# ADD MTL /nologo /D "_DEBUG" /mktyplib203 /win32
+# ADD BASE RSC /l 0x409 /d "_DEBUG"
+# ADD RSC /l 0x409 /d "_DEBUG"
+BSC32=bscmake.exe
+# ADD BASE BSC32 /nologo
+# ADD BSC32 /nologo
+LINK32=link.exe
+# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /dll /debug /machine:I386 /pdbtype:sept
+# ADD LINK32 kernel32.lib ws2_32.lib winmm.lib /nologo /dll /incremental:no /map /debug /machine:I386 /out:"Debug/libspandsp.dll" /pdbtype:sept
+# SUBTRACT LINK32 /nodefaultlib
+
+!ENDIF 
+
+# Begin Target
+
+# Name "spandsp - Win32 Release"
+# Name "spandsp - Win32 Debug"

Added: freeswitch/trunk/libs/spandsp/src/msvc/spandsp.def
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/msvc/spandsp.def	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,270 @@
+EXPORTS
+adsi_rx_init
+adsi_rx
+adsi_tx_init
+adsi_next_field
+adsi_add_field
+async_tx_init
+async_rx_init
+awgn
+bert_init
+bert_get_bit
+bert_put_bit
+bert_set_report
+bert_result
+filter_create
+filter_delete
+filter_step
+cfilter_create
+cfilter_delete
+cfilter_step
+dds_phase_rate
+dds_frequency
+dds_scaling_dbm0
+dds_scaling_dbov
+dds_lookup
+dds_offset
+dds
+dds_mod
+dds_phase_ratef
+dds_frequencyf
+dds_scaling_dbm0f
+dds_scaling_dbovf
+ddsf
+dds_modf
+dds_complexf
+dds_complex_modf
+echo_can_create
+echo_can_free
+echo_can_flush
+echo_can_adaption_mode
+echo_can_update
+fax_rx
+fax_tx
+fax_set_flush_handler
+fax_init
+fax_release
+fsk_tx_init
+fsk_tx_power
+fsk_tx_set_get_bit
+fsk_tx
+fsk_rx_signal_power
+fsk_rx_signal_cutoff
+fsk_rx_init
+fsk_rx
+fsk_rx_set_put_bit
+g722_encode_init
+g722_encode_release
+g722_encode
+g722_decode_init
+g722_decode_release
+g722_decode
+g726_init
+g726_release
+crc_itu32_calc
+crc_itu32_append
+crc_itu32_check
+crc_itu16_calc
+crc_itu16_append
+crc_itu16_check
+hdlc_rx_init
+hdlc_rx_get_stats
+hdlc_tx_init
+hdlc_tx_frame
+ima_adpcm_init
+ima_adpcm_release
+span_log_test
+span_log
+span_log_buf
+span_log_init
+span_log_set_protocol
+span_set_message_handler
+span_set_error_handler
+modem_echo_can_create
+modem_echo_can_free
+modem_echo_can_flush
+modem_echo_can_adaption_mode
+modem_echo_can_update
+noise
+oki_adpcm_init
+oki_adpcm_release
+playout_put
+playout_get
+playout_get_unconditional
+playout_current_length
+playout_next_due
+playout_new
+playout_free
+playout_restart
+plc_rx
+plc_fillin
+plc_init
+power_meter_init
+power_meter_damping
+power_meter_update
+power_meter_level_dbm0
+power_meter_level_dbov
+queue_empty
+queue_free_space
+queue_contents
+queue_flush
+queue_view
+queue_read
+queue_write
+queue_read_msg
+queue_write_msg
+sig_tone_init
+sig_tone_rx
+sig_tone_tx
+super_tone_rx_make_descriptor
+super_tone_rx_add_tone
+super_tone_rx_add_element
+super_tone_rx_init
+super_tone_rx_free
+super_tone_rx_segment_callback
+super_tone_rx
+super_tone_tx_make_step
+super_tone_tx_free
+super_tone_tx_init
+super_tone_tx
+t30_init
+t30_release
+t30_restart
+t30_free
+t30_frametype
+t30_decode_dis_dtc_dcs
+t30_completion_code_to_str
+t30_get_transfer_statistics
+t30_set_phase_b_handler
+t30_set_phase_d_handler
+t30_set_phase_e_handler
+t30_set_document_handler
+t30_set_rx_file
+t30_set_tx_file
+t30_local_interrupt_request
+t30_hdlc_accept
+t30_timer_update
+t31_call_event
+t31_at_rx
+t31_rx
+t31_tx
+t31_init
+t31_release
+t35_decode
+t38_terminal_init
+t38_gateway_init
+t4_rx_init
+t4_rx_start_page
+t4_rx_end_page
+t4_rx_delete
+t4_rx_end
+t4_rx_set_rx_encoding
+t4_rx_set_sub_address
+t4_rx_set_far_ident
+t4_rx_set_vendor
+t4_rx_set_model
+t4_tx_init
+t4_tx_start_page
+t4_tx_restart_page
+t4_tx_end_page
+t4_tx_delete
+t4_tx_end
+t4_tx_set_tx_encoding
+t4_tx_set_min_row_bits
+t4_tx_set_local_ident
+t4_tx_set_header_info
+t4_get_transfer_statistics
+t4_encoding_to_str
+time_scale_init
+time_scale_rate
+time_scale
+make_goertzel_descriptor
+goertzel_init
+goertzel_update
+goertzel_result
+dtmf_rx_init
+dtmf_rx_set_realtime_callback
+dtmf_rx_parms
+dtmf_rx
+make_tone_gen_descriptor
+tone_gen_init
+tone_gen
+dtmf_tx_init
+dtmf_tx
+v17_rx_init
+v17_rx_restart
+v17_rx_set_put_bit
+v17_rx
+v17_rx_equalizer_state
+v17_rx_carrier_frequency
+v17_rx_symbol_timing_correction
+v17_rx_signal_power
+v17_rx_signal_cutoff
+v17_rx_set_qam_report_handler
+v17_tx_power
+v17_tx_init
+v17_tx_restart
+v17_tx_set_get_bit
+v17_tx
+v22bis_rx_restart
+v22bis_rx
+v22bis_rx_carrier_frequency
+v22bis_rx_signal_power
+v22bis_tx
+v22bis_tx_power
+v22bis_restart
+v22bis_init
+v22bis_set_get_bit
+v22bis_set_put_bit
+v27ter_rx_init
+v27ter_rx_restart
+v27ter_rx_set_put_bit
+v27ter_rx
+v27ter_rx_equalizer_state
+v27ter_rx_carrier_frequency
+v27ter_rx_symbol_timing_correction
+v27ter_rx_signal_power
+v27ter_rx_signal_cutoff
+v27ter_rx_set_qam_report_handler
+v27ter_tx_power
+v27ter_tx_init
+v27ter_tx_restart
+v27ter_tx_set_get_bit
+v27ter_tx
+v29_rx_init
+v29_rx_restart
+v29_rx_set_put_bit
+v29_rx
+v29_rx_equalizer_state
+v29_rx_carrier_frequency
+v29_rx_symbol_timing_correction
+v29_rx_signal_power
+v29_rx_signal_cutoff
+v29_rx_set_qam_report_handler
+v29_tx_power
+v29_tx_init
+v29_tx_restart
+v29_tx_set_get_bit
+v29_tx
+lapm_dump
+lapm_receive
+lapm_tx
+lapm_tx_iframe
+v42_set_status_callback
+v42_rx_bit
+v42_tx_bit
+v42_init
+v42_restart
+v42_release
+v42bis_compress
+v42bis_compress_flush
+v42bis_decompress
+v42bis_decompress_flush
+v42bis_init
+v42bis_release
+v8_init
+v8_release
+v8_tx
+v8_rx
+v8_log_supported_modulations
+

Added: freeswitch/trunk/libs/spandsp/src/msvc/sys/time.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/msvc/sys/time.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1 @@
+extern void gettimeofday(struct timeval *tv, void *tz);

Added: freeswitch/trunk/libs/spandsp/src/msvc/tgmath.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/msvc/tgmath.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,34 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * tgmath.h - a fudge for MSVC, which lacks this header
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2006 Michael Jerris
+ *
+ *
+ * This file is released in the public domain.
+ *
+ */
+
+#if !defined(_TGMATH_H_)
+#define _TGMATH_H_
+
+#include <math.h>
+
+#if !defined(M_PI)
+/* C99 systems may not define M_PI */
+#define M_PI 3.14159265358979323846264338327
+#endif
+
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif

Added: freeswitch/trunk/libs/spandsp/src/msvc/unistd.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/msvc/unistd.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,31 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * inttypes.h - a fudge for MSVC, which lacks this header
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2006 Michael Jerris
+ *
+ *
+ * This file is released in the public domain.
+ *
+ */
+
+#if !defined(_INTTYPES_H_)
+#define _INTTYPES_H_
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#define open _open
+#define write _write
+
+extern int gethostname (char *name, size_t len);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif

Added: freeswitch/trunk/libs/spandsp/src/msvc/vc8proj.foot
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/msvc/vc8proj.foot	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,15 @@
+		</Filter>
+		<Filter
+			Name="Resource Files"
+			Filter="rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav"
+			UniqueIdentifier="{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}"
+			>
+		</Filter>
+		<File
+			RelativePath=".\src\msvc\spandsp.def"
+			>
+		</File>
+	</Files>
+	<Globals>
+	</Globals>
+</VisualStudioProject>

Added: freeswitch/trunk/libs/spandsp/src/msvc/vc8proj.head
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/msvc/vc8proj.head	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,180 @@
+<?xml version="1.0" encoding="Windows-1252"?>
+<VisualStudioProject
+	ProjectType="Visual C"
+	Version="9.00"
+	Name="libspandsp"
+	ProjectGUID="{CF70F278-3364-4395-A2E1-23501C9B8AD2}"
+	RootNamespace="libspandsp"
+	Keyword="Win32Proj"
+	TargetFrameworkVersion="131072"
+	>
+	<Platforms>
+		<Platform
+			Name="Win32"
+		/>
+	</Platforms>
+	<ToolFiles>
+	</ToolFiles>
+	<Configurations>
+		<Configuration
+			Name="Debug|Win32"
+			OutputDirectory="$(SolutionDir)$(ConfigurationName)"
+			IntermediateDirectory="$(ConfigurationName)"
+			ConfigurationType="2"
+			CharacterSet="1"
+			>
+			<Tool
+				Name="VCPreBuildEventTool"
+			/>
+			<Tool
+				Name="VCCustomBuildTool"
+			/>
+			<Tool
+				Name="VCXMLDataGeneratorTool"
+			/>
+			<Tool
+				Name="VCWebServiceProxyGeneratorTool"
+			/>
+			<Tool
+				Name="VCMIDLTool"
+			/>
+			<Tool
+				Name="VCCLCompilerTool"
+				Optimization="0"
+				AdditionalIncludeDirectories="src; src\spandsp; src\msvc;..\libtiff\include"
+				PreprocessorDefinitions="WIN32;_DEBUG;_WINDOWS;_USRDLL;LIBSPANDSP_EXPORTS;_CRT_SECURE_NO_DEPRECATE;_CRT_NONSTDC_NO_DEPRECATE;HAVE_MATH_H;HAVE_TGMATH_H"
+				MinimalRebuild="true"
+				BasicRuntimeChecks="3"
+				RuntimeLibrary="3"
+				UsePrecompiledHeader="0"
+				WarningLevel="4"
+				Detect64BitPortabilityProblems="true"
+				DebugInformationFormat="4"
+				CompileAs="1"
+			/>
+			<Tool
+				Name="VCManagedResourceCompilerTool"
+			/>
+			<Tool
+				Name="VCResourceCompilerTool"
+			/>
+			<Tool
+				Name="VCPreLinkEventTool"
+			/>
+			<Tool
+				Name="VCLinkerTool"
+				LinkIncremental="2"
+				ModuleDefinitionFile="src/msvc/spandsp.def"
+				GenerateDebugInformation="true"
+				SubSystem="2"
+				RandomizedBaseAddress="1"
+				DataExecutionPrevention="0"
+				TargetMachine="1"
+			/>
+			<Tool
+				Name="VCALinkTool"
+			/>
+			<Tool
+				Name="VCManifestTool"
+			/>
+			<Tool
+				Name="VCXDCMakeTool"
+			/>
+			<Tool
+				Name="VCBscMakeTool"
+			/>
+			<Tool
+				Name="VCFxCopTool"
+			/>
+			<Tool
+				Name="VCAppVerifierTool"
+			/>
+			<Tool
+				Name="VCPostBuildEventTool"
+			/>
+		</Configuration>
+		<Configuration
+			Name="Release|Win32"
+			OutputDirectory="$(SolutionDir)$(ConfigurationName)"
+			IntermediateDirectory="$(ConfigurationName)"
+			ConfigurationType="2"
+			CharacterSet="1"
+			WholeProgramOptimization="1"
+			>
+			<Tool
+				Name="VCPreBuildEventTool"
+			/>
+			<Tool
+				Name="VCCustomBuildTool"
+			/>
+			<Tool
+				Name="VCXMLDataGeneratorTool"
+			/>
+			<Tool
+				Name="VCWebServiceProxyGeneratorTool"
+			/>
+			<Tool
+				Name="VCMIDLTool"
+			/>
+			<Tool
+				Name="VCCLCompilerTool"
+				AdditionalIncludeDirectories="src; src\spandsp; src\msvc;..\libtiff\include"
+				PreprocessorDefinitions="WIN32;NDEBUG;_WINDOWS;_USRDLL;LIBSPANDSP_EXPORTS;_CRT_SECURE_NO_DEPRECATE;_CRT_NONSTDC_NO_DEPRECATE;HAVE_MATH_H;HAVE_TGMATH_H"
+				RuntimeLibrary="2"
+				UsePrecompiledHeader="0"
+				WarningLevel="4"
+				Detect64BitPortabilityProblems="true"
+				DebugInformationFormat="3"
+			/>
+			<Tool
+				Name="VCManagedResourceCompilerTool"
+			/>
+			<Tool
+				Name="VCResourceCompilerTool"
+			/>
+			<Tool
+				Name="VCPreLinkEventTool"
+			/>
+			<Tool
+				Name="VCLinkerTool"
+				LinkIncremental="1"
+				ModuleDefinitionFile="src/msvc/spandsp.def"
+				GenerateDebugInformation="true"
+				SubSystem="2"
+				OptimizeReferences="2"
+				EnableCOMDATFolding="2"
+				RandomizedBaseAddress="1"
+				DataExecutionPrevention="0"
+				TargetMachine="1"
+			/>
+			<Tool
+				Name="VCALinkTool"
+			/>
+			<Tool
+				Name="VCManifestTool"
+			/>
+			<Tool
+				Name="VCXDCMakeTool"
+			/>
+			<Tool
+				Name="VCBscMakeTool"
+			/>
+			<Tool
+				Name="VCFxCopTool"
+			/>
+			<Tool
+				Name="VCAppVerifierTool"
+			/>
+			<Tool
+				Name="VCPostBuildEventTool"
+			/>
+		</Configuration>
+	</Configurations>
+	<References>
+	</References>
+	<Files>
+		<Filter
+			Name="Source Files"
+			Filter="cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx"
+			UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
+			>

Added: freeswitch/trunk/libs/spandsp/src/noise.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/noise.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,114 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * noise.c - A low complexity audio noise generator, suitable for
+ *           real time generation (current AWGN, and Hoth)
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2005 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: noise.c,v 1.24 2008/08/17 14:18:11 steveu Exp $
+ */
+
+/*! \file */
+
+#if defined(HAVE_CONFIG_H)
+#include <config.h>
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <inttypes.h>
+#include <memory.h>
+#include "floating_fudge.h"
+#if defined(HAVE_TGMATH_H)
+#include <tgmath.h>
+#endif
+#if defined(HAVE_MATH_H)
+#include <math.h>
+#endif
+
+#include "spandsp/telephony.h"
+#include "spandsp/dc_restore.h"
+#include "spandsp/noise.h"
+
+int16_t noise(noise_state_t *s)
+{
+    int32_t val;
+    int i;
+
+    /* The central limit theorem says if you add a few random numbers together,
+       the result starts to look Gaussian. Quantities above 7 give diminishing
+       returns. Quantites above 20 are exceedingly Gaussian. */
+    val = 0;
+    for (i = 0;  i < s->quality;  i++)
+    {
+        s->rndnum = 1664525U*s->rndnum + 1013904223U;
+        val += ((int32_t) s->rndnum) >> 22;
+    }
+    if (s->class_of_noise == NOISE_CLASS_HOTH)
+    {
+        /* Hoth noise is room-like. It should be sculpted, at the high and low ends,
+           and roll off at 5dB/octave across the main part of the band. However,
+           merely rolling off at 6dB/octave across the band gets you close
+           to the subjective effect. */
+        s->state = (3*val + 5*s->state) >> 3;
+        /* Bring the overall power level back to the pre-filtered level. This
+           simple approx. leaves the signal about 0.35dB low. */
+        val = s->state << 1;
+    }
+    return saturate((val*s->rms) >> 10);
+}
+/*- End of function --------------------------------------------------------*/
+
+noise_state_t *noise_init_dbm0(noise_state_t *s, int seed, float level, int class_of_noise, int quality)
+{
+    return noise_init_dbov(s, seed, (level - DBM0_MAX_POWER), class_of_noise, quality);
+}
+/*- End of function --------------------------------------------------------*/
+
+noise_state_t *noise_init_dbov(noise_state_t *s, int seed, float level, int class_of_noise, int quality)
+{
+    float rms;
+
+    if (s == NULL)
+    {
+        if ((s = (noise_state_t *) malloc(sizeof(*s))) == NULL)
+            return NULL;
+    }
+    memset(s, 0, sizeof(*s));
+    s->rndnum = (uint32_t) seed;
+    rms = 32768.0f*powf(10.0f, level/20.0f);
+    if (quality < 4)
+        s->quality = 4;
+    else if (quality > 20)
+        s->quality = 20;
+    else
+        s->quality = quality;
+    if (class_of_noise == NOISE_CLASS_HOTH)
+    {
+        /* Allow for the gain of the filter */
+        rms *= 1.043f;
+    }
+    s->rms = (int32_t) (rms*sqrtf(12.0f/s->quality));
+    s->class_of_noise = class_of_noise;
+    return s;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/oki_adpcm.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/oki_adpcm.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,379 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * oki_adpcm.c - Conversion routines between linear 16 bit PCM data and
+ *               OKI (Dialogic) ADPCM format. Supports with the 32kbps
+ *               and 24kbps variants used by Dialogic.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2001, 2004 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * The actual OKI ADPCM encode and decode method is derived from freely
+ * available code, whose exact origins seem uncertain.
+ *
+ * $Id: oki_adpcm.c,v 1.27 2008/05/13 13:17:23 steveu Exp $
+ */
+
+/*! \file */
+
+#if defined(HAVE_CONFIG_H)
+#include <config.h>
+#endif
+
+#include <stdlib.h>
+#include <inttypes.h>
+#include <string.h>
+
+#include "spandsp/telephony.h"
+#include "spandsp/oki_adpcm.h"
+
+/* Routines to convert 12 bit linear samples to the Oki ADPCM coding format,
+   widely used in CTI, because Dialogic use it. */
+
+static const int16_t step_size[49] =
+{
+      16,   17,   19,   21,   23,   25,   28,   31,
+      34,   37,   41,   45,   50,   55,   60,   66,
+      73,   80,   88,   97,  107,  118,  130,  143,
+     157,  173,  190,  209,  230,  253,  279,  307,
+     337,  371,  408,  449,  494,  544,  598,  658,
+     724,  796,  876,  963, 1060, 1166, 1282, 1408,
+    1552
+};
+
+static const int16_t step_adjustment[8] =
+{
+    -1, -1, -1, -1, 2, 4, 6, 8
+};
+
+/* Band limiting filter, to allow sample rate conversion to and
+   from 6k samples/second. */
+static const float cutoff_coeffs[] =
+{
+    -3.648392e-4f,
+     5.062391e-4f,
+     1.206247e-3f,
+     1.804452e-3f,
+     1.691750e-3f,
+     4.083405e-4f,
+    -1.931085e-3f,
+    -4.452107e-3f,
+    -5.794821e-3f,
+    -4.778489e-3f,
+    -1.161266e-3f,
+     3.928504e-3f,
+     8.259786e-3f,
+     9.500425e-3f,
+     6.512800e-3f,
+     2.227856e-4f,
+    -6.531275e-3f,
+    -1.026843e-2f,
+    -8.718062e-3f,
+    -2.280487e-3f,
+     5.817733e-3f,
+     1.096777e-2f,
+     9.634404e-3f,
+     1.569301e-3f,
+    -9.522632e-3f,
+    -1.748273e-2f,
+    -1.684408e-2f,
+    -6.100054e-3f,
+     1.071206e-2f,
+     2.525209e-2f,
+     2.871779e-2f,
+     1.664411e-2f,
+    -7.706268e-3f,
+    -3.331083e-2f,
+    -4.521249e-2f,
+    -3.085962e-2f,
+     1.373653e-2f,
+     8.089593e-2f,
+     1.529060e-1f,
+     2.080487e-1f,
+     2.286834e-1f,
+     2.080487e-1f,
+     1.529060e-1f,
+     8.089593e-2f,
+     1.373653e-2f,
+    -3.085962e-2f,
+    -4.521249e-2f,
+    -3.331083e-2f,
+    -7.706268e-3f,
+     1.664411e-2f,
+     2.871779e-2f,
+     2.525209e-2f,
+     1.071206e-2f,
+    -6.100054e-3f,
+    -1.684408e-2f,
+    -1.748273e-2f,
+    -9.522632e-3f,
+     1.569301e-3f,
+     9.634404e-3f,
+     1.096777e-2f,
+     5.817733e-3f,
+    -2.280487e-3f,
+    -8.718062e-3f,
+    -1.026843e-2f,
+    -6.531275e-3f,
+     2.227856e-4f,
+     6.512800e-3f,
+     9.500425e-3f,
+     8.259786e-3f,
+     3.928504e-3f,
+    -1.161266e-3f,
+    -4.778489e-3f,
+    -5.794821e-3f,
+    -4.452107e-3f,
+    -1.931085e-3f,
+     4.083405e-4f,
+     1.691750e-3f,
+     1.804452e-3f,
+     1.206247e-3f,
+     5.062391e-4f,
+    -3.648392e-4f
+};
+
+static int16_t decode(oki_adpcm_state_t *s, uint8_t adpcm)
+{
+    int16_t e;
+    int16_t ss;
+    int16_t linear;
+
+    /* Doing the next part as follows:
+     *
+     * x = adpcm & 0x07;
+     * e = (step_size[s->step_index]*(x + x + 1)) >> 3;
+     * 
+     * Seems an obvious improvement on a modern machine, but remember
+     * the truncation errors do not come out the same. It would
+     * not, therefore, be an exact match for what this code is doing.
+     *
+     * Just what a Dialogic card does, I do not know!
+     */
+
+    ss = step_size[s->step_index];
+    e = ss >> 3;
+    if (adpcm & 0x01)
+        e += (ss >> 2);
+    /*endif*/
+    if (adpcm & 0x02)
+        e += (ss >> 1);
+    /*endif*/
+    if (adpcm & 0x04)
+        e += ss;
+    /*endif*/
+    if (adpcm & 0x08)
+        e = -e;
+    /*endif*/
+    linear = s->last + e;
+
+    /* Saturate the values to +/- 2^11 (supposed to be 12 bits) */
+    if (linear > 2047)
+        linear = 2047;
+    else if (linear < -2048)
+        linear = -2048;
+    /*endif*/
+
+    s->last = linear;
+    s->step_index += step_adjustment[adpcm & 0x07];
+    if (s->step_index < 0)
+        s->step_index = 0;
+    else if (s->step_index > 48)
+        s->step_index = 48;
+    /*endif*/
+    /* Note: the result here is a 12 bit value */
+    return linear;
+}
+/*- End of function --------------------------------------------------------*/
+
+static uint8_t encode(oki_adpcm_state_t *s, int16_t linear)
+{
+    int16_t e;
+    int16_t ss;
+    uint8_t adpcm;
+
+    ss = step_size[s->step_index];
+    e = (linear >> 4) - s->last;
+    adpcm = (uint8_t) 0x00;
+    if (e < 0)
+    {
+        adpcm = (uint8_t) 0x08;
+        e = -e;
+    }
+    /*endif*/
+    if (e >= ss)
+    {
+        adpcm |= (uint8_t) 0x04;
+        e -= ss;
+    }
+    /*endif*/
+    if (e >= (ss >> 1))
+    {
+        adpcm |= (uint8_t) 0x02;
+        e -= ss;
+    }
+    /*endif*/
+    if (e >= (ss >> 2))
+        adpcm |= (uint8_t) 0x01;
+    /*endif*/
+
+    /* Use the decoder to set the estimate of the last sample. */
+    /* It also will adjust the step_index for us. */
+    s->last = decode(s, adpcm);
+    return adpcm;
+}
+/*- End of function --------------------------------------------------------*/
+
+oki_adpcm_state_t *oki_adpcm_init(oki_adpcm_state_t *s, int bit_rate)
+{
+    if (bit_rate != 32000  &&  bit_rate != 24000)
+        return NULL;
+    if (s == NULL)
+    {
+        if ((s = (oki_adpcm_state_t *) malloc(sizeof(*s))) == NULL)
+            return  NULL;
+    }
+    memset(s, 0, sizeof(*s));
+    s->bit_rate = bit_rate;
+    
+    return  s;
+}
+/*- End of function --------------------------------------------------------*/
+
+int oki_adpcm_release(oki_adpcm_state_t *s)
+{
+    free(s);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int oki_adpcm_decode(oki_adpcm_state_t *s,
+                     int16_t amp[],
+                     const uint8_t oki_data[],
+                     int oki_bytes)
+{
+    int i;
+    int x;
+    int l;
+    int n;
+    int samples;
+    float z;
+
+#if (_MSC_VER >= 1400) 
+    __analysis_assume(s->phase >= 0  &&  s->phase <= 4);
+#endif
+    samples = 0;
+    if (s->bit_rate == 32000)
+    {
+        for (i = 0;  i < oki_bytes;  i++)
+        {
+            amp[samples++] = decode(s, (oki_data[i] >> 4) & 0xF) << 4;
+            amp[samples++] = decode(s, oki_data[i] & 0xF) << 4;
+        }
+        /*endwhile*/
+    }
+    else
+    {
+        n = 0;
+        for (i = 0;  i < oki_bytes;  )
+        {
+            /* 6k to 8k sample/second conversion */
+            if (s->phase)
+            {
+                s->history[s->ptr++] =
+                    decode(s, (n++ & 1)  ?  (oki_data[i++] & 0xF)  :  ((oki_data[i] >> 4) & 0xF)) << 4;
+                s->ptr &= (32 - 1);
+            }
+            /*endif*/
+            z = 0.0f;
+            for (l = 80 - 3 + s->phase, x = s->ptr - 1;  l >= 0;  l -= 4, x--)
+                z += cutoff_coeffs[l]*s->history[x & (32 - 1)];
+            amp[samples++] = (int16_t) (z*4.0f);
+            if (++s->phase > 3)
+                s->phase = 0;
+            /*endif*/
+        }
+        /*endfor*/
+    }
+    /*endif*/
+    return  samples;
+}
+/*- End of function --------------------------------------------------------*/
+
+int oki_adpcm_encode(oki_adpcm_state_t *s,
+                     uint8_t oki_data[],
+                     const int16_t amp[],
+                     int len)
+{
+    int x;
+    int l;
+    int n;
+    int bytes;
+    float z;
+
+    bytes = 0;
+    if (s->bit_rate == 32000)
+    {
+        for (n = 0;  n < len;  n++)
+        {
+            s->oki_byte = (s->oki_byte << 4) | encode(s, amp[n]);
+            if ((s->mark++ & 1))
+                oki_data[bytes++] = s->oki_byte;
+            /*endif*/
+        }
+        /*endfor*/
+    }
+    else
+    {
+        n = 0;
+        for (;;)
+        {
+            /* 8k to 6k sample/second conversion */
+            if (s->phase > 2)
+            {
+                s->history[s->ptr++] = amp[n];
+                s->ptr &= (32 - 1);
+                s->phase = 0;
+                if (++n >= len)
+                    break;
+                /*endif*/
+            }
+            /*endif*/
+            s->history[s->ptr++] = amp[n];
+            s->ptr &= (32 - 1);
+            z = 0.0f;
+            for (l = 80 - s->phase, x = s->ptr - 1;  l >= 0;  l -= 3, x--)
+                z += cutoff_coeffs[l]*s->history[x & (32 - 1)];
+            /*endfor*/
+            s->oki_byte = (s->oki_byte << 4) | encode(s, (int16_t) (z*3.0f));
+            if ((s->mark++ & 1))
+                oki_data[bytes++] = s->oki_byte;
+            /*endif*/
+            s->phase++;
+            if (++n >= len)
+                break;
+            /*endif*/
+        }
+        /*endfor*/
+    }
+    /*endif*/
+    return  bytes;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/playout.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/playout.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,367 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * playout.c
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2005 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This was kicked off from jitter buffering code
+ *      Copyright (C) 2004, Horizon Wimba, Inc.
+ *      Author Steve Kann <stevek at stevek.com>
+ * However, there isn't a lot of the original left, now. The original
+ * was licenced under the LGPL, so any remaining fragments are
+ * compatible with the GPL licence used here.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: playout.c,v 1.14 2008/05/13 13:17:23 steveu Exp $
+ */
+
+#if defined(HAVE_CONFIG_H)
+#include <config.h>
+#endif
+
+#include <stdio.h>
+#include <inttypes.h>
+#include <stdlib.h>
+#include <string.h>
+#include <limits.h>
+
+#include "spandsp/telephony.h"
+#include "spandsp/playout.h"
+
+static playout_frame_t *queue_get(playout_state_t *s, timestamp_t sender_stamp)
+{
+    playout_frame_t *frame;
+
+    if ((frame = s->first_frame) == NULL)
+        return NULL;
+
+    if (sender_stamp >= frame->sender_stamp)
+    {
+        /* Remove this frame from the queue */
+        if (frame->later)
+        {
+            frame->later->earlier = NULL;
+            s->first_frame = frame->later;
+        }
+        else
+        {
+            /* The queue is now empty */
+            s->first_frame = NULL;
+            s->last_frame = NULL;
+        }
+        return frame;
+    } 
+
+    return NULL;
+}
+/*- End of function --------------------------------------------------------*/
+
+timestamp_t playout_next_due(playout_state_t *s)
+{
+    return s->last_speech_sender_stamp + s->last_speech_sender_len;
+}
+/*- End of function --------------------------------------------------------*/
+
+timestamp_t playout_current_length(playout_state_t *s)
+{
+    return s->target_buffer_length;
+}
+/*- End of function --------------------------------------------------------*/
+
+playout_frame_t *playout_get_unconditional(playout_state_t *s)
+{
+    playout_frame_t *frame;
+    
+    if ((frame = queue_get(s, 0x7FFFFFFF)))
+    {
+        /* Put it on the free list */
+        frame->later = s->free_frames;
+        s->free_frames = frame;
+
+        /* We return the frame pointer, even though it's on the free list.
+           The caller *must* copy the data before this frame has any chance
+           of being reused. */
+    }
+    return frame;
+}
+/*- End of function --------------------------------------------------------*/
+
+int playout_get(playout_state_t *s, playout_frame_t *frameout, timestamp_t now)
+{
+    playout_frame_t *frame;
+
+    /* Make the last_speech_sender_stamp the current expected one. */
+    s->last_speech_sender_stamp += s->last_speech_sender_len;
+    if ((frame = queue_get(s, s->last_speech_sender_stamp)) == NULL)
+    {
+        /* The required frame was not received (or at least not in time) */
+        s->frames_missing++;
+        return PLAYOUT_FILLIN;
+    }
+
+    if (s->dynamic  &&  frame->type == PLAYOUT_TYPE_SPEECH)
+    {
+        /* Assess whether the buffer length is appropriate */
+        if (!s->not_first)
+        {
+            /* Prime things the first time through */
+            s->not_first = TRUE;
+            s->latest_expected = frame->receiver_stamp + s->min_length;
+        }
+        /* Leaky integrate the rate of occurance of frames received just in time and late */
+        s->state_late += ((((frame->receiver_stamp > s->latest_expected)  ?  0x10000000  :  0) - s->state_late) >> 8);
+        s->state_just_in_time += ((((frame->receiver_stamp > s->latest_expected - frame->sender_len)  ?  0x10000000  :  0) - s->state_just_in_time) >> 8);
+        s->latest_expected += frame->sender_len;
+        
+        if (s->state_late > s->dropable_threshold)
+        {
+            if (s->since_last_step < 10)
+            {
+                if (s->target_buffer_length < s->max_length - 2)
+                {
+                    /* The late bin is too big - increase buffering */
+                    s->target_buffer_length += 3*frame->sender_len;
+                    s->latest_expected += 3*frame->sender_len;
+                    s->state_just_in_time = s->dropable_threshold;
+                    s->state_late = 0;
+                    s->since_last_step = 0;
+
+                    s->last_speech_sender_stamp -= 3*s->last_speech_sender_len;
+                }
+            }
+            else
+            {
+                if (s->target_buffer_length < s->max_length)
+                {
+                    /* The late bin is too big - increase buffering */
+                    s->target_buffer_length += frame->sender_len;
+                    s->latest_expected += frame->sender_len;
+                    s->state_just_in_time = s->dropable_threshold;
+                    s->state_late = 0;
+                    s->since_last_step = 0;
+
+                    s->last_speech_sender_stamp -= s->last_speech_sender_len;
+                }
+            }
+        }
+        else if (s->since_last_step > 500  &&  s->state_just_in_time < s->dropable_threshold)
+        {
+            if (s->target_buffer_length > s->min_length)
+            {
+                /* The just-in-time bin is pretty small - decrease buffering */
+                s->target_buffer_length -= frame->sender_len;
+                s->latest_expected -= frame->sender_len;
+                s->state_just_in_time = s->dropable_threshold;
+                s->state_late = 0;
+                s->since_last_step = 0;
+    
+                s->last_speech_sender_stamp += s->last_speech_sender_len;
+            }
+        }
+        s->since_last_step++;
+    }
+
+    /* If its not a speech frame, just return it. */
+    if (frame->type != PLAYOUT_TYPE_SPEECH)
+    {
+        /* Rewind last_speech_sender_stamp, since this isn't speech */
+        s->last_speech_sender_stamp -= s->last_speech_sender_len;
+            
+        *frameout = *frame;
+        /* Put it on the free list */
+        frame->later = s->free_frames;
+        s->free_frames = frame;
+        
+        s->frames_out++;
+        return PLAYOUT_OK;
+    }
+    if (frame->sender_stamp < s->last_speech_sender_stamp)
+    {
+        /* This speech frame is late */
+        *frameout = *frame;
+        /* Put it on the free list */
+        frame->later = s->free_frames;
+        s->free_frames = frame;
+
+        /* Rewind last_speech_sender_stamp, since we're just dumping */
+        s->last_speech_sender_stamp -= s->last_speech_sender_len;
+        s->frames_out++;
+        s->frames_late++;
+        s->frames_missing--;
+        return PLAYOUT_DROP;
+    }
+    /* Keep track of frame sizes, to allow for variable sized frames */
+    if (frame->sender_len > 0)
+        s->last_speech_sender_len = frame->sender_len;
+
+    /* Normal case. Return the frame, and increment stuff */
+    *frameout = *frame;
+    /* Put it on the free list */
+    frame->later = s->free_frames;
+    s->free_frames = frame;
+
+    s->frames_out++;
+    return PLAYOUT_OK;
+}
+/*- End of function --------------------------------------------------------*/
+
+int playout_put(playout_state_t *s, void *data, int type, timestamp_t sender_len, timestamp_t sender_stamp, timestamp_t receiver_stamp)
+{
+    playout_frame_t *frame;
+    playout_frame_t *p;
+
+    /* When a frame arrives we just queue it in order. We leave all the tricky stuff until frames
+       are read from the queue. */
+    s->frames_in++;
+
+    /* Acquire a frame */
+    if ((frame = s->free_frames))
+    {
+        s->free_frames = frame->later;
+    }
+    else
+    {
+        if ((frame = (playout_frame_t *) malloc(sizeof(*frame))) == NULL)
+            return PLAYOUT_ERROR;
+    }
+
+    /* Fill out the frame */
+    frame->data = data;
+    frame->type = type;
+    frame->sender_stamp = sender_stamp;
+    frame->sender_len = sender_len;
+    frame->receiver_stamp = receiver_stamp;
+
+    /* Frames are kept in a list, sorted by the timestamp assigned by the sender. */
+    if (s->last_frame == NULL)
+    {
+        /* The queue is empty. */
+        frame->later = NULL;
+        frame->earlier = NULL;
+        s->first_frame = frame;
+        s->last_frame = frame;
+    }
+    else if (sender_stamp >= s->last_frame->sender_stamp)
+    {
+        /* Frame goes at the end of the queue. */
+        frame->later = NULL;
+        frame->earlier = s->last_frame;
+        s->last_frame->later = frame;
+        s->last_frame = frame;
+    }
+    else
+    {
+        /* Frame is out of sequence. */
+        s->frames_oos++;
+
+        /* Find where it should go in the queue */
+        p = s->last_frame;
+        while (sender_stamp < p->sender_stamp  &&  p->earlier) 
+            p = p->earlier;
+
+        if (p->earlier)
+        {
+            /* It needs to go somewhere in the queue */
+            frame->later = p->later;
+            frame->earlier = p;
+            p->later->earlier = frame;
+            p->later = frame;
+        }
+        else
+        {
+            /* It needs to go at the very beginning of the queue */
+            frame->later = p;
+            frame->earlier = NULL;
+            p->earlier = frame;
+            s->first_frame = frame;
+        }
+    }
+
+    if (s->start  &&  type == PLAYOUT_TYPE_SPEECH)
+    {
+        s->last_speech_sender_stamp = sender_stamp - sender_len - s->min_length;
+        s->last_speech_sender_len = sender_len;
+        s->start = FALSE;
+    }
+
+    return PLAYOUT_OK;
+}
+/*- End of function --------------------------------------------------------*/
+
+void playout_restart(playout_state_t *s, int min_length, int max_length)
+{
+    playout_frame_t *frame;
+    playout_frame_t *next;
+
+    /* Free all the frames on the free list */
+    for (frame = s->free_frames;  frame;  frame = next)
+    {
+        next = frame->later;
+        free(frame);
+    }
+
+    memset(s, 0, sizeof(*s));
+    s->dynamic = (min_length < max_length);
+    s->min_length = min_length;
+    s->max_length = (max_length > min_length)  ?  max_length  :  min_length;
+    s->dropable_threshold = 1*0x10000000/100;
+    s->start = TRUE;
+    s->since_last_step = 0x7FFFFFFF;
+    /* Start with the minimum buffer length allowed, and work from there */
+    s->actual_buffer_length = 
+    s->target_buffer_length = (s->max_length - s->min_length)/2;
+}
+/*- End of function --------------------------------------------------------*/
+
+playout_state_t *playout_new(int min_length, int max_length)
+{
+    playout_state_t *s;
+
+    if ((s = (playout_state_t *) malloc(sizeof(playout_state_t))) == NULL)
+        return NULL;
+    memset(s, 0, sizeof(*s));
+    playout_restart(s, min_length, max_length);
+    return s;
+}
+/*- End of function --------------------------------------------------------*/
+
+void playout_free(playout_state_t *s)
+{
+    playout_frame_t *frame;
+    playout_frame_t *next;
+    
+    /* Free all the frames in the queue. In most cases these should have been
+       removed already, so their associated data could be freed. */
+    for (frame = s->first_frame;  frame;  frame = next)
+    {
+        next = frame->later;
+        free(frame);
+    }
+    /* Free all the frames on the free list */
+    for (frame = s->free_frames;  frame;  frame = next)
+    {
+        next = frame->later;
+        free(frame);
+    }
+    /* Finally, free ourselves! */ 
+    free(s);
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/plc.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/plc.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,257 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * plc.c
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2004 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: plc.c,v 1.22 2008/07/02 14:48:25 steveu Exp $
+ */
+
+/*! \file */
+
+#if defined(HAVE_CONFIG_H)
+#include <config.h>
+#endif
+
+#include <stdio.h>
+#include <inttypes.h>
+#include <stdlib.h>
+#include <string.h>
+#include "floating_fudge.h"
+#if defined(HAVE_TGMATH_H)
+#include <tgmath.h>
+#endif
+#if defined(HAVE_MATH_H)
+#include <math.h>
+#endif
+#include <limits.h>
+
+#include "spandsp/telephony.h"
+#include "spandsp/dc_restore.h"
+#include "spandsp/plc.h"
+
+/* We do a straight line fade to zero volume in 50ms when we are filling in for missing data. */
+#define ATTENUATION_INCREMENT       0.0025f     /* Attenuation per sample */
+
+#define ms_to_samples(t)            (((t)*SAMPLE_RATE)/1000)
+
+static void save_history(plc_state_t *s, int16_t *buf, int len)
+{
+    if (len >= PLC_HISTORY_LEN)
+    {
+        /* Just keep the last part of the new data, starting at the beginning of the buffer */
+        memcpy(s->history, buf + len - PLC_HISTORY_LEN, sizeof(int16_t)*PLC_HISTORY_LEN);
+        s->buf_ptr = 0;
+        return;
+    }
+    if (s->buf_ptr + len > PLC_HISTORY_LEN)
+    {
+        /* Wraps around - must break into two sections */
+        memcpy(s->history + s->buf_ptr, buf, sizeof(int16_t)*(PLC_HISTORY_LEN - s->buf_ptr));
+        len -= (PLC_HISTORY_LEN - s->buf_ptr);
+        memcpy(s->history, buf + (PLC_HISTORY_LEN - s->buf_ptr), sizeof(int16_t)*len);
+        s->buf_ptr = len;
+        return;
+    }
+    /* Can use just one section */
+    memcpy(s->history + s->buf_ptr, buf, sizeof(int16_t)*len);
+    s->buf_ptr += len;
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ void normalise_history(plc_state_t *s)
+{
+    int16_t tmp[PLC_HISTORY_LEN];
+
+    if (s->buf_ptr == 0)
+        return;
+    memcpy(tmp, s->history, sizeof(int16_t)*s->buf_ptr);
+    memcpy(s->history, s->history + s->buf_ptr, sizeof(int16_t)*(PLC_HISTORY_LEN - s->buf_ptr));
+    memcpy(s->history + PLC_HISTORY_LEN - s->buf_ptr, tmp, sizeof(int16_t)*s->buf_ptr);
+    s->buf_ptr = 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ int amdf_pitch(int min_pitch, int max_pitch, int16_t amp[], int len)
+{
+    int i;
+    int j;
+    int acc;
+    int min_acc;
+    int pitch;
+
+    pitch = min_pitch;
+    min_acc = INT_MAX;
+    for (i = max_pitch;  i <= min_pitch;  i++)
+    {
+        acc = 0;
+        for (j = 0;  j < len;  j++)
+            acc += abs(amp[i + j] - amp[j]);
+        if (acc < min_acc)
+        {
+            min_acc = acc;
+            pitch = i;
+        }
+    }
+    return pitch;
+}
+/*- End of function --------------------------------------------------------*/
+
+int plc_rx(plc_state_t *s, int16_t amp[], int len)
+{
+    int i;
+    int pitch_overlap;
+    float old_step;
+    float new_step;
+    float old_weight;
+    float new_weight;
+    float gain;
+    
+    if (s->missing_samples)
+    {
+        /* Although we have a real signal, we need to smooth it to fit well
+           with the synthetic signal we used for the previous block */
+
+        /* The start of the real data is overlapped with the next 1/4 cycle
+           of the synthetic data. */
+        pitch_overlap = s->pitch >> 2;
+        if (pitch_overlap > len)
+            pitch_overlap = len;
+        gain = 1.0f - s->missing_samples*ATTENUATION_INCREMENT;
+        if (gain < 0.0f)
+            gain = 0.0f;
+        new_step = 1.0f/pitch_overlap;
+        old_step = new_step*gain;
+        new_weight = new_step;
+        old_weight = (1.0f - new_step)*gain;
+        for (i = 0;  i < pitch_overlap;  i++)
+        {
+            amp[i] = fsaturate(old_weight*s->pitchbuf[s->pitch_offset] + new_weight*amp[i]);
+            if (++s->pitch_offset >= s->pitch)
+                s->pitch_offset = 0;
+            new_weight += new_step;
+            old_weight -= old_step;
+            if (old_weight < 0.0f)
+                old_weight = 0.0f;
+        }
+        s->missing_samples = 0;
+    }
+    save_history(s, amp, len);
+    return len;
+}
+/*- End of function --------------------------------------------------------*/
+
+int plc_fillin(plc_state_t *s, int16_t amp[], int len)
+{
+    int i;
+    int pitch_overlap;
+    float old_step;
+    float new_step;
+    float old_weight;
+    float new_weight;
+    float gain;
+    int16_t *orig_amp;
+    int orig_len;
+
+    orig_amp = amp;
+    orig_len = len;
+    if (s->missing_samples == 0)
+    {
+        /* As the gap in real speech starts we need to assess the last known pitch,
+           and prepare the synthetic data we will use for fill-in */
+        normalise_history(s);
+        s->pitch = amdf_pitch(PLC_PITCH_MIN, PLC_PITCH_MAX, s->history + PLC_HISTORY_LEN - CORRELATION_SPAN - PLC_PITCH_MIN, CORRELATION_SPAN);
+        /* We overlap a 1/4 wavelength */
+        pitch_overlap = s->pitch >> 2;
+        /* Cook up a single cycle of pitch, using a single of the real signal with 1/4
+           cycle OLA'ed to make the ends join up nicely */
+        /* The first 3/4 of the cycle is a simple copy */
+        for (i = 0;  i < s->pitch - pitch_overlap;  i++)
+            s->pitchbuf[i] = s->history[PLC_HISTORY_LEN - s->pitch + i];
+        /* The last 1/4 of the cycle is overlapped with the end of the previous cycle */
+        new_step = 1.0f/pitch_overlap;
+        new_weight = new_step;
+        for (  ;  i < s->pitch;  i++)
+        {
+            s->pitchbuf[i] = s->history[PLC_HISTORY_LEN - s->pitch + i]*(1.0f - new_weight) + s->history[PLC_HISTORY_LEN - 2*s->pitch + i]*new_weight;
+            new_weight += new_step;
+        }
+        /* We should now be ready to fill in the gap with repeated, decaying cycles
+           of what is in pitchbuf */
+
+        gain = 1.0f;
+        /* We need to OLA the first 1/4 wavelength of the synthetic data, to smooth
+           it into the previous real data. To avoid the need to introduce a delay
+           in the stream, reverse the last 1/4 wavelength, and OLA with that. */
+        new_step = 1.0f/pitch_overlap;
+        old_step = new_step;
+        new_weight = new_step;
+        old_weight = 1.0f - new_step;
+        for (i = 0;  i < pitch_overlap;  i++)
+        {
+            amp[i] = fsaturate(old_weight*s->history[PLC_HISTORY_LEN - 1 - i] + new_weight*s->pitchbuf[i]);
+            new_weight += new_step;
+            old_weight -= old_step;
+            if (old_weight < 0.0f)
+                old_weight = 0.0f;
+        }
+        s->pitch_offset = i;
+    }
+    else
+    {
+        gain = 1.0f - s->missing_samples*ATTENUATION_INCREMENT;
+        i = 0;
+    }
+    for (  ;  gain > 0.0f  &&  i < len;  i++)
+    {
+        amp[i] = (int16_t) (s->pitchbuf[s->pitch_offset]*gain);
+        gain -= ATTENUATION_INCREMENT;
+        if (++s->pitch_offset >= s->pitch)
+            s->pitch_offset = 0;
+    }
+    for (  ;  i < len;  i++)
+        amp[i] = 0;
+    s->missing_samples += orig_len;
+    save_history(s, amp, len);
+    return len;
+}
+/*- End of function --------------------------------------------------------*/
+
+plc_state_t *plc_init(plc_state_t *s)
+{
+    if (s == NULL)
+    {
+        if ((s = (plc_state_t *) malloc(sizeof(*s))) == NULL)
+            return NULL;
+    }
+    memset(s, 0, sizeof(*s));
+    return s;
+}
+/*- End of function --------------------------------------------------------*/
+
+int plc_free(plc_state_t *s)
+{
+    if (s)
+        free(s);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/power_meter.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/power_meter.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,124 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * power_meter.c
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2003 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: power_meter.c,v 1.24 2008/07/02 14:48:26 steveu Exp $
+ */
+
+/*! \file */
+
+#if defined(HAVE_CONFIG_H)
+#include <config.h>
+#endif
+
+#include <inttypes.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <string.h>
+#include <float.h>
+#include "floating_fudge.h"
+#if defined(HAVE_TGMATH_H)
+#include <tgmath.h>
+#endif
+#if defined(HAVE_MATH_H)
+#include <math.h>
+#endif
+#include <assert.h>
+
+#include "spandsp/telephony.h"
+#include "spandsp/power_meter.h"
+
+power_meter_t *power_meter_init(power_meter_t *s, int shift)
+{
+    if (s == NULL)
+    {
+        if ((s = (power_meter_t *) malloc(sizeof(*s))) == NULL)
+            return NULL;
+    }
+    s->shift = shift;
+    s->reading = 0;
+    return s;
+}
+/*- End of function --------------------------------------------------------*/
+
+power_meter_t *power_meter_damping(power_meter_t *s, int shift)
+{
+    s->shift = shift;
+    return s;
+}
+/*- End of function --------------------------------------------------------*/
+
+int32_t power_meter_update(power_meter_t *s, int16_t amp)
+{
+    s->reading += ((amp*amp - s->reading) >> s->shift);
+    return s->reading;
+}
+/*- End of function --------------------------------------------------------*/
+
+int32_t power_meter_level_dbm0(float level)
+{
+    float l;
+
+    level -= DBM0_MAX_POWER;
+    if (level > 0.0)
+        level = 0.0;
+    l = powf(10.0f, level/10.0f)*(32767.0f*32767.0f);
+    return (int32_t) l;
+}
+/*- End of function --------------------------------------------------------*/
+
+int32_t power_meter_level_dbov(float level)
+{
+    float l;
+
+    if (level > 0.0)
+        level = 0.0;
+    l = powf(10.0f, level/10.0f)*(32767.0f*32767.0f);
+    return (int32_t) l;
+}
+/*- End of function --------------------------------------------------------*/
+
+int32_t power_meter_current(power_meter_t *s)
+{
+    return s->reading;
+}
+/*- End of function --------------------------------------------------------*/
+
+float power_meter_current_dbm0(power_meter_t *s)
+{
+    if (s->reading <= 0)
+        return FLT_MIN;
+    /* This is based on A-law, but u-law is only 0.03dB different, so don't worry. */
+    return log10f((float) s->reading/(32767.0f*32767.0f))*10.0f + DBM0_MAX_POWER;
+}
+/*- End of function --------------------------------------------------------*/
+
+float power_meter_current_dbov(power_meter_t *s)
+{
+    if (s->reading <= 0)
+        return FLT_MIN;
+    return log10f((float) s->reading/(32767.0f*32767.0f))*10.0f;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/queue.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/queue.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,415 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * queue.c - simple in-process message queuing
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2004 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: queue.c,v 1.22 2008/05/15 14:26:30 steveu Exp $
+ */
+
+/*! \file */
+
+#if defined(HAVE_CONFIG_H)
+#include <config.h>
+#endif
+
+#include <stdio.h>
+#include <string.h>
+#include <time.h>
+#include <ctype.h>
+#include <stdlib.h>
+#include <inttypes.h>
+#include <sys/types.h>
+
+#include "spandsp/queue.h"
+
+int queue_empty(queue_state_t *s)
+{
+    return (s->iptr == s->optr);
+}
+/*- End of function --------------------------------------------------------*/
+
+int queue_free_space(queue_state_t *s)
+{
+    int len;
+    
+    if ((len = s->optr - s->iptr - 1) < 0)
+        len += s->len;
+    /*endif*/
+    return len;
+}
+/*- End of function --------------------------------------------------------*/
+
+int queue_contents(queue_state_t *s)
+{
+    int len;
+    
+    if ((len = s->iptr - s->optr) < 0)
+        len += s->len;
+    /*endif*/
+    return len;
+}
+/*- End of function --------------------------------------------------------*/
+
+void queue_flush(queue_state_t *s)
+{
+    s->optr = s->iptr;
+}
+/*- End of function --------------------------------------------------------*/
+
+int queue_view(queue_state_t *s, uint8_t *buf, int len)
+{
+    int real_len;
+    int to_end;
+    int iptr;
+    int optr;
+    
+    /* Snapshot the values (although only iptr should be changeable during this processing) */
+    iptr = s->iptr;
+    optr = s->optr;
+    if ((real_len = iptr - optr) < 0)
+        real_len += s->len;
+    /*endif*/
+    if (real_len < len)
+    {
+        if (s->flags & QUEUE_READ_ATOMIC)
+            return -1;
+        /*endif*/
+    }
+    else
+    {
+        real_len = len;
+    }
+    /*endif*/
+    if (real_len == 0)
+        return 0;
+    /*endif*/
+    to_end = s->len - optr;
+    if (iptr < optr  &&  to_end < real_len)
+    {
+        /* A two step process */
+        if (buf)
+        {
+            memcpy(buf, s->data + optr, to_end);
+            memcpy(buf + to_end, s->data, real_len - to_end);
+        }
+        /*endif*/
+    }
+    else
+    {
+        /* A one step process */
+        if (buf)
+            memcpy(buf, s->data + optr, real_len);
+        /*endif*/
+    }
+    /*endif*/
+    return real_len;
+}
+/*- End of function --------------------------------------------------------*/
+
+int queue_read(queue_state_t *s, uint8_t *buf, int len)
+{
+    int real_len;
+    int to_end;
+    int new_optr;
+    int iptr;
+    int optr;
+    
+    /* Snapshot the values (although only iptr should be changeable during this processing) */
+    iptr = s->iptr;
+    optr = s->optr;
+    if ((real_len = iptr - optr) < 0)
+        real_len += s->len;
+    /*endif*/
+    if (real_len < len)
+    {
+        if (s->flags & QUEUE_READ_ATOMIC)
+            return -1;
+        /*endif*/
+    }
+    else
+    {
+        real_len = len;
+    }
+    /*endif*/
+    if (real_len == 0)
+        return 0;
+    /*endif*/
+    to_end = s->len - optr;
+    if (iptr < optr  &&  to_end < real_len)
+    {
+        /* A two step process */
+        if (buf)
+        {
+            memcpy(buf, s->data + optr, to_end);
+            memcpy(buf + to_end, s->data, real_len - to_end);
+        }
+        /*endif*/
+        new_optr = real_len - to_end;
+    }
+    else
+    {
+        /* A one step process */
+        if (buf)
+            memcpy(buf, s->data + optr, real_len);
+        /*endif*/
+        new_optr = optr + real_len;
+        if (new_optr > s->len)
+            new_optr = 0;
+        /*endif*/
+    }
+    /*endif*/
+    /* Only change the pointer now we have really finished */
+    s->optr = new_optr;
+    return real_len;
+}
+/*- End of function --------------------------------------------------------*/
+
+int queue_read_byte(queue_state_t *s)
+{
+    int real_len;
+    int to_end;
+    int iptr;
+    int optr;
+    int byte;
+    
+    /* Snapshot the values (although only iptr should be changeable during this processing) */
+    iptr = s->iptr;
+    optr = s->optr;
+    if ((real_len = iptr - optr) < 0)
+        real_len += s->len;
+    /*endif*/
+    if (real_len < 1)
+        return -1;
+    /*endif*/
+    to_end = s->len - optr;
+    byte = s->data[optr];
+    if (++optr >= s->len)
+        optr = 0;
+    /*endif*/
+    /* Only change the pointer now we have really finished */
+    s->optr = optr;
+    return byte;
+}
+/*- End of function --------------------------------------------------------*/
+
+int queue_write(queue_state_t *s, const uint8_t *buf, int len)
+{
+    int real_len;
+    int to_end;
+    int new_iptr;
+    int iptr;
+    int optr;
+
+    /* Snapshot the values (although only optr should be changeable during this processing) */
+    iptr = s->iptr;
+    optr = s->optr;
+
+    if ((real_len = optr - iptr - 1) < 0)
+        real_len += s->len;
+    /*endif*/
+    if (real_len < len)
+    {
+        if (s->flags & QUEUE_WRITE_ATOMIC)
+            return -1;
+        /*endif*/
+    }
+    else
+    {
+        real_len = len;
+    }
+    /*endif*/
+    if (real_len == 0)
+        return 0;
+    /*endif*/
+    to_end = s->len - iptr;
+    if (iptr < optr  ||  to_end >= real_len)
+    {
+        /* A one step process */
+        memcpy(s->data + iptr, buf, real_len);
+        new_iptr = iptr + real_len;
+        if (new_iptr > s->len)
+            new_iptr = 0;
+        /*endif*/
+    }
+    else
+    {
+        /* A two step process */
+        memcpy(s->data + iptr, buf, to_end);
+        memcpy(s->data, buf + to_end, real_len - to_end);
+        new_iptr = real_len - to_end;
+    }
+    /*endif*/
+    /* Only change the pointer now we have really finished */
+    s->iptr = new_iptr;
+    return real_len;
+}
+/*- End of function --------------------------------------------------------*/
+
+int queue_write_byte(queue_state_t *s, uint8_t byte)
+{
+    int real_len;
+    int iptr;
+    int optr;
+
+    /* Snapshot the values (although only optr should be changeable during this processing) */
+    iptr = s->iptr;
+    optr = s->optr;
+
+    if ((real_len = optr - iptr - 1) < 0)
+        real_len += s->len;
+    /*endif*/
+    if (real_len < 1)
+    {
+        if (s->flags & QUEUE_WRITE_ATOMIC)
+            return -1;
+        /*endif*/
+        return 0;
+    }
+    /*endif*/
+    s->data[iptr] = byte;
+    if (++iptr >= s->len)
+        iptr = 0;
+    /*endif*/
+    /* Only change the pointer now we have really finished */
+    s->iptr = iptr;
+    return 1;
+}
+/*- End of function --------------------------------------------------------*/
+
+int queue_state_test_msg(queue_state_t *s)
+{
+    uint16_t lenx;
+
+    if (queue_view(s, (uint8_t *) &lenx, sizeof(uint16_t)) != sizeof(uint16_t))
+        return -1;
+    /*endif*/
+    return lenx;
+}
+/*- End of function --------------------------------------------------------*/
+
+int queue_read_msg(queue_state_t *s, uint8_t *buf, int len)
+{
+    uint16_t lenx;
+
+    /* If we assume the write message was atomic, this read message should be
+      safe when conducted in multiple chunks */
+    if (queue_read(s, (uint8_t *) &lenx, sizeof(uint16_t)) != sizeof(uint16_t))
+        return -1;
+    /*endif*/
+    /* If we got this far, the actual message chunk should be guaranteed to be
+       available */
+    if (lenx == 0)
+        return 0;
+    /*endif*/
+    if ((int) lenx > len)
+    {
+        len = queue_read(s, buf, len);
+        /* Discard the rest of the message */
+        queue_read(s, NULL, lenx - len);
+        return len;
+    }
+    /*endif*/
+    return queue_read(s, buf, lenx);
+}
+/*- End of function --------------------------------------------------------*/
+
+int queue_write_msg(queue_state_t *s, const uint8_t *buf, int len)
+{
+    int real_len;
+    int to_end;
+    int new_iptr;
+    int iptr;
+    int optr;
+    uint16_t lenx;
+
+    /* Snapshot the values (although only optr should be changeable during this processing) */
+    iptr = s->iptr;
+    optr = s->optr;
+
+    if ((real_len = optr - iptr - 1) < 0)
+        real_len += s->len;
+    /*endif*/
+    if (real_len < len + sizeof(uint16_t))
+        return -1;
+    /*endif*/
+    real_len = len + sizeof(uint16_t);
+
+    to_end = s->len - iptr;
+    lenx = (uint16_t) len;
+    if (iptr < optr  ||  to_end >= real_len)
+    {
+        /* A one step process */
+        memcpy(s->data + iptr, &lenx, sizeof(uint16_t));
+        memcpy(s->data + iptr + sizeof(uint16_t), buf, len);
+        new_iptr = iptr + real_len;
+        if (new_iptr > s->len)
+            new_iptr = 0;
+        /*endif*/
+    }
+    else
+    {
+        /* A two step process */
+        if (to_end >= sizeof(uint16_t))
+        {
+            /* The actual message wraps around the end of the buffer */
+            memcpy(s->data + iptr, &lenx, sizeof(uint16_t));
+            memcpy(s->data + iptr + sizeof(uint16_t), buf, to_end - sizeof(uint16_t));
+            memcpy(s->data, buf + to_end - sizeof(uint16_t), real_len - to_end);
+        }
+        else
+        {
+            /* The message length wraps around the end of the buffer */
+            memcpy(s->data + iptr, (uint8_t *) &lenx, to_end);
+            memcpy(s->data, ((uint8_t *) &lenx) + to_end, sizeof(uint16_t) - to_end);
+            memcpy(s->data + sizeof(uint16_t) - to_end, buf, len);
+        }
+        new_iptr = real_len - to_end;
+    }
+    /*endif*/
+    /* Only change the pointer now we have really finished */
+    s->iptr = new_iptr;
+    return len;
+}
+/*- End of function --------------------------------------------------------*/
+
+queue_state_t *queue_init(queue_state_t *s, int len, int flags)
+{
+    if (s == NULL)
+    {
+        if ((s = (queue_state_t *) malloc(sizeof(*s) + len + 1)) == NULL)
+            return NULL;
+    }
+    s->iptr =
+    s->optr = 0;
+    s->flags = flags;
+    s->len = len + 1;
+    return s;
+}
+/*- End of function --------------------------------------------------------*/
+
+int queue_free(queue_state_t *s)
+{
+    free(s);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/schedule.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/schedule.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,149 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * schedule.c
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2004 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: schedule.c,v 1.17 2008/05/13 13:17:23 steveu Exp $
+ */
+
+#if defined(HAVE_CONFIG_H)
+#include <config.h>
+#endif
+
+#include <stdio.h>
+#include <inttypes.h>
+#include <stdlib.h>
+#include <memory.h>
+
+#include "spandsp/telephony.h"
+#include "spandsp/logging.h"
+#include "spandsp/schedule.h"
+
+int span_schedule_event(span_sched_state_t *s, int us, span_sched_callback_func_t function, void *user_data)
+{
+    int i;
+
+    for (i = 0;  i < s->max_to_date;  i++)
+    {
+        if (s->sched[i].callback == NULL)
+            break;
+        /*endif*/
+    }
+    /*endfor*/
+    if (i >= s->allocated)
+    {
+        s->allocated += 5;
+        s->sched = (span_sched_t *) realloc(s->sched, sizeof(span_sched_t)*s->allocated);
+    }
+    /*endif*/
+    if (i >= s->max_to_date)
+        s->max_to_date = i + 1;
+    /*endif*/
+    s->sched[i].when = s->ticker + us;
+    s->sched[i].callback = function;
+    s->sched[i].user_data = user_data;
+    return i;
+}
+/*- End of function --------------------------------------------------------*/
+
+uint64_t span_schedule_next(span_sched_state_t *s)
+{
+    int i;
+    uint64_t earliest;
+
+    earliest = ~((uint64_t) 0);
+    for (i = 0;  i < s->max_to_date;  i++)
+    {
+        if (s->sched[i].callback  &&  earliest > s->sched[i].when)
+            earliest = s->sched[i].when;
+        /*endif*/
+    }
+    /*endfor*/
+    return earliest;
+}
+/*- End of function --------------------------------------------------------*/
+
+uint64_t span_schedule_time(span_sched_state_t *s)
+{
+    return s->ticker;
+}
+/*- End of function --------------------------------------------------------*/
+
+void span_schedule_update(span_sched_state_t *s, int us)
+{
+    int i;
+    span_sched_callback_func_t callback;
+    void *user_data;
+
+    s->ticker += us;
+    for (i = 0;  i < s->max_to_date;  i++)
+    {
+        if (s->sched[i].callback  &&  s->sched[i].when <= s->ticker)
+        {
+            callback = s->sched[i].callback;
+            user_data = s->sched[i].user_data;
+            s->sched[i].callback = NULL;
+            s->sched[i].user_data = NULL;
+            callback(s, user_data);
+        }
+        /*endif*/
+    }
+    /*endfor*/
+}
+/*- End of function --------------------------------------------------------*/
+
+void span_schedule_del(span_sched_state_t *s, int i)
+{
+    if (i >= s->max_to_date
+        ||
+        i < 0
+        ||
+        s->sched[i].callback == NULL)
+    {
+        span_log(&s->logging, SPAN_LOG_WARNING, "Requested to delete invalid scheduled ID %d ?\n", i);
+        return;
+    }
+    /*endif*/
+    s->sched[i].callback = NULL;
+}
+/*- End of function --------------------------------------------------------*/
+
+span_sched_state_t *span_schedule_init(span_sched_state_t *s)
+{
+    memset(s, 0, sizeof(*s));
+    span_log_init(&s->logging, SPAN_LOG_NONE, NULL);
+    span_log_set_protocol(&s->logging, "SCHEDULE");
+    return s;
+}
+/*- End of function --------------------------------------------------------*/
+
+int span_schedule_release(span_sched_state_t *s)
+{
+    if (s->sched)
+    {
+        free(s->sched);
+        s->sched = NULL;
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/sig_tone.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/sig_tone.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,673 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * sig_tone.c - Signalling tone processing for the 2280Hz, 2600Hz and similar
+ *              signalling tone used in older protocols.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2004 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: sig_tone.c,v 1.24 2008/08/17 16:25:52 steveu Exp $
+ */
+
+/*! \file */
+
+#if defined(HAVE_CONFIG_H)
+#include <config.h>
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <inttypes.h>
+#include "floating_fudge.h"
+#if defined(HAVE_TGMATH_H)
+#include <tgmath.h>
+#endif
+#if defined(HAVE_MATH_H)
+#include <math.h>
+#endif
+#include <memory.h>
+#include <string.h>
+
+#undef SPANDSP_USE_FIXED_POINT
+#include "spandsp/telephony.h"
+#include "spandsp/dc_restore.h"
+#include "spandsp/complex.h"
+#include "spandsp/dds.h"
+#include "spandsp/sig_tone.h"
+
+#define PI 3.14159265358979323
+
+/* The coefficients for the data notch filter. This filter is also the
+   guard filter for tone detection. */
+
+sig_tone_descriptor_t sig_tones[4] =
+{
+    {
+        /* 2280Hz (e.g. AC15, and many other European protocols) */
+        {2280,  0},
+        {-10, -20},                 /* -10+-1 dBmO and -20+-1 dBm0 */
+        400*(SAMPLE_RATE/1000),     /* 300ms to 550ms */
+    
+        225*(SAMPLE_RATE/1000),
+    
+        225*(SAMPLE_RATE/1000),
+        TRUE,
+    
+        24,
+        64,
+
+        1,
+        {
+            {
+#if defined(SPANDSP_USE_FIXED_POINT)
+                {  3600,        14397,          32767},
+                {     0,        -9425,         -28954},
+                {     0,        14196,          32767},
+                {     0,       -17393,         -28954},
+                12,
+#else
+                {0.878906f,     0.439362f,      1.0f},
+                {0.0f,         -0.287627f,     -0.883605f},
+                {0.0f,          0.433228f,      1.0f},
+                {0.0f,         -0.530792f,     -0.883605f},
+#endif
+            },
+            {
+#if defined(SPANDSP_USE_FIXED_POINT)
+                {     0,            0,              0},
+                {     0,            0,              0},
+                {     0,            0,              0},
+                {     0,            0,              0},
+                0,
+#else
+                {0.0f,          0.0f,           0.0f},
+                {0.0f,          0.0f,           0.0f},
+                {0.0f,          0.0f,           0.0f},
+                {0.0f,          0.0f,           0.0f},
+#endif
+            }
+        },
+#if defined(SPANDSP_USE_FIXED_POINT)
+        { 12900,       -16384,         -16384}, 
+        {     0,        -8578,         -11796},
+        15,
+#else
+        {0.393676f,    -0.5f,          -0.5f}, 
+        {0.0f,         -0.261778f,     -0.359985f},
+#endif
+
+        31744,
+        1024,
+    
+        31744,
+        187,
+    
+        31744,
+        187,
+    
+        -1,
+        -32,
+    
+        57
+    },
+    {
+        /* 2600Hz (e.g. many US protocols) */
+        {2600, 0},
+        {-8, -8},
+        400*(SAMPLE_RATE/1000),
+    
+        225*(SAMPLE_RATE/1000),
+    
+        225*(SAMPLE_RATE/1000),
+        FALSE,
+    
+        24,
+        64,
+
+        1,
+        {
+            {            
+#if defined(SPANDSP_USE_FIXED_POINT)
+                {  3539,        29569,          32767},
+                {     0,       -24010,         -28341},
+                {     0,        29844,          32767},
+                {     0,       -31208,         -28341},
+                12,
+#else
+                {0.864014f,     0.902374f,      1.0f},
+                {0.0f,         -0.732727f,     -0.864899f},
+                {0.0f,          0.910766f,      1.0f},
+                {0.0f,         -0.952393f,     -0.864899f},
+#endif
+            },
+            {            
+#if defined(SPANDSP_USE_FIXED_POINT)
+                {     0,            0,              0},
+                {     0,            0,              0},
+                {     0,            0,              0},
+                {     0,            0,              0},
+                0,
+#else
+                {0.0f,          0.0f,           0.0f},
+                {0.0f,          0.0f,           0.0f},
+                {0.0f,          0.0f,           0.0f},
+                {0.0f,          0.0f,           0.0f},
+#endif
+            }
+        },
+#if defined(SPANDSP_USE_FIXED_POINT)
+        { 32768,            0,              0},
+        {     0,            0,              0},
+        15,
+#else
+        {1.0f,          0.0f,           0.0f},
+        {0.0f,          0.0f,           0.0f},
+#endif
+    
+        31744,
+        1024,
+    
+        31744,
+        170,
+    
+        31744,
+        170,
+    
+        -1,
+        -32,
+    
+        52
+    },
+    {
+        /* 2400Hz/2600Hz (e.g. SS5 and SS5bis) */
+        {2400, 2600},
+        {-8, -8},
+        400*(SAMPLE_RATE/1000),
+
+        225*(SAMPLE_RATE/1000),
+
+        225*(SAMPLE_RATE/1000),
+        FALSE,
+
+        24,
+        64,
+
+        2,
+        {
+            {
+#if defined(SPANDSP_USE_FIXED_POINT)
+                {  3539,        29569,          32767},
+                {     0,       -24010,         -28341},
+                {     0,        29844,          32767},
+                {     0,       -31208,         -28341},
+                12,
+#else
+                {0.864014f,     0.902374f,      1.0f},
+                {0.0f,         -0.732727f,     -0.864899f},
+                {0.0f,          0.910766f,      1.0f},
+                {0.0f,         -0.952393f,     -0.864899f},
+#endif
+            },
+            {
+#if defined(SPANDSP_USE_FIXED_POINT)
+                {  3539,        20349,          32767},
+                {     0,       -22075,         -31856},
+                {     0,        20174,          32767},
+                {     0,       -17832,         -31836},
+                12,
+#else
+                {0.864014f,     0.621007f,      1.0f},
+                {0.0f,         -0.673667f,     -0.972167f},
+                {0.0f,          0.615669f,      1.0f},
+                {0.0f,         -0.544180f,     -0.971546f},
+#endif
+            }
+        },
+#if defined(SPANDSP_USE_FIXED_POINT)
+        { 32768,            0,              0},
+        {     0,            0,              0},
+        15,
+#else
+        {1.0f,          0.0f,           0.0f},
+        {0.0f,          0.0f,           0.0f},
+#endif
+    
+        31744,
+        1024,
+    
+        31744,
+        170,
+    
+        31744,
+        170,
+    
+        -1,
+        -32,
+    
+        52
+    }
+};
+
+int sig_tone_tx(sig_tone_tx_state_t *s, int16_t amp[], int len)
+{
+    int i;
+    int j;
+    int n;
+    int16_t tone;
+    int update;
+    int high_low;
+
+    for (i = 0;  i < len;  i += n)
+    {
+        if (s->current_tx_timeout < len - i)
+            n = s->current_tx_timeout;
+        else
+            n = len - i;
+        if ((s->current_tx_tone & (SIG_TONE_1_PRESENT  ||  SIG_TONE_2_PRESENT)))
+        {
+            /* Are we in the early phase (high tone energy level), or the sustaining
+               phase (low tone energy level) of tone generation? */
+            if (s->high_low_timer > 0)
+            {
+                if (n > s->high_low_timer)
+                    n = s->high_low_timer;
+                s->high_low_timer -= n;
+                high_low = 0;
+            }
+            else
+            {
+                high_low = 1;
+            }
+            /*endif*/
+            if (s->current_tx_tone & SIG_TONE_TX_PASSTHROUGH)
+            {
+                for (j = i;  j < i + n;  j++)
+                {
+                    tone = dds_mod(&(s->phase_acc[0]), s->phase_rate[0], s->tone_scaling[high_low], 0);
+                    amp[j] = saturate(amp[j] + tone);
+                }
+                /*endfor*/
+            }
+            else
+            {
+                for (j = i;  j < i + n;  j++)
+                    amp[j] = dds_mod(&(s->phase_acc[0]), s->phase_rate[0], s->tone_scaling[high_low], 0);
+                /*endfor*/
+            }
+            /*endif*/
+        }
+        else
+        {
+            /* There is no tone. We either leave the audio in the buffer, or silence it. */
+            if (!(s->current_tx_tone & SIG_TONE_TX_PASSTHROUGH))
+            {
+                /* Zap any audio in the buffer */
+                memset(amp + i, 0, sizeof(int16_t)*n);
+            }
+            /*endif*/
+        }
+        /*endif*/
+        if ((s->current_tx_timeout -= n) <= 0)
+        {
+            if (s->sig_update)
+            {
+                update = s->sig_update(s->user_data, SIG_TONE_UPDATE_REQUEST);
+                if ((update & 0x03) == 0x03)
+                    s->high_low_timer = s->desc->high_low_timeout;
+                /*endif*/
+                s->current_tx_tone = update & 0xFFFF;
+                s->current_tx_timeout = (update >> 16) & 0xFFFF;
+            }
+            /*endif*/
+        }
+        /*endif*/
+    }
+    /*endfor*/
+    return len;
+}
+/*- End of function --------------------------------------------------------*/
+
+void sig_tone_tx_set_mode(sig_tone_tx_state_t *s, int mode)
+{
+    if ((mode & 0x03) == 0x03  &&  !(s->current_tx_tone & SIG_TONE_1_PRESENT))
+        s->high_low_timer = s->desc->high_low_timeout;
+    /*endif*/
+    s->current_tx_tone = mode & 0xFFFF;
+    s->current_tx_timeout = (mode >> 16) & 0xFFFF;
+}
+/*- End of function --------------------------------------------------------*/
+
+sig_tone_tx_state_t *sig_tone_tx_init(sig_tone_tx_state_t *s, int tone_type, sig_tone_func_t sig_update, void *user_data)
+{
+    int i;
+
+    if (sig_update == NULL  ||  tone_type < 1  ||  tone_type > 3)
+        return NULL;
+    /*endif*/
+
+    if (s == NULL)
+    {
+        if ((s = (sig_tone_tx_state_t *) malloc(sizeof(*s))) == NULL)
+            return NULL;
+    }
+    memset(s, 0, sizeof(*s));
+
+    s->sig_update = sig_update;
+    s->user_data = user_data;
+
+    s->desc = &sig_tones[tone_type - 1];
+
+    for (i = 0;  i < 2;  i++)
+    {
+        if (s->desc->tone_freq[i])
+            s->phase_rate[i] = dds_phase_rate((float) s->desc->tone_freq[i]);
+        else
+            s->phase_rate[i] = 0;
+        s->tone_scaling[i] = dds_scaling_dbm0((float) s->desc->tone_amp[i]);
+    }
+    return s;
+}
+/*- End of function --------------------------------------------------------*/
+
+int sig_tone_rx(sig_tone_rx_state_t *s, int16_t amp[], int len)
+{
+#if defined(SPANDSP_USE_FIXED_POINT)
+    int32_t x;
+    int32_t notched_signal;
+    int32_t bandpass_signal;
+#else
+    float x;
+    float notched_signal;
+    float bandpass_signal;
+#endif
+    int i;
+    int j;
+    int32_t mown_notch[2];
+    int32_t mown_bandpass;
+
+    for (i = 0;  i < len;  i++)
+    {
+        if (s->signaling_state_duration < 0xFFFF)
+            s->signaling_state_duration++;
+        /*endif*/
+        notched_signal = 0;
+        for (j = 0;  j < s->desc->tones;  j++)
+        {
+            /* The notch filter is two cascaded biquads. */
+            notched_signal = amp[i];
+
+#if defined(SPANDSP_USE_FIXED_POINT)
+            notched_signal *= s->desc->tone[j].notch_a1[0];
+            notched_signal += s->tone[j].notch_z1[1]*s->desc->tone[j].notch_b1[1];
+            notched_signal += s->tone[j].notch_z1[2]*s->desc->tone[j].notch_b1[2];
+            x = notched_signal;
+            notched_signal += s->tone[j].notch_z1[1]*s->desc->tone[j].notch_a1[1];
+            notched_signal += s->tone[j].notch_z1[2]*s->desc->tone[j].notch_a1[2];
+            s->tone[j].notch_z1[2] = s->tone[j].notch_z1[1];
+            s->tone[j].notch_z1[1] = x >> 15;
+
+            notched_signal += s->tone[j].notch_z2[1]*s->desc->tone[j].notch_b2[1];
+            notched_signal += s->tone[j].notch_z2[2]*s->desc->tone[j].notch_b2[2];
+            x = notched_signal;
+            notched_signal += s->tone[j].notch_z2[1]*s->desc->tone[j].notch_a2[1];
+            notched_signal += s->tone[j].notch_z2[2]*s->desc->tone[j].notch_a2[2];
+            s->tone[j].notch_z2[2] = s->tone[j].notch_z2[1];
+            s->tone[j].notch_z2[1] = x >> 15;
+
+            notched_signal >>= s->desc->notch_postscale;
+#else
+            notched_signal *= s->desc->tone[j].notch_a1[0];
+            notched_signal += s->tone[j].notch_z1[1]*s->desc->tone[j].notch_b1[1];
+            notched_signal += s->tone[j].notch_z1[2]*s->desc->tone[j].notch_b1[2];
+            x = notched_signal;
+            notched_signal += s->tone[j].notch_z1[1]*s->desc->tone[j].notch_a1[1];
+            notched_signal += s->tone[j].notch_z1[2]*s->desc->tone[j].notch_a1[2];
+            s->tone[j].notch_z1[2] = s->tone[j].notch_z1[1];
+            s->tone[j].notch_z1[1] = x;
+
+            notched_signal += s->tone[j].notch_z2[1]*s->desc->tone[j].notch_b2[1];
+            notched_signal += s->tone[j].notch_z2[2]*s->desc->tone[j].notch_b2[2];
+            x = notched_signal;
+            notched_signal += s->tone[j].notch_z2[1]*s->desc->tone[j].notch_a2[1];
+            notched_signal += s->tone[j].notch_z2[2]*s->desc->tone[j].notch_a2[2];
+            s->tone[j].notch_z2[2] = s->tone[j].notch_z2[1];
+            s->tone[j].notch_z2[1] = x;
+#endif
+            /* Modulus and leaky integrate the notched data. The result of
+               this isn't used in low tone detect mode, but we must keep notch_zl
+               rolling along. */
+            s->tone[j].notch_zl = ((s->tone[j].notch_zl*s->desc->notch_slugi) >> 15)
+                                + ((abs(notched_signal)*s->desc->notch_slugp) >> 15);
+            /* Mow the grass to weed out the noise! */
+            mown_notch[j] = s->tone[0].notch_zl & s->desc->notch_threshold;
+        }
+
+        if (s->tone_present)
+        {
+            if (s->flat_mode_timeout <= 0)
+                s->flat_mode = TRUE;
+            else
+                s->flat_mode_timeout--;
+            /*endif*/
+        }
+        else
+        {
+            s->flat_mode_timeout = s->desc->sharp_flat_timeout;
+            s->flat_mode = FALSE;
+        }
+        /*endif*/
+
+        if (s->flat_mode)
+        {
+            /* Flat mode */
+    
+            /* The bandpass filter is a single bi-quad stage */
+            bandpass_signal = amp[i];
+#if defined(SPANDSP_USE_FIXED_POINT)
+            bandpass_signal *= s->desc->broad_a[0];
+            bandpass_signal += s->broad_z[1]*s->desc->broad_b[1];
+            bandpass_signal += s->broad_z[2]*s->desc->broad_b[2];
+            x = bandpass_signal;
+            bandpass_signal += s->broad_z[1]*s->desc->broad_a[1];
+            bandpass_signal += s->broad_z[2]*s->desc->broad_a[2];
+            s->broad_z[2] = s->broad_z[1];
+            s->broad_z[1] = x >> 15;
+            bandpass_signal >>= s->desc->broad_postscale;
+#else
+            bandpass_signal *= s->desc->broad_a[0];
+            bandpass_signal += s->broad_z[1]*s->desc->broad_b[1];
+            bandpass_signal += s->broad_z[2]*s->desc->broad_b[2];
+            x = bandpass_signal;
+            bandpass_signal += s->broad_z[1]*s->desc->broad_a[1];
+            bandpass_signal += s->broad_z[2]*s->desc->broad_a[2];
+            s->broad_z[2] = s->broad_z[1];
+            s->broad_z[1] = x;
+#endif            
+            /* Leaky integrate the bandpassed data */
+            s->broad_zl = ((s->broad_zl*s->desc->broad_slugi) >> 15)
+                        + ((abs(bandpass_signal)*s->desc->broad_slugp) >> 15);
+    
+            /* For the broad band receiver we use a simple linear threshold! */
+            if (s->tone_present)
+            {
+                s->tone_present = (s->broad_zl > s->desc->broad_threshold);
+                if (!s->tone_present)
+                {
+                    if (s->sig_update)
+                        s->sig_update(s->user_data, SIG_TONE_1_CHANGE | (s->signaling_state_duration << 16));
+                    /*endif*/
+                    s->signaling_state_duration = 0;
+                }
+                /*endif*/
+            }
+            else
+            {
+                s->tone_present = (s->broad_zl > s->desc->broad_threshold);
+                if (s->tone_present)
+                {
+                    if (s->sig_update)
+                        s->sig_update(s->user_data, SIG_TONE_1_CHANGE | SIG_TONE_1_PRESENT | (s->signaling_state_duration << 16));
+                    /*endif*/
+                    s->signaling_state_duration = 0;
+                }
+                /*endif*/
+            }
+            /*endif*/
+            /* Notch insertion logic */
+    
+            /* tone_present and tone_on are equivalent in flat mode */
+            if (s->tone_present)
+            {
+                s->notch_enabled = s->desc->notch_allowed;
+                s->notch_insertion_timeout = s->desc->notch_lag_time;
+            }
+            else
+            {
+                if (s->notch_insertion_timeout > 0)
+                    s->notch_insertion_timeout--;
+                else
+                    s->notch_enabled = FALSE;
+                /*endif*/
+            }
+            /*endif*/
+        }
+        else
+        {
+            /* Sharp mode */
+
+            /* Modulus and leaky integrate the data */
+            s->broad_zl = ((s->broad_zl*s->desc->unfiltered_slugi) >> 15)
+                        + ((abs(amp[i])*s->desc->unfiltered_slugp) >> 15);
+     
+            /* Mow the grass to weed out the noise! */
+            mown_bandpass = s->broad_zl & s->desc->unfiltered_threshold;
+    
+            /* Persistence checking and notch insertion logic */
+            if (!s->tone_present)
+            {
+                if (mown_notch[0] < mown_bandpass)
+                {
+                    /* Tone is detected this sample */
+                    if (s->tone_persistence_timeout <= 0)
+                    {
+                        s->tone_present = TRUE;
+                        s->notch_enabled = s->desc->notch_allowed;
+                        s->tone_persistence_timeout = s->desc->tone_off_check_time;
+                        s->notch_insertion_timeout = s->desc->notch_lag_time;
+                        if (s->sig_update)
+                            s->sig_update(s->user_data, SIG_TONE_1_CHANGE | SIG_TONE_1_PRESENT | (s->signaling_state_duration << 16));
+                        /*endif*/
+                        s->signaling_state_duration = 0;
+                    }
+                    else
+                    {
+                        s->tone_persistence_timeout--;
+                        if (s->notch_insertion_timeout > 0)
+                            s->notch_insertion_timeout--;
+                        else
+                            s->notch_enabled = FALSE;
+                        /*endif*/
+                    }
+                    /*endif*/
+                }
+                else
+                {
+                    s->tone_persistence_timeout = s->desc->tone_on_check_time;
+                    if (s->notch_insertion_timeout > 0)
+                        s->notch_insertion_timeout--;
+                    else
+                        s->notch_enabled = FALSE;
+                    /*endif*/
+                }
+                /*endif*/
+            }
+            else
+            {
+                if (mown_notch[0] > mown_bandpass)
+                {
+                    /* Tone is not detected this sample */
+                    if (s->tone_persistence_timeout <= 0)
+                    {
+                        s->tone_present = FALSE;
+                        s->tone_persistence_timeout = s->desc->tone_on_check_time;
+                        if (s->sig_update)
+                            s->sig_update(s->user_data, SIG_TONE_1_CHANGE | (s->signaling_state_duration << 16));
+                        /*endif*/
+                        s->signaling_state_duration = 0;
+                    }
+                    else
+                    {
+                        s->tone_persistence_timeout--;
+                    }
+                    /*endif*/
+                }
+                else
+                {
+                    s->tone_persistence_timeout = s->desc->tone_off_check_time;
+                }
+                /*endif*/
+            }
+            /*endif*/
+        }
+        /*endif*/
+
+        if ((s->current_rx_tone & SIG_TONE_RX_PASSTHROUGH))
+        {
+            if (s->notch_enabled)
+                amp[i] = (int16_t) notched_signal;
+            /*endif*/
+        }
+        else
+        {
+            amp[i] = 0;
+        }
+        /*endif*/
+    }
+    /*endfor*/
+    return len;
+}
+/*- End of function --------------------------------------------------------*/
+
+sig_tone_rx_state_t *sig_tone_rx_init(sig_tone_rx_state_t *s, int tone_type, sig_tone_func_t sig_update, void *user_data)
+{
+    if (sig_update == NULL  ||  tone_type < 1  ||  tone_type > 3)
+        return NULL;
+    /*endif*/
+
+    if (s == NULL)
+    {
+        if ((s = (sig_tone_rx_state_t *) malloc(sizeof(*s))) == NULL)
+            return NULL;
+    }
+    memset(s, 0, sizeof(*s));
+
+    s->sig_update = sig_update;
+    s->user_data = user_data;
+
+    s->desc = &sig_tones[tone_type - 1];
+
+    s->flat_mode_timeout = 0;
+    s->notch_insertion_timeout = 0;
+    s->tone_persistence_timeout = 0;
+    s->signaling_state_duration = 0;
+    return s;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/silence_gen.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/silence_gen.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,145 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * silence_gen.c - A silence generator, for inserting timed silences.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2006 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: silence_gen.c,v 1.14 2008/07/26 04:53:00 steveu Exp $
+ */
+
+/*! \file */
+
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
+
+#include <inttypes.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <string.h>
+#include "floating_fudge.h"
+#if defined(HAVE_TGMATH_H)
+#include <tgmath.h>
+#endif
+#if defined(HAVE_MATH_H)
+#include <math.h>
+#endif
+#include <assert.h>
+#include <limits.h>
+
+#include "spandsp/telephony.h"
+#include "spandsp/logging.h"
+#include "spandsp/async.h"
+#include "spandsp/silence_gen.h"
+
+int silence_gen(silence_gen_state_t *s, int16_t *amp, int max_len)
+{
+    if (s->remaining_samples != INT_MAX)
+    {
+        if (max_len >= s->remaining_samples)
+        {
+            max_len = s->remaining_samples;
+            if (max_len  &&  s->status_handler)
+                s->status_handler(s->status_user_data, MODEM_TX_STATUS_SHUTDOWN_COMPLETE);
+        }
+        s->remaining_samples -= max_len;
+    }
+    if (INT_MAX - s->total_samples >= max_len)
+        s->total_samples += max_len;
+    memset(amp, 0, max_len*sizeof(int16_t));
+    return max_len;
+}
+/*- End of function --------------------------------------------------------*/
+
+void silence_gen_always(silence_gen_state_t *s)
+{
+    s->remaining_samples = INT_MAX;
+}
+/*- End of function --------------------------------------------------------*/
+
+void silence_gen_set(silence_gen_state_t *s, int silent_samples)
+{
+    s->remaining_samples = silent_samples;
+    s->total_samples = 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+void silence_gen_alter(silence_gen_state_t *s, int silent_samples)
+{
+    /* Block negative silences */
+    if (silent_samples < 0)
+    {
+        if (-silent_samples > s->remaining_samples)
+            silent_samples = -s->remaining_samples;
+    }
+    s->remaining_samples += silent_samples;
+    s->total_samples += silent_samples;
+}
+/*- End of function --------------------------------------------------------*/
+
+int silence_gen_remainder(silence_gen_state_t *s)
+{
+    return s->remaining_samples;
+}
+/*- End of function --------------------------------------------------------*/
+
+int silence_gen_generated(silence_gen_state_t *s)
+{
+    return s->total_samples;
+}
+/*- End of function --------------------------------------------------------*/
+
+void silence_gen_status_handler(silence_gen_state_t *s, modem_tx_status_func_t handler, void *user_data)
+{
+    s->status_handler = handler;
+    s->status_user_data = user_data;
+}
+/*- End of function --------------------------------------------------------*/
+
+silence_gen_state_t *silence_gen_init(silence_gen_state_t *s, int silent_samples)
+{
+    if (s == NULL)
+    {
+        if ((s = (silence_gen_state_t *) malloc(sizeof(*s))) == NULL)
+            return NULL;
+    }
+    memset(s, 0, sizeof(*s));
+    s->remaining_samples = silent_samples;
+    return s;
+}
+/*- End of function --------------------------------------------------------*/
+
+/* The following dummy routines, to absorb data, don't really have a proper home,
+   so they have been put here. */
+
+int span_dummy_rx(void *user_data, const int16_t amp[], int len)
+{
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int span_dummy_mod(void *user_data, int16_t amp[], int len)
+{
+    return len;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/spandsp.h.in
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/spandsp.h.in	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,117 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * spandsp.h - The head guy amongst the headers
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2003 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: spandsp.h.in,v 1.9 2008/08/14 14:06:05 steveu Exp $
+ */
+
+/*! \file */
+
+#if !defined(_SPANDSP_H_)
+#define _SPANDSP_H_
+
+ at SPANDSP_USE_FIXED_POINT@
+ at SPANDSP_MISALIGNED_ACCESS_FAILS@
+
+#include <stdlib.h>
+ at INSERT_INTTYPES_HEADER@
+#include <string.h>
+#include <limits.h>
+#include <time.h>
+ at INSERT_MATH_HEADER@
+#include <tiffio.h>
+
+#include <spandsp/telephony.h>
+#include <spandsp/logging.h>
+#include <spandsp/complex.h>
+#include <spandsp/bit_operations.h>
+#include <spandsp/bitstream.h>
+#include <spandsp/queue.h>
+#include <spandsp/schedule.h>
+#include <spandsp/g711.h>
+#include <spandsp/timing.h>
+#include <spandsp/vector_float.h>
+#include <spandsp/complex_vector_float.h>
+#include <spandsp/vector_int.h>
+#include <spandsp/arctan2.h>
+#include <spandsp/biquad.h>
+#include <spandsp/fir.h>
+#include <spandsp/awgn.h>
+#include <spandsp/bert.h>
+#include <spandsp/power_meter.h>
+#include <spandsp/complex_filters.h>
+#include <spandsp/dc_restore.h>
+#include <spandsp/dds.h>
+#include <spandsp/echo.h>
+#include <spandsp/modem_echo.h>
+#include <spandsp/crc.h>
+#include <spandsp/hdlc.h>
+#include <spandsp/async.h>
+#include <spandsp/noise.h>
+#include <spandsp/time_scale.h>
+#include <spandsp/tone_detect.h>
+#include <spandsp/tone_generate.h>
+#include <spandsp/super_tone_rx.h>
+#include <spandsp/super_tone_tx.h>
+#include <spandsp/dtmf.h>
+#include <spandsp/bell_r2_mf.h>
+#include <spandsp/sig_tone.h>
+#include <spandsp/fsk.h>
+#include <spandsp/modem_connect_tones.h>
+#include <spandsp/silence_gen.h>
+#include <spandsp/v29rx.h>
+#include <spandsp/v29tx.h>
+#include <spandsp/v17rx.h>
+#include <spandsp/v17tx.h>
+#include <spandsp/v22bis.h>
+#include <spandsp/v27ter_rx.h>
+#include <spandsp/v27ter_tx.h>
+#include <spandsp/v8.h>
+#include <spandsp/v42.h>
+#include <spandsp/v42bis.h>
+#include <spandsp/t4.h>
+#include <spandsp/t30.h>
+#include <spandsp/t30_api.h>
+#include <spandsp/t30_fcf.h>
+#include <spandsp/t30_logging.h>
+#include <spandsp/t35.h>
+#include <spandsp/at_interpreter.h>
+#include <spandsp/fax_modems.h>
+#include <spandsp/fax.h>
+#include <spandsp/t38_core.h>
+#include <spandsp/t38_non_ecm_buffer.h>
+#include <spandsp/t38_gateway.h>
+#include <spandsp/t38_terminal.h>
+#include <spandsp/t31.h>
+#include <spandsp/adsi.h>
+#include <spandsp/oki_adpcm.h>
+#include <spandsp/ima_adpcm.h>
+#include <spandsp/g722.h>
+#include <spandsp/g726.h>
+#include <spandsp/lpc10.h>
+#include <spandsp/gsm0610.h>
+#include <spandsp/plc.h>
+#include <spandsp/playout.h>
+
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/spandsp/adsi.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/spandsp/adsi.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,525 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * adsi.h - Analogue display services interface and other call ID related handling.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2003 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: adsi.h,v 1.31 2008/05/05 11:25:01 steveu Exp $
+ */
+
+/*! \file */
+
+#if !defined(_SPANDSP_ADSI_H_)
+#define _SPANDSP_ADSI_H_
+
+/*! \page adsi_page ADSI transmission and reception
+\section adsi_page_sec_1 What does it do?
+Although ADSI has a specific meaning in some places, the term is used here to indicate
+any form of Analogue Display Service Interface, which includes caller ID, SMS, and others.
+
+The ADSI module provides for the transmission and reception of ADSI messages
+in various formats. Currently, the supported formats are:
+
+    - Bellcore/Telcordia GR-30 CORE CLASS (Custom Local Area Signaling Services) standard
+      (North America, Australia, China, Taiwan, and Hong Kong).
+
+    - ETSI ETS 300 648, ETS 300 659-1 CLIP (Calling Line Identity Presentation) FSK standard
+      (France, Germany, Norway, Italy, Spain, South Africa, Turkey, and the UK).
+
+    - ETSI Caller-ID support for the UK, British Telecom SIN227 and SIN242.
+
+    - ETSI ETS 300 648, ETS 300 659-1 CLIP (Calling Line Identity Presentation) DTMF standard
+      variant 1 (Belgium, Brazil, Denmark, Finland, Iceland, India, Netherlands, Saudi Arabia,
+      Sweden and Uruguay).
+    
+    - ETSI ETS 300 648, ETS 300 659-1 CLIP (Calling Line Identity Presentation) DTMF standard
+      variant 2 (Denmark and Holland).
+    
+    - ETSI ETS 300 648, ETS 300 659-1 CLIP (Calling Line Identity Presentation) DTMF standard
+      variant 3.
+    
+    - ETSI ETS 300 648, ETS 300 659-1 CLIP (Calling Line Identity Presentation) DTMF standard
+      variant 4. (Taiwan and Kuwait).
+    
+    - ETSI Caller-ID support in UK (British Telecom), British Telecomm SIN227, SIN242.
+
+    - Nippon Telegraph & Telephone Corporation JCLIP (Japanese Calling Line Identity
+      Presentation) standard.
+
+    - Telecommunications Authority of Singapore ACLIP (Analog Calling Line Identity
+      Presentation) standard.
+
+    - TDD (Telecommunications Device for the Deaf).
+
+\section adsi_page_sec_2 How does it work?
+
+\section adsi_page_sec_2a The Bellcore CLASS specification
+Most FSK based CLI formats are similar to the US CLASS one, which is as follows:
+
+The alert tone for CLI during a call is at least 100ms of silence, then
+2130Hz + 2750Hz for 88ms to 110ms. When CLI is presented at ringing time,
+this tone is not sent. In the US, CLI is usually sent between the first
+two rings. This silence period is long in the US, so the message fits easily.
+In other places, where the standard ring tone has much smaller silences,
+a line voltage reversal is used to wake up a power saving receiver, then the
+message is sent, then the phone begins to ring.
+    
+The message is sent using a Bell 202 FSK modem. The data rate is 1200 bits
+per second. The message protocol uses 8-bit data words (bytes), each bounded
+by a start bit and a stop bit.
+
+Channel     Carrier     Message     Message     Data        Checksum
+Seizure     Signal      Type        Length      Word(s)     Word
+Signal                  Word        Word
+    
+\section adsi_page_sec_2a1 CHANNEL SEIZURE SIGNAL
+The channel seizure is 30 continuous bytes of 55h (01010101), including
+the start and stop bits (i.e. 300 bits of alternations in total).
+This provides a detectable alternating function to the CPE (i.e. the
+modem data pump).
+    
+\section adsi_page_sec_2a2 CARRIER SIGNAL
+The carrier signal consists of 180 bits of 1s. This may be reduced to 80
+bits of 1s for caller ID on call waiting.
+    
+\section adsi_page_sec_2a3 MESSAGE TYPE WORD
+Various message types are defined. The commonest ones for the US CLASS 
+standard are:
+
+    - Type 0x04 (SDMF) - single data message. Simple caller ID (CND)
+    - Type 0x80 (MDMF) - multiple data message. A more flexible caller ID,
+                         with extra information.
+
+Other messages support message waiting, for voice mail, and other display features. 
+
+\section adsi_page_sec_2a4 MESSAGE LENGTH WORD
+The message length word specifies the total number of data words
+to follow.
+    
+\section adsi_page_sec_2a5 DATA WORDS
+The data words contain the actual message.
+    
+\section adsi_page_sec_2a6 CHECKSUM WORD
+The Checksum Word contains the twos complement of the modulo 256
+sum of the other words in the data message (i.e., message type,
+message length, and data words).  The receiving equipment may
+calculate the modulo 256 sum of the received words and add this
+sum to the received checksum word.  A result of zero generally
+indicates that the message was correctly received.  Message
+retransmission is not supported. The sumcheck word should be followed
+by a minimum of two stop bits.
+
+\section adsi_page_sec_2b The ETSI CLIP specification
+The ETSI CLIP specification uses similar messages to the Bellcore specification.
+They are not, however, identical. First, ETSI use the V.23 modem standard, rather
+than Bell 202. Second, different fields, and different message types are available.
+
+The wake up indication generally differs from the Bellcore specification, to
+accomodate differences in European ring cadences.
+
+\section adsi_page_sec_2c The ETSI caller ID by DTMF specification
+CLI by DTMF is usually sent in a very simple way. The exchange does not give
+any prior warning (no reversal, or ring) to wake up the receiver. It just
+sends a string of DTMF digits. Around the world several variants of this
+basic scheme are used.
+
+One variant of the digit string is used in Belgium, Brazil, Denmark, Finland, Iceland,
+India, Netherlands, Saudi Arabia, Sweden and Uruguay:
+
+    - A<caller's phone number>D<redirected number>B<special information>C
+
+Each of these fields may be omitted. The following special information codes are defined
+
+    - "00" indicates the calling party number is not available.
+    - "10" indicates that the presentation of the calling party number is restricted.
+
+A second variant of the digit string is one of the following:
+
+    - A<caller's phone number>#
+    - D1#     Number not available because the caller has restricted it.
+    - D2#     Number not available because the call is international.
+    - D3#     Number not available due to technical reasons.
+
+A third variant of the digit string is used in Taiwan and Kuwait:
+
+    - D<caller's phone number>C
+
+A forth variant of the digit string is used in Denmark and Holland:
+
+    - <caller's phone number>#
+
+There is no distinctive start marker in this format.
+
+\section adsi_page_sec_2d The Japanese specification from NTT
+
+The Japanese caller ID specification is considerably different from any of the others. It
+uses V.23 modem signals, but the message structure is uniqeue. Also, the message is delivered
+while off hook. This results in a sequence
+
+    - The phone line rings
+    - CPE answers and waits for the caller ID message
+    - CPE hangs up on receipt of the caller ID message
+    - The phone line rings a second time
+    - The CPE answers a second time, connecting the called party with the caller.
+    
+Timeouts are, obviously, required to ensure this system behaves well when the caller ID message
+or the second ring are missing.
+*/
+
+enum
+{
+    ADSI_STANDARD_NONE = 0,
+    ADSI_STANDARD_CLASS = 1,
+    ADSI_STANDARD_CLIP = 2,
+    ADSI_STANDARD_ACLIP = 3,
+    ADSI_STANDARD_JCLIP = 4,
+    ADSI_STANDARD_CLIP_DTMF = 5,
+    ADSI_STANDARD_TDD = 6
+};
+
+/* In some of the messages code characters are used, as follows:
+        'C' for public callbox
+        'L' for long distance
+        'O' for overseas
+        'P' for private
+        'S' for service conflict
+
+    Taiwan and Kuwait change this pattern to:
+        'C' for coin/public callbox
+        'I' for international call
+        'O' for out of area call
+        'P' for private
+ */
+
+/*! Definitions for CLASS (Custom Local Area Signaling Services) */
+enum
+{
+    /*! Single data message caller ID */
+    CLASS_SDMF_CALLERID =               0x04,
+    /*! Multiple data message caller ID */
+    CLASS_MDMF_CALLERID =               0x80,
+    /*! Single data message message waiting */
+    CLASS_SDMF_MSG_WAITING =            0x06,
+    /*! Multiple data message message waiting */
+    CLASS_MDMF_MSG_WAITING =            0x82
+};
+
+/*! CLASS MDMF message IDs */
+enum
+{
+    /*! Date and time (MMDDHHMM) */
+    MCLASS_DATETIME =                   0x01,
+    /*! Caller number */
+    MCLASS_CALLER_NUMBER =              0x02,
+    /*! Dialed number */
+    MCLASS_DIALED_NUMBER =              0x03,
+    /*! Caller number absent: 'O' or 'P' */
+    MCLASS_ABSENCE1 =                   0x04,
+    /*! Call forward: universal ('0'), on busy ('1'), or on unanswered ('2') */
+    MCLASS_REDIRECT =                   0x05,
+    /*! Long distance: 'L' */
+    MCLASS_QUALIFIER =                  0x06,
+    /*! Caller's name */
+    MCLASS_CALLER_NAME =                0x07,
+    /*! Caller's name absent: 'O' or 'P' */
+    MCLASS_ABSENCE2 =                   0x08,
+    /*! Alternate route */
+    MCLASS_ALT_ROUTE =                  0x09
+};
+
+/*! CLASS MDMF message waiting message IDs */
+/*! Message waiting/not waiting */
+#define MCLASS_VISUAL_INDICATOR         0x0B
+
+/*! Definitions for CLIP (Calling Line Identity Presentation) */
+enum
+{
+    /*! Multiple data message caller ID */
+    CLIP_MDMF_CALLERID =                0x80,
+    /*! Multiple data message message waiting */
+    CLIP_MDMF_MSG_WAITING =             0x82,
+    /*! Multiple data message charge information */
+    CLIP_MDMF_CHARGE_INFO =             0x86,
+    /*! Multiple data message SMS */
+    CLIP_MDMF_SMS =                     0x89
+};
+
+/*! CLIP message IDs */
+enum
+{
+    /*! Date and time (MMDDHHMM) */
+    CLIP_DATETIME =                     0x01,
+    /*! Caller number */
+    CLIP_CALLER_NUMBER =                0x02,
+    /*! Dialed number */
+    CLIP_DIALED_NUMBER =                0x03,
+    /*! Caller number absent: 'O' or 'P' */
+    CLIP_ABSENCE1 =                     0x04,
+    /*! Caller's name */
+    CLIP_CALLER_NAME =                  0x07,
+    /*! Caller's name absent: 'O' or 'P' */
+    CLIP_ABSENCE2 =                     0x08,
+    /*! Visual indicator */
+    CLIP_VISUAL_INDICATOR =             0x0B,
+    /*! Message ID */
+    CLIP_MESSAGE_ID =                   0x0D,
+    /*! Voice call, ring-back-when-free call, or msg waiting call */
+    CLIP_CALLTYPE =                     0x11,
+    /*! Number of messages */
+    CLIP_NUM_MSG =                      0x13,
+    /*! Redirecting number */
+    CLIP_REDIR_NUMBER =                 0x03,
+    /*! Charge */
+    CLIP_CHARGE =                       0x20,
+    /*! Duration of the call */
+    CLIP_DURATION =                     0x23,
+    /*! Additional charge */
+    CLIP_ADD_CHARGE =                   0x21,
+    /*! Display information */
+    CLIP_DISPLAY_INFO =                 0x50,
+    /*! Service information */
+    CLIP_SERVICE_INFO =                 0x55
+};
+
+/*! Definitions for A-CLIP (Analog Calling Line Identity Presentation) */
+enum
+{
+    /*! Single data message caller ID frame */
+    ACLIP_SDMF_CALLERID =               0x04,
+    /*! Multiple data message caller ID frame */
+    ACLIP_MDMF_CALLERID =               0x80
+};
+
+/*! A-CLIP MDM message IDs */
+enum
+{
+    /*! Date and time (MMDDHHMM) */
+    ACLIP_DATETIME =                    0x01,
+    /*! Caller number */
+    ACLIP_CALLER_NUMBER =               0x02,
+    /*! Dialed number */
+    ACLIP_DIALED_NUMBER =               0x03,
+    /*! Caller number absent: 'O' or 'P' */
+    ACLIP_NUMBER_ABSENCE =              0x04,
+    /*! Call forward: universal, on busy, or on unanswered */
+    ACLIP_REDIRECT =                    0x05,
+    /*! Long distance call: 'L' */
+    ACLIP_QUALIFIER =                   0x06,
+    /*! Caller's name */
+    ACLIP_CALLER_NAME =                 0x07,
+    /*! Caller's name absent: 'O' or 'P' */
+    ACLIP_NAME_ABSENCE =                0x08
+};
+
+/*! Definitions for J-CLIP (Japan Calling Line Identity Presentation) */
+/*! Multiple data message caller ID frame */
+#define JCLIP_MDMF_CALLERID             0x40
+
+/*! J-CLIP MDM message IDs */
+enum
+{
+    /*! Caller number */
+    JCLIP_CALLER_NUMBER =               0x02,
+    /*! Caller number data extension signal */
+    JCLIP_CALLER_NUM_DES =              0x21,
+    /*! Dialed number */
+    JCLIP_DIALED_NUMBER =               0x09,
+    /*! Dialed number data extension signal */
+    JCLIP_DIALED_NUM_DES =              0x22,
+    /*! Caller number absent: 'C', 'O', 'P' or 'S' */
+    JCLIP_ABSENCE =                     0x04
+};
+
+/*! Definitions for CLIP-DTMF and its variants */
+
+#define CLIP_DTMF_HASH_TERMINATED       '#'
+#define CLIP_DTMF_C_TERMINATED          'C'
+
+/*! Caller number */
+#define CLIP_DTMF_HASH_CALLER_NUMBER    'A'
+/*! Caller number absent: private (1), overseas (2) or not available (3) */
+#define CLIP_DTMF_HASH_ABSENCE          'D'
+/*! Caller ID field with no explicit field type */
+#define CLIP_DTMF_HASH_UNSPECIFIED      0
+
+/*! Caller number */
+#define CLIP_DTMF_C_CALLER_NUMBER       'A'
+/*! Diverting number */
+#define CLIP_DTMF_C_REDIRECT_NUMBER     'D'
+/*! Caller number absent: private/restricted (00) or not available (10) */
+#define CLIP_DTMF_C_ABSENCE             'B'
+
+/*!
+    ADSI transmitter descriptor. This contains all the state information for an ADSI
+    (caller ID, CLASS, CLIP, ACLIP) transmit channel.
+ */
+typedef struct
+{
+    int standard;
+
+    tone_gen_descriptor_t alert_tone_desc;
+    tone_gen_state_t alert_tone_gen;
+    fsk_tx_state_t fsktx;
+    dtmf_tx_state_t dtmftx;
+    async_tx_state_t asynctx;
+    
+    int tx_signal_on;
+    
+    int byte_no;
+    int bit_pos;
+    int bit_no;
+    uint8_t msg[256];
+    int msg_len;
+    int preamble_len;
+    int preamble_ones_len;
+    int postamble_ones_len;
+    int stop_bits;
+    int baudot_shift;
+    
+    logging_state_t logging;
+} adsi_tx_state_t;
+
+/*!
+    ADSI receiver descriptor. This contains all the state information for an ADSI
+    (caller ID, CLASS, CLIP, ACLIP, JCLIP) receive channel.
+ */
+typedef struct
+{
+    int standard;
+    put_msg_func_t put_msg;
+    void *user_data;
+
+    fsk_rx_state_t fskrx;
+    dtmf_rx_state_t dtmfrx;
+    async_rx_state_t asyncrx;
+    
+    int consecutive_ones;
+    int bit_pos;
+    int in_progress;
+    uint8_t msg[256];
+    int msg_len;
+    int baudot_shift;
+    
+    /*! A count of the framing errors. */
+    int framing_errors;
+
+    logging_state_t logging;
+} adsi_rx_state_t;
+
+#if defined(__cplusplus)
+extern "C"
+{
+#endif
+
+/*! \brief Initialise an ADSI receive context.
+    \param s The ADSI receive context.
+    \param standard The code for the ADSI standard to be used.
+    \param put_msg A callback routine called to deliver the received messages
+           to the application.
+    \param user_data An opaque pointer for the callback routine.
+    \return A pointer to the initialised context, or NULL if there was a problem.
+*/
+adsi_rx_state_t *adsi_rx_init(adsi_rx_state_t *s, int standard, put_msg_func_t put_msg, void *user_data);
+
+/*! \brief Receive a chunk of ADSI audio.
+    \param s The ADSI receive context.
+    \param amp The audio sample buffer.
+    \param len The number of samples in the buffer.
+    \return The number of samples unprocessed.
+*/
+int adsi_rx(adsi_rx_state_t *s, const int16_t *amp, int len);
+
+/*! \brief Initialise an ADSI transmit context.
+    \param s The ADSI transmit context.
+    \param standard The code for the ADSI standard to be used.
+    \return A pointer to the initialised context, or NULL if there was a problem.
+*/
+adsi_tx_state_t *adsi_tx_init(adsi_tx_state_t *s, int standard);
+
+/*! \brief Adjust the preamble associated with an ADSI transmit context.
+    \param s The ADSI transmit context.
+    \param preamble_len The number of bits of preamble.
+    \param preamble_ones_len The number of bits of continuous one before a message.
+    \param postamble_ones_len The number of bits of continuous one after a message.
+    \param stop_bits The number of stop bits per character.
+*/
+void adsi_tx_set_preamble(adsi_tx_state_t *s,
+                          int preamble_len,
+                          int preamble_ones_len,
+                          int postamble_ones_len,
+                          int stop_bits);
+
+/*! \brief Generate a block of ADSI audio samples.
+    \param s The ADSI transmit context.
+    \param amp The audio sample buffer.
+    \param max_len The number of samples to be generated.
+    \return The number of samples actually generated.
+*/
+int adsi_tx(adsi_tx_state_t *s, int16_t *amp, int max_len);
+
+/*! \brief Request generation of an ADSI alert tone.
+    \param s The ADSI transmit context.
+*/
+void adsi_tx_send_alert_tone(adsi_tx_state_t *s);
+
+/*! \brief Put a message into the input buffer of an ADSI transmit context.
+    \param s The ADSI transmit context.
+    \param msg The message.
+    \param len The length of the message.
+    \return The length actually added. If a message is already in progress
+            in the transmitter, this function will return zero, as it will
+            not successfully add the message to the buffer.
+*/
+int adsi_tx_put_message(adsi_tx_state_t *s, const uint8_t *msg, int len);
+
+/*! \brief Get a field from an ADSI message.
+    \param s The ADSI receive context.
+    \param msg The message buffer.
+    \param msg_len The length of the message.
+    \param pos Current position within the message. Set to -1 when starting a message.
+    \param field_type The type code for the field.
+    \param field_body Pointer to the body of the field.
+    \param field_len The length of the field, or -1 for no more fields, or -2 for message structure corrupt.
+*/
+int adsi_next_field(adsi_rx_state_t *s, const uint8_t *msg, int msg_len, int pos, uint8_t *field_type, uint8_t const **field_body, int *field_len);
+
+/*! \brief Insert the header or a field into an ADSI message.
+    \param s The ADSI transmit context.
+    \param msg The message buffer.
+    \param len The current length of the message.
+    \param field_type The type code for the new field.
+    \param field_body Pointer to the body of the new field.
+    \param field_len The length of the new field.
+*/
+int adsi_add_field(adsi_tx_state_t *s, uint8_t *msg, int len, uint8_t field_type, uint8_t const *field_body, int field_len);
+
+/*! \brief Return a short name for an ADSI standard
+    \param standard The code for the standard.
+    \return A pointer to the name.
+*/
+const char *adsi_standard_to_str(int standard);
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/spandsp/arctan2.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/spandsp/arctan2.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,108 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * arctan2.h - A quick rough approximate arc tan
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2003 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: arctan2.h,v 1.13 2008/05/29 13:04:19 steveu Exp $
+ */
+
+/*! \file */
+
+#if !defined(_SPANDSP_ARCTAN2_H_)
+#define _SPANDSP_ARCTAN2_H_
+
+/*! \page arctan2_page Fast approximate four quadrant arc-tangent
+\section arctan2_page_sec_1 What does it do?
+This module provides a fast approximate 4-quadrant arc tangent function,
+based on something at dspguru.com. The worst case error is about 4.07 degrees.
+This is fine for many "where am I" type evaluations in comms. work.
+
+\section arctan2_page_sec_2 How does it work?
+???.
+*/
+
+#if defined(__cplusplus)
+extern "C"
+{
+#endif
+
+/* This returns its answer as a signed 32 bit integer phase value. */
+static __inline__ int32_t arctan2(float y, float x)
+{
+    float abs_y;
+    float angle;
+
+    if (x == 0.0f  ||  y == 0.0f)
+        return 0;
+    
+    abs_y = fabsf(y);
+
+    /* If we are in quadrant II or III, flip things around */
+    if (x < 0.0f)
+        angle = 3.0f - (x + abs_y)/(abs_y - x);
+    else
+        angle = 1.0f - (x - abs_y)/(abs_y + x);
+    angle *= 536870912.0f;
+
+    /* If we are in quadrant III or IV, negate to return an
+       answer in the range +-pi */
+    if (y < 0.0f)
+        angle = -angle;
+    return (int32_t) angle;
+}
+/*- End of function --------------------------------------------------------*/
+
+#if 0
+/* This returns its answer in radians, in the range +-pi. */
+static __inline__ float arctan2f(float y, float x)
+{
+    float angle;
+    float fx;
+    float fy;
+
+    if (x == 0.0f  ||  y == 0.0f)
+        return 0;
+    fx = fabsf(x);
+    fy = fabsf(y);
+    /* Deal with the octants */
+    /* N.B. 0.28125 == (1/4 + 1/32) */
+    if (fy > fx)
+        angle = 3.1415926f/2.0f - fx*fy/(y*y + 0.28125f*x*x);
+    else
+        angle = fy*fx/(x*x + 0.28125f*y*y);
+    
+    /* Deal with the quadrants, to bring the final answer to the range +-pi */
+    if (x < 0.0f)
+        angle = 3.1415926f - angle;
+    if (y < 0.0f)
+        angle = -angle;
+    return angle;
+}
+/*- End of function --------------------------------------------------------*/
+#endif
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/spandsp/async.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/spandsp/async.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,246 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * async.h - Asynchronous serial bit stream encoding and decoding
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2003 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: async.h,v 1.16 2008/07/17 14:27:11 steveu Exp $
+ */
+
+/*! \file */
+
+/*! \page async_page Asynchronous bit stream processing
+\section async_page_sec_1 What does it do?
+The asynchronous serial bit stream processing module provides
+generation and decoding facilities for most asynchronous data
+formats. It supports:
+ - 1 or 2 stop bits.
+ - Odd, even or no parity.
+ - 5, 6, 7, or 8 bit characters.
+ - V.14 rate adaption.
+The input to this module is a bit stream. This means any symbol synchronisation
+and decoding must occur before data is fed to this module.
+
+\section async_page_sec_2 The transmitter
+???.
+
+\section async_page_sec_3 The receiver
+???.
+*/
+
+#if !defined(_SPANDSP_ASYNC_H_)
+#define _SPANDSP_ASYNC_H_
+
+/*! Special "bit" values for the put and get bit functions */
+enum
+{
+    /*! \brief The carrier signal has dropped. */
+    PUTBIT_CARRIER_DOWN = -1,
+    /*! \brief The carrier signal is up. This merely indicates that carrier
+         energy has been seen. It is not an indication that the carrier is either
+         valid, or of the expected type. */
+    PUTBIT_CARRIER_UP = -2,
+    /*! \brief The modem is training. This is an early indication that the
+        signal seems to be of the right type. This may be needed in time critical
+        applications, like T.38, to forward an early indication of what is happening
+        on the wire. */
+    PUTBIT_TRAINING_IN_PROGRESS = -3,
+    /*! \brief The modem has trained, and is ready for data exchange. */
+    PUTBIT_TRAINING_SUCCEEDED = -4,
+    /*! \brief The modem has failed to train. */
+    PUTBIT_TRAINING_FAILED = -5,
+    /*! \brief Packet framing (e.g. HDLC framing) is OK. */
+    PUTBIT_FRAMING_OK = -6,
+    /*! \brief The data stream has ended. */
+    PUTBIT_END_OF_DATA = -7,
+    /*! \brief An abort signal (e.g. an HDLC abort) has been received. */
+    PUTBIT_ABORT = -8,
+    /*! \brief A break signal (e.g. an async break) has been received. */
+    PUTBIT_BREAK = -9,
+    /*! \brief Regular octet report for things like HDLC to the MTP standards. */
+    PUTBIT_OCTET_REPORT = -10
+};
+
+enum
+{
+    /*! \brief The data source for a transmitter is exhausted. */
+    MODEM_TX_STATUS_DATA_EXHAUSTED = -1,
+    /*! \brief The transmitter has completed its task, and shut down. */
+    MODEM_TX_STATUS_SHUTDOWN_COMPLETE = -2
+};
+
+/*! Message put function for data pumps */
+typedef void (*put_msg_func_t)(void *user_data, const uint8_t *msg, int len);
+
+/*! Message get function for data pumps */
+typedef int (*get_msg_func_t)(void *user_data, uint8_t *msg, int max_len);
+
+/*! Byte put function for data pumps */
+typedef void (*put_byte_func_t)(void *user_data, int byte);
+
+/*! Byte get function for data pumps */
+typedef int (*get_byte_func_t)(void *user_data);
+
+/*! Bit put function for data pumps */
+typedef void (*put_bit_func_t)(void *user_data, int bit);
+
+/*! Bit get function for data pumps */
+typedef int (*get_bit_func_t)(void *user_data);
+
+/*! Completion callback function for tx data pumps */
+typedef int (*modem_tx_status_func_t)(void *user_data, int status);
+
+/*! Completion callback function for rx data pumps */
+typedef int (*modem_rx_status_func_t)(void *user_data, int status);
+
+enum
+{
+    /*! No parity bit should be used */
+    ASYNC_PARITY_NONE = 0,
+    /*! An even parity bit will exist, after the data bits */
+    ASYNC_PARITY_EVEN,
+    /*! An odd parity bit will exist, after the data bits */
+    ASYNC_PARITY_ODD
+};
+
+/*!
+    Asynchronous data transmit descriptor. This defines the state of a single
+    working instance of a byte to asynchronous serial converter, for use
+    in FSK modems.
+*/
+typedef struct
+{
+    /*! \brief The number of data bits per character. */
+    int data_bits;
+    /*! \brief The type of parity. */
+    int parity;
+    /*! \brief The number of stop bits per character. */
+    int stop_bits;
+    /*! \brief A pointer to the callback routine used to get characters to be transmitted. */
+    get_byte_func_t get_byte;
+    /*! \brief An opaque pointer passed when calling get_byte. */
+    void *user_data;
+
+    /*! \brief A current, partially transmitted, character. */
+    int byte_in_progress;
+    /*! \brief The current bit position within a partially transmitted character. */
+    int bitpos;
+    /*! \brief Parity bit. */
+    int parity_bit;
+} async_tx_state_t;
+
+/*!
+    Asynchronous data receive descriptor. This defines the state of a single
+    working instance of an asynchronous serial to byte converter, for use
+    in FSK modems.
+*/
+typedef struct
+{
+    /*! \brief The number of data bits per character. */
+    int data_bits;
+    /*! \brief The type of parity. */
+    int parity;
+    /*! \brief The number of stop bits per character. */
+    int stop_bits;
+    /*! \brief TRUE if V.14 rate adaption processing should be performed. */
+    int use_v14;
+    /*! \brief A pointer to the callback routine used to handle received characters. */
+    put_byte_func_t put_byte;
+    /*! \brief An opaque pointer passed when calling put_byte. */
+    void *user_data;
+
+    /*! \brief A current, partially complete, character. */
+    int byte_in_progress;
+    /*! \brief The current bit position within a partially complete character. */
+    int bitpos;
+    /*! \brief Parity bit. */
+    int parity_bit;
+
+    /*! A count of the number of parity errors seen. */
+    int parity_errors;
+    /*! A count of the number of character framing errors seen. */
+    int framing_errors;
+} async_rx_state_t;
+
+#if defined(__cplusplus)
+extern "C"
+{
+#endif
+
+/*! Initialise an asynchronous data transmit context.
+    \brief Initialise an asynchronous data transmit context.
+    \param s The transmitter context.
+    \param data_bits The number of data bit.
+    \param parity_bits The type of parity.
+    \param stop_bits The number of stop bits.
+    \param use_v14 TRUE if V.14 rate adaption processing should be used.
+    \param get_byte The callback routine used to get the data to be transmitted.
+    \param user_data An opaque pointer.
+    \return A pointer to the initialised context, or NULL if there was a problem. */
+async_tx_state_t *async_tx_init(async_tx_state_t *s,
+                                int data_bits,
+                                int parity_bits,
+                                int stop_bits,
+                                int use_v14,
+                                get_byte_func_t get_byte,
+                                void *user_data);
+
+/*! Get the next bit of a transmitted serial bit stream.
+    \brief Get the next bit of a transmitted serial bit stream.
+    \param user_data An opaque point which must point to a transmitter context.
+    \return the next bit, or PUTBIT_END_OF_DATA to indicate the data stream has ended. */
+int async_tx_get_bit(void *user_data);
+
+/*! Initialise an asynchronous data receiver context.
+    \brief Initialise an asynchronous data receiver context.
+    \param s The receiver context.
+    \param data_bits The number of data bits.
+    \param parity_bits The type of parity.
+    \param stop_bits The number of stop bits.
+    \param use_v14 TRUE if V.14 rate adaption processing should be used.
+    \param put_byte The callback routine used to put the received data.
+    \param user_data An opaque pointer.
+    \return A pointer to the initialised context, or NULL if there was a problem. */
+async_rx_state_t *async_rx_init(async_rx_state_t *s,
+                                int data_bits,
+                                int parity_bits,
+                                int stop_bits,
+                                int use_v14,
+                                put_byte_func_t put_byte,
+                                void *user_data);
+
+/*! Accept a bit from a received serial bit stream
+    \brief Accept a bit from a received serial bit stream
+    \param user_data An opaque point which must point to a receiver context.
+    \param bit The new bit. Some special values are supported for this field.
+        - PUTBIT_CARRIER_UP
+        - PUTBIT_CARRIER_DOWN
+        - PUTBIT_TRAINING_SUCCEEDED
+        - PUTBIT_TRAINING_FAILED
+        - PUTBIT_END_OF_DATA */
+void async_rx_put_bit(void *user_data, int bit);
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/spandsp/at_interpreter.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/spandsp/at_interpreter.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,286 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * at_interpreter.h - AT command interpreter to V.251, V.252, V.253, T.31 and the 3GPP specs.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2004, 2005, 2006 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: at_interpreter.h,v 1.18 2008/06/11 17:52:30 steveu Exp $
+ */
+
+/*! \file */
+
+#if !defined(_SPANDSP_AT_INTERPRETER_H_)
+#define _SPANDSP_AT_INTERPRETER_H_
+
+/*! \page at_page AT command interpreter
+\section at_page_sec_1 What does it do?
+The AT interpreter module implements V.251, V.252, V.253, T.31 and various 3GPP
+modem control commands.
+
+\section at_page_sec_2 How does it work?
+*/
+
+typedef struct at_state_s at_state_t;
+
+typedef int (at_modem_control_handler_t)(at_state_t *s, void *user_data, int op, const char *num);
+typedef int (at_tx_handler_t)(at_state_t *s, void *user_data, const uint8_t *buf, size_t len);
+typedef int (at_class1_handler_t)(at_state_t *s, void *user_data, int direction, int operation, int val);
+
+enum at_rx_mode_e
+{
+    AT_MODE_ONHOOK_COMMAND,
+    AT_MODE_OFFHOOK_COMMAND,
+    AT_MODE_CONNECTED,
+    AT_MODE_DELIVERY,
+    AT_MODE_HDLC,
+    AT_MODE_STUFFED
+};
+
+enum at_call_event_e
+{
+    AT_CALL_EVENT_ALERTING = 1,
+    AT_CALL_EVENT_CONNECTED,
+    AT_CALL_EVENT_ANSWERED,
+    AT_CALL_EVENT_BUSY,
+    AT_CALL_EVENT_NO_DIALTONE,
+    AT_CALL_EVENT_NO_ANSWER,
+    AT_CALL_EVENT_HANGUP
+};
+
+enum at_modem_control_operation_e
+{
+    /*! Start an outgoing call. */
+    AT_MODEM_CONTROL_CALL,
+    /*! Answer an incoming call. */
+    AT_MODEM_CONTROL_ANSWER,
+    /*! Hangup a call. */
+    AT_MODEM_CONTROL_HANGUP,
+    /*! Take the line off hook. */
+    AT_MODEM_CONTROL_OFFHOOK,
+    /*! Put the line on hook. */
+    AT_MODEM_CONTROL_ONHOOK,
+    /*! Control V.24 Circuit 108, "data terminal ready". */
+    AT_MODEM_CONTROL_DTR,
+    /*! Control V.24 Circuit 105, "request to send". */
+    AT_MODEM_CONTROL_RTS,
+    /*! Control V.24 Circuit 106, "clear to send". */
+    AT_MODEM_CONTROL_CTS,
+    /*! Control V.24 Circuit 109, "receive line signal detector" (i.e. carrier detect). */
+    AT_MODEM_CONTROL_CAR,
+    /*! Control V.24 Circuit 125, "ring indicator". */
+    AT_MODEM_CONTROL_RNG,
+    /*! Control V.24 Circuit 107, "data set ready". */
+    AT_MODEM_CONTROL_DSR,
+    /*! Set the caller ID for outgoing calls. */
+    AT_MODEM_CONTROL_SETID,
+    /* The remainder of the control functions should not get past the modem, to the
+       application. */
+    AT_MODEM_CONTROL_RESTART,
+    AT_MODEM_CONTROL_DTE_TIMEOUT
+};
+
+enum
+{
+    AT_RESPONSE_CODE_OK = 0,
+    AT_RESPONSE_CODE_CONNECT,
+    AT_RESPONSE_CODE_RING,
+    AT_RESPONSE_CODE_NO_CARRIER,
+    AT_RESPONSE_CODE_ERROR,
+    AT_RESPONSE_CODE_XXX,
+    AT_RESPONSE_CODE_NO_DIALTONE,
+    AT_RESPONSE_CODE_BUSY,
+    AT_RESPONSE_CODE_NO_ANSWER,
+    AT_RESPONSE_CODE_FCERROR,
+    AT_RESPONSE_CODE_FRH3
+};
+
+struct at_call_id
+{
+    char *id;
+    char *value;
+    struct at_call_id *next;
+};
+
+/*!
+    AT profile.
+*/
+typedef struct
+{
+    /*! TRUE if character echo is enabled */
+    int echo;
+    /*! TRUE if verbose reporting is enabled */
+    int verbose;
+    /*! TRUE if result codes are verbose */
+    int result_code_format;
+    /*! TRUE if pulse dialling is the default */
+    int pulse_dial;
+    /*! ??? */
+    int double_escape;
+    /*! ??? */
+    int adaptive_receive;
+    /*! The state of all possible S registers */
+    uint8_t s_regs[100];
+} at_profile_t;
+
+/*!
+    AT descriptor. This defines the working state for a single instance of
+    the AT interpreter.
+*/
+struct at_state_s
+{
+    at_profile_t p;
+    /*! Value set by +GCI */
+    int country_of_installation;
+    /*! Value set by +FIT */
+    int dte_inactivity_timeout;
+    /*! Value set by +FIT */
+    int dte_inactivity_action;
+    /*! Value set by L */
+    int speaker_volume;
+    /*! Value set by M */
+    int speaker_mode;
+    /*! This is no real DTE rate. This variable is for compatibility this serially
+        connected modems. */
+    /*! Value set by +IPR/+FPR */
+    int dte_rate;
+    /*! Value set by +ICF */
+    int dte_char_format;
+    /*! Value set by +ICF */
+    int dte_parity;
+    /*! Value set by &C */
+    int rlsd_behaviour;
+    /*! Value set by &D */
+    int dtr_behaviour;
+    /*! Value set by +FCL */
+    int carrier_loss_timeout;
+    /*! Value set by X */
+    int result_code_mode;
+    /*! Value set by +IDSR */
+    int dsr_option;
+    /*! Value set by +ILSD */
+    int long_space_disconnect_option;
+    /*! Value set by +ICLOK */
+    int sync_tx_clock_source;
+    /*! Value set by +EWIND */
+    int rx_window;
+    /*! Value set by +EWIND */
+    int tx_window;
+    
+    int v8bis_signal;
+    int v8bis_1st_message;
+    int v8bis_2nd_message;
+    int v8bis_sig_en;
+    int v8bis_msg_en;
+    int v8bis_supp_delay;
+
+    uint8_t rx_data[256];
+    int rx_data_bytes;
+
+    int display_call_info;
+    int call_info_displayed;
+    struct at_call_id *call_id;
+    char *local_id;
+    /*! The currently select FAX modem class. 0 = data modem mode. */
+    int fclass_mode;
+    int at_rx_mode;
+    int rings_indicated;
+    int do_hangup;
+    int silent_dial;
+    int ok_is_pending;
+    int dte_is_waiting;
+    /*! \brief TRUE if a carrier is presnt. Otherwise FALSE. */
+    int rx_signal_present;
+    /*! \brief TRUE if a modem has trained, Otherwise FALSE. */
+    int rx_trained;
+    int transmit;
+
+    char line[256];
+    int line_ptr;
+
+    at_modem_control_handler_t *modem_control_handler;
+    void *modem_control_user_data;
+    at_tx_handler_t *at_tx_handler;
+    void *at_tx_user_data;
+    at_class1_handler_t *class1_handler;
+    void *class1_user_data;
+
+    /*! \brief Error and flow logging control */
+    logging_state_t logging;
+};
+
+#if defined(__cplusplus)
+extern "C"
+{
+#endif
+
+void at_set_at_rx_mode(at_state_t *s, int new_mode);
+
+void at_put_response(at_state_t *s, const char *t);
+
+void at_put_numeric_response(at_state_t *s, int val);
+
+void at_put_response_code(at_state_t *s, int code);
+
+void at_reset_call_info(at_state_t *s);
+
+/*! Set the call information for an AT interpreter.
+    \brief Set the call information for an AT interpreter.
+    \param s The AT interpreter context.
+    \param id .
+    \param value . */
+void at_set_call_info(at_state_t *s, char const *id, char const *value);
+
+void at_display_call_info(at_state_t *s);
+
+int at_modem_control(at_state_t *s, int op, const char *num);
+
+void at_call_event(at_state_t *s, int event);
+
+void at_interpreter(at_state_t *s, const char *cmd, int len);
+
+void at_set_class1_handler(at_state_t *s, at_class1_handler_t handler, void *user_data);
+
+/*! Initialise an AT interpreter context.
+    \brief Initialise an AT interpreter context.
+    \param s The AT context.
+    \param at_tx_handler x.
+    \param at_tx_user_data x.
+    \param modem_control_handler x.
+    \param modem_control_user_data x.
+    \return A pointer to the AT context, or NULL if there was a problem. */
+at_state_t *at_init(at_state_t *s,
+                    at_tx_handler_t *at_tx_handler,
+                    void *at_tx_user_data,
+                    at_modem_control_handler_t *modem_control_handler,
+                    void *modem_control_user_data);
+
+/*! Free an AT interpreter context.
+    \brief Free an AT interpreter context.
+    \param s The AT context.
+    \return 0 for OK */
+int at_free(at_state_t *s);
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/spandsp/awgn.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/spandsp/awgn.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,101 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * awgn.h - An additive Gaussian white noise generator
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2001 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: awgn.h,v 1.14 2008/04/17 14:26:59 steveu Exp $
+ */
+
+/*! \file */
+
+/* This code is based on some demonstration code in a research
+   paper somewhere. I can't track down where I got the original from,
+   so that due recognition can be given. The original had no explicit
+   copyright notice, and I hope nobody objects to its use here.
+   
+   Having a reasonable Gaussian noise generator is pretty important for
+   telephony testing (in fact, pretty much any DSP testing), and this
+   one seems to have served me OK. Since the generation of Gaussian
+   noise is only for test purposes, and not a core system component,
+   I don't intend to worry excessively about copyright issues, unless
+   someone worries me.
+        
+   The non-core nature of this code also explains why it is unlikely
+   to ever be optimised. */
+
+#if !defined(_SPANDSP_AWGN_H_)
+#define _SPANDSP_AWGN_H_
+
+/*! \page awgn_page Additive white gaussian noise (AWGN) generation
+
+\section awgn_page_sec_1 What does it do?
+Adding noise is not the most useful thing in most DSP applications, but it is
+awfully useful for test suites. 
+
+\section awgn_page_sec_2 How does it work?
+
+This code is based on some demonstration code in a research paper somewhere. I
+can't track down where I got the original from, so that due recognition can be
+given. The original had no explicit copyright notice, and I hope nobody objects
+to its use here. 
+
+Having a reasonable Gaussian noise generator is pretty important for telephony
+testing (in fact, pretty much any DSP testing), and this one seems to have
+served me OK. Since the generation of Gaussian noise is only for test purposes,
+and not a core system component, I don't intend to worry excessively about
+copyright issues, unless someone worries me. 
+
+The non-core nature of this code also explains why it is unlikely to ever be
+optimised.
+*/
+
+/*!
+    AWGN generator descriptor. This contains all the state information for an AWGN generator.
+ */
+typedef struct
+{
+    double rms;
+    long int ix1;
+    long int ix2;
+    long int ix3;
+    double r[98];
+    double gset;
+    int iset;
+} awgn_state_t;
+
+#if defined(__cplusplus)
+extern "C"
+{
+#endif
+
+awgn_state_t *awgn_init_dbm0(awgn_state_t *s, int idum, float level);
+
+awgn_state_t *awgn_init_dbov(awgn_state_t *s, int idum, float level);
+
+int16_t awgn(awgn_state_t *s);
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/spandsp/bell_r2_mf.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/spandsp/bell_r2_mf.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,306 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * bell_r2_mf.h - Bell MF and MFC/R2 tone generation and detection.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2001 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: bell_r2_mf.h,v 1.19 2008/05/30 13:51:28 steveu Exp $
+ */
+
+/*! \file */
+
+#if !defined(_SPANDSP_BELL_R2_MF_H_)
+#define _SPANDSP_BELL_R2_MF_H_
+
+/*! \page mfc_r2_tone_generation_page MFC/R2 tone generation
+\section mfc_r2_tone_generation_page_sec_1 What does it do?
+The MFC/R2 tone generation module provides for the generation of the
+repertoire of 15 dual tones needs for the digital MFC/R2 signalling protocol. 
+
+\section mfc_r2_tone_generation_page_sec_2 How does it work?
+*/
+
+/*! \page bell_mf_tone_generation_page Bell MF tone generation
+\section bell_mf_tone_generation_page_sec_1 What does it do?
+The Bell MF tone generation module provides for the generation of the
+repertoire of 15 dual tones needs for various Bell MF signalling protocols. 
+
+\section bell_mf_tone_generation_page_sec_2 How does it work?
+Basic Bell MF tone generation specs:
+    - Tone on time = KP: 100+-7ms. All other signals: 68+-7ms
+    - Tone off time (between digits) = 68+-7ms
+    - Frequency tolerance +- 1.5%
+    - Signal level -7+-1dBm per frequency
+*/
+
+/*! \page mfc_r2_tone_rx_page MFC/R2 tone receiver
+
+\section mfc_r2_tone_rx_page_sec_1 What does it do?
+The MFC/R2 tone receiver module provides for the detection of the
+repertoire of 15 dual tones needs for the digital MFC/R2 signalling protocol. 
+It is compliant with ITU-T Q.441D.
+
+\section mfc_r2_tone_rx_page_sec_2 How does it work?
+Basic MFC/R2 tone detection specs:
+    - Receiver response range: -5dBm to -35dBm
+    - Difference in level for a pair of frequencies
+        - Adjacent tones: <5dB
+        - Non-adjacent tones: <7dB
+    - Receiver not to detect a signal of 2 frequencies of level -5dB and
+      duration <7ms.
+    - Receiver not to recognise a signal of 2 frequencies having a difference
+      in level >=20dB.
+    - Max received signal frequency error: +-10Hz
+    - The sum of the operate and release times of a 2 frequency signal not to
+      exceed 80ms (there are no individual specs for the operate and release
+      times).
+    - Receiver not to release for signal interruptions <=7ms.
+    - System malfunction due to signal interruptions >7ms (typically 20ms) is
+      prevented by further logic elements.
+*/
+
+/*! \page bell_mf_tone_rx_page Bell MF tone receiver
+
+\section bell_mf_tone_rx_page_sec_1 What does it do?
+The Bell MF tone receiver module provides for the detection of the
+repertoire of 15 dual tones needs for various Bell MF signalling protocols. 
+It is compliant with ITU-T Q.320, ITU-T Q.322, ITU-T Q.323B.
+
+\section bell_mf_tone_rx_page_sec_2 How does it work?
+Basic Bell MF tone detection specs:
+    - Frequency tolerance +- 1.5% +-10Hz
+    - Signal level -14dBm to 0dBm
+    - Perform a "two and only two tones present" test.
+    - Twist <= 6dB accepted
+    - Receiver sensitive to signals above -22dBm per frequency
+    - Test for a minimum of 55ms if KP, or 30ms of other signals.
+    - Signals to be recognised if the two tones arrive within 8ms of each other.
+    - Invalid signals result in the return of the re-order tone.
+
+Note: Above -3dBm the signal starts to clip. We can detect with a little clipping,
+      but not up to 0dBm, which the above spec seems to require. There isn't a lot
+      we can do about that. Is the spec. incorrectly worded about the dBm0 reference
+      point, or have I misunderstood it?
+*/
+
+/*! The maximum number of Bell MF digits we can buffer. */
+#define MAX_BELL_MF_DIGITS 128
+
+/*!
+    Bell MF generator state descriptor. This defines the state of a single
+    working instance of a Bell MF generator.
+*/
+typedef struct
+{
+    /*! The tone generator. */
+    tone_gen_state_t tones;
+    int current_sample;
+    union
+    {
+        queue_state_t queue;
+        uint8_t buf[QUEUE_STATE_T_SIZE(MAX_BELL_MF_DIGITS)];
+    } queue;
+} bell_mf_tx_state_t;
+
+/*!
+    Bell MF digit detector descriptor.
+*/
+typedef struct
+{
+    /*! Optional callback funcion to deliver received digits. */
+    digits_rx_callback_t digits_callback;
+    /*! An opaque pointer passed to the callback function. */
+    void *digits_callback_data;
+    /*! Tone detector working states */
+    goertzel_state_t out[6];
+    /*! Short term history of results from the tone detection, using in persistence checking */
+    uint8_t hits[5];
+    /*! The current sample number within a processing block. */
+    int current_sample;
+
+    /*! The number of digits which have been lost due to buffer overflows. */
+    int lost_digits;
+    /*! The number of digits currently in the digit buffer. */
+    int current_digits;
+    /*! The received digits buffer. This is a NULL terminated string. */
+    char digits[MAX_BELL_MF_DIGITS + 1];
+} bell_mf_rx_state_t;
+
+/*!
+    MFC/R2 tone detector descriptor.
+*/
+typedef struct
+{
+    /*! The tone generator. */
+    tone_gen_state_t tone;
+    /*! TRUE if generating forward tones, otherwise generating reverse tones. */
+    int fwd;
+    /*! The current digit being generated. */
+    int digit;
+} r2_mf_tx_state_t;
+
+/*!
+    MFC/R2 tone detector descriptor.
+*/
+typedef struct
+{
+    /*! Optional callback funcion to deliver received digits. */
+    tone_report_func_t callback;
+    /*! An opaque pointer passed to the callback function. */
+    void *callback_data;
+    /*! TRUE is we are detecting forward tones. FALSE if we are detecting backward tones */
+    int fwd;
+    /*! Tone detector working states */
+    goertzel_state_t out[6];
+    /*! The current sample number within a processing block. */
+    int current_sample;
+    /*! The currently detected digit. */
+    int current_digit;
+} r2_mf_rx_state_t;
+
+#if defined(__cplusplus)
+extern "C"
+{
+#endif
+
+/*! \brief Generate a buffer of Bell MF tones.
+    \param s The Bell MF generator context.
+    \param amp The buffer for the generated signal.
+    \param max_samples The required number of generated samples.
+    \return The number of samples actually generated. This may be less than 
+            max_samples if the input buffer empties. */
+int bell_mf_tx(bell_mf_tx_state_t *s, int16_t amp[], int max_samples);
+
+/*! \brief Put a string of digits in a Bell MF generator's input buffer.
+    \param s The Bell MF generator context.
+    \param digits The string of digits to be added.
+    \param len The length of the string of digits. If negative, the string is
+           assumed to be a NULL terminated string.
+    \return The number of digits actually added. This may be less than the
+            length of the digit string, if the buffer fills up. */
+size_t bell_mf_tx_put(bell_mf_tx_state_t *s, const char *digits, int len);
+
+/*! \brief Initialise a Bell MF generator context.
+    \param s The Bell MF generator context.
+    \return A pointer to the Bell MF generator context.*/
+bell_mf_tx_state_t *bell_mf_tx_init(bell_mf_tx_state_t *s);
+
+/*! \brief Free a Bell MF generator context.
+    \param s The Bell MF generator context.
+    \return 0 for OK, else -1. */
+int bell_mf_tx_free(bell_mf_tx_state_t *s);
+
+/*! \brief Generate a block of R2 MF tones.
+    \param s The R2 MF generator context.
+    \param amp The buffer for the generated signal.
+    \param samples The required number of generated samples.
+    \return The number of samples actually generated. */
+int r2_mf_tx(r2_mf_tx_state_t *s, int16_t amp[], int samples);
+
+/*! \brief Generate a block of R2 MF tones.
+    \param s The R2 MF generator context.
+    \param digit The digit to be generated.
+    \return 0 for OK, or -1 for a bad request. */
+int r2_mf_tx_put(r2_mf_tx_state_t *s, char digit);
+
+/*! \brief Initialise an R2 MF tone generator context.
+    \param s The R2 MF generator context.
+    \param fwd TRUE if the context is for forward signals. FALSE if the
+           context is for backward signals.
+    \return A pointer to the MFC/R2 generator context.*/
+r2_mf_tx_state_t *r2_mf_tx_init(r2_mf_tx_state_t *s, int fwd);
+
+/*! \brief Free an R2 MF tone generator context.
+    \param s The R2 MF tone generator context.
+    \return 0 for OK, else -1. */
+int r2_mf_tx_free(r2_mf_tx_state_t *s);
+
+/*! Process a block of received Bell MF audio samples.
+    \brief Process a block of received Bell MF audio samples.
+    \param s The Bell MF receiver context.
+    \param amp The audio sample buffer.
+    \param samples The number of samples in the buffer.
+    \return The number of samples unprocessed. */
+int bell_mf_rx(bell_mf_rx_state_t *s, const int16_t amp[], int samples);
+
+/*! \brief Get a string of digits from a Bell MF receiver's output buffer.
+    \param s The Bell MF receiver context.
+    \param buf The buffer for the received digits.
+    \param max The maximum  number of digits to be returned,
+    \return The number of digits actually returned. */
+size_t bell_mf_rx_get(bell_mf_rx_state_t *s, char *buf, int max);
+
+/*! \brief Initialise a Bell MF receiver context.
+    \param s The Bell MF receiver context.
+    \param callback An optional callback routine, used to report received digits. If
+           no callback routine is set, digits may be collected, using the bell_mf_rx_get()
+           function.
+    \param user_data An opaque pointer which is associated with the context,
+           and supplied in callbacks.
+    \return A pointer to the Bell MF receiver context.*/
+bell_mf_rx_state_t *bell_mf_rx_init(bell_mf_rx_state_t *s,
+                                    digits_rx_callback_t callback,
+                                    void *user_data);
+
+/*! \brief Free a Bell MF receiver context.
+    \param s The Bell MF receiver context.
+    \return 0 for OK, else -1. */
+int bell_mf_rx_free(bell_mf_rx_state_t *s);
+
+/*! Process a block of received R2 MF audio samples.
+    \brief Process a block of received R2 MF audio samples.
+    \param s The R2 MF receiver context.
+    \param amp The audio sample buffer.
+    \param samples The number of samples in the buffer.
+    \return The number of samples unprocessed. */
+int r2_mf_rx(r2_mf_rx_state_t *s, const int16_t amp[], int samples);
+
+/*! \brief Get the current digit from an R2 MF receiver.
+    \param s The R2 MF receiver context.
+    \return The number digits being received. */
+int r2_mf_rx_get(r2_mf_rx_state_t *s);
+
+/*! \brief Initialise an R2 MF receiver context.
+    \param s The R2 MF receiver context.
+    \param fwd TRUE if the context is for forward signals. FALSE if the
+           context is for backward signals.
+    \param callback An optional callback routine, used to report received digits. If
+           no callback routine is set, digits may be collected, using the r2_mf_rx_get()
+           function.
+    \param user_data An opaque pointer which is associated with the context,
+           and supplied in callbacks.
+    \return A pointer to the R2 MF receiver context. */
+r2_mf_rx_state_t *r2_mf_rx_init(r2_mf_rx_state_t *s,
+                                int fwd,
+                                tone_report_func_t callback,
+                                void *user_data);
+
+/*! \brief Free an R2 MF receiver context.
+    \param s The R2 MF receiver context.
+    \return 0 for OK, else -1. */
+int r2_mf_rx_free(r2_mf_rx_state_t *s);
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/spandsp/bert.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/spandsp/bert.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,205 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * bert.h - Bit error rate tests.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2004 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: bert.h,v 1.19 2008/04/17 14:26:59 steveu Exp $
+ */
+
+#if !defined(_SPANDSP_BERT_H_)
+#define _SPANDSP_BERT_H_
+
+/*! \page bert_page The Bit Error Rate tester
+\section bert_page_sec_1 What does it do?
+The Bit Error Rate tester generates a pseudo random bit stream. It also accepts such
+a pattern, synchronises to it, and checks the bit error rate in this stream.
+
+\section bert_page_sec_2 How does it work?
+The Bit Error Rate tester generates a bit stream, with a repeating 2047 bit pseudo
+random pattern, using an 11 stage polynomial generator. It also accepts such a pattern,
+synchronises to it, and checks the bit error rate in this stream. If the error rate is
+excessive the tester assumes synchronisation has been lost, and it attempts to
+resynchronise with the stream.
+
+The bit error rate is continuously assessed against decadic ranges -
+    > 1 in 10^2
+    > 1 in 10^3
+    > 1 in 10^4
+    > 1 in 10^5
+    > 1 in 10^6
+    > 1 in 10^7
+    < 1 in 10^7
+To ensure fairly smooth results from this assessment, each decadic level is assessed
+over 10/error rate bits. That is, to assess if the signal's BER is above or below 1 in 10^5
+the software looks over 10*10^5 => 10^6 bits.
+*/
+
+enum
+{
+    BERT_REPORT_SYNCED = 0,
+    BERT_REPORT_UNSYNCED,
+    BERT_REPORT_REGULAR,
+    BERT_REPORT_GT_10_2,
+    BERT_REPORT_LT_10_2,
+    BERT_REPORT_LT_10_3,
+    BERT_REPORT_LT_10_4,
+    BERT_REPORT_LT_10_5,
+    BERT_REPORT_LT_10_6,
+    BERT_REPORT_LT_10_7
+};
+
+/* The QBF strings should be:
+    "VoyeZ Le BricK GeanT QuE J'ExaminE PreS Du WharF 123 456 7890 + - * : = $ % ( )"
+    "ThE QuicK BrowN FoX JumpS OveR ThE LazY DoG 123 456 7890 + - * : = $ % ( )"
+*/
+
+enum
+{
+    BERT_PATTERN_ZEROS = 0,
+    BERT_PATTERN_ONES,
+    BERT_PATTERN_7_TO_1,
+    BERT_PATTERN_3_TO_1,
+    BERT_PATTERN_1_TO_1,
+    BERT_PATTERN_1_TO_3,
+    BERT_PATTERN_1_TO_7,
+    BERT_PATTERN_QBF,
+    BERT_PATTERN_ITU_O151_23,
+    BERT_PATTERN_ITU_O151_20,
+    BERT_PATTERN_ITU_O151_15,
+    BERT_PATTERN_ITU_O152_11,
+    BERT_PATTERN_ITU_O153_9
+};
+
+/*!
+    Bit error rate tester (BERT) results descriptor. This is used to report the
+    results of a BER test.
+*/
+typedef struct
+{
+    int total_bits;
+    int bad_bits;
+    int resyncs;
+} bert_results_t;
+
+typedef void (*bert_report_func_t)(void *user_data, int reason, bert_results_t *bert_results);
+
+/*!
+    Bit error rate tester (BERT) descriptor. This defines the working state for a
+    single instance of the BERT.
+*/
+typedef struct
+{
+    int pattern;
+    int pattern_class;
+    bert_report_func_t reporter;
+    void *user_data;
+    int report_frequency;
+    int limit;
+
+    uint32_t tx_reg;
+    int tx_step;
+    int tx_step_bit;
+    int tx_bits;
+    int tx_zeros;
+
+    uint32_t rx_reg;
+    uint32_t ref_reg;
+    uint32_t master_reg;
+    int rx_step;
+    int rx_step_bit;
+    int resync;
+    int rx_bits;
+    int rx_zeros;
+    int resync_len;
+    int resync_percent;
+    int resync_bad_bits;
+    int resync_cnt;
+    
+    uint32_t mask;
+    int shift;
+    int shift2;
+    int max_zeros;
+    int invert;
+    int resync_time;
+
+    int decade_ptr[9];
+    int decade_bad[9][10];
+    int step;
+    int error_rate;
+
+    int bit_error_status;
+    int report_countdown;
+
+    bert_results_t results;
+
+    /*! \brief Error and flow logging control */
+    logging_state_t logging;
+} bert_state_t;
+
+#if defined(__cplusplus)
+extern "C"
+{
+#endif
+
+/*! Return a short description of a BERT event.
+    \param event The event type.
+    \return A pointer to a short text string describing the event. */
+const char *bert_event_to_str(int event);
+
+/*! Initialise a BERT context.
+    \param s The BERT context.
+    \param limit The maximum test duration.
+    \param pattern One of the supported BERT signal patterns.
+    \param resync_len ???
+    \param resync_percent The percentage of bad bits which will cause a resync.
+    \return The BERT context. */
+bert_state_t *bert_init(bert_state_t *s, int limit, int pattern, int resync_len, int resync_percent);
+
+/*! Get the next bit of the BERT sequence from the generator.
+    \param s The BERT context.
+    \return The bit. */
+int bert_get_bit(bert_state_t *s);
+
+/*! Put the next bit of the BERT sequence to the analyser.
+    \param s The BERT context.
+    \param bit The bit. */
+void bert_put_bit(bert_state_t *s, int bit);
+
+/*! Set the callback function for reporting the test status.
+    \param s The BERT context.
+    \param freq The required frequency of regular reports.
+    \param reporter The callback function.
+    \param user_data An opaque pointer passed to the reporter routine. */
+void bert_set_report(bert_state_t *s, int freq, bert_report_func_t reporter, void *user_data);
+
+/*! Get the results of the BERT.
+    \param s The BERT context.
+    \param results The results.
+    \return The size of the result structure. */
+int bert_result(bert_state_t *s, bert_results_t *results);
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/spandsp/biquad.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/spandsp/biquad.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,117 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * biquad.h - General telephony bi-quad section routines (currently this just
+ *            handles canonic/type 2 form)
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2001 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: biquad.h,v 1.14 2008/04/17 14:26:59 steveu Exp $
+ */
+
+/*! \page biquad_page Bi-quadratic filter sections
+\section biquad_page_sec_1 What does it do?
+???.
+
+\section biquad_page_sec_2 How does it work?
+???.
+*/
+
+#if !defined(_SPANDSP_BIQUAD_H_)
+#define _SPANDSP_BIQUAD_H_
+
+typedef struct
+{
+    int32_t gain;
+    int32_t a1;
+    int32_t a2;
+    int32_t b1;
+    int32_t b2;
+
+    int32_t z1;
+    int32_t z2;
+
+#if FIRST_ORDER_NOISE_SHAPING
+    int32_t residue;
+#elif SECOND_ORDER_NOISE_SHAPING
+    int32_t residue1;
+    int32_t residue2;
+#endif
+} biquad2_state_t;
+
+#if defined(__cplusplus)
+extern "C"
+{
+#endif
+
+static __inline__ void biquad2_init(biquad2_state_t *bq,
+                                    int32_t gain,
+                                    int32_t a1,
+                                    int32_t a2,
+                                    int32_t b1,
+                                    int32_t b2)
+{
+    bq->gain = gain;
+    bq->a1 = a1;
+    bq->a2 = a2;
+    bq->b1 = b1;
+    bq->b2 = b2;
+    
+    bq->z1 = 0;
+    bq->z2 = 0;    
+
+#if FIRST_ORDER_NOISE_SHAPING
+    bq->residue = 0;
+#elif SECOND_ORDER_NOISE_SHAPING
+    bq->residue1 = 0;
+    bq->residue2 = 0;
+#endif
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ int16_t biquad2(biquad2_state_t *bq, int16_t sample)
+{
+    int32_t y;
+    int32_t z0;
+    
+    z0 = sample*bq->gain + bq->z1*bq->a1 + bq->z2*bq->a2;
+    y = z0 + bq->z1*bq->b1 + bq->z2*bq->b2;
+
+    bq->z2 = bq->z1;
+    bq->z1 = z0 >> 15;
+#if FIRST_ORDER_NOISE_SHAPING
+    y += bq->residue; 
+    bq->residue = y & 0x7FFF;
+#elif SECOND_ORDER_NOISE_SHAPING
+    y += (2*bq->residue1 - bq->residue2);
+    bq->residue2 = bq->residue1;
+    bq->residue1 = y & 0x7FFF;
+#endif
+    y >>= 15;
+    return (int16_t) y;
+}
+/*- End of function --------------------------------------------------------*/
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/spandsp/bit_operations.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/spandsp/bit_operations.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,262 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * bit_operations.h - Various bit level operations, such as bit reversal
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2006 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: bit_operations.h,v 1.22 2008/07/10 13:43:40 steveu Exp $
+ */
+
+/*! \file */
+
+#if !defined(_SPANDSP_BIT_OPERATIONS_H_)
+#define _SPANDSP_BIT_OPERATIONS_H_
+
+#if defined(__cplusplus)
+extern "C"
+{
+#endif
+
+/*! \brief Find the bit position of the highest set bit in a word
+    \param bits The word to be searched
+    \return The bit number of the highest set bit, or -1 if the word is zero. */
+static __inline__ int top_bit(unsigned int bits)
+{
+    int res;
+
+#if defined(__i386__)  ||  defined(__x86_64__)
+    __asm__ (" xorl %[res],%[res];\n"
+             " decl %[res];\n"
+             " bsrl %[bits],%[res]\n"
+             : [res] "=&r" (res)
+             : [bits] "rm" (bits));
+    return res;
+#elif defined(__ppc__)  ||   defined(__powerpc__)
+    __asm__ ("cntlzw %[res],%[bits];\n"
+             : [res] "=&r" (res)
+             : [bits] "r" (bits));
+    return 31 - res;
+#else
+    if (bits == 0)
+        return -1;
+    res = 0;
+    if (bits & 0xFFFF0000)
+    {
+        bits &= 0xFFFF0000;
+        res += 16;
+    }
+    if (bits & 0xFF00FF00)
+    {
+        bits &= 0xFF00FF00;
+        res += 8;
+    }
+    if (bits & 0xF0F0F0F0)
+    {
+        bits &= 0xF0F0F0F0;
+        res += 4;
+    }
+    if (bits & 0xCCCCCCCC)
+    {
+        bits &= 0xCCCCCCCC;
+        res += 2;
+    }
+    if (bits & 0xAAAAAAAA)
+    {
+        bits &= 0xAAAAAAAA;
+        res += 1;
+    }
+    return res;
+#endif
+}
+/*- End of function --------------------------------------------------------*/
+
+/*! \brief Find the bit position of the lowest set bit in a word
+    \param bits The word to be searched
+    \return The bit number of the lowest set bit, or -1 if the word is zero. */
+static __inline__ int bottom_bit(unsigned int bits)
+{
+    int res;
+    
+#if defined(__i386__)  ||  defined(__x86_64__)
+    __asm__ (" xorl %[res],%[res];\n"
+             " decl %[res];\n"
+             " bsfl %[bits],%[res]\n"
+             : [res] "=&r" (res)
+             : [bits] "rm" (bits));
+    return res;
+#else
+    if (bits == 0)
+        return -1;
+    res = 31;
+    if (bits & 0x0000FFFF)
+    {
+        bits &= 0x0000FFFF;
+        res -= 16;
+    }
+    if (bits & 0x00FF00FF)
+    {
+        bits &= 0x00FF00FF;
+        res -= 8;
+    }
+    if (bits & 0x0F0F0F0F)
+    {
+        bits &= 0x0F0F0F0F;
+        res -= 4;
+    }
+    if (bits & 0x33333333)
+    {
+        bits &= 0x33333333;
+        res -= 2;
+    }
+    if (bits & 0x55555555)
+    {
+        bits &= 0x55555555;
+        res -= 1;
+    }
+    return res;
+#endif
+}
+/*- End of function --------------------------------------------------------*/
+
+/*! \brief Bit reverse a byte.
+    \param data The byte to be reversed.
+    \return The bit reversed version of data. */
+static __inline__ uint8_t bit_reverse8(uint8_t x)
+{
+#if defined(__i386__)  ||  defined(__x86_64__)  ||  defined(__ppc__)  ||  defined(__powerpc__)
+    /* If multiply is fast */
+    return ((x*0x0802U & 0x22110U) | (x*0x8020U & 0x88440U))*0x10101U >> 16;
+#else
+    /* If multiply is slow, but we have a barrel shifter */
+    x = (x >> 4) | (x << 4);
+    x = ((x & 0xCC) >> 2) | ((x & 0x33) << 2);
+    return ((x & 0xAA) >> 1) | ((x & 0x55) << 1);
+#endif
+}
+/*- End of function --------------------------------------------------------*/
+
+/*! \brief Bit reverse a 16 bit word.
+    \param data The word to be reversed.
+    \return The bit reversed version of data. */
+uint16_t bit_reverse16(uint16_t data);
+
+/*! \brief Bit reverse a 32 bit word.
+    \param data The word to be reversed.
+    \return The bit reversed version of data. */
+uint32_t bit_reverse32(uint32_t data);
+
+/*! \brief Bit reverse each of the four bytes in a 32 bit word.
+    \param data The word to be reversed.
+    \return The bit reversed version of data. */
+uint32_t bit_reverse_4bytes(uint32_t data);
+
+#if defined(__x86_64__)
+/*! \brief Bit reverse each of the eight bytes in a 64 bit word.
+    \param data The word to be reversed.
+    \return The bit reversed version of data. */
+uint64_t bit_reverse_8bytes(uint64_t data);
+#endif
+
+/*! \brief Bit reverse each bytes in a buffer.
+    \param to The buffer to place the reversed data in.
+    \param from The buffer containing the data to be reversed.
+    \param len The length of the data in the buffer. */
+void bit_reverse(uint8_t to[], const uint8_t from[], int len);
+
+/*! \brief Find the number of set bits in a 32 bit word.
+    \param x The word to be searched.
+    \return The number of set bits. */
+int one_bits32(uint32_t x);
+
+/*! \brief Create a mask as wide as the number in a 32 bit word.
+    \param x The word to be searched.
+    \return The mask. */
+uint32_t make_mask32(uint32_t x);
+
+/*! \brief Create a mask as wide as the number in a 16 bit word.
+    \param x The word to be searched.
+    \return The mask. */
+uint16_t make_mask16(uint16_t x);
+
+/*! \brief Find the least significant one in a word, and return a word
+           with just that bit set.
+    \param x The word to be searched.
+    \return The word with the single set bit. */
+static __inline__ uint32_t least_significant_one32(uint32_t x)
+{
+    return (x & (-(int32_t) x));
+}
+/*- End of function --------------------------------------------------------*/
+
+/*! \brief Find the most significant one in a word, and return a word
+           with just that bit set.
+    \param x The word to be searched.
+    \return The word with the single set bit. */
+static __inline__ uint32_t most_significant_one32(uint32_t x)
+{
+#if defined(__i386__)  ||  defined(__x86_64__)  ||  defined(__ppc__)  ||  defined(__powerpc__)
+    return 1 << top_bit(x);
+#else
+    x = make_mask32(x);
+    return (x ^ (x >> 1));
+#endif
+}
+/*- End of function --------------------------------------------------------*/
+
+/*! \brief Find the parity of a byte.
+    \param x The byte to be checked.
+    \return 1 for odd, or 0 for even. */
+static __inline__ int parity8(uint8_t x)
+{
+    x = (x ^ (x >> 4)) & 0x0F;
+    return (0x6996 >> x) & 1;
+}
+/*- End of function --------------------------------------------------------*/
+
+/*! \brief Find the parity of a 16 bit word.
+    \param x The word to be checked.
+    \return 1 for odd, or 0 for even. */
+static __inline__ int parity16(uint16_t x)
+{
+    x ^= (x >> 8);
+    x = (x ^ (x >> 4)) & 0x0F;
+    return (0x6996 >> x) & 1;
+}
+/*- End of function --------------------------------------------------------*/
+
+/*! \brief Find the parity of a 32 bit word.
+    \param x The word to be checked.
+    \return 1 for odd, or 0 for even. */
+static __inline__ int parity32(uint32_t x)
+{
+    x ^= (x >> 16);
+    x ^= (x >> 8);
+    x = (x ^ (x >> 4)) & 0x0F;
+    return (0x6996 >> x) & 1;
+}
+/*- End of function --------------------------------------------------------*/
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/spandsp/bitstream.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/spandsp/bitstream.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,89 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * bitstream.h - Bitstream composition and decomposition routines.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2006 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: bitstream.h,v 1.10 2008/04/17 14:27:00 steveu Exp $
+ */
+
+/*! \file */
+
+#if !defined(_SPANDSP_BITSTREAM_H_)
+#define _SPANDSP_BITSTREAM_H_
+
+/*! \page bitstream_page Bitstream composition and decomposition
+\section bitstream_page_sec_1 What does it do?
+
+\section bitstream_page_sec_2 How does it work?
+*/
+
+/*! Bitstream handler state */
+typedef struct
+{
+    /*! The bit stream. */
+    unsigned int bitstream;
+    /*! The residual bits in bitstream. */
+    unsigned int residue;
+} bitstream_state_t;
+
+
+#if defined(__cplusplus)
+extern "C"
+{
+#endif
+
+/*! \brief Put a chunk of bits into the output buffer.
+    \param s A pointer to the bitstream context.
+    \param c A pointer to the bitstream output buffer.
+    \param value The value to be pushed into the output buffer.
+    \param bits The number of bits of value to be pushed. 1 to 25 bit is valid. */
+void bitstream_put(bitstream_state_t *s, uint8_t **c, unsigned int value, int bits);
+
+void bitstream_put2(bitstream_state_t *s, uint8_t **c, unsigned int value, int bits);
+
+/*! \brief Get a chunk of bits from the input buffer.
+    \param s A pointer to the bitstream context.
+    \param c A pointer to the bitstream input buffer.
+    \param bits The number of bits of value to be grabbed. 1 to 25 bit is valid.
+    \return The value retrieved from the input buffer. */
+unsigned int bitstream_get(bitstream_state_t *s, const uint8_t **c, int bits);
+
+unsigned int bitstream_get2(bitstream_state_t *s, const uint8_t **c, int bits);
+
+/*! \brief Flush any residual bit to the output buffer.
+    \param s A pointer to the bitstream context.
+    \param c A pointer to the bitstream output buffer. */
+void bitstream_flush(bitstream_state_t *s, uint8_t **c);
+
+void bitstream_flush2(bitstream_state_t *s, uint8_t **c);
+
+/*! \brief Initialise a bitstream context.
+    \param s A pointer to the bitstream context.
+    \return A pointer to the bitstream context. */
+bitstream_state_t *bitstream_init(bitstream_state_t *s);
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/spandsp/complex.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/spandsp/complex.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,437 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * complex.h
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2003 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: complex.h,v 1.16 2008/04/17 14:27:00 steveu Exp $
+ */
+
+/*! \file */
+
+/*! \page complex_page Complex number support
+\section complex_page_sec_1 What does it do?
+Complex number support is part of the C99 standard. However, support for this
+in C compilers is still patchy. A set of complex number feaures is provided as
+a "temporary" measure, until native C language complex number support is
+widespread.
+*/
+
+#if !defined(_SPANDSP_COMPLEX_H_)
+#define _SPANDSP_COMPLEX_H_
+
+/*!
+    Floating complex type.
+*/
+typedef struct
+{
+    /*! \brief Real part. */
+    float re;
+    /*! \brief Imaginary part. */
+    float im;
+} complexf_t;
+
+/*!
+    Floating complex type.
+*/
+typedef struct
+{
+    /*! \brief Real part. */
+    double re;
+    /*! \brief Imaginary part. */
+    double im;
+} complex_t;
+
+#if defined(HAVE_LONG_DOUBLE)
+/*!
+    Long double complex type.
+*/
+typedef struct
+{
+    /*! \brief Real part. */
+    long double re;
+    /*! \brief Imaginary part. */
+    long double im;
+} complexl_t;
+#endif
+
+/*!
+    Complex integer type.
+*/
+typedef struct
+{
+    /*! \brief Real part. */
+    int re;
+    /*! \brief Imaginary part. */
+    int im;
+} complexi_t;
+
+/*!
+    Complex 16 bit integer type.
+*/
+typedef struct
+{
+    /*! \brief Real part. */
+    int16_t re;
+    /*! \brief Imaginary part. */
+    int16_t im;
+} complexi16_t;
+
+/*!
+    Complex 32 bit integer type.
+*/
+typedef struct
+{
+    /*! \brief Real part. */
+    int32_t re;
+    /*! \brief Imaginary part. */
+    int32_t im;
+} complexi32_t;
+
+#if defined(__cplusplus)
+extern "C"
+{
+#endif
+
+static __inline__ complexf_t complex_setf(float re, float im)
+{
+    complexf_t z;
+
+    z.re = re;
+    z.im = im;
+    return z;
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ complex_t complex_set(float re, float im)
+{
+    complex_t z;
+
+    z.re = re;
+    z.im = im;
+    return z;
+}
+/*- End of function --------------------------------------------------------*/
+
+#if defined(HAVE_LONG_DOUBLE)
+static __inline__ complexl_t complex_setl(long double re, long double im)
+{
+    complexl_t z;
+
+    z.re = re;
+    z.im = im;
+    return z;
+}
+/*- End of function --------------------------------------------------------*/
+#endif
+
+static __inline__ complexi_t complex_seti(int re, int im)
+{
+    complexi_t z;
+
+    z.re = re;
+    z.im = im;
+    return z;
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ complexi16_t complex_seti16(int16_t re, int16_t im)
+{
+    complexi16_t z;
+
+    z.re = re;
+    z.im = im;
+    return z;
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ complexi32_t complex_seti32(int32_t re, int32_t im)
+{
+    complexi32_t z;
+
+    z.re = re;
+    z.im = im;
+    return z;
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ complexf_t complex_addf(const complexf_t *x, const complexf_t *y)
+{
+    complexf_t z;
+
+    z.re = x->re + y->re;
+    z.im = x->im + y->im;
+    return z;
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ complex_t complex_add(const complex_t *x, const complex_t *y)
+{
+    complex_t z;
+
+    z.re = x->re + y->re;
+    z.im = x->im + y->im;
+    return z;
+}
+/*- End of function --------------------------------------------------------*/
+
+#if defined(HAVE_LONG_DOUBLE)
+static __inline__ complexl_t complex_addl(const complexl_t *x, const complexl_t *y)
+{
+    complexl_t z;
+
+    z.re = x->re + y->re;
+    z.im = x->im + y->im;
+    return z;
+}
+/*- End of function --------------------------------------------------------*/
+#endif
+
+static __inline__ complexi_t complex_addi(const complexi_t *x, const complexi_t *y)
+{
+    complexi_t z;
+
+    z.re = x->re + y->re;
+    z.im = x->im + y->im;
+    return z;
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ complexi16_t complex_addi16(const complexi16_t *x, const complexi16_t *y)
+{
+    complexi16_t z;
+
+    z.re = x->re + y->re;
+    z.im = x->im + y->im;
+    return z;
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ complexi32_t complex_addi32(const complexi32_t *x, const complexi32_t *y)
+{
+    complexi32_t z;
+
+    z.re = x->re + y->re;
+    z.im = x->im + y->im;
+    return z;
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ complexf_t complex_subf(const complexf_t *x, const complexf_t *y)
+{
+    complexf_t z;
+
+    z.re = x->re - y->re;
+    z.im = x->im - y->im;
+    return z;
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ complex_t complex_sub(const complex_t *x, const complex_t *y)
+{
+    complex_t z;
+
+    z.re = x->re - y->re;
+    z.im = x->im - y->im;
+    return z;
+}
+/*- End of function --------------------------------------------------------*/
+
+#if defined(HAVE_LONG_DOUBLE)
+static __inline__ complexl_t complex_subl(const complexl_t *x, const complexl_t *y)
+{
+    complexl_t z;
+
+    z.re = x->re - y->re;
+    z.im = x->im - y->im;
+    return z;
+}
+/*- End of function --------------------------------------------------------*/
+#endif
+
+static __inline__ complexi_t complex_subi(const complexi_t *x, const complexi_t *y)
+{
+    complexi_t z;
+
+    z.re = x->re - y->re;
+    z.im = x->im - y->im;
+    return z;
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ complexi16_t complex_subi16(const complexi16_t *x, const complexi16_t *y)
+{
+    complexi16_t z;
+
+    z.re = x->re - y->re;
+    z.im = x->im - y->im;
+    return z;
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ complexi32_t complex_subi32(const complexi32_t *x, const complexi32_t *y)
+{
+    complexi32_t z;
+
+    z.re = x->re - y->re;
+    z.im = x->im - y->im;
+    return z;
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ complexf_t complex_mulf(const complexf_t *x, const complexf_t *y)
+{
+    complexf_t z;
+
+    z.re = x->re*y->re - x->im*y->im;
+    z.im = x->re*y->im + x->im*y->re;
+    return z;
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ complex_t complex_mul(const complex_t *x, const complex_t *y)
+{
+    complex_t z;
+
+    z.re = x->re*y->re - x->im*y->im;
+    z.im = x->re*y->im + x->im*y->re;
+    return z;
+}
+/*- End of function --------------------------------------------------------*/
+
+#if defined(HAVE_LONG_DOUBLE)
+static __inline__ complexl_t complex_mull(const complexl_t *x, const complexl_t *y)
+{
+    complexl_t z;
+
+    z.re = x->re*y->re - x->im*y->im;
+    z.im = x->re*y->im + x->im*y->re;
+    return z;
+}
+/*- End of function --------------------------------------------------------*/
+#endif
+
+static __inline__ complexf_t complex_divf(const complexf_t *x, const complexf_t *y)
+{
+    complexf_t z;
+    float f;
+    
+    f = y->re*y->re + y->im*y->im;
+    z.re = ( x->re*y->re + x->im*y->im)/f;
+    z.im = (-x->re*y->im + x->im*y->re)/f;
+    return z;
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ complex_t complex_div(const complex_t *x, const complex_t *y)
+{
+    complex_t z;
+    double f;
+    
+    f = y->re*y->re + y->im*y->im;
+    z.re = ( x->re*y->re + x->im*y->im)/f;
+    z.im = (-x->re*y->im + x->im*y->re)/f;
+    return z;
+}
+/*- End of function --------------------------------------------------------*/
+
+#if defined(HAVE_LONG_DOUBLE)
+static __inline__ complexl_t complex_divl(const complexl_t *x, const complexl_t *y)
+{
+    complexl_t z;
+    long double f;
+    
+    f = y->re*y->re + y->im*y->im;
+    z.re = ( x->re*y->re + x->im*y->im)/f;
+    z.im = (-x->re*y->im + x->im*y->re)/f;
+    return z;
+}
+/*- End of function --------------------------------------------------------*/
+#endif
+
+static __inline__ complexf_t complex_conjf(const complexf_t *x)
+{
+    complexf_t z;
+
+    z.re = x->re;
+    z.im = -x->im;
+    return z;
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ complex_t complex_conj(const complex_t *x)
+{
+    complex_t z;
+
+    z.re = x->re;
+    z.im = -x->im;
+    return z;
+}
+/*- End of function --------------------------------------------------------*/
+
+#if defined(HAVE_LONG_DOUBLE)
+static __inline__ complexl_t complex_conjl(const complexl_t *x)
+{
+    complexl_t z;
+
+    z.re = x->re;
+    z.im = -x->im;
+    return z;
+}
+/*- End of function --------------------------------------------------------*/
+#endif
+
+static __inline__ complexi_t complexi_conj(const complexi_t *x)
+{
+    complexi_t z;
+
+    z.re = x->re;
+    z.im = -x->im;
+    return z;
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ float powerf(const complexf_t *x)
+{
+    return x->re*x->re + x->im*x->im;
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ double power(const complex_t *x)
+{
+    return x->re*x->re + x->im*x->im;
+}
+/*- End of function --------------------------------------------------------*/
+
+#if defined(HAVE_LONG_DOUBLE)
+static __inline__ long double powerl(const complexl_t *x)
+{
+    return x->re*x->re + x->im*x->im;
+}
+/*- End of function --------------------------------------------------------*/
+#endif
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/spandsp/complex_filters.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/spandsp/complex_filters.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,75 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * complex_filters.h
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2003 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: complex_filters.h,v 1.12 2008/04/17 14:27:00 steveu Exp $
+ */
+
+#if !defined(_SPANDSP_COMPLEX_FILTERS_H_)
+#define _SPANDSP_COMPLEX_FILTERS_H_
+
+typedef struct filter_s filter_t;
+
+typedef float (*filter_step_func_t)(filter_t *fi, float x);
+
+/*! Filter state */
+typedef struct
+{
+    int                 nz;
+    int                 np;
+    filter_step_func_t  fsf;
+} fspec_t;
+
+struct filter_s
+{
+    fspec_t             *fs;
+    float               sum;
+    int                 ptr;		/* for moving average filters only */
+    float               v[];
+};
+
+typedef struct
+{
+    filter_t            *ref;
+    filter_t            *imf;
+} cfilter_t;
+
+#if defined(__cplusplus)
+extern "C"
+{
+#endif
+
+filter_t *filter_create(fspec_t *fs);
+void filter_delete(filter_t *fi);
+float filter_step(filter_t *fi, float x);
+
+cfilter_t *cfilter_create(fspec_t *fs);
+void cfilter_delete(cfilter_t *cfi);
+complexf_t cfilter_step(cfilter_t *cfi, const complexf_t *z);
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/spandsp/complex_vector_float.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/spandsp/complex_vector_float.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,128 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * complex_vector_float.h
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2003 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: complex_vector_float.h,v 1.8 2008/04/17 14:27:00 steveu Exp $
+ */
+
+#if !defined(_SPANDSP_COMPLEX_VECTOR_FLOAT_H_)
+#define _SPANDSP_COMPLEX_VECTOR_FLOAT_H_
+
+#if defined(__cplusplus)
+extern "C"
+{
+#endif
+
+static __inline__ void cvec_copyf(complexf_t z[], const complexf_t x[], int n)
+{
+    int i;
+    
+    for (i = 0;  i < n;  i++)
+        z[i] = x[i];
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ void cvec_copy(complex_t z[], const complex_t x[], int n)
+{
+    int i;
+    
+    for (i = 0;  i < n;  i++)
+        z[i] = x[i];
+}
+/*- End of function --------------------------------------------------------*/
+
+#if defined(HAVE_LONG_DOUBLE)
+static __inline__ void cvec_copyl(complexl_t z[], const complexl_t x[], int n)
+{
+    int i;
+    
+    for (i = 0;  i < n;  i++)
+        z[i] = x[i];
+}
+/*- End of function --------------------------------------------------------*/
+#endif
+
+static __inline__ void cvec_zerof(complexf_t z[], int n)
+{
+    int i;
+    
+    for (i = 0;  i < n;  i++)
+        z[i] = complex_setf(0.0f, 0.0f);
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ void cvec_zero(complex_t z[], int n)
+{
+    int i;
+    
+    for (i = 0;  i < n;  i++)
+        z[i] = complex_set(0.0, 0.0);
+}
+/*- End of function --------------------------------------------------------*/
+
+#if defined(HAVE_LONG_DOUBLE)
+static __inline__ void cvec_zerol(complexl_t z[], int n)
+{
+    int i;
+    
+    for (i = 0;  i < n;  i++)
+        z[i] = complex_setl(0.0, 0.0);
+}
+/*- End of function --------------------------------------------------------*/
+#endif
+
+static __inline__ void cvec_setf(complexf_t z[], complexf_t *x, int n)
+{
+    int i;
+    
+    for (i = 0;  i < n;  i++)
+        z[i] = *x;
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ void cvec_set(complex_t z[], complex_t *x, int n)
+{
+    int i;
+    
+    for (i = 0;  i < n;  i++)
+        z[i] = *x;
+}
+/*- End of function --------------------------------------------------------*/
+
+#if defined(HAVE_LONG_DOUBLE)
+static __inline__ void cvec_setl(complexl_t z[], complexl_t *x, int n)
+{
+    int i;
+    
+    for (i = 0;  i < n;  i++)
+        z[i] = *x;
+}
+/*- End of function --------------------------------------------------------*/
+#endif
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/spandsp/crc.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/spandsp/crc.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,98 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * crc.h
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2003 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: crc.h,v 1.4 2008/04/17 14:27:00 steveu Exp $
+ */
+
+/*! \file */
+
+/*! \page crc_page CRC
+
+\section crc_page_sec_1 What does it do?
+
+\section crc_page_sec_2 How does it work?
+*/
+
+#if !defined(_SPANDSP_CRC_H_)
+#define _SPANDSP_CRC_H_
+
+#if defined(__cplusplus)
+extern "C"
+{
+#endif
+
+/*! \brief Calculate the ITU/CCITT CRC-32 value in buffer.
+    \param buf The buffer containing the data.
+    \param len The length of the frame.
+    \param crc The initial CRC value. This is usually 0xFFFFFFFF, or 0 for a new block (it depends on
+           the application). It is previous returned CRC value for the continuation of a block.
+    \return The CRC value.
+*/
+uint32_t crc_itu32_calc(const uint8_t *buf, int len, uint32_t crc);
+
+/*! \brief Append an ITU/CCITT CRC-32 value to a frame.
+    \param buf The buffer containing the frame. This must be at least 2 bytes longer than
+               the frame it contains, to allow room for the CRC value.
+    \param len The length of the frame.
+    \return The new length of the frame.
+*/
+int crc_itu32_append(uint8_t *buf, int len);
+
+/*! \brief Check the ITU/CCITT CRC-32 value in a frame.
+    \param buf The buffer containing the frame.
+    \param len The length of the frame.
+    \return TRUE if the CRC is OK, else FALSE.
+*/
+int crc_itu32_check(const uint8_t *buf, int len);
+
+/*! \brief Calculate the ITU/CCITT CRC-16 value in buffer.
+    \param buf The buffer containing the data.
+    \param len The length of the frame.
+    \param crc The initial CRC value. This is usually 0xFFFF, or 0 for a new block (it depends on
+           the application). It is previous returned CRC value for the continuation of a block.
+    \return The CRC value.
+*/
+uint16_t crc_itu16_calc(const uint8_t *buf, int len, uint16_t crc);
+
+/*! \brief Append an ITU/CCITT CRC-16 value to a frame.
+    \param buf The buffer containing the frame. This must be at least 2 bytes longer than
+               the frame it contains, to allow room for the CRC value.
+    \param len The length of the frame.
+    \return The new length of the frame.
+*/
+int crc_itu16_append(uint8_t *buf, int len);
+
+/*! \brief Check the ITU/CCITT CRC-16 value in a frame.
+    \param buf The buffer containing the frame.
+    \param len The length of the frame.
+    \return TRUE if the CRC is OK, else FALSE.
+*/
+int crc_itu16_check(const uint8_t *buf, int len);
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/spandsp/dc_restore.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/spandsp/dc_restore.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,147 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * dc_restore.h - General telephony routines to restore the zero D.C.
+ *                level to audio which has a D.C. bias.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2001 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: dc_restore.h,v 1.23 2008/08/16 14:59:50 steveu Exp $
+ */
+
+/*! \file */
+
+#if !defined(_SPANDSP_DC_RESTORE_H_)
+#define _SPANDSP_DC_RESTORE_H_
+
+/*! \page dc_restore_page Removing DC bias from a signal
+
+\section dc_restore_page_sec_1 What does it do?
+
+Telecoms signals often contain considerable DC, but DC upsets a lot of signal
+processing functions. Placing a zero DC restorer at the front of the processing
+chain can often simplify the downstream processing. 
+
+\section dc_restore_page_sec_2 How does it work?
+
+The DC restorer uses a leaky integrator to provide a long-ish term estimate of
+the DC bias in the signal. A 32 bit estimate is used for the 16 bit audio, so
+the noise introduced by the estimation can be keep in the lower bits, and the 16
+bit DC value, which is subtracted from the signal, is fairly clean. The
+following code fragment shows the algorithm used. dc_bias is a 32 bit integer,
+while the sample and the resulting clean_sample are 16 bit integers. 
+
+    dc_bias += ((((int32_t) sample << 15) - dc_bias) >> 14);
+    clean_sample = sample - (dc_bias >> 15); 
+*/
+
+/*!
+    Zero DC restoration descriptor. This defines the working state for a single
+    instance of DC content filter.
+*/
+typedef struct
+{
+    int32_t state;
+} dc_restore_state_t;
+
+#if defined(__cplusplus)
+extern "C"
+{
+#endif
+
+static __inline__ void dc_restore_init(dc_restore_state_t *dc)
+{
+    dc->state = 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ int16_t dc_restore(dc_restore_state_t *dc, int16_t sample)
+{
+    dc->state += ((((int32_t) sample << 15) - dc->state) >> 14);
+    return (int16_t) (sample - (dc->state >> 15));
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ int16_t dc_restore_estimate(dc_restore_state_t *dc)
+{
+    return (int16_t) (dc->state >> 15);
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ int16_t saturate(int32_t amp)
+{
+    int16_t amp16;
+
+    /* Hopefully this is optimised for the common case - not clipping */
+    amp16 = (int16_t) amp;
+    if (amp == amp16)
+        return amp16;
+    if (amp > INT16_MAX)
+        return  INT16_MAX;
+    return  INT16_MIN;
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ int16_t fsaturatef(float famp)
+{
+    if (famp > 32767.0f)
+        return  INT16_MAX;
+    if (famp < -32768.0f)
+        return  INT16_MIN;
+    return (int16_t) rintf(famp);
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ int16_t fsaturate(double damp)
+{
+    if (damp > 32767.0)
+        return  INT16_MAX;
+    if (damp < -32768.0)
+        return  INT16_MIN;
+    return (int16_t) rint(damp);
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ float ffsaturatef(float famp)
+{
+    if (famp > 32767.0f)
+        return  (float) INT16_MAX;
+    if (famp < -32768.0f)
+        return  (float) INT16_MIN;
+    return famp;
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ double ffsaturate(double famp)
+{
+    if (famp > 32767.0)
+        return  (double) INT16_MAX;
+    if (famp < -32768.0)
+        return  (double) INT16_MIN;
+    return famp;
+}
+/*- End of function --------------------------------------------------------*/
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/spandsp/dds.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/spandsp/dds.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,204 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * dds.h
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2003 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: dds.h,v 1.18 2008/04/17 14:27:00 steveu Exp $
+ */
+
+/*! \file */
+
+#if !defined(_SPANDSP_DDS_H_)
+#define _SPANDSP_DDS_H_
+
+#if defined(__cplusplus)
+extern "C"
+{
+#endif
+
+/*! \brief Find the phase rate value to achieve a particular frequency.
+    \param frequency The desired frequency, in Hz.
+    \return The phase rate which while achieve the desired frequency.
+*/
+int32_t dds_phase_rate(float frequency);
+
+/*! \brief Find the frequency, in Hz, equivalent to a phase rate.
+    \param phase_rate The phase rate.
+    \return The equivalent frequency, in Hz.
+*/
+float dds_frequency(int32_t phase_rate);
+
+/*! \brief Find the scaling factor needed to achieve a specified level in dBm0.
+    \param level The desired signal level, in dBm0.
+    \return The scaling factor.
+*/
+int dds_scaling_dbm0(float level);
+
+/*! \brief Find the scaling factor needed to achieve a specified level in dBmov.
+    \param level The desired signal level, in dBmov.
+    \return The scaling factor.
+*/
+int dds_scaling_dbov(float level);
+
+/*! \brief Find the amplitude for a particular phase.
+    \param phase The desired phase 32 bit phase.
+    \return The signal amplitude.
+*/
+int16_t dds_lookup(uint32_t phase);
+
+/*! \brief Find the amplitude for a particular phase offset from an accumulated phase.
+    \param phase_acc The accumulated phase.
+    \param phase_offset The phase offset.
+    \return The signal amplitude.
+*/
+int16_t dds_offset(uint32_t phase_acc, int32_t phase_offset);
+
+/*! \brief Advance the phase, without returning any new signal sample.
+    \param phase_acc A pointer to a phase accumulator value.
+    \param phase_rate The phase increment to be applied.
+*/
+void dds_advance(uint32_t *phase_acc, int32_t phase_rate);
+
+/*! \brief Generate an integer tone sample.
+    \param phase_acc A pointer to a phase accumulator value.
+    \param phase_rate The phase increment to be applied.
+    \return The signal amplitude, between -32767 and 32767.
+*/
+int16_t dds(uint32_t *phase_acc, int32_t phase_rate);
+
+/*! \brief Lookup the integer value of a specified phase.
+    \param phase The phase accumulator value to be looked up.
+    \return The signal amplitude, between -32767 and 32767.
+*/
+int16_t dds_lookup(uint32_t phase);
+
+/*! \brief Generate an integer tone sample, with modulation.
+    \param phase_acc A pointer to a phase accumulator value.
+    \param phase_rate The phase increment to be applied.
+    \param scale The scaling factor.
+    \param phase The phase offset.
+    \return The signal amplitude, between -32767 and 32767.
+*/
+int16_t dds_mod(uint32_t *phase_acc, int32_t phase_rate, int scale, int32_t phase);
+
+/*! \brief Generate a complex integer tone sample.
+    \param phase_acc A pointer to a phase accumulator value.
+    \param phase_rate The phase increment to be applied.
+    \return The complex signal amplitude, between (-32767, -32767) and (32767, 32767).
+*/
+complexi_t dds_complexi(uint32_t *phase_acc, int32_t phase_rate);
+
+/*! \brief Lookup the complex integer value of a specified phase.
+    \param phase The phase accumulator value to be looked up.
+    \return The complex signal amplitude, between (-32767, -32767) and (32767, 32767).
+*/
+complexi_t dds_lookup_complexi(uint32_t phase);
+
+/*! \brief Generate a complex integer tone sample, with modulation.
+    \param phase_acc A pointer to a phase accumulator value.
+    \param phase_rate The phase increment to be applied.
+    \param scale The scaling factor.
+    \param phase The phase offset.
+    \return The complex signal amplitude, between (-32767, -32767) and (32767, 32767).
+*/
+complexi_t dds_complexi_mod(uint32_t *phase_acc, int32_t phase_rate, int scale, int32_t phase);
+
+/*! \brief Find the phase rate equivalent to a frequency, in Hz.
+    \param frequency The frequency, in Hz.
+    \return The equivalent phase rate.
+*/
+int32_t dds_phase_ratef(float frequency);
+
+/*! \brief Find the frequency, in Hz, equivalent to a phase rate.
+    \param phase_rate The phase rate.
+    \return The equivalent frequency, in Hz.
+*/
+float dds_frequencyf(int32_t phase_rate);
+
+/*! \brief Find the scaling factor equivalent to a dBm0 value.
+    \param level The signal level in dBm0.
+    \return The equivalent scaling factor.
+*/
+float dds_scaling_dbm0f(float level);
+
+/*! \brief Find the scaling factor equivalent to a dBmov value.
+    \param level The signal level in dBmov.
+    \return The equivalent scaling factor.
+*/
+float dds_scaling_dbovf(float level);
+
+/*! \brief Advance the phase, without returning any new signal sample.
+    \param phase_acc A pointer to a phase accumulator value.
+    \param phase_rate The phase increment to be applied.
+*/
+void dds_advancef(uint32_t *phase_acc, int32_t phase_rate);
+
+/*! \brief Generate a floating point tone sample.
+    \param phase_acc A pointer to a phase accumulator value.
+    \param phase_rate The phase increment to be applied.
+    \return The signal amplitude, between -1.0 and 1.0.
+*/
+float ddsf(uint32_t *phase_acc, int32_t phase_rate);
+
+/*! \brief Lookup the floating point value of a specified phase.
+    \param phase The phase accumulator value to be looked up.
+    \return The signal amplitude, between -1.0 and 1.0.
+*/
+float dds_lookupf(uint32_t phase);
+
+/*! \brief Generate a floating point tone sample, with modulation.
+    \param phase_acc A pointer to a phase accumulator value.
+    \param phase_rate The phase increment to be applied.
+    \param scale The scaling factor.
+    \param phase The phase offset.
+    \return The signal amplitude, between -1.0 and 1.0.
+*/
+float dds_modf(uint32_t *phase_acc, int32_t phase_rate, float scale, int32_t phase);
+
+/*! \brief Generate a complex floating point tone sample.
+    \param phase_acc A pointer to a phase accumulator value.
+    \param phase_rate The phase increment to be applied.
+    \return The complex signal amplitude, between (-1.0, -1.0) and (1.0, 1.0).
+*/
+complexf_t dds_complexf(uint32_t *phase_acc, int32_t phase_rate);
+
+/*! \brief Lookup the complex value of a specified phase.
+    \param phase The phase accumulator value to be looked up.
+    \return The complex signal amplitude, between (-1.0, -1.0) and (1.0, 1.0).
+*/
+complexf_t dds_lookup_complexf(uint32_t phase_acc);
+
+/*! \brief Generate a complex floating point tone sample, with modulation.
+    \param phase_acc A pointer to a phase accumulator value.
+    \param phase_rate The phase increment to be applied.
+    \param scale The scaling factor.
+    \param phase The phase offset.
+    \return The complex signal amplitude, between (-1.0, -1.0) and (1.0, 1.0).
+*/
+complexf_t dds_complex_modf(uint32_t *phase_acc, int32_t phase_rate, float scale, int32_t phase);
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/spandsp/dtmf.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/spandsp/dtmf.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,276 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * dtmf.h - 
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2001, 2005 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: dtmf.h,v 1.28 2008/06/13 14:46:52 steveu Exp $
+ */
+
+#if !defined(_SPANDSP_DTMF_H_)
+#define _SPANDSP_DTMF_H_
+
+/*! \page dtmf_rx_page DTMF receiver
+\section dtmf_rx_page_sec_1 What does it do?
+The DTMF receiver detects the standard DTMF digits. It is compliant with
+ITU-T Q.23, ITU-T Q.24, and the local DTMF specifications of most administrations.
+Its passes the test suites. It also scores *very* well on the standard
+talk-off tests. 
+
+The current design uses floating point extensively. It is not tolerant of DC.
+It is expected that a DC restore stage will be placed before the DTMF detector.
+Unless the dial tone filter is switched on, the detector has poor tolerance
+of dial tone. Whether this matter depends on your application. If you are using
+the detector in an IVR application you will need proper echo cancellation to
+get good performance in the presence of speech prompts, so dial tone will not
+exist. If you do need good dial tone tolerance, a dial tone filter can be
+enabled in the detector.
+
+The DTMF receiver's design assumes the channel is free of any DC component.
+
+\section dtmf_rx_page_sec_2 How does it work?
+Like most other DSP based DTMF detector's, this one uses the Goertzel algorithm
+to look for the DTMF tones. What makes each detector design different is just how
+that algorithm is used.
+
+Basic DTMF specs:
+    - Minimum tone on = 40ms
+    - Minimum tone off = 50ms
+    - Maximum digit rate = 10 per second
+    - Normal twist <= 8dB accepted
+    - Reverse twist <= 4dB accepted
+    - S/N >= 15dB will detect OK
+    - Attenuation <= 26dB will detect OK
+    - Frequency tolerance +- 1.5% will detect, +-3.5% will reject
+
+TODO:
+*/
+
+/*! \page dtmf_tx_page DTMF tone generation
+\section dtmf_tx_page_sec_1 What does it do?
+
+The DTMF tone generation module provides for the generation of the
+repertoire of 16 DTMF dual tones. 
+
+\section dtmf_tx_page_sec_2 How does it work?
+*/
+
+#define MAX_DTMF_DIGITS 128
+
+typedef void (*digits_rx_callback_t)(void *user_data, const char *digits, int len);
+
+/*!
+    DTMF generator state descriptor. This defines the state of a single
+    working instance of a DTMF generator.
+*/
+typedef struct
+{
+    tone_gen_state_t tones;
+    float low_level;
+    float high_level;
+    int on_time;
+    int off_time;
+    union
+    {
+        queue_state_t queue;
+        uint8_t buf[QUEUE_STATE_T_SIZE(MAX_DTMF_DIGITS)];
+    } queue;
+} dtmf_tx_state_t;
+
+/*!
+    DTMF digit detector descriptor.
+*/
+typedef struct
+{
+    /*! Optional callback funcion to deliver received digits. */
+    digits_rx_callback_t digits_callback;
+    /*! An opaque pointer passed to the callback function. */
+    void *digits_callback_data;
+    /*! Optional callback funcion to deliver real time digit state changes. */
+    tone_report_func_t realtime_callback;
+    /*! An opaque pointer passed to the real time callback function. */
+    void *realtime_callback_data;
+    /*! TRUE if dialtone should be filtered before processing */
+    int filter_dialtone;
+#if defined(SPANDSP_USE_FIXED_POINT)
+    /*! 350Hz filter state for the optional dialtone filter. */
+    float z350[2];
+    /*! 440Hz filter state for the optional dialtone filter. */
+    float z440[2];
+    /*! Maximum acceptable "normal" (lower bigger than higher) twist ratio. */
+    float normal_twist;
+    /*! Maximum acceptable "reverse" (higher bigger than lower) twist ratio. */
+    float reverse_twist;
+    /*! Minimum acceptable tone level for detection. */
+    int32_t threshold;
+    /*! The accumlating total energy on the same period over which the Goertzels work. */
+    int32_t energy;
+#else
+    /*! 350Hz filter state for the optional dialtone filter. */
+    float z350[2];
+    /*! 440Hz filter state for the optional dialtone filter. */
+    float z440[2];
+    /*! Maximum acceptable "normal" (lower bigger than higher) twist ratio. */
+    float normal_twist;
+    /*! Maximum acceptable "reverse" (higher bigger than lower) twist ratio. */
+    float reverse_twist;
+    /*! Minimum acceptable tone level for detection. */
+    float threshold;
+    /*! The accumlating total energy on the same period over which the Goertzels work. */
+    float energy;
+#endif
+    /*! Tone detector working states for the row tones. */
+    goertzel_state_t row_out[4];
+    /*! Tone detector working states for the column tones. */
+    goertzel_state_t col_out[4];
+    /*! The result of the last tone analysis. */
+    uint8_t last_hit;
+    /*! The confirmed digit we are currently receiving */
+    uint8_t in_digit;
+    /*! The current sample number within a processing block. */
+    int current_sample;
+
+    /*! The number of digits which have been lost due to buffer overflows. */
+    int lost_digits;
+    /*! The number of digits currently in the digit buffer. */
+    int current_digits;
+    /*! The received digits buffer. This is a NULL terminated string. */
+    char digits[MAX_DTMF_DIGITS + 1];
+} dtmf_rx_state_t;
+
+#if defined(__cplusplus)
+extern "C"
+{
+#endif
+
+/*! \brief Generate a buffer of DTMF tones.
+    \param s The DTMF generator context.
+    \param amp The buffer for the generated signal.
+    \param max_samples The required number of generated samples.
+    \return The number of samples actually generated. This may be less than 
+            max_samples if the input buffer empties. */
+int dtmf_tx(dtmf_tx_state_t *s, int16_t amp[], int max_samples);
+
+/*! \brief Put a string of digits in a DTMF generator's input buffer.
+    \param s The DTMF generator context.
+    \param digits The string of digits to be added.
+    \param len The length of the string of digits. If negative, the string is
+           assumed to be a NULL terminated string.
+    \return The number of digits actually added. This may be less than the
+            length of the digit string, if the buffer fills up. */
+size_t dtmf_tx_put(dtmf_tx_state_t *s, const char *digits, int len);
+
+/*! \brief Change the transmit level for a DTMF tone generator context.
+    \param s The DTMF generator context.
+    \param level The level of the low tone, in dBm0.
+    \param twist The twist, in dB. */
+void dtmf_tx_set_level(dtmf_tx_state_t *s, int level, int twist);
+
+/*! \brief Change the transmit on and off time for a DTMF tone generator context.
+    \param s The DTMF generator context.
+    \param on-time The on time, in ms.
+    \param off_time The off time, in ms. */
+void dtmf_tx_set_timing(dtmf_tx_state_t *s, int on_time, int off_time);
+
+/*! \brief Initialise a DTMF tone generator context.
+    \param s The DTMF generator context.
+    \return A pointer to the DTMF generator context. */
+dtmf_tx_state_t *dtmf_tx_init(dtmf_tx_state_t *s);
+
+/*! \brief Free a DTMF tone generator context.
+    \param s The DTMF tone generator context.
+    \return 0 for OK, else -1. */
+int dtmf_tx_free(dtmf_tx_state_t *s);
+
+/*! Set a optional realtime callback for a DTMF receiver context. This function
+    is called immediately a confirmed state change occurs in the received DTMF. It
+    is called with the ASCII value for a DTMF tone pair, or zero to indicate no tone
+    is being received.
+    \brief Set a realtime callback for a DTMF receiver context.
+    \param s The DTMF receiver context.
+    \param callback Callback routine used to report the start and end of digits.
+    \param user_data An opaque pointer which is associated with the context,
+           and supplied in callbacks. */
+void dtmf_rx_set_realtime_callback(dtmf_rx_state_t *s,
+                                   tone_report_func_t callback,
+                                   void *user_data);
+
+/*! \brief Adjust a DTMF receiver context.
+    \param s The DTMF receiver context.
+    \param filter_dialtone TRUE to enable filtering of dialtone, FALSE
+           to disable, < 0 to leave unchanged.
+    \param twist Acceptable twist, in dB. < 0 to leave unchanged.
+    \param reverse_twist Acceptable reverse twist, in dB. < 0 to leave unchanged.
+    \param threshold The minimum acceptable tone level for detection, in dBm0.
+           <= -99 to leave unchanged. */
+void dtmf_rx_parms(dtmf_rx_state_t *s,
+                   int filter_dialtone,
+                   int twist,
+                   int reverse_twist,
+                   int threshold);
+
+/*! Process a block of received DTMF audio samples.
+    \brief Process a block of received DTMF audio samples.
+    \param s The DTMF receiver context.
+    \param amp The audio sample buffer.
+    \param samples The number of samples in the buffer.
+    \return The number of samples unprocessed. */
+int dtmf_rx(dtmf_rx_state_t *s, const int16_t amp[], int samples);
+
+/*! Get the status of DTMF detection during processing of the last audio
+    chunk.
+    \brief Get the status of DTMF detection during processing of the last
+           audio chunk.
+    \param s The DTMF receiver context.
+    \return The current digit status. Either 'x' for a "maybe" condition, or the
+            digit being detected. */
+int dtmf_rx_status(dtmf_rx_state_t *s);
+
+/*! \brief Get a string of digits from a DTMF receiver's output buffer.
+    \param s The DTMF receiver context.
+    \param digits The buffer for the received digits.
+    \param max The maximum  number of digits to be returned,
+    \return The number of digits actually returned. */
+size_t dtmf_rx_get(dtmf_rx_state_t *s, char *digits, int max);
+
+/*! \brief Initialise a DTMF receiver context.
+    \param s The DTMF receiver context.
+    \param callback An optional callback routine, used to report received digits. If
+           no callback routine is set, digits may be collected, using the dtmf_rx_get()
+           function.
+    \param user_data An opaque pointer which is associated with the context,
+           and supplied in callbacks.
+    \return A pointer to the DTMF receiver context. */
+dtmf_rx_state_t *dtmf_rx_init(dtmf_rx_state_t *s,
+                              digits_rx_callback_t callback,
+                              void *user_data);
+
+/*! \brief Free a DTMF receiver context.
+    \param s The DTMF receiver context.
+    \return 0 for OK, else -1. */
+int dtmf_rx_free(dtmf_rx_state_t *s);
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/spandsp/echo.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/spandsp/echo.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,239 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * echo.h - An echo cancellor, suitable for electrical and acoustic
+ *	        cancellation. This code does not currently comply with
+ *	        any relevant standards (e.g. G.164/5/7/8).
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2001 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: echo.h,v 1.15 2008/08/29 10:02:47 steveu Exp $
+ */
+
+/*! \file */
+
+#if !defined(_SPANDSP_ECHO_H_)
+#define _SPANDSP_ECHO_H_
+
+/*! \page echo_can_page Line echo cancellation for voice
+
+\section echo_can_page_sec_1 What does it do?
+This module aims to provide G.168-2002 compliant echo cancellation, to remove
+electrical echoes (e.g. from 2-4 wire hybrids) from voice calls.
+
+\section echo_can_page_sec_2 How does it work?
+The heart of the echo cancellor is FIR filter. This is adapted to match the echo
+impulse response of the telephone line. It must be long enough to adequately cover
+the duration of that impulse response. The signal transmitted to the telephone line
+is passed through the FIR filter. Once the FIR is properly adapted, the resulting
+output is an estimate of the echo signal received from the line. This is subtracted
+from the received signal. The result is an estimate of the signal which originated
+at the far end of the line, free from echos of our own transmitted signal. 
+
+The least mean squares (LMS) algorithm is attributed to Widrow and Hoff, and was
+introduced in 1960. It is the commonest form of filter adaption used in things
+like modem line equalisers and line echo cancellers. There it works very well.
+However, it only works well for signals of constant amplitude. It works very poorly
+for things like speech echo cancellation, where the signal level varies widely.
+This is quite easy to fix. If the signal level is normalised - similar to applying
+AGC - LMS can work as well for a signal of varying amplitude as it does for a modem
+signal. This normalised least mean squares (NLMS) algorithm is the commonest one used
+for speech echo cancellation. Many other algorithms exist - e.g. RLS (essentially
+the same as Kalman filtering), FAP, etc. Some perform significantly better than NLMS.
+However, factors such as computational complexity and patents favour the use of NLMS.
+
+A simple refinement to NLMS can improve its performance with speech. NLMS tends
+to adapt best to the strongest parts of a signal. If the signal is white noise,
+the NLMS algorithm works very well. However, speech has more low frequency than
+high frequency content. Pre-whitening (i.e. filtering the signal to flatten
+its spectrum) the echo signal improves the adapt rate for speech, and ensures the
+final residual signal is not heavily biased towards high frequencies. A very low
+complexity filter is adequate for this, so pre-whitening adds little to the
+compute requirements of the echo canceller.
+
+An FIR filter adapted using pre-whitened NLMS performs well, provided certain
+conditions are met: 
+
+    - The transmitted signal has poor self-correlation.
+    - There is no signal being generated within the environment being cancelled.
+
+The difficulty is that neither of these can be guaranteed.
+
+If the adaption is performed while transmitting noise (or something fairly noise
+like, such as voice) the adaption works very well. If the adaption is performed
+while transmitting something highly correlative (typically narrow band energy
+such as signalling tones or DTMF), the adaption can go seriously wrong. The reason
+is there is only one solution for the adaption on a near random signal - the impulse
+response of the line. For a repetitive signal, there are any number of solutions
+which converge the adaption, and nothing guides the adaption to choose the generalised
+one. Allowing an untrained canceller to converge on this kind of narrowband
+energy probably a good thing, since at least it cancels the tones. Allowing a well
+converged canceller to continue converging on such energy is just a way to ruin
+its generalised adaption. A narrowband detector is needed, so adapation can be
+suspended at appropriate times.
+
+The adaption process is based on trying to eliminate the received signal. When
+there is any signal from within the environment being cancelled it may upset the
+adaption process. Similarly, if the signal we are transmitting is small, noise
+may dominate and disturb the adaption process. If we can ensure that the
+adaption is only performed when we are transmitting a significant signal level,
+and the environment is not, things will be OK. Clearly, it is easy to tell when
+we are sending a significant signal. Telling, if the environment is generating a
+significant signal, and doing it with sufficient speed that the adaption will
+not have diverged too much more we stop it, is a little harder. 
+
+The key problem in detecting when the environment is sourcing significant energy
+is that we must do this very quickly. Given a reasonably long sample of the
+received signal, there are a number of strategies which may be used to assess
+whether that signal contains a strong far end component. However, by the time
+that assessment is complete the far end signal will have already caused major
+mis-convergence in the adaption process. An assessment algorithm is needed which
+produces a fairly accurate result from a very short burst of far end energy. 
+
+\section echo_can_page_sec_3 How do I use it?
+The echo cancellor processes both the transmit and receive streams sample by
+sample. The processing function is not declared inline. Unfortunately,
+cancellation requires many operations per sample, so the call overhead is only a
+minor burden. 
+*/
+
+#include "fir.h"
+
+/* Mask bits for the adaption mode */
+enum
+{
+    ECHO_CAN_USE_ADAPTION = 0x01,
+    ECHO_CAN_USE_NLP = 0x02,
+    ECHO_CAN_USE_CNG = 0x04,
+    ECHO_CAN_USE_CLIP = 0x08,
+    ECHO_CAN_USE_SUPPRESSOR = 0x10,
+    ECHO_CAN_USE_TX_HPF = 0x20,
+    ECHO_CAN_USE_RX_HPF = 0x40,
+    ECHO_CAN_DISABLE = 0x80,
+};
+
+/*!
+    G.168 echo canceller descriptor. This defines the working state for a line
+    echo canceller.
+*/
+typedef struct
+{
+    int tx_power[4];
+    int rx_power[3];
+    int clean_rx_power;
+
+    int rx_power_threshold;
+    int nonupdate_dwell;
+
+    int curr_pos;
+	
+    int taps;
+    int tap_mask;
+    int adaption_mode;
+    
+    int32_t supp_test1;
+    int32_t supp_test2;
+    int32_t supp1;
+    int32_t supp2;
+    int vad;
+    int cng;
+
+    int16_t geigel_max;
+    int geigel_lag;
+    int dtd_onset;
+    int tap_set;
+    int tap_rotate_counter;
+
+    int32_t latest_correction;  /* Indication of the magnitude of the latest
+                                   adaption, or a code to indicate why adaption
+                                   was skipped, for test purposes */
+    int32_t last_acf[28];
+    int narrowband_count;
+    int narrowband_score;
+
+    fir16_state_t fir_state;
+    /*! Echo FIR taps (16 bit version) */
+    int16_t *fir_taps16[4];
+    /*! Echo FIR taps (32 bit version) */
+    int32_t *fir_taps32;
+
+    /* DC and near DC blocking filter states */
+    int32_t tx_hpf[2];
+    int32_t rx_hpf[2];
+   
+    /* Parameters for the optional Hoth noise generator */
+    int cng_level;
+    int cng_rndnum;
+    int cng_filter;
+    
+    /* Snapshot sample of coeffs used for development */
+    int16_t *snapshot;       
+} echo_can_state_t;
+
+#if defined(__cplusplus)
+extern "C"
+{
+#endif
+
+/*! Create a voice echo canceller context.
+    \param len The length of the canceller, in samples.
+    \return The new canceller context, or NULL if the canceller could not be created.
+*/
+echo_can_state_t *echo_can_create(int len, int adaption_mode);
+
+/*! Free a voice echo canceller context.
+    \param ec The echo canceller context.
+*/
+void echo_can_free(echo_can_state_t *ec);
+
+/*! Flush (reinitialise) a voice echo canceller context.
+    \param ec The echo canceller context.
+*/
+void echo_can_flush(echo_can_state_t *ec);
+
+/*! Set the adaption mode of a voice echo canceller context.
+    \param ec The echo canceller context.
+    \param adaption_mode The mode.
+*/
+void echo_can_adaption_mode(echo_can_state_t *ec, int adaption_mode);
+
+/*! Process a sample through a voice echo canceller.
+    \param ec The echo canceller context.
+    \param tx The transmitted audio sample.
+    \param rx The received audio sample.
+    \return The clean (echo cancelled) received sample.
+*/
+int16_t echo_can_update(echo_can_state_t *ec, int16_t tx, int16_t rx);
+
+/*! Process to high pass filter the tx signal.
+    \param ec The echo canceller context.
+    \param tx The transmitted auio sample.
+    \return The HP filtered transmit sample, send this to your D/A.
+*/
+int16_t echo_can_hpf_tx(echo_can_state_t *ec, int16_t tx);
+
+void echo_can_snapshot(echo_can_state_t *ec);
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/spandsp/fax.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/spandsp/fax.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,132 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * fax.h - definitions for analogue line ITU T.30 fax processing
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2005 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: fax.h,v 1.35 2008/08/13 00:11:30 steveu Exp $
+ */
+
+/*! \file */
+
+#if !defined(_SPANDSP_FAX_H_)
+#define _SPANDSP_FAX_H_
+
+/*! \page fax_page FAX over analogue modem handling
+
+\section fax_page_sec_1 What does it do?
+
+\section fax_page_sec_2 How does it work?
+*/
+
+typedef struct fax_state_s fax_state_t;
+
+/*!
+    Analogue line T.30 FAX channel descriptor. This defines the state of a single working
+    instance of an analogue line soft-FAX machine.
+*/
+struct fax_state_s
+{
+    /*! \brief The T.30 back-end */
+    t30_state_t t30;
+    
+    /*! \brief The analogue modem front-end */
+    fax_modems_state_t modems;
+
+    /*! \brief Error and flow logging control */
+    logging_state_t logging;
+};
+
+#if defined(__cplusplus)
+extern "C"
+{
+#endif
+
+/*! Apply T.30 receive processing to a block of audio samples.
+    \brief Apply T.30 receive processing to a block of audio samples.
+    \param s The FAX context.
+    \param amp The audio sample buffer.
+    \param len The number of samples in the buffer.
+    \return The number of samples unprocessed. This should only be non-zero if
+            the software has reached the end of the FAX call.
+*/
+int fax_rx(fax_state_t *s, int16_t *amp, int len);
+
+/*! Apply T.30 transmit processing to generate a block of audio samples.
+    \brief Apply T.30 transmit processing to generate a block of audio samples.
+    \param s The FAX context.
+    \param amp The audio sample buffer.
+    \param max_len The number of samples to be generated.
+    \return The number of samples actually generated. This will be zero when
+            there is nothing to send.
+*/
+int fax_tx(fax_state_t *s, int16_t *amp, int max_len);
+
+/*! Select whether silent audio will be sent when FAX transmit is idle.
+    \brief Select whether silent audio will be sent when FAX transmit is idle.
+    \param s The FAX context.
+    \param transmit_on_idle TRUE if silent audio should be output when the FAX transmitter is
+           idle. FALSE to transmit zero length audio when the FAX transmitter is idle. The default
+           behaviour is FALSE.
+*/
+void fax_set_transmit_on_idle(fax_state_t *s, int transmit_on_idle);
+
+/*! Select whether talker echo protection tone will be sent for the image modems.
+    \brief Select whether TEP will be sent for the image modems.
+    \param s The FAX context.
+    \param use_tep TRUE if TEP should be sent.
+*/
+void fax_set_tep_mode(fax_state_t *s, int use_tep);
+
+/*! Get a pointer to the T.30 engine associated with a FAX context.
+    \brief Get a pointer to the T.30 engine associated with a FAX context.
+    \param s The FAX context.
+    \return A pointer to the T.30 context, or NULL.
+*/
+t30_state_t *fax_get_t30_state(fax_state_t *s);
+
+/*! Initialise a FAX context.
+    \brief Initialise a FAX context.
+    \param s The FAX context.
+    \param calling_party TRUE if the context is for a calling party. FALSE if the
+           context is for an answering party.
+    \return A pointer to the FAX context, or NULL if there was a problem.
+*/
+fax_state_t *fax_init(fax_state_t *s, int calling_party);
+
+/*! Release a FAX context.
+    \brief Release a FAX context.
+    \param s The FAX context.
+    \return 0 for OK, else -1. */
+int fax_release(fax_state_t *s);
+
+/*! Free a FAX context.
+    \brief Free a FAX context.
+    \param s The FAX context.
+    \return 0 for OK, else -1. */
+int fax_free(fax_state_t *s);
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/spandsp/fax_modems.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/spandsp/fax_modems.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,138 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * fax_modems.h - definitions for the analogue modem set for fax processing
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2008 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: fax_modems.h,v 1.5 2008/08/13 00:11:30 steveu Exp $
+ */
+
+/*! \file */
+
+#if !defined(_SPANDSP_FAX_MODEMS_H_)
+#define _SPANDSP_FAX_MODEMS_H_
+
+/*!
+    The set of modems needed for FAX, plus the auxilliary stuff, like tone generation.
+*/
+typedef struct
+{
+    /*! TRUE is talker echo protection should be sent for the image modems */
+    int use_tep;
+
+    /*! If TRUE, transmit silence when there is nothing else to transmit. If FALSE return only
+        the actual generated audio. Note that this only affects untimed silences. Timed silences
+        (e.g. the 75ms silence between V.21 and a high speed modem) will alway be transmitted as
+        silent audio. */
+    int transmit_on_idle;
+
+    /*! \brief An HDLC context used when transmitting HDLC messages. */
+    hdlc_tx_state_t hdlc_tx;
+    /*! \brief An HDLC context used when receiving HDLC messages. */
+    hdlc_rx_state_t hdlc_rx;
+    /*! \brief A V.21 FSK modem context used when transmitting HDLC over V.21
+               messages. */
+    fsk_tx_state_t v21_tx;
+    /*! \brief A V.21 FSK modem context used when receiving HDLC over V.21
+               messages. */
+    fsk_rx_state_t v21_rx;
+    /*! \brief A V.17 modem context used when sending FAXes at 7200bps, 9600bps
+               12000bps or 14400bps */
+    v17_tx_state_t v17_tx;
+    /*! \brief A V.29 modem context used when receiving FAXes at 7200bps, 9600bps
+               12000bps or 14400bps */
+    v17_rx_state_t v17_rx;
+    /*! \brief A V.29 modem context used when sending FAXes at 7200bps or
+               9600bps */
+    v29_tx_state_t v29_tx;
+    /*! \brief A V.29 modem context used when receiving FAXes at 7200bps or
+               9600bps */
+    v29_rx_state_t v29_rx;
+    /*! \brief A V.27ter modem context used when sending FAXes at 2400bps or
+               4800bps */
+    v27ter_tx_state_t v27ter_tx;
+    /*! \brief A V.27ter modem context used when receiving FAXes at 2400bps or
+               4800bps */
+    v27ter_rx_state_t v27ter_rx;
+    /*! \brief Used to insert timed silences. */
+    silence_gen_state_t silence_gen;
+    /*! \brief CED or CNG generator */
+    modem_connect_tones_tx_state_t connect_tx;
+    /*! \brief CED or CNG detector */
+    modem_connect_tones_rx_state_t connect_rx;
+    /*! \brief */
+    dc_restore_state_t dc_restore;
+
+    /*! \brief The currently select receiver type */
+    int current_rx_type;
+    /*! \brief The currently select transmitter type */
+    int current_tx_type;
+
+    /*! \brief TRUE if a carrier is present. Otherwise FALSE. */
+    int rx_signal_present;
+    /*! \brief TRUE if a modem has trained correctly. */
+    int rx_trained;
+
+    /*! The current receive signal handler */
+    span_rx_handler_t *rx_handler;
+    void *rx_user_data;
+
+    /*! The current transmit signal handler */
+    span_tx_handler_t *tx_handler;
+    void *tx_user_data;
+
+    /*! The next transmit signal handler, for two stage transmit operations.
+        E.g. a short silence followed by a modem signal. */
+    span_tx_handler_t *next_tx_handler;
+    void *next_tx_user_data;
+
+    /*! The current bit rate of the transmitter. */
+    int tx_bit_rate;
+    /*! The current bit rate of the receiver. */
+    int rx_bit_rate;
+
+    /*! If TRUE, transmission is in progress */
+    int transmit;
+    /*! \brief Audio logging file handle for received audio. */
+    int audio_rx_log;
+    /*! \brief Audio logging file handle for transmitted audio. */
+    int audio_tx_log;
+    /*! \brief Error and flow logging control */
+    logging_state_t logging;
+} fax_modems_state_t;
+
+#if defined(__cplusplus)
+extern "C"
+{
+#endif
+
+/* N.B. the following are currently a work in progress */
+int fax_modems_v17_v21_rx(void *user_data, const int16_t amp[], int len);
+int fax_modems_v27ter_v21_rx(void *user_data, const int16_t amp[], int len);
+int fax_modems_v29_v21_rx(void *user_data, const int16_t amp[], int len);
+fax_modems_state_t *fax_modems_init(fax_modems_state_t *s, void *user_data);
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/spandsp/fir.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/spandsp/fir.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,304 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * fir.h - General telephony FIR routines
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2002 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: fir.h,v 1.13 2008/04/17 14:27:00 steveu Exp $
+ */
+
+/*! \page fir_page FIR filtering
+\section fir_page_sec_1 What does it do?
+???.
+
+\section fir_page_sec_2 How does it work?
+???.
+*/
+
+#if !defined(_SPANDSP_FIR_H_)
+#define _SPANDSP_FIR_H_
+
+#if defined(USE_MMX)  ||  defined(USE_SSE2)
+#include "mmx.h"
+#endif
+
+/*!
+    16 bit integer FIR descriptor. This defines the working state for a single
+    instance of an FIR filter using 16 bit integer coefficients.
+*/
+typedef struct
+{
+    int taps;
+    int curr_pos;
+    const int16_t *coeffs;
+    int16_t *history;
+} fir16_state_t;
+
+/*!
+    32 bit integer FIR descriptor. This defines the working state for a single
+    instance of an FIR filter using 32 bit integer coefficients, and filtering
+    16 bit integer data.
+*/
+typedef struct
+{
+    int taps;
+    int curr_pos;
+    const int32_t *coeffs;
+    int16_t *history;
+} fir32_state_t;
+
+/*!
+    Floating point FIR descriptor. This defines the working state for a single
+    instance of an FIR filter using floating point coefficients and data.
+*/
+typedef struct
+{
+    int taps;
+    int curr_pos;
+    const float *coeffs;
+    float *history;
+} fir_float_state_t;
+
+#if defined(__cplusplus)
+extern "C"
+{
+#endif
+
+static __inline__ const int16_t *fir16_create(fir16_state_t *fir,
+                                              const int16_t *coeffs,
+                                              int taps)
+{
+    fir->taps = taps;
+    fir->curr_pos = taps - 1;
+    fir->coeffs = coeffs;
+#if defined(USE_MMX)  ||  defined(USE_SSE2)
+    if ((fir->history = malloc(2*taps*sizeof(int16_t))))
+        memset(fir->history, 0, 2*taps*sizeof(int16_t));
+#else
+    if ((fir->history = (int16_t *) malloc(taps*sizeof(int16_t))))
+        memset(fir->history, 0, taps*sizeof(int16_t));
+#endif
+    return fir->history;
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ void fir16_flush(fir16_state_t *fir)
+{
+#if defined(USE_MMX)  ||  defined(USE_SSE2)
+    memset(fir->history, 0, 2*fir->taps*sizeof(int16_t));
+#else
+    memset(fir->history, 0, fir->taps*sizeof(int16_t));
+#endif
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ void fir16_free(fir16_state_t *fir)
+{
+    free(fir->history);
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ int16_t fir16(fir16_state_t *fir, int16_t sample)
+{
+    int i;
+    int32_t y;
+#if defined(USE_MMX)
+    mmx_t *mmx_coeffs;
+    mmx_t *mmx_hist;
+
+    fir->history[fir->curr_pos] = sample;
+    fir->history[fir->curr_pos + fir->taps] = sample;
+
+    mmx_coeffs = (mmx_t *) fir->coeffs;
+    mmx_hist = (mmx_t *) &fir->history[fir->curr_pos];
+    i = fir->taps;
+    pxor_r2r(mm4, mm4);
+    /* 8 samples per iteration, so the filter must be a multiple of 8 long. */
+    while (i > 0)
+    {
+        movq_m2r(mmx_coeffs[0], mm0);
+        movq_m2r(mmx_coeffs[1], mm2);
+        movq_m2r(mmx_hist[0], mm1);
+        movq_m2r(mmx_hist[1], mm3);
+        mmx_coeffs += 2;
+        mmx_hist += 2;
+        pmaddwd_r2r(mm1, mm0);
+        pmaddwd_r2r(mm3, mm2);
+        paddd_r2r(mm0, mm4);
+        paddd_r2r(mm2, mm4);
+        i -= 8;
+    }
+    movq_r2r(mm4, mm0);
+    psrlq_i2r(32, mm0);
+    paddd_r2r(mm0, mm4);
+    movd_r2m(mm4, y);
+    emms();
+#elif defined(USE_SSE2)
+    xmm_t *xmm_coeffs;
+    xmm_t *xmm_hist;
+
+    fir->history[fir->curr_pos] = sample;
+    fir->history[fir->curr_pos + fir->taps] = sample;
+
+    xmm_coeffs = (xmm_t *) fir->coeffs;
+    xmm_hist = (xmm_t *) &fir->history[fir->curr_pos];
+    i = fir->taps;
+    pxor_r2r(xmm4, xmm4);
+    /* 16 samples per iteration, so the filter must be a multiple of 16 long. */
+    while (i > 0)
+    {
+        movdqu_m2r(xmm_coeffs[0], xmm0);
+        movdqu_m2r(xmm_coeffs[1], xmm2);
+        movdqu_m2r(xmm_hist[0], xmm1);
+        movdqu_m2r(xmm_hist[1], xmm3);
+        xmm_coeffs += 2;
+        xmm_hist += 2;
+        pmaddwd_r2r(xmm1, xmm0);
+        pmaddwd_r2r(xmm3, xmm2);
+        paddd_r2r(xmm0, xmm4);
+        paddd_r2r(xmm2, xmm4);
+        i -= 16;
+    }
+    movdqa_r2r(xmm4, xmm0);
+    psrldq_i2r(8, xmm0);
+    paddd_r2r(xmm0, xmm4);
+    movdqa_r2r(xmm4, xmm0);
+    psrldq_i2r(4, xmm0);
+    paddd_r2r(xmm0, xmm4);
+    movd_r2m(xmm4, y);
+#else
+    int offset1;
+    int offset2;
+
+    fir->history[fir->curr_pos] = sample;
+
+    offset2 = fir->curr_pos;
+    offset1 = fir->taps - offset2;
+    y = 0;
+    for (i = fir->taps - 1;  i >= offset1;  i--)
+        y += fir->coeffs[i]*fir->history[i - offset1];
+    for (  ;  i >= 0;  i--)
+        y += fir->coeffs[i]*fir->history[i + offset2];
+#endif
+    if (fir->curr_pos <= 0)
+    	fir->curr_pos = fir->taps;
+    fir->curr_pos--;
+    return (int16_t) (y >> 15);
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ const int16_t *fir32_create(fir32_state_t *fir,
+                                              const int32_t *coeffs,
+                                              int taps)
+{
+    fir->taps = taps;
+    fir->curr_pos = taps - 1;
+    fir->coeffs = coeffs;
+    fir->history = (int16_t *) malloc(taps*sizeof(int16_t));
+    if (fir->history)
+    	memset(fir->history, '\0', taps*sizeof(int16_t));
+    return fir->history;
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ void fir32_flush(fir32_state_t *fir)
+{
+    memset(fir->history, 0, fir->taps*sizeof(int16_t));
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ void fir32_free(fir32_state_t *fir)
+{
+    free(fir->history);
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ int16_t fir32(fir32_state_t *fir, int16_t sample)
+{
+    int i;
+    int32_t y;
+    int offset1;
+    int offset2;
+
+    fir->history[fir->curr_pos] = sample;
+    offset2 = fir->curr_pos;
+    offset1 = fir->taps - offset2;
+    y = 0;
+    for (i = fir->taps - 1;  i >= offset1;  i--)
+        y += fir->coeffs[i]*fir->history[i - offset1];
+    for (  ;  i >= 0;  i--)
+        y += fir->coeffs[i]*fir->history[i + offset2];
+    if (fir->curr_pos <= 0)
+    	fir->curr_pos = fir->taps;
+    fir->curr_pos--;
+    return (int16_t) (y >> 15);
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ const float *fir_float_create(fir_float_state_t *fir,
+                                                const float *coeffs,
+    	    	    	                        int taps)
+{
+    fir->taps = taps;
+    fir->curr_pos = taps - 1;
+    fir->coeffs = coeffs;
+    fir->history = (float *) malloc(taps*sizeof(float));
+    if (fir->history)
+        memset(fir->history, '\0', taps*sizeof(float));
+    return fir->history;
+}
+/*- End of function --------------------------------------------------------*/
+    
+static __inline__ void fir_float_free(fir_float_state_t *fir)
+{
+    free(fir->history);
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ int16_t fir_float(fir_float_state_t *fir, int16_t sample)
+{
+    int i;
+    float y;
+    int offset1;
+    int offset2;
+
+    fir->history[fir->curr_pos] = sample;
+
+    offset2 = fir->curr_pos;
+    offset1 = fir->taps - offset2;
+    y = 0;
+    for (i = fir->taps - 1;  i >= offset1;  i--)
+        y += fir->coeffs[i]*fir->history[i - offset1];
+    for (  ;  i >= 0;  i--)
+        y += fir->coeffs[i]*fir->history[i + offset2];
+    if (fir->curr_pos <= 0)
+    	fir->curr_pos = fir->taps;
+    fir->curr_pos--;
+    return  (int16_t) y;
+}
+/*- End of function --------------------------------------------------------*/
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/spandsp/fsk.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/spandsp/fsk.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,278 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * fsk.h - FSK modem transmit and receive parts
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2003 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: fsk.h,v 1.30 2008/07/16 14:23:48 steveu Exp $
+ */
+
+/*! \file */
+
+/*! \page fsk_page FSK modems
+\section fsk_page_sec_1 What does it do?
+Most of the oldest telephony modems use incoherent FSK modulation. This module can
+be used to implement both the transmit and receive sides of a number of these
+modems. There are integrated definitions for: 
+
+ - V.21
+ - V.23
+ - Bell 103
+ - Bell 202
+ - Weitbrecht (Used for TDD - Telecoms Device for the Deaf)
+
+The audio output or input is a stream of 16 bit samples, at 8000 samples/second.
+The transmit and receive sides can be used independantly. 
+
+\section fsk_page_sec_2 The transmitter
+
+The FSK transmitter uses a DDS generator to synthesise the waveform. This
+naturally produces phase coherent transitions, as the phase update rate is
+switched, producing a clean spectrum. The symbols are not generally an integer
+number of samples long. However, the symbol time for the fastest data rate
+generally used (1200bps) is more than 7 samples long. The jitter resulting from
+switching at the nearest sample is, therefore, acceptable. No interpolation is
+used. 
+
+\section fsk_page_sec_3 The receiver
+
+The FSK receiver uses a quadrature correlation technique to demodulate the
+signal. Two DDS quadrature oscillators are used. The incoming signal is
+correlated with the oscillator signals over a period of one symbol. The
+oscillator giving the highest net correlation from its I and Q outputs is the
+one that matches the frequency being transmitted during the correlation
+interval. Because the transmission is totally asynchronous, the demodulation
+process must run sample by sample to find the symbol transitions. The
+correlation is performed on a sliding window basis, so the computational load of
+demodulating sample by sample is not great. 
+
+Two modes of symbol synchronisation are provided:
+
+    - In synchronous mode, symbol transitions are smoothed, to track their true
+      position in the prescence of high timing jitter. This provides the most
+      reliable symbol recovery in poor signal to noise conditions. However, it
+      takes a little time to settle, so it not really suitable for data streams
+      which must start up instantaneously (e.g. the TDD systems used by hearing
+      impaired people).
+
+    - In asynchronous mode each transition is taken at face value, with no temporal
+      smoothing. There is no settling time for this mode, but when the signal to
+      noise ratio is very poor it does not perform as well as the synchronous mode.
+*/
+
+#if !defined(_SPANDSP_FSK_H_)
+#define _SPANDSP_FSK_H_
+
+/*!
+    FSK modem specification. This defines the frequencies, signal levels and
+    baud rate (== bit rate for simple FSK) for a single channel of an FSK modem.
+*/
+typedef struct
+{
+    /*! Short text name for the modem. */
+    const char *name;
+    /*! The frequency of the zero bit state, in Hz */
+    int freq_zero;
+    /*! The frequency of the one bit state, in Hz */
+    int freq_one;
+    /*! The transmit power level, in dBm0 */
+    int tx_level;
+    /*! The minimum acceptable receive power level, in dBm0 */
+    int min_level;
+    /*! The bit rate of the modem, in units of 1/100th bps */
+    int baud_rate;
+} fsk_spec_t;
+
+/* Predefined FSK modem channels */
+enum
+{
+    FSK_V21CH1 = 0,
+    FSK_V21CH2,
+    FSK_V23CH1,
+    FSK_V23CH2,
+    FSK_BELL103CH1,
+    FSK_BELL103CH2,
+    FSK_BELL202,
+    FSK_WEITBRECHT,     /* Used for TDD (Telecom Device for the Deaf) */
+};
+
+extern const fsk_spec_t preset_fsk_specs[];
+
+/*!
+    FSK modem transmit descriptor. This defines the state of a single working
+    instance of an FSK modem transmitter.
+*/
+typedef struct
+{
+    int baud_rate;
+    /*! \brief The callback function used to get the next bit to be transmitted. */
+    get_bit_func_t get_bit;
+    /*! \brief A user specified opaque pointer passed to the get_bit function. */
+    void *get_bit_user_data;
+
+    /*! \brief The callback function used to report modem status changes. */
+    modem_tx_status_func_t status_handler;
+    /*! \brief A user specified opaque pointer passed to the status function. */
+    void *status_user_data;
+
+    int32_t phase_rates[2];
+    int scaling;
+    int32_t current_phase_rate;
+    uint32_t phase_acc;
+    int baud_frac;
+    int baud_inc;
+    int shutdown;
+} fsk_tx_state_t;
+
+/* The longest window will probably be 106 for 75 baud */
+#define FSK_MAX_WINDOW_LEN 128
+
+/*!
+    FSK modem receive descriptor. This defines the state of a single working
+    instance of an FSK modem receiver.
+*/
+typedef struct
+{
+    int baud_rate;
+    int sync_mode;
+    /*! \brief The callback function used to put each bit received. */
+    put_bit_func_t put_bit;
+    /*! \brief A user specified opaque pointer passed to the put_bit routine. */
+    void *put_bit_user_data;
+
+    /*! \brief The callback function used to report modem status changes. */
+    modem_tx_status_func_t status_handler;
+    /*! \brief A user specified opaque pointer passed to the status function. */
+    void *status_user_data;
+
+    int32_t carrier_on_power;
+    int32_t carrier_off_power;
+    power_meter_t power;
+    /*! \brief The value of the last signal sample, using the a simple HPF for signal power estimation. */
+    int16_t last_sample;
+    /*! \brief >0 if a signal above the minimum is present. It may or may not be a V.29 signal. */
+    int signal_present;
+
+    int32_t phase_rate[2];
+    uint32_t phase_acc[2];
+
+    int correlation_span;
+
+    complexi32_t window[2][FSK_MAX_WINDOW_LEN];
+    complexi32_t dot[2];
+    int buf_ptr;
+
+    int baud_inc;
+    int baud_pll;
+    int lastbit;
+    int scaling_shift;
+} fsk_rx_state_t;
+
+#if defined(__cplusplus)
+extern "C"
+{
+#endif
+
+/*! Initialise an FSK modem transmit context.
+    \brief Initialise an FSK modem transmit context.
+    \param s The modem context.
+    \param spec The specification of the modem tones and rate.
+    \param get_bit The callback routine used to get the data to be transmitted.
+    \param user_data An opaque pointer.
+    \return A pointer to the modem context, or NULL if there was a problem. */
+fsk_tx_state_t *fsk_tx_init(fsk_tx_state_t *s,
+                            const fsk_spec_t *spec,
+                            get_bit_func_t get_bit,
+                            void *user_data);
+
+/*! Adjust an FSK modem transmit context's power output.
+    \brief Adjust an FSK modem transmit context's power output.
+    \param s The modem context.
+    \param power The power level, in dBm0 */
+void fsk_tx_power(fsk_tx_state_t *s, float power);
+
+void fsk_tx_set_get_bit(fsk_tx_state_t *s, get_bit_func_t get_bit, void *user_data);
+
+/*! Change the modem status report function associated with an FSK modem transmit context.
+    \brief Change the modem status report function associated with an FSK modem transmit context.
+    \param s The modem context.
+    \param handler The callback routine used to report modem status changes.
+    \param user_data An opaque pointer. */
+void fsk_tx_set_modem_status_handler(fsk_tx_state_t *s, modem_tx_status_func_t handler, void *user_data);
+
+/*! Generate a block of FSK modem audio samples.
+    \brief Generate a block of FSK modem audio samples.
+    \param s The modem context.
+    \param amp The audio sample buffer.
+    \param len The number of samples to be generated.
+    \return The number of samples actually generated.
+*/
+int fsk_tx(fsk_tx_state_t *s, int16_t *amp, int len);
+
+/*! Get the current received signal power.
+    \param s The modem context.
+    \return The signal power, in dBm0. */
+float fsk_rx_signal_power(fsk_rx_state_t *s);
+
+/*! Adjust an FSK modem receive context's carrier detect power threshold.
+    \brief Adjust an FSK modem receive context's carrier detect power threshold.
+    \param s The modem context.
+    \param cutoff The power level, in dBm0 */
+void fsk_rx_signal_cutoff(fsk_rx_state_t *s, float cutoff);
+
+/*! Initialise an FSK modem receive context.
+    \brief Initialise an FSK modem receive context.
+    \param s The modem context.
+    \param spec The specification of the modem tones and rate.
+    \param sync_mode TRUE for synchronous modem. FALSE for asynchronous mode.
+    \param put_bit The callback routine used to put the received data.
+    \param user_data An opaque pointer.
+    \return A pointer to the modem context, or NULL if there was a problem. */
+fsk_rx_state_t *fsk_rx_init(fsk_rx_state_t *s,
+                            const fsk_spec_t *spec,
+                            int sync_mode,
+                            put_bit_func_t put_bit,
+                            void *user_data);
+
+/*! Process a block of received FSK modem audio samples.
+    \brief Process a block of received FSK modem audio samples.
+    \param s The modem context.
+    \param amp The audio sample buffer.
+    \param len The number of samples in the buffer.
+    \return The number of samples unprocessed.
+*/
+int fsk_rx(fsk_rx_state_t *s, const int16_t *amp, int len);
+
+void fsk_rx_set_put_bit(fsk_rx_state_t *s, put_bit_func_t put_bit, void *user_data);
+
+/*! Change the modem status report function associated with an FSK modem receive context.
+    \brief Change the modem status report function associated with an FSK modem receive context.
+    \param s The modem context.
+    \param handler The callback routine used to report modem status changes.
+    \param user_data An opaque pointer. */
+void fsk_rx_set_modem_status_handler(fsk_rx_state_t *s, modem_rx_status_func_t handler, void *user_data);
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/spandsp/g168models.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/spandsp/g168models.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,340 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * g168models.h - line models for echo cancellation tests against the G.168
+ *  	          spec.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2001 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: g168models.h,v 1.9 2008/08/29 09:28:13 steveu Exp $
+ */
+
+/*! \file */
+
+#if !defined(_SPANDSP_G168MODELS_H_)
+#define _SPANDSP_G168MODELS_H_
+
+/*! \page g168_test_data_page The test data from the G.168 specification
+*/
+
+/*!
+    The line model from section D.2 of G.168.
+
+    These are the coefficients for the line simulation model defined in
+    section D.2 of G.168. It may be used with the fir32_xxx
+    routines to build a line simulator.
+*/
+const int32_t line_model_d2_coeffs[] =
+{
+      -436,   -829,  -2797,  -4208, -17968, -11215,  46150,  34480,
+    -10427,   9049,  -1309,  -6320,    390,  -8191,  -1751,  -6051,
+     -3796,  -4055,  -3948,  -2557,  -3372,  -1808,  -2259,  -1300, 
+     -1098,   -618,   -340,    -61,    323,    419,    745,    716,
+       946,    880,   1014,    976,   1033,   1091,   1053,   1042,
+       794,    831,    899,    716,    390,    313,    304,    304, 
+        73,   -119,   -109,   -176,   -359,   -407,   -512,   -580,
+      -704,   -618,   -685,   -791,   -772,   -820,   -839,   -724  
+};
+#define LINE_MODEL_D2_GAIN      1.39E-5f
+
+/*!
+    The line model from section D.3 of G.168.
+
+    These are the coefficients for the line simulation model defined in
+    section D.3 of G.168. It may be used with the fir32_xxx
+    routines to build a line simulator.
+*/
+const int32_t line_model_d3_coeffs[] =
+{
+      -381,    658,   1730,    -51,  -3511,  -1418,   7660,   8861,
+     -8106, -21370,  -5307,  23064,  24020,   1020, -12374, -16296,
+    -19524,  -7480,  13509,  17115,  13952,  13952,     97,  -9326,
+     -9046, -15208,  -9853,  -3858,  -1979,   6029,   5616,   7214,
+      6820,   3935,   3919,    921,   1316,   -693,   -759,  -1517,
+     -2176,  -2028,  -2654,  -1814,  -2077,  -1468,  -1221,   -842,
+      -463,   -298,    -68,     64,    493,    723,    789,    954,
+       756,    839,    872,   1020,    789,    822,    558,    658,
+       476,    377,    377,    262,     97,    -68,   -183,   -232,
+      -331,   -347,   -430,   -314,   -430,   -463,   -463,   -414,
+      -381,   -479,   -479,   -512,   -479,   -397,   -430,   -397,
+      -298,   -265,   -249,   -216,   -249,   -265,   -166,   -232
+};
+#define LINE_MODEL_D3_GAIN      1.44E-5f
+
+/*!
+    The line model from section D.4 of G.168.
+
+    These are the coefficients for the line simulation model defined in
+    section D.4 of G.168. It may be used with the fir32_xxx
+    routines to build a line simulator.
+*/
+const int32_t line_model_d4_coeffs[] =
+{
+      -448,   -436,   2230,   2448,  -4178,  -7050,   5846,  18581,
+      2322, -26261, -16249,  21637,  25649,  -2267, -10311,  -4693,
+    -12690,  -7428,  14164,  13467,   4438,   8627,    456, -11879,
+     -6352,  -5104,  -7496,   3271,   6566,   4277,  11131,   7562,
+      1475,   3728,  -3525,  -7301,  -3101,  -9269,  -6146,  -2553,
+     -6272,    811,    124,    788,   5147,   2172,   5387,   4598,
+      3535,   4004,   2311,   2150,   1017,    330,   -139,   -573,
+     -1100,  -1157,  -1180,  -1455,  -1123,  -1386,  -1123,  -1066,
+     -1020,  -1100,  -1008,  -1077,  -1088,   -917,   -917,   -963,
+      -814,   -871,   -734,   -642,   -562,   -356,   -379,   -345,
+      -230,   -233,   -333,   -356,   -390,   -310,   -265,   -368,
+      -310,   -310,   -390,   -482,   -459,   -482,   -551,   -573
+};
+#define LINE_MODEL_D4_GAIN      1.52E-5f
+
+/*!
+    The line model from section D.5 of G.168.
+
+    These are the coefficients for the line simulation model defined in
+    section D.5 of G.168. It may be used with the fir32_xxx
+    routines to build a line simulator.
+*/
+const int32_t line_model_d5_coeffs[] =
+{
+       160,    312,   -241,   -415,    897,    908,  -1326,  -1499,
+      2405,   3347,  -3624,  -7733,   4041,  14484,  -1477, -21739,
+     -4470,  25356,  11458, -19696, -11800,   5766,    789,   6633,
+     14624,  -6975, -17156,   -187,    149,   1515,  14907,   4345,
+     -7128,  -2757, -10185,  -7083,   6850,   3944,   6969,   8694,
+     -4068,  -3852,  -5793,  -9371,    453,   1060,   3965,   9463, 
+      2393,   2784,   -892,  -7366,  -3376,  -5847,  -2399,   3011,
+      1537,   6623,   4205,   1602,   1592,  -4752,  -3646,  -5207,
+     -5577,   -501,  -1174,   4041,   5647,   4628,   7252,   2123, 
+      2654,   -881,  -4113,  -3244,  -7289,  -3830,  -4600,  -2508,
+       431,   -144,   4184,   2372,   4617,   3576,   2382,   2839,
+      -404,    539,  -1803,  -1401,  -1705,  -2269,   -783,  -1608, 
+      -220,   -306,    257,    615,    225,    561,      8,    344,
+       127,    -57,    182,     41,    203,   -111,     95,    -79,
+        30,     84,    -13,    -68,   -241,    -68,    -24,     19, 
+       -57,    -24,     30,    -68,     84,   -155,    -68,     19
+};
+#define LINE_MODEL_D5_GAIN      1.77E-5f
+
+/*!
+    The line model from section D.6 of G.168.
+
+    These are the coefficients for the line simulation model defined in
+    section D.6 of G.168. It may be used with the fir32_xxx
+    routines to build a line simulator.
+*/
+const int32_t line_model_d6_coeffs[] =
+{
+       293,    268,    475,    460,    517,    704,    581,    879,
+       573,    896,    604,    787,    561,    538,    440,     97,
+       265,   -385,     20,   -938,   -523,  -1438,  -1134,  -1887,
+     -1727,  -1698,  -4266, -22548, -43424,   2743,  25897,   7380,
+     21499,  11983,  10400,  11667,   3889,   7241,    925,   2018,
+      -821,  -2068,  -2236,  -4283,  -3406,  -5022,  -4039,  -4842,
+     -4104,  -4089,  -3582,  -2978,  -2734,  -1805,  -1608,   -645,
+      -495,    279,    471,    947,   1186,   1438,   1669,   1640,
+      1901,   1687,   1803,   1543,   1566,   1342,   1163,    963, 
+       733,    665,    323,    221,    -14,   -107,   -279,   -379,
+      -468,   -513,   -473,   -588,   -612,   -652,   -616,   -566,
+      -515,   -485,   -404,   -344,   -290,   -202,   -180,   -123
+};
+#define LINE_MODEL_D6_GAIN      9.33E-6f
+
+/*!
+    The line model from section D.7 of G.168.
+
+    These are the coefficients for the line simulation model defined in
+    section D.8 of G.168. It may be used with the fir32_xxx
+    routines to build a line simulator.
+*/
+const int32_t line_model_d7_coeffs[] =
+{
+        29,    109,    -83,    198,   -294,   -135,   -415,   -202,
+      -444,   -337,   -313,   -450,   -105,   -503,    145,   -490,
+       267,   -231,    340,     77,    343,    783,    158,   1341,
+       195,   1798,    344,   1845,    629,   1604,   1182,    940,
+      5163,  19522,   8421, -50953,  -9043,  18046, -13553,  13336,
+     -3471,   -107,   1788,  -7409,   2469,  -7994,    490,  -3860,
+      -837,    490,   -636,   3682,   1141,   5019,   2635,   5025,
+      3946,   4414,   4026,   3005,   3380,   1616,   2007,    158,
+       388,  -1198,  -1117,  -2134,  -2547,  -2589,  -3310,  -2778,
+     -3427,  -2779,  -3116,  -2502,  -2399,  -1956,  -1539,  -1239,
+      -570,   -377,    251,    331,    964,   1177,   1449,   1564, 
+      1724,   1871,   1767,   1802,   1630,   1632,   1379,   1271,
+      1063,    856,    711,    482,    289,     54,   -137,   -321,
+      -490,   -638,   -764,   -836,   -800,   -859,   -838,   -837,
+      -834,   -740,   -673,   -581,   -493,   -436,   -327,   -201
+};
+#define LINE_MODEL_D7_GAIN      1.51E-5f
+
+/*!
+    The line model from section D.8 of G.168.
+
+    These are the coefficients for the line simulation model defined in
+    section D.8 of G.168. It may be used with the fir32_xxx
+    routines to build a line simulator.
+*/
+const int32_t line_model_d8_coeffs[] =
+{
+       258,   -111,    337,   -319,    347,   -434,    192,   -450,
+      -108,   -343,   -596,   -177,  -1187,    -52,  -1781,   -147,
+     -1959,   -326,  -1601,  -1389, -13620,   -720,  33818, -10683,
+     -6742,  12489,  -9862,   8950,  -1574,    758,   3526,  -3118,
+      2421,  -8966,  -4901,  11385,  18072, -14410,  -7473,  19836,
+    -16854,  -3115,   9483, -17799,   7399,  -4342,  -7415,   7929,
+    -10726,   6239,  -2526,  -1317,   5345,  -4565,   6868,  -2195,
+      3425,   1969,   -109,   3963,  -1275,   3087,   -892,   1239,
+         2,   -427,    596,  -1184,    551,  -1244,    141,   -743, 
+      -415,   -372,   -769,   -183,   -785,   -270,   -659,   -377,
+      -523,   -325,   -245,   -255,    -60,     35,    218,    149,
+       340,    233,    365,    303,    251,    230,    209,    179
+};
+#define LINE_MODEL_D8_GAIN      2.33E-5f
+
+/*!
+    The line model from section D.9 of G.168.
+
+    These are the coefficients for the line simulation model defined in
+    section D.9 of G.168. It may be used with the fir32_xxx
+    routines to build a line simulator.
+*/
+const int32_t line_model_d9_coeffs[] =
+{
+        80,     31,      4,     42,     42,    -61,    -81,    -64,
+       121,   -102,    -26,   1002,  -9250, -22562,  39321,  35681,
+    -35289,  25312,  -1457,   -229,  15659,  -6786,  16791,   3860,
+      2239, -28730, -11885,  33871,   -176, -16421,  18173,  -9669,
+    -10163,   9941, -19365,   3592,  -5907, -10257,   5336, -12933,
+      4348,  -4802,  -1791,   3035,  -4433,   5553,  -2596,   3992,
+      1255,   1450,   4079,    324,   4340,   1059,   3083,   1917,
+      1756,   2478,   1027,   1871,    845,   1284,    813,    806,
+       869,    471,    646,    438,    449,    432,    473,    394,
+       452,    538,    717,    723,    850,    756,    753,    899,
+       555,    669,    619,    500,    650,    615,    516,    492,
+       427,    291,    356,    147,    107,    -50,    -88,    -59,
+      -238,   -165,   -183
+};
+#define LINE_MODEL_D9_GAIN      1.33E-5f
+
+/*!
+    The filter coefficients for the bandpass filter specified for level measurements
+    in section 6.4.1.2.1 of G.168.
+*/
+const float level_measurement_bp_coeffs[] =
+{
+    0.0000,    0.0006,    0.0005,    0.0004,    0.0011,
+    0.0000,    0.0015,   -0.0003,    0.0012,   -0.0002,
+    0.0000,    0.0002,   -0.0020,    0.0005,   -0.0040,
+    0.0000,   -0.0047,   -0.0019,   -0.0033,   -0.0047,
+    0.0000,   -0.0068,    0.0036,   -0.0057,    0.0054,
+    0.0000,    0.0044,    0.0095,    0.0017,    0.0188,
+    0.0000,    0.0225,    0.0024,    0.0163,    0.0092,
+    0.0000,    0.0164,   -0.0210,    0.0161,   -0.0375,
+    0.0000,   -0.0406,   -0.0357,   -0.0267,   -0.0871,
+    0.0000,   -0.1420,    0.0289,   -0.1843,    0.0475,
+    0.8006,    0.0475,   -0.1843,    0.0289,   -0.1420,
+    0.0000,   -0.0871,   -0.0267,   -0.0357,   -0.0406,
+    0.0000,   -0.0375,    0.0161,   -0.0210,    0.0164,
+    0.0000,    0.0092,    0.0163,    0.0024,    0.0225,
+    0.0000,    0.0188,    0.0017,    0.0095,    0.0044,
+    0.0000,    0.0054,   -0.0057,    0.0036,   -0.0068,
+    0.0000,   -0.0047,   -0.0033,   -0.0019,   -0.0047,
+    0.0000,   -0.0040,    0.0005,   -0.0020,    0.0002,
+    0.0000,   -0.0002,    0.0012,   -0.0003,    0.0015,
+    0.0000,    0.0011,    0.0004,    0.0005,    0.0006,
+    0.0000
+};
+
+/*!
+    The composite source signal "voiced" section from section C.1 of G.168.
+*/
+const int css_c1[] =
+{
+     -155,   276,   517,   578,   491,   302,    86,  -103,
+     -207,  -198,    60,   190,   543,   948,  1362,  1741,
+     2043,  2276,  2422,  2500,  2552,  2595,  2655,  2758,
+     2896,  3060,  3224,  3370,  3500,  3569,  3603,  3603,
+     3595,  3586,  3595,  3638,  3724,  3819,  3922,  4000,
+     4043,  4034,  3974,  3862,  3724,  3577,  3439,  3336,
+     3267,  3224,  3198,  3172,  3129,  3043,  2914,  2750,
+     2560,  2353,  2155,  1991,  1853,  1750,  1672,  1603,
+     1534,  1440,  1310,  1146,   965,   776,   603,   448,
+      345,   276,   250,   250,   267,   267,   241,   190,
+      103,    -9,  -138,  -267,  -388,  -491,  -569,  -638,
+     -698,  -759,  -813,  -888,  -957, -1034, -1103, -1146,
+    -1181, -1190, -1198, -1215, -1259, -1327, -1457, -1629,
+    -1853, -2121, -2414, -2707, -3017, -3319, -3612, -3913,
+    -4224, -4560, -4922, -5301, -5715, -6137, -6560, -6948,
+    -7301, -7568, -7732, -7758, -7620, -7310, -6810, -6155,
+    -5344, -4439, -3474, -2508, -1595,  -802
+};
+
+/*!
+    The composite source signal "voiced" section from section C.3 of G.168.
+*/
+const int css_c3[] =
+{
+     -198,  -112,    -9,   103,   233,   388,   543,   724,
+      896,  1060,  1233,  1388,  1517,  1638,  1747,  1810,
+     1845,  1845,  1802,  1707,  1569,  1379,  1146,   871,
+      560,   233,  -121,  -491,  -871, -1250, -1638, -2043,
+    -2465, -2896, -3345, -3819, -4310, -4810, -5319, -5836,
+    -6353, -6853, -7353, -7836, -8292, -8715, -9077, -9370,
+    -9542, -9542, -9361, -8956, -8327, -7465, -6396, -5163,
+    -3827, -2448, -1103,   155,  1293,  2241,  3034,  3655,
+     4138,  4517,  4827,  5094,  5344,  5594,  5827,  6043,
+     6215,  6344,  6413,  6422,  6379,  6310,  6215,  6120,
+     6051,  6000,  5991,  5991,  6000,  6008,  5991,  5939,
+     5853,  5715,  5560,  5387,  5215,  5043,  4879,  4732,
+     4586,  4439,  4276,  4086,  3870,  3629,  3370,  3086,
+     2801,  2534,  2267,  2034,  1819,  1612,  1422,  1224,
+     1026,   819,   603,   388,   181,     9,  -181,  -328,
+     -448,  -543,  -629,  -707,  -784,  -871,  -948, -1026,
+    -1112, -1181, -1241, -1276, -1293, -1302, -1293, -1267,
+    -1250, -1233, -1224, -1224, -1224, -1224, -1215, -1198,
+    -1172, -1129, -1077, -1026,  -974,  -922,  -888,  -871,
+     -845,  -828,  -810,  -793,  -767,  -741,  -698,  -672,
+     -638,  -603,  -595,  -586,  -595,  -603,  -621,  -629,
+     -938,  -638,  -638,  -638,  -638,  -638,  -647,  -664,
+     -690,  -724,  -767,  -793,  -819,  -845,  -853,  -871,
+     -879,  -888,  -896,  -922,  -948,  -974, -1009, -1026,
+    -1052, -1069, -1077, -1069, -1060, -1060, -1052, -1043,
+    -1043, -1052, -1060, -1060, -1060, -1052, -1034, -1017,
+     -991,  -957,  -931,  -905,  -888,  -862,  -845,  -819,
+     -793,  -767,  -724,  -672,  -621,  -560,  -509,  -457,
+     -397,  -345,  -276,  -207,  -112
+};
+
+/*!
+    From section 6.4.2.7 of G.168 - Test No. 6 Non-divergence on narrow-band signals.
+    These tones and tone pairs are each applied for 5 seconds.
+*/
+const int tones_6_4_2_7[][2] =
+{
+    { 697,    0},
+    { 941,    0},
+    {1336,    0},
+    {1633,    0},
+    { 697, 1209},
+    { 770, 1336},
+    { 852, 1477},
+    { 941, 1633},
+    {   0,    0}
+};
+
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/spandsp/g711.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/spandsp/g711.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,297 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * g711.h - In line A-law and u-law conversion routines
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2001 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: g711.h,v 1.14 2008/05/02 17:57:32 steveu Exp $
+ */
+
+/*! \file */
+
+/*! \page g711_page A-law and mu-law handling
+Lookup tables for A-law and u-law look attractive, until you consider the impact
+on the CPU cache. If it causes a substantial area of your processor cache to get
+hit too often, cache sloshing will severely slow things down. The main reason
+these routines are slow in C, is the lack of direct access to the CPU's "find
+the first 1" instruction. A little in-line assembler fixes that, and the
+conversion routines can be faster than lookup tables, in most real world usage.
+A "find the first 1" instruction is available on most modern CPUs, and is a
+much underused feature. 
+
+If an assembly language method of bit searching is not available, these routines
+revert to a method that can be a little slow, so the cache thrashing might not
+seem so bad :(
+
+Feel free to submit patches to add fast "find the first 1" support for your own
+favourite processor.
+
+Look up tables are used for transcoding between A-law and u-law, since it is
+difficult to achieve the precise transcoding procedure laid down in the G.711
+specification by other means.
+*/
+
+#if !defined(_SPANDSP_G711_H_)
+#define _SPANDSP_G711_H_
+
+/* The usual values to use on idle channels, to emulate silence */
+#define G711_ALAW_IDLE_OCTET        0x5D
+#define G711_ULAW_IDLE_OCTET        0xFF
+
+enum
+{
+    G711_ALAW = 0,
+    G711_ULAW
+};
+
+typedef struct
+{
+    /*! One of the G.711_xxx options */
+    int mode;
+} g711_state_t;
+
+#if defined(__cplusplus)
+extern "C"
+{
+#endif
+
+/* N.B. It is tempting to use look-up tables for A-law and u-law conversion.
+ *      However, you should consider the cache footprint.
+ *
+ *      A 64K byte table for linear to x-law and a 512 byte table for x-law to
+ *      linear sound like peanuts these days, and shouldn't an array lookup be
+ *      real fast? No! When the cache sloshes as badly as this one will, a tight
+ *      calculation may be better. The messiest part is normally finding the
+ *      segment, but a little inline assembly can fix that on an i386, x86_64 and
+ *      many other modern processors.
+ */
+ 
+/*
+ * Mu-law is basically as follows:
+ *
+ *      Biased Linear Input Code        Compressed Code
+ *      ------------------------        ---------------
+ *      00000001wxyza                   000wxyz
+ *      0000001wxyzab                   001wxyz
+ *      000001wxyzabc                   010wxyz
+ *      00001wxyzabcd                   011wxyz
+ *      0001wxyzabcde                   100wxyz
+ *      001wxyzabcdef                   101wxyz
+ *      01wxyzabcdefg                   110wxyz
+ *      1wxyzabcdefgh                   111wxyz
+ *
+ * Each biased linear code has a leading 1 which identifies the segment
+ * number. The value of the segment number is equal to 7 minus the number
+ * of leading 0's. The quantization interval is directly available as the
+ * four bits wxyz.  * The trailing bits (a - h) are ignored.
+ *
+ * Ordinarily the complement of the resulting code word is used for
+ * transmission, and so the code word is complemented before it is returned.
+ *
+ * For further information see John C. Bellamy's Digital Telephony, 1982,
+ * John Wiley & Sons, pps 98-111 and 472-476.
+ */
+
+//#define ULAW_ZEROTRAP                 /* turn on the trap as per the MIL-STD */
+#define ULAW_BIAS        0x84           /* Bias for linear code. */
+
+/*! \brief Encode a linear sample to u-law
+    \param linear The sample to encode.
+    \return The u-law value.
+*/
+static __inline__ uint8_t linear_to_ulaw(int linear)
+{
+    uint8_t u_val;
+    int mask;
+    int seg;
+
+    /* Get the sign and the magnitude of the value. */
+    if (linear >= 0)
+    {
+        linear = ULAW_BIAS + linear;
+        mask = 0xFF;
+    }
+    else
+    {
+        linear = ULAW_BIAS - linear;
+        mask = 0x7F;
+    }
+
+    seg = top_bit(linear | 0xFF) - 7;
+
+    /*
+     * Combine the sign, segment, quantization bits,
+     * and complement the code word.
+     */
+    if (seg >= 8)
+        u_val = (uint8_t) (0x7F ^ mask);
+    else
+        u_val = (uint8_t) (((seg << 4) | ((linear >> (seg + 3)) & 0xF)) ^ mask);
+#ifdef ULAW_ZEROTRAP
+    /* Optional ITU trap */
+    if (u_val == 0)
+        u_val = 0x02;
+#endif
+    return  u_val;
+}
+/*- End of function --------------------------------------------------------*/
+
+/*! \brief Decode an u-law sample to a linear value.
+    \param ulaw The u-law sample to decode.
+    \return The linear value.
+*/
+static __inline__ int16_t ulaw_to_linear(uint8_t ulaw)
+{
+    int t;
+    
+    /* Complement to obtain normal u-law value. */
+    ulaw = ~ulaw;
+    /*
+     * Extract and bias the quantization bits. Then
+     * shift up by the segment number and subtract out the bias.
+     */
+    t = (((ulaw & 0x0F) << 3) + ULAW_BIAS) << (((int) ulaw & 0x70) >> 4);
+    return  (int16_t) ((ulaw & 0x80)  ?  (ULAW_BIAS - t)  :  (t - ULAW_BIAS));
+}
+/*- End of function --------------------------------------------------------*/
+
+/*
+ * A-law is basically as follows:
+ *
+ *      Linear Input Code        Compressed Code
+ *      -----------------        ---------------
+ *      0000000wxyza             000wxyz
+ *      0000001wxyza             001wxyz
+ *      000001wxyzab             010wxyz
+ *      00001wxyzabc             011wxyz
+ *      0001wxyzabcd             100wxyz
+ *      001wxyzabcde             101wxyz
+ *      01wxyzabcdef             110wxyz
+ *      1wxyzabcdefg             111wxyz
+ *
+ * For further information see John C. Bellamy's Digital Telephony, 1982,
+ * John Wiley & Sons, pps 98-111 and 472-476.
+ */
+
+#define ALAW_AMI_MASK       0x55
+
+/*! \brief Encode a linear sample to A-law
+    \param linear The sample to encode.
+    \return The A-law value.
+*/
+static __inline__ uint8_t linear_to_alaw(int linear)
+{
+    int mask;
+    int seg;
+    
+    if (linear >= 0)
+    {
+        /* Sign (bit 7) bit = 1 */
+        mask = ALAW_AMI_MASK | 0x80;
+    }
+    else
+    {
+        /* Sign (bit 7) bit = 0 */
+        mask = ALAW_AMI_MASK;
+        linear = -linear - 1;
+    }
+
+    /* Convert the scaled magnitude to segment number. */
+    seg = top_bit(linear | 0xFF) - 7;
+    if (seg >= 8)
+    {
+        if (linear >= 0)
+        {
+            /* Out of range. Return maximum value. */
+            return (uint8_t) (0x7F ^ mask);
+        }
+        /* We must be just a tiny step below zero */
+        return (uint8_t) (0x00 ^ mask);
+    }
+    /* Combine the sign, segment, and quantization bits. */
+    return (uint8_t) (((seg << 4) | ((linear >> ((seg)  ?  (seg + 3)  :  4)) & 0x0F)) ^ mask);
+}
+/*- End of function --------------------------------------------------------*/
+
+/*! \brief Decode an A-law sample to a linear value.
+    \param alaw The A-law sample to decode.
+    \return The linear value.
+*/
+static __inline__ int16_t alaw_to_linear(uint8_t alaw)
+{
+    int i;
+    int seg;
+
+    alaw ^= ALAW_AMI_MASK;
+    i = ((alaw & 0x0F) << 4);
+    seg = (((int) alaw & 0x70) >> 4);
+    if (seg)
+        i = (i + 0x108) << (seg - 1);
+    else
+        i += 8;
+    return (int16_t) ((alaw & 0x80)  ?  i  :  -i);
+}
+/*- End of function --------------------------------------------------------*/
+
+/*! \brief Transcode from A-law to u-law, using the procedure defined in G.711.
+    \param alaw The A-law sample to transcode.
+    \return The best matching u-law value.
+*/
+uint8_t alaw_to_ulaw(uint8_t alaw);
+
+/*! \brief Transcode from u-law to A-law, using the procedure defined in G.711.
+    \param ulaw The u-law sample to transcode.
+    \return The best matching A-law value.
+*/
+uint8_t ulaw_to_alaw(uint8_t ulaw);
+
+int g711_decode(g711_state_t *s,
+                int16_t amp[],
+                const uint8_t g711_data[],
+                int g711_bytes);
+
+int g711_encode(g711_state_t *s,
+                uint8_t g711_data[],
+                const int16_t amp[],
+                int len);
+
+int g711_transcode(g711_state_t *s,
+                   uint8_t g711_out[],
+                   const uint8_t g711_in[],
+                   int g711_bytes);
+
+/*! Initialise a G.711 encode or decode context.
+    \param s The G.711 context.
+    \param mode The G.711 mode.
+    \return A pointer to the G.711 context, or NULL for error. */
+g711_state_t *g711_init(g711_state_t *s, int mode);
+
+/*! Free a G.711 encode or decode context.
+    \param s The G.711 context.
+    \return 0 for OK. */
+int g711_release(g711_state_t *s);
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/spandsp/g722.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/spandsp/g722.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,180 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * g722.h - The ITU G.722 codec.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2005 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * Based on a single channel G.722 codec which is:
+ *
+ *****    Copyright (c) CMU    1993      *****
+ * Computer Science, Speech Group
+ * Chengxiang Lu and Alex Hauptmann
+ *
+ * $Id: g722.h,v 1.19 2008/04/17 14:27:00 steveu Exp $
+ */
+
+
+/*! \file */
+
+#if !defined(_SPANDSP_G722_H_)
+#define _SPANDSP_G722_H_
+
+/*! \page g722_page G.722 encoding and decoding
+\section g722_page_sec_1 What does it do?
+The G.722 module is a bit exact implementation of the ITU G.722 specification for all three
+specified bit rates - 64000bps, 56000bps and 48000bps. It passes the ITU tests.
+
+To allow fast and flexible interworking with narrow band telephony, the encoder and decoder
+support an option for the linear audio to be an 8k samples/second stream. In this mode the
+codec is considerably faster, and still fully compatible with wideband terminals using G.722.
+
+\section g722_page_sec_2 How does it work?
+???.
+*/
+
+enum
+{
+    G722_SAMPLE_RATE_8000 = 0x0001,
+    G722_PACKED = 0x0002
+};
+
+typedef struct
+{
+    /*! TRUE if the operating in the special ITU test mode, with the band split filters
+             disabled. */
+    int itu_test_mode;
+    /*! TRUE if the G.722 data is packed */
+    int packed;
+    /*! TRUE if encode from 8k samples/second */
+    int eight_k;
+    /*! 6 for 48000kbps, 7 for 56000kbps, or 8 for 64000kbps. */
+    int bits_per_sample;
+
+    /*! Signal history for the QMF */
+    int x[24];
+
+    struct
+    {
+        int s;
+        int sp;
+        int sz;
+        int r[3];
+        int a[3];
+        int ap[3];
+        int p[3];
+        int d[7];
+        int b[7];
+        int bp[7];
+        int sg[7];
+        int nb;
+        int det;
+    } band[2];
+
+    unsigned int in_buffer;
+    int in_bits;
+    unsigned int out_buffer;
+    int out_bits;
+} g722_encode_state_t;
+
+typedef struct
+{
+    /*! TRUE if the operating in the special ITU test mode, with the band split filters
+             disabled. */
+    int itu_test_mode;
+    /*! TRUE if the G.722 data is packed */
+    int packed;
+    /*! TRUE if decode to 8k samples/second */
+    int eight_k;
+    /*! 6 for 48000kbps, 7 for 56000kbps, or 8 for 64000kbps. */
+    int bits_per_sample;
+
+    /*! Signal history for the QMF */
+    int x[24];
+
+    struct
+    {
+        int s;
+        int sp;
+        int sz;
+        int r[3];
+        int a[3];
+        int ap[3];
+        int p[3];
+        int d[7];
+        int b[7];
+        int bp[7];
+        int sg[7];
+        int nb;
+        int det;
+    } band[2];
+    
+    unsigned int in_buffer;
+    int in_bits;
+    unsigned int out_buffer;
+    int out_bits;
+} g722_decode_state_t;
+
+#if defined(__cplusplus)
+extern "C"
+{
+#endif
+
+/*! Initialise an G.722 encode context.
+    \param s The G.722 encode context.
+    \param rate The required bit rate for the G.722 data.
+           The valid rates are 64000, 56000 and 48000.
+    \param options
+    \return A pointer to the G.722 encode context, or NULL for error. */
+g722_encode_state_t *g722_encode_init(g722_encode_state_t *s, int rate, int options);
+
+int g722_encode_release(g722_encode_state_t *s);
+
+/*! Encode a buffer of linear PCM data to G.722
+    \param s The G.722 context.
+    \param g722_data The G.722 data produced.
+    \param amp The audio sample buffer.
+    \param len The number of samples in the buffer.
+    \return The number of bytes of G.722 data produced. */
+int g722_encode(g722_encode_state_t *s, uint8_t g722_data[], const int16_t amp[], int len);
+
+/*! Initialise an G.722 decode context.
+    \param s The G.722 decode context.
+    \param rate The bit rate of the G.722 data.
+           The valid rates are 64000, 56000 and 48000.
+    \param options
+    \return A pointer to the G.722 decode context, or NULL for error. */
+g722_decode_state_t *g722_decode_init(g722_decode_state_t *s, int rate, int options);
+
+int g722_decode_release(g722_decode_state_t *s);
+
+/*! Decode a buffer of G.722 data to linear PCM.
+    \param s The G.722 context.
+    \param amp The audio sample buffer.
+    \param g722_data
+    \param len
+    \return The number of samples returned. */
+int g722_decode(g722_decode_state_t *s, int16_t amp[], const uint8_t g722_data[], int len);
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif

Added: freeswitch/trunk/libs/spandsp/src/spandsp/g726.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/spandsp/g726.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,169 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * g726.h - ITU G.726 codec.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2006 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: g726.h,v 1.21 2008/04/17 14:27:00 steveu Exp $
+ */
+
+/*! \file */
+
+#if !defined(_SPANDSP_G726_H_)
+#define _SPANDSP_G726_H_
+
+/*! \page g726_page G.726 encoding and decoding
+\section g726_page_sec_1 What does it do?
+
+The G.726 module is a bit exact implementation of the full ITU G.726 specification.
+It supports:
+    - 16 kbps, 24kbps, 32kbps, and 40kbps operation.
+    - Tandem adjustment, for interworking with A-law and u-law.
+    - Annex A support, for use in environments not using A-law or u-law.
+
+It passes the ITU tests.
+
+\section g726_page_sec_2 How does it work?
+???.
+*/
+
+enum
+{
+    G726_ENCODING_LINEAR = 0,   /* Interworking with 16 bit signed linear */
+    G726_ENCODING_ULAW,         /* Interworking with u-law */
+    G726_ENCODING_ALAW          /* Interworking with A-law */
+};
+
+enum
+{
+    G726_PACKING_NONE = 0,
+    G726_PACKING_LEFT = 1,
+    G726_PACKING_RIGHT = 2
+};
+
+struct g726_state_s;
+
+typedef int16_t (*g726_decoder_func_t)(struct g726_state_s *s, uint8_t code);
+
+typedef uint8_t (*g726_encoder_func_t)(struct g726_state_s *s, int16_t amp);
+
+/*!
+ * The following is the definition of the state structure
+ * used by the G.726 encoder and decoder to preserve their internal
+ * state between successive calls.  The meanings of the majority
+ * of the state structure fields are explained in detail in the
+ * CCITT Recommendation G.721.  The field names are essentially indentical
+ * to variable names in the bit level description of the coding algorithm
+ * included in this Recommendation.
+ */
+typedef struct g726_state_s
+{
+    /*! The bit rate */
+    int rate;
+    /*! The external coding, for tandem operation */
+    int ext_coding;
+    /*! The number of bits per sample */
+    unsigned int bits_per_sample;
+    /*! One of the G.726_PACKING_xxx options */
+    int packing;
+
+    /*! Locked or steady state step size multiplier. */
+    int32_t yl;
+    /*! Unlocked or non-steady state step size multiplier. */
+    int16_t yu;
+    /*! int16_t term energy estimate. */
+    int16_t dms;
+    /*! Long term energy estimate. */
+    int16_t dml;
+    /*! Linear weighting coefficient of 'yl' and 'yu'. */
+    int16_t ap;
+    
+    /*! Coefficients of pole portion of prediction filter. */
+    int16_t a[2];
+    /*! Coefficients of zero portion of prediction filter. */
+    int16_t b[6];
+    /*! Signs of previous two samples of a partially reconstructed signal. */
+    int16_t pk[2];
+    /*! Previous 6 samples of the quantized difference signal represented in
+        an internal floating point format. */
+    int16_t dq[6];
+    /*! Previous 2 samples of the quantized difference signal represented in an
+        internal floating point format. */
+    int16_t sr[2];
+    /*! Delayed tone detect */
+    int td;
+    
+    /*! \brief The bit stream processing context. */
+    bitstream_state_t bs;
+
+    /*! \brief The current encoder function. */
+    g726_encoder_func_t enc_func;
+    /*! \brief The current decoder function. */
+    g726_decoder_func_t dec_func;
+} g726_state_t;
+
+#if defined(__cplusplus)
+extern "C"
+{
+#endif
+
+/*! Initialise a G.726 encode or decode context.
+    \param s The G.726 context.
+    \param bit_rate The required bit rate for the ADPCM data.
+           The valid rates are 16000, 24000, 32000 and 40000.
+    \param ext_coding The coding used outside G.726.
+    \param packing One of the G.726_PACKING_xxx options.
+    \return A pointer to the G.726 context, or NULL for error. */
+g726_state_t *g726_init(g726_state_t *s, int bit_rate, int ext_coding, int packing);
+
+/*! Free a G.726 encode or decode context.
+    \param s The G.726 context.
+    \return 0 for OK. */
+int g726_release(g726_state_t *s);
+
+/*! Decode a buffer of G.726 ADPCM data to linear PCM, a-law or u-law.
+    \param s The G.726 context.
+    \param amp The audio sample buffer.
+    \param g726_data
+    \param g726_bytes
+    \return The number of samples returned. */
+int g726_decode(g726_state_t *s,
+                int16_t amp[],
+                const uint8_t g726_data[],
+                int g726_bytes);
+
+/*! Encode a buffer of linear PCM data to G.726 ADPCM.
+    \param s The G.726 context.
+    \param g726_data The G.726 data produced.
+    \param amp The audio sample buffer.
+    \param len The number of samples in the buffer.
+    \return The number of bytes of G.726 data produced. */
+int g726_encode(g726_state_t *s,
+                uint8_t g726_data[],
+                const int16_t amp[],
+                int len);
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/spandsp/gsm0610.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/spandsp/gsm0610.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,175 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * gsm0610.h - GSM 06.10 full rate speech codec.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2006 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: gsm0610.h,v 1.17 2008/04/17 14:27:00 steveu Exp $
+ */
+
+#if !defined(_SPANDSP_GSM0610_H_)
+#define _SPANDSP_GSM0610_H_
+
+/*! \page gsm0610_page GSM 06.10 encoding and decoding
+\section gsm0610_page_sec_1 What does it do?
+
+The GSM 06.10 module is an version of the widely used GSM FR codec software
+available from http://kbs.cs.tu-berlin.de/~jutta/toast.html. This version
+was produced since some versions of this codec are not bit exact, or not
+very efficient on modern processors. This implementation can use MMX instructions
+on Pentium class processors, or alternative methods on other processors. It
+passes all the ETSI test vectors. That is, it is a tested bit exact implementation.
+
+This implementation supports encoded data in one of three packing formats:
+    - Unpacked, with the 76 parameters of a GSM 06.10 code frame each occupying a
+      separate byte. (note that none of the parameters exceed 8 bits).
+    - Packed the the 33 byte per frame, used for VoIP, where 4 bits per frame are wasted.
+    - Packed in WAV49 format, where 2 frames are packed into 65 bytes.
+
+\section gsm0610_page_sec_2 How does it work?
+???.
+*/
+
+enum
+{
+    GSM0610_PACKING_NONE,
+    GSM0610_PACKING_WAV49,
+    GSM0610_PACKING_VOIP
+};
+
+/*!
+    GSM 06.10 FR codec unpacked frame.
+*/
+typedef struct
+{
+    int16_t LARc[8];
+    int16_t Nc[4];
+    int16_t bc[4];
+    int16_t Mc[4];
+    int16_t xmaxc[4];
+    int16_t xMc[4][13];
+} gsm0610_frame_t;
+
+/*!
+    GSM 06.10 FR codec state descriptor. This defines the state of
+    a single working instance of the GSM 06.10 FR encoder or decoder.
+*/
+typedef struct
+{
+    /*! \brief One of the packing modes */
+    int packing;
+
+    int16_t dp0[280];
+
+    /*! Preprocessing */
+    int16_t z1;
+    int32_t L_z2;
+    /*! Pre-emphasis */
+    int16_t mp;
+
+    /*! Short term delay filter */
+    int16_t u[8];
+    int16_t LARpp[2][8];
+    int16_t j;
+
+    /*! Long term synthesis */
+    int16_t nrp;
+    /*! Short term synthesis */
+    int16_t v[9];
+    /*! Decoder postprocessing */
+    int16_t msr;
+    
+    /*! Encoder data */
+    int16_t e[50];
+} gsm0610_state_t;
+
+#if defined(__cplusplus)
+extern "C"
+{
+#endif
+
+/*! Initialise a GSM 06.10 encode or decode context.
+    \param s The GSM 06.10 context
+    \param packing One of the GSM0610_PACKING_xxx options.
+    \return A pointer to the GSM 06.10 context, or NULL for error. */
+gsm0610_state_t *gsm0610_init(gsm0610_state_t *s, int packing);
+
+/*! Release a GSM 06.10 encode or decode context.
+    \param s The GSM 06.10 context
+    \return 0 for success, else -1. */
+int gsm0610_release(gsm0610_state_t *s);
+
+/*! Set the packing format for a GSM 06.10 encode or decode context.
+    \param s The GSM 06.10 context
+    \param packing One of the GSM0610_PACKING_xxx options.
+    \return 0 for success, else -1. */
+int gsm0610_set_packing(gsm0610_state_t *s, int packing);
+
+/*! Encode a buffer of linear PCM data to GSM 06.10.
+    \param s The GSM 06.10 context.
+    \param code The GSM 06.10 data produced.
+    \param amp The audio sample buffer.
+    \param len The number of samples in the buffer.
+    \return The number of bytes of GSM 06.10 data produced. */
+int gsm0610_encode(gsm0610_state_t *s, uint8_t code[], const int16_t amp[], int len);
+
+/*! Decode a buffer of GSM 06.10 data to linear PCM.
+    \param s The GSM 06.10 context.
+    \param amp The audio sample buffer.
+    \param code The GSM 06.10 data.
+    \param len The number of bytes of GSM 06.10 data to be decoded.
+    \return The number of samples returned. */
+int gsm0610_decode(gsm0610_state_t *s, int16_t amp[], const uint8_t code[], int len);
+
+int gsm0610_pack_none(uint8_t c[], const gsm0610_frame_t *s);
+
+/*! Pack a pair of GSM 06.10 frames in the format used for wave files (wave type 49).
+    \param c The buffer for the packed data. This must be at least 65 bytes long.
+    \param s A pointer to the frames to be packed.
+    \return The number of bytes generated. */
+int gsm0610_pack_wav49(uint8_t c[], const gsm0610_frame_t *s);
+
+/*! Pack a GSM 06.10 frames in the format used for VoIP.
+    \param c The buffer for the packed data. This must be at least 33 bytes long.
+    \param s A pointer to the frame to be packed.
+    \return The number of bytes generated. */
+int gsm0610_pack_voip(uint8_t c[], const gsm0610_frame_t *s);
+
+int gsm0610_unpack_none(gsm0610_frame_t *s, const uint8_t c[]);
+
+/*! Unpack a pair of GSM 06.10 frames from the format used for wave files (wave type 49).
+    \param s A pointer to a buffer into which the frames will be packed.
+    \param c The buffer containing the data to be unpacked. This must be at least 65 bytes long.
+    \return The number of bytes absorbed. */
+int gsm0610_unpack_wav49(gsm0610_frame_t *s, const uint8_t c[]);
+
+/*! Unpack a GSM 06.10 frame from the format used for VoIP.
+    \param s A pointer to a buffer into which the frame will be packed.
+    \param c The buffer containing the data to be unpacked. This must be at least 33 bytes long.
+    \return The number of bytes absorbed. */
+int gsm0610_unpack_voip(gsm0610_frame_t *s, const uint8_t c[]);
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
+/*- End of include ---------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/spandsp/hdlc.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/spandsp/hdlc.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,312 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * hdlc.h
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2003 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: hdlc.h,v 1.39 2008/04/17 14:27:00 steveu Exp $
+ */
+
+/*! \file */
+
+/*! \page hdlc_page HDLC
+
+\section hdlc_page_sec_1 What does it do?
+The HDLC module provides bit stuffing, destuffing, framing and deframing,
+according to the HDLC protocol. It also provides 16 and 32 bit CRC generation
+and checking services for HDLC frames.
+
+HDLC may not be a DSP function, but is needed to accompany several DSP components.
+
+\section hdlc_page_sec_2 How does it work?
+*/
+
+#if !defined(_SPANDSP_HDLC_H_)
+#define _SPANDSP_HDLC_H_
+
+/*! 
+    HDLC_MAXFRAME_LEN is the maximum length of a stuffed HDLC frame, excluding the CRC.
+*/
+#define HDLC_MAXFRAME_LEN       400	
+
+typedef void (*hdlc_frame_handler_t)(void *user_data, const uint8_t *pkt, int len, int ok);
+typedef void (*hdlc_underflow_handler_t)(void *user_data);
+
+/*!
+    HDLC receive descriptor. This contains all the state information for an HDLC receiver.
+ */
+typedef struct
+{
+    /*! 2 for CRC-16, 4 for CRC-32 */
+    int crc_bytes;
+    /*! \brief Maximum permitted frame length. */
+    size_t max_frame_len;
+    /*! \brief The callback routine called to process each good received frame. */
+    hdlc_frame_handler_t frame_handler;
+    /*! \brief An opaque parameter passed to the callback routine. */
+    void *user_data;
+    /*! \brief TRUE if bad frames are to be reported. */
+    int report_bad_frames;
+    /*! \brief The number of consecutive flags which must be seen before framing is
+        declared OK. */
+    int framing_ok_threshold;
+    /*! \brief TRUE if framing OK has been announced. */
+    int framing_ok_announced;
+    /*! \brief Number of consecutive flags seen so far. */
+    int flags_seen;
+
+    /*! \brief The raw (stuffed) bit stream buffer. */
+    unsigned int raw_bit_stream;
+    /*! \brief The destuffed bit stream buffer. */
+    unsigned int byte_in_progress;
+    /*! \brief The current number of bits in byte_in_progress. */
+    int num_bits;
+    /*! \brief TRUE if in octet counting mode (e.g. for MTP). */
+    int octet_counting_mode;
+    /*! \brief Octet count, to achieve the functionality needed for things
+               like MTP. */
+    int octet_count;
+    /*! \brief The number of octets to be allowed between octet count reports. */
+    int octet_count_report_interval;
+
+    /*! \brief Buffer for a frame in progress. */
+    uint8_t buffer[HDLC_MAXFRAME_LEN + 4];
+    /*! \brief Length of a frame in progress. */
+    size_t len;
+
+    /*! \brief The number of bytes of good frames received (CRC not included). */
+    unsigned long int rx_bytes;
+    /*! \brief The number of good frames received. */
+    unsigned long int rx_frames;
+    /*! \brief The number of frames with CRC errors received. */
+    unsigned long int rx_crc_errors;
+    /*! \brief The number of too short and too long frames received. */
+    unsigned long int rx_length_errors;
+    /*! \brief The number of HDLC aborts received. */
+    unsigned long int rx_aborts;
+} hdlc_rx_state_t;
+
+/*!
+    HDLC received data statistics.
+ */
+typedef struct
+{
+    /*! \brief The number of bytes of good frames received (CRC not included). */
+    unsigned long int bytes;
+    /*! \brief The number of good frames received. */
+    unsigned long int good_frames;
+    /*! \brief The number of frames with CRC errors received. */
+    unsigned long int crc_errors;
+    /*! \brief The number of too short and too long frames received. */
+    unsigned long int length_errors;
+    /*! \brief The number of HDLC aborts received. */
+    unsigned long int aborts;
+} hdlc_rx_stats_t;
+
+/*!
+    HDLC transmit descriptor. This contains all the state information for an
+    HDLC transmitter.
+ */
+typedef struct
+{
+    /*! 2 for CRC-16, 4 for CRC-32 */
+    int crc_bytes;
+    /*! \brief The callback routine called to indicate transmit underflow. */
+    hdlc_underflow_handler_t underflow_handler;
+    /*! \brief An opaque parameter passed to the callback routine. */
+    void *user_data;
+    /*! \brief The minimum flag octets to insert between frames. */
+    int inter_frame_flags;
+    /*! \brief TRUE if frame creation works in progressive mode. */
+    int progressive;
+    /*! \brief Maximum permitted frame length. */
+    size_t max_frame_len;
+
+    /*! \brief The stuffed bit stream being created. */
+    uint32_t octets_in_progress;
+    /*! \brief The number of bits currently in octets_in_progress. */
+    int num_bits;
+    /*! \brief The currently rotated state of the flag octet. */
+    int idle_octet;
+    /*! \brief The number of flag octets to send for a timed burst of flags. */
+    int flag_octets;
+    /*! \brief The number of abort octets to send for a timed burst of aborts. */
+    int abort_octets;
+    /*! \brief TRUE if the next underflow of timed flag octets should be reported */
+    int report_flag_underflow;
+
+    /*! \brief The current message being transmitted, with its CRC attached. */
+    uint8_t buffer[HDLC_MAXFRAME_LEN + 4];
+    /*! \brief The length of the message in the buffer. */
+    size_t len;
+    /*! \brief The current send position within the buffer. */
+    int pos;
+    /*! \brief The running CRC, as data fills the frame buffer. */
+    uint32_t crc;
+
+    /*! \brief The current byte being broken into bits for transmission. */
+    int byte;
+    /*! \brief The number of bits remaining in byte. */
+    int bits;
+    
+    /*! \brief TRUE if transmission should end on buffer underflow .*/
+    int tx_end;
+} hdlc_tx_state_t;
+
+#if defined(__cplusplus)
+extern "C"
+{
+#endif
+
+/*! \brief Initialise an HDLC receiver context.
+    \param s A pointer to an HDLC receiver context.
+    \param crc32 TRUE to use ITU CRC32. FALSE to use ITU CRC16.
+    \param report_bad_frames TRUE to request the reporting of bad frames.
+    \param framing_ok_threshold The number of back-to-back flags needed to
+           start the framing OK condition. This may be used where a series of
+           flag octets is used as a preamble, such as in the T.30 protocol.
+    \param handler The function to be called when a good HDLC frame is received.
+    \param user_data An opaque parameter for the callback routine.
+    \return A pointer to the HDLC receiver context.
+*/
+hdlc_rx_state_t *hdlc_rx_init(hdlc_rx_state_t *s,
+                              int crc32,
+                              int report_bad_frames,
+                              int framing_ok_threshold,
+                              hdlc_frame_handler_t handler,
+                              void *user_data);
+
+/*! \brief Set the maximum frame length for an HDLC receiver context.
+    \param s A pointer to an HDLC receiver context.
+    \param max_len The maximum permitted length of a frame.
+*/
+void hdlc_rx_set_max_frame_len(hdlc_rx_state_t *s, size_t max_len);
+
+/*! \brief Set the octet counting report interval.
+    \param s A pointer to an HDLC receiver context.
+    \param interval The interval, in octets.
+*/
+void hdlc_rx_set_octet_counting_report_interval(hdlc_rx_state_t *s, int interval);
+
+/*! \brief Get the current receive statistics.
+    \param s A pointer to an HDLC receiver context.
+    \param t A pointer to the buffer for the statistics.
+    \return 0 for OK, else -1.
+*/
+int hdlc_rx_get_stats(hdlc_rx_state_t *s,
+                      hdlc_rx_stats_t *t);
+
+/*! \brief Put a single bit of data to an HDLC receiver.
+    \param s A pointer to an HDLC receiver context.
+    \param new_bit The bit.
+*/
+void hdlc_rx_put_bit(hdlc_rx_state_t *s, int new_bit);
+
+/*! \brief Put a byte of data to an HDLC receiver.
+    \param s A pointer to an HDLC receiver context.
+    \param new_byte The byte of data.
+*/
+void hdlc_rx_put_byte(hdlc_rx_state_t *s, int new_byte);
+
+/*! \brief Put a series of bytes of data to an HDLC receiver.
+    \param s A pointer to an HDLC receiver context.
+    \param buf The buffer of data.
+    \param len The length of the data in the buffer.
+*/
+void hdlc_rx_put(hdlc_rx_state_t *s, const uint8_t buf[], int len);
+
+/*! \brief Initialise an HDLC transmitter context.
+    \param s A pointer to an HDLC transmitter context.
+    \param crc32 TRUE to use ITU CRC32. FALSE to use ITU CRC16.
+    \param inter_frame_flags The minimum flag octets to insert between frames (usually one).
+    \param progressive TRUE if frame creation works in progressive mode.
+    \param handler The callback function called when the HDLC transmitter underflows.
+    \param user_data An opaque parameter for the callback routine.
+    \return A pointer to the HDLC transmitter context.
+*/
+hdlc_tx_state_t *hdlc_tx_init(hdlc_tx_state_t *s,
+                              int crc32,
+                              int inter_frame_flags,
+                              int progressive,
+                              hdlc_underflow_handler_t handler,
+                              void *user_data);
+
+/*! \brief Set the maximum frame length for an HDLC transmitter context.
+    \param s A pointer to an HDLC transmitter context.
+    \param max_len The maximum length.
+*/
+void hdlc_tx_set_max_frame_len(hdlc_tx_state_t *s, size_t max_len);
+
+/*! \brief Transmit a frame.
+    \param s A pointer to an HDLC transmitter context.
+    \param frame A pointer to the frame to be transmitted.
+    \param len The length of the frame to be transmitted.
+    \return 0 if the frame was accepted for transmission, else -1.
+*/
+int hdlc_tx_frame(hdlc_tx_state_t *s, const uint8_t *frame, size_t len);
+
+/*! \brief Corrupt the frame currently being transmitted, by giving it the wrong CRC.
+    \param s A pointer to an HDLC transmitter context.
+    \return 0 if the frame was corrupted, else -1.
+*/
+int hdlc_tx_corrupt_frame(hdlc_tx_state_t *s);
+
+/*! \brief Transmit a specified quantity of flag octets, typically as a preamble.
+    \param s A pointer to an HDLC transmitter context.
+    \param len The length of the required period of flags, in flag octets. If len is zero this
+           requests that HDLC transmission be terminated when the buffers have fully
+           drained.
+    \return 0 if the flags were accepted for transmission, else -1.
+*/
+int hdlc_tx_flags(hdlc_tx_state_t *s, int len);
+
+/*! \brief Send an abort.
+    \param s A pointer to an HDLC transmitter context.
+    \return 0 if the frame was aborted, else -1.
+*/
+int hdlc_tx_abort(hdlc_tx_state_t *s);
+
+/*! \brief Get the next bit for transmission.
+    \param s A pointer to an HDLC transmitter context.
+    \return The next bit for transmission.
+*/
+int hdlc_tx_get_bit(hdlc_tx_state_t *s);
+
+/*! \brief Get the next byte for transmission.
+    \param s A pointer to an HDLC transmitter context.
+    \return The next byte for transmission.
+*/
+int hdlc_tx_get_byte(hdlc_tx_state_t *s);
+
+/*! \brief Get the next sequence of bytes for transmission.
+    \param s A pointer to an HDLC transmitter context.
+    \param buf The buffer for the data.
+    \param max_len The number of bytes to get.
+    \return The number of bytes actually got.
+*/
+int hdlc_tx_get(hdlc_tx_state_t *s, uint8_t buf[], size_t max_len);
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/spandsp/ima_adpcm.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/spandsp/ima_adpcm.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,122 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * imaadpcm.c - Conversion routines between linear 16 bit PCM data and
+ *		        IMA/DVI/Intel ADPCM format.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2004 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * Based on a bit from here, a bit from there, eye of toad,
+ * ear of bat, etc - plus, of course, my own 2 cents.
+ *
+ * $Id: ima_adpcm.h,v 1.20 2008/04/17 14:27:00 steveu Exp $
+ */
+
+/*! \file */
+
+#if !defined(_SPANDSP_IMA_ADPCM_H_)
+#define _SPANDSP_IMA_ADPCM_H_
+
+/*! \page ima_adpcm_page IMA/DVI/Intel ADPCM encoding and decoding
+\section ima_adpcm_page_sec_1 What does it do?
+IMA ADPCM offers a good balance of simplicity and quality at a rate of
+32kbps.
+
+\section ima_adpcm_page_sec_2 How does it work?
+
+\section ima_adpcm_page_sec_3 How do I use it?
+*/
+
+enum
+{
+    /*! IMA4 is the original IMA ADPCM variant */
+    IMA_ADPCM_IMA4 = 0,
+    /*! DVI4 is the IMA ADPCM variant defined in RFC3551 */
+    IMA_ADPCM_DVI4 = 1,
+    /*! VDVI is the variable bit rate IMA ADPCM variant defined in RFC3551 */
+    IMA_ADPCM_VDVI = 2
+};
+
+/*!
+    IMA (DVI/Intel) ADPCM conversion state descriptor. This defines the state of
+    a single working instance of the IMA ADPCM converter. This is used for
+    either linear to ADPCM or ADPCM to linear conversion.
+*/
+typedef struct
+{
+    int variant;
+    /*! \brief The size of a chunk, in samples. */
+    int chunk_size;
+    /*! \brief The last state of the ADPCM algorithm. */
+    int last;
+    /*! \brief Current index into the step size table. */
+    int step_index;
+    /*! \brief The current IMA code byte in progress. */
+    uint16_t ima_byte;
+    int bits;
+} ima_adpcm_state_t;
+
+#if defined(__cplusplus)
+extern "C"
+{
+#endif
+
+/*! Initialise an IMA ADPCM encode or decode context.
+    \param s The IMA ADPCM context
+    \param variant ???
+    \param chunk_size The size of a chunk, in samples. A chunk size of
+           zero sample samples means treat each encode or decode operation
+           as a chunk.
+    \return A pointer to the IMA ADPCM context, or NULL for error. */
+ima_adpcm_state_t *ima_adpcm_init(ima_adpcm_state_t *s, int variant, int chunk_size);
+
+/*! Free an IMA ADPCM encode or decode context.
+    \param s The IMA ADPCM context.
+    \return 0 for OK. */
+int ima_adpcm_release(ima_adpcm_state_t *s);
+
+/*! Encode a buffer of linear PCM data to IMA ADPCM.
+    \param s The IMA ADPCM context.
+    \param ima_data The IMA ADPCM data produced.
+    \param amp The audio sample buffer.
+    \param len The number of samples in the buffer.
+    \return The number of bytes of IMA ADPCM data produced. */
+int ima_adpcm_encode(ima_adpcm_state_t *s,
+                     uint8_t ima_data[],
+                     const int16_t amp[],
+                     int len);
+
+/*! Decode a buffer of IMA ADPCM data to linear PCM.
+    \param s The IMA ADPCM context.
+    \param amp The audio sample buffer.
+    \param ima_data
+    \param ima_bytes
+    \return The number of samples returned. */
+int ima_adpcm_decode(ima_adpcm_state_t *s,
+                     int16_t amp[],
+                     const uint8_t ima_data[],
+                     int ima_bytes);
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/spandsp/logging.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/spandsp/logging.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,147 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * logging.h - definitions for error and debug logging.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2005 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: logging.h,v 1.16 2008/05/05 11:25:01 steveu Exp $
+ */
+
+/*! \file */
+
+/*! \page logging_page Logging
+\section logging_page_sec_1 What does it do?
+???.
+*/
+
+#if !defined(_SPANDSP_LOGGING_H_)
+#define _SPANDSP_LOGGING_H_
+
+/*! General logging function for spandsp logging. */
+typedef void (*message_handler_func_t)(int level, const char *text);
+
+/*! Error logging function for spandsp logging. */
+typedef void (*error_handler_func_t)(const char *text);
+
+/* Logging elements */
+enum
+{
+    SPAN_LOG_SEVERITY_MASK              = 0x00FF,
+    SPAN_LOG_SHOW_DATE                  = 0x0100,
+    SPAN_LOG_SHOW_SAMPLE_TIME           = 0x0200,
+    SPAN_LOG_SHOW_SEVERITY              = 0x0400,
+    SPAN_LOG_SHOW_PROTOCOL              = 0x0800,
+    SPAN_LOG_SHOW_VARIANT               = 0x1000,
+    SPAN_LOG_SHOW_TAG                   = 0x2000,
+    SPAN_LOG_SUPPRESS_LABELLING         = 0x8000
+};
+
+/* Logging severity levels */
+enum
+{
+    SPAN_LOG_NONE                       = 0,
+    SPAN_LOG_ERROR                      = 1,
+    SPAN_LOG_WARNING                    = 2,
+    SPAN_LOG_PROTOCOL_ERROR             = 3,
+    SPAN_LOG_PROTOCOL_WARNING           = 4,
+    SPAN_LOG_FLOW                       = 5,
+    SPAN_LOG_FLOW_2                     = 6,
+    SPAN_LOG_FLOW_3                     = 7,
+    SPAN_LOG_DEBUG                      = 8,
+    SPAN_LOG_DEBUG_2                    = 9,
+    SPAN_LOG_DEBUG_3                    = 10
+};
+
+/*!
+    Logging descriptor. This defines the working state for a single instance of
+    the logging facility for spandsp.
+*/
+typedef struct
+{
+    int level;
+    int samples_per_second;
+    int64_t elapsed_samples;
+    const char *tag;
+    const char *protocol;
+
+    message_handler_func_t span_message;
+    error_handler_func_t span_error;
+} logging_state_t;
+
+#if defined(__cplusplus)
+extern "C"
+{
+#endif
+
+/*! Test if logging of a specified severity level is enabled.
+    \brief Test if logging of a specified severity level is enabled.
+    \param s The logging context.
+    \param level The severity level to be tested.
+    \return TRUE if logging is enable, else FALSE.
+*/
+int span_log_test(logging_state_t *s, int level);
+
+/*! Generate a log entry.
+    \brief Generate a log entry.
+    \param s The logging context.
+    \param level The severity level of the entry.
+    \param format ???
+    \return 0 if no output generated, else 1.
+*/
+int span_log(logging_state_t *s, int level, const char *format, ...);
+
+/*! Generate a log entry displaying the contents of a buffer.
+    \brief Generate a log entry displaying the contents of a buffer
+    \param s The logging context.
+    \param level The severity level of the entry.
+    \param tag A label for the log entry.
+    \param buf The buffer to be dumped to the log.
+    \param len The length of buf.
+    \return 0 if no output generated, else 1.
+*/
+int span_log_buf(logging_state_t *s, int level, const char *tag, const uint8_t *buf, int len);
+
+int span_log_init(logging_state_t *s, int level, const char *tag);
+
+int span_log_set_level(logging_state_t *s, int level);
+
+int span_log_set_tag(logging_state_t *s, const char *tag);
+
+int span_log_set_protocol(logging_state_t *s, const char *protocol);
+
+int span_log_set_sample_rate(logging_state_t *s, int samples_per_second);
+
+int span_log_bump_samples(logging_state_t *s, int samples);
+
+void span_log_set_message_handler(logging_state_t *s, message_handler_func_t func);
+
+void span_log_set_error_handler(logging_state_t *s, error_handler_func_t func);
+
+void span_set_message_handler(message_handler_func_t func);
+
+void span_set_error_handler(error_handler_func_t func);
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/spandsp/lpc10.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/spandsp/lpc10.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,215 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * lpc10.h - LPC10 low bit rate speech codec.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2006 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: lpc10.h,v 1.17 2008/04/17 14:27:00 steveu Exp $
+ */
+
+#if !defined(_SPANDSP_LPC10_H_)
+#define _SPANDSP_LPC10_H_
+
+/*! \page lpc10_page LPC10 encoding and decoding
+\section lpc10_page_sec_1 What does it do?
+The LPC10 module implements the US Department of Defense LPC10
+codec. This codec produces compressed data at 2400bps. At such
+a low rate high fidelity cannot be expected. However, the speech
+clarity is quite good, and this codec is unencumbered by patent
+or other restrictions.
+
+\section lpc10_page_sec_2 How does it work?
+???.
+*/
+
+#define LPC10_SAMPLES_PER_FRAME 180
+#define LPC10_BITS_IN_COMPRESSED_FRAME 54
+
+/*!
+    LPC10 codec unpacked frame.
+*/
+typedef struct
+{
+    int32_t ipitch;
+    int32_t irms;
+    int32_t irc[10];
+} lpc10_frame_t;
+
+/*!
+    LPC10 codec encoder state descriptor. This defines the state of
+    a single working instance of the LPC10 encoder.
+*/
+typedef struct
+{
+    int error_correction;
+
+    /* State used only by function high_pass_100hz */
+    float z11;
+    float z21;
+    float z12;
+    float z22;
+    
+    /* State used by function lpc10_analyse */
+    float inbuf[LPC10_SAMPLES_PER_FRAME*3];
+    float pebuf[LPC10_SAMPLES_PER_FRAME*3];
+    float lpbuf[696];
+    float ivbuf[312];
+    float bias;
+    int32_t osbuf[10];      /* No initial value necessary */
+    int32_t osptr;          /* Initial value 1 */
+    int32_t obound[3];
+    int32_t vwin[3][2];     /* Initial value vwin[2][0] = 307; vwin[2][1] = 462; */
+    int32_t awin[3][2];     /* Initial value awin[2][0] = 307; awin[2][1] = 462; */
+    int32_t voibuf[4][2];
+    float rmsbuf[3];
+    float rcbuf[3][10];
+    float zpre;
+
+    /* State used by function onset */
+    float n;
+    float d__;          /* Initial value 1.0f */
+    float fpc;          /* No initial value necessary */
+    float l2buf[16];
+    float l2sum1;
+    int32_t l2ptr1;     /* Initial value 1 */
+    int32_t l2ptr2;     /* Initial value 9 */
+    int32_t lasti;      /* No initial value necessary */
+    int hyst;           /* Initial value FALSE */
+
+    /* State used by function lpc10_voicing */
+    float dither;       /* Initial value 20.0f */
+    float snr;
+    float maxmin;
+    float voice[3][2];  /* Initial value is probably unnecessary */
+    int32_t lbve;
+    int32_t lbue;
+    int32_t fbve;
+    int32_t fbue;
+    int32_t ofbue;
+    int32_t sfbue;
+    int32_t olbue;
+    int32_t slbue;
+
+    /* State used by function dynamic_pitch_tracking */
+    float s[60];
+    int32_t p[2][60];
+    int32_t ipoint;
+    float alphax;
+
+    /* State used by function lpc10_pack */
+    int32_t isync;
+} lpc10_encode_state_t;
+
+/*!
+    LPC10 codec decoder state descriptor. This defines the state of
+    a single working instance of the LPC10 decoder.
+*/
+typedef struct
+{
+    int error_correction;
+
+    /* State used by function decode */
+    int32_t iptold;     /* Initial value 60 */
+    int first;          /* Initial value TRUE */
+    int32_t ivp2h;
+    int32_t iovoic;
+    int32_t iavgp;      /* Initial value 60 */
+    int32_t erate;
+    int32_t drc[10][3];
+    int32_t dpit[3];
+    int32_t drms[3];
+
+    /* State used by function synths */
+    float buf[LPC10_SAMPLES_PER_FRAME*2];
+    int32_t buflen;     /* Initial value LPC10_SAMPLES_PER_FRAME */
+
+    /* State used by function pitsyn */
+    int32_t ivoico;     /* No initial value necessary as long as first_pitsyn is initially TRUE_ */
+    int32_t ipito;      /* No initial value necessary as long as first_pitsyn is initially TRUE_ */
+    float rmso;         /* Initial value 1.0f */
+    float rco[10];      /* No initial value necessary as long as first_pitsyn is initially TRUE_ */
+    int32_t jsamp;      /* Nno initial value necessary as long as first_pitsyn is initially TRUE_ */
+    int first_pitsyn;   /* Initial value TRUE */
+
+    /* State used by function bsynz */
+    int32_t ipo;
+    float exc[166];
+    float exc2[166];
+    float lpi[3];
+    float hpi[3];
+    float rmso_bsynz;
+
+    /* State used by function random */
+    int32_t j;
+    int32_t k;
+    int16_t y[5];
+
+    /* State used by function deemp */
+    float dei[2];
+    float deo[3];
+} lpc10_decode_state_t;
+
+#if defined(__cplusplus)
+extern "C"
+{
+#endif
+
+/*! Initialise an LPC10e encode context.
+    \param s The LPC10e context
+    \param error_correction ???
+    \return A pointer to the LPC10e context, or NULL for error. */
+lpc10_encode_state_t *lpc10_encode_init(lpc10_encode_state_t *s, int error_correction);
+
+int lpc10_encode_release(lpc10_encode_state_t *s);
+
+/*! Encode a buffer of linear PCM data to LPC10e.
+    \param s The LPC10e context.
+    \param ima_data The LPC10e data produced.
+    \param amp The audio sample buffer.
+    \param len The number of samples in the buffer. This must be a multiple of 180, as
+           this is the number of samples on a frame.
+    \return The number of bytes of LPC10e data produced. */
+int lpc10_encode(lpc10_encode_state_t *s, uint8_t code[], const int16_t amp[], int len);
+
+/*! Initialise an LPC10e decode context.
+    \param s The LPC10e context
+    \param error_correction ???
+    \return A pointer to the LPC10e context, or NULL for error. */
+lpc10_decode_state_t *lpc10_decode_init(lpc10_decode_state_t *st, int error_correction);
+
+int lpc10_decode_release(lpc10_decode_state_t *s);
+
+/*! Decode a buffer of LPC10e data to linear PCM.
+    \param s The LPC10e context.
+    \param amp The audio sample buffer.
+    \param code The LPC10e data.
+    \param len The number of bytes of LPC10e data to be decoded. This must be a multiple of 7,
+           as each frame is packed into 7 bytes.
+    \return The number of samples returned. */
+int lpc10_decode(lpc10_decode_state_t *s, int16_t amp[], const uint8_t code[], int len);
+
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
+/*- End of include ---------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/spandsp/modem_connect_tones.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/spandsp/modem_connect_tones.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,218 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * modem_connect_tones.c - Generation and detection of tones
+ * associated with modems calling and answering calls.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2006 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: modem_connect_tones.h,v 1.18 2008/08/10 03:42:38 steveu Exp $
+ */
+ 
+/*! \file */
+
+#if !defined(_SPANDSP_MODEM_CONNECT_TONES_H_)
+#define _SPANDSP_MODEM_CONNECT_TONES_H_
+
+/*! \page modem_connect_tones_page Modem connect tone detection
+
+\section modem_connect_tones_page_sec_1 What does it do?
+Some telephony terminal equipment, such as modems, require a channel which is as
+clear as possible. They use their own echo cancellation. If the network is also
+performing echo cancellation the two cancellors can end up squabbling about the
+nature of the channel, with bad results. A special tone is defined which should
+cause the network to disable any echo cancellation processes. This is the echo
+canceller disable tone.
+
+The tone detector's design assumes the channel is free of any DC component.
+
+\section modem_connect_tones_page_sec_2 How does it work?
+A sharp notch filter is implemented as a single bi-quad section. The presence of
+the 2100Hz disable tone is detected by comparing the notched filtered energy
+with the unfiltered energy. If the notch filtered energy is much lower than the
+unfiltered energy, then a large proportion of the energy must be at the notch
+frequency. This type of detector may seem less intuitive than using a narrow
+bandpass filter to isolate the energy at the notch freqency. However, a sharp
+bandpass implemented as an IIR filter rings badly. The reciprocal notch filter
+is very well behaved for our purpose. 
+*/
+
+enum
+{
+    /*! \brief This is reported when a tone stops. */
+    MODEM_CONNECT_TONES_NONE = 0,
+    /*! \brief CNG tone is a pure 1100Hz tone, in 0.5s bursts, with 3s silences in between. The
+               bursts repeat for as long as is required. */
+    MODEM_CONNECT_TONES_FAX_CNG = 1,
+    /*! \brief ANS tone is a pure continuous 2100Hz+-15Hz tone for 3.3s+-0.7s. */
+    MODEM_CONNECT_TONES_ANS = 2,
+    /*! \brief ANS with phase reversals tone is a 2100Hz+-15Hz tone for 3.3s+-0.7s, with a 180 degree
+               phase jump every 450ms+-25ms. */
+    MODEM_CONNECT_TONES_ANS_PR = 3,
+    /*! \brief The ANSam tone is a version of ANS with 20% of 15Hz+-0.1Hz AM modulation, as per V.8 */
+    MODEM_CONNECT_TONES_ANSAM = 4,
+    /*! \brief The ANSam with phase reversals tone is a version of ANS_PR with 20% of 15Hz+-0.1Hz AM
+               modulation, as per V.8 */
+    MODEM_CONNECT_TONES_ANSAM_PR = 5,
+    /*! \brief FAX preamble in a string of V.21 HDLC flag octets. This is only valid as a result of tone
+               detection. It should not be specified as a tone type to transmit or receive. */
+    MODEM_CONNECT_TONES_FAX_PREAMBLE = 6,
+    /*! \brief CED tone is the same as ANS tone. FAX preamble in a string of V.21 HDLC flag octets.
+               This is only valid as a tone type to receive. It is never reported as a detected tone
+               type. The report will either be for FAX preamble or CED/ANS tone. */
+    MODEM_CONNECT_TONES_FAX_CED_OR_PREAMBLE = 7
+};
+
+/*! \brief FAX CED tone is the same as ANS tone. */
+#define MODEM_CONNECT_TONES_FAX_CED MODEM_CONNECT_TONES_ANS
+
+/*!
+    Modem connect tones generator descriptor. This defines the state
+    of a single working instance of the tone generator.
+*/
+typedef struct
+{
+    int tone_type;
+
+    int32_t tone_phase_rate;
+    uint32_t tone_phase;
+    int level;
+    /*! \brief Countdown to the next phase hop */
+    int hop_timer;
+    /*! \brief Maximum duration timer */
+    int duration_timer;
+    uint32_t mod_phase;
+    int32_t mod_phase_rate;
+    int mod_level;
+} modem_connect_tones_tx_state_t;
+
+/*!
+    Modem connect tones receiver descriptor. This defines the state
+    of a single working instance of the tone detector.
+*/
+typedef struct
+{
+    /*! \brief The tone type being detected. */
+    int tone_type;
+    /*! \brief Callback routine, using to report detection of the tone. */
+    tone_report_func_t tone_callback;
+    /*! \brief An opaque pointer passed to tone_callback. */
+    void *callback_data;
+
+    /*! \brief The notch filter state. */
+    float z1;
+    float z2;
+    /*! \brief The in notch power estimate */
+    int notch_level;
+    /*! \brief The total channel power estimate */
+    int channel_level;
+    /*! \brief Sample counter for the small chunks of samples, after which a test is conducted. */
+    int chunk_remainder;
+    /*! \brief TRUE is the tone is currently confirmed present in the audio. */
+    int tone_present;
+    /*! \brief */
+    int tone_on;
+    /*! \brief A millisecond counter, to time the duration of tone sections. */
+    int tone_cycle_duration;
+    /*! \brief A count of the number of good cycles of tone reversal seen. */
+    int good_cycles;
+    /*! \brief TRUE if the tone has been seen since the last time the user tested for it */
+    int hit;
+    /*! \brief A V.21 FSK modem context used when searching for FAX preamble. */
+    fsk_rx_state_t v21rx;
+    /*! \brief The raw (stuffed) bit stream buffer. */
+    unsigned int raw_bit_stream;
+    /*! \brief The current number of bits in the octet in progress. */
+    int num_bits;
+    /*! \brief Number of consecutive flags seen so far. */
+    int flags_seen;
+    /*! \brief TRUE if framing OK has been announced. */
+    int framing_ok_announced;
+} modem_connect_tones_rx_state_t;
+
+#if defined(__cplusplus)
+extern "C"
+{
+#endif
+
+/*! \brief Initialise an instance of the modem connect tones generator.
+    \param s The context.
+*/
+modem_connect_tones_tx_state_t *modem_connect_tones_tx_init(modem_connect_tones_tx_state_t *s,
+                                                            int tone_type);
+
+/*! \brief Free an instance of the modem connect tones generator.
+    \param s The context.
+    \return 0 for OK, else -1.
+*/
+int modem_connect_tones_tx_free(modem_connect_tones_tx_state_t *s);
+
+/*! \brief Generate a block of modem connect tones samples.
+    \param s The context.
+    \param amp An array of signal samples.
+    \param len The number of samples to generate.
+    \return The number of samples generated.
+*/
+int modem_connect_tones_tx(modem_connect_tones_tx_state_t *s,
+                           int16_t amp[],
+                           int len);
+
+/*! \brief Process a block of samples through an instance of the modem connect
+           tones detector.
+    \param s The context.
+    \param amp An array of signal samples.
+    \param len The number of samples in the array.
+    \return The number of unprocessed samples.
+*/
+int modem_connect_tones_rx(modem_connect_tones_rx_state_t *s,
+                           const int16_t amp[],
+                           int len);
+                             
+/*! \brief Test if a modem_connect tone has been detected.
+    \param s The context.
+    \return TRUE if tone is detected, else FALSE.
+*/
+int modem_connect_tones_rx_get(modem_connect_tones_rx_state_t *s);
+
+/*! \brief Initialise an instance of the modem connect tones detector.
+    \param s The context.
+    \param tone_type The type of connect tone being tested for.
+    \param tone_callback An optional callback routine, used to report tones
+    \param user_data An opaque pointer passed to the callback routine,
+    \return A pointer to the context.
+*/
+modem_connect_tones_rx_state_t *modem_connect_tones_rx_init(modem_connect_tones_rx_state_t *s,
+                                                            int tone_type,
+                                                            tone_report_func_t tone_callback,
+                                                            void *user_data);
+
+/*! \brief Free an instance of the modem connect tones detector.
+    \param s The context.
+    \return 0 for OK, else -1. */
+int modem_connect_tones_rx_free(modem_connect_tones_rx_state_t *s);
+
+const char *modem_connect_tone_to_str(int tone);
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/spandsp/modem_echo.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/spandsp/modem_echo.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,144 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * modem_echo.h - An echo cancellor, suitable for electrical echos in GSTN modems
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2001, 2004 Steve Underwood
+ *
+ * Based on a bit from here, a bit from there, eye of toad,
+ * ear of bat, etc - plus, of course, my own 2 cents.
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: modem_echo.h,v 1.11 2008/04/17 14:27:00 steveu Exp $
+ */
+
+/*! \file */
+
+#if !defined(_SPANDSP_MODEM_ECHO_H_)
+#define _SPANDSP_MODEM_ECHO_H_
+
+/*! \page modem_echo_can_page Line echo cancellation for modems
+
+\section modem_echo_can_page_sec_1 What does it do?
+This module aims to cancel electrical echoes (e.g. from 2-4 wire hybrids)
+in modem applications. It is not very suitable for speech applications, which
+require additional refinements for satisfactory performance. It is, however, more
+efficient and better suited to modem applications. 
+
+\section modem_echo_can_page_sec_2 How does it work?
+The heart of the echo cancellor is an adaptive FIR filter. This is adapted to
+match the impulse response of the environment being cancelled. It must be long
+enough to adequately cover the duration of that impulse response. The signal
+being transmitted into the environment being cancelled is passed through the
+FIR filter. The resulting output is an estimate of the echo signal. This is
+then subtracted from the received signal, and the result should be an estimate
+of the signal which originates within the environment being cancelled (people
+talking in the room, or the signal from the far end of a telephone line) free
+from the echos of our own transmitted signal. 
+
+The FIR filter is adapted using the least mean squares (LMS) algorithm. This
+algorithm is attributed to Widrow and Hoff, and was introduced in 1960. It is
+the commonest form of filter adaption used in things like modem line equalisers
+and line echo cancellers. It works very well if the signal level is constant,
+which is true for a modem signal. To ensure good performa certain conditions must
+be met: 
+
+    - The transmitted signal has weak self-correlation.
+    - There is no signal being generated within the environment being cancelled.
+
+The difficulty is that neither of these can be guaranteed. If the adaption is
+performed while transmitting noise (or something fairly noise like, such as
+voice) the adaption works very well. If the adaption is performed while
+transmitting something highly correlative (e.g. tones, like DTMF), the adaption
+can go seriously wrong. The reason is there is only one solution for the
+adaption on a near random signal. For a repetitive signal, there are a number of
+solutions which converge the adaption, and nothing guides the adaption to choose
+the correct one. 
+
+\section modem_echo_can_page_sec_3 How do I use it?
+The echo cancellor processes both the transmit and receive streams sample by
+sample. The processing function is not declared inline. Unfortunately,
+cancellation requires many operations per sample, so the call overhead is only a
+minor burden. 
+*/
+
+#include "fir.h"
+
+/*!
+    Modem line echo canceller descriptor. This defines the working state for a line
+    echo canceller.
+*/
+typedef struct
+{
+    int adapt;
+    int taps;
+
+    fir16_state_t fir_state;
+    /*! Echo FIR taps (16 bit version) */
+    int16_t *fir_taps16;
+    /*! Echo FIR taps (32 bit version) */
+    int32_t *fir_taps32;
+
+    int tx_power;
+    int rx_power;
+
+    int curr_pos;
+} modem_echo_can_state_t;
+
+#if defined(__cplusplus)
+extern "C"
+{
+#endif
+
+/*! Create a modem echo canceller context.
+    \param len The length of the canceller, in samples.
+    eturn The new canceller context, or NULL if the canceller could not be created.
+*/
+modem_echo_can_state_t *modem_echo_can_create(int len);
+
+/*! Free a modem echo canceller context.
+    \param ec The echo canceller context.
+*/
+void modem_echo_can_free(modem_echo_can_state_t *ec);
+
+/*! Flush (reinitialise) a modem echo canceller context.
+    \param ec The echo canceller context.
+*/
+void modem_echo_can_flush(modem_echo_can_state_t *ec);
+
+/*! Set the adaption mode of a modem echo canceller context.
+    \param ec The echo canceller context.
+    \param adapt The mode.
+*/
+void modem_echo_can_adaption_mode(modem_echo_can_state_t *ec, int adapt);
+
+/*! Process a sample through a modem echo canceller.
+    \param ec The echo canceller context.
+    \param tx The transmitted audio sample.
+    \param rx The received audio sample.
+    eturn The clean (echo cancelled) received sample.
+*/
+int16_t modem_echo_can_update(modem_echo_can_state_t *ec, int16_t tx, int16_t rx);
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/spandsp/noise.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/spandsp/noise.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,134 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * noise.h - A low complexity audio noise generator, suitable for
+ *           real time generation (current just approx AWGN)
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2005 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: noise.h,v 1.13 2008/04/17 14:27:00 steveu Exp $
+ */
+
+/*! \file */
+
+#if !defined(_SPANDSP_NOISE_H_)
+#define _SPANDSP_NOISE_H_
+
+/*! \page noise_page Noise generation
+
+\section noise_page_sec_1 What does it do?
+It generates audio noise. Currently it only generates reasonable quality
+AWGN. It is designed to be of sufficiently low complexity to generate large
+volumes of reasonable quality noise, in real time.
+
+Hoth noise is used to model indoor ambient noise when evaluating communications
+systems such as telephones. It is named after D.F. Hoth, who made the first 
+systematic study of this. The official definition of Hoth noise is IEEE
+standard 269-2001 (revised from 269-1992), "Draft Standard Methods for Measuring
+Transmission Performance of Analog and Digital Telephone Sets, Handsets and Headsets."
+
+The table below gives the spectral density of Hoth noise, adjusted in level to produce
+a reading of 50 dBA.
+
+Freq (Hz)  Spectral     Bandwidth       Total power in
+           density      10 log_f        each 1/3 octave band
+           (dB SPL/Hz)  (dB)            (dB SPL)
+ 100        32.4        13.5            45.9
+ 125        30.9        14.7            45.5
+ 160        29.1        15.7            44.9
+ 200        27.6        16.5            44.1
+ 250        26.0        17.6            43.6
+ 315        24.4        18.7            43.1
+ 400        22.7        19.7            42.3
+ 500        21.1        20.6            41.7
+ 630        19.5        21.7            41.2
+ 800        17.8        22.7            40.4
+1000        16.2        23.5            39.7
+1250        14.6        24.7            39.3
+1600        12.9        25.7            38.7
+2000        11.3        26.5            37.8
+2500         9.6        27.6            37.2
+3150         7.8        28.7            36.5
+4000         5.4        29.7            34.8
+5000         2.6        30.6            33.2
+6300        -1.3        31.7            30.4
+8000        -6.6        32.7            26.0
+
+The tolerance for each 1/3rd octave band is ¡Ó3dB.
+
+\section awgn_page_sec_2 How does it work?
+The central limit theorem says if you add a few random numbers together,
+the result starts to look Gaussian. In this case we sum 8 random numbers.
+The result is fast, and perfectly good as a noise source for many purposes.
+It should not be trusted as a high quality AWGN generator, for elaborate
+modelling purposes.
+*/
+
+enum
+{
+    NOISE_CLASS_AWGN = 1,
+    NOISE_CLASS_HOTH
+};
+
+/*!
+    Noise generator descriptor. This contains all the state information for an instance
+    of the noise generator.
+ */
+typedef struct
+{
+    int class_of_noise;
+    int quality;
+    int32_t rms;
+    uint32_t rndnum;
+    int32_t state;
+} noise_state_t;
+
+#if defined(__cplusplus)
+extern "C"
+{
+#endif
+
+/*! Initialise an audio noise generator.
+    \brief Initialise an audio noise generator.
+    \param s The noise generator context.
+    \param seed A seed for the underlying random number generator.
+    \param level The noise power level in dBmO.
+    \param class_of_noise The class of noise (e.g. AWGN).
+    \param quality A parameter which permits speed and accuracy of the noise
+           generation to be adjusted.
+    \return A pointer to the noise generator context.
+*/
+noise_state_t *noise_init_dbm0(noise_state_t *s, int seed, float level, int class_of_noise, int quality);
+
+noise_state_t *noise_init_dbov(noise_state_t *s, int seed, float level, int class_of_noise, int quality);
+
+/*! Generate a sample of audio noise.
+    \brief Generate a sample of audio noise.
+    \param s The noise generator context.
+    \return The generated sample.
+*/
+int16_t noise(noise_state_t *s);
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/spandsp/oki_adpcm.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/spandsp/oki_adpcm.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,116 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * oki_adpcm.h - Conversion routines between linear 16 bit PCM data and
+ *		         OKI (Dialogic) ADPCM format.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2001 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: oki_adpcm.h,v 1.20 2008/04/17 14:27:00 steveu Exp $
+ */
+
+/*! \file */
+
+#if !defined(_SPANDSP_OKI_ADPCM_H_)
+#define _SPANDSP_OKI_ADPCM_H_
+
+/*! \page okiadpcm_page OKI (Dialogic) ADPCM encoding and decoding
+\section okiadpcm_page_sec_1 What does it do?
+OKI ADPCM is widely used in the CTI industry because it is the principal format
+supported by Dialogic. As the market leader, they tend to define "common
+practice". It offers a good balance of simplicity and quality at rates of
+24kbps or 32kbps. 32kbps is obtained by ADPCM compressing 8k samples/second linear
+PCM. 24kbps is obtained by resampling to 6k samples/second and using the same ADPCM
+compression algorithm on the slower samples.
+
+The algorithms for this ADPCM codec can be found in "PC Telephony - The complete guide
+to designing, building and programming systems using Dialogic and Related Hardware"
+by Bob Edgar. pg 272-276. */
+
+/*!
+    Oki (Dialogic) ADPCM conversion state descriptor. This defines the state of
+    a single working instance of the Oki ADPCM converter. This is used for
+    either linear to ADPCM or ADPCM to linear conversion.
+*/
+typedef struct
+{
+    /*! \brief The bit rate - 24000 or 32000. */
+    int bit_rate;
+    /*! \brief The last state of the ADPCM algorithm. */
+    int16_t last;
+    /*! \brief Current index into the step size table. */
+    int16_t step_index;
+    /*! \brief The compressed data byte in progress. */
+    uint8_t oki_byte;
+    /*! \brief The signal history for the sample rate converter. */
+    int16_t history[32];
+    /*! \brief Pointer into the history buffer. */
+    int ptr;
+    /*! \brief Odd/even sample counter. */
+    int mark;
+    /*! \brief Phase accumulator for the sample rate converter. */
+    int phase;
+} oki_adpcm_state_t;
+
+#if defined(__cplusplus)
+extern "C"
+{
+#endif
+
+/*! Initialise an Oki ADPCM encode or decode context.
+    \param s The Oki ADPCM context.
+    \param bit_rate The required bit rate for the ADPCM data.
+           The valid rates are 24000 and 32000.
+    \return A pointer to the Oki ADPCM context, or NULL for error. */
+oki_adpcm_state_t *oki_adpcm_init(oki_adpcm_state_t *s, int bit_rate);
+
+/*! Free an Oki ADPCM encode or decode context.
+    \param s The Oki ADPCM context.
+    \return 0 for OK. */
+int oki_adpcm_release(oki_adpcm_state_t *s);
+
+/*! Decode a buffer of Oki ADPCM data to linear PCM.
+    \param s The Oki ADPCM context.
+    \param amp The audio sample buffer.
+    \param oki_data
+    \param oki_bytes
+    \return The number of samples returned. */
+int oki_adpcm_decode(oki_adpcm_state_t *s,
+                     int16_t amp[],
+                     const uint8_t oki_data[],
+                     int oki_bytes);
+
+/*! Encode a buffer of linear PCM data to Oki ADPCM.
+    \param s The Oki ADPCM context.
+    \param oki_data The Oki ADPCM data produced
+    \param amp The audio sample buffer.
+    \param len The number of samples in the buffer.
+    \return The number of bytes of Oki ADPCM data produced. */
+int oki_adpcm_encode(oki_adpcm_state_t *s,
+                     uint8_t oki_data[],
+                     const int16_t amp[],
+                     int len);
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/spandsp/playout.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/spandsp/playout.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,210 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * playout.h
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2005 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: playout.h,v 1.11 2008/04/17 14:27:00 steveu Exp $
+ */
+
+#if !defined(_SPANDSP_PLAYOUT_H_)
+#define _SPANDSP_PLAYOUT_H_
+
+/*! \page playout_page Play-out (jitter buffering)
+\section playout_page_sec_1 What does it do?
+The play-out module provides a static or dynamic length buffer for received frames of
+audio or video data. It's goal is to maximise the receiver's tolerance of jitter in the
+timing of the received frames.
+
+Dynamic buffers are generally good for speech, since they adapt to provide the smallest delay
+consistent with a low rate of packets arriving too late to be used. For things like FoIP and
+MoIP, a static length of buffer is normally necessary. Any attempt to elastically change the
+buffer length would wreck a modem's data flow.
+*/
+
+/* Return codes */
+enum
+{
+    PLAYOUT_OK = 0,
+    PLAYOUT_ERROR,
+    PLAYOUT_EMPTY,
+    PLAYOUT_NOFRAME,
+    PLAYOUT_FILLIN,
+    PLAYOUT_DROP
+};
+
+/* Frame types */
+#define PLAYOUT_TYPE_CONTROL    0
+#define PLAYOUT_TYPE_SILENCE	1
+#define PLAYOUT_TYPE_SPEECH     2
+
+typedef int timestamp_t;
+
+typedef struct playout_frame_s
+{
+    /*! The actual frame data */
+    void *data;
+    /*! The type of frame */
+    int type;
+    /*! The timestamp assigned by the sending end */
+    timestamp_t sender_stamp;
+    /*! The timespan covered by the data in this frame */
+    timestamp_t sender_len;
+    /*! The timestamp assigned by the receiving end */
+    timestamp_t receiver_stamp;
+    /*! Pointer to the next earlier frame */
+    struct playout_frame_s *earlier;
+    /*! Pointer to the next later frame */
+    struct playout_frame_s *later;
+} playout_frame_t;
+
+/*!
+    Playout (jitter buffer) descriptor. This defines the working state
+    for a single instance of playout buffering.
+*/
+typedef struct
+{
+    /*! TRUE if the buffer is dynamically sized */
+    int dynamic;
+    /*! The minimum length (dynamic) or fixed length (static) of the buffer */
+    int min_length;
+    /*! The maximum length (dynamic) or fixed length (static) of the buffer */
+    int max_length;
+    /*! The target filter threshold for adjusting dynamic buffering. */
+    int dropable_threshold;
+
+    int start;
+
+    /*! The queued frame list */
+    playout_frame_t *first_frame;
+    playout_frame_t *last_frame;
+    /*! The free frame pool */
+    playout_frame_t *free_frames;
+
+    /*! The total frames input to the buffer, to date. */
+    int frames_in;
+    /*! The total frames output from the buffer, to date. */
+    int frames_out;
+    /*! The number of frames received out of sequence. */
+    int frames_oos;
+    /*! The number of frames which were discarded, due to late arrival. */
+    int frames_late;
+    /*! The number of frames which were never received. */
+    int frames_missing;
+    /*! The number of frames trimmed from the stream, due to buffer shrinkage. */
+    int frames_trimmed;
+
+    timestamp_t latest_expected;
+    /*! The present jitter adjustment */
+    timestamp_t current;
+    /*! The sender_stamp of the last speech frame */
+    timestamp_t last_speech_sender_stamp;
+    /*! The duration of the last speech frame */
+    timestamp_t last_speech_sender_len;
+
+    int not_first;
+    /*! The time since the target buffer length was last changed. */
+    timestamp_t since_last_step;
+    /*! Filter state for tracking the packets arriving just in time */
+    int32_t state_just_in_time;
+    /*! Filter state for tracking the packets arriving late */
+    int32_t state_late;
+    /*! The current target length of the buffer */
+    int target_buffer_length;
+    /*! The current actual length of the buffer, which may lag behind the target value */
+    int actual_buffer_length;
+} playout_state_t;
+
+#if defined(__cplusplus)
+extern "C"
+{
+#endif
+
+/*! Queue a frame
+    \param s The play-out context.
+    \param data The frame data.
+    \param sender_len Length of frame (for voice) in timestamp units.
+    \param sender_stamp Sending end's time stamp.
+    \param receiver_stamp Local time at which packet was received, in timestamp units.
+    \return One of
+        PLAYOUT_OK:  Frame queued OK.
+        PLAYOUT_ERROR: Some problem occured - e.g. out of memory. */
+int playout_put(playout_state_t *s, void *data, int type, timestamp_t sender_len, timestamp_t sender_stamp, timestamp_t receiver_stamp);
+
+/*! Get the next frame.
+    \param s The play-out context.
+    \param frame The frame.
+    \param sender_stamp The sender's timestamp.
+    \return One of
+        PLAYOUT_OK:  Suitable frame found.
+        PLAYOUT_DROP: A frame which should be dropped was found (e.g. it arrived too late).
+                      The caller should request the same time again when this occurs.
+        PLAYOUT_NOFRAME: There's no frame scheduled for this time.
+        PLAYOUT_FILLIN: Synthetic signal must be generated, as no real data is available for
+                        this time (either we need to grow, or there was a lost frame).
+        PLAYOUT_EMPTY: The buffer is empty.
+ */
+int	playout_get(playout_state_t *s, playout_frame_t *frame, timestamp_t sender_stamp);
+
+/*! Unconditionally get the first buffered frame. This may be used to clear out the queue, and free
+    all its contents, before the context is freed.
+    \param s The play-out context.
+    \return The frame, or NULL is the queue is empty. */
+playout_frame_t *playout_get_unconditional(playout_state_t *s);
+
+/*! Find the current length of the buffer.
+    \param s The play-out context.
+    \return The length of the buffer. */
+timestamp_t playout_current_length(playout_state_t *s);
+
+/*! Find the time at which the next queued frame is due to play.
+    Note: This value may change backwards as freshly received out of order frames are
+          added to the buffer.
+    \param s The play-out context.
+    \return The next timestamp. */
+timestamp_t playout_next_due(playout_state_t *s);
+
+/*! Create a new instance of play-out buffering.
+    \param min_length Minimum length of the buffer, in samples.
+    \param max_length Maximum length of the buffer, in samples. If this equals min_length, static
+           length buffering is used.
+    \return The new context */
+playout_state_t *playout_new(int min_length, int max_length);
+
+/*! Destroy an instance of play-out buffering.
+    \param s The play-out context to be destroyed */
+void playout_free(playout_state_t *s);
+
+/*! Reset an instance of play-out buffering.
+    NOTE:  The buffer should be empty before you call this function, otherwise
+           you will leak queued frames, and some internal structures
+    \param s The play-out context.
+    \param min_length Minimum length of the buffer, in samples.
+    \param max_length Maximum length of the buffer, in samples. If this equals min_length, static
+           length buffering is used. */
+void playout_restart(playout_state_t *s, int min_length, int max_length);
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/spandsp/plc.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/spandsp/plc.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,168 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * plc.h
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2004 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: plc.h,v 1.18 2008/04/17 14:27:00 steveu Exp $
+ */
+
+/*! \file */
+
+#if !defined(_SPANDSP_PLC_H_)
+#define _SPANDSP_PLC_H_
+
+/*! \page plc_page Packet loss concealment
+\section plc_page_sec_1 What does it do?
+The packet loss concealment module provides a synthetic fill-in signal, to minimise
+the audible effect of lost packets in VoIP applications. It is not tied to any
+particular codec, and could be used with almost any codec which does not
+specify its own procedure for packet loss concealment.
+
+Where a codec specific concealment procedure exists, that algorithm is usually built
+around knowledge of the characteristics of the particular codec. It will, therefore,
+generally give better results for that particular codec than this generic concealer will.
+
+The PLC code implements an algorithm similar to the one described in Appendix 1 of G.711.
+However, the G.711 algorithm is optimised for 10ms packets. Few people use such small
+packets. 20ms is a much more common value, and longer packets are also quite common. The
+algorithm has been adjusted with this in mind. Also, the G.711 approach causes an
+algorithmic delay, and requires significant buffer manipulation when there is no packet
+loss. The algorithm used here avoids this. It causes no delay, and achieves comparable
+quality with normal speech.
+
+Note that both this algorithm, and the one in G.711 are optimised for speech. For most kinds
+of music a much slower decay on bursts of lost packets give better results.
+
+\section plc_page_sec_2 How does it work?
+While good packets are being received, the plc_rx() routine keeps a record of the trailing
+section of the known speech signal. If a packet is missed, plc_fillin() is called to produce
+a synthetic replacement for the real speech signal. The average mean difference function
+(AMDF) is applied to the last known good signal, to determine its effective pitch.
+Based on this, the last pitch period of signal is saved. Essentially, this cycle of speech
+will be repeated over and over until the real speech resumes. However, several refinements
+are needed to obtain smooth pleasant sounding results.
+
+- The two ends of the stored cycle of speech will not always fit together smoothly. This can
+  cause roughness, or even clicks, at the joins between cycles. To soften this, the
+  1/4 pitch period of real speech preceeding the cycle to be repeated is blended with the last
+  1/4 pitch period of the cycle to be repeated, using an overlap-add (OLA) technique (i.e.
+  in total, the last 5/4 pitch periods of real speech are used).
+
+- The start of the synthetic speech will not always fit together smoothly with the tail of
+  real speech passed on before the erasure was identified. Ideally, we would like to modify
+  the last 1/4 pitch period of the real speech, to blend it into the synthetic speech. However,
+  it is too late for that. We could have delayed the real speech a little, but that would
+  require more buffer manipulation, and hurt the efficiency of the no-lost-packets case
+  (which we hope is the dominant case). Instead we use a degenerate form of OLA to modify
+  the start of the synthetic data. The last 1/4 pitch period of real speech is time reversed,
+  and OLA is used to blend it with the first 1/4 pitch period of synthetic speech. The result
+  seems quite acceptable.
+
+- As we progress into the erasure, the chances of the synthetic signal being anything like
+  correct steadily fall. Therefore, the volume of the synthesized signal is made to decay
+  linearly, such that after 50ms of missing audio it is reduced to silence.
+
+- When real speech resumes, an extra 1/4 pitch period of synthetic speech is blended with the
+  start of the real speech. If the erasure is small, this smoothes the transition. If the erasure
+  is long, and the synthetic signal has faded to zero, the blending softens the start up of the
+  real signal, avoiding a kind of "click" or "pop" effect that might occur with a sudden onset.
+
+\section plc_page_sec_3 How do I use it?
+Before audio is processed, call plc_init() to create an instance of the packet loss
+concealer. For each received audio packet that is acceptable (i.e. not including those being
+dropped for being too late) call plc_rx() to record the content of the packet. Note this may
+modify the packet a little after a period of packet loss, to blend real synthetic data smoothly.
+When a real packet is not available in time, call plc_fillin() to create a sythetic substitute.
+That's it!
+*/
+
+/*! Minimum allowed pitch (66 Hz) */
+#define PLC_PITCH_MIN           120
+/*! Maximum allowed pitch (200 Hz) */
+#define PLC_PITCH_MAX           40
+/*! Maximum pitch OLA window */
+#define PLC_PITCH_OVERLAP_MAX   (PLC_PITCH_MIN >> 2)
+/*! The length over which the AMDF function looks for similarity (20 ms) */
+#define CORRELATION_SPAN        160
+/*! History buffer length. The buffer much also be at leat 1.25 times
+    PLC_PITCH_MIN, but that is much smaller than the buffer needs to be for
+    the pitch assessment. */
+#define PLC_HISTORY_LEN         (CORRELATION_SPAN + PLC_PITCH_MIN)
+
+/*!
+    The generic packet loss concealer context.
+*/
+typedef struct
+{
+    /*! Consecutive erased samples */
+    int missing_samples;
+    /*! Current offset into pitch period */
+    int pitch_offset;
+    /*! Pitch estimate */
+    int pitch;
+    /*! Buffer for a cycle of speech */
+    float pitchbuf[PLC_PITCH_MIN];
+    /*! History buffer */
+    int16_t history[PLC_HISTORY_LEN];
+    /*! Current pointer into the history buffer */
+    int buf_ptr;
+} plc_state_t;
+
+
+#if defined(__cplusplus)
+extern "C"
+{
+#endif
+
+/*! Process a block of received audio samples for PLC.
+    \brief Process a block of received audio samples for PLC.
+    \param s The packet loss concealer context.
+    \param amp The audio sample buffer.
+    \param len The number of samples in the buffer.
+    \return The number of samples in the buffer. */
+int plc_rx(plc_state_t *s, int16_t amp[], int len);
+
+/*! Fill-in a block of missing audio samples.
+    \brief Fill-in a block of missing audio samples.
+    \param s The packet loss concealer context.
+    \param amp The audio sample buffer.
+    \param len The number of samples to be synthesised.
+    \return The number of samples synthesized. */
+int plc_fillin(plc_state_t *s, int16_t amp[], int len);
+
+/*! Initialise a packet loss concealer context.
+    \brief Initialise a PLC context.
+    \param s The packet loss concealer context.
+    \return A pointer to the the packet loss concealer context. */
+plc_state_t *plc_init(plc_state_t *s);
+
+/*! Free a packet loss concealer context.
+    \param s The packet loss concealer context.
+    \return 0 for OK. */
+int plc_free(plc_state_t *s);
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/spandsp/power_meter.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/spandsp/power_meter.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,120 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * power_meter.h
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2003 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: power_meter.h,v 1.15 2008/04/17 14:27:00 steveu Exp $
+ */
+
+#if !defined(_POWER_METER_H_)
+#define _POWER_METER_H_
+
+/*! \page power_meter_page Power metering
+
+\section power_meter_page_sec_1 What does it do?
+The power metering module implements a simple IIR type running power meter. The damping
+factor of the IIR is selectable when the meter instance is created.
+
+Note that the definition of dBOv is quite vague in most places - is it peak since wave,
+peak square wave, etc.? This code is based on the well defined wording in RFC3389:
+
+"For example, in the case of a u-law system, the reference would be a square wave with
+values +/-8031, and this square wave represents 0dBov.  This translates into 6.18dBm0".
+
+\section power_meter_page_sec_2 How does it work?
+*/
+
+/*!
+    Power meter descriptor. This defines the working state for a
+    single instance of a power measurement device.
+*/
+typedef struct
+{
+    /*! The shift factor, which controls the damping of the power meter. */
+    int shift;
+
+    /*! The current power reading. */
+    int32_t reading;
+} power_meter_t;
+
+#if defined(__cplusplus)
+extern "C"
+{
+#endif
+
+/*! Initialise a power meter context.
+    \brief Initialise a power meter context.
+    \param s The power meter context.
+    \param shift The shift to be used by the IIR filter.
+    \return The power meter context. */
+power_meter_t *power_meter_init(power_meter_t *s, int shift);
+
+/*! Change the damping factor of a power meter context.
+    \brief Change the damping factor of a power meter context.
+    \param s The power meter context.
+    \param shift The new shift to be used by the IIR filter.
+    \return The power meter context. */
+power_meter_t *power_meter_damping(power_meter_t *s, int shift);
+
+/*! Update a power meter.
+    \brief Update a power meter.
+    \param s The power meter context.
+    \param amp The amplitude of the new audio sample.
+    \return The current power meter reading. */
+int32_t power_meter_update(power_meter_t *s, int16_t amp);
+
+/*! Get the current power meter reading.
+    \brief Get the current power meter reading.
+    \param s The power meter context.
+    \return The current power meter reading. */
+int32_t power_meter_current(power_meter_t *s);
+
+/*! Get the current power meter reading, in dBm0.
+    \brief Get the current power meter reading, in dBm0.
+    \param s The power meter context.
+    \return The current power meter reading, in dBm0. */
+float power_meter_current_dbm0(power_meter_t *s);
+
+/*! Get the current power meter reading, in dBOv.
+    \brief Get the current power meter reading, in dBOv.
+    \param s The power meter context.
+    \return The current power meter reading, in dBOv. */
+float power_meter_current_dbov(power_meter_t *s);
+
+/*! Get the power meter reading which represents a specified power level in dBm0.
+    \brief Get the current power meter reading, in dBm0.
+    \param level A power level, in dB0m.
+    \return The equivalent power meter reading. */
+int32_t power_meter_level_dbm0(float level);
+
+/*! Get the power meter reading which represents a specified power level in dBOv.
+    \brief Get the current power meter reading, in dBOv.
+    \param level A power level, in dBOv.
+    \return The equivalent power meter reading. */
+int32_t power_meter_level_dbov(float level);
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/spandsp/queue.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/spandsp/queue.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,186 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * queue.h - simple in process message queuing
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2004 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: queue.h,v 1.16 2008/05/30 13:51:28 steveu Exp $
+ */
+
+/*! \file */
+
+/*! \page queue_page Queuing
+\section queue_page_sec_1 What does it do?
+This module provides lock free queuing for either octet streams or messages.
+Specifically, lock free means one thread can write and another can read without
+locking the queue. It does NOT means a free-for-all is possible, with many
+threads writing or many threads reading. Those things would require locking,
+to avoid conflicts between the multiple threads acting on one end of the queue.
+
+\section queue_page_sec_2 How does it work?
+???.
+*/
+
+#if !defined(_SPANDSP_QUEUE_H_)
+#define _SPANDSP_QUEUE_H_
+
+/*! Flag bit to indicate queue reads are atomic operations. This must be set
+    if the queue is to be used with the message oriented functions. */
+#define QUEUE_READ_ATOMIC   0x0001
+/*! Flag bit to indicate queue writes are atomic operations. This must be set
+    if the queue is to be used with the message oriented functions. */
+#define QUEUE_WRITE_ATOMIC  0x0002
+
+/*!
+    Queue descriptor. This defines the working state for a single instance of
+    a byte stream or message oriented queue.
+*/
+typedef struct
+{
+    /*! \brief Flags indicating the mode of the queue. */
+    int flags;
+    /*! \brief The length of the data buffer. */
+    int len;
+    /*! \brief The buffer input pointer. */
+    volatile int iptr;
+    /*! \brief The buffer output pointer. */
+    volatile int optr;
+    /*! \brief The data buffer, sized at the time the structure is created. */
+    uint8_t data[];
+} queue_state_t;
+
+#define QUEUE_STATE_T_SIZE(len) (sizeof(queue_state_t) + len + 1)
+
+#if defined(__cplusplus)
+extern "C"
+{
+#endif
+
+/*! Check if a queue is empty.
+    \brief Check if a queue is empty.
+    \param s The queue context.
+    \return TRUE if empty, else FALSE. */
+int queue_empty(queue_state_t *s);
+
+/*! Check the available free space in a queue's buffer.
+    \brief Check available free space.
+    \param s The queue context.
+    \return The number of bytes of free space. */
+int queue_free_space(queue_state_t *s);
+
+/*! Check the contents of a queue.
+    \brief Check the contents of a queue.
+    \param s The queue context.
+    \return The number of bytes in the queue. */
+int queue_contents(queue_state_t *s);
+
+/*! Flush the contents of a queue.
+    \brief Flush the contents of a queue.
+    \param s The queue context. */
+void queue_flush(queue_state_t *s);
+
+/*! Copy bytes from a queue. This is similar to queue_read, but
+    the data remains in the queue.
+    \brief Copy bytes from a queue.
+    \param s The queue context.
+    \param buf The buffer into which the bytes will be read.
+    \param len The length of the buffer.
+    \return the number of bytes returned. */
+int queue_view(queue_state_t *s, uint8_t *buf, int len);
+
+/*! Read bytes from a queue.
+    \brief Read bytes from a queue.
+    \param s The queue context.
+    \param buf The buffer into which the bytes will be read.
+    \param len The length of the buffer.
+    \return the number of bytes returned. */
+int queue_read(queue_state_t *s, uint8_t *buf, int len);
+
+/*! Read a byte from a queue.
+    \brief Read a byte from a queue.
+    \param s The queue context.
+    \return the byte, or -1 if the queue is empty. */
+int queue_read_byte(queue_state_t *s);
+
+/*! Write bytes to a queue.
+    \brief Write bytes to a queue.
+    \param s The queue context.
+    \param buf The buffer containing the bytes to be written.
+    \param len The length of the buffer.
+    \return the number of bytes actually written. */
+int queue_write(queue_state_t *s, const uint8_t *buf, int len);
+
+/*! Write a byte to a queue.
+    \brief Write a byte to a queue.
+    \param s The queue context.
+    \param byte The byte to be written.
+    \return the number of bytes actually written. */
+int queue_write_byte(queue_state_t *s, uint8_t byte);
+
+/*! Test the length of the message at the head of a queue.
+    \brief Test message length.
+    \param s The queue context.
+    \return The length of the next message, in byte. If there are
+            no messages in the queue, -1 is returned. */
+int queue_state_test_msg(queue_state_t *s);
+
+/*! Read a message from a queue. If the message is longer than the buffer
+    provided, only the first len bytes of the message will be returned. The
+    remainder of the message will be discarded.
+    \brief Read a message from a queue.
+    \param s The queue context.
+    \param buf The buffer into which the message will be read.
+    \param len The length of the buffer.
+    \return The number of bytes returned. If there are
+            no messages in the queue, -1 is returned. */
+int queue_read_msg(queue_state_t *s, uint8_t *buf, int len);
+
+/*! Write a message to a queue.
+    \brief Write a message to a queue.
+    \param s The queue context.
+    \param buf The buffer from which the message will be written.
+    \param len The length of the message.
+    \return The number of bytes actually written. */
+int queue_write_msg(queue_state_t *s, const uint8_t *buf, int len);
+
+/*! Initialise a queue.
+    \brief Initialise a queue.
+    \param s The queue context. If is imperative that the context this
+           points to is immediately followed by a buffer of the required
+           size + 1 octet.
+    \param len The length of the queue's buffer.
+    \param flags Flags controlling the operation of the queue.
+           Valid flags are QUEUE_READ_ATOMIC and QUEUE_WRITE_ATOMIC.
+    \return A pointer to the context if OK, else NULL. */
+queue_state_t *queue_init(queue_state_t *s, int len, int flags);
+
+/*! Delete a queue.
+    \brief Delete a queue.
+    \param s The queue context.
+    \return 0 if deleted OK, else -1. */
+int queue_free(queue_state_t *s);
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/spandsp/schedule.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/spandsp/schedule.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,83 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * schedule.h
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2004 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: schedule.h,v 1.16 2008/04/17 14:27:00 steveu Exp $
+ */
+
+/*! \file */
+
+/*! \page schedule_page Scheduling
+\section schedule_page_sec_1 What does it do?
+???.
+
+\section schedule_page_sec_2 How does it work?
+???.
+*/
+
+#if !defined(_SPANDSP_SCHEDULE_H_)
+#define _SPANDSP_SCHEDULE_H_
+
+typedef struct span_sched_state_s span_sched_state_t;
+
+typedef void (*span_sched_callback_func_t)(span_sched_state_t *s, void *user_data);
+
+/*! A scheduled event entry. */
+typedef struct
+{
+    uint64_t when;
+    span_sched_callback_func_t callback;
+    void *user_data;
+} span_sched_t;
+
+/*! A scheduled event queue. */
+struct span_sched_state_s
+{
+    uint64_t ticker;
+    int allocated;
+    int max_to_date;
+    span_sched_t *sched;
+    logging_state_t logging;
+};
+
+#if defined(__cplusplus)
+extern "C"
+{
+#endif
+
+uint64_t span_schedule_next(span_sched_state_t *s);
+uint64_t span_schedule_time(span_sched_state_t *s);
+
+int span_schedule_event(span_sched_state_t *s, int us, span_sched_callback_func_t function, void *user_data);
+void span_schedule_update(span_sched_state_t *s, int us);
+void span_schedule_del(span_sched_state_t *s, int id);
+
+span_sched_state_t *span_schedule_init(span_sched_state_t *s);
+int span_schedule_release(span_sched_state_t *s);
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/spandsp/sig_tone.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/spandsp/sig_tone.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,287 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * sig_tone.h - Signalling tone processing for the 2280Hz, 2400Hz, 2600Hz
+ *              and similar signalling tone used in older protocols.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2004 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: sig_tone.h,v 1.14 2008/07/29 14:15:21 steveu Exp $
+ */
+
+/*! \file */
+
+/*! \page sig_tone_page The signaling tone processor
+\section sig_tone_sec_1 What does it do?
+The signaling tone processor handles the 2280Hz, 2400Hz and 2600Hz tones, used
+in many analogue signaling procotols, and digital ones derived from them.
+
+\section sig_tone_sec_2 How does it work?
+Most single and two voice frequency signalling systems share many features, as these
+features have developed in similar ways over time, to address the limitations of
+early tone signalling systems.
+
+The usual practice is to start the generation of tone at a high energy level, so a
+strong signal is available at the receiver, for crisp tone detection. If the tone
+remains on for a significant period, the energy level is reduced, to minimise crosstalk.
+During the signalling transitions, only the tone is sent through the channel, and the media
+signal is suppressed. This means the signalling receiver has a very clean signal to work with,
+allowing for crisp detection of the signalling tone. However, when the signalling tone is on
+for extended periods, there may be supervisory information in the media signal, such as voice
+announcements. To allow these to pass through the system, the signalling tone is mixed with
+the media signal. It is the job of the signalling receiver to separate the signalling tone
+and the media. The necessary filtering may degrade the quality of the voice signal, but at
+least supervisory information may be heard.
+*/
+
+#if !defined(_SPANDSP_SIG_TONE_H_)
+#define _SPANDSP_SIG_TONE_H_
+
+typedef int (*sig_tone_func_t)(void *user_data, int what);
+
+/* The optional tone sets */
+enum
+{
+    /*! European 2280Hz signaling tone */
+    SIG_TONE_2280HZ = 1,
+    /*! US 2600Hz signaling tone */
+    SIG_TONE_2600HZ,
+    /*! US 2400Hz + 2600Hz signaling tones */
+    SIG_TONE_2400HZ_2600HZ
+};
+
+enum
+{
+    /*! Signaling tone 1 is present */
+    SIG_TONE_1_PRESENT          = 0x001,
+    SIG_TONE_1_CHANGE           = 0x002,
+    /*! Signaling tone 2 is present */
+    SIG_TONE_2_PRESENT          = 0x004,
+    SIG_TONE_2_CHANGE           = 0x008,
+    /*! The media signal is passing through. Tones might be added to it. */
+    SIG_TONE_TX_PASSTHROUGH     = 0x010,
+    /*! The media signal is passing through. Tones might be extracted from it, if detected. */
+    SIG_TONE_RX_PASSTHROUGH     = 0x020,
+    SIG_TONE_UPDATE_REQUEST     = 0x100
+};
+
+/*!
+    Signaling tone descriptor. This defines the working state for a
+    single instance of the transmit and receive sides of a signaling
+    tone processor.
+*/
+typedef struct
+{
+    /*! \brief The tones used. */
+    int tone_freq[2];
+    /*! \brief The high and low tone amplitudes. */
+    int tone_amp[2];
+
+    /*! \brief The delay, in audio samples, before the high level tone drops
+               to a low level tone. */
+    int high_low_timeout;
+
+    /*! \brief Some signaling tone detectors use a sharp initial filter,
+               changing to a broader band filter after some delay. This
+               parameter defines the delay. 0 means it never changes. */
+    int sharp_flat_timeout;
+
+    /*! \brief Parameters to control the behaviour of the notch filter, used
+               to remove the tone from the voice path in some protocols. */
+    int notch_lag_time;
+    int notch_allowed;
+
+    /*! \brief The tone on persistence check, in audio samples. */
+    int tone_on_check_time;
+    /*! \brief The tone off persistence check, in audio samples. */
+    int tone_off_check_time;
+
+    int tones;
+    /*! \brief The coefficients for the cascaded bi-quads notch filter. */
+    struct
+    {
+#if defined(SPANDSP_USE_FIXED_POINT)
+        int32_t notch_a1[3];
+        int32_t notch_b1[3];
+        int32_t notch_a2[3];
+        int32_t notch_b2[3];
+        int notch_postscale;
+#else
+        float notch_a1[3];
+        float notch_b1[3];
+        float notch_a2[3];
+        float notch_b2[3];
+#endif
+    } tone[2];
+
+
+    /*! \brief Flat mode bandpass bi-quad parameters */
+#if defined(SPANDSP_USE_FIXED_POINT)
+    int32_t broad_a[3];
+    int32_t broad_b[3];
+    int broad_postscale;
+#else
+    float broad_a[3];
+    float broad_b[3];
+#endif
+    /*! \brief The coefficients for the post notch leaky integrator. */
+    int32_t notch_slugi;
+    int32_t notch_slugp;
+
+    /*! \brief The coefficients for the post modulus leaky integrator in the
+               unfiltered data path.  The prescale value incorporates the
+               detection ratio. This is called the guard ratio in some
+               protocols. */
+    int32_t unfiltered_slugi;
+    int32_t unfiltered_slugp;
+
+    /*! \brief The coefficients for the post modulus leaky integrator in the
+               bandpass filter data path. */
+    int32_t broad_slugi;
+    int32_t broad_slugp;
+
+    /*! \brief Masks which effectively threshold the notched, weighted and
+               bandpassed data. */
+    int32_t notch_threshold;
+    int32_t unfiltered_threshold;
+    int32_t broad_threshold;
+} sig_tone_descriptor_t;
+
+typedef struct
+{
+    /*! \brief The callback function used to handle signaling changes. */
+    sig_tone_func_t sig_update;
+    /*! \brief A user specified opaque pointer passed to the callback function. */
+    void *user_data;
+
+    sig_tone_descriptor_t *desc;
+
+    /*! The scaling values for the high and low level tones */
+    int32_t tone_scaling[2];
+    /*! The sample timer, used to switch between the high and low level tones. */
+    int high_low_timer;
+
+    /*! The phase rates for the one or two tones */
+    int32_t phase_rate[2];
+    /*! The phase accumulators for the one or two tones */
+    uint32_t phase_acc[2];
+
+    int current_tx_tone;
+    int current_tx_timeout;
+    int signaling_state_duration;
+} sig_tone_tx_state_t;
+
+typedef struct
+{
+    /*! \brief The callback function used to handle signaling changes. */
+    sig_tone_func_t sig_update;
+    /*! \brief A user specified opaque pointer passed to the callback function. */
+    void *user_data;
+
+    sig_tone_descriptor_t *desc;
+
+    int current_rx_tone;
+    int high_low_timer;
+
+    struct
+    {
+        /*! \brief The z's for the notch filter */
+#if defined(SPANDSP_USE_FIXED_POINT)
+        int32_t notch_z1[3];
+        int32_t notch_z2[3];
+#else
+        float notch_z1[3];
+        float notch_z2[3];
+#endif
+
+        /*! \brief The z's for the notch integrators. */
+        int32_t notch_zl;
+    } tone[2];
+
+    /*! \brief The z's for the weighting/bandpass filter. */
+#if defined(SPANDSP_USE_FIXED_POINT)
+    int32_t broad_z[3];
+#else
+    float broad_z[3];
+#endif
+    /*! \brief The z for the broadband integrator. */
+    int32_t broad_zl;
+
+    int flat_mode;
+    int tone_present;
+    int notch_enabled;
+    int flat_mode_timeout;
+    int notch_insertion_timeout;
+    int tone_persistence_timeout;
+    
+    int signaling_state_duration;
+} sig_tone_rx_state_t;
+
+#if defined(__cplusplus)
+extern "C"
+{
+#endif
+
+/*! Process a block of received audio samples.
+    \brief Process a block of received audio samples.
+    \param s The signaling tone context.
+    \param amp The audio sample buffer.
+    \param len The number of samples in the buffer.
+    \return The number of samples unprocessed. */
+int sig_tone_rx(sig_tone_rx_state_t *s, int16_t amp[], int len);
+
+/*! Initialise a signaling tone receiver context.
+    \brief Initialise a signaling tone context.
+    \param s The signaling tone context.
+    \param tone_type The type of signaling tone.
+    \param sig_update Callback function to handle signaling updates.
+    \param user_data An opaque pointer.
+    \return A pointer to the signalling tone context, or NULL if there was a problem. */
+sig_tone_rx_state_t *sig_tone_rx_init(sig_tone_rx_state_t *s, int tone_type, sig_tone_func_t sig_update, void *user_data);
+
+/*! Generate a block of signaling tone audio samples.
+    \brief Generate a block of signaling tone audio samples.
+    \param s The signaling tone context.
+    \param amp The audio sample buffer.
+    \param len The number of samples to be generated.
+    \return The number of samples actually generated. */
+int sig_tone_tx(sig_tone_tx_state_t *s, int16_t amp[], int len);
+
+/*! Set the tone mode.
+    \brief Set the tone mode.
+    \param s The signaling tone context.
+    \param mode The new mode for the transmitted tones. */
+void sig_tone_tx_set_mode(sig_tone_tx_state_t *s, int mode);
+
+/*! Initialise a signaling tone transmitter context.
+    \brief Initialise a signaling tone context.
+    \param s The signaling tone context.
+    \param tone_type The type of signaling tone.
+    \param sig_update Callback function to handle signaling updates.
+    \param user_data An opaque pointer.
+    \return A pointer to the signalling tone context, or NULL if there was a problem. */
+sig_tone_tx_state_t *sig_tone_tx_init(sig_tone_tx_state_t *s, int tone_type, sig_tone_func_t sig_update, void *user_data);
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/spandsp/silence_gen.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/spandsp/silence_gen.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,138 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * silence_gen.c - A silence generator, for inserting timed silences.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2006 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: silence_gen.h,v 1.12 2008/07/26 04:53:00 steveu Exp $
+ */
+
+#if !defined(_SPANDSP_SILENCE_GEN_H_)
+#define _SPANDSP_SILENCE_GEN_H_
+
+typedef struct
+{
+    /*! \brief The callback function used to report status changes. */
+    modem_tx_status_func_t status_handler;
+    /*! \brief A user specified opaque pointer passed to the status function. */
+    void *status_user_data;
+
+    int remaining_samples;
+    int total_samples;
+} silence_gen_state_t;
+
+#if defined(__cplusplus)
+extern "C"
+{
+#endif
+
+/*! Generate a block of silent audio samples.
+    \brief Generate a block of silent audio samples.
+    \param s The silence generator context.
+    \param amp The audio sample buffer.
+    \param max_len The number of samples to be generated.
+    \return The number of samples actually generated. This will be zero when
+            there is nothing to send.
+*/
+int silence_gen(silence_gen_state_t *s, int16_t *amp, int max_len);
+
+/*! Set a silence generator context to output continuous silence.
+    \brief Set a silence generator context to output continuous silence.
+    \param s The silence generator context.
+*/
+void silence_gen_always(silence_gen_state_t *s);
+
+/*! Set a silence generator context to output a specified period of silence.
+    \brief Set a silence generator context to output a specified period of silence.
+    \param s The silence generator context.
+    \param silent_samples The number of samples to be generated.
+*/
+void silence_gen_set(silence_gen_state_t *s, int silent_samples);
+
+/*! Alter the period of a silence generator context by a specified amount.
+    \brief Alter the period of a silence generator context by a specified amount.
+    \param s The silence generator context.
+    \param silent_samples The number of samples to change the setting by. A positive number
+                          increases the duration. A negative number reduces it. The duration
+                          is prevented from going negative.
+*/
+void silence_gen_alter(silence_gen_state_t *s, int silent_samples);
+
+/*! Find how long a silence generator context has to run.
+    \brief Find how long a silence generator context has to run.
+    \param s The silence generator context.
+    \return The number of samples remaining.
+*/
+int silence_gen_remainder(silence_gen_state_t *s);
+
+/*! Find the total silence generated to date by a silence generator context.
+    \brief Find the total silence generated to date.
+    \param s The silence generator context.
+    \return The number of samples generated.
+*/
+int silence_gen_generated(silence_gen_state_t *s);
+
+/*! Change the status reporting function associated with a silence generator context.
+    \brief Change the status reporting function associated with a silence generator context.
+    \param s The silence generator context.
+    \param handler The callback routine used to report status changes.
+    \param user_data An opaque pointer. */
+void silence_gen_status_handler(silence_gen_state_t *s, modem_tx_status_func_t handler, void *user_data);
+
+/*! Initialise a timed silence generator context.
+    \brief Initialise a timed silence generator context.
+    \param s The silence generator context.
+    \param silent_samples The initial number of samples to set the silence to.
+    \return A pointer to the silence generator context.
+*/
+silence_gen_state_t *silence_gen_init(silence_gen_state_t *s, int silent_samples);
+
+/* The following dummy routines, to absorb data, don't really have a proper home,
+   so they have been put here. */
+
+/*! A dummy routine to use as a receive callback, when we aren't really
+    trying to process what is received. It just absorbs and ignores the
+    data.
+    \brief Dummy receive callback.
+    \param user_data The context.
+    \param amp The signal.buffer
+    \param len The length of the signal buffer
+    \return 0.
+*/
+int span_dummy_rx(void *user_data, const int16_t amp[], int len);
+
+/*! A dummy routine to use as a signal modifier callback, when we aren't
+    really trying to process the signal. It just returns without affecting
+    anything.
+    \brief Dummy signal modifier callback.
+    \param user_data The context.
+    \param amp The signal.buffer
+    \param len The length of the signal buffer
+    \return 0.
+*/
+int span_dummy_mod(void *user_data, int16_t amp[], int len);
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/spandsp/super_tone_rx.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/spandsp/super_tone_rx.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,175 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * super_tone_rx.h - Flexible telephony supervisory tone detection.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2003 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: super_tone_rx.h,v 1.17 2008/06/13 14:46:52 steveu Exp $
+ */
+
+#if !defined(_SPANDSP_SUPER_TONE_RX_H_)
+#define _SPANDSP_SUPER_TONE_RX_H_
+
+/*! \page super_tone_rx_page Supervisory tone detection
+
+\section super_tone_rx_page_sec_1 What does it do?
+
+The supervisory tone detector may be configured to detect most of the world's
+telephone supervisory tones - things like ringback, busy, number unobtainable,
+and so on.
+
+\section super_tone_rx_page_sec_2 How does it work?
+
+The supervisory tone detector is passed a series of data structures describing
+the tone patterns - the frequencies and cadencing - of the tones to be searched
+for. It constructs one or more Goertzel filters to monitor the required tones.
+If tones are close in frequency a single Goertzel set to the centre of the
+frequency range will be used. This optimises the efficiency of the detector. The
+Goertzel filters are applied without applying any special window functional
+(i.e. they use a rectangular window), so they have a sinc like response.
+However, for most tone patterns their rejection qualities are adequate. 
+
+The detector aims to meet the need of the standard call progress tones, to
+ITU-T E.180/Q.35 (busy, dial, ringback, reorder). Also, the extended tones,
+to ITU-T E.180, Supplement 2 and EIA/TIA-464-A (recall dial tone, special
+ringback tone, intercept tone, call waiting tone, busy verification tone,
+executive override tone, confirmation tone).
+*/
+
+/*! Tone detection indication callback routine */
+typedef void (*tone_report_func_t)(void *user_data, int code, int level, int delay);
+
+#define BINS            128
+
+typedef struct
+{
+    int f1;
+    int f2;
+    int recognition_duration;
+    int min_duration;
+    int max_duration;
+} super_tone_rx_segment_t;
+
+typedef struct
+{
+    int used_frequencies;
+    int monitored_frequencies;
+    int pitches[BINS/2][2];
+    int tones;
+    super_tone_rx_segment_t **tone_list;
+    int *tone_segs;
+    goertzel_descriptor_t *desc;
+} super_tone_rx_descriptor_t;
+
+typedef struct
+{
+    super_tone_rx_descriptor_t *desc;
+    float energy;
+    int detected_tone;
+    int rotation;
+    tone_report_func_t tone_callback;
+    void (*segment_callback)(void *data, int f1, int f2, int duration);
+    void *callback_data;
+    super_tone_rx_segment_t segments[11];
+    goertzel_state_t state[];
+} super_tone_rx_state_t;
+
+#if defined(__cplusplus)
+extern "C"
+{
+#endif
+
+/*! Create a new supervisory tone detector descriptor.
+    \param desc The supervisory tone set desciptor. If NULL, the routine will allocate space for a
+                descriptor.
+    \return The supervisory tone set descriptor.
+*/
+super_tone_rx_descriptor_t *super_tone_rx_make_descriptor(super_tone_rx_descriptor_t *desc);
+
+/*! Free a supervisory tone detector descriptor.
+    \param desc The supervisory tone set desciptor.
+    \return 0 for OK, -1 for fail.
+*/
+int super_tone_rx_free_descriptor(super_tone_rx_descriptor_t *desc);
+
+/*! Add a new tone pattern to a supervisory tone detector set.
+    \param desc The supervisory tone set descriptor.
+    \return The new tone ID. */
+int super_tone_rx_add_tone(super_tone_rx_descriptor_t *desc);
+
+/*! Add a new tone pattern element to a tone pattern in a supervisory tone detector.
+    \param desc The supervisory tone set desciptor.
+    \param tone The tone ID within the descriptor.
+    \param f1 Frequency 1 (-1 for a silent period).
+    \param f2 Frequency 2 (-1 for a silent period, or only one frequency).
+    \param min The minimum duration, in ms.
+    \param max The maximum duration, in ms.
+    \return The new number of elements in the tone description.
+*/
+int super_tone_rx_add_element(super_tone_rx_descriptor_t *desc,
+                              int tone,
+                              int f1,
+                              int f2,
+                              int min,
+                              int max);
+
+/*! Initialise a supervisory tone detector.
+    \param s The supervisory tone detector context.
+    \param desc The tone descriptor.
+    \param callback The callback routine called to report the valid detection or termination of
+           one of the monitored tones.
+    \param user_data An opaque pointer passed when calling the callback routine.
+    \return The supervisory tone detector context.
+*/
+super_tone_rx_state_t *super_tone_rx_init(super_tone_rx_state_t *s,
+                                          super_tone_rx_descriptor_t *desc,
+                                          tone_report_func_t callback,
+                                          void *user_data);
+
+/*! Release a supervisory tone detector.
+    \param s The supervisory tone context.
+    \return 0 for OK, -1 for fail.
+*/
+int super_tone_rx_free(super_tone_rx_state_t *s);
+
+/*! Define a callback routine to be called each time a tone pattern element is complete. This is
+    mostly used when analysing a tone.
+    \param s The supervisory tone context.
+    \param callback The callback routine.
+*/
+void super_tone_rx_segment_callback(super_tone_rx_state_t *s,
+                                    void (*callback)(void *data, int f1, int f2, int duration));
+
+/*! Apply supervisory tone detection processing to a block of audio samples.
+    \brief Apply supervisory tone detection processing to a block of audio samples.
+    \param super The supervisory tone context.
+    \param amp The audio sample buffer.
+    \param samples The number of samples in the buffer.
+    \return The number of samples processed.
+*/
+int super_tone_rx(super_tone_rx_state_t *super, const int16_t amp[], int samples);
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/spandsp/super_tone_tx.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/spandsp/super_tone_tx.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,101 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * super_tone_tx.h - Flexible telephony supervisory tone generation.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2001 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: super_tone_tx.h,v 1.13 2008/04/17 14:27:00 steveu Exp $
+ */
+
+#if !defined(_SPANDSP_SUPER_TONE_TX_H_)
+#define _SPANDSP_SUPER_TONE_TX_H_
+
+/*! \page super_tone_tx_page Supervisory tone generation
+
+\section super_tone_tx_page_sec_1 What does it do?
+
+The supervisory tone generator may be configured to generate most of the world's
+telephone supervisory tones - things like ringback, busy, number unobtainable,
+and so on. It uses tree structure tone descriptions, which can deal with quite
+complex cadence patterns. 
+
+\section super_tone_tx_page_sec_2 How does it work?
+
+*/
+
+typedef struct super_tone_tx_step_s super_tone_tx_step_t;
+
+struct super_tone_tx_step_s
+{
+    tone_gen_tone_descriptor_t tone[4];
+    int tone_on;
+    int length;
+    int cycles;
+    super_tone_tx_step_t *next;
+    super_tone_tx_step_t *nest;
+};
+
+typedef struct
+{
+    tone_gen_tone_descriptor_t tone[4];
+    uint32_t phase[4];
+    int current_position;
+    int level;
+    super_tone_tx_step_t *levels[4];
+    int cycles[4];
+} super_tone_tx_state_t;
+
+#if defined(__cplusplus)
+extern "C"
+{
+#endif
+
+super_tone_tx_step_t *super_tone_tx_make_step(super_tone_tx_step_t *s,
+                                              float f1,
+                                              float l1,
+                                              float f2,
+                                              float l2,
+                                              int length,
+                                              int cycles);
+
+void super_tone_tx_free(super_tone_tx_step_t *s);
+
+/*! Initialise a supervisory tone generator.
+    \brief Initialise a supervisory tone generator.
+    \param s The supervisory tone generator context.
+    \param tree The supervisory tone tree to be generated.
+    \return The supervisory tone generator context. */
+super_tone_tx_state_t *super_tone_tx_init(super_tone_tx_state_t *s, super_tone_tx_step_t *tree);
+
+/*! Generate a block of audio samples for a supervisory tone pattern.
+    \brief Generate a block of audio samples for a supervisory tone pattern.
+    \param s The supervisory tone context.
+    \param amp The audio sample buffer.
+    \param max_samples The maximum number of samples to be generated.
+    \return The number of samples generated. */
+int super_tone_tx(super_tone_tx_state_t *s, int16_t amp[], int max_samples);
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/spandsp/t30.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/spandsp/t30.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,878 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * t30.h - definitions for T.30 fax processing
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2003 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: t30.h,v 1.114 2008/08/13 00:11:30 steveu Exp $
+ */
+
+/*! \file */
+
+#if !defined(_SPANDSP_T30_H_)
+#define _SPANDSP_T30_H_
+
+/*! \page t30_page T.30 FAX protocol handling
+
+\section t30_page_sec_1 What does it do?
+The T.30 protocol is the core protocol used for FAX transmission. This module
+implements most of its key featrues. It does not interface to the outside work.
+Seperate modules do that for T.38, analogue line, and other forms of FAX
+communication.
+
+Current features of this module include:
+
+    - FAXing to and from multi-page TIFF/F files, whose images are one of the standard
+      FAX sizes.
+    - V.27ter, V.29 and V.17 modes (2400bps, to 14,400bps).
+    - T.4 1D (MH), T.4 2D,(MR) and T.6 (MMR) compression.
+    - Error correction mode (ECM).
+    - All standard horizonal resolutions (R8, R16, 300dpi, 600dpi, 800dpi, 1200dpi).
+    - All standard vertical resolutions (standard, fine, superfine, 300dpi, 600dpi, 800dpi, 1200dpi).
+    - All standard page widths (A4, B4, A3).
+    - All standard page lengths (A4, B4, North American letter, North American legal, continuous).
+    - Monitoring and sending identifier strings (CSI, TSI, and CIG).
+    - Monitoring and sending sub-address strings (SUB).
+    - Monitoring and sending polling sub-addresses (SEP).
+    - Monitoring and sending polled sub-addresses (PSA).
+    - Monitoring and sending sender identifications (SID).
+    - Monitoring and sending passwords (PWD).
+    - Monitoring of non-standard facility frames (NSF, NSC, and NSS).
+    - Sending custom non-standard facility frames (NSF, NSC, and NSS).
+    - Analogue modem and T.38 operation.
+
+\section t30_page_sec_2 How does it work?
+
+Some of the following is paraphrased from some notes found a while ago on the Internet.
+I cannot remember exactly where they came from, but they are useful.
+
+\subsection t30_page_sec_2a The answer (CED) tone
+
+The T.30 standard says an answering fax device must send CED (a 2100Hz tone) for
+approximately 3 seconds before sending the first handshake message. Some machines
+send an 1100Hz or 1850Hz tone, and some send no tone at all. In fact, this answer
+tone is so unpredictable, it cannot really be used. It should, however, always be
+generated according to the specification.
+
+\subsection t30_page_sec_2b Common Timing Deviations
+
+The T.30 spec. specifies a number of time-outs. For example, after dialing a number,
+a calling fax system should listen for a response for 35 seconds before giving up.
+These time-out periods are as follows: 
+
+    - T1 - 35+-5s: the maximum time for which two fax system will attempt to identify each other
+    - T2 - 6+-1s:  a time-out used to start the sequence for changing transmit parameters
+    - T3 - 10+-5s: a time-out used in handling operator interrupts
+    - T5 - 60+-5s: a time-out used in error correction mode
+
+These time-outs are sometimes misinterpreted. In addition, they are routinely
+ignored, sometimes with good reason. For example, after placing a call, the
+calling fax system is supposed to wait for 35 seconds before giving up. If the
+answering unit does not answer on the first ring or if a voice answering machine
+is connected to the line, or if there are many delays through the network,
+the delay before answer can be much longer than 35 seconds. 
+
+Fax units that support error correction mode (ECM) can respond to a post-image
+handshake message with a receiver not ready (RNR) message. The calling unit then
+queries the receiving fax unit with a receiver ready (RR) message. If the
+answering unit is still busy (printing for example), it will repeat the RNR
+message. According to the T.30 standard, this sequence (RR/RNR RR/RNR) can be
+repeated for up to the end of T5 (60+-5s). However, many fax systems
+ignore the time-out and will continue the sequence indefinitely, unless the user
+manually overrides. 
+
+All the time-outs are subject to alteration, and sometimes misuse. Good T.30
+implementations must do the right thing, and tolerate others doing the wrong thing.
+
+\subsection t30_page_sec_2c Variations in the inter-carrier gap
+
+T.30 specifies 75+-20ms of silence between signals using different modulation
+schemes. Examples are between the end of a DCS signal and the start of a TCF signal,
+and between the end of an image and the start of a post-image signal. Many fax systems
+violate this requirement, especially for the silent period between DCS and TCF.
+This may be stretched to well over 100ms. If this period is too long, it can interfere with
+handshake signal error recovery, should a packet be corrupted on the line. Systems
+should ensure they stay within the prescribed T.30 limits, and be tolerant of others
+being out of spec.. 
+
+\subsection t30_page_sec_2d Other timing variations
+
+Testing is required to determine the ability of a fax system to handle
+variations in the duration of pauses between unacknowledged handshake message
+repetitions, and also in the pauses between the receipt of a handshake command and
+the start of a response to that command. In order to reduce the total
+transmission time, many fax systems start sending a response message before the
+end of the command has been received. 
+
+\subsection t30_page_sec_2e Other deviations from the T.30 standard
+
+There are many other commonly encountered variations between machines, including:
+
+    - frame sequence deviations
+    - preamble and flag sequence variations
+    - improper EOM usage
+    - unusual data rate fallback sequences
+    - common training pattern detection algorithms
+    - image transmission deviations
+    - use of the talker echo protect tone
+    - image padding and short lines
+    - RTP/RTN handshake message usage
+    - long duration lines
+    - nonstandard disconnect sequences
+    - DCN usage
+*/
+
+#define T30_MAX_DIS_DTC_DCS_LEN     22
+#define T30_MAX_IDENT_LEN           20
+#define T30_MAX_PAGE_HEADER_INFO    50
+
+typedef struct t30_state_s t30_state_t;
+
+/*!
+    T.30 phase B callback handler. This handler can be used to process addition
+    information available in some FAX calls, such as passwords. The callback handler
+    can access whatever additional information might have been received, using
+    t30_get_received_info().
+    \brief T.30 phase B callback handler.
+    \param s The T.30 context.
+    \param user_data An opaque pointer.
+    \param result The phase B event code.
+    \return The new status. Normally, T30_ERR_OK is returned.
+*/
+typedef int (t30_phase_b_handler_t)(t30_state_t *s, void *user_data, int result);
+
+/*!
+    T.30 phase D callback handler.
+    \brief T.30 phase D callback handler.
+    \param s The T.30 context.
+    \param user_data An opaque pointer.
+    \param result The phase D event code.
+    \return The new status. Normally, T30_ERR_OK is returned.
+*/
+typedef int (t30_phase_d_handler_t)(t30_state_t *s, void *user_data, int result);
+
+/*!
+    T.30 phase E callback handler.
+    \brief T.30 phase E callback handler.
+    \param s The T.30 context.
+    \param user_data An opaque pointer.
+    \param completion_code The phase E completion code.
+*/
+typedef void (t30_phase_e_handler_t)(t30_state_t *s, void *user_data, int completion_code);
+
+/*!
+    T.30 real time frame handler.
+    \brief T.30 real time frame handler.
+    \param s The T.30 context.
+    \param user_data An opaque pointer.
+    \param direction TRUE for incoming, FALSE for outgoing.
+    \param msg The HDLC message.
+    \param len The length of the message.
+*/
+typedef void (t30_real_time_frame_handler_t)(t30_state_t *s,
+                                             void *user_data,
+                                             int direction,
+                                             const uint8_t *msg,
+                                             int len);
+
+/*!
+    T.30 document handler.
+    \brief T.30 document handler.
+    \param s The T.30 context.
+    \param user_data An opaque pointer.
+    \param result The document event code.
+*/
+typedef int (t30_document_handler_t)(t30_state_t *s, void *user_data, int status);
+
+/*!
+    T.30 set a receive or transmit type handler.
+    \brief T.30 set a receive or transmit type handler.
+    \param user_data An opaque pointer.
+    \param type The modem, tone or silence to be sent or received.
+    \param bit_rate The bit rate of the modem to be sent or received.
+    \param short_train TRUE if the short training sequence should be used (where one exists).
+    \param use_hdlc FALSE for bit stream, TRUE for HDLC framing.
+*/
+typedef void (t30_set_handler_t)(void *user_data, int type, int bit_rate, int short_train, int use_hdlc);
+
+/*!
+    T.30 send HDLC handler.
+    \brief T.30 send HDLC handler.
+    \param user_data An opaque pointer.
+    \param msg The HDLC message.
+    \param len The length of the message.
+*/
+typedef void (t30_send_hdlc_handler_t)(void *user_data, const uint8_t *msg, int len);
+
+/*!
+    T.30 protocol completion codes, at phase E.
+*/
+enum
+{
+    T30_ERR_OK = 0,             /*! OK */
+
+    /* Link problems */
+    T30_ERR_CEDTONE,            /*! The CED tone exceeded 5s */
+    T30_ERR_T0_EXPIRED,         /*! Timed out waiting for initial communication */
+    T30_ERR_T1_EXPIRED,         /*! Timed out waiting for the first message */
+    T30_ERR_T3_EXPIRED,         /*! Timed out waiting for procedural interrupt */
+    T30_ERR_HDLC_CARRIER,       /*! The HDLC carrier did not stop in a timely manner */
+    T30_ERR_CANNOT_TRAIN,       /*! Failed to train with any of the compatible modems */
+    T30_ERR_OPER_INT_FAIL,      /*! Operator intervention failed */
+    T30_ERR_INCOMPATIBLE,       /*! Far end is not compatible */
+    T30_ERR_RX_INCAPABLE,       /*! Far end is not able to receive */
+    T30_ERR_TX_INCAPABLE,       /*! Far end is not able to transmit */
+    T30_ERR_NORESSUPPORT,       /*! Far end cannot receive at the resolution of the image */
+    T30_ERR_NOSIZESUPPORT,      /*! Far end cannot receive at the size of image */
+    T30_ERR_UNEXPECTED,         /*! Unexpected message received */
+
+    /* Phase E status values returned to a transmitter */
+    T30_ERR_TX_BADDCS,          /*! Received bad response to DCS or training */
+    T30_ERR_TX_BADPG,           /*! Received a DCN from remote after sending a page */
+    T30_ERR_TX_ECMPHD,          /*! Invalid ECM response received from receiver */
+    T30_ERR_TX_GOTDCN,          /*! Received a DCN while waiting for a DIS */
+    T30_ERR_TX_INVALRSP,        /*! Invalid response after sending a page */
+    T30_ERR_TX_NODIS,           /*! Received other than DIS while waiting for DIS */
+    T30_ERR_TX_PHBDEAD,         /*! Received no response to DCS, training or TCF */
+    T30_ERR_TX_PHDDEAD,         /*! No response after sending a page */
+    T30_ERR_TX_T5EXP,           /*! Timed out waiting for receiver ready (ECM mode) */
+
+    /* Phase E status values returned to a receiver */
+    T30_ERR_RX_ECMPHD,          /*! Invalid ECM response received from transmitter */
+    T30_ERR_RX_GOTDCS,          /*! DCS received while waiting for DTC */
+    T30_ERR_RX_INVALCMD,        /*! Unexpected command after page received */
+    T30_ERR_RX_NOCARRIER,       /*! Carrier lost during fax receive */
+    T30_ERR_RX_NOEOL,           /*! Timed out while waiting for EOL (end Of line) */
+    T30_ERR_RX_NOFAX,           /*! Timed out while waiting for first line */
+    T30_ERR_RX_T2EXPDCN,        /*! Timer T2 expired while waiting for DCN */
+    T30_ERR_RX_T2EXPD,          /*! Timer T2 expired while waiting for phase D */
+    T30_ERR_RX_T2EXPFAX,        /*! Timer T2 expired while waiting for fax page */
+    T30_ERR_RX_T2EXPMPS,        /*! Timer T2 expired while waiting for next fax page */
+    T30_ERR_RX_T2EXPRR,         /*! Timer T2 expired while waiting for RR command */
+    T30_ERR_RX_T2EXP,           /*! Timer T2 expired while waiting for NSS, DCS or MCF */
+    T30_ERR_RX_DCNWHY,          /*! Unexpected DCN while waiting for DCS or DIS */
+    T30_ERR_RX_DCNDATA,         /*! Unexpected DCN while waiting for image data */
+    T30_ERR_RX_DCNFAX,          /*! Unexpected DCN while waiting for EOM, EOP or MPS */
+    T30_ERR_RX_DCNPHD,          /*! Unexpected DCN after EOM or MPS sequence */
+    T30_ERR_RX_DCNRRD,          /*! Unexpected DCN after RR/RNR sequence */
+    T30_ERR_RX_DCNNORTN,        /*! Unexpected DCN after requested retransmission */
+
+    /* TIFF file problems */
+    T30_ERR_FILEERROR,          /*! TIFF/F file cannot be opened */
+    T30_ERR_NOPAGE,             /*! TIFF/F page not found */
+    T30_ERR_BADTIFF,            /*! TIFF/F format is not compatible */
+    T30_ERR_BADPAGE,            /*! TIFF/F page number tag missing */
+    T30_ERR_BADTAG,             /*! Incorrect values for TIFF/F tags */
+    T30_ERR_BADTIFFHDR,         /*! Bad TIFF/F header - incorrect values in fields */
+    T30_ERR_NOMEM,              /*! Cannot allocate memory for more pages */
+    
+    /* General problems */
+    T30_ERR_RETRYDCN,           /*! Disconnected after permitted retries */
+    T30_ERR_CALLDROPPED,        /*! The call dropped prematurely */
+    
+    /* Feature negotiation issues */
+    T30_ERR_NOPOLL,             /*! Poll not accepted */
+    T30_ERR_IDENT_UNACCEPTABLE, /*! Far end's ident is not acceptable */
+    T30_ERR_SUB_UNACCEPTABLE,   /*! Far end's sub-address is not acceptable */
+    T30_ERR_SEP_UNACCEPTABLE,   /*! Far end's selective polling address is not acceptable */
+    T30_ERR_PSA_UNACCEPTABLE,   /*! Far end's polled sub-address is not acceptable */
+    T30_ERR_SID_UNACCEPTABLE,   /*! Far end's sender identification is not acceptable */
+    T30_ERR_PWD_UNACCEPTABLE,   /*! Far end's password is not acceptable */
+    T30_ERR_TSA_UNACCEPTABLE,   /*! Far end's transmitting subscriber internet address is not acceptable */
+    T30_ERR_IRA_UNACCEPTABLE,   /*! Far end's internet routing address is not acceptable */
+    T30_ERR_CIA_UNACCEPTABLE,   /*! Far end's calling subscriber internet address is not acceptable */
+    T30_ERR_ISP_UNACCEPTABLE,   /*! Far end's internet selective polling address is not acceptable */
+    T30_ERR_CSA_UNACCEPTABLE    /*! Far end's called subscriber internet address is not acceptable */
+};
+
+/*!
+    I/O modes for the T.30 protocol.
+    These are allocated such that the lower 4 bits represents the variant of the modem - e.g. the
+    particular bit rate selected.
+*/
+enum
+{
+    T30_MODEM_NONE = 0,
+    T30_MODEM_PAUSE,
+    T30_MODEM_CED,
+    T30_MODEM_CNG,
+    T30_MODEM_V21,
+    T30_MODEM_V27TER,
+    T30_MODEM_V29,
+    T30_MODEM_V17,
+    T30_MODEM_DONE
+};
+
+enum
+{
+    T30_FRONT_END_SEND_STEP_COMPLETE = 0,
+    /*! The current receive has completed. This is only needed to report an
+        unexpected end of the receive operation, as might happen with T.38
+        dying. */
+    T30_FRONT_END_RECEIVE_COMPLETE,
+    T30_FRONT_END_SIGNAL_PRESENT,
+    T30_FRONT_END_SIGNAL_ABSENT
+};
+
+enum
+{
+    T30_SUPPORT_V27TER = 0x01,
+    T30_SUPPORT_V29 = 0x02,
+    T30_SUPPORT_V17 = 0x04,
+    T30_SUPPORT_V34 = 0x08,
+    T30_SUPPORT_IAF = 0x10,
+};
+
+enum
+{
+    T30_SUPPORT_NO_COMPRESSION = 0x01,
+    T30_SUPPORT_T4_1D_COMPRESSION = 0x02,
+    T30_SUPPORT_T4_2D_COMPRESSION = 0x04,
+    T30_SUPPORT_T6_COMPRESSION = 0x08,
+    T30_SUPPORT_T85_COMPRESSION = 0x10,     /* Monochrome JBIG */
+    T30_SUPPORT_T43_COMPRESSION = 0x20,     /* Colour JBIG */
+    T30_SUPPORT_T45_COMPRESSION = 0x40      /* Run length colour compression */
+};
+
+enum
+{
+    T30_SUPPORT_STANDARD_RESOLUTION = 0x01,
+    T30_SUPPORT_FINE_RESOLUTION = 0x02,
+    T30_SUPPORT_SUPERFINE_RESOLUTION = 0x04,
+
+    T30_SUPPORT_R4_RESOLUTION = 0x10000,
+    T30_SUPPORT_R8_RESOLUTION = 0x20000,
+    T30_SUPPORT_R16_RESOLUTION = 0x40000,
+
+    T30_SUPPORT_300_300_RESOLUTION = 0x100000,
+    T30_SUPPORT_400_400_RESOLUTION = 0x200000,
+    T30_SUPPORT_600_600_RESOLUTION = 0x400000,
+    T30_SUPPORT_1200_1200_RESOLUTION = 0x800000,
+    T30_SUPPORT_300_600_RESOLUTION = 0x1000000,
+    T30_SUPPORT_400_800_RESOLUTION = 0x2000000,
+    T30_SUPPORT_600_1200_RESOLUTION = 0x4000000
+};
+
+enum
+{
+    T30_SUPPORT_215MM_WIDTH = 0x01,
+    T30_SUPPORT_255MM_WIDTH = 0x02,
+    T30_SUPPORT_303MM_WIDTH = 0x04,
+
+    T30_SUPPORT_UNLIMITED_LENGTH = 0x10000,
+    T30_SUPPORT_A4_LENGTH = 0x20000,
+    T30_SUPPORT_B4_LENGTH = 0x40000,
+    T30_SUPPORT_US_LETTER_LENGTH = 0x80000,
+    T30_SUPPORT_US_LEGAL_LENGTH = 0x100000
+};
+
+enum
+{
+    /*! Enable support of identification, through the SID and/or PWD frames */
+    T30_SUPPORT_IDENTIFICATION = 0x01,
+    /*! Enable support of selective polling, through the SEP frame */
+    T30_SUPPORT_SELECTIVE_POLLING = 0x02,
+    /*! Enable support of polling sub-addressing, through the PSA frame */
+    T30_SUPPORT_POLLED_SUB_ADDRESSING = 0x04,
+    /*! Enable support of multiple selective polling, through repeated used of the SEP and PSA frames */
+    T30_SUPPORT_MULTIPLE_SELECTIVE_POLLING = 0x08,
+    /*! Enable support of sub-addressing, through the SUB frame */
+    T30_SUPPORT_SUB_ADDRESSING = 0x10,
+    /*! Enable support of transmitting subscriber internet address, through the TSA frame */
+    T30_SUPPORT_TRANSMITTING_SUBSCRIBER_INTERNET_ADDRESS = 0x20,
+    /*! Enable support of internet routing address, through the IRA frame */
+    T30_SUPPORT_INTERNET_ROUTING_ADDRESS = 0x40,
+    /*! Enable support of calling subscriber internet address, through the CIA frame */
+    T30_SUPPORT_CALLING_SUBSCRIBER_INTERNET_ADDRESS = 0x80,
+    /*! Enable support of internet selective polling address, through the ISP frame */
+    T30_SUPPORT_INTERNET_SELECTIVE_POLLING_ADDRESS = 0x100,
+    /*! Enable support of called subscriber internet address, through the CSA frame */
+    T30_SUPPORT_CALLED_SUBSCRIBER_INTERNET_ADDRESS = 0x200,
+    /*! Enable support of the field not valid (FNV) frame */
+    T30_SUPPORT_FIELD_NOT_VALID = 0x400,
+    /*! Enable support of the command repeat (CRP) frame */
+    T30_SUPPORT_COMMAND_REPEAT = 0x800
+};
+
+enum
+{
+    T30_IAF_MODE_T37 = 0x01,
+    T30_IAF_MODE_T38 = 0x02,
+    T30_IAF_MODE_FLOW_CONTROL = 0x04,
+    /*! Continuous flow mode means data is sent as fast as possible, usually across
+        the Internet, where speed is not constrained by a PSTN modem. */
+    T30_IAF_MODE_CONTINUOUS_FLOW = 0x08,
+    /*! No TCF means TCF is not exchanged. The end points must sort out usable speed
+        issues locally. */
+    T30_IAF_MODE_NO_TCF = 0x10,
+    /*! No fill bits means do not insert fill bits, even if the T.30 messages request
+        them. */
+    T30_IAF_MODE_NO_FILL_BITS = 0x20,
+    /*! No indicators means do not send indicator messages when using T.38. */
+    T30_IAF_MODE_NO_INDICATORS = 0x40
+};
+
+typedef struct
+{
+    /*! \brief The identifier string (CSI, TSI, CIG). */
+    char ident[T30_MAX_IDENT_LEN + 1];
+    /*! \brief The sub-address string (SUB). */
+    char sub_address[T30_MAX_IDENT_LEN + 1];
+    /*! \brief The selective polling sub-address (SEP). */
+    char selective_polling_address[T30_MAX_IDENT_LEN + 1];
+    /*! \brief The polled sub-address (PSA). */
+    char polled_sub_address[T30_MAX_IDENT_LEN + 1];
+    /*! \brief The sender identification (SID). */
+    char sender_ident[T30_MAX_IDENT_LEN + 1];
+    /*! \brief The password (PWD). */
+    char password[T30_MAX_IDENT_LEN + 1];
+    /*! \brief Non-standard facilities (NSF). */
+    uint8_t *nsf;
+    size_t nsf_len;
+    /*! \brief Non-standard facilities command (NSC). */
+    uint8_t *nsc;
+    size_t nsc_len;
+    /*! \brief Non-standard facilities set-up (NSS). */
+    uint8_t *nss;
+    size_t nss_len;
+    /*! \brief Transmitting subscriber internet address (TSA). */
+    int tsa_type;
+    char *tsa;
+    size_t tsa_len;
+    /*! \brief Internet routing address (IRA). */
+    int ira_type;
+    char *ira;
+    size_t ira_len;
+    /*! \brief Calling subscriber internet address (CIA). */
+    int cia_type;
+    char *cia;
+    size_t cia_len;
+    /*! \brief Internet selective polling address (ISP). */
+    int isp_type;
+    char *isp;
+    size_t isp_len;
+    /*! \brief Called subscriber internet address (CSA). */
+    int csa_type;
+    char *csa;
+    size_t csa_len;
+} t30_exchanged_info_t;
+
+/*!
+    T.30 FAX channel descriptor. This defines the state of a single working
+    instance of a T.30 FAX channel.
+*/
+struct t30_state_s
+{
+    /* This must be kept the first thing in the structure, so it can be pointed
+       to reliably as the structures change over time. */
+    /*! \brief T.4 context for reading or writing image data. */
+    t4_state_t t4;
+    
+    int operation_in_progress;
+
+    /*! \brief TRUE if behaving as the calling party */
+    int calling_party;
+    
+    /*! \brief The received DCS, formatted as an ASCII string, for inclusion
+               in the TIFF file. */
+    char rx_dcs_string[T30_MAX_DIS_DTC_DCS_LEN*3 + 1];
+    /*! \brief The text which will be used in FAX page header. No text results
+               in no header line. */
+    char header_info[T30_MAX_PAGE_HEADER_INFO + 1];
+    /*! \brief The information fields received. */
+    t30_exchanged_info_t rx_info;
+    /*! \brief The information fields to be transmitted. */
+    t30_exchanged_info_t tx_info;
+    /*! \brief The country of origin of the remote machine, if known, else NULL. */
+    const char *country;
+    /*! \brief The vendor of the remote machine, if known, else NULL. */
+    const char *vendor;
+    /*! \brief The model of the remote machine, if known, else NULL. */
+    const char *model;
+
+    /*! \brief A pointer to a callback routine to be called when phase B events
+        occur. */
+    t30_phase_b_handler_t *phase_b_handler;
+    /*! \brief An opaque pointer supplied in event B callbacks. */
+    void *phase_b_user_data;
+    /*! \brief A pointer to a callback routine to be called when phase D events
+        occur. */
+    t30_phase_d_handler_t *phase_d_handler;
+    /*! \brief An opaque pointer supplied in event D callbacks. */
+    void *phase_d_user_data;
+    /*! \brief A pointer to a callback routine to be called when phase E events
+        occur. */
+    t30_phase_e_handler_t *phase_e_handler;
+    /*! \brief An opaque pointer supplied in event E callbacks. */
+    void *phase_e_user_data;
+    /*! \brief A pointer to a callback routine to be called when frames are
+        exchanged. */
+    t30_real_time_frame_handler_t *real_time_frame_handler;
+    /*! \brief An opaque pointer supplied in real time frame callbacks. */
+    void *real_time_frame_user_data;
+
+    /*! \brief A pointer to a callback routine to be called when document events
+        (e.g. end of transmitted document) occur. */
+    t30_document_handler_t *document_handler;
+    /*! \brief An opaque pointer supplied in document callbacks. */
+    void *document_user_data;
+
+    /*! \brief The handler for changes to the receive mode */
+    t30_set_handler_t *set_rx_type_handler;
+    /*! \brief An opaque pointer passed to the handler for changes to the receive mode */
+    void *set_rx_type_user_data;
+    /*! \brief The handler for changes to the transmit mode */
+    t30_set_handler_t *set_tx_type_handler;
+    /*! \brief An opaque pointer passed to the handler for changes to the transmit mode */
+    void *set_tx_type_user_data;
+
+    /*! \brief The transmitted HDLC frame handler. */
+    t30_send_hdlc_handler_t *send_hdlc_handler;
+    /*! \brief An opaque pointer passed to the transmitted HDLC frame handler. */
+    void *send_hdlc_user_data;
+
+    /*! \brief The DIS code for the minimum scan row time we require. This is usually 0ms,
+        but if we are trying to simulate another type of FAX machine, we may need a non-zero
+        value here. */
+    uint8_t local_min_scan_time_code;
+
+    /*! \brief The current T.30 phase. */
+    int phase;
+    /*! \brief The T.30 phase to change to when the current phase ends. */
+    int next_phase;
+    /*! \brief The current state of the T.30 state machine. */
+    int state;
+    /*! \brief The step in sending a sequence of HDLC frames. */
+    int step;
+
+    /*! \brief The preparation buffer for the DCS message to be transmitted. */
+    uint8_t dcs_frame[T30_MAX_DIS_DTC_DCS_LEN];
+    /*! \brief The length of the DCS message to be transmitted. */
+    int dcs_len;
+    /*! \brief The preparation buffer for DIS or DTC message to be transmitted. */
+    uint8_t local_dis_dtc_frame[T30_MAX_DIS_DTC_DCS_LEN];
+    /*! \brief The length of the DIS or DTC message to be transmitted. */
+    int local_dis_dtc_len;
+    /*! \brief The last DIS or DTC message received form the far end. */
+    uint8_t far_dis_dtc_frame[T30_MAX_DIS_DTC_DCS_LEN];
+    /*! \brief The length of the last DIS or DTC message received form the far end. */
+    int far_dis_dtc_len;
+    /*! \brief TRUE if a valid DIS has been received from the far end. */
+    int dis_received;
+
+    /*! \brief A flag to indicate a message is in progress. */
+    int in_message;
+
+    /*! \brief TRUE if the short training sequence should be used. */
+    int short_train;
+
+    /*! \brief A count of the number of bits in the trainability test. This counts down to zero when
+        sending TCF, and counts up when receiving it. */
+    int tcf_test_bits;
+    /*! \brief The current count of consecutive received zero bits, during the trainability test. */
+    int tcf_current_zeros;
+    /*! \brief The maximum consecutive received zero bits seen to date, during the trainability test. */
+    int tcf_most_zeros;
+
+    /*! \brief The current fallback step for the fast message transfer modem. */
+    int current_fallback;
+    /*! \brief The subset of supported modems allowed at the current time, allowing for negotiation. */
+    int current_permitted_modems;
+    /*! \brief TRUE if a carrier is present. Otherwise FALSE. */
+    int rx_signal_present;
+    /*! \brief TRUE if a modem has trained correctly. */
+    int rx_trained;
+    /*! \brief TRUE if a valid HDLC frame has been received in the current reception period. */
+    int rx_frame_received;
+
+    /*! \brief Current reception mode. */
+    int current_rx_type;
+    /*! \brief Current transmission mode. */
+    int current_tx_type;
+
+    /*! \brief T0 is the answer timeout when calling another FAX machine.
+        Placing calls is handled outside the FAX processing, but this timeout keeps
+        running until V.21 modulation is sent or received.
+        T1 is the remote terminal identification timeout (in audio samples). */
+    int timer_t0_t1;
+    /*! \brief T2, T2A and T2B are the HDLC command timeouts.
+               T4, T4A and T4B are the HDLC response timeouts (in audio samples). */
+    int timer_t2_t4;
+    /*! \brief A value specifying which of the possible timers is currently running in timer_t2_t4 */
+    int timer_t2_t4_is;
+    /*! \brief Procedural interrupt timeout (in audio samples). */
+    int timer_t3;
+    /*! \brief This is only used in error correcting mode. */
+    int timer_t5;
+    /*! \brief This is only used in full duplex (e.g. ISDN) modes. */
+    int timer_t6;
+    /*! \brief This is only used in full duplex (e.g. ISDN) modes. */
+    int timer_t7;
+    /*! \brief This is only used in full duplex (e.g. ISDN) modes. */
+    int timer_t8;
+
+    /*! \brief TRUE once the far end FAX entity has been detected. */
+    int far_end_detected;
+
+    /*! \brief TRUE if a local T.30 interrupt is pending. */
+    int local_interrupt_pending;
+    /*! \brief The image coding being used on the line. */
+    int line_encoding;
+    /*! \brief The image coding being used for output files. */
+    int output_encoding;
+    /*! \brief The current DCS message minimum scan time code. */
+    uint8_t min_scan_time_code;
+    /*! \brief The X direction resolution of the current image, in pixels per metre. */
+    int x_resolution;
+    /*! \brief The Y direction resolution of the current image, in pixels per metre. */
+    int y_resolution;
+    /*! \brief The width of the current image, in pixels. */
+    t4_image_width_t image_width;
+    /*! \brief Current number of retries of the action in progress. */
+    int retries;
+    /*! \brief TRUE if error correcting mode is used. */
+    int error_correcting_mode;
+    /*! \brief The current count of consecutive T30_PPR messages. */
+    int ppr_count;
+    /*! \brief The current count of consecutive T30_RNR messages. */
+    int receiver_not_ready_count;
+    /*! \brief The number of octets to be used per ECM frame. */
+    int octets_per_ecm_frame;
+    /*! \brief The ECM partial page buffer. */
+    uint8_t ecm_data[256][260];
+    /*! \brief The lengths of the frames in the ECM partial page buffer. */
+    int16_t ecm_len[256];
+    /*! \brief A bit map of the OK ECM frames, constructed as a PPR frame. */
+    uint8_t ecm_frame_map[3 + 32];
+    
+    /*! \brief The current page number for receiving, in ECM mode. This is reset at the start of a call. */
+    int ecm_rx_page;
+    /*! \brief The current page number for sending, in ECM mode. This is reset at the start of a call. */
+    int ecm_tx_page;
+    /*! \brief The current block number, in ECM mode */
+    int ecm_block;
+    /*! \brief The number of frames in the current block number, in ECM mode */
+    int ecm_frames;
+    /*! \brief The number of frames sent in the current burst of image transmission, in ECM mode */
+    int ecm_frames_this_tx_burst;
+    /*! \brief The current ECM frame, during ECM transmission. */
+    int ecm_current_tx_frame;
+    /*! \brief TRUE if we are at the end of an ECM page to se sent - i.e. there are no more
+        partial pages still to come. */
+    int ecm_at_page_end;
+    int next_tx_step;
+    int next_rx_step;
+    /*! \brief Image file name for image reception. */
+    char rx_file[256];
+    /*! \brief The last page we are prepared accept for a received image file. -1 means no restriction. */
+    int rx_stop_page;
+    /*! \brief Image file name to be sent. */
+    char tx_file[256];
+    /*! \brief The first page to be sent from the image file. -1 means no restriction. */
+    int tx_start_page;
+    /*! \brief The last page to be sent from the image file. -1 means no restriction. */
+    int tx_stop_page;
+    int current_status;
+    /*! \brief Internet Aware FAX mode bit mask. */
+    int iaf;
+    /*! \brief A bit mask of the currently supported modem types. */
+    int supported_modems;
+    /*! \brief A bit mask of the currently supported image compression modes. */
+    int supported_compressions;
+    /*! \brief A bit mask of the currently supported image resolutions. */
+    int supported_resolutions;
+    /*! \brief A bit mask of the currently supported image sizes. */
+    int supported_image_sizes;
+    /*! \brief A bit mask of the currently supported T.30 special features. */
+    int supported_t30_features;
+    /*! \brief TRUE is ECM mode handling is enabled. */
+    int ecm_allowed;
+    
+    /*! \brief the FCF2 field of the last PPS message we received. */
+    int last_pps_fcf2;
+    /*! \brief The number of the first ECM frame which we do not currently received correctly. For
+        a partial page received correctly, this will be one greater than the number of frames it
+        contains. */
+    int ecm_first_bad_frame;
+    /*! \brief A count of successfully received ECM frames, to assess progress as a basis for
+        deciding whether to continue error correction when PPRs keep repeating. */
+    int ecm_progress;
+
+    /*! \brief Error and flow logging control */
+    logging_state_t logging;
+};
+
+typedef struct
+{
+    /*! \brief The current bit rate for image transfer. */
+    int bit_rate;
+    /*! \brief TRUE if error correcting mode is used. */
+    int error_correcting_mode;
+    /*! \brief The number of pages transferred so far. */
+    int pages_transferred;
+    /*! \brief The number of pages in the file (<0 if not known). */
+    int pages_in_file;
+    /*! \brief The number of horizontal pixels in the most recent page. */
+    int width;
+    /*! \brief The number of vertical pixels in the most recent page. */
+    int length;
+    /*! \brief The number of bad pixel rows in the most recent page. */
+    int bad_rows;
+    /*! \brief The largest number of bad pixel rows in a block in the most recent page. */
+    int longest_bad_row_run;
+    /*! \brief The horizontal column-to-column resolution of the page in pixels per metre */
+    int x_resolution;
+    /*! \brief The vertical row-to-row resolution of the page in pixels per metre */
+    int y_resolution;
+    /*! \brief The type of compression used between the FAX machines */
+    int encoding;
+    /*! \brief The size of the image, in bytes */
+    int image_size;
+    /*! \brief Current status */
+    int current_status;
+} t30_stats_t;
+
+#if defined(__cplusplus)
+extern "C"
+{
+#endif
+
+/*! Initialise a T.30 context.
+    \brief Initialise a T.30 context.
+    \param s The T.30 context.
+    \param calling_party TRUE if the context is for a calling party. FALSE if the
+           context is for an answering party.
+    \param set_rx_type_handler
+    \param set_rx_type_user_data
+    \param set_tx_type_handler
+    \param set_tx_type_user_data
+    \param send_hdlc_handler
+    \param send_hdlc_user_data
+    \return A pointer to the context, or NULL if there was a problem. */
+t30_state_t *t30_init(t30_state_t *s,
+                      int calling_party,
+                      t30_set_handler_t *set_rx_type_handler,
+                      void *set_rx_type_user_data,
+                      t30_set_handler_t *set_tx_type_handler,
+                      void *set_tx_type_user_data,
+                      t30_send_hdlc_handler_t *send_hdlc_handler,
+                      void *send_hdlc_user_data);
+
+/*! Release a T.30 context.
+    \brief Release a T.30 context.
+    \param s The T.30 context.
+    \return 0 for OK, else -1. */
+int t30_release(t30_state_t *s);
+
+/*! Free a T.30 context.
+    \brief Free a T.30 context.
+    \param s The T.30 context.
+    \return 0 for OK, else -1. */
+int t30_free(t30_state_t *s);
+
+/*! Restart a T.30 context.
+    \brief Restart a T.30 context.
+    \param s The T.30 context.
+    \return 0 for OK, else -1. */
+int t30_restart(t30_state_t *s);
+
+/*! Cleanup a T.30 context if the call terminates.
+    \brief Cleanup a T.30 context if the call terminates.
+    \param s The T.30 context. */
+void t30_terminate(t30_state_t *s);
+
+/*! Inform the T.30 engine of a status change in the front end (end of tx, rx signal change, etc.).
+    \brief Inform the T.30 engine of a status change in the front end (end of tx, rx signal change, etc.).
+    \param user_data The T.30 context.
+    \param status The type of status change which occured. */
+void t30_front_end_status(void *user_data, int status);
+
+/*! Get a bit of received non-ECM image data.
+    \brief Get a bit of received non-ECM image data.
+    \param user_data An opaque pointer, which must point to the T.30 context.
+    \return The next bit to transmit. */
+int t30_non_ecm_get_bit(void *user_data);
+
+/*! Get a byte of received non-ECM image data.
+    \brief Get a byte of received non-ECM image data.
+    \param user_data An opaque pointer, which must point to the T.30 context.
+    \return The next byte to transmit. */
+int t30_non_ecm_get_byte(void *user_data);
+
+/*! Get a chunk of received non-ECM image data.
+    \brief Get a bit of received non-ECM image data.
+    \param user_data An opaque pointer, which must point to the T.30 context.
+    \param buf The buffer to contain the data.
+    \param max_len The maximum length of the chunk.
+    \return The actual length of the chunk. */
+int t30_non_ecm_get_chunk(void *user_data, uint8_t buf[], int max_len);
+
+/*! Process a bit of received non-ECM image data.
+    \brief Process a bit of received non-ECM image data
+    \param user_data An opaque pointer, which must point to the T.30 context.
+    \param bit The received bit. */
+void t30_non_ecm_put_bit(void *user_data, int bit);
+
+/*! Process a byte of received non-ECM image data.
+    \brief Process a byte of received non-ECM image data
+    \param user_data An opaque pointer, which must point to the T.30 context.
+    \param byte The received byte. */
+void t30_non_ecm_put_byte(void *user_data, int byte);
+
+/*! Process a chunk of received non-ECM image data.
+    \brief Process a chunk of received non-ECM image data
+    \param user_data An opaque pointer, which must point to the T.30 context.
+    \param buf The buffer containing the received data.
+    \param len The length of the data in buf. */
+void t30_non_ecm_put_chunk(void *user_data, const uint8_t buf[], int len);
+
+/*! Process a received HDLC frame.
+    \brief Process a received HDLC frame.
+    \param user_data The T.30 context.
+    \param msg The HDLC message.
+    \param len The length of the message, in octets.
+    \param ok TRUE if the frame was received without error. */
+void t30_hdlc_accept(void *user_data, const uint8_t *msg, int len, int ok);
+
+/*! Report the passage of time to the T.30 engine.
+    \brief Report the passage of time to the T.30 engine.
+    \param s The T.30 context.
+    \param samples The time change in 1/8000th second steps. */
+void t30_timer_update(t30_state_t *s, int samples);
+
+/*! Get the current transfer statistics for the file being sent or received.
+    \brief Get the current transfer statistics.
+    \param s The T.30 context.
+    \param t A pointer to a buffer for the statistics. */
+void t30_get_transfer_statistics(t30_state_t *s, t30_stats_t *t);
+
+/*! Request a local interrupt of FAX exchange.
+    \brief Request a local interrupt of FAX exchange.
+    \param s The T.30 context.
+    \param state TRUE to enable interrupt request, else FALSE. */
+void t30_local_interrupt_request(t30_state_t *s, int state);
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/spandsp/t30_api.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/spandsp/t30_api.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,560 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * t30_api.h - definitions for T.30 fax processing
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2003 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: t30_api.h,v 1.6 2008/08/14 14:06:05 steveu Exp $
+ */
+
+/*! \file */
+
+#if !defined(_SPANDSP_T30_API_H_)
+#define _SPANDSP_T30_API_H_
+
+#if defined(__cplusplus)
+extern "C"
+{
+#endif
+
+/*! Set the transmitted NSF frame to be associated with a T.30 context.
+    \brief Set the transmitted NSF frame to be associated with a T.30 context.
+    \param s The T.30 context.
+    \param nsf A pointer to the frame.
+    \param len The length of the frame.
+    \return 0 for OK, else -1. */
+int t30_set_tx_nsf(t30_state_t *s, const uint8_t *nsf, int len);
+
+/*! Get an NSF frame to be associated with a T.30 context.
+    \brief Set an NSF frame to be associated with a T.30 context.
+    \param s The T.30 context.
+    \param nsf A pointer to the frame.
+    \return the length of the NSF message. */
+size_t t30_get_tx_nsf(t30_state_t *s, const uint8_t *nsf[]);
+
+/*! Get an NSF frame to be associated with a T.30 context.
+    \brief Set an NSF frame to be associated with a T.30 context.
+    \param s The T.30 context.
+    \param nsf A pointer to the frame.
+    \return the length of the NSF message. */
+size_t t30_get_rx_nsf(t30_state_t *s, const uint8_t *nsf[]);
+
+/*! Set the transmitted NSC frame to be associated with a T.30 context.
+    \brief Set the transmitted NSC frame to be associated with a T.30 context.
+    \param s The T.30 context.
+    \param nsf A pointer to the frame.
+    \param len The length of the frame.
+    \return 0 for OK, else -1. */
+int t30_set_tx_nsc(t30_state_t *s, const uint8_t *nsc, int len);
+
+/*! Get an NSC frame to be associated with a T.30 context.
+    \brief Set an NSC frame to be associated with a T.30 context.
+    \param s The T.30 context.
+    \param nsc A pointer to the frame.
+    \return the length of the NSC message. */
+size_t t30_get_tx_nsc(t30_state_t *s, const uint8_t *nsc[]);
+
+/*! Get an NSC frame to be associated with a T.30 context.
+    \brief Set an NSC frame to be associated with a T.30 context.
+    \param s The T.30 context.
+    \param nsc A pointer to the frame.
+    \return the length of the NSC message. */
+size_t t30_get_rx_nsc(t30_state_t *s, const uint8_t *nsc[]);
+
+/*! Set the transmitted NSS frame to be associated with a T.30 context.
+    \brief Set the transmitted NSS frame to be associated with a T.30 context.
+    \param s The T.30 context.
+    \param nsf A pointer to the frame.
+    \param len The length of the frame.
+    \return 0 for OK, else -1. */
+int t30_set_tx_nss(t30_state_t *s, const uint8_t *nss, int len);
+
+/*! Get an NSS frame to be associated with a T.30 context.
+    \brief Set an NSS frame to be associated with a T.30 context.
+    \param s The T.30 context.
+    \param nss A pointer to the frame.
+    \return the length of the NSS message. */
+size_t t30_get_tx_nss(t30_state_t *s, const uint8_t *nss[]);
+
+/*! Get an NSS frame to be associated with a T.30 context.
+    \brief Set an NSS frame to be associated with a T.30 context.
+    \param s The T.30 context.
+    \param nss A pointer to the frame.
+    \return the length of the NSS message. */
+size_t t30_get_rx_nss(t30_state_t *s, const uint8_t *nss[]);
+
+/*! Set the transmitted identifier associated with a T.30 context.
+    \brief Set the transmitted identifier associated with a T.30 context.
+    \param s The T.30 context.
+    \param id A pointer to the identifier.
+    \return 0 for OK, else -1. */
+int t30_set_tx_ident(t30_state_t *s, const char *id);
+
+/*! Get the transmitted identifier associated with a T.30 context.
+    \brief Set the transmitted identifier associated with a T.30 context.
+    \param s The T.30 context.
+    \param id A pointer to the identifier.
+    \return 0 for OK, else -1. */
+const char *t30_get_tx_ident(t30_state_t *s);
+
+/*! Get the transmitted identifier associated with a T.30 context.
+    \brief Set the transmitted identifier associated with a T.30 context.
+    \param s The T.30 context.
+    \param id A pointer to the identifier.
+    \return 0 for OK, else -1. */
+const char *t30_get_rx_ident(t30_state_t *s);
+
+/*! Set the transmitted sub-address associated with a T.30 context.
+    \brief Set the transmitted sub-address associated with a T.30 context.
+    \param s The T.30 context.
+    \param sub_address A pointer to the sub-address.
+    \return 0 for OK, else -1. */
+int t30_set_tx_sub_address(t30_state_t *s, const char *sub_address);
+
+/*! Get the received sub-address associated with a T.30 context.
+    \brief Get the received sub-address associated with a T.30 context.
+    \param s The T.30 context.
+    \param sub_address A pointer to the sub-address.
+    \return 0 for OK, else -1. */
+const char *t30_get_tx_sub_address(t30_state_t *s);
+
+/*! Get the received sub-address associated with a T.30 context.
+    \brief Get the received sub-address associated with a T.30 context.
+    \param s The T.30 context.
+    \param sub_address A pointer to the sub-address.
+    \return 0 for OK, else -1. */
+const char *t30_get_rx_sub_address(t30_state_t *s);
+
+/*! Set the transmitted selective polling address (i.e. the one we will send to the far
+    end) associated with a T.30 context.
+    \brief Set the transmitted selective polling address associated with a T.30 context.
+    \param s The T.30 context.
+    \param selective_polling_address A pointer to the selective polling address.
+    \return 0 for OK, else -1. */
+int t30_set_tx_selective_polling_address(t30_state_t *s, const char *selective_polling_address);
+
+/*! Get the received selective polling address (i.e. the one we will send to the far
+    end) associated with a T.30 context.
+    \brief Get the received selective polling address associated with a T.30 context.
+    \param s The T.30 context.
+    \param selective_polling_address A pointer to the selective polling address.
+    \return 0 for OK, else -1. */
+const char *t30_get_tx_selective_polling_address(t30_state_t *s);
+
+/*! Get the received selective polling address (i.e. the one we will send to the far
+    end) associated with a T.30 context.
+    \brief Get the received selective polling address associated with a T.30 context.
+    \param s The T.30 context.
+    \param selective_polling_address A pointer to the selective polling address.
+    \return 0 for OK, else -1. */
+const char *t30_get_rx_selective_polling_address(t30_state_t *s);
+
+/*! Set the transmitted polled sub-address (i.e. the one we will send to the far
+    end) associated with a T.30 context.
+    \brief Set the transmitted polled sub-address associated with a T.30 context.
+    \param s The T.30 context.
+    \param polled_sub_address A pointer to the polled sub-address.
+    \return 0 for OK, else -1. */
+int t30_set_tx_polled_sub_address(t30_state_t *s, const char *polled_sub_address);
+
+/*! Get the received polled sub-address (i.e. the one we will send to the far
+    end) associated with a T.30 context.
+    \brief Get the received polled sub-address associated with a T.30 context.
+    \param s The T.30 context.
+    \param polled_sub_address A pointer to the polled sub-address.
+    \return 0 for OK, else -1. */
+const char *t30_get_tx_polled_sub_address(t30_state_t *s);
+
+/*! Get the received polled sub-address (i.e. the one we will send to the far
+    end) associated with a T.30 context.
+    \brief Get the received polled sub-address associated with a T.30 context.
+    \param s The T.30 context.
+    \param polled_sub_address A pointer to the polled sub-address.
+    \return 0 for OK, else -1. */
+const char *t30_get_rx_polled_sub_address(t30_state_t *s);
+
+/*! Set the transmitted sender ident (i.e. the one we will send to the far
+    end) associated with a T.30 context.
+    \brief Set the transmitted sender ident associated with a T.30 context.
+    \param s The T.30 context.
+    \param sender_ident A pointer to the sender ident.
+    \return 0 for OK, else -1. */
+int t30_set_tx_sender_ident(t30_state_t *s, const char *sender_ident);
+
+/*! Get the received sender ident (i.e. the one we will send to the far
+    end) associated with a T.30 context.
+    \brief Get the received sender ident associated with a T.30 context.
+    \param s The T.30 context.
+    \param sender_ident A pointer to the sender ident.
+    \return 0 for OK, else -1. */
+const char *t30_get_tx_sender_ident(t30_state_t *s);
+
+/*! Get the received sender ident (i.e. the one we will send to the far
+    end) associated with a T.30 context.
+    \brief Get the received sender ident associated with a T.30 context.
+    \param s The T.30 context.
+    \param sender_ident A pointer to the sender ident.
+    \return 0 for OK, else -1. */
+const char *t30_get_rx_sender_ident(t30_state_t *s);
+
+/*! Set the transmitted password (i.e. the one we will send to the far
+    end) associated with a T.30 context.
+    \brief Set the transmitted password associated with a T.30 context.
+    \param s The T.30 context.
+    \param password A pointer to the password.
+    \return 0 for OK, else -1. */
+int t30_set_tx_password(t30_state_t *s, const char *password);
+
+/*! Get the received password (i.e. the one we will send to the far
+    end) associated with a T.30 context.
+    \brief Get the received password associated with a T.30 context.
+    \param s The T.30 context.
+    \param password A pointer to the password.
+    \return 0 for OK, else -1. */
+const char *t30_get_tx_password(t30_state_t *s);
+
+/*! Get the received password (i.e. the one we will send to the far
+    end) associated with a T.30 context.
+    \brief Get the received password associated with a T.30 context.
+    \param s The T.30 context.
+    \param password A pointer to the password.
+    \return 0 for OK, else -1. */
+const char *t30_get_rx_password(t30_state_t *s);
+
+/*! Set the transmitted ??? (i.e. the one we will send to the far
+    end) associated with a T.30 context.
+    \brief Set the transmitted ??? associated with a T.30 context.
+    \param s The T.30 context.
+    \param type The type of address.
+    \param type A pointer to the address.
+    \param len The length of the address.
+    \return 0 for OK, else -1. */
+int t30_set_tx_tsa(t30_state_t *s, int type, const char *address, int len);
+
+/*! Get the received ??? (i.e. the one we will send to the far
+    end) associated with a T.30 context.
+    \brief Get the received ??? associated with a T.30 context.
+    \param s The T.30 context.
+    \param type The type of address.
+    \param type A pointer to the address.
+    \param len The length of the address.
+    \return 0 for OK, else -1. */
+size_t t30_get_tx_tsa(t30_state_t *s, int *type, const char *address[]);
+
+/*! Get the received ??? (i.e. the one we will send to the far
+    end) associated with a T.30 context.
+    \brief Get the received ??? associated with a T.30 context.
+    \param s The T.30 context.
+    \param type The type of address.
+    \param type A pointer to the address.
+    \param len The length of the address.
+    \return 0 for OK, else -1. */
+size_t t30_get_rx_tsa(t30_state_t *s, int *type, const char *address[]);
+
+/*! Set the transmitted ??? (i.e. the one we will send to the far
+    end) associated with a T.30 context.
+    \brief Set the transmitted ??? associated with a T.30 context.
+    \param s The T.30 context.
+    \param type The type of address.
+    \param type A pointer to the address.
+    \param len The length of the address.
+    \return 0 for OK, else -1. */
+int t30_set_tx_ira(t30_state_t *s, int type, const char *address, int len);
+
+/*! Get the received ??? (i.e. the one we will send to the far
+    end) associated with a T.30 context.
+    \brief Get the received ??? associated with a T.30 context.
+    \param s The T.30 context.
+    \param type The type of address.
+    \param type A pointer to the address.
+    \param len The length of the address.
+    \return 0 for OK, else -1. */
+size_t t30_get_tx_ira(t30_state_t *s, int *type, const char *address[]);
+
+/*! Get the received ??? (i.e. the one we will send to the far
+    end) associated with a T.30 context.
+    \brief Get the received ??? associated with a T.30 context.
+    \param s The T.30 context.
+    \param type The type of address.
+    \param type A pointer to the address.
+    \param len The length of the address.
+    \return 0 for OK, else -1. */
+size_t t30_get_rx_ira(t30_state_t *s, int *type, const char *address[]);
+
+/*! Set the transmitted ??? (i.e. the one we will send to the far
+    end) associated with a T.30 context.
+    \brief Set the transmitted ??? associated with a T.30 context.
+    \param s The T.30 context.
+    \param type The type of address.
+    \param type A pointer to the address.
+    \param len The length of the address.
+    \return 0 for OK, else -1. */
+int t30_set_tx_cia(t30_state_t *s, int type, const char *address, int len);
+
+/*! Get the received ??? (i.e. the one we will send to the far
+    end) associated with a T.30 context.
+    \brief Get the received ??? associated with a T.30 context.
+    \param s The T.30 context.
+    \param type The type of address.
+    \param type A pointer to the address.
+    \param len The length of the address.
+    \return 0 for OK, else -1. */
+size_t t30_get_tx_cia(t30_state_t *s, int *type, const char *address[]);
+
+/*! Get the received ??? (i.e. the one we will send to the far
+    end) associated with a T.30 context.
+    \brief Get the received ??? associated with a T.30 context.
+    \param s The T.30 context.
+    \param type The type of address.
+    \param type A pointer to the address.
+    \param len The length of the address.
+    \return 0 for OK, else -1. */
+size_t t30_get_rx_cia(t30_state_t *s, int *type, const char *address[]);
+
+/*! Set the transmitted ??? (i.e. the one we will send to the far
+    end) associated with a T.30 context.
+    \brief Set the transmitted ??? associated with a T.30 context.
+    \param s The T.30 context.
+    \param type The type of address.
+    \param type A pointer to the address.
+    \param len The length of the address.
+    \return 0 for OK, else -1. */
+int t30_set_tx_isp(t30_state_t *s, int type, const char *address, int len);
+
+/*! Get the received ??? (i.e. the one we will send to the far
+    end) associated with a T.30 context.
+    \brief Get the received ??? associated with a T.30 context.
+    \param s The T.30 context.
+    \param type The type of address.
+    \param type A pointer to the address.
+    \return 0 for OK, else -1. */
+size_t t30_get_tx_isp(t30_state_t *s, int *type, const char *address[]);
+
+/*! Get the received ??? (i.e. the one we will send to the far
+    end) associated with a T.30 context.
+    \brief Get the received ??? associated with a T.30 context.
+    \param s The T.30 context.
+    \param type The type of address.
+    \param type A pointer to the address.
+    \return 0 for OK, else -1. */
+size_t t30_get_rx_isp(t30_state_t *s, int *type, const char *address[]);
+
+/*! Set the transmitted ??? (i.e. the one we will send to the far
+    end) associated with a T.30 context.
+    \brief Set the transmitted ??? associated with a T.30 context.
+    \param s The T.30 context.
+    \param type The type of address.
+    \param type A pointer to the address.
+    \param len The length of the address.
+    \return 0 for OK, else -1. */
+int t30_set_tx_csa(t30_state_t *s, int type, const char *address, int len);
+
+/*! Get the received ??? (i.e. the one we will send to the far
+    end) associated with a T.30 context.
+    \brief Get the received ??? associated with a T.30 context.
+    \param s The T.30 context.
+    \param type The type of address.
+    \param type A pointer to the address.
+    \param len The length of the address.
+    \return 0 for OK, else -1. */
+size_t t30_get_tx_csa(t30_state_t *s, int *type, const char *address[]);
+
+/*! Get the received ??? (i.e. the one we will send to the far
+    end) associated with a T.30 context.
+    \brief Get the received ??? associated with a T.30 context.
+    \param s The T.30 context.
+    \param type The type of address.
+    \param type A pointer to the address.
+    \param len The length of the address.
+    \return 0 for OK, else -1. */
+size_t t30_get_rx_csa(t30_state_t *s, int *type, const char *address[]);
+
+/*! Set the transmitted header information associated with a T.30 context.
+    \brief Set the transmitted header information associated with a T.30 context.
+    \param s The T.30 context.
+    \param info A pointer to the information string.
+    \return 0 for OK, else -1. */
+int t30_set_tx_page_header_info(t30_state_t *s, const char *info);
+
+/*! Get the header information associated with a T.30 context.
+    \brief Get the header information associated with a T.30 context.
+    \param s The T.30 context.
+    \param info A pointer to a buffer for the header information.  The buffer
+           should be at least 51 bytes long.
+    \return the length of the string. */
+size_t t30_get_tx_page_header_info(t30_state_t *s, char *info);
+
+/*! Get the country of origin of the remote FAX machine associated with a T.30 context.
+    \brief Get the country of origin of the remote FAX machine associated with a T.30 context.
+    \param s The T.30 context.
+    \return a pointer to the country name, or NULL if the country is not known. */
+const char *t30_get_rx_country(t30_state_t *s);
+
+/*! Get the name of the vendor of the remote FAX machine associated with a T.30 context.
+    \brief Get the name of the vendor of the remote FAX machine associated with a T.30 context.
+    \param s The T.30 context.
+    \return a pointer to the vendor name, or NULL if the vendor is not known. */
+const char *t30_get_rx_vendor(t30_state_t *s);
+
+/*! Get the name of the model of the remote FAX machine associated with a T.30 context.
+    \brief Get the name of the model of the remote FAX machine associated with a T.30 context.
+    \param s The T.30 context.
+    \return a pointer to the model name, or NULL if the model is not known. */
+const char *t30_get_rx_model(t30_state_t *s);
+
+/*! Specify the file name of the next TIFF file to be received by a T.30
+    context.
+    \brief Set next receive file name.
+    \param s The T.30 context.
+    \param file The file name
+    \param stop_page The maximum page to receive. -1 for no restriction. */
+void t30_set_rx_file(t30_state_t *s, const char *file, int stop_page);
+
+/*! Specify the file name of the next TIFF file to be transmitted by a T.30
+    context.
+    \brief Set next transmit file name.
+    \param s The T.30 context.
+    \param file The file name
+    \param start_page The first page to send. -1 for no restriction.
+    \param stop_page The last page to send. -1 for no restriction. */
+void t30_set_tx_file(t30_state_t *s, const char *file, int start_page, int stop_page);
+
+/*! Set Internet aware FAX (IAF) mode.
+    \brief Set Internet aware FAX (IAF) mode.
+    \param s The T.30 context.
+    \param iaf TRUE for IAF, or FALSE for non-IAF. */
+void t30_set_iaf_mode(t30_state_t *s, int iaf);
+
+/*! Specify if error correction mode (ECM) is allowed by a T.30 context.
+    \brief Select ECM capability.
+    \param s The T.30 context.
+    \param enabled TRUE for ECM capable, FALSE for not ECM capable.
+    \return 0 if OK, else -1. */
+int t30_set_ecm_capability(t30_state_t *s, int enabled);
+
+/*! Specify the output encoding for TIFF files created during FAX reception.
+    \brief Specify the output encoding for TIFF files created during FAX reception.
+    \param s The T.30 context.
+    \param encoding The coding required. The options are T4_COMPRESSION_ITU_T4_1D,
+           T4_COMPRESSION_ITU_T4_2D, T4_COMPRESSION_ITU_T6. T6 is usually the
+           densest option, but support for it is broken in a number of software
+           packages.
+    \return 0 if OK, else -1. */
+int t30_set_rx_encoding(t30_state_t *s, int encoding);
+
+/*! Specify the minimum scan line time supported by a T.30 context.
+    \brief Specify minimum scan line time.
+    \param s The T.30 context.
+    \param min_time The minimum permitted scan line time, in milliseconds.
+    \return 0 if OK, else -1. */
+int t30_set_minimum_scan_line_time(t30_state_t *s, int min_time);
+
+/*! Specify which modem types are supported by a T.30 context.
+    \brief Specify supported modems.
+    \param s The T.30 context.
+    \param supported_modems Bit field list of the supported modems.
+    \return 0 if OK, else -1. */
+int t30_set_supported_modems(t30_state_t *s, int supported_modems);
+
+/*! Specify which compression types are supported by a T.30 context.
+    \brief Specify supported compression types.
+    \param s The T.30 context.
+    \param supported_compressions Bit field list of the supported compression types.
+    \return 0 if OK, else -1. */
+int t30_set_supported_compressions(t30_state_t *s, int supported_compressions);
+
+/*! Specify which resolutions are supported by a T.30 context.
+    \brief Specify supported resolutions.
+    \param s The T.30 context.
+    \param supported_resolutions Bit field list of the supported resolutions.
+    \return 0 if OK, else -1. */
+int t30_set_supported_resolutions(t30_state_t *s, int supported_resolutions);
+
+/*! Specify which images sizes are supported by a T.30 context.
+    \brief Specify supported image sizes.
+    \param s The T.30 context.
+    \param supported_image_sizes Bit field list of the supported widths and lengths.
+    \return 0 if OK, else -1. */
+int t30_set_supported_image_sizes(t30_state_t *s, int supported_image_sizes);
+
+/*! Specify which special T.30 features are supported by a T.30 context.
+    \brief Specify supported T.30 features.
+    \param s The T.30 context.
+    \param supported_t30_features Bit field list of the supported features.
+    \return 0 if OK, else -1. */
+int t30_set_supported_t30_features(t30_state_t *s, int supported_t30_features);
+
+/*! Set T.30 status. This may be used to adjust the status from within
+    the phase B and phase D callbacks.
+    \brief Set T.30 status.
+    \param s The T.30 context.
+    \param status The new status. */
+void t30_set_status(t30_state_t *s, int status);
+
+/*! Specify a period of responding with receiver not ready.
+    \brief Specify a period of responding with receiver not ready.
+    \param s The T.30 context.
+    \param count The number of times to report receiver not ready.
+    \return 0 if OK, else -1. */
+int t30_set_receiver_not_ready(t30_state_t *s, int count);
+
+/*! Set a callback function for T.30 phase B handling.
+    \brief Set a callback function for T.30 phase B handling.
+    \param s The T.30 context.
+    \param handler The callback function.
+    \param user_data An opaque pointer passed to the callback function. */
+void t30_set_phase_b_handler(t30_state_t *s, t30_phase_b_handler_t *handler, void *user_data);
+
+/*! Set a callback function for T.30 phase D handling.
+    \brief Set a callback function for T.30 phase D handling.
+    \param s The T.30 context.
+    \param handler The callback function.
+    \param user_data An opaque pointer passed to the callback function. */
+void t30_set_phase_d_handler(t30_state_t *s, t30_phase_d_handler_t *handler, void *user_data);
+
+/*! Set a callback function for T.30 phase E handling.
+    \brief Set a callback function for T.30 phase E handling.
+    \param s The T.30 context.
+    \param handler The callback function.
+    \param user_data An opaque pointer passed to the callback function. */
+void t30_set_phase_e_handler(t30_state_t *s, t30_phase_e_handler_t *handler, void *user_data);
+
+/*! Set a callback function for T.30 end of document handling.
+    \brief Set a callback function for T.30 end of document handling.
+    \param s The T.30 context.
+    \param handler The callback function.
+    \param user_data An opaque pointer passed to the callback function. */
+void t30_set_document_handler(t30_state_t *s, t30_document_handler_t *handler, void *user_data);
+
+/*! Set a callback function for T.30 frame exchange monitoring. This is called from the heart
+    of the signal processing, so don't take too long in the handler routine.
+    \brief Set a callback function for T.30 frame exchange monitoring.
+    \param s The T.30 context.
+    \param handler The callback function.
+    \param user_data An opaque pointer passed to the callback function. */
+void t30_set_real_time_frame_handler(t30_state_t *s, t30_real_time_frame_handler_t *handler, void *user_data);
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/spandsp/t30_fcf.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/spandsp/t30_fcf.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,123 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * fcf.h - ITU T.30 fax control field definitions
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2003 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: t30_fcf.h,v 1.17 2008/08/03 03:44:00 steveu Exp $
+ */
+
+/*! \file */
+
+#if !defined(_SPANDSP_T30_FCF_H_)
+#define _SPANDSP_T30_FCF_H_
+
+enum
+{
+    /*! Initial identification messages */
+    /*! From the called to the calling terminal. */
+    T30_DIS = 0x80,         /*! [0000 0001] Digital identification signal */
+    T30_CSI = 0x40,         /*! [0000 0010] Called subscriber identification */
+    T30_NSF = 0x20,         /*! [0000 0100] Non-standard facilities */
+
+    /*! Commands to send */
+    /*! From a calling terminal wishing to be a receiver, to a called terminal
+        which is capable of transmitting. */
+    T30_DTC = 0x81,         /*! [1000 0001] Digital transmit command */
+    T30_CIG = 0x41,         /*! [1000 0010] Calling subscriber identification */
+    T30_NSC = 0x21,         /*! [1000 0100] Non-standard facilities command */
+    T30_PWD = 0xC1,         /*! [1000 0011] Password */
+    T30_SEP = 0xA1,         /*! [1000 0101] Selective polling */
+    T30_PSA = 0x61,         /*! [1000 0110] Polled subaddress */
+    T30_CIA = 0xE1,         /*! [1000 0111] Calling subscriber internet address */
+    T30_ISP = 0x11,         /*! [1000 1000] Internet selective polling address */
+
+    /*! Commands to receive */
+    /*! From a calling terminal wishing to be a transmitter, to a called terminal
+        which is capable of receiving. */
+    T30_DCS = 0x82,         /*! [X100 0001] Digital command signal */
+    T30_TSI = 0x42,         /*! [X100 0010] Transmitting subscriber information */
+    T30_NSS = 0x22,         /*! [X100 0100] Non-standard facilities set-up */
+    T30_SUB = 0xC2,         /*! [X100 0011] Sub-address */
+    T30_SID = 0xA2,         /*! [X100 0101] Sender identification */
+    /*! T30_TCF - Training check is a burst of 1.5s of zeros sent using the image modem */
+    T30_CTC = 0x12,         /*! [X100 1000] Continue to correct */
+    T30_TSA = 0x62,         /*! [X100 0110] Transmitting subscriber internet address */
+    T30_IRA = 0xE2,         /*! [X100 0111] Internet routing address */
+
+    /*! Pre-message response signals */
+    /*! From the receiver to the transmitter. */
+    T30_CFR = 0x84,         /*! [X010 0001] Confirmation to receive */
+    T30_FTT = 0x44,         /*! [X010 0010] Failure to train */
+    T30_CTR = 0xC4,         /*! [X010 0011] Response for continue to correct */
+    T30_CSA = 0x24,         /*! [X010 0100] Called subscriber internet address */
+
+    /*! Post-message commands */
+    T30_EOM = 0x8E,         /*! [X111 0001] End of message */
+    T30_MPS = 0x4E,         /*! [X111 0010] Multipage signal */
+    T30_EOP = 0x2E,         /*! [X111 0100] End of procedure */
+    T30_PRI_EOM = 0x9E,     /*! [X111 1001] Procedure interrupt - end of procedure */
+    T30_PRI_MPS = 0x5E,     /*! [X111 1010] Procedure interrupt - multipage signal */
+    T30_PRI_EOP = 0x3E,     /*! [X111 1100] Procedure interrupt - end of procedure */
+    T30_EOS = 0x1E,         /*! [X111 1000] End of selection */
+    T30_PPS = 0xBE,         /*! [X111 1101] Partial page signal */
+    T30_EOR = 0xCE,         /*! [X111 0011] End of retransmission */
+    T30_RR = 0x6E,          /*! [X111 0110] Receiver ready */
+
+    /*! Post-message responses */
+    T30_MCF = 0x8C,         /*! [X011 0001] Message confirmation */
+    T30_RTP = 0xCC,         /*! [X011 0011] Retrain positive */
+    T30_RTN = 0x4C,         /*! [X011 0010] Retrain negative */
+    T30_PIP = 0xAC,         /*! [X011 0101] Procedure interrupt positive */
+    T30_PIN = 0x2C,         /*! [X011 0100] Procedure interrupt negative */
+    T30_PPR = 0xBC,         /*! [X011 1101] Partial page request */
+    T30_RNR = 0xEC,         /*! [X011 0111] Receive not ready */
+    T30_ERR = 0x1C,         /*! [X011 1000] Response for end of retransmission */
+    T30_FDM = 0xFC,         /*! [X011 1111] File diagnostics message */
+
+    /*! Other line control signals */
+    T30_DCN = 0xFA,         /*! [X101 1111] Disconnect */
+    T30_CRP = 0x1A,         /*! [X101 1000] Command repeat */
+    T30_FNV = 0xCA,         /*! [X101 0011] Field not valid */
+    T30_TNR = 0xEA,         /*! [X101 0111] Transmitter not ready */
+    T30_TR = 0x6A,          /*! [X101 0110] Transmitter ready */
+    T30_TK = 0x4B,          /*! [1101 0010] Transmitter keys */
+    T30_RK = 0x4A,          /*! [0101 0010] Receiver keys */
+    T30_PSS = 0x1F,         /*! [1111 1000] Present signature signal (used only as FCF2) */
+    T30_DES = 0xA0,         /*! [0000 0101] Digital extended signal */
+    T30_DEC = 0x93,         /*! [1100 1001] Digital extended command */
+    T30_DER = 0x53,         /*! [1100 1010] Digital extended request */
+    T30_DTR = 0x11,         /*! [1000 1000] Digital turnaround request (conflicts with ISP) */
+    T30_DNK = 0x9A,         /*! [X101 1001] Digital not acknowledge */
+    T30_PID = 0x6C,         /*! [X011 0110] Procedure interrupt disconnect */
+    T30_SPI = 0x10,         /*! [0000 1000] Security page indicator */
+    T30_SPT = 0x80,         /*! [0000 0001] Security page type */
+
+    /*! Something only use as a secondary value in error correcting mode */
+    T30_NULL = 0x00,        /*! [0000 0000] Nothing to say */
+
+    /*! Information frame types used for error correction mode, in T.4 */
+    T4_FCD = 0x06,          /*! [0110 0000] Facsimile coded data */
+    T4_RCP = 0x86           /*! [0110 0001] Return to control for partial page */
+};
+
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/spandsp/t30_logging.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/spandsp/t30_logging.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,63 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * t30_logging.h - definitions for T.30 fax processing
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2003 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: t30_logging.h,v 1.2 2008/04/17 14:27:00 steveu Exp $
+ */
+
+/*! \file */
+
+#if !defined(_SPANDSP_T30_LOGGING_H_)
+#define _SPANDSP_T30_LOGGING_H_
+
+#if defined(__cplusplus)
+extern "C"
+{
+#endif
+
+/*! Return a text name for a T.30 frame type.
+    \brief Return a text name for a T.30 frame type.
+    \param x The frametype octet.
+    \return A pointer to the text name for the frame type. If the frame type is
+            not value, the string "???" is returned. */
+const char *t30_frametype(uint8_t x);
+
+/*! Decode a DIS, DTC or DCS frame, and log the contents.
+    \brief Decode a DIS, DTC or DCS frame, and log the contents.
+    \param s The T.30 context.
+    \param dis A pointer to the frame to be decoded.
+    \param len The length of the frame. */
+void t30_decode_dis_dtc_dcs(t30_state_t *s, const uint8_t *dis, int len);
+
+/*! Convert a phase E completion code to a short text description.
+    \brief Convert a phase E completion code to a short text description.
+    \param result The result code.
+    \return A pointer to the description. */
+const char *t30_completion_code_to_str(int result);
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/spandsp/t31.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/spandsp/t31.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,277 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * t31.h - A T.31 compatible class 1 FAX modem interface.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2004 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: t31.h,v 1.52 2008/07/31 12:55:30 steveu Exp $
+ */
+
+/*! \file */
+
+#if !defined(_SPANDSP_T31_H_)
+#define _SPANDSP_T31_H_
+
+/*! \page t31_page T.31 Class 1 FAX modem protocol handling
+\section t31_page_sec_1 What does it do?
+The T.31 class 1 FAX modem modules implements a class 1 interface to the FAX
+modems in spandsp.
+
+\section t31_page_sec_2 How does it work?
+*/
+
+typedef struct t31_state_s t31_state_t;
+
+typedef int (t31_modem_control_handler_t)(t31_state_t *s, void *user_data, int op, const char *num);
+
+#define T31_TX_BUF_LEN          (4096)
+#define T31_TX_BUF_HIGH_TIDE    (4096 - 1024)
+#define T31_TX_BUF_LOW_TIDE     (1024)
+#define T31_MAX_HDLC_LEN        284
+#define T31_T38_MAX_HDLC_LEN    260
+
+/*!
+    Analogue FAX front end channel descriptor. This defines the state of a single working
+    instance of an analogue line FAX front end.
+*/
+typedef struct
+{
+    fax_modems_state_t modems;
+
+    /*! The transmit signal handler to be used when the current one has finished sending. */
+    span_tx_handler_t *next_tx_handler;
+    void *next_tx_user_data;
+
+    /*! \brief No of data bits in current_byte. */
+    int bit_no;
+    /*! \brief The current data byte in progress. */
+    int current_byte;
+
+    /*! \brief Rx power meter, used to detect silence. */
+    power_meter_t rx_power;
+    /*! \brief Last sample, used for an elementary HPF for the power meter. */
+    int16_t last_sample;
+    /*! \brief The current silence threshold. */
+    int32_t silence_threshold_power;
+
+    /*! \brief Samples of silence heard */
+    int silence_heard;
+} t31_audio_front_end_state_t;
+
+/*!
+    Analogue FAX front end channel descriptor. This defines the state of a single working
+    instance of an analogue line FAX front end.
+*/
+typedef struct
+{
+    t38_core_state_t t38;
+
+    /*! \brief The number of octets to send in each image packet (non-ECM or ECM) at the current
+               rate and the current specified packet interval. */
+    int octets_per_data_packet;
+
+    int timed_step;
+
+    int ms_per_tx_chunk;
+    int merge_tx_fields;
+
+    /*! \brief TRUE is there has been some T.38 data missed */
+    int missing_data;
+
+    /*! \brief The number of times an indicator packet will be sent. Numbers greater than one
+               will increase reliability for UDP transmission. Zero is valid, to suppress all
+               indicator packets for TCP transmission. */
+    int indicator_tx_count;
+
+    /*! \brief The number of times a data packet which ends transmission will be sent. Numbers
+               greater than one will increase reliability for UDP transmission. Zero is not valid. */
+    int data_end_tx_count;
+
+    /*! \brief The current T.38 data type being transmitted */
+    int current_tx_data_type;
+    /*! \brief The next queued tramsit indicator */
+    int next_tx_indicator;
+
+    int trailer_bytes;
+
+    struct
+    {
+        uint8_t buf[T31_T38_MAX_HDLC_LEN];
+        int len;
+    } hdlc_rx;
+
+    int current_rx_type;
+    int current_tx_type;
+
+    int32_t next_tx_samples;
+    int32_t timeout_rx_samples;
+    /*! \brief A "sample" count, used to time events */
+    int32_t samples;
+} t31_t38_front_end_state_t;
+
+/*!
+    T.31 descriptor. This defines the working state for a single instance of
+    a T.31 FAX modem.
+*/
+struct t31_state_s
+{
+    at_state_t at_state;
+    t31_modem_control_handler_t *modem_control_handler;
+    void *modem_control_user_data;
+
+    t31_audio_front_end_state_t audio;
+    t31_t38_front_end_state_t t38_fe;
+    /*! TRUE if working in T.38 mode. */
+    int t38_mode;
+
+    /*! HDLC buffer, for composing an HDLC frame from the computer to the channel. */
+    struct
+    {
+        uint8_t buf[T31_MAX_HDLC_LEN];
+        int len;
+        int ptr;
+        /*! \brief TRUE when the end of HDLC data from the computer has been detected. */
+        int final;
+    } hdlc_tx;
+    /*! Buffer for data from the computer to the channel. */
+    struct
+    {
+        uint8_t data[T31_TX_BUF_LEN];
+        /*! \brief The number of bytes stored in transmit buffer. */
+        int in_bytes;
+        /*! \brief The number of bytes sent from the transmit buffer. */
+        int out_bytes;
+        /*! \brief TRUE if the flow of real data has started. */
+        int data_started;
+        /*! \brief TRUE if holding up further data into the buffer, for flow control. */
+        int holding;
+        /*! \brief TRUE when the end of non-ECM data from the computer has been detected. */
+        int final;
+    } tx;
+
+    /*! TRUE if DLE prefix just used */
+    int dled;
+
+	/*! \brief Samples of silence awaited, as specified in a "wait for silence" command */
+    int silence_awaited;
+
+    /*! \brief The current bit rate for the FAX fast message transfer modem. */
+    int bit_rate;
+    /*! \brief TRUE if a valid HDLC frame has been received in the current reception period. */
+    int rx_frame_received;
+
+    /*! \brief Samples elapsed in the current call */
+    int64_t call_samples;
+    int64_t dte_data_timeout;
+
+    /*! \brief The currently queued modem type. */
+    int modem;
+    /*! \brief TRUE when short training mode has been selected by the computer. */
+    int short_train;
+    queue_state_t *rx_queue;
+
+    /*! \brief Error and flow logging control */
+    logging_state_t logging;
+};
+
+#if defined(__cplusplus)
+extern "C"
+{
+#endif
+
+void t31_call_event(t31_state_t *s, int event);
+
+int t31_at_rx(t31_state_t *s, const char *t, int len);
+
+/*! Process a block of received T.31 modem audio samples.
+    \brief Process a block of received T.31 modem audio samples.
+    \param s The T.31 modem context.
+    \param amp The audio sample buffer.
+    \param len The number of samples in the buffer.
+    \return The number of samples unprocessed. */
+int t31_rx(t31_state_t *s, int16_t amp[], int len);
+
+/*! Generate a block of T.31 modem audio samples.
+    \brief Generate a block of T.31 modem audio samples.
+    \param s The T.31 modem context.
+    \param amp The audio sample buffer.
+    \param max_len The number of samples to be generated.
+    \return The number of samples actually generated.
+*/
+int t31_tx(t31_state_t *s, int16_t amp[], int max_len);
+
+int t31_t38_send_timeout(t31_state_t *s, int samples);
+
+/*! Select whether silent audio will be sent when transmit is idle.
+    \brief Select whether silent audio will be sent when transmit is idle.
+    \param s The T.31 modem context.
+    \param transmit_on_idle TRUE if silent audio should be output when the transmitter is
+           idle. FALSE to transmit zero length audio when the transmitter is idle. The default
+           behaviour is FALSE.
+*/
+void t31_set_transmit_on_idle(t31_state_t *s, int transmit_on_idle);
+
+/*! Select whether TEP mode will be used (or time allowed for it (when transmitting).
+    \brief Select whether TEP mode will be used.
+    \param s The T.31 modem context.
+    \param use_tep TRUE if TEP is to be ised.
+*/
+void t31_set_tep_mode(t31_state_t *s, int use_tep);
+
+/*! Select whether T.38 data will be paced as it is transmitted.
+    \brief Select whether T.38 data will be paced.
+    \param s The T.31 modem context.
+    \param without_pacing TRUE if data is to be sent as fast as possible. FALSE if it is
+           to be paced.
+*/
+void t31_set_t38_config(t31_state_t *s, int without_pacing);
+
+/*! Initialise a T.31 context. This must be called before the first
+    use of the context, to initialise its contents.
+    \brief Initialise a T.31 context.
+    \param s The T.31 context.
+    \param at_tx_handler A callback routine to handle AT interpreter channel output.
+    \param at_tx_user_data An opaque pointer passed in called to at_tx_handler.
+    \param modem_control_handler A callback routine to handle control of the modem (off-hook, etc).
+    \param modem_control_user_data An opaque pointer passed in called to modem_control_handler.
+    \param tx_t38_packet_handler ???
+    \param tx_t38_packet_user_data ???
+    \return A pointer to the T.31 context. */
+t31_state_t *t31_init(t31_state_t *s,
+                      at_tx_handler_t *at_tx_handler,
+                      void *at_tx_user_data,
+                      t31_modem_control_handler_t *modem_control_handler,
+                      void *modem_control_user_data,
+                      t38_tx_packet_handler_t *tx_t38_packet_handler,
+                      void *tx_t38_packet_user_data);
+
+/*! Release a T.31 context.
+    \brief Release a T.31 context.
+    \param s The T.31 context.
+    \return 0 for OK */
+int t31_release(t31_state_t *s);
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/spandsp/t35.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/spandsp/t35.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,73 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * t35.h - ITU T.35 FAX non-standard facility processing.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2003 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: t35.h,v 1.14 2008/04/17 14:27:00 steveu Exp $
+ */
+
+/*! \file */
+
+#if !defined(_SPANDSP_T35_H_)
+#define _SPANDSP_T35_H_
+
+/*! \page t35_page T.35 manufacturer specific processing for FAX machines
+\section t35_page_sec_1 What does it do?
+???.
+
+\section t35_page_sec_2 How does it work?
+???.
+*/
+
+/*! A table of the country names associated with each possible value of the T.35 country code
+    selector octet. */
+extern const char *t35_country_codes[256];
+
+#if defined(__cplusplus)
+extern "C"
+{
+#endif
+
+/*! Decode an NSF field to try to determine the make and model of the
+    remote machine.
+    \brief Decode an NSF field.
+    \param msg The NSF message.
+    \param len The length of the NSF message.
+    \param country A pointer which will be pointed to the identified country of origin.
+           If a NULL pointer is given, the country of origin will not be returned.
+           If the country of origin is not identified, NULL will be returned.
+    \param vendor A pointer which will be pointed to the identified vendor.
+           If a NULL pointer is given, the vendor ID will not be returned.
+           If the vendor is not identified, NULL will be returned.
+    \param model A pointer which will be pointed to the identified model.
+           If a NULL pointer is given, the model will not be returned.
+           If the model is not identified, NULL will be returned.
+    \return TRUE if the machine was identified, otherwise FALSE.
+*/
+int t35_decode(const uint8_t *msg, int len, const char **country, const char **vendor, const char **model);
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/spandsp/t38_core.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/spandsp/t38_core.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,422 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * t38_core.h - An implementation of T.38, less the packet exchange part
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2005 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: t38_core.h,v 1.28 2008/06/19 13:27:45 steveu Exp $
+ */
+
+/*! \file */
+
+#if !defined(_SPANDSP_T38_CORE_H_)
+#define _SPANDSP_T38_CORE_H_
+
+/*! \page t38_core_page T.38 real time FAX over IP message handling
+There are two ITU recommendations which address sending FAXes over IP networks. T.37 specifies a
+method of encapsulating FAX images in e-mails, and transporting them to the recipient (an e-mail
+box, or another FAX machine) in a store-and-forward manner. T.38 defines a protocol for
+transmitting a FAX across an IP network in real time. The core T.38 modules implements the basic
+message handling for the T.38, real time, FAX over IP (FoIP) protocol.
+
+The T.38 protocol can operate between:
+    - Internet-aware FAX terminals, which connect directly to an IP network. The T.38 terminal module
+      extends this module to provide a complete T.38 terminal.
+    - FAX gateways, which allow traditional PSTN FAX terminals to communicate through the Internet.
+      The T.38 gateway module extends this module to provide a T.38 gateway.
+    - A combination of terminals and gateways.
+
+T.38 is the only standardised protocol which exists for real-time FoIP. Reliably transporting a
+FAX between PSTN FAX terminals, through an IP network, requires use of the T.38 protocol at FAX
+gateways. VoIP connections are not robust for modem use, including FAX modem use. Most use low
+bit rate codecs, which cannot convey the modem signals accurately. Even when high bit rate
+codecs are used, VoIP connections suffer dropouts and timing adjustments, which modems cannot
+tolerate. In a LAN environment the dropout rate may be very low, but the timing adjustments which
+occur in VoIP connections still make modem operation unreliable. T.38 FAX gateways deal with the
+delays, timing jitter, and packet loss experienced in packet networks, and isolate the PSTN FAX
+terminals from these as far as possible. In addition, by sending FAXes as image data, rather than
+digitised audio, they reduce the required bandwidth of the IP network.
+
+\section t38_core_page_sec_1 What does it do?
+
+\section t38_core_page_sec_2 How does it work?
+
+Timing differences and jitter between two T.38 entities can be a serious problem, if one of those
+entities is a PSTN gateway.
+
+Flow control for non-ECM image data takes advantage of several features of the T.30 specification.
+First, an unspecified number of 0xFF octets may be sent at the start of transmission. This means we
+can add endless extra 0xFF bytes at this point, without breaking the T.30 spec. In practice, we
+cannot add too many, or we will affect the timing tolerance of the T.30 protocol by delaying the
+response at the end of each image. Secondly, just before an end of line (EOL) marker we can pad
+with zero bits. Again, the number is limited only by need to avoid upsetting the timing of the
+step following the non-ECM data.
+*/
+
+/*! T.38 indicator types */
+enum t30_indicator_types_e
+{
+    T38_IND_NO_SIGNAL = 0,
+    T38_IND_CNG,
+    T38_IND_CED,
+    T38_IND_V21_PREAMBLE,
+    T38_IND_V27TER_2400_TRAINING,
+    T38_IND_V27TER_4800_TRAINING,
+    T38_IND_V29_7200_TRAINING,
+    T38_IND_V29_9600_TRAINING,
+    T38_IND_V17_7200_SHORT_TRAINING,
+    T38_IND_V17_7200_LONG_TRAINING,
+    T38_IND_V17_9600_SHORT_TRAINING,
+    T38_IND_V17_9600_LONG_TRAINING,
+    T38_IND_V17_12000_SHORT_TRAINING,
+    T38_IND_V17_12000_LONG_TRAINING,
+    T38_IND_V17_14400_SHORT_TRAINING,
+    T38_IND_V17_14400_LONG_TRAINING,
+    T38_IND_V8_ANSAM,
+    T38_IND_V8_SIGNAL,
+    T38_IND_V34_CNTL_CHANNEL_1200,
+    T38_IND_V34_PRI_CHANNEL,
+    T38_IND_V34_CC_RETRAIN,
+    T38_IND_V33_12000_TRAINING,
+    T38_IND_V33_14400_TRAINING
+};
+
+/*! T.38 data types */
+enum t38_data_types_e
+{
+    T38_DATA_NONE = -1,
+    T38_DATA_V21 = 0,
+    T38_DATA_V27TER_2400,
+    T38_DATA_V27TER_4800,
+    T38_DATA_V29_7200,
+    T38_DATA_V29_9600,
+    T38_DATA_V17_7200,
+    T38_DATA_V17_9600,
+    T38_DATA_V17_12000,
+    T38_DATA_V17_14400,
+    T38_DATA_V8,
+    T38_DATA_V34_PRI_RATE,
+    T38_DATA_V34_CC_1200,
+    T38_DATA_V34_PRI_CH,
+    T38_DATA_V33_12000,
+    T38_DATA_V33_14400
+};
+
+/*! T.38 data field types */
+enum t38_field_types_e
+{
+    T38_FIELD_HDLC_DATA = 0,
+    T38_FIELD_HDLC_SIG_END,
+    T38_FIELD_HDLC_FCS_OK,
+    T38_FIELD_HDLC_FCS_BAD,
+    T38_FIELD_HDLC_FCS_OK_SIG_END,
+    T38_FIELD_HDLC_FCS_BAD_SIG_END,
+    T38_FIELD_T4_NON_ECM_DATA,
+    T38_FIELD_T4_NON_ECM_SIG_END,
+    T38_FIELD_CM_MESSAGE,
+    T38_FIELD_JM_MESSAGE,
+    T38_FIELD_CI_MESSAGE,
+    T38_FIELD_V34RATE
+};
+
+/*! T.38 field classes */
+enum t38_field_classes_e
+{
+    T38_FIELD_CLASS_NONE = 0,
+    T38_FIELD_CLASS_HDLC,
+    T38_FIELD_CLASS_NON_ECM,
+};
+
+/*! T.38 message types */
+enum t38_message_types_e
+{
+    T38_TYPE_OF_MSG_T30_INDICATOR = 0,
+    T38_TYPE_OF_MSG_T30_DATA
+};
+
+/*! T.38 transport types */
+enum t38_transport_types_e
+{
+    T38_TRANSPORT_UDPTL = 0,
+    T38_TRANSPORT_RTP,
+    T38_TRANSPORT_TCP
+};
+
+#define T38_RX_BUF_LEN  2048
+#define T38_TX_BUF_LEN  16384
+
+/*! T.38 data field */
+typedef struct
+{
+    int field_type;
+    const uint8_t *field;
+    int field_len;
+} t38_data_field_t;
+
+typedef struct t38_core_state_s t38_core_state_t;
+
+typedef int (t38_tx_packet_handler_t)(t38_core_state_t *s, void *user_data, const uint8_t *buf, int len, int count);
+
+typedef int (t38_rx_indicator_handler_t)(t38_core_state_t *s, void *user_data, int indicator);
+typedef int (t38_rx_data_handler_t)(t38_core_state_t *s, void *user_data, int data_type, int field_type, const uint8_t *buf, int len);
+typedef int (t38_rx_missing_handler_t)(t38_core_state_t *s, void *user_data, int rx_seq_no, int expected_seq_no);
+
+/*!
+    Core T.38 state, common to all modes of T.38.
+*/
+struct t38_core_state_s
+{
+    /*! Handler routine to transmit IFP packets generated by the T.38 protocol engine */
+    t38_tx_packet_handler_t *tx_packet_handler;
+    /*! An opaque pointer passed to tx_packet_handler */
+    void *tx_packet_user_data;
+
+    /*! Handler routine to process received indicator packets */
+    t38_rx_indicator_handler_t *rx_indicator_handler;
+    /*! Handler routine to process received data packets */
+    t38_rx_data_handler_t *rx_data_handler;
+    /*! Handler routine to process the missing packet condition */
+    t38_rx_missing_handler_t *rx_missing_handler;
+    /*! An opaque pointer passed to any of the above receive handling routines */
+    void *rx_user_data;
+
+    /*! NOTE - Bandwidth reduction shall only be done on suitable Phase C data, i.e., MH, MR
+        and - in the case of transcoding to JBIG - MMR. MMR and JBIG require reliable data
+        transport such as that provided by TCP. When transcoding is selected, it shall be
+        applied to every suitable page in a call. */
+
+    /*! Method 1: Local generation of TCF (required for use with TCP).
+        Method 2: Transfer of TCF is required for use with UDP (UDPTL or RTP).
+        Method 2 is not recommended for use with TCP. */
+    int data_rate_management_method;
+    
+    /*! The emitting gateway may indicate a preference for either UDP/UDPTL, or
+        UDP/RTP, or TCP for transport of T.38 IFP Packets. The receiving device
+        selects the transport protocol. */
+    int data_transport_protocol;
+
+    /*! Indicates the capability to remove and insert fill bits in Phase C, non-ECM
+        data to reduce bandwidth in the packet network. */
+    int fill_bit_removal;
+
+    /*! Indicates the ability to convert to/from MMR from/to the line format to
+        improve the compression of the data, and reduce the bandwidth, in the
+        packet network. */
+    int mmr_transcoding;
+
+    /*! Indicates the ability to convert to/from JBIG to reduce bandwidth. */
+    int jbig_transcoding;
+
+    /*! For UDP (UDPTL or RTP) modes, this option indicates the maximum
+        number of octets that can be stored on the remote device before an overflow
+        condition occurs. It is the responsibility of the transmitting application to
+        limit the transfer rate to prevent an overflow. The negotiated data rate
+        should be used to determine the rate at which data is being removed from
+        the buffer. */
+    int max_buffer_size;
+
+    /*! This option indicates the maximum size of a UDPTL packet or the
+        maximum size of the payload within an RTP packet that can be accepted by
+        the remote device. */
+    int max_datagram_size;
+
+    /*! This is the version number of ITU-T Rec. T.38. New versions shall be
+        compatible with previous versions. */
+    int t38_version;
+
+    /*! The fastest data rate supported by the T.38 channel. */
+    int fastest_image_data_rate;
+
+    /*! \brief The number of times an indicator packet will be sent. Numbers greater than one
+               will increase reliability for UDP transmission. Zero is valid, to suppress all
+               indicator packets for TCP transmission. */
+    int indicator_tx_count;
+
+    /*! \brief The number of times a data packet which does not end transmission will be sent.
+               Numbers greater than one will increase reliability for UDP transmission. Zero
+               is not valid. */
+    int data_tx_count;
+
+    /*! \brief The number of times a data packet which ends transmission will be sent. Numbers
+               greater than one will increase reliability for UDP transmission. Zero is not valid. */
+    int data_end_tx_count;
+
+    /*! TRUE if IFP packet sequence numbers are relevant. For some transports, like TPKT
+        over TCP they are not relevent. */
+    int check_sequence_numbers;
+
+    /*! The sequence number for the next packet to be transmitted */
+    int tx_seq_no;
+    /*! The sequence number expected in the next received packet */
+    int rx_expected_seq_no;
+
+    /*! The current receive indicator - i.e. the last indicator received */
+    int current_rx_indicator;
+    /*! The current receive data type - i.e. the last data type received */
+    int current_rx_data_type;
+    /*! The current receive field type - i.e. the last field_type received */
+    int current_rx_field_type;
+    /*! The current transmit indicator - i.e. the last indicator transmitted */
+    int current_tx_indicator;
+    /*! The bit rate for V.34 operation */
+    int v34_rate;
+
+    /*! A count of missing receive packets. This count might not be accurate if the
+        received packet numbers jump wildly. */
+    int missing_packets;
+
+    logging_state_t logging;
+};
+
+#if defined(__cplusplus)
+extern "C"
+{
+#endif
+
+/*! \brief Convert the code for an indicator to a short text name.
+    \param indicator The type of indicator.
+    \return A pointer to a short text name for the indicator. */
+const char *t38_indicator_to_str(int indicator);
+
+/*! \brief Convert the code for a type of data to a short text name.
+    \param data_type The data type.
+    \return A pointer to a short text name for the data type. */
+const char *t38_data_type_to_str(int data_type);
+
+/*! \brief Convert the code for a type of data field to a short text name.
+    \param field_type The field type.
+    \return A pointer to a short text name for the field type. */
+const char *t38_field_type_to_str(int field_type);
+
+/*! \brief Convert the code for a CM profile code to text description.
+    \param profile The profile code from a CM message.
+    \return A pointer to a short text description of the profile. */
+const char *t38_cm_profile_to_str(int profile);
+
+/*! \brief Convert a JM message code to text description.
+    \param data The data field of the message.
+    \param len The length of the data field.
+    \return A pointer to a short text description of the profile. */
+const char *t38_jm_to_str(const uint8_t *data, int len);
+
+/*! \brief Convert a V34rate message to an actual bit rate.
+    \param data The data field of the message.
+    \param len The length of the data field.
+    \return The bit rate, or -1 for a bad message. */
+int t38_v34rate_to_bps(const uint8_t *data, int len);
+
+/*! \brief Send an indicator packet
+    \param s The T.38 context.
+    \param indicator The indicator to send.
+    \param count The number of copies of the packet to send.
+    \return ??? */
+int t38_core_send_indicator(t38_core_state_t *s, int indicator, int count);
+
+/*! \brief Send a data packet
+    \param s The T.38 context.
+    \param data_type The packet's data type.
+    \param field_type The packet's field type.
+    \param field The message data content for the packet.
+    \param field_len The length of the message data, in bytes.
+    \param count The number of copies of the packet to send.
+    \return ??? */
+int t38_core_send_data(t38_core_state_t *s, int data_type, int field_type, const uint8_t field[], int field_len, int count);
+
+/*! \brief Send a data packet
+    \param s The T.38 context.
+    \param data_type The packet's data type.
+    \param field The list of fields.
+    \param fields The number of fields in the list.
+    \param count The number of copies of the packet to send.
+    \return ??? */
+int t38_core_send_data_multi_field(t38_core_state_t *s, int data_type, const t38_data_field_t field[], int fields, int count);
+
+/*! \brief Process a received T.38 IFP packet.
+    \param s The T.38 context.
+    \param buf The packet contents.
+    \param len The length of the packet contents.
+    \param seq_no The packet sequence number.
+    \return 0 for OK, else -1. */
+int t38_core_rx_ifp_packet(t38_core_state_t *s, const uint8_t *buf, int len, uint16_t seq_no);
+
+/*! Set the method to be used for data rate management, as per the T.38 spec.
+    \param s The T.38 context.
+    \param method 1 for pass TCF across the T.38 link, 2 for handle TCF locally.
+*/
+void t38_set_data_rate_management_method(t38_core_state_t *s, int method);
+
+/*! Set the data transport protocol.
+    \param s The T.38 context.
+    \param data_transport_protocol UDPTL, RTP or TPKT.
+*/
+void t38_set_data_transport_protocol(t38_core_state_t *s, int data_transport_protocol);
+
+/*! Set the non-ECM fill bit removal mode.
+    \param s The T.38 context.
+    \param fill_bit_removal TRUE to remove fill bits across the T.38 link, else FALSE.
+*/
+void t38_set_fill_bit_removal(t38_core_state_t *s, int fill_bit_removal);
+
+/*! Set the MMR transcoding mode.
+    \param s The T.38 context.
+    \param mmr_transcoding TRUE to transcode to MMR across the T.38 link, else FALSE.
+*/
+void t38_set_mmr_transcoding(t38_core_state_t *s, int mmr_transcoding);
+
+/*! Set the JBIG transcoding mode.
+    \param s The T.38 context.
+    \param jbig_transcoding TRUE to transcode to JBIG across the T.38 link, else FALSE.
+*/
+void t38_set_jbig_transcoding(t38_core_state_t *s, int jbig_transcoding);
+
+void t38_set_max_buffer_size(t38_core_state_t *s, int max_buffer_size);
+
+void t38_set_max_datagram_size(t38_core_state_t *s, int max_datagram_size);
+
+int t38_get_fastest_image_data_rate(t38_core_state_t *s);
+
+/*! Set the T.38 version to be emulated.
+    \param s The T.38 context.
+    \param t38_version Version number, as in the T.38 spec.
+*/
+void t38_set_t38_version(t38_core_state_t *s, int t38_version);
+
+/* Set the sequence number handling option.
+    \param s The T.38 context.
+    \param check TRUE to check sequence numbers, and handle gaps reasonably. FALSE
+           for no sequence number processing (e.g. for TPKT over TCP transport).
+*/
+void t38_set_sequence_number_handling(t38_core_state_t *s, int check);
+
+t38_core_state_t *t38_core_init(t38_core_state_t *s,
+                                t38_rx_indicator_handler_t *rx_indicator_handler,
+                                t38_rx_data_handler_t *rx_data_handler,
+                                t38_rx_missing_handler_t *rx_missing_handler,
+                                void *rx_user_data,
+                                t38_tx_packet_handler_t tx_packet_handler,
+                                void *tx_packet_user_data);
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/spandsp/t38_gateway.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/spandsp/t38_gateway.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,333 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * t38_gateway.h - A T.38, less the packet exchange part
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2005, 2006, 2007 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: t38_gateway.h,v 1.56 2008/08/14 14:06:05 steveu Exp $
+ */
+
+/*! \file */
+
+#if !defined(_SPANDSP_T38_GATEWAY_H_)
+#define _SPANDSP_T38_GATEWAY_H_
+
+/*! \page t38_gateway_page T.38 real time FAX over IP PSTN gateway
+\section t38_gateway_page_sec_1 What does it do?
+
+The T.38 gateway facility provides a robust interface between T.38 IP packet streams and
+and 8k samples/second audio streams. It provides the buffering and flow control features needed
+to maximum the tolerance of jitter and packet loss on the IP network.
+
+\section t38_gateway_page_sec_2 How does it work?
+*/
+
+#define T38_RX_BUF_LEN          2048
+#define T38_TX_HDLC_BUFS        256
+/* Make sure the HDLC frame buffers are big enough for ECM frames. */
+#define T38_MAX_HDLC_LEN        260
+
+typedef struct t38_gateway_state_s t38_gateway_state_t;
+
+/*!
+    T.30 real time frame handler.
+    \brief T.30 real time frame handler.
+    \param s The T.30 context.
+    \param user_data An opaque pointer.
+    \param direction TRUE for incoming, FALSE for outgoing.
+    \param msg The HDLC message.
+    \param len The length of the message.
+*/
+typedef void (t38_gateway_real_time_frame_handler_t)(t38_gateway_state_t *s,
+                                                     void *user_data,
+                                                     int direction,
+                                                     const uint8_t *msg,
+                                                     int len);
+
+/*!
+    T.38 gateway T.38 side channel descriptor.
+*/
+typedef struct
+{
+    /*! Core T.38 IFP support */
+    t38_core_state_t t38;
+
+    /*! \brief TRUE if the NSF, NSC, and NSS are to be suppressed by altering
+               their contents to something the far end will not recognise. */
+    int suppress_nsx_len[2];
+    /*! \brief TRUE if we need to corrupt the HDLC frame in progress, so the receiver cannot
+               interpret it. The two values are for the two directions. */
+    int corrupt_current_frame[2];
+
+    /*! \brief the current class of field being received - i.e. none, non-ECM or HDLC */
+    int current_rx_field_class;
+    /*! \brief The T.38 indicator currently in use */
+    int in_progress_rx_indicator;
+
+    /*! \brief The current T.38 data type being sent. */
+    int current_tx_data_type;
+} t38_gateway_t38_state_t;
+
+/*!
+    T.38 gateway audio side channel descriptor.
+*/
+typedef struct
+{
+    fax_modems_state_t modems;
+    /*! \brief The current receive signal handler. Actual receiving hop between this
+               and a dummy receive routine. */
+    span_rx_handler_t *base_rx_handler;
+} t38_gateway_audio_state_t;
+
+typedef struct
+{
+    /*! \brief non-ECM and HDLC modem receive data buffer. */
+    uint8_t data[T38_RX_BUF_LEN];
+    /*! \brief Current pointer into the data buffer. */
+    int data_ptr;
+    /*! \brief The current octet being received as non-ECM data. */
+    unsigned int bit_stream;
+    /*! \brief The number of bits taken from the modem for the current scan row. This
+               is used during non-ECM transmission will fill bit removal to see that
+               T.38 packet transmissions do not stretch too far apart. */
+    int bits_absorbed;
+    /*! \brief The current bit number in the current non-ECM octet. */
+    int bit_no;
+    /*! \brief Progressively calculated CRC for HDLC messages received from a modem. */
+    uint16_t crc;
+    /*! \brief TRUE if non-ECM fill bits are to be stripped when sending image data. */
+    int fill_bit_removal;
+    /*! \brief The number of octets to send in each image packet (non-ECM or ECM) at the current
+               rate and the current specified packet interval. */
+    int octets_per_data_packet;
+
+    int in_bits;
+    int out_octets;
+} t38_gateway_to_t38_state_t;
+
+typedef struct
+{
+    /*! \brief HDLC message buffers. */
+    uint8_t buf[T38_TX_HDLC_BUFS][T38_MAX_HDLC_LEN];
+    /*! \brief HDLC message lengths. */
+    int len[T38_TX_HDLC_BUFS];
+    /*! \brief HDLC message status flags. */
+    int flags[T38_TX_HDLC_BUFS];
+    /*! \brief HDLC buffer contents. */
+    int contents[T38_TX_HDLC_BUFS];
+    /*! \brief HDLC buffer number for input. */
+    int in;
+    /*! \brief HDLC buffer number for output. */
+    int out;
+} t38_gateway_hdlc_state_t;
+
+/*!
+    T.38 gateway core descriptor.
+*/
+typedef struct
+{
+    /*! \brief A bit mask of the currently supported modem types. */
+    int supported_modems;
+    /*! \brief TRUE if ECM FAX mode is allowed through the gateway. */
+    int ecm_allowed;
+
+    /*! \brief TRUE if in image data modem is to use short training. This usually
+               follows image_data_mode, but in ECM mode T.30 defines recovery
+               conditions in which long training is used for image data. */
+    int short_train;
+    /*! \brief TRUE if in image data mode, as opposed to TCF mode. */
+    int image_data_mode;
+    /*! \brief The minimum permitted bits per FAX scan line row. */
+    int min_row_bits;
+
+    /*! \brief TRUE if we should count the next MCF as a page end, else FALSE */
+    int count_page_on_mcf;
+    /*! \brief The number of pages for which a confirm (MCF) message was returned. */
+    int pages_confirmed;
+
+    /*! \brief TRUE if we are in error correcting (ECM) mode */
+    int ecm_mode;
+    /*! \brief The current bit rate for the fast modem. */
+    int fast_bit_rate;
+    /*! \brief The current fast modem type. */
+    int fast_modem;
+    /*! \brief The type of fast receive modem currently active, which may be T38_NONE */
+    int fast_rx_active;
+
+    /*! \brief TRUE if between DCS and TCF, and we want the fast image modem to
+               start in the T.38 data at a predictable time from the end of the
+               V.21 signal. */
+    int tcf_mode_predictable_modem_start;
+
+    /*! \brief The number of samples until the next timeout event */
+    int samples_to_timeout;
+
+    /*! Buffer for HDLC and non-ECM data going to the T.38 channel */
+    t38_gateway_to_t38_state_t to_t38;
+    /*! Buffer for data going to an HDLC modem. */
+    t38_gateway_hdlc_state_t hdlc_to_modem;
+    /*! Buffer for data going to a non-ECM mode modem. */
+    t38_non_ecm_buffer_state_t non_ecm_to_modem;
+
+    /*! \brief A pointer to a callback routine to be called when frames are
+        exchanged. */
+    t38_gateway_real_time_frame_handler_t *real_time_frame_handler;
+    /*! \brief An opaque pointer supplied in real time frame callbacks. */
+    void *real_time_frame_user_data;
+} t38_gateway_core_state_t;
+
+/*!
+    T.38 gateway state.
+*/
+struct t38_gateway_state_s
+{
+    t38_gateway_t38_state_t t38x;
+    t38_gateway_audio_state_t audio;
+    t38_gateway_core_state_t core;
+
+    /*! \brief Error and flow logging control */
+    logging_state_t logging;
+};
+
+typedef struct
+{
+    /*! \brief The current bit rate for image transfer. */
+    int bit_rate;
+    /*! \brief TRUE if error correcting mode is used. */
+    int error_correcting_mode;
+    /*! \brief The number of pages transferred so far. */
+    int pages_transferred;
+} t38_stats_t;
+
+#if defined(__cplusplus)
+extern "C"
+{
+#endif
+
+/*! \brief Initialise a gateway mode T.38 context.
+    \param s The T.38 context.
+    \param tx_packet_handler A callback routine to encapsulate and transmit T.38 packets.
+    \param tx_packet_user_data An opaque pointer passed to the tx_packet_handler routine.
+    \return A pointer to the termination mode T.38 context, or NULL if there was a problem. */
+t38_gateway_state_t *t38_gateway_init(t38_gateway_state_t *s,
+                                      t38_tx_packet_handler_t *tx_packet_handler,
+                                      void *tx_packet_user_data);
+
+/*! Free a gateway mode T.38 context.
+    \brief Free a T.38 context.
+    \param s The T.38 context.
+    \return 0 for OK, else -1. */
+int t38_gateway_free(t38_gateway_state_t *s);
+
+/*! Process a block of received FAX audio samples.
+    \brief Process a block of received FAX audio samples.
+    \param s The T.38 context.
+    \param amp The audio sample buffer.
+    \param len The number of samples in the buffer.
+    \return The number of samples unprocessed. */
+int t38_gateway_rx(t38_gateway_state_t *s, int16_t amp[], int len);
+
+/*! Generate a block of FAX audio samples.
+    \brief Generate a block of FAX audio samples.
+    \param s The T.38 context.
+    \param amp The audio sample buffer.
+    \param max_len The number of samples to be generated.
+    \return The number of samples actually generated.
+*/
+int t38_gateway_tx(t38_gateway_state_t *s, int16_t amp[], int max_len);
+
+/*! Control whether error correcting mode (ECM) is allowed.
+    \brief Control whether error correcting mode (ECM) is allowed.
+    \param s The T.38 context.
+    \param ecm_allowed TRUE is ECM is to be allowed.
+*/
+void t38_gateway_set_ecm_capability(t38_gateway_state_t *s, int ecm_allowed);
+
+/*! Select whether silent audio will be sent when transmit is idle.
+    \brief Select whether silent audio will be sent when transmit is idle.
+    \param s The T.38 context.
+    \param transmit_on_idle TRUE if silent audio should be output when the FAX transmitter is
+           idle. FALSE to transmit zero length audio when the FAX transmitter is idle. The default
+           behaviour is FALSE.
+*/
+void t38_gateway_set_transmit_on_idle(t38_gateway_state_t *s, int transmit_on_idle);
+
+/*! Specify which modem types are supported by a T.30 context.
+    \brief Specify supported modems.
+    \param s The T.38 context.
+    \param supported_modems Bit field list of the supported modems.
+*/
+void t38_gateway_set_supported_modems(t38_gateway_state_t *s, int supported_modems);
+
+/*! Select whether NSC, NSF, and NSS should be suppressed. It selected, the contents of
+    these messages are forced to zero for all octets beyond the message type. This makes
+    them look like manufacturer specific messages, from a manufacturer which does not exist.
+    \brief Select whether NSC, NSF, and NSS should be suppressed.
+    \param s The T.38 context.
+    \param from_t38 A string of bytes to overwrite the header of any NSC, NSF, and NSS
+           frames passing through the gateway from T.38 the the modem.
+    \param from_t38_len The length of the overwrite string.
+    \param from_modem A string of bytes to overwrite the header of any NSC, NSF, and NSS
+           frames passing through the gateway from the modem to T.38.
+    \param from_modem_len The length of the overwrite string.
+*/
+void t38_gateway_set_nsx_suppression(t38_gateway_state_t *s,
+                                     const uint8_t *from_t38,
+                                     int from_t38_len,
+                                     const uint8_t *from_modem,
+                                     int from_modem_len);
+
+/*! Select whether talker echo protection tone will be sent for the image modems.
+    \brief Select whether TEP will be sent for the image modems.
+    \param s The T.38 context.
+    \param use_tep TRUE if TEP should be sent.
+*/
+void t38_gateway_set_tep_mode(t38_gateway_state_t *s, int use_tep);
+
+/*! Select whether non-ECM fill bits are to be removed during transmission.
+    \brief Select whether non-ECM fill bits are to be removed during transmission.
+    \param s The T.38 context.
+    \param remove TRUE if fill bits are to be removed.
+*/
+void t38_gateway_set_fill_bit_removal(t38_gateway_state_t *s, int remove);
+
+/*! Get the current transfer statistics for the current T.38 session.
+    \brief Get the current transfer statistics.
+    \param s The T.38 context.
+    \param t A pointer to a buffer for the statistics. */
+void t38_gateway_get_transfer_statistics(t38_gateway_state_t *s, t38_stats_t *t);
+
+/*! Set a callback function for T.30 frame exchange monitoring. This is called from the heart
+    of the signal processing, so don't take too long in the handler routine.
+    \brief Set a callback function for T.30 frame exchange monitoring.
+    \param s The T.30 context.
+    \param handler The callback function.
+    \param user_data An opaque pointer passed to the callback function. */
+void t38_gateway_set_real_time_frame_handler(t38_gateway_state_t *s,
+                                             t38_gateway_real_time_frame_handler_t *handler,
+                                             void *user_data);
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/spandsp/t38_non_ecm_buffer.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/spandsp/t38_non_ecm_buffer.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,136 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * t38_non_ecm_buffer.h - A rate adapting buffer for T.38 non-ECM image
+ *                        and TCF data
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2005, 2006, 2007, 2008 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: t38_non_ecm_buffer.h,v 1.1 2008/08/14 14:06:05 steveu Exp $
+ */
+
+/*! \file */
+
+#if !defined(_SPANDSP_T38_NON_ECM_BUFFER_H_)
+#define _SPANDSP_T38_NON_ECM_BUFFER_H_
+
+/*! \page t38_non_ecm_buffer_page T.38 rate adapting non-ECM image data buffer
+\section t38_non_ecm_buffer_page_sec_1 What does it do?
+
+The T.38 rate adapting non-ECM image data buffer is used to buffer TCF and non-ECM
+FAX image data being gatewayed from a T.38 linke to an analogue FAX modem link.
+
+\section t38_non_ecm_buffer_page_sec_2 How does it work?
+*/
+
+/*! The buffer length much be a power of two. The chosen length is big enough for
+    over 9s of data at the V.17 14,400bps rate. */    
+#define T38_NON_ECM_TX_BUF_LEN  16384
+
+/*! \brief A flow controlled non-ECM image data buffer, for buffering T.38 to analogue
+           modem data.
+*/
+typedef struct
+{
+    /*! \brief Minimum number of bits per row, used when fill bits are being deleted on the
+               link, and restored at the emitting gateway. */
+    int min_row_bits;
+
+    /*! \brief non-ECM modem transmit data buffer. */
+    uint8_t data[T38_NON_ECM_TX_BUF_LEN];
+    /*! \brief The current write point in the buffer. */
+    int in_ptr;
+    /*! \brief The current read point in the buffer. */
+    int out_ptr;
+    /*! \brief The location of the most recent EOL marker in the buffer. */
+    int latest_eol_ptr;
+    /*! \brief The number of bits to date in the current row, used when min_row_bits is
+               to be applied. */
+    int row_bits;
+
+    /*! \brief The bit stream entering the buffer, used to detect EOLs */
+    unsigned int bit_stream;
+    /*! \brief The non-ECM flow control fill octet (0xFF before the first data, and 0x00
+               once data has started). */
+    uint8_t flow_control_fill_octet;
+    /*! \brief TRUE if we are in the initial all ones part of non-ECM transmission. */
+    int at_initial_all_ones;
+    /*! \brief TRUE is the end of non-ECM data indication has been received. */
+    int data_finished;
+    /*! \brief The current octet being transmitted from the buffer. */
+    unsigned int octet;
+    /*! \brief The current bit number in the current non-ECM octet. */
+    int bit_no;
+    /*! \brief TRUE if in image data mode, as opposed to TCF mode. */
+    int image_data_mode;
+
+    /*! \brief The number of octets input to the buffer. */
+    int in_octets;
+    /*! \brief The number of octets output from the buffer. */
+    int out_octets;
+    /*! \brief The number of rows input to the buffer. */
+    int in_rows;
+    /*! \brief The number of rows output from the buffer. */
+    int out_rows;
+    /*! \brief The number of non-ECM fill octets generated for flow control
+               purposes. */
+    int flow_control_fill_octets;
+} t38_non_ecm_buffer_state_t;
+
+#if defined(__cplusplus)
+extern "C"
+{
+#endif
+
+/*! \brief Initialise a T.38 rate adapting non-ECM buffer context.
+    \param s The buffer context.
+    \param mode TRUE for image data mode, or FALSE for TCF mode.
+    \param bits The minimum number of bits per FAX image row.
+    \return A pointer to the buffer context, or NULL if there was a problem. */
+t38_non_ecm_buffer_state_t *t38_non_ecm_buffer_init(t38_non_ecm_buffer_state_t *s, int mode, int min_row_bits);
+
+/*! \brief Inject data to T.38 rate adapting non-ECM buffer context.
+    \param s The buffer context.
+    \param buf The data buffer to be injected.
+    \param len The length of the data to be injected. */
+void t38_non_ecm_buffer_inject(t38_non_ecm_buffer_state_t *s, const uint8_t *buf, int len);
+
+/*! \brief Inform a T.38 rate adapting non-ECM buffer context that the incoming data has finished,
+           and the contents of the buffer should be played out as quickly as possible.
+    \param s The buffer context. */
+void t38_non_ecm_buffer_push(t38_non_ecm_buffer_state_t *s);
+
+/*! \brief Report the status of a T.38 rate adapting non-ECM buffer context to the specified
+           logging context.
+    \param s The buffer context.
+    \param logging The logging context. */
+void t38_non_ecm_buffer_report_status(t38_non_ecm_buffer_state_t *s, logging_state_t *logging);
+
+/*! \brief Get the next bit of data from a T.38 rate adapting non-ECM buffer context.
+    \param user_data The buffer context, cast to a void pointer.
+    \return The next bit, or one of the values indicating a change of modem status. */
+int t38_non_ecm_buffer_get_bit(void *user_data);
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/spandsp/t38_terminal.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/spandsp/t38_terminal.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,187 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * t38_terminal.h - T.38 termination, less the packet exchange part
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2005 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: t38_terminal.h,v 1.37 2008/08/14 14:06:05 steveu Exp $
+ */
+
+/*! \file */
+
+#if !defined(_SPANDSP_T38_TERMINAL_H_)
+#define _SPANDSP_T38_TERMINAL_H_
+
+/*! \page t38_terminal_page T.38 real time FAX over IP termination
+\section t38_terminal_page_sec_1 What does it do?
+
+\section t38_terminal_page_sec_2 How does it work?
+*/
+
+/* Make sure the HDLC frame buffers are big enough for ECM frames. */
+#define T38_MAX_HDLC_LEN        260
+
+typedef struct
+{
+    /*! \brief Internet Aware FAX mode bit mask. */
+    int iaf;
+    /*! \brief Required time between T.38 transmissions, in ms. */
+    int ms_per_tx_chunk;
+    /*! \brief Bit fields controlling the way data is packed into chunked for transmission. */
+    int chunking_modes;
+
+    /*! \brief Core T.38 IFP support */
+    t38_core_state_t t38;
+
+    /*! \brief The current transmit step being timed */
+    int timed_step;
+
+    /*! \brief TRUE is there has been some T.38 data missed (i.e. lost packets) in the current
+               reception period. */
+    int rx_data_missing;
+
+    /*! \brief The number of octets to send in each image packet (non-ECM or ECM) at the current
+               rate and the current specified packet interval. */
+    int octets_per_data_packet;
+
+    struct
+    {
+        /*! \brief HDLC receive buffer */
+        uint8_t buf[T38_MAX_HDLC_LEN];
+        /*! \brief The length of the contents of the HDLC receive buffer */
+        int len;
+    } hdlc_rx;
+
+    struct
+    {
+        /*! \brief HDLC transmit buffer */
+        uint8_t buf[T38_MAX_HDLC_LEN];
+        /*! \brief The length of the contents of the HDLC transmit buffer */
+        int len;
+        /*! \brief Current pointer within the contents of the HDLC transmit buffer */
+        int ptr;
+        /*! \brief The number of extra bits in a fully stuffed version of the
+                   contents of the HDLC transmit buffer. This is needed to accurately
+                   estimate the playout time for this frame, through an analogue modem. */
+        int extra_bits;
+    } tx;
+
+    /*! \brief Counter for trailing non-ECM bytes, used to flush out the far end's modem. */
+    int non_ecm_trailer_bytes;
+
+    /*! \brief The next T.38 indicator queued for transmission. */
+    int next_tx_indicator;
+    /*! \brief The current T.38 data type being transmitted. */
+    int current_tx_data_type;
+
+    /*! \brief TRUE if a carrier is present. Otherwise FALSE. */
+    int rx_signal_present;
+
+    /*! \brief The current operating mode of the receiver. */
+    int current_rx_type;
+    /*! \brief The current operating mode of the transmitter. */
+    int current_tx_type;
+
+    /*! \brief Current transmission bit rate. */
+    int tx_bit_rate;
+    /*! \brief A "sample" count, used to time events. */
+    int32_t samples;
+    /*! \brief The value for samples at the next transmission point. */
+    int32_t next_tx_samples;
+    /*! \brief The current receive timeout. */
+    int32_t timeout_rx_samples;
+} t38_terminal_front_end_state_t;
+
+/*!
+    T.38 terminal state.
+*/
+typedef struct
+{
+    /*! \brief The T.30 back-end */
+    t30_state_t t30;
+
+    /*! \brief The T.38 front-end */
+    t38_terminal_front_end_state_t t38_fe;
+
+    /*! \brief Error and flow logging control */
+    logging_state_t logging;
+} t38_terminal_state_t;
+
+#if defined(__cplusplus)
+extern "C"
+{
+#endif
+
+int t38_terminal_send_timeout(t38_terminal_state_t *s, int samples);
+
+void t38_terminal_set_config(t38_terminal_state_t *s, int without_pacing);
+
+/*! Select whether the time for talker echo protection tone will be allowed for when sending.
+    \brief Select whether TEP time will be allowed for.
+    \param s The T.38 context.
+    \param use_tep TRUE if TEP should be allowed for.
+*/
+void t38_terminal_set_tep_mode(t38_terminal_state_t *s, int use_tep);
+
+/*! Select whether non-ECM fill bits are to be removed during transmission.
+    \brief Select whether non-ECM fill bits are to be removed during transmission.
+    \param s The T.38 context.
+    \param remove TRUE if fill bits are to be removed.
+*/
+void t38_terminal_set_fill_bit_removal(t38_terminal_state_t *s, int remove);
+
+/*! Get a pointer to the T.30 engine associated with a termination mode T.38 context.
+    \brief Get a pointer to the T.30 engine associated with a T.38 context.
+    \param s The T.38 context.
+    \return A pointer to the T.30 context, or NULL.
+*/
+t30_state_t *t38_terminal_get_t30_state(t38_terminal_state_t *s);
+
+/*! \brief Initialise a termination mode T.38 context.
+    \param s The T.38 context.
+    \param calling_party TRUE if the context is for a calling party. FALSE if the
+           context is for an answering party.
+    \param tx_packet_handler A callback routine to encapsulate and transmit T.38 packets.
+    \param tx_packet_user_data An opaque pointer passed to the tx_packet_handler routine.
+    \return A pointer to the termination mode T.38 context, or NULL if there was a problem. */
+t38_terminal_state_t *t38_terminal_init(t38_terminal_state_t *s,
+                                        int calling_party,
+                                        t38_tx_packet_handler_t *tx_packet_handler,
+                                        void *tx_packet_user_data);
+
+/*! Release a termination mode T.38 context.
+    \brief Release a T.38 context.
+    \param s The T.38 context.
+    \return 0 for OK, else -1. */
+int t38_terminal_release(t38_terminal_state_t *s);
+
+/*! Free a a termination mode T.38 context.
+    \brief Free a T.38 context.
+    \param s The T.38 context.
+    \return 0 for OK, else -1. */
+int t38_terminal_free(t38_terminal_state_t *s);
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/spandsp/t4.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/spandsp/t4.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,618 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * t4.h - definitions for T.4 fax processing
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2003 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: t4.h,v 1.52 2008/07/22 13:48:15 steveu Exp $
+ */
+
+/*! \file */
+
+#if !defined(_SPANDSP_T4_H_)
+#define _SPANDSP_T4_H_
+
+/*! \page t4_page T.4 image compression and decompression
+
+\section t4_page_sec_1 What does it do?
+The T.4 image compression and decompression routines implement the 1D and 2D
+encoding methods defined in ITU specification T.4. They also implement the pure
+2D encoding method defined in T.6. These are image compression algorithms used
+for FAX transmission.
+
+\section t4_page_sec_1 How does it work?
+*/
+
+typedef int (*t4_row_read_handler_t)(void *user_data, uint8_t buf[], size_t len);
+typedef int (*t4_row_write_handler_t)(void *user_data, const uint8_t buf[], size_t len);
+
+/*! Supported compression modes. */
+typedef enum
+{
+    T4_COMPRESSION_ITU_T4_1D = 1,
+    T4_COMPRESSION_ITU_T4_2D = 2,
+    T4_COMPRESSION_ITU_T6 = 3
+} t4_image_compression_t;
+
+/*! Supported X resolutions, in pixels per metre. */
+typedef enum
+{
+    T4_X_RESOLUTION_R4 = 4016,
+    T4_X_RESOLUTION_R8 = 8031,
+    T4_X_RESOLUTION_300 = 11811,
+    T4_X_RESOLUTION_R16 = 16063,
+    T4_X_RESOLUTION_600 = 23622,
+    T4_X_RESOLUTION_800 = 31496,
+    T4_X_RESOLUTION_1200 = 47244
+} t4_image_x_resolution_t;
+
+/*! Supported Y resolutions, in pixels per metre. */
+typedef enum
+{
+    T4_Y_RESOLUTION_STANDARD = 3850,
+    T4_Y_RESOLUTION_FINE = 7700,
+    T4_Y_RESOLUTION_300 = 11811,
+    T4_Y_RESOLUTION_SUPERFINE = 15400,  /* 400 is 15748 */
+    T4_Y_RESOLUTION_600 = 23622,
+    T4_Y_RESOLUTION_800 = 31496,
+    T4_Y_RESOLUTION_1200 = 47244
+} t4_image_y_resolution_t;
+
+/*!
+    Exact widths in PELs for the difference resolutions, and page widths.
+    Note:
+        The A4 widths also apply to North American letter and legal.
+        The R4 resolution widths are not supported in recent versions of T.30
+        Only images of exactly these widths are acceptable for FAX transmisson.
+
+    R4    864 pels/215mm for ISO A4, North American Letter and Legal
+    R4   1024 pels/255mm for ISO B4
+    R4   1216 pels/303mm for ISO A3
+    R8   1728 pels/215mm for ISO A4, North American Letter and Legal
+    R8   2048 pels/255mm for ISO B4
+    R8   2432 pels/303mm for ISO A3
+    R16  3456 pels/215mm for ISO A4, North American Letter and Legal
+    R16  4096 pels/255mm for ISO B4
+    R16  4864 pels/303mm for ISO A3
+*/
+typedef enum
+{
+    T4_WIDTH_R4_A4 = 864,
+    T4_WIDTH_R4_B4 = 1024,
+    T4_WIDTH_R4_A3 = 1216,
+    T4_WIDTH_R8_A4 = 1728,
+    T4_WIDTH_R8_B4 = 2048,
+    T4_WIDTH_R8_A3 = 2432,
+    T4_WIDTH_300_A4 = 2592,
+    T4_WIDTH_300_B4 = 3072,
+    T4_WIDTH_300_A3 = 3648,
+    T4_WIDTH_R16_A4 = 3456,
+    T4_WIDTH_R16_B4 = 4096,
+    T4_WIDTH_R16_A3 = 4864,
+    T4_WIDTH_600_A4 = 5184,
+    T4_WIDTH_600_B4 = 6144,
+    T4_WIDTH_600_A3 = 7296,
+    T4_WIDTH_1200_A4 = 10368,
+    T4_WIDTH_1200_B4 = 12288,
+    T4_WIDTH_1200_A3 = 14592
+} t4_image_width_t;
+
+/*!
+    Length of the various supported paper sizes, in pixels at the various Y resolutions.
+    Paper sizes are
+        A4 (215mm x 297mm)
+        B4 (255mm x 364mm)
+        A3 (303mm x 418.56mm)
+        North American Letter (215.9mm x 279.4mm)
+        North American Legal (215.9mm x 355.6mm)
+        Unlimited
+
+    T.4 does not accurately define the maximum number of scan lines in a page. A wide
+    variety of maximum row counts are used in the real world. It is important not to
+    set our sending limit too high, or a receiving machine might split pages. It is
+    important not to set it too low, or we might clip pages.
+
+    Values seen for standard resolution A4 pages include 1037, 1045, 1109, 1126 and 1143.
+    1109 seems the most-popular.  At fine res 2150, 2196, 2200, 2237, 2252-2262, 2264,
+    2286, and 2394 are used. 2255 seems the most popular. We try to use balanced choices
+    here.
+*/
+typedef enum
+{
+    /* A4 is 297mm long */
+    T4_LENGTH_STANDARD_A4 = 1143,
+    T4_LENGTH_FINE_A4 = 2286,
+    T4_LENGTH_300_A4 = 4665,
+    T4_LENGTH_SUPERFINE_A4 = 4573,
+    T4_LENGTH_600_A4 = 6998,
+    T4_LENGTH_800_A4 = 9330,
+    T4_LENGTH_1200_A4 = 13996,
+    /* B4 is 364mm long */
+    T4_LENGTH_STANDARD_B4 = 1401,
+    T4_LENGTH_FINE_B4 = 2802,
+    T4_LENGTH_300_B4 = 0,
+    T4_LENGTH_SUPERFINE_B4 = 5605,
+    T4_LENGTH_600_B4 = 0,
+    T4_LENGTH_800_B4 = 0,
+    T4_LENGTH_1200_B4 = 0,
+    /* North American letter is 279.4mm long */
+    T4_LENGTH_STANDARD_US_LETTER = 1075,
+    T4_LENGTH_FINE_US_LETTER = 2151,
+    T4_LENGTH_300_US_LETTER = 0,
+    T4_LENGTH_SUPERFINE_US_LETTER = 4302,
+    T4_LENGTH_600_US_LETTER = 0,
+    T4_LENGTH_800_US_LETTER = 0,
+    T4_LENGTH_1200_US_LETTER = 0,
+    /* North American legal is 355.6mm long */
+    T4_LENGTH_STANDARD_US_LEGAL = 1369,
+    T4_LENGTH_FINE_US_LEGAL = 2738,
+    T4_LENGTH_300_US_LEGAL = 0,
+    T4_LENGTH_SUPERFINE_US_LEGAL = 5476,
+    T4_LENGTH_600_US_LEGAL = 0,
+    T4_LENGTH_800_US_LEGAL = 0,
+    T4_LENGTH_1200_US_LEGAL = 0
+} t4_image_length_t;
+
+/*!
+    T.4 FAX compression/decompression descriptor. This defines the working state
+    for a single instance of a T.4 FAX compression or decompression channel.
+*/
+typedef struct
+{
+    /*! \brief The same structure is used for T.4 transmit and receive. This variable
+               records which mode is in progress. */
+    int rx;
+    /* "Background" information about the FAX, which can be stored in a TIFF file. */
+    /*! \brief The vendor of the machine which produced the TIFF file. */ 
+    const char *vendor;
+    /*! \brief The model of machine which produced the TIFF file. */ 
+    const char *model;
+    /*! \brief The local ident string. */ 
+    const char *local_ident;
+    /*! \brief The remote end's ident string. */ 
+    const char *far_ident;
+    /*! \brief The FAX sub-address. */ 
+    const char *sub_address;
+    /*! \brief The FAX DCS information, as an ASCII string. */ 
+    const char *dcs;
+    /*! \brief The text which will be used in FAX page header. No text results
+               in no header line. */
+    const char *header_info;
+
+    /*! \brief The type of compression used between the FAX machines. */
+    int line_encoding;
+    /*! \brief The minimum number of encoded bits per row. This is a timing thing
+               for hardware FAX machines. */
+    int min_bits_per_row;
+    
+    /*! \brief The compression type for output to the TIFF file. */
+    int output_compression;
+    /*! \brief The TIFF G3 FAX options. */
+    int output_t4_options;
+
+    /*! \brief Callback function to read a row of pixels from the image source. */
+    t4_row_read_handler_t row_read_handler;
+    /*! \brief Opaque pointer passed to row_read_handler. */
+    void *row_read_user_data;
+    /*! \brief Callback function to write a row of pixels to the image destination. */
+    t4_row_write_handler_t row_write_handler;
+    /*! \brief Opaque pointer passed to row_write_handler. */
+    void *row_write_user_data;
+
+    /*! \brief The time at which handling of the current page began. */
+    time_t page_start_time;
+
+    /*! \brief The current number of bytes per row of uncompressed image data. */
+    int bytes_per_row;
+    /*! \brief The size of the image in the image buffer, in bytes. */
+    int image_size;
+    /*! \brief The size of the compressed image on the line side, in bits. */
+    int line_image_size;
+    /*! \brief The current size of the image buffer. */
+    int image_buffer_size;
+    /*! \brief A point to the image buffer. */
+    uint8_t *image_buffer;
+
+    /*! \brief The libtiff context for the current TIFF file */
+    TIFF *tiff_file;
+    /*! \brief The current file name. */
+    const char *file;
+    /*! \brief The first page to transfer. -1 to start at the beginning of the file. */
+    int start_page;
+    /*! \brief The last page to transfer. -1 to continue to the end of the file. */
+    int stop_page;
+
+    /*! \brief The number of pages transferred to date. */
+    int pages_transferred;
+    /*! \brief The number of pages in the current TIFF file. */
+    int pages_in_file;
+    /*! \brief Column-to-column (X) resolution in pixels per metre. */
+    int x_resolution;
+    /*! \brief Row-to-row (Y) resolution in pixels per metre. */
+    int y_resolution;
+    /*! \brief Width of the current page, in pixels. */
+    int image_width;
+    /*! \brief Length of the current page, in pixels. */
+    int image_length;
+    /*! \brief Current pixel row number. */
+    int row;
+    /*! \brief The current number of consecutive bad rows. */
+    int curr_bad_row_run;
+    /*! \brief The longest run of consecutive bad rows seen in the current page. */
+    int longest_bad_row_run;
+    /*! \brief The total number of bad rows in the current page. */
+    int bad_rows;
+
+    /*! \brief Incoming bit buffer for decompression. */
+    uint32_t rx_bitstream;
+    /*! \brief The number of bits currently in rx_bitstream. */
+    int rx_bits;
+    /*! \brief The number of bits to be skipped before trying to match the next code word. */
+    int rx_skip_bits;
+
+    /*! \brief This variable is set if we are treating the current row as a 2D encoded
+               one. */
+    int row_is_2d;
+    /*! \brief TRUE if the current run is black */
+    int its_black;
+    /*! \brief The current length of the current row. */
+    int row_len;
+    /*! \brief This variable is used to count the consecutive EOLS we have seen. If it
+               reaches six, this is the end of the image. It is initially set to -1 for
+               1D and 2D decoding, as an indicator that we must wait for the first EOL,
+               before decodin any image data. */
+    int consecutive_eols;
+
+    /*! \brief Black and white run-lengths for the current row. */
+    uint32_t *cur_runs;
+    /*! \brief Black and white run-lengths for the reference row. */
+    uint32_t *ref_runs;
+    /*! \brief The number of runs currently in the reference row. */
+    int ref_steps;
+    /*! \brief The current step into the reference row run-lengths buffer. */
+    int b_cursor;
+    /*! \brief The current step into the current row run-lengths buffer. */
+    int a_cursor;
+
+    /*! \brief The reference or starting changing element on the coding line. At the
+               start of the coding line, a0 is set on an imaginary white changing element
+               situated just before the first element on the line. During the coding of
+               the coding line, the position of a0 is defined by the previous coding mode.
+               (See 4.2.1.3.2.). */
+    int a0;
+    /*! \brief The first changing element on the reference line to the right of a0 and of
+               opposite colour to a0. */
+    int b1;
+    /*! \brief The length of the in-progress run of black or white. */
+    int run_length;
+    /*! \brief 2D horizontal mode control. */
+    int black_white;
+
+    /*! \brief Encoded data bits buffer. */
+    uint32_t tx_bitstream;
+    /*! \brief The number of bits currently in tx_bitstream. */
+    int tx_bits;
+
+    /*! \brief A pointer into the image buffer indicating where the last row begins */
+    int last_row_starts_at;
+    /*! \brief A pointer into the image buffer indicating where the current row begins */
+    int row_starts_at;
+    
+    /*! \brief Pointer to the buffer for the current pixel row. */
+    uint8_t *row_buf;
+    
+    /*! \brief Pointer to the byte containing the next image bit to transmit. */
+    int bit_pos;
+    /*! \brief Pointer to the bit within the byte containing the next image bit to transmit. */
+    int bit_ptr;
+
+    /*! \brief The current maximum contiguous rows that may be 2D encoded. */
+    int max_rows_to_next_1d_row;
+    /*! \brief Number of rows left that can be 2D encoded, before a 1D encoded row
+               must be used. */
+    int rows_to_next_1d_row;
+    /*! \brief The current number of bits in the current encoded row. */
+    int row_bits;
+    /*! \brief The minimum bits in any row of the current page. For monitoring only. */
+    int min_row_bits;
+    /*! \brief The maximum bits in any row of the current page. For monitoring only. */
+    int max_row_bits;
+
+    /*! \brief Error and flow logging control */
+    logging_state_t logging;
+} t4_state_t;
+
+/*!
+    T.4 FAX compression/decompression statistics.
+*/
+typedef struct
+{
+    /*! \brief The number of pages transferred so far. */
+    int pages_transferred;
+    /*! \brief The number of pages in the file (<0 if unknown). */
+    int pages_in_file;
+    /*! \brief The number of horizontal pixels in the most recent page. */
+    int width;
+    /*! \brief The number of vertical pixels in the most recent page. */
+    int length;
+    /*! \brief The number of bad pixel rows in the most recent page. */
+    int bad_rows;
+    /*! \brief The largest number of bad pixel rows in a block in the most recent page. */
+    int longest_bad_row_run;
+    /*! \brief The horizontal resolution of the page in pixels per metre */
+    int x_resolution;
+    /*! \brief The vertical resolution of the page in pixels per metre */
+    int y_resolution;
+    /*! \brief The type of compression used between the FAX machines */
+    int encoding;
+    /*! \brief The size of the image on the line, in bytes */
+    int line_image_size;
+} t4_stats_t;
+    
+#if defined(__cplusplus)
+extern "C" {
+#endif
+
+/*! \brief Prepare for reception of a document.
+    \param s The T.4 context.
+    \param file The name of the file to be received.
+    \param output_encoding The output encoding.
+    \return A pointer to the context, or NULL if there was a problem. */
+t4_state_t *t4_rx_init(t4_state_t *s, const char *file, int output_encoding);
+
+/*! \brief Prepare to receive the next page of the current document.
+    \param s The T.4 context.
+    \return zero for success, -1 for failure. */
+int t4_rx_start_page(t4_state_t *s);
+
+/*! \brief Put a bit of the current document page.
+    \param s The T.4 context.
+    \param bit The data bit.
+    \return TRUE when the bit ends the document page, otherwise FALSE. */
+int t4_rx_put_bit(t4_state_t *s, int bit);
+
+/*! \brief Put a byte of the current document page.
+    \param s The T.4 context.
+    \param byte The data byte.
+    \return TRUE when the byte ends the document page, otherwise FALSE. */
+int t4_rx_put_byte(t4_state_t *s, uint8_t byte);
+
+/*! \brief Put a byte of the current document page.
+    \param s The T.4 context.
+    \param buf The buffer containing the chunk.
+    \param len The length of the chunk.
+    \return TRUE when the byte ends the document page, otherwise FALSE. */
+int t4_rx_put_chunk(t4_state_t *s, const uint8_t buf[], int len);
+
+/*! \brief Complete the reception of a page.
+    \param s The T.4 receive context.
+    \return 0 for success, otherwise -1. */
+int t4_rx_end_page(t4_state_t *s);
+
+/*! \brief End reception of a document. Tidy up, close the file and
+           free the context. This should be used to end T.4 reception
+           started with t4_rx_init.
+    \param s The T.4 receive context.
+    \return 0 for success, otherwise -1. */
+int t4_rx_delete(t4_state_t *s);
+
+/*! \brief End reception of a document. Tidy up and close the file.
+           This should be used to end T.4 reception started with
+           t4_rx_init.
+    \param s The T.4 context.
+    \return 0 for success, otherwise -1. */
+int t4_rx_end(t4_state_t *s);
+
+int t4_rx_set_row_write_handler(t4_state_t *s, t4_row_write_handler_t handler, void *user_data);
+
+/*! \brief Set the encoding for the received data.
+    \param s The T.4 context.
+    \param encoding The encoding. */
+void t4_rx_set_rx_encoding(t4_state_t *s, int encoding);
+
+/*! \brief Set the expected width of the received image, in pixel columns.
+    \param s The T.4 context.
+    \param width The number of pixels across the image. */
+void t4_rx_set_image_width(t4_state_t *s, int width);
+
+/*! \brief Set the row-to-row (y) resolution to expect for a received image.
+    \param s The T.4 context.
+    \param resolution The resolution, in pixels per metre. */
+void t4_rx_set_y_resolution(t4_state_t *s, int resolution);
+
+/*! \brief Set the column-to-column (x) resolution to expect for a received image.
+    \param s The T.4 context.
+    \param resolution The resolution, in pixels per metre. */
+void t4_rx_set_x_resolution(t4_state_t *s, int resolution);
+
+/*! \brief Set the DCS information of the fax, for inclusion in the file.
+    \param s The T.4 context.
+    \param dcs The DCS information, formatted as an ASCII string. */
+void t4_rx_set_dcs(t4_state_t *s, const char *dcs);
+
+/*! \brief Set the sub-address of the fax, for inclusion in the file.
+    \param s The T.4 context.
+    \param sub_address The sub-address string. */
+void t4_rx_set_sub_address(t4_state_t *s, const char *sub_address);
+
+/*! \brief Set the identity of the remote machine, for inclusion in the file.
+    \param s The T.4 context.
+    \param ident The identity string. */
+void t4_rx_set_far_ident(t4_state_t *s, const char *ident);
+
+/*! \brief Set the vendor of the remote machine, for inclusion in the file.
+    \param s The T.4 context.
+    \param vendor The vendor string, or NULL. */
+void t4_rx_set_vendor(t4_state_t *s, const char *vendor);
+
+/*! \brief Set the model of the remote machine, for inclusion in the file.
+    \param s The T.4 context.
+    \param model The model string, or NULL. */
+void t4_rx_set_model(t4_state_t *s, const char *model);
+
+/*! \brief Prepare for transmission of a document.
+    \param s The T.4 context.
+    \param file The name of the file to be sent.
+    \param start_page The first page to send. -1 for no restriction.
+    \param stop_page The last page to send. -1 for no restriction.
+    \return A pointer to the context, or NULL if there was a problem. */
+t4_state_t *t4_tx_init(t4_state_t *s, const char *file, int start_page, int stop_page);
+
+/*! \brief Prepare to send the next page of the current document.
+    \param s The T.4 context.
+    \return zero for success, -1 for failure. */
+int t4_tx_start_page(t4_state_t *s);
+
+/*! \brief Prepare the current page for a resend.
+    \param s The T.4 context.
+    \return zero for success, -1 for failure. */
+int t4_tx_restart_page(t4_state_t *s);
+
+/*! \brief Check for the existance of the next page. This information can
+    be needed before it is determined that the current page is finished with.
+    \param s The T.4 context.
+    \return zero for next page found, -1 for failure. */
+int t4_tx_more_pages(t4_state_t *s);
+
+/*! \brief Complete the sending of a page.
+    \param s The T.4 context.
+    \return zero for success, -1 for failure. */
+int t4_tx_end_page(t4_state_t *s);
+
+/*! \brief Get the next bit of the current document page. The document will
+           be padded for the current minimum scan line time. If the
+           file does not contain an RTC (return to control) code at
+           the end of the page, one will be added where appropriate.
+    \param s The T.4 context.
+    \return The next bit (i.e. 0 or 1). For the last bit of data, bit 1 is
+            set (i.e. the returned value is 2 or 3). */
+int t4_tx_get_bit(t4_state_t *s);
+
+/*! \brief Get the next byte of the current document page. The document will
+           be padded for the current minimum scan line time. If the
+           file does not contain an RTC (return to control) code at
+           the end of the page, one will be added where appropriate.
+    \param s The T.4 context.
+    \return The next byte. For the last byte of data, bit 8 is
+            set. In this case, one or more bits of the byte may be padded with
+            zeros, to complete the byte. */
+int t4_tx_get_byte(t4_state_t *s);
+
+/*! \brief Get the next chunk of the current document page. The document will
+           be padded for the current minimum scan line time. If the
+           file does not contain an RTC (return to control) code at
+           the end of the page, one will be added where appropriate.
+    \param s The T.4 context.
+    \param buf The buffer into which the chunk is to written.
+    \param max_len The maximum length of the chunk.
+    \return The actual length of the chunk. If this is less than max_len it 
+            indicates that the end of the document has been reached. */
+int t4_tx_get_chunk(t4_state_t *s, uint8_t buf[], int max_len);
+
+/*! \brief Return the next bit of the current document page, without actually
+           moving forward in the buffer. The document will be padded for the
+           current minimum scan line time. If the file does not contain an
+           RTC (return to control) code at the end of the page, one will be
+           added.
+    \param s The T.4 context.
+    \return The next bit (i.e. 0 or 1). For the last bit of data, bit 1 is
+            set (i.e. the returned value is 2 or 3). */
+int t4_tx_check_bit(t4_state_t *s);
+
+/*! \brief End the transmission of a document. Tidy up, close the file and
+           free the context. This should be used to end T.4 transmission
+           started with t4_tx_init.
+    \param s The T.4 context.
+    \return 0 for success, otherwise -1. */
+int t4_tx_delete(t4_state_t *s);
+
+/*! \brief End the transmission of a document. Tidy up and close the file.
+           This should be used to end T.4 transmission started with t4_tx_init.
+    \param s The T.4 context.
+    \return 0 for success, otherwise -1. */
+int t4_tx_end(t4_state_t *s);
+
+/*! \brief Set the encoding for the encoded data.
+    \param s The T.4 context.
+    \param encoding The encoding. */
+void t4_tx_set_tx_encoding(t4_state_t *s, int encoding);
+
+/*! \brief Set the minimum number of encoded bits per row. This allows the
+           makes the encoding process to be set to comply with the minimum row
+           time specified by a remote receiving machine.
+    \param s The T.4 context.
+    \param bits The minimum number of bits per row. */
+void t4_tx_set_min_row_bits(t4_state_t *s, int bits);
+
+/*! \brief Set the identity of the local machine, for inclusion in page headers.
+    \param s The T.4 context.
+    \param ident The identity string. */
+void t4_tx_set_local_ident(t4_state_t *s, const char *ident);
+
+/*! Set the info field, included in the header line included in each page of an encoded
+    FAX. This is a string of up to 50 characters. Other information (date, local ident, etc.)
+    are automatically included in the header. If the header info is set to NULL or a zero
+    length string, no header lines will be added to the encoded FAX.
+    \brief Set the header info.
+    \param s The T.4 context.
+    \param info A string, of up to 50 bytes, which will form the info field. */
+void t4_tx_set_header_info(t4_state_t *s, const char *info);
+
+int t4_tx_set_row_read_handler(t4_state_t *s, t4_row_read_handler_t handler, void *user_data);
+
+/*! \brief Get the row-to-row (y) resolution of the current page.
+    \param s The T.4 context.
+    \return The resolution, in pixels per metre. */
+int t4_tx_get_y_resolution(t4_state_t *s);
+
+/*! \brief Get the column-to-column (x) resolution of the current page.
+    \param s The T.4 context.
+    \return The resolution, in pixels per metre. */
+int t4_tx_get_x_resolution(t4_state_t *s);
+
+/*! \brief Get the width of the current page, in pixel columns.
+    \param s The T.4 context.
+    \return The number of columns. */
+int t4_tx_get_image_width(t4_state_t *s);
+
+/*! \brief Get the number of pages in the file.
+    \param s The T.4 context.
+    \return The number of pages, or -1 if there is an error. */
+int t4_tx_get_pages_in_file(t4_state_t *s);
+
+/*! Get the current image transfer statistics. 
+    \brief Get the current transfer statistics.
+    \param s The T.4 context.
+    \param t A pointer to a statistics structure. */
+void t4_get_transfer_statistics(t4_state_t *s, t4_stats_t *t);
+
+/*! Get the short text name of an encoding format. 
+    \brief Get the short text name of an encoding format.
+    \param encoding The encoding type.
+    \return A pointer to the string. */
+const char *t4_encoding_to_str(int encoding);
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/spandsp/telephony.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/spandsp/telephony.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,67 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * telephony.h - some very basic telephony definitions
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2003 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: telephony.h,v 1.12 2008/04/17 14:27:01 steveu Exp $
+ */
+
+#if !defined(_SPANDSP_TELEPHONY_H_)
+#define _SPANDSP_TELEPHONY_H_
+
+#define SAMPLE_RATE             8000
+
+/* This is based on A-law, but u-law is only 0.03dB different */
+#define DBM0_MAX_POWER          (3.14f + 3.02f)
+#define DBM0_MAX_SINE_POWER     (3.14f)
+/* This is based on the ITU definition of dbOv in G.100.1 */
+#define DBOV_MAX_POWER          (0.0f)
+#define DBOV_MAX_SINE_POWER     (-3.02f)
+
+/*! \brief A handler for pure receive. The buffer cannot be altered. */
+typedef int (span_rx_handler_t)(void *s, const int16_t amp[], int len);
+
+/*! \brief A handler for receive, where the buffer can be altered. */
+typedef int (span_mod_handler_t)(void *s, int16_t amp[], int len);
+
+/*! \brief A handler for transmit, where the buffer will be filled. */
+typedef int (span_tx_handler_t)(void *s, int16_t amp[], int max_len);
+
+#define ms_to_samples(t)    (((t)*SAMPLE_RATE)/1000)
+
+#if !defined(FALSE)
+#define FALSE 0
+#endif
+#if !defined(TRUE)
+#define TRUE (!FALSE)
+#endif
+
+#if defined(__cplusplus)
+/* C++ doesn't seem to have sane rounding functions/macros yet */
+#ifndef _MSC_VER
+#define lrint(x) ((long int) (x))
+#define lrintf(x) ((long int) (x))
+#endif
+#endif
+
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/spandsp/time_scale.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/spandsp/time_scale.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,130 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * time_scale.h - Time scaling for linear speech data
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2004 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: time_scale.h,v 1.16 2008/07/28 15:14:30 steveu Exp $
+ */
+
+#if !defined(_SPANDSP_TIME_SCALE_H_)
+#define _SPANDSP_TIME_SCALE_H_
+
+/*! \page time_scale_page Time scaling speech
+\section time_scale_page_sec_1 What does it do?
+The time scaling module allows speech files to be played back at a
+different speed from the speed at which they were recorded. If this
+were done by simply speeding up or slowing down replay, the pitch of
+the voice would change, and sound very odd. This module keeps the pitch
+of the voice at its original level.
+
+The speed of the voice may be altered over a wide range. However, the practical
+useful rates are between about half normal speed and twice normal speed.
+
+\section time_scale_page_sec_2 How does it work?
+The time scaling module is based on the Pointer Interval Controlled
+OverLap and Add (PICOLA) method, developed by Morita Naotaka.
+Mikio Ikeda has an excellent web page on this subject at
+http://keizai.yokkaichi-u.ac.jp/~ikeda/research/picola.html
+There is also working code there. This implementation uses
+exactly the same algorithms, but the code is a complete rewrite.
+Mikio's code batch processes files. This version works incrementally
+on streams, and allows multiple streams to be processed concurrently.
+
+\section time_scale_page_sec_3 How do I used it?
+The output buffer must be big enough to hold the maximum number of samples which
+could result from the data in the input buffer, which is:
+
+    input_len*playout_rate + sample_rate/TIME_SCALE_MIN_PITCH + 1
+*/
+
+#define TIME_SCALE_MAX_SAMPLE_RATE  48000
+#define TIME_SCALE_MIN_PITCH        60
+#define TIME_SCALE_MAX_PITCH        250
+#define TIME_SCALE_BUF_LEN          (2*TIME_SCALE_MAX_SAMPLE_RATE/TIME_SCALE_MIN_PITCH)
+
+/*! Audio time scaling descriptor. */
+typedef struct
+{
+    int sample_rate;
+    int min_pitch;
+    int max_pitch;
+    int buf_len;
+    float playout_rate;
+    double rcomp;
+    double rate_nudge;
+    int fill;
+    int lcp;
+    int16_t buf[TIME_SCALE_BUF_LEN];
+} time_scale_state_t;
+
+#if defined(__cplusplus)
+extern "C"
+{
+#endif
+
+/*! Initialise a time scale context. This must be called before the first
+    use of the context, to initialise its contents.
+    \brief Initialise a time scale context.
+    \param s The time scale context.
+    \param sample_rate The sample rate of the signal.
+    \param playout_rate The ratio between the output speed and the input speed.
+    \return A pointer to the context, or NULL if there was a problem. */
+time_scale_state_t *time_scale_init(time_scale_state_t *s, int sample_rate, float playout_rate);
+
+/*! \brief Free a time scale context.
+    \param s The time scale context.
+    \return 0 for OK, else -1. */
+int time_scale_free(time_scale_state_t *s);
+
+/*! Change the time scale rate.
+    \brief Change the time scale rate.
+    \param s The time scale context.
+    \param playout_rate The ratio between the output speed and the input speed.
+    \return 0 if changed OK, else -1. */
+int time_scale_rate(time_scale_state_t *s, float playout_rate);
+
+/*! Find the maximum possible samples which could result from scaling the specified
+    number of input samples, at the current playback rate.
+    \brief Find the maximum possible output samples.
+    \param s The time scale context.
+    \param input_len The number of input samples.
+    \return The maximum possible output samples. */
+int time_scale_max_output_len(time_scale_state_t *s, int input_len);
+
+/*! Time scale a chunk of audio samples.
+    \brief Time scale a chunk of audio samples.
+    \param s The time scale context.
+    \param out The output audio sample buffer. This must be large enough to accept
+           the longest possible result from processing the input data. See the
+           algorithm documentation for how the longest possible result may be calculated.
+    \param in The input audio sample buffer.
+    \param len The number of input samples.
+    \return The number of output samples.
+*/
+int time_scale(time_scale_state_t *s, int16_t out[], int16_t in[], int len);
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/spandsp/timing.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/spandsp/timing.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,83 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * timing.h - Provide access to the Pentium/Athlon TSC timer register
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2001 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: timing.h,v 1.13 2008/04/17 14:27:01 steveu Exp $
+ */
+
+#if !defined(_SPANDSP_TIMING_H_)
+#define _SPANDSP_TIMING_H_
+
+#if defined(__cplusplus)
+extern "C"
+{
+#endif
+
+#if defined(__MSVC__)
+__declspec(naked) unsigned __int64 __cdecl rdtscll(void)
+{
+   __asm
+   {
+      rdtsc
+      ret       ; return value at EDX:EAX
+   }
+}
+/*- End of function --------------------------------------------------------*/
+#elif defined(__GNUC__)
+#if defined(__i386__)
+static __inline__ uint64_t rdtscll(void)
+{
+    uint64_t now;
+
+    __asm__ __volatile__(" rdtsc\n" : "=A" (now));
+    return now;
+}
+/*- End of function --------------------------------------------------------*/
+#elif defined(__x86_64__)
+static __inline__ uint64_t rdtscll(void)
+{
+    unsigned int a;
+    unsigned int d;
+
+    /* For x86_64 we need to merge the result in 2 32 bit registers
+       into one clean 64 bit result. */
+    __asm__ __volatile__(" rdtsc\n" : "=a" (a), "=d" (d));
+    return ((uint64_t) a) | (((uint64_t) d) << 32);
+}
+/*- End of function --------------------------------------------------------*/
+#else
+static __inline__ uint64_t rdtscll(void)
+{
+    /* This architecture doesn't have a suitable timer */
+    return 0llu;
+}
+/*- End of function --------------------------------------------------------*/
+#endif
+#endif
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/spandsp/tone_detect.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/spandsp/tone_detect.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,234 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * tone_detect.h - General telephony tone detection.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2001, 2005 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: tone_detect.h,v 1.41 2008/06/13 14:46:52 steveu Exp $
+ */
+
+#if !defined(_SPANDSP_TONE_DETECT_H_)
+#define _SPANDSP_TONE_DETECT_H_
+
+/*!
+    Goertzel filter descriptor.
+*/
+typedef struct
+{
+#if defined(SPANDSP_USE_FIXED_POINT)
+    int16_t fac;
+#else
+    float fac;
+#endif
+    int samples;
+} goertzel_descriptor_t;
+
+/*!
+    Goertzel filter state descriptor.
+*/
+typedef struct
+{
+#if defined(SPANDSP_USE_FIXED_POINT)
+    int16_t v2;
+    int16_t v3;
+    int16_t fac;
+#else
+    float v2;
+    float v3;
+    float fac;
+#endif
+    int samples;
+    int current_sample;
+} goertzel_state_t;
+
+#if defined(__cplusplus)
+extern "C"
+{
+#endif
+
+/*! \brief Create a descriptor for use with either a Goertzel transform */
+void make_goertzel_descriptor(goertzel_descriptor_t *t,
+                              float freq,
+                              int samples);
+
+/*! \brief Initialise the state of a Goertzel transform.
+    \param s The Goertzel context. If NULL, a context is allocated with malloc.
+    \param t The Goertzel descriptor.
+    \return A pointer to the Goertzel state. */
+goertzel_state_t *goertzel_init(goertzel_state_t *s,
+                                goertzel_descriptor_t *t);
+
+/*! \brief Reset the state of a Goertzel transform.
+    \param s The Goertzel context. */
+void goertzel_reset(goertzel_state_t *s);
+
+/*! \brief Update the state of a Goertzel transform.
+    \param s The Goertzel context.
+    \param amp The samples to be transformed.
+    \param samples The number of samples.
+    \return The number of samples unprocessed */
+int goertzel_update(goertzel_state_t *s,
+                    const int16_t amp[],
+                    int samples);
+
+/*! \brief Evaluate the final result of a Goertzel transform.
+    \param s The Goertzel context.
+    \return The result of the transform. The expected result for a pure sine wave
+            signal of level x dBm0, at the very centre of the bin is:
+    [Floating point] ((samples_per_goertzel_block*32768.0/1.4142)*10^((x - DBM0_MAX_SINE_POWER)/20.0))^2
+    [Fixed point] ((samples_per_goertzel_block*256.0/1.4142)*10^((x - DBM0_MAX_SINE_POWER)/20.0))^2 */
+#if defined(SPANDSP_USE_FIXED_POINT)
+int32_t goertzel_result(goertzel_state_t *s);
+#else
+float goertzel_result(goertzel_state_t *s);
+#endif
+
+/*! \brief Update the state of a Goertzel transform.
+    \param s The Goertzel context.
+    \param amp The sample to be transformed. */
+static __inline__ void goertzel_sample(goertzel_state_t *s, int16_t amp)
+{
+#if defined(SPANDSP_USE_FIXED_POINT)
+    int16_t x;
+    int16_t v1;
+#else
+    float v1;
+#endif
+
+    v1 = s->v2;
+    s->v2 = s->v3;
+#if defined(SPANDSP_USE_FIXED_POINT)
+    x = (((int32_t) s->fac*s->v2) >> 14);
+    /* Scale down the input signal to avoid overflows. 9 bits is enough to
+       monitor the signals of interest with adequate dynamic range and
+       resolution. In telephony we generally only start with 13 or 14 bits,
+       anyway. */
+    s->v3 = x - v1 + (amp >> 7);
+#else
+    s->v3 = s->fac*s->v2 - v1 + amp;
+#endif
+    s->current_sample++;
+}
+/*- End of function --------------------------------------------------------*/
+
+/* Scale down the input signal to avoid overflows. 9 bits is enough to
+   monitor the signals of interest with adequate dynamic range and
+   resolution. In telephony we generally only start with 13 or 14 bits,
+   anyway. This is sufficient for the longest Goertzel we currently use. */
+#if defined(SPANDSP_USE_FIXED_POINT)
+#define goertzel_preadjust_amp(amp) (((int16_t) amp) >> 7)
+#else
+#define goertzel_preadjust_amp(amp) ((float) amp)
+#endif
+
+/* Minimal update the state of a Goertzel transform. This is similar to
+   goertzel_sample, but more suited to blocks of Goertzels. It assumes
+   the amplitude is pre-shifted, and does not update the per-state sample
+   count.
+    \brief Update the state of a Goertzel transform.
+    \param s The Goertzel context.
+    \param amp The adjusted sample to be transformed. */
+#if defined(SPANDSP_USE_FIXED_POINT)
+static __inline__ void goertzel_samplex(goertzel_state_t *s, int16_t amp)
+#else
+static __inline__ void goertzel_samplex(goertzel_state_t *s, float amp)
+#endif
+{
+#if defined(SPANDSP_USE_FIXED_POINT)
+    int16_t x;
+    int16_t v1;
+#else
+    float v1;
+#endif
+
+    v1 = s->v2;
+    s->v2 = s->v3;
+#if defined(SPANDSP_USE_FIXED_POINT)
+    x = (((int32_t) s->fac*s->v2) >> 14);
+    s->v3 = x - v1 + amp;
+#else
+    s->v3 = s->fac*s->v2 - v1 + amp;
+#endif
+}
+/*- End of function --------------------------------------------------------*/
+
+/*! Generate a Hamming weighted coefficient set, to be used for a periodogram analysis.
+    \param coeffs The generated coefficients.
+    \param freq The frequency to be matched by the periodogram, in Hz.
+    \param sample_rate The sample rate of the signal, in samples per second.
+    \param window_len The length of the periodogram window. This must be an even number.
+    \return The number of generated coefficients.
+*/
+int periodogram_generate_coeffs(complexf_t coeffs[], float freq, int sample_rate, int window_len);
+
+/*! Generate the phase offset to be expected between successive periodograms evaluated at the 
+    specified interval.
+    \param offset A point to the generated phase offset.
+    \param freq The frequency being matched by the periodogram, in Hz.
+    \param sample_rate The sample rate of the signal, in samples per second.
+    \param interval The interval between periodograms, in samples.
+    \return The scaling factor.
+*/
+float periodogram_generate_phase_offset(complexf_t *offset, float freq, int sample_rate, int interval);
+
+/*! Evaluate a periodogram.
+    \param coeffs A set of coefficients generated by periodogram_generate_coeffs().
+    \param amp The complex amplitude of the signal.
+    \param len The length of the periodogram, in samples. This must be an even number.
+    \return The periodogram result.
+*/
+complexf_t periodogram(const complexf_t coeffs[], const complexf_t amp[], int len);
+
+/*! Prepare data for evaluating a set of periodograms.
+    \param sum A vector of sums of pairs of signal samples. This will be half the length of len.
+    \param diff A vector of differences between pairs of signal samples. This will be half the length of len.
+    \param amp The complex amplitude of the signal.
+    \param len The length of the periodogram, in samples. This must be an even number.
+    \return The length of the vectors sum and diff.
+*/
+int periodogram_prepare(complexf_t sum[], complexf_t diff[], const complexf_t amp[], int len);
+
+/*! Evaluate a periodogram, based on data prepared by periodogram_prepare(). This is more efficient
+    than using periodogram() when several periodograms are to be applied to the same signal.
+    \param coeffs A set of coefficients generated by periodogram_generate_coeffs().
+    \param sum A vector of sums produced by periodogram_prepare().
+    \param diff A vector of differences produced by periodogram_prepare().
+    \param len The length of the periodogram, in samples. This must be an even number.
+    \return The periodogram result.
+*/
+complexf_t periodogram_apply(const complexf_t coeffs[], const complexf_t sum[], const complexf_t diff[], int len);
+
+/*! Apply a phase offset, to find the frequency error between periodogram evaluations.
+    specified interval.
+    \param phase_offset A point to the expected phase offset.
+    \param scale The scaling factor to be used.
+    \param last_result A pointer to the previous periodogram result.
+    \param result A pointer to the current periodogram result.
+    \return The frequency error, in Hz.
+*/
+float periodogram_freq_error(const complexf_t *phase_offset, float scale, const complexf_t *last_result, const complexf_t *result);
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/spandsp/tone_generate.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/spandsp/tone_generate.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,119 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * tone_generate.h - General telephony tone generation.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2001 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: tone_generate.h,v 1.33 2008/04/17 14:27:01 steveu Exp $
+ */
+
+/*! \file */
+
+#if !defined(_SPANDSP_TONE_GENERATE_H_)
+#define _SPANDSP_TONE_GENERATE_H_
+
+/*! \page tone_generation_page Tone generation
+\section tone_generation_page_sec_1 What does it do?
+The tone generation module provides for the generation of cadenced tones,
+suitable for a wide range of telephony applications. 
+
+\section tone_generation_page_sec_2 How does it work?
+Oscillators are a problem. They oscillate due to instability, and yet we need
+them to behave in a stable manner. A look around the web will reveal many papers
+on this subject. Many describe rather complex solutions to the problem. However,
+we are only concerned with telephony applications. It is possible to generate
+the tones we need with a very simple efficient scheme. It is also practical to
+use an exhaustive test to prove the oscillator is stable under all the
+conditions in which we will use it. 
+*/
+
+typedef struct
+{
+    int32_t phase_rate;
+    float gain;
+} tone_gen_tone_descriptor_t;
+
+/*!
+    Cadenced dual tone generator descriptor.
+*/
+typedef struct
+{
+    tone_gen_tone_descriptor_t tone[4];
+    int duration[4];
+    int repeat;
+} tone_gen_descriptor_t;
+
+/*!
+    Cadenced dual tone generator state descriptor. This defines the state of
+    a single working instance of a generator.
+*/
+typedef struct
+{
+    tone_gen_tone_descriptor_t tone[4];
+
+    uint32_t phase[4];
+    int duration[4];
+    int repeat;
+
+    int current_section;
+    int current_position;
+} tone_gen_state_t;
+
+#if defined(__cplusplus)
+extern "C"
+{
+#endif
+
+/*! Create a tone generator descriptor
+    \brief Create a tone generator descriptor
+    \param s The descriptor
+    \param f1 The first frequency, in Hz
+    \param l1 The level of the first frequency, in dBm0
+    \param f2 0 for no second frequency, a positive number for the second frequency,
+           in Hz, or a negative number for an AM modulation frequency, in Hz
+    \param l2 The level of the second frequency, in dBm0, or the percentage modulation depth
+           for an AM modulated tone.
+    \param d1 x
+    \param d2 x
+    \param d3 x
+    \param d4 x
+    \param repeat x */
+void make_tone_gen_descriptor(tone_gen_descriptor_t *s,
+                              int f1,
+                              int l1,
+                              int f2,
+                              int l2,
+                              int d1,
+                              int d2,
+                              int d3,
+                              int d4,
+                              int repeat);
+
+tone_gen_state_t *tone_gen_init(tone_gen_state_t *s, tone_gen_descriptor_t *t);
+
+int tone_gen(tone_gen_state_t *s, int16_t amp[], int max_samples);
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/spandsp/v17rx.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/spandsp/v17rx.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,479 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * v17rx.h - ITU V.17 modem receive part
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2003 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: v17rx.h,v 1.52 2008/07/16 14:23:48 steveu Exp $
+ */
+
+/*! \file */
+
+#if !defined(_V17RX_H_)
+#define _V17RX_H_
+
+/*! \page v17rx_page The V.17 receiver
+\section v17rx_page_sec_1 What does it do?
+The V.17 receiver implements the receive side of a V.17 modem. This can operate
+at data rates of 14400, 12000, 9600 and 7200 bits/second. The audio input is a stream
+of 16 bit samples, at 8000 samples/second. The transmit and receive side of V.17
+modems operate independantly. V.17 is mostly used for FAX transmission over PSTN
+lines, where it provides the standard 14400 bits/second rate. 
+
+\section v17rx_page_sec_2 How does it work?
+V.17 uses QAM modulation, at 2400 baud, and trellis coding. Constellations with
+16, 32, 64, and 128 points are defined. After one bit per baud is absorbed by the
+trellis coding, this gives usable bit rates of 7200, 9600, 12000, and 14400 per
+second.
+
+V.17 specifies a training sequence at the start of transmission, which makes the
+design of a V.17 receiver relatively straightforward. The first stage of the
+training sequence consists of 256
+symbols, alternating between two constellation positions. The receiver monitors
+the signal power, to sense the possible presence of a valid carrier. When the
+alternating signal begins, the power rising above a minimum threshold (-43dBm0)
+causes the main receiver computation to begin. The initial measured power is
+used to quickly set the gain of the receiver. After this initial settling, the
+front end gain is locked, and the adaptive equalizer tracks any subsequent
+signal level variation. The signal is oversampled to 24000 samples/second (i.e.
+signal, zero, zero, signal, zero, zero, ...) and fed to a complex root raised
+cosine pulse shaping filter. This filter has been modified from the conventional
+root raised cosine filter, by shifting it up the band, to be centred at the nominal
+carrier frequency. This filter interpolates the samples, pulse shapes, and performs
+a fractional sample delay at the same time. 192 sets of filter coefficients are used
+to achieve a set of finely spaces fractional sample delays, between zero and
+one sample. By choosing every fifth sample, and the appropriate set of filter
+coefficients, the properly tuned symbol tracker can select data samples at 4800
+samples/second from points within 0.28 degrees of the centre and mid-points of
+each symbol. The output of the filter is multiplied by a complex carrier, generated
+by a DDS. The result is a baseband signal, requiring no further filtering, apart from
+an adaptive equalizer. The baseband signal is fed to a T/2 adaptive equalizer.
+A band edge component maximisation algorithm is used to tune the sampling, so the samples
+fed to the equalizer are close to the mid point and edges of each symbol. Initially
+the algorithm is very lightly damped, to ensure the symbol alignment pulls in
+quickly. Because the sampling rate will not be precisely the same as the
+transmitter's (the spec. says the symbol timing should be within 0.01%), the
+receiver constantly evaluates and corrects this sampling throughout its
+operation. During the symbol timing maintainence phase, the algorithm uses
+a heavier damping.
+
+The carrier is specified as 1800Hz +- 1Hz at the transmitter, and 1800 +-7Hz at
+the receiver. The receive carrier would only be this inaccurate if the link
+includes FDM sections. These are being phased out, but the design must still
+allow for the worst case. Using an initial 1800Hz signal for demodulation gives
+a worst case rotation rate for the constellation of about one degree per symbol.
+Once the symbol timing synchronisation algorithm has been given time to lock to the
+symbol timing of the initial alternating pattern, the phase of the demodulated signal
+is recorded on two successive symbols - once for each of the constellation positions.
+The receiver then tracks the symbol alternations, until a large phase jump occurs.
+This signifies the start of the next phase of the training sequence. At this
+point the total phase shift between the original recorded symbol phase, and the
+symbol phase just before the phase jump occurred is used to provide a coarse
+estimation of the rotation rate of the constellation, and it current absolute
+angle of rotation. These are used to update the current carrier phase and phase
+update rate in the carrier DDS. The working data already in the pulse shaping
+filter and equalizer buffers is given a similar step rotation to pull it all
+into line. From this point on, a heavily damped integrate and dump approach,
+based on the angular difference between each received constellation position and
+its expected position, is sufficient to track the carrier, and maintain phase
+alignment. A fast rough approximator for the arc-tangent function is adequate
+for the estimation of the angular error. 
+
+The next phase of the training sequence is a scrambled sequence of two
+particular symbols. We train the T/2 adaptive equalizer using this sequence. The
+scrambling makes the signal sufficiently diverse to ensure the equalizer
+converges to the proper generalised solution. At the end of this sequence, the
+equalizer should be sufficiently well adapted that is can correctly resolve the
+full QAM constellation. However, the equalizer continues to adapt throughout
+operation of the modem, fine tuning on the more complex data patterns of the
+full QAM constellation. 
+
+In the last phase of the training sequence, the modem enters normal data
+operation, with a short defined period of all ones as data. As in most high
+speed modems, data in a V.17 modem passes through a scrambler, to whiten the
+spectrum of the signal. The transmitter should initialise its data scrambler,
+and pass the ones through it. At the end of the ones, real data begins to pass
+through the scrambler, and the transmit modem is in normal operation. The
+receiver tests that ones are really received, in order to verify the modem
+trained correctly. If all is well, the data following the ones is fed to the
+application, and the receive modem is up and running. Unfortunately, some
+transmit side of some real V.17 modems fail to initialise their scrambler before
+sending the ones. This means the first 23 received bits (the length of the
+scrambler register) cannot be trusted for the test. The receive modem,
+therefore, only tests that bits starting at bit 24 are really ones.
+
+The V.17 signal is trellis coded. Two bits of each symbol are convolutionally coded
+to form a 3 bit trellis code - the two original bits, plus an extra redundant bit. It
+is possible to ignore the trellis coding, and just decode the non-redundant bits.
+However, the noise performance of the receiver would suffer. Using a proper
+trellis decoder adds several dB to the noise tolerance to the receiving modem. Trellis
+coding seems quite complex at first sight, but is fairly straightforward once you
+get to grips with it.
+
+Trellis decoding tracks the data in terms of the possible states of the convolutional
+coder at the transmitter. There are 8 possible states of the V.17 coder. The first
+step in trellis decoding is to find the best candidate constellation point
+for each of these 8 states. One of thse will be our final answer. The constellation
+has been designed so groups of 8 are spread fairly evenly across it. Locating them
+is achieved is a reasonably fast manner, by looking up the answers in a set of space
+map tables. The disadvantage is the tables are potentially large enough to affect
+cache performance. The trellis decoder works over 16 successive symbols. The result
+of decoding is not known until 16 symbols after the data enters the decoder. The
+minimum total accumulated mismatch between each received point and the actual
+constellation (termed the distance) is assessed for each of the 8 states. A little
+analysis of the coder shows that each of the 8 current states could be arrived at
+from 4 different previous states, through 4 different constellation bit patterns.
+For each new state, the running total distance is arrived at by inspecting a previous
+total plus a new distance for the appropriate 4 previous states. The minimum of the 4
+values becomes the new distance for the state. Clearly, a mechanism is needed to stop
+this distance from growing indefinitely. A sliding window, and several other schemes
+are possible. However, a simple single pole IIR is very simple, and provides adequate
+results.
+
+For each new state we store the constellation bit pattern, or path, to that state, and
+the number of the previous state. We find the minimum distance amongst the 8 new
+states for each new symbol. We then trace back through the states, until we reach the
+one 16 states ago which leads to the current minimum distance. The bit pattern stored
+there is the error corrected bit pattern for that symbol.
+
+So, what does Trellis coding actually achieve? TCM is easier to understand by looking
+at the V.23bis modem spec. The V.32bis spec. is very similar to V.17, except that it
+is a full duplex modem and has non-TCM options, as well as the TCM ones in V.17.
+
+V32bis defines two options for pumping 9600 bits per second down a phone line - one
+with and one without TCM. Both run at 2400 baud. The non-TCM one uses simple 16 point
+QAM on the raw data. The other takes two out of every four raw bits, and convolutionally
+encodes them to 3. Now we have 5 bits per symbol, and we need 32 point QAM to send the
+data.
+
+The raw error rate from simple decoding of the 32 point QAM is horrible compared to
+decoding the 16 point QAM. If a point decoded from the 32 point QAM is wrong, the likely
+correct choice should be one of the adjacent ones. It is unlikely to have been one that
+is far away across the constellation, unless there was a huge noise spike, interference,
+or something equally nasty. Now, the 32 point symbols do not exist in isolation. There
+was a kind of temporal smearing in the convolutional coding. It created a well defined
+dependency between successive symbols. If we knew for sure what the last few symbols
+were, they would lead us to a limited group of possible values for the current symbol,
+constrained by the behaviour of the convolutional coder. If you look at how the symbols
+were mapped to constellation points, you will see the mapping tries to spread those
+possible symbols as far apart as possible. This will leave only one that is pretty
+close to the received point, which must be the correct choice. However, this assumes
+we know the last few symbols for sure. Since we don't, we have a bit more work to do
+to achieve reliable decoding.
+
+Instead of decoding to the nearest point on the constellation, we decode to a group of
+likely constellation points in the neighbourhood of the received point. We record the
+mismatch for each - that is the distance across the constellation between the received
+point and the group of nearby points. To avoid square roots, recording x2 + y2 can be
+good enough. Symbol by symbol, we record this information. After a few symbols we can
+stand back and look at the recorded information.
+
+For each symbol we have a set of possible symbol values and error metric pairs. The
+dependency between symbols, created by the convolutional coder, means some paths from
+symbol to symbol are possible and some are not. It we trace back through the possible
+symbol to symbol paths, and total up the error metric through those paths, we end up
+with a set of figures of merit (or more accurately figures of demerit, since
+larger == worse) for the likelihood of each path being the correct one. The path with
+the lowest total metric is the most likely, and gives us our final choice for what we
+think the current symbol really is.
+
+That was hard work. It takes considerable computation to do this selection and traceback,
+symbol by symbol. We need to get quite a lot from this. It needs to drive the error rate
+down so far that is compensates for the much higher error rate due to the larger
+constellation, and then buys us some actual benefit. Well in the example we are looking
+at - V.32bis at 9600bps - it works out the error rate from the TCM option is like using
+the non-TCM option with several dB more signal to noise ratio. That's nice. The non-TCM
+option is pretty reasonable on most phone lines, but a better error rate is always a
+good thing. However, V32bis includes a 14,400bps option. That uses 2400 baud, and 6 bit
+symbols. Convolutional encoding increases that to 7 bits per symbol, by taking 2 bits and
+encoding them to 3. This give a 128 point QAM constellation. Again, the difference between
+using this, and using just an uncoded 64 point constellation is equivalent to maybe 5dB of
+extra signal to noise ratio. However, in this case it is the difference between the modem
+working only on the most optimal lines, and being widely usable across most phone lines.
+TCM absolutely transformed the phone line modem business.
+*/
+
+/* Target length for the equalizer is about 63 taps, to deal with the worst stuff
+   in V.56bis. */
+#define V17_EQUALIZER_PRE_LEN       7  /* this much before the real event */
+#define V17_EQUALIZER_POST_LEN      7  /* this much after the real event */
+#define V17_EQUALIZER_MASK          63 /* one less than a power of 2 >= (V17_EQUALIZER_PRE_LEN + 1 + V17_EQUALIZER_POST_LEN) */
+
+#define V17_RX_FILTER_STEPS         27
+
+/* We can store more trellis depth that we look back over, so that we can push out a group
+   of symbols in one go, giving greater processing efficiency, at the expense of a bit more
+   latency through the modem. */
+/* Right now we don't take advantage of this optimisation. */
+#define V17_TRELLIS_STORAGE_DEPTH   16
+#define V17_TRELLIS_LOOKBACK_DEPTH  16
+
+/*!
+    V.17 modem receive side descriptor. This defines the working state for a
+    single instance of a V.17 modem receiver.
+*/
+typedef struct
+{
+    /*! \brief The bit rate of the modem. Valid values are 7200 9600, 12000 and 14400. */
+    int bit_rate;
+    /*! \brief The callback function used to put each bit received. */
+    put_bit_func_t put_bit;
+    /*! \brief A user specified opaque pointer passed to the put_but routine. */
+    void *put_bit_user_data;
+
+    /*! \brief The callback function used to report modem status changes. */
+    modem_rx_status_func_t status_handler;
+    /*! \brief A user specified opaque pointer passed to the status function. */
+    void *status_user_data;
+
+    /*! \brief A callback function which may be enabled to report every symbol's
+               constellation position. */
+    qam_report_handler_t qam_report;
+    /*! \brief A user specified opaque pointer passed to the qam_report callback
+               routine. */
+    void *qam_user_data;
+
+    /*! \brief The route raised cosine (RRC) pulse shaping filter buffer. */
+#if defined(SPANDSP_USE_FIXED_POINT)
+    int16_t rrc_filter[2*V17_RX_FILTER_STEPS];
+#else
+    float rrc_filter[2*V17_RX_FILTER_STEPS];
+#endif
+    /*! \brief Current offset into the RRC pulse shaping filter buffer. */
+    int rrc_filter_step;
+
+    /*! \brief The state of the differential decoder */
+    int diff;
+    /*! \brief The register for the data scrambler. */
+    unsigned int scramble_reg;
+    /*! \brief TRUE if the short training sequence is to be used. */
+    int short_train;
+    /*! \brief The section of the training data we are currently in. */
+    int training_stage;
+    /*! \brief A count of how far through the current training step we are. */
+    int training_count;
+    /*! \brief A measure of how much mismatch there is between the real constellation,
+        and the decoded symbol positions. */
+    float training_error;
+    /*! \brief The value of the last signal sample, using the a simple HPF for signal power estimation. */
+    int16_t last_sample;
+    /*! \brief >0 if a signal above the minimum is present. It may or may not be a V.17 signal. */
+    int signal_present;
+    /*! \brief Whether or not a carrier drop was detected and the signal delivery is pending. */
+    int carrier_drop_pending;
+    /*! \brief A count of the current consecutive samples below the carrier off threshold. */
+    int low_samples;
+    /*! \brief A highest magnitude sample seen. */
+    int16_t high_sample;
+
+    /*! \brief The current phase of the carrier (i.e. the DDS parameter). */
+    uint32_t carrier_phase;
+    /*! \brief The update rate for the phase of the carrier (i.e. the DDS increment). */
+    int32_t carrier_phase_rate;
+    /*! \brief The carrier update rate saved for reuse when using short training. */
+    int32_t carrier_phase_rate_save;
+    /*! \brief The proportional part of the carrier tracking filter. */
+    float carrier_track_p;
+    /*! \brief The integral part of the carrier tracking filter. */
+    float carrier_track_i;
+
+    /*! \brief A power meter, to measure the HPF'ed signal power in the channel. */    
+    power_meter_t power;
+    /*! \brief The power meter level at which carrier on is declared. */
+    int32_t carrier_on_power;
+    /*! \brief The power meter level at which carrier off is declared. */
+    int32_t carrier_off_power;
+    /*! \brief The scaling factor accessed by the AGC algorithm. */
+    float agc_scaling;
+    /*! \brief The previous value of agc_scaling, needed to reuse old training. */
+    float agc_scaling_save;
+
+    /*! \brief The current delta factor for updating the equalizer coefficients. */
+    float eq_delta;
+#if defined(SPANDSP_USE_FIXED_POINTx)
+    /*! \brief The adaptive equalizer coefficients. */
+    complexi_t eq_coeff[V17_EQUALIZER_PRE_LEN + 1 + V17_EQUALIZER_POST_LEN];
+    /*! \brief A saved set of adaptive equalizer coefficients for use after restarts. */
+    complexi_t eq_coeff_save[V17_EQUALIZER_PRE_LEN + 1 + V17_EQUALIZER_POST_LEN];
+    /*! \brief The equalizer signal buffer. */
+    complexi_t eq_buf[V17_EQUALIZER_MASK + 1];
+#else
+    complexf_t eq_coeff[V17_EQUALIZER_PRE_LEN + 1 + V17_EQUALIZER_POST_LEN];
+    complexf_t eq_coeff_save[V17_EQUALIZER_PRE_LEN + 1 + V17_EQUALIZER_POST_LEN];
+    complexf_t eq_buf[V17_EQUALIZER_MASK + 1];
+#endif
+    /*! \brief Current read offset into the equalizer buffer. */
+    int eq_step;
+    /*! \brief Current write offset into the equalizer buffer. */
+    int eq_put_step;
+
+    /*! \brief The current half of the baud. */
+    int baud_half;
+#if defined(SPANDSP_USE_FIXED_POINTx)
+    /*! Low band edge filter for symbol sync. */
+    int32_t symbol_sync_low[2];
+    /*! High band edge filter for symbol sync. */
+    int32_t symbol_sync_high[2];
+    /*! DC filter for symbol sync. */
+    int32_t symbol_sync_dc_filter[2];
+    /*! Baud phase for symbol sync. */
+    int32_t baud_phase;
+#else
+    /*! Low band edge filter for symbol sync. */
+    float symbol_sync_low[2];
+    /*! High band edge filter for symbol sync. */
+    float symbol_sync_high[2];
+    /*! DC filter for symbol sync. */
+    float symbol_sync_dc_filter[2];
+    /*! Baud phase for symbol sync. */
+    float baud_phase;
+#endif
+    /*! \brief The total symbol timing correction since the carrier came up.
+               This is only for performance analysis purposes. */
+    int total_baud_timing_correction;
+
+    /*! \brief Starting phase angles for the coarse carrier aquisition step. */
+    int32_t start_angles[2];
+    /*! \brief History list of phase angles for the coarse carrier aquisition step. */
+    int32_t angles[16];
+    /*! \brief A pointer to the current constellation. */
+#if defined(SPANDSP_USE_FIXED_POINTx)
+    const complexi16_t *constellation;
+#else
+    const complexf_t *constellation;
+#endif
+    /*! \brief A pointer to the current space map. There is a space map for
+               each trellis state. */
+    int space_map;
+    /*! \brief The number of bits in each symbol at the current bit rate. */
+    int bits_per_symbol;
+
+    /*! \brief Current pointer to the trellis buffers */
+    int trellis_ptr;
+    /*! \brief The trellis. */
+    int full_path_to_past_state_locations[V17_TRELLIS_STORAGE_DEPTH][8];
+    /*! \brief The trellis. */
+    int past_state_locations[V17_TRELLIS_STORAGE_DEPTH][8];
+    /*! \brief Euclidean distances (actually the squares of the distances)
+               from the last states of the trellis. */
+#if defined(SPANDSP_USE_FIXED_POINTx)
+    uint32_t distances[8];
+#else
+    float distances[8];
+#endif
+    /*! \brief Error and flow logging control */
+    logging_state_t logging;
+} v17_rx_state_t;
+
+#if defined(__cplusplus)
+extern "C"
+{
+#endif
+
+/*! Initialise a V.17 modem receive context.
+    \brief Initialise a V.17 modem receive context.
+    \param s The modem context.
+    \param bit_rate The bit rate of the modem. Valid values are 7200, 9600, 12000 and 14400.
+    \param put_bit The callback routine used to put the received data.
+    \param user_data An opaque pointer passed to the put_bit routine.
+    \return A pointer to the modem context, or NULL if there was a problem. */
+v17_rx_state_t *v17_rx_init(v17_rx_state_t *s, int bit_rate, put_bit_func_t put_bit, void *user_data);
+
+/*! Reinitialise an existing V.17 modem receive context.
+    \brief Reinitialise an existing V.17 modem receive context.
+    \param s The modem context.
+    \param bit_rate The bit rate of the modem. Valid values are 7200, 9600, 12000 and 14400.
+    \param short_train TRUE if a short training sequence is expected.
+    \return 0 for OK, -1 for bad parameter */
+int v17_rx_restart(v17_rx_state_t *s, int bit_rate, int short_train);
+
+/*! Free a V.17 modem receive context.
+    \brief Free a V.17 modem receive context.
+    \param s The modem context.
+    \return 0 for OK */
+int v17_rx_free(v17_rx_state_t *s);
+
+/*! Change the put_bit function associated with a V.17 modem receive context.
+    \brief Change the put_bit function associated with a V.17 modem receive context.
+    \param s The modem context.
+    \param put_bit The callback routine used to handle received bits.
+    \param user_data An opaque pointer. */
+void v17_rx_set_put_bit(v17_rx_state_t *s, put_bit_func_t put_bit, void *user_data);
+
+/*! Change the modem status report function associated with a V.17 modem receive context.
+    \brief Change the modem status report function associated with a V.17 modem receive context.
+    \param s The modem context.
+    \param handler The callback routine used to report modem status changes.
+    \param user_data An opaque pointer. */
+void v17_rx_set_modem_status_handler(v17_rx_state_t *s, modem_rx_status_func_t handler, void *user_data);
+
+/*! Process a block of received V.17 modem audio samples.
+    \brief Process a block of received V.17 modem audio samples.
+    \param s The modem context.
+    \param amp The audio sample buffer.
+    \param len The number of samples in the buffer.
+    \return The number of samples unprocessed.
+*/
+int v17_rx(v17_rx_state_t *s, const int16_t amp[], int len);
+
+/*! Get a snapshot of the current equalizer coefficients.
+    \brief Get a snapshot of the current equalizer coefficients.
+    \param s The modem context.
+    \param coeffs The vector of complex coefficients.
+    \return The number of coefficients in the vector. */
+#if defined(SPANDSP_USE_FIXED_POINTx)
+int v17_rx_equalizer_state(v17_rx_state_t *s, complexi_t **coeffs);
+#else
+int v17_rx_equalizer_state(v17_rx_state_t *s, complexf_t **coeffs);
+#endif
+
+/*! Get the current received carrier frequency.
+    \param s The modem context.
+    \return The frequency, in Hertz. */
+float v17_rx_carrier_frequency(v17_rx_state_t *s);
+
+/*! Get the current symbol timing correction since startup.
+    \param s The modem context.
+    \return The correction. */
+float v17_rx_symbol_timing_correction(v17_rx_state_t *s);
+
+/*! Get a current received signal power.
+    \param s The modem context.
+    \return The signal power, in dBm0. */
+float v17_rx_signal_power(v17_rx_state_t *s);
+
+/*! Set the power level at which the carrier detection will cut in
+    \param s The modem context.
+    \param cutoff The signal cutoff power, in dBm0. */
+void v17_rx_signal_cutoff(v17_rx_state_t *s, float cutoff);
+
+/*! Set a handler routine to process QAM status reports
+    \param s The modem context.
+    \param handler The handler routine.
+    \param user_data An opaque pointer passed to the handler routine. */
+void v17_rx_set_qam_report_handler(v17_rx_state_t *s, qam_report_handler_t handler, void *user_data);
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/spandsp/v17tx.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/spandsp/v17tx.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,223 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * v17tx.h - ITU V.17 modem transmit part
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2004 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: v17tx.h,v 1.36 2008/07/16 14:23:48 steveu Exp $
+ */
+
+/*! \file */
+
+#if !defined(_SPANDSP_V17TX_H_)
+#define _SPANDSP_V17TX_H_
+
+/*! \page v17tx_page The V.17 transmitter
+\section v17tx_page_sec_1 What does it do?
+The V.17 transmitter implements the transmit side of a V.17 modem. This can
+operate at data rates of 14400, 12000, 9600 and 7200 bits/second. The audio
+output is a stream of 16 bit samples, at 8000 samples/second. The transmit and
+receive side of V.17 modems operate independantly. V.17 is mostly used for FAX
+transmission, where it provides the standard 14400 bits/second rate. 
+
+\section v17tx_page_sec_2 How does it work?
+V.17 uses QAM modulation and trellis coding. The data to be transmitted is
+scrambled, to whiten it. The least significant 2 bits of each symbol are then
+differentially encoded, using a simple lookup approach. The resulting 2 bits are
+convolutionally encoded, producing 3 bits. The extra bit is the redundant bit
+of the trellis code. The other bits of the symbol pass by the differential
+and convolutional coding unchanged. The resulting bits define the constellation
+point to be transmitted for the symbol. The redundant bit doubles the size of the
+constellation, and so increases the error rate for detecting individual symbols
+at the receiver. However, when a number of successive symbols are processed at
+the receiver, the redundancy actually provides several dB of improved error
+performance.
+
+The standard method of producing a QAM modulated signal is to use a sampling
+rate which is a multiple of the baud rate. The raw signal is then a series of
+complex pulses, each an integer number of samples long. These can be shaped,
+using a suitable complex filter, and multiplied by a complex carrier signal
+to produce the final QAM signal for transmission. 
+
+The pulse shaping filter is only vaguely defined by the V.17 spec. Some of the
+other ITU modem specs. fully define the filter, typically specifying a root
+raised cosine filter, with 50% excess bandwidth. This is a pity, since it
+increases the variability of the received signal. However, the receiver's
+adaptive equalizer will compensate for these differences. The current
+design uses a root raised cosine filter with 25% excess bandwidth. Greater
+excess bandwidth will not allow the tranmitted signal to meet the spectral
+requirements.
+
+The sampling rate for our transmitter is defined by the channel - 8000 per
+second. This is not a multiple of the baud rate (i.e. 2400 baud). The baud
+interval is actually 10/3 sample periods. Instead of using a symmetric
+FIR to pulse shape the signal, a polyphase filter is used. This consists of
+10 sets of coefficients, offering zero to 9/10ths of a baud phase shift as well
+as root raised cosine filtering. The appropriate coefficient set is chosen for
+each signal sample generated.
+
+The carrier is generated using the DDS method. Using two second order resonators,
+started in quadrature, might be more efficient, as it would have less impact on
+the processor cache than a table lookup approach. However, the DDS approach
+suits the receiver better, so the same signal generator is also used for the
+transmitter. 
+*/
+
+#define V17_TX_FILTER_STEPS     9
+
+/*!
+    V.17 modem transmit side descriptor. This defines the working state for a
+    single instance of a V.17 modem transmitter.
+*/
+typedef struct
+{
+    /*! \brief The bit rate of the modem. Valid values are 4800, 7200 and 9600. */
+    int bit_rate;
+    /*! \brief The callback function used to get the next bit to be transmitted. */
+    get_bit_func_t get_bit;
+    /*! \brief A user specified opaque pointer passed to the get_bit function. */
+    void *get_bit_user_data;
+
+    /*! \brief The callback function used to report modem status changes. */
+    modem_tx_status_func_t status_handler;
+    /*! \brief A user specified opaque pointer passed to the status function. */
+    void *status_user_data;
+
+    /*! \brief The gain factor needed to achieve the specified output power. */
+#if defined(SPANDSP_USE_FIXED_POINT)
+    int32_t gain;
+#else
+    float gain;
+#endif
+
+    /*! \brief The route raised cosine (RRC) pulse shaping filter buffer. */
+#if defined(SPANDSP_USE_FIXED_POINT)
+    complexi16_t rrc_filter[2*V17_TX_FILTER_STEPS];
+#else
+    complexf_t rrc_filter[2*V17_TX_FILTER_STEPS];
+#endif
+    /*! \brief Current offset into the RRC pulse shaping filter buffer. */
+    int rrc_filter_step;
+
+    /*! \brief The current state of the differential encoder. */
+    int diff;
+    /*! \brief The current state of the convolutional encoder. */
+    int convolution;
+
+    /*! \brief The register for the data scrambler. */
+    unsigned int scramble_reg;
+    /*! \brief TRUE if transmitting the training sequence. FALSE if transmitting user data. */
+    int in_training;
+    /*! \brief TRUE if the short training sequence is to be used. */
+    int short_train;
+    /*! \brief A counter used to track progress through sending the training sequence. */
+    int training_step;
+
+    /*! \brief The current phase of the carrier (i.e. the DDS parameter). */
+    uint32_t carrier_phase;
+    /*! \brief The update rate for the phase of the carrier (i.e. the DDS increment). */
+    int32_t carrier_phase_rate;
+    /*! \brief The current fractional phase of the baud timing. */
+    int baud_phase;
+    /*! \brief The code number for the current position in the constellation. */
+    int constellation_state;
+    
+    /*! \brief A pointer to the constellation currently in use. */
+#if defined(SPANDSP_USE_FIXED_POINT)
+    const complexi16_t *constellation;
+#else
+    const complexf_t *constellation;
+#endif
+    /*! \brief The current number of data bits per symbol. This does not include
+               the redundant bit. */
+    int bits_per_symbol;
+    /*! \brief The get_bit function in use at any instant. */
+    get_bit_func_t current_get_bit;
+    /*! \brief Error and flow logging control */
+    logging_state_t logging;
+} v17_tx_state_t;
+
+#if defined(__cplusplus)
+extern "C"
+{
+#endif
+
+/*! Adjust a V.17 modem transmit context's power output.
+    \brief Adjust a V.17 modem transmit context's output power.
+    \param s The modem context.
+    \param power The power level, in dBm0 */
+void v17_tx_power(v17_tx_state_t *s, float power);
+
+/*! Initialise a V.17 modem transmit context. This must be called before the first
+    use of the context, to initialise its contents.
+    \brief Initialise a V.17 modem transmit context.
+    \param s The modem context.
+    \param rate The bit rate of the modem. Valid values are 7200, 9600, 12000 and 14400.
+    \param tep TRUE is the optional TEP tone is to be transmitted.
+    \param get_bit The callback routine used to get the data to be transmitted.
+    \param user_data An opaque pointer.
+    \return A pointer to the modem context, or NULL if there was a problem. */
+v17_tx_state_t *v17_tx_init(v17_tx_state_t *s, int rate, int tep, get_bit_func_t get_bit, void *user_data);
+
+/*! Reinitialise an existing V.17 modem transmit context, so it may be reused.
+    \brief Reinitialise an existing V.17 modem transmit context.
+    \param s The modem context.
+    \param bit_rate The bit rate of the modem. Valid values are 7200, 9600, 12000 and 14400.
+    \param tep TRUE is the optional TEP tone is to be transmitted.
+    \param short_train TRUE if the short training sequence should be used.
+    \return 0 for OK, -1 for parameter error. */
+int v17_tx_restart(v17_tx_state_t *s, int bit_rate, int tep, int short_train);
+
+/*! Free a V.17 modem transmit context.
+    \brief Free a V.17 modem transmit context.
+    \param s The modem context.
+    \return 0 for OK */
+int v17_tx_free(v17_tx_state_t *s);
+
+/*! Change the get_bit function associated with a V.17 modem transmit context.
+    \brief Change the get_bit function associated with a V.17 modem transmit context.
+    \param s The modem context.
+    \param get_bit The callback routine used to get the data to be transmitted.
+    \param user_data An opaque pointer. */
+void v17_tx_set_get_bit(v17_tx_state_t *s, get_bit_func_t get_bit, void *user_data);
+
+/*! Change the modem status report function associated with a V.17 modem transmit context.
+    \brief Change the modem status report function associated with a V.17 modem transmit context.
+    \param s The modem context.
+    \param handler The callback routine used to report modem status changes.
+    \param user_data An opaque pointer. */
+void v17_tx_set_modem_status_handler(v17_tx_state_t *s, modem_tx_status_func_t handler, void *user_data);
+
+/*! Generate a block of V.17 modem audio samples.
+    \brief Generate a block of V.17 modem audio samples.
+    \param s The modem context.
+    \param amp The audio sample buffer.
+    \param len The number of samples to be generated.
+    \return The number of samples actually generated.
+*/
+int v17_tx(v17_tx_state_t *s, int16_t amp[], int len);
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/spandsp/v22bis.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/spandsp/v22bis.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,325 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * v22bis.h - ITU V.22bis modem
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2004 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: v22bis.h,v 1.30 2008/07/25 13:56:54 steveu Exp $
+ */
+
+/*! \file */
+
+/*! \page v22bis_page The V.22bis modem
+\section v22bis_page_sec_1 What does it do?
+The V.22bis modem is a duplex modem for general data use on the PSTN, at rates
+of 1200 and 2400 bits/second. It is a compatible extension of the V.22 modem,
+which is a 1200 bits/second only design. It is a band-split design, using carriers
+of 1200Hz and 2400Hz. It is the fastest PSTN modem in general use which does not
+use echo-cancellation.
+
+\section v22bis__page_sec_2 How does it work?
+V.22bis uses 4PSK modulation at 1200 bits/second or 16QAM modulation at 2400
+bits/second. At 1200bps the symbols are so long that a fixed compromise equaliser
+is sufficient to recover the 4PSK signal reliably. At 2400bps an adaptive
+equaliser is necessary.
+
+The V.22bis training sequence includes sections which allow the modems to determine
+if the far modem can support (or is willing to support) 2400bps operation. The
+modems will automatically use 2400bps if both ends are willing to use that speed,
+or 1200bps if one or both ends to not acknowledge that 2400bps is OK.
+*/
+
+#if !defined(_V22BIS_H_)
+#define _V22BIS_H_
+
+#define V22BIS_EQUALIZER_LEN    7  /* this much to the left and this much to the right */
+#define V22BIS_EQUALIZER_MASK   15 /* one less than a power of 2 >= (2*V22BIS_EQUALIZER_LEN + 1) */
+
+#define V22BIS_TX_FILTER_STEPS  9
+
+#define V22BIS_RX_FILTER_STEPS  37
+
+/*!
+    V.22bis modem descriptor. This defines the working state for a single instance
+    of a V.22bis modem.
+*/
+typedef struct
+{
+    /*! \brief The bit rate of the modem. Valid values are 1200 and 2400. */
+    int bit_rate;
+    /*! \brief TRUE is this is the calling side modem. */
+    int caller;
+    /*! \brief The callback function used to put each bit received. */
+    put_bit_func_t put_bit;
+    /*! \brief The callback function used to get the next bit to be transmitted. */
+    get_bit_func_t get_bit;
+    /*! \brief A user specified opaque pointer passed to the callback routines. */
+    void *user_data;
+
+    /* RECEIVE SECTION */
+    struct
+    {
+        /*! \brief The route raised cosine (RRC) pulse shaping filter buffer. */
+        float rrc_filter[2*V22BIS_RX_FILTER_STEPS];
+        /*! \brief Current offset into the RRC pulse shaping filter buffer. */
+        int rrc_filter_step;
+
+        /*! \brief The register for the data scrambler. */
+        unsigned int scramble_reg;
+        /*! \brief A counter for the number of consecutive bits of repeating pattern through
+                   the scrambler. */
+        int scrambler_pattern_count;
+
+        /*! \brief 0 if receiving user data. A training stage value during training */
+        int training;
+        /*! \brief A count of how far through the current training step we are. */
+        int training_count;
+
+        /*! \brief >0 if a signal above the minimum is present. It may or may not be a V.22bis signal. */
+        int signal_present;
+
+        /*! \brief A measure of how much mismatch there is between the real constellation,
+            and the decoded symbol positions. */
+        float training_error;
+
+        /*! \brief The current phase of the carrier (i.e. the DDS parameter). */
+        uint32_t carrier_phase;
+        /*! \brief The update rate for the phase of the carrier (i.e. the DDS increment). */
+        int32_t carrier_phase_rate;
+        /*! \brief The proportional part of the carrier tracking filter. */
+        float carrier_track_p;
+        /*! \brief The integral part of the carrier tracking filter. */
+        float carrier_track_i;
+
+        /*! \brief A callback function which may be enabled to report every symbol's
+                   constellation position. */
+        qam_report_handler_t qam_report;
+        /*! \brief A user specified opaque pointer passed to the qam_report callback
+                   routine. */
+        void *qam_user_data;
+
+        /*! \brief A power meter, to measure the HPF'ed signal power in the channel. */    
+        power_meter_t rx_power;
+        /*! \brief The power meter level at which carrier on is declared. */
+        int32_t carrier_on_power;
+        /*! \brief The power meter level at which carrier off is declared. */
+        int32_t carrier_off_power;
+        /*! \brief The scaling factor accessed by the AGC algorithm. */
+        float agc_scaling;
+    
+        int constellation_state;
+
+        /*! \brief The current delta factor for updating the equalizer coefficients. */
+        float eq_delta;
+#if defined(SPANDSP_USE_FIXED_POINTx)
+        /*! \brief The adaptive equalizer coefficients. */
+        complexi_t eq_coeff[2*V22BIS_EQUALIZER_LEN + 1];
+        /*! \brief The equalizer signal buffer. */
+        complexi_t eq_buf[V22BIS_EQUALIZER_MASK + 1];
+#else
+        complexf_t eq_coeff[2*V22BIS_EQUALIZER_LEN + 1];
+        complexf_t eq_buf[V22BIS_EQUALIZER_MASK + 1];
+#endif
+        /*! \brief Current offset into the equalizer buffer. */
+        int eq_step;
+        /*! \brief Current write offset into the equalizer buffer. */
+        int eq_put_step;
+
+        /*! \brief Integration variable for damping the Gardner algorithm tests. */
+        int gardner_integrate;
+        /*! \brief Current step size of Gardner algorithm integration. */
+        int gardner_step;
+        /*! \brief The total symbol timing correction since the carrier came up.
+                   This is only for performance analysis purposes. */
+        int total_baud_timing_correction;
+        /*! \brief The current fractional phase of the baud timing. */
+        int baud_phase;
+    
+        int sixteen_way_decisions;
+    } rx;
+
+    /* TRANSMIT SECTION */
+    struct
+    {
+        /*! \brief The gain factor needed to achieve the specified output power. */
+        float gain;
+
+        /*! \brief The route raised cosine (RRC) pulse shaping filter buffer. */
+        complexf_t rrc_filter[2*V22BIS_TX_FILTER_STEPS];
+        /*! \brief Current offset into the RRC pulse shaping filter buffer. */
+        int rrc_filter_step;
+
+        /*! \brief The register for the data scrambler. */
+        unsigned int scramble_reg;
+        /*! \brief A counter for the number of consecutive bits of repeating pattern through
+                   the scrambler. */
+        int scrambler_pattern_count;
+
+        /*! \brief 0 if transmitting user data. A training stage value during training */
+        int training;
+        /*! \brief A counter used to track progress through sending the training sequence. */
+        int training_count;
+        /*! \brief The current phase of the carrier (i.e. the DDS parameter). */
+        uint32_t carrier_phase;
+        /*! \brief The update rate for the phase of the carrier (i.e. the DDS increment). */
+        int32_t carrier_phase_rate;
+        /*! \brief The current phase of the guard tone (i.e. the DDS parameter). */
+        uint32_t guard_phase;
+        /*! \brief The update rate for the phase of the guard tone (i.e. the DDS increment). */
+        int32_t guard_phase_rate;
+        float guard_level;
+        /*! \brief The current fractional phase of the baud timing. */
+        int baud_phase;
+        /*! \brief The code number for the current position in the constellation. */
+        int constellation_state;
+        /*! \brief An indicator to mark that we are tidying up to stop transmission. */
+        int shutdown;
+        /*! \brief The get_bit function in use at any instant. */
+        get_bit_func_t current_get_bit;
+    } tx;
+
+    int detected_unscrambled_ones;
+    int detected_unscrambled_zeros;
+
+    int detected_unscrambled_ones_or_zeros;
+    int detected_unscrambled_0011_ending;
+    int detected_scrambled_ones_or_zeros_at_1200bps;
+    int detected_scrambled_ones_at_2400bps;
+
+    /*! \brief Error and flow logging control */
+    logging_state_t logging;
+} v22bis_state_t;
+
+extern const complexf_t v22bis_constellation[16];
+
+#if defined(__cplusplus)
+extern "C"
+{
+#endif
+
+/*! Reinitialise an existing V.22bis modem receive context.
+    \brief Reinitialise an existing V.22bis modem receive context.
+    \param s The modem context.
+    \param bit_rate The bit rate of the modem. Valid values are 1200 and 2400.
+    \return 0 for OK, -1 for bad parameter */
+int v22bis_rx_restart(v22bis_state_t *s, int bit_rate);
+
+/*! Process a block of received V.22bis modem audio samples.
+    \brief Process a block of received V.22bis modem audio samples.
+    \param s The modem context.
+    \param amp The audio sample buffer.
+    \param len The number of samples in the buffer.
+    \return The number of samples unprocessed. */
+int v22bis_rx(v22bis_state_t *s, const int16_t amp[], int len);
+
+/*! Get a snapshot of the current equalizer coefficients.
+    \brief Get a snapshot of the current equalizer coefficients.
+    \param coeffs The vector of complex coefficients.
+    \return The number of coefficients in the vector. */
+int v22bis_equalizer_state(v22bis_state_t *s, complexf_t **coeffs);
+
+/*! Get the current received carrier frequency.
+    \param s The modem context.
+    \return The frequency, in Hertz. */
+float v22bis_rx_carrier_frequency(v22bis_state_t *s);
+
+/*! Get the current symbol timing correction since startup.
+    \param s The modem context.
+    \return The correction. */
+float v22bis_symbol_timing_correction(v22bis_state_t *s);
+
+/*! Get a current received signal power.
+    \param s The modem context.
+    \return The signal power, in dBm0. */
+float v22bis_rx_signal_power(v22bis_state_t *s);
+
+/*! Set a handler routine to process QAM status reports
+    \param s The modem context.
+    \param handler The handler routine.
+    \param user_data An opaque pointer passed to the handler routine. */
+void v22bis_set_qam_report_handler(v22bis_state_t *s, qam_report_handler_t handler, void *user_data);
+
+/*! Generate a block of V.22bis modem audio samples.
+    \brief Generate a block of V.22bis modem audio samples.
+    \param s The modem context.
+    \param amp The audio sample buffer.
+    \param len The number of samples to be generated.
+    \return The number of samples actually generated. */
+int v22bis_tx(v22bis_state_t *s, int16_t amp[], int len);
+
+/*! Adjust a V.22bis modem transmit context's power output.
+    \brief Adjust a V.22bis modem transmit context's output power.
+    \param s The modem context.
+    \param power The power level, in dBm0 */
+void v22bis_tx_power(v22bis_state_t *s, float power);
+
+/*! Reinitialise an existing V.22bis modem context, so it may be reused.
+    \brief Reinitialise an existing V.22bis modem context.
+    \param s The modem context.
+    \param bit_rate The bit rate of the modem. Valid values are 1200 and 2400.
+    \return 0 for OK, -1 for bad parameter. */
+int v22bis_restart(v22bis_state_t *s, int bit_rate);
+
+/*! Initialise a V.22bis modem context. This must be called before the first
+    use of the context, to initialise its contents.
+    \brief Initialise a V.22bis modem context.
+    \param s The modem context.
+    \param bit_rate The bit rate of the modem. Valid values are 1200 and 2400.
+    \param guard The guard tone option. 0 = none, 1 = 550Hz, 2 = 1800Hz.
+    \param caller TRUE if this is the calling modem.
+    \param get_bit The callback routine used to get the data to be transmitted.
+    \param put_bit The callback routine used to get the data to be transmitted.
+    \param user_data An opaque pointer, passed in calls to the get and put routines.
+    \return A pointer to the modem context, or NULL if there was a problem. */
+v22bis_state_t *v22bis_init(v22bis_state_t *s,
+                            int bit_rate,
+                            int guard,
+                            int caller,
+                            get_bit_func_t get_bit,
+                            put_bit_func_t put_bit,
+                            void *user_data);
+
+/*! Free a V.22bis modem receive context.
+    \brief Free a V.22bis modem receive context.
+    \param s The modem context.
+    \return 0 for OK */
+int v22bis_free(v22bis_state_t *s);
+
+/*! Change the get_bit function associated with a V.22bis modem context.
+    \brief Change the get_bit function associated with a V.22bis modem context.
+    \param s The modem context.
+    \param get_bit The callback routine used to get the data to be transmitted.
+    \param user_data An opaque pointer. */
+void v22bis_set_get_bit(v22bis_state_t *s, get_bit_func_t get_bit, void *user_data);
+
+/*! Change the get_bit function associated with a V.22bis modem context.
+    \brief Change the put_bit function associated with a V.22bis modem context.
+    \param s The modem context.
+    \param put_bit The callback routine used to process the data received.
+    \param user_data An opaque pointer. */
+void v22bis_set_put_bit(v22bis_state_t *s, put_bit_func_t put_bit, void *user_data);
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/spandsp/v27ter_rx.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/spandsp/v27ter_rx.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,276 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * v27ter_rx.h - ITU V.27ter modem receive part
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2003 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: v27ter_rx.h,v 1.47 2008/07/16 14:23:48 steveu Exp $
+ */
+
+/*! \file */
+
+#if !defined(_V27TER_RX_H_)
+#define _V27TER_RX_H_
+
+/*! \page v27ter_rx_page The V.27ter receiver
+
+\section v27ter_rx_page_sec_1 What does it do?
+The V.27ter receiver implements the receive side of a V.27ter modem. This can operate
+at data rates of 4800 and 2400 bits/s. The audio input is a stream of 16 bit samples,
+at 8000 samples/second. The transmit and receive side of V.27ter modems operate
+independantly. V.27ter is mostly used for FAX transmission, where it provides the
+standard 4800 bits/s rate (the 2400 bits/s mode is not used for FAX). 
+
+\section v27ter_rx_page_sec_2 How does it work?
+V.27ter defines two modes of operation. One uses 8-PSK at 1600 baud, giving 4800bps.
+The other uses 4-PSK at 1200 baud, giving 2400bps. A training sequence is specified
+at the start of transmission, which makes the design of a V.27ter receiver relatively
+straightforward.
+*/
+
+/* Target length for the equalizer is about 43 taps for 4800bps and 32 taps for 2400bps
+   to deal with the worst stuff in V.56bis. */
+#define V27TER_EQUALIZER_PRE_LEN        15  /* this much before the real event */
+#define V27TER_EQUALIZER_POST_LEN       15  /* this much after the real event */
+#define V27TER_EQUALIZER_MASK           63  /* one less than a power of 2 >= (V27TER_EQUALIZER_PRE_LEN + 1 + V27TER_EQUALIZER_POST_LEN) */
+
+#define V27TER_RX_4800_FILTER_STEPS     27
+#define V27TER_RX_2400_FILTER_STEPS     27
+
+#if V27TER_RX_4800_FILTER_STEPS > V27TER_RX_2400_FILTER_STEPS
+#define V27TER_RX_FILTER_STEPS V27TER_RX_4800_FILTER_STEPS
+#else
+#define V27TER_RX_FILTER_STEPS V27TER_RX_2400_FILTER_STEPS
+#endif
+
+/*!
+    V.27ter modem receive side descriptor. This defines the working state for a
+    single instance of a V.27ter modem receiver.
+*/
+typedef struct
+{
+    /*! \brief The bit rate of the modem. Valid values are 2400 and 4800. */
+    int bit_rate;
+    /*! \brief The callback function used to put each bit received. */
+    put_bit_func_t put_bit;
+    /*! \brief A user specified opaque pointer passed to the put_bit routine. */
+    void *put_bit_user_data;
+
+    /*! \brief The callback function used to report modem status changes. */
+    modem_rx_status_func_t status_handler;
+    /*! \brief A user specified opaque pointer passed to the status function. */
+    void *status_user_data;
+
+    /*! \brief A callback function which may be enabled to report every symbol's
+               constellation position. */
+    qam_report_handler_t qam_report;
+    /*! \brief A user specified opaque pointer passed to the qam_report callback
+               routine. */
+    void *qam_user_data;
+
+    /*! \brief The route raised cosine (RRC) pulse shaping filter buffer. */
+#if defined(SPANDSP_USE_FIXED_POINT)
+    int16_t rrc_filter[2*V27TER_RX_FILTER_STEPS];
+#else
+    float rrc_filter[2*V27TER_RX_FILTER_STEPS];
+#endif
+    /*! \brief Current offset into the RRC pulse shaping filter buffer. */
+    int rrc_filter_step;
+
+    /*! \brief The register for the training and data scrambler. */
+    unsigned int scramble_reg;
+    /*! \brief A counter for the number of consecutive bits of repeating pattern through
+               the scrambler. */
+    int scrambler_pattern_count;
+    /*! \brief The section of the training data we are currently in. */
+    int training_stage;
+    /*! \brief The current step in the table of BC constellation positions. */
+    int training_bc;
+    /*! \brief A count of how far through the current training step we are. */
+    int training_count;
+    /*! \brief A measure of how much mismatch there is between the real constellation,
+        and the decoded symbol positions. */
+    float training_error;
+    /*! \brief The value of the last signal sample, using the a simple HPF for signal power estimation. */
+    int16_t last_sample;
+    /*! \brief >0 if a signal above the minimum is present. It may or may not be a V.27ter signal. */
+    int signal_present;
+    /*! \brief Whether or not a carrier drop was detected and the signal delivery is pending. */
+    int carrier_drop_pending;
+    /*! \brief A count of the current consecutive samples below the carrier off threshold. */
+    int low_samples;
+    /*! \brief A highest magnitude sample seen. */
+    int16_t high_sample;
+    /*! \brief TRUE if the previous trained values are to be reused. */
+    int old_train;
+
+    /*! \brief The current phase of the carrier (i.e. the DDS parameter). */
+    uint32_t carrier_phase;
+    /*! \brief The update rate for the phase of the carrier (i.e. the DDS increment). */
+    int32_t carrier_phase_rate;
+    /*! \brief The carrier update rate saved for reuse when using short training. */
+    int32_t carrier_phase_rate_save;
+    /*! \brief The proportional part of the carrier tracking filter. */
+    float carrier_track_p;
+    /*! \brief The integral part of the carrier tracking filter. */
+    float carrier_track_i;
+
+    /*! \brief A power meter, to measure the HPF'ed signal power in the channel. */    
+    power_meter_t power;
+    /*! \brief The power meter level at which carrier on is declared. */
+    int32_t carrier_on_power;
+    /*! \brief The power meter level at which carrier off is declared. */
+    int32_t carrier_off_power;
+    /*! \brief The scaling factor accessed by the AGC algorithm. */
+    float agc_scaling;
+    /*! \brief The previous value of agc_scaling, needed to reuse old training. */
+    float agc_scaling_save;
+
+    int constellation_state;
+
+    /*! \brief The current delta factor for updating the equalizer coefficients. */
+    float eq_delta;
+#if defined(SPANDSP_USE_FIXED_POINTx)
+    /*! \brief The adaptive equalizer coefficients. */
+    complexi_t eq_coeff[V27TER_EQUALIZER_PRE_LEN + 1 + V27TER_EQUALIZER_POST_LEN];
+    /*! \brief A saved set of adaptive equalizer coefficients for use after restarts. */
+    complexi_t eq_coeff_save[V27TER_EQUALIZER_PRE_LEN + 1 + V27TER_EQUALIZER_POST_LEN];
+    /*! \brief The equalizer signal buffer. */
+    complexi_t eq_buf[V27TER_EQUALIZER_MASK + 1];
+#else
+    complexf_t eq_coeff[V27TER_EQUALIZER_PRE_LEN + 1 + V27TER_EQUALIZER_POST_LEN];
+    complexf_t eq_coeff_save[V27TER_EQUALIZER_PRE_LEN + 1 + V27TER_EQUALIZER_POST_LEN];
+    complexf_t eq_buf[V27TER_EQUALIZER_MASK + 1];
+#endif
+    /*! \brief Current offset into the equalizer buffer. */
+    int eq_step;
+    /*! \brief Current write offset into the equalizer buffer. */
+    int eq_put_step;
+    /*! \brief Symbol counter to the next equalizer update. */
+    int eq_skip;
+
+    /*! \brief Integration variable for damping the Gardner algorithm tests. */
+    int gardner_integrate;
+    /*! \brief Current step size of Gardner algorithm integration. */
+    int gardner_step;
+    /*! \brief The total symbol timing correction since the carrier came up.
+               This is only for performance analysis purposes. */
+    int total_baud_timing_correction;
+    /*! \brief The current fractional phase of the baud timing. */
+    int baud_phase;
+
+    /*! \brief Starting phase angles for the coarse carrier aquisition step. */
+    int32_t start_angles[2];
+    /*! \brief History list of phase angles for the coarse carrier aquisition step. */
+    int32_t angles[16];
+    /*! \brief Error and flow logging control */
+    logging_state_t logging;
+} v27ter_rx_state_t;
+
+#if defined(__cplusplus)
+extern "C"
+{
+#endif
+
+/*! Initialise a V.27ter modem receive context.
+    \brief Initialise a V.27ter modem receive context.
+    \param s The modem context.
+    \param bit_rate The bit rate of the modem. Valid values are 2400 and 4800.
+    \param put_bit The callback routine used to put the received data.
+    \param user_data An opaque pointer passed to the put_bit routine.
+    \return A pointer to the modem context, or NULL if there was a problem. */
+v27ter_rx_state_t *v27ter_rx_init(v27ter_rx_state_t *s, int bit_rate, put_bit_func_t put_bit, void *user_data);
+
+/*! Reinitialise an existing V.27ter modem receive context.
+    \brief Reinitialise an existing V.27ter modem receive context.
+    \param s The modem context.
+    \param bit_rate The bit rate of the modem. Valid values are 2400 and 4800.
+    \param old_train TRUE if a previous trained values are to be reused.
+    \return 0 for OK, -1 for bad parameter */
+int v27ter_rx_restart(v27ter_rx_state_t *s, int bit_rate, int old_train);
+
+/*! Free a V.27ter modem receive context.
+    \brief Free a V.27ter modem receive context.
+    \param s The modem context.
+    \return 0 for OK */
+int v27ter_rx_free(v27ter_rx_state_t *s);
+
+/*! Change the put_bit function associated with a V.27ter modem receive context.
+    \brief Change the put_bit function associated with a V.27ter modem receive context.
+    \param s The modem context.
+    \param put_bit The callback routine used to handle received bits.
+    \param user_data An opaque pointer. */
+void v27ter_rx_set_put_bit(v27ter_rx_state_t *s, put_bit_func_t put_bit, void *user_data);
+
+/*! Change the modem status report function associated with a V.27ter modem receive context.
+    \brief Change the modem status report function associated with a V.27ter modem receive context.
+    \param s The modem context.
+    \param handler The callback routine used to report modem status changes.
+    \param user_data An opaque pointer. */
+void v27ter_rx_set_modem_status_handler(v27ter_rx_state_t *s, modem_rx_status_func_t handler, void *user_data);
+
+/*! Process a block of received V.27ter modem audio samples.
+    \brief Process a block of received V.27ter modem audio samples.
+    \param s The modem context.
+    \param amp The audio sample buffer.
+    \param len The number of samples in the buffer.
+    \return The number of samples unprocessed.
+*/
+int v27ter_rx(v27ter_rx_state_t *s, const int16_t amp[], int len);
+
+/*! Get a snapshot of the current equalizer coefficients.
+    \brief Get a snapshot of the current equalizer coefficients.
+    \param coeffs The vector of complex coefficients.
+    \return The number of coefficients in the vector. */
+int v27ter_rx_equalizer_state(v27ter_rx_state_t *s, complexf_t **coeffs);
+
+/*! Get the current received carrier frequency.
+    \param s The modem context.
+    \return The frequency, in Hertz. */
+float v27ter_rx_carrier_frequency(v27ter_rx_state_t *s);
+
+/*! Get the current symbol timing correction since startup.
+    \param s The modem context.
+    \return The correction. */
+float v27ter_rx_symbol_timing_correction(v27ter_rx_state_t *s);
+
+/*! Get a current received signal power.
+    \param s The modem context.
+    \return The signal power, in dBm0. */
+float v27ter_rx_signal_power(v27ter_rx_state_t *s);
+
+/*! Set the power level at which the carrier detection will cut in
+    \param s The modem context.
+    \param cutoff The signal cutoff power, in dBm0. */
+void v27ter_rx_signal_cutoff(v27ter_rx_state_t *s, float cutoff);
+
+/*! Set a handler routine to process QAM status reports
+    \param s The modem context.
+    \param handler The handler routine.
+    \param user_data An opaque pointer passed to the handler routine. */
+void v27ter_rx_set_qam_report_handler(v27ter_rx_state_t *s, qam_report_handler_t handler, void *user_data);
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/spandsp/v27ter_tx.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/spandsp/v27ter_tx.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,195 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * v27ter_tx.h - ITU V.27ter modem transmit part
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2003 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: v27ter_tx.h,v 1.36 2008/07/16 14:23:48 steveu Exp $
+ */
+
+/*! \file */
+
+#if !defined(_SPANDSP_V27TER_TX_H_)
+#define _SPANDSP_V27TER_TX_H_
+
+/*! \page v27ter_tx_page The V.27ter transmitter
+\section v27ter_tx_page_sec_1 What does it do?
+The V.27ter transmitter implements the transmit side of a V.27ter modem. This
+can operate at data rates of 4800 and 2400 bits/s. The audio output is a stream
+of 16 bit samples, at 8000 samples/second. The transmit and receive side of
+V.27ter modems operate independantly. V.27ter is used for FAX transmission,
+where it provides the standard 4800 and 2400 bits/s rates. 
+
+\section v27ter_tx_page_sec_2 How does it work?
+V.27ter uses DPSK modulation. A common method of producing a DPSK modulated
+signal is to use a sampling rate which is a multiple of the baud rate. The raw
+signal is then a series of complex pulses, each an integer number of samples
+long. These can be shaped, using a suitable complex filter, and multiplied by a
+complex carrier signal to produce the final DPSK signal for transmission. 
+
+The pulse shaping filter for V.27ter is defined in the spec. It is a root raised
+cosine filter with 50% excess bandwidth. 
+
+The sampling rate for our transmitter is defined by the channel - 8000 samples/s.
+This is a multiple of the baud rate at 4800 bits/s (8-PSK at 1600 baud, 5 samples per
+symbol), but not at 2400 bits/s (4-PSK at 1200 baud, 20/3 samples per symbol). The baud
+interval is actually 20/3 sample periods at 2400bis/s. A symmetric FIR is used to
+apply root raised cosine filtering in the 4800bits/s mode. In the 2400bits/s mode
+a polyphase FIR filter is used. This consists of 20 sets of coefficients, offering
+zero to 19/20ths of a baud phase shift as well as root raised cosine filtering.
+The appropriate coefficient set is chosen for each signal sample generated.
+
+The carrier is generated using the DDS method. Using 2 second order resonators,
+started in quadrature, might be more efficient, as it would have less impact on
+the processor cache than a table lookup approach. However, the DDS approach
+suits the receiver better, so then same signal generator is also used for the
+transmitter.
+*/
+
+#define V27TER_TX_FILTER_STEPS      9
+
+/*!
+    V.27ter modem transmit side descriptor. This defines the working state for a
+    single instance of a V.27ter modem transmitter.
+*/
+typedef struct
+{
+    /*! \brief The bit rate of the modem. Valid values are 2400 and 4800. */
+    int bit_rate;
+    /*! \brief The callback function used to get the next bit to be transmitted. */
+    get_bit_func_t get_bit;
+    /*! \brief A user specified opaque pointer passed to the get_bit function. */
+    void *get_bit_user_data;
+
+    /*! \brief The callback function used to report modem status changes. */
+    modem_tx_status_func_t status_handler;
+    /*! \brief A user specified opaque pointer passed to the status function. */
+    void *status_user_data;
+
+#if defined(SPANDSP_USE_FIXED_POINT)
+    /*! \brief The gain factor needed to achieve the specified output power at 2400bps. */
+    int32_t gain_2400;
+    /*! \brief The gain factor needed to achieve the specified output power at 4800bps. */
+    int32_t gain_4800;
+#else
+    /*! \brief The gain factor needed to achieve the specified output power at 2400bps. */
+    float gain_2400;
+    /*! \brief The gain factor needed to achieve the specified output power at 4800bps. */
+    float gain_4800;
+#endif
+    /*! \brief The route raised cosine (RRC) pulse shaping filter buffer. */
+#if defined(SPANDSP_USE_FIXED_POINT)
+    complexi16_t rrc_filter[2*V27TER_TX_FILTER_STEPS];
+#else
+    complexf_t rrc_filter[2*V27TER_TX_FILTER_STEPS];
+#endif
+    /*! \brief Current offset into the RRC pulse shaping filter buffer. */
+    int rrc_filter_step;
+    
+    /*! \brief The register for the training and data scrambler. */
+    unsigned int scramble_reg;
+    /*! \brief A counter for the number of consecutive bits of repeating pattern through
+               the scrambler. */
+    int scrambler_pattern_count;
+    /*! \brief TRUE if transmitting the training sequence, or shutting down transmission.
+               FALSE if transmitting user data. */
+    int in_training;
+    /*! \brief A counter used to track progress through sending the training sequence. */
+    int training_step;
+
+    /*! \brief The current phase of the carrier (i.e. the DDS parameter). */
+    uint32_t carrier_phase;
+    /*! \brief The update rate for the phase of the carrier (i.e. the DDS increment). */
+    int32_t carrier_phase_rate;
+    /*! \brief The current fractional phase of the baud timing. */
+    int baud_phase;
+    /*! \brief The code number for the current position in the constellation. */
+    int constellation_state;
+    /*! \brief The get_bit function in use at any instant. */
+    get_bit_func_t current_get_bit;
+    /*! \brief Error and flow logging control */
+    logging_state_t logging;
+} v27ter_tx_state_t;
+
+#if defined(__cplusplus)
+extern "C"
+{
+#endif
+
+/*! Adjust a V.27ter modem transmit context's power output.
+    \brief Adjust a V.27ter modem transmit context's output power.
+    \param s The modem context.
+    \param power The power level, in dBm0 */
+void v27ter_tx_power(v27ter_tx_state_t *s, float power);
+
+/*! Initialise a V.27ter modem transmit context.
+    \brief Initialise a V.27ter modem transmit context.
+    \param s The modem context.
+    \param bit_rate The bit rate of the modem. Valid values are 2400 and 4800.
+    \param tep TRUE is the optional TEP tone is to be transmitted.
+    \param get_bit The callback routine used to get the data to be transmitted.
+    \param user_data An opaque pointer.
+    \return A pointer to the modem context, or NULL if there was a problem. */
+v27ter_tx_state_t *v27ter_tx_init(v27ter_tx_state_t *s, int bit_rate, int tep, get_bit_func_t get_bit, void *user_data);
+
+/*! Reinitialise an existing V.27ter modem transmit context, so it may be reused.
+    \brief Reinitialise an existing V.27ter modem transmit context.
+    \param s The modem context.
+    \param bit_rate The bit rate of the modem. Valid values are 2400 and 4800.
+    \param tep TRUE is the optional TEP tone is to be transmitted.
+    \return 0 for OK, -1 for bad parameter */
+int v27ter_tx_restart(v27ter_tx_state_t *s, int bit_rate, int tep);
+
+/*! Free a V.27ter modem transmit context.
+    \brief Free a V.27ter modem transmit context.
+    \param s The modem context.
+    \return 0 for OK */
+int v27ter_tx_free(v27ter_tx_state_t *s);
+
+/*! Change the get_bit function associated with a V.27ter modem transmit context.
+    \brief Change the get_bit function associated with a V.27ter modem transmit context.
+    \param s The modem context.
+    \param get_bit The callback routine used to get the data to be transmitted.
+    \param user_data An opaque pointer. */
+void v27ter_tx_set_get_bit(v27ter_tx_state_t *s, get_bit_func_t get_bit, void *user_data);
+
+/*! Change the modem status report function associated with a V.27ter modem transmit context.
+    \brief Change the modem status report function associated with a V.27ter modem transmit context.
+    \param s The modem context.
+    \param handler The callback routine used to report modem status changes.
+    \param user_data An opaque pointer. */
+void v27ter_tx_set_modem_status_handler(v27ter_tx_state_t *s, modem_tx_status_func_t handler, void *user_data);
+
+/*! Generate a block of V.27ter modem audio samples.
+    \brief Generate a block of V.27ter modem audio samples.
+    \param s The modem context.
+    \param amp The audio sample buffer.
+    \param len The number of samples to be generated.
+    \return The number of samples actually generated.
+*/
+int v27ter_tx(v27ter_tx_state_t *s, int16_t amp[], int len);
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/spandsp/v29rx.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/spandsp/v29rx.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,360 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * v29rx.h - ITU V.29 modem receive part
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2003 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: v29rx.h,v 1.55 2008/07/16 14:23:48 steveu Exp $
+ */
+
+/*! \file */
+
+#if !defined(_V29RX_H_)
+#define _V29RX_H_
+
+/*! \page v29rx_page The V.29 receiver
+\section v29rx_page_sec_1 What does it do?
+The V.29 receiver implements the receive side of a V.29 modem. This can operate
+at data rates of 9600, 7200 and 4800 bits/s. The audio input is a stream of 16
+bit samples, at 8000 samples/second. The transmit and receive side of V.29
+modems operate independantly. V.29 is mostly used for FAX transmission, where it
+provides the standard 9600 and 7200 bits/s rates (the 4800 bits/s mode is not
+used for FAX). 
+
+\section v29rx_page_sec_2 How does it work?
+V.29 operates at 2400 baud for all three bit rates. It uses 16-QAM modulation for
+9600bps, 8-QAM for 7200bps, and 4-PSK for 4800bps. A training sequence is specified
+at the start of transmission, which makes the design of a V.29 receiver relatively
+straightforward.
+
+The first stage of the training sequence consists of 128
+symbols, alternating between two constellation positions. The receiver monitors
+the signal power, to sense the possible presence of a valid carrier. When the
+alternating signal begins, the power rising above a minimum threshold (-26dBm0)
+causes the main receiver computation to begin. The initial measured power is
+used to quickly set the gain of the receiver. After this initial settling, the
+front end gain is locked, and the adaptive equalizer tracks any subsequent
+signal level variation. The signal is oversampled to 24000 samples/second (i.e.
+signal, zero, zero, signal, zero, zero, ...) and fed to a complex root raised
+cosine pulse shaping filter. This filter has been modified from the conventional
+root raised cosine filter, by shifting it up the band, to be centred at the nominal
+carrier frequency. This filter interpolates the samples, pulse shapes, and performs
+a fractional sample delay at the same time. 48 sets of filter coefficients are used to
+achieve a set of finely spaces fractional sample delays, between zero and
+one sample. By choosing every fifth sample, and the appropriate set of filter
+coefficients, the properly tuned symbol tracker can select data samples at 4800
+samples/second from points within 1.125 degrees of the centre and mid-points of
+each symbol. The output of the filter is multiplied by a complex carrier, generated
+by a DDS. The result is a baseband signal, requiring no further filtering, apart from
+an adaptive equalizer. The baseband signal is fed to a T/2 adaptive equalizer.
+A band edge component maximisation algorithm is used to tune the sampling, so the samples
+fed to the equalizer are close to the mid point and edges of each symbol. Initially
+the algorithm is very lightly damped, to ensure the symbol alignment pulls in
+quickly. Because the sampling rate will not be precisely the same as the
+transmitter's (the spec. says the symbol timing should be within 0.01%), the
+receiver constantly evaluates and corrects this sampling throughout its
+operation. During the symbol timing maintainence phase, the algorithm uses
+a heavier damping.
+
+The carrier is specified as 1700Hz +-1Hz at the transmitter, and 1700 +-7Hz at
+the receiver. The receive carrier would only be this inaccurate if the link
+includes FDM sections. These are being phased out, but the design must still
+allow for the worst case. Using an initial 1700Hz signal for demodulation gives
+a worst case rotation rate for the constellation of about one degree per symbol.
+Once the symbol timing synchronisation algorithm has been given time to lock to
+the symbol timing of the initial alternating pattern, the phase of the demodulated
+signal is recorded on two successive symbols - once for each of the constellation
+positions. The receiver then tracks the symbol alternations, until a large phase jump
+occurs. This signifies the start of the next phase of the training sequence. At this
+point the total phase shift between the original recorded symbol phase, and the
+symbol phase just before the phase jump occurred is used to provide a coarse
+estimation of the rotation rate of the constellation, and it current absolute
+angle of rotation. These are used to update the current carrier phase and phase
+update rate in the carrier DDS. The working data already in the pulse shaping
+filter and equalizer buffers is given a similar step rotation to pull it all
+into line. From this point on, a heavily damped integrate and dump approach,
+based on the angular difference between each received constellation position and
+its expected position, is sufficient to track the carrier, and maintain phase
+alignment. A fast rough approximator for the arc-tangent function is adequate
+for the estimation of the angular error. 
+
+The next phase of the training sequence is a scrambled sequence of two
+particular symbols. We train the T/2 adaptive equalizer using this sequence. The
+scrambling makes the signal sufficiently diverse to ensure the equalizer
+converges to the proper generalised solution. At the end of this sequence, the
+equalizer should be sufficiently well adapted that is can correctly resolve the
+full QAM constellation. However, the equalizer continues to adapt throughout
+operation of the modem, fine tuning on the more complex data patterns of the
+full QAM constellation. 
+
+In the last phase of the training sequence, the modem enters normal data
+operation, with a short defined period of all ones as data. As in most high
+speed modems, data in a V.29 modem passes through a scrambler, to whiten the
+spectrum of the signal. The transmitter should initialise its data scrambler,
+and pass the ones through it. At the end of the ones, real data begins to pass
+through the scrambler, and the transmit modem is in normal operation. The
+receiver tests that ones are really received, in order to verify the modem
+trained correctly. If all is well, the data following the ones is fed to the
+application, and the receive modem is up and running. Unfortunately, some
+transmit side of some real V.29 modems fail to initialise their scrambler before
+sending the ones. This means the first 23 received bits (the length of the
+scrambler register) cannot be trusted for the test. The receive modem,
+therefore, only tests that bits starting at bit 24 are really ones. 
+*/
+
+/* Target length for the equalizer is about 63 taps, to deal with the worst stuff
+   in V.56bis. */
+#define V29_EQUALIZER_PRE_LEN   15  /* this much before the real event */
+#define V29_EQUALIZER_POST_LEN  15  /* this much after the real event */
+#define V29_EQUALIZER_MASK      63  /* one less than a power of 2 >= (2*V29_EQUALIZER_LEN + 1) */
+
+#define V29_RX_FILTER_STEPS     27
+
+typedef void (*qam_report_handler_t)(void *user_data, const complexf_t *constel, const complexf_t *target, int symbol);
+
+/*!
+    V.29 modem receive side descriptor. This defines the working state for a
+    single instance of a V.29 modem receiver.
+*/
+typedef struct
+{
+    /*! \brief The bit rate of the modem. Valid values are 4800, 7200 and 9600. */
+    int bit_rate;
+    /*! \brief The callback function used to put each bit received. */
+    put_bit_func_t put_bit;
+    /*! \brief A user specified opaque pointer passed to the put_bit routine. */
+    void *put_bit_user_data;
+
+    /*! \brief The callback function used to report modem status changes. */
+    modem_rx_status_func_t status_handler;
+    /*! \brief A user specified opaque pointer passed to the status function. */
+    void *status_user_data;
+
+    /*! \brief A callback function which may be enabled to report every symbol's
+               constellation position. */
+    qam_report_handler_t qam_report;
+    /*! \brief A user specified opaque pointer passed to the qam_report callback
+               routine. */
+    void *qam_user_data;
+
+    /*! \brief The route raised cosine (RRC) pulse shaping filter buffer. */
+#if defined(SPANDSP_USE_FIXED_POINT)
+    int16_t rrc_filter[2*V29_RX_FILTER_STEPS];
+#else
+    float rrc_filter[2*V29_RX_FILTER_STEPS];
+#endif
+    /*! \brief Current offset into the RRC pulse shaping filter buffer. */
+    int rrc_filter_step;
+
+    /*! \brief The register for the data scrambler. */
+    unsigned int scramble_reg;
+    /*! \brief The register for the training scrambler. */
+    uint8_t training_scramble_reg;
+    /*! \brief The section of the training data we are currently in. */
+    int training_stage;
+    /*! \brief The current step in the table of CD constellation positions. */
+    int training_cd;
+    /*! \brief A count of how far through the current training step we are. */
+    int training_count;
+    /*! \brief A measure of how much mismatch there is between the real constellation,
+        and the decoded symbol positions. */
+    float training_error;
+    /*! \brief The value of the last signal sample, using the a simple HPF for signal power estimation. */
+    int16_t last_sample;
+    /*! \brief >0 if a signal above the minimum is present. It may or may not be a V.29 signal. */
+    int signal_present;
+    /*! \brief Whether or not a carrier drop was detected and the signal delivery is pending. */
+    int carrier_drop_pending;
+    /*! \brief A count of the current consecutive samples below the carrier off threshold. */
+    int low_samples;
+    /*! \brief A highest magnitude sample seen. */
+    int16_t high_sample;
+    /*! \brief TRUE if the previous trained values are to be reused. */
+    int old_train;
+
+    /*! \brief The current phase of the carrier (i.e. the DDS parameter). */
+    uint32_t carrier_phase;
+    /*! \brief The update rate for the phase of the carrier (i.e. the DDS increment). */
+    int32_t carrier_phase_rate;
+    /*! \brief The carrier update rate saved for reuse when using short training. */
+    int32_t carrier_phase_rate_save;
+    /*! \brief The proportional part of the carrier tracking filter. */
+    float carrier_track_p;
+    /*! \brief The integral part of the carrier tracking filter. */
+    float carrier_track_i;
+
+    /*! \brief A power meter, to measure the HPF'ed signal power in the channel. */    
+    power_meter_t power;
+    /*! \brief The power meter level at which carrier on is declared. */
+    int32_t carrier_on_power;
+    /*! \brief The power meter level at which carrier off is declared. */
+    int32_t carrier_off_power;
+    /*! \brief The scaling factor accessed by the AGC algorithm. */
+    float agc_scaling;
+    /*! \brief The previous value of agc_scaling, needed to reuse old training. */
+    float agc_scaling_save;
+
+    int constellation_state;
+
+    /*! \brief The current delta factor for updating the equalizer coefficients. */
+    float eq_delta;
+#if defined(SPANDSP_USE_FIXED_POINTx)
+    /*! \brief The adaptive equalizer coefficients. */
+    complexi_t eq_coeff[V29_EQUALIZER_PRE_LEN + 1 + V29_EQUALIZER_POST_LEN];
+    /*! \brief A saved set of adaptive equalizer coefficients for use after restarts. */
+    complexi_t eq_coeff_save[V29_EQUALIZER_PRE_LEN + 1 + V29_EQUALIZER_POST_LEN];
+    /*! \brief The equalizer signal buffer. */
+    complexi_t eq_buf[V29_EQUALIZER_MASK + 1];
+#else
+    complexf_t eq_coeff[V29_EQUALIZER_PRE_LEN + 1 + V29_EQUALIZER_POST_LEN];
+    complexf_t eq_coeff_save[V29_EQUALIZER_PRE_LEN + 1 + V29_EQUALIZER_POST_LEN];
+    complexf_t eq_buf[V29_EQUALIZER_MASK + 1];
+#endif
+    /*! \brief Current offset into the equalizer buffer. */
+    int eq_step;
+    /*! \brief Current write offset into the equalizer buffer. */
+    int eq_put_step;
+    /*! \brief Symbol counter to the next equalizer update. */
+    int eq_skip;
+
+    /*! \brief The current half of the baud. */
+    int baud_half;
+#if defined(SPANDSP_USE_FIXED_POINTx)
+    /*! Low band edge filter for symbol sync. */
+    int32_t symbol_sync_low[2];
+    /*! High band edge filter for symbol sync. */
+    int32_t symbol_sync_high[2];
+    /*! DC filter for symbol sync. */
+    int32_t symbol_sync_dc_filter[2];
+    /*! Baud phase for symbol sync. */
+    int32_t baud_phase;
+#else
+    /*! Low band edge filter for symbol sync. */
+    float symbol_sync_low[2];
+    /*! High band edge filter for symbol sync. */
+    float symbol_sync_high[2];
+    /*! DC filter for symbol sync. */
+    float symbol_sync_dc_filter[2];
+    /*! Baud phase for symbol sync. */
+    float baud_phase;
+#endif
+
+    /*! \brief The total symbol timing correction since the carrier came up.
+               This is only for performance analysis purposes. */
+    int total_baud_timing_correction;
+
+    /*! \brief Starting phase angles for the coarse carrier aquisition step. */
+    int32_t start_angles[2];
+    /*! \brief History list of phase angles for the coarse carrier aquisition step. */
+    int32_t angles[16];
+    /*! \brief Error and flow logging control */
+    logging_state_t logging;
+} v29_rx_state_t;
+
+#if defined(__cplusplus)
+extern "C"
+{
+#endif
+
+/*! Initialise a V.29 modem receive context.
+    \brief Initialise a V.29 modem receive context.
+    \param s The modem context.
+    \param bit_rate The bit rate of the modem. Valid values are 4800, 7200 and 9600.
+    \param put_bit The callback routine used to put the received data.
+    \param user_data An opaque pointer passed to the put_bit routine.
+    \return A pointer to the modem context, or NULL if there was a problem. */
+v29_rx_state_t *v29_rx_init(v29_rx_state_t *s, int bit_rate, put_bit_func_t put_bit, void *user_data);
+
+/*! Reinitialise an existing V.29 modem receive context.
+    \brief Reinitialise an existing V.29 modem receive context.
+    \param s The modem context.
+    \param bit_rate The bit rate of the modem. Valid values are 4800, 7200 and 9600.
+    \param old_train TRUE if a previous trained values are to be reused.
+    \return 0 for OK, -1 for bad parameter */
+int v29_rx_restart(v29_rx_state_t *s, int bit_rate, int old_train);
+
+/*! Free a V.29 modem receive context.
+    \brief Free a V.29 modem receive context.
+    \param s The modem context.
+    \return 0 for OK */
+int v29_rx_free(v29_rx_state_t *s);
+
+/*! Change the put_bit function associated with a V.29 modem receive context.
+    \brief Change the put_bit function associated with a V.29 modem receive context.
+    \param s The modem context.
+    \param put_bit The callback routine used to handle received bits.
+    \param user_data An opaque pointer. */
+void v29_rx_set_put_bit(v29_rx_state_t *s, put_bit_func_t put_bit, void *user_data);
+
+/*! Change the modem status report function associated with a V.29 modem receive context.
+    \brief Change the modem status report function associated with a V.29 modem receive context.
+    \param s The modem context.
+    \param handler The callback routine used to report modem status changes.
+    \param user_data An opaque pointer. */
+void v29_rx_set_modem_status_handler(v29_rx_state_t *s, modem_rx_status_func_t handler, void *user_data);
+
+/*! Process a block of received V.29 modem audio samples.
+    \brief Process a block of received V.29 modem audio samples.
+    \param s The modem context.
+    \param amp The audio sample buffer.
+    \param len The number of samples in the buffer.
+    \return The number of samples unprocessed. */
+int v29_rx(v29_rx_state_t *s, const int16_t amp[], int len);
+
+/*! Get a snapshot of the current equalizer coefficients.
+    \brief Get a snapshot of the current equalizer coefficients.
+    \param s The modem context.
+    \param coeffs The vector of complex coefficients.
+    \return The number of coefficients in the vector. */
+int v29_rx_equalizer_state(v29_rx_state_t *s, complexf_t **coeffs);
+
+/*! Get the current received carrier frequency.
+    \param s The modem context.
+    \return The frequency, in Hertz. */
+float v29_rx_carrier_frequency(v29_rx_state_t *s);
+
+/*! Get the current symbol timing correction since startup.
+    \param s The modem context.
+    \return The correction. */
+float v29_rx_symbol_timing_correction(v29_rx_state_t *s);
+
+/*! Get the current received signal power.
+    \param s The modem context.
+    \return The signal power, in dBm0. */
+float v29_rx_signal_power(v29_rx_state_t *s);
+
+/*! Set the power level at which the carrier detection will cut in
+    \param s The modem context.
+    \param cutoff The signal cutoff power, in dBm0. */
+void v29_rx_signal_cutoff(v29_rx_state_t *s, float cutoff);
+
+/*! Set a handler routine to process QAM status reports
+    \param s The modem context.
+    \param handler The handler routine.
+    \param user_data An opaque pointer passed to the handler routine. */
+void v29_rx_set_qam_report_handler(v29_rx_state_t *s, qam_report_handler_t handler, void *user_data);
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/spandsp/v29tx.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/spandsp/v29tx.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,228 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * v29tx.h - ITU V.29 modem transmit part
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2003 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: v29tx.h,v 1.34 2008/07/16 14:23:48 steveu Exp $
+ */
+
+/*! \file */
+
+#if !defined(_SPANDSP_V29TX_H_)
+#define _SPANDSP_V29TX_H_
+
+/*! \page v29tx_page The V.29 transmitter
+\section v29tx_page_sec_1 What does it do?
+The V.29 transmitter implements the transmit side of a V.29 modem. This can
+operate at data rates of 9600, 7200 and 4800 bits/s. The audio output is a
+stream of 16 bit samples, at 8000 samples/second. The transmit and receive side
+of V.29 modems operate independantly. V.29 is mostly used for FAX transmission,
+where it provides the standard 9600 and 7200 bits/s rates (the 4800 bits/s mode
+is not used for FAX). 
+
+\section v29tx_page_sec_2 How does it work?
+V.29 uses QAM modulation. The standard method of producing a QAM modulated
+signal is to use a sampling rate which is a multiple of the baud rate. The raw
+signal is then a series of complex pulses, each an integer number of samples
+long. These can be shaped, using a suitable complex filter, and multiplied by a
+complex carrier signal to produce the final QAM signal for transmission. 
+
+The pulse shaping filter is only vaguely defined by the V.29 spec. Some of the
+other ITU modem specs. fully define the filter, typically specifying a root
+raised cosine filter, with 50% excess bandwidth. This is a pity, since it
+increases the variability of the received signal. However, the receiver's
+adaptive equalizer will compensate for these differences. The current
+design uses a root raised cosine filter with 25% excess bandwidth. Greater
+excess bandwidth will not allow the tranmitted signal to meet the spectral
+requirements.
+
+The sampling rate for our transmitter is defined by the channel - 8000 per
+second. This is not a multiple of the baud rate (i.e. 2400 baud). The baud
+interval is actually 10/3 sample periods. Instead of using a symmetric
+FIR to pulse shape the signal, a polyphase filter is used. This consists of
+10 sets of coefficients, offering zero to 9/10ths of a baud phase shift as well
+as root raised cosine filtering. The appropriate coefficient set is chosen for
+each signal sample generated.
+
+The carrier is generated using the DDS method. Using two second order resonators,
+started in quadrature, might be more efficient, as it would have less impact on
+the processor cache than a table lookup approach. However, the DDS approach
+suits the receiver better, so the same signal generator is also used for the
+transmitter. 
+
+The equation defining QAM modulation is:
+
+    s(n) = A*cos(2*pi*f*n + phi(n))
+
+where phi(n) is the phase of the information, and A is the amplitude of the information
+
+using the identity
+
+    cos(x + y) = cos(x)*cos(y) - sin(x)*sin(y)
+    
+we get
+
+    s(n) = A {cos(2*pi*f*n)*cos(phi(n)) - sin(2*pi*f*n)*sin(phi(n))}
+    
+substituting with the constellation positions
+
+    I(n) = A*cos(phi(n))
+    Q(n) = A*sin(phi(n))
+    
+gives
+
+    s(n) = I(n)*cos(2*pi*f*n) - Q(n)*sin(2*pi*f*n)
+
+*/
+
+#define V29_TX_FILTER_STEPS     9
+
+/*!
+    V.29 modem transmit side descriptor. This defines the working state for a
+    single instance of a V.29 modem transmitter.
+*/
+typedef struct
+{
+    /*! \brief The bit rate of the modem. Valid values are 4800, 7200 and 9600. */
+    int bit_rate;
+    /*! \brief The callback function used to get the next bit to be transmitted. */
+    get_bit_func_t get_bit;
+    /*! \brief A user specified opaque pointer passed to the get_bit function. */
+    void *get_bit_user_data;
+
+    /*! \brief The callback function used to report modem status changes. */
+    modem_tx_status_func_t status_handler;
+    /*! \brief A user specified opaque pointer passed to the status function. */
+    void *status_user_data;
+
+    /*! \brief Gain required to achieve the specified output power, not allowing
+               for the size of the current constellation. */
+    float base_gain;
+    /*! \brief Gain required to achieve the specified output power, allowing
+               for the size of the current constellation. */
+#if defined(SPANDSP_USE_FIXED_POINT)
+    int32_t gain;
+#else
+    float gain;
+#endif
+
+    /*! \brief The route raised cosine (RRC) pulse shaping filter buffer. */
+#if defined(SPANDSP_USE_FIXED_POINT)
+    complexi16_t rrc_filter[2*V29_TX_FILTER_STEPS];
+#else
+    complexf_t rrc_filter[2*V29_TX_FILTER_STEPS];
+#endif
+    /*! \brief Current offset into the RRC pulse shaping filter buffer. */
+    int rrc_filter_step;
+
+    /*! \brief The register for the data scrambler. */
+    unsigned int scramble_reg;
+    /*! \brief The register for the training scrambler. */
+    uint8_t training_scramble_reg;
+    /*! \brief TRUE if transmitting the training sequence, or shutting down transmission.
+               FALSE if transmitting user data. */
+    int in_training;
+    /*! \brief A counter used to track progress through sending the training sequence. */
+    int training_step;
+    /*! \brief An offset value into the table of training parameters, used to match the
+               training pattern to the bit rate. */
+    int training_offset;
+
+    /*! \brief The current phase of the carrier (i.e. the DDS parameter). */
+    uint32_t carrier_phase;
+    /*! \brief The update rate for the phase of the carrier (i.e. the DDS increment). */
+    int32_t carrier_phase_rate;
+    /*! \brief The current fractional phase of the baud timing. */
+    int baud_phase;
+    /*! \brief The code number for the current position in the constellation. */
+    int constellation_state;
+    /*! \brief The get_bit function in use at any instant. */
+    get_bit_func_t current_get_bit;
+    /*! \brief Error and flow logging control */
+    logging_state_t logging;
+} v29_tx_state_t;
+
+#if defined(__cplusplus)
+extern "C"
+{
+#endif
+
+/*! Adjust a V.29 modem transmit context's power output.
+    \brief Adjust a V.29 modem transmit context's output power.
+    \param s The modem context.
+    \param power The power level, in dBm0 */
+void v29_tx_power(v29_tx_state_t *s, float power);
+
+/*! Initialise a V.29 modem transmit context. This must be called before the first
+    use of the context, to initialise its contents.
+    \brief Initialise a V.29 modem transmit context.
+    \param s The modem context.
+    \param bit_rate The bit rate of the modem. Valid values are 4800, 7200 and 9600.
+    \param tep TRUE is the optional TEP tone is to be transmitted.
+    \param get_bit The callback routine used to get the data to be transmitted.
+    \param user_data An opaque pointer.
+    \return A pointer to the modem context, or NULL if there was a problem. */
+v29_tx_state_t *v29_tx_init(v29_tx_state_t *s, int bit_rate, int tep, get_bit_func_t get_bit, void *user_data);
+
+/*! Reinitialise an existing V.29 modem transmit context, so it may be reused.
+    \brief Reinitialise an existing V.29 modem transmit context.
+    \param s The modem context.
+    \param bit_rate The bit rate of the modem. Valid values are 4800, 7200 and 9600.
+    \param tep TRUE is the optional TEP tone is to be transmitted.
+    \return 0 for OK, -1 for bad parameter */
+int v29_tx_restart(v29_tx_state_t *s, int bit_rate, int tep);
+
+/*! Free a V.29 modem transmit context.
+    \brief Free a V.29 modem transmit context.
+    \param s The modem context.
+    \return 0 for OK */
+int v29_tx_free(v29_tx_state_t *s);
+
+/*! Change the get_bit function associated with a V.29 modem transmit context.
+    \brief Change the get_bit function associated with a V.29 modem transmit context.
+    \param s The modem context.
+    \param get_bit The callback routine used to get the data to be transmitted.
+    \param user_data An opaque pointer. */
+void v29_tx_set_get_bit(v29_tx_state_t *s, get_bit_func_t get_bit, void *user_data);
+
+/*! Change the modem status report function associated with a V.29 modem transmit context.
+    \brief Change the modem status report function associated with a V.29 modem transmit context.
+    \param s The modem context.
+    \param handler The callback routine used to report modem status changes.
+    \param user_data An opaque pointer. */
+void v29_tx_set_modem_status_handler(v29_tx_state_t *s, modem_tx_status_func_t handler, void *user_data);
+
+/*! Generate a block of V.29 modem audio samples.
+    \brief Generate a block of V.29 modem audio samples.
+    \param s The modem context.
+    \param amp The audio sample buffer.
+    \param len The number of samples to be generated.
+    \return The number of samples actually generated.
+*/
+int v29_tx(v29_tx_state_t *s, int16_t *amp, int len);
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/spandsp/v42.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/spandsp/v42.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,232 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * v42.h
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2003 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: v42.h,v 1.25 2008/04/17 14:27:01 steveu Exp $
+ */
+
+/*! \page v42_page V.42 modem error correction
+\section v42_page_sec_1 What does it do?
+The V.42 specification defines an error correcting protocol for PSTN modems, based on
+HDLC and LAP. This makes it similar to an X.25 link. A special variant of LAP, known
+as LAP-M, is defined in the V.42 specification. A means for modems to determine if the
+far modem supports V.42 is also defined.
+
+\section v42_page_sec_2 How does it work?
+*/
+
+#if !defined(_SPANDSP_V42_H_)
+#define _SPANDSP_V42_H_
+
+enum
+{
+    LAPM_DETECT = 0,
+    LAPM_ESTABLISH = 1,
+    LAPM_DATA = 2,
+    LAPM_RELEASE = 3,
+    LAPM_SIGNAL = 4,
+    LAPM_SETPARM = 5,
+    LAPM_TEST = 6,
+    LAPM_UNSUPPORTED = 7
+};
+
+typedef void (*v42_status_func_t)(void *user_data, int status);
+typedef void (*v42_frame_handler_t)(void *user_data, const uint8_t *pkt, int len);
+
+typedef struct lapm_frame_queue_s
+{
+    struct lapm_frame_queue_s *next;
+    int len;
+    uint8_t frame[];
+} lapm_frame_queue_t;
+
+/*!
+    LAP-M descriptor. This defines the working state for a single instance of LAP-M.
+*/
+typedef struct
+{
+    int handle;
+    hdlc_rx_state_t hdlc_rx;
+    hdlc_tx_state_t hdlc_tx;
+    
+    v42_frame_handler_t iframe_receive;
+    void *iframe_receive_user_data;
+
+    v42_status_func_t status_callback;
+    void *status_callback_user_data;
+
+    int state;
+    int tx_waiting;
+    int debug;
+    /*! TRUE if originator. FALSE if answerer */
+    int we_are_originator;
+    /*! Remote network type (unknown, answerer. originator) */
+    int peer_is_originator;
+    /*! Next N(S) for transmission */
+    int next_tx_frame;
+    /*! The last of our frames which the peer acknowledged */
+    int last_frame_peer_acknowledged;
+    /*! Next N(R) for reception */
+    int next_expected_frame;
+    /*! The last of the peer's frames which we acknowledged */
+    int last_frame_we_acknowledged;
+    /*! TRUE if we sent an I or S frame with the F-bit set */
+    int solicit_f_bit;
+    /*! Retransmission count */
+    int retransmissions;
+    /*! TRUE if peer is busy */
+    int busy;
+
+    /*! Acknowledgement timer */
+    int t401_timer;
+    /*! Reply delay timer - optional */
+    int t402_timer;
+    /*! Inactivity timer - optional */
+    int t403_timer;
+    /*! Maximum number of octets in an information field */
+    int n401;
+    /*! Window size */
+    int window_size_k;
+	
+    lapm_frame_queue_t *txqueue;
+    lapm_frame_queue_t *tx_next;
+    lapm_frame_queue_t *tx_last;
+    queue_state_t *tx_queue;
+    
+    span_sched_state_t sched;
+    /*! \brief Error and flow logging control */
+    logging_state_t logging;
+} lapm_state_t;
+
+/*!
+    V.42 descriptor. This defines the working state for a single instance of V.42.
+*/
+typedef struct
+{
+    /*! TRUE if we are the calling party, otherwise FALSE */
+    int caller;
+    /*! TRUE if we should detect whether the far end is V.42 capable. FALSE if we go
+        directly to protocol establishment */
+    int detect;
+
+    /*! Stage in negotiating V.42 support */
+    int rx_negotiation_step;
+    int rxbits;
+    int rxstream;
+    int rxoks;
+    int odp_seen;
+    int txbits;
+    int txstream;
+    int txadps;
+    /*! The LAP.M context */
+    lapm_state_t lapm;
+
+    /*! V.42 support detection timer */
+    int t400_timer;
+    /*! \brief Error and flow logging control */
+    logging_state_t logging;
+} v42_state_t;
+
+/*! Log the raw HDLC frames */
+#define LAPM_DEBUG_LAPM_RAW         (1 << 0)
+/*! Log the interpreted frames */
+#define LAPM_DEBUG_LAPM_DUMP        (1 << 1)
+/*! Log state machine changes */
+#define LAPM_DEBUG_LAPM_STATE 	    (1 << 2)
+
+#if defined(__cplusplus)
+extern "C"
+{
+#endif
+
+const char *lapm_status_to_str(int status);
+
+/*! Dump LAP.M frames in a raw and/or decoded forms
+    \param frame The frame itself
+    \param len The length of the frame, in octets
+    \param showraw TRUE if the raw octets should be dumped
+    \param txrx TRUE if tx, FALSE if rx. Used to highlight the packet's direction.
+*/
+void lapm_dump(lapm_state_t *s, const uint8_t *frame, int len, int showraw, int txrx);
+
+/*! Accept an HDLC packet
+*/
+void lapm_receive(void *user_data, const uint8_t *buf, int len, int ok);
+
+/*! Transmit a LAP.M frame
+*/
+int lapm_tx(lapm_state_t *s, const void *buf, int len);
+
+/*! Transmit a LAP.M information frame
+*/
+int lapm_tx_iframe(lapm_state_t *s, const void *buf, int len, int cr);
+
+/*! Send a break over a LAP.M connection
+*/
+int lapm_break(lapm_state_t *s, int enable);
+
+/*! Initiate an orderly release of a LAP.M connection
+*/
+int lapm_release(lapm_state_t *s);
+
+/*! Enable or disable loopback of a LAP.M connection
+*/
+int lapm_loopback(lapm_state_t *s, int enable);
+
+/*! Assign or remove a callback routine used to deal with V.42 status changes.
+*/
+void v42_set_status_callback(v42_state_t *s, v42_status_func_t callback, void *user_data);
+
+/*! Process a newly received bit for a V.42 context.
+*/
+void v42_rx_bit(void *user_data, int bit);
+
+/*! Get the next transmit bit for a V.42 context.
+*/
+int v42_tx_bit(void *user_data);
+
+/*! Initialise a V.42 context.
+    \param s The V.42 context.
+    \param caller TRUE if caller mode, else answerer mode.
+    \param frame_handler A callback function to handle received frames of data.
+    \param user_data An opaque pointer passed to the frame handler routine.
+    \return ???
+*/
+v42_state_t *v42_init(v42_state_t *s, int caller, int detect, v42_frame_handler_t frame_handler, void *user_data);
+
+/*! Restart a V.42 context.
+    \param s The V.42 context.
+*/
+void v42_restart(v42_state_t *s);
+
+/*! Release a V.42 context.
+    \param s The V.42 context.
+    \return 0 if OK */
+int v42_release(v42_state_t *s);
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/spandsp/v42bis.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/spandsp/v42bis.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,251 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * v42bis.h
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2005 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: v42bis.h,v 1.22 2008/04/17 14:27:01 steveu Exp $
+ */
+
+/*! \page v42bis_page V.42bis modem data compression
+\section v42bis_page_sec_1 What does it do?
+The v.42bis specification defines a data compression scheme, to work in
+conjunction with the error correction scheme defined in V.42.
+
+\section v42bis_page_sec_2 How does it work?
+*/
+
+#if !defined(_SPANDSP_V42BIS_H_)
+#define _SPANDSP_V42BIS_H_
+
+#define V42BIS_MAX_BITS         12
+#define V42BIS_MAX_CODEWORDS    4096    /* 2^V42BIS_MAX_BITS */
+#define V42BIS_TABLE_SIZE       5021    /* This should be a prime >(2^V42BIS_MAX_BITS) */
+#define V42BIS_MAX_STRING_SIZE  250
+
+enum
+{
+    V42BIS_P0_NEITHER_DIRECTION = 0,
+    V42BIS_P0_INITIATOR_RESPONDER,
+    V42BIS_P0_RESPONDER_INITIATOR,
+    V42BIS_P0_BOTH_DIRECTIONS
+};
+
+enum
+{
+    V42BIS_COMPRESSION_MODE_DYNAMIC = 0,
+    V42BIS_COMPRESSION_MODE_ALWAYS,
+    V42BIS_COMPRESSION_MODE_NEVER
+};
+
+typedef void (*v42bis_frame_handler_t)(void *user_data, const uint8_t *pkt, int len);
+typedef void (*v42bis_data_handler_t)(void *user_data, const uint8_t *buf, int len);
+
+/*!
+    V.42bis dictionary node.
+*/
+typedef struct
+{
+    /*! \brief The prior code for each defined code. */
+    uint16_t parent_code;
+    /*! \brief The number of leaf nodes this node has */
+    int16_t leaves;
+    /*! \brief This leaf octet for each defined code. */
+    uint8_t node_octet;
+    /*! \brief Bit map of the children which exist */
+    uint32_t children[8];
+} v42bis_dict_node_t;
+
+/*!
+    V.42bis compression. This defines the working state for a single instance
+    of V.42bis compression.
+*/
+typedef struct
+{
+    /*! \brief Compression mode. */
+    int compression_mode;
+    /*! \brief Callback function to handle received frames. */
+    v42bis_frame_handler_t handler;
+    /*! \brief An opaque pointer passed in calls to frame_handler. */
+    void *user_data;
+    /*! \brief The maximum frame length allowed */
+    int max_len;
+
+    uint32_t string_code;
+    uint32_t latest_code;
+    int string_length;
+    uint32_t output_bit_buffer;
+    int output_bit_count;
+    int output_octet_count;
+    uint8_t output_buf[1024];
+    v42bis_dict_node_t dict[V42BIS_MAX_CODEWORDS];
+    /*! \brief TRUE if we are in transparent (i.e. uncompressable) mode */
+    int transparent;
+    int change_transparency;
+    /*! \brief IIR filter state, used in assessing compressibility. */
+    int compressibility_filter;
+    int compressibility_persistence;
+    
+    /*! \brief Next empty dictionary entry */
+    uint32_t v42bis_parm_c1;
+    /*! \brief Current codeword size */
+    int v42bis_parm_c2;
+    /*! \brief Threshold for codeword size change */
+    uint32_t v42bis_parm_c3;
+
+    /*! \brief Mark that this is the first octet/code to be processed */
+    int first;
+    uint8_t escape_code;
+} v42bis_compress_state_t;
+
+/*!
+    V.42bis decompression. This defines the working state for a single instance
+    of V.42bis decompression.
+*/
+typedef struct
+{
+    /*! \brief Callback function to handle decompressed data. */
+    v42bis_data_handler_t handler;
+    /*! \brief An opaque pointer passed in calls to data_handler. */
+    void *user_data;
+    /*! \brief The maximum decompressed data block length allowed */
+    int max_len;
+
+    uint32_t old_code;
+    uint32_t last_old_code;
+    uint32_t input_bit_buffer;
+    int input_bit_count;
+    int octet;
+    int last_length;
+    int output_octet_count;
+    uint8_t output_buf[1024];
+    v42bis_dict_node_t dict[V42BIS_MAX_CODEWORDS];
+    /*! \brief TRUE if we are in transparent (i.e. uncompressable) mode */
+    int transparent;
+
+    int last_extra_octet;
+
+    /*! \brief Next empty dictionary entry */
+    uint32_t v42bis_parm_c1;
+    /*! \brief Current codeword size */
+    int v42bis_parm_c2;
+    /*! \brief Threshold for codeword size change */
+    uint32_t v42bis_parm_c3;
+        
+    /*! \brief Mark that this is the first octet/code to be processed */
+    int first;
+    uint8_t escape_code;
+    int escaped;
+} v42bis_decompress_state_t;
+
+/*!
+    V.42bis compression/decompression descriptor. This defines the working state for a
+    single instance of V.42bis compress/decompression.
+*/
+typedef struct
+{
+    /*! \brief V.42bis data compression directions. */
+    int v42bis_parm_p0;
+
+    /*! \brief Compression state. */
+    v42bis_compress_state_t compress;
+    /*! \brief Decompression state. */
+    v42bis_decompress_state_t decompress;
+    
+    /*! \brief Maximum codeword size (bits) */
+    int v42bis_parm_n1;
+    /*! \brief Total number of codewords */
+    uint32_t v42bis_parm_n2;
+    /*! \brief Maximum string length */
+    int v42bis_parm_n7;
+} v42bis_state_t;
+
+#if defined(__cplusplus)
+extern "C"
+{
+#endif
+
+/*! Compress a block of octets.
+    \param s The V.42bis context.
+    \param buf The data to be compressed.
+    \param len The length of the data buffer.
+    \return 0 */
+int v42bis_compress(v42bis_state_t *s, const uint8_t *buf, int len);
+
+/*! Flush out any data remaining in a compression buffer.
+    \param s The V.42bis context.
+    \return 0 */
+int v42bis_compress_flush(v42bis_state_t *s);
+
+/*! Decompress a block of octets.
+    \param s The V.42bis context.
+    \param buf The data to be decompressed.
+    \param len The length of the data buffer.
+    \return 0 */
+int v42bis_decompress(v42bis_state_t *s, const uint8_t *buf, int len);
+    
+/*! Flush out any data remaining in the decompression buffer.
+    \param s The V.42bis context.
+    \return 0 */
+int v42bis_decompress_flush(v42bis_state_t *s);
+
+/*! Initialise a V.42bis context.
+    \param s The V.42bis context.
+    \param negotiated_p0 The negotiated P0 parameter, from the V.42bis spec.
+    \param negotiated_p1 The negotiated P1 parameter, from the V.42bis spec.
+    \param negotiated_p2 The negotiated P2 parameter, from the V.42bis spec.
+    \param frame_handler .
+    \param frame_user_data .
+    \param max_frame_len The maximum length that should be passed to the frame handler.
+    \param data_handler .
+    \param data_user_data .
+    \param max_data_len The maximum length that should be passed to the data handler.
+    \return The V.42bis context. */
+v42bis_state_t *v42bis_init(v42bis_state_t *s,
+                            int negotiated_p0,
+                            int negotiated_p1,
+                            int negotiated_p2,
+                            v42bis_frame_handler_t frame_handler,
+                            void *frame_user_data,
+                            int max_frame_len,
+                            v42bis_data_handler_t data_handler,
+                            void *data_user_data,
+                            int max_data_len);
+
+/*! Set the compression mode.
+    \param s The V.42bis context.
+    \param mode One of the V.42bis compression modes -
+            V42BIS_COMPRESSION_MODE_DYNAMIC,
+            V42BIS_COMPRESSION_MODE_ALWAYS,
+            V42BIS_COMPRESSION_MODE_NEVER */
+void v42bis_compression_control(v42bis_state_t *s, int mode);
+
+/*! Release a V.42bis context.
+    \param s The V.42bis context.
+    \return 0 if OK */
+int v42bis_release(v42bis_state_t *s);
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/spandsp/v8.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/spandsp/v8.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,225 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * v8.h - V.8 modem negotiation processing.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2004 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: v8.h,v 1.22 2008/05/14 15:41:25 steveu Exp $
+ */
+ 
+/*! \file */
+
+/*! \page v8_page The V.8 modem negotiation protocol
+\section v8_page_sec_1 What does it do?
+The V.8 specification defines a procedure to be used as PSTN modem answer phone calls,
+which allows the modems to negotiate the optimum modem standard, which both ends can
+support.
+
+\section v8_page_sec_2 How does it work?
+At startup the modems communicate using the V.21 standard at 300 bits/second. They
+exchange simple messages about their capabilities, and choose the modem standard they
+will use for data communication. The V.8 protocol then terminates, and the modems
+being negotiating and training with their chosen modem standard.
+*/
+
+#if !defined(_SPANDSP_V8_H_)
+#define _SPANDSP_V8_H_
+
+typedef struct v8_result_s v8_result_t;
+
+typedef void (v8_result_handler_t)(void *user_data, v8_result_t *result);
+
+enum v8_call_function_e
+{
+    V8_CALL_TBS = 0,
+    V8_CALL_H324,
+    V8_CALL_V18,
+    V8_CALL_T101,
+    V8_CALL_T30_TX,
+    V8_CALL_T30_RX,
+    V8_CALL_V_SERIES,
+    V8_CALL_FUNCTION_EXTENSION
+};
+
+enum v8_modulation_e
+{
+    V8_MOD_V17          = (1 << 0),     /* V.17 half-duplex */
+    V8_MOD_V21          = (1 << 1),     /* V.21 duplex */
+    V8_MOD_V22          = (1 << 2),     /* V.22/V22.bis duplex */
+    V8_MOD_V23HALF      = (1 << 3),     /* V.23 half-duplex */
+    V8_MOD_V23          = (1 << 4),     /* V.23 duplex */
+    V8_MOD_V26BIS       = (1 << 5),     /* V.23 duplex */
+    V8_MOD_V26TER       = (1 << 6),     /* V.23 duplex */
+    V8_MOD_V27TER       = (1 << 7),     /* V.23 duplex */
+    V8_MOD_V29          = (1 << 8),     /* V.29 half-duplex */
+    V8_MOD_V32          = (1 << 9),     /* V.32/V32.bis duplex */
+    V8_MOD_V34HALF      = (1 << 10),    /* V.34 half-duplex */
+    V8_MOD_V34          = (1 << 11),    /* V.34 duplex */
+    V8_MOD_V90          = (1 << 12),    /* V.90 duplex */
+    V8_MOD_V92          = (1 << 13),    /* V.92 duplex */
+
+    V8_MOD_FAILED       = (1 << 15),    /* Indicates failure to negotiate */
+};
+
+enum v8_protocol_e
+{
+    V8_PROTOCOL_NONE = 0,
+    V8_PROTOCOL_LAPM_V42 = 1,
+    V8_PROTOCOL_EXTENSION = 7
+};
+
+enum v8_pstn_access_e
+{
+    V8_PSTN_ACCESS_CALL_DCE_CELLULAR = 0x20,
+    V8_PSTN_ACCESS_ANSWER_DCE_CELLULAR = 0x40,
+    V8_PSTN_ACCESS_DCE_ON_DIGTIAL = 0x80
+};
+
+enum v8_pcm_modem_availability_e
+{
+    V8_PSTN_PCM_MODEM_V90_V92_ANALOGUE = 0x20,
+    V8_PSTN_PCM_MODEM_V90_V92_DIGITAL = 0x40,
+    V8_PSTN_PCM_MODEM_V91 = 0x80
+};
+
+typedef struct
+{
+    /*! \brief TRUE if we are the calling modem */
+    int caller;
+    /*! \brief The current state of the V8 protocol */
+    int state;
+    int negotiation_timer;
+    int ci_timer;
+    int ci_count;
+    fsk_tx_state_t v21tx;
+    fsk_rx_state_t v21rx;
+    queue_state_t *tx_queue;
+    modem_connect_tones_tx_state_t ansam_tx;
+    modem_connect_tones_rx_state_t ansam_rx;
+
+    v8_result_handler_t *result_handler;
+    void *result_handler_user_data;
+
+    /*! \brief Modulation schemes available at this end. */
+    int available_modulations;
+    int common_modulations;
+    int negotiated_modulation;
+    int far_end_modulations;
+    
+    int call_function;
+    int protocol;
+    int pstn_access;
+    int nsf_seen;
+    int pcm_modem_availability;
+    int t66_seen;
+
+    /* V8 data parsing */
+    unsigned int bit_stream;
+    int bit_cnt;
+    /* Indicates the type of message coming up */
+    int preamble_type;
+    uint8_t rx_data[64];
+    int rx_data_ptr;
+    
+    /*! \brief a reference copy of the last CM or JM message, used when
+               testing for matches. */
+    uint8_t cm_jm_data[64];
+    int cm_jm_count;
+    int got_cm_jm;
+    int got_cj;
+    int zero_byte_count;
+    /*! \brief Error and flow logging control */
+    logging_state_t logging;
+} v8_state_t;
+
+struct v8_result_s
+{
+    int call_function;
+    int available_modulations;
+    int negotiated_modulation;
+    int protocol;
+    int pstn_access;
+    int nsf_seen;
+    int pcm_modem_availability;
+    int t66_seen;
+};
+
+#if defined(__cplusplus)
+extern "C"
+{
+#endif
+
+/*! Initialise a V.8 context.
+    \brief Initialise a V.8 context.
+    \param s The V.8 context.
+    \param caller TRUE if caller mode, else answerer mode.
+    \param available_modulations A bitwise list of the modulation schemes to be
+           advertised as available here.
+    \param result_handler The callback routine used to handle the results of negotiation.
+    \param user_data An opaque pointer passed to the result_handler routine.
+    \return A pointer to the V.8 context, or NULL if there was a problem. */
+v8_state_t *v8_init(v8_state_t *s,
+                    int caller,
+                    int available_modulations,
+                    v8_result_handler_t *result_handler,
+                    void *user_data);
+
+/*! Release a V.8 context.
+    \brief Release a V.8 context.
+    \param s The V.8 context.
+    \return 0 for OK. */
+int v8_release(v8_state_t *s);
+
+/*! Generate a block of V.8 audio samples.
+    \brief Generate a block of V.8 audio samples.
+    \param s The V.8 context.
+    \param amp The audio sample buffer.
+    \param max_len The number of samples to be generated.
+    \return The number of samples actually generated.
+*/
+int v8_tx(v8_state_t *s, int16_t *amp, int max_len);
+
+/*! Process a block of received V.8 audio samples.
+    \brief Process a block of received V.8 audio samples.
+    \param s The V.8 context.
+    \param amp The audio sample buffer.
+    \param len The number of samples in the buffer.
+*/
+int v8_rx(v8_state_t *s, const int16_t *amp, int len);
+
+/*! Log the list of supported modulations.
+    \brief Log the list of supported modulations.
+    \param s The V.8 context.
+    \param modulation_schemes The list of supported modulations. */
+void v8_log_supported_modulations(v8_state_t *s, int modulation_schemes);
+
+const char *v8_call_function_to_str(int call_function);
+const char *v8_modulation_to_str(int modulation_scheme);
+const char *v8_protocol_to_str(int protocol);
+const char *v8_pstn_access_to_str(int pstn_access);
+const char *v8_pcm_modem_availability_to_str(int pcm_modem_availability);
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/spandsp/vector_float.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/spandsp/vector_float.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,121 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * vector_float.h
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2003 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: vector_float.h,v 1.10 2008/04/17 14:27:01 steveu Exp $
+ */
+
+#if !defined(_SPANDSP_VECTOR_FLOAT_H_)
+#define _SPANDSP_VECTOR_FLOAT_H_
+
+#if defined(__cplusplus)
+extern "C"
+{
+#endif
+
+void vec_copyf(float z[], const float x[], int n);
+
+void vec_copy(double z[], const double x[], int n);
+
+#if defined(HAVE_LONG_DOUBLE)
+void vec_copyl(long double z[], const long double x[], int n);
+#endif
+
+void vec_zerof(float z[], int n);
+
+void vec_zero(double z[], int n);
+
+#if defined(HAVE_LONG_DOUBLE)
+void vec_zerol(long double z[], int n);
+#endif
+
+void vec_setf(float z[], float x, int n);
+
+void vec_set(double z[], double x, int n);
+
+#if defined(HAVE_LONG_DOUBLE)
+void vec_setl(long double z[], long double x, int n);
+#endif
+
+void vec_addf(float z[], const float x[], const float y[], int n);
+
+void vec_add(double z[], const double x[], const double y[], int n);
+
+#if defined(HAVE_LONG_DOUBLE)
+void vec_addl(long double z[], const long double x[], const long double y[], int n);
+#endif
+
+void vec_scaled_addf(float z[], const float x[], float x_scale, const float y[], float y_scale, int n);
+
+void vec_scaled_add(double z[], const double x[], double x_scale, const double y[], double y_scale, int n);
+
+#if defined(HAVE_LONG_DOUBLE)
+void vec_scaled_addl(long double z[], const long double x[], long double x_scale, const long double y[], long double y_scale, int n);
+#endif
+
+void vec_subf(float z[], const float x[], const float y[], int n);
+
+void vec_sub(double z[], const double x[], const double y[], int n);
+
+#if defined(HAVE_LONG_DOUBLE)
+void vec_subl(long double z[], const long double x[], const long double y[], int n);
+#endif
+
+void vec_scaled_subf(float z[], const float x[], float x_scale, const float y[], float y_scale, int n);
+
+void vec_scaled_sub(double z[], const double x[], double x_scale, const double y[], double y_scale, int n);
+
+#if defined(HAVE_LONG_DOUBLE)
+void vec_scaled_subl(long double z[], const long double x[], long double x_scale, const long double y[], long double y_scale, int n);
+#endif
+
+void vec_scalar_mulf(float z[], const float x[], float y, int n);
+
+void vec_scalar_mul(double z[], const double x[], double y, int n);
+
+#if defined(HAVE_LONG_DOUBLE)
+void vec_scalar_mull(long double z[], const long double x[], long double y, int n);
+#endif
+
+void vec_mulf(float z[], const float x[], const float y[], int n);
+
+void vec_mul(double z[], const double x[], const double y[], int n);
+
+#if defined(HAVE_LONG_DOUBLE)
+void vec_mull(long double z[], const long double x[], const long double y[], int n);
+#endif
+
+float vec_dot_prodf(const float x[], const float y[], int n);
+
+double vec_dot_prod(const double x[], const double y[], int n);
+
+#if defined(HAVE_LONG_DOUBLE)
+long double vec_dot_prodl(const long double x[], const long double y[], int n);
+#endif
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/spandsp/vector_int.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/spandsp/vector_int.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,99 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * vector_int.h
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2003 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: vector_int.h,v 1.10 2008/04/17 14:27:01 steveu Exp $
+ */
+
+#if !defined(_SPANDSP_VECTOR_INT_H_)
+#define _SPANDSP_VECTOR_INT_H_
+
+#if defined(__cplusplus)
+extern "C"
+{
+#endif
+
+int32_t vec_dot_prodi16(const int16_t x[], const int16_t y[], int n);
+
+/*! \brief Find the minimum and maximum values in a vector.
+    \param x The vector to be searched.
+    \param n The number of elements in the vetor.
+    \param out A two element vector. The first will receive the 
+           maximum. The second will receive the minimum. This parameter
+           may be set to NULL.
+    \return The absolute maximum value. Since the range of negative numbers
+            exceeds the range of positive one, the returned integer is longer
+            than the ones being searched. */
+int32_t vec_min_maxi16(const int16_t x[], int n, int16_t out[]);
+
+static __inline__ int vec_norm2i16(const int16_t *vec, int len)
+{
+    int i;
+    int sum;
+
+    sum = 0;
+    for (i = 0;  i < len;  i++)
+        sum += vec[i]*vec[i];
+    return sum;
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ void vec_sari16(int16_t *vec, int len, int shift)
+{
+    int i;
+
+    for (i = 0;  i < len;  i++)
+        vec[i] >>= shift;
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ int vec_max_bitsi16(const int16_t *vec, int len)
+{
+    int i;
+    int max;
+    int v;
+    int b;
+
+    max = 0;
+    for (i = 0;  i < len;  i++)
+    {
+        v = abs(vec[i]);
+        if (v > max)
+            max = v;
+    }
+    b = 0;
+    while (max != 0)
+    {
+        b++;
+        max >>= 1;
+    }
+    return b;
+}
+/*- End of function --------------------------------------------------------*/
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/spandsp/version.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/spandsp/version.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,37 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * version.h - A tag file, so the exact installed revision can be assertained.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2007 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: version.h.in,v 1.2 2007/04/06 13:20:36 steveu Exp $
+ */
+
+#if !defined(_SPANDSP_VERSION_H_)
+#define _SPANDSP_VERSION_H_
+
+/* The date and time of the version are in UTC form. */
+
+#define SPANDSP_RELEASE_DATE    20080829
+#define SPANDSP_RELEASE_TIME    104025
+
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/spandsp/version.h.in
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/spandsp/version.h.in	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,37 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * version.h - A tag file, so the exact installed revision can be assertained.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2007 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: version.h.in,v 1.2 2007/04/06 13:20:36 steveu Exp $
+ */
+
+#if !defined(_SPANDSP_VERSION_H_)
+#define _SPANDSP_VERSION_H_
+
+/* The date and time of the version are in UTC form. */
+
+#define SPANDSP_RELEASE_DATE    $SPANDSP_RELEASE_DATE
+#define SPANDSP_RELEASE_TIME    $SPANDSP_RELEASE_TIME
+
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/super_tone_rx.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/super_tone_rx.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,446 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * super_tone_rx.c - Flexible telephony supervisory tone detection.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2003 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: super_tone_rx.c,v 1.29 2008/07/25 13:56:54 steveu Exp $
+ */
+
+/*! \file */
+
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <ctype.h>
+#include <time.h>
+#include <inttypes.h>
+#include "floating_fudge.h"
+#if defined(HAVE_TGMATH_H)
+#include <tgmath.h>
+#endif
+#if defined(HAVE_MATH_H)
+#include <math.h>
+#endif
+
+#include "spandsp/telephony.h"
+#include "spandsp/complex.h"
+#include "spandsp/vector_float.h"
+#include "spandsp/complex_vector_float.h"
+#include "spandsp/tone_detect.h"
+#include "spandsp/tone_generate.h"
+#include "spandsp/super_tone_rx.h"
+
+#if defined(SPANDSP_USE_FIXED_POINT)
+#define DETECTION_THRESHOLD         16439           /* -42dBm0 */
+#define TONE_TWIST                  4               /* 6dB */
+#define TONE_TO_TOTAL_ENERGY        64              /* -3dB */
+#else
+#define DETECTION_THRESHOLD         269338317.0f    /* -42dBm0 [((128.0*32768.0/1.4142)*10^((-42 - DBM0_MAX_SINE_POWER)/20.0))^2 => 269338317.0] */
+#define TONE_TWIST                  3.981f          /* 6dB */
+#define TONE_TO_TOTAL_ENERGY        1.995f          /* 3dB */
+#define DTMF_TO_TOTAL_ENERGY        64.152f         /* -3dB [BINS*10^(-3/10.0)] */
+#endif
+
+static int add_super_tone_freq(super_tone_rx_descriptor_t *desc, int freq)
+{
+    int i;
+
+    if (freq == 0)
+        return -1;
+    /* Look for an existing frequency */
+    for (i = 0;  i < desc->used_frequencies;  i++)
+    {
+        if (desc->pitches[i][0] == freq)
+            return desc->pitches[i][1];
+    }
+    /* Look for an existing tone which is very close. We may need to merge
+       the detectors. */
+    for (i = 0;  i < desc->used_frequencies;  i++)
+    {
+        if ((desc->pitches[i][0] - 10) <= freq  &&  freq <= (desc->pitches[i][0] + 10))
+        {
+            /* Merge these two */
+            desc->pitches[desc->used_frequencies][0] = freq;
+            desc->pitches[desc->used_frequencies][1] = i;
+            make_goertzel_descriptor(&desc->desc[desc->pitches[i][1]], (float) (freq + desc->pitches[i][0])/2, BINS);
+            desc->used_frequencies++;
+            return desc->pitches[i][1];
+        }
+    }
+    desc->pitches[i][0] = freq;
+    desc->pitches[i][1] = desc->monitored_frequencies;
+    if (desc->monitored_frequencies%5 == 0)
+    {
+        desc->desc = (goertzel_descriptor_t *) realloc(desc->desc, (desc->monitored_frequencies + 5)*sizeof(goertzel_descriptor_t));
+    }
+    make_goertzel_descriptor(&desc->desc[desc->monitored_frequencies++], (float) freq, BINS);
+    desc->used_frequencies++;
+    return desc->pitches[i][1];
+}
+/*- End of function --------------------------------------------------------*/
+
+int super_tone_rx_add_tone(super_tone_rx_descriptor_t *desc)
+{
+    if (desc->tones%5 == 0)
+    {
+        desc->tone_list = (super_tone_rx_segment_t **) realloc(desc->tone_list, (desc->tones + 5)*sizeof(super_tone_rx_segment_t *));
+        desc->tone_segs = (int *) realloc(desc->tone_segs, (desc->tones + 5)*sizeof(int));
+    }
+    desc->tone_list[desc->tones] = NULL;
+    desc->tone_segs[desc->tones] = 0;
+    desc->tones++;
+    return desc->tones - 1;
+}
+/*- End of function --------------------------------------------------------*/
+
+int super_tone_rx_add_element(super_tone_rx_descriptor_t *desc,
+                              int tone,
+                              int f1,
+                              int f2,
+                              int min,
+                              int max)
+{
+    int step;
+
+    step = desc->tone_segs[tone];
+    if (step%5 == 0)
+    {
+        desc->tone_list[tone] = (super_tone_rx_segment_t *) realloc(desc->tone_list[tone], (step + 5)*sizeof(super_tone_rx_segment_t));
+    }
+    desc->tone_list[tone][step].f1 = add_super_tone_freq(desc, f1);
+    desc->tone_list[tone][step].f2 = add_super_tone_freq(desc, f2);
+    desc->tone_list[tone][step].min_duration = min*8;
+    desc->tone_list[tone][step].max_duration = (max == 0)  ?  0x7FFFFFFF  :  max*8;
+    desc->tone_segs[tone]++;
+    return step;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int test_cadence(super_tone_rx_segment_t *pattern,
+                        int steps,
+                        super_tone_rx_segment_t *test,
+                        int rotation)
+{
+    int i;
+    int j;
+
+    if (rotation >= 0)
+    {
+        /* Check only for the sustaining of a tone in progress. This means
+           we only need to check each block if the latest step is compatible
+           with the tone template. */
+        if (steps < 0)
+        {
+            /* A -ve value for steps indicates we just changed step, and need to
+               check the last one ended within spec. If we don't do this
+               extra test a low duration segment might be accepted as OK. */
+            steps = -steps;
+            j = (rotation + steps - 2)%steps;
+            if (pattern[j].f1 != test[8].f1  ||  pattern[j].f2 != test[8].f2)
+                return  0;
+            if (pattern[j].min_duration > test[8].min_duration*BINS
+                ||
+                pattern[j].max_duration < test[8].min_duration*BINS)
+            {
+                return  0;
+            }
+        }
+        j = (rotation + steps - 1)%steps;
+        if (pattern[j].f1 != test[9].f1  ||  pattern[j].f2 != test[9].f2)
+            return  0;
+        if (pattern[j].max_duration < test[9].min_duration*BINS)
+            return  0;
+    }
+    else
+    {
+        /* Look for a complete template match. */
+        for (i = 0;  i < steps;  i++)
+        {
+            j = i + 10 - steps;
+            if (pattern[i].f1 != test[j].f1  ||  pattern[i].f2 != test[j].f2)
+                return  0;
+            if (pattern[i].min_duration > test[j].min_duration*BINS
+                ||
+                pattern[i].max_duration < test[j].min_duration*BINS)
+            {
+                return  0;
+            }
+        }
+    }
+    return  1;
+}
+/*- End of function --------------------------------------------------------*/
+
+super_tone_rx_descriptor_t *super_tone_rx_make_descriptor(super_tone_rx_descriptor_t *desc)
+{
+    if (desc == NULL)
+    {
+        if ((desc = (super_tone_rx_descriptor_t *) malloc(sizeof(*desc))) == NULL)
+            return NULL;
+    }
+    desc->tone_list = NULL;
+    desc->tone_segs = NULL;
+
+    desc->used_frequencies = 0;
+    desc->monitored_frequencies = 0;
+    desc->desc = NULL;
+    desc->tones = 0;
+    return desc;
+}
+/*- End of function --------------------------------------------------------*/
+
+int super_tone_rx_free_descriptor(super_tone_rx_descriptor_t *desc)
+{
+    if (desc)
+        free(desc);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+void super_tone_rx_segment_callback(super_tone_rx_state_t *s,
+                                    void (*callback)(void *data, int f1, int f2, int duration))
+{
+    s->segment_callback = callback;
+}
+/*- End of function --------------------------------------------------------*/
+
+super_tone_rx_state_t *super_tone_rx_init(super_tone_rx_state_t *s,
+                                          super_tone_rx_descriptor_t *desc,
+                                          tone_report_func_t callback,
+                                          void *user_data)
+{
+    int i;
+
+    if (desc == NULL)
+        return NULL;
+    if (callback == NULL)
+        return NULL;
+    if (s == NULL)
+    {
+        if ((s = (super_tone_rx_state_t *) malloc(sizeof(*s) + desc->monitored_frequencies*sizeof(goertzel_state_t))) == NULL)
+            return NULL;
+    }
+
+    for (i = 0;  i < 11;  i++)
+    {
+        s->segments[i].f1 = -1;
+        s->segments[i].f2 = -1;
+        s->segments[i].min_duration = 0;
+    }
+    s->segment_callback = NULL;
+    s->tone_callback = callback;
+    s->callback_data = user_data;
+    if (desc)
+        s->desc = desc;
+    s->detected_tone = -1;
+    s->energy = 0.0f;
+    for (i = 0;  i < desc->monitored_frequencies;  i++)
+        goertzel_init(&s->state[i], &s->desc->desc[i]);
+    return  s;
+}
+/*- End of function --------------------------------------------------------*/
+
+int super_tone_rx_free(super_tone_rx_state_t *s)
+{
+    if (s)
+        free(s);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void super_tone_chunk(super_tone_rx_state_t *s)
+{
+    int i;
+    int j;
+    int k1;
+    int k2;
+#if defined(SPANDSP_USE_FIXED_POINT)
+    int32_t res[BINS/2];
+#else
+    float res[BINS/2];
+#endif
+
+    for (i = 0;  i < s->desc->monitored_frequencies;  i++)
+        res[i] = goertzel_result(&s->state[i]);
+    /* Find our two best monitored frequencies, which also have adequate energy. */
+    if (s->energy < DETECTION_THRESHOLD)
+    {
+        k1 = -1;
+        k2 = -1;
+    }
+    else
+    {
+        if (res[0] > res[1])
+        {
+            k1 = 0;
+            k2 = 1;
+        }
+        else
+        {
+            k1 = 1;
+            k2 = 0;
+        }
+        for (j = 2;  j < s->desc->monitored_frequencies;  j++)
+        {
+            if (res[j] >= res[k1])
+            {
+                k2 = k1;
+                k1 = j;
+            }
+            else if (res[j] >= res[k2])
+            {
+                k2 = j;
+            }
+        }
+        if ((res[k1] + res[k2]) < TONE_TO_TOTAL_ENERGY*s->energy)
+        {
+            k1 = -1;
+            k2 = -1;
+        }
+        else if (res[k1] > TONE_TWIST*res[k2])
+        {
+            k2 = -1;
+        }
+        else if (k2 < k1)
+        {
+            j = k1;
+            k1 = k2;
+            k2 = j;
+        }
+    }
+    /* See if this differs from last time. */
+    if (k1 != s->segments[10].f1  ||  k2 != s->segments[10].f2)
+    {
+        /* It is different, but this might just be a transitional quirk, or
+           a one shot hiccup (eg due to noise). Only if this same thing is
+           seen a second time should we change state. */
+        s->segments[10].f1 = k1;
+        s->segments[10].f2 = k2;
+        /* While things are hopping around, consider this a continuance of the
+           previous state. */
+        s->segments[9].min_duration++;
+    }
+    else
+    {
+        if (k1 != s->segments[9].f1  ||  k2 != s->segments[9].f2)
+        {
+            if (s->detected_tone >= 0)
+            {
+                /* Test for the continuance of the existing tone pattern, based on our new knowledge of an
+                   entire segment length. */
+                if (!test_cadence(s->desc->tone_list[s->detected_tone], -s->desc->tone_segs[s->detected_tone], s->segments, s->rotation++))
+                {
+                    s->detected_tone = -1;
+                    s->tone_callback(s->callback_data, s->detected_tone, -10, 0);
+                }
+            }
+            if (s->segment_callback)
+            {
+                s->segment_callback(s->callback_data,
+                                    s->segments[9].f1,
+                                    s->segments[9].f2,
+                                    s->segments[9].min_duration*BINS/8);
+            }
+            memcpy (&s->segments[0], &s->segments[1], 9*sizeof(s->segments[0]));
+            s->segments[9].f1 = k1;
+            s->segments[9].f2 = k2;
+            s->segments[9].min_duration = 1;
+        }
+        else
+        {
+            /* This is a continuance of the previous state */
+            if (s->detected_tone >= 0)
+            {
+                /* Test for the continuance of the existing tone pattern. We must do this here, so we can sense the
+                   discontinuance of the tone on an excessively long segment. */
+                if (!test_cadence(s->desc->tone_list[s->detected_tone], s->desc->tone_segs[s->detected_tone], s->segments, s->rotation))
+                {
+                    s->detected_tone = -1;
+                    s->tone_callback(s->callback_data, s->detected_tone, -10, 0);
+                }
+            }
+            s->segments[9].min_duration++;
+        }
+    }
+    if (s->detected_tone < 0)
+    {
+        /* Test for the start of any of the monitored tone patterns */
+        for (j = 0;  j < s->desc->tones;  j++)
+        {
+            if (test_cadence(s->desc->tone_list[j], s->desc->tone_segs[j], s->segments, -1))
+            {
+                s->detected_tone = j;
+                s->rotation = 0;
+                s->tone_callback(s->callback_data, s->detected_tone, -10, 0);
+                break;
+            }
+        }
+    }
+#if defined(SPANDSP_USE_FIXED_POINT)
+    s->energy = 0;
+#else
+    s->energy = 0.0f;
+#endif
+}
+/*- End of function --------------------------------------------------------*/
+
+int super_tone_rx(super_tone_rx_state_t *s, const int16_t amp[], int samples)
+{
+    int i;
+    int x;
+    int sample;
+#if defined(SPANDSP_USE_FIXED_POINT)
+    int16_t xamp;
+#else
+    float xamp;
+#endif
+
+    x = 0;
+    for (sample = 0;  sample < samples;  sample += x)
+    {
+        for (i = 0;  i < s->desc->monitored_frequencies;  i++)
+            x = goertzel_update(&s->state[i], amp + sample, samples - sample);
+        for (i = 0;  i < x;  i++)
+        {
+            xamp = goertzel_preadjust_amp(amp[sample + i]);
+#if defined(SPANDSP_USE_FIXED_POINT)
+            s->energy += ((int32_t) xamp*xamp);
+#else
+            s->energy += xamp*xamp;
+#endif
+        }
+        if (s->state[0].current_sample >= BINS)
+        {
+            /* We have finished a Goertzel block. */
+            super_tone_chunk(s);
+            s->energy = 0;
+        }
+    }
+    return samples;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/super_tone_tx.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/super_tone_tx.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,269 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * super_tone_tx.c - Flexible telephony supervisory tone generation.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2003 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: super_tone_tx.c,v 1.25 2008/07/02 14:48:26 steveu Exp $
+ */
+
+/*! \file */
+
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <ctype.h>
+#include <time.h>
+#include <inttypes.h>
+#include "floating_fudge.h"
+#if defined(HAVE_TGMATH_H)
+#include <tgmath.h>
+#endif
+#if defined(HAVE_MATH_H)
+#include <math.h>
+#endif
+
+#include "spandsp/telephony.h"
+#include "spandsp/complex.h"
+#include "spandsp/dds.h"
+#include "spandsp/tone_generate.h"
+#include "spandsp/super_tone_tx.h"
+
+/*
+    The tone played to wake folk up when they have left the phone off hook is an
+    oddity amongst supervisory tones. It is designed to sound loud and nasty. Most
+    tones are one or two pure sine pitches, or one AM moduluated pitch. This alert
+    tone varies between countries, but AT&T are a typical example.
+    
+    AT&T Receiver Off-Hook Tone is 1400 Hz, 2060 Hz, 2450 Hz and 2600 Hz at 0dBm0/frequency
+    on and off every .1 second. On some older space division switching systems
+    Receiver Off-Hook was 1400 Hz, 2060 Hz, 2450 Hz and 2600 Hz at +5 VU on and
+    off every .1 second. On a No. 5 ESS this continues for 30 seconds. On a No.
+    2/2B ESS this continues for 40 seconds. On some other AT&T switches there are
+    two iterations of 50 seconds each.
+*/
+
+super_tone_tx_step_t *super_tone_tx_make_step(super_tone_tx_step_t *s,
+                                              float f1,
+                                              float l1,
+                                              float f2,
+                                              float l2,
+                                              int length,
+                                              int cycles)
+{
+    if (s == NULL)
+    {
+        if ((s = (super_tone_tx_step_t *) malloc(sizeof(*s))) == NULL)
+            return NULL;
+    }
+    if (f1 >= 1.0f)
+    {    
+        s->tone[0].phase_rate = dds_phase_ratef(f1);
+        s->tone[0].gain = dds_scaling_dbm0f(l1);
+    }
+    else
+    {
+        s->tone[0].phase_rate = 0;
+        s->tone[0].gain = 0.0f;
+    }
+    if (f2 >= 1.0f)
+    {
+        s->tone[1].phase_rate = dds_phase_ratef(f2);
+        s->tone[1].gain = dds_scaling_dbm0f(l2);
+    }
+    else
+    {
+        s->tone[1].phase_rate = 0;
+        s->tone[1].gain = 0.0f;
+    }
+    s->tone_on = (f1 > 0.0f);
+    s->length = length*SAMPLE_RATE/1000;
+    s->cycles = cycles;
+    s->next = NULL;
+    s->nest = NULL;
+    return s;
+}
+/*- End of function --------------------------------------------------------*/
+
+void super_tone_tx_free(super_tone_tx_step_t *s)
+{
+    super_tone_tx_step_t *t;
+
+    while (s)
+    {
+        /* Follow nesting... */
+        if (s->nest)
+            super_tone_tx_free(s->nest);
+        t = s;
+        s = s->next;
+        free(t);
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+super_tone_tx_state_t *super_tone_tx_init(super_tone_tx_state_t *s, super_tone_tx_step_t *tree)
+{
+    if (tree == NULL)
+        return NULL;
+    if (s == NULL)
+    {
+        if ((s = (super_tone_tx_state_t *) malloc(sizeof(*s))) == NULL)
+            return NULL;
+    }
+    memset(s, 0, sizeof(*s));
+    s->level = 0;
+    s->levels[0] = tree;
+    s->cycles[0] = tree->cycles;
+
+    s->current_position = 0;
+    return s;
+}
+/*- End of function --------------------------------------------------------*/
+
+int super_tone_tx(super_tone_tx_state_t *s, int16_t amp[], int max_samples)
+{
+    int samples;
+    int limit;
+    int len;
+    int i;
+    float xamp;
+    super_tone_tx_step_t *tree;
+
+    if (s->level < 0  ||  s->level > 3)
+        return  0;
+    samples = 0;
+    tree = s->levels[s->level];
+    while (tree  &&  samples < max_samples)
+    {
+        if (tree->tone_on)
+        {
+            /* A period of tone. A length of zero means infinite length. */
+            if (s->current_position == 0)
+            {
+                /* New step - prepare the tone generator */
+                for (i = 0;  i < 4;  i++)
+                    s->tone[i] = tree->tone[i];
+            }
+            len = tree->length - s->current_position;
+            if (tree->length == 0)
+            {
+                len = max_samples - samples;
+                /* We just need to make current position non-zero */
+                s->current_position = 1;
+            }
+            else if (len > max_samples - samples)
+            {
+                len = max_samples - samples;
+                s->current_position += len;
+            }
+            else
+            {
+                s->current_position = 0;
+            }
+            if (s->tone[0].phase_rate < 0)
+            {
+                for (limit = len + samples;  samples < limit;  samples++)
+                {
+                    /* There must be two, and only two tones */
+                    xamp = dds_modf(&s->phase[0], -s->tone[0].phase_rate, s->tone[0].gain, 0)
+                         *(1.0f + dds_modf(&s->phase[1], s->tone[1].phase_rate, s->tone[1].gain, 0));
+                    amp[samples] = (int16_t) lrintf(xamp);
+                }
+            }
+            else
+            {
+                for (limit = len + samples;  samples < limit;  samples++)
+                {
+                    xamp = 0.0f;
+                    for (i = 0;  i < 4;  i++)
+                    {
+                        if (s->tone[i].phase_rate == 0)
+                            break;
+                        xamp += dds_modf(&s->phase[i], s->tone[i].phase_rate, s->tone[i].gain, 0);
+                    }
+                    amp[samples] = (int16_t) lrintf(xamp);
+                }
+            }
+            if (s->current_position)
+                return samples;
+        }
+        else if (tree->length)
+        {
+            /* A period of silence. The length must always
+               be explicitly stated. A length of zero does
+               not give infinite silence. */
+            len = tree->length - s->current_position;
+            if (len > max_samples - samples)
+            {
+                len = max_samples - samples;
+                s->current_position += len;
+            }
+            else
+            {
+                s->current_position = 0;
+            }
+            memset(amp + samples, 0, sizeof(uint16_t)*len);
+            samples += len;
+            if (s->current_position)
+                return samples;
+        }
+        /* Nesting has priority... */
+        if (tree->nest)
+        {
+            tree = tree->nest;
+            s->levels[++s->level] = tree;
+            s->cycles[s->level] = tree->cycles;
+        }
+        else
+        {
+            /* ...Next comes repeating, and finally moving forward a step. */
+            /* When repeating, note that zero cycles really means endless cycles. */
+            while (tree->cycles  &&  --s->cycles[s->level] <= 0)
+            {
+                tree = tree->next;
+                if (tree)
+                {
+                    /* A fresh new step. */
+                    s->levels[s->level] = tree;
+                    s->cycles[s->level] = tree->cycles;
+                    break;
+                }
+                /* If we are nested we need to pop, otherwise this is the end. */
+                if (s->level <= 0)
+                {
+                    /* Mark the tone as completed */
+                    s->levels[0] = NULL;
+                    break;
+                }
+                tree = s->levels[--s->level];
+            }
+        }
+        
+    }
+    return  samples;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/t30.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/t30.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,5826 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * t30.c - ITU T.30 FAX transfer processing
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2003, 2004, 2005, 2006, 2007 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: t30.c,v 1.262 2008/08/17 16:25:52 steveu Exp $
+ */
+
+/*! \file */
+
+#if defined(HAVE_CONFIG_H)
+#include <config.h>
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <inttypes.h>
+#include <string.h>
+#include <fcntl.h>
+#include <time.h>
+#include "floating_fudge.h"
+#if defined(HAVE_TGMATH_H)
+#include <tgmath.h>
+#endif
+#if defined(HAVE_MATH_H)
+#include <math.h>
+#endif
+#include <tiffio.h>
+
+#include "spandsp/telephony.h"
+#include "spandsp/logging.h"
+#include "spandsp/bit_operations.h"
+#include "spandsp/queue.h"
+#include "spandsp/power_meter.h"
+#include "spandsp/complex.h"
+#include "spandsp/tone_generate.h"
+#include "spandsp/async.h"
+#include "spandsp/hdlc.h"
+#include "spandsp/fsk.h"
+#include "spandsp/v29rx.h"
+#include "spandsp/v29tx.h"
+#include "spandsp/v27ter_rx.h"
+#include "spandsp/v27ter_tx.h"
+#include "spandsp/t4.h"
+#include "spandsp/t30_fcf.h"
+#include "spandsp/t35.h"
+#include "spandsp/t30.h"
+#include "spandsp/t30_api.h"
+#include "spandsp/t30_logging.h"
+
+#include "t30_local.h"
+
+#define MAX_MESSAGE_TRIES   3
+
+#define ms_to_samples(t)    (((t)*SAMPLE_RATE)/1000)
+
+/* T.30 defines the following call phases:
+   Phase A: Call set-up.
+       Exchange of CNG, CED and the called terminal identification.
+   Phase B: Pre-message procedure for identifying and selecting the required facilities.
+       Capabilities negotiation, and training, up the the confirmation to receive.
+   Phase C: Message transmission (includes phasing and synchronization where appropriate).
+       Transfer of the message at high speed.
+   Phase D: Post-message procedure, including end-of-message and confirmation and multi-document procedures.
+       End of message and acknowledgement.
+   Phase E: Call release
+       Final call disconnect. */
+enum
+{
+    T30_PHASE_IDLE = 0,     /* Freshly initialised */
+    T30_PHASE_A_CED,        /* Doing the CED (answer) sequence */
+    T30_PHASE_A_CNG,        /* Doing the CNG (caller) sequence */
+    T30_PHASE_B_RX,         /* Receiving pre-message control messages */
+    T30_PHASE_B_TX,         /* Transmitting pre-message control messages */
+    T30_PHASE_C_NON_ECM_RX, /* Receiving a document message in non-ECM mode */
+    T30_PHASE_C_NON_ECM_TX, /* Transmitting a document message in non-ECM mode */
+    T30_PHASE_C_ECM_RX,     /* Receiving a document message in ECM (HDLC) mode */
+    T30_PHASE_C_ECM_TX,     /* Transmitting a document message in ECM (HDLC) mode */
+    T30_PHASE_D_RX,         /* Receiving post-message control messages */
+    T30_PHASE_D_TX,         /* Transmitting post-message control messages */
+    T30_PHASE_E,            /* In phase E */
+    T30_PHASE_CALL_FINISHED /* Call completely finished */
+};
+
+static const char *phase_names[] =
+{
+    "T30_PHASE_IDLE",
+    "T30_PHASE_A_CED",
+    "T30_PHASE_A_CNG",
+    "T30_PHASE_B_RX",
+    "T30_PHASE_B_TX",
+    "T30_PHASE_C_NON_ECM_RX",
+    "T30_PHASE_C_NON_ECM_TX",
+    "T30_PHASE_C_ECM_RX",
+    "T30_PHASE_C_ECM_TX",
+    "T30_PHASE_D_RX",
+    "T30_PHASE_D_TX",
+    "T30_PHASE_E",
+    "T30_PHASE_CALL_FINISHED"
+};
+
+/* These state names are modelled after places in the T.30 flow charts. */
+enum
+{
+    T30_STATE_ANSWERING = 1,
+    T30_STATE_B,
+    T30_STATE_C,
+    T30_STATE_D,
+    T30_STATE_D_TCF,
+    T30_STATE_D_POST_TCF,
+    T30_STATE_F_TCF,
+    T30_STATE_F_CFR,
+    T30_STATE_F_FTT,
+    T30_STATE_F_DOC_NON_ECM,
+    T30_STATE_F_POST_DOC_NON_ECM,
+    T30_STATE_F_DOC_ECM,
+    T30_STATE_F_POST_DOC_ECM,
+    T30_STATE_F_POST_RCP_MCF,
+    T30_STATE_F_POST_RCP_PPR,
+    T30_STATE_F_POST_RCP_RNR,
+    T30_STATE_R,
+    T30_STATE_T,
+    T30_STATE_I,
+    T30_STATE_II,
+    T30_STATE_II_Q,
+    T30_STATE_III_Q_MCF,
+    T30_STATE_III_Q_RTP,
+    T30_STATE_III_Q_RTN,
+    T30_STATE_IV,
+    T30_STATE_IV_PPS_NULL,
+    T30_STATE_IV_PPS_Q,
+    T30_STATE_IV_PPS_RNR,
+    T30_STATE_IV_CTC,
+    T30_STATE_IV_EOR,
+    T30_STATE_IV_EOR_RNR,
+    T30_STATE_CALL_FINISHED
+};
+
+enum
+{
+    T30_MIN_SCAN_20MS = 0,
+    T30_MIN_SCAN_5MS = 1,
+    T30_MIN_SCAN_10MS = 2,
+    T30_MIN_SCAN_40MS = 4,
+    T30_MIN_SCAN_0MS = 7,
+};
+
+enum
+{
+    T30_MODE_SEND_DOC = 1,
+    T30_MODE_RECEIVE_DOC
+};
+
+/*! These are internal assessments of received image quality, used to determine whether we
+    continue, retrain, or abandon the call. */
+enum
+{
+    T30_COPY_QUALITY_PERFECT = 0,
+    T30_COPY_QUALITY_GOOD,
+    T30_COPY_QUALITY_POOR,
+    T30_COPY_QUALITY_BAD
+};
+
+enum
+{
+    DISBIT1 = 0x01,
+    DISBIT2 = 0x02,
+    DISBIT3 = 0x04,
+    DISBIT4 = 0x08,
+    DISBIT5 = 0x10,
+    DISBIT6 = 0x20,
+    DISBIT7 = 0x40,
+    DISBIT8 = 0x80
+};
+
+/*! There are high level indications of what is happening at any instant, to guide the cleanup
+    process if the call is abandoned. */
+enum
+{
+    OPERATION_IN_PROGRESS_NONE = 0,
+    OPERATION_IN_PROGRESS_T4_RX,
+    OPERATION_IN_PROGRESS_T4_TX
+};
+
+/* All timers specified in milliseconds */
+
+/* Time-out T0 defines the amount of time an automatic calling terminal waits for the called terminal
+to answer the call.
+T0 begins after the dialling of the number is completed and is reset:
+a) when T0 times out; or
+b) when timer T1 is started; or
+c) if the terminal is capable of detecting any condition which indicates that the call will not be
+   successful, when such a condition is detected.
+The recommended value of T0 is 60+-5s. However, when it is anticipated that a long call set-up
+time may be encountered, an alternative value of up to 120s may be used.
+NOTE - National regulations may require the use of other values for T0. */
+#define DEFAULT_TIMER_T0                60000
+
+/* Time-out T1 defines the amount of time two terminals will continue to attempt to identify each
+other. T1 is 35+-5s, begins upon entering phase B, and is reset upon detecting a valid signal or
+when T1 times out.
+For operating methods 3 and 4 (see 3.1), the calling terminal starts time-out T1 upon reception of
+the V.21 modulation scheme.
+For operating method 4 bis a (see 3.1), the calling terminal starts time-out T1 upon starting
+transmission using the V.21 modulation scheme. */
+#define DEFAULT_TIMER_T1                35000
+
+/* Time-out T2 makes use of the tight control between commands and responses to detect the loss of
+command/response synchronization. T2 is 6+-1s, and begins when initiating a command search
+(e.g., the first entrance into the "command received" subroutine, reference flow diagram in section 5.2).
+T2 is reset when an HDLC flag is received or when T2 times out. */
+#define DEFAULT_TIMER_T2                7000
+
+/* Once HDLC flags begin, T2 is reset, and a 3s timer begins. This timer is unnamed in T.30. Here we
+term it T2A. No tolerance is specified for this timer. T2A specifies the maximum time to wait for the
+end of a frame, after the initial flag has been seen. */
+#define DEFAULT_TIMER_T2A               3000
+
+/* If the HDLC carrier falls during reception, we need to apply a minimum time before continuing. if we
+   don't, there are circumstances where we could continue and reply before the incoming signals have
+   really finished. E.g. if a bad DCS is received in a DCS-TCF sequence, we need wait for the TCF
+   carrier to pass, before continuing. This timer is specified as 200ms, but no tolerance is specified.
+   It is unnamed in T.30. Here we termin it T2B */
+#define DEFAULT_TIMER_T2B               200
+
+/* Time-out T3 defines the amount of time a terminal will attempt to alert the local operator in
+response to a procedural interrupt. Failing to achieve operator intervention, the terminal will
+discontinue this attempt and shall issue other commands or responses. T3 is 10+-5s, begins on the
+first detection of a procedural interrupt command/response signal (i.e., PIN/PIP or PRI-Q) and is
+reset when T3 times out or when the operator initiates a line request. */
+#define DEFAULT_TIMER_T3                15000
+
+/* Time-out T4 defines the amount of time a terminal will wait for flags to begin, when waiting for a
+response from a remote terminal. T2 is 3s +-15%, and begins when initiating a response search
+(e.g., the first entrance into the "response received" subroutine, reference flow diagram in section 5.2).
+T4 is reset when an HDLC flag is received or when T4 times out.
+NOTE - For manual FAX units, the value of timer T4 may be either 3.0s +-15% or 4.5s +-15%.
+If the value of 4.5s is used, then after detection of a valid response to the first DIS, it may
+be reduced to 3.0s +-15%. T4 = 3.0s +-15% for automatic units. */
+#define DEFAULT_TIMER_T4                3450
+
+/* Once HDLC flags begin, T4 is reset, and a 3s timer begins. This timer is unnamed in T.30. Here we
+term it T4A. No tolerance is specified for this timer. T4A specifies the maximum time to wait for the
+end of a frame, after the initial flag has been seen. */
+#define DEFAULT_TIMER_T4A               3000
+
+/* If the HDLC carrier falls during reception, we need to apply a minimum time before continuing. if we
+   don't, there are circumstances where we could continue and reply before the incoming signals have
+   really finished. E.g. if a bad DCS is received in a DCS-TCF sequence, we need wait for the TCF
+   carrier to pass, before continuing. This timer is specified as 200ms, but no tolerance is specified.
+   It is unnamed in T.30. Here we termin it T4B */
+#define DEFAULT_TIMER_T4B               200
+
+/* Time-out T5 is defined for the optional T.4 error correction mode. Time-out T5 defines the amount
+of time waiting for clearance of the busy condition of the receiving terminal. T5 is 60+-5s and
+begins on the first detection of the RNR response. T5 is reset when T5 times out or the MCF or PIP
+response is received or when the ERR or PIN response is received in the flow control process after
+transmitting the EOR command. If the timer T5 has expired, the DCN command is transmitted for
+call release. */
+#define DEFAULT_TIMER_T5                65000
+
+/* (Annex C - ISDN) Time-out T6 defines the amount of time two terminals will continue to attempt to
+identify each other. T6 is 5+-0.5s. The timeout begins upon entering Phase B, and is reset upon
+detecting a valid signal, or when T6 times out. */
+#define DEFAULT_TIMER_T6                5000
+
+/* (Annex C - ISDN) Time-out T7 is used to detect loss of command/response synchronization. T7 is 6+-1s.
+The timeout begins when initiating a command search (e.g., the first entrance into the "command received"
+subroutine – see flow diagram in C.5) and is reset upon detecting a valid signal or when T7 times out. */
+#define DEFAULT_TIMER_T7                7000
+
+/* (Annex C - ISDN) Time-out T8 defines the amount of time waiting for clearance of the busy condition
+of the receiving terminal. T8 is 10+-1s. The timeout begins on the first detection of the combination
+of no outstanding corrections and the RNR response. T8 is reset when T8 times out or MCF response is
+received. If the timer T8 expires, a DCN command is transmitted for call release. */
+#define DEFAULT_TIMER_T8                10000
+
+/* Final time we allow for things to flush through the system, before we disconnect, in milliseconds.
+   200ms should be fine for a PSTN call. For a T.38 call something longer is desirable. */
+#define FINAL_FLUSH_TIME                1000
+
+/* The number of PPRs received before CTC or EOR is sent in ECM mode. T.30 defines this as 4,
+   but it could be varied, and the Japanese spec, for example, does make this value a
+   variable. */
+#define PPR_LIMIT_BEFORE_CTC_OR_EOR     4
+
+enum
+{
+    TIMER_IS_T2 = 0,
+    TIMER_IS_T2A = 1,
+    TIMER_IS_T2B = 2,
+    TIMER_IS_T2C = 3,
+    TIMER_IS_T4 = 4,
+    TIMER_IS_T4A = 5,
+    TIMER_IS_T4B = 6,
+    TIMER_IS_T4C = 7
+};
+
+/* Start points in the fallback table for different capabilities */
+#define T30_V17_FALLBACK_START          0
+#define T30_V29_FALLBACK_START          3
+#define T30_V27TER_FALLBACK_START       6
+
+static const struct
+{
+    int bit_rate;
+    int modem_type;
+    int which;
+    uint8_t dcs_code;
+} fallback_sequence[] =
+{
+    {14400, T30_MODEM_V17,      T30_SUPPORT_V17,    DISBIT6},
+    {12000, T30_MODEM_V17,      T30_SUPPORT_V17,    (DISBIT6 | DISBIT4)},
+    { 9600, T30_MODEM_V17,      T30_SUPPORT_V17,    (DISBIT6 | DISBIT3)},
+    { 9600, T30_MODEM_V29,      T30_SUPPORT_V29,    DISBIT3},
+    { 7200, T30_MODEM_V17,      T30_SUPPORT_V17,    (DISBIT6 | DISBIT4 | DISBIT3)},
+    { 7200, T30_MODEM_V29,      T30_SUPPORT_V29,    (DISBIT4 | DISBIT3)},
+    { 4800, T30_MODEM_V27TER,   T30_SUPPORT_V27TER, DISBIT4},
+    { 2400, T30_MODEM_V27TER,   T30_SUPPORT_V27TER, 0},
+    {    0, 0, 0, 0}
+};
+
+static void queue_phase(t30_state_t *s, int phase);
+static void set_phase(t30_state_t *s, int phase);
+static void set_state(t30_state_t *s, int state);
+static void send_simple_frame(t30_state_t *s, int type);
+static void send_frame(t30_state_t *s, const uint8_t *fr, int frlen);
+static void send_dcn(t30_state_t *s);
+static void repeat_last_command(t30_state_t *s);
+static void disconnect(t30_state_t *s);
+static void decode_20digit_msg(t30_state_t *s, char *msg, const uint8_t *pkt, int len);
+static void decode_url_msg(t30_state_t *s, char *msg, const uint8_t *pkt, int len);
+static int set_min_scan_time_code(t30_state_t *s);
+static int send_cfr_sequence(t30_state_t *s, int start);
+static void timer_t2_start(t30_state_t *s);
+static void timer_t2a_start(t30_state_t *s);
+static void timer_t2b_start(t30_state_t *s);
+static void timer_t4_start(t30_state_t *s);
+static void timer_t4a_start(t30_state_t *s);
+static void timer_t4b_start(t30_state_t *s);
+
+#define test_ctrl_bit(s,bit) ((s)[3 + ((bit - 1)/8)] & (1 << ((bit - 1)%8)))
+#define set_ctrl_bit(s,bit) (s)[3 + ((bit - 1)/8)] |= (1 << ((bit - 1)%8))
+#define set_ctrl_bits(s,val,bit) (s)[3 + ((bit - 1)/8)] |= ((val) << ((bit - 1)%8))
+#define clr_ctrl_bit(s,bit) (s)[3 + ((bit - 1)/8)] &= ~(1 << ((bit - 1)%8))
+
+static int tx_start_page(t30_state_t *s)
+{
+    if (t4_tx_start_page(&(s->t4)))
+    {
+        t4_tx_end(&(s->t4));
+        s->operation_in_progress = OPERATION_IN_PROGRESS_NONE;
+        return -1;
+    }
+    s->ecm_tx_page++;
+    s->ecm_block = 0;
+    span_log(&s->logging, SPAN_LOG_FLOW, "Starting page %d of transfer\n", s->ecm_tx_page + 1);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int rx_start_page(t30_state_t *s)
+{
+    int i;
+
+    t4_rx_set_image_width(&s->t4, s->image_width);
+    t4_rx_set_sub_address(&s->t4, s->rx_info.sub_address);
+    t4_rx_set_dcs(&s->t4, s->rx_dcs_string);
+    t4_rx_set_far_ident(&s->t4, s->rx_info.ident);
+    t4_rx_set_vendor(&s->t4, s->vendor);
+    t4_rx_set_model(&s->t4, s->model);
+
+    t4_rx_set_rx_encoding(&s->t4, s->line_encoding);
+    t4_rx_set_x_resolution(&s->t4, s->x_resolution);
+    t4_rx_set_y_resolution(&s->t4, s->y_resolution);
+
+    if (t4_rx_start_page(&(s->t4)))
+        return -1;
+    /* Clear the buffer */
+    for (i = 0;  i < 256;  i++)
+        s->ecm_len[i] = -1;
+    s->ecm_rx_page++;
+    s->ecm_block = 0;
+    s->ecm_frames = -1;
+    s->ecm_frames_this_tx_burst = 0;
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int copy_quality(t30_state_t *s)
+{
+    t4_stats_t stats;
+    int quality;
+
+    t4_get_transfer_statistics(&s->t4, &stats);
+    /* There is no specification for judging copy quality. However, we need to classify
+       it at three levels, to control what we do next: OK; tolerable, but retrain;
+       intolerable. */
+    /* Based on the thresholds used in the TSB85 tests, we consider:
+            <5% bad rows is OK
+            <15% bad rows to be tolerable, but retrain
+            >15% bad rows to be intolerable
+     */
+    span_log(&s->logging, SPAN_LOG_FLOW, "Page no = %d\n", stats.pages_transferred + 1);
+    span_log(&s->logging, SPAN_LOG_FLOW, "Image size = %d x %d pixels\n", stats.width, stats.length);
+    span_log(&s->logging, SPAN_LOG_FLOW, "Image resolution = %d/m x %d/m\n", stats.x_resolution, stats.y_resolution);
+    span_log(&s->logging, SPAN_LOG_FLOW, "Bad rows = %d\n", stats.bad_rows);
+    span_log(&s->logging, SPAN_LOG_FLOW, "Longest bad row run = %d\n", stats.longest_bad_row_run);
+    if (stats.bad_rows == 0)
+    {
+        span_log(&s->logging, SPAN_LOG_FLOW, "Page quality is perfect\n");
+        quality = T30_COPY_QUALITY_PERFECT;
+    }
+    else if (stats.bad_rows*20 < stats.length)
+    {
+        span_log(&s->logging, SPAN_LOG_FLOW, "Page quality is good\n");
+        quality = T30_COPY_QUALITY_GOOD;
+    }
+    else if (stats.bad_rows*20 < stats.length*3)
+    {
+        span_log(&s->logging, SPAN_LOG_FLOW, "Page quality is poor\n");
+        quality = T30_COPY_QUALITY_POOR;
+    }
+    else
+    {
+        span_log(&s->logging, SPAN_LOG_FLOW, "Page quality is bad\n");
+        quality = T30_COPY_QUALITY_BAD;
+    }
+    return quality;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void release_resources(t30_state_t *s)
+{
+    if (s->tx_info.nsf)
+    {
+        free(s->tx_info.nsf);
+        s->tx_info.nsf = NULL;
+    }
+    s->tx_info.nsf_len = 0;
+    if (s->tx_info.nsc)
+    {
+        free(s->tx_info.nsc);
+        s->tx_info.nsc = NULL;
+    }
+    s->tx_info.nsc_len = 0;
+    if (s->tx_info.nss)
+    {
+        free(s->tx_info.nss);
+        s->tx_info.nss = NULL;
+    }
+    s->tx_info.nss_len = 0;
+    if (s->tx_info.tsa)
+    {
+        free(s->tx_info.tsa);
+        s->tx_info.tsa = NULL;
+    }
+    if (s->tx_info.ira)
+    {
+        free(s->tx_info.ira);
+        s->tx_info.ira = NULL;
+    }
+    if (s->tx_info.cia)
+    {
+        free(s->tx_info.cia);
+        s->tx_info.cia = NULL;
+    }
+    if (s->tx_info.isp)
+    {
+        free(s->tx_info.isp);
+        s->tx_info.isp = NULL;
+    }
+    if (s->tx_info.csa)
+    {
+        free(s->tx_info.csa);
+        s->tx_info.csa = NULL;
+    }
+
+    if (s->rx_info.nsf)
+    {
+        free(s->rx_info.nsf);
+        s->rx_info.nsf = NULL;
+    }
+    s->rx_info.nsf_len = 0;
+    if (s->rx_info.nsc)
+    {
+        free(s->rx_info.nsc);
+        s->rx_info.nsc = NULL;
+    }
+    s->rx_info.nsc_len = 0;
+    if (s->rx_info.nss)
+    {
+        free(s->rx_info.nss);
+        s->rx_info.nss = NULL;
+    }
+    s->rx_info.nss_len = 0;
+    if (s->rx_info.tsa)
+    {
+        free(s->rx_info.tsa);
+        s->rx_info.tsa = NULL;
+    }
+    if (s->rx_info.ira)
+    {
+        free(s->rx_info.ira);
+        s->rx_info.ira = NULL;
+    }
+    if (s->rx_info.cia)
+    {
+        free(s->rx_info.cia);
+        s->rx_info.cia = NULL;
+    }
+    if (s->rx_info.isp)
+    {
+        free(s->rx_info.isp);
+        s->rx_info.isp = NULL;
+    }
+    if (s->rx_info.csa)
+    {
+        free(s->rx_info.csa);
+        s->rx_info.csa = NULL;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static int check_next_tx_step(t30_state_t *s)
+{
+    int more;
+
+    if (t4_tx_more_pages(&(s->t4)) == 0)
+        return (s->local_interrupt_pending)  ?  T30_PRI_MPS  :  T30_MPS;
+    /* Call a user handler, if one is set, to check if another document is to be sent.
+       If so, we send an EOM, rather than an EOP. Then we will renegotiate, and the new
+       document will begin. */
+    if (s->document_handler)
+        more = s->document_handler(s, s->document_user_data, 0);
+    else
+        more = FALSE;
+    if (more)
+    {
+        //if (test_ctrl_bit(s->far_dis_dtc_frame, 34))
+        //    return T30_EOS;
+        return (s->local_interrupt_pending)  ?  T30_PRI_EOM  :  T30_EOM;
+    }
+    return (s->local_interrupt_pending)  ?  T30_PRI_EOP  :  T30_EOP;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int get_partial_ecm_page(t30_state_t *s)
+{
+    int i;
+    int len;
+
+    s->ppr_count = 0;
+    s->ecm_progress = 0;
+    /* Fill our partial page buffer with a partial page. Use the negotiated preferred frame size
+       as the basis for the size of the frames produced. */
+    /* We fill the buffer with complete HDLC frames, ready to send out. */
+    /* The frames are all marked as not being final frames. When sent, the are followed by a partial
+       page signal, which is marked as the final frame. */
+    for (i = 3;  i < 32 + 3;  i++)
+        s->ecm_frame_map[i] = 0xFF;
+    for (i = 0;  i < 256;  i++)
+    {
+        s->ecm_len[i] = -1;
+        s->ecm_data[i][0] = 0xFF;
+        s->ecm_data[i][1] = 0x03;
+        s->ecm_data[i][2] = T4_FCD;
+        /* These frames contain a frame sequence number within the partial page (one octet) followed
+           by some image data. */
+        s->ecm_data[i][3] = (uint8_t) i;
+        if ((len = t4_tx_get_chunk(&s->t4, &s->ecm_data[i][4], s->octets_per_ecm_frame)) < s->octets_per_ecm_frame)
+        {
+            /* The image is not big enough to fill the entire buffer */
+            /* We need to pad to a full frame, as most receivers expect that. */
+            if (len > 0)
+            {
+                memset(&s->ecm_data[i][4 + len], 0, s->octets_per_ecm_frame - len);
+                s->ecm_len[i++] = (int16_t) (s->octets_per_ecm_frame + 4);
+            }
+            s->ecm_frames = i;
+            span_log(&s->logging, SPAN_LOG_FLOW, "Partial page buffer contains %d frames (%d per frame)\n", i, s->octets_per_ecm_frame);
+            s->ecm_at_page_end = TRUE;
+            return i;
+        }
+        s->ecm_len[i] = (int16_t) 4 + len;
+    }
+    /* We filled the entire buffer */
+    s->ecm_frames = 256;
+    span_log(&s->logging, SPAN_LOG_FLOW, "Partial page buffer full (%d per frame)\n", s->octets_per_ecm_frame);
+    s->ecm_at_page_end = ((t4_tx_check_bit(&(s->t4)) & 2) != 0);
+    return 256;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int t30_ecm_commit_partial_page(t30_state_t *s)
+{
+    int i;
+    int image_ended;
+
+    span_log(&s->logging, SPAN_LOG_FLOW, "Commiting partial page - block %d, %d frames\n", s->ecm_block, s->ecm_frames);
+    image_ended = FALSE;
+    for (i = 0;  i < s->ecm_frames;  i++)
+    {
+        if (t4_rx_put_chunk(&s->t4, s->ecm_data[i], s->ecm_len[i]))
+        {
+            /* This is the end of the document */
+            /* Clear the buffer */
+            for (i = 0;  i < 256;  i++)
+                s->ecm_len[i] = -1;
+            s->ecm_frames = -1;
+            image_ended = TRUE;
+            break;
+        }
+    }
+    /* Clear the buffer */
+    for (i = 0;  i < 256;  i++)
+        s->ecm_len[i] = -1;
+    s->ecm_block++;
+    s->ecm_frames = -1;
+    return (image_ended)  ?  -1  :  0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int send_next_ecm_frame(t30_state_t *s)
+{
+    int i;
+    uint8_t frame[3];
+
+    if (s->ecm_current_tx_frame < s->ecm_frames)
+    {
+        /* Search for the next frame, within the current partial page, which has
+           not been tagged as transferred OK. */
+        for (i = s->ecm_current_tx_frame;  i < s->ecm_frames;  i++)
+        {
+            if (s->ecm_len[i] >= 0)
+            {
+                send_frame(s, s->ecm_data[i], s->ecm_len[i]);
+                s->ecm_current_tx_frame = i + 1;
+                s->ecm_frames_this_tx_burst++;
+                return 0;
+            }
+        }
+        s->ecm_current_tx_frame = s->ecm_frames;
+    }
+    if (s->ecm_current_tx_frame <= s->ecm_frames + 3)
+    {
+        /* We have sent all the FCD frames. Send some RCP frames. Three seems to be
+           a popular number, to minimise the risk of a bit error stopping the receiving
+           end from recognising the RCP. */
+        s->ecm_current_tx_frame++;
+        /* The RCP frame is an odd man out, as its a simple 1 byte control
+           frame, but is specified to not have the final bit set. It doesn't
+           seem to have the DIS received bit set, either. */
+        frame[0] = 0xFF;
+        frame[1] = 0x03;
+        frame[2] = T4_RCP;
+        send_frame(s, frame, 3);
+        /* In case we are just after a CTC/CTR exchange, which kicked us back to long training */
+        s->short_train = TRUE;
+        return 0;
+    }
+    return -1;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int send_first_ecm_frame(t30_state_t *s)
+{
+    s->ecm_current_tx_frame = 0;
+    s->ecm_frames_this_tx_burst = 0;
+    return send_next_ecm_frame(s);
+}
+/*- End of function --------------------------------------------------------*/
+
+static void print_frame(t30_state_t *s, const char *io, const uint8_t *msg, int len)
+{
+    span_log(&s->logging,
+             SPAN_LOG_FLOW,
+             "%s %s with%s final frame tag\n",
+             io,
+             t30_frametype(msg[2]),
+             (msg[1] & 0x10)  ?  ""  :  "out");
+    span_log_buf(&s->logging, SPAN_LOG_FLOW, io, msg, len);
+}
+/*- End of function --------------------------------------------------------*/
+
+static void send_frame(t30_state_t *s, const uint8_t *msg, int len)
+{
+    print_frame(s, "Tx: ", msg, len);
+
+    if (s->real_time_frame_handler)
+        s->real_time_frame_handler(s, s->real_time_frame_user_data, FALSE, msg, len);
+    if (s->send_hdlc_handler)
+        s->send_hdlc_handler(s->send_hdlc_user_data, msg, len);
+}
+/*- End of function --------------------------------------------------------*/
+
+static void send_simple_frame(t30_state_t *s, int type)
+{
+    uint8_t frame[3];
+
+    /* The simple command/response frames are always final frames */
+    frame[0] = 0xFF;
+    frame[1] = 0x13;
+    frame[2] = (uint8_t) (type | s->dis_received);
+    send_frame(s, frame, 3);
+}
+/*- End of function --------------------------------------------------------*/
+
+static void send_20digit_msg_frame(t30_state_t *s, int cmd, char *msg)
+{
+    size_t len;
+    int p;
+    uint8_t frame[23];
+
+    len = strlen(msg);
+    p = 0;
+    frame[p++] = 0xFF;
+    frame[p++] = 0x03;
+    frame[p++] = (uint8_t) (cmd | s->dis_received);
+    while (len > 0)
+        frame[p++] = msg[--len];
+    while (p < 23)
+        frame[p++] = ' ';
+    send_frame(s, frame, 23);
+}
+/*- End of function --------------------------------------------------------*/
+
+static int send_nsf_frame(t30_state_t *s)
+{
+    /* Only send if there is an NSF message to send. */
+    if (s->tx_info.nsf  &&  s->tx_info.nsf_len)
+    {
+        span_log(&s->logging, SPAN_LOG_FLOW, "Sending user supplied NSF - %d octets\n", s->tx_info.nsf_len);
+        s->tx_info.nsf[0] = 0xFF;
+        s->tx_info.nsf[1] = 0x03;
+        s->tx_info.nsf[2] = (uint8_t) (T30_NSF | s->dis_received);
+        send_frame(s, s->tx_info.nsf, s->tx_info.nsf_len + 3);
+        return TRUE;
+    }
+    return FALSE;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int send_nss_frame(t30_state_t *s)
+{
+    /* Only send if there is an NSF message to send. */
+    if (s->tx_info.nss  &&  s->tx_info.nss_len)
+    {
+        span_log(&s->logging, SPAN_LOG_FLOW, "Sending user supplied NSS - %d octets\n", s->tx_info.nss_len);
+        s->tx_info.nss[0] = 0xFF;
+        s->tx_info.nss[1] = 0x03;
+        s->tx_info.nss[2] = (uint8_t) (T30_NSS | s->dis_received);
+        send_frame(s, s->tx_info.nss, s->tx_info.nss_len + 3);
+        return TRUE;
+    }
+    return FALSE;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int send_nsc_frame(t30_state_t *s)
+{
+    /* Only send if there is an NSF message to send. */
+    if (s->tx_info.nsc  &&  s->tx_info.nsc_len)
+    {
+        span_log(&s->logging, SPAN_LOG_FLOW, "Sending user supplied NSC - %d octets\n", s->tx_info.nsc_len);
+        s->tx_info.nsc[0] = 0xFF;
+        s->tx_info.nsc[1] = 0x03;
+        s->tx_info.nsc[2] = (uint8_t) (T30_NSC | s->dis_received);
+        send_frame(s, s->tx_info.nsc, s->tx_info.nsc_len + 3);
+        return TRUE;
+    }
+    return FALSE;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int send_ident_frame(t30_state_t *s, uint8_t cmd)
+{
+    if (s->tx_info.ident[0])
+    {
+        span_log(&s->logging, SPAN_LOG_FLOW, "Sending ident '%s'\n", s->tx_info.ident);
+        /* 'cmd' should be T30_TSI, T30_CIG or T30_CSI */
+        send_20digit_msg_frame(s, cmd, s->tx_info.ident);
+        return TRUE;
+    }
+    return FALSE;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int send_psa_frame(t30_state_t *s)
+{
+    if (test_ctrl_bit(s->far_dis_dtc_frame, 35)  &&  s->tx_info.polled_sub_address[0])
+    {
+        span_log(&s->logging, SPAN_LOG_FLOW, "Sending polled sub-address '%s'\n", s->tx_info.polled_sub_address);
+        send_20digit_msg_frame(s, T30_PSA, s->tx_info.polled_sub_address);
+        set_ctrl_bit(s->local_dis_dtc_frame, 35);
+        return TRUE;
+    }
+    clr_ctrl_bit(s->local_dis_dtc_frame, 35);
+    return FALSE;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int send_sep_frame(t30_state_t *s)
+{
+    if (test_ctrl_bit(s->far_dis_dtc_frame, 47)  &&  s->tx_info.selective_polling_address[0])
+    {
+        span_log(&s->logging, SPAN_LOG_FLOW, "Sending selective polling address '%s'\n", s->tx_info.selective_polling_address);
+        send_20digit_msg_frame(s, T30_SEP, s->tx_info.selective_polling_address);
+        set_ctrl_bit(s->local_dis_dtc_frame, 47);
+        return TRUE;
+    }
+    clr_ctrl_bit(s->local_dis_dtc_frame, 47);
+    return FALSE;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int send_sid_frame(t30_state_t *s)
+{
+    /* Only send if there is an ID to send. */
+    if (test_ctrl_bit(s->far_dis_dtc_frame, 50)  &&  s->tx_info.sender_ident[0])
+    {
+        span_log(&s->logging, SPAN_LOG_FLOW, "Sending sender identification '%s'\n", s->tx_info.sender_ident);
+        send_20digit_msg_frame(s, T30_SID, s->tx_info.sender_ident);
+        set_ctrl_bit(s->dcs_frame, 50);
+        return TRUE;
+    }
+    clr_ctrl_bit(s->dcs_frame, 50);
+    return FALSE;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int send_pwd_frame(t30_state_t *s)
+{
+    /* Only send if there is a password to send. */
+    if (test_ctrl_bit(s->far_dis_dtc_frame, 50)  &&  s->tx_info.password[0])
+    {
+        span_log(&s->logging, SPAN_LOG_FLOW, "Sending password '%s'\n", s->tx_info.password);
+        send_20digit_msg_frame(s, T30_PWD, s->tx_info.password);
+        set_ctrl_bit(s->local_dis_dtc_frame, 50);
+        return TRUE;
+    }
+    clr_ctrl_bit(s->local_dis_dtc_frame, 50);
+    return FALSE;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int send_sub_frame(t30_state_t *s)
+{
+    /* Only send if there is a sub-address to send. */
+    if (test_ctrl_bit(s->far_dis_dtc_frame, 49)  &&  s->tx_info.sub_address[0])
+    {
+        span_log(&s->logging, SPAN_LOG_FLOW, "Sending sub-address '%s'\n", s->tx_info.sub_address);
+        send_20digit_msg_frame(s, T30_SUB, s->tx_info.sub_address);
+        set_ctrl_bit(s->dcs_frame, 49);
+        return TRUE;
+    }
+    clr_ctrl_bit(s->dcs_frame, 49);
+    return FALSE;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int send_tsa_frame(t30_state_t *s)
+{
+    if ((test_ctrl_bit(s->far_dis_dtc_frame, 1)  ||  test_ctrl_bit(s->far_dis_dtc_frame, 3))  &&  0)
+    {
+        span_log(&s->logging, SPAN_LOG_FLOW, "Sending transmitting subscriber internet address '%s'\n", "");
+        return TRUE;
+    }
+    return FALSE;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int send_ira_frame(t30_state_t *s)
+{
+    if (test_ctrl_bit(s->far_dis_dtc_frame, 102)  &&  0)
+    {
+        span_log(&s->logging, SPAN_LOG_FLOW, "Sending internet routing address '%s'\n", "");
+        set_ctrl_bit(s->dcs_frame, 102);
+        return TRUE;
+    }
+    clr_ctrl_bit(s->dcs_frame, 102);
+    return FALSE;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int send_cia_frame(t30_state_t *s)
+{
+    if ((test_ctrl_bit(s->far_dis_dtc_frame, 1)  ||  test_ctrl_bit(s->far_dis_dtc_frame, 3))  &&  0)
+    {
+        span_log(&s->logging, SPAN_LOG_FLOW, "Sending calling subscriber internet address '%s'\n", "");
+        return TRUE;
+    }
+    return FALSE;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int send_isp_frame(t30_state_t *s)
+{
+    if (test_ctrl_bit(s->far_dis_dtc_frame, 101)  &&  0)
+    {
+        span_log(&s->logging, SPAN_LOG_FLOW, "Sending internet selective polling address '%s'\n", "");
+        set_ctrl_bit(s->local_dis_dtc_frame, 101);
+        return TRUE;
+    }
+    clr_ctrl_bit(s->local_dis_dtc_frame, 101);
+    return FALSE;
+}
+/*- End of function --------------------------------------------------------*/
+
+#if 0
+static int send_csa_frame(t30_state_t *s)
+{
+#if 0
+    if (("in T.37 mode"  ||  "in T.38 mode")  &&  0)
+    {
+        span_log(&s->logging, SPAN_LOG_FLOW, "Sending called subscriber internet address '%s'\n", "");
+        return TRUE;
+    }
+#endif
+    return FALSE;
+}
+/*- End of function --------------------------------------------------------*/
+#endif
+
+static int send_pps_frame(t30_state_t *s)
+{
+    uint8_t frame[7];
+    
+    frame[0] = 0xFF;
+    frame[1] = 0x13;
+    frame[2] = (uint8_t) (T30_PPS | s->dis_received);
+    frame[3] = (s->ecm_at_page_end)  ?  ((uint8_t) (s->next_tx_step | s->dis_received))  :  T30_NULL;
+    frame[4] = (uint8_t) (s->ecm_tx_page & 0xFF);
+    frame[5] = (uint8_t) (s->ecm_block & 0xFF);
+    frame[6] = (uint8_t) ((s->ecm_frames_this_tx_burst == 0)  ?  0  :  (s->ecm_frames_this_tx_burst - 1));
+    span_log(&s->logging, SPAN_LOG_FLOW, "Sending PPS + %s\n", t30_frametype(frame[3]));
+    send_frame(s, frame, 7);
+    return frame[3] & 0xFE;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int set_dis_or_dtc(t30_state_t *s)
+{
+    /* Whether we use a DIS or a DTC is determined by whether we have received a DIS.
+       We just need to edit the prebuilt message. */
+    s->local_dis_dtc_frame[2] = (uint8_t) (T30_DIS | s->dis_received);
+    /* If we have a file name to receive into, then we are receive capable */
+    if (s->rx_file[0])
+        set_ctrl_bit(s->local_dis_dtc_frame, 10);
+    else
+        clr_ctrl_bit(s->local_dis_dtc_frame, 10);
+    /* If we have a file name to transmit, then we are ready to transmit (polling) */
+    if (s->tx_file[0])
+        set_ctrl_bit(s->local_dis_dtc_frame, 9);
+    else
+        clr_ctrl_bit(s->local_dis_dtc_frame, 9);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int t30_build_dis_or_dtc(t30_state_t *s)
+{
+    int i;
+
+    /* Build a skeleton for the DIS and DTC messages. This will be edited for
+       the dynamically changing capabilities (e.g. can receive) just before
+       it is sent. It might also be edited if the application changes our
+       capabilities (e.g. disabling fine mode). Right now we set up all the
+       unchanging stuff about what we are capable of doing. */
+    s->local_dis_dtc_frame[0] = 0xFF;
+    s->local_dis_dtc_frame[1] = 0x13;
+    s->local_dis_dtc_frame[2] = (uint8_t) (T30_DIS | s->dis_received);
+    for (i = 3;  i < 19;  i++)
+        s->local_dis_dtc_frame[i] = 0x00;
+
+    /* Always say 256 octets per ECM frame preferred, as 64 is never used in the
+       real world. */
+    if ((s->iaf & T30_IAF_MODE_T37))
+        set_ctrl_bit(s->local_dis_dtc_frame, 1);
+    if ((s->iaf & T30_IAF_MODE_T38))
+        set_ctrl_bit(s->local_dis_dtc_frame, 3);
+    /* No 3G mobile  */
+    /* No V.8 */
+    /* 256 octets preferred - don't bother making this optional, as everything uses 256 */
+    /* Ready to transmit a fax (polling) will be determined separately, and this message edited. */
+    /* Ready to receive a fax will be determined separately, and this message edited. */
+    /* With no modems set we are actually selecting V.27ter fallback at 2400bps */
+    if ((s->supported_modems & T30_SUPPORT_V27TER))
+        set_ctrl_bit(s->local_dis_dtc_frame, 12);
+    if ((s->supported_modems & T30_SUPPORT_V29))
+        set_ctrl_bit(s->local_dis_dtc_frame, 11);
+    /* V.17 is only valid when combined with V.29 and V.27ter, so if we enable V.17 we force the others too. */
+    if ((s->supported_modems & T30_SUPPORT_V17))
+        s->local_dis_dtc_frame[4] |= (DISBIT6 | DISBIT4 | DISBIT3);
+    if ((s->supported_resolutions & T30_SUPPORT_FINE_RESOLUTION))
+        set_ctrl_bit(s->local_dis_dtc_frame, 15);
+    if ((s->supported_compressions & T30_SUPPORT_T4_2D_COMPRESSION))
+        set_ctrl_bit(s->local_dis_dtc_frame, 16);
+    /* 215mm wide is always supported */
+    if ((s->supported_image_sizes & T30_SUPPORT_303MM_WIDTH))
+        set_ctrl_bit(s->local_dis_dtc_frame, 18);
+    else if ((s->supported_image_sizes & T30_SUPPORT_255MM_WIDTH))
+        set_ctrl_bit(s->local_dis_dtc_frame, 17);
+    /* A4 is always supported. */
+    if ((s->supported_image_sizes & T30_SUPPORT_UNLIMITED_LENGTH))
+        set_ctrl_bit(s->local_dis_dtc_frame, 20);
+    else if ((s->supported_image_sizes & T30_SUPPORT_B4_LENGTH))
+        set_ctrl_bit(s->local_dis_dtc_frame, 19);
+    /* No scan-line padding required, but some may be specified by the application. */
+    set_ctrl_bits(s->local_dis_dtc_frame, s->local_min_scan_time_code, 21);
+    if ((s->supported_compressions & T30_SUPPORT_NO_COMPRESSION))
+        set_ctrl_bit(s->local_dis_dtc_frame, 26);
+    if (s->ecm_allowed)
+    {
+        /* ECM allowed */
+        set_ctrl_bit(s->local_dis_dtc_frame, 27);
+        /* Only offer the option of fancy compression schemes, if we are
+           also offering the ECM option needed to support them. */
+        if ((s->supported_compressions & T30_SUPPORT_T6_COMPRESSION))
+            set_ctrl_bit(s->local_dis_dtc_frame, 31);
+        if ((s->supported_compressions & T30_SUPPORT_T43_COMPRESSION))
+            set_ctrl_bit(s->local_dis_dtc_frame, 36);
+        if ((s->supported_compressions & T30_SUPPORT_T85_COMPRESSION))
+            set_ctrl_bit(s->local_dis_dtc_frame, 78);
+        /* No T.85 optional L0. */
+        if ((s->supported_compressions & T30_SUPPORT_T45_COMPRESSION))
+            set_ctrl_bit(s->local_dis_dtc_frame, 116);
+    }
+    if ((s->supported_t30_features & T30_SUPPORT_FIELD_NOT_VALID))
+        set_ctrl_bit(s->local_dis_dtc_frame, 33);
+    if ((s->supported_t30_features & T30_SUPPORT_MULTIPLE_SELECTIVE_POLLING))
+        set_ctrl_bit(s->local_dis_dtc_frame, 34);
+    if ((s->supported_t30_features & T30_SUPPORT_POLLED_SUB_ADDRESSING))
+        set_ctrl_bit(s->local_dis_dtc_frame, 35);
+    /* No plane interleave */
+    /* No G.726 */
+    /* No extended voice coding */
+    if ((s->supported_resolutions & T30_SUPPORT_SUPERFINE_RESOLUTION))
+        set_ctrl_bit(s->local_dis_dtc_frame, 41);
+    if ((s->supported_resolutions & T30_SUPPORT_300_300_RESOLUTION))
+        set_ctrl_bit(s->local_dis_dtc_frame, 42);
+    if ((s->supported_resolutions & (T30_SUPPORT_400_400_RESOLUTION | T30_SUPPORT_R16_RESOLUTION)))
+        set_ctrl_bit(s->local_dis_dtc_frame, 43);
+    /* Metric */ 
+    set_ctrl_bit(s->local_dis_dtc_frame, 45);
+    /* Superfine minimum scan line time pattern follows fine */
+    if ((s->supported_t30_features & T30_SUPPORT_SELECTIVE_POLLING))
+        set_ctrl_bit(s->local_dis_dtc_frame, 47);
+    if ((s->supported_t30_features & T30_SUPPORT_SUB_ADDRESSING))
+        set_ctrl_bit(s->local_dis_dtc_frame, 49);
+    if ((s->supported_t30_features & T30_SUPPORT_IDENTIFICATION))
+        set_ctrl_bit(s->local_dis_dtc_frame, 50);
+    /* Ready to transmit a data file (polling) */
+    if (s->tx_file[0])
+        set_ctrl_bit(s->local_dis_dtc_frame, 51);
+    /* No Binary file transfer (BFT) */
+    /* No Document transfer mode (DTM) */
+    /* No Electronic data interchange (EDI) */
+    /* No Basic transfer mode (BTM) */
+    /* No mixed mode (polling) */
+    /* No character mode */
+    /* No mixed mode (Annex E/T.4) */
+    /* No mode 26 (T.505) */
+    /* No digital network capability */
+    /* No duplex operation */
+    /* No JPEG */
+    /* No full colour */
+    /* No 12bits/pel */
+    /* No sub-sampling (1:1:1) */
+    /* No custom illuminant */
+    /* No custom gamut range */
+    if ((s->supported_image_sizes & T30_SUPPORT_US_LETTER_LENGTH))
+        set_ctrl_bit(s->local_dis_dtc_frame, 76);
+    if ((s->supported_image_sizes & T30_SUPPORT_US_LEGAL_LENGTH))
+        set_ctrl_bit(s->local_dis_dtc_frame, 77);
+    /* No HKM key management */
+    /* No RSA key management */
+    /* No override */
+    /* No HFX40 cipher */
+    /* No alternative cipher number 2 */
+    /* No alternative cipher number 3 */
+    /* No HFX40-I hashing */
+    /* No alternative hashing system number 2 */
+    /* No alternative hashing system number 3 */
+    /* No T.44 (mixed raster content) */
+    /* No page length maximum strip size for T.44 (mixed raster content) */
+    /* No colour/grey scale 300x300 or 400x400 */
+    /* No colour/grey scale 100x100 */
+    /* No simple phase C BFT negotiations */
+    /* No extended BFT negotiations */
+    if ((s->supported_t30_features & T30_SUPPORT_INTERNET_SELECTIVE_POLLING_ADDRESS))
+        set_ctrl_bit(s->local_dis_dtc_frame, 101);
+    if ((s->supported_t30_features & T30_SUPPORT_INTERNET_ROUTING_ADDRESS))
+        set_ctrl_bit(s->local_dis_dtc_frame, 102);
+    if ((s->supported_resolutions & T30_SUPPORT_600_600_RESOLUTION))
+        set_ctrl_bit(s->local_dis_dtc_frame, 105);
+    if ((s->supported_resolutions & T30_SUPPORT_1200_1200_RESOLUTION))
+        set_ctrl_bit(s->local_dis_dtc_frame, 106);
+    if ((s->supported_resolutions & T30_SUPPORT_300_600_RESOLUTION))
+        set_ctrl_bit(s->local_dis_dtc_frame, 107);
+    if ((s->supported_resolutions & T30_SUPPORT_400_800_RESOLUTION))
+        set_ctrl_bit(s->local_dis_dtc_frame, 108);
+    if ((s->supported_resolutions & T30_SUPPORT_600_1200_RESOLUTION))
+        set_ctrl_bit(s->local_dis_dtc_frame, 109);
+    /* No colour/grey scale 600x600 */
+    /* No colour/grey scale 1200x1200 */
+    /* No double sided printing (alternate mode) */
+    /* No double sided printing (continuous mode) */
+    /* No black and white mixed raster content profile */
+    /* No shared data memory */
+    /* No T.44 colour space */
+    if ((s->iaf & T30_IAF_MODE_FLOW_CONTROL))
+        set_ctrl_bit(s->local_dis_dtc_frame, 121);
+    /* No k > 4 */
+    if ((s->iaf & T30_IAF_MODE_CONTINUOUS_FLOW))
+        set_ctrl_bit(s->local_dis_dtc_frame, 123);
+    /* No T.89 profile */
+    s->local_dis_dtc_len = 19;
+    //t30_decode_dis_dtc_dcs(s, s->local_dis_dtc_frame, s->local_dis_dtc_len);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int prune_dis_dtc(t30_state_t *s)
+{
+    int i;
+
+    /* Find the last octet that is really needed, set the extension bits, and trim the message length */
+    for (i = 18;  i >= 6;  i--)
+    {
+        /* Strip the top bit */
+        s->local_dis_dtc_frame[i] &= (DISBIT1 | DISBIT2 | DISBIT3 | DISBIT4 | DISBIT5 | DISBIT6 | DISBIT7);
+        /* Check if there is some real message content here */
+        if (s->local_dis_dtc_frame[i])
+            break;
+    }
+    s->local_dis_dtc_len = i + 1;
+    /* Fill in any required extension bits */
+    s->local_dis_dtc_frame[i] &= ~DISBIT8;
+    for (i--;  i > 4;  i--)
+        s->local_dis_dtc_frame[i] |= DISBIT8;
+    t30_decode_dis_dtc_dcs(s, s->local_dis_dtc_frame, s->local_dis_dtc_len);
+    return s->local_dis_dtc_len;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int build_dcs(t30_state_t *s)
+{
+    int i;
+    int bad;
+    
+    /* Make a DCS frame based on local issues and the latest received DIS/DTC frame. Negotiate
+       the result based on what both parties can do. */
+    s->dcs_frame[0] = 0xFF;
+    s->dcs_frame[1] = 0x13;
+    s->dcs_frame[2] = (uint8_t) (T30_DCS | s->dis_received);
+    for (i = 3;  i < 19;  i++)
+        s->dcs_frame[i] = 0x00;
+
+#if 0
+    /* Check for T.37 simple mode. */
+    if (test_ctrl_bit(s->far_dis_dtc_frame, 1))
+        set_ctrl_bit(s->dcs_frame, 1);
+    /* Check for T.38 mode. */
+    if (test_ctrl_bit(s->far_dis_dtc_frame, 3))
+        set_ctrl_bit(s->dcs_frame, 3);
+#endif
+
+    /* Set to required modem rate */
+    s->dcs_frame[4] |= fallback_sequence[s->current_fallback].dcs_code;
+
+    /* Select the compression to use. */
+    switch (s->line_encoding)
+    {
+    case T4_COMPRESSION_ITU_T6:
+        set_ctrl_bit(s->dcs_frame, 31);
+        set_ctrl_bits(s->dcs_frame, T30_MIN_SCAN_0MS, 21);
+        break;
+    case T4_COMPRESSION_ITU_T4_2D:
+        set_ctrl_bit(s->dcs_frame, 16);
+        set_ctrl_bits(s->dcs_frame, s->min_scan_time_code, 21);
+        break;
+    case T4_COMPRESSION_ITU_T4_1D:
+        set_ctrl_bits(s->dcs_frame, s->min_scan_time_code, 21);
+        break;
+    default:
+        set_ctrl_bits(s->dcs_frame, T30_MIN_SCAN_0MS, 21);
+        break;
+    }
+    /* We have a file to send, so tell the far end to go into receive mode. */
+    set_ctrl_bit(s->dcs_frame, 10);
+    /* Set the Y resolution bits */
+    bad = T30_ERR_OK;
+    switch (s->y_resolution)
+    {
+    case T4_Y_RESOLUTION_1200:
+        switch (s->x_resolution)
+        {
+        case T4_X_RESOLUTION_600:
+            if (!(s->supported_resolutions & T30_SUPPORT_600_1200_RESOLUTION))
+                bad = T30_ERR_NORESSUPPORT;
+            else
+                set_ctrl_bit(s->dcs_frame, 109);
+            break;
+        case T4_X_RESOLUTION_1200:
+            if (!(s->supported_resolutions & T30_SUPPORT_1200_1200_RESOLUTION))
+                bad = T30_ERR_NORESSUPPORT;
+            else
+                set_ctrl_bit(s->dcs_frame, 106);
+            break;
+        default:
+            bad = T30_ERR_NORESSUPPORT;
+            break;
+        }
+        break;
+    case T4_Y_RESOLUTION_800:
+        switch (s->x_resolution)
+        {
+        case T4_X_RESOLUTION_R16:
+            if (!(s->supported_resolutions & T30_SUPPORT_400_800_RESOLUTION))
+                bad = T30_ERR_NORESSUPPORT;
+            else
+                set_ctrl_bit(s->dcs_frame, 108);
+            break;
+        default:
+            bad = T30_ERR_NORESSUPPORT;
+            break;
+        }
+        break;
+    case T4_Y_RESOLUTION_600:
+        switch (s->x_resolution)
+        {
+        case T4_X_RESOLUTION_300:
+            if (!(s->supported_resolutions & T30_SUPPORT_300_600_RESOLUTION))
+                bad = T30_ERR_NORESSUPPORT;
+            else
+                set_ctrl_bit(s->dcs_frame, 107);
+            break;
+        case T4_X_RESOLUTION_600:
+            if (!(s->supported_resolutions & T30_SUPPORT_600_600_RESOLUTION))
+                bad = T30_ERR_NORESSUPPORT;
+            else
+                set_ctrl_bit(s->dcs_frame, 105);
+            break;
+        default:
+            bad = T30_ERR_NORESSUPPORT;
+            break;
+        }
+        break;
+    case T4_Y_RESOLUTION_SUPERFINE:
+        if (!(s->supported_resolutions & T30_SUPPORT_SUPERFINE_RESOLUTION))
+        {
+            bad = T30_ERR_NORESSUPPORT;
+        }
+        else
+        {
+            switch (s->x_resolution)
+            {
+            case T4_X_RESOLUTION_R8:
+                set_ctrl_bit(s->dcs_frame, 41);
+                break;
+            case T4_X_RESOLUTION_R16:
+                set_ctrl_bit(s->dcs_frame, 43);
+                break;
+            default:
+                bad = T30_ERR_NORESSUPPORT;
+                break;
+            }
+        }
+        break;
+    case T4_Y_RESOLUTION_300:
+        switch (s->x_resolution)
+        {
+        case T4_X_RESOLUTION_300:
+            if (!(s->supported_resolutions & T30_SUPPORT_300_300_RESOLUTION))
+                bad = T30_ERR_NORESSUPPORT;
+            else
+                set_ctrl_bit(s->dcs_frame, 42);
+            break;
+        default:
+            bad = T30_ERR_NORESSUPPORT;
+            break;
+        }
+        break;
+    case T4_Y_RESOLUTION_FINE:
+        if (!(s->supported_resolutions & T30_SUPPORT_FINE_RESOLUTION))
+        {
+            bad = T30_ERR_NORESSUPPORT;
+        }
+        else
+        {
+            switch (s->x_resolution)
+            {
+            case T4_X_RESOLUTION_R8:
+                set_ctrl_bit(s->dcs_frame, 15);
+                break;
+            default:
+                bad = T30_ERR_NORESSUPPORT;
+                break;
+            }
+        }
+        break;
+    default:
+    case T4_Y_RESOLUTION_STANDARD:
+        switch (s->x_resolution)
+        {
+        case T4_X_RESOLUTION_R8:
+            /* No bits to set for this */
+            break;
+        default:
+            bad = T30_ERR_NORESSUPPORT;
+            break;
+        }
+        break;
+    }
+    if (bad != T30_ERR_OK)
+    {
+        s->current_status = bad;
+        span_log(&s->logging, SPAN_LOG_FLOW, "Image resolution (%d x %d) not acceptable\n", s->x_resolution, s->y_resolution);
+        return -1;
+    }
+    /* Deal with the image width. The X resolution will fall in line with any valid width. */
+    /* Low (R4) res widths are not supported in recent versions of T.30 */
+    bad = T30_ERR_OK;
+    switch (s->image_width)
+    {
+    case T4_WIDTH_R8_A4:
+    case T4_WIDTH_300_A4:
+    case T4_WIDTH_R16_A4:
+    case T4_WIDTH_600_A4:
+    case T4_WIDTH_1200_A4:
+        /* No width related bits need to be set. */
+        break;
+    case T4_WIDTH_R8_B4:
+    case T4_WIDTH_300_B4:
+    case T4_WIDTH_R16_B4:
+    case T4_WIDTH_600_B4:
+    case T4_WIDTH_1200_B4:
+        if ((s->far_dis_dtc_frame[5] & (DISBIT2 | DISBIT1)) < 1)
+            bad = T30_ERR_NOSIZESUPPORT;
+        else if (!(s->supported_image_sizes & T30_SUPPORT_255MM_WIDTH))
+            bad = T30_ERR_BADTIFF;
+        else
+            set_ctrl_bit(s->dcs_frame, 17);
+        break;
+    case T4_WIDTH_R8_A3:
+    case T4_WIDTH_300_A3:
+    case T4_WIDTH_R16_A3:
+    case T4_WIDTH_600_A3:
+    case T4_WIDTH_1200_A3:
+        if ((s->far_dis_dtc_frame[5] & (DISBIT2 | DISBIT1)) < 2)
+            bad = T30_ERR_NOSIZESUPPORT;
+        else if (!(s->supported_image_sizes & T30_SUPPORT_303MM_WIDTH))    
+            bad = T30_ERR_BADTIFF;
+        else
+            set_ctrl_bit(s->dcs_frame, 18);
+        break;
+    default:
+        /* T.30 does not support this width */
+        bad = T30_ERR_BADTIFF;
+        break;
+    }
+    if (bad != T30_ERR_OK)
+    {
+        s->current_status = bad;
+        span_log(&s->logging, SPAN_LOG_FLOW, "Image width (%d pixels) not a valid FAX image width\n", s->image_width);
+        return -1;
+    }
+    switch (s->image_width)
+    {
+    case T4_WIDTH_R8_A4:
+    case T4_WIDTH_R8_B4:
+    case T4_WIDTH_R8_A3:
+        /* These are always OK */
+        break;
+    case T4_WIDTH_300_A4:
+    case T4_WIDTH_300_B4:
+    case T4_WIDTH_300_A3:
+        if (!test_ctrl_bit(s->far_dis_dtc_frame, 42)  &&  !test_ctrl_bit(s->far_dis_dtc_frame, 107))
+            bad = T30_ERR_NOSIZESUPPORT;
+        break;
+    case T4_WIDTH_R16_A4:
+    case T4_WIDTH_R16_B4:
+    case T4_WIDTH_R16_A3:
+        if (!test_ctrl_bit(s->far_dis_dtc_frame, 43))
+            bad = T30_ERR_NOSIZESUPPORT;
+        break;
+    case T4_WIDTH_600_A4:
+    case T4_WIDTH_600_B4:
+    case T4_WIDTH_600_A3:
+        if (!test_ctrl_bit(s->far_dis_dtc_frame, 105)  &&  !test_ctrl_bit(s->far_dis_dtc_frame, 109))
+            bad = T30_ERR_NOSIZESUPPORT;
+        break;
+    case T4_WIDTH_1200_A4:
+    case T4_WIDTH_1200_B4:
+    case T4_WIDTH_1200_A3:
+        if (!test_ctrl_bit(s->far_dis_dtc_frame, 106))
+            bad = T30_ERR_NOSIZESUPPORT;
+        break;
+    default:
+        /* T.30 does not support this width */
+        bad = T30_ERR_BADTIFF;
+        break;
+    }
+    if (bad != T30_ERR_OK)
+    {
+        s->current_status = bad;
+        span_log(&s->logging, SPAN_LOG_FLOW, "Image width (%d pixels) not a valid FAX image width\n", s->image_width);
+        return -1;
+    }
+    /* Deal with the image length */
+    /* If the other end supports unlimited length, then use that. Otherwise, if the other end supports
+       B4 use that, as its longer than the default A4 length. */
+    if (test_ctrl_bit(s->far_dis_dtc_frame, 20))
+        set_ctrl_bit(s->dcs_frame, 20);
+    else if (test_ctrl_bit(s->far_dis_dtc_frame, 19))
+        set_ctrl_bit(s->dcs_frame, 19);
+
+    if (s->error_correcting_mode)
+        set_ctrl_bit(s->dcs_frame, 27);
+
+    if ((s->iaf & T30_IAF_MODE_FLOW_CONTROL)  &&  test_ctrl_bit(s->far_dis_dtc_frame, 121))
+        set_ctrl_bit(s->dcs_frame, 121);
+    if ((s->iaf & T30_IAF_MODE_CONTINUOUS_FLOW)  &&  test_ctrl_bit(s->far_dis_dtc_frame, 123))
+        set_ctrl_bit(s->dcs_frame, 123);
+    s->dcs_len = 19;
+    //t30_decode_dis_dtc_dcs(s, s->dcs_frame, s->dcs_len);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int prune_dcs(t30_state_t *s)
+{
+    int i;
+
+    /* Find the last octet that is really needed, set the extension bits, and trim the message length */
+    for (i = 18;  i >= 6;  i--)
+    {
+        /* Strip the top bit */
+        s->dcs_frame[i] &= (DISBIT1 | DISBIT2 | DISBIT3 | DISBIT4 | DISBIT5 | DISBIT6 | DISBIT7);
+        /* Check if there is some real message content here */
+        if (s->dcs_frame[i])
+            break;
+    }
+    s->dcs_len = i + 1;
+    /* Fill in any required extension bits */
+    s->local_dis_dtc_frame[i] &= ~DISBIT8;
+    for (i--  ;  i > 4;  i--)
+        s->dcs_frame[i] |= DISBIT8;
+    t30_decode_dis_dtc_dcs(s, s->dcs_frame, s->dcs_len);
+    return s->dcs_len;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int step_fallback_entry(t30_state_t *s)
+{
+    int min_row_bits;
+
+    while (fallback_sequence[++s->current_fallback].which)
+    {
+        if ((fallback_sequence[s->current_fallback].which & s->current_permitted_modems))
+            break;
+    }
+    if (fallback_sequence[s->current_fallback].which == 0)
+        return -1;
+    /* TODO: This only sets the minimum row time for future pages. It doesn't fix up the
+             current page, though it is benign - fallback will only result in an excessive
+             minimum. */
+    min_row_bits = set_min_scan_time_code(s);
+    t4_tx_set_min_row_bits(&s->t4, min_row_bits);
+    /* We need to rebuild the DCS message we will send. */
+    build_dcs(s);
+    return s->current_fallback;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int find_fallback_entry(int dcs_code)
+{
+    int i;
+
+    /* The table is short, and not searched often, so a brain-dead linear scan seems OK */
+    for (i = 0;  fallback_sequence[i].bit_rate;  i++)
+    {
+        if (fallback_sequence[i].dcs_code == dcs_code)
+            break;
+    }
+    if (fallback_sequence[i].bit_rate == 0)
+        return -1;
+    return i;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void send_dcn(t30_state_t *s)
+{
+    queue_phase(s, T30_PHASE_D_TX);
+    set_state(s, T30_STATE_C);
+    send_simple_frame(s, T30_DCN);
+}
+/*- End of function --------------------------------------------------------*/
+
+static int send_dis_or_dtc_sequence(t30_state_t *s, int start)
+{
+    /* (NSF) (CSI) DIS */
+    /* (NSC) (CIG) (PWD) (SEP) (PSA) (CIA) (ISP) DTC */
+    if (start)
+    {
+        set_dis_or_dtc(s);
+        set_state(s, T30_STATE_R);
+        s->step = 0;
+    }
+    if (!s->dis_received)
+    {
+        /* DIS sequence */
+        switch (s->step)
+        {
+        case 0:
+            s->step++;
+            if (send_nsf_frame(s))
+                break;
+            /* Fall through */
+        case 1:
+            s->step++;
+            if (send_ident_frame(s, T30_CSI))
+                break;
+            /* Fall through */
+        case 2:
+            s->step++;
+            prune_dis_dtc(s);
+            send_frame(s, s->local_dis_dtc_frame, s->local_dis_dtc_len);
+            break;
+        case 3:
+            s->step++;
+            /* Shut down HDLC transmission. */
+            if (s->send_hdlc_handler)
+                s->send_hdlc_handler(s->send_hdlc_user_data, NULL, 0);
+            break;
+        default:
+            return -1;
+        }
+    }
+    else
+    {
+        /* DTC sequence */
+        switch (s->step)
+        {
+        case 0:
+            s->step++;
+            if (send_nsc_frame(s))
+                break;
+            /* Fall through */
+        case 1:
+            s->step++;
+            if (send_ident_frame(s, T30_CIG))
+                break;
+            /* Fall through */
+        case 2:
+            s->step++;
+            if (send_pwd_frame(s))
+                break;
+            /* Fall through */
+        case 3:
+            s->step++;
+            if (send_sep_frame(s))
+                break;
+            /* Fall through */
+        case 4:
+            s->step++;
+            if (send_psa_frame(s))
+                break;
+            /* Fall through */
+        case 5:
+            s->step++;
+            if (send_cia_frame(s))
+                break;
+            /* Fall through */
+        case 6:
+            s->step++;
+            if (send_isp_frame(s))
+                break;
+            /* Fall through */
+        case 7:
+            s->step++;
+            prune_dis_dtc(s);
+            send_frame(s, s->local_dis_dtc_frame, s->local_dis_dtc_len);
+            break;
+        case 8:
+            s->step++;
+            /* Shut down HDLC transmission. */
+            if (s->send_hdlc_handler)
+                s->send_hdlc_handler(s->send_hdlc_user_data, NULL, 0);
+            break;
+        default:
+            return -1;
+        }
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int send_dcs_sequence(t30_state_t *s, int start)
+{
+    /* (NSS) (TSI) (SUB) (SID) (TSA) (IRA) DCS */
+    /* Schedule training after the messages */
+    if (start)
+    {
+        prune_dcs(s);
+        set_state(s, T30_STATE_D);
+        s->step = 0;
+    }
+    switch (s->step)
+    {
+    case 0:
+        s->step++;
+        if (send_nss_frame(s))
+            break;
+        /* Fall through */
+    case 1:
+        s->step++;
+        if (send_ident_frame(s, T30_TSI))
+            break;
+        /* Fall through */
+    case 2:
+        s->step++;
+        if (send_sub_frame(s))
+            break;
+        /* Fall through */
+    case 3:
+        s->step++;
+        if (send_sid_frame(s))
+            break;
+        /* Fall through */
+    case 4:
+        s->step++;
+        if (send_tsa_frame(s))
+            break;
+        /* Fall through */
+    case 5:
+        s->step++;
+        if (send_ira_frame(s))
+            break;
+        /* Fall through */
+    case 6:
+        s->step++;
+        prune_dcs(s);
+        send_frame(s, s->dcs_frame, s->dcs_len);
+        break;
+    case 7:
+        s->step++;
+        /* Shut down HDLC transmission. */
+        if (s->send_hdlc_handler)
+            s->send_hdlc_handler(s->send_hdlc_user_data, NULL, 0);
+        break;
+    default:
+        return -1;
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int send_cfr_sequence(t30_state_t *s, int start)
+{
+    /* (CSA) CFR */
+    /* CFR is usually a simple frame, but can become a sequence with Internet
+       FAXing. */
+    send_simple_frame(s, T30_CFR);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void disconnect(t30_state_t *s)
+{
+    span_log(&s->logging, SPAN_LOG_FLOW, "Disconnecting\n");
+    /* Make sure any FAX in progress is tidied up. If the tidying up has
+       already happened, repeating it here is harmless. */
+    switch (s->operation_in_progress)
+    {
+    case OPERATION_IN_PROGRESS_T4_TX:
+        t4_tx_end(&(s->t4));
+        break;
+    case OPERATION_IN_PROGRESS_T4_RX:
+        t4_rx_end(&(s->t4));
+        break;
+    }
+    s->operation_in_progress = OPERATION_IN_PROGRESS_NONE;
+    s->timer_t0_t1 = 0;
+    s->timer_t2_t4 = 0;
+    s->timer_t3 = 0;
+    s->timer_t5 = 0;
+    set_phase(s, T30_PHASE_E);
+    set_state(s, T30_STATE_B);
+}
+/*- End of function --------------------------------------------------------*/
+
+static int set_min_scan_time_code(t30_state_t *s)
+{
+    /* Translation between the codes for the minimum scan times the other end needs,
+       and the codes for what we say will be used. We need 0 minimum. */
+    static const uint8_t translate_min_scan_time[3][8] =
+    {
+        {T30_MIN_SCAN_20MS, T30_MIN_SCAN_5MS, T30_MIN_SCAN_10MS, T30_MIN_SCAN_20MS, T30_MIN_SCAN_40MS, T30_MIN_SCAN_40MS, T30_MIN_SCAN_10MS, T30_MIN_SCAN_0MS}, /* normal */
+        {T30_MIN_SCAN_20MS, T30_MIN_SCAN_5MS, T30_MIN_SCAN_10MS, T30_MIN_SCAN_10MS, T30_MIN_SCAN_40MS, T30_MIN_SCAN_20MS, T30_MIN_SCAN_5MS,  T30_MIN_SCAN_0MS}, /* fine */
+        {T30_MIN_SCAN_10MS, T30_MIN_SCAN_5MS, T30_MIN_SCAN_5MS,  T30_MIN_SCAN_5MS,  T30_MIN_SCAN_20MS, T30_MIN_SCAN_10MS, T30_MIN_SCAN_5MS,  T30_MIN_SCAN_0MS}  /* superfine, when half fine time is selected */
+    };
+    /* Translation between the codes for the minimum scan time we will use, and milliseconds. */
+    static const int min_scan_times[8] =
+    {
+        20, 5, 10, 0, 40, 0, 0, 0
+    };
+    int min_bits_field;
+
+    /* Set the minimum scan time bits */
+    if (s->error_correcting_mode)
+        min_bits_field = T30_MIN_SCAN_0MS;
+    else
+        min_bits_field = (s->far_dis_dtc_frame[5] >> 4) & 7;
+    switch (s->y_resolution)
+    {
+    case T4_Y_RESOLUTION_SUPERFINE:
+        if (!test_ctrl_bit(s->far_dis_dtc_frame, 41))
+        {
+            s->current_status = T30_ERR_NORESSUPPORT;
+            span_log(&s->logging, SPAN_LOG_FLOW, "Remote FAX does not support super-fine resolution.\n");
+            return -1;
+        }
+        s->min_scan_time_code = translate_min_scan_time[(test_ctrl_bit(s->far_dis_dtc_frame, 46))  ?  2  :  1][min_bits_field];
+        break;
+    case T4_Y_RESOLUTION_FINE:
+        if (!test_ctrl_bit(s->far_dis_dtc_frame, 15))
+        {
+            s->current_status = T30_ERR_NORESSUPPORT;
+            span_log(&s->logging, SPAN_LOG_FLOW, "Remote FAX does not support fine resolution.\n");
+            return -1;
+        }
+        s->min_scan_time_code = translate_min_scan_time[1][min_bits_field];
+        break;
+    default:
+    case T4_Y_RESOLUTION_STANDARD:
+        s->min_scan_time_code = translate_min_scan_time[0][min_bits_field];
+        break;
+    }
+    if (!s->error_correcting_mode  &&  (s->iaf & T30_IAF_MODE_NO_FILL_BITS))
+        return 0;
+    return fallback_sequence[s->current_fallback].bit_rate*min_scan_times[s->min_scan_time_code]/1000;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int start_sending_document(t30_state_t *s)
+{
+    int min_row_bits;
+
+    if (s->tx_file[0] == '\0')
+    {
+        /* There is nothing to send */
+        span_log(&s->logging, SPAN_LOG_FLOW, "No document to send\n");
+        return -1;
+    }
+    span_log(&s->logging, SPAN_LOG_FLOW, "Start sending document\n");
+    if (t4_tx_init(&s->t4, s->tx_file, s->tx_start_page, s->tx_stop_page) == NULL)
+    {
+        span_log(&s->logging, SPAN_LOG_WARNING, "Cannot open source TIFF file '%s'\n", s->tx_file);
+        s->current_status = T30_ERR_FILEERROR;
+        return -1;
+    }
+    s->operation_in_progress = OPERATION_IN_PROGRESS_T4_TX;
+    t4_tx_get_pages_in_file(&s->t4);
+    t4_tx_set_tx_encoding(&s->t4, s->line_encoding);
+    t4_tx_set_local_ident(&s->t4, s->tx_info.ident);
+    t4_tx_set_header_info(&s->t4, s->header_info);
+
+    s->x_resolution = t4_tx_get_x_resolution(&s->t4);
+    s->y_resolution = t4_tx_get_y_resolution(&s->t4);
+    /* The minimum scan time to be used can't be evaluated until we know the Y resolution, and
+       must be evaluated before the minimum scan row bits can be evaluated. */
+    if ((min_row_bits = set_min_scan_time_code(s)) < 0)
+    {
+        t4_tx_end(&(s->t4));
+        s->operation_in_progress = OPERATION_IN_PROGRESS_NONE;
+        return -1;
+    }
+    span_log(&s->logging, SPAN_LOG_FLOW, "Minimum bits per row will be %d\n", min_row_bits);
+    t4_tx_set_min_row_bits(&s->t4, min_row_bits);
+
+    if (tx_start_page(s))
+        return -1;
+    s->image_width = t4_tx_get_image_width(&(s->t4));
+    if (s->error_correcting_mode)
+    {
+        if (get_partial_ecm_page(s) == 0)
+            span_log(&s->logging, SPAN_LOG_WARNING, "No image data to send\n");
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int restart_sending_document(t30_state_t *s)
+{
+    t4_tx_restart_page(&(s->t4));
+    s->retries = 0;
+    s->ecm_block = 0;
+    send_dcs_sequence(s, TRUE);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int start_receiving_document(t30_state_t *s)
+{
+    if (s->rx_file[0] == '\0')
+    {
+        /* There is nothing to receive to */
+        span_log(&s->logging, SPAN_LOG_FLOW, "No document to receive\n");
+        return -1;
+    }
+    span_log(&s->logging, SPAN_LOG_FLOW, "Start receiving document\n");
+    queue_phase(s, T30_PHASE_B_TX);
+    s->dis_received = FALSE;
+    s->ecm_block = 0;
+    send_dis_or_dtc_sequence(s, TRUE);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void unexpected_frame(t30_state_t *s, const uint8_t *msg, int len)
+{
+    span_log(&s->logging, SPAN_LOG_FLOW, "Unexpected %s received in state %d\n", t30_frametype(msg[2]), s->state);
+    switch (s->state)
+    {
+    case T30_STATE_F_DOC_ECM:
+    case T30_STATE_F_DOC_NON_ECM:
+        s->current_status = T30_ERR_RX_INVALCMD;
+        break;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static void unexpected_non_final_frame(t30_state_t *s, const uint8_t *msg, int len)
+{
+    span_log(&s->logging, SPAN_LOG_FLOW, "Unexpected %s frame in state %d\n", t30_frametype(msg[2]), s->state);
+    s->current_status = T30_ERR_UNEXPECTED;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void unexpected_final_frame(t30_state_t *s, const uint8_t *msg, int len)
+{
+    span_log(&s->logging, SPAN_LOG_FLOW, "Unexpected %s received in state %d\n", t30_frametype(msg[2]), s->state);
+    s->current_status = T30_ERR_UNEXPECTED;
+    send_dcn(s);
+}
+/*- End of function --------------------------------------------------------*/
+
+static void unexpected_frame_length(t30_state_t *s, const uint8_t *msg, int len)
+{
+    span_log(&s->logging, SPAN_LOG_FLOW, "Unexpected %s frame length - %d\n", t30_frametype(msg[0]), len);
+    s->current_status = T30_ERR_UNEXPECTED;
+    send_dcn(s);
+}
+/*- End of function --------------------------------------------------------*/
+
+static int process_rx_dis_dtc(t30_state_t *s, const uint8_t *msg, int len)
+{
+    int new_status;
+
+    t30_decode_dis_dtc_dcs(s, msg, len);
+    if (len < 6)
+    {
+        span_log(&s->logging, SPAN_LOG_FLOW, "Short DIS/DTC frame\n");
+        return -1;
+    }
+
+    if (msg[2] == T30_DIS)
+        s->dis_received = TRUE;
+    /* Make a local copy of the message, padded to the maximum possible length with zeros. This allows
+       us to simply pick out the bits, without worrying about whether they were set from the remote side. */
+    s->far_dis_dtc_len = (len > T30_MAX_DIS_DTC_DCS_LEN)  ?  T30_MAX_DIS_DTC_DCS_LEN  :  len;
+    memcpy(s->far_dis_dtc_frame, msg, s->far_dis_dtc_len);
+    if (s->far_dis_dtc_len < T30_MAX_DIS_DTC_DCS_LEN)
+        memset(s->far_dis_dtc_frame + s->far_dis_dtc_len, 0, T30_MAX_DIS_DTC_DCS_LEN - s->far_dis_dtc_len);
+    s->error_correcting_mode = (s->ecm_allowed  &&  (s->far_dis_dtc_frame[6] & DISBIT3) != 0);
+    /* 256 octets per ECM frame */
+    s->octets_per_ecm_frame = 256;
+    /* Select the compression to use. */
+    if (s->error_correcting_mode  &&  (s->supported_compressions & T30_SUPPORT_T6_COMPRESSION)  &&  test_ctrl_bit(s->far_dis_dtc_frame, 31))
+    {
+        s->line_encoding = T4_COMPRESSION_ITU_T6;
+    }
+    else if ((s->supported_compressions & T30_SUPPORT_T4_2D_COMPRESSION)  &&  test_ctrl_bit(s->far_dis_dtc_frame, 16))
+    {
+        s->line_encoding = T4_COMPRESSION_ITU_T4_2D;
+    }
+    else
+    {
+        s->line_encoding = T4_COMPRESSION_ITU_T4_1D;
+    }
+    span_log(&s->logging, SPAN_LOG_FLOW, "Selected compression %d\n", s->line_encoding);
+    switch (s->far_dis_dtc_frame[4] & (DISBIT6 | DISBIT5 | DISBIT4 | DISBIT3))
+    {
+    case (DISBIT6 | DISBIT4 | DISBIT3):
+        if ((s->supported_modems & T30_SUPPORT_V17))
+        {
+            s->current_permitted_modems = T30_SUPPORT_V17 | T30_SUPPORT_V29 | T30_SUPPORT_V27TER;
+            s->current_fallback = T30_V17_FALLBACK_START;
+            break;
+        }
+        /* Fall through */
+    case (DISBIT4 | DISBIT3):
+        if ((s->supported_modems & T30_SUPPORT_V29))
+        {
+            s->current_permitted_modems = T30_SUPPORT_V29 | T30_SUPPORT_V27TER;
+            s->current_fallback = T30_V29_FALLBACK_START;
+            break;
+        }
+        /* Fall through */
+    case DISBIT4:
+        s->current_permitted_modems = T30_SUPPORT_V27TER;
+        s->current_fallback = T30_V27TER_FALLBACK_START;
+        break;
+    case 0:
+        s->current_permitted_modems = T30_SUPPORT_V27TER;
+        s->current_fallback = T30_V27TER_FALLBACK_START + 1;
+        break;
+    case DISBIT3:
+        if ((s->supported_modems & T30_SUPPORT_V29))
+        {
+            /* TODO: this doesn't allow for skipping the V.27ter modes */
+            s->current_permitted_modems = T30_SUPPORT_V29;
+            s->current_fallback = T30_V29_FALLBACK_START;
+            break;
+        }
+        /* Fall through */
+    default:
+        span_log(&s->logging, SPAN_LOG_FLOW, "Remote does not support a compatible modem\n");
+        /* We cannot talk to this machine! */
+        s->current_status = T30_ERR_INCOMPATIBLE;
+        return -1;
+    }
+    if (s->phase_b_handler)
+    {
+        new_status = s->phase_b_handler(s, s->phase_d_user_data, msg[2]);
+        if (new_status != T30_ERR_OK)
+        {
+            span_log(&s->logging, SPAN_LOG_FLOW, "Application rejected DIS/DTC - '%s'\n", t30_completion_code_to_str(new_status));
+            s->current_status = new_status;
+            /* TODO: If FNV is allowed, process it here */
+            send_dcn(s);
+            return -1;
+        }
+    }
+    queue_phase(s, T30_PHASE_B_TX);
+    /* Try to send something */
+    if (s->tx_file[0])
+    {
+        span_log(&s->logging, SPAN_LOG_FLOW, "Trying to send file '%s'\n", s->tx_file);
+        if (!test_ctrl_bit(s->far_dis_dtc_frame, 10))
+        {
+            span_log(&s->logging, SPAN_LOG_FLOW, "%s far end cannot receive\n", t30_frametype(msg[2]));
+            s->current_status = T30_ERR_RX_INCAPABLE;
+            send_dcn(s);
+        }
+        if (start_sending_document(s))
+        {
+            send_dcn(s);
+            return -1;
+        }
+        if (build_dcs(s))
+        {
+            span_log(&s->logging, SPAN_LOG_FLOW, "The far end is incompatible\n", s->tx_file);
+            send_dcn(s);
+            return -1;
+        }
+        s->retries = 0;
+        send_dcs_sequence(s, TRUE);
+        return 0;
+    }
+    span_log(&s->logging, SPAN_LOG_FLOW, "%s nothing to send\n", t30_frametype(msg[2]));
+    /* ... then try to receive something */
+    if (s->rx_file[0])
+    {
+        span_log(&s->logging, SPAN_LOG_FLOW, "Trying to receive file '%s'\n", s->rx_file);
+        if (!test_ctrl_bit(s->far_dis_dtc_frame, 9))
+        {
+            span_log(&s->logging, SPAN_LOG_FLOW, "%s far end cannot transmit\n", t30_frametype(msg[2]));
+            s->current_status = T30_ERR_TX_INCAPABLE;
+            send_dcn(s);
+            return -1;
+        }
+        if (start_receiving_document(s))
+        {
+            send_dcn(s);
+            return -1;
+        }
+        s->dis_received = TRUE;
+        if (set_dis_or_dtc(s))
+        {
+            s->current_status = T30_ERR_INCOMPATIBLE;
+            send_dcn(s);
+            return -1;
+        }
+        s->retries = 0;
+        send_dis_or_dtc_sequence(s, TRUE);
+        return 0;
+    }
+    span_log(&s->logging, SPAN_LOG_FLOW, "%s nothing to receive\n", t30_frametype(msg[2]));
+    /* There is nothing to do, or nothing we are able to do. */
+    send_dcn(s);
+    return -1;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int process_rx_dcs(t30_state_t *s, const uint8_t *msg, int len)
+{
+    static const int widths[6][4] =
+    {
+        {  T4_WIDTH_R4_A4,   T4_WIDTH_R4_B4,   T4_WIDTH_R4_A3, -1}, /* R4 resolution - no longer used in recent versions of T.30 */
+        {  T4_WIDTH_R8_A4,   T4_WIDTH_R8_B4,   T4_WIDTH_R8_A3, -1}, /* R8 resolution */
+        { T4_WIDTH_300_A4,  T4_WIDTH_300_B4,  T4_WIDTH_300_A3, -1}, /* 300/inch resolution */
+        { T4_WIDTH_R16_A4,  T4_WIDTH_R16_B4,  T4_WIDTH_R16_A3, -1}, /* R16 resolution */
+        { T4_WIDTH_600_A4,  T4_WIDTH_600_B4,  T4_WIDTH_600_A3, -1}, /* 600/inch resolution */
+        {T4_WIDTH_1200_A4, T4_WIDTH_1200_B4, T4_WIDTH_1200_A3, -1}  /* 1200/inch resolution */
+    };
+    uint8_t dcs_frame[T30_MAX_DIS_DTC_DCS_LEN];
+    int i;
+    int new_status;
+
+    t30_decode_dis_dtc_dcs(s, msg, len);
+
+    /* Check DCS frame from remote */
+    if (len < 6)
+    {
+        span_log(&s->logging, SPAN_LOG_FLOW, "Short DCS frame\n");
+        return -1;
+    }
+
+    /* Make an ASCII string format copy of the message, for logging in the
+       received file. This string does not include the frame header octets. */
+    sprintf(s->rx_dcs_string, "%02X", bit_reverse8(msg[3]));
+    for (i = 4;  i < len;  i++)
+        sprintf(s->rx_dcs_string + 3*i - 10, " %02X", bit_reverse8(msg[i]));
+    /* Make a local copy of the message, padded to the maximum possible length with zeros. This allows
+       us to simply pick out the bits, without worrying about whether they were set from the remote side. */
+    if (len > T30_MAX_DIS_DTC_DCS_LEN)
+    {
+        memcpy(dcs_frame, msg, T30_MAX_DIS_DTC_DCS_LEN);
+    }
+    else
+    {
+        memcpy(dcs_frame, msg, len);
+        if (len < T30_MAX_DIS_DTC_DCS_LEN)
+            memset(dcs_frame + len, 0, T30_MAX_DIS_DTC_DCS_LEN - len);
+    }
+
+    s->octets_per_ecm_frame = test_ctrl_bit(dcs_frame, 28)  ?  256  :  64;
+
+    if (test_ctrl_bit(dcs_frame, 106))
+        s->x_resolution = T4_X_RESOLUTION_1200;
+    else if (test_ctrl_bit(dcs_frame, 105)  ||  test_ctrl_bit(dcs_frame, 109))
+        s->x_resolution = T4_X_RESOLUTION_600;
+    else if (test_ctrl_bit(dcs_frame, 43)  ||  test_ctrl_bit(dcs_frame, 108))
+        s->x_resolution = T4_X_RESOLUTION_R16;
+    else if (test_ctrl_bit(dcs_frame, 42)  ||  test_ctrl_bit(dcs_frame, 107))
+        s->x_resolution = T4_X_RESOLUTION_300;
+    else
+        s->x_resolution = T4_X_RESOLUTION_R8;
+
+    if (test_ctrl_bit(dcs_frame, 106)  ||  test_ctrl_bit(dcs_frame, 109))
+        s->y_resolution = T4_Y_RESOLUTION_1200;
+    else if (test_ctrl_bit(dcs_frame, 108))
+        s->y_resolution = T4_Y_RESOLUTION_800;
+    else if (test_ctrl_bit(dcs_frame, 105)  ||  test_ctrl_bit(dcs_frame, 107))
+        s->y_resolution = T4_Y_RESOLUTION_600;
+    else if (test_ctrl_bit(dcs_frame, 41)  ||  test_ctrl_bit(dcs_frame, 43))
+        s->y_resolution = T4_Y_RESOLUTION_SUPERFINE;
+    else if (test_ctrl_bit(dcs_frame, 42))
+        s->y_resolution = T4_Y_RESOLUTION_300;
+    else if (test_ctrl_bit(dcs_frame, 15))
+        s->y_resolution = T4_Y_RESOLUTION_FINE;
+    else
+        s->y_resolution = T4_Y_RESOLUTION_STANDARD;
+
+    if (s->x_resolution == T4_X_RESOLUTION_1200)
+        i = 5;
+    else if (s->x_resolution == T4_X_RESOLUTION_600)
+        i = 4;
+    else if (s->x_resolution == T4_X_RESOLUTION_R16)
+        i = 3;
+    else if (s->x_resolution == T4_X_RESOLUTION_300)
+        i = 2;
+    else if (s->x_resolution == T4_X_RESOLUTION_R4)
+        i = 0;
+    else
+        i = 1;
+
+    s->image_width = widths[i][dcs_frame[5] & (DISBIT2 | DISBIT1)];
+
+    /* Check which compression we will use. */
+    if (test_ctrl_bit(dcs_frame, 31))
+        s->line_encoding = T4_COMPRESSION_ITU_T6;
+    else if (test_ctrl_bit(dcs_frame, 16))
+        s->line_encoding = T4_COMPRESSION_ITU_T4_2D;
+    else
+        s->line_encoding = T4_COMPRESSION_ITU_T4_1D;
+    span_log(&s->logging, SPAN_LOG_FLOW, "Selected compression %d\n", s->line_encoding);
+    if (!test_ctrl_bit(dcs_frame, 10))
+        span_log(&s->logging, SPAN_LOG_PROTOCOL_WARNING, "Remote is not requesting receive in DCS\n");
+
+    if ((s->current_fallback = find_fallback_entry(dcs_frame[4] & (DISBIT6 | DISBIT5 | DISBIT4 | DISBIT3))) < 0)
+    {
+        span_log(&s->logging, SPAN_LOG_FLOW, "Remote asked for a modem standard we do not support\n");
+        return -1;
+    }
+    s->error_correcting_mode = (test_ctrl_bit(dcs_frame, 27) != 0);
+
+    if (s->phase_b_handler)
+    {
+        new_status = s->phase_b_handler(s, s->phase_d_user_data, msg[2]);
+        if (new_status != T30_ERR_OK)
+        {
+            span_log(&s->logging, SPAN_LOG_FLOW, "Application rejected DCS - '%s'\n", t30_completion_code_to_str(new_status));
+            s->current_status = new_status;
+            /* TODO: If FNV is allowed, process it here */
+            send_dcn(s);
+            return -1;
+        }
+    }
+    /* Start document reception */
+    span_log(&s->logging,
+             SPAN_LOG_FLOW, 
+             "Get document at %dbps, modem %d\n",
+             fallback_sequence[s->current_fallback].bit_rate,
+             fallback_sequence[s->current_fallback].modem_type);
+    if (s->rx_file[0] == '\0')
+    {
+        span_log(&s->logging, SPAN_LOG_FLOW, "No document to receive\n");
+        s->current_status = T30_ERR_FILEERROR;
+        send_dcn(s);
+        return -1;
+    }
+    if (!s->in_message)
+    {
+        if (t4_rx_init(&s->t4, s->rx_file, s->output_encoding) == NULL)
+        {
+            span_log(&s->logging, SPAN_LOG_WARNING, "Cannot open target TIFF file '%s'\n", s->rx_file);
+            s->current_status = T30_ERR_FILEERROR;
+            send_dcn(s);
+            return -1;
+        }
+        s->operation_in_progress = OPERATION_IN_PROGRESS_T4_RX;
+    }
+    if (!(s->iaf & T30_IAF_MODE_NO_TCF))
+    {
+        /* TCF is always sent with long training */
+        s->short_train = FALSE;
+        set_state(s, T30_STATE_F_TCF);
+        queue_phase(s, T30_PHASE_C_NON_ECM_RX);
+        timer_t2_start(s);
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int send_deferred_pps_response(t30_state_t *s)
+{
+    queue_phase(s, T30_PHASE_D_TX);
+    if (s->ecm_first_bad_frame >= s->ecm_frames)
+    {
+        /* Everything was OK. We can accept the data and move on. */
+        t30_ecm_commit_partial_page(s);
+        switch (s->last_pps_fcf2)
+        {
+        case T30_NULL:
+            /* We can confirm this partial page. */
+            break;
+        default:
+            /* We can confirm the whole page. */
+            s->next_rx_step = s->last_pps_fcf2;
+            t4_rx_end_page(&(s->t4));
+            if (s->phase_d_handler)
+                s->phase_d_handler(s, s->phase_d_user_data, s->last_pps_fcf2);
+            rx_start_page(s);
+            break;
+        }
+        set_state(s, T30_STATE_F_POST_RCP_MCF);
+        send_simple_frame(s, T30_MCF);
+    }
+    else
+    {
+        /* We need to send the PPR frame we have created, to try to fill in the missing/bad data. */
+        set_state(s, T30_STATE_F_POST_RCP_PPR);
+        s->ecm_frame_map[0] = 0xFF;
+        s->ecm_frame_map[1] = 0x13;
+        s->ecm_frame_map[2] = (uint8_t) (T30_PPR | s->dis_received);
+        send_frame(s, s->ecm_frame_map, 3 + 32);
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int process_rx_pps(t30_state_t *s, const uint8_t *msg, int len)
+{
+    int page;
+    int block;
+    int frames;
+    int i;
+    int j;
+    int frame_no;
+
+    if (len < 7)
+    {
+        span_log(&s->logging, SPAN_LOG_FLOW, "Bad PPS message length %d.\n", len);
+        return -1;
+    }
+    s->last_pps_fcf2 = msg[3] & 0xFE;
+    page = msg[4];
+    block = msg[5];
+
+    /* The frames count is not well specified in T.30. In practice it seems it might be the
+       number of frames in the current block, or it might be the number of frames in the
+       current burst of transmission. For a burst of resent frames this would make it smaller
+       than the actual size of the block. If we only accept the number when it exceeds
+       previous values, we should get the real number of frames in the block. */
+    frames = msg[6] + 1;
+    if (s->ecm_frames < 0)
+    {
+        /* First time. Take the number and believe in it. */
+        s->ecm_frames = frames;
+    }
+    else
+    {
+        /* If things have gone wrong, the far end might try to send us zero FCD
+           frames. It can't represent zero in the block count field, so it might
+           put zero there, or it might simplistically insert (blocks - 1), and put
+           0xFF there. Beware of this. */
+        if (frames == 0xFF)
+        {
+            /* This is probably zero, erroneously rolled over to the maximum count */
+            frames = 0;
+        }
+    }
+    span_log(&s->logging, SPAN_LOG_FLOW, "Received PPS + %s - page %d, block %d, %d frames\n", t30_frametype(msg[3]), page, block, frames);
+    if (page != s->ecm_rx_page)
+    {
+        span_log(&s->logging, SPAN_LOG_FLOW, "ECM rx page mismatch - expected %d, but received %d.\n", s->ecm_rx_page, page);
+    }
+    if (block != s->ecm_block)
+    {
+        span_log(&s->logging, SPAN_LOG_FLOW, "ECM rx block mismatch - expected %d, but received %d.\n", s->ecm_block, block);
+    }
+    /* Build a bit map of which frames we now have stored OK */
+    frame_no = 0;
+    s->ecm_first_bad_frame = 256;
+    for (i = 3;  i < 3 + 32;  i++)
+    {
+        s->ecm_frame_map[i] = 0;
+        for (j = 0;  j < 8;  j++)
+        {
+            if (s->ecm_len[frame_no] < 0)
+            {   
+                s->ecm_frame_map[i] |= (1 << j);
+                if (frame_no < s->ecm_first_bad_frame)
+                    s->ecm_first_bad_frame = frame_no;
+            }
+            frame_no++;
+        }
+    }
+    /* Are there any bad frames, or does our scan represent things being OK? */
+    switch (s->last_pps_fcf2)
+    {
+    case T30_NULL:
+    case T30_EOP:
+    case T30_PRI_EOP:
+    case T30_EOM:
+    case T30_PRI_EOM:
+    case T30_EOS:
+    case T30_MPS:
+    case T30_PRI_MPS:
+        if (s->receiver_not_ready_count > 0)
+        {
+            s->receiver_not_ready_count--;
+            queue_phase(s, T30_PHASE_D_TX);
+            set_state(s, T30_STATE_F_POST_RCP_RNR);
+            send_simple_frame(s, T30_RNR);
+        }
+        else
+        {
+            send_deferred_pps_response(s);
+        }
+        break;
+    default:
+        unexpected_final_frame(s, msg, len);
+        break;
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void process_rx_ppr(t30_state_t *s, const uint8_t *msg, int len)
+{
+    int i;
+    int j;
+    int frame_no;
+    uint8_t frame[4];
+
+    if (len != 3 + 32)
+    {
+        span_log(&s->logging, SPAN_LOG_FLOW, "Bad length for PPR bits - %d\n", len);
+        /* TODO: probably should send DCN */
+        return;
+    }
+    /* Check which frames are OK, and mark them as OK. */
+    for (i = 0;  i < 32;  i++)
+    {
+        for (j = 0;  j < 8;  j++)
+        {
+            frame_no = (i << 3) + j;
+            /* Tick off the frames they are not complaining about as OK */
+            if ((msg[i + 3] & (1 << j)) == 0)
+            {
+                if (s->ecm_len[frame_no] >= 0)
+                    s->ecm_progress++;
+                s->ecm_len[frame_no] = -1;
+            }
+            else
+            {
+                if (frame_no < s->ecm_frames)
+                    span_log(&s->logging, SPAN_LOG_FLOW, "Frame %d to be resent\n", frame_no);
+#if 0
+                /* Diagnostic: See if the other end is complaining about something we didn't even send this time. */
+                if (s->ecm_len[frame_no] < 0)
+                    span_log(&s->logging, SPAN_LOG_FLOW, "PPR contains complaint about frame %d, which was not sent\n", frame_no);
+#endif
+            }
+        }
+    }
+    if (++s->ppr_count >= PPR_LIMIT_BEFORE_CTC_OR_EOR)
+    {
+        /* Continue to correct? */
+        /* Continue only if we have been making progress */
+        s->ppr_count = 0;
+        if (s->ecm_progress)
+        {
+            s->ecm_progress = 0;
+            queue_phase(s, T30_PHASE_D_TX);
+            set_state(s, T30_STATE_IV_CTC);
+            send_simple_frame(s, T30_CTC);
+        }
+        else
+        {
+            set_state(s, T30_STATE_IV_EOR);
+            queue_phase(s, T30_PHASE_D_TX);
+            frame[0] = 0xFF;
+            frame[1] = 0x13;
+            frame[2] = (uint8_t) (T30_EOR | s->dis_received);
+            frame[3] = (s->ecm_at_page_end)  ?  ((uint8_t) (s->next_tx_step | s->dis_received))  :  T30_NULL;
+            span_log(&s->logging, SPAN_LOG_FLOW, "Sending EOR + %s\n", t30_frametype(frame[3]));
+            send_frame(s, frame, 4);
+        }
+    }
+    else
+    {
+        /* Initiate resending of the remainder of the frames. */
+        set_state(s, T30_STATE_IV);
+        queue_phase(s, T30_PHASE_C_ECM_TX);
+        send_first_ecm_frame(s);
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static void process_rx_fcd(t30_state_t *s, const uint8_t *msg, int len)
+{
+    int frame_no;
+
+    /* Facsimile coded data */
+    switch (s->state)
+    {
+    case T30_STATE_F_DOC_ECM:
+        if (len <= 4 + 256)
+        {
+            frame_no = msg[3];
+            /* Just store the actual image data, and record its length */
+            span_log(&s->logging, SPAN_LOG_FLOW, "Storing ECM frame %d, length %d\n", frame_no, len - 4);
+            memcpy(&s->ecm_data[frame_no][0], &msg[4], len - 4);
+            s->ecm_len[frame_no] = (int16_t) (len - 4);
+            /* In case we are just after a CTC/CTR exchange, which kicked us back to long training */
+            s->short_train = TRUE;
+        }
+        else
+        {
+            unexpected_frame_length(s, msg, len);
+        }
+        break;
+    default:
+        unexpected_non_final_frame(s, msg, len);
+        break;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static void process_rx_rcp(t30_state_t *s, const uint8_t *msg, int len)
+{
+    /* Return to control for partial page. These might come through with or without the final frame tag,
+       so we have this routine to deal with the "no final frame tag" case. */
+    switch (s->state)
+    {
+    case T30_STATE_F_DOC_ECM:
+        set_state(s, T30_STATE_F_POST_DOC_ECM);
+        queue_phase(s, T30_PHASE_D_RX);
+        break;
+    case T30_STATE_F_POST_DOC_ECM:
+        /* Just ignore this */
+        break;
+    default:
+        unexpected_non_final_frame(s, msg, len);
+        break;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static void process_rx_fnv(t30_state_t *s, const uint8_t *msg, int len)
+{
+    logging_state_t *log;
+    const char *x;
+
+    /* Field not valid */
+    /* TODO: analyse the message, as per 5.3.6.2.13 */
+    if (!span_log_test(&s->logging, SPAN_LOG_FLOW))
+        return;
+    log = &s->logging;
+
+    if ((msg[3] & 0x01))
+        span_log(log, SPAN_LOG_FLOW, "  Incorrect password (PWD).\n");
+    if ((msg[3] & 0x02))
+        span_log(log, SPAN_LOG_FLOW, "  Selective polling reference (SEP) not known.\n");
+    if ((msg[3] & 0x04))
+        span_log(log, SPAN_LOG_FLOW, "  Sub-address (SUB) not known.\n");
+    if ((msg[3] & 0x08))
+        span_log(log, SPAN_LOG_FLOW, "  Sender identity (SID) not known.\n");
+    if ((msg[3] & 0x10))
+        span_log(log, SPAN_LOG_FLOW, "  Secure fax error.\n");
+    if ((msg[3] & 0x20))
+        span_log(log, SPAN_LOG_FLOW, "  Transmitting subscriber identity (TSI) not accepted.\n");
+    if ((msg[3] & 0x40))
+        span_log(log, SPAN_LOG_FLOW, "  Polled sub-address (PSA) not known.\n");
+    if (len > 4  &&  (msg[3] & DISBIT8))
+    {
+        if ((msg[4] & 0x01))
+            span_log(log, SPAN_LOG_FLOW, "  BFT negotiations request not accepted.\n");
+        if ((msg[4] & 0x02))
+            span_log(log, SPAN_LOG_FLOW, "  Internet routing address (IRA) not known.\n");
+        if ((msg[4] & 0x04))
+            span_log(log, SPAN_LOG_FLOW, "  Internet selective polling address (ISP) not known.\n");
+    }
+    if (len > 5)
+    {
+        span_log(log, SPAN_LOG_FLOW, "  FNV sequence number %d.\n", msg[5]);
+    }
+    if (len > 6)
+    {
+        switch (msg[6])
+        {
+        case T30_PWD:
+            x = "Incorrect password (PWD)";
+            break;
+        case T30_SEP:
+            x = "Selective polling reference (SEP) not known";
+            break;
+        case T30_SUB:
+        case T30_SUB | 0x01:
+            x = "Sub-address (SUB) not known";
+            break;
+        case T30_SID:
+        case T30_SID | 0x01:
+            x = "Sender identity (SID) not known";
+            break;
+        case T30_SPI:
+            x = "Secure fax error";
+            break;
+        case T30_TSI:
+        case T30_TSI | 0x01:
+            x = "Transmitting subscriber identity (TSI) not accepted";
+            break;
+        case T30_PSA:
+            x = "Polled sub-address (PSA) not known";
+            break;
+        default:
+            x = "???";
+            break;
+        }
+        span_log(log, SPAN_LOG_FLOW, "  FNV diagnostic info type %s.\n", x);
+    }
+    if (len > 7)
+    {
+        span_log(log, SPAN_LOG_FLOW, "  FNV length %d.\n", msg[7]);
+    }
+    /* We've decoded it, but we don't yet know how to deal with it, so treat it as unexpected */
+    unexpected_final_frame(s, msg, len);
+}
+/*- End of function --------------------------------------------------------*/
+
+static void process_state_answering(t30_state_t *s, const uint8_t *msg, int len)
+{
+    switch (msg[2] & 0xFE)
+    {
+    case T30_DIS:
+        /* TODO: This is a fudge to allow for starting up in T.38, where the other end has
+           seen DIS by analogue modem means, and has immediately sent DIS/DTC. We might have
+           missed useful info, like TSI, but just accept things and carry on form now. */
+        span_log(&s->logging, SPAN_LOG_FLOW, "DIS/DTC before DIS\n");
+        process_rx_dis_dtc(s, msg, len);
+        break;
+    case T30_DCS:
+        /* TODO: This is a fudge to allow for starting up in T.38, where the other end has
+           seen DIS by analogue modem means, and has immediately sent DCS. We might have
+           missed useful info, like TSI, but just accept things and carry on form now. */
+        span_log(&s->logging, SPAN_LOG_FLOW, "DCS before DIS\n");
+        process_rx_dcs(s, msg, len);
+        break;
+    case T30_DCN:
+        s->current_status = T30_ERR_TX_GOTDCN;
+        disconnect(s);
+        break;
+    default:
+        /* We don't know what to do with this. */
+        unexpected_final_frame(s, msg, len);
+        break;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static void process_state_b(t30_state_t *s, const uint8_t *msg, int len)
+{
+    switch (msg[2] & 0xFE)
+    {
+    case T30_DCN:
+        /* Just ignore any DCN's which appear at this stage. */
+        break;
+    case T30_CRP:
+        repeat_last_command(s);
+        break;
+    case T30_FNV:
+        process_rx_fnv(s, msg, len);
+        break;
+    default:
+        /* We don't know what to do with this. */
+        unexpected_final_frame(s, msg, len);
+        break;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static void process_state_c(t30_state_t *s, const uint8_t *msg, int len)
+{
+    switch (msg[2] & 0xFE)
+    {
+    case T30_DCN:
+        /* Just ignore any DCN's which appear at this stage. */
+        break;
+    case T30_CRP:
+        repeat_last_command(s);
+        break;
+    case T30_FNV:
+        process_rx_fnv(s, msg, len);
+        break;
+    default:
+        /* We don't know what to do with this. */
+        unexpected_final_frame(s, msg, len);
+        break;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static void process_state_d(t30_state_t *s, const uint8_t *msg, int len)
+{
+    /* We should be sending the DCS sequence right now */
+    switch (msg[2] & 0xFE)
+    {
+    case T30_DCN:
+        s->current_status = T30_ERR_TX_BADDCS;
+        disconnect(s);
+        break;
+    case T30_CRP:
+        repeat_last_command(s);
+        break;
+    case T30_FNV:
+        process_rx_fnv(s, msg, len);
+        break;
+    default:
+        /* We don't know what to do with this. */
+        unexpected_final_frame(s, msg, len);
+        break;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static void process_state_d_tcf(t30_state_t *s, const uint8_t *msg, int len)
+{
+    /* We should be sending the TCF data right now */
+    switch (msg[2] & 0xFE)
+    {
+    case T30_DCN:
+        s->current_status = T30_ERR_TX_BADDCS;
+        disconnect(s);
+        break;
+    case T30_CRP:
+        repeat_last_command(s);
+        break;
+    case T30_FNV:
+        process_rx_fnv(s, msg, len);
+        break;
+    default:
+        /* We don't know what to do with this. */
+        unexpected_final_frame(s, msg, len);
+        break;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static void process_state_d_post_tcf(t30_state_t *s, const uint8_t *msg, int len)
+{
+    switch (msg[2] & 0xFE)
+    {
+    case T30_CFR:
+        /* Trainability test succeeded. Send the document. */
+        span_log(&s->logging, SPAN_LOG_FLOW, "Trainability test succeeded\n");
+        s->retries = 0;
+        s->short_train = TRUE;
+        if (s->error_correcting_mode)
+        {
+            set_state(s, T30_STATE_IV);
+            queue_phase(s, T30_PHASE_C_ECM_TX);
+            send_first_ecm_frame(s);
+        }
+        else
+        {
+            set_state(s, T30_STATE_I);
+            queue_phase(s, T30_PHASE_C_NON_ECM_TX);
+        }
+        break;
+    case T30_FTT:
+        /* Trainability test failed. Try again. */
+        span_log(&s->logging, SPAN_LOG_FLOW, "Trainability test failed\n");
+        s->retries = 0;
+        s->short_train = FALSE;
+        if (step_fallback_entry(s) < 0)
+        {
+            /* We have fallen back as far as we can go. Give up. */
+            s->current_fallback = 0;
+            s->current_status = T30_ERR_CANNOT_TRAIN;
+            send_dcn(s);
+            break;
+        }
+        queue_phase(s, T30_PHASE_B_TX);
+        send_dcs_sequence(s, TRUE);
+        break;
+    case T30_DIS:
+        /* It appears they didn't see what we sent - retry the TCF */
+        if (++s->retries > MAX_MESSAGE_TRIES)
+        {
+            s->current_status = T30_ERR_RETRYDCN;
+            send_dcn(s);
+            break;
+        }
+        queue_phase(s, T30_PHASE_B_TX);
+        /* TODO: should we reassess the new DIS message, and possibly adjust the DCS we use? */
+        send_dcs_sequence(s, TRUE);
+        break;
+    case T30_DCN:
+        s->current_status = T30_ERR_TX_BADDCS;
+        disconnect(s);
+        break;
+    case T30_CRP:
+        repeat_last_command(s);
+        break;
+    case T30_FNV:
+        process_rx_fnv(s, msg, len);
+        break;
+    default:
+        /* We don't know what to do with this. */
+        unexpected_final_frame(s, msg, len);
+        break;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static void process_state_f_tcf(t30_state_t *s, const uint8_t *msg, int len)
+{
+    /* We should be receiving TCF right now, not HDLC messages */
+    switch (msg[2] & 0xFE)
+    {
+    case T30_CRP:
+        repeat_last_command(s);
+        break;
+    case T30_FNV:
+        process_rx_fnv(s, msg, len);
+        break;
+    default:
+        /* We don't know what to do with this. */
+        unexpected_final_frame(s, msg, len);
+        break;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static void process_state_f_cfr(t30_state_t *s, const uint8_t *msg, int len)
+{
+    /* We're waiting for a response to the CFR we sent */
+    switch (msg[2] & 0xFE)
+    {
+    case T30_CRP:
+        repeat_last_command(s);
+        break;
+    case T30_FNV:
+        process_rx_fnv(s, msg, len);
+        break;
+    default:
+        /* We don't know what to do with this. */
+        unexpected_final_frame(s, msg, len);
+        break;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static void process_state_f_ftt(t30_state_t *s, const uint8_t *msg, int len)
+{
+    /* We're waiting for a response to the FTT we sent */
+    switch (msg[2] & 0xFE)
+    {
+    case T30_DCS:
+        process_rx_dcs(s, msg, len);
+        break;
+    case T30_CRP:
+        repeat_last_command(s);
+        break;
+    case T30_FNV:
+        process_rx_fnv(s, msg, len);
+        break;
+    default:
+        /* We don't know what to do with this. */
+        unexpected_final_frame(s, msg, len);
+        break;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static void process_state_f_doc_non_ecm(t30_state_t *s, const uint8_t *msg, int len)
+{
+    /* If we are getting HDLC messages, and we have not moved to the _POST_DOC_NON_ECM
+       state, it looks like we didn't see the image data carrier properly. */
+    switch (msg[2] & 0xFE)
+    {
+    case T30_DIS:
+        process_rx_dis_dtc(s, msg, len);
+        break;
+    case T30_DCS:
+        process_rx_dcs(s, msg, len);
+        break;
+    case T30_MPS:
+        /* Treat this as a bad quality page. */
+        if (s->phase_d_handler)
+            s->phase_d_handler(s, s->phase_d_user_data, msg[2] & 0xFE);
+        s->next_rx_step = msg[2] & 0xFE;
+        queue_phase(s, T30_PHASE_D_TX);
+        set_state(s, T30_STATE_III_Q_RTN);
+        send_simple_frame(s, T30_RTN);
+        break;
+    case T30_PRI_MPS:
+        /* Treat this as a bad quality page. */
+        if (s->phase_d_handler)
+        {
+            s->phase_d_handler(s, s->phase_d_user_data, msg[2] & 0xFE);
+            s->timer_t3 = ms_to_samples(DEFAULT_TIMER_T3);
+        }
+        s->next_rx_step = msg[2] & 0xFE;
+        set_state(s, T30_STATE_III_Q_RTN);
+        break;
+    case T30_EOM:
+    case T30_EOS:
+        /* Treat this as a bad quality page. */
+        if (s->phase_d_handler)
+            s->phase_d_handler(s, s->phase_d_user_data, msg[2] & 0xFE);
+        s->next_rx_step = msg[2] & 0xFE;
+        /* Return to phase B */
+        queue_phase(s, T30_PHASE_B_TX);
+        set_state(s, T30_STATE_III_Q_RTN);
+        send_simple_frame(s, T30_RTN);
+        break;
+    case T30_PRI_EOM:
+        /* Treat this as a bad quality page. */
+        if (s->phase_d_handler)
+        {
+            s->phase_d_handler(s, s->phase_d_user_data, msg[2] & 0xFE);
+            s->timer_t3 = ms_to_samples(DEFAULT_TIMER_T3);
+        }
+        s->next_rx_step = T30_PRI_EOM;
+        set_state(s, T30_STATE_III_Q_RTN);
+        break;
+    case T30_EOP:
+        /* Treat this as a bad quality page. */
+        if (s->phase_d_handler)
+            s->phase_d_handler(s, s->phase_d_user_data, msg[2] & 0xFE);
+        s->next_rx_step = msg[2] & 0xFE;
+        queue_phase(s, T30_PHASE_D_TX);
+        set_state(s, T30_STATE_III_Q_RTN);
+        send_simple_frame(s, T30_RTN);
+        break;
+    case T30_PRI_EOP:
+        /* Treat this as a bad quality page. */
+        if (s->phase_d_handler)
+        {
+            s->phase_d_handler(s, s->phase_d_user_data, msg[2] & 0xFE);
+            s->timer_t3 = ms_to_samples(DEFAULT_TIMER_T3);
+        }
+        s->next_rx_step = msg[2] & 0xFE;
+        set_state(s, T30_STATE_III_Q_RTN);
+        break;
+    case T30_DCN:
+        s->current_status = T30_ERR_RX_DCNDATA;
+        disconnect(s);
+        break;
+    case T30_CRP:
+        repeat_last_command(s);
+        break;
+    case T30_FNV:
+        process_rx_fnv(s, msg, len);
+        break;
+    default:
+        /* We don't know what to do with this. */
+        unexpected_final_frame(s, msg, len);
+        break;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static void process_state_f_post_doc_non_ecm(t30_state_t *s, const uint8_t *msg, int len)
+{
+    switch (msg[2] & 0xFE)
+    {
+    case T30_MPS:
+        if (s->phase_d_handler)
+            s->phase_d_handler(s, s->phase_d_user_data, msg[2] & 0xFE);
+        s->next_rx_step = msg[2] & 0xFE;
+        queue_phase(s, T30_PHASE_D_TX);
+        switch (copy_quality(s))
+        {
+        case T30_COPY_QUALITY_PERFECT:
+        case T30_COPY_QUALITY_GOOD:
+            t4_rx_end_page(&(s->t4));
+            rx_start_page(s);
+            set_state(s, T30_STATE_III_Q_MCF);
+            send_simple_frame(s, T30_MCF);
+            break;
+        case T30_COPY_QUALITY_POOR:
+            t4_rx_end_page(&(s->t4));
+            rx_start_page(s);
+            set_state(s, T30_STATE_III_Q_RTP);
+            send_simple_frame(s, T30_RTP);
+            break;
+        case T30_COPY_QUALITY_BAD:
+            rx_start_page(s);
+            set_state(s, T30_STATE_III_Q_RTN);
+            send_simple_frame(s, T30_RTN);
+            break;
+        }
+        break;
+    case T30_PRI_MPS:
+        if (s->phase_d_handler)
+        {
+            s->phase_d_handler(s, s->phase_d_user_data, msg[2] & 0xFE);
+            s->timer_t3 = ms_to_samples(DEFAULT_TIMER_T3);
+        }
+        s->next_rx_step = msg[2] & 0xFE;
+        switch (copy_quality(s))
+        {
+        case T30_COPY_QUALITY_PERFECT:
+        case T30_COPY_QUALITY_GOOD:
+            t4_rx_end_page(&(s->t4));
+            t4_rx_end(&(s->t4));
+            s->operation_in_progress = OPERATION_IN_PROGRESS_NONE;
+            s->in_message = FALSE;
+            set_state(s, T30_STATE_III_Q_MCF);
+            break;
+        case T30_COPY_QUALITY_POOR:
+            t4_rx_end_page(&(s->t4));
+            t4_rx_end(&(s->t4));
+            s->operation_in_progress = OPERATION_IN_PROGRESS_NONE;
+            s->in_message = FALSE;
+            set_state(s, T30_STATE_III_Q_RTP);
+            break;
+        case T30_COPY_QUALITY_BAD:
+            set_state(s, T30_STATE_III_Q_RTN);
+            break;
+        }
+        break;
+    case T30_EOM:
+    case T30_EOS:
+        if (s->phase_d_handler)
+            s->phase_d_handler(s, s->phase_d_user_data, msg[2] & 0xFE);
+        s->next_rx_step = msg[2] & 0xFE;
+        /* Return to phase B */
+        queue_phase(s, T30_PHASE_B_TX);
+        switch (copy_quality(s))
+        {
+        case T30_COPY_QUALITY_PERFECT:
+        case T30_COPY_QUALITY_GOOD:
+            t4_rx_end_page(&(s->t4));
+            rx_start_page(s);
+            set_state(s, T30_STATE_III_Q_MCF);
+            send_simple_frame(s, T30_MCF);
+            break;
+        case T30_COPY_QUALITY_POOR:
+            t4_rx_end_page(&(s->t4));
+            rx_start_page(s);
+            set_state(s, T30_STATE_III_Q_RTP);
+            send_simple_frame(s, T30_RTP);
+            break;
+        case T30_COPY_QUALITY_BAD:
+            rx_start_page(s);
+            set_state(s, T30_STATE_III_Q_RTN);
+            send_simple_frame(s, T30_RTN);
+            break;
+        }
+        break;
+    case T30_PRI_EOM:
+        if (s->phase_d_handler)
+        {
+            s->phase_d_handler(s, s->phase_d_user_data, msg[2] & 0xFE);
+            s->timer_t3 = ms_to_samples(DEFAULT_TIMER_T3);
+        }
+        s->next_rx_step = msg[2] & 0xFE;
+        switch (copy_quality(s))
+        {
+        case T30_COPY_QUALITY_PERFECT:
+        case T30_COPY_QUALITY_GOOD:
+            t4_rx_end_page(&(s->t4));
+            t4_rx_end(&(s->t4));
+            s->operation_in_progress = OPERATION_IN_PROGRESS_NONE;
+            s->in_message = FALSE;
+            set_state(s, T30_STATE_III_Q_MCF);
+            break;
+        case T30_COPY_QUALITY_POOR:
+            t4_rx_end_page(&(s->t4));
+            t4_rx_end(&(s->t4));
+            s->operation_in_progress = OPERATION_IN_PROGRESS_NONE;
+            s->in_message = FALSE;
+            set_state(s, T30_STATE_III_Q_RTP);
+            break;
+        case T30_COPY_QUALITY_BAD:
+            set_state(s, T30_STATE_III_Q_RTN);
+            break;
+        }
+        break;
+    case T30_EOP:
+        if (s->phase_d_handler)
+            s->phase_d_handler(s, s->phase_d_user_data, msg[2] & 0xFE);
+        s->next_rx_step = msg[2] & 0xFE;
+        queue_phase(s, T30_PHASE_D_TX);
+        switch (copy_quality(s))
+        {
+        case T30_COPY_QUALITY_PERFECT:
+        case T30_COPY_QUALITY_GOOD:
+            t4_rx_end_page(&(s->t4));
+            t4_rx_end(&(s->t4));
+            s->operation_in_progress = OPERATION_IN_PROGRESS_NONE;
+            s->in_message = FALSE;
+            set_state(s, T30_STATE_III_Q_MCF);
+            send_simple_frame(s, T30_MCF);
+            break;
+        case T30_COPY_QUALITY_POOR:
+            t4_rx_end_page(&(s->t4));
+            t4_rx_end(&(s->t4));
+            s->operation_in_progress = OPERATION_IN_PROGRESS_NONE;
+            s->in_message = FALSE;
+            set_state(s, T30_STATE_III_Q_RTP);
+            send_simple_frame(s, T30_RTP);
+            break;
+        case T30_COPY_QUALITY_BAD:
+            set_state(s, T30_STATE_III_Q_RTN);
+            send_simple_frame(s, T30_RTN);
+            break;
+        }
+        break;
+    case T30_PRI_EOP:
+        if (s->phase_d_handler)
+        {
+            s->phase_d_handler(s, s->phase_d_user_data, msg[2] & 0xFE);
+            s->timer_t3 = ms_to_samples(DEFAULT_TIMER_T3);
+        }
+        s->next_rx_step = msg[2] & 0xFE;
+        switch (copy_quality(s))
+        {
+        case T30_COPY_QUALITY_PERFECT:
+        case T30_COPY_QUALITY_GOOD:
+            t4_rx_end_page(&(s->t4));
+            t4_rx_end(&(s->t4));
+            s->operation_in_progress = OPERATION_IN_PROGRESS_NONE;
+            s->in_message = FALSE;
+            set_state(s, T30_STATE_III_Q_MCF);
+            break;
+        case T30_COPY_QUALITY_POOR:
+            t4_rx_end_page(&(s->t4));
+            t4_rx_end(&(s->t4));
+            s->operation_in_progress = OPERATION_IN_PROGRESS_NONE;
+            s->in_message = FALSE;
+            set_state(s, T30_STATE_III_Q_RTP);
+            break;
+        case T30_COPY_QUALITY_BAD:
+            set_state(s, T30_STATE_III_Q_RTN);
+            break;
+        }
+        break;
+    case T30_DCN:
+        s->current_status = T30_ERR_RX_DCNFAX;
+        disconnect(s);
+        break;
+    case T30_CRP:
+        repeat_last_command(s);
+        break;
+    case T30_FNV:
+        process_rx_fnv(s, msg, len);
+        break;
+    default:
+        /* We don't know what to do with this. */
+        unexpected_final_frame(s, msg, len);
+        break;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static void process_state_f_doc_ecm(t30_state_t *s, const uint8_t *msg, int len)
+{
+    int fcf2;
+    
+    /* This actually handles 2 states - _DOC_ECM and _POST_DOC_ECM - as they are very similar */
+    switch (msg[2] & 0xFE)
+    {
+    case T30_DIS:
+        process_rx_dis_dtc(s, msg, len);
+        break;
+    case T30_DCS:
+        process_rx_dcs(s, msg, len);
+        break;
+    case T4_RCP:
+        if (s->state == T30_STATE_F_DOC_ECM)
+        {
+            /* Return to control for partial page */
+            queue_phase(s, T30_PHASE_D_RX);
+            set_state(s, T30_STATE_F_POST_DOC_ECM);
+        }
+        else
+        {
+            /* Ignore extra RCP frames. The source will usually send several to maximise the chance of
+               one getting through OK. */
+        }
+        break;
+    case T30_EOR:
+        if (len != 4)
+        {
+            unexpected_frame_length(s, msg, len);
+            break;
+        }
+        fcf2 = msg[3] & 0xFE;
+        span_log(&s->logging, SPAN_LOG_FLOW, "Received EOR + %s\n", t30_frametype(msg[3]));
+        switch (fcf2)
+        {
+        case T30_PRI_EOP:
+        case T30_PRI_EOM:
+        case T30_PRI_MPS:
+            /* TODO: Alert operator */
+            /* Fall through */
+        case T30_NULL:
+        case T30_EOP:
+        case T30_EOM:
+        case T30_EOS:
+        case T30_MPS:
+            s->next_rx_step = fcf2;
+            queue_phase(s, T30_PHASE_D_TX);
+            set_state(s, T30_STATE_F_DOC_ECM);
+            send_simple_frame(s, T30_ERR);
+            break;
+        default:
+            unexpected_final_frame(s, msg, len);
+            break;
+        }
+        break;
+    case T30_PPS:
+        process_rx_pps(s, msg, len);
+        break;
+    case T30_CTC:
+        /* T.30 says we change back to long training here */
+        s->short_train = FALSE;
+        queue_phase(s, T30_PHASE_D_TX);
+        set_state(s, T30_STATE_F_DOC_ECM);
+        send_simple_frame(s, T30_CTR);
+        break;
+    case T30_RR:
+        break;
+    case T30_DCN:
+        s->current_status = T30_ERR_RX_DCNDATA;
+        disconnect(s);
+        break;
+    case T30_CRP:
+        repeat_last_command(s);
+        break;
+    case T30_FNV:
+        process_rx_fnv(s, msg, len);
+        break;
+    default:
+        /* We don't know what to do with this. */
+        unexpected_final_frame(s, msg, len);
+        break;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static void process_state_f_post_rcp_mcf(t30_state_t *s, const uint8_t *msg, int len)
+{
+    switch (msg[2] & 0xFE)
+    {
+    case T30_CRP:
+        repeat_last_command(s);
+        break;
+    case T30_FNV:
+        process_rx_fnv(s, msg, len);
+        break;
+    case T30_DCN:
+        disconnect(s);
+        break;
+    default:
+        /* We don't know what to do with this. */
+        unexpected_final_frame(s, msg, len);
+        break;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static void process_state_f_post_rcp_ppr(t30_state_t *s, const uint8_t *msg, int len)
+{
+    switch (msg[2] & 0xFE)
+    {
+    case T30_CRP:
+        repeat_last_command(s);
+        break;
+    case T30_FNV:
+        process_rx_fnv(s, msg, len);
+        break;
+    default:
+        /* We don't know what to do with this. */
+        unexpected_final_frame(s, msg, len);
+        break;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static void process_state_f_post_rcp_rnr(t30_state_t *s, const uint8_t *msg, int len)
+{
+    switch (msg[2] & 0xFE)
+    {
+    case T30_RR:
+        if (s->receiver_not_ready_count > 0)
+        {
+            s->receiver_not_ready_count--;
+            queue_phase(s, T30_PHASE_D_TX);
+            set_state(s, T30_STATE_F_POST_RCP_RNR);
+            send_simple_frame(s, T30_RNR);
+        }
+        else
+        {
+            send_deferred_pps_response(s);
+        }
+        break;
+    case T30_CRP:
+        repeat_last_command(s);
+        break;
+    case T30_FNV:
+        process_rx_fnv(s, msg, len);
+        break;
+    default:
+        /* We don't know what to do with this. */
+        unexpected_final_frame(s, msg, len);
+        break;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static void process_state_r(t30_state_t *s, const uint8_t *msg, int len)
+{
+    switch (msg[2] & 0xFE)
+    {
+    case T30_DIS:
+        process_rx_dis_dtc(s, msg, len);
+        break;
+    case T30_DCS:
+        process_rx_dcs(s, msg, len);
+        break;
+    case T30_DCN:
+        /* Received a DCN while waiting for a DIS */
+        s->current_status = T30_ERR_TX_GOTDCN;
+        disconnect(s);
+        break;
+    case T30_CRP:
+        repeat_last_command(s);
+        break;
+    case T30_FNV:
+        process_rx_fnv(s, msg, len);
+        break;
+    default:
+        /* We don't know what to do with this. */
+        unexpected_final_frame(s, msg, len);
+        break;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static void process_state_t(t30_state_t *s, const uint8_t *msg, int len)
+{
+    switch (msg[2] & 0xFE)
+    {
+    case T30_DIS:
+        process_rx_dis_dtc(s, msg, len);
+        break;
+    case T30_DCN:
+        s->current_status = T30_ERR_RX_DCNWHY;
+        disconnect(s);
+        break;
+    case T30_CRP:
+        repeat_last_command(s);
+        break;
+    case T30_FNV:
+        process_rx_fnv(s, msg, len);
+        break;
+    default:
+        /* We don't know what to do with this. */
+        unexpected_final_frame(s, msg, len);
+        s->current_status = T30_ERR_TX_NODIS;
+        break;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static void process_state_i(t30_state_t *s, const uint8_t *msg, int len)
+{
+    switch (msg[2] & 0xFE)
+    {
+    case T30_CRP:
+        repeat_last_command(s);
+        break;
+    case T30_FNV:
+        process_rx_fnv(s, msg, len);
+        break;
+    default:
+        /* We don't know what to do with this. */
+        unexpected_final_frame(s, msg, len);
+        break;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static void process_state_ii(t30_state_t *s, const uint8_t *msg, int len)
+{
+    switch (msg[2] & 0xFE)
+    {
+    case T30_CRP:
+        repeat_last_command(s);
+        break;
+    case T30_FNV:
+        process_rx_fnv(s, msg, len);
+        break;
+    default:
+        /* We don't know what to do with this. */
+        unexpected_final_frame(s, msg, len);
+        break;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static void process_state_ii_q(t30_state_t *s, const uint8_t *msg, int len)
+{
+    t4_stats_t stats;
+
+    switch (msg[2] & 0xFE)
+    {
+    case T30_MCF:
+        switch (s->next_tx_step)
+        {
+        case T30_MPS:
+        case T30_PRI_MPS:
+            s->retries = 0;
+            t4_tx_end_page(&(s->t4));
+            if (s->phase_d_handler)
+                s->phase_d_handler(s, s->phase_d_user_data, T30_MCF);
+            if (tx_start_page(s))
+            {
+                /* TODO: recover */
+                break;
+            }
+            set_state(s, T30_STATE_I);
+            queue_phase(s, T30_PHASE_C_NON_ECM_TX);
+            break;
+        case T30_EOM:
+        case T30_PRI_EOM:
+        case T30_EOS:
+            s->retries = 0;
+            t4_tx_end_page(&(s->t4));
+            if (s->phase_d_handler)
+                s->phase_d_handler(s, s->phase_d_user_data, T30_MCF);
+            t4_tx_end(&(s->t4));
+            s->operation_in_progress = OPERATION_IN_PROGRESS_NONE;
+            set_state(s, T30_STATE_R);
+            if (span_log_test(&s->logging, SPAN_LOG_FLOW))
+            {
+                t4_get_transfer_statistics(&s->t4, &stats);
+                span_log(&s->logging, SPAN_LOG_FLOW, "Success - delivered %d pages\n", stats.pages_transferred);
+            }
+            break;
+        case T30_EOP:
+        case T30_PRI_EOP:
+            s->retries = 0;
+            t4_tx_end_page(&(s->t4));
+            if (s->phase_d_handler)
+                s->phase_d_handler(s, s->phase_d_user_data, T30_MCF);
+            t4_tx_end(&(s->t4));
+            s->operation_in_progress = OPERATION_IN_PROGRESS_NONE;
+            send_dcn(s);
+            if (span_log_test(&s->logging, SPAN_LOG_FLOW))
+            {
+                t4_get_transfer_statistics(&s->t4, &stats);
+                span_log(&s->logging, SPAN_LOG_FLOW, "Success - delivered %d pages\n", stats.pages_transferred);
+            }
+            break;
+        }
+        break;
+    case T30_RTP:
+        switch (s->next_tx_step)
+        {
+        case T30_MPS:
+        case T30_PRI_MPS:
+            s->retries = 0;
+            if (s->phase_d_handler)
+                s->phase_d_handler(s, s->phase_d_user_data, T30_RTP);
+            /* Send fresh training, and then the next page */
+            if (step_fallback_entry(s) < 0)
+            {
+                /* We have fallen back as far as we can go. Give up. */
+                s->current_fallback = 0;
+                s->current_status = T30_ERR_CANNOT_TRAIN;
+                send_dcn(s);
+                break;
+            }
+            queue_phase(s, T30_PHASE_B_TX);
+            restart_sending_document(s);
+            break;
+        case T30_EOM:
+        case T30_PRI_EOM:
+        case T30_EOS:
+            s->retries = 0;
+            if (s->phase_d_handler)
+                s->phase_d_handler(s, s->phase_d_user_data, T30_RTP);
+            if (step_fallback_entry(s) < 0)
+            {
+                /* We have fallen back as far as we can go. Give up. */
+                s->current_fallback = 0;
+                s->current_status = T30_ERR_CANNOT_TRAIN;
+                send_dcn(s);
+                break;
+            }
+            /* TODO: should go back to T, and resend */
+            set_state(s, T30_STATE_R);
+            break;
+        case T30_EOP:
+        case T30_PRI_EOP:
+            s->retries = 0;
+            if (s->phase_d_handler)
+                s->phase_d_handler(s, s->phase_d_user_data, T30_RTP);
+            s->current_status = T30_ERR_TX_INVALRSP;
+            send_dcn(s);
+            break;
+        }
+        break;
+    case T30_RTN:
+        switch (s->next_tx_step)
+        {
+        case T30_MPS:
+        case T30_PRI_MPS:
+            s->retries = 0;
+            if (s->phase_d_handler)
+                s->phase_d_handler(s, s->phase_d_user_data, T30_RTN);
+            /* Send fresh training, and then repeat the last page */
+            if (step_fallback_entry(s) < 0)
+            {
+                /* We have fallen back as far as we can go. Give up. */
+                s->current_fallback = 0;
+                s->current_status = T30_ERR_CANNOT_TRAIN;
+                send_dcn(s);
+                break;
+            }
+            queue_phase(s, T30_PHASE_B_TX);
+            restart_sending_document(s);
+            break;
+        case T30_EOP:
+        case T30_PRI_EOP:
+        case T30_EOM:
+        case T30_PRI_EOM:
+        case T30_EOS:
+            s->retries = 0;
+            if (s->phase_d_handler)
+                s->phase_d_handler(s, s->phase_d_user_data, T30_RTN);
+            s->current_status = T30_ERR_TX_INVALRSP;
+            send_dcn(s);
+            break;
+        }
+        break;
+    case T30_PIP:
+        s->retries = 0;
+        if (s->phase_d_handler)
+        {
+            s->phase_d_handler(s, s->phase_d_user_data, T30_PIP);
+            s->timer_t3 = ms_to_samples(DEFAULT_TIMER_T3);
+        }
+        break;
+    case T30_PIN:
+        s->retries = 0;
+        if (s->phase_d_handler)
+        {
+            s->phase_d_handler(s, s->phase_d_user_data, T30_PIN);
+            s->timer_t3 = ms_to_samples(DEFAULT_TIMER_T3);
+        }
+        break;
+    case T30_DCN:
+        switch (s->next_tx_step)
+        {
+        case T30_MPS:
+        case T30_PRI_MPS:
+        case T30_EOM:
+        case T30_PRI_EOM:
+        case T30_EOS:
+            /* Unexpected DCN after EOM, EOS or MPS sequence */
+            s->current_status = T30_ERR_RX_DCNPHD;
+            break;
+        default:
+            s->current_status = T30_ERR_TX_BADPG;
+            break;
+        }
+        disconnect(s);
+        break;
+    case T30_CRP:
+        repeat_last_command(s);
+        break;
+    case T30_FNV:
+        process_rx_fnv(s, msg, len);
+        break;
+    default:
+        /* We don't know what to do with this. */
+        unexpected_final_frame(s, msg, len);
+        break;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static void process_state_iii_q_mcf(t30_state_t *s, const uint8_t *msg, int len)
+{
+    switch (msg[2] & 0xFE)
+    {
+    case T30_EOP:
+    case T30_EOM:
+    case T30_EOS:
+    case T30_MPS:
+        /* Looks like they didn't see our signal. Repeat it */
+        queue_phase(s, T30_PHASE_D_TX);
+        set_state(s, T30_STATE_III_Q_MCF);
+        send_simple_frame(s, T30_MCF);
+        break;
+    case T30_DIS:
+        if (msg[2] == T30_DTC)
+            process_rx_dis_dtc(s, msg, len);
+        break;
+    case T30_CRP:
+        repeat_last_command(s);
+        break;
+    case T30_FNV:
+        process_rx_fnv(s, msg, len);
+        break;
+    case T30_DCN:
+        disconnect(s);
+        break;
+    default:
+        /* We don't know what to do with this. */
+        unexpected_final_frame(s, msg, len);
+        break;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static void process_state_iii_q_rtp(t30_state_t *s, const uint8_t *msg, int len)
+{
+    switch (msg[2] & 0xFE)
+    {
+    case T30_EOP:
+    case T30_EOM:
+    case T30_EOS:
+    case T30_MPS:
+        /* Looks like they didn't see our signal. Repeat it */
+        queue_phase(s, T30_PHASE_D_TX);
+        set_state(s, T30_STATE_III_Q_RTP);
+        send_simple_frame(s, T30_RTP);
+        break;
+    case T30_DIS:
+        if (msg[2] == T30_DTC)
+            process_rx_dis_dtc(s, msg, len);
+        break;
+    case T30_CRP:
+        repeat_last_command(s);
+        break;
+    case T30_FNV:
+        process_rx_fnv(s, msg, len);
+        break;
+    default:
+        /* We don't know what to do with this. */
+        unexpected_final_frame(s, msg, len);
+        break;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static void process_state_iii_q_rtn(t30_state_t *s, const uint8_t *msg, int len)
+{
+    switch (msg[2] & 0xFE)
+    {
+    case T30_EOP:
+    case T30_EOM:
+    case T30_EOS:
+    case T30_MPS:
+        /* Looks like they didn't see our signal. Repeat it */
+        queue_phase(s, T30_PHASE_D_TX);
+        set_state(s, T30_STATE_III_Q_RTN);
+        send_simple_frame(s, T30_RTN);
+        break;
+    case T30_DIS:
+        if (msg[2] == T30_DTC)
+            process_rx_dis_dtc(s, msg, len);
+        break;
+    case T30_CRP:
+        repeat_last_command(s);
+        break;
+    case T30_FNV:
+        process_rx_fnv(s, msg, len);
+        break;
+    case T30_DCN:
+        s->current_status = T30_ERR_RX_DCNNORTN;
+        disconnect(s);
+        break;
+    default:
+        /* We don't know what to do with this. */
+        unexpected_final_frame(s, msg, len);
+        break;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static void process_state_iv(t30_state_t *s, const uint8_t *msg, int len)
+{
+    switch (msg[2] & 0xFE)
+    {
+    case T30_CRP:
+        repeat_last_command(s);
+        break;
+    case T30_FNV:
+        process_rx_fnv(s, msg, len);
+        break;
+    default:
+        /* We don't know what to do with this. */
+        unexpected_final_frame(s, msg, len);
+        break;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static void process_state_iv_pps_null(t30_state_t *s, const uint8_t *msg, int len)
+{
+    t4_stats_t stats;
+
+    switch (msg[2] & 0xFE)
+    {
+    case T30_MCF:
+        s->retries = 0;
+        s->timer_t5 = 0;
+        /* Is there more of the current page to get, or do we move on? */
+        span_log(&s->logging, SPAN_LOG_FLOW, "Is there more to send? - %d %d\n", s->ecm_frames, s->ecm_len[255]);
+        if (!s->ecm_at_page_end  &&  get_partial_ecm_page(s) > 0)
+        {
+            span_log(&s->logging, SPAN_LOG_WARNING, "Additional image data to send\n");
+            s->ecm_block++;
+            set_state(s, T30_STATE_IV);
+            queue_phase(s, T30_PHASE_C_ECM_TX);
+            send_first_ecm_frame(s);
+        }
+        else
+        {
+            span_log(&s->logging, SPAN_LOG_FLOW, "Moving on to the next page\n");
+            switch (s->next_tx_step)
+            {
+            case T30_MPS:
+            case T30_PRI_MPS:
+                s->retries = 0;
+                t4_tx_end_page(&(s->t4));
+                if (s->phase_d_handler)
+                    s->phase_d_handler(s, s->phase_d_user_data, T30_MCF);
+                if (tx_start_page(s))
+                {
+                    /* TODO: recover */
+                    break;
+                }
+                if (get_partial_ecm_page(s) > 0)
+                {
+                    set_state(s, T30_STATE_IV);
+                    queue_phase(s, T30_PHASE_C_ECM_TX);
+                    send_first_ecm_frame(s);
+                }
+                break;
+            case T30_EOM:
+            case T30_PRI_EOM:
+            case T30_EOS:
+                s->retries = 0;
+                t4_tx_end_page(&(s->t4));
+                if (s->phase_d_handler)
+                    s->phase_d_handler(s, s->phase_d_user_data, T30_MCF);
+                t4_tx_end(&(s->t4));
+                s->operation_in_progress = OPERATION_IN_PROGRESS_NONE;
+                set_state(s, T30_STATE_R);
+                if (span_log_test(&s->logging, SPAN_LOG_FLOW))
+                {
+                    t4_get_transfer_statistics(&s->t4, &stats);
+                    span_log(&s->logging, SPAN_LOG_FLOW, "Success - delivered %d pages\n", stats.pages_transferred);
+                }
+                break;
+            case T30_EOP:
+            case T30_PRI_EOP:
+                s->retries = 0;
+                t4_tx_end_page(&(s->t4));
+                if (s->phase_d_handler)
+                    s->phase_d_handler(s, s->phase_d_user_data, T30_MCF);
+                t4_tx_end(&(s->t4));
+                s->operation_in_progress = OPERATION_IN_PROGRESS_NONE;
+                send_dcn(s);
+                if (span_log_test(&s->logging, SPAN_LOG_FLOW))
+                {
+                    t4_get_transfer_statistics(&s->t4, &stats);
+                    span_log(&s->logging, SPAN_LOG_FLOW, "Success - delivered %d pages\n", stats.pages_transferred);
+                }
+                break;
+            }
+        }
+        break;
+    case T30_PPR:
+        process_rx_ppr(s, msg, len);
+        break;
+    case T30_RNR:
+        if (s->timer_t5 == 0)
+            s->timer_t5 = ms_to_samples(DEFAULT_TIMER_T5);
+        queue_phase(s, T30_PHASE_D_TX);
+        set_state(s, T30_STATE_IV_PPS_RNR);
+        send_simple_frame(s, T30_RR);
+        break;
+    case T30_DCN:
+        s->current_status = T30_ERR_TX_BADPG;
+        disconnect(s);
+        break;
+    case T30_CRP:
+        repeat_last_command(s);
+        break;
+    case T30_FNV:
+        process_rx_fnv(s, msg, len);
+        break;
+    default:
+        /* We don't know what to do with this. */
+        unexpected_final_frame(s, msg, len);
+        s->current_status = T30_ERR_TX_ECMPHD;
+        break;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static void process_state_iv_pps_q(t30_state_t *s, const uint8_t *msg, int len)
+{
+    t4_stats_t stats;
+
+    switch (msg[2] & 0xFE)
+    {
+    case T30_MCF:
+        s->retries = 0;
+        s->timer_t5 = 0;
+        /* Is there more of the current page to get, or do we move on? */
+        span_log(&s->logging, SPAN_LOG_FLOW, "Is there more to send? - %d %d\n", s->ecm_frames, s->ecm_len[255]);
+        if (!s->ecm_at_page_end  &&  get_partial_ecm_page(s) > 0)
+        {
+            span_log(&s->logging, SPAN_LOG_WARNING, "Additional image data to send\n");
+            s->ecm_block++;
+            set_state(s, T30_STATE_IV);
+            queue_phase(s, T30_PHASE_C_ECM_TX);
+            send_first_ecm_frame(s);
+        }
+        else
+        {
+
+            span_log(&s->logging, SPAN_LOG_FLOW, "Moving on to the next page\n");
+            switch (s->next_tx_step)
+            {
+            case T30_MPS:
+            case T30_PRI_MPS:
+                s->retries = 0;
+                t4_tx_end_page(&(s->t4));
+                if (s->phase_d_handler)
+                    s->phase_d_handler(s, s->phase_d_user_data, T30_MCF);
+                if (tx_start_page(s))
+                {
+                    /* TODO: recover */
+                    break;
+                }
+                if (get_partial_ecm_page(s) > 0)
+                {
+                    set_state(s, T30_STATE_IV);
+                    queue_phase(s, T30_PHASE_C_ECM_TX);
+                    send_first_ecm_frame(s);
+                }
+                break;
+            case T30_EOM:
+            case T30_PRI_EOM:
+            case T30_EOS:
+                s->retries = 0;
+                t4_tx_end_page(&(s->t4));
+                if (s->phase_d_handler)
+                    s->phase_d_handler(s, s->phase_d_user_data, T30_MCF);
+                t4_tx_end(&(s->t4));
+                s->operation_in_progress = OPERATION_IN_PROGRESS_NONE;
+                set_state(s, T30_STATE_R);
+                if (span_log_test(&s->logging, SPAN_LOG_FLOW))
+                {
+                    t4_get_transfer_statistics(&s->t4, &stats);
+                    span_log(&s->logging, SPAN_LOG_FLOW, "Success - delivered %d pages\n", stats.pages_transferred);
+                }
+                break;
+            case T30_EOP:
+            case T30_PRI_EOP:
+                s->retries = 0;
+                t4_tx_end_page(&(s->t4));
+                if (s->phase_d_handler)
+                    s->phase_d_handler(s, s->phase_d_user_data, T30_MCF);
+                t4_tx_end(&(s->t4));
+                s->operation_in_progress = OPERATION_IN_PROGRESS_NONE;
+                send_dcn(s);
+                if (span_log_test(&s->logging, SPAN_LOG_FLOW))
+                {
+                    t4_get_transfer_statistics(&s->t4, &stats);
+                    span_log(&s->logging, SPAN_LOG_FLOW, "Success - delivered %d pages\n", stats.pages_transferred);
+                }
+                break;
+            }
+        }
+        break;
+    case T30_RNR:
+        if (s->timer_t5 == 0)
+            s->timer_t5 = ms_to_samples(DEFAULT_TIMER_T5);
+        queue_phase(s, T30_PHASE_D_TX);
+        set_state(s, T30_STATE_IV_PPS_RNR);
+        send_simple_frame(s, T30_RR);
+        break;
+    case T30_PIP:
+        s->retries = 0;
+        if (s->phase_d_handler)
+        {
+            s->phase_d_handler(s, s->phase_d_user_data, T30_PIP);
+            s->timer_t3 = ms_to_samples(DEFAULT_TIMER_T3);
+        }
+        break;
+    case T30_PIN:
+        s->retries = 0;
+        if (s->phase_d_handler)
+        {
+            s->phase_d_handler(s, s->phase_d_user_data, T30_PIN);
+            s->timer_t3 = ms_to_samples(DEFAULT_TIMER_T3);
+        }
+        break;
+    case T30_PPR:
+        process_rx_ppr(s, msg, len);
+        break;
+    case T30_DCN:
+        s->current_status = T30_ERR_TX_BADPG;
+        disconnect(s);
+        break;
+    case T30_CRP:
+        repeat_last_command(s);
+        break;
+    case T30_FNV:
+        process_rx_fnv(s, msg, len);
+        break;
+    default:
+        /* We don't know what to do with this. */
+        unexpected_final_frame(s, msg, len);
+        s->current_status = T30_ERR_TX_ECMPHD;
+        break;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static void process_state_iv_pps_rnr(t30_state_t *s, const uint8_t *msg, int len)
+{
+    t4_stats_t stats;
+
+    switch (msg[2] & 0xFE)
+    {
+    case T30_MCF:
+        s->retries = 0;
+        s->timer_t5 = 0;
+        /* Is there more of the current page to get, or do we move on? */
+        span_log(&s->logging, SPAN_LOG_FLOW, "Is there more to send? - %d %d\n", s->ecm_frames, s->ecm_len[255]);
+        if (!s->ecm_at_page_end  &&  get_partial_ecm_page(s) > 0)
+        {
+            span_log(&s->logging, SPAN_LOG_WARNING, "Additional image data to send\n");
+            s->ecm_block++;
+            set_state(s, T30_STATE_IV);
+            queue_phase(s, T30_PHASE_C_ECM_TX);
+            send_first_ecm_frame(s);
+        }
+        else
+        {
+            span_log(&s->logging, SPAN_LOG_FLOW, "Moving on to the next page\n");
+            switch (s->next_tx_step)
+            {
+            case T30_MPS:
+            case T30_PRI_MPS:
+                s->retries = 0;
+                t4_tx_end_page(&(s->t4));
+                if (s->phase_d_handler)
+                    s->phase_d_handler(s, s->phase_d_user_data, T30_MCF);
+                if (tx_start_page(s))
+                {
+                    /* TODO: recover */
+                    break;
+                }
+                if (get_partial_ecm_page(s) > 0)
+                {
+                    set_state(s, T30_STATE_IV);
+                    queue_phase(s, T30_PHASE_C_ECM_TX);
+                    send_first_ecm_frame(s);
+                }
+                break;
+            case T30_EOM:
+            case T30_PRI_EOM:
+            case T30_EOS:
+                s->retries = 0;
+                t4_tx_end_page(&(s->t4));
+                if (s->phase_d_handler)
+                    s->phase_d_handler(s, s->phase_d_user_data, T30_MCF);
+                t4_tx_end(&(s->t4));
+                s->operation_in_progress = OPERATION_IN_PROGRESS_NONE;
+                set_state(s, T30_STATE_R);
+                if (span_log_test(&s->logging, SPAN_LOG_FLOW))
+                {
+                    t4_get_transfer_statistics(&s->t4, &stats);
+                    span_log(&s->logging, SPAN_LOG_FLOW, "Success - delivered %d pages\n", stats.pages_transferred);
+                }
+                break;
+            case T30_EOP:
+            case T30_PRI_EOP:
+                s->retries = 0;
+                t4_tx_end_page(&(s->t4));
+                if (s->phase_d_handler)
+                    s->phase_d_handler(s, s->phase_d_user_data, T30_MCF);
+                t4_tx_end(&(s->t4));
+                s->operation_in_progress = OPERATION_IN_PROGRESS_NONE;
+                send_dcn(s);
+                if (span_log_test(&s->logging, SPAN_LOG_FLOW))
+                {
+                    t4_get_transfer_statistics(&s->t4, &stats);
+                    span_log(&s->logging, SPAN_LOG_FLOW, "Success - delivered %d pages\n", stats.pages_transferred);
+                }
+                break;
+            }
+        }
+        break;
+    case T30_RNR:
+        if (s->timer_t5 == 0)
+            s->timer_t5 = ms_to_samples(DEFAULT_TIMER_T5);
+        queue_phase(s, T30_PHASE_D_TX);
+        set_state(s, T30_STATE_IV_PPS_RNR);
+        send_simple_frame(s, T30_RR);
+        break;
+    case T30_PIP:
+        s->retries = 0;
+        if (s->phase_d_handler)
+        {
+            s->phase_d_handler(s, s->phase_d_user_data, T30_PIP);
+            s->timer_t3 = ms_to_samples(DEFAULT_TIMER_T3);
+        }
+        break;
+    case T30_PIN:
+        s->retries = 0;
+        if (s->phase_d_handler)
+        {
+            s->phase_d_handler(s, s->phase_d_user_data, T30_PIN);
+            s->timer_t3 = ms_to_samples(DEFAULT_TIMER_T3);
+        }
+        break;
+    case T30_DCN:
+        s->current_status = T30_ERR_RX_DCNRRD;
+        disconnect(s);
+        break;
+    case T30_CRP:
+        repeat_last_command(s);
+        break;
+    case T30_FNV:
+        process_rx_fnv(s, msg, len);
+        break;
+    default:
+        /* We don't know what to do with this. */
+        unexpected_final_frame(s, msg, len);
+        break;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static void process_state_iv_ctc(t30_state_t *s, const uint8_t *msg, int len)
+{
+    switch (msg[2] & 0xFE)
+    {
+    case T30_CTR:
+        /* Valid response to a CTC received */
+        /* T.30 says we change back to long training here */
+        s->short_train = FALSE;
+        /* Initiate resending of the remainder of the frames. */
+        set_state(s, T30_STATE_IV);
+        queue_phase(s, T30_PHASE_C_ECM_TX);
+        send_first_ecm_frame(s);
+        break;
+    case T30_CRP:
+        repeat_last_command(s);
+        break;
+    case T30_FNV:
+        process_rx_fnv(s, msg, len);
+        break;
+    default:
+        /* We don't know what to do with this. */
+        unexpected_final_frame(s, msg, len);
+        break;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static void process_state_iv_eor(t30_state_t *s, const uint8_t *msg, int len)
+{
+    switch (msg[2] & 0xFE)
+    {
+    case T30_RNR:
+        if (s->timer_t5 == 0)
+            s->timer_t5 = ms_to_samples(DEFAULT_TIMER_T5);
+        queue_phase(s, T30_PHASE_D_TX);
+        set_state(s, T30_STATE_IV_EOR_RNR);
+        send_simple_frame(s, T30_RR);
+        break;
+    case T30_PIN:
+        s->retries = 0;
+        if (s->phase_d_handler)
+        {
+            s->phase_d_handler(s, s->phase_d_user_data, T30_PIN);
+            s->timer_t3 = ms_to_samples(DEFAULT_TIMER_T3);
+        }
+        break;
+    case T30_ERR:
+        /* TODO: Continue with the next message if MPS or EOM? */
+        s->timer_t5 = 0;
+        send_dcn(s);
+        break;
+    case T30_CRP:
+        repeat_last_command(s);
+        break;
+    case T30_FNV:
+        process_rx_fnv(s, msg, len);
+        break;
+    default:
+        /* We don't know what to do with this. */
+        unexpected_final_frame(s, msg, len);
+        break;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static void process_state_iv_eor_rnr(t30_state_t *s, const uint8_t *msg, int len)
+{
+    switch (msg[2] & 0xFE)
+    {
+    case T30_RNR:
+        if (s->timer_t5 == 0)
+            s->timer_t5 = ms_to_samples(DEFAULT_TIMER_T5);
+        queue_phase(s, T30_PHASE_D_TX);
+        set_state(s, T30_STATE_IV_EOR_RNR);
+        send_simple_frame(s, T30_RR);
+        break;
+    case T30_PIN:
+        s->retries = 0;
+        if (s->phase_d_handler)
+        {
+            s->phase_d_handler(s, s->phase_d_user_data, T30_PIN);
+            s->timer_t3 = ms_to_samples(DEFAULT_TIMER_T3);
+        }
+        break;
+    case T30_ERR:
+        /* TODO: Continue with the next message if MPS or EOM? */
+        s->timer_t5 = 0;
+        send_dcn(s);
+        break;
+    case T30_DCN:
+        s->current_status = T30_ERR_RX_DCNRRD;
+        disconnect(s);
+        break;
+    case T30_CRP:
+        repeat_last_command(s);
+        break;
+    case T30_FNV:
+        process_rx_fnv(s, msg, len);
+        break;
+    default:
+        /* We don't know what to do with this. */
+        unexpected_final_frame(s, msg, len);
+        break;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static void process_state_call_finished(t30_state_t *s, const uint8_t *msg, int len)
+{
+    /* Simply ignore anything which comes in when we have declared the call
+       to have finished. */
+}
+/*- End of function --------------------------------------------------------*/
+
+static void process_rx_control_msg(t30_state_t *s, const uint8_t *msg, int len)
+{
+    /* We should only get good frames here. */
+    print_frame(s, "Rx: ", msg, len);
+    if (s->real_time_frame_handler)
+        s->real_time_frame_handler(s, s->real_time_frame_user_data, TRUE, msg, len);
+
+    if ((msg[1] & 0x10) == 0)
+    {
+        /* This is not a final frame */
+        /* It seems we should not restart the command or response timer when exchanging HDLC image
+           data. If the modem looses sync in the middle of the image, we should just wait until
+           the carrier goes away before proceeding. */
+        if (s->phase != T30_PHASE_C_ECM_RX)
+        {
+            /* Restart the command or response timer, T2 or T4 */
+            switch (s->timer_t2_t4_is)
+            {
+            case TIMER_IS_T2:
+            case TIMER_IS_T2A:
+            case TIMER_IS_T2B:
+                timer_t2a_start(s);
+                break;
+            case TIMER_IS_T4:
+            case TIMER_IS_T4A:
+            case TIMER_IS_T4B:
+                timer_t4a_start(s);
+                break;
+            }
+        }
+        /* The following handles all the message types we expect to get without
+           a final frame tag. If we get one that T.30 says we should not expect
+           in a particular context, its pretty harmless, so don't worry. */
+        switch (msg[2] & 0xFE)
+        {
+        case (T30_CSI & 0xFE):
+            /* Called subscriber identification or Calling subscriber identification (T30_CIG) */
+            /* OK in (NSF) (CSI) DIS */
+            /* OK in (NSC) (CIG) DTC */
+            /* OK in (PWD) (SEP) (CIG) DTC */
+            decode_20digit_msg(s, s->rx_info.ident, &msg[2], len - 2);
+            break;
+        case (T30_NSF & 0xFE):
+            if (msg[2] == T30_NSF)
+            {
+                /* Non-standard facilities */
+                /* OK in (NSF) (CSI) DIS */
+                t35_decode(&msg[3], len - 3, &s->country, &s->vendor, &s->model);
+                if (s->country)
+                    span_log(&s->logging, SPAN_LOG_FLOW, "The remote was made in '%s'\n", s->country);
+                if (s->vendor)
+                    span_log(&s->logging, SPAN_LOG_FLOW, "The remote was made by '%s'\n", s->vendor);
+                if (s->model)
+                    span_log(&s->logging, SPAN_LOG_FLOW, "The remote is a '%s'\n", s->model);
+            }
+            else
+            {
+                /* NSC - Non-standard facilities command */
+                /* OK in (NSC) (CIG) DTC */
+            }
+            break;
+        case (T30_PWD & 0xFE):
+            if (msg[2] == T30_PWD)
+            {
+                /* Password */
+                /* OK in (SUB) (SID) (SEP) (PWD) (TSI) DCS */
+                /* OK in (SUB) (SID) (SEP) (PWD) (CIG) DTC */
+                decode_20digit_msg(s, s->rx_info.password, &msg[2], len - 2);
+            }
+            else
+            {
+                unexpected_frame(s, msg, len);
+            }
+            break;
+        case (T30_SEP & 0xFE):
+            if (msg[2] == T30_SEP)
+            {
+                /* Selective polling address */
+                /* OK in (PWD) (SEP) (CIG) DTC */
+                decode_20digit_msg(s, s->rx_info.selective_polling_address, &msg[2], len - 2);
+            }
+            else
+            {
+                unexpected_frame(s, msg, len);
+            }
+            break;
+        case (T30_PSA & 0xFE):
+            if (msg[2] == T30_PSA)
+            {
+                /* Polled sub-address */
+                decode_20digit_msg(s, s->rx_info.polled_sub_address, &msg[2], len - 2);
+            }
+            else
+            {
+                unexpected_frame(s, msg, len);
+            }
+            break;
+        case (T30_CIA & 0xFE):
+            if (msg[2] == T30_CIA)
+            {
+                /* Calling subscriber internet address */
+                decode_url_msg(s, NULL, &msg[2], len - 2);
+            }
+            else
+            {
+                unexpected_frame(s, msg, len);
+            }
+            break;
+        case (T30_ISP & 0xFE):
+            if (msg[2] == T30_ISP)
+            {
+                /* Internet selective polling address */
+                decode_url_msg(s, NULL, &msg[2], len - 2);
+            }
+            else
+            {
+                unexpected_frame(s, msg, len);
+            }
+            break;
+        case (T30_TSI & 0xFE):
+            /* Transmitting subscriber identity */
+            /* OK in (PWD) (SUB) (TSI) DCS */
+            decode_20digit_msg(s, s->rx_info.ident, &msg[2], len - 2);
+            break;
+        case (T30_NSS & 0xFE):
+            /* Non-standard facilities set-up */
+            break;
+        case (T30_SUB & 0xFE):
+            /* Sub-address */
+            /* OK in (PWD) (SUB) (TSI) DCS */
+            decode_20digit_msg(s, s->rx_info.sub_address, &msg[2], len - 2);
+            break;
+        case (T30_SID & 0xFE):
+            /* Sender Identification */
+            /* OK in (SUB) (SID) (SEP) (PWD) (TSI) DCS */
+            /* OK in (SUB) (SID) (SEP) (PWD) (CIG) DTC */
+            decode_20digit_msg(s, s->rx_info.sender_ident, &msg[2], len - 2);
+            break;
+        case (T30_CSA & 0xFE):
+            /* Calling subscriber internet address */
+            decode_url_msg(s, NULL, &msg[2], len - 2);
+            break;
+        case (T30_TSA & 0xFE):
+            /* Transmitting subscriber internet address */
+            decode_url_msg(s, NULL, &msg[2], len - 2);
+            break;
+        case (T30_IRA & 0xFE):
+            /* Internet routing address */
+            decode_url_msg(s, NULL, &msg[2], len - 2);
+            break;
+        case T4_FCD:
+            process_rx_fcd(s, msg, len);
+            break;
+        case T4_RCP:
+            process_rx_rcp(s, msg, len);
+            break;
+        default:
+            unexpected_non_final_frame(s, msg, len);
+            break;
+        }
+    }
+    else
+    {
+        /* This is a final frame */
+        /* Once we have any successful message from the far end, we
+           cancel timer T1 */
+        s->timer_t0_t1 = 0;
+
+        /* The following handles context sensitive message types, which should
+           occur at the end of message sequences. They should, therefore have
+           the final frame flag set. */
+        span_log(&s->logging, SPAN_LOG_FLOW, "In state %d\n", s->state);
+
+        switch (s->state)
+        {
+        case T30_STATE_ANSWERING:
+            process_state_answering(s, msg, len);
+            break;
+        case T30_STATE_B:
+            process_state_b(s, msg, len);
+            break;
+        case T30_STATE_C:
+            process_state_c(s, msg, len);
+            break;
+        case T30_STATE_D:
+            process_state_d(s, msg, len);
+            break;
+        case T30_STATE_D_TCF:
+            process_state_d_tcf(s, msg, len);
+            break;
+        case T30_STATE_D_POST_TCF:
+            process_state_d_post_tcf(s, msg, len);
+            break;
+        case T30_STATE_F_TCF:
+            process_state_f_tcf(s, msg, len);
+            break;
+        case T30_STATE_F_CFR:
+            process_state_f_cfr(s, msg, len);
+            break;
+        case T30_STATE_F_FTT:
+            process_state_f_ftt(s, msg, len);
+            break;
+        case T30_STATE_F_DOC_NON_ECM:
+            process_state_f_doc_non_ecm(s, msg, len);
+            break;
+        case T30_STATE_F_POST_DOC_NON_ECM:
+            process_state_f_post_doc_non_ecm(s, msg, len);
+            break;
+        case T30_STATE_F_DOC_ECM:
+        case T30_STATE_F_POST_DOC_ECM:
+            process_state_f_doc_ecm(s, msg, len);
+            break;
+        case T30_STATE_F_POST_RCP_MCF:
+            process_state_f_post_rcp_mcf(s, msg, len);
+            break;
+        case T30_STATE_F_POST_RCP_PPR:
+            process_state_f_post_rcp_ppr(s, msg, len);
+            break;
+        case T30_STATE_F_POST_RCP_RNR:
+            process_state_f_post_rcp_rnr(s, msg, len);
+            break;
+        case T30_STATE_R:
+            process_state_r(s, msg, len);
+            break;
+        case T30_STATE_T:
+            process_state_t(s, msg, len);
+            break;
+        case T30_STATE_I:
+            process_state_i(s, msg, len);
+            break;
+        case T30_STATE_II:
+            process_state_ii(s, msg, len);
+            break;
+        case T30_STATE_II_Q:
+            process_state_ii_q(s, msg, len);
+            break;
+        case T30_STATE_III_Q_MCF:
+            process_state_iii_q_mcf(s, msg, len);
+            break;
+        case T30_STATE_III_Q_RTP:
+            process_state_iii_q_rtp(s, msg, len);
+            break;
+        case T30_STATE_III_Q_RTN:
+            process_state_iii_q_rtn(s, msg, len);
+            break;
+        case T30_STATE_IV:
+            process_state_iv(s, msg, len);
+            break;
+        case T30_STATE_IV_PPS_NULL:
+            process_state_iv_pps_null(s, msg, len);
+            break;
+        case T30_STATE_IV_PPS_Q:
+            process_state_iv_pps_q(s, msg, len);
+            break;
+        case T30_STATE_IV_PPS_RNR:
+            process_state_iv_pps_rnr(s, msg, len);
+            break;
+        case T30_STATE_IV_CTC:
+            process_state_iv_ctc(s, msg, len);
+            break;
+        case T30_STATE_IV_EOR:
+            process_state_iv_eor(s, msg, len);
+            break;
+        case T30_STATE_IV_EOR_RNR:
+            process_state_iv_eor_rnr(s, msg, len);
+            break;
+        case T30_STATE_CALL_FINISHED:
+            process_state_call_finished(s, msg, len);
+            break;
+        default:
+            /* We don't know what to do with this. */
+            unexpected_final_frame(s, msg, len);
+            break;
+        }
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static void queue_phase(t30_state_t *s, int phase)
+{
+    if (s->rx_signal_present)
+    {
+        /* We need to wait for that signal to go away */
+        s->next_phase = phase;
+    }
+    else
+    {
+        set_phase(s, phase);
+        s->next_phase = T30_PHASE_IDLE;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static void set_phase(t30_state_t *s, int phase)
+{
+    if (phase != s->phase)
+    {
+        span_log(&s->logging, SPAN_LOG_FLOW, "Changing from phase %s to %s\n", phase_names[s->phase], phase_names[phase]);
+        /* We may be killing a receiver before it has declared the end of the
+           signal. Force the signal present indicator to off, because the
+           receiver will never be able to. */
+        if (s->phase != T30_PHASE_A_CED  &&  s->phase != T30_PHASE_A_CNG)
+            s->rx_signal_present = FALSE;
+        s->rx_trained = FALSE;
+        s->rx_frame_received = FALSE;
+        s->phase = phase;
+        switch (phase)
+        {
+        case T30_PHASE_A_CED:
+            if (s->set_rx_type_handler)
+                s->set_rx_type_handler(s->set_rx_type_user_data, T30_MODEM_V21, 300, FALSE, TRUE);
+            if (s->set_tx_type_handler)
+                s->set_tx_type_handler(s->set_tx_type_user_data, T30_MODEM_CED, 0, FALSE, FALSE);
+            break;
+        case T30_PHASE_A_CNG:
+            if (s->set_rx_type_handler)
+                s->set_rx_type_handler(s->set_rx_type_user_data, T30_MODEM_V21, 300, FALSE, TRUE);
+            if (s->set_tx_type_handler)
+                s->set_tx_type_handler(s->set_tx_type_user_data, T30_MODEM_CNG, 0, FALSE, FALSE);
+            break;
+        case T30_PHASE_B_RX:
+        case T30_PHASE_D_RX:
+            if (s->set_rx_type_handler)
+                s->set_rx_type_handler(s->set_rx_type_user_data, T30_MODEM_V21, 300, FALSE, TRUE);
+            if (s->set_tx_type_handler)
+                s->set_tx_type_handler(s->set_tx_type_user_data, T30_MODEM_NONE, 0, FALSE, FALSE);
+            break;
+        case T30_PHASE_B_TX:
+        case T30_PHASE_D_TX:
+            if (!s->far_end_detected  &&  s->timer_t0_t1 > 0)
+            {
+                s->timer_t0_t1 = ms_to_samples(DEFAULT_TIMER_T1);
+                s->far_end_detected = TRUE;
+            }
+            if (s->set_rx_type_handler)
+                s->set_rx_type_handler(s->set_rx_type_user_data, T30_MODEM_NONE, 0, FALSE, FALSE);
+            if (s->set_tx_type_handler)
+                s->set_tx_type_handler(s->set_tx_type_user_data, T30_MODEM_V21, 300, FALSE, TRUE);
+            break;
+        case T30_PHASE_C_NON_ECM_RX:
+            if (s->set_rx_type_handler)
+                s->set_rx_type_handler(s->set_rx_type_user_data, fallback_sequence[s->current_fallback].modem_type, fallback_sequence[s->current_fallback].bit_rate, s->short_train, FALSE);
+            if (s->set_tx_type_handler)
+                s->set_tx_type_handler(s->set_tx_type_user_data, T30_MODEM_NONE, 0, FALSE, FALSE);
+            break;
+        case T30_PHASE_C_NON_ECM_TX:
+            /* Pause before switching from anything to phase C */
+            /* Always prime the training count for 1.5s of data at the current rate. Its harmless if
+               we prime it and are not doing TCF. */
+            s->tcf_test_bits = (3*fallback_sequence[s->current_fallback].bit_rate)/2;
+            if (s->set_rx_type_handler)
+                s->set_rx_type_handler(s->set_rx_type_user_data, T30_MODEM_NONE, 0, FALSE, FALSE);
+            if (s->set_tx_type_handler)
+                s->set_tx_type_handler(s->set_tx_type_user_data, fallback_sequence[s->current_fallback].modem_type, fallback_sequence[s->current_fallback].bit_rate, s->short_train, FALSE);
+            break;
+        case T30_PHASE_C_ECM_RX:
+            if (s->set_rx_type_handler)
+                s->set_rx_type_handler(s->set_rx_type_user_data, fallback_sequence[s->current_fallback].modem_type, fallback_sequence[s->current_fallback].bit_rate, s->short_train, TRUE);
+            if (s->set_tx_type_handler)
+                s->set_tx_type_handler(s->set_tx_type_user_data, T30_MODEM_NONE, 0, FALSE, FALSE);
+            break;
+        case T30_PHASE_C_ECM_TX:
+            /* Pause before switching from anything to phase C */
+            if (s->set_rx_type_handler)
+                s->set_rx_type_handler(s->set_rx_type_user_data, T30_MODEM_NONE, 0, FALSE, FALSE);
+            if (s->set_tx_type_handler)
+                s->set_tx_type_handler(s->set_tx_type_user_data, fallback_sequence[s->current_fallback].modem_type, fallback_sequence[s->current_fallback].bit_rate, s->short_train, TRUE);
+            break;
+        case T30_PHASE_E:
+            /* Send a little silence before ending things, to ensure the
+               buffers are all flushed through, and the far end has seen
+               the last message we sent. */
+            s->tcf_test_bits = 0;
+            s->tcf_current_zeros = 0;
+            s->tcf_most_zeros = 0;
+            if (s->set_rx_type_handler)
+                s->set_rx_type_handler(s->set_rx_type_user_data, T30_MODEM_NONE, 0, FALSE, FALSE);
+            if (s->set_tx_type_handler)
+                s->set_tx_type_handler(s->set_tx_type_user_data, T30_MODEM_PAUSE, 0, FINAL_FLUSH_TIME, FALSE);
+            break;
+        case T30_PHASE_CALL_FINISHED:
+            if (s->set_rx_type_handler)
+                s->set_rx_type_handler(s->set_rx_type_user_data, T30_MODEM_DONE, 0, FALSE, FALSE);
+            if (s->set_tx_type_handler)
+                s->set_tx_type_handler(s->set_tx_type_user_data, T30_MODEM_DONE, 0, FALSE, FALSE);
+            break;
+        }
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static void set_state(t30_state_t *s, int state)
+{
+    if (s->state != state)
+    {
+        span_log(&s->logging, SPAN_LOG_FLOW, "Changing from state %d to %d\n", s->state, state);
+        s->state = state;
+    }
+    s->step = 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void repeat_last_command(t30_state_t *s)
+{
+    s->step = 0;
+    switch (s->state)
+    {
+    case T30_STATE_R:
+        s->dis_received = FALSE;
+        queue_phase(s, T30_PHASE_B_TX);
+        send_dis_or_dtc_sequence(s, TRUE);
+        break;
+    case T30_STATE_III_Q_MCF:
+        queue_phase(s, T30_PHASE_D_TX);
+        send_simple_frame(s, T30_MCF);
+        break;
+    case T30_STATE_III_Q_RTP:
+        queue_phase(s, T30_PHASE_D_TX);
+        send_simple_frame(s, T30_RTP);
+        break;
+    case T30_STATE_III_Q_RTN:
+        queue_phase(s, T30_PHASE_D_TX);
+        send_simple_frame(s, T30_RTN);
+        break;
+    case T30_STATE_II_Q:
+        queue_phase(s, T30_PHASE_D_TX);
+        send_simple_frame(s, s->next_tx_step);
+        break;
+    case T30_STATE_IV_PPS_NULL:
+    case T30_STATE_IV_PPS_Q:
+        queue_phase(s, T30_PHASE_D_TX);
+        send_pps_frame(s);
+        break;
+    case T30_STATE_IV_PPS_RNR:
+    case T30_STATE_IV_EOR_RNR:
+        queue_phase(s, T30_PHASE_D_TX);
+        send_simple_frame(s, T30_RNR);
+        break;
+    case T30_STATE_D:
+        queue_phase(s, T30_PHASE_B_TX);
+        send_dcs_sequence(s, TRUE);
+        break;
+    case T30_STATE_F_FTT:
+        queue_phase(s, T30_PHASE_B_TX);
+        send_simple_frame(s, T30_FTT);
+        break;
+    case T30_STATE_F_CFR:
+        queue_phase(s, T30_PHASE_B_TX);
+        send_cfr_sequence(s, TRUE);
+        break;
+    case T30_STATE_D_POST_TCF:
+        /* Need to send the whole training thing again */
+        s->short_train = FALSE;
+        queue_phase(s, T30_PHASE_B_TX);
+        send_dcs_sequence(s, TRUE);
+        break;
+    case T30_STATE_F_POST_RCP_RNR:
+        /* Just ignore */
+        break;
+    default:
+        span_log(&s->logging,
+                 SPAN_LOG_FLOW,
+                 "Repeat command called with nothing to repeat - phase %s, state %d\n",
+                 phase_names[s->phase],
+                 s->state);
+        break;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static void timer_t2_start(t30_state_t *s)
+{
+    span_log(&s->logging, SPAN_LOG_FLOW, "Start T2\n");
+    s->timer_t2_t4 = ms_to_samples(DEFAULT_TIMER_T2);
+    s->timer_t2_t4_is = TIMER_IS_T2;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void timer_t2a_start(t30_state_t *s)
+{
+    span_log(&s->logging, SPAN_LOG_FLOW, "Start T2A\n");
+    s->timer_t2_t4 = ms_to_samples(DEFAULT_TIMER_T2A);
+    s->timer_t2_t4_is = TIMER_IS_T2A;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void timer_t2b_start(t30_state_t *s)
+{
+    span_log(&s->logging, SPAN_LOG_FLOW, "Start T2B\n");
+    s->timer_t2_t4 = ms_to_samples(DEFAULT_TIMER_T2B);
+    s->timer_t2_t4_is = TIMER_IS_T2B;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void timer_t4_start(t30_state_t *s)
+{
+    span_log(&s->logging, SPAN_LOG_FLOW, "Start T4\n");
+    s->timer_t2_t4 = ms_to_samples(DEFAULT_TIMER_T4);
+    s->timer_t2_t4_is = TIMER_IS_T4;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void timer_t4a_start(t30_state_t *s)
+{
+    span_log(&s->logging, SPAN_LOG_FLOW, "Start T4A\n");
+    s->timer_t2_t4 = ms_to_samples(DEFAULT_TIMER_T4A);
+    s->timer_t2_t4_is = TIMER_IS_T4A;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void timer_t4b_start(t30_state_t *s)
+{
+    span_log(&s->logging, SPAN_LOG_FLOW, "Start T4B\n");
+    s->timer_t2_t4 = ms_to_samples(DEFAULT_TIMER_T4B);
+    s->timer_t2_t4_is = TIMER_IS_T4B;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void timer_t0_expired(t30_state_t *s)
+{
+    span_log(&s->logging, SPAN_LOG_FLOW, "T0 expired in state %d\n", s->state);
+    s->current_status = T30_ERR_T0_EXPIRED;
+    /* Just end the call */
+    disconnect(s);
+}
+/*- End of function --------------------------------------------------------*/
+
+static void timer_t1_expired(t30_state_t *s)
+{
+    span_log(&s->logging, SPAN_LOG_FLOW, "T1 expired in state %d\n", s->state);
+    /* The initial connection establishment has timeout out. In other words, we
+       have been unable to communicate successfully with a remote machine.
+       It is time to abandon the call. */
+    s->current_status = T30_ERR_T1_EXPIRED;
+    switch (s->state)
+    {
+    case T30_STATE_T:
+        /* Just end the call */
+        disconnect(s);
+        break;
+    case T30_STATE_R:
+        /* Send disconnect, and then end the call. Since we have not
+           successfully contacted the far end, it is unclear why we should
+           send a disconnect message at this point. However, it is what T.30
+           says we should do. */
+        send_dcn(s);
+        break;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static void timer_t2_expired(t30_state_t *s)
+{
+    span_log(&s->logging, SPAN_LOG_FLOW, "T2 expired in phase %s, state %d\n", phase_names[s->phase], s->state);
+    switch (s->state)
+    {
+    case T30_STATE_III_Q_MCF:
+    case T30_STATE_III_Q_RTP:
+    case T30_STATE_III_Q_RTN:
+    case T30_STATE_F_POST_RCP_PPR:
+    case T30_STATE_F_POST_RCP_MCF:
+        switch (s->next_rx_step)
+        {
+        case T30_EOM:
+        case T30_PRI_EOM:
+        case T30_EOS:
+            /* We didn't receive a response to our T30_MCF after T30_EOM, so we must be OK
+               to proceed to phase B, and pretty act like its the beginning of a call. */
+            span_log(&s->logging, SPAN_LOG_FLOW, "Returning to phase B after %s\n", t30_frametype(s->next_rx_step));
+            set_phase(s, T30_PHASE_B_TX);
+            timer_t2_start(s);
+            s->dis_received = FALSE;
+            send_dis_or_dtc_sequence(s, TRUE);
+            return;
+        }
+        break;
+    case T30_STATE_F_TCF:
+        span_log(&s->logging, SPAN_LOG_FLOW, "No TCF data received\n");
+        set_phase(s, T30_PHASE_B_TX);
+        set_state(s, T30_STATE_F_FTT);
+        send_simple_frame(s, T30_FTT);
+        return;
+    case T30_STATE_F_DOC_ECM:
+    case T30_STATE_F_DOC_NON_ECM:
+        /* While waiting for FAX page */
+        s->current_status = T30_ERR_RX_T2EXPFAX;
+        break;
+    case T30_STATE_F_POST_DOC_ECM:
+    case T30_STATE_F_POST_DOC_NON_ECM:
+        /* While waiting for next FAX page */
+        s->current_status = T30_ERR_RX_T2EXPMPS;
+        break;
+#if 0
+    case ??????:
+        /* While waiting for DCN */
+        s->current_status = T30_ERR_RX_T2EXPDCN;
+        break;
+    case ??????:
+        /* While waiting for phase D */
+        s->current_status = T30_ERR_RX_T2EXPD;
+        break;
+#endif
+    case T30_STATE_IV_PPS_RNR:
+    case T30_STATE_IV_EOR_RNR:
+        /* While waiting for RR command */
+        s->current_status = T30_ERR_RX_T2EXPRR;
+        break;
+    case T30_STATE_R:
+        /* While waiting for NSS, DCS or MCF */
+        s->current_status = T30_ERR_RX_T2EXP;
+        break;
+    case T30_STATE_F_FTT:
+        break;
+    }
+    queue_phase(s, T30_PHASE_B_TX);
+    start_receiving_document(s);
+}
+/*- End of function --------------------------------------------------------*/
+
+static void timer_t2a_expired(t30_state_t *s)
+{
+    span_log(&s->logging, SPAN_LOG_FLOW, "T2A expired in phase %s, state %d. An HDLC frame lasted too long.\n", phase_names[s->phase], s->state);
+    s->current_status = T30_ERR_HDLC_CARRIER;
+    disconnect(s);
+}
+/*- End of function --------------------------------------------------------*/
+
+static void timer_t2b_expired(t30_state_t *s)
+{
+    span_log(&s->logging, SPAN_LOG_FLOW, "T2B expired in phase %s, state %d. The line is now quiet.\n", phase_names[s->phase], s->state);
+    timer_t2_expired(s);
+}
+/*- End of function --------------------------------------------------------*/
+
+static void timer_t3_expired(t30_state_t *s)
+{
+    span_log(&s->logging, SPAN_LOG_FLOW, "T3 expired in phase %s, state %d\n", phase_names[s->phase], s->state);
+    s->current_status = T30_ERR_T3_EXPIRED;
+    disconnect(s);
+}
+/*- End of function --------------------------------------------------------*/
+
+static void timer_t4_expired(t30_state_t *s)
+{
+    /* There was no response (or only a corrupt response) to a command,
+       within the T4 timeout period. */
+    span_log(&s->logging, SPAN_LOG_FLOW, "T4 expired in phase %s, state %d\n", phase_names[s->phase], s->state);
+    if (++s->retries > MAX_MESSAGE_TRIES)
+    {
+        switch (s->state)
+        {
+        case T30_STATE_D_POST_TCF:
+            /* Received no response to DCS or TCF */
+            s->current_status = T30_ERR_TX_PHBDEAD;
+            break;
+        case T30_STATE_II_Q:
+        case T30_STATE_IV_PPS_NULL:
+        case T30_STATE_IV_PPS_Q:
+            /* No response after sending a page */
+            s->current_status = T30_ERR_TX_PHDDEAD;
+            break;
+        default:
+            /* Disconnected after permitted retries */
+            s->current_status = T30_ERR_RETRYDCN;
+            break;
+        }
+        send_dcn(s);
+        return;
+    }
+    /* Of course, things might just be a little late, especially if there are T.38
+       links in the path. There is no point in simply timing out, and resending,
+       if we are currently receiving something from the far end - its a half-duplex
+       path, so the two transmissions will conflict. Our best strategy is to wait
+       until there is nothing being received, or give up after a long backstop timeout.
+       In the meantime, if we get a meaningful, if somewhat delayed, response, we
+       should accept it and carry on. */
+    repeat_last_command(s);
+}
+/*- End of function --------------------------------------------------------*/
+
+static void timer_t4a_expired(t30_state_t *s)
+{
+    span_log(&s->logging, SPAN_LOG_FLOW, "T4A expired in phase %s, state %d. An HDLC frame lasted too long.\n", phase_names[s->phase], s->state);
+    s->current_status = T30_ERR_HDLC_CARRIER;
+    disconnect(s);
+}
+/*- End of function --------------------------------------------------------*/
+
+static void timer_t4b_expired(t30_state_t *s)
+{
+    span_log(&s->logging, SPAN_LOG_FLOW, "T4B expired in phase %s, state %d. The line is now quiet.\n", phase_names[s->phase], s->state);
+    timer_t4_expired(s);
+}
+/*- End of function --------------------------------------------------------*/
+
+static void timer_t5_expired(t30_state_t *s)
+{
+    /* Give up waiting for the receiver to become ready in error correction mode */
+    span_log(&s->logging, SPAN_LOG_FLOW, "T5 expired in phase %s, state %d\n", phase_names[s->phase], s->state);
+    s->current_status = T30_ERR_TX_T5EXP;
+    send_dcn(s);
+}
+/*- End of function --------------------------------------------------------*/
+
+static void decode_20digit_msg(t30_state_t *s, char *msg, const uint8_t *pkt, int len)
+{
+    int p;
+    int k;
+    char text[T30_MAX_IDENT_LEN + 1];
+
+    if (msg == NULL)
+        msg = text;
+    if (len > T30_MAX_IDENT_LEN + 1)
+    {
+        unexpected_frame_length(s, pkt, len);
+        msg[0] = '\0';
+        return;
+    }
+    p = len;
+    /* Strip trailing spaces */
+    while (p > 1  &&  pkt[p - 1] == ' ')
+        p--;
+    /* The string is actually backwards in the message */
+    k = 0;
+    while (p > 1)
+        msg[k++] = pkt[--p];
+    msg[k] = '\0';
+    span_log(&s->logging, SPAN_LOG_FLOW, "Remote gave %s as: \"%s\"\n", t30_frametype(pkt[0]), msg);
+}
+/*- End of function --------------------------------------------------------*/
+
+static void decode_url_msg(t30_state_t *s, char *msg, const uint8_t *pkt, int len)
+{
+    char text[77 + 1];
+
+    /* TODO: decode properly, as per T.30 5.3.6.2.12 */
+    if (msg == NULL)
+        msg = text;
+    if (len < 3  ||  len > 77 + 3  ||  len != pkt[2] + 3)
+    {
+        unexpected_frame_length(s, pkt, len);
+        msg[0] = '\0';
+        return;
+    }
+    /* First octet is the sequence number of the packet.
+            Bit 7 = 1 for more follows, 0 for last packet in the sequence.
+            Bits 6-0 = The sequence number, 0 to 0x7F
+       Second octet is the type of internet address.
+            Bits 7-4 = reserved
+            Bits 3-0 = type:
+                    0 = reserved
+                    1 = e-mail address
+                    2 = URL
+                    3 = TCP/IP V4
+                    4 = TCP/IP V6
+                    5 = international phone number, in the usual +... format
+                    6-15 = reserved
+       Third octet is the length of the internet address
+            Bit 7 = 1 for more follows, 0 for last packet in the sequence.
+            Bits 6-0 = length
+     */
+    memcpy(msg, &pkt[3], len - 3);
+    msg[len - 3] = '\0';
+    span_log(&s->logging, SPAN_LOG_FLOW, "Remote fax gave %s as: %d, %d, \"%s\"\n", t30_frametype(pkt[0]), pkt[0], pkt[1], msg);
+}
+/*- End of function --------------------------------------------------------*/
+
+static void t30_non_ecm_rx_status(void *user_data, int status)
+{
+    t30_state_t *s;
+    int was_trained;
+
+    s = (t30_state_t *) user_data;
+    switch (status)
+    {
+    case PUTBIT_TRAINING_IN_PROGRESS:
+        break;
+    case PUTBIT_TRAINING_FAILED:
+        span_log(&s->logging, SPAN_LOG_FLOW, "Non-ECM carrier training failed in state %d\n", s->state);
+        s->rx_trained = FALSE;
+        break;
+    case PUTBIT_TRAINING_SUCCEEDED:
+        /* The modem is now trained */
+        span_log(&s->logging, SPAN_LOG_FLOW, "Non-ECM carrier trained in state %d\n", s->state);
+        /* In case we are in trainability test mode... */
+        s->tcf_test_bits = 0;
+        s->tcf_current_zeros = 0;
+        s->tcf_most_zeros = 0;
+        s->rx_signal_present = TRUE;
+        s->rx_trained = TRUE;
+        s->timer_t2_t4 = 0;
+        break;
+    case PUTBIT_CARRIER_UP:
+        span_log(&s->logging, SPAN_LOG_FLOW, "Non-ECM carrier up in state %d\n", s->state);
+        break;
+    case PUTBIT_CARRIER_DOWN:
+        span_log(&s->logging, SPAN_LOG_FLOW, "Non-ECM carrier down in state %d\n", s->state);
+        was_trained = s->rx_trained;
+        s->rx_signal_present = FALSE;
+        s->rx_trained = FALSE;
+        switch (s->state)
+        {
+        case T30_STATE_F_TCF:
+            /* Only respond if we managed to actually sync up with the source. We don't
+               want to respond just because we saw a click. These often occur just
+               before the real signal, with many modems. Presumably this is due to switching
+               within the far end modem. We also want to avoid the possibility of responding
+               to the tail end of any slow modem signal. If there was a genuine data signal
+               which we failed to train on it should not matter. If things are that bad, we
+               do not stand much chance of good quality communications. */
+            if (was_trained)
+            {
+                /* Although T.30 says the training test should be 1.5s of all 0's, some FAX
+                   machines send a burst of all 1's before the all 0's. Tolerate this. */
+                if (s->tcf_current_zeros > s->tcf_most_zeros)
+                    s->tcf_most_zeros = s->tcf_current_zeros;
+                span_log(&s->logging, SPAN_LOG_FLOW, "Trainability (TCF) test result - %d total bits. longest run of zeros was %d\n", s->tcf_test_bits, s->tcf_most_zeros);
+                if (s->tcf_most_zeros < fallback_sequence[s->current_fallback].bit_rate)
+                {
+                    span_log(&s->logging, SPAN_LOG_FLOW, "Trainability (TCF) test failed - longest run of zeros was %d\n", s->tcf_most_zeros);
+                    set_phase(s, T30_PHASE_B_TX);
+                    set_state(s, T30_STATE_F_FTT);
+                    send_simple_frame(s, T30_FTT);
+                }
+                else
+                {
+                    /* The training went OK */
+                    s->short_train = TRUE;
+                    s->in_message = TRUE;
+                    rx_start_page(s);
+                    set_phase(s, T30_PHASE_B_TX);
+                    set_state(s, T30_STATE_F_CFR);
+                    send_cfr_sequence(s, TRUE);
+                }
+            }
+            break;
+        case T30_STATE_F_POST_DOC_NON_ECM:
+            /* Page ended cleanly */
+            if (s->current_status == T30_ERR_RX_NOCARRIER)
+                s->current_status = T30_ERR_OK;
+            break;
+        default:
+            /* We should be receiving a document right now, but it did not end cleanly. */
+            if (was_trained)
+            {
+                span_log(&s->logging, SPAN_LOG_WARNING, "Page did not end cleanly\n");
+                /* We trained OK, so we should have some kind of received page, even though
+                   it did not end cleanly. */
+                set_state(s, T30_STATE_F_POST_DOC_NON_ECM);
+                set_phase(s, T30_PHASE_D_RX);
+                timer_t2_start(s);
+                if (s->current_status == T30_ERR_RX_NOCARRIER)
+                    s->current_status = T30_ERR_OK;
+            }
+            else
+            {
+                span_log(&s->logging, SPAN_LOG_WARNING, "Non-ECM carrier not found\n");
+                s->current_status = T30_ERR_RX_NOCARRIER;
+            }
+            break;
+        }
+        if (s->next_phase != T30_PHASE_IDLE)
+        {
+            set_phase(s, s->next_phase);
+            s->next_phase = T30_PHASE_IDLE;
+        }
+        break;
+    default:
+        span_log(&s->logging, SPAN_LOG_WARNING, "Unexpected non-ECM rx status - %d!\n", status);
+        break;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+void t30_non_ecm_put_bit(void *user_data, int bit)
+{
+    t30_state_t *s;
+
+    if (bit < 0)
+    {
+        t30_non_ecm_rx_status(user_data, bit);
+        return;
+    }
+    s = (t30_state_t *) user_data;
+    switch (s->state)
+    {
+    case T30_STATE_F_TCF:
+        /* Trainability test */
+        s->tcf_test_bits++;
+        if (bit)
+        {
+            if (s->tcf_current_zeros > s->tcf_most_zeros)
+                s->tcf_most_zeros = s->tcf_current_zeros;
+            s->tcf_current_zeros = 0;
+        }
+        else
+        {
+            s->tcf_current_zeros++;
+        }
+        break;
+    case T30_STATE_F_DOC_NON_ECM:
+        /* Document transfer */
+        if (t4_rx_put_bit(&s->t4, bit))
+        {
+            /* That is the end of the document */
+            set_state(s, T30_STATE_F_POST_DOC_NON_ECM);
+            queue_phase(s, T30_PHASE_D_RX);
+            timer_t2_start(s);
+        }
+        break;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+void t30_non_ecm_put_byte(void *user_data, int byte)
+{
+    t30_state_t *s;
+
+    s = (t30_state_t *) user_data;
+    switch (s->state)
+    {
+    case T30_STATE_F_TCF:
+        /* Trainability test */
+        /* This makes counting zeros fast, but approximate. That really doesn't matter */
+        s->tcf_test_bits += 8;
+        if (byte)
+        {
+            if (s->tcf_current_zeros > s->tcf_most_zeros)
+                s->tcf_most_zeros = s->tcf_current_zeros;
+            s->tcf_current_zeros = 0;
+        }
+        else
+        {
+            s->tcf_current_zeros += 8;
+        }
+        break;
+    case T30_STATE_F_DOC_NON_ECM:
+        /* Document transfer */
+        if (t4_rx_put_byte(&s->t4, byte))
+        {
+            /* That is the end of the document */
+            set_state(s, T30_STATE_F_POST_DOC_NON_ECM);
+            queue_phase(s, T30_PHASE_D_RX);
+            timer_t2_start(s);
+        }
+        break;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+void t30_non_ecm_put_chunk(void *user_data, const uint8_t buf[], int len)
+{
+    t30_state_t *s;
+    int i;
+
+    s = (t30_state_t *) user_data;
+    switch (s->state)
+    {
+    case T30_STATE_F_TCF:
+        /* Trainability test */
+        /* This makes counting zeros fast, but approximate. That really doesn't matter */
+        s->tcf_test_bits += 8*len;
+        for (i = 0;  i < len;  i++)
+        {
+            if (buf[i])
+            {
+                if (s->tcf_current_zeros > s->tcf_most_zeros)
+                    s->tcf_most_zeros = s->tcf_current_zeros;
+                s->tcf_current_zeros = 0;
+            }
+            else
+            {
+                s->tcf_current_zeros += 8;
+            }
+        }
+        break;
+    case T30_STATE_F_DOC_NON_ECM:
+        /* Document transfer */
+        if (t4_rx_put_chunk(&s->t4, buf, len))
+        {
+            /* That is the end of the document */
+            set_state(s, T30_STATE_F_POST_DOC_NON_ECM);
+            queue_phase(s, T30_PHASE_D_RX);
+            timer_t2_start(s);
+        }
+        break;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+int t30_non_ecm_get_bit(void *user_data)
+{
+    int bit;
+    t30_state_t *s;
+
+    s = (t30_state_t *) user_data;
+    switch (s->state)
+    {
+    case T30_STATE_D_TCF:
+        /* Trainability test. */
+        bit = 0;
+        if (s->tcf_test_bits-- < 0)
+        {
+            /* Finished sending training test. */
+            bit = PUTBIT_END_OF_DATA;
+        }
+        break;
+    case T30_STATE_I:
+        /* Transferring real data. */
+        bit = t4_tx_get_bit(&(s->t4));
+        break;
+    case T30_STATE_D_POST_TCF:
+    case T30_STATE_II_Q:
+        /* We should be padding out a block of samples if we are here */
+        bit = 0;
+        break;
+    default:
+        span_log(&s->logging, SPAN_LOG_WARNING, "t30_non_ecm_get_bit in bad state %d\n", s->state);
+        bit = PUTBIT_END_OF_DATA;
+        break;
+    }
+    return bit;
+}
+/*- End of function --------------------------------------------------------*/
+
+int t30_non_ecm_get_byte(void *user_data)
+{
+    int byte;
+    t30_state_t *s;
+
+    s = (t30_state_t *) user_data;
+    switch (s->state)
+    {
+    case T30_STATE_D_TCF:
+        /* Trainability test. */
+        byte = 0;
+        if ((s->tcf_test_bits -= 8) < 0)
+        {
+            /* Finished sending training test. */
+            byte = 0x100;
+        }
+        break;
+    case T30_STATE_I:
+        /* Transferring real data. */
+        byte = t4_tx_get_byte(&(s->t4));
+        break;
+    case T30_STATE_D_POST_TCF:
+    case T30_STATE_II_Q:
+        /* We should be padding out a block of samples if we are here */
+        byte = 0;
+        break;
+    default:
+        span_log(&s->logging, SPAN_LOG_WARNING, "t30_non_ecm_get_byte in bad state %d\n", s->state);
+        byte = 0x100;
+        break;
+    }
+    return byte;
+}
+/*- End of function --------------------------------------------------------*/
+
+int t30_non_ecm_get_chunk(void *user_data, uint8_t buf[], int max_len)
+{
+    int len;
+    t30_state_t *s;
+
+    s = (t30_state_t *) user_data;
+    switch (s->state)
+    {
+    case T30_STATE_D_TCF:
+        /* Trainability test. */
+        for (len = 0;  len < max_len;  len++)
+        {
+            buf[len] = 0;
+            if ((s->tcf_test_bits -= 8) < 0)
+                break;
+        }
+        break;
+    case T30_STATE_I:
+        /* Transferring real data. */
+        len = t4_tx_get_chunk(&s->t4, buf, max_len);
+        break;
+    case T30_STATE_D_POST_TCF:
+    case T30_STATE_II_Q:
+        /* We should be padding out a block of samples if we are here */
+        len = 0;
+        break;
+    default:
+        span_log(&s->logging, SPAN_LOG_WARNING, "t30_non_ecm_get_chunk in bad state %d\n", s->state);
+        len = 0;
+        break;
+    }
+    return len;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void t30_hdlc_rx_status(void *user_data, int status)
+{
+    t30_state_t *s;
+
+    s = (t30_state_t *) user_data;
+    switch (status)
+    {
+    case PUTBIT_TRAINING_IN_PROGRESS:
+        break;
+    case PUTBIT_TRAINING_FAILED:
+        span_log(&s->logging, SPAN_LOG_FLOW, "HDLC carrier training failed in state %d\n", s->state);
+        s->rx_trained = FALSE;
+        break;
+    case PUTBIT_TRAINING_SUCCEEDED:
+        /* The modem is now trained */
+        span_log(&s->logging, SPAN_LOG_FLOW, "HDLC carrier trained in state %d\n", s->state);
+        s->rx_signal_present = TRUE;
+        s->rx_trained = TRUE;
+        break;
+    case PUTBIT_CARRIER_UP:
+        span_log(&s->logging, SPAN_LOG_FLOW, "HDLC carrier up in state %d\n", s->state);
+        s->rx_signal_present = TRUE;
+        switch (s->timer_t2_t4_is)
+        {
+        case TIMER_IS_T2B:
+            s->timer_t2_t4_is = TIMER_IS_T2C;
+            s->timer_t2_t4 = 0;
+            break;
+        case TIMER_IS_T4B:
+            s->timer_t2_t4_is = TIMER_IS_T4C;
+            s->timer_t2_t4 = 0;
+            break;
+        }
+        break;
+    case PUTBIT_CARRIER_DOWN:
+        span_log(&s->logging, SPAN_LOG_FLOW, "HDLC carrier down in state %d\n", s->state);
+        s->rx_signal_present = FALSE;
+        s->rx_trained = FALSE;
+        /* If a phase change has been queued to occur after the receive signal drops,
+           its time to change. */
+        if (s->next_phase != T30_PHASE_IDLE)
+        {
+            s->timer_t2_t4 = 0;
+            set_phase(s, s->next_phase);
+            if (s->next_phase == T30_PHASE_C_NON_ECM_RX)
+                timer_t2_start(s);
+            s->next_phase = T30_PHASE_IDLE;
+        }
+        else
+        {
+            switch (s->timer_t2_t4_is)
+            {
+            case TIMER_IS_T2A:
+            case TIMER_IS_T2C:
+                timer_t2b_start(s);
+                break;
+            case TIMER_IS_T4A:
+            case TIMER_IS_T4C:
+                timer_t4b_start(s);
+                break;
+            }
+        }
+        break;
+    case PUTBIT_FRAMING_OK:
+        span_log(&s->logging, SPAN_LOG_FLOW, "HDLC framing OK in state %d\n", s->state);
+        if (!s->far_end_detected  &&  s->timer_t0_t1 > 0)
+        {
+            s->timer_t0_t1 = ms_to_samples(DEFAULT_TIMER_T1);
+            s->far_end_detected = TRUE;
+            if (s->phase == T30_PHASE_A_CED  ||  s->phase == T30_PHASE_A_CNG)
+                set_phase(s, T30_PHASE_B_RX);
+        }
+        /* 5.4.3.1 Timer T2 is reset if flag is received. Timer T2A must be started. */
+        /* Unstated, but implied, is that timer T4 and T4A are handled the same way. */
+        if (s->timer_t2_t4 > 0)
+        {
+            switch(s->timer_t2_t4_is)
+            {
+            case TIMER_IS_T2:
+            case TIMER_IS_T2A:
+                timer_t2a_start(s);
+                break;
+            case TIMER_IS_T4:
+            case TIMER_IS_T4A:
+                timer_t4a_start(s);
+                break;
+            }
+        }
+        break;
+    case PUTBIT_ABORT:
+        /* Just ignore these */
+        break;
+    default:
+        span_log(&s->logging, SPAN_LOG_FLOW, "Unexpected HDLC special length - %d!\n", status);
+        break;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+void t30_hdlc_accept(void *user_data, const uint8_t *msg, int len, int ok)
+{
+    t30_state_t *s;
+
+    if (len < 0)
+    {
+        t30_hdlc_rx_status(user_data, len);
+        return;
+    }
+
+    s = (t30_state_t *) user_data;
+    /* The spec. says a command or response is not valid if:
+        - any of the frames, optional or mandatory, have an FCS error.
+        - any single frame exceeds 3s +- 15% (i.e. no frame should exceed 2.55s)
+        - the final frame is not tagged as a final frame
+        - the final frame is not a recognised one.
+       The first point seems benign. If we accept an optional frame, and a later
+       frame is bad, having accepted the optional frame should be harmless.
+       The 2.55s maximum seems to limit signalling frames to no more than 95 octets,
+       including FCS, and flag octets (assuming the use of V.21).
+    */
+    if (!ok  &&  s->phase != T30_PHASE_C_ECM_RX)
+    {
+        span_log(&s->logging, SPAN_LOG_FLOW, "Bad CRC received\n");
+        /* We either force a resend, or we wait until a resend occurs through a timeout. */
+        if ((s->supported_t30_features & T30_SUPPORT_COMMAND_REPEAT))
+        {
+            s->step = 0;
+            if (s->phase == T30_PHASE_B_RX)
+                queue_phase(s, T30_PHASE_B_TX);
+            else
+                queue_phase(s, T30_PHASE_D_TX);
+            send_simple_frame(s, T30_CRP);
+        }
+        return;
+    }
+
+    if (len < 3)
+    {
+        span_log(&s->logging, SPAN_LOG_FLOW, "Bad HDLC frame length - %d\n", len);
+        return;
+    }
+    if (msg[0] != 0xFF  ||  !(msg[1] == 0x03  ||  msg[1] == 0x13))
+    {
+        span_log(&s->logging, SPAN_LOG_FLOW, "Bad HDLC frame header - %02x %02x\n", msg[0], msg[1]);
+        return;
+    }
+    s->rx_frame_received = TRUE;
+    /* Cancel the command or response timer */
+    s->timer_t2_t4 = 0;
+    process_rx_control_msg(s, msg, len);
+}
+/*- End of function --------------------------------------------------------*/
+
+void t30_front_end_status(void *user_data, int status)
+{
+    t30_state_t *s;
+    
+    s = (t30_state_t *) user_data;
+
+    switch (status)
+    {
+    case T30_FRONT_END_SEND_STEP_COMPLETE:
+        span_log(&s->logging, SPAN_LOG_FLOW, "Send complete in phase %s, state %d\n", phase_names[s->phase], s->state);
+        /* We have finished sending our messages, so move on to the next operation. */
+        switch (s->state)
+        {
+        case T30_STATE_ANSWERING:
+            span_log(&s->logging, SPAN_LOG_FLOW, "Starting answer mode\n");
+            set_phase(s, T30_PHASE_B_TX);
+            timer_t2_start(s);
+            s->dis_received = FALSE;
+            send_dis_or_dtc_sequence(s, TRUE);
+            break;
+        case T30_STATE_R:
+            if (send_dis_or_dtc_sequence(s, FALSE))
+            {
+                /* Wait for an acknowledgement. */
+                set_phase(s, T30_PHASE_B_RX);
+                timer_t4_start(s);
+            }
+            break;
+        case T30_STATE_F_CFR:
+            if (s->step == 0)
+            {
+                /* Shut down HDLC transmission. */
+                if (s->send_hdlc_handler)
+                    s->send_hdlc_handler(s->send_hdlc_user_data, NULL, 0);
+                s->step++;
+            }
+            else
+            {
+                if (s->error_correcting_mode)
+                {
+                    set_state(s, T30_STATE_F_DOC_ECM);
+                    queue_phase(s, T30_PHASE_C_ECM_RX);
+                }
+                else
+                {
+                    set_state(s, T30_STATE_F_DOC_NON_ECM);
+                    queue_phase(s, T30_PHASE_C_NON_ECM_RX);
+                }
+                timer_t2_start(s);
+                s->next_rx_step = T30_MPS;
+            }
+            break;
+        case T30_STATE_F_FTT:
+            if (s->step == 0)
+            {
+                /* Shut down HDLC transmission. */
+                if (s->send_hdlc_handler)
+                    s->send_hdlc_handler(s->send_hdlc_user_data, NULL, 0);
+                s->step++;
+            }
+            else
+            {
+                set_phase(s, T30_PHASE_B_RX);
+                timer_t2_start(s);
+            }
+            break;
+        case T30_STATE_III_Q_MCF:
+        case T30_STATE_III_Q_RTP:
+        case T30_STATE_III_Q_RTN:
+        case T30_STATE_F_POST_RCP_PPR:
+        case T30_STATE_F_POST_RCP_MCF:
+            if (s->step == 0)
+            {
+                /* Shut down HDLC transmission. */
+                if (s->send_hdlc_handler)
+                    s->send_hdlc_handler(s->send_hdlc_user_data, NULL, 0);
+                s->step++;
+            }
+            else
+            {
+                switch (s->next_rx_step)
+                {
+                case T30_MPS:
+                case T30_PRI_MPS:
+                    /* We should now start to get another page */
+                    if (s->error_correcting_mode)
+                    {
+                        set_state(s, T30_STATE_F_DOC_ECM);
+                        queue_phase(s, T30_PHASE_C_ECM_RX);
+                    }
+                    else
+                    {
+                        set_state(s, T30_STATE_F_DOC_NON_ECM);
+                        queue_phase(s, T30_PHASE_C_NON_ECM_RX);
+                    }
+                    timer_t2_start(s);
+                    break;
+                case T30_EOM:
+                case T30_PRI_EOM:
+                case T30_EOS:
+                    /* See if we get something back, before moving to phase B. */
+                    timer_t2_start(s);
+                    set_phase(s, T30_PHASE_D_RX);
+                    break;
+                case T30_EOP:
+                case T30_PRI_EOP:
+                    /* Wait for a DCN. */
+                    set_phase(s, T30_PHASE_D_RX);
+                    timer_t4_start(s);
+                    break;
+                default:
+                    span_log(&s->logging, SPAN_LOG_FLOW, "Unknown next rx step - %d\n", s->next_rx_step);
+                    disconnect(s);
+                    break;
+                }
+            }
+            break;
+        case T30_STATE_II_Q:
+        case T30_STATE_IV_PPS_NULL:
+        case T30_STATE_IV_PPS_Q:
+        case T30_STATE_IV_PPS_RNR:
+        case T30_STATE_IV_EOR_RNR:
+        case T30_STATE_F_POST_RCP_RNR:
+        case T30_STATE_IV_EOR:
+        case T30_STATE_IV_CTC:
+            if (s->step == 0)
+            {
+                /* Shut down HDLC transmission. */
+                if (s->send_hdlc_handler)
+                    s->send_hdlc_handler(s->send_hdlc_user_data, NULL, 0);
+                s->step++;
+            }
+            else
+            {
+                /* We have finished sending the post image message. Wait for an
+                   acknowledgement. */
+                set_phase(s, T30_PHASE_D_RX);
+                timer_t4_start(s);
+            }
+            break;
+        case T30_STATE_B:
+            /* We have now allowed time for the last message to flush
+               through the system, so it is safe to report the end of the
+               call. */
+            if (s->phase_e_handler)
+                s->phase_e_handler(s, s->phase_e_user_data, s->current_status);
+            set_state(s, T30_STATE_CALL_FINISHED);
+            set_phase(s, T30_PHASE_CALL_FINISHED);
+            release_resources(s);
+            break;
+        case T30_STATE_C:
+            if (s->step == 0)
+            {
+                /* Shut down HDLC transmission. */
+                if (s->send_hdlc_handler)
+                    s->send_hdlc_handler(s->send_hdlc_user_data, NULL, 0);
+                s->step++;
+            }
+            else
+            {
+                /* We just sent the disconnect message. Now it is time to disconnect */
+                disconnect(s);
+            }
+            break;
+        case T30_STATE_D:
+            if (send_dcs_sequence(s, FALSE))
+            {
+                if ((s->iaf & T30_IAF_MODE_NO_TCF))
+                {
+                    /* Skip the trainability test */
+                    s->retries = 0;
+                    s->short_train = TRUE;
+                    if (s->error_correcting_mode)
+                    {
+                        set_state(s, T30_STATE_IV);
+                        queue_phase(s, T30_PHASE_C_ECM_TX);
+                    }
+                    else
+                    {
+                        set_state(s, T30_STATE_I);
+                        queue_phase(s, T30_PHASE_C_NON_ECM_TX);
+                    }
+                }
+                else
+                {
+                    /* Do the trainability test */
+                    /* TCF is always sent with long training */
+                    s->short_train = FALSE;
+                    set_state(s, T30_STATE_D_TCF);
+                    set_phase(s, T30_PHASE_C_NON_ECM_TX);
+                }
+            }
+            break;
+        case T30_STATE_D_TCF:
+            /* Finished sending training test. Listen for the response. */
+            set_phase(s, T30_PHASE_B_RX);
+            timer_t4_start(s);
+            set_state(s, T30_STATE_D_POST_TCF);
+            break;
+        case T30_STATE_I:
+            /* Send the end of page message */
+            set_phase(s, T30_PHASE_D_TX);
+            set_state(s, T30_STATE_II_Q);
+            /* We might need to resend the page we are on, but we need to check if there
+               are any more pages to send, so we can send the correct signal right now. */
+            send_simple_frame(s, s->next_tx_step = check_next_tx_step(s));
+            break;
+        case T30_STATE_IV:
+            /* We have finished sending an FCD frame */
+            if (s->step == 0)
+            {
+                if (send_next_ecm_frame(s))
+                {
+                    /* Shut down HDLC transmission. */
+                    if (s->send_hdlc_handler)
+                        s->send_hdlc_handler(s->send_hdlc_user_data, NULL, 0);
+                    s->step++;
+                }
+            }
+            else
+            {
+                /* Send the end of page or partial page message */
+                set_phase(s, T30_PHASE_D_TX);
+                s->next_tx_step = check_next_tx_step(s);
+                if (send_pps_frame(s) == T30_NULL)
+                    set_state(s, T30_STATE_IV_PPS_NULL);
+                else
+                    set_state(s, T30_STATE_IV_PPS_Q);
+            }
+            break;
+        case T30_STATE_F_DOC_ECM:
+            /* This should be the end of a CTR being sent. */
+            if (s->step == 0)
+            {
+                /* Shut down HDLC transmission. */
+                if (s->send_hdlc_handler)
+                    s->send_hdlc_handler(s->send_hdlc_user_data, NULL, 0);
+                s->step++;
+            }
+            else
+            {
+                /* We have finished sending the CTR. Wait for image data again. */
+                queue_phase(s, T30_PHASE_C_ECM_RX);
+                timer_t2_start(s);
+            }
+            break;
+        case T30_STATE_CALL_FINISHED:
+            /* Just ignore anything that happens now. We might get here if a premature
+               disconnect from the far end overlaps something. */
+            break;
+        default:
+            span_log(&s->logging, SPAN_LOG_FLOW, "Bad state for send complete in t30_front_end_status - %d\n", s->state);
+            break;
+        }
+        break;
+    case T30_FRONT_END_RECEIVE_COMPLETE:
+        span_log(&s->logging, SPAN_LOG_FLOW, "Receive complete in phase %s, state %d\n", phase_names[s->phase], s->state);
+        /* Usually receive complete is notified by a carrier down signal. However,
+           in cases like a T.38 packet stream dying in the middle of reception
+           there needs to be a means to stop things. */
+        switch (s->phase)
+        {
+        case T30_PHASE_C_NON_ECM_RX:
+            t30_non_ecm_rx_status(s, PUTBIT_CARRIER_DOWN);
+            break;
+        default:
+            t30_hdlc_rx_status(s, PUTBIT_CARRIER_DOWN);
+            break;
+        }
+        break;
+    case T30_FRONT_END_SIGNAL_PRESENT:
+        span_log(&s->logging, SPAN_LOG_FLOW, "A signal is present\n");
+        /* The front end is explicitly telling us the signal we expect is present. This might
+           be a premature indication from a T.38 implementation, but we have to believe it.
+           if we don't we can time out improperly. For example, we might get an image modem
+           carrier signal, but the first HDLC frame might only occur several seconds later.
+           Many ECM senders idle on HDLC flags while waiting for the paper or filing system
+           to become ready. T.38 offers no specific indication of correct carrier training, so
+           if we don't kill the timer on the initial carrier starting signal, we will surely
+           time out quite often before the next thing we receive. */
+        switch (s->phase)
+        {
+        case T30_PHASE_A_CED:
+        case T30_PHASE_A_CNG:
+        case T30_PHASE_B_RX:
+        case T30_PHASE_D_RX:
+            /* We are running a V.21 receive modem, where an explicit training indication
+               will not occur. */
+            t30_hdlc_rx_status(s, PUTBIT_CARRIER_UP);
+            t30_hdlc_rx_status(s, PUTBIT_FRAMING_OK);
+            break;
+        default:
+            /* Cancel any receive timeout, and declare that a receive signal is present,
+               since the front end is explicitly telling us we have seen something. */
+            s->rx_signal_present = TRUE;
+            s->timer_t2_t4 = 0;
+            break;
+        }
+        break;
+    case T30_FRONT_END_SIGNAL_ABSENT:
+        span_log(&s->logging, SPAN_LOG_FLOW, "No signal is present\n");
+        /* TODO: Should we do anything here? */
+        break;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+void t30_timer_update(t30_state_t *s, int samples)
+{
+    if (s->timer_t0_t1 > 0)
+    {
+        s->timer_t0_t1 -= samples;
+        if (s->timer_t0_t1 <= 0)
+        {
+            if (s->far_end_detected)
+                timer_t1_expired(s);
+            else
+                timer_t0_expired(s);
+        }
+    }
+    if (s->timer_t3 > 0)
+    {
+        s->timer_t3 -= samples;
+        if (s->timer_t3 <= 0)
+            timer_t3_expired(s);
+    }
+    if (s->timer_t2_t4 > 0)
+    {
+        s->timer_t2_t4 -= samples;
+        if (s->timer_t2_t4 <= 0)
+        {
+            switch (s->timer_t2_t4_is)
+            {
+            case TIMER_IS_T2:
+                timer_t2_expired(s);
+                break;
+            case TIMER_IS_T2A:
+                timer_t2a_expired(s);
+                break;
+            case TIMER_IS_T2B:
+                timer_t2b_expired(s);
+                break;
+            case TIMER_IS_T4:
+                timer_t4_expired(s);
+                break;
+            case TIMER_IS_T4A:
+                timer_t4a_expired(s);
+                break;
+            case TIMER_IS_T4B:
+                timer_t4b_expired(s);
+                break;
+            }
+        }
+    }
+    if (s->timer_t5 > 0)
+    {
+        s->timer_t5 -= samples;
+        if (s->timer_t5 <= 0)
+            timer_t5_expired(s);
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+void t30_terminate(t30_state_t *s)
+{
+    if (s->phase != T30_PHASE_CALL_FINISHED)
+    {
+        /* The far end disconnected early, but was it just a tiny bit too early,
+           as we were just tidying up, or seriously early as in a failure? */
+        switch (s->state)
+        {
+        case T30_STATE_C:
+            /* We were sending the final disconnect, so just hussle things along. */
+            disconnect(s);
+            break;
+        case T30_STATE_B:
+            /* We were in the final wait for everything to flush through, so just
+               hussle things along. */
+            break;
+        default:
+            /* The call terminated prematurely. */
+            s->current_status = T30_ERR_CALLDROPPED;
+            break;
+        }
+        if (s->phase_e_handler)
+            s->phase_e_handler(s, s->phase_e_user_data, s->current_status);
+        set_state(s, T30_STATE_CALL_FINISHED);
+        set_phase(s, T30_PHASE_CALL_FINISHED);
+        release_resources(s);
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+void t30_get_transfer_statistics(t30_state_t *s, t30_stats_t *t)
+{
+    t4_stats_t stats;
+
+    t->bit_rate = fallback_sequence[s->current_fallback].bit_rate;
+    t->error_correcting_mode = s->error_correcting_mode;
+    t4_get_transfer_statistics(&s->t4, &stats);
+    t->pages_transferred = stats.pages_transferred;
+    t->pages_in_file = stats.pages_in_file;
+    t->width = stats.width;
+    t->length = stats.length;
+    t->bad_rows = stats.bad_rows;
+    t->longest_bad_row_run = stats.longest_bad_row_run;
+    t->x_resolution = stats.x_resolution;
+    t->y_resolution = stats.y_resolution;
+    t->encoding = stats.encoding;
+    t->image_size = stats.line_image_size;
+    t->current_status = s->current_status;
+}
+/*- End of function --------------------------------------------------------*/
+
+void t30_local_interrupt_request(t30_state_t *s, int state)
+{
+    if (s->timer_t3 > 0)
+    {
+        /* Accept the far end's outstanding request for interrupt. */
+        /* TODO: */
+        send_simple_frame(s, (state)  ?  T30_PIP  :  T30_PIN);
+    }
+    s->local_interrupt_pending = state;
+}
+/*- End of function --------------------------------------------------------*/
+
+int t30_restart(t30_state_t *s)
+{
+    s->phase = T30_PHASE_IDLE;
+    s->next_phase = T30_PHASE_IDLE;
+    s->current_fallback = 0;
+    s->rx_signal_present = FALSE;
+    s->rx_trained = FALSE;
+    s->rx_frame_received = FALSE;
+    s->current_status = T30_ERR_OK;
+    s->ppr_count = 0;
+    s->ecm_progress = 0;
+    s->receiver_not_ready_count = 0;
+    s->far_dis_dtc_len = 0;
+    memset(&s->far_dis_dtc_frame, 0, sizeof(s->far_dis_dtc_frame));
+    t30_build_dis_or_dtc(s);
+    if (s->calling_party)
+    {
+        set_state(s, T30_STATE_T);
+        set_phase(s, T30_PHASE_A_CNG);
+    }
+    else
+    {
+        set_state(s, T30_STATE_ANSWERING);
+        set_phase(s, T30_PHASE_A_CED);
+    }
+    memset(&s->rx_info, 0, sizeof(s->rx_info));
+    s->far_end_detected = FALSE;
+    s->timer_t0_t1 = ms_to_samples(DEFAULT_TIMER_T0);
+    release_resources(s);
+    s->ecm_rx_page = -1;
+    s->ecm_tx_page = -1;
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+t30_state_t *t30_init(t30_state_t *s,
+                      int calling_party,
+                      t30_set_handler_t *set_rx_type_handler,
+                      void *set_rx_type_user_data,
+                      t30_set_handler_t *set_tx_type_handler,
+                      void *set_tx_type_user_data,
+                      t30_send_hdlc_handler_t *send_hdlc_handler,
+                      void *send_hdlc_user_data)
+{
+    if (s == NULL)
+    {
+        if ((s = (t30_state_t *) malloc(sizeof(*s))) == NULL)
+            return NULL;
+    }
+    memset(s, 0, sizeof(*s));
+    s->calling_party = calling_party;
+    s->set_rx_type_handler = set_rx_type_handler;
+    s->set_rx_type_user_data = set_rx_type_user_data;
+    s->set_tx_type_handler = set_tx_type_handler;
+    s->set_tx_type_user_data = set_tx_type_user_data;
+    s->send_hdlc_handler = send_hdlc_handler;
+    s->send_hdlc_user_data = send_hdlc_user_data;
+
+    /* Default to the basic modems. */
+    s->supported_modems = T30_SUPPORT_V27TER | T30_SUPPORT_V29;
+    s->supported_compressions = T30_SUPPORT_T4_1D_COMPRESSION | T30_SUPPORT_T4_2D_COMPRESSION;
+    s->supported_resolutions = T30_SUPPORT_STANDARD_RESOLUTION | T30_SUPPORT_FINE_RESOLUTION | T30_SUPPORT_SUPERFINE_RESOLUTION
+                             | T30_SUPPORT_R8_RESOLUTION;
+    s->supported_image_sizes = T30_SUPPORT_US_LETTER_LENGTH | T30_SUPPORT_US_LEGAL_LENGTH | T30_SUPPORT_UNLIMITED_LENGTH
+                             | T30_SUPPORT_215MM_WIDTH;
+    /* Set the output encoding to something safe. Most things get 1D and 2D
+       encoding right. Quite a lot get other things wrong. */
+    s->output_encoding = T4_COMPRESSION_ITU_T4_2D;
+    s->local_min_scan_time_code = T30_MIN_SCAN_0MS;
+    span_log_init(&s->logging, SPAN_LOG_NONE, NULL);
+    span_log_set_protocol(&s->logging, "T.30");
+    t30_restart(s);
+    return s;
+}
+/*- End of function --------------------------------------------------------*/
+
+int t30_release(t30_state_t *s)
+{
+    /* Make sure any FAX in progress is tidied up. If the tidying up has
+       already happened, repeating it here is harmless. */
+    switch (s->operation_in_progress)
+    {
+    case OPERATION_IN_PROGRESS_T4_TX:
+        t4_tx_end(&(s->t4));
+        break;
+    case OPERATION_IN_PROGRESS_T4_RX:
+        t4_rx_end(&(s->t4));
+        break;
+    }
+    s->operation_in_progress = OPERATION_IN_PROGRESS_NONE;
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int t30_free(t30_state_t *s)
+{
+    t30_release(s);
+    free(s);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/t30_api.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/t30_api.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,727 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * t30_api.c - ITU T.30 FAX transfer processing
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2003, 2004, 2005, 2006, 2007 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: t30_api.c,v 1.8 2008/08/14 14:06:05 steveu Exp $
+ */
+
+/*! \file */
+
+#if defined(HAVE_CONFIG_H)
+#include <config.h>
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <inttypes.h>
+#include <string.h>
+#include <fcntl.h>
+#include <time.h>
+#include "floating_fudge.h"
+#if defined(HAVE_TGMATH_H)
+#include <tgmath.h>
+#endif
+#if defined(HAVE_MATH_H)
+#include <math.h>
+#endif
+#include <tiffio.h>
+
+#include "spandsp/telephony.h"
+#include "spandsp/logging.h"
+#include "spandsp/bit_operations.h"
+#include "spandsp/queue.h"
+#include "spandsp/power_meter.h"
+#include "spandsp/complex.h"
+#include "spandsp/tone_generate.h"
+#include "spandsp/async.h"
+#include "spandsp/hdlc.h"
+#include "spandsp/fsk.h"
+#include "spandsp/v29rx.h"
+#include "spandsp/v29tx.h"
+#include "spandsp/v27ter_rx.h"
+#include "spandsp/v27ter_tx.h"
+#include "spandsp/t4.h"
+#include "spandsp/t30_fcf.h"
+#include "spandsp/t35.h"
+#include "spandsp/t30.h"
+#include "spandsp/t30_api.h"
+#include "spandsp/t30_logging.h"
+
+#include "t30_local.h"
+
+int t30_set_tx_ident(t30_state_t *s, const char *id)
+{
+    if (id == NULL)
+    {
+        s->tx_info.ident[0] = '\0';
+        return 0;
+    }
+    if (strlen(id) > T30_MAX_IDENT_LEN)
+        return -1;
+    strcpy(s->tx_info.ident, id);
+    t4_tx_set_local_ident(&(s->t4), s->tx_info.ident);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+const char *t30_get_tx_ident(t30_state_t *s)
+{
+    if (s->tx_info.ident[0] == '\0')
+        return NULL;
+    return s->tx_info.ident;
+}
+/*- End of function --------------------------------------------------------*/
+
+const char *t30_get_rx_ident(t30_state_t *s)
+{
+    if (s->rx_info.ident[0] == '\0')
+        return NULL;
+    return s->rx_info.ident;
+}
+/*- End of function --------------------------------------------------------*/
+
+int t30_set_tx_sub_address(t30_state_t *s, const char *sub_address)
+{
+    if (sub_address == NULL)
+    {
+        s->tx_info.sub_address[0] = '\0';
+        return 0;
+    }
+    if (strlen(sub_address) > T30_MAX_IDENT_LEN)
+        return -1;
+    strcpy(s->tx_info.sub_address, sub_address);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+const char *t30_get_tx_sub_address(t30_state_t *s)
+{
+    if (s->tx_info.sub_address[0] == '\0')
+        return NULL;
+    return s->tx_info.sub_address;
+}
+/*- End of function --------------------------------------------------------*/
+
+const char *t30_get_rx_sub_address(t30_state_t *s)
+{
+    if (s->rx_info.sub_address[0] == '\0')
+        return NULL;
+    return s->rx_info.sub_address;
+}
+/*- End of function --------------------------------------------------------*/
+
+int t30_set_tx_selective_polling_address(t30_state_t *s, const char *selective_polling_address)
+{
+    if (selective_polling_address == NULL)
+    {
+        s->tx_info.selective_polling_address[0] = '\0';
+        return 0;
+    }
+    if (strlen(selective_polling_address) > T30_MAX_IDENT_LEN)
+        return -1;
+    strcpy(s->tx_info.selective_polling_address, selective_polling_address);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+const char *t30_get_tx_selective_polling_address(t30_state_t *s)
+{
+    if (s->tx_info.selective_polling_address[0] == '\0')
+        return NULL;
+    return s->tx_info.selective_polling_address;
+}
+/*- End of function --------------------------------------------------------*/
+
+const char *t30_get_rx_selective_polling_address(t30_state_t *s)
+{
+    if (s->rx_info.selective_polling_address[0] == '\0')
+        return NULL;
+    return s->rx_info.selective_polling_address;
+}
+/*- End of function --------------------------------------------------------*/
+
+int t30_set_tx_polled_sub_address(t30_state_t *s, const char *polled_sub_address)
+{
+    if (polled_sub_address == NULL)
+    {
+        s->tx_info.polled_sub_address[0] = '\0';
+        return 0;
+    }
+    if (strlen(polled_sub_address) > T30_MAX_IDENT_LEN)
+        return -1;
+    strcpy(s->tx_info.polled_sub_address, polled_sub_address);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+const char *t30_get_tx_polled_sub_address(t30_state_t *s)
+{
+    if (s->tx_info.polled_sub_address[0] == '\0')
+        return NULL;
+    return s->tx_info.polled_sub_address;
+}
+/*- End of function --------------------------------------------------------*/
+
+const char *t30_get_rx_polled_sub_address(t30_state_t *s)
+{
+    if (s->rx_info.polled_sub_address[0] == '\0')
+        return NULL;
+    return s->rx_info.polled_sub_address;
+}
+/*- End of function --------------------------------------------------------*/
+
+int t30_set_tx_sender_ident(t30_state_t *s, const char *sender_ident)
+{
+    if (sender_ident == NULL)
+    {
+        s->tx_info.sender_ident[0] = '\0';
+        return 0;
+    }
+    if (strlen(sender_ident) > T30_MAX_IDENT_LEN)
+        return -1;
+    strcpy(s->tx_info.sender_ident, sender_ident);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+const char *t30_get_tx_sender_ident(t30_state_t *s)
+{
+    if (s->tx_info.sender_ident[0] == '\0')
+        return NULL;
+    return s->tx_info.sender_ident;
+}
+/*- End of function --------------------------------------------------------*/
+
+const char *t30_get_rx_sender_ident(t30_state_t *s)
+{
+    if (s->rx_info.sender_ident[0] == '\0')
+        return NULL;
+    return s->rx_info.sender_ident;
+}
+/*- End of function --------------------------------------------------------*/
+
+int t30_set_tx_password(t30_state_t *s, const char *password)
+{
+    if (password == NULL)
+    {
+        s->tx_info.password[0] = '\0';
+        return 0;
+    }
+    if (strlen(password) > T30_MAX_IDENT_LEN)
+        return -1;
+    strcpy(s->tx_info.password, password);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+const char *t30_get_tx_password(t30_state_t *s)
+{
+    if (s->tx_info.password[0] == '\0')
+        return NULL;
+    return s->tx_info.password;
+}
+/*- End of function --------------------------------------------------------*/
+
+const char *t30_get_rx_password(t30_state_t *s)
+{
+    if (s->rx_info.password[0] == '\0')
+        return NULL;
+    return s->rx_info.password;
+}
+/*- End of function --------------------------------------------------------*/
+
+int t30_set_tx_nsf(t30_state_t *s, const uint8_t *nsf, int len)
+{
+    if (s->tx_info.nsf)
+        free(s->tx_info.nsf);
+    if (nsf  &&  len > 0  &&  (s->tx_info.nsf = malloc(len + 3)))
+    {
+        memcpy(s->tx_info.nsf + 3, nsf, len);
+        s->tx_info.nsf_len = len;
+    }
+    else
+    {
+        s->tx_info.nsf = NULL;
+        s->tx_info.nsf_len = 0;
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+size_t t30_get_tx_nsf(t30_state_t *s, const uint8_t *nsf[])
+{
+    if (nsf)
+        *nsf = s->tx_info.nsf;
+    return s->tx_info.nsf_len;
+}
+/*- End of function --------------------------------------------------------*/
+
+size_t t30_get_rx_nsf(t30_state_t *s, const uint8_t *nsf[])
+{
+    if (nsf)
+        *nsf = s->rx_info.nsf;
+    return s->rx_info.nsf_len;
+}
+/*- End of function --------------------------------------------------------*/
+
+int t30_set_tx_nsc(t30_state_t *s, const uint8_t *nsc, int len)
+{
+    if (s->tx_info.nsc)
+        free(s->tx_info.nsc);
+    if (nsc  &&  len > 0  &&  (s->tx_info.nsc = malloc(len + 3)))
+    {
+        memcpy(s->tx_info.nsc + 3, nsc, len);
+        s->tx_info.nsc_len = len;
+    }
+    else
+    {
+        s->tx_info.nsc = NULL;
+        s->tx_info.nsc_len = 0;
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+size_t t30_get_tx_nsc(t30_state_t *s, const uint8_t *nsc[])
+{
+    if (nsc)
+        *nsc = s->tx_info.nsc;
+    return s->tx_info.nsc_len;
+}
+/*- End of function --------------------------------------------------------*/
+
+size_t t30_get_rx_nsc(t30_state_t *s, const uint8_t *nsc[])
+{
+    if (nsc)
+        *nsc = s->rx_info.nsc;
+    return s->rx_info.nsc_len;
+}
+/*- End of function --------------------------------------------------------*/
+
+int t30_set_tx_nss(t30_state_t *s, const uint8_t *nss, int len)
+{
+    if (s->tx_info.nss)
+        free(s->tx_info.nss);
+    if (nss  &&  len > 0  &&  (s->tx_info.nss = malloc(len + 3)))
+    {
+        memcpy(s->tx_info.nss + 3, nss, len);
+        s->tx_info.nss_len = len;
+    }
+    else
+    {
+        s->tx_info.nss = NULL;
+        s->tx_info.nss_len = 0;
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+size_t t30_get_tx_nss(t30_state_t *s, const uint8_t *nss[])
+{
+    if (nss)
+        *nss = s->tx_info.nss;
+    return s->tx_info.nss_len;
+}
+/*- End of function --------------------------------------------------------*/
+
+size_t t30_get_rx_nss(t30_state_t *s, const uint8_t *nss[])
+{
+    if (nss)
+        *nss = s->rx_info.nss;
+    return s->rx_info.nss_len;
+}
+/*- End of function --------------------------------------------------------*/
+
+int t30_set_tx_tsa(t30_state_t *s, int type, const char *address, int len)
+{
+    if (s->tx_info.tsa)
+        free(s->tx_info.tsa);
+    if (address == NULL  ||  len == 0)
+    {
+        s->tx_info.tsa = NULL;
+        s->tx_info.tsa_len = 0;
+        return 0;
+    }
+    s->tx_info.tsa_type = type;
+    if (len < 0)
+        len = strlen(address);
+    if ((s->tx_info.tsa = malloc(len)))
+    {
+        memcpy(s->tx_info.tsa, address, len);
+        s->tx_info.tsa_len = len;
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+size_t t30_get_tx_tsa(t30_state_t *s, int *type, const char *address[])
+{
+    if (type)
+        *type = s->tx_info.tsa_type;
+    if (address)
+        *address = s->tx_info.tsa;
+    return s->tx_info.tsa_len;
+}
+/*- End of function --------------------------------------------------------*/
+
+size_t t30_get_rx_tsa(t30_state_t *s, int *type, const char *address[])
+{
+    if (type)
+        *type = s->rx_info.tsa_type;
+    if (address)
+        *address = s->rx_info.tsa;
+    return s->rx_info.tsa_len;
+}
+/*- End of function --------------------------------------------------------*/
+
+int t30_set_tx_ira(t30_state_t *s, int type, const char *address, int len)
+{
+    if (s->tx_info.ira)
+        free(s->tx_info.ira);
+    if (address == NULL)
+    {
+        s->tx_info.ira = NULL;
+        return 0;
+    }
+    s->tx_info.ira = strdup(address);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+size_t t30_get_tx_ira(t30_state_t *s, int *type, const char *address[])
+{
+    if (type)
+        *type = s->tx_info.ira_type;
+    if (address)
+        *address = s->tx_info.ira;
+    return s->tx_info.ira_len;
+}
+/*- End of function --------------------------------------------------------*/
+
+size_t t30_get_rx_ira(t30_state_t *s, int *type, const char *address[])
+{
+    if (type)
+        *type = s->rx_info.ira_type;
+    if (address)
+        *address = s->rx_info.ira;
+    return s->rx_info.ira_len;
+}
+/*- End of function --------------------------------------------------------*/
+
+int t30_set_tx_cia(t30_state_t *s, int type, const char *address, int len)
+{
+    if (s->tx_info.cia)
+        free(s->tx_info.cia);
+    if (address == NULL)
+    {
+        s->tx_info.cia = NULL;
+        return 0;
+    }
+    s->tx_info.cia = strdup(address);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+size_t t30_get_tx_cia(t30_state_t *s, int *type, const char *address[])
+{
+    if (type)
+        *type = s->tx_info.cia_type;
+    if (address)
+        *address = s->tx_info.cia;
+    return s->tx_info.cia_len;
+}
+/*- End of function --------------------------------------------------------*/
+
+size_t t30_get_rx_cia(t30_state_t *s, int *type, const char *address[])
+{
+    if (type)
+        *type = s->rx_info.cia_type;
+    if (address)
+        *address = s->rx_info.cia;
+    return s->rx_info.cia_len;
+}
+/*- End of function --------------------------------------------------------*/
+
+int t30_set_tx_isp(t30_state_t *s, int type, const char *address, int len)
+{
+    if (s->tx_info.isp)
+        free(s->tx_info.isp);
+    if (address == NULL)
+    {
+        s->tx_info.isp = NULL;
+        return 0;
+    }
+    s->tx_info.isp = strdup(address);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+size_t t30_get_tx_isp(t30_state_t *s, int *type, const char *address[])
+{
+    if (type)
+        *type = s->tx_info.isp_type;
+    if (address)
+        *address = s->tx_info.isp;
+    return s->tx_info.isp_len;
+}
+/*- End of function --------------------------------------------------------*/
+
+size_t t30_get_rx_isp(t30_state_t *s, int *type, const char *address[])
+{
+    if (type)
+        *type = s->rx_info.isp_type;
+    if (address)
+        *address = s->rx_info.isp;
+    return s->rx_info.isp_len;
+}
+/*- End of function --------------------------------------------------------*/
+
+int t30_set_tx_csa(t30_state_t *s, int type, const char *address, int len)
+{
+    if (s->tx_info.csa)
+        free(s->tx_info.csa);
+    if (address == NULL)
+    {
+        s->tx_info.csa = NULL;
+        return 0;
+    }
+    s->tx_info.csa = strdup(address);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+size_t t30_get_tx_csa(t30_state_t *s, int *type, const char *address[])
+{
+    if (type)
+        *type = s->tx_info.csa_type;
+    if (address)
+        *address = s->tx_info.csa;
+    return s->tx_info.csa_len;
+}
+/*- End of function --------------------------------------------------------*/
+
+size_t t30_get_rx_csa(t30_state_t *s, int *type, const char *address[])
+{
+    if (type)
+        *type = s->rx_info.csa_type;
+    if (address)
+        *address = s->rx_info.csa;
+    return s->rx_info.csa_len;
+}
+/*- End of function --------------------------------------------------------*/
+
+int t30_set_tx_page_header_info(t30_state_t *s, const char *info)
+{
+    if (info == NULL)
+    {
+        s->header_info[0] = '\0';
+        return 0;
+    }
+    if (strlen(info) > T30_MAX_PAGE_HEADER_INFO)
+        return -1;
+    strcpy(s->header_info, info);
+    t4_tx_set_header_info(&(s->t4), s->header_info);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+size_t t30_get_tx_page_header_info(t30_state_t *s, char *info)
+{
+    if (info)
+        strcpy(info, s->header_info);
+    return strlen(s->header_info);
+}
+/*- End of function --------------------------------------------------------*/
+
+const char *t30_get_rx_country(t30_state_t *s)
+{
+    return s->country;
+}
+/*- End of function --------------------------------------------------------*/
+
+const char *t30_get_rx_vendor(t30_state_t *s)
+{
+    return s->vendor;
+}
+/*- End of function --------------------------------------------------------*/
+
+const char *t30_get_rx_model(t30_state_t *s)
+{
+    return s->model;
+}
+/*- End of function --------------------------------------------------------*/
+
+void t30_set_rx_file(t30_state_t *s, const char *file, int stop_page)
+{
+    strncpy(s->rx_file, file, sizeof(s->rx_file));
+    s->rx_file[sizeof(s->rx_file) - 1] = '\0';
+    s->rx_stop_page = stop_page;
+}
+/*- End of function --------------------------------------------------------*/
+
+void t30_set_tx_file(t30_state_t *s, const char *file, int start_page, int stop_page)
+{
+    strncpy(s->tx_file, file, sizeof(s->tx_file));
+    s->tx_file[sizeof(s->tx_file) - 1] = '\0';
+    s->tx_start_page = start_page;
+    s->tx_stop_page = stop_page;
+}
+/*- End of function --------------------------------------------------------*/
+
+void t30_set_iaf_mode(t30_state_t *s, int iaf)
+{
+    s->iaf = iaf;
+}
+/*- End of function --------------------------------------------------------*/
+
+int t30_set_ecm_capability(t30_state_t *s, int enabled)
+{
+    s->ecm_allowed = enabled;
+    t30_build_dis_or_dtc(s);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int t30_set_rx_encoding(t30_state_t *s, int encoding)
+{
+    switch (encoding)
+    {
+    case T4_COMPRESSION_ITU_T4_1D:
+    case T4_COMPRESSION_ITU_T4_2D:
+    case T4_COMPRESSION_ITU_T6:
+        s->output_encoding = encoding;
+        return 0;
+    }
+    return -1;
+}
+/*- End of function --------------------------------------------------------*/
+
+int t30_set_minimum_scan_line_time(t30_state_t *s, int min_time)
+{
+    /* There are only certain possible times supported, so we need to select
+       the code which best matches the request. */
+    if (min_time == 0)
+        s->local_min_scan_time_code = 7;
+    else if (min_time <= 5)
+        s->local_min_scan_time_code = 1;
+    else if (min_time <= 10)
+        s->local_min_scan_time_code = 2;
+    else if (min_time <= 20)
+        s->local_min_scan_time_code = 0;
+    else if (min_time <= 40)
+        s->local_min_scan_time_code = 4;
+    else
+        return -1;
+    t30_build_dis_or_dtc(s);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int t30_set_supported_modems(t30_state_t *s, int supported_modems)
+{
+    s->supported_modems = supported_modems;
+    t30_build_dis_or_dtc(s);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int t30_set_supported_compressions(t30_state_t *s, int supported_compressions)
+{
+    s->supported_compressions = supported_compressions;
+    t30_build_dis_or_dtc(s);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int t30_set_supported_resolutions(t30_state_t *s, int supported_resolutions)
+{
+    s->supported_resolutions = supported_resolutions;
+    t30_build_dis_or_dtc(s);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int t30_set_supported_image_sizes(t30_state_t *s, int supported_image_sizes)
+{
+    s->supported_image_sizes = supported_image_sizes;
+    t30_build_dis_or_dtc(s);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int t30_set_supported_t30_features(t30_state_t *s, int supported_t30_features)
+{
+    s->supported_t30_features = supported_t30_features;
+    t30_build_dis_or_dtc(s);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+void t30_set_status(t30_state_t *s, int status)
+{
+    s->current_status = status;
+}
+/*- End of function --------------------------------------------------------*/
+
+int t30_set_receiver_not_ready(t30_state_t *s, int count)
+{
+    s->receiver_not_ready_count = count;
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+void t30_set_phase_b_handler(t30_state_t *s, t30_phase_b_handler_t *handler, void *user_data)
+{
+    s->phase_b_handler = handler;
+    s->phase_b_user_data = user_data;
+}
+/*- End of function --------------------------------------------------------*/
+
+void t30_set_phase_d_handler(t30_state_t *s, t30_phase_d_handler_t *handler, void *user_data)
+{
+    s->phase_d_handler = handler;
+    s->phase_d_user_data = user_data;
+}
+/*- End of function --------------------------------------------------------*/
+
+void t30_set_phase_e_handler(t30_state_t *s, t30_phase_e_handler_t *handler, void *user_data)
+{
+    s->phase_e_handler = handler;
+    s->phase_e_user_data = user_data;
+}
+/*- End of function --------------------------------------------------------*/
+
+void t30_set_document_handler(t30_state_t *s, t30_document_handler_t *handler, void *user_data)
+{
+    s->document_handler = handler;
+    s->document_user_data = user_data;
+}
+/*- End of function --------------------------------------------------------*/
+
+void t30_set_real_time_frame_handler(t30_state_t *s, t30_real_time_frame_handler_t *handler, void *user_data)
+{
+    s->real_time_frame_handler = handler;
+    s->real_time_frame_user_data = user_data;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/t30_local.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/t30_local.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,45 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * t30_local.h - definitions for T.30 fax processing
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2003 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: t30_local.h,v 1.2 2008/04/17 14:26:57 steveu Exp $
+ */
+
+/*! \file */
+
+#if !defined(_T30_LOCAL_H_)
+#define _T30_LOCAL_H_
+
+#if defined(__cplusplus)
+extern "C"
+{
+#endif
+
+int t30_build_dis_or_dtc(t30_state_t *s);
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/t30_logging.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/t30_logging.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,938 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * t30_logging.c - ITU T.30 FAX transfer processing
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2003, 2004, 2005, 2006, 2007 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: t30_logging.c,v 1.6 2008/08/01 17:59:46 steveu Exp $
+ */
+
+/*! \file */
+
+#if defined(HAVE_CONFIG_H)
+#include <config.h>
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <inttypes.h>
+#include <string.h>
+#include <fcntl.h>
+#include <time.h>
+#include "floating_fudge.h"
+#if defined(HAVE_TGMATH_H)
+#include <tgmath.h>
+#endif
+#if defined(HAVE_MATH_H)
+#include <math.h>
+#endif
+#include <tiffio.h>
+
+#include "spandsp/telephony.h"
+#include "spandsp/logging.h"
+#include "spandsp/bit_operations.h"
+#include "spandsp/queue.h"
+#include "spandsp/power_meter.h"
+#include "spandsp/complex.h"
+#include "spandsp/tone_generate.h"
+#include "spandsp/async.h"
+#include "spandsp/hdlc.h"
+#include "spandsp/fsk.h"
+#include "spandsp/v29rx.h"
+#include "spandsp/v29tx.h"
+#include "spandsp/v27ter_rx.h"
+#include "spandsp/v27ter_tx.h"
+#include "spandsp/t4.h"
+#include "spandsp/t30_fcf.h"
+#include "spandsp/t35.h"
+#include "spandsp/t30.h"
+#include "spandsp/t30_logging.h"
+
+#include "t30_local.h"
+
+typedef struct
+{
+    int val;
+    const char *str;
+} value_string_t;
+
+enum
+{
+    DISBIT1 = 0x01,
+    DISBIT2 = 0x02,
+    DISBIT3 = 0x04,
+    DISBIT4 = 0x08,
+    DISBIT5 = 0x10,
+    DISBIT6 = 0x20,
+    DISBIT7 = 0x40,
+    DISBIT8 = 0x80
+};
+
+const char *t30_completion_code_to_str(int result)
+{
+    switch (result)
+    {
+    case T30_ERR_OK:
+        return "OK";
+    case T30_ERR_CEDTONE:
+        return "The CED tone exceeded 5s";
+    case T30_ERR_T0_EXPIRED:
+        return "Timed out waiting for initial communication";
+    case T30_ERR_T1_EXPIRED:
+        return "Timed out waiting for the first message";
+    case T30_ERR_T3_EXPIRED:
+        return "Timed out waiting for procedural interrupt";
+    case T30_ERR_HDLC_CARRIER:
+        return "The HDLC carrier did not stop in a timely manner";
+    case T30_ERR_CANNOT_TRAIN:
+        return "Failed to train with any of the compatible modems";
+    case T30_ERR_OPER_INT_FAIL:
+        return "Operator intervention failed";
+    case T30_ERR_INCOMPATIBLE:
+        return "Far end is not compatible";
+    case T30_ERR_RX_INCAPABLE:
+        return "Far end is not able to receive";
+    case T30_ERR_TX_INCAPABLE:
+        return "Far end is not able to transmit";
+    case T30_ERR_NORESSUPPORT:
+        return "Far end cannot receive at the resolution of the image";
+    case T30_ERR_NOSIZESUPPORT:
+        return "Far end cannot receive at the size of image";
+    case T30_ERR_UNEXPECTED:
+        return "Unexpected message received";
+    case T30_ERR_TX_BADDCS:
+        return "Received bad response to DCS or training";
+    case T30_ERR_TX_BADPG:
+        return "Received a DCN from remote after sending a page";
+    case T30_ERR_TX_ECMPHD:
+        return "Invalid ECM response received from receiver";
+    case T30_ERR_TX_GOTDCN:
+        return "Received a DCN while waiting for a DIS";
+    case T30_ERR_TX_INVALRSP:
+        return "Invalid response after sending a page";
+    case T30_ERR_TX_NODIS:
+        return "Received other than DIS while waiting for DIS";
+    case T30_ERR_TX_PHBDEAD:
+        return "Received no response to DCS or TCF";
+    case T30_ERR_TX_PHDDEAD:
+        return "No response after sending a page";
+    case T30_ERR_TX_T5EXP:
+        return "Timed out waiting for receiver ready (ECM mode)";
+    case T30_ERR_RX_ECMPHD:
+        return "Invalid ECM response received from transmitter";
+    case T30_ERR_RX_GOTDCS:
+        return "DCS received while waiting for DTC";
+    case T30_ERR_RX_INVALCMD:
+        return "Unexpected command after page received";
+    case T30_ERR_RX_NOCARRIER:
+        return "Carrier lost during fax receive";
+    case T30_ERR_RX_NOEOL:
+        return "Timed out while waiting for EOL (end Of line)";
+    case T30_ERR_RX_NOFAX:
+        return "Timed out while waiting for first line";
+    case T30_ERR_RX_T2EXPDCN:
+        return "Timer T2 expired while waiting for DCN";
+    case T30_ERR_RX_T2EXPD:
+        return "Timer T2 expired while waiting for phase D";
+    case T30_ERR_RX_T2EXPFAX:
+        return "Timer T2 expired while waiting for fax page";
+    case T30_ERR_RX_T2EXPMPS:
+        return "Timer T2 expired while waiting for next fax page";
+    case T30_ERR_RX_T2EXPRR:
+        return "Timer T2 expired while waiting for RR command";
+    case T30_ERR_RX_T2EXP:
+        return "Timer T2 expired while waiting for NSS, DCS or MCF";
+    case T30_ERR_RX_DCNWHY:
+        return "Unexpected DCN while waiting for DCS or DIS";
+    case T30_ERR_RX_DCNDATA:
+        return "Unexpected DCN while waiting for image data";
+    case T30_ERR_RX_DCNFAX:
+        return "Unexpected DCN while waiting for EOM, EOP or MPS";
+    case T30_ERR_RX_DCNPHD:
+        return "Unexpected DCN after EOM or MPS sequence";
+    case T30_ERR_RX_DCNRRD:
+        return "Unexpected DCN after RR/RNR sequence";
+    case T30_ERR_RX_DCNNORTN:
+        return "Unexpected DCN after requested retransmission";
+    case T30_ERR_FILEERROR:
+        return "TIFF/F file cannot be opened";
+    case T30_ERR_NOPAGE:
+        return "TIFF/F page not found";
+    case T30_ERR_BADTIFF:
+        return "TIFF/F format is not compatible";
+    case T30_ERR_BADPAGE:
+        return "TIFF/F page number tag missing";
+    case T30_ERR_BADTAG:
+        return "Incorrect values for TIFF/F tags";
+    case T30_ERR_BADTIFFHDR:
+        return "Bad TIFF/F header - incorrect values in fields";
+    case T30_ERR_NOMEM:
+        return "Cannot allocate memory for more pages";
+    case T30_ERR_RETRYDCN:
+        return "Disconnected after permitted retries";
+    case T30_ERR_CALLDROPPED:
+        return "The call dropped prematurely";
+    case T30_ERR_NOPOLL:
+        return "Poll not accepted";
+    case T30_ERR_IDENT_UNACCEPTABLE:
+        return "Ident not accepted";
+    case T30_ERR_PSA_UNACCEPTABLE:
+        return "Polled sub-address not accepted";
+    case T30_ERR_SEP_UNACCEPTABLE:
+        return "Selective polling address not accepted";
+    case T30_ERR_SID_UNACCEPTABLE:
+        return "Sender identification not accepted";
+    case T30_ERR_PWD_UNACCEPTABLE:
+        return "Password not accepted";
+    case T30_ERR_SUB_UNACCEPTABLE:
+        return "Sub-address not accepted";
+    case T30_ERR_TSA_UNACCEPTABLE:
+        return "Transmitting subscriber internet address not accepted";
+    case T30_ERR_IRA_UNACCEPTABLE:
+        return "Internet routing address not accepted";
+    case T30_ERR_CIA_UNACCEPTABLE:
+        return "Calling subscriber internet address not accepted";
+    case T30_ERR_ISP_UNACCEPTABLE:
+        return "Internet selective polling address not accepted";
+    case T30_ERR_CSA_UNACCEPTABLE:
+        return "Called subscriber internet address not accepted";
+    }
+    return "???";
+}
+/*- End of function --------------------------------------------------------*/
+
+const char *t30_frametype(uint8_t x)
+{
+    switch (x)
+    {
+    case T30_DIS:
+        return "DIS";
+    case T30_CSI:
+        return "CSI";
+    case T30_NSF:
+        return "NSF";
+    case T30_DTC:
+        return "DTC";
+    case T30_CIG:
+        return "CIG";
+    case T30_NSC:
+        return "NSC";
+    case T30_PWD:
+        return "PWD";
+    case T30_SEP:
+        return "SEP";
+    case T30_PSA:
+        return "PSA";
+    case T30_CIA:
+        return "CIA";
+    case T30_ISP:
+        return "ISP";
+    case T30_DCS:
+    case T30_DCS | 0x01:
+        return "DCS";
+    case T30_TSI:
+    case T30_TSI | 0x01:
+        return "TSI";
+    case T30_NSS:
+    case T30_NSS | 0x01:
+        return "NSS";
+    case T30_SUB:
+    case T30_SUB | 0x01:
+        return "SUB";
+    case T30_SID:
+    case T30_SID | 0x01:
+        return "SID";
+    case T30_CTC:
+    case T30_CTC | 0x01:
+        return "CTC";
+    case T30_TSA:
+    case T30_TSA | 0x01:
+        return "TSA";
+    case T30_IRA:
+    case T30_IRA | 0x01:
+        return "IRA";
+    case T30_CFR:
+    case T30_CFR | 0x01:
+        return "CFR";
+    case T30_FTT:
+    case T30_FTT | 0x01:
+        return "FTT";
+    case T30_CTR:
+    case T30_CTR | 0x01:
+        return "CTR";
+    case T30_CSA:
+    case T30_CSA | 0x01:
+        return "CSA";
+    case T30_EOM:
+    case T30_EOM | 0x01:
+        return "EOM";
+    case T30_MPS:
+    case T30_MPS | 0x01:
+        return "MPS";
+    case T30_EOP:
+    case T30_EOP | 0x01:
+        return "EOP";
+    case T30_PRI_EOM:
+    case T30_PRI_EOM | 0x01:
+        return "PRI-EOM";
+    case T30_PRI_MPS:
+    case T30_PRI_MPS | 0x01:
+        return "PRI-MPS";
+    case T30_PRI_EOP:
+    case T30_PRI_EOP | 0x01:
+        return "PRI-EOP";
+    case T30_EOS:
+    case T30_EOS | 0x01:
+        return "EOS";
+    case T30_PPS:
+    case T30_PPS | 0x01:
+        return "PPS";
+    case T30_EOR:
+    case T30_EOR | 0x01:
+        return "EOR";
+    case T30_RR:
+    case T30_RR | 0x01:
+        return "RR";
+    case T30_MCF:
+    case T30_MCF | 0x01:
+        return "MCF";
+    case T30_RTP:
+    case T30_RTP | 0x01:
+        return "RTP";
+    case T30_RTN:
+    case T30_RTN | 0x01:
+        return "RTN";
+    case T30_PIP:
+    case T30_PIP | 0x01:
+        return "PIP";
+    case T30_PIN:
+    case T30_PIN | 0x01:
+        return "PIN";
+    case T30_PPR:
+    case T30_PPR | 0x01:
+        return "PPR";
+    case T30_RNR:
+    case T30_RNR | 0x01:
+        return "RNR";
+    case T30_ERR:
+    case T30_ERR | 0x01:
+        return "ERR";
+    case T30_FDM:
+    case T30_FDM | 0x01:
+        return "FDM";
+    case T30_DCN:
+    case T30_DCN | 0x01:
+        return "DCN";
+    case T30_CRP:
+    case T30_CRP | 0x01:
+        return "CRP";
+    case T30_FNV:
+    case T30_FNV | 0x01:
+        return "FNV";
+    case T30_TNR:
+    case T30_TNR | 0x01:
+        return "TNR";
+    case T30_TR:
+    case T30_TR | 0x01:
+        return "TR";
+    case T30_TK:
+        return "TK";
+    case T30_RK:
+        return "RK";
+#if 0
+    case T30_PSS:
+        return "PSS";
+#endif
+    case T30_DES:
+        return "DES";
+    case T30_DEC:
+        return "DEC";
+    case T30_DER:
+        return "DER";
+#if 0
+    case T30_DTR:
+        return "DTR";
+#endif
+    case T30_DNK:
+    case T30_DNK | 0x01:
+        return "DNK";
+    case T30_PID:
+    case T30_PID | 0x01:
+        return "PID";
+    case T30_NULL:
+        return "NULL";
+    case T4_FCD:
+        return "FCD";
+    case T4_RCP:
+        return "RCP";
+    }
+    return "???";
+}
+/*- End of function --------------------------------------------------------*/
+
+static void octet_reserved_bit(logging_state_t *log,
+                               const uint8_t *msg,
+                               int bit_no,
+                               int expected)
+{
+    char s[10] = ".... ....";
+    int bit;
+    uint8_t octet;
+    
+    /* Break out the octet and the bit number within it. */
+    octet = msg[((bit_no - 1) >> 3) + 3];
+    bit_no = (bit_no - 1) & 7;
+    /* Now get the actual bit. */
+    bit = (octet >> bit_no) & 1;
+    /* Is it what it should be. */
+    if (bit ^ expected)
+    {
+        /* Only log unexpected values. */
+        s[7 - bit_no + ((bit_no < 4)  ?  1  :  0)] = (uint8_t) (bit + '0');
+        span_log(log, SPAN_LOG_FLOW, "  %s= Unexpected state for reserved bit: %d\n", s, bit);
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static void octet_bit_field(logging_state_t *log,
+                            const uint8_t *msg,
+                            int bit_no,
+                            const char *desc,
+                            const char *yeah,
+                            const char *neigh)
+{
+    char s[10] = ".... ....";
+    int bit;
+    uint8_t octet;
+    const char *tag;
+
+    /* Break out the octet and the bit number within it. */
+    octet = msg[((bit_no - 1) >> 3) + 3];
+    bit_no = (bit_no - 1) & 7;
+    /* Now get the actual bit. */
+    bit = (octet >> bit_no) & 1;
+    /* Edit the bit string for display. */
+    s[7 - bit_no + ((bit_no < 4)  ?  1  :  0)] = (uint8_t) (bit + '0');
+    /* Find the right tag to display. */
+    if (bit)
+    {
+        if ((tag = yeah) == NULL)
+            tag = "Set";
+    }
+    else
+    {
+        if ((tag = neigh) == NULL)
+            tag = "Not set";
+    }
+    /* Eh, voila! */
+    span_log(log, SPAN_LOG_FLOW, "  %s= %s: %s\n", s, desc, tag);
+}
+/*- End of function --------------------------------------------------------*/
+
+static void octet_field(logging_state_t *log,
+                        const uint8_t *msg,
+                        int start,
+                        int end,
+                        const char *desc,
+                        const value_string_t tags[])
+{
+    char s[10] = ".... ....";
+    int i;
+    uint8_t octet;
+    const char *tag;
+    
+    /* Break out the octet and the bit number range within it. */
+    octet = msg[((start - 1) >> 3) + 3];
+    start = (start - 1) & 7;
+    end = ((end - 1) & 7) + 1;
+
+    /* Edit the bit string for display. */
+    for (i = start;  i < end;  i++)
+        s[7 - i + ((i < 4)  ?  1  :  0)] = (uint8_t) ((octet >> i) & 1) + '0';
+
+    /* Find the right tag to display. */
+    octet = (uint8_t) ((octet >> start) & ((0xFF + (1 << (end - start))) & 0xFF));
+    tag = "Invalid";
+    for (i = 0;  tags[i].str;  i++)
+    {
+        if (octet == tags[i].val)
+        {
+            tag = tags[i].str;
+            break;
+        }
+    }
+    /* Eh, voila! */
+    span_log(log, SPAN_LOG_FLOW, "  %s= %s: %s\n", s, desc, tag);
+}
+/*- End of function --------------------------------------------------------*/
+
+void t30_decode_dis_dtc_dcs(t30_state_t *s, const uint8_t *pkt, int len)
+{
+    logging_state_t *log;
+    uint8_t frame_type;
+    static const value_string_t available_signalling_rate_tags[] =
+    {
+        { 0x00, "V.27 ter fall-back mode" },
+        { 0x01, "V.29" },
+        { 0x02, "V.27 ter" },
+        { 0x03, "V.27 ter and V.29" },
+        { 0x0B, "V.27 ter, V.29, and V.17" },
+        { 0x06, "Reserved" },
+        { 0x0A, "Reserved" },
+        { 0x0E, "Reserved" },
+        { 0x0F, "Reserved" },
+        { 0x04, "Not used" },
+        { 0x05, "Not used" },
+        { 0x08, "Not used" },
+        { 0x09, "Not used" },
+        { 0x0C, "Not used" },
+        { 0x0D, "Not used" },
+        { 0x00, NULL }
+    };
+    static const value_string_t selected_signalling_rate_tags[] =
+    {
+        { 0x00, "V.27ter 2400bps" },
+        { 0x01, "V.29, 9600bps" },
+        { 0x02, "V.27ter 4800bps" },
+        { 0x03, "V.29 7200bps" },
+        { 0x08, "V.17 14400bps" },
+        { 0x09, "V.17 9600bps" },
+        { 0x0A, "V.17 12000bps" },
+        { 0x0B, "V.17 7200bps" },
+        { 0x05, "Reserved" },
+        { 0x07, "Reserved" },
+        { 0x0C, "Reserved" },
+        { 0x0D, "Reserved" },
+        { 0x0E, "Reserved" },
+        { 0x0F, "Reserved" },
+        { 0x00, NULL }
+    };
+    static const value_string_t available_scan_line_length_tags[] =
+    {
+        { 0x00, "215mm +- 1%" },
+        { 0x01, "215mm +- 1% and 255mm +- 1%" },
+        { 0x02, "215mm +- 1%, 255mm +- 1% and 303mm +- 1%" },
+        { 0x00, NULL }
+    };
+    static const value_string_t selected_scan_line_length_tags[] =
+    {
+        { 0x00, "215mm +- 1%" },
+        { 0x01, "255mm +- 1%" },
+        { 0x02, "303mm +- 1%" },
+        { 0x00, NULL }
+    };
+    static const value_string_t available_recording_length_tags[] =
+    {
+        { 0x00, "A4 (297mm)" },
+        { 0x01, "A4 (297mm) and B4 (364mm)" },
+        { 0x02, "Unlimited" },
+        { 0x00, NULL }
+    };
+    static const value_string_t selected_recording_length_tags[] =
+    {
+        { 0x00, "A4 (297mm)" },
+        { 0x01, "B4 (364mm)" },
+        { 0x02, "Unlimited" },
+        { 0x00, NULL }
+    };
+    static const value_string_t available_minimum_scan_line_time_tags[] =
+    {
+        { 0x00, "20ms at 3.85 l/mm; T7.7 = T3.85" },
+        { 0x01, "5ms at 3.85 l/mm; T7.7 = T3.85" },
+        { 0x02, "10ms at 3.85 l/mm; T7.7 = T3.85" },
+        { 0x03, "20ms at 3.85 l/mm; T7.7 = 1/2 T3.85" },
+        { 0x04, "40ms at 3.85 l/mm; T7.7 = T3.85" },
+        { 0x05, "40ms at 3.85 l/mm; T7.7 = 1/2 T3.85" },
+        { 0x06, "10ms at 3.85 l/mm; T7.7 = 1/2 T3.85" },
+        { 0x07, "0ms at 3.85 l/mm; T7.7 = T3.85" },
+        { 0x00, NULL }
+    };
+    static const value_string_t selected_minimum_scan_line_time_tags[] =
+    {
+        { 0x00, "20ms" },
+        { 0x01, "5ms" },
+        { 0x02, "10ms" },
+        { 0x04, "40ms" },
+        { 0x07, "0ms" },
+        { 0x00, NULL }
+    };
+    static const value_string_t shared_data_memory_capacity_tags[] =
+    {
+        { 0x00, "Not available" },
+        { 0x01, "Level 2 = 2.0 Mbytes" },
+        { 0x02, "Level 1 = 1.0 Mbytes" },
+        { 0x03, "Level 3 = unlimited (i.e. >= 32 Mbytes)" },
+        { 0x00, NULL }
+    };
+    static const value_string_t t89_profile_tags[] =
+    {
+        { 0x00, "Not used" },
+        { 0x01, "Profiles 2 and 3" },
+        { 0x02, "Profile 2" },
+        { 0x04, "Profile 1" },
+        { 0x06, "Profile 3" },
+        { 0x03, "Reserved" },
+        { 0x05, "Reserved" },
+        { 0x07, "Reserved" },
+        { 0x00, NULL }
+    };
+    static const value_string_t t44_mixed_raster_content_tags[] =
+    {
+        { 0x00, "0" },
+        { 0x01, "1" },
+        { 0x02, "2" },
+        { 0x32, "3" },
+        { 0x04, "4" },
+        { 0x05, "5" },
+        { 0x06, "6" },
+        { 0x07, "7" },
+        { 0x00, NULL }
+    };
+
+    if (!span_log_test(&s->logging, SPAN_LOG_FLOW))
+        return;
+    frame_type = pkt[2] & 0xFE;
+    log = &s->logging;
+    if (len <= 2)
+    {
+        span_log(log, SPAN_LOG_FLOW, "  Frame is short\n");
+        return;
+    }
+    
+    span_log(log, SPAN_LOG_FLOW, "%s:\n", t30_frametype(pkt[2]));
+    if (len <= 3)
+    {
+        span_log(log, SPAN_LOG_FLOW, "  Frame is short\n");
+        return;
+    }
+    octet_bit_field(log, pkt, 1, "Store and forward Internet fax (T.37)", NULL, NULL);
+    octet_reserved_bit(log, pkt, 2, 0);
+    octet_bit_field(log, pkt, 3, "Real-time Internet fax (T.38)", NULL, NULL);
+    octet_bit_field(log, pkt, 4, "3G mobile network", NULL, NULL);
+    octet_reserved_bit(log, pkt, 5, 0);
+    if (frame_type == T30_DCS)
+    {
+        octet_reserved_bit(log, pkt, 6, 0);
+        octet_reserved_bit(log, pkt, 7, 0);
+    }
+    else
+    {
+        octet_bit_field(log, pkt, 6, "V.8 capabilities", NULL, NULL);
+        octet_bit_field(log, pkt, 7, "Preferred octets", "64 octets", "256 octets");
+    }
+    octet_reserved_bit(log, pkt, 8, 0);
+    if (len <= 4)
+    {
+        span_log(log, SPAN_LOG_FLOW, "  Frame is short\n");
+        return;
+    }
+    
+    if (frame_type == T30_DCS)
+    {
+        octet_reserved_bit(log, pkt, 9, 0);
+        octet_bit_field(log, pkt, 10, "Receive fax", NULL, NULL);
+        octet_field(log, pkt, 11, 14, "Selected data signalling rate", selected_signalling_rate_tags);
+    }
+    else
+    {
+        octet_bit_field(log, pkt, 9, "Ready to transmit a fax document (polling)", NULL, NULL);
+        octet_bit_field(log, pkt, 10, "Can receive fax", NULL, NULL);
+        octet_field(log, pkt, 11, 14, "Supported data signalling rates", available_signalling_rate_tags);
+    }
+    octet_bit_field(log, pkt, 15, "R8x7.7lines/mm and/or 200x200pels/25.4mm", NULL, NULL);
+    octet_bit_field(log, pkt, 16, "2-D coding", NULL, NULL);
+    if (len <= 5)
+    {
+        span_log(log, SPAN_LOG_FLOW, "  Frame is short\n");
+        return;
+    }
+
+    if (frame_type == T30_DCS)
+    {
+        octet_field(log, pkt, 17, 18, "Recording width", selected_scan_line_length_tags);
+        octet_field(log, pkt, 19, 20, "Recording length", selected_recording_length_tags);
+        octet_field(log, pkt, 21, 23, "Minimum scan line time", selected_minimum_scan_line_time_tags);
+    }
+    else
+    {
+        octet_field(log, pkt, 17, 18, "Recording width", available_scan_line_length_tags);
+        octet_field(log, pkt, 19, 20, "Recording length", available_recording_length_tags);
+        octet_field(log, pkt, 21, 23, "Receiver's minimum scan line time", available_minimum_scan_line_time_tags);
+    }
+    octet_bit_field(log, pkt, 24, "Extension indicator", NULL, NULL);
+    if (!(pkt[5] & DISBIT8))
+        return;
+    if (len <= 6)
+    {
+        span_log(log, SPAN_LOG_FLOW, "  Frame is short\n");
+        return;
+    }
+
+    octet_reserved_bit(log, pkt, 25, 0);
+    octet_bit_field(log, pkt, 26, "Compressed/uncompressed mode", "Uncompressed", "Compressed");
+    octet_bit_field(log, pkt, 27, "Error correction mode (ECM)", "ECM", "Non-ECM");
+    if (frame_type == T30_DCS)
+        octet_bit_field(log, pkt, 28, "Frame size", "64 octets", "256 octets");
+    else
+        octet_reserved_bit(log, pkt, 28, 0);
+    octet_reserved_bit(log, pkt, 29, 0);
+    octet_reserved_bit(log, pkt, 30, 0);
+    octet_bit_field(log, pkt, 31, "T.6 coding", NULL, NULL);
+    octet_bit_field(log, pkt, 32, "Extension indicator", NULL, NULL);
+    if (!(pkt[6] & DISBIT8))
+        return;
+    if (len <= 7)
+    {
+        span_log(log, SPAN_LOG_FLOW, "  Frame is short\n");
+        return;
+    }
+
+    octet_bit_field(log, pkt, 33, "\"Field not valid\" supported", NULL, NULL);
+    if (frame_type == T30_DCS)
+    {
+        octet_reserved_bit(log, pkt, 34, 0);
+        octet_reserved_bit(log, pkt, 35, 0);
+    }
+    else
+    {
+        octet_bit_field(log, pkt, 34, "Multiple selective polling", NULL, NULL);
+        octet_bit_field(log, pkt, 35, "Polled sub-address", NULL, NULL);
+    }
+    octet_bit_field(log, pkt, 36, "T.43 coding", NULL, NULL);
+    octet_bit_field(log, pkt, 37, "Plane interleave", NULL, NULL);
+    octet_bit_field(log, pkt, 38, "Voice coding with 32kbit/s ADPCM (Rec. G.726)", NULL, NULL);
+    octet_bit_field(log, pkt, 39, "Reserved for the use of extended voice coding set", NULL, NULL);
+    octet_bit_field(log, pkt, 40, "Extension indicator", NULL, NULL);
+    if (!(pkt[7] & DISBIT8))
+        return;
+    if (len <= 8)
+    {
+        span_log(log, SPAN_LOG_FLOW, "  Frame is short\n");
+        return;
+    }
+    octet_bit_field(log, pkt, 41, "R8x15.4lines/mm", NULL, NULL);
+    octet_bit_field(log, pkt, 42, "300x300pels/25.4mm", NULL, NULL);
+    octet_bit_field(log, pkt, 43, "R16x15.4lines/mm and/or 400x400pels/25.4mm", NULL, NULL);
+    if (frame_type == T30_DCS)
+    {
+        octet_bit_field(log, pkt, 44, "Resolution type selection", "Inch", "Metric");
+        octet_reserved_bit(log, pkt, 45, 0);
+        octet_reserved_bit(log, pkt, 46, 0);
+        octet_reserved_bit(log, pkt, 47, 0);
+    }
+    else
+    {
+        octet_bit_field(log, pkt, 44, "Inch-based resolution preferred", NULL, NULL);
+        octet_bit_field(log, pkt, 45, "Metric-based resolution preferred", NULL, NULL);
+        octet_bit_field(log, pkt, 46, "Minimum scan line time for higher resolutions", "T15.4 = 1/2 T7.7", "T15.4 = T7.7");
+        octet_bit_field(log, pkt, 47, "Selective polling", NULL, NULL);
+    }
+    octet_bit_field(log, pkt, 48, "Extension indicator", NULL, NULL);
+    if (!(pkt[8] & DISBIT8))
+        return;
+    if (len <= 9)
+    {
+        span_log(log, SPAN_LOG_FLOW, "  Frame is short\n");
+        return;
+    }
+
+    octet_bit_field(log, pkt, 49, "Sub-addressing", NULL, NULL);
+    if (frame_type == T30_DCS)
+    {
+        octet_bit_field(log, pkt, 50, "Sender identification transmission", NULL, NULL);
+        octet_reserved_bit(log, pkt, 51, 0);
+    }
+    else
+    {
+        octet_bit_field(log, pkt, 50, "Password", NULL, NULL);
+        octet_bit_field(log, pkt, 51, "Ready to transmit a data file (polling)", NULL, NULL);
+    }
+    octet_reserved_bit(log, pkt, 52, 0);
+    octet_bit_field(log, pkt, 53, "Binary file transfer (BFT)", NULL, NULL);
+    octet_bit_field(log, pkt, 54, "Document transfer mode (DTM)", NULL, NULL);
+    octet_bit_field(log, pkt, 55, "Electronic data interchange (EDI)", NULL, NULL);
+    octet_bit_field(log, pkt, 56, "Extension indicator", NULL, NULL);
+    if (!(pkt[9] & DISBIT8))
+        return;
+    if (len <= 10)
+    {
+        span_log(log, SPAN_LOG_FLOW, "  Frame is short\n");
+        return;
+    }
+
+    octet_bit_field(log, pkt, 57, "Basic transfer mode (BTM)", NULL, NULL);
+    octet_reserved_bit(log, pkt, 58, 0);
+    if (frame_type == T30_DCS)
+        octet_reserved_bit(log, pkt, 59, 0);
+    else
+        octet_bit_field(log, pkt, 59, "Ready to transfer a character or mixed mode document (polling)", NULL, NULL);
+    octet_bit_field(log, pkt, 60, "Character mode", NULL, NULL);
+    octet_reserved_bit(log, pkt, 61, 0);
+    octet_bit_field(log, pkt, 62, "Mixed mode (Annex E/T.4)", NULL, NULL);
+    octet_reserved_bit(log, pkt, 63, 0);
+    octet_bit_field(log, pkt, 64, "Extension indicator", NULL, NULL);
+    if (!(pkt[10] & DISBIT8))
+        return;
+    if (len <= 11)
+    {
+        span_log(log, SPAN_LOG_FLOW, "  Frame is short\n");
+        return;
+    }
+
+    octet_bit_field(log, pkt, 65, "Processable mode 26 (Rec. T.505)", NULL, NULL);
+    octet_bit_field(log, pkt, 66, "Digital network capability", NULL, NULL);
+    octet_bit_field(log, pkt, 67, "Duplex capability", "Full", "Half only");
+    if (frame_type == T30_DCS)
+        octet_bit_field(log, pkt, 68, "Full colour mode", NULL, NULL);
+    else
+        octet_bit_field(log, pkt, 68, "JPEG coding", NULL, NULL);
+    octet_bit_field(log, pkt, 69, "Full colour mode", NULL, NULL);
+    if (frame_type == T30_DCS)
+        octet_bit_field(log, pkt, 70, "Preferred Huffman tables", NULL, NULL);
+    else
+        octet_reserved_bit(log, pkt, 70, 0);
+    octet_bit_field(log, pkt, 71, "12bits/pel component", NULL, NULL);
+    octet_bit_field(log, pkt, 72, "Extension indicator", NULL, NULL);
+    if (!(pkt[11] & DISBIT8))
+        return;
+    if (len <= 12)
+    {
+        span_log(log, SPAN_LOG_FLOW, "  Frame is short\n");
+        return;
+    }
+
+    octet_bit_field(log, pkt, 73, "No subsampling (1:1:1)", NULL, NULL);
+    octet_bit_field(log, pkt, 74, "Custom illuminant", NULL, NULL);
+    octet_bit_field(log, pkt, 75, "Custom gamut range", NULL, NULL);
+    octet_bit_field(log, pkt, 76, "North American Letter (215.9mm x 279.4mm)", NULL, NULL);
+    octet_bit_field(log, pkt, 77, "North American Legal (215.9mm x 355.6mm)", NULL, NULL);
+    octet_bit_field(log, pkt, 78, "Single-progression sequential coding (Rec. T.85) basic", NULL, NULL);
+    octet_bit_field(log, pkt, 79, "Single-progression sequential coding (Rec. T.85) optional L0", NULL, NULL);
+    octet_bit_field(log, pkt, 80, "Extension indicator", NULL, NULL);
+    if (!(pkt[12] & DISBIT8))
+        return;
+    if (len <= 13)
+    {
+        span_log(log, SPAN_LOG_FLOW, "  Frame is short\n");
+        return;
+    }
+
+    octet_bit_field(log, pkt, 81, "HKM key management", NULL, NULL);
+    octet_bit_field(log, pkt, 82, "RSA key management", NULL, NULL);
+    octet_bit_field(log, pkt, 83, "Override", NULL, NULL);
+    octet_bit_field(log, pkt, 84, "HFX40 cipher", NULL, NULL);
+    octet_bit_field(log, pkt, 85, "Alternative cipher number 2", NULL, NULL);
+    octet_bit_field(log, pkt, 86, "Alternative cipher number 3", NULL, NULL);
+    octet_bit_field(log, pkt, 87, "HFX40-I hashing", NULL, NULL);
+    octet_bit_field(log, pkt, 88, "Extension indicator", NULL, NULL);
+    if (!(pkt[13] & DISBIT8))
+        return;
+    if (len <= 14)
+    {
+        span_log(log, SPAN_LOG_FLOW, "  Frame is short\n");
+        return;
+    }
+
+    octet_bit_field(log, pkt, 89, "Alternative hashing system 2", NULL, NULL);
+    octet_bit_field(log, pkt, 90, "Alternative hashing system 3", NULL, NULL);
+    octet_bit_field(log, pkt, 91, "Reserved for future security features", NULL, NULL);
+    octet_field(log, pkt, 92, 94, "T.44 (Mixed Raster Content)", t44_mixed_raster_content_tags);
+    octet_bit_field(log, pkt, 95, "Page length maximum stripe size for T.44 (Mixed Raster Content)", NULL, NULL);
+    octet_bit_field(log, pkt, 96, "Extension indicator", NULL, NULL);
+    if (!(pkt[14] & DISBIT8))
+        return;
+    if (len <= 15)
+    {
+        span_log(log, SPAN_LOG_FLOW, "  Frame is short\n");
+        return;
+    }
+
+    octet_bit_field(log, pkt, 97, "Colour/gray-scale 300pels/25.4mm x 300lines/25.4mm or 400pels/25.4mm x 400lines/25.4mm resolution", NULL, NULL);
+    octet_bit_field(log, pkt, 98, "100pels/25.4mm x 100lines/25.4mm for colour/gray scale", NULL, NULL);
+    octet_bit_field(log, pkt, 99, "Simple phase C BFT negotiations", NULL, NULL);
+    if (frame_type == T30_DCS)
+    {
+        octet_reserved_bit(log, pkt, 100, 0);
+        octet_reserved_bit(log, pkt, 101, 0);
+    }
+    else
+    {
+        octet_bit_field(log, pkt, 100, "Extended BFT Negotiations capable", NULL, NULL);
+        octet_bit_field(log, pkt, 101, "Internet Selective Polling address (ISP)", NULL, NULL);
+    }
+    octet_bit_field(log, pkt, 102, "Internet Routing Address (IRA)", NULL, NULL);
+    octet_reserved_bit(log, pkt, 103, 0);
+    octet_bit_field(log, pkt, 104, "Extension indicator", NULL, NULL);
+    if (!(pkt[15] & DISBIT8))
+        return;
+    if (len <= 16)
+    {
+        span_log(log, SPAN_LOG_FLOW, "  Frame is short\n");
+        return;
+    }
+
+    octet_bit_field(log, pkt, 105, "600pels/25.4mm x 600lines/25.4mm", NULL, NULL);
+    octet_bit_field(log, pkt, 106, "1200pels/25.4mm x 1200lines/25.4mm", NULL, NULL);
+    octet_bit_field(log, pkt, 107, "300pels/25.4mm x 600lines/25.4mm", NULL, NULL);
+    octet_bit_field(log, pkt, 108, "400pels/25.4mm x 800lines/25.4mm", NULL, NULL);
+    octet_bit_field(log, pkt, 109, "600pels/25.4mm x 1200lines/25.4mm", NULL, NULL);
+    octet_bit_field(log, pkt, 110, "Colour/gray scale 600pels/25.4mm x 600lines/25.4mm", NULL, NULL);
+    octet_bit_field(log, pkt, 111, "Colour/gray scale 1200pels/25.4mm x 1200lines/25.4mm", NULL, NULL);
+    octet_bit_field(log, pkt, 112, "Extension indicator", NULL, NULL);
+    if (!(pkt[16] & DISBIT8))
+        return;
+    if (len <= 17)
+    {
+        span_log(log, SPAN_LOG_FLOW, "  Frame is short\n");
+        return;
+    }
+
+    octet_bit_field(log, pkt, 113, "Double sided printing capability (alternate mode)", NULL, NULL);
+    octet_bit_field(log, pkt, 114, "Double sided printing capability (continuous mode)", NULL, NULL);
+    if (frame_type == T30_DCS)
+        octet_bit_field(log, pkt, 115, "Black and white mixed raster content profile (MRCbw)", NULL, NULL);
+    else
+        octet_reserved_bit(log, pkt, 115, 0);
+    octet_bit_field(log, pkt, 116, "T.45 (run length colour encoded)", NULL, NULL);
+    octet_field(log, pkt, 117, 118, "Shared memory", shared_data_memory_capacity_tags);
+    octet_bit_field(log, pkt, 119, "T.44 colour space", NULL, NULL);
+    octet_bit_field(log, pkt, 120, "Extension indicator", NULL, NULL);
+    if (!(pkt[17] & DISBIT8))
+        return;
+    if (len <= 18)
+    {
+        span_log(log, SPAN_LOG_FLOW, "  Frame is short\n");
+        return;
+    }
+
+    octet_bit_field(log, pkt, 121, "Flow control capability for T.38 communication", NULL, NULL);
+    octet_bit_field(log, pkt, 122, "K>4", NULL, NULL);
+    octet_bit_field(log, pkt, 123, "Internet aware T.38 mode fax (not affected by data signal rate bits)", NULL, NULL);
+    octet_field(log, pkt, 124, 126, "T.89 (Application profiles for ITU-T Rec T.8)", t89_profile_tags);
+    octet_bit_field(log, pkt, 127, "sYCC-JPEG coding", NULL, NULL);
+    octet_bit_field(log, pkt, 128, "Extension indicator", NULL, NULL);
+    if (!(pkt[18] & DISBIT8))
+        return;
+
+    span_log(log, SPAN_LOG_FLOW, "  Extended beyond the current T.30 specification!\n");
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/t31.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/t31.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,2024 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * t31.c - A T.31 compatible class 1 FAX modem interface.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Special thanks to Lee Howard <faxguy at howardsilvan.com>
+ * for his great work debugging and polishing this code.
+ *
+ * Copyright (C) 2004, 2005, 2006 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: t31.c,v 1.119 2008/08/09 05:09:56 steveu Exp $
+ */
+
+/*! \file */
+
+#if defined(HAVE_CONFIG_H)
+#include <config.h>
+#endif
+
+#include <inttypes.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <time.h>
+#include <memory.h>
+#include <string.h>
+#include <ctype.h>
+#include <malloc.h>
+#include "floating_fudge.h"
+#if defined(HAVE_TGMATH_H)
+#include <tgmath.h>
+#endif
+#if defined(HAVE_MATH_H)
+#include <math.h>
+#endif
+#include <assert.h>
+#include <tiffio.h>
+
+#include "spandsp/telephony.h"
+#include "spandsp/logging.h"
+#include "spandsp/bit_operations.h"
+#include "spandsp/dc_restore.h"
+#include "spandsp/queue.h"
+#include "spandsp/power_meter.h"
+#include "spandsp/complex.h"
+#include "spandsp/tone_detect.h"
+#include "spandsp/tone_generate.h"
+#include "spandsp/async.h"
+#include "spandsp/hdlc.h"
+#include "spandsp/silence_gen.h"
+#include "spandsp/fsk.h"
+#include "spandsp/v29rx.h"
+#include "spandsp/v29tx.h"
+#include "spandsp/v27ter_rx.h"
+#include "spandsp/v27ter_tx.h"
+#include "spandsp/v17rx.h"
+#include "spandsp/v17tx.h"
+#include "spandsp/super_tone_rx.h"
+#include "spandsp/modem_connect_tones.h"
+#include "spandsp/t4.h"
+#include "spandsp/t30.h"
+#include "spandsp/t30_logging.h"
+#include "spandsp/t38_core.h"
+
+#include "spandsp/at_interpreter.h"
+#include "spandsp/fax_modems.h"
+#include "spandsp/t31.h"
+
+/* Settings suitable for paced transmission over a UDP transport */
+#define MS_PER_TX_CHUNK                 30
+#define INDICATOR_TX_COUNT              3
+#define DATA_TX_COUNT                   1
+#define DATA_END_TX_COUNT               3
+#define DEFAULT_DTE_TIMEOUT             5
+
+/* Settings suitable for unpaced transmission over a TCP transport */
+#define MAX_OCTETS_PER_UNPACED_CHUNK    300
+
+/* Backstop timeout if reception of packets stops in the middle of a burst */
+#define MID_RX_TIMEOUT                  15000
+
+#define HDLC_FRAMING_OK_THRESHOLD       5
+
+typedef const char *(*at_cmd_service_t)(t31_state_t *s, const char *cmd);
+
+#define ETX 0x03
+#define DLE 0x10
+#define SUB 0x1A
+
+/* BEWARE: right now this must match up with a list in the AT interpreter code. */
+enum
+{
+    T31_NONE = -1,
+    T31_FLUSH = 0,
+    T31_SILENCE_TX,
+    T31_SILENCE_RX,
+    T31_CED_TONE,
+    T31_CNG_TONE,
+    T31_NOCNG_TONE,
+    T31_V21_TX,
+    T31_V17_TX,
+    T31_V27TER_TX,
+    T31_V29_TX,
+    T31_V21_RX,
+    T31_V17_RX,
+    T31_V27TER_RX,
+    T31_V29_RX
+};
+
+enum
+{
+    T38_TIMED_STEP_NONE = 0,
+    T38_TIMED_STEP_NON_ECM_MODEM,
+    T38_TIMED_STEP_NON_ECM_MODEM_2,
+    T38_TIMED_STEP_NON_ECM_MODEM_3,
+    T38_TIMED_STEP_NON_ECM_MODEM_4,
+    T38_TIMED_STEP_NON_ECM_MODEM_5,
+    T38_TIMED_STEP_HDLC_MODEM,
+    T38_TIMED_STEP_HDLC_MODEM_2,
+    T38_TIMED_STEP_HDLC_MODEM_3,
+    T38_TIMED_STEP_HDLC_MODEM_4,
+    T38_TIMED_STEP_CED,
+    T38_TIMED_STEP_CED_2,
+    T38_TIMED_STEP_CNG,
+    T38_TIMED_STEP_CNG_2,
+    T38_TIMED_STEP_PAUSE
+};
+
+static int restart_modem(t31_state_t *s, int new_modem);
+static void hdlc_accept(void *user_data, const uint8_t *msg, int len, int ok);
+static int v17_v21_rx(void *user_data, const int16_t amp[], int len);
+static int v27ter_v21_rx(void *user_data, const int16_t amp[], int len);
+static int v29_v21_rx(void *user_data, const int16_t amp[], int len);
+static int silence_rx(void *user_data, const int16_t amp[], int len);
+static int cng_rx(void *user_data, const int16_t amp[], int len);
+
+static __inline__ void t31_set_at_rx_mode(t31_state_t *s, int new_mode)
+{
+    s->at_state.at_rx_mode = new_mode;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int process_rx_missing(t38_core_state_t *t, void *user_data, int rx_seq_no, int expected_seq_no)
+{
+    t31_state_t *s;
+    
+    s = (t31_state_t *) user_data;
+    s->t38_fe.missing_data = TRUE;
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int process_rx_indicator(t38_core_state_t *t, void *user_data, int indicator)
+{
+    t31_state_t *s;
+    t31_t38_front_end_state_t *fe;
+    
+    s = (t31_state_t *) user_data;
+    fe = &s->t38_fe;
+
+    if (t->current_rx_indicator == indicator)
+    {
+        /* This is probably due to the far end repeating itself. Ignore it. Its harmless */
+        return 0;
+    }
+    switch (indicator)
+    {
+    case T38_IND_NO_SIGNAL:
+        if (t->current_rx_indicator == T38_IND_V21_PREAMBLE
+            &&
+            (fe->current_rx_type == T30_MODEM_V21  ||  fe->current_rx_type == T30_MODEM_CNG))
+        {
+            /* TODO: report carrier down */
+        }
+        fe->timeout_rx_samples = 0;
+        /* TODO: report end of signal */
+        break;
+    case T38_IND_CNG:
+        break;
+    case T38_IND_CED:
+        break;
+    case T38_IND_V21_PREAMBLE:
+        /* Some people pop these preamble indicators between HDLC frames, so we need to be tolerant of that. */
+        fe->timeout_rx_samples = fe->samples + ms_to_samples(MID_RX_TIMEOUT);
+        /* TODO: report signal present */
+        break;
+    case T38_IND_V27TER_2400_TRAINING:
+    case T38_IND_V27TER_4800_TRAINING:
+    case T38_IND_V29_7200_TRAINING:
+    case T38_IND_V29_9600_TRAINING:
+    case T38_IND_V17_7200_SHORT_TRAINING:
+    case T38_IND_V17_7200_LONG_TRAINING:
+    case T38_IND_V17_9600_SHORT_TRAINING:
+    case T38_IND_V17_9600_LONG_TRAINING:
+    case T38_IND_V17_12000_SHORT_TRAINING:
+    case T38_IND_V17_12000_LONG_TRAINING:
+    case T38_IND_V17_14400_SHORT_TRAINING:
+    case T38_IND_V17_14400_LONG_TRAINING:
+    case T38_IND_V33_12000_TRAINING:
+    case T38_IND_V33_14400_TRAINING:
+        fe->timeout_rx_samples = fe->samples + ms_to_samples(MID_RX_TIMEOUT);
+        /* TODO: report signal present */
+        break;
+    case T38_IND_V8_ANSAM:
+    case T38_IND_V8_SIGNAL:
+    case T38_IND_V34_CNTL_CHANNEL_1200:
+    case T38_IND_V34_PRI_CHANNEL:
+    case T38_IND_V34_CC_RETRAIN:
+        /* TODO: report signal present */
+        break;
+    default:
+        /* TODO: report end of signal */
+        break;
+    }
+    fe->hdlc_rx.len = 0;
+    fe->missing_data = FALSE;
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int process_rx_data(t38_core_state_t *t, void *user_data, int data_type, int field_type, const uint8_t *buf, int len)
+{
+    t31_state_t *s;
+    t31_t38_front_end_state_t *fe;
+#if defined(_MSC_VER)
+    uint8_t *buf2 = (uint8_t *) alloca(len);
+#else
+    uint8_t buf2[len];
+#endif
+
+    s = (t31_state_t *) user_data;
+    fe = &s->t38_fe;
+#if 0
+    switch (data_type)
+    {
+    case T38_DATA_V21:
+    case T38_DATA_V27TER_2400:
+    case T38_DATA_V27TER_4800:
+    case T38_DATA_V29_7200:
+    case T38_DATA_V29_9600:
+    case T38_DATA_V17_7200:
+    case T38_DATA_V17_9600:
+    case T38_DATA_V17_12000:
+    case T38_DATA_V17_14400:
+    case T38_DATA_V8:
+    case T38_DATA_V34_PRI_RATE:
+    case T38_DATA_V34_CC_1200:
+    case T38_DATA_V34_PRI_CH:
+    case T38_DATA_V33_12000:
+    case T38_DATA_V33_14400:
+    default:
+        break;
+    }
+#endif
+    switch (field_type)
+    {
+    case T38_FIELD_HDLC_DATA:
+        if (fe->timeout_rx_samples == 0)
+        {
+            /* HDLC can just start without any signal indicator on some platforms, even when
+               there is zero packet lost. Nasty, but true. Its a good idea to be tolerant of
+               loss, though, so accepting a sudden start of HDLC data is the right thing to do. */
+            fe->timeout_rx_samples = fe->samples + ms_to_samples(MID_RX_TIMEOUT);
+            /* TODO: report signal present */
+            /* All real HDLC messages in the FAX world start with 0xFF. If this one is not starting
+               with 0xFF it would appear some octets must have been missed before this one. */
+            if (buf[0] != 0xFF)
+                fe->missing_data = TRUE;
+        }
+        if (fe->hdlc_rx.len + len <= T31_T38_MAX_HDLC_LEN)
+        {
+            bit_reverse(fe->hdlc_rx.buf + fe->hdlc_rx.len, buf, len);
+            fe->hdlc_rx.len += len;
+        }
+        fe->timeout_rx_samples = fe->samples + ms_to_samples(MID_RX_TIMEOUT);
+        break;
+    case T38_FIELD_HDLC_FCS_OK:
+        if (len > 0)
+        {
+            span_log(&s->logging, SPAN_LOG_WARNING, "There is data in a T38_FIELD_HDLC_FCS_OK!\n");
+            /* The sender has incorrectly included data in this message. It is unclear what we should do
+               with it, to maximise tolerance of buggy implementations. */
+        }
+        /* Some T.38 implementations send multiple T38_FIELD_HDLC_FCS_OK messages, in IFP packets with
+           incrementing sequence numbers, which are actually repeats. They get through to this point because
+           of the incrementing sequence numbers. We need to filter them here in a context sensitive manner. */
+        if (t->current_rx_data_type != data_type  ||  t->current_rx_field_type != field_type)
+        {
+            span_log(&s->logging, SPAN_LOG_FLOW, "Type %s - CRC OK (%s)\n", (fe->hdlc_rx.len >= 3)  ?  t30_frametype(fe->hdlc_rx.buf[2])  :  "???", (fe->missing_data)  ?  "missing octets"  :  "clean");
+            hdlc_accept((void *) s, fe->hdlc_rx.buf, fe->hdlc_rx.len, !fe->missing_data);
+        }
+        fe->hdlc_rx.len = 0;
+        fe->missing_data = FALSE;
+        break;
+    case T38_FIELD_HDLC_FCS_BAD:
+        if (len > 0)
+        {
+            span_log(&s->logging, SPAN_LOG_WARNING, "There is data in a T38_FIELD_HDLC_FCS_BAD!\n");
+            /* The sender has incorrectly included data in this message. We can safely ignore it, as the
+               bad FCS means we will throw away the whole message, anyway. */
+        }
+        /* Some T.38 implementations send multiple T38_FIELD_HDLC_FCS_BAD messages, in IFP packets with
+           incrementing sequence numbers, which are actually repeats. They get through to this point because
+           of the incrementing sequence numbers. We need to filter them here in a context sensitive manner. */
+        if (t->current_rx_data_type != data_type  ||  t->current_rx_field_type != field_type)
+        {
+            span_log(&s->logging, SPAN_LOG_FLOW, "Type %s - CRC bad (%s)\n", (fe->hdlc_rx.len >= 3)  ?  t30_frametype(fe->hdlc_rx.buf[2])  :  "???", (fe->missing_data)  ?  "missing octets"  :  "clean");
+            hdlc_accept((void *) s, fe->hdlc_rx.buf, fe->hdlc_rx.len, FALSE);
+        }
+        fe->hdlc_rx.len = 0;
+        fe->missing_data = FALSE;
+        break;
+    case T38_FIELD_HDLC_FCS_OK_SIG_END:
+        if (len > 0)
+        {
+            span_log(&s->logging, SPAN_LOG_WARNING, "There is data in a T38_FIELD_HDLC_FCS_OK_SIG_END!\n");
+            /* The sender has incorrectly included data in this message. It is unclear what we should do
+               with it, to maximise tolerance of buggy implementations. */
+        }
+        span_log(&s->logging, SPAN_LOG_FLOW, "Type %s - CRC OK, sig end (%s)\n", t30_frametype(s->tx.data[2]), (fe->missing_data)  ?  "missing octets"  :  "clean");
+        if (fe->current_rx_type == T31_V21_RX)
+        {
+            /* Don't deal with zero length frames. Some T.38 implementations send multiple T38_FIELD_HDLC_FCS_OK
+               packets, when they have sent no data for the body of the frame. */
+            if (s->tx.out_bytes > 0)
+                hdlc_accept((void *) s, fe->hdlc_rx.buf, fe->hdlc_rx.len, TRUE);
+            hdlc_accept((void *) s, NULL, PUTBIT_CARRIER_DOWN, TRUE);
+        }
+        s->tx.out_bytes = 0;
+        fe->missing_data = FALSE;
+        fe->hdlc_rx.len = 0;
+        break;
+    case T38_FIELD_HDLC_FCS_BAD_SIG_END:
+        if (len > 0)
+            span_log(&s->logging, SPAN_LOG_WARNING, "There is data in a T38_FIELD_HDLC_FCS_BAD_SIG_END!\n");
+        span_log(&s->logging, SPAN_LOG_FLOW, "Type %s - CRC bad, sig end (%s)\n", t30_frametype(s->tx.data[2]), (fe->missing_data)  ?  "missing octets"  :  "clean");
+        if (fe->current_rx_type == T31_V21_RX)
+            hdlc_accept((void *) s, NULL, PUTBIT_CARRIER_DOWN, TRUE);
+        fe->hdlc_rx.len = 0;
+        fe->missing_data = FALSE;
+        break;
+    case T38_FIELD_HDLC_SIG_END:
+        if (len > 0)
+            span_log(&s->logging, SPAN_LOG_WARNING, "There is data in a T38_FIELD_HDLC_SIG_END!\n");
+        /* This message is expected under 2 circumstances. One is as an alternative to T38_FIELD_HDLC_FCS_OK_SIG_END - 
+           i.e. they send T38_FIELD_HDLC_FCS_OK, and then T38_FIELD_HDLC_SIG_END when the carrier actually drops.
+           The other is because the HDLC signal drops unexpectedly - i.e. not just after a final frame. */
+        if (fe->current_rx_type == T31_V21_RX)
+            hdlc_accept((void *) s, NULL, PUTBIT_CARRIER_DOWN, TRUE);
+        fe->hdlc_rx.len = 0;
+        fe->missing_data = FALSE;
+        break;
+    case T38_FIELD_T4_NON_ECM_DATA:
+        if (!s->at_state.rx_signal_present)
+        {
+            /* TODO: report training succeeded */
+            s->at_state.rx_signal_present = TRUE;
+        }
+        bit_reverse(buf2, buf, len);
+        /* TODO: put the chunk */
+        fe->timeout_rx_samples = fe->samples + ms_to_samples(MID_RX_TIMEOUT);
+        break;
+    case T38_FIELD_T4_NON_ECM_SIG_END:
+        /* Some T.38 implementations send multiple T38_FIELD_T4_NON_ECM_SIG_END messages, in IFP packets with
+           incrementing sequence numbers, which are actually repeats. They get through to this point because
+           of the incrementing sequence numbers. We need to filter them here in a context sensitive manner. */
+        if (t->current_rx_data_type != data_type  ||  t->current_rx_field_type != field_type)
+        {
+            if (len > 0)
+            {
+                if (!s->at_state.rx_signal_present)
+                {
+                    /* TODO: report training succeeded */
+                    s->at_state.rx_signal_present = TRUE;
+                }
+                bit_reverse(buf2, buf, len);
+                /* TODO: put the chunk */
+            }
+            /* WORKAROUND: At least some Mediatrix boxes have a bug, where they can send HDLC signal end where
+                           they should send non-ECM signal end. It is possible they also do the opposite.
+                           We need to tolerate this, so we use the generic receive complete
+                           indication, rather than the specific non-ECM carrier down. */
+            /* TODO: report receive complete */
+        }
+        s->at_state.rx_signal_present = FALSE;
+        fe->timeout_rx_samples = 0;
+        break;
+    case T38_FIELD_CM_MESSAGE:
+    case T38_FIELD_JM_MESSAGE:
+    case T38_FIELD_CI_MESSAGE:
+    case T38_FIELD_V34RATE:
+    default:
+        break;
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int t31_t38_send_timeout(t31_state_t *s, int samples)
+{
+    int len;
+    int i;
+    int previous;
+    uint8_t buf[MAX_OCTETS_PER_UNPACED_CHUNK + 50];
+    t31_t38_front_end_state_t *fe;
+    t38_data_field_t data_fields[2];
+    /* The times for training, the optional TEP, and the HDLC preamble, for all the modem options, in ms.
+       Note that the preamble for V.21 is 1s+-15%, and for the other modems is 200ms+100ms. */
+    static const struct
+    {
+        int tep;
+        int training;
+        int flags;
+    } startup_time[] =
+    {
+        {   0,    0,    0},     /* T38_IND_NO_SIGNAL */
+        {   0,    0,    0},     /* T38_IND_CNG */
+        {   0,    0,    0},     /* T38_IND_CED */
+        {   0,    0, 1000},     /* T38_IND_V21_PREAMBLE */ /* TODO: 850ms should be OK for this, but it causes trouble with some ATAs. Why? */
+        { 215,  943,  200},     /* T38_IND_V27TER_2400_TRAINING */
+        { 215,  708,  200},     /* T38_IND_V27TER_4800_TRAINING */
+        { 215,  234,  200},     /* T38_IND_V29_7200_TRAINING */
+        { 215,  234,  200},     /* T38_IND_V29_9600_TRAINING */
+        { 215,  142,  200},     /* T38_IND_V17_7200_SHORT_TRAINING */
+        { 215, 1393,  200},     /* T38_IND_V17_7200_LONG_TRAINING */
+        { 215,  142,  200},     /* T38_IND_V17_9600_SHORT_TRAINING */
+        { 215, 1393,  200},     /* T38_IND_V17_9600_LONG_TRAINING */
+        { 215,  142,  200},     /* T38_IND_V17_12000_SHORT_TRAINING */
+        { 215, 1393,  200},     /* T38_IND_V17_12000_LONG_TRAINING */
+        { 215,  142,  200},     /* T38_IND_V17_14400_SHORT_TRAINING */
+        { 215, 1393,  200},     /* T38_IND_V17_14400_LONG_TRAINING */
+        { 215,    0,    0},     /* T38_IND_V8_ANSAM */
+        { 215,    0,    0},     /* T38_IND_V8_SIGNAL */
+        { 215,    0,    0},     /* T38_IND_V34_CNTL_CHANNEL_1200 */
+        { 215,    0,    0},     /* T38_IND_V34_PRI_CHANNEL */
+        { 215,    0,    0},     /* T38_IND_V34_CC_RETRAIN */
+        { 215,    0,    0},     /* T38_IND_V33_12000_TRAINING */
+        { 215,    0,    0}      /* T38_IND_V33_14400_TRAINING */
+    };
+
+    fe = &s->t38_fe;
+    if (fe->current_rx_type == T30_MODEM_DONE  ||  fe->current_tx_type == T30_MODEM_DONE)
+        return TRUE;
+
+    fe->samples += samples;
+    if (fe->timeout_rx_samples  &&  fe->samples > fe->timeout_rx_samples)
+    {
+        span_log(&s->logging, SPAN_LOG_FLOW, "Timeout mid-receive\n");
+        fe->timeout_rx_samples = 0;
+        /* TODO: report completion */
+    }
+    if (fe->timed_step == T38_TIMED_STEP_NONE)
+        return FALSE;
+    if (fe->samples < fe->next_tx_samples)
+        return FALSE;
+    /* Its time to send something */
+    switch (fe->timed_step)
+    {
+    case T38_TIMED_STEP_NON_ECM_MODEM:
+        /* Create a 75ms silence */
+        if (fe->t38.current_tx_indicator != T38_IND_NO_SIGNAL)
+            t38_core_send_indicator(&fe->t38, T38_IND_NO_SIGNAL, fe->t38.indicator_tx_count);
+        fe->timed_step = T38_TIMED_STEP_NON_ECM_MODEM_2;
+        fe->next_tx_samples += ms_to_samples(75);
+        break;
+    case T38_TIMED_STEP_NON_ECM_MODEM_2:
+        /* Switch on a fast modem, and give the training time to complete */
+        t38_core_send_indicator(&fe->t38, fe->next_tx_indicator, fe->t38.indicator_tx_count);
+        fe->timed_step = T38_TIMED_STEP_NON_ECM_MODEM_3;
+        fe->next_tx_samples += ms_to_samples(startup_time[fe->next_tx_indicator].training);
+        if (s->audio.modems.use_tep)
+            fe->next_tx_samples += ms_to_samples(startup_time[fe->next_tx_indicator].tep);
+        break;
+    case T38_TIMED_STEP_NON_ECM_MODEM_3:
+        /* Send a chunk of non-ECM image data */
+        /* T.38 says it is OK to send the last of the non-ECM data in the signal end message.
+           However, I think the early versions of T.38 said the signal end message should not
+           contain data. Hopefully, following the current spec will not cause compatibility
+           issues. */
+        /* Get a chunk of data */
+        len = fe->octets_per_data_packet;
+        bit_reverse(buf, buf, len);
+        if (len < fe->octets_per_data_packet)
+        {
+            /* That's the end of the image data. Do a little padding now */
+            memset(buf + len, 0, fe->octets_per_data_packet - len);
+            fe->trailer_bytes = 3*fe->octets_per_data_packet + len;
+            len = fe->octets_per_data_packet;
+            fe->timed_step = T38_TIMED_STEP_NON_ECM_MODEM_4;
+        }
+        t38_core_send_data(&fe->t38, fe->current_tx_data_type, T38_FIELD_T4_NON_ECM_DATA, buf, len, DATA_TX_COUNT);
+        fe->next_tx_samples += ms_to_samples(fe->ms_per_tx_chunk);
+        break;
+    case T38_TIMED_STEP_NON_ECM_MODEM_4:
+        /* This pads the end of the data with some zeros. If we just stop abruptly
+           at the end of the EOLs, some ATAs fail to clean up properly before
+           shutting down their transmit modem, and the last few rows of the image
+           get corrupted. Simply delaying the no-signal message does not help for
+           all implentations. It often appears to be ignored. */
+        len = fe->octets_per_data_packet;
+        fe->trailer_bytes -= len;
+        if (fe->trailer_bytes <= 0)
+        {
+            len += fe->trailer_bytes;
+            memset(buf, 0, len);
+            t38_core_send_data(&fe->t38, fe->current_tx_data_type, T38_FIELD_T4_NON_ECM_SIG_END, buf, len, fe->data_end_tx_count);
+            fe->timed_step = T38_TIMED_STEP_NON_ECM_MODEM_5;
+            fe->next_tx_samples += ms_to_samples(60);
+            break;
+        }
+        memset(buf, 0, len);
+        t38_core_send_data(&fe->t38, fe->current_tx_data_type, T38_FIELD_T4_NON_ECM_DATA, buf, len, DATA_TX_COUNT);
+        fe->next_tx_samples += ms_to_samples(fe->ms_per_tx_chunk);
+        break;
+    case T38_TIMED_STEP_NON_ECM_MODEM_5:
+        /* This should not be needed, since the message above indicates the end of the signal, but it
+           seems like it can improve compatibility with quirky implementations. */
+        t38_core_send_indicator(&fe->t38, T38_IND_NO_SIGNAL, fe->t38.indicator_tx_count);
+        fe->timed_step = T38_TIMED_STEP_NONE;
+        /* TODO: report send complete */
+        break;
+    case T38_TIMED_STEP_HDLC_MODEM:
+        /* Send HDLC preambling */
+        t38_core_send_indicator(&fe->t38, fe->next_tx_indicator, fe->t38.indicator_tx_count);
+        fe->next_tx_samples += ms_to_samples(startup_time[fe->next_tx_indicator].training + startup_time[fe->next_tx_indicator].flags);
+        if (s->audio.modems.use_tep)
+            fe->next_tx_samples += ms_to_samples(startup_time[fe->next_tx_indicator].tep);
+        fe->timed_step = T38_TIMED_STEP_HDLC_MODEM_2;
+        break;
+    case T38_TIMED_STEP_HDLC_MODEM_2:
+        /* Send a chunk of HDLC data */
+        i = s->hdlc_tx.len - s->hdlc_tx.ptr;
+        if (fe->octets_per_data_packet >= i)
+        {
+            /* The last part of the HDLC frame */
+            if (fe->merge_tx_fields)
+            {
+                /* Copy the data, as we might be about to refill the buffer it is in */
+                memcpy(buf, &s->hdlc_tx.buf[s->hdlc_tx.ptr], i);
+                data_fields[0].field_type = T38_FIELD_HDLC_DATA;
+                data_fields[0].field = buf;
+                data_fields[0].field_len = i;
+
+                /* Now see about the next HDLC frame. This will tell us whether to send FCS_OK or FCS_OK_SIG_END */
+                previous = fe->current_tx_data_type;
+                s->hdlc_tx.ptr = 0;
+                s->hdlc_tx.len = 0;
+                /* TODO: report completion */
+                /* The above step should have got the next HDLC step ready - either another frame, or an instruction to stop transmission. */
+                if (s->hdlc_tx.len < 0)
+                {
+                    data_fields[1].field_type = T38_FIELD_HDLC_FCS_OK_SIG_END;
+                    fe->timed_step = T38_TIMED_STEP_HDLC_MODEM_4;
+                }
+                else
+                {
+                    data_fields[1].field_type = T38_FIELD_HDLC_FCS_OK;
+                    fe->timed_step = T38_TIMED_STEP_HDLC_MODEM_2;
+                }
+                data_fields[1].field = NULL;
+                data_fields[1].field_len = 0;
+                t38_core_send_data_multi_field(&fe->t38, fe->current_tx_data_type, data_fields, 2, DATA_TX_COUNT);
+            }
+            else
+            {
+                t38_core_send_data(&fe->t38, fe->current_tx_data_type, T38_FIELD_HDLC_DATA, &s->hdlc_tx.buf[s->hdlc_tx.ptr], i, DATA_TX_COUNT);
+                fe->timed_step = T38_TIMED_STEP_HDLC_MODEM_3;
+            }
+            fe->next_tx_samples += ms_to_samples(fe->ms_per_tx_chunk);
+            break;
+        }
+        t38_core_send_data(&fe->t38, fe->current_tx_data_type, T38_FIELD_HDLC_DATA, &s->hdlc_tx.buf[s->hdlc_tx.ptr], fe->octets_per_data_packet, DATA_TX_COUNT);
+        s->hdlc_tx.ptr += fe->octets_per_data_packet;
+        fe->next_tx_samples += ms_to_samples(fe->ms_per_tx_chunk);
+        break;
+    case T38_TIMED_STEP_HDLC_MODEM_3:
+        /* End of HDLC frame */
+        previous = fe->current_tx_data_type;
+        s->hdlc_tx.ptr = 0;
+        s->hdlc_tx.len = 0;
+        /* TODO: report completion */
+        /* The above step should have got the next HDLC step ready - either another frame, or an instruction to stop transmission. */
+        if (s->hdlc_tx.len < 0)
+        {
+            t38_core_send_data(&fe->t38, previous, T38_FIELD_HDLC_FCS_OK_SIG_END, NULL, 0, fe->data_end_tx_count);
+            fe->timed_step = T38_TIMED_STEP_HDLC_MODEM_4;
+            fe->next_tx_samples += ms_to_samples(100);
+            break;
+        }
+        t38_core_send_data(&fe->t38, previous, T38_FIELD_HDLC_FCS_OK, NULL, 0, DATA_TX_COUNT);
+        if (s->hdlc_tx.len)
+            fe->timed_step = T38_TIMED_STEP_HDLC_MODEM_2;
+        fe->next_tx_samples += ms_to_samples(fe->ms_per_tx_chunk);
+        break;
+    case T38_TIMED_STEP_HDLC_MODEM_4:
+        /* Note that some boxes do not like us sending a T38_FIELD_HDLC_SIG_END at this point.
+           A T38_IND_NO_SIGNAL should always be OK. */
+        t38_core_send_indicator(&fe->t38, T38_IND_NO_SIGNAL, fe->t38.indicator_tx_count);
+        s->hdlc_tx.len = 0;
+        //t30_front_end_status(&(s->t30), T30_FRONT_END_SEND_STEP_COMPLETE);
+        break;
+    case T38_TIMED_STEP_CED:
+        /* It seems common practice to start with a no signal indicator, though
+           this is not a specified requirement. Since we should be sending 200ms
+           of silence, starting the delay with a no signal indication makes sense.
+           We do need a 200ms delay, as that is a specification requirement. */
+        fe->timed_step = T38_TIMED_STEP_CED_2;
+        fe->next_tx_samples = fe->samples + ms_to_samples(200);
+        t38_core_send_indicator(&fe->t38, T38_IND_NO_SIGNAL, fe->t38.indicator_tx_count);
+        fe->current_tx_data_type = T38_DATA_NONE;
+        break;
+    case T38_TIMED_STEP_CED_2:
+        /* Initial 200ms delay over. Send the CED indicator */
+        fe->next_tx_samples = fe->samples + ms_to_samples(3000);
+        fe->timed_step = T38_TIMED_STEP_PAUSE;
+        t38_core_send_indicator(&fe->t38, T38_IND_CED, fe->t38.indicator_tx_count);
+        fe->current_tx_data_type = T38_DATA_NONE;
+        break;
+    case T38_TIMED_STEP_CNG:
+        /* It seems common practice to start with a no signal indicator, though
+           this is not a specified requirement. Since we should be sending 200ms
+           of silence, starting the delay with a no signal indication makes sense.
+           We do need a 200ms delay, as that is a specification requirement. */
+        fe->timed_step = T38_TIMED_STEP_CNG_2;
+        fe->next_tx_samples = fe->samples + ms_to_samples(200);
+        t38_core_send_indicator(&fe->t38, T38_IND_NO_SIGNAL, fe->t38.indicator_tx_count);
+        fe->current_tx_data_type = T38_DATA_NONE;
+        break;
+    case T38_TIMED_STEP_CNG_2:
+        /* Initial short delay over. Send the CNG indicator */
+        fe->timed_step = T38_TIMED_STEP_NONE;
+        t38_core_send_indicator(&fe->t38, T38_IND_CNG, fe->t38.indicator_tx_count);
+        fe->current_tx_data_type = T38_DATA_NONE;
+        break;
+    case T38_TIMED_STEP_PAUSE:
+        /* End of timed pause */
+        fe->timed_step = T38_TIMED_STEP_NONE;
+        //t30_front_end_status(&(s->t30), T30_FRONT_END_SEND_STEP_COMPLETE);
+        break;
+    }
+    return FALSE;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int t31_modem_control_handler(at_state_t *s, void *user_data, int op, const char *num)
+{
+    t31_state_t *t;
+    
+    t = (t31_state_t *) user_data;
+    switch (op)
+    {
+    case AT_MODEM_CONTROL_CALL:
+        t->call_samples = 0;
+        break;
+    case AT_MODEM_CONTROL_ANSWER:
+        t->call_samples = 0;
+        break;
+    case AT_MODEM_CONTROL_ONHOOK:
+        if (t->tx.holding)
+        {
+            t->tx.holding = FALSE;
+            /* Tell the application to release further data */
+            at_modem_control(&t->at_state, AT_MODEM_CONTROL_CTS, (void *) 1);
+        }
+        if (t->at_state.rx_signal_present)
+        {
+            t->at_state.rx_data[t->at_state.rx_data_bytes++] = DLE;
+            t->at_state.rx_data[t->at_state.rx_data_bytes++] = ETX;
+            t->at_state.at_tx_handler(&t->at_state, t->at_state.at_tx_user_data, t->at_state.rx_data, t->at_state.rx_data_bytes);
+            t->at_state.rx_data_bytes = 0;
+        }
+        restart_modem(t, T31_SILENCE_TX);
+        break;
+    case AT_MODEM_CONTROL_RESTART:
+        restart_modem(t, (int) (intptr_t) num);
+        return 0;
+    case AT_MODEM_CONTROL_DTE_TIMEOUT:
+        if (num)
+            t->dte_data_timeout = t->call_samples + ms_to_samples((intptr_t) num);
+        else
+            t->dte_data_timeout = 0;
+        return 0;
+    }
+    return t->modem_control_handler(t, t->modem_control_user_data, op, num);
+}
+/*- End of function --------------------------------------------------------*/
+
+static void non_ecm_rx_status(void *user_data, int status)
+{
+    t31_state_t *s;
+
+    s = (t31_state_t *) user_data;
+    switch (status)
+    {
+    case PUTBIT_TRAINING_IN_PROGRESS:
+        break;
+    case PUTBIT_TRAINING_FAILED:
+        s->at_state.rx_trained = FALSE;
+        break;
+    case PUTBIT_TRAINING_SUCCEEDED:
+        /* The modem is now trained */
+        at_put_response_code(&s->at_state, AT_RESPONSE_CODE_CONNECT);
+        s->at_state.rx_signal_present = TRUE;
+        s->at_state.rx_trained = TRUE;
+        break;
+    case PUTBIT_CARRIER_UP:
+        break;
+    case PUTBIT_CARRIER_DOWN:
+        if (s->at_state.rx_signal_present)
+        {
+            s->at_state.rx_data[s->at_state.rx_data_bytes++] = DLE;
+            s->at_state.rx_data[s->at_state.rx_data_bytes++] = ETX;
+            s->at_state.at_tx_handler(&s->at_state, s->at_state.at_tx_user_data, s->at_state.rx_data, s->at_state.rx_data_bytes);
+            s->at_state.rx_data_bytes = 0;
+            at_put_response_code(&s->at_state, AT_RESPONSE_CODE_NO_CARRIER);
+            t31_set_at_rx_mode(s, AT_MODE_OFFHOOK_COMMAND);
+        }
+        s->at_state.rx_signal_present = FALSE;
+        s->at_state.rx_trained = FALSE;
+        break;
+    default:
+        if (s->at_state.p.result_code_format)
+            span_log(&s->logging, SPAN_LOG_FLOW, "Eh!\n");
+        break;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static void non_ecm_put_bit(void *user_data, int bit)
+{
+    t31_state_t *s;
+
+    if (bit < 0)
+    {
+        non_ecm_rx_status(user_data, bit);
+        return;
+    }
+    s = (t31_state_t *) user_data;
+    s->audio.current_byte = (s->audio.current_byte >> 1) | (bit << 7);
+    if (++s->audio.bit_no >= 8)
+    {
+        if (s->audio.current_byte == DLE)
+            s->at_state.rx_data[s->at_state.rx_data_bytes++] = DLE;
+        s->at_state.rx_data[s->at_state.rx_data_bytes++] = (uint8_t) s->audio.current_byte;
+        if (s->at_state.rx_data_bytes >= 250)
+        {
+            s->at_state.at_tx_handler(&s->at_state, s->at_state.at_tx_user_data, s->at_state.rx_data, s->at_state.rx_data_bytes);
+            s->at_state.rx_data_bytes = 0;
+        }
+        s->audio.bit_no = 0;
+        s->audio.current_byte = 0;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static int non_ecm_get_bit(void *user_data)
+{
+    t31_state_t *s;
+    int bit;
+
+    s = (t31_state_t *) user_data;
+    if (s->audio.bit_no <= 0)
+    {
+        if (s->tx.out_bytes != s->tx.in_bytes)
+        {
+            /* There is real data available to send */
+            s->audio.current_byte = s->tx.data[s->tx.out_bytes++];
+            if (s->tx.out_bytes > T31_TX_BUF_LEN - 1)
+            {
+                s->tx.out_bytes = T31_TX_BUF_LEN - 1;
+                fprintf(stderr, "End of transmit buffer reached!\n");
+            }
+            if (s->tx.holding)
+            {
+                /* See if the buffer is approaching empty. It might be time to release flow control. */
+                if (s->tx.out_bytes > 1024)
+                {
+                    s->tx.holding = FALSE;
+                    /* Tell the application to release further data */
+                    at_modem_control(&s->at_state, AT_MODEM_CONTROL_CTS, (void *) 1);
+                }
+            }
+            s->tx.data_started = TRUE;
+        }
+        else
+        {
+            if (s->tx.final)
+            {
+                s->tx.final = FALSE;
+                /* This will put the modem into its shutdown sequence. When
+                   it has finally shut down, an OK response will be sent. */
+                return PUTBIT_END_OF_DATA;
+            }
+            /* Fill with 0xFF bytes at the start of transmission, or 0x00 if we are in
+               the middle of transmission. This follows T.31 and T.30 practice. */
+            s->audio.current_byte = (s->tx.data_started)  ?  0x00  :  0xFF;
+        }
+        s->audio.bit_no = 8;
+    }
+    s->audio.bit_no--;
+    bit = s->audio.current_byte & 1;
+    s->audio.current_byte >>= 1;
+    return bit;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void tone_detected(void *user_data, int on, int level, int delay)
+{
+    t31_state_t *s;
+
+    s = (t31_state_t *) user_data;
+    span_log(&s->logging, SPAN_LOG_FLOW, "FAX tone declared %s (%ddBm0)\n", (on)  ?  "on"  :  "off", level);
+}
+/*- End of function --------------------------------------------------------*/
+
+static void hdlc_tx_underflow(void *user_data)
+{
+    t31_state_t *s;
+
+    s = (t31_state_t *) user_data;
+    if (s->hdlc_tx.final)
+    {
+        s->hdlc_tx.final = FALSE;
+        /* Schedule an orderly shutdown of the modem */
+        hdlc_tx_frame(&(s->audio.modems.hdlc_tx), NULL, 0);
+    }
+    else
+    {
+        at_put_response_code(&s->at_state, AT_RESPONSE_CODE_CONNECT);
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static void hdlc_rx_status(void *user_data, int status)
+{
+    t31_state_t *s;
+    uint8_t buf[2];
+
+    s = (t31_state_t *) user_data;
+    switch (status)
+    {
+    case PUTBIT_TRAINING_IN_PROGRESS:
+        break;
+    case PUTBIT_TRAINING_FAILED:
+        s->at_state.rx_trained = FALSE;
+        break;
+    case PUTBIT_TRAINING_SUCCEEDED:
+        /* The modem is now trained */
+        s->at_state.rx_signal_present = TRUE;
+        s->at_state.rx_trained = TRUE;
+        break;
+    case PUTBIT_CARRIER_UP:
+        if (s->modem == T31_CNG_TONE  ||  s->modem == T31_NOCNG_TONE  ||  s->modem == T31_V21_RX)
+        {
+            s->at_state.rx_signal_present = TRUE;
+            s->rx_frame_received = FALSE;
+        }
+        break;
+    case PUTBIT_CARRIER_DOWN:
+        if (s->rx_frame_received)
+        {
+            if (s->at_state.dte_is_waiting)
+            {
+                if (s->at_state.ok_is_pending)
+                {
+                    at_put_response_code(&s->at_state, AT_RESPONSE_CODE_OK);
+                    s->at_state.ok_is_pending = FALSE;
+                }
+                else
+                {
+                    at_put_response_code(&s->at_state, AT_RESPONSE_CODE_NO_CARRIER);
+                }
+                s->at_state.dte_is_waiting = FALSE;
+                t31_set_at_rx_mode(s, AT_MODE_OFFHOOK_COMMAND);
+            }
+            else
+            {
+                buf[0] = AT_RESPONSE_CODE_NO_CARRIER;
+                queue_write_msg(s->rx_queue, buf, 1);
+            }
+        }
+        s->at_state.rx_signal_present = FALSE;
+        s->at_state.rx_trained = FALSE;
+        break;
+    case PUTBIT_FRAMING_OK:
+        if (s->modem == T31_CNG_TONE  ||  s->modem == T31_NOCNG_TONE)
+        {
+            /* Once we get any valid HDLC the CNG tone stops, and we drop
+               to the V.21 receive modem on its own. */
+            s->modem = T31_V21_RX;
+            s->at_state.transmit = FALSE;
+        }
+        if (s->modem == T31_V17_RX  ||  s->modem == T31_V27TER_RX  ||  s->modem == T31_V29_RX)
+        {
+            /* V.21 has been detected while expecting a different carrier.
+               If +FAR=0 then result +FCERROR and return to command-mode.
+               If +FAR=1 then report +FRH:3 and CONNECT, switching to
+               V.21 receive mode. */
+            if (s->at_state.p.adaptive_receive)
+            {
+                s->at_state.rx_signal_present = TRUE;
+                s->rx_frame_received = TRUE;
+                s->modem = T31_V21_RX;
+                s->at_state.transmit = FALSE;
+                s->at_state.dte_is_waiting = TRUE;
+                at_put_response_code(&s->at_state, AT_RESPONSE_CODE_FRH3);
+                at_put_response_code(&s->at_state, AT_RESPONSE_CODE_CONNECT);
+            }
+            else
+            {
+                s->modem = T31_SILENCE_TX;
+                t31_set_at_rx_mode(s, AT_MODE_OFFHOOK_COMMAND);
+                s->rx_frame_received = FALSE;
+                at_put_response_code(&s->at_state, AT_RESPONSE_CODE_FCERROR);
+            }
+        }
+        else
+        {
+            if (!s->rx_frame_received)
+            {
+                if (s->at_state.dte_is_waiting)
+                {
+                    /* Report CONNECT as soon as possible to avoid a timeout. */
+                    at_put_response_code(&s->at_state, AT_RESPONSE_CODE_CONNECT);
+                    s->rx_frame_received = TRUE;
+                }
+                else
+                {
+                    buf[0] = AT_RESPONSE_CODE_CONNECT;
+                    queue_write_msg(s->rx_queue, buf, 1);
+                }
+            }
+        }
+        break;
+    case PUTBIT_ABORT:
+        /* Just ignore these */
+        break;
+    default:
+        span_log(&s->logging, SPAN_LOG_WARNING, "Unexpected HDLC rx status - %d!\n", status);
+        break;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static void hdlc_accept(void *user_data, const uint8_t *msg, int len, int ok)
+{
+    t31_state_t *s;
+    uint8_t buf[256];
+    int i;
+
+    if (len < 0)
+    {
+        hdlc_rx_status(user_data, len);
+        return;
+    }
+    s = (t31_state_t *) user_data;
+    if (!s->rx_frame_received)
+    {
+        if (s->at_state.dte_is_waiting)
+        {
+            /* Report CONNECT as soon as possible to avoid a timeout. */
+            at_put_response_code(&s->at_state, AT_RESPONSE_CODE_CONNECT);
+            s->rx_frame_received = TRUE;
+        }
+        else
+        {
+            buf[0] = AT_RESPONSE_CODE_CONNECT;
+            queue_write_msg(s->rx_queue, buf, 1);
+        }
+    }
+    /* If OK is pending then we just ignore whatever comes in */
+    if (!s->at_state.ok_is_pending)
+    {
+        if (s->at_state.dte_is_waiting)
+        {
+            /* Send straight away */
+            /* It is safe to look at the two bytes beyond the length of the message,
+               and expect to find the FCS there. */
+            for (i = 0;  i < len + 2;  i++)
+            {
+                if (msg[i] == DLE)
+                    s->at_state.rx_data[s->at_state.rx_data_bytes++] = DLE;
+                s->at_state.rx_data[s->at_state.rx_data_bytes++] = msg[i];
+            }
+            s->at_state.rx_data[s->at_state.rx_data_bytes++] = DLE;
+            s->at_state.rx_data[s->at_state.rx_data_bytes++] = ETX;
+            s->at_state.at_tx_handler(&s->at_state, s->at_state.at_tx_user_data, s->at_state.rx_data, s->at_state.rx_data_bytes);
+            s->at_state.rx_data_bytes = 0;
+            if (msg[1] == 0x13  &&  ok)
+            {
+                /* This is the last frame.  We don't send OK until the carrier drops to avoid
+                   redetecting it later. */
+                s->at_state.ok_is_pending = TRUE;
+            }
+            else
+            {
+                at_put_response_code(&s->at_state, (ok)  ?  AT_RESPONSE_CODE_OK  :  AT_RESPONSE_CODE_ERROR);
+                s->at_state.dte_is_waiting = FALSE;
+                s->rx_frame_received = FALSE;
+            }
+        }
+        else
+        {
+            /* Queue it */
+            buf[0] = (ok)  ?  AT_RESPONSE_CODE_OK  :  AT_RESPONSE_CODE_ERROR;
+            /* It is safe to look at the two bytes beyond the length of the message,
+               and expect to find the FCS there. */
+            memcpy(buf + 1, msg, len + 2);
+            queue_write_msg(s->rx_queue, buf, len + 3);
+        }
+    }
+    t31_set_at_rx_mode(s, AT_MODE_OFFHOOK_COMMAND);
+}
+/*- End of function --------------------------------------------------------*/
+
+static void t31_v21_rx(t31_state_t *s)
+{
+    hdlc_rx_init(&(s->audio.modems.hdlc_rx), FALSE, TRUE, HDLC_FRAMING_OK_THRESHOLD, hdlc_accept, s);
+    s->at_state.ok_is_pending = FALSE;
+    s->hdlc_tx.final = FALSE;
+    s->hdlc_tx.len = 0;
+    s->dled = FALSE;
+    fsk_rx_init(&(s->audio.modems.v21_rx), &preset_fsk_specs[FSK_V21CH2], TRUE, (put_bit_func_t) hdlc_rx_put_bit, &(s->audio.modems.hdlc_rx));
+    fsk_rx_signal_cutoff(&(s->audio.modems.v21_rx), -39.09);
+    s->at_state.transmit = TRUE;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int restart_modem(t31_state_t *s, int new_modem)
+{
+    int ind;
+    fax_modems_state_t *t;
+
+    t = &s->audio.modems;
+    span_log(&s->logging, SPAN_LOG_FLOW, "Restart modem %d\n", new_modem);
+    if (s->modem == new_modem)
+        return 0;
+    queue_flush(s->rx_queue);
+    s->modem = new_modem;
+    s->tx.final = FALSE;
+    s->at_state.rx_signal_present = FALSE;
+    s->at_state.rx_trained = FALSE;
+    s->rx_frame_received = FALSE;
+    t->rx_handler = (span_rx_handler_t *) &span_dummy_rx;
+    t->rx_user_data = NULL;
+    switch (s->modem)
+    {
+    case T31_CNG_TONE:
+        if (s->t38_mode)
+        {
+            t38_core_send_indicator(&s->t38_fe.t38, T38_IND_CNG, INDICATOR_TX_COUNT);
+        }
+        else
+        {
+            modem_connect_tones_tx_init(&t->connect_tx, MODEM_CONNECT_TONES_FAX_CNG);
+            /* CNG is special, since we need to receive V.21 HDLC messages while sending the
+               tone. Everything else in FAX processing sends only one way at a time. */
+            /* Do V.21/HDLC receive in parallel. The other end may send its
+               first message at any time. The CNG tone will continue until
+               we get a valid preamble. */
+            t->rx_handler = (span_rx_handler_t *) &cng_rx;
+            t->rx_user_data = s;
+            t31_v21_rx(s);
+            t->tx_handler = (span_tx_handler_t *) &modem_connect_tones_tx;
+            t->tx_user_data = &t->connect_tx;
+            s->audio.next_tx_handler = NULL;
+        }
+        s->at_state.transmit = TRUE;
+        break;
+    case T31_NOCNG_TONE:
+        if (s->t38_mode)
+        {
+        }
+        else
+        {
+            t->rx_handler = (span_rx_handler_t *) &cng_rx;
+            t->rx_user_data = s;
+            t31_v21_rx(s);
+            silence_gen_set(&t->silence_gen, 0);
+            t->tx_handler = (span_tx_handler_t *) &silence_gen;
+            t->tx_user_data = &t->silence_gen;
+        }
+        s->at_state.transmit = FALSE;
+        break;
+    case T31_CED_TONE:
+        if (s->t38_mode)
+        {
+            t38_core_send_indicator(&s->t38_fe.t38, T38_IND_CED, INDICATOR_TX_COUNT);
+        }
+        else
+        {
+            modem_connect_tones_tx_init(&t->connect_tx, MODEM_CONNECT_TONES_FAX_CED);
+            t->tx_handler = (span_tx_handler_t *) &modem_connect_tones_tx;
+            t->tx_user_data = &t->connect_tx;
+            s->audio.next_tx_handler = NULL;
+        }
+        s->at_state.transmit = TRUE;
+        break;
+    case T31_V21_TX:
+        if (s->t38_mode)
+        {
+            t38_core_send_indicator(&s->t38_fe.t38, T38_IND_V21_PREAMBLE, INDICATOR_TX_COUNT);
+        }
+        else
+        {
+            hdlc_tx_init(&t->hdlc_tx, FALSE, 2, FALSE, hdlc_tx_underflow, s);
+            /* The spec says 1s +-15% of preamble. So, the minimum is 32 octets. */
+            hdlc_tx_flags(&t->hdlc_tx, 32);
+            fsk_tx_init(&t->v21_tx, &preset_fsk_specs[FSK_V21CH2], (get_bit_func_t) hdlc_tx_get_bit, &t->hdlc_tx);
+            t->tx_handler = (span_tx_handler_t *) &fsk_tx;
+            t->tx_user_data = &t->v21_tx;
+            s->audio.next_tx_handler = NULL;
+        }
+        s->hdlc_tx.final = FALSE;
+        s->hdlc_tx.len = 0;
+        s->dled = FALSE;
+        s->at_state.transmit = TRUE;
+        break;
+    case T31_V21_RX:
+        if (s->t38_mode)
+        {
+        }
+        else
+        {
+            t->rx_handler = (span_rx_handler_t *) &fsk_rx;
+            t->rx_user_data = &t->v21_rx;
+            t31_v21_rx(s);
+        }
+        break;
+    case T31_V17_TX:
+        if (s->t38_mode)
+        {
+            switch (s->bit_rate)
+            {
+            case 7200:
+                ind = (s->short_train)  ?  T38_IND_V17_7200_SHORT_TRAINING  :  T38_IND_V17_7200_LONG_TRAINING;
+                break;
+            case 9600:
+                ind = (s->short_train)  ?  T38_IND_V17_9600_SHORT_TRAINING  :  T38_IND_V17_9600_LONG_TRAINING;
+                break;
+            case 12000:
+                ind = (s->short_train)  ?  T38_IND_V17_12000_SHORT_TRAINING  :  T38_IND_V17_12000_LONG_TRAINING;
+                break;
+            case 14400:
+            default:
+                ind = (s->short_train)  ?  T38_IND_V17_14400_SHORT_TRAINING  :  T38_IND_V17_14400_LONG_TRAINING;
+                break;
+            }
+            t38_core_send_indicator(&s->t38_fe.t38, ind, INDICATOR_TX_COUNT);
+        }
+        else
+        {
+            v17_tx_restart(&t->v17_tx, s->bit_rate, FALSE, s->short_train);
+            t->tx_handler = (span_tx_handler_t *) &v17_tx;
+            t->tx_user_data = &t->v17_tx;
+            s->audio.next_tx_handler = NULL;
+        }
+        s->tx.out_bytes = 0;
+        s->tx.data_started = FALSE;
+        s->at_state.transmit = TRUE;
+        break;
+    case T31_V17_RX:
+        if (!s->t38_mode)
+        {
+            t->rx_handler = (span_rx_handler_t *) &v17_v21_rx;
+            t->rx_user_data = s;
+            v17_rx_restart(&t->v17_rx, s->bit_rate, s->short_train);
+            /* Allow for +FCERROR/+FRH:3 */
+            t31_v21_rx(s);
+        }
+        s->at_state.transmit = FALSE;
+        break;
+    case T31_V27TER_TX:
+        if (s->t38_mode)
+        {
+            switch (s->bit_rate)
+            {
+            case 2400:
+                ind = T38_IND_V27TER_2400_TRAINING;
+                break;
+            case 4800:
+            default:
+                ind = T38_IND_V27TER_4800_TRAINING;
+                break;
+            }
+            t38_core_send_indicator(&s->t38_fe.t38, ind, INDICATOR_TX_COUNT);
+        }
+        else
+        {
+            v27ter_tx_restart(&t->v27ter_tx, s->bit_rate, FALSE);
+            t->tx_handler = (span_tx_handler_t *) &v27ter_tx;
+            t->tx_user_data = &t->v27ter_tx;
+            s->audio.next_tx_handler = NULL;
+        }
+        s->tx.out_bytes = 0;
+        s->tx.data_started = FALSE;
+        s->at_state.transmit = TRUE;
+        break;
+    case T31_V27TER_RX:
+        if (!s->t38_mode)
+        {
+            t->rx_handler = (span_rx_handler_t *) &v27ter_v21_rx;
+            t->rx_user_data = s;
+            v27ter_rx_restart(&t->v27ter_rx, s->bit_rate, FALSE);
+            /* Allow for +FCERROR/+FRH:3 */
+            t31_v21_rx(s);
+        }
+        s->at_state.transmit = FALSE;
+        break;
+    case T31_V29_TX:
+        if (s->t38_mode)
+        {
+            switch (s->bit_rate)
+            {
+            case 7200:
+                ind = T38_IND_V29_7200_TRAINING;
+                break;
+            case 9600:
+            default:
+                ind = T38_IND_V29_9600_TRAINING;
+                break;
+            }
+            t38_core_send_indicator(&s->t38_fe.t38, ind, INDICATOR_TX_COUNT);
+        }
+        else
+        {
+            v29_tx_restart(&t->v29_tx, s->bit_rate, FALSE);
+            t->tx_handler = (span_tx_handler_t *) &v29_tx;
+            t->tx_user_data = &t->v29_tx;
+            s->audio.next_tx_handler = NULL;
+        }
+        s->tx.out_bytes = 0;
+        s->tx.data_started = FALSE;
+        s->at_state.transmit = TRUE;
+        break;
+    case T31_V29_RX:
+        if (!s->t38_mode)
+        {
+            t->rx_handler = (span_rx_handler_t *) &v29_v21_rx;
+            t->rx_user_data = s;
+            v29_rx_restart(&t->v29_rx, s->bit_rate, FALSE);
+            /* Allow for +FCERROR/+FRH:3 */
+            t31_v21_rx(s);
+        }
+        s->at_state.transmit = FALSE;
+        break;
+    case T31_SILENCE_TX:
+        if (s->t38_mode)
+        {
+            t38_core_send_indicator(&s->t38_fe.t38, T38_IND_NO_SIGNAL, INDICATOR_TX_COUNT);
+        }
+        else
+        {
+            silence_gen_set(&t->silence_gen, 0);
+            t->tx_handler = (span_tx_handler_t *) &silence_gen;
+            t->tx_user_data = &t->silence_gen;
+            s->audio.next_tx_handler = NULL;
+        }
+        s->at_state.transmit = FALSE;
+        break;
+    case T31_SILENCE_RX:
+        if (!s->t38_mode)
+        {
+            t->rx_handler = (span_rx_handler_t *) &silence_rx;
+            t->rx_user_data = s;
+
+            silence_gen_set(&t->silence_gen, 0);
+            t->tx_handler = (span_tx_handler_t *) &silence_gen;
+            t->tx_user_data = &t->silence_gen;
+            s->audio.next_tx_handler = NULL;
+        }
+        s->at_state.transmit = FALSE;
+        break;
+    case T31_FLUSH:
+        /* Send 200ms of silence to "push" the last audio out */
+        if (s->t38_mode)
+        {
+            t38_core_send_indicator(&s->t38_fe.t38, T38_IND_NO_SIGNAL, INDICATOR_TX_COUNT);
+        }
+        else
+        {
+            s->modem = T31_SILENCE_TX;
+            silence_gen_alter(&t->silence_gen, ms_to_samples(200));
+            t->tx_handler = (span_tx_handler_t *) &silence_gen;
+            t->tx_user_data = &t->silence_gen;
+            s->audio.next_tx_handler = NULL;
+            s->at_state.transmit = TRUE;
+        }
+        break;
+    }
+    s->audio.bit_no = 0;
+    s->audio.current_byte = 0xFF;
+    s->tx.in_bytes = 0;
+    s->tx.out_bytes = 0;
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ void dle_unstuff_hdlc(t31_state_t *s, const char *stuffed, int len)
+{
+    int i;
+
+    for (i = 0;  i < len;  i++)
+    {
+        if (s->dled)
+        {
+            s->dled = FALSE;
+            if (stuffed[i] == ETX)
+            {
+                if (s->t38_mode)
+                {
+                }
+                else
+                {
+                    hdlc_tx_frame(&(s->audio.modems.hdlc_tx), s->hdlc_tx.buf, s->hdlc_tx.len);
+                }
+                s->hdlc_tx.final = (s->hdlc_tx.buf[1] & 0x10);
+                s->hdlc_tx.len = 0;
+            }
+            else if (stuffed[i] == SUB)
+            {
+                s->hdlc_tx.buf[s->hdlc_tx.len++] = DLE;
+                s->hdlc_tx.buf[s->hdlc_tx.len++] = DLE;
+            }
+            else
+            {
+                s->hdlc_tx.buf[s->hdlc_tx.len++] = stuffed[i];
+            }
+        }
+        else
+        {
+            if (stuffed[i] == DLE)
+                s->dled = TRUE;
+            else
+                s->hdlc_tx.buf[s->hdlc_tx.len++] = stuffed[i];
+        }
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ void dle_unstuff(t31_state_t *s, const char *stuffed, int len)
+{
+    int i;
+    
+    for (i = 0;  i < len;  i++)
+    {
+        if (s->dled)
+        {
+            s->dled = FALSE;
+            if (stuffed[i] == ETX)
+            {
+                s->tx.final = TRUE;
+                t31_set_at_rx_mode(s, AT_MODE_OFFHOOK_COMMAND);
+                return;
+            }
+        }
+        else if (stuffed[i] == DLE)
+        {
+            s->dled = TRUE;
+            continue;
+        }
+        s->tx.data[s->tx.in_bytes++] = stuffed[i];
+        if (s->tx.in_bytes > T31_TX_BUF_LEN - 1)
+        {
+            /* Oops. We hit the end of the buffer. Give up. Loose stuff. :-( */
+            fprintf(stderr, "No room in buffer for new data!\n");
+            return;
+        }
+    }
+    if (!s->tx.holding)
+    {
+        /* See if the buffer is approaching full. We might need to apply flow control. */
+        if (s->tx.in_bytes > T31_TX_BUF_LEN - 1024)
+        {
+            s->tx.holding = TRUE;
+            /* Tell the application to hold further data */
+            at_modem_control(&s->at_state, AT_MODEM_CONTROL_CTS, (void *) 0);
+        }
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static int process_class1_cmd(at_state_t *t, void *user_data, int direction, int operation, int val)
+{
+    int new_modem;
+    int new_transmit;
+    int i;
+    int len;
+    int immediate_response;
+    t31_state_t *s;
+    uint8_t msg[256];
+
+    s = (t31_state_t *) user_data;
+    new_transmit = direction;
+    immediate_response = TRUE;
+    switch (operation)
+    {
+    case 'S':
+        s->at_state.transmit = new_transmit;
+        if (new_transmit)
+        {
+            /* Send a specified period of silence, to space transmissions. */
+            restart_modem(s, T31_SILENCE_TX);
+            silence_gen_alter(&(s->audio.modems.silence_gen), ms_to_samples(val*10));
+            s->at_state.transmit = TRUE;
+        }
+        else
+        {
+            /* Wait until we have received a specified period of silence. */
+            queue_flush(s->rx_queue);
+            s->silence_awaited = ms_to_samples(val*10);
+            t31_set_at_rx_mode(s, AT_MODE_DELIVERY);
+            restart_modem(s, T31_SILENCE_RX);
+        }
+        immediate_response = FALSE;
+        span_log(&s->logging, SPAN_LOG_FLOW, "Silence %dms\n", val*10);
+        break;
+    case 'H':
+        switch (val)
+        {
+        case 3:
+            new_modem = (new_transmit)  ?  T31_V21_TX  :  T31_V21_RX;
+            s->short_train = FALSE;
+            s->bit_rate = 300;
+            break;
+        default:
+            return -1;
+        }
+        span_log(&s->logging, SPAN_LOG_FLOW, "HDLC\n");
+        if (new_modem != s->modem)
+        {
+            restart_modem(s, new_modem);
+            immediate_response = FALSE;
+        }
+        s->at_state.transmit = new_transmit;
+        if (new_transmit)
+        {
+            t31_set_at_rx_mode(s, AT_MODE_HDLC);
+            at_put_response_code(&s->at_state, AT_RESPONSE_CODE_CONNECT);
+        }
+        else
+        {
+            /* Send straight away, if there is something queued. */
+            t31_set_at_rx_mode(s, AT_MODE_DELIVERY);
+            s->rx_frame_received = FALSE;
+            do
+            {
+                if (!queue_empty(s->rx_queue))
+                {
+                    len = queue_read_msg(s->rx_queue, msg, 256);
+                    if (len > 1)
+                    {
+                        if (msg[0] == AT_RESPONSE_CODE_OK)
+                            at_put_response_code(&s->at_state, AT_RESPONSE_CODE_CONNECT);
+                        for (i = 1;  i < len;  i++)
+                        {
+                            if (msg[i] == DLE)
+                                s->at_state.rx_data[s->at_state.rx_data_bytes++] = DLE;
+                            s->at_state.rx_data[s->at_state.rx_data_bytes++] = msg[i];
+                        }
+                        s->at_state.rx_data[s->at_state.rx_data_bytes++] = DLE;
+                        s->at_state.rx_data[s->at_state.rx_data_bytes++] = ETX;
+                        s->at_state.at_tx_handler(&s->at_state, s->at_state.at_tx_user_data, s->at_state.rx_data, s->at_state.rx_data_bytes);
+                        s->at_state.rx_data_bytes = 0;
+                    }
+                    at_put_response_code(&s->at_state, msg[0]);
+                }
+                else
+                {
+                    s->at_state.dte_is_waiting = TRUE;
+                    break;
+                }
+            }
+            while (msg[0] == AT_RESPONSE_CODE_CONNECT);
+        }
+        immediate_response = FALSE;
+        break;
+    default:
+        switch (val)
+        {
+        case 24:
+            new_modem = (new_transmit)  ?  T31_V27TER_TX  :  T31_V27TER_RX;
+            s->short_train = FALSE;
+            s->bit_rate = 2400;
+            break;
+        case 48:
+            new_modem = (new_transmit)  ?  T31_V27TER_TX  :  T31_V27TER_RX;
+            s->short_train = FALSE;
+            s->bit_rate = 4800;
+            break;
+        case 72:
+            new_modem = (new_transmit)  ?  T31_V29_TX  :  T31_V29_RX;
+            s->short_train = FALSE;
+            s->bit_rate = 7200;
+            break;
+        case 96:
+            new_modem = (new_transmit)  ?  T31_V29_TX  :  T31_V29_RX;
+            s->short_train = FALSE;
+            s->bit_rate = 9600;
+            break;
+        case 73:
+            new_modem = (new_transmit)  ?  T31_V17_TX  :  T31_V17_RX;
+            s->short_train = FALSE;
+            s->bit_rate = 7200;
+            break;
+        case 74:
+            new_modem = (new_transmit)  ?  T31_V17_TX  :  T31_V17_RX;
+            s->short_train = TRUE;
+            s->bit_rate = 7200;
+            break;
+        case 97:
+            new_modem = (new_transmit)  ?  T31_V17_TX  :  T31_V17_RX;
+            s->short_train = FALSE;
+            s->bit_rate = 9600;
+            break;
+        case 98:
+            new_modem = (new_transmit)  ?  T31_V17_TX  :  T31_V17_RX;
+            s->short_train = TRUE;
+            s->bit_rate = 9600;
+            break;
+        case 121:
+            new_modem = (new_transmit)  ?  T31_V17_TX  :  T31_V17_RX;
+            s->short_train = FALSE;
+            s->bit_rate = 12000;
+            break;
+        case 122:
+            new_modem = (new_transmit)  ?  T31_V17_TX  :  T31_V17_RX;
+            s->short_train = TRUE;
+            s->bit_rate = 12000;
+            break;
+        case 145:
+            new_modem = (new_transmit)  ?  T31_V17_TX  :  T31_V17_RX;
+            s->short_train = FALSE;
+            s->bit_rate = 14400;
+            break;
+        case 146:
+            new_modem = (new_transmit)  ?  T31_V17_TX  :  T31_V17_RX;
+            s->short_train = TRUE;
+            s->bit_rate = 14400;
+            break;
+        default:
+            return -1;
+        }
+        span_log(&s->logging, SPAN_LOG_FLOW, "Short training = %d, bit rate = %d\n", s->short_train, s->bit_rate);
+        if (new_transmit)
+        {
+            t31_set_at_rx_mode(s, AT_MODE_STUFFED);
+            at_put_response_code(&s->at_state, AT_RESPONSE_CODE_CONNECT);
+        }
+        else
+        {
+            t31_set_at_rx_mode(s, AT_MODE_DELIVERY);
+        }
+        restart_modem(s, new_modem);
+        immediate_response = FALSE;
+        break;
+    }
+    return immediate_response;
+}
+/*- End of function --------------------------------------------------------*/
+
+void t31_call_event(t31_state_t *s, int event)
+{
+    span_log(&s->logging, SPAN_LOG_FLOW, "Call event %d received\n", event);
+    at_call_event(&s->at_state, event);
+}
+/*- End of function --------------------------------------------------------*/
+
+int t31_at_rx(t31_state_t *s, const char *t, int len)
+{
+    if (s->dte_data_timeout)
+        s->dte_data_timeout = s->call_samples + ms_to_samples(5000);
+    switch (s->at_state.at_rx_mode)
+    {
+    case AT_MODE_ONHOOK_COMMAND:
+    case AT_MODE_OFFHOOK_COMMAND:
+        at_interpreter(&s->at_state, t, len);
+        break;
+    case AT_MODE_DELIVERY:
+        /* Data from the DTE in this state returns us to command mode */
+        if (len)
+        {
+            if (s->at_state.rx_signal_present)
+            {
+                s->at_state.rx_data[s->at_state.rx_data_bytes++] = DLE;
+                s->at_state.rx_data[s->at_state.rx_data_bytes++] = ETX;
+                s->at_state.at_tx_handler(&s->at_state, s->at_state.at_tx_user_data, s->at_state.rx_data, s->at_state.rx_data_bytes);
+            }
+            s->at_state.rx_data_bytes = 0;
+            s->at_state.transmit = FALSE;
+            s->modem = T31_SILENCE_TX;
+            s->audio.modems.rx_handler = span_dummy_rx;
+            t31_set_at_rx_mode(s, AT_MODE_OFFHOOK_COMMAND);
+            at_put_response_code(&s->at_state, AT_RESPONSE_CODE_OK);
+        }
+        break;
+    case AT_MODE_HDLC:
+        dle_unstuff_hdlc(s, t, len);
+        break;
+    case AT_MODE_STUFFED:
+        if (s->tx.out_bytes)
+        {
+            /* Make room for new data in existing data buffer. */
+            s->tx.in_bytes = &(s->tx.data[s->tx.in_bytes]) - &(s->tx.data[s->tx.out_bytes]);
+            memmove(&(s->tx.data[0]), &(s->tx.data[s->tx.out_bytes]), s->tx.in_bytes);
+            s->tx.out_bytes = 0;
+        }
+        dle_unstuff(s, t, len);
+        break;
+    }
+    return len;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int silence_rx(void *user_data, const int16_t amp[], int len)
+{
+    t31_state_t *s;
+
+    /* Searching for a specified minimum period of silence. */
+    s = (t31_state_t *) user_data;
+    if (s->silence_awaited  &&  s->audio.silence_heard >= s->silence_awaited)
+    {
+        at_put_response_code(&s->at_state, AT_RESPONSE_CODE_OK);
+        t31_set_at_rx_mode(s, AT_MODE_OFFHOOK_COMMAND);
+        s->audio.silence_heard = 0;
+        s->silence_awaited = 0;
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int cng_rx(void *user_data, const int16_t amp[], int len)
+{
+    t31_state_t *s;
+
+    s = (t31_state_t *) user_data;
+    if (s->call_samples > ms_to_samples(s->at_state.p.s_regs[7]*1000))
+    {
+        /* After calling, S7 has elapsed... no carrier found. */
+        at_put_response_code(&s->at_state, AT_RESPONSE_CODE_NO_CARRIER);
+        restart_modem(s, T31_SILENCE_TX);
+        at_modem_control(&s->at_state, AT_MODEM_CONTROL_HANGUP, NULL);
+        t31_set_at_rx_mode(s, AT_MODE_ONHOOK_COMMAND);
+    }
+    else
+    {
+        fsk_rx(&(s->audio.modems.v21_rx), amp, len);
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int v17_v21_rx(void *user_data, const int16_t amp[], int len)
+{
+    t31_state_t *t;
+    fax_modems_state_t *s;
+
+    t = (t31_state_t *) user_data;
+    s = &t->audio.modems;
+    v17_rx(&s->v17_rx, amp, len);
+    if (t->at_state.rx_trained)
+    {
+        /* The fast modem has trained, so we no longer need to run the slow
+           one in parallel. */
+        span_log(&t->logging, SPAN_LOG_FLOW, "Switching from V.17 + V.21 to V.17 (%.2fdBm0)\n", v17_rx_signal_power(&s->v17_rx));
+        s->rx_handler = (span_rx_handler_t *) &v17_rx;
+        s->rx_user_data = &s->v17_rx;
+    }
+    else
+    {
+        fsk_rx(&s->v21_rx, amp, len);
+        if (t->rx_frame_received)
+        {
+            /* We have received something, and the fast modem has not trained. We must
+               be receiving valid V.21 */
+            span_log(&t->logging, SPAN_LOG_FLOW, "Switching from V.17 + V.21 to V.21 (%.2fdBm0)\n", fsk_rx_signal_power(&s->v21_rx));
+            s->rx_handler = (span_rx_handler_t *) &fsk_rx;
+            s->rx_user_data = &s->v21_rx;
+        }
+    }
+    return len;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int v27ter_v21_rx(void *user_data, const int16_t amp[], int len)
+{
+    t31_state_t *t;
+    fax_modems_state_t *s;
+
+    t = (t31_state_t *) user_data;
+    s = &t->audio.modems;
+    v27ter_rx(&s->v27ter_rx, amp, len);
+    if (t->at_state.rx_trained)
+    {
+        /* The fast modem has trained, so we no longer need to run the slow
+           one in parallel. */
+        span_log(&t->logging, SPAN_LOG_FLOW, "Switching from V.27ter + V.21 to V.27ter (%.2fdBm0)\n", v27ter_rx_signal_power(&s->v27ter_rx));
+        s->rx_handler = (span_rx_handler_t *) &v27ter_rx;
+        s->rx_user_data = &s->v27ter_rx;
+    }
+    else
+    {
+        fsk_rx(&s->v21_rx, amp, len);
+        if (t->rx_frame_received)
+        {
+            /* We have received something, and the fast modem has not trained. We must
+               be receiving valid V.21 */
+            span_log(&t->logging, SPAN_LOG_FLOW, "Switching from V.27ter + V.21 to V.21 (%.2fdBm0)\n", fsk_rx_signal_power(&s->v21_rx));
+            s->rx_handler = (span_rx_handler_t *) &fsk_rx;
+            s->rx_user_data = &s->v21_rx;
+        }
+    }
+    return len;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int v29_v21_rx(void *user_data, const int16_t amp[], int len)
+{
+    t31_state_t *t;
+    fax_modems_state_t *s;
+
+    t = (t31_state_t *) user_data;
+    s = &t->audio.modems;
+    v29_rx(&s->v29_rx, amp, len);
+    if (t->at_state.rx_trained)
+    {
+        /* The fast modem has trained, so we no longer need to run the slow
+           one in parallel. */
+        span_log(&s->logging, SPAN_LOG_FLOW, "Switching from V.29 + V.21 to V.29 (%.2fdBm0)\n", v29_rx_signal_power(&s->v29_rx));
+        s->rx_handler = (span_rx_handler_t *) &v29_rx;
+        s->rx_user_data = &s->v29_rx;
+    }
+    else
+    {
+        fsk_rx(&s->v21_rx, amp, len);
+        if (t->rx_frame_received)
+        {
+            /* We have received something, and the fast modem has not trained. We must
+               be receiving valid V.21 */
+            span_log(&t->logging, SPAN_LOG_FLOW, "Switching from V.29 + V.21 to V.21 (%.2fdBm0)\n", fsk_rx_signal_power(&s->v21_rx));
+            s->rx_handler = (span_rx_handler_t *) &fsk_rx;
+            s->rx_user_data = &s->v21_rx;
+        }
+    }
+    return len;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void t31_fax_modems_init(fax_modems_state_t *s, int use_tep, void *user_data)
+{
+    s->use_tep = use_tep;
+
+    hdlc_rx_init(&s->hdlc_rx, FALSE, TRUE, HDLC_FRAMING_OK_THRESHOLD, hdlc_accept, user_data);
+    hdlc_tx_init(&s->hdlc_tx, FALSE, 2, FALSE, hdlc_tx_underflow, user_data);
+    fsk_rx_init(&s->v21_rx, &preset_fsk_specs[FSK_V21CH2], TRUE, (put_bit_func_t) hdlc_rx_put_bit, &s->hdlc_rx);
+    fsk_rx_signal_cutoff(&s->v21_rx, -39.09);
+    fsk_tx_init(&s->v21_tx, &preset_fsk_specs[FSK_V21CH2], (get_bit_func_t) hdlc_tx_get_bit, &s->hdlc_tx);
+    v17_rx_init(&s->v17_rx, 14400, non_ecm_put_bit, user_data);
+    v17_tx_init(&s->v17_tx, 14400, s->use_tep, non_ecm_get_bit, user_data);
+    v29_rx_init(&s->v29_rx, 9600, non_ecm_put_bit, user_data);
+    v29_rx_signal_cutoff(&s->v29_rx, -45.5);
+    v29_tx_init(&s->v29_tx, 9600, s->use_tep, non_ecm_get_bit, user_data);
+    v27ter_rx_init(&s->v27ter_rx, 4800, non_ecm_put_bit, user_data);
+    v27ter_tx_init(&s->v27ter_tx, 4800, s->use_tep, non_ecm_get_bit, user_data);
+    silence_gen_init(&s->silence_gen, 0);
+    modem_connect_tones_tx_init(&s->connect_tx, MODEM_CONNECT_TONES_FAX_CNG);
+    modem_connect_tones_rx_init(&s->connect_rx,
+                                MODEM_CONNECT_TONES_FAX_CNG,
+                                tone_detected,
+                                user_data);
+    dc_restore_init(&s->dc_restore);
+
+    s->rx_signal_present = FALSE;
+    s->rx_handler = (span_rx_handler_t *) &span_dummy_rx;
+    s->rx_user_data = NULL;
+    s->tx_handler = (span_tx_handler_t *) &silence_gen;
+    s->tx_user_data = &s->silence_gen;
+}
+/*- End of function --------------------------------------------------------*/
+
+int t31_rx(t31_state_t *s, int16_t amp[], int len)
+{
+    int i;
+    int32_t power;
+
+    /* Monitor for received silence.  Maximum needed detection is AT+FRS=255 (255*10ms). */
+    /* We could probably only run this loop if (s->modem == T31_SILENCE_RX), however,
+       the spec says "when silence has been present on the line for the amount of
+       time specified".  That means some of the silence may have occurred before
+       the AT+FRS=n command. This condition, however, is not likely to ever be the
+       case.  (AT+FRS=n will usually be issued before the remote goes silent.) */
+    for (i = 0;  i < len;  i++)
+    {
+        /* Clean up any DC influence. */
+        power = power_meter_update(&(s->audio.rx_power), amp[i] - s->audio.last_sample);
+        s->audio.last_sample = amp[i];
+        if (power > s->audio.silence_threshold_power)
+        {
+            s->audio.silence_heard = 0;
+        }
+        else
+        {        
+            if (s->audio.silence_heard <= ms_to_samples(255*10))
+                s->audio.silence_heard++;
+        }
+    }
+
+    /* Time is determined by counting the samples in audio packets coming in. */
+    s->call_samples += len;
+
+    /* In HDLC transmit mode, if 5 seconds elapse without data from the DTE
+       we must treat this as an error. We return the result ERROR, and change
+       to command-mode. */
+    if (s->dte_data_timeout  &&  s->call_samples > s->dte_data_timeout)
+    {
+        t31_set_at_rx_mode(s, AT_MODE_OFFHOOK_COMMAND);
+        at_put_response_code(&s->at_state, AT_RESPONSE_CODE_ERROR);
+        restart_modem(s, T31_SILENCE_TX);
+    }
+
+    if (!s->at_state.transmit  ||  s->modem == T31_CNG_TONE)
+        s->audio.modems.rx_handler(s->audio.modems.rx_user_data, amp, len);
+    return  0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int set_next_tx_type(t31_state_t *s)
+{
+    if (s->audio.next_tx_handler)
+    {
+        s->audio.modems.tx_handler = s->audio.next_tx_handler;
+        s->audio.modems.tx_user_data = s->audio.next_tx_user_data;
+        s->audio.next_tx_handler = NULL;
+        return 0;
+    }
+    /* If there is nothing else to change to, so use zero length silence */
+    silence_gen_alter(&(s->audio.modems.silence_gen), 0);
+    s->audio.modems.tx_handler = (span_tx_handler_t *) &silence_gen;
+    s->audio.modems.tx_user_data = &(s->audio.modems.silence_gen);
+    s->audio.next_tx_handler = NULL;
+    return -1;
+}
+/*- End of function --------------------------------------------------------*/
+
+int t31_tx(t31_state_t *s, int16_t amp[], int max_len)
+{
+    int len;
+
+    len = 0;
+    if (s->at_state.transmit)
+    {
+        if ((len = s->audio.modems.tx_handler(s->audio.modems.tx_user_data, amp, max_len)) < max_len)
+        {
+            /* Allow for one change of tx handler within a block */
+            set_next_tx_type(s);
+            if ((len += s->audio.modems.tx_handler(s->audio.modems.tx_user_data, amp + len, max_len - len)) < max_len)
+            {
+                switch (s->modem)
+                {
+                case T31_SILENCE_TX:
+                    s->modem = T31_NONE;
+                    at_put_response_code(&s->at_state, AT_RESPONSE_CODE_OK);
+                    if (s->at_state.do_hangup)
+                    {
+                        at_modem_control(&s->at_state, AT_MODEM_CONTROL_HANGUP, NULL);
+                        t31_set_at_rx_mode(s, AT_MODE_ONHOOK_COMMAND);
+                        s->at_state.do_hangup = FALSE;
+                    }
+                    else
+                    {
+                        t31_set_at_rx_mode(s, AT_MODE_OFFHOOK_COMMAND);
+                    }
+                    break;
+                case T31_CED_TONE:
+                    /* Go directly to V.21/HDLC transmit. */
+                    s->modem = T31_NONE;
+                    restart_modem(s, T31_V21_TX);
+                    t31_set_at_rx_mode(s, AT_MODE_HDLC);
+                    break;
+                case T31_V21_TX:
+                case T31_V17_TX:
+                case T31_V27TER_TX:
+                case T31_V29_TX:
+                    s->modem = T31_NONE;
+                    at_put_response_code(&s->at_state, AT_RESPONSE_CODE_OK);
+                    t31_set_at_rx_mode(s, AT_MODE_OFFHOOK_COMMAND);
+                    restart_modem(s, T31_SILENCE_TX);
+                    break;
+                }
+            }
+        }
+    }
+    if (s->audio.modems.transmit_on_idle)
+    {
+        /* Pad to the requested length with silence */
+        memset(amp + len, 0, (max_len - len)*sizeof(int16_t));
+        len = max_len;        
+    }
+    return len;
+}
+/*- End of function --------------------------------------------------------*/
+
+void t31_set_transmit_on_idle(t31_state_t *s, int transmit_on_idle)
+{
+    s->audio.modems.transmit_on_idle = transmit_on_idle;
+}
+/*- End of function --------------------------------------------------------*/
+
+void t31_set_tep_mode(t31_state_t *s, int use_tep)
+{
+    s->audio.modems.use_tep = use_tep;
+}
+/*- End of function --------------------------------------------------------*/
+
+void t31_set_t38_config(t31_state_t *s, int without_pacing)
+{
+    if (without_pacing)
+    {
+        /* Continuous streaming mode, as used for TPKT over TCP transport */
+        s->t38_fe.t38.indicator_tx_count = 0;
+        s->t38_fe.t38.data_end_tx_count = 1;
+        s->t38_fe.ms_per_tx_chunk = 0;
+    }
+    else
+    {
+        /* Paced streaming mode, as used for UDP transports */
+        s->t38_fe.t38.indicator_tx_count = INDICATOR_TX_COUNT;
+        s->t38_fe.t38.data_end_tx_count = DATA_END_TX_COUNT;
+        s->t38_fe.ms_per_tx_chunk = MS_PER_TX_CHUNK;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+t31_state_t *t31_init(t31_state_t *s,
+                      at_tx_handler_t *at_tx_handler,
+                      void *at_tx_user_data,
+                      t31_modem_control_handler_t *modem_control_handler,
+                      void *modem_control_user_data,
+                      t38_tx_packet_handler_t *tx_t38_packet_handler,
+                      void *tx_t38_packet_user_data)
+{
+    int alloced;
+    
+    if (at_tx_handler == NULL  ||  modem_control_handler == NULL)
+        return NULL;
+
+    alloced = FALSE;
+    if (s == NULL)
+    {
+        if ((s = (t31_state_t *) malloc(sizeof (*s))) == NULL)
+            return NULL;
+        alloced = TRUE;
+    }
+    memset(s, 0, sizeof(*s));
+    span_log_init(&s->logging, SPAN_LOG_NONE, NULL);
+    span_log_set_protocol(&s->logging, "T.31");
+
+    s->modem_control_handler = modem_control_handler;
+    s->modem_control_user_data = modem_control_user_data;
+    t31_fax_modems_init(&s->audio.modems, FALSE, s);
+    power_meter_init(&(s->audio.rx_power), 4);
+    s->audio.last_sample = 0;
+    s->audio.silence_threshold_power = power_meter_level_dbm0(-36);
+    s->at_state.rx_signal_present = FALSE;
+    s->at_state.rx_trained = FALSE;
+
+    s->at_state.do_hangup = FALSE;
+    s->at_state.line_ptr = 0;
+    s->audio.silence_heard = 0;
+    s->silence_awaited = 0;
+    s->call_samples = 0;
+    s->modem = T31_NONE;
+    s->at_state.transmit = TRUE;
+
+    if ((s->rx_queue = queue_init(NULL, 4096, QUEUE_WRITE_ATOMIC | QUEUE_READ_ATOMIC)) == NULL)
+    {
+        if (alloced)
+            free(s);
+        return NULL;
+    }
+    at_init(&s->at_state, at_tx_handler, at_tx_user_data, t31_modem_control_handler, s);
+    at_set_class1_handler(&s->at_state, process_class1_cmd, s);
+    s->at_state.dte_inactivity_timeout = DEFAULT_DTE_TIMEOUT;
+    if (tx_t38_packet_handler)
+    {
+        t38_core_init(&s->t38_fe.t38,
+                      process_rx_indicator,
+                      process_rx_data,
+                      process_rx_missing,
+                      (void *) s,
+                      tx_t38_packet_handler,
+                      tx_t38_packet_user_data);
+        t31_set_t38_config(s, FALSE);
+    }
+    s->t38_mode = FALSE;
+    return s;
+}
+/*- End of function --------------------------------------------------------*/
+
+int t31_release(t31_state_t *s)
+{
+    at_reset_call_info(&s->at_state);
+    free(s);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/t35.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/t35.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,900 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * t35.c - ITU T.35 FAX non-standard facility processing.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2003 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: t35.c,v 1.27 2008/05/13 13:17:23 steveu Exp $
+ */
+
+/*
+ * The NSF data tables are adapted from the NSF handling in HylaFAX, which
+ * carries the following copyright notice:
+ *
+ * Created by Dmitry Bely, April 2000
+ * Copyright (c) 1994-1996 Sam Leffler
+ * Copyright (c) 1994-1996 Silicon Graphics, Inc.
+ * HylaFAX is a trademark of Silicon Graphics
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and 
+ * its documentation for any purpose is hereby granted without fee, provided
+ * that (i) the above copyright notices and this permission notice appear in
+ * all copies of the software and related documentation, and (ii) the names of
+ * Sam Leffler and Silicon Graphics may not be used in any advertising or
+ * publicity relating to the software without the specific, prior written
+ * permission of Sam Leffler and Silicon Graphics.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
+ * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
+ * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
+ * 
+ * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
+ * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
+ * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
+ * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
+ * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
+ * OF THIS SOFTWARE.
+ */
+
+/*! \file */
+
+#if defined(HAVE_CONFIG_H)
+#include <config.h>
+#endif
+
+#include <inttypes.h>
+#include <ctype.h>
+#include <string.h>
+
+#include "spandsp/telephony.h"
+#include "spandsp/bit_operations.h"
+#include "spandsp/t35.h"
+
+typedef struct
+{
+    int model_id_size;
+    const char *model_id;
+    const char *model_name;
+} model_data_t;
+
+typedef struct
+{
+    const char *vendor_id;
+    int vendor_id_len;
+    const char *vendor_name;
+    int inverse_station_id_order;
+    const model_data_t *known_models;
+} nsf_data_t;
+
+const char *t35_country_codes[256] =
+{
+    "Japan",                                    /* 0x00 */
+    "Albania",
+    "Algeria",
+    "American Samoa",
+    "Germany",
+    "Anguilla",
+    "Antigua and Barbuda",
+    "Argentina",
+    "Ascension (see S. Helena)",
+    "Australia",
+    "Austria",
+    "Bahamas",
+    "Bahrain",
+    "Bangladesh",
+    "Barbados",
+    "Belgium",
+    "Belize",
+    "Benin (Republic of)",
+    "Bermudas",
+    "Bhutan (Kingdom of)",
+    "Bolivia",
+    "Botswana",
+    "Brazil",
+    "British Antarctic Territory",
+    "British Indian Ocean Territory",
+    "British Virgin Islands",
+    "Brunei Darussalam",
+    "Bulgaria",
+    "Myanmar (Union of)",
+    "Burundi",
+    "Byelorussia",
+    "Cameroon",
+    "Canada",                                   /* 0x20 */
+    "Cape Verde",
+    "Cayman Islands",
+    "Central African Republic",
+    "Chad",
+    "Chile",
+    "China",
+    "Colombia",
+    "Comoros",
+    "Congo",
+    "Cook Islands",
+    "Costa Rica",
+    "Cuba",
+    "Cyprus",
+    "Czech and Slovak Federal Republic",
+    "Cambodia",
+    "Democratic People's Republic of Korea",
+    "Denmark",
+    "Djibouti",
+    "Dominican Republic",
+    "Dominica",
+    "Ecuador",
+    "Egypt",
+    "El Salvador",
+    "Equatorial Guinea",
+    "Ethiopia",
+    "Falkland Islands",
+    "Fiji",
+    "Finland",
+    "France",
+    "French Polynesia",
+    "French Southern and Antarctic Lands",
+    "Gabon",                                        /* 0x40 */
+    "Gambia",
+    "Germany (Federal Republic of)",
+    "Angola",
+    "Ghana",
+    "Gibraltar",
+    "Greece",
+    "Grenada",
+    "Guam",
+    "Guatemala",
+    "Guernsey",
+    "Guinea",
+    "Guinea-Bissau",
+    "Guayana",
+    "Haiti",
+    "Honduras",
+    "Hong Kong",
+    "Hungary (Republic of)",
+    "Iceland",
+    "India",
+    "Indonesia",
+    "Iran (Islamic Republic of)",
+    "Iraq",
+    "Ireland",
+    "Israel",
+    "Italy",
+    "Cote d'Ivoire",
+    "Jamaica",
+    "Afghanistan",
+    "Jersey",
+    "Jordan",
+    "Kenya",
+    "Kiribati",                                     /* 0x60 */
+    "Korea (Republic of)",
+    "Kuwait",
+    "Lao (People's Democratic Republic)",
+    "Lebanon",
+    "Lesotho",
+    "Liberia",
+    "Libya",
+    "Liechtenstein",
+    "Luxembourg",
+    "Macau",
+    "Madagascar",
+    "Malaysia",
+    "Malawi",
+    "Maldives",
+    "Mali",
+    "Malta",
+    "Mauritania",
+    "Mauritius",
+    "Mexico",
+    "Monaco",
+    "Mongolia",
+    "Montserrat",
+    "Morocco",
+    "Mozambique",
+    "Nauru",
+    "Nepal",
+    "Netherlands",
+    "Netherlands Antilles",
+    "New Caledonia",
+    "New Zealand",
+    "Nicaragua",
+    "Niger",                                        /* 0x80 */
+    "Nigeria",
+    "Norway",
+    "Oman",
+    "Pakistan",
+    "Panama",
+    "Papua New Guinea",
+    "Paraguay",
+    "Peru",
+    "Philippines",
+    "Poland (Republic of)",
+    "Portugal",
+    "Puerto Rico",
+    "Qatar",
+    "Romania",
+    "Rwanda",
+    "Saint Kitts and Nevis",
+    "Saint Croix",
+    "Saint Helena and Ascension",
+    "Saint Lucia",
+    "San Marino",
+    "Saint Thomas",
+    "Sao Tome and Principe",
+    "Saint Vincent and the Grenadines",
+    "Saudi Arabia",
+    "Senegal",
+    "Seychelles",
+    "Sierra Leone",
+    "Singapore",
+    "Solomon Islands",
+    "Somalia",
+    "South Africa",
+    "Spain",                                        /* 0xA0 */
+    "Sri Lanka",
+    "Sudan",
+    "Suriname",
+    "Swaziland",
+    "Sweden",
+    "Switzerland",
+    "Syria",
+    "Tanzania",
+    "Thailand",
+    "Togo",
+    "Tonga",
+    "Trinidad and Tobago",
+    "Tunisia",
+    "Turkey",
+    "Turks and Caicos Islands",
+    "Tuvalu",
+    "Uganda",
+    "Ukraine",
+    "United Arab Emirates",
+    "United Kingdom",
+    "United States",
+    "Burkina Faso",
+    "Uruguay",
+    "U.S.S.R.",
+    "Vanuatu",
+    "Vatican City State",
+    "Venezuela",
+    "Viet Nam",
+    "Wallis and Futuna",
+    "Western Samoa",
+    "Yemen (Republic of)",
+    "Yemen (Republic of)",                          /* 0xC0 */
+    "Yugoslavia",
+    "Zaire",
+    "Zambia",
+    "Zimbabwe"
+    "Slovakia",
+    "Slovenia",
+    "(available)",
+    "(available)",
+    "(available)",
+    "(available)",
+    "(available)",
+    "(available)",
+    "(available)",
+    "(available)",
+    "(available)",
+    "(available)",                                  /* 0xD0 */
+    "(available)",
+    "(available)",
+    "(available)",
+    "(available)",
+    "(available)",
+    "(available)",
+    "(available)",
+    "(available)",
+    "(available)",
+    "(available)",
+    "(available)",
+    "(available)",
+    "(available)",
+    "(available)",
+    "(available)",
+    "(available)",                                  /* 0xE0 */
+    "(available)",
+    "(available)",
+    "(available)",
+    "(available)",
+    "(available)",
+    "(available)",
+    "(available)",
+    "(available)",
+    "(available)",
+    "(available)",
+    "(available)",
+    "(available)",
+    "(available)",
+    "(available)",
+    "(available)",
+    "(available)",                                  /* 0xF0 */
+    "(available)",
+    "(available)",
+    "(available)",
+    "(available)",
+    "(available)",
+    "(available)",
+    "Lithuania",
+    "Latvia",
+    "Estonia",
+    "US Virgin Islands",
+    "(available)",
+    "(available)",
+    "(Universal)",
+    "Taiwan",
+    "(extension)"
+};
+
+static const model_data_t Canon[] =
+{
+    {5, "\x80\x00\x80\x48\x00", "Faxphone B640"},
+    {5, "\x80\x00\x80\x49\x10", "Fax B100"},
+    {5, "\x80\x00\x8A\x49\x10", "Laser Class 9000 Series"},
+    {0, NULL, NULL}
+};  
+
+static const model_data_t Brother[] =
+{
+    {9, "\x55\x55\x00\x88\x90\x80\x5F\x00\x15\x51", "Intellifax 770"},
+    {9, "\x55\x55\x00\x80\xB0\x80\x00\x00\x59\xD4", "Personal fax 190"},
+    {9, "\x55\x55\x00\x8C\x90\x80\xF0\x02\x20", "MFC-8600"},
+    {0, NULL, NULL}
+};
+
+static const model_data_t Panasonic0E[] =
+{
+    {10, "\x00\x00\x00\x96\x0F\x01\x02\x00\x10\x05\x02\x95\xC8\x08\x01\x49\x02\x41\x53\x54\x47", "KX-F90"},
+    {10, "\x00\x00\x00\x96\x0F\x01\x03\x00\x10\x05\x02\x95\xC8\x08\x01\x49\x02                \x03", "KX-F230 or KX-FT21 or ..."},
+    {10, "\x00\x00\x00\x16\x0F\x01\x03\x00\x10\x05\x02\x95\xC8\x08", "KX-F780"},
+    {10, "\x00\x00\x00\x16\x0F\x01\x03\x00\x10\x00\x02\x95\x80\x08\x75\xB5", "KX-M260"},
+    {10, "\x00\x00\x00\x16\x0F\x01\x02\x00\x10\x05\x02\x85\xC8\x08\xAD", "KX-F2050BS"},
+    {0, NULL, NULL}
+};
+
+static const model_data_t Panasonic79[] =
+{
+    {10, "\x00\x00\x00\x02\x0F\x09\x12\x00\x10\x05\x02\x95\xC8\x88\x80\x80\x01", "UF-S10"},
+    {10, "\x00\x00\x00\x16\x7F\x09\x13\x00\x10\x05\x16\x8D\xC0\xD0\xF8\x80\x01", "/Siemens Fax 940"},
+    {10, "\x00\x00\x00\x16\x0F\x09\x13\x00\x10\x05\x06\x8D\xC0\x50\xCB", "Panafax UF-321"},
+    {0, NULL, NULL}
+};
+
+static const model_data_t Ricoh[] =
+{
+    {10, "\x00\x00\x00\x12\x10\x0D\x02\x00\x50\x00\x2A\xB8\x2C", "/Nashuatec P394"},
+    {0, NULL, NULL}
+};
+
+static const model_data_t Samsung16[] =
+{
+    {4, "\x00\x00\xA4\x01", "M545 6800"},
+    {0, NULL, NULL}
+};
+
+static const model_data_t Samsung5A[] =
+{
+    {4, "\x00\x00\xC0\x00", "SF-5100"},
+    {0, NULL, NULL}
+};
+
+static const model_data_t Samsung8C[] =
+{
+    {4, "\x00\x00\x01\x00", "SF-2010"},
+    {0, NULL, NULL}
+};
+
+static const model_data_t SamsungA2[] =
+{
+    {4, "\x00\x00\x80\x00", "FX-4000"},
+    {0, NULL, NULL}
+};
+
+static const model_data_t Sanyo[] =
+{
+    {10, "\x00\x00\x10\xB1\x04\x04\x04\x04\x04\x04\x04\x04\x04\x04\x04\x04\x04\x04\x04\x04\x04\x04\x04\x04\x04\x04\x04\x04\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x41\x26\xFF\xFF\x00\x00\x85\xA1", "SFX-107"},
+    {10, "\x00\x00\x00\xB1\x12\xF2\x62\xB4\x82\x0A\xF2\x2A\x12\xD2\xA2\x04\x04\x04\x04\x04\x04\x04\x04\x04\x04\x04\x04\x04\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x41\x4E\xFF\xFF\x00\x00", "MFP-510"},
+    {0, NULL, NULL}
+};
+
+static const model_data_t HP[] =
+{
+    {5, "\x20\x00\x45\x00\x0C\x04\x70\xCD\x4F\x00\x7F\x49", "LaserJet 3150"},
+    {5, "\x40\x80\x84\x01\xF0\x6A", "OfficeJet"},
+    {5, "\xC0\x00\x00\x00\x00", "OfficeJet 500"},
+    {5, "\xC0\x00\x00\x00\x00\x8B", "Fax-920"},
+    {0, NULL, NULL}
+};
+
+static const model_data_t Sharp[] =
+{
+    {32, "\x00\xCE\xB8\x80\x80\x11\x85\x0D\xDD\x00\x00\xDD\xDD\x00\x00\xDD\xDD\x00\x00\x00\x00\x00\x00\x00\x00\xED\x22\xB0\x00\x00\x90\x00", "Sharp F0-10"},
+    {33, "\x00\xCE\xB8\x80\x80\x11\x85\x0D\xDD\x00\x00\xDD\xDD\x00\x00\xDD\xDD\x00\x00\x00\x00\x00\x00\x00\x00\xED\x22\xB0\x00\x00\x90\x00\x8C", "Sharp UX-460"},
+    {33, "\x00\x4E\xB8\x80\x80\x11\x84\x0D\xDD\x00\x00\xDD\xDD\x00\x00\xDD\xDD\x00\x00\x00\x00\x00\x00\x00\x00\xED\x22\xB0\x00\x00\x90\x00\xAD", "Sharp UX-177"},
+    {33, "\x00\xCE\xB8\x00\x84\x0D\xDD\x00\x00\xDD\xDD\x00\x00\xDD\xDD\xDD\xDD\xDD\x02\x05\x28\x02\x22\x43\x29\xED\x23\x90\x00\x00\x90\x01\x00", "Sharp FO-4810"},
+    {0, NULL, NULL}
+};
+
+static const model_data_t Xerox[] =
+{
+    {10, "\x00\x08\x2D\x43\x57\x50\x61\x75\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x01\x1A\x02\x02\x10\x01\x82\x01\x30\x34", "635 Workcenter"},
+    {0, NULL, NULL}
+};
+
+static const model_data_t XeroxDA[] =
+{
+    {4, "\x00\x00\xC0\x00", "Workcentre Pro 580"},
+    {0, NULL, NULL}
+};
+
+static const model_data_t Lexmark[] =
+{
+    {4, "\x00\x80\xA0\x00", "X4270"},
+    {0, NULL, NULL}
+};
+
+static const model_data_t JetFax[] =
+{
+    {6, "\x01\x00\x45\x00\x0D\x7F", "M910e"},
+    {0, NULL, NULL}
+};
+
+static const model_data_t PitneyBowes[] = 
+{
+    {6, "\x79\x91\xB1\xB8\x7A\xD8", "9550"},
+    {0, NULL, NULL}
+};
+
+static const model_data_t Dialogic[] = 
+{
+    {8, "\x56\x8B\x06\x55\x00\x15\x00\x00", "VFX/40ESC"},
+    {0, NULL, NULL}
+};
+
+static const model_data_t Muratec45[] =
+{
+    {10, "\xF4\x91\xFF\xFF\xFF\x42\x2A\xBC\x01\x57", "M4700"},
+    {0, NULL, NULL}
+};
+
+/* Muratec uses unregistered Japan code "00 00 48" */
+static const model_data_t Muratec48[] =
+{
+    {3, "\x53\x53\x61", "M620"},
+    {0, NULL, NULL}
+};
+
+/*
+ * Country code first byte, then manufacturer is last two bytes. See T.35.
+ * Apparently Germany issued some manufacturer codes before the two-byte
+ * standard was accepted, and so some few German manufacturers are
+ * identified by a single manufacturer byte.
+ *
+ * T.30 5.3.6.2.7 (2003) states that the NSF FIF is transmitted
+ * in MSB2LSB order.  Revisions of T.30 prior to 2003 did not 
+ * contain explicit specification as to the transmit bit order.
+ * (Although it did otherwise state that all HDLC frame data should
+ * be in MSB order except as noted.)  Because CSI, TSI, and other
+ * prologue frames were in LSB order by way of an exception to the
+ * general rule (T.30 5.3.6.2.4-11) many manufacturers assumed that
+ * NSF should also be in LSB order.  Consequently there will be
+ * some country-code "masquerading" as a terminal may use the
+ * proper country-code, but with an inverted bit order.
+ *
+ * Thus, country code x61 (Korea) turns into x86 (Papua New Guinea),
+ * code xB5 (USA) turns into xAD (Tunisia), code x26 (China) turns
+ * into x64 (Lebanon), code x04 (Germany) turns into x20 (Canada), 
+ * and code x3D (France) turns into xBC (Vietnam).
+ *
+ * For the most part it should be safe to identify a manufacturer
+ * both with the MSB and LSB ordered bits, as the "masqueraded" country
+ * is likely to not be actively assigning T.38 manufacturer codes.
+ * However, some manufacturers (e.g. Microsoft) may use MSB for the
+ * country code and LSB for the rest of the NSF, and so basically this
+ * table must be verified and corrected against actual real-world
+ * results.
+ */
+static const nsf_data_t known_nsf[] =
+{
+    /* Japan */
+    {"\x00\x00\x00", 3, "unknown - indeterminate", TRUE, NULL},
+    {"\x00\x00\x01", 3, "Anjitsu", FALSE, NULL},
+    {"\x00\x00\x02", 3, "Nippon Telephone", FALSE, NULL},
+    {"\x00\x00\x05", 3, "Mitsuba Electric", FALSE, NULL},
+    {"\x00\x00\x06", 3, "Master Net", FALSE, NULL},
+    {"\x00\x00\x09", 3, "Xerox/Toshiba", TRUE, Xerox},
+    {"\x00\x00\x0A", 3, "Kokusai", FALSE, NULL},
+    {"\x00\x00\x0D", 3, "Logic System International", FALSE, NULL},
+    {"\x00\x00\x0E", 3, "Panasonic", FALSE, Panasonic0E},
+    {"\x00\x00\x11", 3, "Canon", FALSE, Canon},
+    {"\x00\x00\x15", 3, "Toyotsushen Machinery", FALSE, NULL},
+    {"\x00\x00\x16", 3, "System House Mind", FALSE, NULL},
+    {"\x00\x00\x19", 3, "Xerox", TRUE, NULL},
+    {"\x00\x00\x1D", 3, "Hitachi Software", FALSE, NULL},
+    {"\x00\x00\x21", 3, "OKI Electric/Lanier", TRUE, NULL},
+    {"\x00\x00\x25", 3, "Ricoh", TRUE, Ricoh},
+    {"\x00\x00\x26", 3, "Konica", FALSE, NULL},
+    {"\x00\x00\x29", 3, "Japan Wireless", FALSE, NULL},
+    {"\x00\x00\x2D", 3, "Sony", FALSE, NULL},
+    {"\x00\x00\x31", 3, "Sharp/Olivetti", FALSE, Sharp},
+    {"\x00\x00\x35", 3, "Kogyu", FALSE, NULL},
+    {"\x00\x00\x36", 3, "Japan Telecom", FALSE, NULL},
+    {"\x00\x00\x3D", 3, "IBM Japan", FALSE, NULL},
+    {"\x00\x00\x39", 3, "Panasonic", FALSE, NULL},
+    {"\x00\x00\x41", 3, "Swasaki Communication", FALSE, NULL},
+    {"\x00\x00\x45", 3, "Muratec", FALSE, Muratec45},
+    {"\x00\x00\x46", 3, "Pheonix", FALSE, NULL},
+    {"\x00\x00\x48", 3, "Muratec", FALSE, Muratec48},	// not registered
+    {"\x00\x00\x49", 3, "Japan Electric", FALSE, NULL},
+    {"\x00\x00\x4D", 3, "Okura Electric", FALSE, NULL},
+    {"\x00\x00\x51", 3, "Sanyo", FALSE, Sanyo},
+    {"\x00\x00\x55", 3, "unknown - Japan 55", FALSE, NULL},
+    {"\x00\x00\x56", 3, "Brother", FALSE, Brother},
+    {"\x00\x00\x59", 3, "Fujitsu", FALSE, NULL},
+    {"\x00\x00\x5D", 3, "Kuoni", FALSE, NULL},
+    {"\x00\x00\x61", 3, "Casio", FALSE, NULL},
+    {"\x00\x00\x65", 3, "Tateishi Electric", FALSE, NULL},
+    {"\x00\x00\x66", 3, "Utax/Mita", TRUE, NULL},
+    {"\x00\x00\x69", 3, "Hitachi Production", FALSE, NULL},
+    {"\x00\x00\x6D", 3, "Hitachi Telecom", FALSE, NULL},
+    {"\x00\x00\x71", 3, "Tamura Electric Works", FALSE, NULL},
+    {"\x00\x00\x75", 3, "Tokyo Electric Corp.", FALSE, NULL},
+    {"\x00\x00\x76", 3, "Advance", FALSE, NULL},
+    {"\x00\x00\x79", 3, "Panasonic", FALSE, Panasonic79},
+    {"\x00\x00\x7D", 3, "Seiko", FALSE, NULL},
+    {"\x00\x08\x00", 3, "Daiko", FALSE, NULL},
+    {"\x00\x10\x00", 3, "Funai Electric", FALSE, NULL},
+    {"\x00\x20\x00", 3, "Eagle System", FALSE, NULL},
+    {"\x00\x30\x00", 3, "Nippon Business Systems", FALSE, NULL},
+    {"\x00\x40\x00", 3, "Comtron", FALSE, NULL},
+    {"\x00\x48\x00", 3, "Cosmo Consulting", FALSE, NULL},
+    {"\x00\x50\x00", 3, "Orion Electric", FALSE, NULL},
+    {"\x00\x60\x00", 3, "Nagano Nippon", FALSE, NULL},
+    {"\x00\x70\x00", 3, "Kyocera", FALSE, NULL},
+    {"\x00\x80\x00", 3, "Kanda Networks", FALSE, NULL},
+    {"\x00\x88\x00", 3, "Soft Front", FALSE, NULL},
+    {"\x00\x90\x00", 3, "Arctic", FALSE, NULL},
+    {"\x00\xA0\x00", 3, "Nakushima", FALSE, NULL},
+    {"\x00\xB0\x00", 3, "Minolta", FALSE, NULL},
+    {"\x00\xC0\x00", 3, "Tohoku Pioneer", FALSE, NULL},
+    {"\x00\xD0\x00", 3, "USC", FALSE, NULL},
+    {"\x00\xE0\x00", 3, "Hiboshi", FALSE, NULL},
+    {"\x00\xF0\x00", 3, "Sumitomo Electric", FALSE, NULL},
+
+    /* Germany */
+    {"\x20\x09",     2, "ITK Institut für Telekommunikation GmbH & Co KG", FALSE, NULL},
+    {"\x20\x11",     2, "Dr. Neuhaus Mikroelektronik", FALSE, NULL},
+    {"\x20\x21",     2, "ITO Communication", FALSE, NULL},
+    {"\x20\x31",     2, "mbp Kommunikationssysteme GmbH", FALSE, NULL},
+    {"\x20\x41",     2, "Siemens", FALSE, NULL},
+    {"\x20\x42",     2, "Deutsche Telekom AG", FALSE, NULL},
+    {"\x20\x51",     2, "mps Software", FALSE, NULL},
+    {"\x20\x61",     2, "Hauni Elektronik", FALSE, NULL},
+    {"\x20\x71",     2, "Digitronic computersysteme gmbh", FALSE, NULL},
+    {"\x20\x81\x00", 3, "Innovaphone GmbH", FALSE, NULL},
+    {"\x20\x81\x40", 3, "TEDAS Gesellschaft für Telekommunikations-, Daten- und Audiosysteme mbH", FALSE, NULL},
+    {"\x20\x81\x80", 3, "AVM Audiovisuelles Marketing und Computersysteme GmbH", FALSE, NULL},
+    {"\x20\x81\xC0", 3, "EICON Technology Research GmbH", FALSE, NULL},
+    {"\x20\xB1",     2, "Schneider Rundfunkwerke AG", FALSE, NULL},
+    {"\x20\xC2",     2, "Deutsche Telekom AG", FALSE, NULL},
+    {"\x20\xD1",     2, "Ferrari electronik GmbH", FALSE, NULL},
+    {"\x20\xF1",     2, "DeTeWe - Deutsche Telephonwerke AG & Co", FALSE, NULL},
+    {"\x20\xFF",     2, "Germany Regional Code", FALSE, NULL},
+
+    /* China */
+    {"\x64\x00\x00", 3, "unknown - China 00 00", FALSE, NULL},
+    {"\x64\x01\x00", 3, "unknown - China 01 00", FALSE, NULL},
+    {"\x64\x01\x01", 3, "unknown - China 01 01", FALSE, NULL},
+    {"\x64\x01\x02", 3, "unknown - China 01 02", FALSE, NULL},
+
+    /* France */
+    {"\xBC\x53\x01", 3, "Minolta", FALSE, NULL},
+
+    /* Korea */
+    {"\x86\x00\x02", 3, "unknown - Korea 02", FALSE, NULL},
+    {"\x86\x00\x06", 3, "unknown - Korea 06", FALSE, NULL},
+    {"\x86\x00\x08", 3, "unknown - Korea 08", FALSE, NULL},
+    {"\x86\x00\x0A", 3, "unknown - Korea 0A", FALSE, NULL},
+    {"\x86\x00\x0E", 3, "unknown - Korea 0E", FALSE, NULL},
+    {"\x86\x00\x10", 3, "Samsung", FALSE, NULL},
+    {"\x86\x00\x11", 3, "unknown - Korea 11", FALSE, NULL},
+    {"\x86\x00\x16", 3, "Samsung", FALSE, Samsung16},
+    {"\x86\x00\x1A", 3, "unknown - Korea 1A", FALSE, NULL},
+    {"\x86\x00\x40", 3, "unknown - Korea 40", FALSE, NULL},
+    {"\x86\x00\x48", 3, "unknown - Korea 48", FALSE, NULL},
+    {"\x86\x00\x52", 3, "unknown - Korea 52", FALSE, NULL},
+    {"\x86\x00\x5A", 3, "Samsung", FALSE, Samsung5A},
+    {"\x86\x00\x5E", 3, "unknown - Korea 5E", FALSE, NULL},
+    {"\x86\x00\x66", 3, "unknown - Korea 66", FALSE, NULL},
+    {"\x86\x00\x6E", 3, "unknown - Korea 6E", FALSE, NULL},
+    {"\x86\x00\x82", 3, "unknown - Korea 82", FALSE, NULL},
+    {"\x86\x00\x88", 3, "unknown - Korea 88", FALSE, NULL},
+    {"\x86\x00\x8A", 3, "unknown - Korea 8A", FALSE, NULL},
+    {"\x86\x00\x8C", 3, "Samsung", FALSE, Samsung8C},
+    {"\x86\x00\x92", 3, "unknown - Korea 92", FALSE, NULL},
+    {"\x86\x00\x98", 3, "Samsung", FALSE, NULL},
+    {"\x86\x00\xA2", 3, "Samsung", FALSE, SamsungA2},
+    {"\x86\x00\xA4", 3, "unknown - Korea A4", FALSE, NULL},
+    {"\x86\x00\xC2", 3, "Samsung", FALSE, NULL},
+    {"\x86\x00\xC9", 3, "unknown - Korea C9", FALSE, NULL},
+    {"\x86\x00\xCC", 3, "unknown - Korea CC", FALSE, NULL},
+    {"\x86\x00\xD2", 3, "unknown - Korea D2", FALSE, NULL},
+    {"\x86\x00\xDA", 3, "Xerox", FALSE, XeroxDA},
+    {"\x86\x00\xE2", 3, "unknown - Korea E2", FALSE, NULL},
+    {"\x86\x00\xEC", 3, "unknown - Korea EC", FALSE, NULL},
+    {"\x86\x00\xEE", 3, "unknown - Korea EE", FALSE, NULL},
+
+    /* United Kingdom */
+    {"\xB4\x00\xB0", 3, "DCE", FALSE, NULL},
+    {"\xB4\x00\xB1", 3, "Hasler", FALSE, NULL},
+    {"\xB4\x00\xB2", 3, "Interquad", FALSE, NULL},
+    {"\xB4\x00\xB3", 3, "Comwave", FALSE, NULL},
+    {"\xB4\x00\xB4", 3, "Iconographic", FALSE, NULL},
+    {"\xB4\x00\xB5", 3, "Wordcraft", FALSE, NULL},
+    {"\xB4\x00\xB6", 3, "Acorn", FALSE, NULL},
+
+    /* United States */
+    {"\xAD\x00\x00", 3, "Pitney Bowes", FALSE, PitneyBowes},
+    {"\xAD\x00\x0C", 3, "Dialogic", FALSE, NULL},
+    {"\xAD\x00\x15", 3, "Lexmark", FALSE, Lexmark},
+    {"\xAD\x00\x16", 3, "JetFax", FALSE, JetFax},
+    {"\xAD\x00\x24", 3, "Octel", FALSE, NULL},
+    {"\xAD\x00\x36", 3, "HP", FALSE, HP},
+    {"\xAD\x00\x42", 3, "FaxTalk", FALSE, NULL},
+    {"\xAD\x00\x44", 3, NULL, TRUE, NULL},
+    {"\xAD\x00\x46", 3, "BrookTrout", FALSE, NULL},
+    {"\xAD\x00\x51", 3, "Telogy Networks", FALSE, NULL},
+    {"\xAD\x00\x55", 3, "HylaFAX", FALSE, NULL},
+    {"\xAD\x00\x5C", 3, "IBM", FALSE, NULL},
+    {"\xAD\x00\x98", 3, "unknown - USA 98", TRUE, NULL},
+    {"\xB5\x00\x01", 3, "Picturetel", FALSE, NULL},
+    {"\xB5\x00\x20", 3, "Conexant", FALSE, NULL},
+    {"\xB5\x00\x22", 3, "Comsat", FALSE, NULL},
+    {"\xB5\x00\x24", 3, "Octel", FALSE, NULL},
+    {"\xB5\x00\x26", 3, "ROLM", FALSE, NULL},
+    {"\xB5\x00\x28", 3, "SOFNET", FALSE, NULL},
+    {"\xB5\x00\x29", 3, "TIA TR-29 Committee", FALSE, NULL},
+    {"\xB5\x00\x2A", 3, "STF Tech", FALSE, NULL},
+    {"\xB5\x00\x2C", 3, "HKB", FALSE, NULL},
+    {"\xB5\x00\x2E", 3, "Delrina", FALSE, NULL},
+    {"\xB5\x00\x30", 3, "Dialogic", FALSE, NULL},
+    {"\xB5\x00\x32", 3, "Applied Synergy", FALSE, NULL},
+    {"\xB5\x00\x34", 3, "Syncro Development", FALSE, NULL},
+    {"\xB5\x00\x36", 3, "Genoa", FALSE, NULL},
+    {"\xB5\x00\x38", 3, "Texas Instruments", FALSE, NULL},
+    {"\xB5\x00\x3A", 3, "IBM", FALSE, NULL},
+    {"\xB5\x00\x3C", 3, "ViaSat", FALSE, NULL},
+    {"\xB5\x00\x3E", 3, "Ericsson", FALSE, NULL},
+    {"\xB5\x00\x42", 3, "Bogosian", FALSE, NULL},
+    {"\xB5\x00\x44", 3, "Adobe", FALSE, NULL},
+    {"\xB5\x00\x46", 3, "Fremont Communications", FALSE, NULL},
+    {"\xB5\x00\x48", 3, "Hayes", FALSE, NULL},
+    {"\xB5\x00\x4A", 3, "Lucent", FALSE, NULL},
+    {"\xB5\x00\x4C", 3, "Data Race", FALSE, NULL},
+    {"\xB5\x00\x4E", 3, "TRW", FALSE, NULL},
+    {"\xB5\x00\x52", 3, "Audiofax", FALSE, NULL},
+    {"\xB5\x00\x54", 3, "Computer Automation", FALSE, NULL},
+    {"\xB5\x00\x56", 3, "Serca", FALSE, NULL},
+    {"\xB5\x00\x58", 3, "Octocom", FALSE, NULL},
+    {"\xB5\x00\x5C", 3, "Power Solutions", FALSE, NULL},
+    {"\xB5\x00\x5A", 3, "Digital Sound", FALSE, NULL},
+    {"\xB5\x00\x5E", 3, "Pacific Data", FALSE, NULL},
+    {"\xB5\x00\x60", 3, "Commetrex", FALSE, NULL},
+    {"\xB5\x00\x62", 3, "BrookTrout", FALSE, NULL},
+    {"\xB5\x00\x64", 3, "Gammalink", FALSE, NULL},
+    {"\xB5\x00\x66", 3, "Castelle", FALSE, NULL},
+    {"\xB5\x00\x68", 3, "Hybrid Fax", FALSE, NULL},
+    {"\xB5\x00\x6A", 3, "Omnifax", FALSE, NULL},
+    {"\xB5\x00\x6C", 3, "HP", FALSE, NULL},
+    {"\xB5\x00\x6E", 3, "Microsoft", FALSE, NULL},
+    {"\xB5\x00\x72", 3, "Speaking Devices", FALSE, NULL},
+    {"\xB5\x00\x74", 3, "Compaq", FALSE, NULL},
+/*
+    {"\xB5\x00\x76", 3, "Trust - Cryptek", FALSE, NULL},	// collision with Microsoft
+*/
+    {"\xB5\x00\x76", 3, "Microsoft", FALSE, NULL},		// uses LSB for country but MSB for manufacturer
+    {"\xB5\x00\x78", 3, "Cylink", FALSE, NULL},
+    {"\xB5\x00\x7A", 3, "Pitney Bowes", FALSE, NULL},
+    {"\xB5\x00\x7C", 3, "Digiboard", FALSE, NULL},
+    {"\xB5\x00\x7E", 3, "Codex", FALSE, NULL},
+    {"\xB5\x00\x82", 3, "Wang Labs", FALSE, NULL},
+    {"\xB5\x00\x84", 3, "Netexpress Communications", FALSE, NULL},
+    {"\xB5\x00\x86", 3, "Cable-Sat", FALSE, NULL},
+    {"\xB5\x00\x88", 3, "MFPA", FALSE, NULL},
+    {"\xB5\x00\x8A", 3, "Telogy Networks", FALSE, NULL},
+    {"\xB5\x00\x8E", 3, "Telecom Multimedia Systems", FALSE, NULL},
+    {"\xB5\x00\x8C", 3, "AT&T", FALSE, NULL},
+    {"\xB5\x00\x92", 3, "Nuera", FALSE, NULL},
+    {"\xB5\x00\x94", 3, "K56flex", FALSE, NULL},
+    {"\xB5\x00\x96", 3, "MiBridge", FALSE, NULL},
+    {"\xB5\x00\x98", 3, "Xerox", FALSE, NULL},
+    {"\xB5\x00\x9A", 3, "Fujitsu", FALSE, NULL},
+    {"\xB5\x00\x9B", 3, "Fujitsu", FALSE, NULL},
+    {"\xB5\x00\x9C", 3, "Natural Microsystems", FALSE, NULL},
+    {"\xB5\x00\x9E", 3, "CopyTele", FALSE, NULL},
+    {"\xB5\x00\xA2", 3, "Murata", FALSE, NULL},
+    {"\xB5\x00\xA4", 3, "Lanier", FALSE, NULL},
+    {"\xB5\x00\xA6", 3, "Qualcomm", FALSE, NULL},
+    {"\xB5\x00\xAA", 3, "HylaFAX", FALSE, NULL},		// we did it backwards for a while
+    {NULL, 0, NULL, FALSE, NULL}
+};
+
+#if 0
+void nsf_find_station_id(int reverse_order)
+{
+    const char *id = NULL;
+    int idSize = 0;
+    const char *maxId = NULL;
+    int maxIdSize = 0;
+    const char *p;
+
+    /* Trying to find the longest printable ASCII sequence */
+    for (p = (const char *) nsf + T35_VENDOR_ID_LEN, *end = p + nsf.length();
+         p < end;
+         p++)
+    {
+        if (isprint(*p))
+        {
+            if (!idSize++)
+                id = p;
+            if (idSize > maxIdSize)
+            {
+                max_id = id;
+                max_id_size = idSize;
+            }
+        }
+        else
+        {
+            id = NULL;
+            id_size = 0;
+        }
+    }
+    
+    /* Minimum acceptable id length */
+    const int MinIdSize = 4;
+
+    if (maxIdSize >= min_id_size)
+    {
+        stationId.resize(0);
+        const char *p;
+        int dir;
+
+        if (reverseOrder)
+        {
+            p = maxId + maxIdSize - 1;
+            dir = -1;
+        }
+        else
+        {
+            p = maxId;
+            dir = 1;
+        }
+        for (int i = 0;  i < maxIdSize;  i++)
+        {
+            stationId.append(*p);
+            p += dir;
+        }
+        station_id_decoded = TRUE;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+#endif
+
+int t35_decode(const uint8_t *msg, int len, const char **country, const char **vendor, const char **model)
+{
+    int vendor_decoded;
+    const nsf_data_t *p;
+    const model_data_t *pp;
+
+    vendor_decoded = FALSE;
+    if (country)
+        *country = NULL;
+    if (vendor)
+        *vendor = NULL;
+    if (model)
+        *model = NULL;
+    if (country)
+    {
+        /* We need to apply realism over accuracy, though it blocks out some countries.
+           It is very rare to find a machine from any country but the following:
+        
+           Japan 0x00 (no confusion)
+           Germany 0x04 (0x20) (Canada/Germany confusion)
+           China 0x26 (0x64) (China/Lebanon confusion)
+           Korea 0x61 (0x86) (Korea/Papua New Guinea confusion)
+           UK 0xB4 (0x2D) (UK/Cyprus confusion)
+           USA 0xB5 (0xAD) (USA/Tunisia confusion)
+           France 0x3D (0xBC) (France/Vietnam confusion)
+
+           If we force the most likely of the two possible countries (forward or bit reversed),
+           the only mixup with any realistic probability is the Canada/Germany confusion. We
+           will just live with this, and force the more likely countries. */
+        switch (msg[0])
+        {
+        case 0x20:
+            /* Force Germany */
+            *country = t35_country_codes[0x04];
+            break;
+        case 0x64:
+            /* Force China */
+            *country = t35_country_codes[0x26];
+            break;
+        case 0x86:
+            /* Force Korea */
+            *country = t35_country_codes[0x61];
+            break;
+        case 0x2D:
+            /* Force UK */
+            *country = t35_country_codes[0xB4];
+            break;
+        case 0xAD:
+            /* Force USA */
+            *country = t35_country_codes[0xB5];
+            break;
+        case 0xBC:
+            /* Force France */
+            *country = t35_country_codes[0x3D];
+            break;
+        default:
+            /* Try the country code at face value, then bit reversed */
+            if (t35_country_codes[msg[0]])
+                *country = t35_country_codes[msg[0]];
+            else if (t35_country_codes[bit_reverse8(msg[0])])
+                *country = t35_country_codes[bit_reverse8(msg[0])];
+            break;
+        }
+    }
+    for (p = known_nsf;  p->vendor_id;  p++)
+    {
+        if (len >= p->vendor_id_len
+            &&
+            memcmp(p->vendor_id, msg, p->vendor_id_len) == 0)
+        {
+            if (p->vendor_name  &&  vendor)
+                *vendor = p->vendor_name;
+            if (p->known_models  &&  model)
+            {
+                for (pp = p->known_models;  pp->model_id;  pp++)
+                {
+                    if (len == p->vendor_id_len + pp->model_id_size
+                        &&
+                        memcmp(pp->model_id, &msg[p->vendor_id_len], pp->model_id_size) == 0)
+                    {
+                        *model = pp->model_name;
+                        break;
+                    }
+                }
+            }
+#if 0
+            findStationId(p->inverse_station_id_order);
+#endif
+            vendor_decoded = TRUE;
+            break;
+        }
+    }
+#if 0
+    if (!vendor_found())
+        find_station_id(0);
+#endif
+    return vendor_decoded;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/t38_core.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/t38_core.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,900 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * t38_core.c - Encode and decode the ASN.1 of a T.38 IFP message
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2005, 2006 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: t38_core.c,v 1.44 2008/07/02 14:48:26 steveu Exp $
+ */
+
+/*! \file */
+
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
+
+#include <inttypes.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <time.h>
+#include <string.h>
+#include "floating_fudge.h"
+#if defined(HAVE_TGMATH_H)
+#include <tgmath.h>
+#endif
+#if defined(HAVE_MATH_H)
+#include <math.h>
+#endif
+#include <assert.h>
+#include <memory.h>
+#include <tiffio.h>
+
+#include "spandsp/telephony.h"
+#include "spandsp/logging.h"
+#include "spandsp/bit_operations.h"
+#include "spandsp/t38_core.h"
+
+#define ACCEPTABLE_SEQ_NO_OFFSET    2000
+
+const char *t38_indicator_to_str(int indicator)
+{
+    switch (indicator)
+    {
+    case T38_IND_NO_SIGNAL:
+        return "no-signal";
+    case T38_IND_CNG:
+        return "cng";
+    case T38_IND_CED:
+        return "ced";
+    case T38_IND_V21_PREAMBLE:
+        return "v21-preamble";
+    case T38_IND_V27TER_2400_TRAINING:
+        return "v27-2400-training";
+    case T38_IND_V27TER_4800_TRAINING:
+        return "v27-4800-training";
+    case T38_IND_V29_7200_TRAINING:
+        return "v29-7200-training";
+    case T38_IND_V29_9600_TRAINING:
+        return "v29-9600-training";
+    case T38_IND_V17_7200_SHORT_TRAINING:
+        return "v17-7200-short-training";
+    case T38_IND_V17_7200_LONG_TRAINING:
+        return "v17-7200-long-training";
+    case T38_IND_V17_9600_SHORT_TRAINING:
+        return "v17-9600-short-training";
+    case T38_IND_V17_9600_LONG_TRAINING:
+        return "v17-9600-long-training";
+    case T38_IND_V17_12000_SHORT_TRAINING:
+        return "v17-12000-short-training";
+    case T38_IND_V17_12000_LONG_TRAINING:
+        return "v17-12000-long-training";
+    case T38_IND_V17_14400_SHORT_TRAINING:
+        return "v17-14400-short-training";
+    case T38_IND_V17_14400_LONG_TRAINING:
+        return "v17-14400-long-training";
+    case T38_IND_V8_ANSAM:
+        return "v8-ansam";
+    case T38_IND_V8_SIGNAL:
+        return "v8-signal";
+    case T38_IND_V34_CNTL_CHANNEL_1200:
+        return "v34-cntl-channel-1200";
+    case T38_IND_V34_PRI_CHANNEL:
+        return "v34-pri-channel";
+    case T38_IND_V34_CC_RETRAIN:
+        return "v34-CC-retrain";
+    case T38_IND_V33_12000_TRAINING:
+        return "v33-12000-training";
+    case T38_IND_V33_14400_TRAINING:
+        return "v33-14400-training";
+    }
+    return "???";
+}
+/*- End of function --------------------------------------------------------*/
+
+const char *t38_data_type_to_str(int data_type)
+{
+    switch (data_type)
+    {
+    case T38_DATA_V21:
+        return "v21";
+    case T38_DATA_V27TER_2400:
+        return "v27-2400";
+    case T38_DATA_V27TER_4800:
+        return "v27-4800";
+    case T38_DATA_V29_7200:
+        return "v29-7200";
+    case T38_DATA_V29_9600:
+        return "v29-9600";
+    case T38_DATA_V17_7200:
+        return "v17-7200";
+    case T38_DATA_V17_9600:
+        return "v17-9600";
+    case T38_DATA_V17_12000:
+        return "v17-12000";
+    case T38_DATA_V17_14400:
+        return "v17-14400";
+    case T38_DATA_V8:
+        return "v8";
+    case T38_DATA_V34_PRI_RATE:
+        return "v34-pri-rate";
+    case T38_DATA_V34_CC_1200:
+        return "v34-CC-1200";
+    case T38_DATA_V34_PRI_CH:
+        return "v34-pri-vh";
+    case T38_DATA_V33_12000:
+        return "v33-12000";
+    case T38_DATA_V33_14400:
+        return "v33-14400";
+    }
+    return "???";
+}
+/*- End of function --------------------------------------------------------*/
+
+const char *t38_field_type_to_str(int field_type)
+{
+    switch (field_type)
+    {
+    case T38_FIELD_HDLC_DATA:
+        return "hdlc-data";
+    case T38_FIELD_HDLC_SIG_END:
+        return "hdlc-sig-end";
+    case T38_FIELD_HDLC_FCS_OK:
+        return "hdlc-fcs-OK";
+    case T38_FIELD_HDLC_FCS_BAD:
+        return "hdlc-fcs-BAD";
+    case T38_FIELD_HDLC_FCS_OK_SIG_END:
+        return "hdlc-fcs-OK-sig-end";
+    case T38_FIELD_HDLC_FCS_BAD_SIG_END:
+        return "hdlc-fcs-BAD-sig-end";
+    case T38_FIELD_T4_NON_ECM_DATA:
+        return "t4-non-ecm-data";
+    case T38_FIELD_T4_NON_ECM_SIG_END:
+        return "t4-non-ecm-sig-end";
+    case T38_FIELD_CM_MESSAGE:
+        return "cm-message";
+    case T38_FIELD_JM_MESSAGE:
+        return "jm-message";
+    case T38_FIELD_CI_MESSAGE:
+        return "ci-message";
+    case T38_FIELD_V34RATE:
+        return "v34rate";
+    }
+    return "???";
+}
+/*- End of function --------------------------------------------------------*/
+
+const char *t38_cm_profile_to_str(int profile)
+{
+    switch (profile)
+    {
+    case '1':
+        return "G3 Facsimile Terminal: (Sending Facsimile)";
+    case '2':
+        return "G3 Facsimile Terminal: (Receiving Facsimile)";
+    case '3':
+        return "V.34 HDX and G3 Facsimile Terminal: (Sending Facsimile)";
+    case '4':
+        return "V.34 HDX and G3 Facsimile Terminal: (Receiving Facsimile)";
+    case '5':
+        return "V.34 HDX-only Facsimile Terminal: (Sending Facsimile)";
+    case '6':
+        return "V.34 HDX-only Facsimile Terminal: (Receiving Facsimile)";
+    }
+    return "???";
+}
+/*- End of function --------------------------------------------------------*/
+
+const char *t38_jm_to_str(const uint8_t *data, int len)
+{
+    if (len < 2)
+        return "???";
+    switch (data[0])
+    {
+    case 'A':
+        switch (data[1])
+        {
+        case '0':
+            return "ACK";
+        }
+        break;
+    case 'N':
+        switch (data[1])
+        {
+        case '0':
+            return "NACK: No compatible mode available";
+        case '1':
+            /* Response for profiles 1 and 2 */
+            return "NACK: No V.34 fax, use G3 fax";
+        case '2':
+            /* Response for profiles 5 and 6 */
+            return "NACK: V.34 fax only.";
+        }
+        break;
+    }
+    return "???";
+}
+/*- End of function --------------------------------------------------------*/
+
+int t38_v34rate_to_bps(const uint8_t *data, int len)
+{
+    int i;
+    int rate;
+
+    if (len < 3)
+        return -1;
+    for (i = 0, rate = 0;  i < 3;  i++)
+    {
+        if (data[i] < '0'  ||  data[i] > '9')
+            return -1;
+        rate = rate*10 + data[i] - '0';
+    }
+    return rate*100;
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ int classify_seq_no_offset(int expected, int actual)
+{
+    /* Classify the mismatch between expected and actual sequence numbers
+       according to whether the actual is a little in the past (late), a
+       little in the future (some packets have been lost), or a large jump
+       that represents the sequence being lost (possibly when some RTP
+       gets dumped to a UDPTL port). */
+    /* This assumes they are not equal */
+    if (expected > actual)
+    {
+        if (expected > actual + 0x10000 - ACCEPTABLE_SEQ_NO_OFFSET)
+        {
+            /* In the near future */
+            return 1;
+        }
+        if (expected < actual + ACCEPTABLE_SEQ_NO_OFFSET)
+        {
+            /* In the recent past */
+            return -1;
+        }
+    }
+    else
+    {
+        if (expected + ACCEPTABLE_SEQ_NO_OFFSET > actual)
+        {
+            /* In the near future */
+            return 1;
+        }
+        if (expected + 0x10000 - ACCEPTABLE_SEQ_NO_OFFSET < actual)
+        {
+            /* In the recent past */
+            return -1;
+        }
+    }
+    /* There has been a huge step in the sequence */
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int t38_core_rx_ifp_packet(t38_core_state_t *s, const uint8_t *buf, int len, uint16_t seq_no)
+{
+    int i;
+    int t30_indicator;
+    int t30_data;
+    int ptr;
+    int other_half;
+    int numocts;
+    int log_seq_no;
+    const uint8_t *msg;
+    unsigned int count;
+    unsigned int t30_field_type;
+    uint8_t type;
+    uint8_t data_field_present;
+    uint8_t field_data_present;
+    char tag[20];
+
+    log_seq_no = (s->check_sequence_numbers)  ?  seq_no  :  s->rx_expected_seq_no;
+
+    if (span_log_test(&s->logging, SPAN_LOG_FLOW))
+    {
+        sprintf(tag, "Rx %5d: IFP", log_seq_no);
+        span_log_buf(&s->logging, SPAN_LOG_FLOW, tag, buf, len);
+    }
+    if (len < 1)
+    {
+        span_log(&s->logging, SPAN_LOG_PROTOCOL_WARNING, "Rx %5d: Bad packet length - %d\n", log_seq_no, len);
+        return -1;
+    }
+    if (s->check_sequence_numbers)
+    {
+        seq_no &= 0xFFFF;
+        if (seq_no != s->rx_expected_seq_no)
+        {
+            /* An expected value of -1 indicates this is the first received packet, and will accept
+               anything for that. We can't assume they will start from zero, even though they should. */
+            if (s->rx_expected_seq_no != -1)
+            {
+                /* We have a packet with a serial number that is not in sequence. The cause could be:
+                    - 1. a repeat copy of a recent packet. Many T.38 implementations can preduce quite a lot of these.
+                    - 2. a late packet, whose point in the sequence we have already passed.
+                    - 3. the result of a hop in the sequence numbers cause by something weird from the other
+                         end. Stream switching might cause this
+                    - 4. missing packets.
+    
+                    In cases 1 and 2 we need to drop this packet. In case 2 it might make sense to try to do
+                    something with it in the terminal case. Currently we don't. For gateway operation it will be
+                    too late to do anything useful.
+                 */
+                if (((seq_no + 1) & 0xFFFF) == s->rx_expected_seq_no)
+                {
+                    /* Assume this is truly a repeat packet, and don't bother checking its contents. */
+                    span_log(&s->logging, SPAN_LOG_FLOW, "Rx %5d: Repeat packet number\n", log_seq_no);
+                    return 0;
+                }
+                /* Distinguish between a little bit out of sequence, and a huge hop. */
+                switch (classify_seq_no_offset(s->rx_expected_seq_no, seq_no))
+                {
+                case -1:
+                    /* This packet is in the near past, so its late. */
+                    span_log(&s->logging, SPAN_LOG_FLOW, "Rx %5d: Late packet - expected %d\n", log_seq_no, s->rx_expected_seq_no);
+                    return 0;
+                case 1:
+                    /* This packet is in the near future, so some packets have been lost */
+                    span_log(&s->logging, SPAN_LOG_FLOW, "Rx %5d: Missing from %d\n", log_seq_no, s->rx_expected_seq_no);
+                    s->rx_missing_handler(s, s->rx_user_data, s->rx_expected_seq_no, seq_no);
+                    s->missing_packets += (seq_no - s->rx_expected_seq_no);
+                    break;
+                default:
+                    /* The sequence has jumped wildly */
+                    span_log(&s->logging, SPAN_LOG_FLOW, "Rx %5d: Sequence restart\n", log_seq_no);
+                    s->rx_missing_handler(s, s->rx_user_data, -1, -1);
+                    s->missing_packets++;
+                    break;
+                }
+            }
+            s->rx_expected_seq_no = seq_no;
+        }
+        s->rx_expected_seq_no = (s->rx_expected_seq_no + 1) & 0xFFFF;
+    }
+    else
+    {
+        s->rx_expected_seq_no++;
+    }
+    data_field_present = (buf[0] >> 7) & 1;
+    type = (buf[0] >> 6) & 1;
+    ptr = 0;
+    switch (type)
+    {
+    case T38_TYPE_OF_MSG_T30_INDICATOR:
+        /* Indicators should never have a data field */
+        if (data_field_present)
+        {
+            span_log(&s->logging, SPAN_LOG_PROTOCOL_WARNING, "Rx %5d: Data field with indicator\n", log_seq_no);
+            return -1;
+        }
+        /* Any received indicator should mean we no longer have a valid concept of "last received data/field type". */
+        s->current_rx_data_type = -1;
+        s->current_rx_field_type = -1;
+        if ((buf[0] & 0x20))
+        {
+            /* Extension */
+            if (len != 2)
+            {
+                span_log(&s->logging, SPAN_LOG_PROTOCOL_WARNING, "Rx %5d: Invalid length for indicator (A)\n", log_seq_no);
+                return -1;
+            }
+            t30_indicator = T38_IND_V8_ANSAM + (((buf[0] << 2) & 0x3C) | ((buf[1] >> 6) & 0x3));
+            if (t30_indicator > T38_IND_V33_14400_TRAINING)
+            {
+                span_log(&s->logging, SPAN_LOG_PROTOCOL_WARNING, "Rx %5d: Unknown indicator - %d\n", log_seq_no, t30_indicator);
+                return -1;
+            }
+        }
+        else
+        {
+            if (len != 1)
+            {
+                span_log(&s->logging, SPAN_LOG_PROTOCOL_WARNING, "Rx %5d: Invalid length for indicator (B)\n", log_seq_no);
+                return -1;
+            }
+            t30_indicator = (buf[0] >> 1) & 0xF;
+        }
+        span_log(&s->logging, SPAN_LOG_FLOW, "Rx %5d: indicator %s\n", log_seq_no, t38_indicator_to_str(t30_indicator));
+        s->rx_indicator_handler(s, s->rx_user_data, t30_indicator);
+        /* This must come after the indicator handler, so the handler routine sees the existing state of the
+           indicator. */
+        s->current_rx_indicator = t30_indicator;
+        break;
+    case T38_TYPE_OF_MSG_T30_DATA:
+        if ((buf[0] & 0x20))
+        {
+            /* Extension */
+            if (len < 2)
+            {
+                span_log(&s->logging, SPAN_LOG_PROTOCOL_WARNING, "Rx %5d: Invalid length for data (A)\n", log_seq_no);
+                return -1;
+            }
+            t30_data = T38_DATA_V8 + (((buf[0] << 2) & 0x3C) | ((buf[1] >> 6) & 0x3));
+            if (t30_data > T38_DATA_V33_14400)
+            {
+                span_log(&s->logging, SPAN_LOG_PROTOCOL_WARNING, "Rx %5d: Unknown data type - %d\n", log_seq_no, t30_data);
+                return -1;
+            }
+            ptr = 2;
+        }
+        else
+        {
+            t30_data = (buf[0] >> 1) & 0xF;
+            if (t30_data > T38_DATA_V17_14400)
+            {
+                span_log(&s->logging, SPAN_LOG_PROTOCOL_WARNING, "Rx %5d: Unknown data type - %d\n", log_seq_no, t30_data);
+                return -1;
+            }
+            ptr = 1;
+        }
+        if (!data_field_present)
+        {
+            /* This is kinda weird, but I guess if the length checks out we accept it. */
+            span_log(&s->logging, SPAN_LOG_PROTOCOL_WARNING, "Rx %5d: Data type with no data field\n", log_seq_no);
+            if (ptr != len)
+            {
+                span_log(&s->logging, SPAN_LOG_PROTOCOL_WARNING, "Rx %5d: Invalid length for data (B)\n", log_seq_no);
+                return -1;
+            }
+            break;
+        }
+        if (ptr >= len)
+        {
+            span_log(&s->logging, SPAN_LOG_PROTOCOL_WARNING, "Rx %5d: Invalid length for data (C)\n", log_seq_no);
+            return -1;
+        }
+        count = buf[ptr++];
+        //printf("Count is %d\n", count);
+        other_half = FALSE;
+        t30_field_type = 0;
+        for (i = 0;  i < (int) count;  i++)
+        {
+            if (ptr >= len)
+            {
+                span_log(&s->logging, SPAN_LOG_PROTOCOL_WARNING, "Rx %5d: Invalid length for data (D)\n", log_seq_no);
+                return -1;
+            }
+            if (s->t38_version == 0)
+            {
+                /* The original version of T.38 with a typo in the ASN.1 spec. */
+                if (other_half)
+                {
+                    /* The lack of a data field in the previous message means
+                       we are currently in the middle of an octet. */
+                    field_data_present = (buf[ptr] >> 3) & 1;
+                    /* Decode field_type */
+                    t30_field_type = buf[ptr] & 0x7;
+                    ptr++;
+                    other_half = FALSE;
+                }
+                else
+                {
+                    field_data_present = (buf[ptr] >> 7) & 1;
+                    /* Decode field_type */
+                    t30_field_type = (buf[ptr] >> 4) & 0x7;
+                    if (field_data_present)
+                        ptr++;
+                    else
+                        other_half = TRUE;
+                }
+                if (t30_field_type > T38_FIELD_T4_NON_ECM_SIG_END)
+                {
+                    span_log(&s->logging, SPAN_LOG_PROTOCOL_WARNING, "Rx %5d: Unknown field type - %d\n", log_seq_no, t30_field_type);
+                    return -1;
+                }
+            }
+            else
+            {
+                field_data_present = (buf[ptr] >> 7) & 1;
+                /* Decode field_type */
+                if ((buf[ptr] & 0x40))
+                {
+                    if (ptr > len - 2)
+                    {
+                        span_log(&s->logging, SPAN_LOG_PROTOCOL_WARNING, "Rx %5d: Invalid length for data (E)\n", log_seq_no);
+                        return -1;
+                    }
+                    t30_field_type = T38_FIELD_CM_MESSAGE + (((buf[ptr] << 2) & 0x3C) | ((buf[ptr + 1] >> 6) & 0x3));
+                    if (t30_field_type > T38_FIELD_V34RATE)
+                    {
+                        span_log(&s->logging, SPAN_LOG_PROTOCOL_WARNING, "Rx %5d: Unknown field type - %d\n", log_seq_no, t30_field_type);
+                        return -1;
+                    }
+                    ptr += 2;
+                }
+                else
+                {
+                    t30_field_type = (buf[ptr++] >> 3) & 0x7;
+                }
+            }
+            /* Decode field_data */
+            if (field_data_present)
+            {
+                if (ptr > len - 2)
+                {
+                    span_log(&s->logging, SPAN_LOG_PROTOCOL_WARNING, "Rx %5d: Invalid length for data (F)\n", log_seq_no);
+                    return -1;
+                }
+                numocts = ((buf[ptr] << 8) | buf[ptr + 1]) + 1;
+                msg = buf + ptr + 2;
+                ptr += numocts + 2;
+            }
+            else
+            {
+                numocts = 0;
+                msg = NULL;
+            }
+            if (ptr > len)
+            {
+                span_log(&s->logging, SPAN_LOG_PROTOCOL_WARNING, "Rx %5d: Invalid length for data (G)\n", log_seq_no);
+                return -1;
+            }
+            span_log(&s->logging,
+                     SPAN_LOG_FLOW,
+                     "Rx %5d: (%d) data %s/%s + %d byte(s)\n",
+                     log_seq_no,
+                     i,
+                     t38_data_type_to_str(t30_data),
+                     t38_field_type_to_str(t30_field_type),
+                     numocts);
+            s->rx_data_handler(s, s->rx_user_data, t30_data, t30_field_type, msg, numocts);
+            s->current_rx_data_type = t30_data;
+            s->current_rx_field_type = t30_field_type;
+        }
+        if (ptr != len)
+        {
+            if (s->t38_version != 0  ||  ptr != (len - 1)  ||  !other_half)
+            {
+                span_log(&s->logging, SPAN_LOG_PROTOCOL_WARNING, "Rx %5d: Invalid length for data (H) - %d %d\n", log_seq_no, ptr, len);
+                return -1;
+            }
+        }
+        break;
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int t38_encode_indicator(t38_core_state_t *s, uint8_t buf[], int indicator)
+{
+    int len;
+
+    /* Build the IFP packet */
+    /* Data field not present */
+    /* Indicator packet */
+    /* Type of indicator */
+    if (indicator <= T38_IND_V17_14400_LONG_TRAINING)
+    {
+        buf[0] = (uint8_t) (indicator << 1);
+        len = 1;
+    }
+    else if (s->t38_version != 0  &&  indicator <= T38_IND_V33_14400_TRAINING)
+    {
+        buf[0] = (uint8_t) (0x20 | (((indicator - T38_IND_V8_ANSAM) & 0xF) >> 2));
+        buf[1] = (uint8_t) (((indicator - T38_IND_V8_ANSAM) << 6) & 0xFF);
+        len = 2;
+    }
+    else
+    {
+        len = -1;
+    }
+    return len;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int t38_encode_data(t38_core_state_t *s, uint8_t buf[], int data_type, const t38_data_field_t field[], int fields)
+{
+    int len;
+    int i;
+    int enclen;
+    int multiplier;
+    int data_field_no;
+    const t38_data_field_t *q;
+    unsigned int encoded_len;
+    unsigned int fragment_len;
+    unsigned int value;
+    uint8_t data_field_present;
+    uint8_t field_data_present;
+    char tag[20];
+
+    /* Build the IFP packet */
+
+    /* There seems no valid reason why a packet would ever be generated without a data field present */
+    data_field_present = TRUE;
+
+    for (data_field_no = 0;  data_field_no < fields;  data_field_no++)
+    {
+        span_log(&s->logging,
+                 SPAN_LOG_FLOW,
+                 "Tx %5d: (%d) data %s/%s + %d byte(s)\n",
+                 s->tx_seq_no,
+                 data_field_no,
+                 t38_data_type_to_str(data_type),
+                 t38_field_type_to_str(field[data_field_no].field_type),
+                 field[data_field_no].field_len);
+    }
+    
+    data_field_no = 0;
+    len = 0;
+    /* Data field present */
+    /* Data packet */
+    /* Type of data */
+    if (data_type <= T38_DATA_V17_14400)
+    {
+        buf[len++] = (uint8_t) ((data_field_present << 7) | 0x40 | (data_type << 1));
+    }
+    else if (s->t38_version != 0  &&  data_type <= T38_DATA_V33_14400)
+    {
+        buf[len++] = (uint8_t) ((data_field_present << 7) | 0x60 | (((data_type - T38_DATA_V8) & 0xF) >> 2));
+        buf[len++] = (uint8_t) (((data_type - T38_DATA_V8) << 6) & 0xFF);
+    }
+    else
+    {
+        return -1;
+    }
+    if (data_field_present)
+    {
+        encoded_len = 0;
+        data_field_no = 0;
+        do
+        {
+            value = fields - encoded_len;
+            if (value < 0x80)
+            {
+                /* 1 octet case */
+                buf[len++] = (uint8_t) value;
+                enclen = value;
+            }
+            else if (value < 0x4000)
+            {
+                /* 2 octet case */
+                buf[len++] = (uint8_t) (0x80 | ((value >> 8) & 0xFF));
+                buf[len++] = (uint8_t) (value & 0xFF);
+                enclen = value;
+            }
+            else
+            {
+                /* Fragmentation case */
+                multiplier = (value/0x4000 < 4)  ?  value/0x4000  :  4;
+                buf[len++] = (uint8_t) (0xC0 | multiplier);
+                enclen = 0x4000*multiplier;
+            }
+
+            fragment_len = enclen;
+            encoded_len += fragment_len;
+            /* Encode the elements */
+            for (i = 0;  i < (int) encoded_len;  i++)
+            {
+                q = &field[data_field_no];
+                field_data_present = (uint8_t) (q->field_len > 0);
+                /* Encode field_type */
+                if (s->t38_version == 0)
+                {
+                    /* Original version of T.38 with a typo */
+                    if (q->field_type > T38_FIELD_T4_NON_ECM_SIG_END)
+                        return -1;
+                    buf[len++] = (uint8_t) ((field_data_present << 7) | (q->field_type << 4));
+                }
+                else
+                {
+                    if (q->field_type <= T38_FIELD_T4_NON_ECM_SIG_END)
+                    {
+                        buf[len++] = (uint8_t) ((field_data_present << 7) | (q->field_type << 3));
+                    }
+                    else if (q->field_type <= T38_FIELD_V34RATE)
+                    {
+                        buf[len++] = (uint8_t) ((field_data_present << 7) | 0x40 | ((q->field_type - T38_FIELD_CM_MESSAGE) >> 2));
+                        buf[len++] = (uint8_t) (((q->field_type - T38_FIELD_CM_MESSAGE) << 6) & 0xC0);
+                    }
+                    else
+                    {
+                        return -1;
+                    }
+                }
+                /* Encode field_data */
+                if (field_data_present)
+                {
+                    if (q->field_len < 1  ||  q->field_len > 65535)
+                        return -1;
+                    buf[len++] = (uint8_t) (((q->field_len - 1) >> 8) & 0xFF);
+                    buf[len++] = (uint8_t) ((q->field_len - 1) & 0xFF);
+                    memcpy(buf + len, q->field, q->field_len);
+                    len += q->field_len;
+                }
+                data_field_no++;
+            }
+        }
+        while (fields != (int) encoded_len  ||  fragment_len >= 16384);
+    }
+
+    if (span_log_test(&s->logging, SPAN_LOG_FLOW))
+    {
+        sprintf(tag, "Tx %5d: IFP", s->tx_seq_no);
+        span_log_buf(&s->logging, SPAN_LOG_FLOW, tag, buf, len);
+    }
+    return len;
+}
+/*- End of function --------------------------------------------------------*/
+
+int t38_core_send_indicator(t38_core_state_t *s, int indicator, int count)
+{
+    uint8_t buf[100];
+    int len;
+
+    /* Zero is a valid count, to suppress the transmission of indicators when the
+       transport is TCP. */       
+    if (count)
+    {
+        if ((len = t38_encode_indicator(s, buf, indicator)) < 0)
+        {
+            span_log(&s->logging, SPAN_LOG_FLOW, "T.38 indicator len is %d\n", len);
+            return len;
+        }
+        span_log(&s->logging, SPAN_LOG_FLOW, "Tx %5d: indicator %s\n", s->tx_seq_no, t38_indicator_to_str(indicator));
+        s->tx_packet_handler(s, s->tx_packet_user_data, buf, len, count);
+        s->tx_seq_no = (s->tx_seq_no + 1) & 0xFFFF;
+    }
+    s->current_tx_indicator = indicator;
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int t38_core_send_data(t38_core_state_t *s, int data_type, int field_type, const uint8_t field[], int field_len, int count)
+{
+    t38_data_field_t field0;
+    uint8_t buf[1000];
+    int len;
+
+    field0.field_type = field_type;
+    field0.field = field;
+    field0.field_len = field_len;
+    if ((len = t38_encode_data(s, buf, data_type, &field0, 1)) < 0)
+    {
+        span_log(&s->logging, SPAN_LOG_FLOW, "T.38 data len is %d\n", len);
+        return len;
+    }
+    s->tx_packet_handler(s, s->tx_packet_user_data, buf, len, count);
+    s->tx_seq_no = (s->tx_seq_no + 1) & 0xFFFF;
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int t38_core_send_data_multi_field(t38_core_state_t *s, int data_type, const t38_data_field_t field[], int fields, int count)
+{
+    uint8_t buf[1000];
+    int len;
+
+    if ((len = t38_encode_data(s, buf, data_type, field, fields)) < 0)
+    {
+        span_log(&s->logging, SPAN_LOG_FLOW, "T.38 data len is %d\n", len);
+        return len;
+    }
+    s->tx_packet_handler(s, s->tx_packet_user_data, buf, len, count);
+    s->tx_seq_no = (s->tx_seq_no + 1) & 0xFFFF;
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+void t38_set_data_rate_management_method(t38_core_state_t *s, int method)
+{
+    s->data_rate_management_method = method;
+}
+/*- End of function --------------------------------------------------------*/
+
+void t38_set_data_transport_protocol(t38_core_state_t *s, int data_transport_protocol)
+{
+    s->data_transport_protocol = data_transport_protocol;
+}
+/*- End of function --------------------------------------------------------*/
+
+void t38_set_fill_bit_removal(t38_core_state_t *s, int fill_bit_removal)
+{
+    s->fill_bit_removal = fill_bit_removal;
+}
+/*- End of function --------------------------------------------------------*/
+
+void t38_set_mmr_transcoding(t38_core_state_t *s, int mmr_transcoding)
+{
+    s->mmr_transcoding = mmr_transcoding;
+}
+/*- End of function --------------------------------------------------------*/
+
+void t38_set_jbig_transcoding(t38_core_state_t *s, int jbig_transcoding)
+{
+    s->jbig_transcoding = jbig_transcoding;
+}
+/*- End of function --------------------------------------------------------*/
+
+void t38_set_max_buffer_size(t38_core_state_t *s, int max_buffer_size)
+{
+    s->max_buffer_size = max_buffer_size;
+}
+/*- End of function --------------------------------------------------------*/
+
+void t38_set_max_datagram_size(t38_core_state_t *s, int max_datagram_size)
+{
+    s->max_datagram_size = max_datagram_size;
+}
+/*- End of function --------------------------------------------------------*/
+
+void t38_set_t38_version(t38_core_state_t *s, int t38_version)
+{
+    s->t38_version = t38_version;
+}
+/*- End of function --------------------------------------------------------*/
+
+void t38_set_sequence_number_handling(t38_core_state_t *s, int check)
+{
+    s->check_sequence_numbers = check;
+}
+/*- End of function --------------------------------------------------------*/
+
+int t38_get_fastest_image_data_rate(t38_core_state_t *s)
+{
+    return s->fastest_image_data_rate;
+}
+/*- End of function --------------------------------------------------------*/
+
+t38_core_state_t *t38_core_init(t38_core_state_t *s,
+                                t38_rx_indicator_handler_t *rx_indicator_handler,
+                                t38_rx_data_handler_t *rx_data_handler,
+                                t38_rx_missing_handler_t *rx_missing_handler,
+                                void *rx_user_data,
+                                t38_tx_packet_handler_t *tx_packet_handler,
+                                void *tx_packet_user_data)
+{
+    if (s == NULL)
+    {
+        if ((s = (t38_core_state_t *) malloc(sizeof(*s))) == NULL)
+            return NULL;
+    }
+    memset(s, 0, sizeof(*s));
+    span_log_init(&s->logging, SPAN_LOG_NONE, NULL);
+    span_log_set_protocol(&s->logging, "T.38");
+
+    span_log(&s->logging, SPAN_LOG_FLOW, "Start tx document\n");
+    s->data_rate_management_method = 2;
+    s->data_transport_protocol = T38_TRANSPORT_UDPTL;
+    s->fill_bit_removal = FALSE;
+    s->mmr_transcoding = FALSE;
+    s->jbig_transcoding = FALSE;
+    s->max_buffer_size = 400;
+    s->max_datagram_size = 100;
+    s->t38_version = 0;
+    s->check_sequence_numbers = TRUE;
+
+    s->current_rx_indicator = -1;
+    s->current_rx_data_type = -1;
+    s->current_rx_field_type = -1;
+
+    s->rx_indicator_handler = rx_indicator_handler;
+    s->rx_data_handler = rx_data_handler;
+    s->rx_missing_handler = rx_missing_handler;
+    s->rx_user_data = rx_user_data;
+    s->tx_packet_handler = tx_packet_handler;
+    s->tx_packet_user_data = tx_packet_user_data;
+
+    s->rx_expected_seq_no = -1;
+    return s;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/t38_gateway.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/t38_gateway.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,2125 @@
+//#define LOG_FAX_AUDIO
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * t38_gateway.c - A T.38 gateway, less the packet exchange part
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2005, 2006, 2007, 2008 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: t38_gateway.c,v 1.139 2008/08/17 16:25:52 steveu Exp $
+ */
+
+/*! \file */
+
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
+
+#include <inttypes.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <time.h>
+#include <string.h>
+#include "floating_fudge.h"
+#if defined(HAVE_TGMATH_H)
+#include <tgmath.h>
+#endif
+#if defined(HAVE_MATH_H)
+#include <math.h>
+#endif
+#include <assert.h>
+#if defined(LOG_FAX_AUDIO)
+#include <unistd.h>
+#endif
+#include <tiffio.h>
+
+#include "spandsp/telephony.h"
+#include "spandsp/logging.h"
+#include "spandsp/queue.h"
+#include "spandsp/dc_restore.h"
+#include "spandsp/bit_operations.h"
+#include "spandsp/power_meter.h"
+#include "spandsp/complex.h"
+#include "spandsp/tone_detect.h"
+#include "spandsp/tone_generate.h"
+#include "spandsp/async.h"
+#include "spandsp/crc.h"
+#include "spandsp/hdlc.h"
+#include "spandsp/silence_gen.h"
+#include "spandsp/fsk.h"
+#include "spandsp/v29rx.h"
+#include "spandsp/v29tx.h"
+#include "spandsp/v27ter_rx.h"
+#include "spandsp/v27ter_tx.h"
+#include "spandsp/v17rx.h"
+#include "spandsp/v17tx.h"
+#include "spandsp/super_tone_rx.h"
+#include "spandsp/modem_connect_tones.h"
+#include "spandsp/t4.h"
+#include "spandsp/t30_fcf.h"
+#include "spandsp/t35.h"
+#include "spandsp/t30.h"
+#include "spandsp/t30_logging.h"
+
+#include "spandsp/fax_modems.h"
+#include "spandsp/t38_core.h"
+#include "spandsp/t38_non_ecm_buffer.h"
+#include "spandsp/t38_gateway.h"
+
+/* This is the target time per transmission chunk. The actual
+   packet timing will sync to the data octets. */
+#define MS_PER_TX_CHUNK                 30
+#define HDLC_START_BUFFER_LEVEL         8
+
+#define INDICATOR_TX_COUNT              3
+#define DATA_TX_COUNT                   1
+#define DATA_END_TX_COUNT               3
+
+enum
+{
+    DISBIT1 = 0x01,
+    DISBIT2 = 0x02,
+    DISBIT3 = 0x04,
+    DISBIT4 = 0x08,
+    DISBIT5 = 0x10,
+    DISBIT6 = 0x20,
+    DISBIT7 = 0x40,
+    DISBIT8 = 0x80
+};
+
+enum
+{
+    T38_NONE,
+    T38_V27TER_RX,
+    T38_V29_RX,
+    T38_V17_RX
+};
+
+enum
+{
+    HDLC_FLAG_FINISHED = 0x01,
+    HDLC_FLAG_CORRUPT_CRC = 0x02,
+    HDLC_FLAG_PROCEED_WITH_OUTPUT = 0x04,
+    HDLC_FLAG_MISSING_DATA = 0x08
+};
+
+enum
+{
+    FLAG_INDICATOR = 0x100,
+    FLAG_DATA = 0x200
+};
+
+#define MAX_NSX_SUPPRESSION             10
+
+#define HDLC_FRAMING_OK_THRESHOLD       5
+
+static uint8_t nsx_overwrite[2][MAX_NSX_SUPPRESSION] =
+{
+    {0xFF, 0, 0, 0, 0, 0, 0, 0, 0, 0},
+    {0xFF, 0, 0, 0, 0, 0, 0, 0, 0, 0},
+};
+
+static int restart_rx_modem(t38_gateway_state_t *s);
+static int process_rx_indicator(t38_core_state_t *t, void *user_data, int indicator);
+static void hdlc_underflow_handler(void *user_data);
+static void to_t38_buffer_init(t38_gateway_to_t38_state_t *s);
+static void t38_hdlc_rx_put_bit(hdlc_rx_state_t *t, int new_bit);
+static void non_ecm_put_bit(void *user_data, int bit);
+static void non_ecm_remove_fill_and_put_bit(void *user_data, int bit);
+static void non_ecm_push_residue(t38_gateway_state_t *s);
+static void tone_detected(void *user_data, int on, int level, int delay);
+
+static void set_rx_handler(t38_gateway_state_t *s, span_rx_handler_t *handler, void *user_data)
+{
+    if (s->audio.modems.rx_handler != span_dummy_rx)
+        s->audio.modems.rx_handler = handler;
+    s->audio.base_rx_handler = handler;
+    s->audio.modems.rx_user_data = user_data;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void set_rx_active(t38_gateway_state_t *s, int active)
+{
+    s->audio.modems.rx_handler = (active)  ?  s->audio.base_rx_handler  :  span_dummy_rx;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int v17_v21_rx(void *user_data, const int16_t amp[], int len)
+{
+    t38_gateway_state_t *t;
+    fax_modems_state_t *s;
+
+    t = (t38_gateway_state_t *) user_data;
+    s = &t->audio.modems;
+    v17_rx(&s->v17_rx, amp, len);
+    fsk_rx(&s->v21_rx, amp, len);
+    if (s->rx_signal_present)
+    {
+        if (s->rx_trained)
+        {
+            /* The fast modem has trained, so we no longer need to run the slow
+               one in parallel. */
+            span_log(&t->logging, SPAN_LOG_FLOW, "Switching from V.17 + V.21 to V.17 (%.2fdBm0)\n", v17_rx_signal_power(&s->v17_rx));
+            set_rx_handler(t, (span_rx_handler_t *) &v17_rx, &s->v17_rx);
+        }
+        else
+        {
+            span_log(&t->logging, SPAN_LOG_FLOW, "Switching from V.17 + V.21 to V.21 (%.2fdBm0)\n", fsk_rx_signal_power(&s->v21_rx));
+            set_rx_handler(t, (span_rx_handler_t *) &fsk_rx, &s->v21_rx);
+        }
+        /*endif*/
+    }
+    /*endif*/
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int v27ter_v21_rx(void *user_data, const int16_t amp[], int len)
+{
+    t38_gateway_state_t *t;
+    fax_modems_state_t *s;
+
+    t = (t38_gateway_state_t *) user_data;
+    s = &t->audio.modems;
+    v27ter_rx(&s->v27ter_rx, amp, len);
+    fsk_rx(&s->v21_rx, amp, len);
+    if (s->rx_signal_present)
+    {
+        if (s->rx_trained)
+        {
+            /* The fast modem has trained, so we no longer need to run the slow
+               one in parallel. */
+            span_log(&t->logging, SPAN_LOG_FLOW, "Switching from V.27ter + V.21 to V.27ter (%.2fdBm0)\n", v27ter_rx_signal_power(&s->v27ter_rx));
+            set_rx_handler(t, (span_rx_handler_t *) &v27ter_rx, &s->v27ter_rx);
+        }
+        else
+        {
+            span_log(&t->logging, SPAN_LOG_FLOW, "Switching from V.27ter + V.21 to V.21 (%.2fdBm0)\n", fsk_rx_signal_power(&s->v21_rx));
+            set_rx_handler(t, (span_rx_handler_t *) &fsk_rx, &s->v21_rx);
+        }
+        /*endif*/
+    }
+    /*endif*/
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int v29_v21_rx(void *user_data, const int16_t amp[], int len)
+{
+    t38_gateway_state_t *t;
+    fax_modems_state_t *s;
+
+    t = (t38_gateway_state_t *) user_data;
+    s = &t->audio.modems;
+    v29_rx(&s->v29_rx, amp, len);
+    fsk_rx(&s->v21_rx, amp, len);
+    if (s->rx_signal_present)
+    {
+        if (s->rx_trained)
+        {
+            /* The fast modem has trained, so we no longer need to run the slow
+               one in parallel. */
+            span_log(&t->logging, SPAN_LOG_FLOW, "Switching from V.29 + V.21 to V.29 (%.2fdBm0)\n", v29_rx_signal_power(&s->v29_rx));
+            set_rx_handler(t, (span_rx_handler_t *) &v29_rx, &s->v29_rx);
+        }
+        else
+        {
+            span_log(&t->logging, SPAN_LOG_FLOW, "Switching from V.29 + V.21 to V.21 (%.2fdBm0)\n", fsk_rx_signal_power(&s->v21_rx));
+            set_rx_handler(t, (span_rx_handler_t *) &fsk_rx, &s->v21_rx);
+        }
+        /*endif*/
+    }
+    /*endif*/
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void t38_fax_modems_init(fax_modems_state_t *s, int use_tep, void *user_data)
+{
+    s->use_tep = use_tep;
+
+    hdlc_rx_init(&s->hdlc_rx, FALSE, TRUE, HDLC_FRAMING_OK_THRESHOLD, NULL, user_data);
+    hdlc_tx_init(&s->hdlc_tx, FALSE, 2, TRUE, hdlc_underflow_handler, user_data);
+    fsk_rx_init(&s->v21_rx, &preset_fsk_specs[FSK_V21CH2], TRUE, (put_bit_func_t) t38_hdlc_rx_put_bit, &s->hdlc_rx);
+#if 0
+    fsk_rx_signal_cutoff(&s->v21_rx, -45.5);
+#endif
+    fsk_tx_init(&s->v21_tx, &preset_fsk_specs[FSK_V21CH2], (get_bit_func_t) hdlc_tx_get_bit, &s->hdlc_tx);
+    v17_rx_init(&s->v17_rx, 14400, non_ecm_put_bit, user_data);
+    v17_tx_init(&s->v17_tx, 14400, s->use_tep, t38_non_ecm_buffer_get_bit, user_data);
+    v29_rx_init(&s->v29_rx, 9600, non_ecm_put_bit, user_data);
+#if 0
+    v29_rx_signal_cutoff(&s->v29_rx, -45.5);
+#endif
+    v29_tx_init(&s->v29_tx, 9600, s->use_tep, t38_non_ecm_buffer_get_bit, user_data);
+    v27ter_rx_init(&s->v27ter_rx, 4800, non_ecm_put_bit, user_data);
+    v27ter_tx_init(&s->v27ter_tx, 4800, s->use_tep, t38_non_ecm_buffer_get_bit, user_data);
+    silence_gen_init(&s->silence_gen, 0);
+    modem_connect_tones_tx_init(&s->connect_tx, MODEM_CONNECT_TONES_FAX_CNG);
+    modem_connect_tones_rx_init(&s->connect_rx,
+                                MODEM_CONNECT_TONES_FAX_CNG,
+                                tone_detected,
+                                user_data);
+    dc_restore_init(&s->dc_restore);
+
+    s->rx_signal_present = FALSE;
+    s->rx_handler = (span_rx_handler_t *) &span_dummy_rx;
+    s->rx_user_data = NULL;
+    s->tx_handler = (span_tx_handler_t *) &silence_gen;
+    s->tx_user_data = &s->silence_gen;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void tone_detected(void *user_data, int on, int level, int delay)
+{
+    t38_gateway_state_t *s;
+
+    s = (t38_gateway_state_t *) user_data;
+    span_log(&s->logging, SPAN_LOG_FLOW, "FAX tone declared %s (%ddBm0)\n", (on)  ?  "on"  :  "off", level);
+}
+/*- End of function --------------------------------------------------------*/
+
+static void hdlc_underflow_handler(void *user_data)
+{
+    t38_gateway_state_t *s;
+    t38_gateway_hdlc_state_t *t;
+    int old_data_type;
+
+    s = (t38_gateway_state_t *) user_data;
+    t = &s->core.hdlc_to_modem;
+    span_log(&s->logging, SPAN_LOG_FLOW, "HDLC underflow at %d\n", t->out);
+    /* If the current HDLC buffer is not at the HDLC_FLAG_PROCEED_WITH_OUTPUT stage, this
+       underflow must be an end of preamble condition. */
+    if ((t->flags[t->out] & HDLC_FLAG_PROCEED_WITH_OUTPUT))
+    {
+        old_data_type = t->contents[t->out];
+        t->len[t->out] = 0;
+        t->flags[t->out] = 0;
+        t->contents[t->out] = 0;
+        if (++t->out >= T38_TX_HDLC_BUFS)
+            t->out = 0;
+        span_log(&s->logging, SPAN_LOG_FLOW, "HDLC next is 0x%X\n", t->contents[t->out]);
+        if ((t->contents[t->out] & FLAG_INDICATOR))
+        {
+            /* The next thing in the queue is an indicator, so we need to stop this modem. */
+            span_log(&s->logging, SPAN_LOG_FLOW, "HDLC shutdown\n");
+            hdlc_tx_frame(&s->audio.modems.hdlc_tx, NULL, 0);
+        }
+        else if ((t->contents[t->out] & FLAG_DATA))
+        {
+            /* Check if we should start sending the next frame */
+            if ((t->flags[t->out] & HDLC_FLAG_PROCEED_WITH_OUTPUT))
+            {
+                /* This frame is ready to go, and uses the same modem we are running now. So, send
+                   whatever we have. This might or might not be an entire frame. */
+                span_log(&s->logging, SPAN_LOG_FLOW, "HDLC start next frame\n");
+                hdlc_tx_frame(&s->audio.modems.hdlc_tx, t->buf[t->out], t->len[t->out]);
+                if ((t->flags[t->out] & HDLC_FLAG_CORRUPT_CRC))
+                    hdlc_tx_corrupt_frame(&s->audio.modems.hdlc_tx);
+                /*endif*/
+            }
+            /*endif*/
+        }
+        /*endif*/
+    }
+    /*endif*/
+}
+/*- End of function --------------------------------------------------------*/
+
+static int set_next_tx_type(t38_gateway_state_t *s)
+{
+    get_bit_func_t get_bit_func;
+    void *get_bit_user_data;
+    int indicator;
+    int short_train;
+    fax_modems_state_t *t;
+    t38_gateway_hdlc_state_t *u;
+
+    t = &s->audio.modems;
+    u = &s->core.hdlc_to_modem;
+    if (t->next_tx_handler)
+    {
+        /* There is a handler queued, so that is the next one */
+        t->tx_handler = t->next_tx_handler;
+        t->tx_user_data = t->next_tx_user_data;
+        t->next_tx_handler = NULL;
+        if (t->tx_handler == (span_tx_handler_t *) &(silence_gen)
+            ||
+            t->tx_handler == (span_tx_handler_t *) &(tone_gen))
+        {
+            set_rx_active(s, TRUE);
+        }
+        else
+        {
+            set_rx_active(s, FALSE);
+        }
+        /*endif*/
+        return TRUE;
+    }
+    /*endif*/
+    if (u->in == u->out)
+        return FALSE;
+    /*endif*/
+    if ((u->contents[u->out] & FLAG_INDICATOR) == 0)
+        return FALSE;
+    /*endif*/
+    indicator = (u->contents[u->out] & 0xFF);
+    u->len[u->out] = 0;
+    u->flags[u->out] = 0;
+    u->contents[u->out] = 0;
+    if (++u->out >= T38_TX_HDLC_BUFS)
+        u->out = 0;
+    /*endif*/
+    span_log(&s->logging, SPAN_LOG_FLOW, "Changing to %s\n", t38_indicator_to_str(indicator));
+    if (s->core.image_data_mode  &&  s->core.ecm_mode)
+    {
+        span_log(&s->logging, SPAN_LOG_FLOW, "HDLC mode\n");
+        hdlc_tx_init(&t->hdlc_tx, FALSE, 2, TRUE, hdlc_underflow_handler, s);
+        get_bit_func = (get_bit_func_t) hdlc_tx_get_bit;
+        get_bit_user_data = (void *) &t->hdlc_tx;
+    }
+    else
+    {
+        span_log(&s->logging, SPAN_LOG_FLOW, "non-ECM mode\n");
+        get_bit_func = t38_non_ecm_buffer_get_bit;
+        get_bit_user_data = (void *) &s->core.non_ecm_to_modem;
+    }
+    /*endif*/
+    switch (indicator)
+    {
+    case T38_IND_NO_SIGNAL:
+        t->tx_bit_rate = 0;
+        /* Impose 75ms minimum on transmitted silence */
+        //silence_gen_set(&t->silence_gen, ms_to_samples(75));
+        t->tx_handler = (span_tx_handler_t *) &(silence_gen);
+        t->tx_user_data = &t->silence_gen;
+        t->next_tx_handler = NULL;
+        set_rx_active(s, TRUE);
+        break;
+    case T38_IND_CNG:
+        t->tx_bit_rate = 0;
+        modem_connect_tones_tx_init(&t->connect_tx, MODEM_CONNECT_TONES_FAX_CNG);
+        t->tx_handler = (span_tx_handler_t *) &modem_connect_tones_tx;
+        t->tx_user_data = &t->connect_tx;
+        silence_gen_set(&t->silence_gen, 0);
+        t->next_tx_handler = (span_tx_handler_t *) &(silence_gen);
+        t->next_tx_user_data = &t->silence_gen;
+        set_rx_active(s, TRUE);
+        break;
+    case T38_IND_CED:
+        t->tx_bit_rate = 0;
+        modem_connect_tones_tx_init(&t->connect_tx, MODEM_CONNECT_TONES_FAX_CED);
+        t->tx_handler = (span_tx_handler_t *) &modem_connect_tones_tx;
+        t->tx_user_data = &t->connect_tx;
+        t->next_tx_handler = NULL;
+        set_rx_active(s, TRUE);
+        break;
+    case T38_IND_V21_PREAMBLE:
+        t->tx_bit_rate = 300;
+        hdlc_tx_init(&t->hdlc_tx, FALSE, 2, TRUE, hdlc_underflow_handler, s);
+        hdlc_tx_flags(&t->hdlc_tx, 32);
+        silence_gen_alter(&t->silence_gen, ms_to_samples(75));
+        u->len[u->in] = 0;
+        fsk_tx_init(&t->v21_tx, &preset_fsk_specs[FSK_V21CH2], (get_bit_func_t) hdlc_tx_get_bit, &t->hdlc_tx);
+        t->tx_handler = (span_tx_handler_t *) &(silence_gen);
+        t->tx_user_data = &t->silence_gen;
+        t->next_tx_handler = (span_tx_handler_t *) &(fsk_tx);
+        t->next_tx_user_data = &t->v21_tx;
+        set_rx_active(s, TRUE);
+        break;
+    case T38_IND_V27TER_2400_TRAINING:
+    case T38_IND_V27TER_4800_TRAINING:
+        switch (indicator)
+        {
+        case T38_IND_V27TER_2400_TRAINING:
+            t->tx_bit_rate = 2400;
+            break;
+        case T38_IND_V27TER_4800_TRAINING:
+            t->tx_bit_rate = 2400;
+            break;
+        }
+        silence_gen_alter(&t->silence_gen, ms_to_samples(75));
+        v27ter_tx_restart(&t->v27ter_tx, t->tx_bit_rate, t->use_tep);
+        v27ter_tx_set_get_bit(&t->v27ter_tx, get_bit_func, get_bit_user_data);
+        t->tx_handler = (span_tx_handler_t *) &(silence_gen);
+        t->tx_user_data = &t->silence_gen;
+        t->next_tx_handler = (span_tx_handler_t *) &(v27ter_tx);
+        t->next_tx_user_data = &t->v27ter_tx;
+        set_rx_active(s, TRUE);
+        break;
+    case T38_IND_V29_7200_TRAINING:
+    case T38_IND_V29_9600_TRAINING:
+        switch (indicator)
+        {
+        case T38_IND_V29_7200_TRAINING:
+            t->tx_bit_rate = 7200;
+            break;
+        case T38_IND_V29_9600_TRAINING:
+            t->tx_bit_rate = 9600;
+            break;
+        }
+        silence_gen_alter(&t->silence_gen, ms_to_samples(75));
+        v29_tx_restart(&t->v29_tx, t->tx_bit_rate, t->use_tep);
+        v29_tx_set_get_bit(&t->v29_tx, get_bit_func, get_bit_user_data);
+        t->tx_handler = (span_tx_handler_t *) &(silence_gen);
+        t->tx_user_data = &t->silence_gen;
+        t->next_tx_handler = (span_tx_handler_t *) &(v29_tx);
+        t->next_tx_user_data = &t->v29_tx;
+        set_rx_active(s, TRUE);
+        break;
+    case T38_IND_V17_7200_SHORT_TRAINING:
+    case T38_IND_V17_7200_LONG_TRAINING:
+    case T38_IND_V17_9600_SHORT_TRAINING:
+    case T38_IND_V17_9600_LONG_TRAINING:
+    case T38_IND_V17_12000_SHORT_TRAINING:
+    case T38_IND_V17_12000_LONG_TRAINING:
+    case T38_IND_V17_14400_SHORT_TRAINING:
+    case T38_IND_V17_14400_LONG_TRAINING:
+        short_train = FALSE;
+        switch (indicator)
+        {
+        case T38_IND_V17_7200_SHORT_TRAINING:
+            short_train = TRUE;
+            t->tx_bit_rate = 7200;
+            break;
+        case T38_IND_V17_7200_LONG_TRAINING:
+            t->tx_bit_rate = 7200;
+            break;
+        case T38_IND_V17_9600_SHORT_TRAINING:
+            short_train = TRUE;
+            t->tx_bit_rate = 9600;
+            break;
+        case T38_IND_V17_9600_LONG_TRAINING:
+            t->tx_bit_rate = 9600;
+            break;
+        case T38_IND_V17_12000_SHORT_TRAINING:
+            short_train = TRUE;
+            t->tx_bit_rate = 12000;
+            break;
+        case T38_IND_V17_12000_LONG_TRAINING:
+            t->tx_bit_rate = 12000;
+            break;
+        case T38_IND_V17_14400_SHORT_TRAINING:
+            short_train = TRUE;
+            t->tx_bit_rate = 14400;
+            break;
+        case T38_IND_V17_14400_LONG_TRAINING:
+            t->tx_bit_rate = 14400;
+            break;
+        }
+        silence_gen_alter(&t->silence_gen, ms_to_samples(75));
+        v17_tx_restart(&t->v17_tx, t->tx_bit_rate, t->use_tep, short_train);
+        v17_tx_set_get_bit(&t->v17_tx, get_bit_func, get_bit_user_data);
+        t->tx_handler = (span_tx_handler_t *) &(silence_gen);
+        t->tx_user_data = &t->silence_gen;
+        t->next_tx_handler = (span_tx_handler_t *) &(v17_tx);
+        t->next_tx_user_data = &t->v17_tx;
+        set_rx_active(s, TRUE);
+        break;
+    case T38_IND_V8_ANSAM:
+        t->tx_bit_rate = 0;
+        break;
+    case T38_IND_V8_SIGNAL:
+        t->tx_bit_rate = 0;
+        break;
+    case T38_IND_V34_CNTL_CHANNEL_1200:
+        t->tx_bit_rate = 0;
+        break;
+    case T38_IND_V34_PRI_CHANNEL:
+        t->tx_bit_rate = 0;
+        break;
+    case T38_IND_V34_CC_RETRAIN:
+        t->tx_bit_rate = 0;
+        break;
+    case T38_IND_V33_12000_TRAINING:
+        t->tx_bit_rate = 12000;
+        break;
+    case T38_IND_V33_14400_TRAINING:
+        t->tx_bit_rate = 14400;
+        break;
+    default:
+        break;
+    }
+    /*endswitch*/
+    /* For any fast modem, set 200ms of preamble flags */
+    if (t->tx_bit_rate > 300)
+        hdlc_tx_flags(&t->hdlc_tx, t->tx_bit_rate/(8*5));
+    /*endif*/
+    t38_non_ecm_buffer_report_status(&s->core.non_ecm_to_modem, &s->logging);
+    t38_non_ecm_buffer_init(&s->core.non_ecm_to_modem, s->core.image_data_mode, s->core.min_row_bits);
+    s->t38x.in_progress_rx_indicator = indicator;
+    return TRUE;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void pump_out_final_hdlc(t38_gateway_state_t *s, int good_fcs)
+{
+    if (!good_fcs)
+        s->core.hdlc_to_modem.flags[s->core.hdlc_to_modem.in] |= HDLC_FLAG_CORRUPT_CRC;
+    /*endif*/
+    if (s->core.hdlc_to_modem.in == s->core.hdlc_to_modem.out)
+    {
+        /* This is the frame in progress at the output. */
+        if ((s->core.hdlc_to_modem.flags[s->core.hdlc_to_modem.out] & HDLC_FLAG_PROCEED_WITH_OUTPUT) == 0)
+        {
+            /* Output of this frame has not yet begun. Throw it all out now. */
+            hdlc_tx_frame(&s->audio.modems.hdlc_tx, s->core.hdlc_to_modem.buf[s->core.hdlc_to_modem.out], s->core.hdlc_to_modem.len[s->core.hdlc_to_modem.out]);
+        }
+        /*endif*/
+        if ((s->core.hdlc_to_modem.flags[s->core.hdlc_to_modem.out] & HDLC_FLAG_CORRUPT_CRC))
+            hdlc_tx_corrupt_frame(&s->audio.modems.hdlc_tx);
+        /*endif*/
+    }
+    /*endif*/
+    s->core.hdlc_to_modem.flags[s->core.hdlc_to_modem.in] |= (HDLC_FLAG_PROCEED_WITH_OUTPUT | HDLC_FLAG_FINISHED);
+    if (++s->core.hdlc_to_modem.in >= T38_TX_HDLC_BUFS)
+        s->core.hdlc_to_modem.in = 0;
+    /*endif*/
+}
+/*- End of function --------------------------------------------------------*/
+
+static void edit_control_messages(t38_gateway_state_t *s, int from_modem, uint8_t *buf, int len)
+{
+    /* Frames need to be fed to this routine byte by byte as they arrive. It basically just
+       edits the last byte received, based on the frame up to that point. */
+    if (s->t38x.corrupt_current_frame[from_modem])
+    {
+        /* We simply need to overwrite a section of the message, so it is not recognisable at
+           the receiver. This is used for the NSF, NSC, and NSS messages. Several strategies are
+           possible for the replacement data. If you have a manufacturer code of your own, the
+           sane thing is to overwrite the original data with that. */
+        if (len <= s->t38x.suppress_nsx_len[from_modem])
+            buf[len - 1] = nsx_overwrite[from_modem][len - 4];
+        /*endif*/
+        return;
+    }
+    /*endif*/
+    /* Edit the message, if we need to control the communication between the end points. */
+    switch (len)
+    {
+    case 3:
+        switch (buf[2])
+        {
+        case T30_NSF:
+        case T30_NSC:
+        case T30_NSS:
+            if (s->t38x.suppress_nsx_len[from_modem])
+            {
+                /* Corrupt the message, so it will be ignored by the far end. If it were
+                   processed, 2 machines which recognise each other might do special things
+                   we cannot handle as a middle man. */
+                span_log(&s->logging, SPAN_LOG_FLOW, "Corrupting %s message to prevent recognition\n", t30_frametype(buf[2]));
+                s->t38x.corrupt_current_frame[from_modem] = TRUE;
+            }
+            /*endif*/
+            break;
+        }
+        /*endswitch*/
+        break;
+    case 5:
+        switch (buf[2])
+        {
+        case T30_DIS:
+            /* We may need to adjust the capabilities, so they do not exceed our own */
+            span_log(&s->logging, SPAN_LOG_FLOW, "Applying fast modem type constraints.\n");
+            switch (buf[4] & (DISBIT6 | DISBIT5 | DISBIT4 | DISBIT3))
+            {
+            case 0:
+            case DISBIT4:
+                /* V.27ter only */
+                break;
+            case DISBIT3:
+            case (DISBIT4 | DISBIT3):
+                /* V.27ter and V.29 */
+                if (!(s->core.supported_modems & T30_SUPPORT_V29))
+                    buf[4] &= ~DISBIT3;
+                /*endif*/
+                break;
+            case (DISBIT6 | DISBIT4 | DISBIT3):
+                /* V.27ter, V.29 and V.17 */
+                if (!(s->core.supported_modems & T30_SUPPORT_V17))
+                    buf[4] &= ~DISBIT6;
+                /*endif*/
+                if (!(s->core.supported_modems & T30_SUPPORT_V29))
+                    buf[4] &= ~DISBIT3;
+                /*endif*/
+                break;
+            case (DISBIT5 | DISBIT4):
+            case (DISBIT6 | DISBIT4):
+            case (DISBIT6 | DISBIT5 | DISBIT4):
+            case (DISBIT6 | DISBIT5 | DISBIT4 | DISBIT3):
+                /* Reserved */
+                buf[4] &= ~(DISBIT6 | DISBIT5);
+                buf[4] |= (DISBIT4 | DISBIT3);
+                break;
+            default:
+                /* Not used */
+                buf[4] &= ~(DISBIT6 | DISBIT5);
+                buf[4] |= (DISBIT4 | DISBIT3);
+                break;
+            }
+            /*endswitch*/
+            break;
+        }
+        /*endswitch*/
+        break;
+    case 7:
+        switch (buf[2])
+        {
+        case T30_DIS:
+            if (!s->core.ecm_allowed)
+            {
+                /* Do not allow ECM or T.6 coding */
+                span_log(&s->logging, SPAN_LOG_FLOW, "Inhibiting ECM\n");
+                buf[6] &= ~(DISBIT3 | DISBIT7);
+            }
+            /*endif*/
+            break;
+        }
+        /*endswitch*/
+        break;
+    }
+    /*endswitch*/
+}
+/*- End of function --------------------------------------------------------*/
+
+static void monitor_control_messages(t38_gateway_state_t *s, int from_modem, uint8_t *buf, int len)
+{
+    static const struct
+    {
+        int bit_rate;
+        int modem_type;
+        uint8_t dcs_code;
+    } modem_codes[] =
+    {
+        {14400, T38_V17_RX,      DISBIT6},
+        {12000, T38_V17_RX,      (DISBIT6 | DISBIT4)},
+        { 9600, T38_V17_RX,      (DISBIT6 | DISBIT3)},
+        { 9600, T38_V29_RX,      DISBIT3},
+        { 7200, T38_V17_RX,      (DISBIT6 | DISBIT4 | DISBIT3)},
+        { 7200, T38_V29_RX,      (DISBIT4 | DISBIT3)},
+        { 4800, T38_V27TER_RX,   DISBIT4},
+        { 2400, T38_V27TER_RX,   0},
+        {    0, T38_NONE,        0}
+    };
+    static const int minimum_scan_line_times[8] =
+    {
+        20,
+        5,
+        10,
+        0,
+        40,
+        0,
+        0,
+        0
+    };
+    int dcs_code;
+    int i;
+    int j;
+
+    /* Monitor the control messages, at the point where we have the whole message, so we can
+       see what is happening to things like training success/failure. */
+    span_log(&s->logging, SPAN_LOG_FLOW, "Monitoring %s\n", t30_frametype(buf[2]));
+    if (len < 3)
+        return;
+    /*endif*/
+    s->core.tcf_mode_predictable_modem_start = 0;
+    switch (buf[2])
+    {
+    case T30_CFR:
+        /* We are changing from TCF exchange to image exchange */
+        /* Successful training means we should change to short training */
+        s->core.image_data_mode = TRUE;
+        s->core.short_train = TRUE;
+        span_log(&s->logging, SPAN_LOG_FLOW, "CFR - short train = %d, ECM = %d\n", s->core.short_train, s->core.ecm_mode);
+        if (!from_modem)
+            restart_rx_modem(s);
+        /*endif*/
+        break;
+    case T30_RTN:
+    case T30_RTP:
+        /* We are going back to the exchange of fresh TCF */
+        s->core.image_data_mode = FALSE;
+        s->core.short_train = FALSE;
+        break;
+    case T30_CTR:
+        /* T.30 says the first image data after this does full training, yet does not
+           return to TCF. This seems to be the sole case of long training for image
+           data. */
+        s->core.short_train = FALSE;
+        break;
+    case T30_DTC:
+    case T30_DCS:
+    case T30_DCS | 1:
+        /* We need to check which modem type is about to be used, so we can start the
+           correct modem. */
+        if (len >= 5)
+        {
+            /* The table is short, and not searched often, so a brain-dead linear scan seems OK */
+            dcs_code = buf[4] & (DISBIT6 | DISBIT5 | DISBIT4 | DISBIT3);
+            for (i = 0;  modem_codes[i].bit_rate;  i++)
+            {
+                if (modem_codes[i].dcs_code == dcs_code)
+                    break;
+                /*endif*/
+            }
+            /*endfor*/
+            s->core.fast_bit_rate = modem_codes[i].bit_rate;
+            s->core.fast_modem = modem_codes[i].modem_type;
+        }
+        /*endif*/
+        if (len >= 6)
+        {
+            j = (buf[5] & (DISBIT7 | DISBIT6 | DISBIT5)) >> 4;
+            span_log(&s->logging, SPAN_LOG_FLOW, "Min bits test = 0x%X\n", buf[5]);
+            s->core.min_row_bits = (s->core.fast_bit_rate*minimum_scan_line_times[j])/1000;
+            span_log(&s->logging, SPAN_LOG_FLOW, "Min bits per row = %d\n", j);
+        }
+        /*endif*/
+        s->core.ecm_mode = (len >= 7)  &&  (buf[6] & DISBIT3);
+        s->core.image_data_mode = FALSE;
+        s->core.short_train = FALSE;
+        if (from_modem)
+            s->core.tcf_mode_predictable_modem_start = 2;
+        /*endif*/
+        break;
+    case T30_PPS:
+    case T30_PPS | 1:
+        switch (buf[3] & 0xFE)
+        {
+        case T30_EOP:
+        case T30_EOM:
+        case T30_EOS:
+        case T30_MPS:
+        case T30_PRI_EOP:
+        case T30_PRI_EOM:
+        case T30_PRI_MPS:
+            s->core.count_page_on_mcf = TRUE;
+            break;
+        }
+        /*endswitch*/
+        break;
+    case T30_EOP:
+    case T30_EOM:
+    case T30_EOS:
+    case T30_MPS:
+    case T30_PRI_EOP:
+    case T30_PRI_EOM:
+    case T30_PRI_MPS:
+    case T30_EOP | 1:
+    case T30_EOM | 1:
+    case T30_EOS | 1:
+    case T30_MPS | 1:
+    case T30_PRI_EOP | 1:
+    case T30_PRI_EOM | 1:
+    case T30_PRI_MPS | 1:
+        s->core.count_page_on_mcf = TRUE;
+        break;
+    case T30_MCF:
+    case T30_MCF | 1:
+        if (s->core.count_page_on_mcf)
+        {
+            s->core.pages_confirmed++;
+            span_log(&s->logging, SPAN_LOG_FLOW, "Pages confirmed = %d\n", s->core.pages_confirmed);
+            s->core.count_page_on_mcf = FALSE;
+        }
+        /*endif*/
+        break;
+    default:
+        break;
+    }
+    /*endswitch*/
+}
+/*- End of function --------------------------------------------------------*/
+
+static void queue_missing_indicator(t38_gateway_state_t *s, int data_type)
+{
+    t38_core_state_t *t;
+    
+    t = &s->t38x.t38;
+    /* Missing packets might have lost us the indicator that should have put us in
+       the required mode of operation. It might be a bit late to fill in such a gap
+       now, but we should try. We may also want to force indicators into the queue,
+       such as when the data says 'end of signal'. */
+    switch (data_type)
+    {
+    case T38_DATA_NONE:
+        if (t->current_rx_indicator != T38_IND_NO_SIGNAL)
+            process_rx_indicator(t, (void *) s, T38_IND_NO_SIGNAL);
+        break;
+    case T38_DATA_V21:
+        if (t->current_rx_indicator != T38_IND_V21_PREAMBLE)
+            process_rx_indicator(t, (void *) s, T38_IND_V21_PREAMBLE);
+        break;
+    case T38_DATA_V27TER_2400:
+        if (t->current_rx_indicator != T38_IND_V27TER_2400_TRAINING)
+            process_rx_indicator(t, (void *) s, T38_IND_V27TER_2400_TRAINING);
+        break;
+    case T38_DATA_V27TER_4800:
+        if (t->current_rx_indicator != T38_IND_V27TER_4800_TRAINING)
+            process_rx_indicator(t, (void *) s, T38_IND_V27TER_4800_TRAINING);
+        break;
+    case T38_DATA_V29_7200:
+        if (t->current_rx_indicator != T38_IND_V29_7200_TRAINING)
+            process_rx_indicator(t, (void *) s, T38_IND_V29_7200_TRAINING);
+        break;
+    case T38_DATA_V29_9600:
+        if (t->current_rx_indicator != T38_IND_V29_9600_TRAINING)
+            process_rx_indicator(t, (void *) s, T38_IND_V29_9600_TRAINING);
+        break;
+    case T38_DATA_V17_7200:
+        if (t->current_rx_indicator != T38_IND_V17_7200_SHORT_TRAINING  &&  t->current_rx_indicator != T38_IND_V17_7200_LONG_TRAINING)
+            process_rx_indicator(t, (void *) s, T38_IND_V17_7200_LONG_TRAINING);
+        break;
+    case T38_DATA_V17_9600:
+        if (t->current_rx_indicator != T38_IND_V17_9600_SHORT_TRAINING  &&  t->current_rx_indicator != T38_IND_V17_9600_LONG_TRAINING)
+            process_rx_indicator(t, (void *) s, T38_IND_V17_9600_LONG_TRAINING);
+        break;
+    case T38_DATA_V17_12000:
+        if (t->current_rx_indicator != T38_IND_V17_12000_SHORT_TRAINING  &&  t->current_rx_indicator != T38_IND_V17_12000_LONG_TRAINING)
+            process_rx_indicator(t, (void *) s, T38_IND_V17_12000_LONG_TRAINING);
+        break;
+    case T38_DATA_V17_14400:
+        if (t->current_rx_indicator != T38_IND_V17_14400_SHORT_TRAINING  &&  t->current_rx_indicator != T38_IND_V17_14400_LONG_TRAINING)
+            process_rx_indicator(t, (void *) s, T38_IND_V17_14400_LONG_TRAINING);
+        break;
+    case T38_DATA_V8:
+        break;
+    case T38_DATA_V34_PRI_RATE:
+        break;
+    case T38_DATA_V34_CC_1200:
+        break;
+    case T38_DATA_V34_PRI_CH:
+        break;
+    case T38_DATA_V33_12000:
+        break;
+    case T38_DATA_V33_14400:
+        break;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static int process_rx_missing(t38_core_state_t *t, void *user_data, int rx_seq_no, int expected_seq_no)
+{
+    t38_gateway_state_t *s;
+    
+    s = (t38_gateway_state_t *) user_data;
+    s->core.hdlc_to_modem.flags[s->core.hdlc_to_modem.in] |= HDLC_FLAG_MISSING_DATA;
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int process_rx_indicator(t38_core_state_t *t, void *user_data, int indicator)
+{
+    t38_gateway_state_t *s;
+    
+    s = (t38_gateway_state_t *) user_data;
+
+    if (t->current_rx_indicator == indicator)
+    {
+        /* This is probably due to the far end repeating itself. Ignore it. Its harmless */
+        return 0;
+    }
+    /*endif*/
+    if (s->core.hdlc_to_modem.contents[s->core.hdlc_to_modem.in])
+    {
+        if (++s->core.hdlc_to_modem.in >= T38_TX_HDLC_BUFS)
+            s->core.hdlc_to_modem.in = 0;
+        /*endif*/
+    }
+    /*endif*/
+    s->core.hdlc_to_modem.contents[s->core.hdlc_to_modem.in] = (indicator | FLAG_INDICATOR);
+    if (++s->core.hdlc_to_modem.in >= T38_TX_HDLC_BUFS)
+        s->core.hdlc_to_modem.in = 0;
+    /*endif*/
+    span_log(&s->logging,
+             SPAN_LOG_FLOW,
+             "Queued change - (%d) %s -> %s\n",
+             silence_gen_remainder(&(s->audio.modems.silence_gen)),
+             t38_indicator_to_str(t->current_rx_indicator),
+             t38_indicator_to_str(indicator));
+    s->t38x.current_rx_field_class = T38_FIELD_CLASS_NONE;
+    /* We need to set this here, since we might have been called as a fake
+       indication when the real one was missing */
+    t->current_rx_indicator = indicator;
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int process_rx_data(t38_core_state_t *t, void *user_data, int data_type, int field_type, const uint8_t *buf, int len)
+{
+    int i;
+    int previous;
+    t38_gateway_state_t *s;
+    t38_gateway_t38_state_t *xx;
+
+    s = (t38_gateway_state_t *) user_data;
+    xx = &s->t38x;
+    switch (field_type)
+    {
+    case T38_FIELD_HDLC_DATA:
+        xx->current_rx_field_class = T38_FIELD_CLASS_HDLC;
+        if (s->core.hdlc_to_modem.contents[s->core.hdlc_to_modem.in] != (data_type | FLAG_DATA))
+            queue_missing_indicator(s, data_type);
+        /*endif*/
+        previous = s->core.hdlc_to_modem.len[s->core.hdlc_to_modem.in];
+        /* Check if this data would overflow the buffer. */
+        if (s->core.hdlc_to_modem.len[s->core.hdlc_to_modem.in] + len > T38_MAX_HDLC_LEN)
+            break;
+        /*endif*/
+        s->core.hdlc_to_modem.contents[s->core.hdlc_to_modem.in] = (data_type | FLAG_DATA);
+        bit_reverse(&s->core.hdlc_to_modem.buf[s->core.hdlc_to_modem.in][s->core.hdlc_to_modem.len[s->core.hdlc_to_modem.in]], buf, len);
+        /* We need to send out the control messages as they are arriving. They are
+           too slow to capture a whole frame, and then pass it on.
+           For the faster frames, take in the whole frame before sending it out. Also, there
+           is no need to monitor, or modify, the contents of the faster frames. */
+        if (data_type == T38_DATA_V21)
+        {
+            for (i = 1;  i <= len;  i++)
+                edit_control_messages(s, 0, s->core.hdlc_to_modem.buf[s->core.hdlc_to_modem.in], s->core.hdlc_to_modem.len[s->core.hdlc_to_modem.in] + i);
+            /*endfor*/
+            /* Don't start pumping data into the actual output stream until there is
+               enough backlog to create some elasticity for jitter tolerance. */
+            if (s->core.hdlc_to_modem.len[s->core.hdlc_to_modem.in] + len >= HDLC_START_BUFFER_LEVEL)
+            {
+                if (s->core.hdlc_to_modem.in == s->core.hdlc_to_modem.out)
+                {
+                    if ((s->core.hdlc_to_modem.flags[s->core.hdlc_to_modem.in] & HDLC_FLAG_PROCEED_WITH_OUTPUT) == 0)
+                        previous = 0;
+                    /*endif*/
+                    hdlc_tx_frame(&s->audio.modems.hdlc_tx, s->core.hdlc_to_modem.buf[s->core.hdlc_to_modem.out] + previous, s->core.hdlc_to_modem.len[s->core.hdlc_to_modem.out] - previous + len);
+                }
+                /*endif*/
+                s->core.hdlc_to_modem.flags[s->core.hdlc_to_modem.in] |= HDLC_FLAG_PROCEED_WITH_OUTPUT;
+            }
+            /*endif*/
+        }
+        /*endif*/
+        s->core.hdlc_to_modem.len[s->core.hdlc_to_modem.in] += len;
+        break;
+    case T38_FIELD_HDLC_FCS_OK:
+        xx->current_rx_field_class = T38_FIELD_CLASS_HDLC;
+        if (len > 0)
+        {
+            span_log(&s->logging, SPAN_LOG_WARNING, "There is data in a T38_FIELD_HDLC_FCS_OK!\n");
+            /* The sender has incorrectly included data in this message. It is unclear what we should do
+               with it, to maximise tolerance of buggy implementations. */
+        }
+        /*endif*/
+        /* Some T.38 implementations send multiple T38_FIELD_HDLC_FCS_OK messages, in IFP packets with
+           incrementing sequence numbers, which are actually repeats. They get through to this point because
+           of the incrementing sequence numbers. We need to filter them here in a context sensitive manner. */
+        if (t->current_rx_data_type != data_type
+            ||
+            t->current_rx_field_type != field_type)
+        {
+            span_log(&s->logging, SPAN_LOG_FLOW, "HDLC frame type %s - CRC good\n", t30_frametype(s->core.hdlc_to_modem.buf[s->core.hdlc_to_modem.in][2]));
+            if (s->core.hdlc_to_modem.contents[s->core.hdlc_to_modem.in] != (data_type | FLAG_DATA))
+                queue_missing_indicator(s, data_type);
+            /*endif*/
+            s->core.hdlc_to_modem.contents[s->core.hdlc_to_modem.in] = (data_type | FLAG_DATA);
+            if (data_type == T38_DATA_V21)
+            {
+                if ((s->core.hdlc_to_modem.flags[s->core.hdlc_to_modem.in] & HDLC_FLAG_MISSING_DATA) == 0)
+                {
+                    monitor_control_messages(s, FALSE, s->core.hdlc_to_modem.buf[s->core.hdlc_to_modem.in], s->core.hdlc_to_modem.len[s->core.hdlc_to_modem.in]);
+                    if (s->core.real_time_frame_handler)
+                        s->core.real_time_frame_handler(s, s->core.real_time_frame_user_data, FALSE, s->core.hdlc_to_modem.buf[s->core.hdlc_to_modem.in], s->core.hdlc_to_modem.len[s->core.hdlc_to_modem.in]);
+                    /*endif*/
+                }
+                /*endif*/
+            }
+            else
+            {
+                /* Make sure we go back to short training if CTC/CTR has kicked us into
+                   long training. Theer has to be more than one value HDLC frame in a
+                   chunk of image data, so just setting short training mode heer should
+                   be enough. */
+                s->core.short_train = TRUE;
+            }
+            /*endif*/
+            pump_out_final_hdlc(s, (s->core.hdlc_to_modem.flags[s->core.hdlc_to_modem.in] & HDLC_FLAG_MISSING_DATA) == 0);
+        }
+        /*endif*/
+        s->core.hdlc_to_modem.len[s->core.hdlc_to_modem.in] = 0;
+        s->core.hdlc_to_modem.flags[s->core.hdlc_to_modem.in] = 0;
+        xx->corrupt_current_frame[0] = FALSE;
+        break;
+    case T38_FIELD_HDLC_FCS_BAD:
+        xx->current_rx_field_class = T38_FIELD_CLASS_HDLC;
+        if (len > 0)
+        {
+            span_log(&s->logging, SPAN_LOG_WARNING, "There is data in a T38_FIELD_HDLC_FCS_BAD!\n");
+            /* The sender has incorrectly included data in this message. We can safely ignore it, as the
+               bad FCS means we will throw away the whole message, anyway. */
+        }
+        /*endif*/
+        /* Some T.38 implementations send multiple T38_FIELD_HDLC_FCS_BAD messages, in IFP packets with
+           incrementing sequence numbers, which are actually repeats. They get through to this point because
+           of the incrementing sequence numbers. We need to filter them here in a context sensitive manner. */
+        if (t->current_rx_data_type != data_type  ||  t->current_rx_field_type != field_type)
+        {
+            span_log(&s->logging, SPAN_LOG_FLOW, "HDLC frame type %s - CRC bad\n", t30_frametype(s->core.hdlc_to_modem.buf[s->core.hdlc_to_modem.in][2]));
+            if (s->core.hdlc_to_modem.contents[s->core.hdlc_to_modem.in] != (data_type | FLAG_DATA))
+                queue_missing_indicator(s, data_type);
+            /*endif*/
+            if (s->core.hdlc_to_modem.len[s->core.hdlc_to_modem.in] > 0)
+            {
+                s->core.hdlc_to_modem.contents[s->core.hdlc_to_modem.in] = (data_type | FLAG_DATA);
+                pump_out_final_hdlc(s, FALSE);
+            }
+            /*endif*/
+        }
+        /*endif*/
+        s->core.hdlc_to_modem.len[s->core.hdlc_to_modem.in] = 0;
+        s->core.hdlc_to_modem.flags[s->core.hdlc_to_modem.in] = 0;
+        xx->corrupt_current_frame[0] = FALSE;
+        break;
+    case T38_FIELD_HDLC_FCS_OK_SIG_END:
+        xx->current_rx_field_class = T38_FIELD_CLASS_HDLC;
+        if (len > 0)
+        {
+            span_log(&s->logging, SPAN_LOG_WARNING, "There is data in a T38_FIELD_HDLC_FCS_OK_SIG_END!\n");
+            /* The sender has incorrectly included data in this message. It is unclear what we should do
+               with it, to maximise tolerance of buggy implementations. */
+        }
+        /*endif*/
+        /* Some T.38 implementations send multiple T38_FIELD_HDLC_FCS_OK_SIG_END messages, in IFP packets with
+           incrementing sequence numbers, which are actually repeats. They get through to this point because
+           of the incrementing sequence numbers. We need to filter them here in a context sensitive manner. */
+        if (t->current_rx_data_type != data_type  ||  t->current_rx_field_type != field_type)
+        {
+            span_log(&s->logging, SPAN_LOG_FLOW, "HDLC frame type %s - CRC OK, sig end\n", t30_frametype(s->core.hdlc_to_modem.buf[s->core.hdlc_to_modem.in][2]));
+            if (s->core.hdlc_to_modem.contents[s->core.hdlc_to_modem.in] != (data_type | FLAG_DATA))
+                queue_missing_indicator(s, data_type);
+            /*endif*/
+            s->core.hdlc_to_modem.contents[s->core.hdlc_to_modem.in] = (data_type | FLAG_DATA);
+            if (data_type == T38_DATA_V21  &&  (s->core.hdlc_to_modem.flags[s->core.hdlc_to_modem.in] & HDLC_FLAG_MISSING_DATA) == 0)
+            {
+                monitor_control_messages(s, FALSE, s->core.hdlc_to_modem.buf[s->core.hdlc_to_modem.in], s->core.hdlc_to_modem.len[s->core.hdlc_to_modem.in]);
+                if (s->core.real_time_frame_handler)
+                    s->core.real_time_frame_handler(s, s->core.real_time_frame_user_data, FALSE, s->core.hdlc_to_modem.buf[s->core.hdlc_to_modem.in], s->core.hdlc_to_modem.len[s->core.hdlc_to_modem.in]);
+                /*endif*/
+            }
+            /*endif*/
+            pump_out_final_hdlc(s, (s->core.hdlc_to_modem.flags[s->core.hdlc_to_modem.in] & HDLC_FLAG_MISSING_DATA) == 0);
+            s->core.hdlc_to_modem.len[s->core.hdlc_to_modem.in] = 0;
+            s->core.hdlc_to_modem.flags[s->core.hdlc_to_modem.in] = 0;
+            s->core.hdlc_to_modem.contents[s->core.hdlc_to_modem.in] = 0;
+            queue_missing_indicator(s, T38_DATA_NONE);
+            xx->current_rx_field_class = T38_FIELD_CLASS_NONE;
+        }
+        /*endif*/
+        xx->corrupt_current_frame[0] = FALSE;
+        break;
+    case T38_FIELD_HDLC_FCS_BAD_SIG_END:
+        xx->current_rx_field_class = T38_FIELD_CLASS_HDLC;
+        if (len > 0)
+        {
+            span_log(&s->logging, SPAN_LOG_WARNING, "There is data in a T38_FIELD_HDLC_FCS_BAD_SIG_END!\n");
+            /* The sender has incorrectly included data in this message. We can safely ignore it, as the
+               bad FCS means we will throw away the whole message, anyway. */
+        }
+        /*endif*/
+        /* Some T.38 implementations send multiple T38_FIELD_HDLC_FCS_BAD_SIG_END messages, in IFP packets with
+           incrementing sequence numbers, which are actually repeats. They get through to this point because
+           of the incrementing sequence numbers. We need to filter them here in a context sensitive manner. */
+        if (t->current_rx_data_type != data_type  ||  t->current_rx_field_type != field_type)
+        {
+            span_log(&s->logging, SPAN_LOG_FLOW, "HDLC frame type %s - CRC bad, sig end\n", t30_frametype(s->core.hdlc_to_modem.buf[s->core.hdlc_to_modem.in][2]));
+            if (s->core.hdlc_to_modem.contents[s->core.hdlc_to_modem.in] != (data_type | FLAG_DATA))
+                queue_missing_indicator(s, data_type);
+            /*endif*/
+            if (s->core.hdlc_to_modem.len[s->core.hdlc_to_modem.in] > 0)
+            {
+                s->core.hdlc_to_modem.contents[s->core.hdlc_to_modem.in] = (data_type | FLAG_DATA);
+                pump_out_final_hdlc(s, FALSE);
+            }
+            /*endif*/
+            s->core.hdlc_to_modem.len[s->core.hdlc_to_modem.in] = 0;
+            s->core.hdlc_to_modem.flags[s->core.hdlc_to_modem.in] = 0;
+            s->core.hdlc_to_modem.contents[s->core.hdlc_to_modem.in] = 0;
+            queue_missing_indicator(s, T38_DATA_NONE);
+            xx->current_rx_field_class = T38_FIELD_CLASS_NONE;
+        }
+        /*endif*/
+        xx->corrupt_current_frame[0] = FALSE;
+        break;
+    case T38_FIELD_HDLC_SIG_END:
+        if (len > 0)
+        {
+            span_log(&s->logging, SPAN_LOG_WARNING, "There is data in a T38_FIELD_HDLC_SIG_END!\n");
+            /* The sender has incorrectly included data in this message, but there seems nothing meaningful
+               it could be. There could not be an FCS good/bad report beyond this. */
+        }
+        /*endif*/
+        /* Some T.38 implementations send multiple T38_FIELD_HDLC_SIG_END messages, in IFP packets with
+           incrementing sequence numbers, which are actually repeats. They get through to this point because
+           of the incrementing sequence numbers. We need to filter them here in a context sensitive manner. */
+        if (t->current_rx_data_type != data_type  ||  t->current_rx_field_type != field_type)
+        {
+            if (s->core.hdlc_to_modem.contents[s->core.hdlc_to_modem.in] != (data_type | FLAG_DATA))
+                queue_missing_indicator(s, data_type);
+            /* WORKAROUND: At least some Mediatrix boxes have a bug, where they can send this message at the
+                           end of non-ECM data. We need to tolerate this. */
+            if (xx->current_rx_field_class == T38_FIELD_CLASS_NON_ECM)
+            {
+                span_log(&s->logging, SPAN_LOG_WARNING, "T38_FIELD_HDLC_SIG_END received at the end of non-ECM data!\n");
+                /* Don't flow control the data any more. Just pump out the remainder as fast as we can. */
+                t38_non_ecm_buffer_push(&s->core.non_ecm_to_modem);
+            }
+            else
+            {
+                /* This message is expected under 2 circumstances. One is as an alternative to T38_FIELD_HDLC_FCS_OK_SIG_END - 
+                   i.e. they send T38_FIELD_HDLC_FCS_OK, and then T38_FIELD_HDLC_SIG_END when the carrier actually drops.
+                   The other is because the HDLC signal drops unexpectedly - i.e. not just after a final frame. */
+                s->core.hdlc_to_modem.len[s->core.hdlc_to_modem.in] = 0;
+                s->core.hdlc_to_modem.flags[s->core.hdlc_to_modem.in] = 0;
+                s->core.hdlc_to_modem.contents[s->core.hdlc_to_modem.in] = 0;
+            }
+            /*endif*/
+            queue_missing_indicator(s, T38_DATA_NONE);
+            xx->current_rx_field_class = T38_FIELD_CLASS_NONE;
+        }
+        /*endif*/
+        xx->corrupt_current_frame[0] = FALSE;
+        break;
+    case T38_FIELD_T4_NON_ECM_DATA:
+        xx->current_rx_field_class = T38_FIELD_CLASS_NON_ECM;
+        if (s->core.hdlc_to_modem.contents[s->core.hdlc_to_modem.in] != (data_type | FLAG_DATA))
+            queue_missing_indicator(s, data_type);
+        t38_non_ecm_buffer_inject(&s->core.non_ecm_to_modem, buf, len);
+        xx->corrupt_current_frame[0] = FALSE;
+        break;
+    case T38_FIELD_T4_NON_ECM_SIG_END:
+        /* Some T.38 implementations send multiple T38_FIELD_T4_NON_ECM_SIG_END messages, in IFP packets with
+           incrementing sequence numbers, which are actually repeats. They get through to this point because
+           of the incrementing sequence numbers. We need to filter them here in a context sensitive manner. */
+        if (t->current_rx_data_type != data_type  ||  t->current_rx_field_type != field_type)
+        {
+            /* WORKAROUND: At least some Mediatrix boxes have a bug, where they can send HDLC signal end where
+                           they should send non-ECM signal end. It is possible they also do the opposite.
+                           We need to tolerate this. */
+            if (xx->current_rx_field_class == T38_FIELD_CLASS_NON_ECM)
+            {
+                if (len > 0)
+                {
+                    if (s->core.hdlc_to_modem.contents[s->core.hdlc_to_modem.in] != (data_type | FLAG_DATA))
+                        queue_missing_indicator(s, data_type);
+                    /*endif*/
+                    t38_non_ecm_buffer_inject(&s->core.non_ecm_to_modem, buf, len);
+                }
+                /*endif*/
+                if (s->core.hdlc_to_modem.contents[s->core.hdlc_to_modem.in] != (data_type | FLAG_DATA))
+                    queue_missing_indicator(s, data_type);
+                /*endif*/
+                /* Don't flow control the data any more. Just pump out the remainder as fast as we can. */
+                t38_non_ecm_buffer_push(&s->core.non_ecm_to_modem);
+            }
+            else
+            {
+                span_log(&s->logging, SPAN_LOG_WARNING, "T38_FIELD_NON_ECM_SIG_END received at the end of HDLC data!\n");
+                if (s->core.hdlc_to_modem.contents[s->core.hdlc_to_modem.in] != (data_type | FLAG_DATA))
+                    queue_missing_indicator(s, data_type);
+                /*endif*/
+                s->core.hdlc_to_modem.len[s->core.hdlc_to_modem.in] = 0;
+                s->core.hdlc_to_modem.flags[s->core.hdlc_to_modem.in] = 0;
+                s->core.hdlc_to_modem.contents[s->core.hdlc_to_modem.in] = 0;
+            }
+            /*endif*/
+            queue_missing_indicator(s, T38_DATA_NONE);
+            xx->current_rx_field_class = T38_FIELD_CLASS_NONE;
+        }
+        /*endif*/
+        xx->corrupt_current_frame[0] = FALSE;
+        break;
+    case T38_FIELD_CM_MESSAGE:
+        if (len >= 1)
+            span_log(&s->logging, SPAN_LOG_FLOW, "CM profile %d - %s\n", buf[0] - '0', t38_cm_profile_to_str(buf[0]));
+        else
+            span_log(&s->logging, SPAN_LOG_FLOW, "Bad length for CM message - %d\n", len);
+        /*endif*/
+        break;
+    case T38_FIELD_JM_MESSAGE:
+        if (len >= 2)
+            span_log(&s->logging, SPAN_LOG_FLOW, "JM - %s\n", t38_jm_to_str(buf, len));
+        else
+            span_log(&s->logging, SPAN_LOG_FLOW, "Bad length for JM message - %d\n", len);
+        /*endif*/
+        break;
+    case T38_FIELD_CI_MESSAGE:
+        if (len >= 1)
+            span_log(&s->logging, SPAN_LOG_FLOW, "CI 0x%X\n", buf[0]);
+        else
+            span_log(&s->logging, SPAN_LOG_FLOW, "Bad length for CI message - %d\n", len);
+        /*endif*/
+        break;
+    case T38_FIELD_V34RATE:
+        if (len >= 3)
+        {
+            xx->t38.v34_rate = t38_v34rate_to_bps(buf, len);
+            span_log(&s->logging, SPAN_LOG_FLOW, "V.34 rate %d bps\n", xx->t38.v34_rate);
+        }
+        else
+        {
+            span_log(&s->logging, SPAN_LOG_FLOW, "Bad length for V34rate message - %d\n", len);
+        }
+        /*endif*/
+        break;
+    default:
+        break;
+    }
+    /*endswitch*/
+
+#if 0
+    if (span_log_test(&s->logging, SPAN_LOG_FLOW))
+    {
+        int i;
+
+        if (len > 0)
+        {
+            span_log(&s->logging, SPAN_LOG_FLOW, "Data: ");
+            for (i = 0;  i < len;  i++)
+                span_log(&s->logging, SPAN_LOG_FLOW | SPAN_LOG_SUPPRESS_LABELLING, " %02X", buf[i]);
+            /*endfor*/
+        }
+        /*endif*/
+    }
+    /*endif*/
+    span_log(&s->logging, SPAN_LOG_FLOW | SPAN_LOG_SUPPRESS_LABELLING, "\n");
+#endif
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void set_octets_per_data_packet(t38_gateway_state_t *s, int bit_rate)
+{
+    int octets;
+    
+    octets = MS_PER_TX_CHUNK*bit_rate/(8*1000);
+    if (octets < 1)
+        octets = 1;
+    /*endif*/
+    s->core.to_t38.octets_per_data_packet = octets;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int set_slow_packetisation(t38_gateway_state_t *s)
+{
+    set_octets_per_data_packet(s, 300);
+    s->t38x.current_tx_data_type = T38_DATA_V21;
+    return T38_IND_V21_PREAMBLE;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int set_fast_packetisation(t38_gateway_state_t *s)
+{
+    int ind;
+
+    ind = T38_IND_NO_SIGNAL;
+    switch (s->core.fast_rx_active)
+    {
+    case T38_V17_RX:
+        set_octets_per_data_packet(s, s->core.fast_bit_rate);
+        switch (s->core.fast_bit_rate)
+        {
+        case 7200:
+            ind = (s->core.short_train)  ?  T38_IND_V17_7200_SHORT_TRAINING  :  T38_IND_V17_7200_LONG_TRAINING;
+            s->t38x.current_tx_data_type = T38_DATA_V17_7200;
+            break;
+        case 9600:
+            ind = (s->core.short_train)  ?  T38_IND_V17_9600_SHORT_TRAINING  :  T38_IND_V17_9600_LONG_TRAINING;
+            s->t38x.current_tx_data_type = T38_DATA_V17_9600;
+            break;
+        case 12000:
+            ind = (s->core.short_train)  ?  T38_IND_V17_12000_SHORT_TRAINING  :  T38_IND_V17_12000_LONG_TRAINING;
+            s->t38x.current_tx_data_type = T38_DATA_V17_12000;
+            break;
+        default:
+        case 14400:
+            ind = (s->core.short_train)  ?  T38_IND_V17_14400_SHORT_TRAINING  :  T38_IND_V17_14400_LONG_TRAINING;
+            s->t38x.current_tx_data_type = T38_DATA_V17_14400;
+            break;
+        }
+        break;
+    case T38_V27TER_RX:
+        set_octets_per_data_packet(s, s->core.fast_bit_rate);
+        switch (s->core.fast_bit_rate)
+        {
+        case 2400:
+            ind = T38_IND_V27TER_2400_TRAINING;
+            s->t38x.current_tx_data_type = T38_DATA_V27TER_2400;
+            break;
+        default:
+        case 4800:
+            ind = T38_IND_V27TER_4800_TRAINING;
+            s->t38x.current_tx_data_type = T38_DATA_V27TER_4800;
+            break;
+        }
+        break;
+    case T38_V29_RX:
+        set_octets_per_data_packet(s, s->core.fast_bit_rate);
+        switch (s->core.fast_bit_rate)
+        {
+        case 7200:
+            ind = T38_IND_V29_7200_TRAINING;
+            s->t38x.current_tx_data_type = T38_DATA_V29_7200;
+            break;
+        default:
+        case 9600:
+            ind = T38_IND_V29_9600_TRAINING;
+            s->t38x.current_tx_data_type = T38_DATA_V29_9600;
+            break;
+        }
+        break;
+    }
+    return ind;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void announce_training(t38_gateway_state_t *s)
+{
+    t38_core_send_indicator(&s->t38x.t38, set_fast_packetisation(s), s->t38x.t38.indicator_tx_count);
+}
+/*- End of function --------------------------------------------------------*/
+
+static void non_ecm_rx_status(void *user_data, int status)
+{
+    t38_gateway_state_t *s;
+
+    s = (t38_gateway_state_t *) user_data;
+    switch (status)
+    {
+    case PUTBIT_TRAINING_IN_PROGRESS:
+        span_log(&s->logging, SPAN_LOG_FLOW, "Non-ECM carrier training in progress\n");
+        if (s->core.tcf_mode_predictable_modem_start)
+            s->core.tcf_mode_predictable_modem_start = 0;
+        else
+            announce_training(s);
+        break;
+    case PUTBIT_TRAINING_FAILED:
+        span_log(&s->logging, SPAN_LOG_FLOW, "Non-ECM carrier training failed\n");
+        break;
+    case PUTBIT_TRAINING_SUCCEEDED:
+        /* The modem is now trained */
+        span_log(&s->logging, SPAN_LOG_FLOW, "Non-ECM carrier trained\n");
+        s->audio.modems.rx_signal_present = TRUE;
+        s->audio.modems.rx_trained = TRUE;
+        to_t38_buffer_init(&s->core.to_t38);
+        break;
+    case PUTBIT_CARRIER_UP:
+        span_log(&s->logging, SPAN_LOG_FLOW, "Non-ECM carrier up\n");
+        break;
+    case PUTBIT_CARRIER_DOWN:
+        span_log(&s->logging, SPAN_LOG_FLOW, "Non-ECM carrier down\n");
+        s->core.tcf_mode_predictable_modem_start = 0;
+        switch (s->t38x.current_tx_data_type)
+        {
+        case T38_DATA_V17_7200:
+        case T38_DATA_V17_9600:
+        case T38_DATA_V17_12000:
+        case T38_DATA_V17_14400:
+        case T38_DATA_V27TER_2400:
+        case T38_DATA_V27TER_4800:
+        case T38_DATA_V29_7200:
+        case T38_DATA_V29_9600:
+            non_ecm_push_residue(s);
+            t38_core_send_indicator(&s->t38x.t38, T38_IND_NO_SIGNAL, s->t38x.t38.indicator_tx_count);
+            restart_rx_modem(s);
+            break;
+        }
+        break;
+    default:
+        span_log(&s->logging, SPAN_LOG_WARNING, "Unexpected non-ECM special bit - %d!\n", status);
+        break;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static void to_t38_buffer_init(t38_gateway_to_t38_state_t *s)
+{
+    s->data_ptr = 0;
+    s->bit_stream = 0xFFFF;
+    s->bit_no = 0;
+
+    s->in_bits = 0;
+    s->out_octets = 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void non_ecm_push_residue(t38_gateway_state_t *t)
+{
+    t38_gateway_to_t38_state_t *s;
+
+    s = &t->core.to_t38;
+    if (s->bit_no)
+    {
+        /* There is a fractional octet in progress. We might as well send every last bit we can. */
+        s->data[s->data_ptr++] = s->bit_stream << (8 - s->bit_no);
+    }
+    t38_core_send_data(&t->t38x.t38, t->t38x.current_tx_data_type, T38_FIELD_T4_NON_ECM_SIG_END, s->data, s->data_ptr, t->t38x.t38.data_end_tx_count);
+    s->out_octets += s->data_ptr;
+    s->in_bits += s->bits_absorbed;
+    s->data_ptr = 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void non_ecm_push(t38_gateway_state_t *t)
+{
+    t38_gateway_to_t38_state_t *s;
+
+    s = &t->core.to_t38;
+    if (s->data_ptr)
+    {
+        t38_core_send_data(&t->t38x.t38, t->t38x.current_tx_data_type, T38_FIELD_T4_NON_ECM_DATA, s->data, s->data_ptr, t->t38x.t38.data_tx_count);
+        s->out_octets += s->data_ptr;
+        s->in_bits += s->bits_absorbed;
+        s->bits_absorbed = 0;
+        s->data_ptr = 0;
+    }
+    /*endif*/
+}
+/*- End of function --------------------------------------------------------*/
+
+static void non_ecm_put_bit(void *user_data, int bit)
+{
+    t38_gateway_state_t *t;
+    t38_gateway_to_t38_state_t *s;
+    
+    if (bit < 0)
+    {
+        non_ecm_rx_status(user_data, bit);
+        return;
+    }
+    t = (t38_gateway_state_t *) user_data;
+    s = &t->core.to_t38;
+
+    s->in_bits++;
+    bit &= 1;
+    s->bit_stream = (s->bit_stream << 1) | bit;
+    if (++s->bit_no >= 8)
+    {
+        s->data[s->data_ptr++] = (uint8_t) s->bit_stream & 0xFF;
+        if (s->data_ptr >= s->octets_per_data_packet)
+            non_ecm_push(t);
+        s->bit_no = 0;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static void non_ecm_remove_fill_and_put_bit(void *user_data, int bit)
+{
+    t38_gateway_state_t *t;
+    t38_gateway_to_t38_state_t *s;
+    
+    if (bit < 0)
+    {
+        non_ecm_rx_status(user_data, bit);
+        return;
+    }
+    t = (t38_gateway_state_t *) user_data;
+    s = &t->core.to_t38;
+
+    s->bits_absorbed++;
+    bit &= 1;
+    /* Drop any extra zero bits when we already have enough for an EOL symbol. */
+    /* The snag here is that if we just look for 11 bits, a line ending with
+       a code that has trailing zero bits will cause problems. The longest run of
+       trailing zeros for any code is 3, so we need to look for at least 14 zeros
+       if we don't want to actually analyse the compressed data in depth. This means
+       we do not strip every fill bit, but we strip most of them. */
+    if ((s->bit_stream & 0x3FFF) == 0  &&  bit == 0)
+    {
+        if (s->bits_absorbed > 2*8*s->octets_per_data_packet)
+        {
+            /* We need to pump out what we have, even though we have not accumulated a full
+               buffer of data. If we don't, we stand to delay rows excessively, so the far
+               end gateway (assuming the far end is a gateway) cannot play them out. */
+            non_ecm_push(t);
+        }
+        return;
+    }
+    s->bit_stream = (s->bit_stream << 1) | bit;
+    if (++s->bit_no >= 8)
+    {
+        s->data[s->data_ptr++] = (uint8_t) s->bit_stream & 0xFF;
+        if (s->data_ptr >= s->octets_per_data_packet)
+            non_ecm_push(t);
+        s->bit_no = 0;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static void hdlc_rx_status(hdlc_rx_state_t *t, int status)
+{
+    t38_gateway_state_t *s;
+
+    s = (t38_gateway_state_t *) t->user_data;
+    switch (status)
+    {
+    case PUTBIT_TRAINING_IN_PROGRESS:
+        span_log(&s->logging, SPAN_LOG_FLOW, "HDLC carrier training in progress\n");
+        announce_training(s);
+        break;
+    case PUTBIT_TRAINING_FAILED:
+        span_log(&s->logging, SPAN_LOG_FLOW, "HDLC carrier training failed\n");
+        break;
+    case PUTBIT_TRAINING_SUCCEEDED:
+        /* The modem is now trained. */
+        span_log(&s->logging, SPAN_LOG_FLOW, "HDLC carrier trained\n");
+        s->audio.modems.rx_signal_present = TRUE;
+        s->audio.modems.rx_trained = TRUE;
+        /* Behave like HDLC preamble has been announced. */
+        t->framing_ok_announced = TRUE;
+        to_t38_buffer_init(&s->core.to_t38);
+        break;
+    case PUTBIT_CARRIER_UP:
+        span_log(&s->logging, SPAN_LOG_FLOW, "HDLC carrier up\n");
+        /* Reset the HDLC receiver. */
+        t->raw_bit_stream = 0;
+        t->len = 0;
+        t->num_bits = 0;
+        t->flags_seen = 0;
+        t->framing_ok_announced = FALSE;
+        to_t38_buffer_init(&s->core.to_t38);
+        break;
+    case PUTBIT_CARRIER_DOWN:
+        span_log(&s->logging, SPAN_LOG_FLOW, "HDLC carrier down\n");
+        if (t->framing_ok_announced)
+        {
+            t38_core_send_data(&s->t38x.t38, s->t38x.current_tx_data_type, T38_FIELD_HDLC_SIG_END, NULL, 0, s->t38x.t38.data_end_tx_count);
+            t38_core_send_indicator(&s->t38x.t38, T38_IND_NO_SIGNAL, s->t38x.t38.indicator_tx_count);
+            t->framing_ok_announced = FALSE;
+        }
+        restart_rx_modem(s);
+        if (s->core.tcf_mode_predictable_modem_start == 2)
+        {
+            /* If we are doing TCF, we need to announce the fast carrier training very
+               quickly, to ensure it starts 75+-20ms after the HDLC carrier ends. Waiting until
+               it trains will be too late. We need to announce the fast modem a fixed time after
+               the end of the V.21 carrier, in anticipation of its arrival. */
+            s->core.samples_to_timeout = ms_to_samples(75);
+            s->core.tcf_mode_predictable_modem_start = 1;
+        }
+        break;
+    default:
+        span_log(&s->logging, SPAN_LOG_WARNING, "Unexpected HDLC special bit - %d!\n", status);
+        break;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static void rx_flag_or_abort(hdlc_rx_state_t *t)
+{
+    t38_gateway_state_t *s;
+    t38_gateway_to_t38_state_t *u;
+    
+    s = (t38_gateway_state_t *) t->user_data;
+    u = &s->core.to_t38;
+    if ((t->raw_bit_stream & 0x80))
+    {
+        /* Hit HDLC abort */
+        t->rx_aborts++;
+        if (t->flags_seen < t->framing_ok_threshold)
+            t->flags_seen = 0;
+        else
+            t->flags_seen = t->framing_ok_threshold - 1;
+        /*endif*/
+    }
+    else
+    {
+        /* Hit HDLC flag */
+        if (t->flags_seen >= t->framing_ok_threshold)
+        {
+            if (t->len)
+            {
+                /* This is not back-to-back flags */
+                if (t->len >= 2)
+                {
+                    if (u->data_ptr)
+                    {
+                        bit_reverse(u->data, t->buffer + t->len - 2 - u->data_ptr, u->data_ptr);
+                        t38_core_send_data(&s->t38x.t38, s->t38x.current_tx_data_type, T38_FIELD_HDLC_DATA, u->data, u->data_ptr, s->t38x.t38.data_tx_count);
+                    }
+                    /*endif*/
+                    if (t->num_bits != 7)
+                    {
+                        t->rx_crc_errors++;
+                        span_log(&s->logging, SPAN_LOG_FLOW, "HDLC frame type %s, misaligned terminating flag at %d\n", t30_frametype(t->buffer[2]), t->len);
+                        /* It seems some boxes may not like us sending a _SIG_END here, and then another
+                           when the carrier actually drops. Lets just send T38_FIELD_HDLC_FCS_OK here. */
+                        if (t->len > 2)
+                            t38_core_send_data(&s->t38x.t38, s->t38x.current_tx_data_type, T38_FIELD_HDLC_FCS_BAD, NULL, 0, s->t38x.t38.data_tx_count);
+                        /*endif*/
+                    }
+                    else if ((u->crc & 0xFFFF) != 0xF0B8)
+                    {
+                        t->rx_crc_errors++;
+                        span_log(&s->logging, SPAN_LOG_FLOW, "HDLC frame type %s, bad CRC at %d\n", t30_frametype(t->buffer[2]), t->len);
+                        /* It seems some boxes may not like us sending a _SIG_END here, and then another
+                           when the carrier actually drops. Lets just send T38_FIELD_HDLC_FCS_OK here. */
+                        if (t->len > 2)
+                            t38_core_send_data(&s->t38x.t38, s->t38x.current_tx_data_type, T38_FIELD_HDLC_FCS_BAD, NULL, 0, s->t38x.t38.data_tx_count);
+                        /*endif*/
+                    }
+                    else
+                    {
+                        t->rx_frames++;
+                        t->rx_bytes += t->len - 2;
+                        span_log(&s->logging, SPAN_LOG_FLOW, "HDLC frame type %s, CRC OK\n", t30_frametype(t->buffer[2]));
+                        if (s->t38x.current_tx_data_type == T38_DATA_V21)
+                        {
+                            monitor_control_messages(s, TRUE, t->buffer, t->len - 2);
+                            if (s->core.real_time_frame_handler)
+                                s->core.real_time_frame_handler(s, s->core.real_time_frame_user_data, TRUE, t->buffer, t->len - 2);
+                            /*endif*/
+                        }
+                        else
+                        {
+                            /* Make sure we go back to short training if CTC/CTR has kicked us into
+                               long training. Any successful HDLC frame received at a rate other than
+                               V.21 is an adequate indication we should change. */
+                            s->core.short_train = TRUE;
+                        }
+                        /*endif*/
+                        /* It seems some boxes may not like us sending a _SIG_END here, and then another
+                           when the carrier actually drops. Lets just send T38_FIELD_HDLC_FCS_OK here. */
+                        t38_core_send_data(&s->t38x.t38, s->t38x.current_tx_data_type, T38_FIELD_HDLC_FCS_OK, NULL, 0, s->t38x.t38.data_tx_count);
+                    }
+                    /*endif*/
+                }
+                else
+                {
+                    /* Frame too short */
+                    t->rx_length_errors++;
+                }
+                /*endif*/
+            }
+            /*endif*/
+        }
+        else
+        {
+            /* Check the flags are back-to-back when testing for valid preamble. This
+               greatly reduces the chances of false preamble detection, and anything
+               which doesn't send them back-to-back is badly broken. */
+            if (t->num_bits != 7)
+                t->flags_seen = 0;
+            /*endif*/
+            if (++t->flags_seen >= t->framing_ok_threshold  &&  !t->framing_ok_announced)
+            {
+                if (s->t38x.current_tx_data_type == T38_DATA_V21)
+                {
+                    t38_core_send_indicator(&s->t38x.t38, set_slow_packetisation(s), s->t38x.t38.indicator_tx_count);
+                    s->audio.modems.rx_signal_present = TRUE;
+                }
+                /*endif*/
+                if (s->t38x.in_progress_rx_indicator == T38_IND_CNG)
+                    set_next_tx_type(s);
+                /*endif*/
+                t->framing_ok_announced = TRUE;
+            }
+            /*endif*/
+        }
+        /*endif*/
+    }
+    /*endif*/
+    t->len = 0;
+    t->num_bits = 0;
+    u->crc = 0xFFFF;
+    u->data_ptr = 0;
+    s->t38x.corrupt_current_frame[1] = FALSE;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void t38_hdlc_rx_put_bit(hdlc_rx_state_t *t, int new_bit)
+{
+    t38_gateway_state_t *s;
+    t38_gateway_to_t38_state_t *u;
+
+    if (new_bit < 0)
+    {
+        hdlc_rx_status(t, new_bit);
+        return;
+    }
+    /*endif*/
+    t->raw_bit_stream = (t->raw_bit_stream << 1) | (new_bit & 1);
+    if ((t->raw_bit_stream & 0x3F) == 0x3E)
+    {
+        /* Its time to either skip a bit, for stuffing, or process a flag or abort */
+        if ((t->raw_bit_stream & 0x40))
+            rx_flag_or_abort(t);
+        return;
+    }
+    /*endif*/
+    t->num_bits++;
+    if (!t->framing_ok_announced)
+        return;
+    /*endif*/
+    t->byte_in_progress = (t->byte_in_progress >> 1) | ((t->raw_bit_stream & 0x01) << 7);
+    if (t->num_bits != 8)
+        return;
+    /*endif*/
+    t->num_bits = 0;
+    if (t->len >= (int) sizeof(t->buffer))
+    {
+        /* This is too long. Abandon the frame, and wait for the next flag octet. */
+        t->rx_length_errors++;
+        t->flags_seen = t->framing_ok_threshold - 1;
+        t->len = 0;
+        return;
+    }
+    /*endif*/
+    s = (t38_gateway_state_t *) t->user_data;
+    u = &s->core.to_t38;
+    t->buffer[t->len] = (uint8_t) t->byte_in_progress;
+    /* Calculate the CRC progressively, before we start altering the frame */
+    u->crc = crc_itu16_calc(&t->buffer[t->len], 1, u->crc);
+    /* Make the transmission lag by two octets, so we do not send the CRC, and
+       do not report the CRC result too late. */
+    if (++t->len <= 2)
+        return;
+    /*endif*/
+    if (s->t38x.current_tx_data_type == T38_DATA_V21)
+    {
+        /* The V.21 control messages need to be monitored, and possibly corrupted, to manage the
+           man-in-the-middle role of T.38 */
+        edit_control_messages(s, 1, t->buffer, t->len);
+    }
+    if (++u->data_ptr >= u->octets_per_data_packet)
+    {
+        bit_reverse(u->data, t->buffer + t->len - 2 - u->data_ptr, u->data_ptr);
+        t38_core_send_data(&s->t38x.t38, s->t38x.current_tx_data_type, T38_FIELD_HDLC_DATA, u->data, u->data_ptr, s->t38x.t38.data_tx_count);
+        /* Since we delay transmission by 2 octets, we should now have sent the last of the data octets when
+           we have just received the last of the CRC octets. */
+        u->data_ptr = 0;
+    }
+    /*endif*/
+}
+/*- End of function --------------------------------------------------------*/
+
+static int restart_rx_modem(t38_gateway_state_t *s)
+{
+    put_bit_func_t put_bit_func;
+    void *put_bit_user_data;
+
+    if (s->core.to_t38.in_bits  ||  s->core.to_t38.out_octets)
+    {
+        span_log(&s->logging,
+                 SPAN_LOG_FLOW,
+                 "%d incoming audio bits.  %d outgoing T.38 octets\n",
+                 s->core.to_t38.in_bits,
+                 s->core.to_t38.out_octets);
+        s->core.to_t38.in_bits = 0;
+        s->core.to_t38.out_octets = 0;
+    }
+    span_log(&s->logging, SPAN_LOG_FLOW, "Restart rx modem - modem = %d, short train = %d, ECM = %d\n", s->core.fast_modem, s->core.short_train, s->core.ecm_mode);
+
+    hdlc_rx_init(&(s->audio.modems.hdlc_rx), FALSE, TRUE, HDLC_FRAMING_OK_THRESHOLD, NULL, s);
+    s->audio.modems.rx_signal_present = FALSE;
+    s->audio.modems.rx_trained = FALSE;
+    /* Default to the transmit data being V.21, unless a faster modem pops up trained. */
+    s->t38x.current_tx_data_type = T38_DATA_V21;
+    fsk_rx_init(&(s->audio.modems.v21_rx), &preset_fsk_specs[FSK_V21CH2], TRUE, (put_bit_func_t) t38_hdlc_rx_put_bit, &(s->audio.modems.hdlc_rx));
+#if 0
+    fsk_rx_signal_cutoff(&(s->audio.modems.v21_rx), -45.5);
+#endif
+    if (s->core.image_data_mode  &&  s->core.ecm_mode)
+    {
+        put_bit_func = (put_bit_func_t) t38_hdlc_rx_put_bit;
+        put_bit_user_data = (void *) &(s->audio.modems.hdlc_rx);
+    }
+    else
+    {
+        if (s->core.image_data_mode  &&  s->core.to_t38.fill_bit_removal)
+            put_bit_func = non_ecm_remove_fill_and_put_bit;
+        else
+            put_bit_func = non_ecm_put_bit;
+        put_bit_user_data = (void *) s;
+    }
+    /*endif*/
+    to_t38_buffer_init(&s->core.to_t38);
+    s->core.to_t38.octets_per_data_packet = 1;
+    switch (s->core.fast_modem)
+    {
+    case T38_V17_RX:
+        v17_rx_restart(&s->audio.modems.v17_rx, s->core.fast_bit_rate, s->core.short_train);
+        v17_rx_set_put_bit(&s->audio.modems.v17_rx, put_bit_func, put_bit_user_data);
+        set_rx_handler(s, (span_rx_handler_t *) &v17_v21_rx, s);
+        s->core.fast_rx_active = T38_V17_RX;
+        break;
+    case T38_V27TER_RX:
+        v27ter_rx_restart(&s->audio.modems.v27ter_rx, s->core.fast_bit_rate, FALSE);
+        v27ter_rx_set_put_bit(&s->audio.modems.v27ter_rx, put_bit_func, put_bit_user_data);
+        set_rx_handler(s, (span_rx_handler_t *) &v27ter_v21_rx, s);
+        s->core.fast_rx_active = T38_V27TER_RX;
+        break;
+    case T38_V29_RX:
+        v29_rx_restart(&s->audio.modems.v29_rx, s->core.fast_bit_rate, FALSE);
+        v29_rx_set_put_bit(&s->audio.modems.v29_rx, put_bit_func, put_bit_user_data);
+        set_rx_handler(s, (span_rx_handler_t *) &v29_v21_rx, s);
+        s->core.fast_rx_active = T38_V29_RX;
+        break;
+    default:
+        set_rx_handler(s, (span_rx_handler_t *) &fsk_rx, &(s->audio.modems.v21_rx));
+        s->core.fast_rx_active = T38_NONE;
+        break;
+    }
+    /*endswitch*/
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int t38_gateway_rx(t38_gateway_state_t *s, int16_t amp[], int len)
+{
+    int i;
+
+#if defined(LOG_FAX_AUDIO)
+    if (s->audio.modems.audio_rx_log >= 0)
+        write(s->audio.modems.audio_rx_log, amp, len*sizeof(int16_t));
+#endif
+    if (s->core.samples_to_timeout > 0)
+    {
+        if ((s->core.samples_to_timeout -= len) <= 0)
+        {
+            if (s->core.tcf_mode_predictable_modem_start == 1)
+                announce_training(s);
+            /*endif*/
+        }
+        /*endif*/
+    }
+    /*endif*/
+    for (i = 0;  i < len;  i++)
+        amp[i] = dc_restore(&(s->audio.modems.dc_restore), amp[i]);
+    /*endfor*/
+    s->audio.modems.rx_handler(s->audio.modems.rx_user_data, amp, len);
+    return  0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int t38_gateway_tx(t38_gateway_state_t *s, int16_t amp[], int max_len)
+{
+    int len;
+#if defined(LOG_FAX_AUDIO)
+    int required_len;
+    
+    required_len = max_len;
+#endif
+    if ((len = s->audio.modems.tx_handler(s->audio.modems.tx_user_data, amp, max_len)) < max_len)
+    {
+        if (set_next_tx_type(s))
+        {
+            /* Give the new handler a chance to file the remaining buffer space */
+            len += s->audio.modems.tx_handler(s->audio.modems.tx_user_data, amp + len, max_len - len);
+            if (len < max_len)
+            {
+                silence_gen_set(&(s->audio.modems.silence_gen), 0);
+                set_next_tx_type(s);
+            }
+            /*endif*/
+        }
+        /*endif*/
+    }
+    /*endif*/
+    if (s->audio.modems.transmit_on_idle)
+    {
+        /* Pad to the requested length with silence */
+        memset(amp + len, 0, (max_len - len)*sizeof(int16_t));
+        len = max_len;        
+    }
+    /*endif*/
+#if defined(LOG_FAX_AUDIO)
+    if (s->audio.modems.audio_tx_log >= 0)
+    {
+        if (len < required_len)
+            memset(amp + len, 0, (required_len - len)*sizeof(int16_t));
+        /*endif*/
+        write(s->audio.modems.audio_tx_log, amp, required_len*sizeof(int16_t));
+    }
+    /*endif*/
+#endif
+    return len;
+}
+/*- End of function --------------------------------------------------------*/
+
+void t38_gateway_get_transfer_statistics(t38_gateway_state_t *s, t38_stats_t *t)
+{
+    memset(t, 0, sizeof(*t));
+    t->bit_rate = s->core.fast_bit_rate;
+    t->error_correcting_mode = s->core.ecm_mode;
+    t->pages_transferred = s->core.pages_confirmed;
+}
+/*- End of function --------------------------------------------------------*/
+
+void t38_gateway_set_ecm_capability(t38_gateway_state_t *s, int ecm_allowed)
+{
+    s->core.ecm_allowed = ecm_allowed;
+}
+/*- End of function --------------------------------------------------------*/
+
+void t38_gateway_set_transmit_on_idle(t38_gateway_state_t *s, int transmit_on_idle)
+{
+    s->audio.modems.transmit_on_idle = transmit_on_idle;
+}
+/*- End of function --------------------------------------------------------*/
+
+void t38_gateway_set_supported_modems(t38_gateway_state_t *s, int supported_modems)
+{
+    s->core.supported_modems = supported_modems;
+    if ((s->core.supported_modems & T30_SUPPORT_V17))
+        s->t38x.t38.fastest_image_data_rate = 14400;
+    else if ((s->core.supported_modems & T30_SUPPORT_V29))
+        s->t38x.t38.fastest_image_data_rate = 9600;
+    else
+        s->t38x.t38.fastest_image_data_rate = 4800;
+    /*endif*/
+}
+/*- End of function --------------------------------------------------------*/
+
+void t38_gateway_set_nsx_suppression(t38_gateway_state_t *s,
+                                     const uint8_t *from_t38,
+                                     int from_t38_len,
+                                     const uint8_t *from_modem,
+                                     int from_modem_len)
+{
+    s->t38x.suppress_nsx_len[0] = (from_t38_len < 0  ||  from_t38_len < MAX_NSX_SUPPRESSION)  ?  (from_t38_len + 3)  :  0;
+    s->t38x.suppress_nsx_len[1] = (from_modem_len < 0  ||  from_modem_len < MAX_NSX_SUPPRESSION)  ?  (from_modem_len + 3)  :  0;
+}
+/*- End of function --------------------------------------------------------*/
+
+void t38_gateway_set_tep_mode(t38_gateway_state_t *s, int use_tep)
+{
+    s->audio.modems.use_tep = use_tep;
+}
+/*- End of function --------------------------------------------------------*/
+
+void t38_gateway_set_fill_bit_removal(t38_gateway_state_t *s, int remove)
+{
+    s->core.to_t38.fill_bit_removal = remove;
+}
+/*- End of function --------------------------------------------------------*/
+
+void t38_gateway_set_real_time_frame_handler(t38_gateway_state_t *s,
+                                             t38_gateway_real_time_frame_handler_t *handler,
+                                             void *user_data)
+{
+    s->core.real_time_frame_handler = handler;
+    s->core.real_time_frame_user_data = user_data;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int t38_gateway_audio_init(t38_gateway_state_t *s)
+{
+    t38_fax_modems_init(&s->audio.modems, FALSE, s);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int t38_gateway_t38_init(t38_gateway_state_t *t,
+                                t38_tx_packet_handler_t *tx_packet_handler,
+                                void *tx_packet_user_data)
+{
+    t38_gateway_t38_state_t *s;
+
+    s = &t->t38x;
+    t38_core_init(&s->t38,
+                  process_rx_indicator,
+                  process_rx_data,
+                  process_rx_missing,
+                  (void *) t,
+                  tx_packet_handler,
+                  tx_packet_user_data);
+    s->t38.indicator_tx_count = INDICATOR_TX_COUNT;
+    s->t38.data_tx_count = DATA_TX_COUNT;
+    s->t38.data_end_tx_count = DATA_END_TX_COUNT;
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+t38_gateway_state_t *t38_gateway_init(t38_gateway_state_t *s,
+                                      t38_tx_packet_handler_t *tx_packet_handler,
+                                      void *tx_packet_user_data)
+{
+    if (tx_packet_handler == NULL)
+        return NULL;
+    /*endif*/
+    if (s == NULL)
+    {
+        if ((s = (t38_gateway_state_t *) malloc(sizeof(*s))) == NULL)
+            return NULL;
+        /*endif*/
+    }
+    /*endif*/
+    memset(s, 0, sizeof(*s));
+    span_log_init(&s->logging, SPAN_LOG_NONE, NULL);
+    span_log_set_protocol(&s->logging, "T.38G");
+
+    t38_gateway_audio_init(s);
+    t38_gateway_t38_init(s, tx_packet_handler, tx_packet_user_data);
+    
+    set_rx_active(s, TRUE);
+    t38_gateway_set_supported_modems(s, T30_SUPPORT_V27TER | T30_SUPPORT_V29);
+    t38_gateway_set_nsx_suppression(s, (const uint8_t *) "\x00\x00\x00", 3, (const uint8_t *) "\x00\x00\x00", 3);
+
+    s->core.to_t38.octets_per_data_packet = 1;
+    s->core.ecm_allowed = FALSE;
+    t38_non_ecm_buffer_init(&s->core.non_ecm_to_modem, FALSE, 0);
+    restart_rx_modem(s);
+#if defined(LOG_FAX_AUDIO)
+    {
+        char buf[100 + 1];
+        struct tm *tm;
+        time_t now;
+
+        time(&now);
+        tm = localtime(&now);
+        sprintf(buf,
+                "/tmp/t38-rx-audio-%p-%02d%02d%02d%02d%02d%02d",
+                s,
+                tm->tm_year%100,
+                tm->tm_mon + 1,
+                tm->tm_mday,
+                tm->tm_hour,
+                tm->tm_min,
+                tm->tm_sec);
+        s->audio.modems.audio_rx_log = open(buf, O_CREAT | O_TRUNC | O_WRONLY, 0666);
+        sprintf(buf,
+                "/tmp/t38-tx-audio-%p-%02d%02d%02d%02d%02d%02d",
+                s,
+                tm->tm_year%100,
+                tm->tm_mon + 1,
+                tm->tm_mday,
+                tm->tm_hour,
+                tm->tm_min,
+                tm->tm_sec);
+        s->audio.modems.audio_tx_log = open(buf, O_CREAT | O_TRUNC | O_WRONLY, 0666);
+    }
+#endif
+    return s;
+}
+/*- End of function --------------------------------------------------------*/
+
+int t38_gateway_free(t38_gateway_state_t *s)
+{
+    free(s);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/t38_non_ecm_buffer.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/t38_non_ecm_buffer.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,242 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * t38_non_ecm_buffer.c - A rate adapting buffer for T.38 non-ECM image
+ *                        and TCF data
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2005, 2006, 2007, 2008 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: t38_non_ecm_buffer.c,v 1.1 2008/08/14 14:06:05 steveu Exp $
+ */
+
+/*! \file */
+
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
+
+#include <inttypes.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <time.h>
+#include <string.h>
+#include "floating_fudge.h"
+#if defined(HAVE_TGMATH_H)
+#include <tgmath.h>
+#endif
+#if defined(HAVE_MATH_H)
+#include <math.h>
+#endif
+#include <assert.h>
+#if defined(LOG_FAX_AUDIO)
+#include <unistd.h>
+#endif
+
+#include "spandsp/telephony.h"
+#include "spandsp/logging.h"
+#include "spandsp/queue.h"
+#include "spandsp/dc_restore.h"
+#include "spandsp/bit_operations.h"
+#include "spandsp/async.h"
+
+#include "spandsp/t38_non_ecm_buffer.h"
+
+int t38_non_ecm_buffer_get_bit(void *user_data)
+{
+    t38_non_ecm_buffer_state_t *s;
+    int bit;
+
+    s = (t38_non_ecm_buffer_state_t *) user_data;
+
+    if (s->bit_no <= 0)
+    {
+        /* We need another byte */
+        if (s->out_ptr != s->latest_eol_ptr)
+        {
+            s->octet = s->data[s->out_ptr];
+            s->out_ptr = (s->out_ptr + 1) & (T38_NON_ECM_TX_BUF_LEN - 1);
+        }
+        else
+        {
+            if (s->data_finished)
+            {
+                /* The queue is empty, and we have received the end of data signal. This must
+                   really be the end to transmission. */
+                s->data_finished = FALSE;
+                /* Reset the data pointers for next time. */
+                s->out_ptr = 0;
+                s->in_ptr = 0;
+                s->latest_eol_ptr = 0;
+                return PUTBIT_END_OF_DATA;
+            }
+            /* The queue is blocked, but this does not appear to be the end of the data. Idle with
+               fill octets, which should be safe at this point. */
+            s->octet = s->flow_control_fill_octet;
+            s->flow_control_fill_octets++;
+        }
+        s->out_octets++;
+        s->bit_no = 8;
+    }
+    s->bit_no--;
+    bit = (s->octet >> 7) & 1;
+    s->octet <<= 1;
+    return bit;
+}
+/*- End of function --------------------------------------------------------*/
+
+void t38_non_ecm_buffer_push(t38_non_ecm_buffer_state_t *s)
+{
+    /* Don't flow control the data any more. Just push out the remainder of the data
+       in the buffer as fast as we can, and shut down. */
+    s->latest_eol_ptr = s->in_ptr;
+    s->data_finished = TRUE;
+}
+/*- End of function --------------------------------------------------------*/
+
+void t38_non_ecm_buffer_inject(t38_non_ecm_buffer_state_t *s, const uint8_t *buf, int len)
+{
+    int i;
+    int upper;
+    int lower;
+
+    i = 0;
+    if (s->at_initial_all_ones)
+    {
+        /* Dump initial 0xFF bytes. We will add enough of our own to makes things flow
+           smoothly. If we don't strip these off, we might end up delaying the start of
+           forwarding by a large amount, as we could end up with a large block of 0xFF
+           bytes before the real data begins. This is especially true with PC FAX
+           systems. This test is very simplistic, as a single bit error will throw it
+           off course. */
+        for (  ;  i < len;  i++)
+        {
+            if (buf[i] != 0xFF)
+            {
+                s->at_initial_all_ones = FALSE;
+                break;
+            }
+        }
+    }
+    if (s->image_data_mode)
+    {
+        /* This is image data */
+        for (  ;  i < len;  i++)
+        {
+            /* Check for EOLs, because at an EOL we can pause and pump out zeros while
+               waiting for more incoming data. */
+            if (buf[i])
+            {
+                /* There might be an EOL here. Look for at least 11 zeros, followed by a one, split
+                   between two octets. Between those two octets we can insert numerous zero octets
+                   as a means of flow control. Note that we stuff in blocks of 8 bits, and not at
+                   the minimal level. */
+                /* Or'ing with 0x800 here is simply to avoid zero words looking like they have -1
+                   trailing zeros */
+                upper = bottom_bit(s->bit_stream | 0x800);
+                lower = top_bit(buf[i]);
+                if (upper - lower > 3)
+                {
+                    s->row_bits += (8 - lower);
+                    /* If the row is too short, extend it in chunks of a whole byte. */
+                    /* TODO: extend by the precise amount we should, instead of this
+                             rough approach. */
+                    while (s->row_bits < s->min_row_bits)
+                    {
+                        s->data[s->in_ptr] = 0;
+                        s->row_bits += 8;
+                        /* TODO: We can't buffer overflow, since we wrap around. However, the tail could overwrite
+                                 itself if things fall badly behind. */
+                        s->in_ptr = (s->in_ptr + 1) & (T38_NON_ECM_TX_BUF_LEN - 1);
+                    }
+                    /* Start a new row */
+                    s->row_bits = lower - 8;
+                    s->in_rows++;
+                    s->latest_eol_ptr = s->in_ptr;
+                    s->flow_control_fill_octet = 0x00;
+                }
+            }
+            s->bit_stream = (s->bit_stream << 8) | buf[i];
+            s->data[s->in_ptr] = buf[i];
+            s->row_bits += 8;
+            /* TODO: We can't buffer overflow, since we wrap around. However, the tail could overwrite
+                     itself if things fall badly behind. */
+            s->in_ptr = (s->in_ptr + 1) & (T38_NON_ECM_TX_BUF_LEN - 1);
+            s->in_octets++;
+        }
+    }
+    else
+    {
+        /* This is TCF data */
+        for (  ;  i < len;  i++)
+        {
+            /* Check for zero bytes, as we can pause and pump out zeros while waiting
+               for more incoming data. Of course, the entire TCF data should be zero,
+               but it might not be, due to bit errors, or something weird happening. */
+            if (buf[i] == 0x00)
+            {
+                s->latest_eol_ptr = s->in_ptr;
+                s->flow_control_fill_octet = 0x00;
+            }
+            s->data[s->in_ptr] = buf[i];
+            /* TODO: We can't buffer overflow, since we wrap around. However, the tail could
+                     overwrite itself if things fall badly behind. */
+            s->in_ptr = (s->in_ptr + 1) & (T38_NON_ECM_TX_BUF_LEN - 1);
+            s->in_octets++;
+        }
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+void t38_non_ecm_buffer_report_status(t38_non_ecm_buffer_state_t *s, logging_state_t *logging)
+{
+    if (s->in_octets  ||  s->out_octets)
+    {
+        span_log(logging,
+                 SPAN_LOG_FLOW,
+                 "%d incoming non-ECM octets, %d rows.  %d outgoing non-ECM octets, %d rows\n",
+                 s->in_octets,
+                 s->in_rows,
+                 s->out_octets,
+                 s->out_rows);
+    }
+    if (s->flow_control_fill_octets)
+    {
+        span_log(logging,
+                 SPAN_LOG_FLOW,
+                 "Non-ECM flow control generated %d octets\n",
+                 s->flow_control_fill_octets);
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+t38_non_ecm_buffer_state_t *t38_non_ecm_buffer_init(t38_non_ecm_buffer_state_t *s, int mode, int min_row_bits)
+{
+    memset(s, 0, sizeof(*s));
+    s->octet = 0xFF;
+    s->flow_control_fill_octet = 0xFF;
+    s->at_initial_all_ones = TRUE;
+    s->bit_stream = 0xFFFF;
+    s->image_data_mode = mode;
+    s->min_row_bits = min_row_bits;
+    return s;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/t38_terminal.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/t38_terminal.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,1047 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * t38_terminal.c - T.38 termination, less the packet exchange part
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2005, 2006 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: t38_terminal.c,v 1.100 2008/08/13 14:55:51 steveu Exp $
+ */
+
+/*! \file */
+
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <inttypes.h>
+#include <fcntl.h>
+#include <time.h>
+#include <string.h>
+#include <malloc.h>
+#include "floating_fudge.h"
+#if defined(HAVE_TGMATH_H)
+#include <tgmath.h>
+#endif
+#if defined(HAVE_MATH_H)
+#include <math.h>
+#endif
+#include <assert.h>
+#include <tiffio.h>
+
+#include "spandsp/telephony.h"
+#include "spandsp/logging.h"
+#include "spandsp/bit_operations.h"
+#include "spandsp/queue.h"
+#include "spandsp/power_meter.h"
+#include "spandsp/complex.h"
+#include "spandsp/tone_generate.h"
+#include "spandsp/async.h"
+#include "spandsp/hdlc.h"
+#include "spandsp/fsk.h"
+#include "spandsp/v29rx.h"
+#include "spandsp/v29tx.h"
+#include "spandsp/v27ter_rx.h"
+#include "spandsp/v27ter_tx.h"
+#include "spandsp/v17rx.h"
+#include "spandsp/v17tx.h"
+#include "spandsp/t4.h"
+#include "spandsp/t30_fcf.h"
+#include "spandsp/t35.h"
+#include "spandsp/t30.h"
+#include "spandsp/t30_api.h"
+#include "spandsp/t30_logging.h"
+#include "spandsp/t38_core.h"
+#include "spandsp/t38_terminal.h"
+
+/* Settings suitable for paced transmission over a UDP transport */
+#define MS_PER_TX_CHUNK                         30
+
+#define INDICATOR_TX_COUNT                      3
+#define DATA_TX_COUNT                           1
+#define DATA_END_TX_COUNT                       3
+
+/* Settings suitable for unpaced transmission over a TCP transport */
+#define MAX_OCTETS_PER_UNPACED_CHUNK            300
+
+/* Backstop timeout if reception of packets stops in the middle of a burst */
+#define MID_RX_TIMEOUT                          15000
+
+#define T38_CHUNKING_MERGE_FCS_WITH_DATA        0x0001
+#define T38_CHUNKING_WHOLE_FRAMES               0x0002
+#define T38_CHUNKING_ALLOW_TEP_TIME             0x0004
+
+enum
+{
+    T38_TIMED_STEP_NONE = 0,
+    T38_TIMED_STEP_NON_ECM_MODEM,
+    T38_TIMED_STEP_NON_ECM_MODEM_2,
+    T38_TIMED_STEP_NON_ECM_MODEM_3,
+    T38_TIMED_STEP_NON_ECM_MODEM_4,
+    T38_TIMED_STEP_NON_ECM_MODEM_5,
+    T38_TIMED_STEP_HDLC_MODEM,
+    T38_TIMED_STEP_HDLC_MODEM_2,
+    T38_TIMED_STEP_HDLC_MODEM_3,
+    T38_TIMED_STEP_HDLC_MODEM_4,
+    T38_TIMED_STEP_CED,
+    T38_TIMED_STEP_CED_2,
+    T38_TIMED_STEP_CNG,
+    T38_TIMED_STEP_CNG_2,
+    T38_TIMED_STEP_PAUSE
+};
+
+static int extra_bits_in_stuffed_frame(const uint8_t buf[], int len)
+{
+    int bitstream;
+    int ones;
+    int stuffed;
+    int i;
+    int j;
+    
+    bitstream = 0;
+    ones = 0;
+    stuffed = 0;
+    /* We should really append the CRC, and included the stuffed bits for that, to get
+       the exact number of bits in the frame. */
+    //len = crc_itu16_append(buf, len);
+    for (i = 0;  i < len;  i++)
+    {
+        bitstream = buf[i];
+        for (j = 0;  j < 8;  j++)
+        {
+            if ((bitstream & 1))
+            {
+                if (++ones >= 5)
+                {
+                    ones = 0;
+                    stuffed++;
+                }
+            }
+            else
+            {
+                ones = 0;
+            }
+            bitstream >>= 1;
+        }
+    }
+    /* The total length of the frame is:
+          the number of bits in the body
+        + the number of additional bits in the body due to stuffing
+        + the number of bits in the CRC
+        + the number of additional bits in the CRC due to stuffing
+        + 16 bits for the two terminating flag octets.
+       Lets just allow 3 bits for the CRC, which is the worst case. It
+       avoids calculating the real CRC, and the worst it can do is cause
+       a flag octet's worth of additional output.
+    */
+    return stuffed + 16 + 3 + 16;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int process_rx_missing(t38_core_state_t *t, void *user_data, int rx_seq_no, int expected_seq_no)
+{
+    t38_terminal_state_t *s;
+    
+    s = (t38_terminal_state_t *) user_data;
+    s->t38_fe.rx_data_missing = TRUE;
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int process_rx_indicator(t38_core_state_t *t, void *user_data, int indicator)
+{
+    t38_terminal_state_t *s;
+    t38_terminal_front_end_state_t *fe;
+    
+    s = (t38_terminal_state_t *) user_data;
+    fe = &s->t38_fe;
+
+    if (t->current_rx_indicator == indicator)
+    {
+        /* This is probably due to the far end repeating itself, or slipping
+           preamble messages in between HDLC frames. T.38/V.1.3 tells us to
+           ignore it. Its harmless. */
+        return 0;
+    }
+    /* In termination mode we don't care very much about indicators telling us training
+       is starting. We only care about V.21 preamble starting, for timeout control, and
+       the actual data. */
+    switch (indicator)
+    {
+    case T38_IND_NO_SIGNAL:
+        if (t->current_rx_indicator == T38_IND_V21_PREAMBLE
+            &&
+            (fe->current_rx_type == T30_MODEM_V21  ||  fe->current_rx_type == T30_MODEM_CNG))
+        {
+            t30_hdlc_accept(&s->t30, NULL, PUTBIT_CARRIER_DOWN, TRUE);
+        }
+        fe->timeout_rx_samples = 0;
+        t30_front_end_status(&s->t30, T30_FRONT_END_SIGNAL_ABSENT);
+        break;
+    case T38_IND_CNG:
+    case T38_IND_CED:
+        /* We are completely indifferent to the startup tones. They serve no purpose for us.
+           We can't even assume that the existance of a tone means the far end is achieving
+           proper communication. Some T.38 gateways will just send out a CED or CNG indicator
+           without having seen anything from the far end FAX terminal. */
+        break;
+    case T38_IND_V21_PREAMBLE:
+        /* Some T.38 implementations insert these preamble indicators between HDLC frames, so
+           we need to be tolerant of that. */
+        fe->timeout_rx_samples = fe->samples + ms_to_samples(MID_RX_TIMEOUT);
+        t30_front_end_status(&s->t30, T30_FRONT_END_SIGNAL_PRESENT);
+        break;
+    case T38_IND_V27TER_2400_TRAINING:
+    case T38_IND_V27TER_4800_TRAINING:
+    case T38_IND_V29_7200_TRAINING:
+    case T38_IND_V29_9600_TRAINING:
+    case T38_IND_V17_7200_SHORT_TRAINING:
+    case T38_IND_V17_7200_LONG_TRAINING:
+    case T38_IND_V17_9600_SHORT_TRAINING:
+    case T38_IND_V17_9600_LONG_TRAINING:
+    case T38_IND_V17_12000_SHORT_TRAINING:
+    case T38_IND_V17_12000_LONG_TRAINING:
+    case T38_IND_V17_14400_SHORT_TRAINING:
+    case T38_IND_V17_14400_LONG_TRAINING:
+    case T38_IND_V33_12000_TRAINING:
+    case T38_IND_V33_14400_TRAINING:
+        /* We really don't care what kind of modem is delivering the following image data.
+           We only care that some kind of fast modem signal is coming next. */
+        fe->timeout_rx_samples = fe->samples + ms_to_samples(MID_RX_TIMEOUT);
+        t30_front_end_status(&s->t30, T30_FRONT_END_SIGNAL_PRESENT);
+        break;
+    case T38_IND_V8_ANSAM:
+    case T38_IND_V8_SIGNAL:
+    case T38_IND_V34_CNTL_CHANNEL_1200:
+    case T38_IND_V34_PRI_CHANNEL:
+    case T38_IND_V34_CC_RETRAIN:
+        /* V.34 support is a work in progress. */
+        t30_front_end_status(&s->t30, T30_FRONT_END_SIGNAL_PRESENT);
+        break;
+    default:
+        t30_front_end_status(&s->t30, T30_FRONT_END_SIGNAL_ABSENT);
+        break;
+    }
+    fe->hdlc_rx.len = 0;
+    fe->rx_data_missing = FALSE;
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int process_rx_data(t38_core_state_t *t, void *user_data, int data_type, int field_type, const uint8_t *buf, int len)
+{
+    t38_terminal_state_t *s;
+    t38_terminal_front_end_state_t *fe;
+#if defined(_MSC_VER)
+    uint8_t *buf2 = alloca(len);
+#else
+    uint8_t buf2[len];
+#endif
+
+    s = (t38_terminal_state_t *) user_data;
+    fe = &s->t38_fe;
+#if 0
+    /* In termination mode we don't care very much what the data type is. */
+    switch (data_type)
+    {
+    case T38_DATA_V21:
+    case T38_DATA_V27TER_2400:
+    case T38_DATA_V27TER_4800:
+    case T38_DATA_V29_7200:
+    case T38_DATA_V29_9600:
+    case T38_DATA_V17_7200:
+    case T38_DATA_V17_9600:
+    case T38_DATA_V17_12000:
+    case T38_DATA_V17_14400:
+    case T38_DATA_V8:
+    case T38_DATA_V34_PRI_RATE:
+    case T38_DATA_V34_CC_1200:
+    case T38_DATA_V34_PRI_CH:
+    case T38_DATA_V33_12000:
+    case T38_DATA_V33_14400:
+    default:
+        break;
+    }
+#endif
+
+    switch (field_type)
+    {
+    case T38_FIELD_HDLC_DATA:
+        if (fe->timeout_rx_samples == 0)
+        {
+            /* HDLC can just start without any signal indicator on some platforms, even when
+               there is zero packet lost. Nasty, but true. Its a good idea to be tolerant of
+               loss, though, so accepting a sudden start of HDLC data is the right thing to do. */
+            fe->timeout_rx_samples = fe->samples + ms_to_samples(MID_RX_TIMEOUT);
+            t30_front_end_status(&s->t30, T30_FRONT_END_SIGNAL_PRESENT);
+            /* All real HDLC messages in the FAX world start with 0xFF. If this one is not starting
+               with 0xFF it would appear some octets must have been missed before this one. */
+            if (buf[0] != 0xFF)
+                fe->rx_data_missing = TRUE;
+        }
+        if (fe->hdlc_rx.len + len <= T38_MAX_HDLC_LEN)
+        {
+            bit_reverse(fe->hdlc_rx.buf + fe->hdlc_rx.len, buf, len);
+            fe->hdlc_rx.len += len;
+        }
+        fe->timeout_rx_samples = fe->samples + ms_to_samples(MID_RX_TIMEOUT);
+        break;
+    case T38_FIELD_HDLC_FCS_OK:
+        if (len > 0)
+        {
+            span_log(&s->logging, SPAN_LOG_WARNING, "There is data in a T38_FIELD_HDLC_FCS_OK!\n");
+            /* The sender has incorrectly included data in this message. It is unclear what we should do
+               with it, to maximise tolerance of buggy implementations. */
+        }
+        /* Some T.38 implementations send multiple T38_FIELD_HDLC_FCS_OK messages, in IFP packets with
+           incrementing sequence numbers, which are actually repeats. They get through to this point because
+           of the incrementing sequence numbers. We need to filter them here in a context sensitive manner. */
+        if (t->current_rx_data_type != data_type  ||  t->current_rx_field_type != field_type)
+        {
+            span_log(&s->logging, SPAN_LOG_FLOW, "Type %s - CRC OK (%s)\n", (fe->hdlc_rx.len >= 3)  ?  t30_frametype(fe->hdlc_rx.buf[2])  :  "???", (fe->rx_data_missing)  ?  "missing octets"  :  "clean");
+            t30_hdlc_accept(&s->t30, fe->hdlc_rx.buf, fe->hdlc_rx.len, !fe->rx_data_missing);
+        }
+        fe->hdlc_rx.len = 0;
+        fe->rx_data_missing = FALSE;
+        fe->timeout_rx_samples = fe->samples + ms_to_samples(MID_RX_TIMEOUT);
+        break;
+    case T38_FIELD_HDLC_FCS_BAD:
+        if (len > 0)
+        {
+            span_log(&s->logging, SPAN_LOG_WARNING, "There is data in a T38_FIELD_HDLC_FCS_BAD!\n");
+            /* The sender has incorrectly included data in this message. We can safely ignore it, as the
+               bad FCS means we will throw away the whole message, anyway. */
+        }
+        /* Some T.38 implementations send multiple T38_FIELD_HDLC_FCS_BAD messages, in IFP packets with
+           incrementing sequence numbers, which are actually repeats. They get through to this point because
+           of the incrementing sequence numbers. We need to filter them here in a context sensitive manner. */
+        if (t->current_rx_data_type != data_type  ||  t->current_rx_field_type != field_type)
+        {
+            span_log(&s->logging, SPAN_LOG_FLOW, "Type %s - CRC bad (%s)\n", (fe->hdlc_rx.len >= 3)  ?  t30_frametype(fe->hdlc_rx.buf[2])  :  "???", (fe->rx_data_missing)  ?  "missing octets"  :  "clean");
+            t30_hdlc_accept(&s->t30, fe->hdlc_rx.buf, fe->hdlc_rx.len, FALSE);
+        }
+        fe->hdlc_rx.len = 0;
+        fe->rx_data_missing = FALSE;
+        fe->timeout_rx_samples = fe->samples + ms_to_samples(MID_RX_TIMEOUT);
+        break;
+    case T38_FIELD_HDLC_FCS_OK_SIG_END:
+        if (len > 0)
+        {
+            span_log(&s->logging, SPAN_LOG_WARNING, "There is data in a T38_FIELD_HDLC_FCS_OK_SIG_END!\n");
+            /* The sender has incorrectly included data in this message. It is unclear what we should do
+               with it, to maximise tolerance of buggy implementations. */
+        }
+        /* Some T.38 implementations send multiple T38_FIELD_HDLC_FCS_OK_SIG_END messages, in IFP packets with
+           incrementing sequence numbers, which are actually repeats. They get through to this point because
+           of the incrementing sequence numbers. We need to filter them here in a context sensitive manner. */
+        if (t->current_rx_data_type != data_type  ||  t->current_rx_field_type != field_type)
+        {
+            span_log(&s->logging, SPAN_LOG_FLOW, "Type %s - CRC OK, sig end (%s)\n", (fe->hdlc_rx.len >= 3)  ?  t30_frametype(fe->hdlc_rx.buf[2])  :  "???", (fe->rx_data_missing)  ?  "missing octets"  :  "clean");
+            t30_hdlc_accept(&s->t30, fe->hdlc_rx.buf, fe->hdlc_rx.len, !fe->rx_data_missing);
+            t30_hdlc_accept(&s->t30, NULL, PUTBIT_CARRIER_DOWN, TRUE);
+        }
+        fe->hdlc_rx.len = 0;
+        fe->rx_data_missing = FALSE;
+        fe->timeout_rx_samples = 0;
+        break;
+    case T38_FIELD_HDLC_FCS_BAD_SIG_END:
+        if (len > 0)
+        {
+            span_log(&s->logging, SPAN_LOG_WARNING, "There is data in a T38_FIELD_HDLC_FCS_BAD_SIG_END!\n");
+            /* The sender has incorrectly included data in this message. We can safely ignore it, as the
+               bad FCS means we will throw away the whole message, anyway. */
+        }
+        /* Some T.38 implementations send multiple T38_FIELD_HDLC_FCS_BAD_SIG_END messages, in IFP packets with
+           incrementing sequence numbers, which are actually repeats. They get through to this point because
+           of the incrementing sequence numbers. We need to filter them here in a context sensitive manner. */
+        if (t->current_rx_data_type != data_type  ||  t->current_rx_field_type != field_type)
+        {
+            span_log(&s->logging, SPAN_LOG_FLOW, "Type %s - CRC bad, sig end (%s)\n", (fe->hdlc_rx.len >= 3)  ?  t30_frametype(fe->hdlc_rx.buf[2])  :  "???", (fe->rx_data_missing)  ?  "missing octets"  :  "clean");
+            t30_hdlc_accept(&s->t30, fe->hdlc_rx.buf, fe->hdlc_rx.len, FALSE);
+            t30_hdlc_accept(&s->t30, NULL, PUTBIT_CARRIER_DOWN, TRUE);
+        }
+        fe->hdlc_rx.len = 0;
+        fe->rx_data_missing = FALSE;
+        fe->timeout_rx_samples = 0;
+        break;
+    case T38_FIELD_HDLC_SIG_END:
+        if (len > 0)
+        {
+            span_log(&s->logging, SPAN_LOG_WARNING, "There is data in a T38_FIELD_HDLC_SIG_END!\n");
+            /* The sender has incorrectly included data in this message, but there seems nothing meaningful
+               it could be. There could not be an FCS good/bad report beyond this. */
+        }
+        /* Some T.38 implementations send multiple T38_FIELD_HDLC_SIG_END messages, in IFP packets with
+           incrementing sequence numbers, which are actually repeats. They get through to this point because
+           of the incrementing sequence numbers. We need to filter them here in a context sensitive manner. */
+        if (t->current_rx_data_type != data_type  ||  t->current_rx_field_type != field_type)
+        {
+            /* WORKAROUND: At least some Mediatrix boxes have a bug, where they can send this message at the
+                           end of non-ECM data. We need to tolerate this. We use the generic receive complete
+                           indication, rather than the specific HDLC carrier down. */
+            /* This message is expected under 2 circumstances. One is as an alternative to T38_FIELD_HDLC_FCS_OK_SIG_END - 
+               i.e. they send T38_FIELD_HDLC_FCS_OK, and then T38_FIELD_HDLC_SIG_END when the carrier actually drops.
+               The other is because the HDLC signal drops unexpectedly - i.e. not just after a final frame. */
+            fe->hdlc_rx.len = 0;
+            fe->rx_data_missing = FALSE;
+            fe->timeout_rx_samples = 0;
+            t30_front_end_status(&s->t30, T30_FRONT_END_RECEIVE_COMPLETE);
+        }
+        break;
+    case T38_FIELD_T4_NON_ECM_DATA:
+        if (!fe->rx_signal_present)
+        {
+            t30_non_ecm_put_bit(&s->t30, PUTBIT_TRAINING_SUCCEEDED);
+            fe->rx_signal_present = TRUE;
+        }
+        bit_reverse(buf2, buf, len);
+        t30_non_ecm_put_chunk(&s->t30, buf2, len);
+        fe->timeout_rx_samples = fe->samples + ms_to_samples(MID_RX_TIMEOUT);
+        break;
+    case T38_FIELD_T4_NON_ECM_SIG_END:
+        /* Some T.38 implementations send multiple T38_FIELD_T4_NON_ECM_SIG_END messages, in IFP packets with
+           incrementing sequence numbers, which are actually repeats. They get through to this point because
+           of the incrementing sequence numbers. We need to filter them here in a context sensitive manner. */
+        if (t->current_rx_data_type != data_type  ||  t->current_rx_field_type != field_type)
+        {
+            if (len > 0)
+            {
+                if (!fe->rx_signal_present)
+                {
+                    t30_non_ecm_put_bit(&s->t30, PUTBIT_TRAINING_SUCCEEDED);
+                    fe->rx_signal_present = TRUE;
+                }
+                bit_reverse(buf2, buf, len);
+                t30_non_ecm_put_chunk(&s->t30, buf2, len);
+            }
+            /* WORKAROUND: At least some Mediatrix boxes have a bug, where they can send HDLC signal end where
+                           they should send non-ECM signal end. It is possible they also do the opposite.
+                           We need to tolerate this, so we use the generic receive complete
+                           indication, rather than the specific non-ECM carrier down. */
+            t30_front_end_status(&s->t30, T30_FRONT_END_RECEIVE_COMPLETE);
+        }
+        fe->rx_signal_present = FALSE;
+        fe->timeout_rx_samples = 0;
+        break;
+    case T38_FIELD_CM_MESSAGE:
+        if (len >= 1)
+            span_log(&s->logging, SPAN_LOG_FLOW, "CM profile %d - %s\n", buf[0] - '0', t38_cm_profile_to_str(buf[0]));
+        else
+            span_log(&s->logging, SPAN_LOG_FLOW, "Bad length for CM message - %d\n", len);
+        break;
+    case T38_FIELD_JM_MESSAGE:
+        if (len >= 2)
+            span_log(&s->logging, SPAN_LOG_FLOW, "JM - %s\n", t38_jm_to_str(buf, len));
+        else
+            span_log(&s->logging, SPAN_LOG_FLOW, "Bad length for JM message - %d\n", len);
+        break;
+    case T38_FIELD_CI_MESSAGE:
+        if (len >= 1)
+            span_log(&s->logging, SPAN_LOG_FLOW, "CI 0x%X\n", buf[0]);
+        else
+            span_log(&s->logging, SPAN_LOG_FLOW, "Bad length for CI message - %d\n", len);
+        break;
+    case T38_FIELD_V34RATE:
+        if (len >= 3)
+        {
+            fe->t38.v34_rate = t38_v34rate_to_bps(buf, len);
+            span_log(&s->logging, SPAN_LOG_FLOW, "V.34 rate %d bps\n", fe->t38.v34_rate);
+        }
+        else
+        {
+            span_log(&s->logging, SPAN_LOG_FLOW, "Bad length for V34rate message - %d\n", len);
+        }
+        break;
+    default:
+        break;
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void send_hdlc(void *user_data, const uint8_t *msg, int len)
+{
+    t38_terminal_state_t *s;
+
+    s = (t38_terminal_state_t *) user_data;
+    if (len <= 0)
+    {
+        s->t38_fe.tx.len = -1;
+    }
+    else
+    {
+        s->t38_fe.tx.extra_bits = extra_bits_in_stuffed_frame(msg, len);
+        bit_reverse(s->t38_fe.tx.buf, msg, len);
+        s->t38_fe.tx.len = len;
+        s->t38_fe.tx.ptr = 0;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ int bits_to_samples(t38_terminal_state_t *s, int bits)
+{
+    /* This does not handle fractions properly, so they may accumulate. They
+       shouldn't be able to accumulate far enough to be troublesome. */
+    /* TODO: Is the above statement accurate when sending a long string of
+             octets, one per IFP packet, at V.21 rate? */
+    if (s->t38_fe.ms_per_tx_chunk == 0)
+        return 0;
+    return bits*8000/s->t38_fe.tx_bit_rate;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void set_octets_per_data_packet(t38_terminal_state_t *s, int bit_rate)
+{
+    s->t38_fe.tx_bit_rate = bit_rate;
+    if (s->t38_fe.ms_per_tx_chunk == 0)
+    {
+        s->t38_fe.octets_per_data_packet = MAX_OCTETS_PER_UNPACED_CHUNK;
+    }
+    else
+    {
+        s->t38_fe.octets_per_data_packet = s->t38_fe.ms_per_tx_chunk*bit_rate/(8*1000);
+        /* Make sure we have a positive number (i.e. we didn't truncate to zero). */
+        if (s->t38_fe.octets_per_data_packet < 1)
+            s->t38_fe.octets_per_data_packet = 1;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+int t38_terminal_send_timeout(t38_terminal_state_t *s, int samples)
+{
+    int len;
+    int i;
+    int previous;
+    uint8_t buf[MAX_OCTETS_PER_UNPACED_CHUNK + 50];
+    t38_terminal_front_end_state_t *fe;
+    t38_data_field_t data_fields[2];
+    /* The times for training, the optional TEP, and the HDLC preamble, for all the modem options, in ms.
+       Note that the preamble for V.21 is 1s+-15%, and for the other modems is 200ms+100ms. */
+    static const struct
+    {
+        int tep;
+        int training;
+        int flags;
+    } startup_time[] =
+    {
+        {   0,    0,    0},     /* T38_IND_NO_SIGNAL */
+        {   0,    0,    0},     /* T38_IND_CNG */
+        {   0,    0,    0},     /* T38_IND_CED */
+        {   0,    0, 1000},     /* T38_IND_V21_PREAMBLE */ /* TODO: 850ms should be OK for this, but it causes trouble with some ATAs. Why? */
+        { 215,  943,  200},     /* T38_IND_V27TER_2400_TRAINING */
+        { 215,  708,  200},     /* T38_IND_V27TER_4800_TRAINING */
+        { 215,  234,  200},     /* T38_IND_V29_7200_TRAINING */
+        { 215,  234,  200},     /* T38_IND_V29_9600_TRAINING */
+        { 215,  142,  200},     /* T38_IND_V17_7200_SHORT_TRAINING */
+        { 215, 1393,  200},     /* T38_IND_V17_7200_LONG_TRAINING */
+        { 215,  142,  200},     /* T38_IND_V17_9600_SHORT_TRAINING */
+        { 215, 1393,  200},     /* T38_IND_V17_9600_LONG_TRAINING */
+        { 215,  142,  200},     /* T38_IND_V17_12000_SHORT_TRAINING */
+        { 215, 1393,  200},     /* T38_IND_V17_12000_LONG_TRAINING */
+        { 215,  142,  200},     /* T38_IND_V17_14400_SHORT_TRAINING */
+        { 215, 1393,  200},     /* T38_IND_V17_14400_LONG_TRAINING */
+        { 215,    0,    0},     /* T38_IND_V8_ANSAM */
+        { 215,    0,    0},     /* T38_IND_V8_SIGNAL */
+        { 215,    0,    0},     /* T38_IND_V34_CNTL_CHANNEL_1200 */
+        { 215,    0,    0},     /* T38_IND_V34_PRI_CHANNEL */
+        { 215,    0,    0},     /* T38_IND_V34_CC_RETRAIN */
+        { 215,    0,    0},     /* T38_IND_V33_12000_TRAINING */
+        { 215,    0,    0}      /* T38_IND_V33_14400_TRAINING */
+    };
+
+    fe = &s->t38_fe;
+    if (fe->current_rx_type == T30_MODEM_DONE  ||  fe->current_tx_type == T30_MODEM_DONE)
+        return TRUE;
+
+    fe->samples += samples;
+    t30_timer_update(&s->t30, samples);
+    if (fe->timeout_rx_samples  &&  fe->samples > fe->timeout_rx_samples)
+    {
+        span_log(&s->logging, SPAN_LOG_FLOW, "Timeout mid-receive\n");
+        fe->timeout_rx_samples = 0;
+        t30_front_end_status(&s->t30, T30_FRONT_END_RECEIVE_COMPLETE);
+    }
+    if (fe->timed_step == T38_TIMED_STEP_NONE)
+        return FALSE;
+    /* Wait until the right time comes along, unless we are working in "no delays" mode, while talking to an
+       IAF terminal. */
+    if (fe->ms_per_tx_chunk  &&  fe->samples < fe->next_tx_samples)
+        return FALSE;
+    /* Its time to send something */
+    switch (fe->timed_step)
+    {
+    case T38_TIMED_STEP_NON_ECM_MODEM:
+        /* Create a 75ms silence */
+        if (fe->t38.current_tx_indicator != T38_IND_NO_SIGNAL)
+            t38_core_send_indicator(&fe->t38, T38_IND_NO_SIGNAL, fe->t38.indicator_tx_count);
+        fe->timed_step = T38_TIMED_STEP_NON_ECM_MODEM_2;
+        fe->next_tx_samples += ms_to_samples(75);
+        break;
+    case T38_TIMED_STEP_NON_ECM_MODEM_2:
+        /* Switch on a fast modem, and give the training time to complete */
+        t38_core_send_indicator(&fe->t38, fe->next_tx_indicator, fe->t38.indicator_tx_count);
+        fe->timed_step = T38_TIMED_STEP_NON_ECM_MODEM_3;
+        fe->next_tx_samples += ms_to_samples(startup_time[fe->next_tx_indicator].training);
+        if ((fe->chunking_modes & T38_CHUNKING_ALLOW_TEP_TIME))
+            fe->next_tx_samples += ms_to_samples(startup_time[fe->next_tx_indicator].tep);
+        break;
+    case T38_TIMED_STEP_NON_ECM_MODEM_3:
+        /* Send a chunk of non-ECM image data */
+        /* T.38 says it is OK to send the last of the non-ECM data in the signal end message.
+           However, I think the early versions of T.38 said the signal end message should not
+           contain data. Hopefully, following the current spec will not cause compatibility
+           issues. */
+        len = t30_non_ecm_get_chunk(&s->t30, buf, fe->octets_per_data_packet);
+        bit_reverse(buf, buf, len);
+        if (len < fe->octets_per_data_packet)
+        {
+            /* That's the end of the image data. Do a little padding now */
+            memset(buf + len, 0, fe->octets_per_data_packet - len);
+            fe->non_ecm_trailer_bytes = 3*fe->octets_per_data_packet + len;
+            len = fe->octets_per_data_packet;
+            fe->timed_step = T38_TIMED_STEP_NON_ECM_MODEM_4;
+        }
+        t38_core_send_data(&fe->t38, fe->current_tx_data_type, T38_FIELD_T4_NON_ECM_DATA, buf, len, fe->t38.data_tx_count);
+        fe->next_tx_samples += bits_to_samples(s, 8*len);
+        break;
+    case T38_TIMED_STEP_NON_ECM_MODEM_4:
+        /* This pads the end of the data with some zeros. If we just stop abruptly
+           at the end of the EOLs, some ATAs fail to clean up properly before
+           shutting down their transmit modem, and the last few rows of the image
+           get corrupted. Simply delaying the no-signal message does not help for
+           all implentations. It often appears to be ignored. */
+        len = fe->octets_per_data_packet;
+        fe->non_ecm_trailer_bytes -= len;
+        if (fe->non_ecm_trailer_bytes <= 0)
+        {
+            len += fe->non_ecm_trailer_bytes;
+            memset(buf, 0, len);
+            t38_core_send_data(&fe->t38, fe->current_tx_data_type, T38_FIELD_T4_NON_ECM_SIG_END, buf, len, fe->t38.data_end_tx_count);
+            fe->timed_step = T38_TIMED_STEP_NON_ECM_MODEM_5;
+            /* Allow a bit more time than the data will take to play out, to ensure the far ATA does not
+               cut things short. */
+            fe->next_tx_samples += (bits_to_samples(s, 8*len) + ms_to_samples(60));
+            break;
+        }
+        memset(buf, 0, len);
+        t38_core_send_data(&fe->t38, fe->current_tx_data_type, T38_FIELD_T4_NON_ECM_DATA, buf, len, fe->t38.data_tx_count);
+        fe->next_tx_samples += bits_to_samples(s, 8*len);
+        break;
+    case T38_TIMED_STEP_NON_ECM_MODEM_5:
+        /* This should not be needed, since the message above indicates the end of the signal, but it
+           seems like it can improve compatibility with quirky implementations. */
+        t38_core_send_indicator(&fe->t38, T38_IND_NO_SIGNAL, fe->t38.indicator_tx_count);
+        fe->timed_step = T38_TIMED_STEP_NONE;
+        t30_front_end_status(&s->t30, T30_FRONT_END_SEND_STEP_COMPLETE);
+        break;
+    case T38_TIMED_STEP_HDLC_MODEM:
+        /* Send HDLC preambling */
+        t38_core_send_indicator(&fe->t38, fe->next_tx_indicator, fe->t38.indicator_tx_count);
+        fe->next_tx_samples += ms_to_samples(startup_time[fe->next_tx_indicator].training + startup_time[fe->next_tx_indicator].flags);
+        if (fe->chunking_modes & T38_CHUNKING_ALLOW_TEP_TIME)
+            fe->next_tx_samples += ms_to_samples(startup_time[fe->next_tx_indicator].tep);
+        fe->timed_step = T38_TIMED_STEP_HDLC_MODEM_2;
+        break;
+    case T38_TIMED_STEP_HDLC_MODEM_2:
+        /* Send a chunk of HDLC data */
+        i = fe->tx.len - fe->tx.ptr;
+        if (fe->octets_per_data_packet >= i)
+        {
+            /* The last part of an HDLC frame */
+            if (fe->chunking_modes & T38_CHUNKING_MERGE_FCS_WITH_DATA)
+            {
+                /* Copy the data, as we might be about to refill the buffer it is in */
+                memcpy(buf, &fe->tx.buf[fe->tx.ptr], i);
+                data_fields[0].field_type = T38_FIELD_HDLC_DATA;
+                data_fields[0].field = buf;
+                data_fields[0].field_len = i;
+
+                /* Now see about the next HDLC frame. This will tell us whether to send FCS_OK or FCS_OK_SIG_END */
+                previous = fe->current_tx_data_type;
+                fe->tx.ptr = 0;
+                fe->tx.len = 0;
+                t30_front_end_status(&s->t30, T30_FRONT_END_SEND_STEP_COMPLETE);
+                /* The above step should have got the next HDLC step ready - either another frame, or an instruction to stop transmission. */
+                if (fe->tx.len < 0)
+                {
+                    data_fields[1].field_type = T38_FIELD_HDLC_FCS_OK_SIG_END;
+                    fe->timed_step = T38_TIMED_STEP_HDLC_MODEM_4;
+                    /* We add a bit of extra time here, as with some implementations
+                       the carrier falling too abruptly causes data loss. */
+                    fe->next_tx_samples += (bits_to_samples(s, i*8 + fe->tx.extra_bits) + ms_to_samples(100));
+                }
+                else
+                {
+                    data_fields[1].field_type = T38_FIELD_HDLC_FCS_OK;
+                    fe->timed_step = T38_TIMED_STEP_HDLC_MODEM_2;
+                    fe->next_tx_samples += bits_to_samples(s, i*8 + fe->tx.extra_bits);
+                }
+                data_fields[1].field = NULL;
+                data_fields[1].field_len = 0;
+                t38_core_send_data_multi_field(&fe->t38, fe->current_tx_data_type, data_fields, 2, fe->t38.data_tx_count);
+            }
+            else
+            {
+                t38_core_send_data(&fe->t38, fe->current_tx_data_type, T38_FIELD_HDLC_DATA, &fe->tx.buf[fe->tx.ptr], i, fe->t38.data_tx_count);
+                fe->timed_step = T38_TIMED_STEP_HDLC_MODEM_3;
+                fe->next_tx_samples += bits_to_samples(s, i*8);
+            }
+            break;
+        }
+        t38_core_send_data(&fe->t38, fe->current_tx_data_type, T38_FIELD_HDLC_DATA, &fe->tx.buf[fe->tx.ptr], fe->octets_per_data_packet, fe->t38.data_tx_count);
+        fe->tx.ptr += fe->octets_per_data_packet;
+        fe->next_tx_samples += bits_to_samples(s, fe->octets_per_data_packet*8);
+        break;
+    case T38_TIMED_STEP_HDLC_MODEM_3:
+        /* End of HDLC frame */
+        previous = fe->current_tx_data_type;
+        fe->tx.ptr = 0;
+        fe->tx.len = 0;
+        t30_front_end_status(&s->t30, T30_FRONT_END_SEND_STEP_COMPLETE);
+        /* The above step should have got the next HDLC step ready - either another frame, or an instruction to stop transmission. */
+        if (fe->tx.len < 0)
+        {
+            /* End of transmission */
+            t38_core_send_data(&fe->t38, previous, T38_FIELD_HDLC_FCS_OK_SIG_END, NULL, 0, fe->t38.data_end_tx_count);
+            fe->timed_step = T38_TIMED_STEP_HDLC_MODEM_4;
+            fe->next_tx_samples += (bits_to_samples(s, fe->tx.extra_bits) + ms_to_samples(100));
+            break;
+        }
+        if (fe->tx.len == 0)
+        {
+            /* Now, how did we get here? We have finished a frame, but have no new frame to
+               send, and no end of transmission condition. */
+            span_log(&s->logging, SPAN_LOG_FLOW, "No new frame or end transmission condition.\n");
+        }
+        /* Finish the current frame off, and prepare for the next one. */
+        t38_core_send_data(&fe->t38, previous, T38_FIELD_HDLC_FCS_OK, NULL, 0, fe->t38.data_tx_count);
+        fe->timed_step = T38_TIMED_STEP_HDLC_MODEM_2;
+        /* We should now wait 3 octet times - the duration of the FCS + a flag octet - and send the
+           next chunk. To give a little more latitude, and allow for stuffing in the FCS, add
+           time for an extra flag octet. */
+        fe->next_tx_samples += bits_to_samples(s, fe->tx.extra_bits);
+        break;
+    case T38_TIMED_STEP_HDLC_MODEM_4:
+        /* Note that some boxes do not like us sending a T38_FIELD_HDLC_SIG_END at this point.
+           A T38_IND_NO_SIGNAL should always be OK. */
+        t38_core_send_indicator(&fe->t38, T38_IND_NO_SIGNAL, fe->t38.indicator_tx_count);
+        fe->timed_step = T38_TIMED_STEP_NONE;
+        t30_front_end_status(&s->t30, T30_FRONT_END_SEND_STEP_COMPLETE);
+        break;
+    case T38_TIMED_STEP_CED:
+        /* It seems common practice to start with a no signal indicator, though
+           this is not a specified requirement. Since we should be sending 200ms
+           of silence, starting the delay with a no signal indication makes sense.
+           We do need a 200ms delay, as that is a specification requirement. */
+        fe->timed_step = T38_TIMED_STEP_CED_2;
+        fe->next_tx_samples = fe->samples + ms_to_samples(200);
+        t38_core_send_indicator(&fe->t38, T38_IND_NO_SIGNAL, fe->t38.indicator_tx_count);
+        fe->current_tx_data_type = T38_DATA_NONE;
+        break;
+    case T38_TIMED_STEP_CED_2:
+        /* Initial 200ms delay over. Send the CED indicator */
+        fe->next_tx_samples = fe->samples + ms_to_samples(3000);
+        fe->timed_step = T38_TIMED_STEP_PAUSE;
+        t38_core_send_indicator(&fe->t38, T38_IND_CED, fe->t38.indicator_tx_count);
+        fe->current_tx_data_type = T38_DATA_NONE;
+        break;
+    case T38_TIMED_STEP_CNG:
+        /* It seems common practice to start with a no signal indicator, though
+           this is not a specified requirement. Since we should be sending 200ms
+           of silence, starting the delay with a no signal indication makes sense.
+           We do need a 200ms delay, as that is a specification requirement. */
+        fe->timed_step = T38_TIMED_STEP_CNG_2;
+        fe->next_tx_samples = fe->samples + ms_to_samples(200);
+        t38_core_send_indicator(&fe->t38, T38_IND_NO_SIGNAL, fe->t38.indicator_tx_count);
+        fe->current_tx_data_type = T38_DATA_NONE;
+        break;
+    case T38_TIMED_STEP_CNG_2:
+        /* Initial short delay over. Send the CNG indicator */
+        fe->timed_step = T38_TIMED_STEP_NONE;
+        t38_core_send_indicator(&fe->t38, T38_IND_CNG, fe->t38.indicator_tx_count);
+        fe->current_tx_data_type = T38_DATA_NONE;
+        break;
+    case T38_TIMED_STEP_PAUSE:
+        /* End of timed pause */
+        fe->timed_step = T38_TIMED_STEP_NONE;
+        t30_front_end_status(&s->t30, T30_FRONT_END_SEND_STEP_COMPLETE);
+        break;
+    }
+    return FALSE;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void set_rx_type(void *user_data, int type, int bit_rate, int short_train, int use_hdlc)
+{
+    t38_terminal_state_t *s;
+
+    s = (t38_terminal_state_t *) user_data;
+    span_log(&s->logging, SPAN_LOG_FLOW, "Set rx type %d\n", type);
+    s->t38_fe.current_rx_type = type;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void set_tx_type(void *user_data, int type, int bit_rate, int short_train, int use_hdlc)
+{
+    t38_terminal_state_t *s;
+
+    s = (t38_terminal_state_t *) user_data;
+    span_log(&s->logging, SPAN_LOG_FLOW, "Set tx type %d\n", type);
+    if (s->t38_fe.current_tx_type == type)
+        return;
+
+    set_octets_per_data_packet(s, bit_rate);
+    switch (type)
+    {
+    case T30_MODEM_NONE:
+        s->t38_fe.timed_step = T38_TIMED_STEP_NONE;
+        s->t38_fe.current_tx_data_type = T38_DATA_NONE;
+        break;
+    case T30_MODEM_PAUSE:
+        s->t38_fe.next_tx_samples = s->t38_fe.samples + ms_to_samples(short_train);
+        s->t38_fe.timed_step = T38_TIMED_STEP_PAUSE;
+        s->t38_fe.current_tx_data_type = T38_DATA_NONE;
+        break;
+    case T30_MODEM_CED:
+    case T30_MODEM_CNG:
+        if (type == T30_MODEM_CED)
+        {
+            /* A 200ms initial delay is specified. Delay this amount before the CED indicator is sent. */
+            s->t38_fe.next_tx_samples = s->t38_fe.samples;
+            s->t38_fe.timed_step = T38_TIMED_STEP_CED;
+        }
+        else
+        {
+            /* Allow a short initial delay, so the chances of the other end actually being ready to receive
+               the CNG indicator are improved. */
+            s->t38_fe.next_tx_samples = s->t38_fe.samples;
+            s->t38_fe.timed_step = T38_TIMED_STEP_CNG;
+        }
+        s->t38_fe.current_tx_data_type = T38_DATA_NONE;
+        break;
+    case T30_MODEM_V21:
+        if (s->t38_fe.current_tx_type > T30_MODEM_V21)
+        {
+            /* Pause before switching from phase C, as per T.30. If we omit this, the receiver
+               might not see the carrier fall between the high speed and low speed sections. */
+            s->t38_fe.next_tx_samples = s->t38_fe.samples + ms_to_samples(75);
+        }
+        else
+        {
+            s->t38_fe.next_tx_samples = s->t38_fe.samples;
+        }
+        s->t38_fe.next_tx_indicator = T38_IND_V21_PREAMBLE;
+        s->t38_fe.current_tx_data_type = T38_DATA_V21;
+        s->t38_fe.timed_step = (use_hdlc)  ?  T38_TIMED_STEP_HDLC_MODEM  :  T38_TIMED_STEP_NON_ECM_MODEM;
+        break;
+    case T30_MODEM_V27TER:
+        switch (bit_rate)
+        {
+        case 2400:
+            s->t38_fe.next_tx_indicator = T38_IND_V27TER_2400_TRAINING;
+            s->t38_fe.current_tx_data_type = T38_DATA_V27TER_2400;
+            break;
+        case 4800:
+            s->t38_fe.next_tx_indicator = T38_IND_V27TER_4800_TRAINING;
+            s->t38_fe.current_tx_data_type = T38_DATA_V27TER_4800;
+            break;
+        }
+        s->t38_fe.timed_step = (use_hdlc)  ?  T38_TIMED_STEP_HDLC_MODEM  :  T38_TIMED_STEP_NON_ECM_MODEM;
+        s->t38_fe.next_tx_samples = s->t38_fe.samples + ms_to_samples(30);
+        break;
+    case T30_MODEM_V29:
+        switch (bit_rate)
+        {
+        case 7200:
+            s->t38_fe.next_tx_indicator = T38_IND_V29_7200_TRAINING;
+            s->t38_fe.current_tx_data_type = T38_DATA_V29_7200;
+            break;
+        case 9600:
+            s->t38_fe.next_tx_indicator = T38_IND_V29_9600_TRAINING;
+            s->t38_fe.current_tx_data_type = T38_DATA_V29_9600;
+            break;
+        }
+        s->t38_fe.timed_step = (use_hdlc)  ?  T38_TIMED_STEP_HDLC_MODEM  :  T38_TIMED_STEP_NON_ECM_MODEM;
+        s->t38_fe.next_tx_samples = s->t38_fe.samples + ms_to_samples(30);
+        break;
+    case T30_MODEM_V17:
+        switch (bit_rate)
+        {
+        case 7200:
+            s->t38_fe.next_tx_indicator = (short_train)  ?  T38_IND_V17_7200_SHORT_TRAINING  :  T38_IND_V17_7200_LONG_TRAINING;
+            s->t38_fe.current_tx_data_type = T38_DATA_V17_7200;
+            break;
+        case 9600:
+            s->t38_fe.next_tx_indicator = (short_train)  ?  T38_IND_V17_9600_SHORT_TRAINING  :  T38_IND_V17_9600_LONG_TRAINING;
+            s->t38_fe.current_tx_data_type = T38_DATA_V17_9600;
+            break;
+        case 12000:
+            s->t38_fe.next_tx_indicator = (short_train)  ?  T38_IND_V17_12000_SHORT_TRAINING  :  T38_IND_V17_12000_LONG_TRAINING;
+            s->t38_fe.current_tx_data_type = T38_DATA_V17_12000;
+            break;
+        case 14400:
+            s->t38_fe.next_tx_indicator = (short_train)  ?  T38_IND_V17_14400_SHORT_TRAINING  :  T38_IND_V17_14400_LONG_TRAINING;
+            s->t38_fe.current_tx_data_type = T38_DATA_V17_14400;
+            break;
+        }
+        s->t38_fe.timed_step = (use_hdlc)  ?  T38_TIMED_STEP_HDLC_MODEM  :  T38_TIMED_STEP_NON_ECM_MODEM;
+        s->t38_fe.next_tx_samples = s->t38_fe.samples + ms_to_samples(30);
+        break;
+    case T30_MODEM_DONE:
+        span_log(&s->logging, SPAN_LOG_FLOW, "FAX exchange complete\n");
+        s->t38_fe.timed_step = T38_TIMED_STEP_NONE;
+        s->t38_fe.current_tx_data_type = T38_DATA_NONE;
+        break;
+    }
+    s->t38_fe.current_tx_type = type;
+}
+/*- End of function --------------------------------------------------------*/
+
+void t38_terminal_set_config(t38_terminal_state_t *s, int without_pacing)
+{
+    if (without_pacing)
+    {
+        /* Continuous streaming mode, as used for TPKT over TCP transport */
+        /* Inhibit indicator packets */
+        s->t38_fe.t38.indicator_tx_count = 0;
+        s->t38_fe.t38.data_tx_count = 1;
+        s->t38_fe.t38.data_end_tx_count = 1;
+        s->t38_fe.ms_per_tx_chunk = 0;
+    }
+    else
+    {
+        /* Paced streaming mode, as used for UDP transports */
+        s->t38_fe.t38.indicator_tx_count = INDICATOR_TX_COUNT;
+        s->t38_fe.t38.data_tx_count = DATA_TX_COUNT;
+        s->t38_fe.t38.data_end_tx_count = DATA_END_TX_COUNT;
+        s->t38_fe.ms_per_tx_chunk = MS_PER_TX_CHUNK;
+    }
+    set_octets_per_data_packet(s, 300);
+}
+/*- End of function --------------------------------------------------------*/
+
+void t38_terminal_set_tep_mode(t38_terminal_state_t *s, int use_tep)
+{
+    if (use_tep)
+        s->t38_fe.chunking_modes |= T38_CHUNKING_ALLOW_TEP_TIME;
+    else
+        s->t38_fe.chunking_modes &= ~T38_CHUNKING_ALLOW_TEP_TIME;
+}
+/*- End of function --------------------------------------------------------*/
+
+void t38_terminal_set_fill_bit_removal(t38_terminal_state_t *s, int remove)
+{
+    if (remove)
+        s->t38_fe.iaf |= T30_IAF_MODE_NO_FILL_BITS;
+    else
+        s->t38_fe.iaf &= ~T30_IAF_MODE_NO_FILL_BITS;
+    t30_set_iaf_mode(&s->t30, s->t38_fe.iaf);
+}
+/*- End of function --------------------------------------------------------*/
+
+t30_state_t *t38_terminal_get_t30_state(t38_terminal_state_t *s)
+{
+    return &s->t30;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int t38_terminal_t38_fe_init(t38_terminal_state_t *t,
+                                    t38_tx_packet_handler_t *tx_packet_handler,
+                                    void *tx_packet_user_data)
+{
+    t38_terminal_front_end_state_t *s;
+    
+    s = &t->t38_fe;
+    t38_core_init(&s->t38,
+                  process_rx_indicator,
+                  process_rx_data,
+                  process_rx_missing,
+                  (void *) t,
+                  tx_packet_handler,
+                  tx_packet_user_data);
+    s->rx_signal_present = FALSE;
+    s->timed_step = T38_TIMED_STEP_NONE;
+    s->tx.ptr = 0;
+    s->iaf = T30_IAF_MODE_T37 | T30_IAF_MODE_T38;
+
+    s->current_tx_data_type = T38_DATA_NONE;
+    s->next_tx_samples = 0;
+    s->chunking_modes = T38_CHUNKING_ALLOW_TEP_TIME;
+
+    s->t38.fastest_image_data_rate = 14400;
+
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+t38_terminal_state_t *t38_terminal_init(t38_terminal_state_t *s,
+                                        int calling_party,
+                                        t38_tx_packet_handler_t *tx_packet_handler,
+                                        void *tx_packet_user_data)
+{
+    if (tx_packet_handler == NULL)
+        return NULL;
+
+    if (s == NULL)
+    {
+        if ((s = (t38_terminal_state_t *) malloc(sizeof(*s))) == NULL)
+            return NULL;
+    }
+    memset(s, 0, sizeof(*s));
+    span_log_init(&s->logging, SPAN_LOG_NONE, NULL);
+    span_log_set_protocol(&s->logging, "T.38T");
+
+    t38_terminal_t38_fe_init(s, tx_packet_handler, tx_packet_user_data);
+
+    t38_terminal_set_config(s, FALSE);
+
+    t30_init(&s->t30,
+             calling_party,
+             set_rx_type,
+             (void *) s,
+             set_tx_type,
+             (void *) s,
+             send_hdlc,
+             (void *) s);
+    t30_set_iaf_mode(&s->t30, s->t38_fe.iaf);
+    t30_set_supported_modems(&s->t30,
+                             T30_SUPPORT_V27TER | T30_SUPPORT_V29 | T30_SUPPORT_V17 | T30_SUPPORT_IAF);
+    t30_restart(&s->t30);
+    return s;
+}
+/*- End of function --------------------------------------------------------*/
+
+int t38_terminal_release(t38_terminal_state_t *s)
+{
+    t30_release(&s->t30);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int t38_terminal_free(t38_terminal_state_t *s)
+{
+    t30_release(&s->t30);
+    free(s);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/t4.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/t4.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,2120 @@
+//#define T4_STATE_DEBUGGING
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * t4.c - ITU T.4 FAX image processing
+ * This depends on libtiff (see <http://www.libtiff.org>)
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2003, 2007 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: t4.c,v 1.112 2008/07/22 13:48:15 steveu Exp $
+ */
+
+/*
+ * Much of this file is based on the T.4 and T.6 support in libtiff, which requires
+ * the following notice in any derived source code:
+ *
+ * Copyright (c) 1990-1997 Sam Leffler
+ * Copyright (c) 1991-1997 Silicon Graphics, Inc.
+ *
+ * Permission to use, copy, modify, distribute, and sell this software and 
+ * its documentation for any purpose is hereby granted without fee, provided
+ * that (i) the above copyright notices and this permission notice appear in
+ * all copies of the software and related documentation, and (ii) the names of
+ * Sam Leffler and Silicon Graphics may not be used in any advertising or
+ * publicity relating to the software without the specific, prior written
+ * permission of Sam Leffler and Silicon Graphics.
+ * 
+ * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
+ * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
+ * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
+ * 
+ * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
+ * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
+ * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
+ * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
+ * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
+ * OF THIS SOFTWARE.
+ *
+ * Decoder support is derived from code in Frank Cringle's viewfax program;
+ *      Copyright (C) 1990, 1995  Frank D. Cringle.
+ */
+
+/*! \file */
+
+#if defined(HAVE_CONFIG_H)
+#include <config.h>
+#endif
+
+#include <stdlib.h>
+#include <inttypes.h>
+#include <limits.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <time.h>
+#include <memory.h>
+#include <string.h>
+#include "floating_fudge.h"
+#if defined(HAVE_TGMATH_H)
+#include <tgmath.h>
+#endif
+#if defined(HAVE_MATH_H)
+#include <math.h>
+#endif
+#include <tiffio.h>
+
+#include "spandsp/telephony.h"
+#include "spandsp/logging.h"
+#include "spandsp/bit_operations.h"
+#include "spandsp/async.h"
+#include "spandsp/t4.h"
+
+#define CM_PER_INCH     2.54f
+
+/* Finite state machine state codes */
+enum
+{
+    S_Null      = 0,
+    S_Pass      = 1,
+    S_Horiz     = 2,
+    S_Vert      = 3,
+    S_Ext       = 4,
+    S_TermW     = 5,
+    S_TermB     = 6,
+    S_MakeUpW   = 7,
+    S_MakeUpB   = 8,
+    S_MakeUp    = 9,
+    S_EOL       = 10
+};
+
+#include "faxfont.h"
+
+#if defined(T4_STATE_DEBUGGING)
+static void STATE_TRACE(const char *format, ...)
+{
+    va_list arg_ptr;
+
+    va_start(arg_ptr, format);
+    vprintf(format, arg_ptr);
+    va_end(arg_ptr);
+}
+/*- End of function --------------------------------------------------------*/
+#else
+#define STATE_TRACE(...) /**/
+#endif
+
+/* Finite state machine state table entry */
+typedef struct
+{
+    uint8_t state;          /* See above */
+    uint8_t width;          /* Width of code in bits */
+    int16_t param;          /* Run length in bits */
+} t4_table_entry_t;
+
+typedef struct
+{
+    uint16_t length;        /* Length of T.4 code, in bits */
+    uint16_t code;          /* T.4 code */
+    int16_t run_length;     /* Run length, in bits */
+} t4_run_table_entry_t;
+
+#include "t4_states.h"
+
+static int set_tiff_directory_info(t4_state_t *s)
+{
+    time_t now;
+    struct tm *tm;
+    char buf[256 + 1];
+    uint16_t resunit;
+    float x_resolution;
+    float y_resolution;
+
+    /* Prepare the directory entry fully before writing the image, or libtiff complains */
+    TIFFSetField(s->tiff_file, TIFFTAG_COMPRESSION, s->output_compression);
+    if (s->output_compression == COMPRESSION_CCITT_T4)
+    {
+        TIFFSetField(s->tiff_file, TIFFTAG_T4OPTIONS, s->output_t4_options);
+        TIFFSetField(s->tiff_file, TIFFTAG_FAXMODE, FAXMODE_CLASSF);
+    }
+    TIFFSetField(s->tiff_file, TIFFTAG_IMAGEWIDTH, s->image_width);
+    TIFFSetField(s->tiff_file, TIFFTAG_BITSPERSAMPLE, 1);
+    TIFFSetField(s->tiff_file, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
+    TIFFSetField(s->tiff_file, TIFFTAG_SAMPLESPERPIXEL, 1);
+    if (s->output_compression == COMPRESSION_CCITT_T4
+        ||
+        s->output_compression == COMPRESSION_CCITT_T6)
+    {
+        TIFFSetField(s->tiff_file, TIFFTAG_ROWSPERSTRIP, -1L);
+    }
+    else
+    {
+        TIFFSetField(s->tiff_file,
+                     TIFFTAG_ROWSPERSTRIP,
+                     TIFFDefaultStripSize(s->tiff_file, 0));
+    }
+    TIFFSetField(s->tiff_file, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
+    TIFFSetField(s->tiff_file, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISWHITE);
+    TIFFSetField(s->tiff_file, TIFFTAG_FILLORDER, FILLORDER_LSB2MSB);
+
+    x_resolution = s->x_resolution/100.0f;
+    y_resolution = s->y_resolution/100.0f;
+    /* Metric seems the sane thing to use in the 21st century, but a lot of lousy software
+       gets FAX resolutions wrong, and more get it wrong using metric than using inches. */
+#if 0
+    TIFFSetField(s->tiff_file, TIFFTAG_XRESOLUTION, x_resolution);
+    TIFFSetField(s->tiff_file, TIFFTAG_YRESOLUTION, y_resolution);
+    resunit = RESUNIT_CENTIMETER;
+    TIFFSetField(s->tiff_file, TIFFTAG_RESOLUTIONUNIT, resunit);
+#else
+    TIFFSetField(s->tiff_file, TIFFTAG_XRESOLUTION, floorf(x_resolution*CM_PER_INCH + 0.5f));
+    TIFFSetField(s->tiff_file, TIFFTAG_YRESOLUTION, floorf(y_resolution*CM_PER_INCH + 0.5f));
+    resunit = RESUNIT_INCH;
+    TIFFSetField(s->tiff_file, TIFFTAG_RESOLUTIONUNIT, resunit);
+#endif
+    /* TODO: add the version of spandsp */
+    TIFFSetField(s->tiff_file, TIFFTAG_SOFTWARE, "spandsp");
+    if (gethostname(buf, sizeof(buf)) == 0)
+        TIFFSetField(s->tiff_file, TIFFTAG_HOSTCOMPUTER, buf);
+
+#if defined(TIFFTAG_FAXDCS)
+    if (s->dcs)
+        TIFFSetField(s->tiff_file, TIFFTAG_FAXDCS, s->dcs);
+#endif
+    if (s->sub_address)
+        TIFFSetField(s->tiff_file, TIFFTAG_FAXSUBADDRESS, s->sub_address);
+    if (s->far_ident)
+        TIFFSetField(s->tiff_file, TIFFTAG_IMAGEDESCRIPTION, s->far_ident);
+    if (s->vendor)
+        TIFFSetField(s->tiff_file, TIFFTAG_MAKE, s->vendor);
+    if (s->model)
+        TIFFSetField(s->tiff_file, TIFFTAG_MODEL, s->model);
+
+    time(&now);
+    tm = localtime(&now);
+    sprintf(buf,
+            "%4d/%02d/%02d %02d:%02d:%02d",
+            tm->tm_year + 1900,
+            tm->tm_mon + 1,
+            tm->tm_mday,
+            tm->tm_hour,
+            tm->tm_min,
+            tm->tm_sec);
+    TIFFSetField(s->tiff_file, TIFFTAG_DATETIME, buf);
+    TIFFSetField(s->tiff_file, TIFFTAG_FAXRECVTIME, now - s->page_start_time);
+
+    TIFFSetField(s->tiff_file, TIFFTAG_IMAGELENGTH, s->image_length);
+    /* Set the total pages to 1. For any one page document we will get this
+       right. For multi-page documents we will need to come back and fill in
+       the right answer when we know it. */
+    TIFFSetField(s->tiff_file, TIFFTAG_PAGENUMBER, s->pages_transferred++, 1);
+    s->pages_in_file = s->pages_transferred;
+    if (s->output_compression == COMPRESSION_CCITT_T4)
+    {
+        if (s->bad_rows)
+        {
+            TIFFSetField(s->tiff_file, TIFFTAG_BADFAXLINES, s->bad_rows);
+            TIFFSetField(s->tiff_file, TIFFTAG_CLEANFAXDATA, CLEANFAXDATA_REGENERATED);
+            TIFFSetField(s->tiff_file, TIFFTAG_CONSECUTIVEBADFAXLINES, s->longest_bad_row_run);
+        }
+        else
+        {
+            TIFFSetField(s->tiff_file, TIFFTAG_CLEANFAXDATA, CLEANFAXDATA_CLEAN);
+        }
+    }
+    TIFFSetField(s->tiff_file, TIFFTAG_IMAGEWIDTH, s->image_width);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void update_row_bit_info(t4_state_t *s)
+{
+    if (s->row_bits > s->max_row_bits)
+        s->max_row_bits = s->row_bits;
+    if (s->row_bits < s->min_row_bits)
+        s->min_row_bits = s->row_bits;
+    s->row_bits = 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int test_resolution(int res_unit, float actual, float expected)
+{
+    if (res_unit == RESUNIT_INCH)
+        actual *= 1.0f/CM_PER_INCH;
+    return (expected*0.95f <= actual  &&  actual <= expected*1.05f);
+}
+/*- End of function --------------------------------------------------------*/
+
+static int get_tiff_directory_info(t4_state_t *s)
+{
+    static const struct
+    {
+        float resolution;
+        int code;
+    } x_res_table[] =
+    {
+        { 102.0f/CM_PER_INCH, T4_X_RESOLUTION_R4},
+        { 204.0f/CM_PER_INCH, T4_X_RESOLUTION_R8},
+        { 300.0f/CM_PER_INCH, T4_X_RESOLUTION_300},
+        { 408.0f/CM_PER_INCH, T4_X_RESOLUTION_R16},
+        { 600.0f/CM_PER_INCH, T4_X_RESOLUTION_600},
+        { 800.0f/CM_PER_INCH, T4_X_RESOLUTION_800},
+        {1200.0f/CM_PER_INCH, T4_X_RESOLUTION_1200},
+        {             -1.00f, -1}
+    };
+    static const struct
+    {
+        float resolution;
+        int code;
+        int max_rows_to_next_1d_row;
+    } y_res_table[] =
+    {
+        {             38.50f, T4_Y_RESOLUTION_STANDARD, 2},
+        {             77.00f, T4_Y_RESOLUTION_FINE, 4},
+        { 300.0f/CM_PER_INCH, T4_Y_RESOLUTION_300, 6},
+        {            154.00f, T4_Y_RESOLUTION_SUPERFINE, 8},
+        { 600.0f/CM_PER_INCH, T4_Y_RESOLUTION_600, 12},
+        { 800.0f/CM_PER_INCH, T4_Y_RESOLUTION_800, 16},
+        {1200.0f/CM_PER_INCH, T4_Y_RESOLUTION_1200, 24},
+        {             -1.00f, -1, -1}
+    };
+    uint16_t res_unit;
+    uint32_t parm;
+    float x_resolution;
+    float y_resolution;
+    int i;
+
+    parm = 0;
+    TIFFGetField(s->tiff_file, TIFFTAG_BITSPERSAMPLE, &parm);
+    if (parm != 1)
+        return -1;
+    parm = 0;
+    TIFFGetField(s->tiff_file, TIFFTAG_IMAGEWIDTH, &parm);
+    s->image_width = parm;
+    s->bytes_per_row = (s->image_width + 7)/8;
+    parm = 0;
+    TIFFGetField(s->tiff_file, TIFFTAG_IMAGELENGTH, &parm);
+    s->image_length = parm;
+    x_resolution = 0.0f;
+    TIFFGetField(s->tiff_file, TIFFTAG_XRESOLUTION, &x_resolution);
+    y_resolution = 0.0f;
+    TIFFGetField(s->tiff_file, TIFFTAG_YRESOLUTION, &y_resolution);
+    res_unit = RESUNIT_INCH;
+    TIFFGetField(s->tiff_file, TIFFTAG_RESOLUTIONUNIT, &res_unit);
+
+    /* Allow a little range for the X resolution in centimeters. The spec doesn't pin down the
+       precise value. The other value should be exact. */
+    /* Treat everything we can't match as R8. Most FAXes are this resolution anyway. */
+    s->x_resolution = T4_X_RESOLUTION_R8;
+    for (i = 0;  x_res_table[i].code > 0;  i++)
+    {
+        if (test_resolution(res_unit, x_resolution, x_res_table[i].resolution))
+        {
+            s->x_resolution = x_res_table[i].code;
+            break;
+        }
+    }
+
+    s->y_resolution = T4_Y_RESOLUTION_STANDARD;
+    s->max_rows_to_next_1d_row = 2;
+    for (i = 0;  y_res_table[i].code > 0;  i++)
+    {
+        if (test_resolution(res_unit, y_resolution, y_res_table[i].resolution))
+        {
+            s->y_resolution = y_res_table[i].code;
+            s->max_rows_to_next_1d_row = y_res_table[i].max_rows_to_next_1d_row;
+            break;
+        }
+    }
+
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int open_tiff_input_file(t4_state_t *s, const char *file)
+{
+    if ((s->tiff_file = TIFFOpen(file, "r")) == NULL)
+        return -1;
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int close_tiff_input_file(t4_state_t *s)
+{
+    TIFFClose(s->tiff_file);
+    s->tiff_file = NULL;
+    if (s->file)
+        free((char *) s->file);
+    s->file = NULL;
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int open_tiff_output_file(t4_state_t *s, const char *file)
+{
+    if ((s->tiff_file = TIFFOpen(file, "w")) == NULL)
+        return -1;
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int close_tiff_output_file(t4_state_t *s)
+{
+    int i;
+
+    /* Perform any operations needed to tidy up a written TIFF file before
+       closure. */
+    if (s->pages_transferred > 1)
+    {
+        /* We need to edit the TIFF directories. Until now we did not know
+           the total page count, so the TIFF file currently says one. Now we
+           need to set the correct total page count associated with each page. */
+        for (i = 0;  i < s->pages_transferred;  i++)
+        {
+            TIFFSetDirectory(s->tiff_file, (tdir_t) i);
+            TIFFSetField(s->tiff_file, TIFFTAG_PAGENUMBER, i, s->pages_transferred);
+            TIFFWriteDirectory(s->tiff_file);
+        }
+    }
+    TIFFClose(s->tiff_file);
+    s->tiff_file = NULL;
+    if (s->file)
+    {
+        /* Try not to leave a file behind, if we didn't receive any pages to
+           put in it. */
+        if (s->pages_transferred == 0)
+            remove(s->file);
+        free((char *) s->file);
+    }
+    s->file = NULL;
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+#if defined(__i386__)  ||  defined(__x86_64__)  ||  defined(__ppc__)  ||   defined(__powerpc__)
+static __inline__ int run_length(unsigned int bits)
+{
+    return 7 - top_bit(bits);
+}
+/*- End of function --------------------------------------------------------*/
+#else
+static __inline__ int run_length(unsigned int bits)
+{
+    static const uint8_t run_len[256] =
+    {
+        8, 7, 6, 6, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4, /* 0x00 - 0x0F */
+        3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* 0x10 - 0x1F */
+        2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 0x20 - 0x2F */
+        2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, /* 0x30 - 0x3F */
+        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x40 - 0x4F */
+        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x50 - 0x5F */
+        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x60 - 0x6F */
+        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x70 - 0x7F */
+        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x80 - 0x8F */
+        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x90 - 0x9F */
+        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xA0 - 0xAF */
+        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xB0 - 0xBF */
+        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xC0 - 0xCF */
+        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xD0 - 0xDF */
+        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xE0 - 0xEF */
+        0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0xF0 - 0xFF */
+    };
+
+    return run_len[bits];
+}
+/*- End of function --------------------------------------------------------*/
+#endif
+
+static int row_to_run_lengths(uint32_t list[], const uint8_t row[], int width)
+{
+    uint32_t flip;
+    uint32_t x;
+    int span;
+    int entry;
+    int frag;
+    int rem;
+    int limit;
+    int i;
+    int pos;
+
+    /* Deal with whole words first. We know we are starting on a word boundary. */
+    entry = 0;
+    flip = 0;
+    limit = (width >> 3) & ~3;
+    span = 0;
+    pos = 0;
+    for (i = 0;  i < limit;  i += sizeof(uint32_t))
+    {
+        x = *((uint32_t *) &row[i]);
+        if (x != flip)
+        {
+            x = ((uint32_t) row[i] << 24) | ((uint32_t) row[i + 1] << 16) | ((uint32_t) row[i + 2] << 8) | ((uint32_t) row[i + 3]);
+            /* We know we are going to find at least one transition. */
+            frag = 31 - top_bit(x ^ flip);
+            pos += ((i << 3) - span + frag);
+            list[entry++] = pos;
+            x <<= frag;
+            flip ^= 0xFFFFFFFF;
+            rem = 32 - frag;
+            /* Now see if there are any more */
+            while ((frag = 31 - top_bit(x ^ flip)) < rem)
+            {
+                pos += frag;
+                list[entry++] = pos;
+                x <<= frag;
+                flip ^= 0xFFFFFFFF;
+                rem -= frag;
+            }
+            /* Save the remainder of the word */
+            span = (i << 3) + 32 - rem;
+        }
+    }
+    /* Now deal with some whole bytes, if there are any left. */
+    limit = width >> 3;
+    flip &= 0xFF000000;
+    if (i < limit)
+    {
+        for (  ;  i < limit;  i++)
+        {
+            x = (uint32_t) row[i] << 24;
+            if (x != flip)
+            {
+                /* We know we are going to find at least one transition. */
+                frag = 31 - top_bit(x ^ flip);
+                pos += ((i << 3) - span + frag);
+                list[entry++] = pos;
+                x <<= frag;
+                flip ^= 0xFF000000;
+                rem = 8 - frag;
+                /* Now see if there are any more */
+                while ((frag = 31 - top_bit(x ^ flip)) < rem)
+                {
+                    pos += frag;
+                    list[entry++] = pos;
+                    x <<= frag;
+                    flip ^= 0xFF000000;
+                    rem -= frag;
+                }   
+                /* Save the remainder of the word */
+                span = (i << 3) + 8 - rem;
+            }
+        }
+    }
+    /* Deal with any left over fractional byte. */
+    span = (i << 3) - span;
+    if ((rem = width & 7))
+    {
+        x = row[i];
+        x <<= 24;
+        do
+        {
+            frag = 31 - top_bit(x ^ flip);
+            if (frag > rem)
+                frag = rem;
+            pos += (span + frag);
+            list[entry++] = pos;
+            x <<= frag;
+            span = 0;
+            flip ^= 0xFF000000;
+            rem -= frag;
+        }
+        while (rem > 0);
+    }
+    else
+    {
+        if (span)
+        {
+            pos += span;
+            list[entry++] = pos;
+        }
+    }
+    return entry;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int free_buffers(t4_state_t *s)
+{
+    if (s->image_buffer)
+    {
+        free(s->image_buffer);
+        s->image_buffer = NULL;
+        s->image_buffer_size = 0;
+    }
+    if (s->cur_runs)
+    {
+        free(s->cur_runs);
+        s->cur_runs = NULL;
+    }
+    if (s->ref_runs)
+    {
+        free(s->ref_runs);
+        s->ref_runs = NULL;
+    }
+    if (s->row_buf)
+    {
+        free(s->row_buf);
+        s->row_buf = NULL;
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ void add_run_to_row(t4_state_t *s)
+{
+    if (s->run_length >= 0)
+    {
+        s->row_len += s->run_length;
+        /* Don't allow rows to grow too long, and overflow the buffers */
+        if (s->row_len <= s->image_width)
+            s->cur_runs[s->a_cursor++] = s->run_length;
+    }
+    s->run_length = 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int put_decoded_row(t4_state_t *s)
+{
+    static const int msbmask[9] =
+    {
+        0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff
+    };
+    uint8_t *t;
+    uint32_t i;
+    uint32_t *p;
+    int fudge;
+    int row_starts_at;
+    int x;
+
+    if (s->run_length)
+        add_run_to_row(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_starts_at = s->image_size;
+    /* Make sure there is enough room for another row */
+    if (s->image_size + s->bytes_per_row >= s->image_buffer_size)
+    {
+        if ((t = realloc(s->image_buffer, s->image_buffer_size + 100*s->bytes_per_row)) == NULL)
+            return -1;
+        s->image_buffer_size += 100*s->bytes_per_row;
+        s->image_buffer = t;
+    }
+    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 (i >= s->tx_bits)
+            {
+                s->tx_bitstream = (s->tx_bitstream << s->tx_bits) | (msbmask[s->tx_bits] & fudge);
+                for (i += (8 - s->tx_bits);  i >= 8;  i -= 8)
+                {
+                    s->tx_bits = 8;
+                    s->image_buffer[s->image_size++] = (uint8_t) s->tx_bitstream;
+                    s->tx_bitstream = fudge;
+                }
+            }
+            s->tx_bitstream = (s->tx_bitstream << i) | (msbmask[i] & fudge);
+            s->tx_bits -= 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 (i = 0, fudge = 0;  i < s->a_cursor  &&  fudge < s->image_width;  i++)
+            fudge += s->cur_runs[i];
+        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. */
+        if (s->image_size != s->last_row_starts_at)
+        {
+            /* Copy the previous row over this one */
+            memcpy(s->image_buffer + s->image_size, s->image_buffer + s->last_row_starts_at, s->bytes_per_row);
+            s->image_size += s->bytes_per_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;
+
+    /* Prepare the buffers for the next row. */
+    s->last_row_starts_at = row_starts_at;
+    /* 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;
+
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int t4_rx_end_page(t4_state_t *s)
+{
+    int row;
+    int i;
+
+    if (s->line_encoding == T4_COMPRESSION_ITU_T6)
+    {
+        /* Push enough zeros through the decoder to flush out any remaining codes */
+        for (i = 0;  i < 13;  i++)
+            t4_rx_put_bit(s, 0);
+    }
+    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;
+    }
+
+    if (s->image_size == 0)
+        return -1;
+
+    if (s->row_write_handler)
+    {
+        for (row = 0;  row < s->image_length;  row++)
+        {
+            if (s->row_write_handler(s->row_write_user_data, s->image_buffer + row*s->bytes_per_row, s->bytes_per_row) < 0)
+            {
+                span_log(&s->logging, SPAN_LOG_WARNING, "Write error at row %d.\n", row);
+                break;
+            }
+        }
+        /* Write a blank row to indicate the end of the image. */
+        if (s->row_write_handler(s->row_write_user_data, NULL, 0) < 0)
+            span_log(&s->logging, SPAN_LOG_WARNING, "Write error at row %d.\n", row);
+    }
+    else
+    {
+        /* Set up the TIFF directory info... */
+        set_tiff_directory_info(s);
+        /* ..and then write the image... */
+        if (TIFFWriteEncodedStrip(s->tiff_file, 0, s->image_buffer, s->image_length*s->bytes_per_row) < 0)
+            span_log(&s->logging, SPAN_LOG_WARNING, "%s: Error writing TIFF strip.\n", s->file);
+        /* ...then the directory entry, and libtiff is happy. */
+        TIFFWriteDirectory(s->tiff_file);
+    }
+    s->rx_bits = 0;
+    s->rx_skip_bits = 0;
+    s->rx_bitstream = 0;
+    s->consecutive_eols = 5;
+
+    s->image_size = 0;
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ void drop_rx_bits(t4_state_t *s, int bits)
+{
+    /* Only remove one bit right now. The rest need to be removed step by step,
+       checking for a misaligned EOL along the way. */
+    s->row_bits += bits;
+    s->rx_skip_bits += (bits - 1);
+    s->rx_bits--;
+    s->rx_bitstream >>= 1;
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ void force_drop_rx_bits(t4_state_t *s, int bits)
+{
+    /* This should only be called to drop the bits of an EOL, as that is the
+       only place where it is safe to drop them all at once. */
+    s->row_bits += bits;
+    s->rx_skip_bits = 0;
+    s->rx_bits -= bits;
+    s->rx_bitstream >>= bits;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int rx_put_bits(t4_state_t *s, uint32_t bit_string, int quantity)
+{
+    int bits;
+
+    /* 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->rx_bitstream |= (bit_string << s->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->rx_bits += quantity) < 13)
+        return FALSE;
+    if (s->consecutive_eols)
+    {
+        /* Check if the image has already terminated */
+        if (s->consecutive_eols >= 5)
+            return TRUE;
+        if (s->consecutive_eols < 0)
+        {
+            /* We are waiting for the very first EOL (1D or 2D only). */
+            while ((s->rx_bitstream & 0xFFF) != 0x800)
+            {
+                s->rx_bitstream >>= 1;
+                if (--s->rx_bits < 13)
+                    return FALSE;
+            }
+            s->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->rx_bitstream & 0x1000);
+                force_drop_rx_bits(s, 13);
+            }
+            /* We can proceed to processing the bit stream as an image now */
+        }
+    }
+
+    while (s->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->rx_bitstream & 0x0FFF) == 0x0800)
+        {
+            STATE_TRACE("EOL\n");
+            if (s->row_len == 0)
+            {
+                if (++s->consecutive_eols >= 5)
+                    return TRUE;
+            }
+            else
+            {
+                if (s->run_length > 0)
+                    add_run_to_row(s);
+                s->consecutive_eols = 0;
+                if (put_decoded_row(s))
+                    return TRUE;
+                update_row_bit_info(s);
+            }
+            if (s->line_encoding == T4_COMPRESSION_ITU_T4_1D)
+            {
+                force_drop_rx_bits(s, 12);
+            }
+            else
+            {
+                s->row_is_2d = !(s->rx_bitstream & 0x1000);
+                force_drop_rx_bits(s, 13);
+            }
+            s->its_black = FALSE;
+            s->black_white = 0;
+            s->run_length = 0;
+            s->row_len = 0;
+            continue;
+        }
+        if (s->rx_skip_bits)
+        {
+            /* We are clearing out the remaining bits of the last code word we
+               absorbed. */
+            s->rx_skip_bits--;
+            s->rx_bits--;
+            s->rx_bitstream >>= 1;
+            continue;
+        }
+        if (s->row_is_2d  &&  s->black_white == 0)
+        {
+            bits = s->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->a_cursor)
+            {
+                /* Move past a0, always staying on the current colour */
+                for (  ;  s->b1 <= s->a0;  s->b_cursor += 2)
+                    s->b1 += (s->ref_runs[s->b_cursor] + s->ref_runs[s->b_cursor + 1]);
+            }
+            switch (t4_2d_table[bits].state)
+            {
+            case S_Horiz:
+                STATE_TRACE("Horiz %d %d %d\n",
+                            s->image_width,
+                            s->a0,
+                            s->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->its_black = s->a_cursor & 1;
+                s->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->a0,
+                            s->b1,
+                            s->run_length);
+                s->run_length += (s->b1 - s->a0 + t4_2d_table[bits].param);
+                s->a0 = s->b1 + t4_2d_table[bits].param;
+                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->b1 += s->ref_runs[s->b_cursor++];
+                }
+                else
+                {
+                    if (s->b_cursor)
+                        s->b1 -= s->ref_runs[--s->b_cursor];
+                }
+                break;
+            case S_Pass:
+                STATE_TRACE("Pass %d %d %d %d %d\n",
+                            s->image_width,
+                            s->a0,
+                            s->b1,
+                            s->ref_runs[s->b_cursor],
+                            s->ref_runs[s->b_cursor + 1]);
+                s->b1 += s->ref_runs[s->b_cursor++];
+                s->run_length += (s->b1 - s->a0);
+                s->a0 = s->b1;
+                s->b1 += s->ref_runs[s->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->a0,
+                            ((s->rx_bitstream >> t4_2d_table[bits].width) & 0x7),
+                            s->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);
+        }
+        else
+        {
+            if (s->its_black)
+            {
+                bits = s->rx_bitstream & 0x1FFF;
+                STATE_TRACE("State %d, %d - Black %d %d %d\n",
+                            t4_1d_black_table[bits].state,
+                            t4_1d_black_table[bits].width,
+                            s->image_width,
+                            s->a0,
+                            t4_1d_black_table[bits].param);
+                switch (t4_1d_black_table[bits].state)
+                {
+                case S_MakeUpB:
+                case S_MakeUp:
+                    s->run_length += t4_1d_black_table[bits].param;
+                    s->a0 += t4_1d_black_table[bits].param;
+                    break;
+                case S_TermB:
+                    s->its_black = FALSE;
+                    if (s->row_len < s->image_width)
+                    {
+                        s->run_length += t4_1d_black_table[bits].param;
+                        s->a0 += t4_1d_black_table[bits].param;
+                        add_run_to_row(s);
+                    }
+                    if (s->black_white)
+                        s->black_white--;
+                    break;
+                default:
+                    /* Bad black */
+                    s->black_white = 0;
+                    break;
+                }
+                drop_rx_bits(s, t4_1d_black_table[bits].width);
+            }
+            else
+            {
+                bits = s->rx_bitstream & 0xFFF;
+                STATE_TRACE("State %d, %d - White %d %d %d\n",
+                            t4_1d_white_table[bits].state,
+                            t4_1d_white_table[bits].width,
+                            s->image_width,
+                            s->a0,
+                            t4_1d_white_table[bits].param);
+                switch (t4_1d_white_table[bits].state)
+                {
+                case S_MakeUpW:
+                case S_MakeUp:
+                    s->run_length += t4_1d_white_table[bits].param;
+                    s->a0 += t4_1d_white_table[bits].param;
+                    break;
+                case S_TermW:
+                    s->its_black = TRUE;
+                    if (s->row_len < s->image_width)
+                    {
+                        s->run_length += t4_1d_white_table[bits].param;
+                        s->a0 += t4_1d_white_table[bits].param;
+                        add_run_to_row(s);
+                    }
+                    if (s->black_white)
+                        s->black_white--;
+                    break;
+                default:
+                    /* Bad white */
+                    s->black_white = 0;
+                    break;
+                }
+                drop_rx_bits(s, t4_1d_white_table[bits].width);
+            }
+        }
+        if (s->a0 >= s->image_width)
+            s->a0 = s->image_width - 1;
+
+        if (s->line_encoding == T4_COMPRESSION_ITU_T6)
+        {
+            /* T.6 has no EOL markers. We sense the end of a line by its length alone. */
+            /* The last test here is a backstop protection, so a corrupt image cannot
+               cause us to do bad things. Bad encoders have actually been seen, which
+               demand such protection. */
+            if (s->black_white == 0  &&  s->row_len >= s->image_width)
+            {
+                STATE_TRACE("EOL T.6\n");
+                if (s->run_length > 0)
+                    add_run_to_row(s);
+                update_row_bit_info(s);
+                if (put_decoded_row(s))
+                    return TRUE;
+                s->its_black = FALSE;
+                s->black_white = 0;
+                s->run_length = 0;
+                s->row_len = 0;
+            }
+        }
+    }
+    return FALSE;
+}
+/*- End of function --------------------------------------------------------*/
+
+int t4_rx_put_bit(t4_state_t *s, int bit)
+{
+    return rx_put_bits(s, bit & 1, 1);
+}
+/*- End of function --------------------------------------------------------*/
+
+int t4_rx_put_byte(t4_state_t *s, uint8_t byte)
+{
+    return rx_put_bits(s, byte & 0xFF, 8);
+}
+/*- End of function --------------------------------------------------------*/
+
+int t4_rx_put_chunk(t4_state_t *s, const uint8_t buf[], int len)
+{
+    int i;
+    uint8_t byte;
+
+    for (i = 0;  i < len;  i++)
+    {
+        byte = buf[i];
+        if (rx_put_bits(s, byte & 0xFF, 8))
+            return TRUE;
+    }
+    return FALSE;
+}
+/*- End of function --------------------------------------------------------*/
+
+int t4_rx_set_row_write_handler(t4_state_t *s, t4_row_write_handler_t handler, void *user_data)
+{
+    s->row_write_handler = handler;
+    s->row_write_user_data = user_data;
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+t4_state_t *t4_rx_init(t4_state_t *s, const char *file, int output_encoding)
+{
+    memset(s, 0, sizeof(*s));
+    span_log_init(&s->logging, SPAN_LOG_NONE, NULL);
+    span_log_set_protocol(&s->logging, "T.4");
+    s->rx = TRUE;
+    
+    span_log(&s->logging, SPAN_LOG_FLOW, "Start rx document\n");
+
+    if (open_tiff_output_file(s, file) < 0)
+        return NULL;
+
+    /* Save the file name for logging reports. */
+    s->file = strdup(file);
+    /* Only provide for one form of coding throughout the file, even though the
+       coding on the wire could change between pages. */
+    switch (output_encoding)
+    {
+    case T4_COMPRESSION_ITU_T4_1D:
+        s->output_compression = COMPRESSION_CCITT_T4;
+        s->output_t4_options = GROUP3OPT_FILLBITS;
+        break;
+    case T4_COMPRESSION_ITU_T4_2D:
+        s->output_compression = COMPRESSION_CCITT_T4;
+        s->output_t4_options = GROUP3OPT_FILLBITS | GROUP3OPT_2DENCODING;
+        break;
+    case T4_COMPRESSION_ITU_T6:
+        s->output_compression = COMPRESSION_CCITT_T6;
+        s->output_t4_options = 0;
+        break;
+    }
+
+    /* Until we have a valid figure for the bytes per row, we need it to be set to a suitable
+       value to ensure it will be seen as changing when the real value is used. */
+    s->bytes_per_row = 0;
+
+    s->pages_transferred = 0;
+    s->pages_in_file = 0;
+    s->start_page = 0;
+    s->stop_page = INT_MAX;
+
+    s->image_buffer = NULL;
+    s->image_buffer_size = 0;
+
+    /* Set some default values */
+    s->x_resolution = T4_X_RESOLUTION_R8;
+    s->y_resolution = T4_Y_RESOLUTION_FINE;
+    s->image_width = T4_WIDTH_R8_A4;
+
+    return s;
+}
+/*- End of function --------------------------------------------------------*/
+
+int t4_rx_start_page(t4_state_t *s)
+{
+    int bytes_per_row;
+    int run_space;
+    uint32_t *bufptr;
+
+    span_log(&s->logging, SPAN_LOG_FLOW, "Start rx page - compression %d\n", s->line_encoding);
+    if (s->tiff_file == NULL)
+        return -1;
+
+    /* Calculate the scanline/tile width. */
+    bytes_per_row = (s->image_width + 7)/8;
+    run_space = (s->image_width + 4)*sizeof(uint32_t);
+    if (bytes_per_row != s->bytes_per_row)
+    {
+        /* Allocate the space required for decoding the new row length. */
+        s->bytes_per_row = bytes_per_row;
+        if ((bufptr = (uint32_t *) realloc(s->cur_runs, run_space)) == NULL)
+            return -1;
+        s->cur_runs = bufptr;
+        if ((bufptr = (uint32_t *) realloc(s->ref_runs, run_space)) == NULL)
+            return -1;
+        s->ref_runs = bufptr;
+    }
+    memset(s->cur_runs, 0, run_space);
+    memset(s->ref_runs, 0, run_space);
+
+    s->rx_bits = 0;
+    s->rx_skip_bits = 0;
+    s->rx_bitstream = 0;
+    s->row_bits = 0;
+    s->min_row_bits = INT_MAX;
+    s->max_row_bits = 0;
+
+    s->row_is_2d = (s->line_encoding == T4_COMPRESSION_ITU_T6);
+    /* We start at -1 EOLs for 1D and 2D decoding, as an indication we are waiting for the
+       first EOL. */
+    s->consecutive_eols = (s->line_encoding == T4_COMPRESSION_ITU_T6)  ?  0  :  -1;
+
+    s->bad_rows = 0;
+    s->longest_bad_row_run = 0;
+    s->curr_bad_row_run = 0;
+    s->image_length = 0;
+    s->tx_bitstream = 0;
+    s->tx_bits = 8;
+    s->image_size = 0;
+    s->line_image_size = 0;
+    s->last_row_starts_at = 0;
+
+    s->row_len = 0;
+    s->its_black = FALSE;
+    s->black_white = 0;
+
+    /* Initialise the reference line to all white */
+    s->ref_runs[0] =
+    s->ref_runs[1] =
+    s->ref_runs[2] =
+    s->ref_runs[3] = s->image_width;
+    s->ref_steps = 1;
+
+    s->b_cursor = 1;
+    s->a_cursor = 0;
+    s->b1 = s->ref_runs[0];
+    s->a0 = 0;
+
+    s->run_length = 0;
+
+    time (&s->page_start_time);
+
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int t4_rx_delete(t4_state_t *s)
+{
+    if (t4_rx_end(s))
+        return -1;
+    free_buffers(s);
+    free(s);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int t4_rx_end(t4_state_t *s)
+{
+    if (!s->rx)
+        return -1;
+    if (s->tiff_file)
+        close_tiff_output_file(s);
+    free_buffers(s);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+void t4_rx_set_rx_encoding(t4_state_t *s, int encoding)
+{
+    s->line_encoding = encoding;
+}
+/*- End of function --------------------------------------------------------*/
+
+void t4_rx_set_image_width(t4_state_t *s, int width)
+{
+    s->image_width = width;
+}
+/*- End of function --------------------------------------------------------*/
+
+void t4_rx_set_y_resolution(t4_state_t *s, int resolution)
+{
+    s->y_resolution = resolution;
+}
+/*- End of function --------------------------------------------------------*/
+
+void t4_rx_set_x_resolution(t4_state_t *s, int resolution)
+{
+    s->x_resolution = resolution;
+}
+/*- End of function --------------------------------------------------------*/
+
+void t4_rx_set_dcs(t4_state_t *s, const char *dcs)
+{
+    s->dcs = (dcs  &&  dcs[0])  ?  dcs  :  NULL;
+}
+/*- End of function --------------------------------------------------------*/
+
+void t4_rx_set_sub_address(t4_state_t *s, const char *sub_address)
+{
+    s->sub_address = (sub_address  &&  sub_address[0])  ?  sub_address  :  NULL;
+}
+/*- End of function --------------------------------------------------------*/
+
+void t4_rx_set_far_ident(t4_state_t *s, const char *ident)
+{
+    s->far_ident = (ident  &&  ident[0])  ?  ident  :  NULL;
+}
+/*- End of function --------------------------------------------------------*/
+
+void t4_rx_set_vendor(t4_state_t *s, const char *vendor)
+{
+    s->vendor = vendor;
+}
+/*- End of function --------------------------------------------------------*/
+
+void t4_rx_set_model(t4_state_t *s, const char *model)
+{
+    s->model = model;
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ int put_encoded_bits(t4_state_t *s, uint32_t bits, int length)
+{
+    uint8_t *t;
+
+    /* We might be called with a large length value, to spew out a mass of zero bits for
+       minimum row length padding. */
+    s->tx_bitstream |= (bits << s->tx_bits);
+    s->tx_bits += length;
+    s->row_bits += length;
+    if ((s->image_size + (s->tx_bits + 7)/8) >= s->image_buffer_size)
+    {
+        if ((t = realloc(s->image_buffer, s->image_buffer_size + 100*s->bytes_per_row)) == NULL)
+            return -1;
+        s->image_buffer = t;
+        s->image_buffer_size += 100*s->bytes_per_row;
+    }
+    while (s->tx_bits >= 8)
+    {
+        s->image_buffer[s->image_size++] = (uint8_t) (s->tx_bitstream & 0xFF);
+        s->tx_bitstream >>= 8;
+        s->tx_bits -= 8;
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+/*
+ * Write the sequence of codes that describes
+ * the specified span of zero's or one's.  The
+ * appropriate table that holds the make-up and
+ * terminating codes is supplied.
+ */
+static __inline__ int put_1d_span(t4_state_t *s, int32_t span, const t4_run_table_entry_t *tab)
+{
+    const t4_run_table_entry_t *te;
+
+    te = &tab[63 + (2560 >> 6)];
+    while (span >= 2560 + 64)
+    {
+        if (put_encoded_bits(s, te->code, te->length))
+            return -1;
+        span -= te->run_length;
+    }
+    te = &tab[63 + (span >> 6)];
+    if (span >= 64)
+    {
+        if (put_encoded_bits(s, te->code, te->length))
+            return -1;
+        span -= te->run_length;
+    }
+    if (put_encoded_bits(s, tab[span].code, tab[span].length))
+        return -1;
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+#define pixel_is_black(x,bit) (((x)[(bit) >> 3] << ((bit) & 7)) & 0x80)
+
+/*
+ * Write an EOL code to the output stream.  We also handle writing the tag
+ * bit for the next scanline when doing 2D encoding.
+ */
+static void encode_eol(t4_state_t *s)
+{
+    uint32_t code;
+    int length;
+
+    if (s->line_encoding == T4_COMPRESSION_ITU_T4_2D)
+    {
+        code = 0x0800 | ((!s->row_is_2d) << 12);
+        length = 13;
+    }
+    else
+    {
+        /* T.4 1D EOL, or T.6 EOFB */
+        code = 0x800;
+        length = 12;
+    }
+    if (s->row_bits)
+    {
+        /* We may need to pad the row to a minimum length. */
+        if (s->row_bits + length < s->min_bits_per_row)
+            put_encoded_bits(s, 0, s->min_bits_per_row - (s->row_bits + length));
+        put_encoded_bits(s, code, length);
+        update_row_bit_info(s);
+    }
+    else
+    {
+        /* We don't pad zero length rows. They are the consecutive EOLs which end a page. */
+        put_encoded_bits(s, code, length);
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+/*
+ * 2D-encode a row of pixels.  Consult ITU specification T.4 for the algorithm.
+ */
+static void encode_2d_row(t4_state_t *s)
+{
+    static const t4_run_table_entry_t codes[] =
+    {
+        { 7, 0x60, 0 },         /* VR3          0000 011 */
+        { 6, 0x30, 0 },         /* VR2          0000 11 */
+        { 3, 0x06, 0 },         /* VR1          011 */
+        { 1, 0x01, 0 },         /* V0           1 */
+        { 3, 0x02, 0 },         /* VL1          010 */
+        { 6, 0x10, 0 },         /* VL2          0000 10 */
+        { 7, 0x20, 0 },         /* VL3          0000 010 */
+        { 3, 0x04, 0 },         /* horizontal   001 */
+        { 4, 0x08, 0 }          /* pass         0001 */
+    };
+
+    /* The reference or starting changing element on the coding line. At the start of the coding
+       line, a0 is set on an imaginary white changing element situated just before the first element
+       on the line. During the coding of the coding line, the position of a0 is defined by the
+       previous coding mode. (See T.4/4.2.1.3.2.) */
+    int a0;
+    /* The next changing element to the right of a0 on the coding line. */
+    int a1;
+    /* The next changing element to the right of a1 on the coding line. */
+    int a2;
+    /* The first changing element on the reference line to the right of a0 and of opposite colour to a0. */
+    int b1;
+    /* The next changing element to the right of b1 on the reference line. */
+    int b2;
+    int diff;
+    int a_cursor;
+    int b_cursor;
+    int cur_steps;
+    uint32_t *p;
+
+    /*
+                                                    b1          b2 
+            XX  XX  XX  XX  XX  --  --  --  --  --  XX  XX  XX  --  --  --  --  --
+            XX  XX  XX  --  --  --  --  --  XX  XX  XX  XX  XX  XX  --  --  --  --
+                        a0                  a1                      a2
+
+
+        a)	Pass mode
+	        This mode is identified when the position of b2 lies to the left of a1. When this mode
+            has been coded, a0 is set on the element of the coding line below b2 in preparation for
+            the next coding (i.e. on a0').
+            
+                                    b1          b2 
+            XX  XX  XX  XX  --  --  XX  XX  XX  --  --  --  --  --
+            XX  XX  --  --  --  --  --  --  --  --  --  --  XX  XX 
+                    a0                          a0'         a1
+                                Pass mode
+                                
+
+            However, the state where b2 occurs just above a1, as shown in the figure below, is not
+            considered as a pass mode.
+
+                                    b1          b2 
+            XX  XX  XX  XX  --  --  XX  XX  XX  --  --  --  --  --
+            XX  XX  --  --  --  --  --  --  --  XX  XX  XX  XX  XX
+                    a0                          a1
+                                Not pass mode
+
+
+        b)	Vertical mode
+	        When this mode is identified, the position of a1 is coded relative to the position of b1.
+            The relative distance a1b1 can take on one of seven values V(0), VR(1), VR(2), VR(3),
+            VL(1), VL(2) and VL(3), each of which is represented by a separate code word. The
+            subscripts R and L indicate that a1 is to the right or left respectively of b1, and the
+            number in brackets indicates the value of the distance a1b1. After vertical mode coding
+            has occurred, the position of a0 is set on a1 (see figure below).
+
+        c)	Horizontal mode
+	        When this mode is identified, both the run-lengths a0a1 and a1a2 are coded using the code
+            words H + M(a0a1) + M(a1a2). H is the flag code word 001 taken from the two-dimensional
+            code table. M(a0a1) and M(a1a2) are code words which represent the length and "colour"
+            of the runs a0a1 and a1a2 respectively and are taken from the appropriate white or black
+            one-dimensional code tables. After a horizontal mode coding, the position of a0 is set on
+            a2 (see figure below).
+
+                                                            Vertical
+                                                            <a1 b1>
+                                                                    b1              b2 
+            --  XX  XX  XX  XX  XX  --  --  --  --  --  --  --  --  XX  XX  XX  XX  --  --  --
+            --  --  --  --  --  --  --  --  --  --  --  --  XX  XX  XX  XX  XX  XX  XX  --  --
+                                    a0                      a1                          a2
+                                   <-------- a0a1 --------><-------- a1a2 ------------>
+                                                    Horizontal mode
+                          Vertical and horizontal modes
+     */
+    /* The following implements the 2-D encoding section of the flow chart in Figure7/T.4 */
+    cur_steps = row_to_run_lengths(s->cur_runs, s->row_buf, s->image_width);
+    /* Stretch the row a little, so when we step by 2 we are guaranteed to
+       hit an entry showing the row length */
+    s->cur_runs[cur_steps] =
+    s->cur_runs[cur_steps + 1] =
+    s->cur_runs[cur_steps + 2] = s->cur_runs[cur_steps - 1];
+
+    a0 = 0;
+    a1 = s->cur_runs[0];
+    b1 = s->ref_runs[0];
+    a_cursor = 0;
+    b_cursor = 0;
+    for (;;)
+    {
+        b2 = s->ref_runs[b_cursor + 1];
+        if (b2 >= a1)
+        {
+            diff = b1 - a1;
+            if (abs(diff) <= 3)
+            {
+                /* Vertical mode coding */
+                put_encoded_bits(s, codes[diff + 3].code, codes[diff + 3].length);
+                a0 = a1;
+                a_cursor++;
+            }
+            else
+            {
+                /* Horizontal mode coding */
+                a2 = s->cur_runs[a_cursor + 1];
+                put_encoded_bits(s, codes[7].code, codes[7].length);
+                if (a0 + a1 == 0  ||  pixel_is_black(s->row_buf, a0) == 0)
+                {
+                    put_1d_span(s, a1 - a0, t4_white_codes);
+                    put_1d_span(s, a2 - a1, t4_black_codes);
+                }
+                else
+                {
+                    put_1d_span(s, a1 - a0, t4_black_codes);
+                    put_1d_span(s, a2 - a1, t4_white_codes);
+                }
+                a0 = a2;
+                a_cursor += 2;
+            }
+            if (a0 >= s->image_width)
+                break;
+            if (a_cursor >= cur_steps)
+                a_cursor = cur_steps - 1;
+            a1 = s->cur_runs[a_cursor];
+        }
+        else
+        {
+            /* Pass mode coding */
+            put_encoded_bits(s, codes[8].code, codes[8].length);
+            /* We now set a0 to somewhere in the middle of its current run,
+               but we know are aren't moving beyond that run. */
+            a0 = b2;
+            if (a0 >= s->image_width)
+                break;
+        }
+        /* We need to hunt for the correct position in the reference row, as the
+           runs there have no particular alignment with the runs in the current
+           row. */
+        if (pixel_is_black(s->row_buf, a0))
+            b_cursor |= 1;
+        else
+            b_cursor &= ~1;
+        if (a0 < s->ref_runs[b_cursor])
+        {
+            for (  ;  b_cursor >= 0;  b_cursor -= 2)
+            {
+                if (a0 >= s->ref_runs[b_cursor])
+                    break;
+            }
+            b_cursor += 2;
+        }
+        else
+        {
+            for (  ;  b_cursor < s->ref_steps;  b_cursor += 2)
+            {
+                if (a0 < s->ref_runs[b_cursor])
+                    break;
+            }
+            if (b_cursor >= s->ref_steps)
+                b_cursor = s->ref_steps - 1;
+        }
+        b1 = s->ref_runs[b_cursor];
+    }
+    /* Swap the buffers */
+    s->ref_steps = cur_steps;
+    p = s->cur_runs;
+    s->cur_runs = s->ref_runs;
+    s->ref_runs = p;
+}
+/*- End of function --------------------------------------------------------*/
+
+/*
+ * 1D-encode a row of pixels.  The encoding is
+ * a sequence of all-white or all-black spans
+ * of pixels encoded with Huffman codes.
+ */
+static void encode_1d_row(t4_state_t *s)
+{
+    int i;
+
+    /* Do our work in the reference row buffer, and it is already in place if
+       we need a reference row for a following 2D encoded row. */
+    s->ref_steps = row_to_run_lengths(s->ref_runs, s->row_buf, s->image_width);
+    put_1d_span(s, s->ref_runs[0], t4_white_codes);
+    for (i = 1;  i < s->ref_steps;  i++)
+        put_1d_span(s, s->ref_runs[i] - s->ref_runs[i - 1], (i & 1)  ?  t4_black_codes  :  t4_white_codes);
+    /* Stretch the row a little, so when we step by 2 we are guaranteed to
+       hit an entry showing the row length */
+    s->ref_runs[s->ref_steps] =
+    s->ref_runs[s->ref_steps + 1] =
+    s->ref_runs[s->ref_steps + 2] = s->ref_runs[s->ref_steps - 1];
+}
+/*- End of function --------------------------------------------------------*/
+
+static int encode_row(t4_state_t *s)
+{
+    switch (s->line_encoding)
+    {
+    case T4_COMPRESSION_ITU_T6:
+        /* T.6 compression is a trivial step up from T.4 2D, so we just
+           throw it in here. T.6 is only used with error correction,
+           so it does not need independantly compressed (i.e. 1D) lines
+           to recover from data errors. It doesn't need EOLs, either. */
+        if (s->row_bits)
+            update_row_bit_info(s);
+        encode_2d_row(s);
+        break;
+    case T4_COMPRESSION_ITU_T4_2D:
+        encode_eol(s);
+        if (s->row_is_2d)
+        {
+            encode_2d_row(s);
+            s->rows_to_next_1d_row--;
+        }
+        else
+        {
+            encode_1d_row(s);
+            s->row_is_2d = TRUE;
+        }
+        if (s->rows_to_next_1d_row <= 0)
+        {
+            /* Insert a row of 1D encoding */
+            s->row_is_2d = FALSE;
+            s->rows_to_next_1d_row = s->max_rows_to_next_1d_row - 1;
+        }
+        break;
+    default:
+    case T4_COMPRESSION_ITU_T4_1D:
+        encode_eol(s);
+        encode_1d_row(s);
+        break;
+    }
+    s->row++;
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int t4_tx_set_row_read_handler(t4_state_t *s, t4_row_read_handler_t handler, void *user_data)
+{
+    s->row_read_handler = handler;
+    s->row_read_user_data = user_data;
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+t4_state_t *t4_tx_init(t4_state_t *s, const char *file, int start_page, int stop_page)
+{
+    int run_space;
+
+    memset(s, 0, sizeof(*s));
+    span_log_init(&s->logging, SPAN_LOG_NONE, NULL);
+    span_log_set_protocol(&s->logging, "T.4");
+    s->rx = FALSE;
+
+    span_log(&s->logging, SPAN_LOG_FLOW, "Start tx document\n");
+
+    if (open_tiff_input_file(s, file) < 0)
+        return NULL;
+    s->file = strdup(file);
+    s->start_page = (start_page >= 0)  ?  start_page  :  0;
+    s->stop_page = (stop_page >= 0)  ?  stop_page : INT_MAX;
+
+    if (get_tiff_directory_info(s))
+    {
+        close_tiff_input_file(s);
+        return NULL;
+    }
+
+    s->rows_to_next_1d_row = s->max_rows_to_next_1d_row - 1;
+
+    s->pages_transferred = s->start_page;
+    s->pages_in_file = -1;
+
+    run_space = (s->image_width + 4)*sizeof(uint32_t);
+    if ((s->cur_runs = (uint32_t *) malloc(run_space)) == NULL)
+        return NULL;
+    if ((s->ref_runs = (uint32_t *) malloc(run_space)) == NULL)
+    {
+        free_buffers(s);
+        close_tiff_input_file(s);
+        return NULL;
+    }
+    if ((s->row_buf = malloc(s->bytes_per_row)) == NULL)
+    {
+        free_buffers(s);
+        close_tiff_input_file(s);
+        return NULL;
+    }
+    s->ref_runs[0] =
+    s->ref_runs[1] =
+    s->ref_runs[2] =
+    s->ref_runs[3] = s->image_width;
+    s->ref_steps = 1;
+    s->image_buffer_size = 0;
+    return s;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void make_header(t4_state_t *s, char *header)
+{
+    time_t now;
+    struct tm tm;
+    static const char *months[] =
+    {
+        "Jan",
+        "Feb",
+        "Mar",
+        "Apr",
+        "May",
+        "Jun",
+        "Jul",
+        "Aug",
+        "Sep",
+        "Oct",
+        "Nov",
+        "Dec"
+    };
+
+    time(&now);
+    tm = *localtime(&now);
+    snprintf(header,
+             132,
+             "  %2d-%s-%d  %02d:%02d    %-50s %-21s   p.%d",
+             tm.tm_mday,
+             months[tm.tm_mon],
+             tm.tm_year + 1900,
+             tm.tm_hour,
+             tm.tm_min,
+             s->header_info,
+             s->local_ident,
+             s->pages_transferred + 1);
+}
+/*- End of function --------------------------------------------------------*/
+
+int t4_tx_start_page(t4_state_t *s)
+{
+    int row;
+    int i;
+    int repeats;
+    int pattern;
+    int row_bufptr;
+    int parm;
+    int run_space;
+    int len;
+    char *t;
+    char header[132 + 1];
+    uint8_t *bufptr8;
+    uint32_t *bufptr;
+
+    span_log(&s->logging, SPAN_LOG_FLOW, "Start tx page %d\n", s->pages_transferred);
+    if (s->pages_transferred > s->stop_page)
+        return -1;
+    if (s->tiff_file == NULL)
+        return -1;
+    if (!TIFFSetDirectory(s->tiff_file, (tdir_t) s->pages_transferred))
+        return -1;
+    s->image_size = 0;
+    s->tx_bitstream = 0;
+    s->tx_bits = 0;
+    s->row_is_2d = (s->line_encoding == T4_COMPRESSION_ITU_T6);
+    s->rows_to_next_1d_row = s->max_rows_to_next_1d_row - 1;
+
+    /* Allow for pages being of different width */
+    parm = 0;
+    TIFFGetField(s->tiff_file, TIFFTAG_IMAGEWIDTH, &parm);
+    run_space = (parm + 4)*sizeof(uint32_t);
+    if (parm != s->image_width)
+    {
+        s->image_width = parm;
+        s->bytes_per_row = (s->image_width + 7)/8;
+
+        if ((bufptr = (uint32_t *) realloc(s->cur_runs, run_space)) == NULL)
+            return -1;
+        s->cur_runs = bufptr;
+        if ((bufptr = (uint32_t *) realloc(s->ref_runs, run_space)) == NULL)
+            return -1;
+        s->ref_runs = bufptr;
+        if ((bufptr8 = realloc(s->row_buf, s->bytes_per_row)) == NULL)
+            return -1;
+        s->row_buf = bufptr8;
+    }
+    s->ref_runs[0] =
+    s->ref_runs[1] =
+    s->ref_runs[2] =
+    s->ref_runs[3] = s->image_width;
+    s->ref_steps = 1;
+
+    s->row_bits = 0;
+    s->min_row_bits = INT_MAX;
+    s->max_row_bits = 0;
+
+    if (s->header_info  &&  s->header_info[0])
+    {
+        /* Modify the resulting image to include a header line, typical of hardware FAX machines */
+        make_header(s, header);
+        switch (s->y_resolution)
+        {
+        case T4_Y_RESOLUTION_1200:
+            repeats = 12;
+            break;
+        case T4_Y_RESOLUTION_800:
+            repeats = 8;
+            break;
+        case T4_Y_RESOLUTION_600:
+            repeats = 6;
+            break;
+        case T4_Y_RESOLUTION_SUPERFINE:
+            repeats = 4;
+            break;
+        case T4_Y_RESOLUTION_300:
+            repeats = 3;
+            break;
+        case T4_Y_RESOLUTION_FINE:
+            repeats = 2;
+            break;
+        default:
+            repeats = 1;
+            break;
+        }
+        for (row = 0;  row < 16;  row++)
+        {
+            t = header;
+            row_bufptr = 0;
+            for (t = header;  *t  &&  row_bufptr <= s->bytes_per_row - 2;  t++)
+            {
+                pattern = header_font[(uint8_t) *t][row];
+                s->row_buf[row_bufptr++] = (uint8_t) (pattern >> 8);
+                s->row_buf[row_bufptr++] = (uint8_t) (pattern & 0xFF);
+            }
+            for (  ;  row_bufptr < s->bytes_per_row;  )
+                s->row_buf[row_bufptr++] = 0;
+            for (i = 0;  i < repeats;  i++)
+            {
+                if (encode_row(s))
+                    return -1;
+            }
+        }
+    }
+    if (s->row_read_handler)
+    {
+        for (row = 0;  ;  row++)
+        {
+            if ((len = s->row_read_handler(s->row_read_user_data, s->row_buf, s->bytes_per_row)) < 0)
+            {
+                span_log(&s->logging, SPAN_LOG_WARNING, "%s: Read error at row %d.\n", s->file, row);
+                break;
+            }
+            if (len == 0)
+                break;
+            if (encode_row(s))
+                return -1;
+        }
+        s->image_length = row;
+    }
+    else
+    {
+        s->image_length = 0;
+        TIFFGetField(s->tiff_file, TIFFTAG_IMAGELENGTH, &s->image_length);
+        for (row = 0;  row < s->image_length;  row++)
+        {
+            if (TIFFReadScanline(s->tiff_file, s->row_buf, row, 0) <= 0)
+            {
+                span_log(&s->logging, SPAN_LOG_WARNING, "%s: Read error at row %d.\n", s->file, row);
+                break;
+            }
+            if (encode_row(s))
+                return -1;
+        }
+    }
+    if (s->line_encoding == T4_COMPRESSION_ITU_T6)
+    {
+        /* Attach an EOFB (end of facsimile block) to the end of the page */
+        encode_eol(s);
+        encode_eol(s);
+    }
+    else
+    {
+        /* Attach a return to control (RTC == 6 x EOLs) to the end of the page */
+        s->row_is_2d = FALSE;
+        for (i = 0;  i < 6;  i++)
+            encode_eol(s);
+    }
+
+    /* Force any partial byte in progress to flush */
+    put_encoded_bits(s, 0, 7);
+    s->bit_pos = 7;
+    s->bit_ptr = 0;
+    s->line_image_size = s->image_size*8;
+
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int t4_tx_more_pages(t4_state_t *s)
+{
+    span_log(&s->logging, SPAN_LOG_FLOW, "Checking for the existance of page %d\n", s->pages_transferred + 1);
+    if (s->pages_transferred >= s->stop_page)
+        return -1;
+    if (s->tiff_file == NULL)
+        return -1;
+    if (s->pages_in_file >= 0)
+    {
+        if (s->pages_transferred + 1 >= s->pages_in_file)
+            return -1;
+    }
+    else
+    {
+        if (!TIFFSetDirectory(s->tiff_file, (tdir_t) s->pages_transferred + 1))
+            return -1;
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int t4_tx_restart_page(t4_state_t *s)
+{
+    s->bit_pos = 7;
+    s->bit_ptr = 0;
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int t4_tx_end_page(t4_state_t *s)
+{
+    s->pages_transferred++;
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int t4_tx_get_bit(t4_state_t *s)
+{
+    int bit;
+
+    if (s->bit_ptr >= s->image_size)
+        return PUTBIT_END_OF_DATA;
+    bit = (s->image_buffer[s->bit_ptr] >> (7 - s->bit_pos)) & 1;
+    if (--s->bit_pos < 0)
+    {
+        s->bit_pos = 7;
+        s->bit_ptr++;
+    }
+    return bit;
+}
+/*- End of function --------------------------------------------------------*/
+
+int t4_tx_get_byte(t4_state_t *s)
+{
+    if (s->bit_ptr >= s->image_size)
+        return 0x100;
+    return s->image_buffer[s->bit_ptr++];
+}
+/*- End of function --------------------------------------------------------*/
+
+int t4_tx_get_chunk(t4_state_t *s, uint8_t buf[], int max_len)
+{
+    if (s->bit_ptr >= s->image_size)
+        return 0;
+    if (s->bit_ptr + max_len > s->image_size)
+        max_len = s->image_size - s->bit_ptr;
+    memcpy(buf, &s->image_buffer[s->bit_ptr], max_len);
+    s->bit_ptr += max_len;
+    return max_len;
+}
+/*- End of function --------------------------------------------------------*/
+
+int t4_tx_check_bit(t4_state_t *s)
+{
+    int bit;
+
+    if (s->bit_ptr >= s->image_size)
+        return PUTBIT_END_OF_DATA;
+    bit = (s->image_buffer[s->bit_ptr] >> s->bit_pos) & 1;
+    return bit;
+}
+/*- End of function --------------------------------------------------------*/
+
+int t4_tx_delete(t4_state_t *s)
+{
+    if (t4_tx_end(s))
+        return -1;
+    free_buffers(s);
+    free(s);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int t4_tx_end(t4_state_t *s)
+{
+    if (s->rx)
+        return -1;
+    if (s->tiff_file)
+        close_tiff_input_file(s);
+    free_buffers(s);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+void t4_tx_set_tx_encoding(t4_state_t *s, int encoding)
+{
+    s->line_encoding = encoding;
+    s->rows_to_next_1d_row = s->max_rows_to_next_1d_row - 1;
+    s->row_is_2d = FALSE;
+}
+/*- End of function --------------------------------------------------------*/
+
+void t4_tx_set_min_row_bits(t4_state_t *s, int bits)
+{
+    s->min_bits_per_row = bits;
+}
+/*- End of function --------------------------------------------------------*/
+
+void t4_tx_set_local_ident(t4_state_t *s, const char *ident)
+{
+    s->local_ident = (ident  &&  ident[0])  ?  ident  :  NULL;
+}
+/*- End of function --------------------------------------------------------*/
+
+void t4_tx_set_header_info(t4_state_t *s, const char *info)
+{
+    s->header_info = (info  &&  info[0])  ?  info  :  NULL;
+}
+/*- End of function --------------------------------------------------------*/
+
+int t4_tx_get_y_resolution(t4_state_t *s)
+{
+    return s->y_resolution;
+}
+/*- End of function --------------------------------------------------------*/
+
+int t4_tx_get_x_resolution(t4_state_t *s)
+{
+    return s->x_resolution;
+}
+/*- End of function --------------------------------------------------------*/
+
+int t4_tx_get_image_width(t4_state_t *s)
+{
+    return s->image_width;
+}
+/*- End of function --------------------------------------------------------*/
+
+int t4_tx_get_pages_in_file(t4_state_t *s)
+{
+    int max;
+
+    /* Each page *should* contain the total number of pages, but can this be
+       trusted? Some files say 0. Actually searching for the last page is
+       more reliable. */
+    max = 0;
+    while (TIFFSetDirectory(s->tiff_file, (tdir_t) max))
+    	max++;
+    s->pages_in_file = max;
+    /* Back to the previous page */
+    if (!TIFFSetDirectory(s->tiff_file, (tdir_t) s->pages_transferred))
+        return -1;
+    return max;
+}
+/*- End of function --------------------------------------------------------*/
+
+void t4_get_transfer_statistics(t4_state_t *s, t4_stats_t *t)
+{
+    t->pages_transferred = s->pages_transferred - s->start_page;
+    t->pages_in_file = s->pages_in_file;
+    t->width = s->image_width;
+    t->length = s->image_length;
+    t->bad_rows = s->bad_rows;
+    t->longest_bad_row_run = s->longest_bad_row_run;
+    t->x_resolution = s->x_resolution;
+    t->y_resolution = s->y_resolution;
+    t->encoding = s->line_encoding;
+    t->line_image_size = s->line_image_size/8;
+}
+/*- End of function --------------------------------------------------------*/
+
+const char *t4_encoding_to_str(int encoding)
+{
+    switch (encoding)
+    {
+    case T4_COMPRESSION_ITU_T4_1D:
+        return "T.4 1-D";
+    case T4_COMPRESSION_ITU_T4_2D:
+        return "T.4 2-D";
+    case T4_COMPRESSION_ITU_T6:
+        return "T.6";
+    }
+    return "???";
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/t4_states.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/t4_states.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,12680 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * t4_states.h - state tables for T.4 FAX image processing
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2004 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: t4_states.h,v 1.7 2008/04/17 14:26:58 steveu Exp $
+ */
+
+/* Legitimate runs of zero bits which are the tail end of one code
+   plus the start of the next code do not exceed 10 bits. */
+
+/*
+ * Note that these tables are ordered such that the
+ * index into the table is known to be either the
+ * run length, or (run length / 64) + a fixed offset.
+ */
+static const t4_run_table_entry_t t4_white_codes[] =
+{
+    { 8, 0x00AC,    0},         /* 0011 0101 */
+    { 6, 0x0038,    1},         /* 0001 11 */
+    { 4, 0x000E,    2},         /* 0111 */
+    { 4, 0x0001,    3},         /* 1000 */
+    { 4, 0x000D,    4},         /* 1011 */
+    { 4, 0x0003,    5},         /* 1100 */
+    { 4, 0x0007,    6},         /* 1110 */
+    { 4, 0x000F,    7},         /* 1111 */
+    { 5, 0x0019,    8},         /* 1001 1 */
+    { 5, 0x0005,    9},         /* 1010 0 */
+    { 5, 0x001C,   10},         /* 0011 1 */
+    { 5, 0x0002,   11},         /* 0100 0 */
+    { 6, 0x0004,   12},         /* 0010 00 */
+    { 6, 0x0030,   13},         /* 0000 11 */
+    { 6, 0x000B,   14},         /* 1101 00 */
+    { 6, 0x002B,   15},         /* 1101 01 */
+    { 6, 0x0015,   16},         /* 1010 10 */
+    { 6, 0x0035,   17},         /* 1010 11 */
+    { 7, 0x0072,   18},         /* 0100 111 */
+    { 7, 0x0018,   19},         /* 0001 100 */
+    { 7, 0x0008,   20},         /* 0001 000 */
+    { 7, 0x0074,   21},         /* 0010 111 */
+    { 7, 0x0060,   22},         /* 0000 011 */
+    { 7, 0x0010,   23},         /* 0000 100 */
+    { 7, 0x000A,   24},         /* 0101 000 */
+    { 7, 0x006A,   25},         /* 0101 011 */
+    { 7, 0x0064,   26},         /* 0010 011 */
+    { 7, 0x0012,   27},         /* 0100 100 */
+    { 7, 0x000C,   28},         /* 0011 000 */
+    { 8, 0x0040,   29},         /* 0000 0010 */
+    { 8, 0x00C0,   30},         /* 0000 0011 */
+    { 8, 0x0058,   31},         /* 0001 1010 */
+    { 8, 0x00D8,   32},         /* 0001 1011 */
+    { 8, 0x0048,   33},         /* 0001 0010 */
+    { 8, 0x00C8,   34},         /* 0001 0011 */
+    { 8, 0x0028,   35},         /* 0001 0100 */
+    { 8, 0x00A8,   36},         /* 0001 0101 */
+    { 8, 0x0068,   37},         /* 0001 0110 */
+    { 8, 0x00E8,   38},         /* 0001 0111 */
+    { 8, 0x0014,   39},         /* 0010 1000 */
+    { 8, 0x0094,   40},         /* 0010 1001 */
+    { 8, 0x0054,   41},         /* 0010 1010 */
+    { 8, 0x00D4,   42},         /* 0010 1011 */
+    { 8, 0x0034,   43},         /* 0010 1100 */
+    { 8, 0x00B4,   44},         /* 0010 1101 */
+    { 8, 0x0020,   45},         /* 0000 0100 */
+    { 8, 0x00A0,   46},         /* 0000 0101 */
+    { 8, 0x0050,   47},         /* 0000 1010 */
+    { 8, 0x00D0,   48},         /* 0000 1011 */
+    { 8, 0x004A,   49},         /* 0101 0010 */
+    { 8, 0x00CA,   50},         /* 0101 0011 */
+    { 8, 0x002A,   51},         /* 0101 0100 */
+    { 8, 0x00AA,   52},         /* 0101 0101 */
+    { 8, 0x0024,   53},         /* 0010 0100 */
+    { 8, 0x00A4,   54},         /* 0010 0101 */
+    { 8, 0x001A,   55},         /* 0101 1000 */
+    { 8, 0x009A,   56},         /* 0101 1001 */
+    { 8, 0x005A,   57},         /* 0101 1010 */
+    { 8, 0x00DA,   58},         /* 0101 1011 */
+    { 8, 0x0052,   59},         /* 0100 1010 */
+    { 8, 0x00D2,   60},         /* 0100 1011 */
+    { 8, 0x004C,   61},         /* 0011 0010 */
+    { 8, 0x00CC,   62},         /* 0011 0011 */
+    { 8, 0x002C,   63},         /* 0011 0100 */
+    { 5, 0x001B,   64},         /* 1101 1 */
+    { 5, 0x0009,  128},         /* 1001 0 */
+    { 6, 0x003A,  192},         /* 0101 11 */
+    { 7, 0x0076,  256},         /* 0110 111 */
+    { 8, 0x006C,  320},         /* 0011 0110 */
+    { 8, 0x00EC,  384},         /* 0011 0111 */
+    { 8, 0x0026,  448},         /* 0110 0100 */
+    { 8, 0x00A6,  512},         /* 0110 0101 */
+    { 8, 0x0016,  576},         /* 0110 1000 */
+    { 8, 0x00E6,  640},         /* 0110 0111 */
+    { 9, 0x0066,  704},         /* 0110 0110 0 */
+    { 9, 0x0166,  768},         /* 0110 0110 1 */
+    { 9, 0x0096,  832},         /* 0110 1001 0 */
+    { 9, 0x0196,  896},         /* 0110 1001 1 */
+    { 9, 0x0056,  960},         /* 0110 1010 0 */
+    { 9, 0x0156, 1024},         /* 0110 1010 1 */
+    { 9, 0x00D6, 1088},         /* 0110 1011 0 */
+    { 9, 0x01D6, 1152},         /* 0110 1011 1 */
+    { 9, 0x0036, 1216},         /* 0110 1100 0 */
+    { 9, 0x0136, 1280},         /* 0110 1100 1 */
+    { 9, 0x00B6, 1344},         /* 0110 1101 0 */
+    { 9, 0x01B6, 1408},         /* 0110 1101 1 */
+    { 9, 0x0032, 1472},         /* 0100 1100 0 */
+    { 9, 0x0132, 1536},         /* 0100 1100 1 */
+    { 9, 0x00B2, 1600},         /* 0100 1101 0 */
+    { 6, 0x0006, 1664},         /* 0110 00 */
+    { 9, 0x01B2, 1728},         /* 0100 1101 1 */
+    {11, 0x0080, 1792},         /* 0000 0001 000 */
+    {11, 0x0180, 1856},         /* 0000 0001 100 */
+    {11, 0x0580, 1920},         /* 0000 0001 101 */
+    {12, 0x0480, 1984},         /* 0000 0001 0010 */
+    {12, 0x0C80, 2048},         /* 0000 0001 0011 */
+    {12, 0x0280, 2112},         /* 0000 0001 0100 */
+    {12, 0x0A80, 2176},         /* 0000 0001 0101 */
+    {12, 0x0680, 2240},         /* 0000 0001 0110 */
+    {12, 0x0E80, 2304},         /* 0000 0001 0111 */
+    {12, 0x0380, 2368},         /* 0000 0001 1100 */
+    {12, 0x0B80, 2432},         /* 0000 0001 1101 */
+    {12, 0x0780, 2496},         /* 0000 0001 1110 */
+    {12, 0x0F80, 2560},         /* 0000 0001 1111 */
+};
+
+static const t4_run_table_entry_t t4_black_codes[] =
+{
+    {10, 0x03B0,    0},         /* 0000 1101 11 */
+    { 3, 0x0002,    1},         /* 010 */
+    { 2, 0x0003,    2},         /* 11 */
+    { 2, 0x0001,    3},         /* 10 */
+    { 3, 0x0006,    4},         /* 011 */
+    { 4, 0x000C,    5},         /* 0011 */
+    { 4, 0x0004,    6},         /* 0010 */
+    { 5, 0x0018,    7},         /* 0001 1 */
+    { 6, 0x0028,    8},         /* 0001 01 */
+    { 6, 0x0008,    9},         /* 0001 00 */
+    { 7, 0x0010,   10},         /* 0000 100 */
+    { 7, 0x0050,   11},         /* 0000 101 */
+    { 7, 0x0070,   12},         /* 0000 111 */
+    { 8, 0x0020,   13},         /* 0000 0100 */
+    { 8, 0x00E0,   14},         /* 0000 0111 */
+    { 9, 0x0030,   15},         /* 0000 1100 0 */
+    {10, 0x03A0,   16},         /* 0000 0101 11 */
+    {10, 0x0060,   17},         /* 0000 0110 00 */
+    {10, 0x0040,   18},         /* 0000 0010 00 */
+    {11, 0x0730,   19},         /* 0000 1100 111 */
+    {11, 0x00B0,   20},         /* 0000 1101 000 */
+    {11, 0x01B0,   21},         /* 0000 1101 100 */
+    {11, 0x0760,   22},         /* 0000 0110 111 */
+    {11, 0x00A0,   23},         /* 0000 0101 000 */
+    {11, 0x0740,   24},         /* 0000 0010 111 */
+    {11, 0x00C0,   25},         /* 0000 0011 000 */
+    {12, 0x0530,   26},         /* 0000 1100 1010 */
+    {12, 0x0D30,   27},         /* 0000 1100 1011 */
+    {12, 0x0330,   28},         /* 0000 1100 1100 */
+    {12, 0x0B30,   29},         /* 0000 1100 1101 */
+    {12, 0x0160,   30},         /* 0000 0110 1000 */
+    {12, 0x0960,   31},         /* 0000 0110 1001 */
+    {12, 0x0560,   32},         /* 0000 0110 1010 */
+    {12, 0x0D60,   33},         /* 0000 0110 1011 */
+    {12, 0x04B0,   34},         /* 0000 1101 0010 */
+    {12, 0x0CB0,   35},         /* 0000 1101 0011 */
+    {12, 0x02B0,   36},         /* 0000 1101 0100 */
+    {12, 0x0AB0,   37},         /* 0000 1101 0101 */
+    {12, 0x06B0,   38},         /* 0000 1101 0110 */
+    {12, 0x0EB0,   39},         /* 0000 1101 0111 */
+    {12, 0x0360,   40},         /* 0000 0110 1100 */
+    {12, 0x0B60,   41},         /* 0000 0110 1101 */
+    {12, 0x05B0,   42},         /* 0000 1101 1010 */
+    {12, 0x0DB0,   43},         /* 0000 1101 1011 */
+    {12, 0x02A0,   44},         /* 0000 0101 0100 */
+    {12, 0x0AA0,   45},         /* 0000 0101 0101 */
+    {12, 0x06A0,   46},         /* 0000 0101 0110 */
+    {12, 0x0EA0,   47},         /* 0000 0101 0111 */
+    {12, 0x0260,   48},         /* 0000 0110 0100 */
+    {12, 0x0A60,   49},         /* 0000 0110 0101 */
+    {12, 0x04A0,   50},         /* 0000 0101 0010 */
+    {12, 0x0CA0,   51},         /* 0000 0101 0011 */
+    {12, 0x0240,   52},         /* 0000 0010 0100 */
+    {12, 0x0EC0,   53},         /* 0000 0011 0111 */
+    {12, 0x01C0,   54},         /* 0000 0011 1000 */
+    {12, 0x0E40,   55},         /* 0000 0010 0111 */
+    {12, 0x0140,   56},         /* 0000 0010 1000 */
+    {12, 0x01A0,   57},         /* 0000 0101 1000 */
+    {12, 0x09A0,   58},         /* 0000 0101 1001 */
+    {12, 0x0D40,   59},         /* 0000 0010 1011 */
+    {12, 0x0340,   60},         /* 0000 0010 1100 */
+    {12, 0x05A0,   61},         /* 0000 0101 1010 */
+    {12, 0x0660,   62},         /* 0000 0110 0110 */
+    {12, 0x0E60,   63},         /* 0000 0110 0111 */
+    {10, 0x03C0,   64},         /* 0000 0011 11 */
+    {12, 0x0130,  128},         /* 0000 1100 1000 */
+    {12, 0x0930,  192},         /* 0000 1100 1001 */
+    {12, 0x0DA0,  256},         /* 0000 0101 1011 */
+    {12, 0x0CC0,  320},         /* 0000 0011 0011 */
+    {12, 0x02C0,  384},         /* 0000 0011 0100 */
+    {12, 0x0AC0,  448},         /* 0000 0011 0101 */
+    {13, 0x06C0,  512},         /* 0000 0011 0110 0 */
+    {13, 0x16C0,  576},         /* 0000 0011 0110 1 */
+    {13, 0x0A40,  640},         /* 0000 0010 0101 0 */
+    {13, 0x1A40,  704},         /* 0000 0010 0101 1 */
+    {13, 0x0640,  768},         /* 0000 0010 0110 0 */
+    {13, 0x1640,  832},         /* 0000 0010 0110 1 */
+    {13, 0x09C0,  896},         /* 0000 0011 1001 0 */
+    {13, 0x19C0,  960},         /* 0000 0011 1001 1 */
+    {13, 0x05C0, 1024},         /* 0000 0011 1010 0 */
+    {13, 0x15C0, 1088},         /* 0000 0011 1010 1 */
+    {13, 0x0DC0, 1152},         /* 0000 0011 1011 0 */
+    {13, 0x1DC0, 1216},         /* 0000 0011 1011 1 */
+    {13, 0x0940, 1280},         /* 0000 0010 1001 0 */
+    {13, 0x1940, 1344},         /* 0000 0010 1001 1 */
+    {13, 0x0540, 1408},         /* 0000 0010 1010 0 */
+    {13, 0x1540, 1472},         /* 0000 0010 1010 1 */
+    {13, 0x0B40, 1536},         /* 0000 0010 1101 0 */
+    {13, 0x1B40, 1600},         /* 0000 0010 1101 1 */
+    {13, 0x04C0, 1664},         /* 0000 0011 0010 0 */
+    {13, 0x14C0, 1728},         /* 0000 0011 0010 1 */
+    {11, 0x0080, 1792},         /* 0000 0001 000 */
+    {11, 0x0180, 1856},         /* 0000 0001 100 */
+    {11, 0x0580, 1920},         /* 0000 0001 101 */
+    {12, 0x0480, 1984},         /* 0000 0001 0010 */
+    {12, 0x0C80, 2048},         /* 0000 0001 0011 */
+    {12, 0x0280, 2112},         /* 0000 0001 0100 */
+    {12, 0x0A80, 2176},         /* 0000 0001 0101 */
+    {12, 0x0680, 2240},         /* 0000 0001 0110 */
+    {12, 0x0E80, 2304},         /* 0000 0001 0111 */
+    {12, 0x0380, 2368},         /* 0000 0001 1100 */
+    {12, 0x0B80, 2432},         /* 0000 0001 1101 */
+    {12, 0x0780, 2496},         /* 0000 0001 1110 */
+    {12, 0x0F80, 2560},         /* 0000 0001 1111 */
+};
+
+static const t4_table_entry_t t4_2d_table[128] =
+{
+    {S_Null,       1,    0},
+    {S_Vert,       1,    0},
+    {S_Vert,       3,   -1},
+    {S_Vert,       1,    0},
+    {S_Horiz,      3,    0},
+    {S_Vert,       1,    0},
+    {S_Vert,       3,    1},
+    {S_Vert,       1,    0},
+    {S_Pass,       4,    0},
+    {S_Vert,       1,    0},
+    {S_Vert,       3,   -1},
+    {S_Vert,       1,    0},
+    {S_Horiz,      3,    0},
+    {S_Vert,       1,    0},
+    {S_Vert,       3,    1},
+    {S_Vert,       1,    0},
+    {S_Vert,       6,   -2},
+    {S_Vert,       1,    0},
+    {S_Vert,       3,   -1},
+    {S_Vert,       1,    0},
+    {S_Horiz,      3,    0},
+    {S_Vert,       1,    0},
+    {S_Vert,       3,    1},
+    {S_Vert,       1,    0},
+    {S_Pass,       4,    0},
+    {S_Vert,       1,    0},
+    {S_Vert,       3,   -1},
+    {S_Vert,       1,    0},
+    {S_Horiz,      3,    0},
+    {S_Vert,       1,    0},
+    {S_Vert,       3,    1},
+    {S_Vert,       1,    0},
+    {S_Vert,       7,   -3},
+    {S_Vert,       1,    0},
+    {S_Vert,       3,   -1},
+    {S_Vert,       1,    0},
+    {S_Horiz,      3,    0},
+    {S_Vert,       1,    0},
+    {S_Vert,       3,    1},
+    {S_Vert,       1,    0},
+    {S_Pass,       4,    0},
+    {S_Vert,       1,    0},
+    {S_Vert,       3,   -1},
+    {S_Vert,       1,    0},
+    {S_Horiz,      3,    0},
+    {S_Vert,       1,    0},
+    {S_Vert,       3,    1},
+    {S_Vert,       1,    0},
+    {S_Vert,       6,    2},
+    {S_Vert,       1,    0},
+    {S_Vert,       3,   -1},
+    {S_Vert,       1,    0},
+    {S_Horiz,      3,    0},
+    {S_Vert,       1,    0},
+    {S_Vert,       3,    1},
+    {S_Vert,       1,    0},
+    {S_Pass,       4,    0},
+    {S_Vert,       1,    0},
+    {S_Vert,       3,   -1},
+    {S_Vert,       1,    0},
+    {S_Horiz,      3,    0},
+    {S_Vert,       1,    0},
+    {S_Vert,       3,    1},
+    {S_Vert,       1,    0},
+    {S_Ext,        7,    0},
+    {S_Vert,       1,    0},
+    {S_Vert,       3,   -1},
+    {S_Vert,       1,    0},
+    {S_Horiz,      3,    0},
+    {S_Vert,       1,    0},
+    {S_Vert,       3,    1},
+    {S_Vert,       1,    0},
+    {S_Pass,       4,    0},
+    {S_Vert,       1,    0},
+    {S_Vert,       3,   -1},
+    {S_Vert,       1,    0},
+    {S_Horiz,      3,    0},
+    {S_Vert,       1,    0},
+    {S_Vert,       3,    1},
+    {S_Vert,       1,    0},
+    {S_Vert,       6,   -2},
+    {S_Vert,       1,    0},
+    {S_Vert,       3,   -1},
+    {S_Vert,       1,    0},
+    {S_Horiz,      3,    0},
+    {S_Vert,       1,    0},
+    {S_Vert,       3,    1},
+    {S_Vert,       1,    0},
+    {S_Pass,       4,    0},
+    {S_Vert,       1,    0},
+    {S_Vert,       3,   -1},
+    {S_Vert,       1,    0},
+    {S_Horiz,      3,    0},
+    {S_Vert,       1,    0},
+    {S_Vert,       3,    1},
+    {S_Vert,       1,    0},
+    {S_Vert,       7,    3},
+    {S_Vert,       1,    0},
+    {S_Vert,       3,   -1},
+    {S_Vert,       1,    0},
+    {S_Horiz,      3,    0},
+    {S_Vert,       1,    0},
+    {S_Vert,       3,    1},
+    {S_Vert,       1,    0},
+    {S_Pass,       4,    0},
+    {S_Vert,       1,    0},
+    {S_Vert,       3,   -1},
+    {S_Vert,       1,    0},
+    {S_Horiz,      3,    0},
+    {S_Vert,       1,    0},
+    {S_Vert,       3,    1},
+    {S_Vert,       1,    0},
+    {S_Vert,       6,    2},
+    {S_Vert,       1,    0},
+    {S_Vert,       3,   -1},
+    {S_Vert,       1,    0},
+    {S_Horiz,      3,    0},
+    {S_Vert,       1,    0},
+    {S_Vert,       3,    1},
+    {S_Vert,       1,    0},
+    {S_Pass,       4,    0},
+    {S_Vert,       1,    0},
+    {S_Vert,       3,   -1},
+    {S_Vert,       1,    0},
+    {S_Horiz,      3,    0},
+    {S_Vert,       1,    0},
+    {S_Vert,       3,    1},
+    {S_Vert,       1,    0}
+};
+
+static const t4_table_entry_t t4_1d_white_table[4096] =
+{
+    {S_Null,       1,    0},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      6,   12},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    6, 1664},
+    {S_TermW,      4,    6},
+    {S_TermW,      7,   20},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      7,   24},
+    {S_TermW,      6,   14},
+    {S_TermW,      7,   28},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      7,   23},
+    {S_TermW,      4,    3},
+    {S_TermW,      7,   27},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   39},
+    {S_TermW,      6,   16},
+    {S_MakeUpW,    8,  576},
+    {S_TermW,      4,    6},
+    {S_TermW,      7,   19},
+    {S_TermW,      5,    8},
+    {S_TermW,      8,   55},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   45},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   53},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    8,  448},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   35},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      8,   51},
+    {S_TermW,      6,   15},
+    {S_TermW,      8,   63},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      6,   13},
+    {S_TermW,      4,    3},
+    {S_MakeUpW,    9, 1472},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   43},
+    {S_TermW,      6,   17},
+    {S_MakeUpW,    9, 1216},
+    {S_TermW,      4,    6},
+    {S_TermW,      6,    1},
+    {S_TermW,      5,    8},
+    {S_MakeUpW,    6,  192},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   29},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      6,   12},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    6, 1664},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   33},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      8,   49},
+    {S_TermW,      6,   14},
+    {S_TermW,      8,   61},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   47},
+    {S_TermW,      4,    3},
+    {S_TermW,      8,   59},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   41},
+    {S_TermW,      6,   16},
+    {S_MakeUpW,    9,  960},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   31},
+    {S_TermW,      5,    8},
+    {S_TermW,      8,   57},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      7,   22},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      7,   26},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    9,  704},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   37},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      7,   25},
+    {S_TermW,      6,   15},
+    {S_MakeUpW,    8,  320},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      6,   13},
+    {S_TermW,      4,    3},
+    {S_TermW,      7,   18},
+    {S_TermW,      4,    5},
+    {S_TermW,      7,   21},
+    {S_TermW,      6,   17},
+    {S_MakeUpW,    7,  256},
+    {S_TermW,      4,    6},
+    {S_TermW,      6,    1},
+    {S_TermW,      5,    8},
+    {S_MakeUpW,    6,  192},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_MakeUp,    11, 1792},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      6,   12},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    6, 1664},
+    {S_TermW,      4,    6},
+    {S_TermW,      7,   20},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      7,   24},
+    {S_TermW,      6,   14},
+    {S_TermW,      7,   28},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      7,   23},
+    {S_TermW,      4,    3},
+    {S_TermW,      7,   27},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   40},
+    {S_TermW,      6,   16},
+    {S_MakeUpW,    9,  832},
+    {S_TermW,      4,    6},
+    {S_TermW,      7,   19},
+    {S_TermW,      5,    8},
+    {S_TermW,      8,   56},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   46},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   54},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    8,  512},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   36},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      8,   52},
+    {S_TermW,      6,   15},
+    {S_TermW,      8,    0},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      6,   13},
+    {S_TermW,      4,    3},
+    {S_MakeUpW,    9, 1600},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   44},
+    {S_TermW,      6,   17},
+    {S_MakeUpW,    9, 1344},
+    {S_TermW,      4,    6},
+    {S_TermW,      6,    1},
+    {S_TermW,      5,    8},
+    {S_MakeUpW,    6,  192},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   30},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      6,   12},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    6, 1664},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   34},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      8,   50},
+    {S_TermW,      6,   14},
+    {S_TermW,      8,   62},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   48},
+    {S_TermW,      4,    3},
+    {S_TermW,      8,   60},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   42},
+    {S_TermW,      6,   16},
+    {S_MakeUpW,    9, 1088},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   32},
+    {S_TermW,      5,    8},
+    {S_TermW,      8,   58},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      7,   22},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      7,   26},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    8,  640},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   38},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      7,   25},
+    {S_TermW,      6,   15},
+    {S_MakeUpW,    8,  384},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      6,   13},
+    {S_TermW,      4,    3},
+    {S_TermW,      7,   18},
+    {S_TermW,      4,    5},
+    {S_TermW,      7,   21},
+    {S_TermW,      6,   17},
+    {S_MakeUpW,    7,  256},
+    {S_TermW,      4,    6},
+    {S_TermW,      6,    1},
+    {S_TermW,      5,    8},
+    {S_MakeUpW,    6,  192},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_Null,       1,    0},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      6,   12},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    6, 1664},
+    {S_TermW,      4,    6},
+    {S_TermW,      7,   20},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      7,   24},
+    {S_TermW,      6,   14},
+    {S_TermW,      7,   28},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      7,   23},
+    {S_TermW,      4,    3},
+    {S_TermW,      7,   27},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   39},
+    {S_TermW,      6,   16},
+    {S_MakeUpW,    8,  576},
+    {S_TermW,      4,    6},
+    {S_TermW,      7,   19},
+    {S_TermW,      5,    8},
+    {S_TermW,      8,   55},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   45},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   53},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    8,  448},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   35},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      8,   51},
+    {S_TermW,      6,   15},
+    {S_TermW,      8,   63},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      6,   13},
+    {S_TermW,      4,    3},
+    {S_MakeUpW,    9, 1536},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   43},
+    {S_TermW,      6,   17},
+    {S_MakeUpW,    9, 1280},
+    {S_TermW,      4,    6},
+    {S_TermW,      6,    1},
+    {S_TermW,      5,    8},
+    {S_MakeUpW,    6,  192},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   29},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      6,   12},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    6, 1664},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   33},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      8,   49},
+    {S_TermW,      6,   14},
+    {S_TermW,      8,   61},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   47},
+    {S_TermW,      4,    3},
+    {S_TermW,      8,   59},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   41},
+    {S_TermW,      6,   16},
+    {S_MakeUpW,    9, 1024},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   31},
+    {S_TermW,      5,    8},
+    {S_TermW,      8,   57},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      7,   22},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      7,   26},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    9,  768},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   37},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      7,   25},
+    {S_TermW,      6,   15},
+    {S_MakeUpW,    8,  320},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      6,   13},
+    {S_TermW,      4,    3},
+    {S_TermW,      7,   18},
+    {S_TermW,      4,    5},
+    {S_TermW,      7,   21},
+    {S_TermW,      6,   17},
+    {S_MakeUpW,    7,  256},
+    {S_TermW,      4,    6},
+    {S_TermW,      6,    1},
+    {S_TermW,      5,    8},
+    {S_MakeUpW,    6,  192},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_MakeUp,    11, 1856},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      6,   12},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    6, 1664},
+    {S_TermW,      4,    6},
+    {S_TermW,      7,   20},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      7,   24},
+    {S_TermW,      6,   14},
+    {S_TermW,      7,   28},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      7,   23},
+    {S_TermW,      4,    3},
+    {S_TermW,      7,   27},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   40},
+    {S_TermW,      6,   16},
+    {S_MakeUpW,    9,  896},
+    {S_TermW,      4,    6},
+    {S_TermW,      7,   19},
+    {S_TermW,      5,    8},
+    {S_TermW,      8,   56},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   46},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   54},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    8,  512},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   36},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      8,   52},
+    {S_TermW,      6,   15},
+    {S_TermW,      8,    0},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      6,   13},
+    {S_TermW,      4,    3},
+    {S_MakeUpW,    9, 1728},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   44},
+    {S_TermW,      6,   17},
+    {S_MakeUpW,    9, 1408},
+    {S_TermW,      4,    6},
+    {S_TermW,      6,    1},
+    {S_TermW,      5,    8},
+    {S_MakeUpW,    6,  192},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   30},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      6,   12},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    6, 1664},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   34},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      8,   50},
+    {S_TermW,      6,   14},
+    {S_TermW,      8,   62},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   48},
+    {S_TermW,      4,    3},
+    {S_TermW,      8,   60},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   42},
+    {S_TermW,      6,   16},
+    {S_MakeUpW,    9, 1152},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   32},
+    {S_TermW,      5,    8},
+    {S_TermW,      8,   58},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      7,   22},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      7,   26},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    8,  640},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   38},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      7,   25},
+    {S_TermW,      6,   15},
+    {S_MakeUpW,    8,  384},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      6,   13},
+    {S_TermW,      4,    3},
+    {S_TermW,      7,   18},
+    {S_TermW,      4,    5},
+    {S_TermW,      7,   21},
+    {S_TermW,      6,   17},
+    {S_MakeUpW,    7,  256},
+    {S_TermW,      4,    6},
+    {S_TermW,      6,    1},
+    {S_TermW,      5,    8},
+    {S_MakeUpW,    6,  192},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_Null,       1,    0},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      6,   12},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    6, 1664},
+    {S_TermW,      4,    6},
+    {S_TermW,      7,   20},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      7,   24},
+    {S_TermW,      6,   14},
+    {S_TermW,      7,   28},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      7,   23},
+    {S_TermW,      4,    3},
+    {S_TermW,      7,   27},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   39},
+    {S_TermW,      6,   16},
+    {S_MakeUpW,    8,  576},
+    {S_TermW,      4,    6},
+    {S_TermW,      7,   19},
+    {S_TermW,      5,    8},
+    {S_TermW,      8,   55},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   45},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   53},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    8,  448},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   35},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      8,   51},
+    {S_TermW,      6,   15},
+    {S_TermW,      8,   63},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      6,   13},
+    {S_TermW,      4,    3},
+    {S_MakeUpW,    9, 1472},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   43},
+    {S_TermW,      6,   17},
+    {S_MakeUpW,    9, 1216},
+    {S_TermW,      4,    6},
+    {S_TermW,      6,    1},
+    {S_TermW,      5,    8},
+    {S_MakeUpW,    6,  192},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   29},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      6,   12},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    6, 1664},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   33},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      8,   49},
+    {S_TermW,      6,   14},
+    {S_TermW,      8,   61},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   47},
+    {S_TermW,      4,    3},
+    {S_TermW,      8,   59},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   41},
+    {S_TermW,      6,   16},
+    {S_MakeUpW,    9,  960},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   31},
+    {S_TermW,      5,    8},
+    {S_TermW,      8,   57},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      7,   22},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      7,   26},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    9,  704},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   37},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      7,   25},
+    {S_TermW,      6,   15},
+    {S_MakeUpW,    8,  320},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      6,   13},
+    {S_TermW,      4,    3},
+    {S_TermW,      7,   18},
+    {S_TermW,      4,    5},
+    {S_TermW,      7,   21},
+    {S_TermW,      6,   17},
+    {S_MakeUpW,    7,  256},
+    {S_TermW,      4,    6},
+    {S_TermW,      6,    1},
+    {S_TermW,      5,    8},
+    {S_MakeUpW,    6,  192},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_MakeUp,    12, 2112},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      6,   12},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    6, 1664},
+    {S_TermW,      4,    6},
+    {S_TermW,      7,   20},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      7,   24},
+    {S_TermW,      6,   14},
+    {S_TermW,      7,   28},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      7,   23},
+    {S_TermW,      4,    3},
+    {S_TermW,      7,   27},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   40},
+    {S_TermW,      6,   16},
+    {S_MakeUpW,    9,  832},
+    {S_TermW,      4,    6},
+    {S_TermW,      7,   19},
+    {S_TermW,      5,    8},
+    {S_TermW,      8,   56},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   46},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   54},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    8,  512},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   36},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      8,   52},
+    {S_TermW,      6,   15},
+    {S_TermW,      8,    0},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      6,   13},
+    {S_TermW,      4,    3},
+    {S_MakeUpW,    9, 1600},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   44},
+    {S_TermW,      6,   17},
+    {S_MakeUpW,    9, 1344},
+    {S_TermW,      4,    6},
+    {S_TermW,      6,    1},
+    {S_TermW,      5,    8},
+    {S_MakeUpW,    6,  192},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   30},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      6,   12},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    6, 1664},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   34},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      8,   50},
+    {S_TermW,      6,   14},
+    {S_TermW,      8,   62},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   48},
+    {S_TermW,      4,    3},
+    {S_TermW,      8,   60},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   42},
+    {S_TermW,      6,   16},
+    {S_MakeUpW,    9, 1088},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   32},
+    {S_TermW,      5,    8},
+    {S_TermW,      8,   58},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      7,   22},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      7,   26},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    8,  640},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   38},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      7,   25},
+    {S_TermW,      6,   15},
+    {S_MakeUpW,    8,  384},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      6,   13},
+    {S_TermW,      4,    3},
+    {S_TermW,      7,   18},
+    {S_TermW,      4,    5},
+    {S_TermW,      7,   21},
+    {S_TermW,      6,   17},
+    {S_MakeUpW,    7,  256},
+    {S_TermW,      4,    6},
+    {S_TermW,      6,    1},
+    {S_TermW,      5,    8},
+    {S_MakeUpW,    6,  192},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_Null,       1,    0},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      6,   12},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    6, 1664},
+    {S_TermW,      4,    6},
+    {S_TermW,      7,   20},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      7,   24},
+    {S_TermW,      6,   14},
+    {S_TermW,      7,   28},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      7,   23},
+    {S_TermW,      4,    3},
+    {S_TermW,      7,   27},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   39},
+    {S_TermW,      6,   16},
+    {S_MakeUpW,    8,  576},
+    {S_TermW,      4,    6},
+    {S_TermW,      7,   19},
+    {S_TermW,      5,    8},
+    {S_TermW,      8,   55},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   45},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   53},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    8,  448},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   35},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      8,   51},
+    {S_TermW,      6,   15},
+    {S_TermW,      8,   63},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      6,   13},
+    {S_TermW,      4,    3},
+    {S_MakeUpW,    9, 1536},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   43},
+    {S_TermW,      6,   17},
+    {S_MakeUpW,    9, 1280},
+    {S_TermW,      4,    6},
+    {S_TermW,      6,    1},
+    {S_TermW,      5,    8},
+    {S_MakeUpW,    6,  192},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   29},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      6,   12},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    6, 1664},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   33},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      8,   49},
+    {S_TermW,      6,   14},
+    {S_TermW,      8,   61},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   47},
+    {S_TermW,      4,    3},
+    {S_TermW,      8,   59},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   41},
+    {S_TermW,      6,   16},
+    {S_MakeUpW,    9, 1024},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   31},
+    {S_TermW,      5,    8},
+    {S_TermW,      8,   57},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      7,   22},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      7,   26},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    9,  768},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   37},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      7,   25},
+    {S_TermW,      6,   15},
+    {S_MakeUpW,    8,  320},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      6,   13},
+    {S_TermW,      4,    3},
+    {S_TermW,      7,   18},
+    {S_TermW,      4,    5},
+    {S_TermW,      7,   21},
+    {S_TermW,      6,   17},
+    {S_MakeUpW,    7,  256},
+    {S_TermW,      4,    6},
+    {S_TermW,      6,    1},
+    {S_TermW,      5,    8},
+    {S_MakeUpW,    6,  192},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_MakeUp,    12, 2368},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      6,   12},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    6, 1664},
+    {S_TermW,      4,    6},
+    {S_TermW,      7,   20},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      7,   24},
+    {S_TermW,      6,   14},
+    {S_TermW,      7,   28},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      7,   23},
+    {S_TermW,      4,    3},
+    {S_TermW,      7,   27},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   40},
+    {S_TermW,      6,   16},
+    {S_MakeUpW,    9,  896},
+    {S_TermW,      4,    6},
+    {S_TermW,      7,   19},
+    {S_TermW,      5,    8},
+    {S_TermW,      8,   56},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   46},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   54},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    8,  512},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   36},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      8,   52},
+    {S_TermW,      6,   15},
+    {S_TermW,      8,    0},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      6,   13},
+    {S_TermW,      4,    3},
+    {S_MakeUpW,    9, 1728},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   44},
+    {S_TermW,      6,   17},
+    {S_MakeUpW,    9, 1408},
+    {S_TermW,      4,    6},
+    {S_TermW,      6,    1},
+    {S_TermW,      5,    8},
+    {S_MakeUpW,    6,  192},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   30},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      6,   12},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    6, 1664},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   34},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      8,   50},
+    {S_TermW,      6,   14},
+    {S_TermW,      8,   62},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   48},
+    {S_TermW,      4,    3},
+    {S_TermW,      8,   60},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   42},
+    {S_TermW,      6,   16},
+    {S_MakeUpW,    9, 1152},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   32},
+    {S_TermW,      5,    8},
+    {S_TermW,      8,   58},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      7,   22},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      7,   26},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    8,  640},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   38},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      7,   25},
+    {S_TermW,      6,   15},
+    {S_MakeUpW,    8,  384},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      6,   13},
+    {S_TermW,      4,    3},
+    {S_TermW,      7,   18},
+    {S_TermW,      4,    5},
+    {S_TermW,      7,   21},
+    {S_TermW,      6,   17},
+    {S_MakeUpW,    7,  256},
+    {S_TermW,      4,    6},
+    {S_TermW,      6,    1},
+    {S_TermW,      5,    8},
+    {S_MakeUpW,    6,  192},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_Null,       1,    0},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      6,   12},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    6, 1664},
+    {S_TermW,      4,    6},
+    {S_TermW,      7,   20},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      7,   24},
+    {S_TermW,      6,   14},
+    {S_TermW,      7,   28},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      7,   23},
+    {S_TermW,      4,    3},
+    {S_TermW,      7,   27},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   39},
+    {S_TermW,      6,   16},
+    {S_MakeUpW,    8,  576},
+    {S_TermW,      4,    6},
+    {S_TermW,      7,   19},
+    {S_TermW,      5,    8},
+    {S_TermW,      8,   55},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   45},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   53},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    8,  448},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   35},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      8,   51},
+    {S_TermW,      6,   15},
+    {S_TermW,      8,   63},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      6,   13},
+    {S_TermW,      4,    3},
+    {S_MakeUpW,    9, 1472},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   43},
+    {S_TermW,      6,   17},
+    {S_MakeUpW,    9, 1216},
+    {S_TermW,      4,    6},
+    {S_TermW,      6,    1},
+    {S_TermW,      5,    8},
+    {S_MakeUpW,    6,  192},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   29},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      6,   12},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    6, 1664},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   33},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      8,   49},
+    {S_TermW,      6,   14},
+    {S_TermW,      8,   61},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   47},
+    {S_TermW,      4,    3},
+    {S_TermW,      8,   59},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   41},
+    {S_TermW,      6,   16},
+    {S_MakeUpW,    9,  960},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   31},
+    {S_TermW,      5,    8},
+    {S_TermW,      8,   57},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      7,   22},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      7,   26},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    9,  704},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   37},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      7,   25},
+    {S_TermW,      6,   15},
+    {S_MakeUpW,    8,  320},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      6,   13},
+    {S_TermW,      4,    3},
+    {S_TermW,      7,   18},
+    {S_TermW,      4,    5},
+    {S_TermW,      7,   21},
+    {S_TermW,      6,   17},
+    {S_MakeUpW,    7,  256},
+    {S_TermW,      4,    6},
+    {S_TermW,      6,    1},
+    {S_TermW,      5,    8},
+    {S_MakeUpW,    6,  192},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_MakeUp,    12, 1984},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      6,   12},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    6, 1664},
+    {S_TermW,      4,    6},
+    {S_TermW,      7,   20},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      7,   24},
+    {S_TermW,      6,   14},
+    {S_TermW,      7,   28},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      7,   23},
+    {S_TermW,      4,    3},
+    {S_TermW,      7,   27},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   40},
+    {S_TermW,      6,   16},
+    {S_MakeUpW,    9,  832},
+    {S_TermW,      4,    6},
+    {S_TermW,      7,   19},
+    {S_TermW,      5,    8},
+    {S_TermW,      8,   56},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   46},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   54},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    8,  512},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   36},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      8,   52},
+    {S_TermW,      6,   15},
+    {S_TermW,      8,    0},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      6,   13},
+    {S_TermW,      4,    3},
+    {S_MakeUpW,    9, 1600},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   44},
+    {S_TermW,      6,   17},
+    {S_MakeUpW,    9, 1344},
+    {S_TermW,      4,    6},
+    {S_TermW,      6,    1},
+    {S_TermW,      5,    8},
+    {S_MakeUpW,    6,  192},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   30},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      6,   12},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    6, 1664},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   34},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      8,   50},
+    {S_TermW,      6,   14},
+    {S_TermW,      8,   62},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   48},
+    {S_TermW,      4,    3},
+    {S_TermW,      8,   60},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   42},
+    {S_TermW,      6,   16},
+    {S_MakeUpW,    9, 1088},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   32},
+    {S_TermW,      5,    8},
+    {S_TermW,      8,   58},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      7,   22},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      7,   26},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    8,  640},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   38},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      7,   25},
+    {S_TermW,      6,   15},
+    {S_MakeUpW,    8,  384},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      6,   13},
+    {S_TermW,      4,    3},
+    {S_TermW,      7,   18},
+    {S_TermW,      4,    5},
+    {S_TermW,      7,   21},
+    {S_TermW,      6,   17},
+    {S_MakeUpW,    7,  256},
+    {S_TermW,      4,    6},
+    {S_TermW,      6,    1},
+    {S_TermW,      5,    8},
+    {S_MakeUpW,    6,  192},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_Null,       1,    0},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      6,   12},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    6, 1664},
+    {S_TermW,      4,    6},
+    {S_TermW,      7,   20},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      7,   24},
+    {S_TermW,      6,   14},
+    {S_TermW,      7,   28},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      7,   23},
+    {S_TermW,      4,    3},
+    {S_TermW,      7,   27},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   39},
+    {S_TermW,      6,   16},
+    {S_MakeUpW,    8,  576},
+    {S_TermW,      4,    6},
+    {S_TermW,      7,   19},
+    {S_TermW,      5,    8},
+    {S_TermW,      8,   55},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   45},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   53},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    8,  448},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   35},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      8,   51},
+    {S_TermW,      6,   15},
+    {S_TermW,      8,   63},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      6,   13},
+    {S_TermW,      4,    3},
+    {S_MakeUpW,    9, 1536},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   43},
+    {S_TermW,      6,   17},
+    {S_MakeUpW,    9, 1280},
+    {S_TermW,      4,    6},
+    {S_TermW,      6,    1},
+    {S_TermW,      5,    8},
+    {S_MakeUpW,    6,  192},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   29},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      6,   12},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    6, 1664},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   33},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      8,   49},
+    {S_TermW,      6,   14},
+    {S_TermW,      8,   61},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   47},
+    {S_TermW,      4,    3},
+    {S_TermW,      8,   59},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   41},
+    {S_TermW,      6,   16},
+    {S_MakeUpW,    9, 1024},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   31},
+    {S_TermW,      5,    8},
+    {S_TermW,      8,   57},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      7,   22},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      7,   26},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    9,  768},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   37},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      7,   25},
+    {S_TermW,      6,   15},
+    {S_MakeUpW,    8,  320},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      6,   13},
+    {S_TermW,      4,    3},
+    {S_TermW,      7,   18},
+    {S_TermW,      4,    5},
+    {S_TermW,      7,   21},
+    {S_TermW,      6,   17},
+    {S_MakeUpW,    7,  256},
+    {S_TermW,      4,    6},
+    {S_TermW,      6,    1},
+    {S_TermW,      5,    8},
+    {S_MakeUpW,    6,  192},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_MakeUp,    11, 1920},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      6,   12},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    6, 1664},
+    {S_TermW,      4,    6},
+    {S_TermW,      7,   20},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      7,   24},
+    {S_TermW,      6,   14},
+    {S_TermW,      7,   28},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      7,   23},
+    {S_TermW,      4,    3},
+    {S_TermW,      7,   27},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   40},
+    {S_TermW,      6,   16},
+    {S_MakeUpW,    9,  896},
+    {S_TermW,      4,    6},
+    {S_TermW,      7,   19},
+    {S_TermW,      5,    8},
+    {S_TermW,      8,   56},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   46},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   54},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    8,  512},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   36},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      8,   52},
+    {S_TermW,      6,   15},
+    {S_TermW,      8,    0},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      6,   13},
+    {S_TermW,      4,    3},
+    {S_MakeUpW,    9, 1728},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   44},
+    {S_TermW,      6,   17},
+    {S_MakeUpW,    9, 1408},
+    {S_TermW,      4,    6},
+    {S_TermW,      6,    1},
+    {S_TermW,      5,    8},
+    {S_MakeUpW,    6,  192},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   30},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      6,   12},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    6, 1664},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   34},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      8,   50},
+    {S_TermW,      6,   14},
+    {S_TermW,      8,   62},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   48},
+    {S_TermW,      4,    3},
+    {S_TermW,      8,   60},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   42},
+    {S_TermW,      6,   16},
+    {S_MakeUpW,    9, 1152},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   32},
+    {S_TermW,      5,    8},
+    {S_TermW,      8,   58},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      7,   22},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      7,   26},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    8,  640},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   38},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      7,   25},
+    {S_TermW,      6,   15},
+    {S_MakeUpW,    8,  384},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      6,   13},
+    {S_TermW,      4,    3},
+    {S_TermW,      7,   18},
+    {S_TermW,      4,    5},
+    {S_TermW,      7,   21},
+    {S_TermW,      6,   17},
+    {S_MakeUpW,    7,  256},
+    {S_TermW,      4,    6},
+    {S_TermW,      6,    1},
+    {S_TermW,      5,    8},
+    {S_MakeUpW,    6,  192},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_Null,       1,    0},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      6,   12},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    6, 1664},
+    {S_TermW,      4,    6},
+    {S_TermW,      7,   20},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      7,   24},
+    {S_TermW,      6,   14},
+    {S_TermW,      7,   28},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      7,   23},
+    {S_TermW,      4,    3},
+    {S_TermW,      7,   27},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   39},
+    {S_TermW,      6,   16},
+    {S_MakeUpW,    8,  576},
+    {S_TermW,      4,    6},
+    {S_TermW,      7,   19},
+    {S_TermW,      5,    8},
+    {S_TermW,      8,   55},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   45},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   53},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    8,  448},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   35},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      8,   51},
+    {S_TermW,      6,   15},
+    {S_TermW,      8,   63},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      6,   13},
+    {S_TermW,      4,    3},
+    {S_MakeUpW,    9, 1472},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   43},
+    {S_TermW,      6,   17},
+    {S_MakeUpW,    9, 1216},
+    {S_TermW,      4,    6},
+    {S_TermW,      6,    1},
+    {S_TermW,      5,    8},
+    {S_MakeUpW,    6,  192},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   29},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      6,   12},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    6, 1664},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   33},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      8,   49},
+    {S_TermW,      6,   14},
+    {S_TermW,      8,   61},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   47},
+    {S_TermW,      4,    3},
+    {S_TermW,      8,   59},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   41},
+    {S_TermW,      6,   16},
+    {S_MakeUpW,    9,  960},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   31},
+    {S_TermW,      5,    8},
+    {S_TermW,      8,   57},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      7,   22},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      7,   26},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    9,  704},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   37},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      7,   25},
+    {S_TermW,      6,   15},
+    {S_MakeUpW,    8,  320},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      6,   13},
+    {S_TermW,      4,    3},
+    {S_TermW,      7,   18},
+    {S_TermW,      4,    5},
+    {S_TermW,      7,   21},
+    {S_TermW,      6,   17},
+    {S_MakeUpW,    7,  256},
+    {S_TermW,      4,    6},
+    {S_TermW,      6,    1},
+    {S_TermW,      5,    8},
+    {S_MakeUpW,    6,  192},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_MakeUp,    12, 2240},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      6,   12},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    6, 1664},
+    {S_TermW,      4,    6},
+    {S_TermW,      7,   20},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      7,   24},
+    {S_TermW,      6,   14},
+    {S_TermW,      7,   28},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      7,   23},
+    {S_TermW,      4,    3},
+    {S_TermW,      7,   27},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   40},
+    {S_TermW,      6,   16},
+    {S_MakeUpW,    9,  832},
+    {S_TermW,      4,    6},
+    {S_TermW,      7,   19},
+    {S_TermW,      5,    8},
+    {S_TermW,      8,   56},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   46},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   54},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    8,  512},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   36},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      8,   52},
+    {S_TermW,      6,   15},
+    {S_TermW,      8,    0},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      6,   13},
+    {S_TermW,      4,    3},
+    {S_MakeUpW,    9, 1600},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   44},
+    {S_TermW,      6,   17},
+    {S_MakeUpW,    9, 1344},
+    {S_TermW,      4,    6},
+    {S_TermW,      6,    1},
+    {S_TermW,      5,    8},
+    {S_MakeUpW,    6,  192},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   30},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      6,   12},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    6, 1664},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   34},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      8,   50},
+    {S_TermW,      6,   14},
+    {S_TermW,      8,   62},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   48},
+    {S_TermW,      4,    3},
+    {S_TermW,      8,   60},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   42},
+    {S_TermW,      6,   16},
+    {S_MakeUpW,    9, 1088},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   32},
+    {S_TermW,      5,    8},
+    {S_TermW,      8,   58},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      7,   22},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      7,   26},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    8,  640},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   38},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      7,   25},
+    {S_TermW,      6,   15},
+    {S_MakeUpW,    8,  384},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      6,   13},
+    {S_TermW,      4,    3},
+    {S_TermW,      7,   18},
+    {S_TermW,      4,    5},
+    {S_TermW,      7,   21},
+    {S_TermW,      6,   17},
+    {S_MakeUpW,    7,  256},
+    {S_TermW,      4,    6},
+    {S_TermW,      6,    1},
+    {S_TermW,      5,    8},
+    {S_MakeUpW,    6,  192},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_Null,       1,    0},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      6,   12},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    6, 1664},
+    {S_TermW,      4,    6},
+    {S_TermW,      7,   20},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      7,   24},
+    {S_TermW,      6,   14},
+    {S_TermW,      7,   28},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      7,   23},
+    {S_TermW,      4,    3},
+    {S_TermW,      7,   27},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   39},
+    {S_TermW,      6,   16},
+    {S_MakeUpW,    8,  576},
+    {S_TermW,      4,    6},
+    {S_TermW,      7,   19},
+    {S_TermW,      5,    8},
+    {S_TermW,      8,   55},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   45},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   53},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    8,  448},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   35},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      8,   51},
+    {S_TermW,      6,   15},
+    {S_TermW,      8,   63},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      6,   13},
+    {S_TermW,      4,    3},
+    {S_MakeUpW,    9, 1536},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   43},
+    {S_TermW,      6,   17},
+    {S_MakeUpW,    9, 1280},
+    {S_TermW,      4,    6},
+    {S_TermW,      6,    1},
+    {S_TermW,      5,    8},
+    {S_MakeUpW,    6,  192},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   29},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      6,   12},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    6, 1664},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   33},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      8,   49},
+    {S_TermW,      6,   14},
+    {S_TermW,      8,   61},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   47},
+    {S_TermW,      4,    3},
+    {S_TermW,      8,   59},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   41},
+    {S_TermW,      6,   16},
+    {S_MakeUpW,    9, 1024},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   31},
+    {S_TermW,      5,    8},
+    {S_TermW,      8,   57},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      7,   22},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      7,   26},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    9,  768},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   37},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      7,   25},
+    {S_TermW,      6,   15},
+    {S_MakeUpW,    8,  320},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      6,   13},
+    {S_TermW,      4,    3},
+    {S_TermW,      7,   18},
+    {S_TermW,      4,    5},
+    {S_TermW,      7,   21},
+    {S_TermW,      6,   17},
+    {S_MakeUpW,    7,  256},
+    {S_TermW,      4,    6},
+    {S_TermW,      6,    1},
+    {S_TermW,      5,    8},
+    {S_MakeUpW,    6,  192},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_MakeUp,    12, 2496},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      6,   12},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    6, 1664},
+    {S_TermW,      4,    6},
+    {S_TermW,      7,   20},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      7,   24},
+    {S_TermW,      6,   14},
+    {S_TermW,      7,   28},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      7,   23},
+    {S_TermW,      4,    3},
+    {S_TermW,      7,   27},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   40},
+    {S_TermW,      6,   16},
+    {S_MakeUpW,    9,  896},
+    {S_TermW,      4,    6},
+    {S_TermW,      7,   19},
+    {S_TermW,      5,    8},
+    {S_TermW,      8,   56},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   46},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   54},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    8,  512},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   36},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      8,   52},
+    {S_TermW,      6,   15},
+    {S_TermW,      8,    0},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      6,   13},
+    {S_TermW,      4,    3},
+    {S_MakeUpW,    9, 1728},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   44},
+    {S_TermW,      6,   17},
+    {S_MakeUpW,    9, 1408},
+    {S_TermW,      4,    6},
+    {S_TermW,      6,    1},
+    {S_TermW,      5,    8},
+    {S_MakeUpW,    6,  192},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   30},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      6,   12},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    6, 1664},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   34},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      8,   50},
+    {S_TermW,      6,   14},
+    {S_TermW,      8,   62},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   48},
+    {S_TermW,      4,    3},
+    {S_TermW,      8,   60},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   42},
+    {S_TermW,      6,   16},
+    {S_MakeUpW,    9, 1152},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   32},
+    {S_TermW,      5,    8},
+    {S_TermW,      8,   58},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      7,   22},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      7,   26},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    8,  640},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   38},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      7,   25},
+    {S_TermW,      6,   15},
+    {S_MakeUpW,    8,  384},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      6,   13},
+    {S_TermW,      4,    3},
+    {S_TermW,      7,   18},
+    {S_TermW,      4,    5},
+    {S_TermW,      7,   21},
+    {S_TermW,      6,   17},
+    {S_MakeUpW,    7,  256},
+    {S_TermW,      4,    6},
+    {S_TermW,      6,    1},
+    {S_TermW,      5,    8},
+    {S_MakeUpW,    6,  192},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_EOL,       12,    0},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      6,   12},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    6, 1664},
+    {S_TermW,      4,    6},
+    {S_TermW,      7,   20},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      7,   24},
+    {S_TermW,      6,   14},
+    {S_TermW,      7,   28},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      7,   23},
+    {S_TermW,      4,    3},
+    {S_TermW,      7,   27},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   39},
+    {S_TermW,      6,   16},
+    {S_MakeUpW,    8,  576},
+    {S_TermW,      4,    6},
+    {S_TermW,      7,   19},
+    {S_TermW,      5,    8},
+    {S_TermW,      8,   55},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   45},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   53},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    8,  448},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   35},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      8,   51},
+    {S_TermW,      6,   15},
+    {S_TermW,      8,   63},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      6,   13},
+    {S_TermW,      4,    3},
+    {S_MakeUpW,    9, 1472},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   43},
+    {S_TermW,      6,   17},
+    {S_MakeUpW,    9, 1216},
+    {S_TermW,      4,    6},
+    {S_TermW,      6,    1},
+    {S_TermW,      5,    8},
+    {S_MakeUpW,    6,  192},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   29},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      6,   12},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    6, 1664},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   33},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      8,   49},
+    {S_TermW,      6,   14},
+    {S_TermW,      8,   61},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   47},
+    {S_TermW,      4,    3},
+    {S_TermW,      8,   59},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   41},
+    {S_TermW,      6,   16},
+    {S_MakeUpW,    9,  960},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   31},
+    {S_TermW,      5,    8},
+    {S_TermW,      8,   57},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      7,   22},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      7,   26},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    9,  704},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   37},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      7,   25},
+    {S_TermW,      6,   15},
+    {S_MakeUpW,    8,  320},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      6,   13},
+    {S_TermW,      4,    3},
+    {S_TermW,      7,   18},
+    {S_TermW,      4,    5},
+    {S_TermW,      7,   21},
+    {S_TermW,      6,   17},
+    {S_MakeUpW,    7,  256},
+    {S_TermW,      4,    6},
+    {S_TermW,      6,    1},
+    {S_TermW,      5,    8},
+    {S_MakeUpW,    6,  192},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_MakeUp,    11, 1792},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      6,   12},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    6, 1664},
+    {S_TermW,      4,    6},
+    {S_TermW,      7,   20},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      7,   24},
+    {S_TermW,      6,   14},
+    {S_TermW,      7,   28},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      7,   23},
+    {S_TermW,      4,    3},
+    {S_TermW,      7,   27},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   40},
+    {S_TermW,      6,   16},
+    {S_MakeUpW,    9,  832},
+    {S_TermW,      4,    6},
+    {S_TermW,      7,   19},
+    {S_TermW,      5,    8},
+    {S_TermW,      8,   56},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   46},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   54},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    8,  512},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   36},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      8,   52},
+    {S_TermW,      6,   15},
+    {S_TermW,      8,    0},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      6,   13},
+    {S_TermW,      4,    3},
+    {S_MakeUpW,    9, 1600},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   44},
+    {S_TermW,      6,   17},
+    {S_MakeUpW,    9, 1344},
+    {S_TermW,      4,    6},
+    {S_TermW,      6,    1},
+    {S_TermW,      5,    8},
+    {S_MakeUpW,    6,  192},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   30},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      6,   12},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    6, 1664},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   34},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      8,   50},
+    {S_TermW,      6,   14},
+    {S_TermW,      8,   62},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   48},
+    {S_TermW,      4,    3},
+    {S_TermW,      8,   60},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   42},
+    {S_TermW,      6,   16},
+    {S_MakeUpW,    9, 1088},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   32},
+    {S_TermW,      5,    8},
+    {S_TermW,      8,   58},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      7,   22},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      7,   26},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    8,  640},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   38},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      7,   25},
+    {S_TermW,      6,   15},
+    {S_MakeUpW,    8,  384},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      6,   13},
+    {S_TermW,      4,    3},
+    {S_TermW,      7,   18},
+    {S_TermW,      4,    5},
+    {S_TermW,      7,   21},
+    {S_TermW,      6,   17},
+    {S_MakeUpW,    7,  256},
+    {S_TermW,      4,    6},
+    {S_TermW,      6,    1},
+    {S_TermW,      5,    8},
+    {S_MakeUpW,    6,  192},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_Null,       1,    0},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      6,   12},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    6, 1664},
+    {S_TermW,      4,    6},
+    {S_TermW,      7,   20},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      7,   24},
+    {S_TermW,      6,   14},
+    {S_TermW,      7,   28},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      7,   23},
+    {S_TermW,      4,    3},
+    {S_TermW,      7,   27},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   39},
+    {S_TermW,      6,   16},
+    {S_MakeUpW,    8,  576},
+    {S_TermW,      4,    6},
+    {S_TermW,      7,   19},
+    {S_TermW,      5,    8},
+    {S_TermW,      8,   55},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   45},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   53},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    8,  448},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   35},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      8,   51},
+    {S_TermW,      6,   15},
+    {S_TermW,      8,   63},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      6,   13},
+    {S_TermW,      4,    3},
+    {S_MakeUpW,    9, 1536},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   43},
+    {S_TermW,      6,   17},
+    {S_MakeUpW,    9, 1280},
+    {S_TermW,      4,    6},
+    {S_TermW,      6,    1},
+    {S_TermW,      5,    8},
+    {S_MakeUpW,    6,  192},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   29},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      6,   12},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    6, 1664},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   33},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      8,   49},
+    {S_TermW,      6,   14},
+    {S_TermW,      8,   61},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   47},
+    {S_TermW,      4,    3},
+    {S_TermW,      8,   59},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   41},
+    {S_TermW,      6,   16},
+    {S_MakeUpW,    9, 1024},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   31},
+    {S_TermW,      5,    8},
+    {S_TermW,      8,   57},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      7,   22},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      7,   26},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    9,  768},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   37},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      7,   25},
+    {S_TermW,      6,   15},
+    {S_MakeUpW,    8,  320},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      6,   13},
+    {S_TermW,      4,    3},
+    {S_TermW,      7,   18},
+    {S_TermW,      4,    5},
+    {S_TermW,      7,   21},
+    {S_TermW,      6,   17},
+    {S_MakeUpW,    7,  256},
+    {S_TermW,      4,    6},
+    {S_TermW,      6,    1},
+    {S_TermW,      5,    8},
+    {S_MakeUpW,    6,  192},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_MakeUp,    11, 1856},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      6,   12},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    6, 1664},
+    {S_TermW,      4,    6},
+    {S_TermW,      7,   20},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      7,   24},
+    {S_TermW,      6,   14},
+    {S_TermW,      7,   28},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      7,   23},
+    {S_TermW,      4,    3},
+    {S_TermW,      7,   27},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   40},
+    {S_TermW,      6,   16},
+    {S_MakeUpW,    9,  896},
+    {S_TermW,      4,    6},
+    {S_TermW,      7,   19},
+    {S_TermW,      5,    8},
+    {S_TermW,      8,   56},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   46},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   54},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    8,  512},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   36},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      8,   52},
+    {S_TermW,      6,   15},
+    {S_TermW,      8,    0},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      6,   13},
+    {S_TermW,      4,    3},
+    {S_MakeUpW,    9, 1728},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   44},
+    {S_TermW,      6,   17},
+    {S_MakeUpW,    9, 1408},
+    {S_TermW,      4,    6},
+    {S_TermW,      6,    1},
+    {S_TermW,      5,    8},
+    {S_MakeUpW,    6,  192},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   30},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      6,   12},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    6, 1664},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   34},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      8,   50},
+    {S_TermW,      6,   14},
+    {S_TermW,      8,   62},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   48},
+    {S_TermW,      4,    3},
+    {S_TermW,      8,   60},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   42},
+    {S_TermW,      6,   16},
+    {S_MakeUpW,    9, 1152},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   32},
+    {S_TermW,      5,    8},
+    {S_TermW,      8,   58},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      7,   22},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      7,   26},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    8,  640},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   38},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      7,   25},
+    {S_TermW,      6,   15},
+    {S_MakeUpW,    8,  384},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      6,   13},
+    {S_TermW,      4,    3},
+    {S_TermW,      7,   18},
+    {S_TermW,      4,    5},
+    {S_TermW,      7,   21},
+    {S_TermW,      6,   17},
+    {S_MakeUpW,    7,  256},
+    {S_TermW,      4,    6},
+    {S_TermW,      6,    1},
+    {S_TermW,      5,    8},
+    {S_MakeUpW,    6,  192},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_Null,       1,    0},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      6,   12},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    6, 1664},
+    {S_TermW,      4,    6},
+    {S_TermW,      7,   20},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      7,   24},
+    {S_TermW,      6,   14},
+    {S_TermW,      7,   28},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      7,   23},
+    {S_TermW,      4,    3},
+    {S_TermW,      7,   27},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   39},
+    {S_TermW,      6,   16},
+    {S_MakeUpW,    8,  576},
+    {S_TermW,      4,    6},
+    {S_TermW,      7,   19},
+    {S_TermW,      5,    8},
+    {S_TermW,      8,   55},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   45},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   53},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    8,  448},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   35},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      8,   51},
+    {S_TermW,      6,   15},
+    {S_TermW,      8,   63},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      6,   13},
+    {S_TermW,      4,    3},
+    {S_MakeUpW,    9, 1472},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   43},
+    {S_TermW,      6,   17},
+    {S_MakeUpW,    9, 1216},
+    {S_TermW,      4,    6},
+    {S_TermW,      6,    1},
+    {S_TermW,      5,    8},
+    {S_MakeUpW,    6,  192},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   29},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      6,   12},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    6, 1664},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   33},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      8,   49},
+    {S_TermW,      6,   14},
+    {S_TermW,      8,   61},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   47},
+    {S_TermW,      4,    3},
+    {S_TermW,      8,   59},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   41},
+    {S_TermW,      6,   16},
+    {S_MakeUpW,    9,  960},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   31},
+    {S_TermW,      5,    8},
+    {S_TermW,      8,   57},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      7,   22},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      7,   26},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    9,  704},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   37},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      7,   25},
+    {S_TermW,      6,   15},
+    {S_MakeUpW,    8,  320},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      6,   13},
+    {S_TermW,      4,    3},
+    {S_TermW,      7,   18},
+    {S_TermW,      4,    5},
+    {S_TermW,      7,   21},
+    {S_TermW,      6,   17},
+    {S_MakeUpW,    7,  256},
+    {S_TermW,      4,    6},
+    {S_TermW,      6,    1},
+    {S_TermW,      5,    8},
+    {S_MakeUpW,    6,  192},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_MakeUp,    12, 2176},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      6,   12},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    6, 1664},
+    {S_TermW,      4,    6},
+    {S_TermW,      7,   20},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      7,   24},
+    {S_TermW,      6,   14},
+    {S_TermW,      7,   28},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      7,   23},
+    {S_TermW,      4,    3},
+    {S_TermW,      7,   27},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   40},
+    {S_TermW,      6,   16},
+    {S_MakeUpW,    9,  832},
+    {S_TermW,      4,    6},
+    {S_TermW,      7,   19},
+    {S_TermW,      5,    8},
+    {S_TermW,      8,   56},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   46},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   54},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    8,  512},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   36},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      8,   52},
+    {S_TermW,      6,   15},
+    {S_TermW,      8,    0},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      6,   13},
+    {S_TermW,      4,    3},
+    {S_MakeUpW,    9, 1600},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   44},
+    {S_TermW,      6,   17},
+    {S_MakeUpW,    9, 1344},
+    {S_TermW,      4,    6},
+    {S_TermW,      6,    1},
+    {S_TermW,      5,    8},
+    {S_MakeUpW,    6,  192},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   30},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      6,   12},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    6, 1664},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   34},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      8,   50},
+    {S_TermW,      6,   14},
+    {S_TermW,      8,   62},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   48},
+    {S_TermW,      4,    3},
+    {S_TermW,      8,   60},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   42},
+    {S_TermW,      6,   16},
+    {S_MakeUpW,    9, 1088},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   32},
+    {S_TermW,      5,    8},
+    {S_TermW,      8,   58},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      7,   22},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      7,   26},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    8,  640},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   38},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      7,   25},
+    {S_TermW,      6,   15},
+    {S_MakeUpW,    8,  384},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      6,   13},
+    {S_TermW,      4,    3},
+    {S_TermW,      7,   18},
+    {S_TermW,      4,    5},
+    {S_TermW,      7,   21},
+    {S_TermW,      6,   17},
+    {S_MakeUpW,    7,  256},
+    {S_TermW,      4,    6},
+    {S_TermW,      6,    1},
+    {S_TermW,      5,    8},
+    {S_MakeUpW,    6,  192},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_Null,       1,    0},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      6,   12},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    6, 1664},
+    {S_TermW,      4,    6},
+    {S_TermW,      7,   20},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      7,   24},
+    {S_TermW,      6,   14},
+    {S_TermW,      7,   28},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      7,   23},
+    {S_TermW,      4,    3},
+    {S_TermW,      7,   27},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   39},
+    {S_TermW,      6,   16},
+    {S_MakeUpW,    8,  576},
+    {S_TermW,      4,    6},
+    {S_TermW,      7,   19},
+    {S_TermW,      5,    8},
+    {S_TermW,      8,   55},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   45},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   53},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    8,  448},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   35},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      8,   51},
+    {S_TermW,      6,   15},
+    {S_TermW,      8,   63},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      6,   13},
+    {S_TermW,      4,    3},
+    {S_MakeUpW,    9, 1536},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   43},
+    {S_TermW,      6,   17},
+    {S_MakeUpW,    9, 1280},
+    {S_TermW,      4,    6},
+    {S_TermW,      6,    1},
+    {S_TermW,      5,    8},
+    {S_MakeUpW,    6,  192},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   29},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      6,   12},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    6, 1664},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   33},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      8,   49},
+    {S_TermW,      6,   14},
+    {S_TermW,      8,   61},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   47},
+    {S_TermW,      4,    3},
+    {S_TermW,      8,   59},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   41},
+    {S_TermW,      6,   16},
+    {S_MakeUpW,    9, 1024},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   31},
+    {S_TermW,      5,    8},
+    {S_TermW,      8,   57},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      7,   22},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      7,   26},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    9,  768},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   37},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      7,   25},
+    {S_TermW,      6,   15},
+    {S_MakeUpW,    8,  320},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      6,   13},
+    {S_TermW,      4,    3},
+    {S_TermW,      7,   18},
+    {S_TermW,      4,    5},
+    {S_TermW,      7,   21},
+    {S_TermW,      6,   17},
+    {S_MakeUpW,    7,  256},
+    {S_TermW,      4,    6},
+    {S_TermW,      6,    1},
+    {S_TermW,      5,    8},
+    {S_MakeUpW,    6,  192},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_MakeUp,    12, 2432},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      6,   12},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    6, 1664},
+    {S_TermW,      4,    6},
+    {S_TermW,      7,   20},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      7,   24},
+    {S_TermW,      6,   14},
+    {S_TermW,      7,   28},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      7,   23},
+    {S_TermW,      4,    3},
+    {S_TermW,      7,   27},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   40},
+    {S_TermW,      6,   16},
+    {S_MakeUpW,    9,  896},
+    {S_TermW,      4,    6},
+    {S_TermW,      7,   19},
+    {S_TermW,      5,    8},
+    {S_TermW,      8,   56},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   46},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   54},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    8,  512},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   36},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      8,   52},
+    {S_TermW,      6,   15},
+    {S_TermW,      8,    0},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      6,   13},
+    {S_TermW,      4,    3},
+    {S_MakeUpW,    9, 1728},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   44},
+    {S_TermW,      6,   17},
+    {S_MakeUpW,    9, 1408},
+    {S_TermW,      4,    6},
+    {S_TermW,      6,    1},
+    {S_TermW,      5,    8},
+    {S_MakeUpW,    6,  192},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   30},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      6,   12},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    6, 1664},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   34},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      8,   50},
+    {S_TermW,      6,   14},
+    {S_TermW,      8,   62},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   48},
+    {S_TermW,      4,    3},
+    {S_TermW,      8,   60},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   42},
+    {S_TermW,      6,   16},
+    {S_MakeUpW,    9, 1152},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   32},
+    {S_TermW,      5,    8},
+    {S_TermW,      8,   58},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      7,   22},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      7,   26},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    8,  640},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   38},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      7,   25},
+    {S_TermW,      6,   15},
+    {S_MakeUpW,    8,  384},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      6,   13},
+    {S_TermW,      4,    3},
+    {S_TermW,      7,   18},
+    {S_TermW,      4,    5},
+    {S_TermW,      7,   21},
+    {S_TermW,      6,   17},
+    {S_MakeUpW,    7,  256},
+    {S_TermW,      4,    6},
+    {S_TermW,      6,    1},
+    {S_TermW,      5,    8},
+    {S_MakeUpW,    6,  192},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_Null,       1,    0},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      6,   12},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    6, 1664},
+    {S_TermW,      4,    6},
+    {S_TermW,      7,   20},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      7,   24},
+    {S_TermW,      6,   14},
+    {S_TermW,      7,   28},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      7,   23},
+    {S_TermW,      4,    3},
+    {S_TermW,      7,   27},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   39},
+    {S_TermW,      6,   16},
+    {S_MakeUpW,    8,  576},
+    {S_TermW,      4,    6},
+    {S_TermW,      7,   19},
+    {S_TermW,      5,    8},
+    {S_TermW,      8,   55},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   45},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   53},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    8,  448},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   35},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      8,   51},
+    {S_TermW,      6,   15},
+    {S_TermW,      8,   63},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      6,   13},
+    {S_TermW,      4,    3},
+    {S_MakeUpW,    9, 1472},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   43},
+    {S_TermW,      6,   17},
+    {S_MakeUpW,    9, 1216},
+    {S_TermW,      4,    6},
+    {S_TermW,      6,    1},
+    {S_TermW,      5,    8},
+    {S_MakeUpW,    6,  192},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   29},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      6,   12},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    6, 1664},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   33},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      8,   49},
+    {S_TermW,      6,   14},
+    {S_TermW,      8,   61},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   47},
+    {S_TermW,      4,    3},
+    {S_TermW,      8,   59},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   41},
+    {S_TermW,      6,   16},
+    {S_MakeUpW,    9,  960},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   31},
+    {S_TermW,      5,    8},
+    {S_TermW,      8,   57},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      7,   22},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      7,   26},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    9,  704},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   37},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      7,   25},
+    {S_TermW,      6,   15},
+    {S_MakeUpW,    8,  320},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      6,   13},
+    {S_TermW,      4,    3},
+    {S_TermW,      7,   18},
+    {S_TermW,      4,    5},
+    {S_TermW,      7,   21},
+    {S_TermW,      6,   17},
+    {S_MakeUpW,    7,  256},
+    {S_TermW,      4,    6},
+    {S_TermW,      6,    1},
+    {S_TermW,      5,    8},
+    {S_MakeUpW,    6,  192},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_MakeUp,    12, 2048},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      6,   12},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    6, 1664},
+    {S_TermW,      4,    6},
+    {S_TermW,      7,   20},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      7,   24},
+    {S_TermW,      6,   14},
+    {S_TermW,      7,   28},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      7,   23},
+    {S_TermW,      4,    3},
+    {S_TermW,      7,   27},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   40},
+    {S_TermW,      6,   16},
+    {S_MakeUpW,    9,  832},
+    {S_TermW,      4,    6},
+    {S_TermW,      7,   19},
+    {S_TermW,      5,    8},
+    {S_TermW,      8,   56},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   46},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   54},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    8,  512},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   36},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      8,   52},
+    {S_TermW,      6,   15},
+    {S_TermW,      8,    0},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      6,   13},
+    {S_TermW,      4,    3},
+    {S_MakeUpW,    9, 1600},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   44},
+    {S_TermW,      6,   17},
+    {S_MakeUpW,    9, 1344},
+    {S_TermW,      4,    6},
+    {S_TermW,      6,    1},
+    {S_TermW,      5,    8},
+    {S_MakeUpW,    6,  192},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   30},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      6,   12},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    6, 1664},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   34},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      8,   50},
+    {S_TermW,      6,   14},
+    {S_TermW,      8,   62},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   48},
+    {S_TermW,      4,    3},
+    {S_TermW,      8,   60},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   42},
+    {S_TermW,      6,   16},
+    {S_MakeUpW,    9, 1088},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   32},
+    {S_TermW,      5,    8},
+    {S_TermW,      8,   58},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      7,   22},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      7,   26},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    8,  640},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   38},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      7,   25},
+    {S_TermW,      6,   15},
+    {S_MakeUpW,    8,  384},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      6,   13},
+    {S_TermW,      4,    3},
+    {S_TermW,      7,   18},
+    {S_TermW,      4,    5},
+    {S_TermW,      7,   21},
+    {S_TermW,      6,   17},
+    {S_MakeUpW,    7,  256},
+    {S_TermW,      4,    6},
+    {S_TermW,      6,    1},
+    {S_TermW,      5,    8},
+    {S_MakeUpW,    6,  192},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_Null,       1,    0},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      6,   12},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    6, 1664},
+    {S_TermW,      4,    6},
+    {S_TermW,      7,   20},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      7,   24},
+    {S_TermW,      6,   14},
+    {S_TermW,      7,   28},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      7,   23},
+    {S_TermW,      4,    3},
+    {S_TermW,      7,   27},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   39},
+    {S_TermW,      6,   16},
+    {S_MakeUpW,    8,  576},
+    {S_TermW,      4,    6},
+    {S_TermW,      7,   19},
+    {S_TermW,      5,    8},
+    {S_TermW,      8,   55},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   45},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   53},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    8,  448},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   35},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      8,   51},
+    {S_TermW,      6,   15},
+    {S_TermW,      8,   63},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      6,   13},
+    {S_TermW,      4,    3},
+    {S_MakeUpW,    9, 1536},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   43},
+    {S_TermW,      6,   17},
+    {S_MakeUpW,    9, 1280},
+    {S_TermW,      4,    6},
+    {S_TermW,      6,    1},
+    {S_TermW,      5,    8},
+    {S_MakeUpW,    6,  192},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   29},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      6,   12},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    6, 1664},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   33},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      8,   49},
+    {S_TermW,      6,   14},
+    {S_TermW,      8,   61},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   47},
+    {S_TermW,      4,    3},
+    {S_TermW,      8,   59},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   41},
+    {S_TermW,      6,   16},
+    {S_MakeUpW,    9, 1024},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   31},
+    {S_TermW,      5,    8},
+    {S_TermW,      8,   57},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      7,   22},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      7,   26},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    9,  768},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   37},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      7,   25},
+    {S_TermW,      6,   15},
+    {S_MakeUpW,    8,  320},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      6,   13},
+    {S_TermW,      4,    3},
+    {S_TermW,      7,   18},
+    {S_TermW,      4,    5},
+    {S_TermW,      7,   21},
+    {S_TermW,      6,   17},
+    {S_MakeUpW,    7,  256},
+    {S_TermW,      4,    6},
+    {S_TermW,      6,    1},
+    {S_TermW,      5,    8},
+    {S_MakeUpW,    6,  192},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_MakeUp,    11, 1920},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      6,   12},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    6, 1664},
+    {S_TermW,      4,    6},
+    {S_TermW,      7,   20},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      7,   24},
+    {S_TermW,      6,   14},
+    {S_TermW,      7,   28},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      7,   23},
+    {S_TermW,      4,    3},
+    {S_TermW,      7,   27},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   40},
+    {S_TermW,      6,   16},
+    {S_MakeUpW,    9,  896},
+    {S_TermW,      4,    6},
+    {S_TermW,      7,   19},
+    {S_TermW,      5,    8},
+    {S_TermW,      8,   56},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   46},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   54},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    8,  512},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   36},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      8,   52},
+    {S_TermW,      6,   15},
+    {S_TermW,      8,    0},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      6,   13},
+    {S_TermW,      4,    3},
+    {S_MakeUpW,    9, 1728},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   44},
+    {S_TermW,      6,   17},
+    {S_MakeUpW,    9, 1408},
+    {S_TermW,      4,    6},
+    {S_TermW,      6,    1},
+    {S_TermW,      5,    8},
+    {S_MakeUpW,    6,  192},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   30},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      6,   12},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    6, 1664},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   34},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      8,   50},
+    {S_TermW,      6,   14},
+    {S_TermW,      8,   62},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   48},
+    {S_TermW,      4,    3},
+    {S_TermW,      8,   60},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   42},
+    {S_TermW,      6,   16},
+    {S_MakeUpW,    9, 1152},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   32},
+    {S_TermW,      5,    8},
+    {S_TermW,      8,   58},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      7,   22},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      7,   26},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    8,  640},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   38},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      7,   25},
+    {S_TermW,      6,   15},
+    {S_MakeUpW,    8,  384},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      6,   13},
+    {S_TermW,      4,    3},
+    {S_TermW,      7,   18},
+    {S_TermW,      4,    5},
+    {S_TermW,      7,   21},
+    {S_TermW,      6,   17},
+    {S_MakeUpW,    7,  256},
+    {S_TermW,      4,    6},
+    {S_TermW,      6,    1},
+    {S_TermW,      5,    8},
+    {S_MakeUpW,    6,  192},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_Null,       1,    0},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      6,   12},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    6, 1664},
+    {S_TermW,      4,    6},
+    {S_TermW,      7,   20},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      7,   24},
+    {S_TermW,      6,   14},
+    {S_TermW,      7,   28},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      7,   23},
+    {S_TermW,      4,    3},
+    {S_TermW,      7,   27},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   39},
+    {S_TermW,      6,   16},
+    {S_MakeUpW,    8,  576},
+    {S_TermW,      4,    6},
+    {S_TermW,      7,   19},
+    {S_TermW,      5,    8},
+    {S_TermW,      8,   55},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   45},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   53},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    8,  448},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   35},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      8,   51},
+    {S_TermW,      6,   15},
+    {S_TermW,      8,   63},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      6,   13},
+    {S_TermW,      4,    3},
+    {S_MakeUpW,    9, 1472},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   43},
+    {S_TermW,      6,   17},
+    {S_MakeUpW,    9, 1216},
+    {S_TermW,      4,    6},
+    {S_TermW,      6,    1},
+    {S_TermW,      5,    8},
+    {S_MakeUpW,    6,  192},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   29},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      6,   12},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    6, 1664},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   33},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      8,   49},
+    {S_TermW,      6,   14},
+    {S_TermW,      8,   61},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   47},
+    {S_TermW,      4,    3},
+    {S_TermW,      8,   59},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   41},
+    {S_TermW,      6,   16},
+    {S_MakeUpW,    9,  960},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   31},
+    {S_TermW,      5,    8},
+    {S_TermW,      8,   57},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      7,   22},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      7,   26},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    9,  704},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   37},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      7,   25},
+    {S_TermW,      6,   15},
+    {S_MakeUpW,    8,  320},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      6,   13},
+    {S_TermW,      4,    3},
+    {S_TermW,      7,   18},
+    {S_TermW,      4,    5},
+    {S_TermW,      7,   21},
+    {S_TermW,      6,   17},
+    {S_MakeUpW,    7,  256},
+    {S_TermW,      4,    6},
+    {S_TermW,      6,    1},
+    {S_TermW,      5,    8},
+    {S_MakeUpW,    6,  192},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_MakeUp,    12, 2304},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      6,   12},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    6, 1664},
+    {S_TermW,      4,    6},
+    {S_TermW,      7,   20},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      7,   24},
+    {S_TermW,      6,   14},
+    {S_TermW,      7,   28},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      7,   23},
+    {S_TermW,      4,    3},
+    {S_TermW,      7,   27},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   40},
+    {S_TermW,      6,   16},
+    {S_MakeUpW,    9,  832},
+    {S_TermW,      4,    6},
+    {S_TermW,      7,   19},
+    {S_TermW,      5,    8},
+    {S_TermW,      8,   56},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   46},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   54},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    8,  512},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   36},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      8,   52},
+    {S_TermW,      6,   15},
+    {S_TermW,      8,    0},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      6,   13},
+    {S_TermW,      4,    3},
+    {S_MakeUpW,    9, 1600},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   44},
+    {S_TermW,      6,   17},
+    {S_MakeUpW,    9, 1344},
+    {S_TermW,      4,    6},
+    {S_TermW,      6,    1},
+    {S_TermW,      5,    8},
+    {S_MakeUpW,    6,  192},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   30},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      6,   12},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    6, 1664},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   34},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      8,   50},
+    {S_TermW,      6,   14},
+    {S_TermW,      8,   62},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   48},
+    {S_TermW,      4,    3},
+    {S_TermW,      8,   60},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   42},
+    {S_TermW,      6,   16},
+    {S_MakeUpW,    9, 1088},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   32},
+    {S_TermW,      5,    8},
+    {S_TermW,      8,   58},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      7,   22},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      7,   26},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    8,  640},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   38},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      7,   25},
+    {S_TermW,      6,   15},
+    {S_MakeUpW,    8,  384},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      6,   13},
+    {S_TermW,      4,    3},
+    {S_TermW,      7,   18},
+    {S_TermW,      4,    5},
+    {S_TermW,      7,   21},
+    {S_TermW,      6,   17},
+    {S_MakeUpW,    7,  256},
+    {S_TermW,      4,    6},
+    {S_TermW,      6,    1},
+    {S_TermW,      5,    8},
+    {S_MakeUpW,    6,  192},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_Null,       1,    0},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      6,   12},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    6, 1664},
+    {S_TermW,      4,    6},
+    {S_TermW,      7,   20},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      7,   24},
+    {S_TermW,      6,   14},
+    {S_TermW,      7,   28},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      7,   23},
+    {S_TermW,      4,    3},
+    {S_TermW,      7,   27},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   39},
+    {S_TermW,      6,   16},
+    {S_MakeUpW,    8,  576},
+    {S_TermW,      4,    6},
+    {S_TermW,      7,   19},
+    {S_TermW,      5,    8},
+    {S_TermW,      8,   55},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   45},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   53},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    8,  448},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   35},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      8,   51},
+    {S_TermW,      6,   15},
+    {S_TermW,      8,   63},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      6,   13},
+    {S_TermW,      4,    3},
+    {S_MakeUpW,    9, 1536},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   43},
+    {S_TermW,      6,   17},
+    {S_MakeUpW,    9, 1280},
+    {S_TermW,      4,    6},
+    {S_TermW,      6,    1},
+    {S_TermW,      5,    8},
+    {S_MakeUpW,    6,  192},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   29},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      6,   12},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    6, 1664},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   33},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      8,   49},
+    {S_TermW,      6,   14},
+    {S_TermW,      8,   61},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   47},
+    {S_TermW,      4,    3},
+    {S_TermW,      8,   59},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   41},
+    {S_TermW,      6,   16},
+    {S_MakeUpW,    9, 1024},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   31},
+    {S_TermW,      5,    8},
+    {S_TermW,      8,   57},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      7,   22},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      7,   26},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    9,  768},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   37},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      7,   25},
+    {S_TermW,      6,   15},
+    {S_MakeUpW,    8,  320},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      6,   13},
+    {S_TermW,      4,    3},
+    {S_TermW,      7,   18},
+    {S_TermW,      4,    5},
+    {S_TermW,      7,   21},
+    {S_TermW,      6,   17},
+    {S_MakeUpW,    7,  256},
+    {S_TermW,      4,    6},
+    {S_TermW,      6,    1},
+    {S_TermW,      5,    8},
+    {S_MakeUpW,    6,  192},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_MakeUp,    12, 2560},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      6,   12},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    6, 1664},
+    {S_TermW,      4,    6},
+    {S_TermW,      7,   20},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      7,   24},
+    {S_TermW,      6,   14},
+    {S_TermW,      7,   28},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      7,   23},
+    {S_TermW,      4,    3},
+    {S_TermW,      7,   27},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   40},
+    {S_TermW,      6,   16},
+    {S_MakeUpW,    9,  896},
+    {S_TermW,      4,    6},
+    {S_TermW,      7,   19},
+    {S_TermW,      5,    8},
+    {S_TermW,      8,   56},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   46},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   54},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    8,  512},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   36},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      8,   52},
+    {S_TermW,      6,   15},
+    {S_TermW,      8,    0},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      6,   13},
+    {S_TermW,      4,    3},
+    {S_MakeUpW,    9, 1728},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   44},
+    {S_TermW,      6,   17},
+    {S_MakeUpW,    9, 1408},
+    {S_TermW,      4,    6},
+    {S_TermW,      6,    1},
+    {S_TermW,      5,    8},
+    {S_MakeUpW,    6,  192},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   30},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      6,   12},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    6, 1664},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   34},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      8,   50},
+    {S_TermW,      6,   14},
+    {S_TermW,      8,   62},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      8,   48},
+    {S_TermW,      4,    3},
+    {S_TermW,      8,   60},
+    {S_TermW,      4,    5},
+    {S_TermW,      8,   42},
+    {S_TermW,      6,   16},
+    {S_MakeUpW,    9, 1152},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   32},
+    {S_TermW,      5,    8},
+    {S_TermW,      8,   58},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      7,   22},
+    {S_TermW,      4,    3},
+    {S_TermW,      5,   11},
+    {S_TermW,      4,    5},
+    {S_TermW,      7,   26},
+    {S_TermW,      5,    9},
+    {S_MakeUpW,    8,  640},
+    {S_TermW,      4,    6},
+    {S_TermW,      8,   38},
+    {S_MakeUpW,    5,  128},
+    {S_TermW,      7,   25},
+    {S_TermW,      6,   15},
+    {S_MakeUpW,    8,  384},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7},
+    {S_TermW,      6,   13},
+    {S_TermW,      4,    3},
+    {S_TermW,      7,   18},
+    {S_TermW,      4,    5},
+    {S_TermW,      7,   21},
+    {S_TermW,      6,   17},
+    {S_MakeUpW,    7,  256},
+    {S_TermW,      4,    6},
+    {S_TermW,      6,    1},
+    {S_TermW,      5,    8},
+    {S_MakeUpW,    6,  192},
+    {S_MakeUpW,    5,   64},
+    {S_TermW,      5,   10},
+    {S_TermW,      4,    4},
+    {S_TermW,      4,    2},
+    {S_TermW,      4,    7}
+};
+
+static const t4_table_entry_t t4_1d_black_table[8192] =
+{
+    {S_Null,       1,    0},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   10},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      8,   13},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      9,   15},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     10,   18},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   11},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     10,   17},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   12},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_MakeUp,    11, 1792},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   10},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     11,   23},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     11,   20},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     11,   25},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   11},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      8,   14},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   12},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_Null,       1,    0},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   10},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      8,   13},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_MakeUpB,   12,  128},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     12,   56},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   11},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     12,   30},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   12},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_MakeUp,    11, 1856},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   10},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     12,   57},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     11,   21},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     12,   54},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   11},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      8,   14},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   12},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_Null,       1,    0},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   10},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      8,   13},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      9,   15},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     12,   52},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   11},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     12,   48},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   12},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_MakeUp,    12, 2112},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   10},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     12,   44},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     12,   36},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_MakeUpB,   12,  384},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   11},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      8,   14},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   12},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_Null,       1,    0},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   10},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      8,   13},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     12,   28},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     12,   60},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   11},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     12,   40},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   12},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_MakeUp,    12, 2368},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   10},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     10,   16},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     10,    0},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_MakeUpB,   10,   64},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   11},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      8,   14},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   12},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_Null,       1,    0},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   10},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      8,   13},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      9,   15},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     10,   18},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   11},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     10,   17},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   12},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_MakeUp,    12, 1984},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   10},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     12,   50},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     12,   34},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_MakeUpB,   13, 1664},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   11},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      8,   14},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   12},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_Null,       1,    0},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   10},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      8,   13},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     12,   26},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_MakeUpB,   13, 1408},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   11},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     12,   32},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   12},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_MakeUp,    11, 1920},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   10},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     12,   61},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     12,   42},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_MakeUpB,   13, 1024},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   11},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      8,   14},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   12},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_Null,       1,    0},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   10},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      8,   13},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      9,   15},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_MakeUpB,   13,  768},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   11},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     12,   62},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   12},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_MakeUp,    12, 2240},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   10},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     12,   46},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     12,   38},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_MakeUpB,   13,  512},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   11},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      8,   14},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   12},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_Null,       1,    0},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   10},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      8,   13},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     11,   19},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     11,   24},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   11},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     11,   22},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   12},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_MakeUp,    12, 2496},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   10},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     10,   16},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     10,    0},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_MakeUpB,   10,   64},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   11},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      8,   14},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   12},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_EOL,       12,    0},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   10},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      8,   13},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      9,   15},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     10,   18},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   11},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     10,   17},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   12},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_MakeUp,    11, 1792},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   10},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     11,   23},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     11,   20},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     11,   25},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   11},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      8,   14},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   12},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_Null,       1,    0},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   10},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      8,   13},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_MakeUpB,   12,  192},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_MakeUpB,   13, 1280},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   11},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     12,   31},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   12},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_MakeUp,    11, 1856},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   10},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     12,   58},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     11,   21},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_MakeUpB,   13,  896},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   11},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      8,   14},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   12},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_Null,       1,    0},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   10},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      8,   13},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      9,   15},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_MakeUpB,   13,  640},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   11},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     12,   49},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   12},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_MakeUp,    12, 2176},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   10},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     12,   45},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     12,   37},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_MakeUpB,   12,  448},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   11},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      8,   14},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   12},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_Null,       1,    0},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   10},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      8,   13},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     12,   29},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_MakeUpB,   13, 1536},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   11},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     12,   41},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   12},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_MakeUp,    12, 2432},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   10},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     10,   16},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     10,    0},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_MakeUpB,   10,   64},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   11},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      8,   14},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   12},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_Null,       1,    0},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   10},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      8,   13},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      9,   15},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     10,   18},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   11},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     10,   17},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   12},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_MakeUp,    12, 2048},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   10},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     12,   51},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     12,   35},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_MakeUpB,   12,  320},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   11},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      8,   14},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   12},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_Null,       1,    0},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   10},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      8,   13},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     12,   27},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     12,   59},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   11},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     12,   33},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   12},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_MakeUp,    11, 1920},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   10},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_MakeUpB,   12,  256},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     12,   43},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_MakeUpB,   13, 1152},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   11},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      8,   14},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   12},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_Null,       1,    0},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   10},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      8,   13},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      9,   15},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     12,   55},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   11},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     12,   63},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   12},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_MakeUp,    12, 2304},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   10},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     12,   47},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     12,   39},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     12,   53},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   11},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      8,   14},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   12},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_Null,       1,    0},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   10},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      8,   13},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     11,   19},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     11,   24},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   11},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     11,   22},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   12},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_MakeUp,    12, 2560},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   10},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     10,   16},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     10,    0},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_MakeUpB,   10,   64},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   11},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      8,   14},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   12},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_Null,       1,    0},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   10},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      8,   13},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      9,   15},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     10,   18},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   11},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     10,   17},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   12},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_MakeUp,    11, 1792},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   10},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     11,   23},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     11,   20},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     11,   25},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   11},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      8,   14},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   12},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_Null,       1,    0},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   10},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      8,   13},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_MakeUpB,   12,  128},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     12,   56},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   11},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     12,   30},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   12},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_MakeUp,    11, 1856},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   10},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     12,   57},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     11,   21},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     12,   54},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   11},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      8,   14},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   12},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_Null,       1,    0},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   10},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      8,   13},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      9,   15},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     12,   52},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   11},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     12,   48},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   12},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_MakeUp,    12, 2112},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   10},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     12,   44},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     12,   36},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_MakeUpB,   12,  384},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   11},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      8,   14},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   12},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_Null,       1,    0},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   10},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      8,   13},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     12,   28},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     12,   60},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   11},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     12,   40},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   12},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_MakeUp,    12, 2368},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   10},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     10,   16},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     10,    0},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_MakeUpB,   10,   64},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   11},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      8,   14},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   12},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_Null,       1,    0},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   10},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      8,   13},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      9,   15},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     10,   18},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   11},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     10,   17},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   12},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_MakeUp,    12, 1984},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   10},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     12,   50},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     12,   34},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_MakeUpB,   13, 1728},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   11},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      8,   14},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   12},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_Null,       1,    0},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   10},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      8,   13},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     12,   26},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_MakeUpB,   13, 1472},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   11},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     12,   32},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   12},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_MakeUp,    11, 1920},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   10},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     12,   61},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     12,   42},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_MakeUpB,   13, 1088},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   11},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      8,   14},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   12},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_Null,       1,    0},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   10},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      8,   13},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      9,   15},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_MakeUpB,   13,  832},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   11},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     12,   62},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   12},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_MakeUp,    12, 2240},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   10},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     12,   46},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     12,   38},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_MakeUpB,   13,  576},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   11},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      8,   14},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   12},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_Null,       1,    0},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   10},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      8,   13},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     11,   19},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     11,   24},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   11},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     11,   22},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   12},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_MakeUp,    12, 2496},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   10},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     10,   16},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     10,    0},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_MakeUpB,   10,   64},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   11},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      8,   14},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   12},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_EOL,       12,    0},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   10},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      8,   13},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      9,   15},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     10,   18},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   11},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     10,   17},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   12},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_MakeUp,    11, 1792},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   10},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     11,   23},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     11,   20},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     11,   25},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   11},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      8,   14},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   12},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_Null,       1,    0},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   10},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      8,   13},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_MakeUpB,   12,  192},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_MakeUpB,   13, 1344},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   11},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     12,   31},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   12},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_MakeUp,    11, 1856},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   10},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     12,   58},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     11,   21},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_MakeUpB,   13,  960},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   11},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      8,   14},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   12},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_Null,       1,    0},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   10},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      8,   13},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      9,   15},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_MakeUpB,   13,  704},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   11},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     12,   49},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   12},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_MakeUp,    12, 2176},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   10},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     12,   45},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     12,   37},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_MakeUpB,   12,  448},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   11},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      8,   14},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   12},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_Null,       1,    0},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   10},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      8,   13},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     12,   29},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_MakeUpB,   13, 1600},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   11},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     12,   41},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   12},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_MakeUp,    12, 2432},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   10},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     10,   16},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     10,    0},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_MakeUpB,   10,   64},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   11},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      8,   14},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   12},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_Null,       1,    0},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   10},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      8,   13},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      9,   15},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     10,   18},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   11},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     10,   17},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   12},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_MakeUp,    12, 2048},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   10},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     12,   51},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     12,   35},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_MakeUpB,   12,  320},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   11},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      8,   14},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   12},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_Null,       1,    0},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   10},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      8,   13},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     12,   27},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     12,   59},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   11},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     12,   33},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   12},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_MakeUp,    11, 1920},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   10},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_MakeUpB,   12,  256},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     12,   43},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_MakeUpB,   13, 1216},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   11},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      8,   14},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   12},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_Null,       1,    0},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   10},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      8,   13},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      9,   15},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     12,   55},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   11},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     12,   63},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   12},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_MakeUp,    12, 2304},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   10},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     12,   47},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     12,   39},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     12,   53},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   11},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      8,   14},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   12},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_Null,       1,    0},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   10},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      8,   13},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     11,   19},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     11,   24},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   11},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     11,   22},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   12},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_MakeUp,    12, 2560},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   10},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     10,   16},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,     10,    0},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_MakeUpB,   10,   64},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    9},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   11},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      8,   14},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      6,    8},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      7,   12},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    6},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2},
+    {S_TermB,      5,    7},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    1},
+    {S_TermB,      2,    2},
+    {S_TermB,      4,    5},
+    {S_TermB,      2,    3},
+    {S_TermB,      3,    4},
+    {S_TermB,      2,    2}
+};
+
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/testcpuid.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/testcpuid.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,209 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * testcpuid.c - Check the CPU type, to identify special features, like SSE.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ * Stitched together from bits of testing code found here and there
+ *
+ * Copyright (C) 2004 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: testcpuid.c,v 1.12 2008/05/13 13:17:24 steveu Exp $
+ */
+
+/*! \file */
+
+#if defined(HAVE_CONFIG_H)
+#include <config.h>
+#endif
+
+#include <inttypes.h>
+
+/* Make this file just disappear if we are not on an x86 machine */
+#if defined(__i386__) //  ||  defined(__x86_64__)
+
+#define X86_EFLAGS_CF   0x00000001 /* Carry Flag */
+#define X86_EFLAGS_PF   0x00000004 /* Parity Flag */
+#define X86_EFLAGS_AF   0x00000010 /* Auxillary carry Flag */
+#define X86_EFLAGS_ZF   0x00000040 /* Zero Flag */
+#define X86_EFLAGS_SF   0x00000080 /* Sign Flag */
+#define X86_EFLAGS_TF   0x00000100 /* Trap Flag */
+#define X86_EFLAGS_IF   0x00000200 /* Interrupt Flag */
+#define X86_EFLAGS_DF   0x00000400 /* Direction Flag */
+#define X86_EFLAGS_OF   0x00000800 /* Overflow Flag */
+#define X86_EFLAGS_IOPL 0x00003000 /* IOPL mask */
+#define X86_EFLAGS_NT   0x00004000 /* Nested Task */
+#define X86_EFLAGS_RF   0x00010000 /* Resume Flag */
+#define X86_EFLAGS_VM   0x00020000 /* Virtual Mode */
+#define X86_EFLAGS_AC   0x00040000 /* Alignment Check */
+#define X86_EFLAGS_VIF  0x00080000 /* Virtual Interrupt Flag */
+#define X86_EFLAGS_VIP  0x00100000 /* Virtual Interrupt Pending */
+#define X86_EFLAGS_ID   0x00200000 /* CPUID detection flag */
+
+/* Standard macro to see if a specific flag is changeable */
+static __inline__ int flag_is_changeable_p(uint32_t flag)
+{
+    uint32_t f1;
+    uint32_t f2;
+
+    __asm__ __volatile__ (
+        " pushfl\n"
+        " pushfl\n"
+        " popl %0\n"
+        " movl %0,%1\n"
+        " xorl %2,%0\n"
+        " pushl %0\n"
+        " popfl\n"
+        " pushfl\n"
+        " popl %0\n"
+        " popfl\n"
+        : "=&r" (f1), "=&r" (f2)
+        : "ir" (flag));
+
+    return ((f1^f2) & flag) != 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+/* Probe for the CPUID instruction */
+static int have_cpuid_p(void)
+{
+    return flag_is_changeable_p(X86_EFLAGS_ID);
+}
+/*- End of function --------------------------------------------------------*/
+
+int has_MMX(void)
+{
+    int result;
+
+    if (!have_cpuid_p())
+        return  0;
+    /*endif*/
+    __asm__ __volatile__ (
+        " push  %%ebx;\n"
+	" mov	$1,%%eax;\n"
+	" cpuid;\n"
+	" xor   %%eax,%%eax;\n"
+	" test	$0x800000,%%edx;\n"
+	" jz	1f;\n"	            /* no MMX support */
+	" inc   %%eax;\n"	    /* MMX support */
+        "1:\n"
+        " pop  %%ebx;\n"
+	: "=a" (result)
+        : 
+        : "ecx", "edx");
+    return  result;
+}
+/*- End of function --------------------------------------------------------*/
+        
+int has_SIMD(void)
+{
+    int result;
+
+    if (!have_cpuid_p())
+        return  0;
+    /*endif*/
+    __asm__ __volatile__ (
+        " push  %%ebx;\n"
+	" mov	$1,%%eax;\n"
+	" cpuid;\n"
+	" xor   %%eax,%%eax;\n"
+	" test	$0x02000000,%%edx;\n"
+	" jz	1f;\n"		        /* no SIMD support */
+	" inc	%%eax;\n"		/* SIMD support */
+        "1:\n"
+        " pop  %%ebx;\n"
+	: "=a" (result)
+        : 
+        : "ecx", "edx");
+    return  result;
+}
+/*- End of function --------------------------------------------------------*/
+
+int has_SIMD2(void)
+{
+    int result;
+
+    if (!have_cpuid_p())
+        return  0;
+    /*endif*/
+    __asm__ __volatile__ (
+        " push  %%ebx;\n"
+	" mov	$1,%%eax;\n"
+	" cpuid;\n"
+	" xor   %%eax,%%eax;\n"
+	" test	$0x04000000,%%edx;\n"
+	" jz	1f;\n"		        /* no SIMD2 support */
+	" inc	%%eax;\n"		/* SIMD2 support */
+        "1:\n"
+        " pop  %%ebx;\n"
+	: "=a" (result)
+        : 
+        : "ecx", "edx");
+    return  result;
+}
+/*- End of function --------------------------------------------------------*/
+        
+int has_3DNow(void)
+{
+    int result;
+
+    if (!have_cpuid_p())
+        return  0;
+    /*endif*/
+    __asm__ __volatile__ (
+        " push  %%ebx;\n"
+	" mov	$0x80000000,%%eax;\n"
+	" cpuid;\n"
+        " xor   %%ecx,%%ecx;\n"
+	" cmp	$0x80000000,%%eax;\n"
+	" jbe	1f;\n"		        /* no extended MSR(1), so no 3DNow! */
+	" mov	$0x80000001,%%eax;\n"
+	" cpuid;\n"
+        " xor   %%ecx,%%ecx;\n"
+	" test	$0x80000000,%%edx;\n"
+	" jz	1f;\n"		        /* no 3DNow! support */
+	" inc   %%ecx;\n"		/* 3DNow! support */
+        "1:\n"
+        " pop  %%ebx;\n"
+	: "=c" (result)
+        : 
+        : "eax", "edx");
+    return  result;
+}
+/*- End of function --------------------------------------------------------*/
+
+#if defined(TESTBED)
+int main(int argc, char *argv[])
+{
+    int result;
+
+    result = has_MMX();
+    printf("MMX is %x\n", result);
+    result = has_SIMD();
+    printf("SIMD is %x\n", result);
+    result = has_SIMD2();
+    printf("SIMD2 is %x\n", result);
+    result = has_3DNow();
+    printf("3DNow is %x\n", result);
+    return  0;
+}
+/*- End of function --------------------------------------------------------*/
+#endif
+
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/time_scale.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/time_scale.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,277 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * time_scale.c - Time scaling for linear speech data
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2004 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: time_scale.c,v 1.23 2008/07/28 15:14:30 steveu Exp $
+ */
+
+/*! \file */
+
+#if defined(HAVE_CONFIG_H)
+#include <config.h>
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <inttypes.h>
+#include <string.h>
+#include <fcntl.h>
+#include <time.h>
+#include <limits.h>
+#include "floating_fudge.h"
+#if defined(HAVE_TGMATH_H)
+#include <tgmath.h>
+#endif
+#if defined(HAVE_MATH_H)
+#include <math.h>
+#endif
+
+#include "spandsp/telephony.h"
+#include "spandsp/time_scale.h"
+
+/*
+    Time scaling for speech, based on the Pointer Interval Controlled
+    OverLap and Add (PICOLA) method, developed by Morita Naotaka.
+ */
+
+static __inline__ int amdf_pitch(int min_pitch, int max_pitch, int16_t amp[], int len)
+{
+    int i;
+    int j;
+    int acc;
+    int min_acc;
+    int pitch;
+
+    pitch = min_pitch;
+    min_acc = INT_MAX;
+    for (i = max_pitch;  i <= min_pitch;  i++)
+    {
+        acc = 0;
+        for (j = 0;  j < len;  j++)
+            acc += abs(amp[i + j] - amp[j]);
+        if (acc < min_acc)
+        {
+            min_acc = acc;
+            pitch = i;
+        }
+    }
+    return pitch;
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ void overlap_add(int16_t amp1[], int16_t amp2[], int len)
+{
+    int i;
+    float weight;
+    float step;
+    
+    step = 1.0f/len;
+    weight = 0.0f;
+    for (i = 0;  i < len;  i++)
+    {
+        /* TODO: saturate */
+        amp2[i] = (int16_t) ((float) amp1[i]*(1.0f - weight) + (float) amp2[i]*weight);
+        weight += step;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+int time_scale_rate(time_scale_state_t *s, float playout_rate)
+{
+    if (playout_rate <= 0.0f)
+        return -1;
+    /*endif*/
+    if (playout_rate >= 0.99f  &&  playout_rate <= 1.01f)
+    {
+        /* Treat rate close to normal speed as exactly normal speed, and
+           avoid divide by zero, and other numerical problems. */
+        playout_rate = 1.0f;
+    }
+    else if (playout_rate < 1.0f)
+    {
+        s->rcomp = playout_rate/(1.0f - playout_rate);
+    }
+    else
+    {
+        s->rcomp = 1.0f/(playout_rate - 1.0f);
+    }
+    /*endif*/
+    s->playout_rate = playout_rate;
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+time_scale_state_t *time_scale_init(time_scale_state_t *s, int sample_rate, float playout_rate)
+{
+    int alloced;
+
+    if (sample_rate > TIME_SCALE_MAX_SAMPLE_RATE)
+        return NULL;
+    alloced = FALSE;
+    if (s == NULL)
+    {
+        if ((s = (time_scale_state_t *) malloc(sizeof (*s))) == NULL)
+            return  NULL;
+        /*endif*/
+        alloced = TRUE;
+    }
+    /*endif*/
+    s->sample_rate = sample_rate;
+    s->min_pitch = sample_rate/TIME_SCALE_MIN_PITCH;
+    s->max_pitch = sample_rate/TIME_SCALE_MAX_PITCH;
+    s->buf_len = 2*sample_rate/TIME_SCALE_MIN_PITCH;
+    if (time_scale_rate(s, playout_rate))
+    {
+        if (alloced)
+            free(s);
+        return NULL;
+    }
+    /*endif*/
+    s->rate_nudge = 0.0f;
+    s->fill = 0;
+    s->lcp = 0;
+    return s;
+}
+/*- End of function --------------------------------------------------------*/
+
+int time_scale_free(time_scale_state_t *s)
+{
+    free(s);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int time_scale(time_scale_state_t *s, int16_t out[], int16_t in[], int len)
+{
+    double lcpf;
+    int pitch;
+    int out_len;
+    int in_len;
+    int k;
+
+    out_len = 0;
+    in_len = 0;
+
+    /* Top up the buffer */
+    if (s->fill + len < s->buf_len)
+    {
+        /* Cannot continue without more samples */
+        memcpy(s->buf + s->fill, in, sizeof(int16_t)*len);
+        s->fill += len;
+        return out_len;
+    }
+    k = s->buf_len - s->fill;
+    memcpy(s->buf + s->fill, in, sizeof(int16_t)*k);
+    in_len += k;
+    s->fill = s->buf_len;
+    while (s->fill == s->buf_len)
+    {
+        while (s->lcp >= s->buf_len)
+        {
+            memcpy(out + out_len, s->buf, sizeof(int16_t)*s->buf_len);
+            out_len += s->buf_len;
+            if (len - in_len < s->buf_len)
+            {
+                /* Cannot continue without more samples */
+                memcpy(s->buf, in + in_len, sizeof(int16_t)*(len - in_len));
+                s->fill = len - in_len;
+                s->lcp -= s->buf_len;
+                return out_len;
+            }
+            memcpy(s->buf, in + in_len, sizeof(int16_t)*s->buf_len);
+            in_len += s->buf_len;
+            s->lcp -= s->buf_len;
+        }
+        if (s->lcp > 0)
+        {
+            memcpy(out + out_len, s->buf, sizeof(int16_t)*s->lcp);
+            out_len += s->lcp;
+            memcpy(s->buf, s->buf + s->lcp, sizeof(int16_t)*(s->buf_len - s->lcp));
+            if (len - in_len < s->lcp)
+            {
+                /* Cannot continue without more samples */
+                memcpy(s->buf + (s->buf_len - s->lcp), in + in_len, sizeof(int16_t)*(len - in_len));
+                s->fill = s->buf_len - s->lcp + len - in_len;
+                s->lcp = 0;
+                return out_len;
+            }
+            memcpy(s->buf + (s->buf_len - s->lcp), in + in_len, sizeof(int16_t)*s->lcp);
+            in_len += s->lcp;
+            s->lcp = 0;
+        }
+        if (s->playout_rate == 1.0f)
+        {
+            s->lcp = 0x7FFFFFFF;
+        }
+        else
+        {
+            pitch = amdf_pitch(s->min_pitch, s->max_pitch, s->buf, s->min_pitch);
+            lcpf = (double) pitch*s->rcomp;
+            /* Nudge around to compensate for fractional samples */
+            s->lcp = (int) lcpf;
+            /* Note that s->lcp and lcpf are not the same, as lcpf has a fractional part, and s->lcp doesn't */
+            s->rate_nudge += s->lcp - lcpf;
+            if (s->rate_nudge >= 0.5f)
+            {
+                s->lcp--;
+                s->rate_nudge -= 1.0f;
+            }
+            else if (s->rate_nudge <= -0.5f)
+            {
+                s->lcp++;
+                s->rate_nudge += 1.0f;
+            }
+            if (s->playout_rate < 1.0f)
+            {
+                /* Speed up - drop a chunk of data */
+                overlap_add(s->buf, s->buf + pitch, pitch);
+                memcpy(&s->buf[pitch], &s->buf[2*pitch], sizeof(int16_t)*(s->buf_len - 2*pitch));
+                if (len - in_len < pitch)
+                {
+                    /* Cannot continue without more samples */
+                    memcpy(s->buf + s->buf_len - pitch, in + in_len, sizeof(int16_t)*(len - in_len));
+                    s->fill += (len - in_len - pitch);
+                    return out_len;
+                }
+                memcpy(s->buf + s->buf_len - pitch, in + in_len, sizeof(int16_t)*pitch);
+                in_len += pitch;
+            }
+            else
+            {
+                /* Slow down - insert a chunk of data */
+                memcpy(out + out_len, s->buf, sizeof(int16_t)*pitch);
+                out_len += pitch;
+                overlap_add(s->buf + pitch, s->buf, pitch);
+            }
+        }
+    }
+    return out_len;
+}
+/*- End of function --------------------------------------------------------*/
+
+int time_scale_max_output_len(time_scale_state_t *s, int input_len)
+{
+    return input_len*s->playout_rate + s->min_pitch + 1;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/tone_detect.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/tone_detect.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,286 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * tone_detect.c - General telephony tone detection.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2001-2003, 2005 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: tone_detect.c,v 1.47 2008/07/02 14:48:26 steveu Exp $
+ */
+ 
+/*! \file tone_detect.h */
+
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
+
+#include <inttypes.h>
+#include <stdlib.h>
+#include "floating_fudge.h"
+#if defined(HAVE_TGMATH_H)
+#include <tgmath.h>
+#endif
+#if defined(HAVE_MATH_H)
+#include <math.h>
+#endif
+#include <string.h>
+#include <stdio.h>
+#include <time.h>
+#include <fcntl.h>
+
+#include "spandsp/telephony.h"
+#include "spandsp/complex.h"
+#include "spandsp/complex_vector_float.h"
+#include "spandsp/tone_detect.h"
+#include "spandsp/tone_generate.h"
+
+#if !defined(M_PI)
+/* C99 systems may not define M_PI */
+#define M_PI 3.14159265358979323846264338327
+#endif
+
+void make_goertzel_descriptor(goertzel_descriptor_t *t, float freq, int samples)
+{
+#if defined(SPANDSP_USE_FIXED_POINT)
+    t->fac = 16383.0f*2.0f*cosf(2.0f*M_PI*(freq/(float) SAMPLE_RATE));
+#else
+    t->fac = 2.0f*cosf(2.0f*M_PI*(freq/(float) SAMPLE_RATE));
+#endif
+    t->samples = samples;
+}
+/*- End of function --------------------------------------------------------*/
+
+goertzel_state_t *goertzel_init(goertzel_state_t *s,
+                                goertzel_descriptor_t *t)
+{
+    if (s == NULL)
+    {
+        if ((s = (goertzel_state_t *) malloc(sizeof(*s))) == NULL)
+            return NULL;
+    }
+#if defined(SPANDSP_USE_FIXED_POINT)
+    s->v2 =
+    s->v3 = 0;
+#else
+    s->v2 =
+    s->v3 = 0.0f;
+#endif
+    s->fac = t->fac;
+    s->samples = t->samples;
+    s->current_sample = 0;
+    return s;
+}
+/*- End of function --------------------------------------------------------*/
+
+void goertzel_reset(goertzel_state_t *s)
+{
+#if defined(SPANDSP_USE_FIXED_POINT)
+    s->v2 =
+    s->v3 = 0;
+#else
+    s->v2 =
+    s->v3 = 0.0f;
+#endif
+    s->current_sample = 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int goertzel_update(goertzel_state_t *s,
+                    const int16_t amp[],
+                    int samples)
+{
+    int i;
+#if defined(SPANDSP_USE_FIXED_POINT)
+    int16_t x;
+    int16_t v1;
+#else
+    float v1;
+#endif
+
+    if (samples > s->samples - s->current_sample)
+        samples = s->samples - s->current_sample;
+    for (i = 0;  i < samples;  i++)
+    {
+        v1 = s->v2;
+        s->v2 = s->v3;
+#if defined(SPANDSP_USE_FIXED_POINT)
+        x = (((int32_t) s->fac*s->v2) >> 14);
+        /* Scale down the input signal to avoid overflows. 9 bits is enough to
+           monitor the signals of interest with adequate dynamic range and
+           resolution. In telephony we generally only start with 13 or 14 bits,
+           anyway. */
+        s->v3 = x - v1 + (amp[i] >> 7);
+#else
+        s->v3 = s->fac*s->v2 - v1 + amp[i];
+#endif
+    }
+    s->current_sample += samples;
+    return samples;
+}
+/*- End of function --------------------------------------------------------*/
+
+#if defined(SPANDSP_USE_FIXED_POINT)
+int32_t goertzel_result(goertzel_state_t *s)
+#else
+float goertzel_result(goertzel_state_t *s)
+#endif
+{
+#if defined(SPANDSP_USE_FIXED_POINT)
+    int16_t v1;
+    int32_t x;
+    int32_t y;
+#else
+    float v1;
+#endif
+
+    /* Push a zero through the process to finish things off. */
+    v1 = s->v2;
+    s->v2 = s->v3;
+#if defined(SPANDSP_USE_FIXED_POINT)
+    x = (((int32_t) s->fac*s->v2) >> 14);
+    s->v3 = x - v1;
+#else
+    s->v3 = s->fac*s->v2 - v1;
+#endif
+    /* Now calculate the non-recursive side of the filter. */
+    /* The result here is not scaled down to allow for the magnification
+       effect of the filter (the usual DFT magnification effect). */
+#if defined(SPANDSP_USE_FIXED_POINT)
+    x = (int32_t) s->v3*s->v3;
+    y = (int32_t) s->v2*s->v2;
+    x += y;
+    y = ((int32_t) s->v3*s->fac) >> 14;
+    y *= s->v2;
+    x -= y;
+    x <<= 1;
+    goertzel_reset(s);
+    /* The number returned in a floating point build will be 16384^2 times
+       as big as for a fixed point build, due to the 14 bit shifts
+       (or the square of the 7 bit shifts, depending how you look at it). */
+    return x;
+#else
+    v1 = s->v3*s->v3 + s->v2*s->v2 - s->v2*s->v3*s->fac;
+    v1 *= 2.0;
+    goertzel_reset(s);
+    return v1;
+#endif
+}
+/*- End of function --------------------------------------------------------*/
+
+complexf_t periodogram(const complexf_t coeffs[], const complexf_t amp[], int len)
+{
+    complexf_t sum;
+    complexf_t diff;
+    complexf_t x;
+    int i;
+
+    x = complex_setf(0.0f, 0.0f);
+    for (i = 0;  i < len/2;  i++)
+    {
+        sum = complex_addf(&amp[i], &amp[len - 1 - i]);
+        diff = complex_subf(&amp[i], &amp[len - 1 - i]);
+        x.re += (coeffs[i].re*sum.re - coeffs[i].im*diff.im);
+        x.im += (coeffs[i].re*sum.im + coeffs[i].im*diff.re);
+    }
+    return x;
+}
+/*- End of function --------------------------------------------------------*/
+
+int periodogram_prepare(complexf_t sum[], complexf_t diff[], const complexf_t amp[], int len)
+{
+    int i;
+
+    for (i = 0;  i < len/2;  i++)
+    {
+        sum[i] = complex_addf(&amp[i], &amp[len - 1 - i]);
+        diff[i] = complex_subf(&amp[i], &amp[len - 1 - i]);
+    }
+    return len/2;
+}
+/*- End of function --------------------------------------------------------*/
+
+complexf_t periodogram_apply(const complexf_t coeffs[], const complexf_t sum[], const complexf_t diff[], int len)
+{
+    complexf_t x;
+    int i;
+
+    x = complex_setf(0.0f, 0.0f);
+    for (i = 0;  i < len/2;  i++)
+    {
+        x.re += (coeffs[i].re*sum[i].re - coeffs[i].im*diff[i].im);
+        x.im += (coeffs[i].re*sum[i].im + coeffs[i].im*diff[i].re);
+    }
+    return x;
+}
+/*- End of function --------------------------------------------------------*/
+
+int periodogram_generate_coeffs(complexf_t coeffs[], float freq, int sample_rate, int window_len)
+{
+    float window;
+    float sum;
+    float x;
+    int i;
+
+    sum = 0.0f;
+    for (i = 0;  i < window_len/2;  i++)
+    {
+        /* Apply a Hamming window as we go */
+        window = 0.53836f - 0.46164f*cosf(2.0f*3.1415926535f*i/(window_len - 1.0f));
+        x = (i - window_len/2.0f + 0.5f)*freq*2.0f*3.1415926535f/sample_rate;
+        coeffs[i].re = cosf(x)*window;
+        coeffs[i].im = -sinf(x)*window;
+        sum += window;
+    }
+    /* Rescale for unity gain in the periodogram. The 2.0 factor is to allow for the full window,
+       rather than just the half over which we have summed the coefficients. */
+    sum = 1.0f/(2.0f*sum);
+    for (i = 0;  i < window_len/2;  i++)
+    {
+        coeffs[i].re *= sum;
+        coeffs[i].im *= sum;
+    }
+    return window_len/2;
+}
+/*- End of function --------------------------------------------------------*/
+
+float periodogram_generate_phase_offset(complexf_t *offset, float freq, int sample_rate, int interval)
+{
+    float x;
+
+    /* The phase offset is how far the phase rotates in one frame */
+    x = 2.0f*3.1415926535f*(float) interval/(float) sample_rate;
+    offset->re = cosf(freq*x);
+    offset->im = sinf(freq*x);
+    return 1.0f/x;
+}
+/*- End of function --------------------------------------------------------*/
+
+float periodogram_freq_error(const complexf_t *phase_offset, float scale, const complexf_t *last_result, const complexf_t *result)
+{
+    complexf_t prediction;
+
+    /* Rotate the last result by the expected phasor offset to the current result. Then
+       find the difference between that predicted position, and the actual one. When
+       scaled by the current signal level, this gives us the frequency error. */
+    prediction = complex_mulf(last_result, phase_offset);
+    return scale*(result->im*prediction.re - result->re*prediction.im)/(result->re*result->re + result->im*result->im);
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/tone_generate.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/tone_generate.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,189 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * tone_generate.c - General telephony tone generation.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2001 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: tone_generate.c,v 1.43 2008/07/02 14:48:26 steveu Exp $
+ */
+
+/*! \file */
+
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
+
+#include <inttypes.h>
+#include <string.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <time.h>
+#include <fcntl.h>
+#include "floating_fudge.h"
+#if defined(HAVE_TGMATH_H)
+#include <tgmath.h>
+#endif
+#if defined(HAVE_MATH_H)
+#include <math.h>
+#endif
+
+#include "spandsp/telephony.h"
+#include "spandsp/dc_restore.h"
+#include "spandsp/complex.h"
+#include "spandsp/dds.h"
+#include "spandsp/tone_generate.h"
+
+#if !defined(M_PI)
+/* C99 systems may not define M_PI */
+#define M_PI 3.14159265358979323846264338327
+#endif
+
+#define ms_to_samples(t)            (((t)*SAMPLE_RATE)/1000)
+
+void make_tone_gen_descriptor(tone_gen_descriptor_t *s,
+                              int f1,
+                              int l1,
+                              int f2,
+                              int l2,
+                              int d1,
+                              int d2,
+                              int d3,
+                              int d4,
+                              int repeat)
+{
+    memset(s, 0, sizeof(*s));
+    if (f1)
+    {
+        s->tone[0].phase_rate = dds_phase_ratef((float) f1);
+        if (f2 < 0)
+            s->tone[0].phase_rate = -s->tone[0].phase_rate;
+        s->tone[0].gain = dds_scaling_dbm0f((float) l1);
+    }
+    if (f2)
+    {
+        s->tone[1].phase_rate = dds_phase_ratef((float) abs(f2));
+        s->tone[1].gain = (f2 < 0)  ?  (float) l2/100.0f  :  dds_scaling_dbm0f((float) l2);
+    }
+
+    s->duration[0] = d1*SAMPLE_RATE/1000;
+    s->duration[1] = d2*SAMPLE_RATE/1000;
+    s->duration[2] = d3*SAMPLE_RATE/1000;
+    s->duration[3] = d4*SAMPLE_RATE/1000;
+
+    s->repeat = repeat;
+}
+/*- End of function --------------------------------------------------------*/
+
+tone_gen_state_t *tone_gen_init(tone_gen_state_t *s, tone_gen_descriptor_t *t)
+{
+    int i;
+
+    if (s == NULL)
+        return NULL;
+    for (i = 0;  i < 4;  i++)
+    {
+        s->tone[i] = t->tone[i];
+        s->phase[i] = 0;
+    }
+
+    for (i = 0;  i < 4;  i++)
+        s->duration[i] = t->duration[i];
+    s->repeat = t->repeat;
+
+    s->current_section = 0;
+    s->current_position = 0;
+    return s;
+}
+/*- End of function --------------------------------------------------------*/
+
+int tone_gen(tone_gen_state_t *s, int16_t amp[], int max_samples)
+{
+    int samples;
+    int limit;
+    float xamp;
+    int i;
+
+    if (s->current_section < 0)
+        return  0;
+
+    for (samples = 0;  samples < max_samples;  )
+    {
+        limit = samples + s->duration[s->current_section] - s->current_position;
+        if (limit > max_samples)
+            limit = max_samples;
+        
+        s->current_position += (limit - samples);
+        if (s->current_section & 1)
+        {
+            /* A silent section */
+            for (  ;  samples < limit;  samples++)
+                amp[samples] = 0;
+        }
+        else
+        {
+            if (s->tone[0].phase_rate < 0)
+            {
+                for (  ;  samples < limit;  samples++)
+                {
+                    /* There must be two, and only two tones */
+                    xamp = dds_modf(&s->phase[0], -s->tone[0].phase_rate, s->tone[0].gain, 0)
+                         *(1.0f + dds_modf(&s->phase[1], s->tone[1].phase_rate, s->tone[1].gain, 0));
+                    amp[samples] = (int16_t) lrintf(xamp);
+                }
+            }
+            else
+            {
+                for (  ;  samples < limit;  samples++)
+                {
+                    xamp = 0.0f;
+                    for (i = 0;  i < 4;  i++)
+                    {
+                        if (s->tone[i].phase_rate == 0)
+                            break;
+                        xamp += dds_modf(&s->phase[i], s->tone[i].phase_rate, s->tone[i].gain, 0);
+                    }
+                    /* Saturation of the answer is the right thing at this point.
+                       However, we are normally generating well controlled tones,
+                       that cannot clip. So, the overhead of doing saturation is
+                       a waste of valuable time. */
+                    amp[samples] = (int16_t) lrintf(xamp);
+                }
+            }
+        }
+        if (s->current_position >= s->duration[s->current_section])
+        {
+            s->current_position = 0;
+            if (++s->current_section > 3  ||  s->duration[s->current_section] == 0)
+            {
+                if (!s->repeat)
+                {
+                    /* Force a quick exit */
+                    s->current_section = -1;
+                    break;
+                }
+                s->current_section = 0;
+            }
+        }
+    }
+    return samples;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/v17rx.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/v17rx.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,1234 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * v17rx.c - ITU V.17 modem receive part
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2004, 2005, 2006, 2007 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: v17rx.c,v 1.112 2008/07/17 19:12:27 steveu Exp $
+ */
+
+/*! \file */
+
+#if defined(HAVE_CONFIG_H)
+#include <config.h>
+#endif
+
+#include <stdlib.h>
+#include <inttypes.h>
+#include <string.h>
+#include <stdio.h>
+#include "floating_fudge.h"
+#if defined(HAVE_TGMATH_H)
+#include <tgmath.h>
+#endif
+#if defined(HAVE_MATH_H)
+#include <math.h>
+#endif
+
+#include "spandsp/telephony.h"
+#include "spandsp/logging.h"
+#include "spandsp/complex.h"
+#include "spandsp/vector_float.h"
+#include "spandsp/complex_vector_float.h"
+#include "spandsp/async.h"
+#include "spandsp/power_meter.h"
+#include "spandsp/arctan2.h"
+#include "spandsp/dds.h"
+#include "spandsp/complex_filters.h"
+
+#include "spandsp/v29rx.h"
+#include "spandsp/v17tx.h"
+#include "spandsp/v17rx.h"
+
+#include "v17tx_constellation_maps.h"
+#include "v17rx_constellation_maps.h"
+#if defined(SPANDSP_USE_FIXED_POINT)
+#include "v17rx_fixed_rrc.h"
+#else
+#include "v17rx_floating_rrc.h"
+#endif
+
+#define CARRIER_NOMINAL_FREQ            1800.0f
+#define BAUD_RATE                       2400
+#define EQUALIZER_DELTA                 0.21f
+#define EQUALIZER_SLOW_ADAPT_RATIO      0.1f
+
+/* Segments of the training sequence */
+#define V17_TRAINING_SEG_1_LEN          256
+#define V17_TRAINING_SEG_2_LEN          2976
+#define V17_TRAINING_SHORT_SEG_2_LEN    38
+#define V17_TRAINING_SEG_3_LEN          64
+#define V17_TRAINING_SEG_4A_LEN         15
+#define V17_TRAINING_SEG_4_LEN          48
+
+#define V17_BRIDGE_WORD                 0x8880
+
+enum
+{
+    TRAINING_STAGE_NORMAL_OPERATION = 0,
+    TRAINING_STAGE_SYMBOL_ACQUISITION,
+    TRAINING_STAGE_LOG_PHASE,
+    TRAINING_STAGE_SHORT_WAIT_FOR_CDBA,
+    TRAINING_STAGE_WAIT_FOR_CDBA,
+    TRAINING_STAGE_COARSE_TRAIN_ON_CDBA,
+    TRAINING_STAGE_FINE_TRAIN_ON_CDBA,
+    TRAINING_STAGE_SHORT_TRAIN_ON_CDBA_AND_TEST,
+    TRAINING_STAGE_TRAIN_ON_CDBA_AND_TEST,
+    TRAINING_STAGE_BRIDGE,
+    TRAINING_STAGE_TCM_WINDUP,
+    TRAINING_STAGE_TEST_ONES,
+    TRAINING_STAGE_PARKED
+};
+
+/* Coefficients for the band edge symbol timing synchroniser (alpha = 0.99) */
+#define SYNC_LOW_BAND_EDGE_COEFF_0       1.764193f    /* 2*alpha*cos(low_edge) */
+#define SYNC_LOW_BAND_EDGE_COEFF_1      -0.980100f    /* -alpha^2 */
+#define SYNC_HIGH_BAND_EDGE_COEFF_0     -1.400072f    /* 2*alpha*cos(high_edge) */
+#define SYNC_HIGH_BAND_EDGE_COEFF_1     -0.980100f    /* -alpha^2 */
+#define SYNC_CROSS_CORR_COEFF_A         -0.932131f    /* -alpha^2*sin(freq_diff) */
+#define SYNC_CROSS_CORR_COEFF_B          0.700036f    /* alpha*sin(high_edge) */
+#define SYNC_CROSS_CORR_COEFF_C         -0.449451f    /* -alpha*sin(low_edge) */
+
+float v17_rx_carrier_frequency(v17_rx_state_t *s)
+{
+    return dds_frequencyf(s->carrier_phase_rate);
+}
+/*- End of function --------------------------------------------------------*/
+
+float v17_rx_symbol_timing_correction(v17_rx_state_t *s)
+{
+    return (float) s->total_baud_timing_correction/((float) RX_PULSESHAPER_COEFF_SETS*10.0f/3.0f);
+}
+/*- End of function --------------------------------------------------------*/
+
+float v17_rx_signal_power(v17_rx_state_t *s)
+{
+    return power_meter_current_dbm0(&s->power);
+}
+/*- End of function --------------------------------------------------------*/
+
+void v17_rx_signal_cutoff(v17_rx_state_t *s, float cutoff)
+{
+    /* The 0.4 factor allows for the gain of the DC blocker */
+    s->carrier_on_power = (int32_t) (power_meter_level_dbm0(cutoff + 2.5f)*0.4f);
+    s->carrier_off_power = (int32_t) (power_meter_level_dbm0(cutoff - 2.5f)*0.4f);
+}
+/*- End of function --------------------------------------------------------*/
+
+int v17_rx_equalizer_state(v17_rx_state_t *s, complexf_t **coeffs)
+{
+    *coeffs = s->eq_coeff;
+    return V17_EQUALIZER_PRE_LEN + 1 + V17_EQUALIZER_POST_LEN;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void report_status_change(v17_rx_state_t *s, int status)
+{
+    if (s->status_handler)
+        s->status_handler(s->status_user_data, status);
+    else if (s->put_bit)
+        s->put_bit(s->put_bit_user_data, status);
+}
+/*- End of function --------------------------------------------------------*/
+
+static void equalizer_save(v17_rx_state_t *s)
+{
+    cvec_copyf(s->eq_coeff_save, s->eq_coeff, V17_EQUALIZER_PRE_LEN + 1 + V17_EQUALIZER_POST_LEN);
+}
+/*- End of function --------------------------------------------------------*/
+
+static void equalizer_restore(v17_rx_state_t *s)
+{
+    cvec_copyf(s->eq_coeff, s->eq_coeff_save, V17_EQUALIZER_PRE_LEN + 1 + V17_EQUALIZER_POST_LEN);
+    cvec_zerof(s->eq_buf, V17_EQUALIZER_MASK);
+
+    s->eq_put_step = RX_PULSESHAPER_COEFF_SETS*10/(3*2) - 1;
+    s->eq_step = 0;
+    s->eq_delta = EQUALIZER_SLOW_ADAPT_RATIO*EQUALIZER_DELTA/(V17_EQUALIZER_PRE_LEN + 1 + V17_EQUALIZER_POST_LEN);
+}
+/*- End of function --------------------------------------------------------*/
+
+static void equalizer_reset(v17_rx_state_t *s)
+{
+    /* Start with an equalizer based on everything being perfect */
+    cvec_zerof(s->eq_coeff, V17_EQUALIZER_PRE_LEN + 1 + V17_EQUALIZER_POST_LEN);
+    s->eq_coeff[V17_EQUALIZER_PRE_LEN] = complex_setf(3.0f, 0.0f);
+    cvec_zerof(s->eq_buf, V17_EQUALIZER_MASK);
+
+    s->eq_put_step = RX_PULSESHAPER_COEFF_SETS*10/(3*2) - 1;
+    s->eq_step = 0;
+    s->eq_delta = EQUALIZER_DELTA/(V17_EQUALIZER_PRE_LEN + 1 + V17_EQUALIZER_POST_LEN);
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ complexf_t equalizer_get(v17_rx_state_t *s)
+{
+    int i;
+    int p;
+    complexf_t z;
+    complexf_t z1;
+
+    /* Get the next equalized value. */
+    z = complex_setf(0.0f, 0.0f);
+    p = s->eq_step - 1;
+    for (i = 0;  i < V17_EQUALIZER_PRE_LEN + 1 + V17_EQUALIZER_POST_LEN;  i++)
+    {
+        p = (p - 1) & V17_EQUALIZER_MASK;
+        z1 = complex_mulf(&s->eq_coeff[i], &s->eq_buf[p]);
+        z = complex_addf(&z, &z1);
+    }
+    return z;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void tune_equalizer(v17_rx_state_t *s, const complexf_t *z, const complexf_t *target)
+{
+    int i;
+    int p;
+    complexf_t ez;
+    complexf_t z1;
+
+    /* Find the x and y mismatch from the exact constellation position. */
+    ez = complex_subf(target, z);
+    //span_log(&s->logging, SPAN_LOG_FLOW, "Equalizer error %f\n", sqrt(ez.re*ez.re + ez.im*ez.im));
+    ez.re *= s->eq_delta;
+    ez.im *= s->eq_delta;
+
+    p = s->eq_step - 1;
+    for (i = 0;  i < V17_EQUALIZER_PRE_LEN + 1 + V17_EQUALIZER_POST_LEN;  i++)
+    {
+        p = (p - 1) & V17_EQUALIZER_MASK;
+        z1 = complex_conjf(&s->eq_buf[p]);
+        z1 = complex_mulf(&ez, &z1);
+        s->eq_coeff[i] = complex_addf(&s->eq_coeff[i], &z1);
+        /* Leak a little to tame uncontrolled wandering */
+        s->eq_coeff[i].re *= 0.9999f;
+        s->eq_coeff[i].im *= 0.9999f;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static int descramble(v17_rx_state_t *s, int in_bit)
+{
+    int out_bit;
+
+    out_bit = (in_bit ^ (s->scramble_reg >> 17) ^ (s->scramble_reg >> 22)) & 1;
+    s->scramble_reg <<= 1;
+    if (s->training_stage > TRAINING_STAGE_NORMAL_OPERATION  &&  s->training_stage < TRAINING_STAGE_TCM_WINDUP)
+        s->scramble_reg |= out_bit;
+    else
+        s->scramble_reg |= (in_bit & 1);
+    return out_bit;
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ int find_quadrant(complexf_t *z)
+{
+    int b1;
+    int b2;
+
+    /* Split the space along the two diagonals. */
+    b1 = (z->im > z->re);
+    b2 = (z->im < -z->re);
+    return (b2 << 1) | (b1 ^ b2);
+}
+/*- End of function --------------------------------------------------------*/
+
+static void track_carrier(v17_rx_state_t *s, const complexf_t *z, const complexf_t *target)
+{
+    float error;
+
+    /* For small errors the imaginary part of the difference between the actual and the target
+       positions is proportional to the phase error, for any particular target. However, the
+       different amplitudes of the various target positions scale things. */
+    error = z->im*target->re - z->re*target->im;
+    
+    s->carrier_phase_rate += (int32_t) (s->carrier_track_i*error);
+    s->carrier_phase += (int32_t) (s->carrier_track_p*error);
+    //span_log(&s->logging, SPAN_LOG_FLOW, "Im = %15.5f   f = %15.5f\n", error, dds_frequencyf(s->carrier_phase_rate));
+    //printf("XXX Im = %15.5f   f = %15.5f   %f %f %f %f (%f %f)\n", error, dds_frequencyf(s->carrier_phase_rate), target->re, target->im, z->re, z->im, s->carrier_track_i, s->carrier_track_p);
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ void put_bit(v17_rx_state_t *s, int bit)
+{
+    int out_bit;
+
+    /* We need to strip the last part of the training - the test period of all 1s -
+       before we let data go to the application. */
+    if (s->training_stage == TRAINING_STAGE_NORMAL_OPERATION)
+    {
+        out_bit = descramble(s, bit);
+        s->put_bit(s->put_bit_user_data, out_bit);
+    }
+    else if (s->training_stage == TRAINING_STAGE_TEST_ONES)
+    {
+        /* The bits during the final stage of training should be all ones. However,
+           buggy modems mean you cannot rely on this. Therefore we don't bother
+           testing for ones, but just rely on a constellation mismatch measurement. */
+        out_bit = descramble(s, bit);
+        //span_log(&s->logging, SPAN_LOG_FLOW, "A 1 is really %d\n", out_bit);
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+#if defined(SPANDSP_USE_FIXED_POINTx)
+static __inline__ uint32_t dist_sq(const complexi_t *x, const complexi_t *y)
+{
+    return (x->re - y->re)*(x->re - y->re) + (x->im - y->im)*(x->im - y->im);
+}
+/*- End of function --------------------------------------------------------*/
+#else
+static __inline__ float dist_sq(const complexf_t *x, const complexf_t *y)
+{
+    return (x->re - y->re)*(x->re - y->re) + (x->im - y->im)*(x->im - y->im);
+}
+/*- End of function --------------------------------------------------------*/
+#endif
+
+static int decode_baud(v17_rx_state_t *s, complexf_t *z)
+{
+    static const int diff_code[16] =
+    {
+        0, 3, 2, 1, 1, 0, 3, 2, 2, 1, 0, 3, 3, 2, 1, 0                         
+    };
+    static const int tcm_paths[8][4] =
+    {
+        {0, 6, 2, 4},
+        {6, 0, 4, 2},
+        {2, 4, 0, 6},
+        {4, 2, 6, 0},
+        {1, 3, 7, 5},
+        {5, 7, 3, 1},
+        {7, 5, 1, 3},
+        {3, 1, 5, 7}
+    };
+    int nearest;
+    int i;
+    int j;
+    int k;
+    int re;
+    int im;
+    int raw;
+    int constellation_state;
+#if defined(SPANDSP_USE_FIXED_POINTx)
+#define DIST_FACTOR 2048       /* Something less than sqrt(0xFFFFFFFF/10)/10 */
+    complexi_t zi;
+    uint32_t distances[8];
+    uint32_t new_distances[8];
+    uint32_t min;
+    complexi_t ci;
+#else
+    float distances[8];
+    float new_distances[8];
+    float min;
+#endif
+
+    re = (int) ((z->re + 9.0f)*2.0f);
+    if (re > 35)
+        re = 35;
+    else if (re < 0)
+        re = 0;
+    im = (int) ((z->im + 9.0f)*2.0f);
+    if (im > 35)
+        im = 35;
+    else if (im < 0)
+        im = 0;
+
+    /* Find a set of 8 candidate constellation positions, that are the closest
+       to the target, with different patterns in the last 3 bits. */
+#if defined(SPANDSP_USE_FIXED_POINTx)
+    min = 0xFFFFFFFF;
+    zi = complex_seti(z->re*DIST_FACTOR, z->im*DIST_FACTOR);
+#else
+    min = 9999999.0f;
+#endif
+    j = 0;
+    for (i = 0;  i < 8;  i++)
+    {
+        nearest = constel_maps[s->space_map][re][im][i];
+#if defined(SPANDSP_USE_FIXED_POINTx)
+        ci = complex_seti(s->constellation[nearest].re*DIST_FACTOR,
+                          s->constellation[nearest].im*DIST_FACTOR);
+        distances[i] = dist_sq(&ci, &zi);
+#else
+        distances[i] = dist_sq(&s->constellation[nearest], z);
+#endif
+        if (min > distances[i])
+        {
+            min = distances[i];
+            j = i;
+        }
+    }
+    /* Use the nearest of these soft-decisions as the basis for DFE */
+    constellation_state = constel_maps[s->space_map][re][im][j];
+    /* Control the equalizer, carrier tracking, etc. based on the non-trellis
+       corrected information. The trellis correct stuff comes out a bit late. */
+    track_carrier(s, z, &s->constellation[constellation_state]);
+    //tune_equalizer(s, z, &s->constellation[constellation_state]);
+
+    /* Now do the trellis decoding */
+
+    /* TODO: change to processing blocks of stored symbols here, instead of processing
+             one symbol at a time, to speed up the processing. */
+
+    /* Update the minimum accumulated distance to each of the 8 states */
+    if (++s->trellis_ptr >= V17_TRELLIS_STORAGE_DEPTH)
+        s->trellis_ptr = 0;
+    for (i = 0;  i < 4;  i++)
+    {
+        min = distances[tcm_paths[i][0]] + s->distances[0];
+        k = 0;
+        for (j = 1;  j < 4;  j++)
+        {
+            if (min > distances[tcm_paths[i][j]] + s->distances[j << 1])
+            {
+                min = distances[tcm_paths[i][j]] + s->distances[j << 1];
+                k = j;
+            }
+        }
+        /* Use an elementary IIR filter to track the distance to date. */
+#if defined(SPANDSP_USE_FIXED_POINTx)
+        new_distances[i] = s->distances[k << 1]*9/10 + distances[tcm_paths[i][k]]*1/10;
+#else
+        new_distances[i] = s->distances[k << 1]*0.9f + distances[tcm_paths[i][k]]*0.1f;
+#endif
+        s->full_path_to_past_state_locations[s->trellis_ptr][i] = constel_maps[s->space_map][re][im][tcm_paths[i][k]];
+        s->past_state_locations[s->trellis_ptr][i] = k << 1;
+    }
+    for (i = 4;  i < 8;  i++)
+    {
+        min = distances[tcm_paths[i][0]] + s->distances[1];
+        k = 0;
+        for (j = 1;  j < 4;  j++)
+        {
+            if (min > distances[tcm_paths[i][j]] + s->distances[(j << 1) + 1])
+            {
+                min = distances[tcm_paths[i][j]] + s->distances[(j << 1) + 1];
+                k = j;
+            }
+        }
+#if defined(SPANDSP_USE_FIXED_POINTx)
+        new_distances[i] = s->distances[(k << 1) + 1]*9/10 + distances[tcm_paths[i][k]]*1/10;
+#else
+        new_distances[i] = s->distances[(k << 1) + 1]*0.9f + distances[tcm_paths[i][k]]*0.1f;
+#endif
+        s->full_path_to_past_state_locations[s->trellis_ptr][i] = constel_maps[s->space_map][re][im][tcm_paths[i][k]];
+        s->past_state_locations[s->trellis_ptr][i] = (k << 1) + 1;
+    }
+    memcpy(s->distances, new_distances, sizeof(s->distances));
+
+    /* Find the minimum distance to date. This is the start of the path back to the result. */
+    min = s->distances[0];
+    k = 0;
+    for (i = 1;  i < 8;  i++)
+    {
+        if (min > s->distances[i])
+        {
+            min = s->distances[i];
+            k = i;
+        }
+    }
+    /* Trace back through every time step, starting with the current one, and find the
+       state from which the path came one step before. At the end of this search, the
+       last state found also points to the constellation point at that state. This is the
+       output of the trellis. */
+    for (i = 0, j = s->trellis_ptr;  i < V17_TRELLIS_LOOKBACK_DEPTH - 1;  i++)
+    {
+        k = s->past_state_locations[j][k];
+        if (--j < 0)
+            j = V17_TRELLIS_STORAGE_DEPTH - 1;
+    }
+    nearest = s->full_path_to_past_state_locations[j][k] >> 1;
+
+    /* Differentially decode */
+    raw = (nearest & 0x3C) | diff_code[((nearest & 0x03) << 2) | s->diff];
+    s->diff = nearest & 0x03;
+    for (i = 0;  i < s->bits_per_symbol;  i++)
+    {
+        put_bit(s, raw);
+        raw >>= 1;
+    }
+    return constellation_state;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void process_half_baud(v17_rx_state_t *s, const complexf_t *sample)
+{
+    static const complexf_t cdba[4] =
+    {
+        { 6.0f,  2.0f},
+        {-2.0f,  6.0f},
+        { 2.0f, -6.0f},
+        {-6.0f, -2.0f}
+    };
+    complexf_t z;
+    complexf_t zz;
+#if defined(SPANDSP_USE_FIXED_POINTx)
+    const complexi_t *target;
+#else
+    const complexf_t *target;
+#endif
+    float v;
+    float p;
+    int bit;
+    int i;
+    int j;
+    int32_t angle;
+    int32_t ang;
+    int constellation_state;
+
+    /* This routine processes every half a baud, as we put things into the equalizer at the T/2 rate.
+       This routine adapts the position of the half baud samples, which the caller takes. */
+
+    /* Add a sample to the equalizer's circular buffer, but don't calculate anything
+       at this time. */
+    s->eq_buf[s->eq_step] = *sample;
+    s->eq_step = (s->eq_step + 1) & V17_EQUALIZER_MASK;
+
+    /* On alternate insertions we have a whole baud and must process it. */
+    if ((s->baud_half ^= 1))
+        return;
+
+    /* Symbol timing synchronisation */
+    /* Cross correlate */
+    v = s->symbol_sync_low[1]*s->symbol_sync_high[1]*SYNC_CROSS_CORR_COEFF_A
+      + s->symbol_sync_low[0]*s->symbol_sync_high[1]*SYNC_CROSS_CORR_COEFF_B
+      + s->symbol_sync_low[1]*s->symbol_sync_high[0]*SYNC_CROSS_CORR_COEFF_C;
+
+    /* Filter away any DC component  */
+    p = v - s->symbol_sync_dc_filter[1];
+    s->symbol_sync_dc_filter[1] = s->symbol_sync_dc_filter[0];
+    s->symbol_sync_dc_filter[0] = v;
+    /* A little integration will now filter away much of the HF noise */
+    s->baud_phase -= p;
+
+    if (fabsf(s->baud_phase) > 100.0f)
+    {
+        if (s->baud_phase > 0.0f)
+            i = (s->baud_phase > 1000.0f)  ?  15  :  1;
+        else
+            i = (s->baud_phase < -1000.0f)  ?  -15  :  -1;
+        //printf("v = %10.5f %5d - %f %f %d\n", v, i, p, s->baud_phase, s->total_baud_timing_correction);
+
+        s->eq_put_step += i;
+        s->total_baud_timing_correction += i;
+    }
+
+    z = equalizer_get(s);
+
+    constellation_state = 0;
+    switch (s->training_stage)
+    {
+    case TRAINING_STAGE_NORMAL_OPERATION:
+        /* Normal operation. */
+        constellation_state = decode_baud(s, &z);
+        target = &s->constellation[constellation_state];
+        break;
+    case TRAINING_STAGE_SYMBOL_ACQUISITION:
+        /* Allow time for the symbol synchronisation to settle the symbol timing. */
+        target = &z;
+#if defined(IAXMODEM_STUFF)
+        if (++s->training_count >= 100)
+#else
+        if (++s->training_count >= 50)
+#endif
+        {
+            /* Record the current phase angle */
+            s->angles[0] =
+            s->start_angles[0] = arctan2(z.im, z.re);
+            s->training_stage = TRAINING_STAGE_LOG_PHASE;
+            if (s->agc_scaling_save == 0.0f)
+                s->agc_scaling_save = s->agc_scaling;
+        }
+        break;
+    case TRAINING_STAGE_LOG_PHASE:
+        /* Record the current alternate phase angle */
+        target = &z;
+        angle = arctan2(z.im, z.re);
+        s->training_count = 1;
+        if (s->short_train)
+        {
+            /* We should already know the accurate carrier frequency. All we need to sort
+               out is the phase. */
+            /* Check if we just saw A or B */
+            if ((uint32_t) (angle - s->start_angles[0]) < 0x80000000U)
+            {
+                angle = s->start_angles[0];
+                s->angles[0] = 0xC0000000 + 219937506;
+                s->angles[1] = 0x80000000 + 219937506;
+            }
+            else
+            {
+                s->angles[0] = 0x80000000 + 219937506;
+                s->angles[1] = 0xC0000000 + 219937506;
+            }
+            /* Make a step shift in the phase, to pull it into line. We need to rotate the equalizer
+               buffer, as well as the carrier phase, for this to play out nicely. */
+            /* angle is now the difference between where A is, and where it should be */
+            p = 3.14159f + angle*2.0f*3.14159f/(65536.0f*65536.0f) - 0.321751f;
+            span_log(&s->logging, SPAN_LOG_FLOW, "Spin (short) by %.5f rads\n", p);
+            zz = complex_setf(cosf(p), -sinf(p));
+            for (i = 0;  i <= V17_EQUALIZER_MASK;  i++)
+                s->eq_buf[i] = complex_mulf(&s->eq_buf[i], &zz);
+            s->carrier_phase += (0x80000000 + angle - 219937506);
+
+            s->carrier_track_p = 500000.0f;
+
+            s->training_stage = TRAINING_STAGE_SHORT_WAIT_FOR_CDBA;
+        }
+        else
+        {
+            s->angles[1] =
+            s->start_angles[1] = angle;
+            s->training_stage = TRAINING_STAGE_WAIT_FOR_CDBA;
+        }
+        break;
+    case TRAINING_STAGE_WAIT_FOR_CDBA:
+        target = &z;
+        angle = arctan2(z.im, z.re);
+        /* Look for the initial ABAB sequence to display a phase reversal, which will
+           signal the start of the scrambled CDBA segment */
+        ang = angle - s->angles[(s->training_count - 1) & 0xF];
+        s->angles[(s->training_count + 1) & 0xF] = angle;
+
+        /* Do a coarse frequency adjustment about half way through the reversals, as if we wait until
+           the end, we might have rotated too far to correct properly. */
+        if (s->training_count == 100)
+        {
+            i = s->training_count;
+            /* Avoid the possibility of a divide by zero */
+            if (i)
+            {
+                j = i & 0xF;
+                ang = (s->angles[j] - s->start_angles[0])/i
+                    + (s->angles[j | 0x1] - s->start_angles[1])/i;
+                s->carrier_phase_rate += 3*(ang/20);
+                //span_log(&s->logging, SPAN_LOG_FLOW, "Angles %x, %x, %x, %x, dist %d\n", s->angles[j], s->start_angles[0], s->angles[j | 0x1], s->start_angles[1], i);
+
+                s->start_angles[0] = s->angles[j];
+                s->start_angles[1] = s->angles[j | 0x1];
+            }
+            //span_log(&s->logging, SPAN_LOG_FLOW, "%d %d %d %d %d\n", s->angles[s->training_count & 0xF], s->start_angles[0], s->angles[(s->training_count | 0x1) & 0xF], s->start_angles[1], s->training_count);
+            span_log(&s->logging, SPAN_LOG_FLOW, "First coarse carrier frequency %7.2f (%d)\n", dds_frequencyf(s->carrier_phase_rate), s->training_count);
+
+        }
+        if ((ang > 0x40000000  ||  ang < -0x40000000)  &&  s->training_count >= 13)
+        {
+            span_log(&s->logging, SPAN_LOG_FLOW, "We seem to have a reversal at symbol %d\n", s->training_count);
+            /* We seem to have a phase reversal */
+            /* Slam the carrier frequency into line, based on the total phase drift over the last
+               section. Use the shift from the odd bits and the shift from the even bits to get
+               better jitter suppression. */
+            /* TODO: We are supposed to deal with frequancy errors up to +-8Hz. Over 200+
+                     symbols that is more than half a cycle. We get confused an do crazy things.
+                     We can only cope with errors up to 5Hz right now. We need to implement
+                     greater tolerance to be compliant, although it doesn't really matter much
+                     these days. */
+            /* Step back a few symbols so we don't get ISI distorting things. */
+            i = (s->training_count - 8) & ~1;
+            /* Avoid the possibility of a divide by zero */
+            if (i - 100 + 8)
+            {
+                j = i & 0xF;
+                ang = (s->angles[j] - s->start_angles[0])/(i - 100 + 8)
+                    + (s->angles[j | 0x1] - s->start_angles[1])/(i - 100 + 8);
+                s->carrier_phase_rate += 3*(ang/20);
+                span_log(&s->logging, SPAN_LOG_FLOW, "Angles %x, %x, %x, %x, dist %d\n", s->angles[j], s->start_angles[0], s->angles[j | 0x1], s->start_angles[1], i);
+            }
+            //span_log(&s->logging, SPAN_LOG_FLOW, "%d %d %d %d %d\n", s->angles[s->training_count & 0xF], s->start_angles[0], s->angles[(s->training_count | 0x1) & 0xF], s->start_angles[1], s->training_count);
+            span_log(&s->logging, SPAN_LOG_FLOW, "Second coarse carrier frequency %7.2f (%d)\n", dds_frequencyf(s->carrier_phase_rate), s->training_count);
+            /* Check if the carrier frequency is plausible */
+            if (s->carrier_phase_rate < dds_phase_ratef(CARRIER_NOMINAL_FREQ - 20.0f)
+                ||
+                s->carrier_phase_rate > dds_phase_ratef(CARRIER_NOMINAL_FREQ + 20.0f))
+            {
+                span_log(&s->logging, SPAN_LOG_FLOW, "Training failed (sequence failed)\n");
+                /* Park this modem */
+                s->agc_scaling_save = 0.0f;
+                s->training_stage = TRAINING_STAGE_PARKED;
+                report_status_change(s, PUTBIT_TRAINING_FAILED);
+                break;
+            }
+
+            /* Make a step shift in the phase, to pull it into line. We need to rotate the equalizer buffer,
+               as well as the carrier phase, for this to play out nicely. */
+            /* angle is now the difference between where C is, and where it should be */
+            p = angle*2.0f*3.14159f/(65536.0f*65536.0f) - 0.321751f;
+            span_log(&s->logging, SPAN_LOG_FLOW, "Spin (long) by %.5f rads\n", p);
+            zz = complex_setf(cosf(p), -sinf(p));
+            for (i = 0;  i <= V17_EQUALIZER_MASK;  i++)
+                s->eq_buf[i] = complex_mulf(&s->eq_buf[i], &zz);
+            s->carrier_phase += (angle - 219937506);
+
+            /* We have just seen the first symbol of the scrambled sequence, so skip it. */
+            descramble(s, 1);
+            descramble(s, 1);
+            s->training_count = 1;
+            s->training_stage = TRAINING_STAGE_COARSE_TRAIN_ON_CDBA;
+            report_status_change(s, PUTBIT_TRAINING_IN_PROGRESS);
+            break;
+        }
+        if (++s->training_count > V17_TRAINING_SEG_1_LEN)
+        {
+            /* This is bogus. There are not this many bits in this section
+               of a real training sequence. Note that this might be TEP. */
+            span_log(&s->logging, SPAN_LOG_FLOW, "Training failed (sequence failed)\n");
+            /* Park this modem */
+            s->agc_scaling_save = 0.0f;
+            s->training_stage = TRAINING_STAGE_PARKED;
+            report_status_change(s, PUTBIT_TRAINING_FAILED);
+        }
+        break;
+    case TRAINING_STAGE_COARSE_TRAIN_ON_CDBA:
+        /* Train on the scrambled CDBA section. */
+        bit = descramble(s, 1);
+        bit = (bit << 1) | descramble(s, 1);
+        target = &cdba[bit];
+        track_carrier(s, &z, target);
+        tune_equalizer(s, &z, target);
+#if defined(IAXMODEM_STUFF)
+        zz = complex_subf(&z, target);
+        s->training_error = powerf(&zz);
+        if (++s->training_count == V17_TRAINING_SEG_2_LEN - 2000  ||  s->training_error < 1.0f  ||  s->training_error > 200.0f)
+#else
+        if (++s->training_count == V17_TRAINING_SEG_2_LEN - 2000)
+#endif
+        {
+            /* Now the equaliser adaption should be getting somewhere, slow it down, or it will never
+               tune very well on a noisy signal. */
+            s->eq_delta *= EQUALIZER_SLOW_ADAPT_RATIO;
+            s->carrier_track_i = 1000.0f;
+            s->training_stage = TRAINING_STAGE_FINE_TRAIN_ON_CDBA;
+        }
+        break;
+    case TRAINING_STAGE_FINE_TRAIN_ON_CDBA:
+        /* Train on the scrambled CDBA section. */
+        bit = descramble(s, 1);
+        bit = (bit << 1) | descramble(s, 1);
+        target = &cdba[bit];
+        /* By this point the training should be comming into focus. */
+        track_carrier(s, &z, target);
+        tune_equalizer(s, &z, target);
+        if (++s->training_count >= V17_TRAINING_SEG_2_LEN - 48)
+        {
+            s->training_error = 0.0f;
+            s->carrier_track_i = 100.0f;
+            s->carrier_track_p = 500000.0f;
+            s->training_stage = TRAINING_STAGE_TRAIN_ON_CDBA_AND_TEST;
+        }
+        break;
+    case TRAINING_STAGE_TRAIN_ON_CDBA_AND_TEST:
+        /* Continue training on the scrambled CDBA section, but measure the quality of training too. */
+        bit = descramble(s, 1);
+        bit = (bit << 1) | descramble(s, 1);
+        target = &cdba[bit];
+        //span_log(&s->logging, SPAN_LOG_FLOW, "%5d [%15.5f, %15.5f]     [%15.5f, %15.5f]\n", s->training_count, z.re, z.im, cdba[bit].re, cdba[bit].im);
+        /* We ignore the last few symbols because it seems some modems do not end this
+           part properly, and it throws things off. */
+        if (++s->training_count < V17_TRAINING_SEG_2_LEN - 20)
+        {
+            track_carrier(s, &z, target);
+            tune_equalizer(s, &z, target);
+            /* Measure the training error */
+            zz = complex_subf(&z, &cdba[bit]);
+            s->training_error += powerf(&zz);
+        }
+        else if (s->training_count >= V17_TRAINING_SEG_2_LEN)
+        {
+            span_log(&s->logging, SPAN_LOG_FLOW, "Long training error %f\n", s->training_error);
+            if (s->training_error < 40.0f)
+            {
+                s->training_count = 0;
+                s->training_error = 0.0f;
+                s->training_stage = TRAINING_STAGE_BRIDGE;
+            }
+            else
+            {
+                span_log(&s->logging, SPAN_LOG_FLOW, "Training failed (convergence failed)\n");
+                /* Park this modem */
+                s->agc_scaling_save = 0.0f;
+                s->training_stage = TRAINING_STAGE_PARKED;
+                report_status_change(s, PUTBIT_TRAINING_FAILED);
+            }
+        }
+        break;
+    case TRAINING_STAGE_BRIDGE:
+        descramble(s, V17_BRIDGE_WORD >> ((s->training_count & 0x7) << 1));
+        descramble(s, V17_BRIDGE_WORD >> (((s->training_count & 0x7) << 1) + 1));
+        target = &z;
+        if (++s->training_count >= V17_TRAINING_SEG_3_LEN)
+        {
+            s->training_count = 0;
+            s->training_error = 0.0f;
+            s->training_stage = TRAINING_STAGE_TCM_WINDUP;
+        }
+        break;
+    case TRAINING_STAGE_SHORT_WAIT_FOR_CDBA:
+        target = &cdba[(s->training_count & 1) + 2];
+        /* Look for the initial ABAB sequence to display a phase reversal, which will
+           signal the start of the scrambled CDBA segment */
+        angle = arctan2(z.im, z.re);
+        ang = angle - s->angles[s->training_count & 1];
+        if (ang > 0x40000000  ||  ang < -0x40000000)
+        {
+            /* We seem to have a phase reversal */
+            /* We have just seen the first symbol of the scrambled sequence, so skip it. */
+            descramble(s, 1);
+            descramble(s, 1);
+            s->training_count = 1;
+            s->training_error = 0.0f;
+            s->training_stage = TRAINING_STAGE_SHORT_TRAIN_ON_CDBA_AND_TEST;
+            break;
+        }
+        track_carrier(s, &z, target);
+        if (++s->training_count > V17_TRAINING_SEG_1_LEN)
+        {
+            /* This is bogus. There are not this many bits in this section
+               of a real training sequence. Note that this might be TEP. */
+            span_log(&s->logging, SPAN_LOG_FLOW, "Training failed (sequence failed)\n");
+            /* Park this modem */
+            report_status_change(s, PUTBIT_TRAINING_FAILED);
+            s->training_stage = TRAINING_STAGE_PARKED;
+        }
+        break;
+    case TRAINING_STAGE_SHORT_TRAIN_ON_CDBA_AND_TEST:
+        /* Short retrain on the scrambled CDBA section, but measure the quality of training too. */
+        bit = descramble(s, 1);
+        bit = (bit << 1) | descramble(s, 1);
+        //span_log(&s->logging, SPAN_LOG_FLOW, "%5d [%15.5f, %15.5f]     [%15.5f, %15.5f] %d\n", s->training_count, z.re, z.im, cdba[bit].re, cdba[bit].im, arctan2(z.im, z.re));
+        target = &cdba[bit];
+        track_carrier(s, &z, target);
+        //tune_equalizer(s, &z, target);
+        /* Measure the training error */
+        if (s->training_count > 8)
+        {
+            zz = complex_subf(&z, &cdba[bit]);
+            s->training_error += powerf(&zz);
+        }
+        if (++s->training_count >= V17_TRAINING_SHORT_SEG_2_LEN)
+        {
+            span_log(&s->logging, SPAN_LOG_FLOW, "Short training error %f\n", s->training_error);
+            s->carrier_track_i = 100.0f;
+            s->carrier_track_p = 500000.0f;
+            /* TODO: This was changed from 20.0 to 200.0 after studying real world failures.
+                     However, it is not clear why this is an improvement, If something gives
+                     a training error way over 20, surely it shouldn't decode too well? */
+            if (s->training_error < 200.0f)
+            {
+                s->training_count = 0;
+                s->training_stage = TRAINING_STAGE_TCM_WINDUP;
+                report_status_change(s, PUTBIT_TRAINING_IN_PROGRESS);
+            }
+            else
+            {
+                span_log(&s->logging, SPAN_LOG_FLOW, "Short training failed (convergence failed)\n");
+                /* Park this modem */
+                report_status_change(s, PUTBIT_TRAINING_FAILED);
+                s->training_stage = TRAINING_STAGE_PARKED;
+            }
+        }
+        break;
+    case TRAINING_STAGE_TCM_WINDUP:
+        /* We need to wait 15 bauds while the trellis fills up. */
+        //span_log(&s->logging, SPAN_LOG_FLOW, "%5d %15.5f, %15.5f\n", s->training_count, z.re, z.im);
+        constellation_state = decode_baud(s, &z);
+        target = &s->constellation[constellation_state];
+        /* Measure the training error */
+        zz = complex_subf(&z, target);
+        s->training_error += powerf(&zz);
+        if (++s->training_count >= V17_TRAINING_SEG_4A_LEN)
+        {
+            s->training_count = 0;
+            s->training_error = 0.0f;
+            /* Restart the differential decoder */
+            s->diff = (s->short_train)  ?  0  :  1;
+            s->training_stage = TRAINING_STAGE_TEST_ONES;
+        }
+        break;
+    case TRAINING_STAGE_TEST_ONES:
+        /* We are in the test phase, where we check that we can receive reliably.
+           We should get a run of 1's, 48 symbols long. */
+        //span_log(&s->logging, SPAN_LOG_FLOW, "%5d %15.5f, %15.5f\n", s->training_count, z.re, z.im);
+        constellation_state = decode_baud(s, &z);
+        target = &s->constellation[constellation_state];
+        /* Measure the training error */
+        zz = complex_subf(&z, target);
+        s->training_error += powerf(&zz);
+        if (++s->training_count >= V17_TRAINING_SEG_4_LEN)
+        {
+#if defined(IAXMODEM_STUFF)
+            if (s->training_error < 80.0f)
+#else
+            if (s->training_error < 30.0f)
+#endif
+            {
+                /* We are up and running */
+                span_log(&s->logging, SPAN_LOG_FLOW, "Training succeeded (constellation mismatch %f)\n", s->training_error);
+                report_status_change(s, PUTBIT_TRAINING_SUCCEEDED);
+                /* Apply some lag to the carrier off condition, to ensure the last few bits get pushed through
+                   the processing. */
+                s->signal_present = 60;
+                equalizer_save(s);
+                s->carrier_phase_rate_save = s->carrier_phase_rate;
+                s->short_train = TRUE;
+                s->training_stage = TRAINING_STAGE_NORMAL_OPERATION;
+            }
+            else
+            {
+                /* Training has failed */
+                span_log(&s->logging, SPAN_LOG_FLOW, "Training failed (constellation mismatch %f)\n", s->training_error);
+                /* Park this modem */
+                if (!s->short_train)
+                    s->agc_scaling_save = 0.0f;
+                report_status_change(s, PUTBIT_TRAINING_FAILED);
+                s->training_stage = TRAINING_STAGE_PARKED;
+            }
+        }
+        break;
+    case TRAINING_STAGE_PARKED:
+    default:
+        /* We failed to train! */
+        /* Park here until the carrier drops. */
+        target = &z;
+        break;
+    }
+    if (s->qam_report)
+        s->qam_report(s->qam_user_data, &z, target, constellation_state);
+}
+/*- End of function --------------------------------------------------------*/
+
+int v17_rx(v17_rx_state_t *s, const int16_t amp[], int len)
+{
+    int i;
+    int j;
+    int step;
+    int16_t x;
+    int32_t diff;
+    complexf_t z;
+    complexf_t zz;
+    complexf_t sample;
+#if defined(SPANDSP_USE_FIXED_POINT)
+    complexi_t zi;
+#endif
+    int32_t power;
+    float v;
+
+    for (i = 0;  i < len;  i++)
+    {
+        s->rrc_filter[s->rrc_filter_step] =
+        s->rrc_filter[s->rrc_filter_step + V17_RX_FILTER_STEPS] = amp[i];
+        if (++s->rrc_filter_step >= V17_RX_FILTER_STEPS)
+            s->rrc_filter_step = 0;
+
+        /* There should be no DC in the signal, but sometimes there is.
+           We need to measure the power with the DC blocked, but not using
+           a slow to respond DC blocker. Use the most elementary HPF. */
+        x = amp[i] >> 1;
+        diff = x - s->last_sample;
+        power = power_meter_update(&(s->power), diff);
+#if defined(IAXMODEM_STUFF)
+        /* Quick power drop fudge */
+        diff = abs(diff);
+        if (10*diff < s->high_sample)
+        {
+            if (++s->low_samples > 120)
+            {
+                power_meter_init(&(s->power), 4);
+                s->high_sample = 0;
+                s->low_samples = 0;
+            }
+        }
+        else
+        { 
+            s->low_samples = 0;
+            if (diff > s->high_sample)
+               s->high_sample = diff;
+        }
+#endif
+        s->last_sample = x;
+        if (s->signal_present)
+        {
+            /* Look for power below turnoff threshold to turn the carrier off */
+#if defined(IAXMODEM_STUFF)
+            if (s->carrier_drop_pending  ||  power < s->carrier_off_power)
+#else
+            if (power < s->carrier_off_power)
+#endif
+            {
+                if (--s->signal_present <= 0)
+                {
+                    /* Count down a short delay, to ensure we push the last
+                       few bits through the filters before stopping. */
+                    v17_rx_restart(s, s->bit_rate, s->short_train);
+                    report_status_change(s, PUTBIT_CARRIER_DOWN);
+                    continue;
+                }
+#if defined(IAXMODEM_STUFF)
+                /* Carrier has dropped, but the put_bit is
+                   pending the signal_present delay. */
+                s->carrier_drop_pending = TRUE;
+#endif
+            }
+        }
+        else
+        {
+            /* Look for power exceeding turnon threshold to turn the carrier on */
+            if (power < s->carrier_on_power)
+                continue;
+            s->signal_present = 1;
+#if defined(IAXMODEM_STUFF)
+            s->carrier_drop_pending = FALSE;
+#endif
+            report_status_change(s, PUTBIT_CARRIER_UP);
+        }
+        if (s->training_stage == TRAINING_STAGE_PARKED)
+            continue;
+        /* Only spend effort processing this data if the modem is not
+           parked, after training failure. */
+        s->eq_put_step -= RX_PULSESHAPER_COEFF_SETS;
+        step = -s->eq_put_step;
+        if (step > RX_PULSESHAPER_COEFF_SETS - 1)
+            step = RX_PULSESHAPER_COEFF_SETS - 1;
+        if (step < 0)
+            step += RX_PULSESHAPER_COEFF_SETS;
+#if defined(SPANDSP_USE_FIXED_POINT)
+        zi.re = (int32_t) rx_pulseshaper[step][0].re*(int32_t) s->rrc_filter[s->rrc_filter_step];
+        for (j = 1;  j < V17_RX_FILTER_STEPS;  j++)
+            zi.re += (int32_t) rx_pulseshaper[step][j].re*(int32_t) s->rrc_filter[j + s->rrc_filter_step];
+        sample.re = zi.re*s->agc_scaling;
+#else
+        zz.re = rx_pulseshaper[step][0].re*s->rrc_filter[s->rrc_filter_step];
+        for (j = 1;  j < V17_RX_FILTER_STEPS;  j++)
+            zz.re += rx_pulseshaper[step][j].re*s->rrc_filter[j + s->rrc_filter_step];
+        sample.re = zz.re*s->agc_scaling;
+#endif
+
+        /* Symbol timing synchronisation band edge filters */
+        /* Low Nyquist band edge filter */
+        v = s->symbol_sync_low[0]*SYNC_LOW_BAND_EDGE_COEFF_0 + s->symbol_sync_low[1]*SYNC_LOW_BAND_EDGE_COEFF_1 + sample.re;
+        s->symbol_sync_low[1] = s->symbol_sync_low[0];
+        s->symbol_sync_low[0] = v;
+        /* High Nyquist band edge filter */
+        v = s->symbol_sync_high[0]*SYNC_HIGH_BAND_EDGE_COEFF_0 + s->symbol_sync_high[1]*SYNC_HIGH_BAND_EDGE_COEFF_1 + sample.re;
+        s->symbol_sync_high[1] = s->symbol_sync_high[0];
+        s->symbol_sync_high[0] = v;
+
+        /* Put things into the equalization buffer at T/2 rate. The symbol sync.
+           will fiddle the step to align this with the symbols. */
+        if (s->eq_put_step <= 0)
+        {
+            /* Only AGC until we have locked down the setting. */
+            if (s->agc_scaling_save == 0.0f)
+                s->agc_scaling = (1.0f/RX_PULSESHAPER_GAIN)*2.17f/sqrtf(power);
+            /* Pulse shape while still at the carrier frequency, using a quadrature
+               pair of filters. This results in a properly bandpass filtered complex
+               signal, which can be brought directly to baseband by complex mixing.
+               No further filtering, to remove mixer harmonics, is needed. */
+            step = -s->eq_put_step;
+            if (step > RX_PULSESHAPER_COEFF_SETS - 1)
+                step = RX_PULSESHAPER_COEFF_SETS - 1;
+#if defined(SPANDSP_USE_FIXED_POINT)
+            zi.im = (int32_t) rx_pulseshaper[step][0].im*(int32_t) s->rrc_filter[s->rrc_filter_step];
+            for (j = 1;  j < V17_RX_FILTER_STEPS;  j++)
+                zi.im += (int32_t) rx_pulseshaper[step][j].im*(int32_t) s->rrc_filter[j + s->rrc_filter_step];
+            sample.im = zi.im*s->agc_scaling;
+#else
+            zz.im = rx_pulseshaper[step][0].im*s->rrc_filter[s->rrc_filter_step];
+            for (j = 1;  j < V17_RX_FILTER_STEPS;  j++)
+                zz.im += rx_pulseshaper[step][j].im*s->rrc_filter[j + s->rrc_filter_step];
+            sample.im = zz.im*s->agc_scaling;
+#endif
+            s->eq_put_step += RX_PULSESHAPER_COEFF_SETS*10/(3*2);
+            /* Shift to baseband - since this is done in a full complex form, the
+               result is clean, and requires no further filtering, apart from the
+               equalizer. */
+            z = dds_lookup_complexf(s->carrier_phase);
+            zz.re = sample.re*z.re - sample.im*z.im;
+            zz.im = -sample.re*z.im - sample.im*z.re;
+            process_half_baud(s, &zz);
+        }
+        dds_advancef(&(s->carrier_phase), s->carrier_phase_rate);
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+void v17_rx_set_put_bit(v17_rx_state_t *s, put_bit_func_t put_bit, void *user_data)
+{
+    s->put_bit = put_bit;
+    s->put_bit_user_data = user_data;
+}
+/*- End of function --------------------------------------------------------*/
+
+void v17_rx_set_modem_status_handler(v17_rx_state_t *s, modem_tx_status_func_t handler, void *user_data)
+{
+    s->status_handler = handler;
+    s->status_user_data = user_data;
+}
+/*- End of function --------------------------------------------------------*/
+
+int v17_rx_restart(v17_rx_state_t *s, int bit_rate, int short_train)
+{
+    int i;
+
+    span_log(&s->logging, SPAN_LOG_FLOW, "Restarting V.17, %dbps, %s training\n", bit_rate, (short_train)  ?  "short"  :  "long");
+    switch (bit_rate)
+    {
+    case 14400:
+        s->constellation = v17_14400_constellation;
+        s->space_map = 0;
+        s->bits_per_symbol = 6;
+        break;
+    case 12000:
+        s->constellation = v17_12000_constellation;
+        s->space_map = 1;
+        s->bits_per_symbol = 5;
+        break;
+    case 9600:
+        s->constellation = v17_9600_constellation;
+        s->space_map = 2;
+        s->bits_per_symbol = 4;
+        break;
+    case 7200:
+        s->constellation = v17_7200_constellation;
+        s->space_map = 3;
+        s->bits_per_symbol = 3;
+        break;
+    default:
+        return -1;
+    }
+    s->bit_rate = bit_rate;
+#if defined(SPANDSP_USE_FIXED_POINT)
+    memset(s->rrc_filter, 0, sizeof(s->rrc_filter));
+#else
+    vec_zerof(s->rrc_filter, sizeof(s->rrc_filter)/sizeof(s->rrc_filter[0]));
+#endif
+    s->rrc_filter_step = 0;
+
+    s->diff = 1;
+    s->scramble_reg = 0x2ECDD5;
+    s->training_stage = TRAINING_STAGE_SYMBOL_ACQUISITION;
+    s->training_count = 0;
+    s->training_error = 0.0f;
+    s->signal_present = 0;
+#if defined(IAXMODEM_STUFF)
+    s->high_sample = 0;
+    s->low_samples = 0;
+    s->carrier_drop_pending = FALSE;
+#endif
+    if (short_train != 2)
+        s->short_train = short_train;
+    memset(s->start_angles, 0, sizeof(s->start_angles));
+    memset(s->angles, 0, sizeof(s->angles));
+
+    /* Initialise the TCM decoder parameters. */
+    /* The accumulated distance vectors are set so state zero starts
+       at a value of zero, and all others start larger. This forces the
+       initial paths to merge at the zero states. */
+    for (i = 0;  i < 8;  i++)
+#if defined(SPANDSP_USE_FIXED_POINTx)
+        s->distances[i] = 99*DIST_FACTOR*DIST_FACTOR;
+#else
+        s->distances[i] = 99.0f;
+#endif
+    memset(s->full_path_to_past_state_locations, 0, sizeof(s->full_path_to_past_state_locations));
+    memset(s->past_state_locations, 0, sizeof(s->past_state_locations));
+    s->distances[0] = 0;
+    s->trellis_ptr = 14;
+
+    span_log(&s->logging, SPAN_LOG_FLOW, "Phase rates %f %f\n", dds_frequencyf(s->carrier_phase_rate), dds_frequencyf(s->carrier_phase_rate_save));
+    s->carrier_phase = 0;
+    power_meter_init(&(s->power), 4);
+
+    if (s->short_train)
+    {
+        s->carrier_phase_rate = s->carrier_phase_rate_save;
+        s->agc_scaling = s->agc_scaling_save;
+        equalizer_restore(s);
+        /* Don't allow any frequency correction at all, until we start to pull the phase in. */
+        s->carrier_track_i = 0.0f;
+        s->carrier_track_p = 40000.0f;
+    }
+    else
+    {
+        s->carrier_phase_rate = dds_phase_ratef(CARRIER_NOMINAL_FREQ);
+        s->agc_scaling_save = 0.0f;
+        s->agc_scaling = 0.0017f/RX_PULSESHAPER_GAIN;
+        equalizer_reset(s);
+        s->carrier_track_i = 5000.0f;
+        s->carrier_track_p = 40000.0f;
+    }
+    s->last_sample = 0;
+
+    /* Initialise the working data for symbol timing synchronisation */
+    s->symbol_sync_low[0] = 0.0f;
+    s->symbol_sync_low[1] = 0.0f;
+    s->symbol_sync_high[0] = 0.0f;
+    s->symbol_sync_high[1] = 0.0f;
+    s->symbol_sync_dc_filter[0] = 0.0f;
+    s->symbol_sync_dc_filter[1] = 0.0f;
+    s->baud_phase = 0.0f;
+    s->baud_half = 0;
+    
+    s->total_baud_timing_correction = 0;
+
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+v17_rx_state_t *v17_rx_init(v17_rx_state_t *s, int bit_rate, put_bit_func_t put_bit, void *user_data)
+{
+    if (s == NULL)
+    {
+        if ((s = (v17_rx_state_t *) malloc(sizeof(*s))) == NULL)
+            return NULL;
+    }
+    memset(s, 0, sizeof(*s));
+    span_log_init(&s->logging, SPAN_LOG_NONE, NULL);
+    span_log_set_protocol(&s->logging, "V.17 RX");
+    s->put_bit = put_bit;
+    s->put_bit_user_data = user_data;
+    s->short_train = FALSE;
+    v17_rx_signal_cutoff(s, -45.5f);
+    s->agc_scaling = 0.0017f/RX_PULSESHAPER_GAIN;
+    s->agc_scaling_save = 0.0f;
+    s->carrier_phase_rate_save = dds_phase_ratef(CARRIER_NOMINAL_FREQ);
+
+    v17_rx_restart(s, bit_rate, s->short_train);
+    return s;
+}
+/*- End of function --------------------------------------------------------*/
+
+int v17_rx_free(v17_rx_state_t *s)
+{
+    free(s);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+void v17_rx_set_qam_report_handler(v17_rx_state_t *s, qam_report_handler_t handler, void *user_data)
+{
+    s->qam_report = handler;
+    s->qam_user_data = user_data;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/v17rx_constellation_maps.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/v17rx_constellation_maps.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,5516 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * v17rx_constellation_maps.h - ITU V.17 modem receive part.
+ *                              Constellation mapping.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2004 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: v17rx_constellation_maps.h,v 1.6 2008/07/10 13:34:00 steveu Exp $
+ */
+
+/* The following table maps the 8 soft-decisions associated with every possible point in the
+   constellation space. If you look at the constellations carefully, all 4 can be accurately
+   mapped at 0.5 unit resolution. */
+static const uint8_t constel_maps[4][36][36][8] =
+{
+    {   /* 14,400bps map */
+        {
+            {0x50, 0x69, 0x5a, 0x63, 0x6c, 0x55, 0x66, 0x5f},
+            {0x50, 0x69, 0x5a, 0x63, 0x6c, 0x55, 0x66, 0x5f},
+            {0x50, 0x69, 0x5a, 0x63, 0x6c, 0x55, 0x66, 0x5f},
+            {0x00, 0x49, 0x5a, 0x63, 0x6c, 0x55, 0x66, 0x5f},
+            {0x00, 0x49, 0x5a, 0x63, 0x6c, 0x55, 0x66, 0x5f},
+            {0x00, 0x49, 0x5a, 0x63, 0x6c, 0x55, 0x66, 0x5f},
+            {0x00, 0x49, 0x5a, 0x63, 0x6c, 0x55, 0x66, 0x5f},
+            {0x00, 0x49, 0x5a, 0x63, 0x4c, 0x05, 0x66, 0x5f},
+            {0x00, 0x49, 0x5a, 0x63, 0x4c, 0x05, 0x66, 0x5f},
+            {0x00, 0x49, 0x5a, 0x63, 0x4c, 0x05, 0x66, 0x5f},
+            {0x00, 0x49, 0x5a, 0x63, 0x4c, 0x05, 0x66, 0x5f},
+            {0x00, 0x49, 0x5a, 0x63, 0x4c, 0x05, 0x66, 0x5f},
+            {0x00, 0x49, 0x7a, 0x63, 0x4c, 0x05, 0x66, 0x5f},
+            {0x00, 0x49, 0x7a, 0x63, 0x4c, 0x05, 0x66, 0x5f},
+            {0x00, 0x49, 0x7a, 0x73, 0x4c, 0x05, 0x66, 0x7f},
+            {0x00, 0x49, 0x7a, 0x73, 0x4c, 0x05, 0x66, 0x7f},
+            {0x08, 0x49, 0x7a, 0x73, 0x4c, 0x05, 0x76, 0x7f},
+            {0x08, 0x49, 0x7a, 0x73, 0x4c, 0x05, 0x76, 0x7f},
+            {0x08, 0x41, 0x7a, 0x73, 0x4c, 0x0d, 0x76, 0x7f},
+            {0x08, 0x41, 0x7a, 0x73, 0x4c, 0x0d, 0x76, 0x7f},
+            {0x08, 0x41, 0x6a, 0x73, 0x44, 0x0d, 0x76, 0x7f},
+            {0x08, 0x41, 0x6a, 0x73, 0x44, 0x0d, 0x76, 0x7f},
+            {0x08, 0x41, 0x6a, 0x53, 0x44, 0x0d, 0x76, 0x6f},
+            {0x08, 0x41, 0x6a, 0x53, 0x44, 0x0d, 0x76, 0x6f},
+            {0x08, 0x41, 0x6a, 0x53, 0x44, 0x0d, 0x56, 0x6f},
+            {0x08, 0x41, 0x6a, 0x53, 0x44, 0x0d, 0x56, 0x6f},
+            {0x08, 0x41, 0x6a, 0x53, 0x44, 0x0d, 0x56, 0x6f},
+            {0x08, 0x41, 0x6a, 0x53, 0x44, 0x0d, 0x56, 0x6f},
+            {0x08, 0x41, 0x6a, 0x53, 0x44, 0x0d, 0x56, 0x6f},
+            {0x08, 0x41, 0x6a, 0x53, 0x44, 0x0d, 0x56, 0x6f},
+            {0x58, 0x41, 0x6a, 0x53, 0x44, 0x5d, 0x56, 0x6f},
+            {0x58, 0x41, 0x6a, 0x53, 0x44, 0x5d, 0x56, 0x6f},
+            {0x58, 0x41, 0x6a, 0x53, 0x44, 0x5d, 0x56, 0x6f},
+            {0x58, 0x41, 0x6a, 0x53, 0x44, 0x5d, 0x56, 0x6f},
+            {0x58, 0x61, 0x6a, 0x53, 0x64, 0x5d, 0x56, 0x6f},
+            {0x58, 0x61, 0x6a, 0x53, 0x64, 0x5d, 0x56, 0x6f}
+        },
+        {
+            {0x50, 0x69, 0x5a, 0x63, 0x6c, 0x55, 0x66, 0x5f},
+            {0x50, 0x69, 0x5a, 0x63, 0x6c, 0x55, 0x66, 0x5f},
+            {0x50, 0x69, 0x5a, 0x63, 0x6c, 0x55, 0x66, 0x5f},
+            {0x50, 0x69, 0x5a, 0x63, 0x6c, 0x55, 0x66, 0x5f},
+            {0x00, 0x49, 0x5a, 0x63, 0x6c, 0x55, 0x66, 0x5f},
+            {0x00, 0x49, 0x5a, 0x63, 0x6c, 0x55, 0x66, 0x5f},
+            {0x00, 0x49, 0x5a, 0x63, 0x6c, 0x55, 0x66, 0x5f},
+            {0x00, 0x49, 0x5a, 0x63, 0x6c, 0x55, 0x66, 0x5f},
+            {0x00, 0x49, 0x5a, 0x63, 0x4c, 0x05, 0x66, 0x5f},
+            {0x00, 0x49, 0x5a, 0x63, 0x4c, 0x05, 0x66, 0x5f},
+            {0x00, 0x49, 0x5a, 0x63, 0x4c, 0x05, 0x66, 0x5f},
+            {0x00, 0x49, 0x5a, 0x63, 0x4c, 0x05, 0x66, 0x5f},
+            {0x00, 0x49, 0x7a, 0x63, 0x4c, 0x05, 0x66, 0x5f},
+            {0x00, 0x49, 0x7a, 0x63, 0x4c, 0x05, 0x66, 0x5f},
+            {0x00, 0x49, 0x7a, 0x73, 0x4c, 0x05, 0x66, 0x7f},
+            {0x00, 0x49, 0x7a, 0x73, 0x4c, 0x05, 0x66, 0x7f},
+            {0x08, 0x49, 0x7a, 0x73, 0x4c, 0x05, 0x76, 0x7f},
+            {0x08, 0x49, 0x7a, 0x73, 0x4c, 0x05, 0x76, 0x7f},
+            {0x08, 0x41, 0x7a, 0x73, 0x4c, 0x0d, 0x76, 0x7f},
+            {0x08, 0x41, 0x7a, 0x73, 0x4c, 0x0d, 0x76, 0x7f},
+            {0x08, 0x41, 0x6a, 0x73, 0x44, 0x0d, 0x76, 0x7f},
+            {0x08, 0x41, 0x6a, 0x73, 0x44, 0x0d, 0x76, 0x7f},
+            {0x08, 0x41, 0x6a, 0x53, 0x44, 0x0d, 0x76, 0x6f},
+            {0x08, 0x41, 0x6a, 0x53, 0x44, 0x0d, 0x76, 0x6f},
+            {0x08, 0x41, 0x6a, 0x53, 0x44, 0x0d, 0x56, 0x6f},
+            {0x08, 0x41, 0x6a, 0x53, 0x44, 0x0d, 0x56, 0x6f},
+            {0x08, 0x41, 0x6a, 0x53, 0x44, 0x0d, 0x56, 0x6f},
+            {0x08, 0x41, 0x6a, 0x53, 0x44, 0x0d, 0x56, 0x6f},
+            {0x08, 0x41, 0x6a, 0x53, 0x44, 0x0d, 0x56, 0x6f},
+            {0x58, 0x41, 0x6a, 0x53, 0x44, 0x5d, 0x56, 0x6f},
+            {0x58, 0x41, 0x6a, 0x53, 0x44, 0x5d, 0x56, 0x6f},
+            {0x58, 0x41, 0x6a, 0x53, 0x44, 0x5d, 0x56, 0x6f},
+            {0x58, 0x41, 0x6a, 0x53, 0x44, 0x5d, 0x56, 0x6f},
+            {0x58, 0x61, 0x6a, 0x53, 0x64, 0x5d, 0x56, 0x6f},
+            {0x58, 0x61, 0x6a, 0x53, 0x64, 0x5d, 0x56, 0x6f},
+            {0x58, 0x61, 0x6a, 0x53, 0x64, 0x5d, 0x56, 0x6f}
+        },
+        {
+            {0x50, 0x69, 0x5a, 0x63, 0x6c, 0x55, 0x66, 0x5f},
+            {0x50, 0x69, 0x5a, 0x63, 0x6c, 0x55, 0x66, 0x5f},
+            {0x50, 0x69, 0x5a, 0x63, 0x6c, 0x55, 0x66, 0x5f},
+            {0x50, 0x69, 0x5a, 0x63, 0x6c, 0x55, 0x66, 0x5f},
+            {0x50, 0x69, 0x5a, 0x63, 0x6c, 0x55, 0x66, 0x5f},
+            {0x00, 0x49, 0x5a, 0x63, 0x6c, 0x55, 0x66, 0x5f},
+            {0x00, 0x49, 0x5a, 0x63, 0x6c, 0x55, 0x66, 0x5f},
+            {0x00, 0x49, 0x5a, 0x63, 0x6c, 0x55, 0x66, 0x5f},
+            {0x00, 0x49, 0x5a, 0x63, 0x6c, 0x55, 0x66, 0x5f},
+            {0x00, 0x49, 0x5a, 0x63, 0x4c, 0x05, 0x66, 0x5f},
+            {0x00, 0x49, 0x5a, 0x63, 0x4c, 0x05, 0x66, 0x5f},
+            {0x00, 0x49, 0x5a, 0x63, 0x4c, 0x05, 0x66, 0x5f},
+            {0x00, 0x49, 0x7a, 0x63, 0x4c, 0x05, 0x66, 0x5f},
+            {0x00, 0x49, 0x7a, 0x63, 0x4c, 0x05, 0x66, 0x5f},
+            {0x00, 0x49, 0x7a, 0x73, 0x4c, 0x05, 0x66, 0x7f},
+            {0x00, 0x49, 0x7a, 0x73, 0x4c, 0x05, 0x66, 0x7f},
+            {0x08, 0x49, 0x7a, 0x73, 0x4c, 0x05, 0x76, 0x7f},
+            {0x08, 0x49, 0x7a, 0x73, 0x4c, 0x05, 0x76, 0x7f},
+            {0x08, 0x41, 0x7a, 0x73, 0x4c, 0x0d, 0x76, 0x7f},
+            {0x08, 0x41, 0x7a, 0x73, 0x4c, 0x0d, 0x76, 0x7f},
+            {0x08, 0x41, 0x6a, 0x73, 0x44, 0x0d, 0x76, 0x7f},
+            {0x08, 0x41, 0x6a, 0x73, 0x44, 0x0d, 0x76, 0x7f},
+            {0x08, 0x41, 0x6a, 0x53, 0x44, 0x0d, 0x76, 0x6f},
+            {0x08, 0x41, 0x6a, 0x53, 0x44, 0x0d, 0x76, 0x6f},
+            {0x08, 0x41, 0x6a, 0x53, 0x44, 0x0d, 0x56, 0x6f},
+            {0x08, 0x41, 0x6a, 0x53, 0x44, 0x0d, 0x56, 0x6f},
+            {0x08, 0x41, 0x6a, 0x53, 0x44, 0x0d, 0x56, 0x6f},
+            {0x08, 0x41, 0x6a, 0x53, 0x44, 0x0d, 0x56, 0x6f},
+            {0x58, 0x41, 0x6a, 0x53, 0x44, 0x5d, 0x56, 0x6f},
+            {0x58, 0x41, 0x6a, 0x53, 0x44, 0x5d, 0x56, 0x6f},
+            {0x58, 0x41, 0x6a, 0x53, 0x44, 0x5d, 0x56, 0x6f},
+            {0x58, 0x41, 0x6a, 0x53, 0x44, 0x5d, 0x56, 0x6f},
+            {0x58, 0x61, 0x6a, 0x53, 0x64, 0x5d, 0x56, 0x6f},
+            {0x58, 0x61, 0x6a, 0x53, 0x64, 0x5d, 0x56, 0x6f},
+            {0x58, 0x61, 0x6a, 0x53, 0x64, 0x5d, 0x56, 0x6f},
+            {0x58, 0x61, 0x6a, 0x53, 0x64, 0x5d, 0x56, 0x6f}
+        },
+        {
+            {0x50, 0x69, 0x5a, 0x43, 0x6c, 0x55, 0x46, 0x5f},
+            {0x50, 0x69, 0x5a, 0x63, 0x6c, 0x55, 0x66, 0x5f},
+            {0x50, 0x69, 0x5a, 0x63, 0x6c, 0x55, 0x66, 0x5f},
+            {0x50, 0x69, 0x5a, 0x63, 0x6c, 0x55, 0x66, 0x5f},
+            {0x50, 0x69, 0x5a, 0x63, 0x6c, 0x55, 0x66, 0x5f},
+            {0x50, 0x69, 0x5a, 0x63, 0x6c, 0x55, 0x66, 0x5f},
+            {0x00, 0x49, 0x5a, 0x63, 0x6c, 0x55, 0x66, 0x5f},
+            {0x00, 0x49, 0x5a, 0x63, 0x6c, 0x55, 0x66, 0x5f},
+            {0x00, 0x49, 0x5a, 0x63, 0x6c, 0x55, 0x66, 0x5f},
+            {0x00, 0x49, 0x5a, 0x63, 0x6c, 0x55, 0x66, 0x5f},
+            {0x00, 0x49, 0x5a, 0x63, 0x4c, 0x05, 0x66, 0x5f},
+            {0x00, 0x49, 0x5a, 0x63, 0x4c, 0x05, 0x66, 0x5f},
+            {0x00, 0x49, 0x7a, 0x63, 0x4c, 0x05, 0x66, 0x5f},
+            {0x00, 0x49, 0x7a, 0x63, 0x4c, 0x05, 0x66, 0x5f},
+            {0x00, 0x49, 0x7a, 0x73, 0x4c, 0x05, 0x66, 0x7f},
+            {0x00, 0x49, 0x7a, 0x73, 0x4c, 0x05, 0x66, 0x7f},
+            {0x08, 0x49, 0x7a, 0x73, 0x4c, 0x05, 0x76, 0x7f},
+            {0x08, 0x49, 0x7a, 0x73, 0x4c, 0x05, 0x76, 0x7f},
+            {0x08, 0x41, 0x7a, 0x73, 0x4c, 0x0d, 0x76, 0x7f},
+            {0x08, 0x41, 0x7a, 0x73, 0x4c, 0x0d, 0x76, 0x7f},
+            {0x08, 0x41, 0x6a, 0x73, 0x44, 0x0d, 0x76, 0x7f},
+            {0x08, 0x41, 0x6a, 0x73, 0x44, 0x0d, 0x76, 0x7f},
+            {0x08, 0x41, 0x6a, 0x53, 0x44, 0x0d, 0x76, 0x6f},
+            {0x08, 0x41, 0x6a, 0x53, 0x44, 0x0d, 0x76, 0x6f},
+            {0x08, 0x41, 0x6a, 0x53, 0x44, 0x0d, 0x56, 0x6f},
+            {0x08, 0x41, 0x6a, 0x53, 0x44, 0x0d, 0x56, 0x6f},
+            {0x08, 0x41, 0x6a, 0x53, 0x44, 0x0d, 0x56, 0x6f},
+            {0x58, 0x41, 0x6a, 0x53, 0x44, 0x5d, 0x56, 0x6f},
+            {0x58, 0x41, 0x6a, 0x53, 0x44, 0x5d, 0x56, 0x6f},
+            {0x58, 0x41, 0x6a, 0x53, 0x44, 0x5d, 0x56, 0x6f},
+            {0x58, 0x41, 0x6a, 0x53, 0x44, 0x5d, 0x56, 0x6f},
+            {0x58, 0x61, 0x6a, 0x53, 0x64, 0x5d, 0x56, 0x6f},
+            {0x58, 0x61, 0x6a, 0x53, 0x64, 0x5d, 0x56, 0x6f},
+            {0x58, 0x61, 0x6a, 0x53, 0x64, 0x5d, 0x56, 0x6f},
+            {0x58, 0x61, 0x6a, 0x53, 0x64, 0x5d, 0x56, 0x6f},
+            {0x58, 0x61, 0x4a, 0x03, 0x64, 0x5d, 0x56, 0x6f}
+        },
+        {
+            {0x50, 0x69, 0x5a, 0x43, 0x6c, 0x55, 0x46, 0x5f},
+            {0x50, 0x69, 0x5a, 0x43, 0x6c, 0x55, 0x46, 0x5f},
+            {0x50, 0x69, 0x5a, 0x63, 0x6c, 0x55, 0x66, 0x5f},
+            {0x50, 0x69, 0x5a, 0x63, 0x6c, 0x55, 0x66, 0x5f},
+            {0x50, 0x69, 0x5a, 0x63, 0x6c, 0x55, 0x66, 0x5f},
+            {0x50, 0x69, 0x5a, 0x63, 0x6c, 0x55, 0x66, 0x5f},
+            {0x50, 0x69, 0x5a, 0x63, 0x6c, 0x55, 0x66, 0x5f},
+            {0x00, 0x49, 0x5a, 0x63, 0x6c, 0x55, 0x66, 0x5f},
+            {0x00, 0x49, 0x5a, 0x63, 0x6c, 0x55, 0x66, 0x5f},
+            {0x00, 0x49, 0x5a, 0x63, 0x6c, 0x55, 0x66, 0x5f},
+            {0x00, 0x49, 0x5a, 0x63, 0x6c, 0x15, 0x66, 0x5f},
+            {0x00, 0x49, 0x5a, 0x63, 0x4c, 0x15, 0x66, 0x5f},
+            {0x00, 0x49, 0x7a, 0x63, 0x4c, 0x15, 0x66, 0x5f},
+            {0x00, 0x49, 0x7a, 0x63, 0x4c, 0x15, 0x66, 0x5f},
+            {0x00, 0x49, 0x7a, 0x73, 0x4c, 0x15, 0x66, 0x7f},
+            {0x00, 0x49, 0x7a, 0x73, 0x4c, 0x15, 0x66, 0x7f},
+            {0x08, 0x49, 0x7a, 0x73, 0x4c, 0x15, 0x76, 0x7f},
+            {0x08, 0x49, 0x7a, 0x73, 0x4c, 0x15, 0x76, 0x7f},
+            {0x08, 0x41, 0x7a, 0x73, 0x4c, 0x1d, 0x76, 0x7f},
+            {0x08, 0x41, 0x7a, 0x73, 0x4c, 0x1d, 0x76, 0x7f},
+            {0x08, 0x41, 0x6a, 0x73, 0x44, 0x1d, 0x76, 0x7f},
+            {0x08, 0x41, 0x6a, 0x73, 0x44, 0x1d, 0x76, 0x7f},
+            {0x08, 0x41, 0x6a, 0x53, 0x44, 0x1d, 0x76, 0x6f},
+            {0x08, 0x41, 0x6a, 0x53, 0x44, 0x1d, 0x76, 0x6f},
+            {0x08, 0x41, 0x6a, 0x53, 0x44, 0x1d, 0x56, 0x6f},
+            {0x08, 0x41, 0x6a, 0x53, 0x44, 0x1d, 0x56, 0x6f},
+            {0x58, 0x41, 0x6a, 0x53, 0x44, 0x5d, 0x56, 0x6f},
+            {0x58, 0x41, 0x6a, 0x53, 0x44, 0x5d, 0x56, 0x6f},
+            {0x58, 0x41, 0x6a, 0x53, 0x44, 0x5d, 0x56, 0x6f},
+            {0x58, 0x41, 0x6a, 0x53, 0x44, 0x5d, 0x56, 0x6f},
+            {0x58, 0x61, 0x6a, 0x53, 0x64, 0x5d, 0x56, 0x6f},
+            {0x58, 0x61, 0x6a, 0x53, 0x64, 0x5d, 0x56, 0x6f},
+            {0x58, 0x61, 0x6a, 0x53, 0x64, 0x5d, 0x56, 0x6f},
+            {0x58, 0x61, 0x6a, 0x53, 0x64, 0x5d, 0x56, 0x6f},
+            {0x58, 0x61, 0x4a, 0x03, 0x64, 0x5d, 0x56, 0x6f},
+            {0x58, 0x61, 0x4a, 0x03, 0x64, 0x5d, 0x56, 0x6f}
+        },
+        {
+            {0x50, 0x69, 0x5a, 0x43, 0x6c, 0x55, 0x46, 0x5f},
+            {0x50, 0x69, 0x5a, 0x43, 0x6c, 0x55, 0x46, 0x5f},
+            {0x50, 0x69, 0x5a, 0x43, 0x6c, 0x55, 0x46, 0x5f},
+            {0x50, 0x69, 0x5a, 0x63, 0x6c, 0x55, 0x66, 0x5f},
+            {0x50, 0x69, 0x5a, 0x63, 0x6c, 0x55, 0x66, 0x5f},
+            {0x50, 0x69, 0x5a, 0x63, 0x6c, 0x55, 0x66, 0x5f},
+            {0x50, 0x69, 0x5a, 0x63, 0x6c, 0x55, 0x66, 0x5f},
+            {0x50, 0x69, 0x5a, 0x63, 0x6c, 0x55, 0x66, 0x5f},
+            {0x00, 0x49, 0x5a, 0x63, 0x6c, 0x55, 0x66, 0x5f},
+            {0x00, 0x49, 0x5a, 0x63, 0x6c, 0x55, 0x66, 0x5f},
+            {0x00, 0x49, 0x5a, 0x63, 0x6c, 0x15, 0x66, 0x5f},
+            {0x00, 0x49, 0x5a, 0x63, 0x6c, 0x15, 0x66, 0x5f},
+            {0x00, 0x49, 0x7a, 0x63, 0x4c, 0x15, 0x66, 0x5f},
+            {0x00, 0x49, 0x7a, 0x63, 0x4c, 0x15, 0x66, 0x5f},
+            {0x00, 0x49, 0x7a, 0x73, 0x4c, 0x15, 0x66, 0x7f},
+            {0x00, 0x49, 0x7a, 0x73, 0x4c, 0x15, 0x66, 0x7f},
+            {0x08, 0x49, 0x7a, 0x73, 0x4c, 0x15, 0x76, 0x7f},
+            {0x08, 0x49, 0x7a, 0x73, 0x4c, 0x15, 0x76, 0x7f},
+            {0x08, 0x41, 0x7a, 0x73, 0x4c, 0x1d, 0x76, 0x7f},
+            {0x08, 0x41, 0x7a, 0x73, 0x4c, 0x1d, 0x76, 0x7f},
+            {0x08, 0x41, 0x6a, 0x73, 0x44, 0x1d, 0x76, 0x7f},
+            {0x08, 0x41, 0x6a, 0x73, 0x44, 0x1d, 0x76, 0x7f},
+            {0x08, 0x41, 0x6a, 0x53, 0x44, 0x1d, 0x76, 0x6f},
+            {0x08, 0x41, 0x6a, 0x53, 0x44, 0x1d, 0x76, 0x6f},
+            {0x08, 0x41, 0x6a, 0x53, 0x44, 0x1d, 0x56, 0x6f},
+            {0x58, 0x41, 0x6a, 0x53, 0x44, 0x1d, 0x56, 0x6f},
+            {0x58, 0x41, 0x6a, 0x53, 0x44, 0x5d, 0x56, 0x6f},
+            {0x58, 0x41, 0x6a, 0x53, 0x44, 0x5d, 0x56, 0x6f},
+            {0x58, 0x41, 0x6a, 0x53, 0x44, 0x5d, 0x56, 0x6f},
+            {0x58, 0x61, 0x6a, 0x53, 0x64, 0x5d, 0x56, 0x6f},
+            {0x58, 0x61, 0x6a, 0x53, 0x64, 0x5d, 0x56, 0x6f},
+            {0x58, 0x61, 0x6a, 0x53, 0x64, 0x5d, 0x56, 0x6f},
+            {0x58, 0x61, 0x6a, 0x53, 0x64, 0x5d, 0x56, 0x6f},
+            {0x58, 0x61, 0x4a, 0x03, 0x64, 0x5d, 0x56, 0x6f},
+            {0x58, 0x61, 0x4a, 0x03, 0x64, 0x5d, 0x56, 0x6f},
+            {0x58, 0x61, 0x4a, 0x03, 0x64, 0x5d, 0x56, 0x6f}
+        },
+        {
+            {0x50, 0x69, 0x5a, 0x43, 0x6c, 0x55, 0x46, 0x5f},
+            {0x50, 0x69, 0x5a, 0x43, 0x6c, 0x55, 0x46, 0x5f},
+            {0x50, 0x69, 0x5a, 0x43, 0x6c, 0x55, 0x46, 0x5f},
+            {0x50, 0x69, 0x5a, 0x43, 0x6c, 0x55, 0x46, 0x5f},
+            {0x50, 0x69, 0x5a, 0x63, 0x6c, 0x55, 0x66, 0x5f},
+            {0x50, 0x69, 0x5a, 0x63, 0x6c, 0x55, 0x66, 0x5f},
+            {0x50, 0x69, 0x5a, 0x63, 0x6c, 0x55, 0x66, 0x5f},
+            {0x50, 0x69, 0x5a, 0x63, 0x6c, 0x55, 0x66, 0x5f},
+            {0x10, 0x69, 0x5a, 0x63, 0x6c, 0x55, 0x66, 0x5f},
+            {0x10, 0x49, 0x5a, 0x63, 0x6c, 0x55, 0x66, 0x5f},
+            {0x10, 0x49, 0x5a, 0x63, 0x6c, 0x15, 0x66, 0x5f},
+            {0x10, 0x49, 0x5a, 0x63, 0x6c, 0x15, 0x66, 0x5f},
+            {0x10, 0x49, 0x7a, 0x63, 0x2c, 0x15, 0x66, 0x5f},
+            {0x10, 0x49, 0x7a, 0x63, 0x2c, 0x15, 0x66, 0x5f},
+            {0x10, 0x49, 0x7a, 0x73, 0x2c, 0x15, 0x66, 0x7f},
+            {0x10, 0x49, 0x7a, 0x73, 0x2c, 0x15, 0x66, 0x7f},
+            {0x18, 0x49, 0x7a, 0x73, 0x2c, 0x15, 0x76, 0x7f},
+            {0x18, 0x49, 0x7a, 0x73, 0x2c, 0x15, 0x76, 0x7f},
+            {0x18, 0x41, 0x7a, 0x73, 0x2c, 0x1d, 0x76, 0x7f},
+            {0x18, 0x41, 0x7a, 0x73, 0x2c, 0x1d, 0x76, 0x7f},
+            {0x18, 0x41, 0x6a, 0x73, 0x24, 0x1d, 0x76, 0x7f},
+            {0x18, 0x41, 0x6a, 0x73, 0x24, 0x1d, 0x76, 0x7f},
+            {0x18, 0x41, 0x6a, 0x53, 0x24, 0x1d, 0x76, 0x6f},
+            {0x18, 0x41, 0x6a, 0x53, 0x24, 0x1d, 0x76, 0x6f},
+            {0x58, 0x41, 0x6a, 0x53, 0x24, 0x1d, 0x56, 0x6f},
+            {0x58, 0x41, 0x6a, 0x53, 0x24, 0x1d, 0x56, 0x6f},
+            {0x58, 0x41, 0x6a, 0x53, 0x24, 0x5d, 0x56, 0x6f},
+            {0x58, 0x41, 0x6a, 0x53, 0x24, 0x5d, 0x56, 0x6f},
+            {0x58, 0x61, 0x6a, 0x53, 0x64, 0x5d, 0x56, 0x6f},
+            {0x58, 0x61, 0x6a, 0x53, 0x64, 0x5d, 0x56, 0x6f},
+            {0x58, 0x61, 0x6a, 0x53, 0x64, 0x5d, 0x56, 0x6f},
+            {0x58, 0x61, 0x6a, 0x53, 0x64, 0x5d, 0x56, 0x6f},
+            {0x58, 0x61, 0x4a, 0x03, 0x64, 0x5d, 0x56, 0x6f},
+            {0x58, 0x61, 0x4a, 0x03, 0x64, 0x5d, 0x56, 0x6f},
+            {0x58, 0x61, 0x4a, 0x03, 0x64, 0x5d, 0x56, 0x6f},
+            {0x58, 0x61, 0x4a, 0x03, 0x64, 0x5d, 0x56, 0x6f}
+        },
+        {
+            {0x50, 0x69, 0x0a, 0x43, 0x6c, 0x55, 0x46, 0x0f},
+            {0x50, 0x69, 0x5a, 0x43, 0x6c, 0x55, 0x46, 0x5f},
+            {0x50, 0x69, 0x5a, 0x43, 0x6c, 0x55, 0x46, 0x5f},
+            {0x50, 0x69, 0x5a, 0x43, 0x6c, 0x55, 0x46, 0x5f},
+            {0x50, 0x69, 0x5a, 0x43, 0x6c, 0x55, 0x46, 0x5f},
+            {0x50, 0x69, 0x5a, 0x63, 0x6c, 0x55, 0x66, 0x5f},
+            {0x50, 0x69, 0x5a, 0x63, 0x6c, 0x55, 0x66, 0x5f},
+            {0x50, 0x69, 0x5a, 0x63, 0x6c, 0x55, 0x66, 0x5f},
+            {0x10, 0x69, 0x5a, 0x63, 0x6c, 0x55, 0x66, 0x5f},
+            {0x10, 0x69, 0x5a, 0x63, 0x6c, 0x55, 0x66, 0x5f},
+            {0x10, 0x49, 0x5a, 0x63, 0x6c, 0x15, 0x66, 0x5f},
+            {0x10, 0x49, 0x5a, 0x63, 0x6c, 0x15, 0x66, 0x5f},
+            {0x10, 0x49, 0x7a, 0x63, 0x2c, 0x15, 0x66, 0x5f},
+            {0x10, 0x49, 0x7a, 0x63, 0x2c, 0x15, 0x66, 0x5f},
+            {0x10, 0x49, 0x7a, 0x73, 0x2c, 0x15, 0x66, 0x7f},
+            {0x10, 0x49, 0x7a, 0x73, 0x2c, 0x15, 0x66, 0x7f},
+            {0x18, 0x49, 0x7a, 0x73, 0x2c, 0x15, 0x76, 0x7f},
+            {0x18, 0x49, 0x7a, 0x73, 0x2c, 0x15, 0x76, 0x7f},
+            {0x18, 0x41, 0x7a, 0x73, 0x2c, 0x1d, 0x76, 0x7f},
+            {0x18, 0x41, 0x7a, 0x73, 0x2c, 0x1d, 0x76, 0x7f},
+            {0x18, 0x41, 0x6a, 0x73, 0x24, 0x1d, 0x76, 0x7f},
+            {0x18, 0x41, 0x6a, 0x73, 0x24, 0x1d, 0x76, 0x7f},
+            {0x18, 0x41, 0x6a, 0x53, 0x24, 0x1d, 0x76, 0x6f},
+            {0x18, 0x41, 0x6a, 0x53, 0x24, 0x1d, 0x76, 0x6f},
+            {0x58, 0x41, 0x6a, 0x53, 0x24, 0x1d, 0x56, 0x6f},
+            {0x58, 0x41, 0x6a, 0x53, 0x24, 0x1d, 0x56, 0x6f},
+            {0x58, 0x41, 0x6a, 0x53, 0x24, 0x5d, 0x56, 0x6f},
+            {0x58, 0x61, 0x6a, 0x53, 0x24, 0x5d, 0x56, 0x6f},
+            {0x58, 0x61, 0x6a, 0x53, 0x64, 0x5d, 0x56, 0x6f},
+            {0x58, 0x61, 0x6a, 0x53, 0x64, 0x5d, 0x56, 0x6f},
+            {0x58, 0x61, 0x6a, 0x53, 0x64, 0x5d, 0x56, 0x6f},
+            {0x58, 0x61, 0x4a, 0x03, 0x64, 0x5d, 0x56, 0x6f},
+            {0x58, 0x61, 0x4a, 0x03, 0x64, 0x5d, 0x56, 0x6f},
+            {0x58, 0x61, 0x4a, 0x03, 0x64, 0x5d, 0x56, 0x6f},
+            {0x58, 0x61, 0x4a, 0x03, 0x64, 0x5d, 0x56, 0x6f},
+            {0x58, 0x61, 0x4a, 0x03, 0x64, 0x5d, 0x06, 0x4f}
+        },
+        {
+            {0x50, 0x69, 0x0a, 0x43, 0x6c, 0x55, 0x46, 0x0f},
+            {0x50, 0x69, 0x0a, 0x43, 0x6c, 0x55, 0x46, 0x0f},
+            {0x50, 0x69, 0x5a, 0x43, 0x6c, 0x55, 0x46, 0x5f},
+            {0x50, 0x69, 0x5a, 0x43, 0x6c, 0x55, 0x46, 0x5f},
+            {0x50, 0x69, 0x5a, 0x43, 0x6c, 0x55, 0x46, 0x5f},
+            {0x50, 0x69, 0x5a, 0x43, 0x6c, 0x55, 0x46, 0x5f},
+            {0x50, 0x69, 0x5a, 0x23, 0x6c, 0x55, 0x66, 0x5f},
+            {0x50, 0x69, 0x5a, 0x23, 0x6c, 0x55, 0x66, 0x5f},
+            {0x10, 0x69, 0x5a, 0x23, 0x6c, 0x55, 0x66, 0x5f},
+            {0x10, 0x69, 0x5a, 0x23, 0x6c, 0x55, 0x66, 0x5f},
+            {0x10, 0x29, 0x5a, 0x23, 0x6c, 0x15, 0x66, 0x5f},
+            {0x10, 0x29, 0x5a, 0x23, 0x6c, 0x15, 0x66, 0x5f},
+            {0x10, 0x29, 0x7a, 0x23, 0x2c, 0x15, 0x66, 0x5f},
+            {0x10, 0x29, 0x7a, 0x23, 0x2c, 0x15, 0x66, 0x5f},
+            {0x10, 0x29, 0x7a, 0x33, 0x2c, 0x15, 0x66, 0x7f},
+            {0x10, 0x29, 0x7a, 0x33, 0x2c, 0x15, 0x66, 0x7f},
+            {0x18, 0x29, 0x7a, 0x33, 0x2c, 0x15, 0x76, 0x7f},
+            {0x18, 0x29, 0x7a, 0x33, 0x2c, 0x15, 0x76, 0x7f},
+            {0x18, 0x21, 0x7a, 0x33, 0x2c, 0x1d, 0x76, 0x7f},
+            {0x18, 0x21, 0x7a, 0x33, 0x2c, 0x1d, 0x76, 0x7f},
+            {0x18, 0x21, 0x6a, 0x33, 0x24, 0x1d, 0x76, 0x7f},
+            {0x18, 0x21, 0x6a, 0x33, 0x24, 0x1d, 0x76, 0x7f},
+            {0x18, 0x21, 0x6a, 0x13, 0x24, 0x1d, 0x76, 0x6f},
+            {0x18, 0x21, 0x6a, 0x13, 0x24, 0x1d, 0x76, 0x6f},
+            {0x58, 0x21, 0x6a, 0x13, 0x24, 0x1d, 0x56, 0x6f},
+            {0x58, 0x21, 0x6a, 0x13, 0x24, 0x1d, 0x56, 0x6f},
+            {0x58, 0x61, 0x6a, 0x13, 0x24, 0x5d, 0x56, 0x6f},
+            {0x58, 0x61, 0x6a, 0x13, 0x24, 0x5d, 0x56, 0x6f},
+            {0x58, 0x61, 0x6a, 0x13, 0x64, 0x5d, 0x56, 0x6f},
+            {0x58, 0x61, 0x6a, 0x13, 0x64, 0x5d, 0x56, 0x6f},
+            {0x58, 0x61, 0x4a, 0x03, 0x64, 0x5d, 0x56, 0x6f},
+            {0x58, 0x61, 0x4a, 0x03, 0x64, 0x5d, 0x56, 0x6f},
+            {0x58, 0x61, 0x4a, 0x03, 0x64, 0x5d, 0x56, 0x6f},
+            {0x58, 0x61, 0x4a, 0x03, 0x64, 0x5d, 0x56, 0x6f},
+            {0x58, 0x61, 0x4a, 0x03, 0x64, 0x5d, 0x06, 0x4f},
+            {0x58, 0x61, 0x4a, 0x03, 0x64, 0x5d, 0x06, 0x4f}
+        },
+        {
+            {0x50, 0x69, 0x0a, 0x43, 0x6c, 0x55, 0x46, 0x0f},
+            {0x50, 0x69, 0x0a, 0x43, 0x6c, 0x55, 0x46, 0x0f},
+            {0x50, 0x69, 0x0a, 0x43, 0x6c, 0x55, 0x46, 0x0f},
+            {0x50, 0x69, 0x5a, 0x43, 0x6c, 0x55, 0x46, 0x5f},
+            {0x50, 0x69, 0x5a, 0x43, 0x6c, 0x55, 0x46, 0x5f},
+            {0x50, 0x69, 0x5a, 0x43, 0x6c, 0x55, 0x46, 0x5f},
+            {0x50, 0x69, 0x5a, 0x23, 0x6c, 0x55, 0x46, 0x5f},
+            {0x50, 0x69, 0x5a, 0x23, 0x6c, 0x55, 0x66, 0x5f},
+            {0x10, 0x69, 0x5a, 0x23, 0x6c, 0x55, 0x66, 0x5f},
+            {0x10, 0x69, 0x5a, 0x23, 0x6c, 0x55, 0x66, 0x5f},
+            {0x10, 0x29, 0x5a, 0x23, 0x6c, 0x15, 0x66, 0x5f},
+            {0x10, 0x29, 0x5a, 0x23, 0x6c, 0x15, 0x66, 0x5f},
+            {0x10, 0x29, 0x7a, 0x23, 0x2c, 0x15, 0x66, 0x5f},
+            {0x10, 0x29, 0x7a, 0x23, 0x2c, 0x15, 0x66, 0x5f},
+            {0x10, 0x29, 0x7a, 0x33, 0x2c, 0x15, 0x66, 0x7f},
+            {0x10, 0x29, 0x7a, 0x33, 0x2c, 0x15, 0x66, 0x7f},
+            {0x18, 0x29, 0x7a, 0x33, 0x2c, 0x15, 0x76, 0x7f},
+            {0x18, 0x29, 0x7a, 0x33, 0x2c, 0x15, 0x76, 0x7f},
+            {0x18, 0x21, 0x7a, 0x33, 0x2c, 0x1d, 0x76, 0x7f},
+            {0x18, 0x21, 0x7a, 0x33, 0x2c, 0x1d, 0x76, 0x7f},
+            {0x18, 0x21, 0x6a, 0x33, 0x24, 0x1d, 0x76, 0x7f},
+            {0x18, 0x21, 0x6a, 0x33, 0x24, 0x1d, 0x76, 0x7f},
+            {0x18, 0x21, 0x6a, 0x13, 0x24, 0x1d, 0x76, 0x6f},
+            {0x18, 0x21, 0x6a, 0x13, 0x24, 0x1d, 0x76, 0x6f},
+            {0x58, 0x21, 0x6a, 0x13, 0x24, 0x1d, 0x56, 0x6f},
+            {0x58, 0x21, 0x6a, 0x13, 0x24, 0x1d, 0x56, 0x6f},
+            {0x58, 0x61, 0x6a, 0x13, 0x24, 0x5d, 0x56, 0x6f},
+            {0x58, 0x61, 0x6a, 0x13, 0x24, 0x5d, 0x56, 0x6f},
+            {0x58, 0x61, 0x6a, 0x13, 0x64, 0x5d, 0x56, 0x6f},
+            {0x58, 0x61, 0x4a, 0x13, 0x64, 0x5d, 0x56, 0x6f},
+            {0x58, 0x61, 0x4a, 0x03, 0x64, 0x5d, 0x56, 0x6f},
+            {0x58, 0x61, 0x4a, 0x03, 0x64, 0x5d, 0x56, 0x6f},
+            {0x58, 0x61, 0x4a, 0x03, 0x64, 0x5d, 0x56, 0x6f},
+            {0x58, 0x61, 0x4a, 0x03, 0x64, 0x5d, 0x06, 0x4f},
+            {0x58, 0x61, 0x4a, 0x03, 0x64, 0x5d, 0x06, 0x4f},
+            {0x58, 0x61, 0x4a, 0x03, 0x64, 0x5d, 0x06, 0x4f}
+        },
+        {
+            {0x50, 0x69, 0x0a, 0x43, 0x6c, 0x55, 0x46, 0x0f},
+            {0x50, 0x69, 0x0a, 0x43, 0x6c, 0x55, 0x46, 0x0f},
+            {0x50, 0x69, 0x0a, 0x43, 0x6c, 0x55, 0x46, 0x0f},
+            {0x50, 0x69, 0x0a, 0x43, 0x6c, 0x55, 0x46, 0x0f},
+            {0x50, 0x69, 0x1a, 0x43, 0x6c, 0x55, 0x46, 0x5f},
+            {0x50, 0x69, 0x1a, 0x43, 0x6c, 0x55, 0x46, 0x5f},
+            {0x50, 0x69, 0x1a, 0x23, 0x6c, 0x55, 0x46, 0x5f},
+            {0x50, 0x69, 0x1a, 0x23, 0x6c, 0x55, 0x46, 0x5f},
+            {0x10, 0x69, 0x1a, 0x23, 0x6c, 0x55, 0x26, 0x5f},
+            {0x10, 0x69, 0x1a, 0x23, 0x6c, 0x55, 0x26, 0x5f},
+            {0x10, 0x29, 0x1a, 0x23, 0x6c, 0x15, 0x26, 0x5f},
+            {0x10, 0x29, 0x1a, 0x23, 0x6c, 0x15, 0x26, 0x5f},
+            {0x10, 0x29, 0x3a, 0x23, 0x2c, 0x15, 0x26, 0x5f},
+            {0x10, 0x29, 0x3a, 0x23, 0x2c, 0x15, 0x26, 0x5f},
+            {0x10, 0x29, 0x3a, 0x33, 0x2c, 0x15, 0x26, 0x7f},
+            {0x10, 0x29, 0x3a, 0x33, 0x2c, 0x15, 0x26, 0x7f},
+            {0x18, 0x29, 0x3a, 0x33, 0x2c, 0x15, 0x36, 0x7f},
+            {0x18, 0x29, 0x3a, 0x33, 0x2c, 0x15, 0x36, 0x7f},
+            {0x18, 0x21, 0x3a, 0x33, 0x2c, 0x1d, 0x36, 0x7f},
+            {0x18, 0x21, 0x3a, 0x33, 0x2c, 0x1d, 0x36, 0x7f},
+            {0x18, 0x21, 0x2a, 0x33, 0x24, 0x1d, 0x36, 0x7f},
+            {0x18, 0x21, 0x2a, 0x33, 0x24, 0x1d, 0x36, 0x7f},
+            {0x18, 0x21, 0x2a, 0x13, 0x24, 0x1d, 0x36, 0x6f},
+            {0x18, 0x21, 0x2a, 0x13, 0x24, 0x1d, 0x36, 0x6f},
+            {0x58, 0x21, 0x2a, 0x13, 0x24, 0x1d, 0x16, 0x6f},
+            {0x58, 0x21, 0x2a, 0x13, 0x24, 0x1d, 0x16, 0x6f},
+            {0x58, 0x61, 0x2a, 0x13, 0x24, 0x5d, 0x16, 0x6f},
+            {0x58, 0x61, 0x2a, 0x13, 0x24, 0x5d, 0x16, 0x6f},
+            {0x58, 0x61, 0x4a, 0x13, 0x64, 0x5d, 0x16, 0x6f},
+            {0x58, 0x61, 0x4a, 0x13, 0x64, 0x5d, 0x16, 0x6f},
+            {0x58, 0x61, 0x4a, 0x03, 0x64, 0x5d, 0x16, 0x6f},
+            {0x58, 0x61, 0x4a, 0x03, 0x64, 0x5d, 0x16, 0x6f},
+            {0x58, 0x61, 0x4a, 0x03, 0x64, 0x5d, 0x06, 0x4f},
+            {0x58, 0x61, 0x4a, 0x03, 0x64, 0x5d, 0x06, 0x4f},
+            {0x58, 0x61, 0x4a, 0x03, 0x64, 0x5d, 0x06, 0x4f},
+            {0x58, 0x61, 0x4a, 0x03, 0x64, 0x5d, 0x06, 0x4f}
+        },
+        {
+            {0x50, 0x69, 0x0a, 0x43, 0x6c, 0x55, 0x46, 0x0f},
+            {0x50, 0x69, 0x0a, 0x43, 0x6c, 0x55, 0x46, 0x0f},
+            {0x50, 0x69, 0x0a, 0x43, 0x6c, 0x55, 0x46, 0x0f},
+            {0x50, 0x69, 0x0a, 0x43, 0x6c, 0x55, 0x46, 0x0f},
+            {0x50, 0x69, 0x1a, 0x43, 0x6c, 0x55, 0x46, 0x0f},
+            {0x50, 0x69, 0x1a, 0x43, 0x6c, 0x55, 0x46, 0x5f},
+            {0x50, 0x69, 0x1a, 0x23, 0x6c, 0x55, 0x46, 0x5f},
+            {0x50, 0x69, 0x1a, 0x23, 0x6c, 0x55, 0x46, 0x5f},
+            {0x10, 0x69, 0x1a, 0x23, 0x6c, 0x55, 0x26, 0x5f},
+            {0x10, 0x69, 0x1a, 0x23, 0x6c, 0x55, 0x26, 0x5f},
+            {0x10, 0x29, 0x1a, 0x23, 0x6c, 0x15, 0x26, 0x5f},
+            {0x10, 0x29, 0x1a, 0x23, 0x6c, 0x15, 0x26, 0x5f},
+            {0x10, 0x29, 0x3a, 0x23, 0x2c, 0x15, 0x26, 0x5f},
+            {0x10, 0x29, 0x3a, 0x23, 0x2c, 0x15, 0x26, 0x5f},
+            {0x10, 0x29, 0x3a, 0x33, 0x2c, 0x15, 0x26, 0x7f},
+            {0x10, 0x29, 0x3a, 0x33, 0x2c, 0x15, 0x26, 0x7f},
+            {0x18, 0x29, 0x3a, 0x33, 0x2c, 0x15, 0x36, 0x7f},
+            {0x18, 0x29, 0x3a, 0x33, 0x2c, 0x15, 0x36, 0x7f},
+            {0x18, 0x21, 0x3a, 0x33, 0x2c, 0x1d, 0x36, 0x7f},
+            {0x18, 0x21, 0x3a, 0x33, 0x2c, 0x1d, 0x36, 0x7f},
+            {0x18, 0x21, 0x2a, 0x33, 0x24, 0x1d, 0x36, 0x7f},
+            {0x18, 0x21, 0x2a, 0x33, 0x24, 0x1d, 0x36, 0x7f},
+            {0x18, 0x21, 0x2a, 0x13, 0x24, 0x1d, 0x36, 0x6f},
+            {0x18, 0x21, 0x2a, 0x13, 0x24, 0x1d, 0x36, 0x6f},
+            {0x58, 0x21, 0x2a, 0x13, 0x24, 0x1d, 0x16, 0x6f},
+            {0x58, 0x21, 0x2a, 0x13, 0x24, 0x1d, 0x16, 0x6f},
+            {0x58, 0x61, 0x2a, 0x13, 0x24, 0x5d, 0x16, 0x6f},
+            {0x58, 0x61, 0x2a, 0x13, 0x24, 0x5d, 0x16, 0x6f},
+            {0x58, 0x61, 0x4a, 0x13, 0x64, 0x5d, 0x16, 0x6f},
+            {0x58, 0x61, 0x4a, 0x13, 0x64, 0x5d, 0x16, 0x6f},
+            {0x58, 0x61, 0x4a, 0x03, 0x64, 0x5d, 0x16, 0x6f},
+            {0x58, 0x61, 0x4a, 0x03, 0x64, 0x5d, 0x16, 0x4f},
+            {0x58, 0x61, 0x4a, 0x03, 0x64, 0x5d, 0x06, 0x4f},
+            {0x58, 0x61, 0x4a, 0x03, 0x64, 0x5d, 0x06, 0x4f},
+            {0x58, 0x61, 0x4a, 0x03, 0x64, 0x5d, 0x06, 0x4f},
+            {0x58, 0x61, 0x4a, 0x03, 0x64, 0x5d, 0x06, 0x4f}
+        },
+        {
+            {0x50, 0x69, 0x0a, 0x43, 0x6c, 0x75, 0x46, 0x0f},
+            {0x50, 0x69, 0x0a, 0x43, 0x6c, 0x75, 0x46, 0x0f},
+            {0x50, 0x69, 0x0a, 0x43, 0x6c, 0x75, 0x46, 0x0f},
+            {0x50, 0x69, 0x0a, 0x43, 0x6c, 0x75, 0x46, 0x0f},
+            {0x50, 0x69, 0x1a, 0x43, 0x6c, 0x75, 0x46, 0x0f},
+            {0x50, 0x69, 0x1a, 0x43, 0x6c, 0x75, 0x46, 0x0f},
+            {0x50, 0x69, 0x1a, 0x23, 0x6c, 0x75, 0x46, 0x1f},
+            {0x50, 0x69, 0x1a, 0x23, 0x6c, 0x75, 0x46, 0x1f},
+            {0x10, 0x69, 0x1a, 0x23, 0x6c, 0x75, 0x26, 0x1f},
+            {0x10, 0x69, 0x1a, 0x23, 0x6c, 0x75, 0x26, 0x1f},
+            {0x10, 0x29, 0x1a, 0x23, 0x6c, 0x35, 0x26, 0x1f},
+            {0x10, 0x29, 0x1a, 0x23, 0x6c, 0x35, 0x26, 0x1f},
+            {0x10, 0x29, 0x3a, 0x23, 0x2c, 0x35, 0x26, 0x1f},
+            {0x10, 0x29, 0x3a, 0x23, 0x2c, 0x35, 0x26, 0x1f},
+            {0x10, 0x29, 0x3a, 0x33, 0x2c, 0x35, 0x26, 0x3f},
+            {0x10, 0x29, 0x3a, 0x33, 0x2c, 0x35, 0x26, 0x3f},
+            {0x18, 0x29, 0x3a, 0x33, 0x2c, 0x35, 0x36, 0x3f},
+            {0x18, 0x29, 0x3a, 0x33, 0x2c, 0x35, 0x36, 0x3f},
+            {0x18, 0x21, 0x3a, 0x33, 0x2c, 0x3d, 0x36, 0x3f},
+            {0x18, 0x21, 0x3a, 0x33, 0x2c, 0x3d, 0x36, 0x3f},
+            {0x18, 0x21, 0x2a, 0x33, 0x24, 0x3d, 0x36, 0x3f},
+            {0x18, 0x21, 0x2a, 0x33, 0x24, 0x3d, 0x36, 0x3f},
+            {0x18, 0x21, 0x2a, 0x13, 0x24, 0x3d, 0x36, 0x2f},
+            {0x18, 0x21, 0x2a, 0x13, 0x24, 0x3d, 0x36, 0x2f},
+            {0x58, 0x21, 0x2a, 0x13, 0x24, 0x3d, 0x16, 0x2f},
+            {0x58, 0x21, 0x2a, 0x13, 0x24, 0x3d, 0x16, 0x2f},
+            {0x58, 0x61, 0x2a, 0x13, 0x24, 0x7d, 0x16, 0x2f},
+            {0x58, 0x61, 0x2a, 0x13, 0x24, 0x7d, 0x16, 0x2f},
+            {0x58, 0x61, 0x4a, 0x13, 0x64, 0x7d, 0x16, 0x2f},
+            {0x58, 0x61, 0x4a, 0x13, 0x64, 0x7d, 0x16, 0x2f},
+            {0x58, 0x61, 0x4a, 0x03, 0x64, 0x7d, 0x16, 0x4f},
+            {0x58, 0x61, 0x4a, 0x03, 0x64, 0x7d, 0x16, 0x4f},
+            {0x58, 0x61, 0x4a, 0x03, 0x64, 0x7d, 0x06, 0x4f},
+            {0x58, 0x61, 0x4a, 0x03, 0x64, 0x7d, 0x06, 0x4f},
+            {0x58, 0x61, 0x4a, 0x03, 0x64, 0x7d, 0x06, 0x4f},
+            {0x58, 0x61, 0x4a, 0x03, 0x64, 0x7d, 0x06, 0x4f}
+        },
+        {
+            {0x50, 0x69, 0x0a, 0x43, 0x6c, 0x75, 0x46, 0x0f},
+            {0x50, 0x69, 0x0a, 0x43, 0x6c, 0x75, 0x46, 0x0f},
+            {0x50, 0x69, 0x0a, 0x43, 0x6c, 0x75, 0x46, 0x0f},
+            {0x50, 0x69, 0x0a, 0x43, 0x6c, 0x75, 0x46, 0x0f},
+            {0x50, 0x69, 0x1a, 0x43, 0x6c, 0x75, 0x46, 0x0f},
+            {0x50, 0x69, 0x1a, 0x43, 0x6c, 0x75, 0x46, 0x0f},
+            {0x50, 0x69, 0x1a, 0x23, 0x6c, 0x75, 0x46, 0x1f},
+            {0x50, 0x69, 0x1a, 0x23, 0x6c, 0x75, 0x46, 0x1f},
+            {0x10, 0x69, 0x1a, 0x23, 0x6c, 0x75, 0x26, 0x1f},
+            {0x10, 0x69, 0x1a, 0x23, 0x6c, 0x75, 0x26, 0x1f},
+            {0x10, 0x29, 0x1a, 0x23, 0x6c, 0x35, 0x26, 0x1f},
+            {0x10, 0x29, 0x1a, 0x23, 0x6c, 0x35, 0x26, 0x1f},
+            {0x10, 0x29, 0x3a, 0x23, 0x2c, 0x35, 0x26, 0x1f},
+            {0x10, 0x29, 0x3a, 0x23, 0x2c, 0x35, 0x26, 0x1f},
+            {0x10, 0x29, 0x3a, 0x33, 0x2c, 0x35, 0x26, 0x3f},
+            {0x10, 0x29, 0x3a, 0x33, 0x2c, 0x35, 0x26, 0x3f},
+            {0x18, 0x29, 0x3a, 0x33, 0x2c, 0x35, 0x36, 0x3f},
+            {0x18, 0x29, 0x3a, 0x33, 0x2c, 0x35, 0x36, 0x3f},
+            {0x18, 0x21, 0x3a, 0x33, 0x2c, 0x3d, 0x36, 0x3f},
+            {0x18, 0x21, 0x3a, 0x33, 0x2c, 0x3d, 0x36, 0x3f},
+            {0x18, 0x21, 0x2a, 0x33, 0x24, 0x3d, 0x36, 0x3f},
+            {0x18, 0x21, 0x2a, 0x33, 0x24, 0x3d, 0x36, 0x3f},
+            {0x18, 0x21, 0x2a, 0x13, 0x24, 0x3d, 0x36, 0x2f},
+            {0x18, 0x21, 0x2a, 0x13, 0x24, 0x3d, 0x36, 0x2f},
+            {0x58, 0x21, 0x2a, 0x13, 0x24, 0x3d, 0x16, 0x2f},
+            {0x58, 0x21, 0x2a, 0x13, 0x24, 0x3d, 0x16, 0x2f},
+            {0x58, 0x61, 0x2a, 0x13, 0x24, 0x7d, 0x16, 0x2f},
+            {0x58, 0x61, 0x2a, 0x13, 0x24, 0x7d, 0x16, 0x2f},
+            {0x58, 0x61, 0x4a, 0x13, 0x64, 0x7d, 0x16, 0x2f},
+            {0x58, 0x61, 0x4a, 0x13, 0x64, 0x7d, 0x16, 0x2f},
+            {0x58, 0x61, 0x4a, 0x03, 0x64, 0x7d, 0x16, 0x4f},
+            {0x58, 0x61, 0x4a, 0x03, 0x64, 0x7d, 0x16, 0x4f},
+            {0x58, 0x61, 0x4a, 0x03, 0x64, 0x7d, 0x06, 0x4f},
+            {0x58, 0x61, 0x4a, 0x03, 0x64, 0x7d, 0x06, 0x4f},
+            {0x58, 0x61, 0x4a, 0x03, 0x64, 0x7d, 0x06, 0x4f},
+            {0x58, 0x61, 0x4a, 0x03, 0x64, 0x7d, 0x06, 0x4f}
+        },
+        {
+            {0x70, 0x69, 0x0a, 0x43, 0x7c, 0x75, 0x46, 0x0f},
+            {0x70, 0x69, 0x0a, 0x43, 0x7c, 0x75, 0x46, 0x0f},
+            {0x70, 0x69, 0x0a, 0x43, 0x7c, 0x75, 0x46, 0x0f},
+            {0x70, 0x69, 0x0a, 0x43, 0x7c, 0x75, 0x46, 0x0f},
+            {0x70, 0x69, 0x1a, 0x43, 0x7c, 0x75, 0x46, 0x0f},
+            {0x70, 0x69, 0x1a, 0x43, 0x7c, 0x75, 0x46, 0x0f},
+            {0x70, 0x69, 0x1a, 0x23, 0x7c, 0x75, 0x46, 0x1f},
+            {0x70, 0x69, 0x1a, 0x23, 0x7c, 0x75, 0x46, 0x1f},
+            {0x30, 0x69, 0x1a, 0x23, 0x7c, 0x75, 0x26, 0x1f},
+            {0x30, 0x69, 0x1a, 0x23, 0x7c, 0x75, 0x26, 0x1f},
+            {0x30, 0x29, 0x1a, 0x23, 0x7c, 0x35, 0x26, 0x1f},
+            {0x30, 0x29, 0x1a, 0x23, 0x7c, 0x35, 0x26, 0x1f},
+            {0x30, 0x29, 0x3a, 0x23, 0x3c, 0x35, 0x26, 0x1f},
+            {0x30, 0x29, 0x3a, 0x23, 0x3c, 0x35, 0x26, 0x1f},
+            {0x30, 0x29, 0x3a, 0x33, 0x3c, 0x35, 0x26, 0x3f},
+            {0x30, 0x29, 0x3a, 0x33, 0x3c, 0x35, 0x26, 0x3f},
+            {0x38, 0x29, 0x3a, 0x33, 0x3c, 0x35, 0x36, 0x3f},
+            {0x38, 0x29, 0x3a, 0x33, 0x3c, 0x35, 0x36, 0x3f},
+            {0x38, 0x21, 0x3a, 0x33, 0x3c, 0x3d, 0x36, 0x3f},
+            {0x38, 0x21, 0x3a, 0x33, 0x3c, 0x3d, 0x36, 0x3f},
+            {0x38, 0x21, 0x2a, 0x33, 0x34, 0x3d, 0x36, 0x3f},
+            {0x38, 0x21, 0x2a, 0x33, 0x34, 0x3d, 0x36, 0x3f},
+            {0x38, 0x21, 0x2a, 0x13, 0x34, 0x3d, 0x36, 0x2f},
+            {0x38, 0x21, 0x2a, 0x13, 0x34, 0x3d, 0x36, 0x2f},
+            {0x78, 0x21, 0x2a, 0x13, 0x34, 0x3d, 0x16, 0x2f},
+            {0x78, 0x21, 0x2a, 0x13, 0x34, 0x3d, 0x16, 0x2f},
+            {0x78, 0x61, 0x2a, 0x13, 0x34, 0x7d, 0x16, 0x2f},
+            {0x78, 0x61, 0x2a, 0x13, 0x34, 0x7d, 0x16, 0x2f},
+            {0x78, 0x61, 0x4a, 0x13, 0x74, 0x7d, 0x16, 0x2f},
+            {0x78, 0x61, 0x4a, 0x13, 0x74, 0x7d, 0x16, 0x2f},
+            {0x78, 0x61, 0x4a, 0x03, 0x74, 0x7d, 0x16, 0x4f},
+            {0x78, 0x61, 0x4a, 0x03, 0x74, 0x7d, 0x16, 0x4f},
+            {0x78, 0x61, 0x4a, 0x03, 0x74, 0x7d, 0x06, 0x4f},
+            {0x78, 0x61, 0x4a, 0x03, 0x74, 0x7d, 0x06, 0x4f},
+            {0x78, 0x61, 0x4a, 0x03, 0x74, 0x7d, 0x06, 0x4f},
+            {0x78, 0x61, 0x4a, 0x03, 0x74, 0x7d, 0x06, 0x4f}
+        },
+        {
+            {0x70, 0x69, 0x0a, 0x43, 0x7c, 0x75, 0x46, 0x0f},
+            {0x70, 0x69, 0x0a, 0x43, 0x7c, 0x75, 0x46, 0x0f},
+            {0x70, 0x69, 0x0a, 0x43, 0x7c, 0x75, 0x46, 0x0f},
+            {0x70, 0x69, 0x0a, 0x43, 0x7c, 0x75, 0x46, 0x0f},
+            {0x70, 0x69, 0x1a, 0x43, 0x7c, 0x75, 0x46, 0x0f},
+            {0x70, 0x69, 0x1a, 0x43, 0x7c, 0x75, 0x46, 0x0f},
+            {0x70, 0x69, 0x1a, 0x23, 0x7c, 0x75, 0x46, 0x1f},
+            {0x70, 0x69, 0x1a, 0x23, 0x7c, 0x75, 0x46, 0x1f},
+            {0x30, 0x69, 0x1a, 0x23, 0x7c, 0x75, 0x26, 0x1f},
+            {0x30, 0x69, 0x1a, 0x23, 0x7c, 0x75, 0x26, 0x1f},
+            {0x30, 0x29, 0x1a, 0x23, 0x7c, 0x35, 0x26, 0x1f},
+            {0x30, 0x29, 0x1a, 0x23, 0x7c, 0x35, 0x26, 0x1f},
+            {0x30, 0x29, 0x3a, 0x23, 0x3c, 0x35, 0x26, 0x1f},
+            {0x30, 0x29, 0x3a, 0x23, 0x3c, 0x35, 0x26, 0x1f},
+            {0x30, 0x29, 0x3a, 0x33, 0x3c, 0x35, 0x26, 0x3f},
+            {0x30, 0x29, 0x3a, 0x33, 0x3c, 0x35, 0x26, 0x3f},
+            {0x38, 0x29, 0x3a, 0x33, 0x3c, 0x35, 0x36, 0x3f},
+            {0x38, 0x29, 0x3a, 0x33, 0x3c, 0x35, 0x36, 0x3f},
+            {0x38, 0x21, 0x3a, 0x33, 0x3c, 0x3d, 0x36, 0x3f},
+            {0x38, 0x21, 0x3a, 0x33, 0x3c, 0x3d, 0x36, 0x3f},
+            {0x38, 0x21, 0x2a, 0x33, 0x34, 0x3d, 0x36, 0x3f},
+            {0x38, 0x21, 0x2a, 0x33, 0x34, 0x3d, 0x36, 0x3f},
+            {0x38, 0x21, 0x2a, 0x13, 0x34, 0x3d, 0x36, 0x2f},
+            {0x38, 0x21, 0x2a, 0x13, 0x34, 0x3d, 0x36, 0x2f},
+            {0x78, 0x21, 0x2a, 0x13, 0x34, 0x3d, 0x16, 0x2f},
+            {0x78, 0x21, 0x2a, 0x13, 0x34, 0x3d, 0x16, 0x2f},
+            {0x78, 0x61, 0x2a, 0x13, 0x34, 0x7d, 0x16, 0x2f},
+            {0x78, 0x61, 0x2a, 0x13, 0x34, 0x7d, 0x16, 0x2f},
+            {0x78, 0x61, 0x4a, 0x13, 0x74, 0x7d, 0x16, 0x2f},
+            {0x78, 0x61, 0x4a, 0x13, 0x74, 0x7d, 0x16, 0x2f},
+            {0x78, 0x61, 0x4a, 0x03, 0x74, 0x7d, 0x16, 0x4f},
+            {0x78, 0x61, 0x4a, 0x03, 0x74, 0x7d, 0x16, 0x4f},
+            {0x78, 0x61, 0x4a, 0x03, 0x74, 0x7d, 0x06, 0x4f},
+            {0x78, 0x61, 0x4a, 0x03, 0x74, 0x7d, 0x06, 0x4f},
+            {0x78, 0x61, 0x4a, 0x03, 0x74, 0x7d, 0x06, 0x4f},
+            {0x78, 0x61, 0x4a, 0x03, 0x74, 0x7d, 0x06, 0x4f}
+        },
+        {
+            {0x70, 0x79, 0x0a, 0x4b, 0x7c, 0x75, 0x46, 0x0f},
+            {0x70, 0x79, 0x0a, 0x4b, 0x7c, 0x75, 0x46, 0x0f},
+            {0x70, 0x79, 0x0a, 0x4b, 0x7c, 0x75, 0x46, 0x0f},
+            {0x70, 0x79, 0x0a, 0x4b, 0x7c, 0x75, 0x46, 0x0f},
+            {0x70, 0x79, 0x1a, 0x4b, 0x7c, 0x75, 0x46, 0x0f},
+            {0x70, 0x79, 0x1a, 0x4b, 0x7c, 0x75, 0x46, 0x0f},
+            {0x70, 0x79, 0x1a, 0x2b, 0x7c, 0x75, 0x46, 0x1f},
+            {0x70, 0x79, 0x1a, 0x2b, 0x7c, 0x75, 0x46, 0x1f},
+            {0x30, 0x79, 0x1a, 0x2b, 0x7c, 0x75, 0x26, 0x1f},
+            {0x30, 0x79, 0x1a, 0x2b, 0x7c, 0x75, 0x26, 0x1f},
+            {0x30, 0x39, 0x1a, 0x2b, 0x7c, 0x35, 0x26, 0x1f},
+            {0x30, 0x39, 0x1a, 0x2b, 0x7c, 0x35, 0x26, 0x1f},
+            {0x30, 0x39, 0x3a, 0x2b, 0x3c, 0x35, 0x26, 0x1f},
+            {0x30, 0x39, 0x3a, 0x2b, 0x3c, 0x35, 0x26, 0x1f},
+            {0x30, 0x39, 0x3a, 0x3b, 0x3c, 0x35, 0x26, 0x3f},
+            {0x30, 0x39, 0x3a, 0x3b, 0x3c, 0x35, 0x26, 0x3f},
+            {0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x35, 0x36, 0x3f},
+            {0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x35, 0x36, 0x3f},
+            {0x38, 0x31, 0x3a, 0x3b, 0x3c, 0x3d, 0x36, 0x3f},
+            {0x38, 0x31, 0x3a, 0x3b, 0x3c, 0x3d, 0x36, 0x3f},
+            {0x38, 0x31, 0x2a, 0x3b, 0x34, 0x3d, 0x36, 0x3f},
+            {0x38, 0x31, 0x2a, 0x3b, 0x34, 0x3d, 0x36, 0x3f},
+            {0x38, 0x31, 0x2a, 0x1b, 0x34, 0x3d, 0x36, 0x2f},
+            {0x38, 0x31, 0x2a, 0x1b, 0x34, 0x3d, 0x36, 0x2f},
+            {0x78, 0x31, 0x2a, 0x1b, 0x34, 0x3d, 0x16, 0x2f},
+            {0x78, 0x31, 0x2a, 0x1b, 0x34, 0x3d, 0x16, 0x2f},
+            {0x78, 0x71, 0x2a, 0x1b, 0x34, 0x7d, 0x16, 0x2f},
+            {0x78, 0x71, 0x2a, 0x1b, 0x34, 0x7d, 0x16, 0x2f},
+            {0x78, 0x71, 0x4a, 0x1b, 0x74, 0x7d, 0x16, 0x2f},
+            {0x78, 0x71, 0x4a, 0x1b, 0x74, 0x7d, 0x16, 0x2f},
+            {0x78, 0x71, 0x4a, 0x0b, 0x74, 0x7d, 0x16, 0x4f},
+            {0x78, 0x71, 0x4a, 0x0b, 0x74, 0x7d, 0x16, 0x4f},
+            {0x78, 0x71, 0x4a, 0x0b, 0x74, 0x7d, 0x06, 0x4f},
+            {0x78, 0x71, 0x4a, 0x0b, 0x74, 0x7d, 0x06, 0x4f},
+            {0x78, 0x71, 0x4a, 0x0b, 0x74, 0x7d, 0x06, 0x4f},
+            {0x78, 0x71, 0x4a, 0x0b, 0x74, 0x7d, 0x06, 0x4f}
+        },
+        {
+            {0x70, 0x79, 0x0a, 0x4b, 0x7c, 0x75, 0x46, 0x0f},
+            {0x70, 0x79, 0x0a, 0x4b, 0x7c, 0x75, 0x46, 0x0f},
+            {0x70, 0x79, 0x0a, 0x4b, 0x7c, 0x75, 0x46, 0x0f},
+            {0x70, 0x79, 0x0a, 0x4b, 0x7c, 0x75, 0x46, 0x0f},
+            {0x70, 0x79, 0x1a, 0x4b, 0x7c, 0x75, 0x46, 0x0f},
+            {0x70, 0x79, 0x1a, 0x4b, 0x7c, 0x75, 0x46, 0x0f},
+            {0x70, 0x79, 0x1a, 0x2b, 0x7c, 0x75, 0x46, 0x1f},
+            {0x70, 0x79, 0x1a, 0x2b, 0x7c, 0x75, 0x46, 0x1f},
+            {0x30, 0x79, 0x1a, 0x2b, 0x7c, 0x75, 0x26, 0x1f},
+            {0x30, 0x79, 0x1a, 0x2b, 0x7c, 0x75, 0x26, 0x1f},
+            {0x30, 0x39, 0x1a, 0x2b, 0x7c, 0x35, 0x26, 0x1f},
+            {0x30, 0x39, 0x1a, 0x2b, 0x7c, 0x35, 0x26, 0x1f},
+            {0x30, 0x39, 0x3a, 0x2b, 0x3c, 0x35, 0x26, 0x1f},
+            {0x30, 0x39, 0x3a, 0x2b, 0x3c, 0x35, 0x26, 0x1f},
+            {0x30, 0x39, 0x3a, 0x3b, 0x3c, 0x35, 0x26, 0x3f},
+            {0x30, 0x39, 0x3a, 0x3b, 0x3c, 0x35, 0x26, 0x3f},
+            {0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x35, 0x36, 0x3f},
+            {0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x35, 0x36, 0x3f},
+            {0x38, 0x31, 0x3a, 0x3b, 0x3c, 0x3d, 0x36, 0x3f},
+            {0x38, 0x31, 0x3a, 0x3b, 0x3c, 0x3d, 0x36, 0x3f},
+            {0x38, 0x31, 0x2a, 0x3b, 0x34, 0x3d, 0x36, 0x3f},
+            {0x38, 0x31, 0x2a, 0x3b, 0x34, 0x3d, 0x36, 0x3f},
+            {0x38, 0x31, 0x2a, 0x1b, 0x34, 0x3d, 0x36, 0x2f},
+            {0x38, 0x31, 0x2a, 0x1b, 0x34, 0x3d, 0x36, 0x2f},
+            {0x78, 0x31, 0x2a, 0x1b, 0x34, 0x3d, 0x16, 0x2f},
+            {0x78, 0x31, 0x2a, 0x1b, 0x34, 0x3d, 0x16, 0x2f},
+            {0x78, 0x71, 0x2a, 0x1b, 0x34, 0x7d, 0x16, 0x2f},
+            {0x78, 0x71, 0x2a, 0x1b, 0x34, 0x7d, 0x16, 0x2f},
+            {0x78, 0x71, 0x4a, 0x1b, 0x74, 0x7d, 0x16, 0x2f},
+            {0x78, 0x71, 0x4a, 0x1b, 0x74, 0x7d, 0x16, 0x2f},
+            {0x78, 0x71, 0x4a, 0x0b, 0x74, 0x7d, 0x16, 0x4f},
+            {0x78, 0x71, 0x4a, 0x0b, 0x74, 0x7d, 0x16, 0x4f},
+            {0x78, 0x71, 0x4a, 0x0b, 0x74, 0x7d, 0x06, 0x4f},
+            {0x78, 0x71, 0x4a, 0x0b, 0x74, 0x7d, 0x06, 0x4f},
+            {0x78, 0x71, 0x4a, 0x0b, 0x74, 0x7d, 0x06, 0x4f},
+            {0x78, 0x71, 0x4a, 0x0b, 0x74, 0x7d, 0x06, 0x4f}
+        },
+        {
+            {0x70, 0x79, 0x02, 0x4b, 0x7c, 0x75, 0x4e, 0x0f},
+            {0x70, 0x79, 0x02, 0x4b, 0x7c, 0x75, 0x4e, 0x0f},
+            {0x70, 0x79, 0x02, 0x4b, 0x7c, 0x75, 0x4e, 0x0f},
+            {0x70, 0x79, 0x02, 0x4b, 0x7c, 0x75, 0x4e, 0x0f},
+            {0x70, 0x79, 0x12, 0x4b, 0x7c, 0x75, 0x4e, 0x0f},
+            {0x70, 0x79, 0x12, 0x4b, 0x7c, 0x75, 0x4e, 0x0f},
+            {0x70, 0x79, 0x12, 0x2b, 0x7c, 0x75, 0x4e, 0x1f},
+            {0x70, 0x79, 0x12, 0x2b, 0x7c, 0x75, 0x4e, 0x1f},
+            {0x30, 0x79, 0x12, 0x2b, 0x7c, 0x75, 0x2e, 0x1f},
+            {0x30, 0x79, 0x12, 0x2b, 0x7c, 0x75, 0x2e, 0x1f},
+            {0x30, 0x39, 0x12, 0x2b, 0x7c, 0x35, 0x2e, 0x1f},
+            {0x30, 0x39, 0x12, 0x2b, 0x7c, 0x35, 0x2e, 0x1f},
+            {0x30, 0x39, 0x32, 0x2b, 0x3c, 0x35, 0x2e, 0x1f},
+            {0x30, 0x39, 0x32, 0x2b, 0x3c, 0x35, 0x2e, 0x1f},
+            {0x30, 0x39, 0x32, 0x3b, 0x3c, 0x35, 0x2e, 0x3f},
+            {0x30, 0x39, 0x32, 0x3b, 0x3c, 0x35, 0x2e, 0x3f},
+            {0x38, 0x39, 0x32, 0x3b, 0x3c, 0x35, 0x3e, 0x3f},
+            {0x38, 0x39, 0x32, 0x3b, 0x3c, 0x35, 0x3e, 0x3f},
+            {0x38, 0x31, 0x32, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f},
+            {0x38, 0x31, 0x32, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f},
+            {0x38, 0x31, 0x22, 0x3b, 0x34, 0x3d, 0x3e, 0x3f},
+            {0x38, 0x31, 0x22, 0x3b, 0x34, 0x3d, 0x3e, 0x3f},
+            {0x38, 0x31, 0x22, 0x1b, 0x34, 0x3d, 0x3e, 0x2f},
+            {0x38, 0x31, 0x22, 0x1b, 0x34, 0x3d, 0x3e, 0x2f},
+            {0x78, 0x31, 0x22, 0x1b, 0x34, 0x3d, 0x1e, 0x2f},
+            {0x78, 0x31, 0x22, 0x1b, 0x34, 0x3d, 0x1e, 0x2f},
+            {0x78, 0x71, 0x22, 0x1b, 0x34, 0x7d, 0x1e, 0x2f},
+            {0x78, 0x71, 0x22, 0x1b, 0x34, 0x7d, 0x1e, 0x2f},
+            {0x78, 0x71, 0x42, 0x1b, 0x74, 0x7d, 0x1e, 0x2f},
+            {0x78, 0x71, 0x42, 0x1b, 0x74, 0x7d, 0x1e, 0x2f},
+            {0x78, 0x71, 0x42, 0x0b, 0x74, 0x7d, 0x1e, 0x4f},
+            {0x78, 0x71, 0x42, 0x0b, 0x74, 0x7d, 0x1e, 0x4f},
+            {0x78, 0x71, 0x42, 0x0b, 0x74, 0x7d, 0x0e, 0x4f},
+            {0x78, 0x71, 0x42, 0x0b, 0x74, 0x7d, 0x0e, 0x4f},
+            {0x78, 0x71, 0x42, 0x0b, 0x74, 0x7d, 0x0e, 0x4f},
+            {0x78, 0x71, 0x42, 0x0b, 0x74, 0x7d, 0x0e, 0x4f}
+        },
+        {
+            {0x70, 0x79, 0x02, 0x4b, 0x7c, 0x75, 0x4e, 0x0f},
+            {0x70, 0x79, 0x02, 0x4b, 0x7c, 0x75, 0x4e, 0x0f},
+            {0x70, 0x79, 0x02, 0x4b, 0x7c, 0x75, 0x4e, 0x0f},
+            {0x70, 0x79, 0x02, 0x4b, 0x7c, 0x75, 0x4e, 0x0f},
+            {0x70, 0x79, 0x12, 0x4b, 0x7c, 0x75, 0x4e, 0x0f},
+            {0x70, 0x79, 0x12, 0x4b, 0x7c, 0x75, 0x4e, 0x0f},
+            {0x70, 0x79, 0x12, 0x2b, 0x7c, 0x75, 0x4e, 0x1f},
+            {0x70, 0x79, 0x12, 0x2b, 0x7c, 0x75, 0x4e, 0x1f},
+            {0x30, 0x79, 0x12, 0x2b, 0x7c, 0x75, 0x2e, 0x1f},
+            {0x30, 0x79, 0x12, 0x2b, 0x7c, 0x75, 0x2e, 0x1f},
+            {0x30, 0x39, 0x12, 0x2b, 0x7c, 0x35, 0x2e, 0x1f},
+            {0x30, 0x39, 0x12, 0x2b, 0x7c, 0x35, 0x2e, 0x1f},
+            {0x30, 0x39, 0x32, 0x2b, 0x3c, 0x35, 0x2e, 0x1f},
+            {0x30, 0x39, 0x32, 0x2b, 0x3c, 0x35, 0x2e, 0x1f},
+            {0x30, 0x39, 0x32, 0x3b, 0x3c, 0x35, 0x2e, 0x3f},
+            {0x30, 0x39, 0x32, 0x3b, 0x3c, 0x35, 0x2e, 0x3f},
+            {0x38, 0x39, 0x32, 0x3b, 0x3c, 0x35, 0x3e, 0x3f},
+            {0x38, 0x39, 0x32, 0x3b, 0x3c, 0x35, 0x3e, 0x3f},
+            {0x38, 0x31, 0x32, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f},
+            {0x38, 0x31, 0x32, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f},
+            {0x38, 0x31, 0x22, 0x3b, 0x34, 0x3d, 0x3e, 0x3f},
+            {0x38, 0x31, 0x22, 0x3b, 0x34, 0x3d, 0x3e, 0x3f},
+            {0x38, 0x31, 0x22, 0x1b, 0x34, 0x3d, 0x3e, 0x2f},
+            {0x38, 0x31, 0x22, 0x1b, 0x34, 0x3d, 0x3e, 0x2f},
+            {0x78, 0x31, 0x22, 0x1b, 0x34, 0x3d, 0x1e, 0x2f},
+            {0x78, 0x31, 0x22, 0x1b, 0x34, 0x3d, 0x1e, 0x2f},
+            {0x78, 0x71, 0x22, 0x1b, 0x34, 0x7d, 0x1e, 0x2f},
+            {0x78, 0x71, 0x22, 0x1b, 0x34, 0x7d, 0x1e, 0x2f},
+            {0x78, 0x71, 0x42, 0x1b, 0x74, 0x7d, 0x1e, 0x2f},
+            {0x78, 0x71, 0x42, 0x1b, 0x74, 0x7d, 0x1e, 0x2f},
+            {0x78, 0x71, 0x42, 0x0b, 0x74, 0x7d, 0x1e, 0x4f},
+            {0x78, 0x71, 0x42, 0x0b, 0x74, 0x7d, 0x1e, 0x4f},
+            {0x78, 0x71, 0x42, 0x0b, 0x74, 0x7d, 0x0e, 0x4f},
+            {0x78, 0x71, 0x42, 0x0b, 0x74, 0x7d, 0x0e, 0x4f},
+            {0x78, 0x71, 0x42, 0x0b, 0x74, 0x7d, 0x0e, 0x4f},
+            {0x78, 0x71, 0x42, 0x0b, 0x74, 0x7d, 0x0e, 0x4f}
+        },
+        {
+            {0x70, 0x79, 0x02, 0x4b, 0x7c, 0x65, 0x4e, 0x07},
+            {0x70, 0x79, 0x02, 0x4b, 0x7c, 0x65, 0x4e, 0x07},
+            {0x70, 0x79, 0x02, 0x4b, 0x7c, 0x65, 0x4e, 0x07},
+            {0x70, 0x79, 0x02, 0x4b, 0x7c, 0x65, 0x4e, 0x07},
+            {0x70, 0x79, 0x12, 0x4b, 0x7c, 0x65, 0x4e, 0x07},
+            {0x70, 0x79, 0x12, 0x4b, 0x7c, 0x65, 0x4e, 0x07},
+            {0x70, 0x79, 0x12, 0x2b, 0x7c, 0x65, 0x4e, 0x17},
+            {0x70, 0x79, 0x12, 0x2b, 0x7c, 0x65, 0x4e, 0x17},
+            {0x30, 0x79, 0x12, 0x2b, 0x7c, 0x65, 0x2e, 0x17},
+            {0x30, 0x79, 0x12, 0x2b, 0x7c, 0x65, 0x2e, 0x17},
+            {0x30, 0x39, 0x12, 0x2b, 0x7c, 0x25, 0x2e, 0x17},
+            {0x30, 0x39, 0x12, 0x2b, 0x7c, 0x25, 0x2e, 0x17},
+            {0x30, 0x39, 0x32, 0x2b, 0x3c, 0x25, 0x2e, 0x17},
+            {0x30, 0x39, 0x32, 0x2b, 0x3c, 0x25, 0x2e, 0x17},
+            {0x30, 0x39, 0x32, 0x3b, 0x3c, 0x25, 0x2e, 0x37},
+            {0x30, 0x39, 0x32, 0x3b, 0x3c, 0x25, 0x2e, 0x37},
+            {0x38, 0x39, 0x32, 0x3b, 0x3c, 0x25, 0x3e, 0x37},
+            {0x38, 0x39, 0x32, 0x3b, 0x3c, 0x25, 0x3e, 0x37},
+            {0x38, 0x31, 0x32, 0x3b, 0x3c, 0x2d, 0x3e, 0x37},
+            {0x38, 0x31, 0x32, 0x3b, 0x3c, 0x2d, 0x3e, 0x37},
+            {0x38, 0x31, 0x22, 0x3b, 0x34, 0x2d, 0x3e, 0x37},
+            {0x38, 0x31, 0x22, 0x3b, 0x34, 0x2d, 0x3e, 0x37},
+            {0x38, 0x31, 0x22, 0x1b, 0x34, 0x2d, 0x3e, 0x27},
+            {0x38, 0x31, 0x22, 0x1b, 0x34, 0x2d, 0x3e, 0x27},
+            {0x78, 0x31, 0x22, 0x1b, 0x34, 0x2d, 0x1e, 0x27},
+            {0x78, 0x31, 0x22, 0x1b, 0x34, 0x2d, 0x1e, 0x27},
+            {0x78, 0x71, 0x22, 0x1b, 0x34, 0x6d, 0x1e, 0x27},
+            {0x78, 0x71, 0x22, 0x1b, 0x34, 0x6d, 0x1e, 0x27},
+            {0x78, 0x71, 0x42, 0x1b, 0x74, 0x6d, 0x1e, 0x27},
+            {0x78, 0x71, 0x42, 0x1b, 0x74, 0x6d, 0x1e, 0x27},
+            {0x78, 0x71, 0x42, 0x0b, 0x74, 0x6d, 0x1e, 0x47},
+            {0x78, 0x71, 0x42, 0x0b, 0x74, 0x6d, 0x1e, 0x47},
+            {0x78, 0x71, 0x42, 0x0b, 0x74, 0x6d, 0x0e, 0x47},
+            {0x78, 0x71, 0x42, 0x0b, 0x74, 0x6d, 0x0e, 0x47},
+            {0x78, 0x71, 0x42, 0x0b, 0x74, 0x6d, 0x0e, 0x47},
+            {0x78, 0x71, 0x42, 0x0b, 0x74, 0x6d, 0x0e, 0x47}
+        },
+        {
+            {0x70, 0x79, 0x02, 0x4b, 0x7c, 0x65, 0x4e, 0x07},
+            {0x70, 0x79, 0x02, 0x4b, 0x7c, 0x65, 0x4e, 0x07},
+            {0x70, 0x79, 0x02, 0x4b, 0x7c, 0x65, 0x4e, 0x07},
+            {0x70, 0x79, 0x02, 0x4b, 0x7c, 0x65, 0x4e, 0x07},
+            {0x70, 0x79, 0x12, 0x4b, 0x7c, 0x65, 0x4e, 0x07},
+            {0x70, 0x79, 0x12, 0x4b, 0x7c, 0x65, 0x4e, 0x07},
+            {0x70, 0x79, 0x12, 0x2b, 0x7c, 0x65, 0x4e, 0x17},
+            {0x70, 0x79, 0x12, 0x2b, 0x7c, 0x65, 0x4e, 0x17},
+            {0x30, 0x79, 0x12, 0x2b, 0x7c, 0x65, 0x2e, 0x17},
+            {0x30, 0x79, 0x12, 0x2b, 0x7c, 0x65, 0x2e, 0x17},
+            {0x30, 0x39, 0x12, 0x2b, 0x7c, 0x25, 0x2e, 0x17},
+            {0x30, 0x39, 0x12, 0x2b, 0x7c, 0x25, 0x2e, 0x17},
+            {0x30, 0x39, 0x32, 0x2b, 0x3c, 0x25, 0x2e, 0x17},
+            {0x30, 0x39, 0x32, 0x2b, 0x3c, 0x25, 0x2e, 0x17},
+            {0x30, 0x39, 0x32, 0x3b, 0x3c, 0x25, 0x2e, 0x37},
+            {0x30, 0x39, 0x32, 0x3b, 0x3c, 0x25, 0x2e, 0x37},
+            {0x38, 0x39, 0x32, 0x3b, 0x3c, 0x25, 0x3e, 0x37},
+            {0x38, 0x39, 0x32, 0x3b, 0x3c, 0x25, 0x3e, 0x37},
+            {0x38, 0x31, 0x32, 0x3b, 0x3c, 0x2d, 0x3e, 0x37},
+            {0x38, 0x31, 0x32, 0x3b, 0x3c, 0x2d, 0x3e, 0x37},
+            {0x38, 0x31, 0x22, 0x3b, 0x34, 0x2d, 0x3e, 0x37},
+            {0x38, 0x31, 0x22, 0x3b, 0x34, 0x2d, 0x3e, 0x37},
+            {0x38, 0x31, 0x22, 0x1b, 0x34, 0x2d, 0x3e, 0x27},
+            {0x38, 0x31, 0x22, 0x1b, 0x34, 0x2d, 0x3e, 0x27},
+            {0x78, 0x31, 0x22, 0x1b, 0x34, 0x2d, 0x1e, 0x27},
+            {0x78, 0x31, 0x22, 0x1b, 0x34, 0x2d, 0x1e, 0x27},
+            {0x78, 0x71, 0x22, 0x1b, 0x34, 0x6d, 0x1e, 0x27},
+            {0x78, 0x71, 0x22, 0x1b, 0x34, 0x6d, 0x1e, 0x27},
+            {0x78, 0x71, 0x42, 0x1b, 0x74, 0x6d, 0x1e, 0x27},
+            {0x78, 0x71, 0x42, 0x1b, 0x74, 0x6d, 0x1e, 0x27},
+            {0x78, 0x71, 0x42, 0x0b, 0x74, 0x6d, 0x1e, 0x47},
+            {0x78, 0x71, 0x42, 0x0b, 0x74, 0x6d, 0x1e, 0x47},
+            {0x78, 0x71, 0x42, 0x0b, 0x74, 0x6d, 0x0e, 0x47},
+            {0x78, 0x71, 0x42, 0x0b, 0x74, 0x6d, 0x0e, 0x47},
+            {0x78, 0x71, 0x42, 0x0b, 0x74, 0x6d, 0x0e, 0x47},
+            {0x78, 0x71, 0x42, 0x0b, 0x74, 0x6d, 0x0e, 0x47}
+        },
+        {
+            {0x60, 0x79, 0x02, 0x4b, 0x5c, 0x65, 0x4e, 0x07},
+            {0x60, 0x79, 0x02, 0x4b, 0x5c, 0x65, 0x4e, 0x07},
+            {0x60, 0x79, 0x02, 0x4b, 0x5c, 0x65, 0x4e, 0x07},
+            {0x60, 0x79, 0x02, 0x4b, 0x5c, 0x65, 0x4e, 0x07},
+            {0x60, 0x79, 0x12, 0x4b, 0x5c, 0x65, 0x4e, 0x07},
+            {0x60, 0x79, 0x12, 0x4b, 0x5c, 0x65, 0x4e, 0x07},
+            {0x60, 0x79, 0x12, 0x2b, 0x5c, 0x65, 0x4e, 0x17},
+            {0x60, 0x79, 0x12, 0x2b, 0x5c, 0x65, 0x4e, 0x17},
+            {0x20, 0x79, 0x12, 0x2b, 0x5c, 0x65, 0x2e, 0x17},
+            {0x20, 0x79, 0x12, 0x2b, 0x5c, 0x65, 0x2e, 0x17},
+            {0x20, 0x39, 0x12, 0x2b, 0x5c, 0x25, 0x2e, 0x17},
+            {0x20, 0x39, 0x12, 0x2b, 0x5c, 0x25, 0x2e, 0x17},
+            {0x20, 0x39, 0x32, 0x2b, 0x1c, 0x25, 0x2e, 0x17},
+            {0x20, 0x39, 0x32, 0x2b, 0x1c, 0x25, 0x2e, 0x17},
+            {0x20, 0x39, 0x32, 0x3b, 0x1c, 0x25, 0x2e, 0x37},
+            {0x20, 0x39, 0x32, 0x3b, 0x1c, 0x25, 0x2e, 0x37},
+            {0x28, 0x39, 0x32, 0x3b, 0x1c, 0x25, 0x3e, 0x37},
+            {0x28, 0x39, 0x32, 0x3b, 0x1c, 0x25, 0x3e, 0x37},
+            {0x28, 0x31, 0x32, 0x3b, 0x1c, 0x2d, 0x3e, 0x37},
+            {0x28, 0x31, 0x32, 0x3b, 0x1c, 0x2d, 0x3e, 0x37},
+            {0x28, 0x31, 0x22, 0x3b, 0x14, 0x2d, 0x3e, 0x37},
+            {0x28, 0x31, 0x22, 0x3b, 0x14, 0x2d, 0x3e, 0x37},
+            {0x28, 0x31, 0x22, 0x1b, 0x14, 0x2d, 0x3e, 0x27},
+            {0x28, 0x31, 0x22, 0x1b, 0x14, 0x2d, 0x3e, 0x27},
+            {0x68, 0x31, 0x22, 0x1b, 0x14, 0x2d, 0x1e, 0x27},
+            {0x68, 0x31, 0x22, 0x1b, 0x14, 0x2d, 0x1e, 0x27},
+            {0x68, 0x71, 0x22, 0x1b, 0x14, 0x6d, 0x1e, 0x27},
+            {0x68, 0x71, 0x22, 0x1b, 0x14, 0x6d, 0x1e, 0x27},
+            {0x68, 0x71, 0x42, 0x1b, 0x54, 0x6d, 0x1e, 0x27},
+            {0x68, 0x71, 0x42, 0x1b, 0x54, 0x6d, 0x1e, 0x27},
+            {0x68, 0x71, 0x42, 0x0b, 0x54, 0x6d, 0x1e, 0x47},
+            {0x68, 0x71, 0x42, 0x0b, 0x54, 0x6d, 0x1e, 0x47},
+            {0x68, 0x71, 0x42, 0x0b, 0x54, 0x6d, 0x0e, 0x47},
+            {0x68, 0x71, 0x42, 0x0b, 0x54, 0x6d, 0x0e, 0x47},
+            {0x68, 0x71, 0x42, 0x0b, 0x54, 0x6d, 0x0e, 0x47},
+            {0x68, 0x71, 0x42, 0x0b, 0x54, 0x6d, 0x0e, 0x47}
+        },
+        {
+            {0x60, 0x79, 0x02, 0x4b, 0x5c, 0x65, 0x4e, 0x07},
+            {0x60, 0x79, 0x02, 0x4b, 0x5c, 0x65, 0x4e, 0x07},
+            {0x60, 0x79, 0x02, 0x4b, 0x5c, 0x65, 0x4e, 0x07},
+            {0x60, 0x79, 0x02, 0x4b, 0x5c, 0x65, 0x4e, 0x07},
+            {0x60, 0x79, 0x12, 0x4b, 0x5c, 0x65, 0x4e, 0x07},
+            {0x60, 0x79, 0x12, 0x4b, 0x5c, 0x65, 0x4e, 0x07},
+            {0x60, 0x79, 0x12, 0x2b, 0x5c, 0x65, 0x4e, 0x17},
+            {0x60, 0x79, 0x12, 0x2b, 0x5c, 0x65, 0x4e, 0x17},
+            {0x20, 0x79, 0x12, 0x2b, 0x5c, 0x65, 0x2e, 0x17},
+            {0x20, 0x79, 0x12, 0x2b, 0x5c, 0x65, 0x2e, 0x17},
+            {0x20, 0x39, 0x12, 0x2b, 0x5c, 0x25, 0x2e, 0x17},
+            {0x20, 0x39, 0x12, 0x2b, 0x5c, 0x25, 0x2e, 0x17},
+            {0x20, 0x39, 0x32, 0x2b, 0x1c, 0x25, 0x2e, 0x17},
+            {0x20, 0x39, 0x32, 0x2b, 0x1c, 0x25, 0x2e, 0x17},
+            {0x20, 0x39, 0x32, 0x3b, 0x1c, 0x25, 0x2e, 0x37},
+            {0x20, 0x39, 0x32, 0x3b, 0x1c, 0x25, 0x2e, 0x37},
+            {0x28, 0x39, 0x32, 0x3b, 0x1c, 0x25, 0x3e, 0x37},
+            {0x28, 0x39, 0x32, 0x3b, 0x1c, 0x25, 0x3e, 0x37},
+            {0x28, 0x31, 0x32, 0x3b, 0x1c, 0x2d, 0x3e, 0x37},
+            {0x28, 0x31, 0x32, 0x3b, 0x1c, 0x2d, 0x3e, 0x37},
+            {0x28, 0x31, 0x22, 0x3b, 0x14, 0x2d, 0x3e, 0x37},
+            {0x28, 0x31, 0x22, 0x3b, 0x14, 0x2d, 0x3e, 0x37},
+            {0x28, 0x31, 0x22, 0x1b, 0x14, 0x2d, 0x3e, 0x27},
+            {0x28, 0x31, 0x22, 0x1b, 0x14, 0x2d, 0x3e, 0x27},
+            {0x68, 0x31, 0x22, 0x1b, 0x14, 0x2d, 0x1e, 0x27},
+            {0x68, 0x31, 0x22, 0x1b, 0x14, 0x2d, 0x1e, 0x27},
+            {0x68, 0x71, 0x22, 0x1b, 0x14, 0x6d, 0x1e, 0x27},
+            {0x68, 0x71, 0x22, 0x1b, 0x14, 0x6d, 0x1e, 0x27},
+            {0x68, 0x71, 0x42, 0x1b, 0x54, 0x6d, 0x1e, 0x27},
+            {0x68, 0x71, 0x42, 0x1b, 0x54, 0x6d, 0x1e, 0x27},
+            {0x68, 0x71, 0x42, 0x0b, 0x54, 0x6d, 0x1e, 0x47},
+            {0x68, 0x71, 0x42, 0x0b, 0x54, 0x6d, 0x1e, 0x47},
+            {0x68, 0x71, 0x42, 0x0b, 0x54, 0x6d, 0x0e, 0x47},
+            {0x68, 0x71, 0x42, 0x0b, 0x54, 0x6d, 0x0e, 0x47},
+            {0x68, 0x71, 0x42, 0x0b, 0x54, 0x6d, 0x0e, 0x47},
+            {0x68, 0x71, 0x42, 0x0b, 0x54, 0x6d, 0x0e, 0x47}
+        },
+        {
+            {0x60, 0x59, 0x02, 0x4b, 0x5c, 0x65, 0x4e, 0x07},
+            {0x60, 0x59, 0x02, 0x4b, 0x5c, 0x65, 0x4e, 0x07},
+            {0x60, 0x59, 0x02, 0x4b, 0x5c, 0x65, 0x4e, 0x07},
+            {0x60, 0x59, 0x02, 0x4b, 0x5c, 0x65, 0x4e, 0x07},
+            {0x60, 0x59, 0x12, 0x4b, 0x5c, 0x65, 0x4e, 0x07},
+            {0x60, 0x59, 0x12, 0x4b, 0x5c, 0x65, 0x4e, 0x07},
+            {0x60, 0x59, 0x12, 0x6b, 0x5c, 0x65, 0x4e, 0x17},
+            {0x60, 0x59, 0x12, 0x6b, 0x5c, 0x65, 0x4e, 0x17},
+            {0x20, 0x59, 0x12, 0x6b, 0x5c, 0x65, 0x2e, 0x17},
+            {0x20, 0x59, 0x12, 0x6b, 0x5c, 0x65, 0x2e, 0x17},
+            {0x20, 0x19, 0x12, 0x6b, 0x5c, 0x25, 0x2e, 0x17},
+            {0x20, 0x19, 0x12, 0x6b, 0x5c, 0x25, 0x2e, 0x17},
+            {0x20, 0x19, 0x32, 0x6b, 0x1c, 0x25, 0x2e, 0x17},
+            {0x20, 0x19, 0x32, 0x6b, 0x1c, 0x25, 0x2e, 0x17},
+            {0x20, 0x19, 0x32, 0x7b, 0x1c, 0x25, 0x2e, 0x37},
+            {0x20, 0x19, 0x32, 0x7b, 0x1c, 0x25, 0x2e, 0x37},
+            {0x28, 0x19, 0x32, 0x7b, 0x1c, 0x25, 0x3e, 0x37},
+            {0x28, 0x19, 0x32, 0x7b, 0x1c, 0x25, 0x3e, 0x37},
+            {0x28, 0x11, 0x32, 0x7b, 0x1c, 0x2d, 0x3e, 0x37},
+            {0x28, 0x11, 0x32, 0x7b, 0x1c, 0x2d, 0x3e, 0x37},
+            {0x28, 0x11, 0x22, 0x7b, 0x14, 0x2d, 0x3e, 0x37},
+            {0x28, 0x11, 0x22, 0x7b, 0x14, 0x2d, 0x3e, 0x37},
+            {0x28, 0x11, 0x22, 0x5b, 0x14, 0x2d, 0x3e, 0x27},
+            {0x28, 0x11, 0x22, 0x5b, 0x14, 0x2d, 0x3e, 0x27},
+            {0x68, 0x11, 0x22, 0x5b, 0x14, 0x2d, 0x1e, 0x27},
+            {0x68, 0x11, 0x22, 0x5b, 0x14, 0x2d, 0x1e, 0x27},
+            {0x68, 0x51, 0x22, 0x5b, 0x14, 0x6d, 0x1e, 0x27},
+            {0x68, 0x51, 0x22, 0x5b, 0x14, 0x6d, 0x1e, 0x27},
+            {0x68, 0x51, 0x42, 0x5b, 0x54, 0x6d, 0x1e, 0x27},
+            {0x68, 0x51, 0x42, 0x5b, 0x54, 0x6d, 0x1e, 0x27},
+            {0x68, 0x51, 0x42, 0x5b, 0x54, 0x6d, 0x1e, 0x47},
+            {0x68, 0x51, 0x42, 0x0b, 0x54, 0x6d, 0x1e, 0x47},
+            {0x68, 0x51, 0x42, 0x0b, 0x54, 0x6d, 0x0e, 0x47},
+            {0x68, 0x51, 0x42, 0x0b, 0x54, 0x6d, 0x0e, 0x47},
+            {0x68, 0x51, 0x42, 0x0b, 0x54, 0x6d, 0x0e, 0x47},
+            {0x68, 0x51, 0x42, 0x0b, 0x54, 0x6d, 0x0e, 0x47}
+        },
+        {
+            {0x60, 0x59, 0x02, 0x4b, 0x5c, 0x65, 0x4e, 0x07},
+            {0x60, 0x59, 0x02, 0x4b, 0x5c, 0x65, 0x4e, 0x07},
+            {0x60, 0x59, 0x02, 0x4b, 0x5c, 0x65, 0x4e, 0x07},
+            {0x60, 0x59, 0x02, 0x4b, 0x5c, 0x65, 0x4e, 0x07},
+            {0x60, 0x59, 0x12, 0x4b, 0x5c, 0x65, 0x4e, 0x07},
+            {0x60, 0x59, 0x12, 0x6b, 0x5c, 0x65, 0x4e, 0x07},
+            {0x60, 0x59, 0x12, 0x6b, 0x5c, 0x65, 0x4e, 0x17},
+            {0x60, 0x59, 0x12, 0x6b, 0x5c, 0x65, 0x4e, 0x17},
+            {0x20, 0x59, 0x12, 0x6b, 0x5c, 0x65, 0x2e, 0x17},
+            {0x20, 0x59, 0x12, 0x6b, 0x5c, 0x65, 0x2e, 0x17},
+            {0x20, 0x19, 0x12, 0x6b, 0x5c, 0x25, 0x2e, 0x17},
+            {0x20, 0x19, 0x12, 0x6b, 0x5c, 0x25, 0x2e, 0x17},
+            {0x20, 0x19, 0x32, 0x6b, 0x1c, 0x25, 0x2e, 0x17},
+            {0x20, 0x19, 0x32, 0x6b, 0x1c, 0x25, 0x2e, 0x17},
+            {0x20, 0x19, 0x32, 0x7b, 0x1c, 0x25, 0x2e, 0x37},
+            {0x20, 0x19, 0x32, 0x7b, 0x1c, 0x25, 0x2e, 0x37},
+            {0x28, 0x19, 0x32, 0x7b, 0x1c, 0x25, 0x3e, 0x37},
+            {0x28, 0x19, 0x32, 0x7b, 0x1c, 0x25, 0x3e, 0x37},
+            {0x28, 0x11, 0x32, 0x7b, 0x1c, 0x2d, 0x3e, 0x37},
+            {0x28, 0x11, 0x32, 0x7b, 0x1c, 0x2d, 0x3e, 0x37},
+            {0x28, 0x11, 0x22, 0x7b, 0x14, 0x2d, 0x3e, 0x37},
+            {0x28, 0x11, 0x22, 0x7b, 0x14, 0x2d, 0x3e, 0x37},
+            {0x28, 0x11, 0x22, 0x5b, 0x14, 0x2d, 0x3e, 0x27},
+            {0x28, 0x11, 0x22, 0x5b, 0x14, 0x2d, 0x3e, 0x27},
+            {0x68, 0x11, 0x22, 0x5b, 0x14, 0x2d, 0x1e, 0x27},
+            {0x68, 0x11, 0x22, 0x5b, 0x14, 0x2d, 0x1e, 0x27},
+            {0x68, 0x51, 0x22, 0x5b, 0x14, 0x6d, 0x1e, 0x27},
+            {0x68, 0x51, 0x22, 0x5b, 0x14, 0x6d, 0x1e, 0x27},
+            {0x68, 0x51, 0x42, 0x5b, 0x54, 0x6d, 0x1e, 0x27},
+            {0x68, 0x51, 0x42, 0x5b, 0x54, 0x6d, 0x1e, 0x27},
+            {0x68, 0x51, 0x42, 0x5b, 0x54, 0x6d, 0x1e, 0x47},
+            {0x68, 0x51, 0x42, 0x5b, 0x54, 0x6d, 0x1e, 0x47},
+            {0x68, 0x51, 0x42, 0x0b, 0x54, 0x6d, 0x0e, 0x47},
+            {0x68, 0x51, 0x42, 0x0b, 0x54, 0x6d, 0x0e, 0x47},
+            {0x68, 0x51, 0x42, 0x0b, 0x54, 0x6d, 0x0e, 0x47},
+            {0x68, 0x51, 0x42, 0x0b, 0x54, 0x6d, 0x0e, 0x47}
+        },
+        {
+            {0x60, 0x59, 0x02, 0x4b, 0x5c, 0x65, 0x4e, 0x07},
+            {0x60, 0x59, 0x02, 0x4b, 0x5c, 0x65, 0x4e, 0x07},
+            {0x60, 0x59, 0x02, 0x4b, 0x5c, 0x65, 0x4e, 0x07},
+            {0x60, 0x59, 0x02, 0x4b, 0x5c, 0x65, 0x4e, 0x07},
+            {0x60, 0x59, 0x52, 0x6b, 0x5c, 0x65, 0x4e, 0x07},
+            {0x60, 0x59, 0x52, 0x6b, 0x5c, 0x65, 0x4e, 0x07},
+            {0x60, 0x59, 0x52, 0x6b, 0x5c, 0x65, 0x4e, 0x17},
+            {0x60, 0x59, 0x52, 0x6b, 0x5c, 0x65, 0x4e, 0x17},
+            {0x20, 0x59, 0x52, 0x6b, 0x5c, 0x65, 0x6e, 0x17},
+            {0x20, 0x59, 0x52, 0x6b, 0x5c, 0x65, 0x6e, 0x17},
+            {0x20, 0x19, 0x52, 0x6b, 0x5c, 0x25, 0x6e, 0x17},
+            {0x20, 0x19, 0x52, 0x6b, 0x5c, 0x25, 0x6e, 0x17},
+            {0x20, 0x19, 0x72, 0x6b, 0x1c, 0x25, 0x6e, 0x17},
+            {0x20, 0x19, 0x72, 0x6b, 0x1c, 0x25, 0x6e, 0x17},
+            {0x20, 0x19, 0x72, 0x7b, 0x1c, 0x25, 0x6e, 0x37},
+            {0x20, 0x19, 0x72, 0x7b, 0x1c, 0x25, 0x6e, 0x37},
+            {0x28, 0x19, 0x72, 0x7b, 0x1c, 0x25, 0x7e, 0x37},
+            {0x28, 0x19, 0x72, 0x7b, 0x1c, 0x25, 0x7e, 0x37},
+            {0x28, 0x11, 0x72, 0x7b, 0x1c, 0x2d, 0x7e, 0x37},
+            {0x28, 0x11, 0x72, 0x7b, 0x1c, 0x2d, 0x7e, 0x37},
+            {0x28, 0x11, 0x62, 0x7b, 0x14, 0x2d, 0x7e, 0x37},
+            {0x28, 0x11, 0x62, 0x7b, 0x14, 0x2d, 0x7e, 0x37},
+            {0x28, 0x11, 0x62, 0x5b, 0x14, 0x2d, 0x7e, 0x27},
+            {0x28, 0x11, 0x62, 0x5b, 0x14, 0x2d, 0x7e, 0x27},
+            {0x68, 0x11, 0x62, 0x5b, 0x14, 0x2d, 0x5e, 0x27},
+            {0x68, 0x11, 0x62, 0x5b, 0x14, 0x2d, 0x5e, 0x27},
+            {0x68, 0x51, 0x62, 0x5b, 0x14, 0x6d, 0x5e, 0x27},
+            {0x68, 0x51, 0x62, 0x5b, 0x14, 0x6d, 0x5e, 0x27},
+            {0x68, 0x51, 0x62, 0x5b, 0x54, 0x6d, 0x5e, 0x27},
+            {0x68, 0x51, 0x42, 0x5b, 0x54, 0x6d, 0x5e, 0x27},
+            {0x68, 0x51, 0x42, 0x5b, 0x54, 0x6d, 0x5e, 0x47},
+            {0x68, 0x51, 0x42, 0x5b, 0x54, 0x6d, 0x5e, 0x47},
+            {0x68, 0x51, 0x42, 0x5b, 0x54, 0x6d, 0x5e, 0x47},
+            {0x68, 0x51, 0x42, 0x0b, 0x54, 0x6d, 0x0e, 0x47},
+            {0x68, 0x51, 0x42, 0x0b, 0x54, 0x6d, 0x0e, 0x47},
+            {0x68, 0x51, 0x42, 0x0b, 0x54, 0x6d, 0x0e, 0x47}
+        },
+        {
+            {0x60, 0x59, 0x02, 0x4b, 0x5c, 0x65, 0x4e, 0x07},
+            {0x60, 0x59, 0x02, 0x4b, 0x5c, 0x65, 0x4e, 0x07},
+            {0x60, 0x59, 0x02, 0x4b, 0x5c, 0x65, 0x4e, 0x07},
+            {0x60, 0x59, 0x52, 0x6b, 0x5c, 0x65, 0x4e, 0x07},
+            {0x60, 0x59, 0x52, 0x6b, 0x5c, 0x65, 0x4e, 0x07},
+            {0x60, 0x59, 0x52, 0x6b, 0x5c, 0x65, 0x4e, 0x07},
+            {0x60, 0x59, 0x52, 0x6b, 0x5c, 0x65, 0x4e, 0x17},
+            {0x60, 0x59, 0x52, 0x6b, 0x5c, 0x65, 0x6e, 0x17},
+            {0x20, 0x59, 0x52, 0x6b, 0x5c, 0x65, 0x6e, 0x17},
+            {0x20, 0x59, 0x52, 0x6b, 0x5c, 0x65, 0x6e, 0x17},
+            {0x20, 0x19, 0x52, 0x6b, 0x5c, 0x25, 0x6e, 0x17},
+            {0x20, 0x19, 0x52, 0x6b, 0x5c, 0x25, 0x6e, 0x17},
+            {0x20, 0x19, 0x72, 0x6b, 0x1c, 0x25, 0x6e, 0x17},
+            {0x20, 0x19, 0x72, 0x6b, 0x1c, 0x25, 0x6e, 0x17},
+            {0x20, 0x19, 0x72, 0x7b, 0x1c, 0x25, 0x6e, 0x37},
+            {0x20, 0x19, 0x72, 0x7b, 0x1c, 0x25, 0x6e, 0x37},
+            {0x28, 0x19, 0x72, 0x7b, 0x1c, 0x25, 0x7e, 0x37},
+            {0x28, 0x19, 0x72, 0x7b, 0x1c, 0x25, 0x7e, 0x37},
+            {0x28, 0x11, 0x72, 0x7b, 0x1c, 0x2d, 0x7e, 0x37},
+            {0x28, 0x11, 0x72, 0x7b, 0x1c, 0x2d, 0x7e, 0x37},
+            {0x28, 0x11, 0x62, 0x7b, 0x14, 0x2d, 0x7e, 0x37},
+            {0x28, 0x11, 0x62, 0x7b, 0x14, 0x2d, 0x7e, 0x37},
+            {0x28, 0x11, 0x62, 0x5b, 0x14, 0x2d, 0x7e, 0x27},
+            {0x28, 0x11, 0x62, 0x5b, 0x14, 0x2d, 0x7e, 0x27},
+            {0x68, 0x11, 0x62, 0x5b, 0x14, 0x2d, 0x5e, 0x27},
+            {0x68, 0x11, 0x62, 0x5b, 0x14, 0x2d, 0x5e, 0x27},
+            {0x68, 0x51, 0x62, 0x5b, 0x14, 0x6d, 0x5e, 0x27},
+            {0x68, 0x51, 0x62, 0x5b, 0x14, 0x6d, 0x5e, 0x27},
+            {0x68, 0x51, 0x62, 0x5b, 0x54, 0x6d, 0x5e, 0x27},
+            {0x68, 0x51, 0x62, 0x5b, 0x54, 0x6d, 0x5e, 0x27},
+            {0x68, 0x51, 0x42, 0x5b, 0x54, 0x6d, 0x5e, 0x47},
+            {0x68, 0x51, 0x42, 0x5b, 0x54, 0x6d, 0x5e, 0x47},
+            {0x68, 0x51, 0x42, 0x5b, 0x54, 0x6d, 0x5e, 0x47},
+            {0x68, 0x51, 0x42, 0x5b, 0x54, 0x6d, 0x5e, 0x47},
+            {0x68, 0x51, 0x42, 0x0b, 0x54, 0x6d, 0x0e, 0x47},
+            {0x68, 0x51, 0x42, 0x0b, 0x54, 0x6d, 0x0e, 0x47}
+        },
+        {
+            {0x60, 0x59, 0x02, 0x4b, 0x5c, 0x65, 0x4e, 0x07},
+            {0x60, 0x59, 0x02, 0x4b, 0x5c, 0x65, 0x4e, 0x07},
+            {0x60, 0x59, 0x52, 0x6b, 0x5c, 0x65, 0x4e, 0x07},
+            {0x60, 0x59, 0x52, 0x6b, 0x5c, 0x65, 0x4e, 0x07},
+            {0x60, 0x59, 0x52, 0x6b, 0x5c, 0x65, 0x4e, 0x07},
+            {0x60, 0x59, 0x52, 0x6b, 0x5c, 0x65, 0x4e, 0x07},
+            {0x60, 0x59, 0x52, 0x6b, 0x5c, 0x65, 0x6e, 0x57},
+            {0x60, 0x59, 0x52, 0x6b, 0x5c, 0x65, 0x6e, 0x57},
+            {0x20, 0x59, 0x52, 0x6b, 0x5c, 0x65, 0x6e, 0x57},
+            {0x20, 0x59, 0x52, 0x6b, 0x5c, 0x65, 0x6e, 0x57},
+            {0x20, 0x19, 0x52, 0x6b, 0x5c, 0x45, 0x6e, 0x57},
+            {0x20, 0x19, 0x52, 0x6b, 0x5c, 0x45, 0x6e, 0x57},
+            {0x20, 0x19, 0x72, 0x6b, 0x1c, 0x45, 0x6e, 0x57},
+            {0x20, 0x19, 0x72, 0x6b, 0x1c, 0x45, 0x6e, 0x57},
+            {0x20, 0x19, 0x72, 0x7b, 0x1c, 0x45, 0x6e, 0x77},
+            {0x20, 0x19, 0x72, 0x7b, 0x1c, 0x45, 0x6e, 0x77},
+            {0x28, 0x19, 0x72, 0x7b, 0x1c, 0x45, 0x7e, 0x77},
+            {0x28, 0x19, 0x72, 0x7b, 0x1c, 0x45, 0x7e, 0x77},
+            {0x28, 0x11, 0x72, 0x7b, 0x1c, 0x4d, 0x7e, 0x77},
+            {0x28, 0x11, 0x72, 0x7b, 0x1c, 0x4d, 0x7e, 0x77},
+            {0x28, 0x11, 0x62, 0x7b, 0x14, 0x4d, 0x7e, 0x77},
+            {0x28, 0x11, 0x62, 0x7b, 0x14, 0x4d, 0x7e, 0x77},
+            {0x28, 0x11, 0x62, 0x5b, 0x14, 0x4d, 0x7e, 0x67},
+            {0x28, 0x11, 0x62, 0x5b, 0x14, 0x4d, 0x7e, 0x67},
+            {0x68, 0x11, 0x62, 0x5b, 0x14, 0x4d, 0x5e, 0x67},
+            {0x68, 0x11, 0x62, 0x5b, 0x14, 0x4d, 0x5e, 0x67},
+            {0x68, 0x51, 0x62, 0x5b, 0x14, 0x6d, 0x5e, 0x67},
+            {0x68, 0x51, 0x62, 0x5b, 0x14, 0x6d, 0x5e, 0x67},
+            {0x68, 0x51, 0x62, 0x5b, 0x54, 0x6d, 0x5e, 0x67},
+            {0x68, 0x51, 0x62, 0x5b, 0x54, 0x6d, 0x5e, 0x67},
+            {0x68, 0x51, 0x62, 0x5b, 0x54, 0x6d, 0x5e, 0x67},
+            {0x68, 0x51, 0x42, 0x5b, 0x54, 0x6d, 0x5e, 0x47},
+            {0x68, 0x51, 0x42, 0x5b, 0x54, 0x6d, 0x5e, 0x47},
+            {0x68, 0x51, 0x42, 0x5b, 0x54, 0x6d, 0x5e, 0x47},
+            {0x68, 0x51, 0x42, 0x0b, 0x54, 0x6d, 0x0e, 0x47},
+            {0x68, 0x51, 0x42, 0x0b, 0x54, 0x6d, 0x0e, 0x47}
+        },
+        {
+            {0x60, 0x59, 0x02, 0x4b, 0x5c, 0x65, 0x4e, 0x07},
+            {0x60, 0x59, 0x52, 0x6b, 0x5c, 0x65, 0x4e, 0x07},
+            {0x60, 0x59, 0x52, 0x6b, 0x5c, 0x65, 0x4e, 0x07},
+            {0x60, 0x59, 0x52, 0x6b, 0x5c, 0x65, 0x4e, 0x07},
+            {0x60, 0x59, 0x52, 0x6b, 0x5c, 0x65, 0x4e, 0x07},
+            {0x60, 0x59, 0x52, 0x6b, 0x5c, 0x65, 0x6e, 0x57},
+            {0x60, 0x59, 0x52, 0x6b, 0x5c, 0x65, 0x6e, 0x57},
+            {0x60, 0x59, 0x52, 0x6b, 0x5c, 0x65, 0x6e, 0x57},
+            {0x20, 0x59, 0x52, 0x6b, 0x5c, 0x65, 0x6e, 0x57},
+            {0x20, 0x59, 0x52, 0x6b, 0x5c, 0x45, 0x6e, 0x57},
+            {0x20, 0x19, 0x52, 0x6b, 0x5c, 0x45, 0x6e, 0x57},
+            {0x20, 0x19, 0x52, 0x6b, 0x5c, 0x45, 0x6e, 0x57},
+            {0x20, 0x19, 0x72, 0x6b, 0x1c, 0x45, 0x6e, 0x57},
+            {0x20, 0x19, 0x72, 0x6b, 0x1c, 0x45, 0x6e, 0x57},
+            {0x20, 0x19, 0x72, 0x7b, 0x1c, 0x45, 0x6e, 0x77},
+            {0x20, 0x19, 0x72, 0x7b, 0x1c, 0x45, 0x6e, 0x77},
+            {0x28, 0x19, 0x72, 0x7b, 0x1c, 0x45, 0x7e, 0x77},
+            {0x28, 0x19, 0x72, 0x7b, 0x1c, 0x45, 0x7e, 0x77},
+            {0x28, 0x11, 0x72, 0x7b, 0x1c, 0x4d, 0x7e, 0x77},
+            {0x28, 0x11, 0x72, 0x7b, 0x1c, 0x4d, 0x7e, 0x77},
+            {0x28, 0x11, 0x62, 0x7b, 0x14, 0x4d, 0x7e, 0x77},
+            {0x28, 0x11, 0x62, 0x7b, 0x14, 0x4d, 0x7e, 0x77},
+            {0x28, 0x11, 0x62, 0x5b, 0x14, 0x4d, 0x7e, 0x67},
+            {0x28, 0x11, 0x62, 0x5b, 0x14, 0x4d, 0x7e, 0x67},
+            {0x68, 0x11, 0x62, 0x5b, 0x14, 0x4d, 0x5e, 0x67},
+            {0x68, 0x11, 0x62, 0x5b, 0x14, 0x4d, 0x5e, 0x67},
+            {0x68, 0x51, 0x62, 0x5b, 0x14, 0x4d, 0x5e, 0x67},
+            {0x68, 0x51, 0x62, 0x5b, 0x14, 0x6d, 0x5e, 0x67},
+            {0x68, 0x51, 0x62, 0x5b, 0x54, 0x6d, 0x5e, 0x67},
+            {0x68, 0x51, 0x62, 0x5b, 0x54, 0x6d, 0x5e, 0x67},
+            {0x68, 0x51, 0x62, 0x5b, 0x54, 0x6d, 0x5e, 0x67},
+            {0x68, 0x51, 0x62, 0x5b, 0x54, 0x6d, 0x5e, 0x67},
+            {0x68, 0x51, 0x42, 0x5b, 0x54, 0x6d, 0x5e, 0x47},
+            {0x68, 0x51, 0x42, 0x5b, 0x54, 0x6d, 0x5e, 0x47},
+            {0x68, 0x51, 0x42, 0x5b, 0x54, 0x6d, 0x5e, 0x47},
+            {0x68, 0x51, 0x42, 0x0b, 0x54, 0x6d, 0x0e, 0x47}
+        },
+        {
+            {0x60, 0x59, 0x52, 0x6b, 0x5c, 0x65, 0x4e, 0x07},
+            {0x60, 0x59, 0x52, 0x6b, 0x5c, 0x65, 0x4e, 0x07},
+            {0x60, 0x59, 0x52, 0x6b, 0x5c, 0x65, 0x4e, 0x07},
+            {0x60, 0x59, 0x52, 0x6b, 0x5c, 0x65, 0x4e, 0x07},
+            {0x60, 0x59, 0x52, 0x6b, 0x5c, 0x65, 0x6e, 0x57},
+            {0x60, 0x59, 0x52, 0x6b, 0x5c, 0x65, 0x6e, 0x57},
+            {0x60, 0x59, 0x52, 0x6b, 0x5c, 0x65, 0x6e, 0x57},
+            {0x60, 0x59, 0x52, 0x6b, 0x5c, 0x65, 0x6e, 0x57},
+            {0x40, 0x59, 0x52, 0x6b, 0x5c, 0x45, 0x6e, 0x57},
+            {0x40, 0x59, 0x52, 0x6b, 0x5c, 0x45, 0x6e, 0x57},
+            {0x40, 0x19, 0x52, 0x6b, 0x5c, 0x45, 0x6e, 0x57},
+            {0x40, 0x19, 0x52, 0x6b, 0x5c, 0x45, 0x6e, 0x57},
+            {0x40, 0x19, 0x72, 0x6b, 0x0c, 0x45, 0x6e, 0x57},
+            {0x40, 0x19, 0x72, 0x6b, 0x0c, 0x45, 0x6e, 0x57},
+            {0x40, 0x19, 0x72, 0x7b, 0x0c, 0x45, 0x6e, 0x77},
+            {0x40, 0x19, 0x72, 0x7b, 0x0c, 0x45, 0x6e, 0x77},
+            {0x48, 0x19, 0x72, 0x7b, 0x0c, 0x45, 0x7e, 0x77},
+            {0x48, 0x19, 0x72, 0x7b, 0x0c, 0x45, 0x7e, 0x77},
+            {0x48, 0x11, 0x72, 0x7b, 0x0c, 0x4d, 0x7e, 0x77},
+            {0x48, 0x11, 0x72, 0x7b, 0x0c, 0x4d, 0x7e, 0x77},
+            {0x48, 0x11, 0x62, 0x7b, 0x04, 0x4d, 0x7e, 0x77},
+            {0x48, 0x11, 0x62, 0x7b, 0x04, 0x4d, 0x7e, 0x77},
+            {0x48, 0x11, 0x62, 0x5b, 0x04, 0x4d, 0x7e, 0x67},
+            {0x48, 0x11, 0x62, 0x5b, 0x04, 0x4d, 0x7e, 0x67},
+            {0x68, 0x11, 0x62, 0x5b, 0x04, 0x4d, 0x5e, 0x67},
+            {0x68, 0x11, 0x62, 0x5b, 0x04, 0x4d, 0x5e, 0x67},
+            {0x68, 0x51, 0x62, 0x5b, 0x04, 0x4d, 0x5e, 0x67},
+            {0x68, 0x51, 0x62, 0x5b, 0x04, 0x4d, 0x5e, 0x67},
+            {0x68, 0x51, 0x62, 0x5b, 0x54, 0x6d, 0x5e, 0x67},
+            {0x68, 0x51, 0x62, 0x5b, 0x54, 0x6d, 0x5e, 0x67},
+            {0x68, 0x51, 0x62, 0x5b, 0x54, 0x6d, 0x5e, 0x67},
+            {0x68, 0x51, 0x62, 0x5b, 0x54, 0x6d, 0x5e, 0x67},
+            {0x68, 0x51, 0x62, 0x5b, 0x54, 0x6d, 0x5e, 0x67},
+            {0x68, 0x51, 0x42, 0x5b, 0x54, 0x6d, 0x5e, 0x47},
+            {0x68, 0x51, 0x42, 0x5b, 0x54, 0x6d, 0x5e, 0x47},
+            {0x68, 0x51, 0x42, 0x5b, 0x54, 0x6d, 0x5e, 0x47}
+        },
+        {
+            {0x60, 0x59, 0x52, 0x6b, 0x5c, 0x65, 0x4e, 0x07},
+            {0x60, 0x59, 0x52, 0x6b, 0x5c, 0x65, 0x4e, 0x07},
+            {0x60, 0x59, 0x52, 0x6b, 0x5c, 0x65, 0x4e, 0x07},
+            {0x60, 0x59, 0x52, 0x6b, 0x5c, 0x65, 0x6e, 0x57},
+            {0x60, 0x59, 0x52, 0x6b, 0x5c, 0x65, 0x6e, 0x57},
+            {0x60, 0x59, 0x52, 0x6b, 0x5c, 0x65, 0x6e, 0x57},
+            {0x60, 0x59, 0x52, 0x6b, 0x5c, 0x65, 0x6e, 0x57},
+            {0x40, 0x59, 0x52, 0x6b, 0x5c, 0x45, 0x6e, 0x57},
+            {0x40, 0x59, 0x52, 0x6b, 0x5c, 0x45, 0x6e, 0x57},
+            {0x40, 0x59, 0x52, 0x6b, 0x5c, 0x45, 0x6e, 0x57},
+            {0x40, 0x19, 0x52, 0x6b, 0x5c, 0x45, 0x6e, 0x57},
+            {0x40, 0x19, 0x52, 0x6b, 0x0c, 0x45, 0x6e, 0x57},
+            {0x40, 0x19, 0x72, 0x6b, 0x0c, 0x45, 0x6e, 0x57},
+            {0x40, 0x19, 0x72, 0x6b, 0x0c, 0x45, 0x6e, 0x57},
+            {0x40, 0x19, 0x72, 0x7b, 0x0c, 0x45, 0x6e, 0x77},
+            {0x40, 0x19, 0x72, 0x7b, 0x0c, 0x45, 0x6e, 0x77},
+            {0x48, 0x19, 0x72, 0x7b, 0x0c, 0x45, 0x7e, 0x77},
+            {0x48, 0x19, 0x72, 0x7b, 0x0c, 0x45, 0x7e, 0x77},
+            {0x48, 0x11, 0x72, 0x7b, 0x0c, 0x4d, 0x7e, 0x77},
+            {0x48, 0x11, 0x72, 0x7b, 0x0c, 0x4d, 0x7e, 0x77},
+            {0x48, 0x11, 0x62, 0x7b, 0x04, 0x4d, 0x7e, 0x77},
+            {0x48, 0x11, 0x62, 0x7b, 0x04, 0x4d, 0x7e, 0x77},
+            {0x48, 0x11, 0x62, 0x5b, 0x04, 0x4d, 0x7e, 0x67},
+            {0x48, 0x11, 0x62, 0x5b, 0x04, 0x4d, 0x7e, 0x67},
+            {0x48, 0x11, 0x62, 0x5b, 0x04, 0x4d, 0x5e, 0x67},
+            {0x68, 0x11, 0x62, 0x5b, 0x04, 0x4d, 0x5e, 0x67},
+            {0x68, 0x51, 0x62, 0x5b, 0x04, 0x4d, 0x5e, 0x67},
+            {0x68, 0x51, 0x62, 0x5b, 0x04, 0x4d, 0x5e, 0x67},
+            {0x68, 0x51, 0x62, 0x5b, 0x04, 0x4d, 0x5e, 0x67},
+            {0x68, 0x51, 0x62, 0x5b, 0x54, 0x6d, 0x5e, 0x67},
+            {0x68, 0x51, 0x62, 0x5b, 0x54, 0x6d, 0x5e, 0x67},
+            {0x68, 0x51, 0x62, 0x5b, 0x54, 0x6d, 0x5e, 0x67},
+            {0x68, 0x51, 0x62, 0x5b, 0x54, 0x6d, 0x5e, 0x67},
+            {0x68, 0x51, 0x62, 0x5b, 0x54, 0x6d, 0x5e, 0x67},
+            {0x68, 0x51, 0x42, 0x5b, 0x54, 0x6d, 0x5e, 0x47},
+            {0x68, 0x51, 0x42, 0x5b, 0x54, 0x6d, 0x5e, 0x47}
+        },
+        {
+            {0x60, 0x59, 0x52, 0x6b, 0x5c, 0x65, 0x4e, 0x07},
+            {0x60, 0x59, 0x52, 0x6b, 0x5c, 0x65, 0x4e, 0x07},
+            {0x60, 0x59, 0x52, 0x6b, 0x5c, 0x65, 0x6e, 0x57},
+            {0x60, 0x59, 0x52, 0x6b, 0x5c, 0x65, 0x6e, 0x57},
+            {0x60, 0x59, 0x52, 0x6b, 0x5c, 0x65, 0x6e, 0x57},
+            {0x60, 0x59, 0x52, 0x6b, 0x5c, 0x65, 0x6e, 0x57},
+            {0x40, 0x59, 0x52, 0x6b, 0x5c, 0x45, 0x6e, 0x57},
+            {0x40, 0x59, 0x52, 0x6b, 0x5c, 0x45, 0x6e, 0x57},
+            {0x40, 0x59, 0x52, 0x6b, 0x5c, 0x45, 0x6e, 0x57},
+            {0x40, 0x59, 0x52, 0x6b, 0x5c, 0x45, 0x6e, 0x57},
+            {0x40, 0x09, 0x52, 0x6b, 0x0c, 0x45, 0x6e, 0x57},
+            {0x40, 0x09, 0x52, 0x6b, 0x0c, 0x45, 0x6e, 0x57},
+            {0x40, 0x09, 0x72, 0x6b, 0x0c, 0x45, 0x6e, 0x57},
+            {0x40, 0x09, 0x72, 0x6b, 0x0c, 0x45, 0x6e, 0x57},
+            {0x40, 0x09, 0x72, 0x7b, 0x0c, 0x45, 0x6e, 0x77},
+            {0x40, 0x09, 0x72, 0x7b, 0x0c, 0x45, 0x6e, 0x77},
+            {0x48, 0x09, 0x72, 0x7b, 0x0c, 0x45, 0x7e, 0x77},
+            {0x48, 0x09, 0x72, 0x7b, 0x0c, 0x45, 0x7e, 0x77},
+            {0x48, 0x01, 0x72, 0x7b, 0x0c, 0x4d, 0x7e, 0x77},
+            {0x48, 0x01, 0x72, 0x7b, 0x0c, 0x4d, 0x7e, 0x77},
+            {0x48, 0x01, 0x62, 0x7b, 0x04, 0x4d, 0x7e, 0x77},
+            {0x48, 0x01, 0x62, 0x7b, 0x04, 0x4d, 0x7e, 0x77},
+            {0x48, 0x01, 0x62, 0x5b, 0x04, 0x4d, 0x7e, 0x67},
+            {0x48, 0x01, 0x62, 0x5b, 0x04, 0x4d, 0x7e, 0x67},
+            {0x48, 0x01, 0x62, 0x5b, 0x04, 0x4d, 0x5e, 0x67},
+            {0x48, 0x01, 0x62, 0x5b, 0x04, 0x4d, 0x5e, 0x67},
+            {0x68, 0x51, 0x62, 0x5b, 0x04, 0x4d, 0x5e, 0x67},
+            {0x68, 0x51, 0x62, 0x5b, 0x04, 0x4d, 0x5e, 0x67},
+            {0x68, 0x51, 0x62, 0x5b, 0x04, 0x4d, 0x5e, 0x67},
+            {0x68, 0x51, 0x62, 0x5b, 0x04, 0x4d, 0x5e, 0x67},
+            {0x68, 0x51, 0x62, 0x5b, 0x54, 0x6d, 0x5e, 0x67},
+            {0x68, 0x51, 0x62, 0x5b, 0x54, 0x6d, 0x5e, 0x67},
+            {0x68, 0x51, 0x62, 0x5b, 0x54, 0x6d, 0x5e, 0x67},
+            {0x68, 0x51, 0x62, 0x5b, 0x54, 0x6d, 0x5e, 0x67},
+            {0x68, 0x51, 0x42, 0x5b, 0x54, 0x6d, 0x5e, 0x47},
+            {0x68, 0x51, 0x42, 0x5b, 0x54, 0x6d, 0x5e, 0x47}
+        },
+        {
+            {0x60, 0x59, 0x52, 0x6b, 0x5c, 0x65, 0x4e, 0x07},
+            {0x60, 0x59, 0x52, 0x6b, 0x5c, 0x65, 0x6e, 0x57},
+            {0x60, 0x59, 0x52, 0x6b, 0x5c, 0x65, 0x6e, 0x57},
+            {0x60, 0x59, 0x52, 0x6b, 0x5c, 0x65, 0x6e, 0x57},
+            {0x60, 0x59, 0x52, 0x6b, 0x5c, 0x65, 0x6e, 0x57},
+            {0x40, 0x59, 0x52, 0x6b, 0x5c, 0x45, 0x6e, 0x57},
+            {0x40, 0x59, 0x52, 0x6b, 0x5c, 0x45, 0x6e, 0x57},
+            {0x40, 0x59, 0x52, 0x6b, 0x5c, 0x45, 0x6e, 0x57},
+            {0x40, 0x59, 0x52, 0x6b, 0x5c, 0x45, 0x6e, 0x57},
+            {0x40, 0x09, 0x52, 0x6b, 0x0c, 0x45, 0x6e, 0x57},
+            {0x40, 0x09, 0x52, 0x6b, 0x0c, 0x45, 0x6e, 0x57},
+            {0x40, 0x09, 0x52, 0x6b, 0x0c, 0x45, 0x6e, 0x57},
+            {0x40, 0x09, 0x72, 0x6b, 0x0c, 0x45, 0x6e, 0x57},
+            {0x40, 0x09, 0x72, 0x6b, 0x0c, 0x45, 0x6e, 0x57},
+            {0x40, 0x09, 0x72, 0x7b, 0x0c, 0x45, 0x6e, 0x77},
+            {0x40, 0x09, 0x72, 0x7b, 0x0c, 0x45, 0x6e, 0x77},
+            {0x48, 0x09, 0x72, 0x7b, 0x0c, 0x45, 0x7e, 0x77},
+            {0x48, 0x09, 0x72, 0x7b, 0x0c, 0x45, 0x7e, 0x77},
+            {0x48, 0x01, 0x72, 0x7b, 0x0c, 0x4d, 0x7e, 0x77},
+            {0x48, 0x01, 0x72, 0x7b, 0x0c, 0x4d, 0x7e, 0x77},
+            {0x48, 0x01, 0x62, 0x7b, 0x04, 0x4d, 0x7e, 0x77},
+            {0x48, 0x01, 0x62, 0x7b, 0x04, 0x4d, 0x7e, 0x77},
+            {0x48, 0x01, 0x62, 0x5b, 0x04, 0x4d, 0x7e, 0x67},
+            {0x48, 0x01, 0x62, 0x5b, 0x04, 0x4d, 0x7e, 0x67},
+            {0x48, 0x01, 0x62, 0x5b, 0x04, 0x4d, 0x5e, 0x67},
+            {0x48, 0x01, 0x62, 0x5b, 0x04, 0x4d, 0x5e, 0x67},
+            {0x48, 0x01, 0x62, 0x5b, 0x04, 0x4d, 0x5e, 0x67},
+            {0x68, 0x51, 0x62, 0x5b, 0x04, 0x4d, 0x5e, 0x67},
+            {0x68, 0x51, 0x62, 0x5b, 0x04, 0x4d, 0x5e, 0x67},
+            {0x68, 0x51, 0x62, 0x5b, 0x04, 0x4d, 0x5e, 0x67},
+            {0x68, 0x51, 0x62, 0x5b, 0x04, 0x4d, 0x5e, 0x67},
+            {0x68, 0x51, 0x62, 0x5b, 0x54, 0x6d, 0x5e, 0x67},
+            {0x68, 0x51, 0x62, 0x5b, 0x54, 0x6d, 0x5e, 0x67},
+            {0x68, 0x51, 0x62, 0x5b, 0x54, 0x6d, 0x5e, 0x67},
+            {0x68, 0x51, 0x62, 0x5b, 0x54, 0x6d, 0x5e, 0x67},
+            {0x68, 0x51, 0x42, 0x5b, 0x54, 0x6d, 0x5e, 0x47}
+        },
+        {
+            {0x60, 0x59, 0x52, 0x6b, 0x5c, 0x65, 0x6e, 0x57},
+            {0x60, 0x59, 0x52, 0x6b, 0x5c, 0x65, 0x6e, 0x57},
+            {0x60, 0x59, 0x52, 0x6b, 0x5c, 0x65, 0x6e, 0x57},
+            {0x60, 0x59, 0x52, 0x6b, 0x5c, 0x65, 0x6e, 0x57},
+            {0x40, 0x59, 0x52, 0x6b, 0x5c, 0x45, 0x6e, 0x57},
+            {0x40, 0x59, 0x52, 0x6b, 0x5c, 0x45, 0x6e, 0x57},
+            {0x40, 0x59, 0x52, 0x6b, 0x5c, 0x45, 0x6e, 0x57},
+            {0x40, 0x59, 0x52, 0x6b, 0x5c, 0x45, 0x6e, 0x57},
+            {0x40, 0x09, 0x52, 0x6b, 0x0c, 0x45, 0x6e, 0x57},
+            {0x40, 0x09, 0x52, 0x6b, 0x0c, 0x45, 0x6e, 0x57},
+            {0x40, 0x09, 0x52, 0x6b, 0x0c, 0x45, 0x6e, 0x57},
+            {0x40, 0x09, 0x52, 0x6b, 0x0c, 0x45, 0x6e, 0x57},
+            {0x40, 0x09, 0x72, 0x6b, 0x0c, 0x45, 0x6e, 0x57},
+            {0x40, 0x09, 0x72, 0x6b, 0x0c, 0x45, 0x6e, 0x57},
+            {0x40, 0x09, 0x72, 0x7b, 0x0c, 0x45, 0x6e, 0x77},
+            {0x40, 0x09, 0x72, 0x7b, 0x0c, 0x45, 0x6e, 0x77},
+            {0x48, 0x09, 0x72, 0x7b, 0x0c, 0x45, 0x7e, 0x77},
+            {0x48, 0x09, 0x72, 0x7b, 0x0c, 0x45, 0x7e, 0x77},
+            {0x48, 0x01, 0x72, 0x7b, 0x0c, 0x4d, 0x7e, 0x77},
+            {0x48, 0x01, 0x72, 0x7b, 0x0c, 0x4d, 0x7e, 0x77},
+            {0x48, 0x01, 0x62, 0x7b, 0x04, 0x4d, 0x7e, 0x77},
+            {0x48, 0x01, 0x62, 0x7b, 0x04, 0x4d, 0x7e, 0x77},
+            {0x48, 0x01, 0x62, 0x5b, 0x04, 0x4d, 0x7e, 0x67},
+            {0x48, 0x01, 0x62, 0x5b, 0x04, 0x4d, 0x7e, 0x67},
+            {0x48, 0x01, 0x62, 0x5b, 0x04, 0x4d, 0x5e, 0x67},
+            {0x48, 0x01, 0x62, 0x5b, 0x04, 0x4d, 0x5e, 0x67},
+            {0x48, 0x01, 0x62, 0x5b, 0x04, 0x4d, 0x5e, 0x67},
+            {0x48, 0x01, 0x62, 0x5b, 0x04, 0x4d, 0x5e, 0x67},
+            {0x48, 0x01, 0x62, 0x5b, 0x04, 0x4d, 0x5e, 0x67},
+            {0x68, 0x51, 0x62, 0x5b, 0x04, 0x4d, 0x5e, 0x67},
+            {0x68, 0x51, 0x62, 0x5b, 0x04, 0x4d, 0x5e, 0x67},
+            {0x68, 0x51, 0x62, 0x5b, 0x04, 0x4d, 0x5e, 0x67},
+            {0x68, 0x51, 0x62, 0x5b, 0x04, 0x4d, 0x5e, 0x67},
+            {0x68, 0x51, 0x62, 0x5b, 0x54, 0x6d, 0x5e, 0x67},
+            {0x68, 0x51, 0x62, 0x5b, 0x54, 0x6d, 0x5e, 0x67},
+            {0x68, 0x51, 0x62, 0x5b, 0x54, 0x6d, 0x5e, 0x67}
+        },
+        {
+            {0x60, 0x59, 0x52, 0x6b, 0x5c, 0x65, 0x6e, 0x57},
+            {0x60, 0x59, 0x52, 0x6b, 0x5c, 0x65, 0x6e, 0x57},
+            {0x60, 0x59, 0x52, 0x6b, 0x5c, 0x65, 0x6e, 0x57},
+            {0x40, 0x59, 0x52, 0x6b, 0x5c, 0x45, 0x6e, 0x57},
+            {0x40, 0x59, 0x52, 0x6b, 0x5c, 0x45, 0x6e, 0x57},
+            {0x40, 0x59, 0x52, 0x6b, 0x5c, 0x45, 0x6e, 0x57},
+            {0x40, 0x59, 0x52, 0x6b, 0x5c, 0x45, 0x6e, 0x57},
+            {0x40, 0x09, 0x52, 0x6b, 0x0c, 0x45, 0x6e, 0x57},
+            {0x40, 0x09, 0x52, 0x6b, 0x0c, 0x45, 0x6e, 0x57},
+            {0x40, 0x09, 0x52, 0x6b, 0x0c, 0x45, 0x6e, 0x57},
+            {0x40, 0x09, 0x52, 0x6b, 0x0c, 0x45, 0x6e, 0x57},
+            {0x40, 0x09, 0x52, 0x6b, 0x0c, 0x45, 0x6e, 0x57},
+            {0x40, 0x09, 0x72, 0x6b, 0x0c, 0x45, 0x6e, 0x57},
+            {0x40, 0x09, 0x72, 0x6b, 0x0c, 0x45, 0x6e, 0x57},
+            {0x40, 0x09, 0x72, 0x7b, 0x0c, 0x45, 0x6e, 0x77},
+            {0x40, 0x09, 0x72, 0x7b, 0x0c, 0x45, 0x6e, 0x77},
+            {0x48, 0x09, 0x72, 0x7b, 0x0c, 0x45, 0x7e, 0x77},
+            {0x48, 0x09, 0x72, 0x7b, 0x0c, 0x45, 0x7e, 0x77},
+            {0x48, 0x01, 0x72, 0x7b, 0x0c, 0x4d, 0x7e, 0x77},
+            {0x48, 0x01, 0x72, 0x7b, 0x0c, 0x4d, 0x7e, 0x77},
+            {0x48, 0x01, 0x62, 0x7b, 0x04, 0x4d, 0x7e, 0x77},
+            {0x48, 0x01, 0x62, 0x7b, 0x04, 0x4d, 0x7e, 0x77},
+            {0x48, 0x01, 0x62, 0x5b, 0x04, 0x4d, 0x7e, 0x67},
+            {0x48, 0x01, 0x62, 0x5b, 0x04, 0x4d, 0x7e, 0x67},
+            {0x48, 0x01, 0x62, 0x5b, 0x04, 0x4d, 0x5e, 0x67},
+            {0x48, 0x01, 0x62, 0x5b, 0x04, 0x4d, 0x5e, 0x67},
+            {0x48, 0x01, 0x62, 0x5b, 0x04, 0x4d, 0x5e, 0x67},
+            {0x48, 0x01, 0x62, 0x5b, 0x04, 0x4d, 0x5e, 0x67},
+            {0x48, 0x01, 0x62, 0x5b, 0x04, 0x4d, 0x5e, 0x67},
+            {0x48, 0x01, 0x62, 0x5b, 0x04, 0x4d, 0x5e, 0x67},
+            {0x68, 0x51, 0x62, 0x5b, 0x04, 0x4d, 0x5e, 0x67},
+            {0x68, 0x51, 0x62, 0x5b, 0x04, 0x4d, 0x5e, 0x67},
+            {0x68, 0x51, 0x62, 0x5b, 0x04, 0x4d, 0x5e, 0x67},
+            {0x68, 0x51, 0x62, 0x5b, 0x04, 0x4d, 0x5e, 0x67},
+            {0x68, 0x51, 0x62, 0x5b, 0x54, 0x6d, 0x5e, 0x67},
+            {0x68, 0x51, 0x62, 0x5b, 0x54, 0x6d, 0x5e, 0x67}
+        }
+    },
+    {   /* 12,000bps map */
+        {
+            {0x38, 0x21, 0x32, 0x13, 0x24, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x32, 0x13, 0x24, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x32, 0x13, 0x24, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x32, 0x13, 0x24, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x32, 0x13, 0x24, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x32, 0x13, 0x04, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x32, 0x13, 0x04, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x32, 0x13, 0x04, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x32, 0x13, 0x04, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x32, 0x13, 0x04, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x32, 0x13, 0x04, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x32, 0x13, 0x04, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x22, 0x1b, 0x04, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x22, 0x1b, 0x04, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x22, 0x1b, 0x04, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x22, 0x1b, 0x04, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x22, 0x1b, 0x04, 0x3d, 0x1e, 0x27},
+            {0x38, 0x01, 0x22, 0x1b, 0x04, 0x3d, 0x1e, 0x27},
+            {0x38, 0x01, 0x22, 0x1b, 0x04, 0x3d, 0x1e, 0x27},
+            {0x38, 0x01, 0x22, 0x1b, 0x04, 0x3d, 0x1e, 0x27},
+            {0x30, 0x01, 0x22, 0x1b, 0x04, 0x35, 0x1e, 0x27},
+            {0x30, 0x01, 0x22, 0x1b, 0x04, 0x35, 0x1e, 0x27},
+            {0x30, 0x01, 0x22, 0x1b, 0x04, 0x35, 0x1e, 0x27},
+            {0x30, 0x01, 0x22, 0x1b, 0x04, 0x35, 0x1e, 0x27},
+            {0x30, 0x11, 0x22, 0x1b, 0x14, 0x35, 0x1e, 0x27},
+            {0x30, 0x11, 0x22, 0x1b, 0x14, 0x35, 0x1e, 0x27},
+            {0x30, 0x11, 0x22, 0x1b, 0x14, 0x35, 0x1e, 0x27},
+            {0x30, 0x11, 0x22, 0x1b, 0x14, 0x35, 0x1e, 0x27},
+            {0x30, 0x11, 0x22, 0x1b, 0x14, 0x35, 0x1e, 0x27},
+            {0x30, 0x11, 0x22, 0x1b, 0x14, 0x35, 0x1e, 0x27},
+            {0x30, 0x11, 0x22, 0x1b, 0x14, 0x35, 0x1e, 0x27},
+            {0x30, 0x11, 0x22, 0x1b, 0x14, 0x35, 0x1e, 0x27},
+            {0x30, 0x11, 0x22, 0x3b, 0x14, 0x35, 0x1e, 0x27},
+            {0x30, 0x11, 0x22, 0x3b, 0x14, 0x35, 0x1e, 0x27},
+            {0x30, 0x11, 0x22, 0x3b, 0x14, 0x35, 0x1e, 0x27},
+            {0x30, 0x11, 0x22, 0x3b, 0x14, 0x35, 0x1e, 0x27}
+        },
+        {
+            {0x38, 0x21, 0x32, 0x13, 0x24, 0x1d, 0x16, 0x37},
+            {0x38, 0x21, 0x32, 0x13, 0x24, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x32, 0x13, 0x24, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x32, 0x13, 0x24, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x32, 0x13, 0x24, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x32, 0x13, 0x24, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x32, 0x13, 0x04, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x32, 0x13, 0x04, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x32, 0x13, 0x04, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x32, 0x13, 0x04, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x32, 0x13, 0x04, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x32, 0x13, 0x04, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x22, 0x1b, 0x04, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x22, 0x1b, 0x04, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x22, 0x1b, 0x04, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x22, 0x1b, 0x04, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x22, 0x1b, 0x04, 0x3d, 0x1e, 0x27},
+            {0x38, 0x01, 0x22, 0x1b, 0x04, 0x3d, 0x1e, 0x27},
+            {0x38, 0x01, 0x22, 0x1b, 0x04, 0x3d, 0x1e, 0x27},
+            {0x38, 0x01, 0x22, 0x1b, 0x04, 0x3d, 0x1e, 0x27},
+            {0x30, 0x01, 0x22, 0x1b, 0x04, 0x35, 0x1e, 0x27},
+            {0x30, 0x01, 0x22, 0x1b, 0x04, 0x35, 0x1e, 0x27},
+            {0x30, 0x01, 0x22, 0x1b, 0x04, 0x35, 0x1e, 0x27},
+            {0x30, 0x01, 0x22, 0x1b, 0x04, 0x35, 0x1e, 0x27},
+            {0x30, 0x11, 0x22, 0x1b, 0x14, 0x35, 0x1e, 0x27},
+            {0x30, 0x11, 0x22, 0x1b, 0x14, 0x35, 0x1e, 0x27},
+            {0x30, 0x11, 0x22, 0x1b, 0x14, 0x35, 0x1e, 0x27},
+            {0x30, 0x11, 0x22, 0x1b, 0x14, 0x35, 0x1e, 0x27},
+            {0x30, 0x11, 0x22, 0x1b, 0x14, 0x35, 0x1e, 0x27},
+            {0x30, 0x11, 0x22, 0x1b, 0x14, 0x35, 0x1e, 0x27},
+            {0x30, 0x11, 0x22, 0x1b, 0x14, 0x35, 0x1e, 0x27},
+            {0x30, 0x11, 0x22, 0x3b, 0x14, 0x35, 0x1e, 0x27},
+            {0x30, 0x11, 0x22, 0x3b, 0x14, 0x35, 0x1e, 0x27},
+            {0x30, 0x11, 0x22, 0x3b, 0x14, 0x35, 0x1e, 0x27},
+            {0x30, 0x11, 0x22, 0x3b, 0x14, 0x35, 0x1e, 0x27},
+            {0x30, 0x11, 0x02, 0x3b, 0x14, 0x35, 0x3e, 0x27}
+        },
+        {
+            {0x38, 0x21, 0x32, 0x13, 0x24, 0x1d, 0x16, 0x37},
+            {0x38, 0x21, 0x32, 0x13, 0x24, 0x1d, 0x16, 0x37},
+            {0x38, 0x21, 0x32, 0x13, 0x24, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x32, 0x13, 0x24, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x32, 0x13, 0x24, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x32, 0x13, 0x24, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x32, 0x13, 0x24, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x32, 0x13, 0x04, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x32, 0x13, 0x04, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x32, 0x13, 0x04, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x32, 0x13, 0x04, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x32, 0x13, 0x04, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x22, 0x1b, 0x04, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x22, 0x1b, 0x04, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x22, 0x1b, 0x04, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x22, 0x1b, 0x04, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x22, 0x1b, 0x04, 0x3d, 0x1e, 0x27},
+            {0x38, 0x01, 0x22, 0x1b, 0x04, 0x3d, 0x1e, 0x27},
+            {0x38, 0x01, 0x22, 0x1b, 0x04, 0x3d, 0x1e, 0x27},
+            {0x38, 0x01, 0x22, 0x1b, 0x04, 0x3d, 0x1e, 0x27},
+            {0x30, 0x01, 0x22, 0x1b, 0x04, 0x35, 0x1e, 0x27},
+            {0x30, 0x01, 0x22, 0x1b, 0x04, 0x35, 0x1e, 0x27},
+            {0x30, 0x01, 0x22, 0x1b, 0x04, 0x35, 0x1e, 0x27},
+            {0x30, 0x01, 0x22, 0x1b, 0x04, 0x35, 0x1e, 0x27},
+            {0x30, 0x11, 0x22, 0x1b, 0x14, 0x35, 0x1e, 0x27},
+            {0x30, 0x11, 0x22, 0x1b, 0x14, 0x35, 0x1e, 0x27},
+            {0x30, 0x11, 0x22, 0x1b, 0x14, 0x35, 0x1e, 0x27},
+            {0x30, 0x11, 0x22, 0x1b, 0x14, 0x35, 0x1e, 0x27},
+            {0x30, 0x11, 0x22, 0x1b, 0x14, 0x35, 0x1e, 0x27},
+            {0x30, 0x11, 0x22, 0x1b, 0x14, 0x35, 0x1e, 0x27},
+            {0x30, 0x11, 0x22, 0x3b, 0x14, 0x35, 0x1e, 0x27},
+            {0x30, 0x11, 0x22, 0x3b, 0x14, 0x35, 0x1e, 0x27},
+            {0x30, 0x11, 0x22, 0x3b, 0x14, 0x35, 0x1e, 0x27},
+            {0x30, 0x11, 0x22, 0x3b, 0x14, 0x35, 0x1e, 0x27},
+            {0x30, 0x11, 0x02, 0x3b, 0x14, 0x35, 0x3e, 0x27},
+            {0x30, 0x11, 0x02, 0x3b, 0x14, 0x35, 0x3e, 0x27}
+        },
+        {
+            {0x38, 0x21, 0x32, 0x13, 0x24, 0x1d, 0x16, 0x37},
+            {0x38, 0x21, 0x32, 0x13, 0x24, 0x1d, 0x16, 0x37},
+            {0x38, 0x21, 0x32, 0x13, 0x24, 0x1d, 0x16, 0x37},
+            {0x38, 0x21, 0x32, 0x13, 0x24, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x32, 0x13, 0x24, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x32, 0x13, 0x24, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x32, 0x13, 0x24, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x32, 0x13, 0x24, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x32, 0x13, 0x04, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x32, 0x13, 0x04, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x32, 0x13, 0x04, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x32, 0x13, 0x04, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x22, 0x1b, 0x04, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x22, 0x1b, 0x04, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x22, 0x1b, 0x04, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x22, 0x1b, 0x04, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x22, 0x1b, 0x04, 0x3d, 0x1e, 0x27},
+            {0x38, 0x01, 0x22, 0x1b, 0x04, 0x3d, 0x1e, 0x27},
+            {0x38, 0x01, 0x22, 0x1b, 0x04, 0x3d, 0x1e, 0x27},
+            {0x38, 0x01, 0x22, 0x1b, 0x04, 0x3d, 0x1e, 0x27},
+            {0x30, 0x01, 0x22, 0x1b, 0x04, 0x35, 0x1e, 0x27},
+            {0x30, 0x01, 0x22, 0x1b, 0x04, 0x35, 0x1e, 0x27},
+            {0x30, 0x01, 0x22, 0x1b, 0x04, 0x35, 0x1e, 0x27},
+            {0x30, 0x01, 0x22, 0x1b, 0x04, 0x35, 0x1e, 0x27},
+            {0x30, 0x11, 0x22, 0x1b, 0x14, 0x35, 0x1e, 0x27},
+            {0x30, 0x11, 0x22, 0x1b, 0x14, 0x35, 0x1e, 0x27},
+            {0x30, 0x11, 0x22, 0x1b, 0x14, 0x35, 0x1e, 0x27},
+            {0x30, 0x11, 0x22, 0x1b, 0x14, 0x35, 0x1e, 0x27},
+            {0x30, 0x11, 0x22, 0x1b, 0x14, 0x35, 0x1e, 0x27},
+            {0x30, 0x11, 0x22, 0x3b, 0x14, 0x35, 0x1e, 0x27},
+            {0x30, 0x11, 0x22, 0x3b, 0x14, 0x35, 0x1e, 0x27},
+            {0x30, 0x11, 0x22, 0x3b, 0x14, 0x35, 0x1e, 0x27},
+            {0x30, 0x11, 0x22, 0x3b, 0x14, 0x35, 0x1e, 0x27},
+            {0x30, 0x11, 0x02, 0x3b, 0x14, 0x35, 0x3e, 0x27},
+            {0x30, 0x11, 0x02, 0x3b, 0x14, 0x35, 0x3e, 0x27},
+            {0x30, 0x11, 0x02, 0x3b, 0x14, 0x35, 0x3e, 0x27}
+        },
+        {
+            {0x38, 0x21, 0x32, 0x13, 0x24, 0x1d, 0x16, 0x37},
+            {0x38, 0x21, 0x32, 0x13, 0x24, 0x1d, 0x16, 0x37},
+            {0x38, 0x21, 0x32, 0x13, 0x24, 0x1d, 0x16, 0x37},
+            {0x38, 0x21, 0x32, 0x13, 0x24, 0x1d, 0x16, 0x37},
+            {0x38, 0x21, 0x32, 0x13, 0x24, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x32, 0x13, 0x24, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x32, 0x13, 0x24, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x32, 0x13, 0x24, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x32, 0x13, 0x24, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x32, 0x13, 0x04, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x32, 0x13, 0x04, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x32, 0x13, 0x04, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x22, 0x1b, 0x04, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x22, 0x1b, 0x04, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x22, 0x1b, 0x04, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x22, 0x1b, 0x04, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x22, 0x1b, 0x04, 0x3d, 0x1e, 0x27},
+            {0x38, 0x01, 0x22, 0x1b, 0x04, 0x3d, 0x1e, 0x27},
+            {0x38, 0x01, 0x22, 0x1b, 0x04, 0x3d, 0x1e, 0x27},
+            {0x38, 0x01, 0x22, 0x1b, 0x04, 0x3d, 0x1e, 0x27},
+            {0x30, 0x01, 0x22, 0x1b, 0x04, 0x35, 0x1e, 0x27},
+            {0x30, 0x01, 0x22, 0x1b, 0x04, 0x35, 0x1e, 0x27},
+            {0x30, 0x01, 0x22, 0x1b, 0x04, 0x35, 0x1e, 0x27},
+            {0x30, 0x01, 0x22, 0x1b, 0x04, 0x35, 0x1e, 0x27},
+            {0x30, 0x11, 0x22, 0x1b, 0x14, 0x35, 0x1e, 0x27},
+            {0x30, 0x11, 0x22, 0x1b, 0x14, 0x35, 0x1e, 0x27},
+            {0x30, 0x11, 0x22, 0x1b, 0x14, 0x35, 0x1e, 0x27},
+            {0x30, 0x11, 0x22, 0x1b, 0x14, 0x35, 0x1e, 0x27},
+            {0x30, 0x11, 0x22, 0x3b, 0x14, 0x35, 0x1e, 0x27},
+            {0x30, 0x11, 0x22, 0x3b, 0x14, 0x35, 0x1e, 0x27},
+            {0x30, 0x11, 0x22, 0x3b, 0x14, 0x35, 0x1e, 0x27},
+            {0x30, 0x11, 0x22, 0x3b, 0x14, 0x35, 0x1e, 0x27},
+            {0x30, 0x11, 0x02, 0x3b, 0x14, 0x35, 0x3e, 0x27},
+            {0x30, 0x11, 0x02, 0x3b, 0x14, 0x35, 0x3e, 0x27},
+            {0x30, 0x11, 0x02, 0x3b, 0x14, 0x35, 0x3e, 0x27},
+            {0x30, 0x11, 0x02, 0x3b, 0x14, 0x35, 0x3e, 0x27}
+        },
+        {
+            {0x18, 0x21, 0x32, 0x13, 0x24, 0x1d, 0x16, 0x37},
+            {0x38, 0x21, 0x32, 0x13, 0x24, 0x1d, 0x16, 0x37},
+            {0x38, 0x21, 0x32, 0x13, 0x24, 0x1d, 0x16, 0x37},
+            {0x38, 0x21, 0x32, 0x13, 0x24, 0x1d, 0x16, 0x37},
+            {0x38, 0x21, 0x32, 0x13, 0x24, 0x1d, 0x16, 0x37},
+            {0x38, 0x21, 0x32, 0x13, 0x24, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x32, 0x13, 0x24, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x32, 0x13, 0x24, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x32, 0x13, 0x24, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x32, 0x13, 0x24, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x32, 0x13, 0x04, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x32, 0x0b, 0x04, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x22, 0x0b, 0x04, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x22, 0x1b, 0x04, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x22, 0x1b, 0x04, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x22, 0x1b, 0x04, 0x3d, 0x0e, 0x37},
+            {0x38, 0x01, 0x22, 0x1b, 0x04, 0x3d, 0x0e, 0x27},
+            {0x38, 0x01, 0x22, 0x1b, 0x04, 0x3d, 0x1e, 0x27},
+            {0x38, 0x01, 0x22, 0x1b, 0x04, 0x3d, 0x1e, 0x27},
+            {0x38, 0x01, 0x22, 0x1b, 0x04, 0x2d, 0x1e, 0x27},
+            {0x30, 0x01, 0x22, 0x1b, 0x04, 0x2d, 0x1e, 0x27},
+            {0x30, 0x01, 0x22, 0x1b, 0x04, 0x35, 0x1e, 0x27},
+            {0x30, 0x01, 0x22, 0x1b, 0x04, 0x35, 0x1e, 0x27},
+            {0x30, 0x01, 0x22, 0x1b, 0x0c, 0x35, 0x1e, 0x27},
+            {0x30, 0x11, 0x22, 0x1b, 0x0c, 0x35, 0x1e, 0x27},
+            {0x30, 0x11, 0x22, 0x1b, 0x14, 0x35, 0x1e, 0x27},
+            {0x30, 0x11, 0x22, 0x1b, 0x14, 0x35, 0x1e, 0x27},
+            {0x30, 0x11, 0x22, 0x3b, 0x14, 0x35, 0x1e, 0x27},
+            {0x30, 0x11, 0x22, 0x3b, 0x14, 0x35, 0x1e, 0x27},
+            {0x30, 0x11, 0x22, 0x3b, 0x14, 0x35, 0x1e, 0x27},
+            {0x30, 0x11, 0x22, 0x3b, 0x14, 0x35, 0x1e, 0x27},
+            {0x30, 0x11, 0x02, 0x3b, 0x14, 0x35, 0x3e, 0x27},
+            {0x30, 0x11, 0x02, 0x3b, 0x14, 0x35, 0x3e, 0x27},
+            {0x30, 0x11, 0x02, 0x3b, 0x14, 0x35, 0x3e, 0x27},
+            {0x30, 0x11, 0x02, 0x3b, 0x14, 0x35, 0x3e, 0x27},
+            {0x30, 0x11, 0x02, 0x3b, 0x14, 0x35, 0x3e, 0x07}
+        },
+        {
+            {0x18, 0x21, 0x32, 0x13, 0x24, 0x1d, 0x16, 0x37},
+            {0x18, 0x21, 0x32, 0x13, 0x24, 0x1d, 0x16, 0x37},
+            {0x38, 0x21, 0x32, 0x13, 0x24, 0x1d, 0x16, 0x37},
+            {0x38, 0x21, 0x32, 0x13, 0x24, 0x1d, 0x16, 0x37},
+            {0x38, 0x21, 0x32, 0x13, 0x24, 0x1d, 0x16, 0x37},
+            {0x38, 0x21, 0x32, 0x13, 0x24, 0x1d, 0x16, 0x37},
+            {0x38, 0x21, 0x32, 0x13, 0x24, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x32, 0x13, 0x24, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x32, 0x13, 0x24, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x32, 0x13, 0x24, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x32, 0x0b, 0x24, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x32, 0x0b, 0x04, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x22, 0x0b, 0x04, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x22, 0x0b, 0x04, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x22, 0x1b, 0x04, 0x3d, 0x0e, 0x37},
+            {0x38, 0x01, 0x22, 0x1b, 0x04, 0x3d, 0x0e, 0x37},
+            {0x38, 0x01, 0x22, 0x1b, 0x04, 0x3d, 0x0e, 0x27},
+            {0x38, 0x01, 0x22, 0x1b, 0x04, 0x3d, 0x0e, 0x27},
+            {0x38, 0x01, 0x22, 0x1b, 0x04, 0x2d, 0x1e, 0x27},
+            {0x38, 0x01, 0x22, 0x1b, 0x04, 0x2d, 0x1e, 0x27},
+            {0x30, 0x01, 0x22, 0x1b, 0x04, 0x2d, 0x1e, 0x27},
+            {0x30, 0x01, 0x22, 0x1b, 0x04, 0x2d, 0x1e, 0x27},
+            {0x30, 0x01, 0x22, 0x1b, 0x0c, 0x35, 0x1e, 0x27},
+            {0x30, 0x01, 0x22, 0x1b, 0x0c, 0x35, 0x1e, 0x27},
+            {0x30, 0x11, 0x22, 0x1b, 0x0c, 0x35, 0x1e, 0x27},
+            {0x30, 0x11, 0x22, 0x1b, 0x0c, 0x35, 0x1e, 0x27},
+            {0x30, 0x11, 0x22, 0x3b, 0x14, 0x35, 0x1e, 0x27},
+            {0x30, 0x11, 0x22, 0x3b, 0x14, 0x35, 0x1e, 0x27},
+            {0x30, 0x11, 0x22, 0x3b, 0x14, 0x35, 0x1e, 0x27},
+            {0x30, 0x11, 0x22, 0x3b, 0x14, 0x35, 0x1e, 0x27},
+            {0x30, 0x11, 0x02, 0x3b, 0x14, 0x35, 0x3e, 0x27},
+            {0x30, 0x11, 0x02, 0x3b, 0x14, 0x35, 0x3e, 0x27},
+            {0x30, 0x11, 0x02, 0x3b, 0x14, 0x35, 0x3e, 0x27},
+            {0x30, 0x11, 0x02, 0x3b, 0x14, 0x35, 0x3e, 0x27},
+            {0x30, 0x11, 0x02, 0x3b, 0x14, 0x35, 0x3e, 0x07},
+            {0x30, 0x11, 0x02, 0x3b, 0x14, 0x35, 0x3e, 0x07}
+        },
+        {
+            {0x18, 0x21, 0x32, 0x13, 0x24, 0x1d, 0x16, 0x37},
+            {0x18, 0x21, 0x32, 0x13, 0x24, 0x1d, 0x16, 0x37},
+            {0x18, 0x21, 0x32, 0x13, 0x24, 0x1d, 0x16, 0x37},
+            {0x38, 0x21, 0x32, 0x13, 0x24, 0x1d, 0x16, 0x37},
+            {0x38, 0x21, 0x32, 0x13, 0x24, 0x1d, 0x16, 0x37},
+            {0x38, 0x21, 0x32, 0x13, 0x24, 0x1d, 0x16, 0x37},
+            {0x38, 0x21, 0x32, 0x13, 0x24, 0x1d, 0x16, 0x37},
+            {0x38, 0x21, 0x32, 0x13, 0x24, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x32, 0x13, 0x24, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x32, 0x0b, 0x24, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x32, 0x0b, 0x24, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x32, 0x0b, 0x24, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x22, 0x0b, 0x04, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x22, 0x0b, 0x04, 0x3d, 0x0e, 0x37},
+            {0x38, 0x01, 0x22, 0x0b, 0x04, 0x3d, 0x0e, 0x37},
+            {0x38, 0x01, 0x22, 0x1b, 0x04, 0x3d, 0x0e, 0x37},
+            {0x38, 0x01, 0x22, 0x1b, 0x04, 0x3d, 0x0e, 0x27},
+            {0x38, 0x01, 0x22, 0x1b, 0x04, 0x2d, 0x0e, 0x27},
+            {0x38, 0x01, 0x22, 0x1b, 0x04, 0x2d, 0x0e, 0x27},
+            {0x38, 0x01, 0x22, 0x1b, 0x04, 0x2d, 0x1e, 0x27},
+            {0x30, 0x01, 0x22, 0x1b, 0x04, 0x2d, 0x1e, 0x27},
+            {0x30, 0x01, 0x22, 0x1b, 0x0c, 0x2d, 0x1e, 0x27},
+            {0x30, 0x01, 0x22, 0x1b, 0x0c, 0x2d, 0x1e, 0x27},
+            {0x30, 0x01, 0x22, 0x1b, 0x0c, 0x35, 0x1e, 0x27},
+            {0x30, 0x11, 0x22, 0x1b, 0x0c, 0x35, 0x1e, 0x27},
+            {0x30, 0x11, 0x22, 0x3b, 0x0c, 0x35, 0x1e, 0x27},
+            {0x30, 0x11, 0x22, 0x3b, 0x0c, 0x35, 0x1e, 0x27},
+            {0x30, 0x11, 0x22, 0x3b, 0x14, 0x35, 0x1e, 0x27},
+            {0x30, 0x11, 0x22, 0x3b, 0x14, 0x35, 0x1e, 0x27},
+            {0x30, 0x11, 0x02, 0x3b, 0x14, 0x35, 0x3e, 0x27},
+            {0x30, 0x11, 0x02, 0x3b, 0x14, 0x35, 0x3e, 0x27},
+            {0x30, 0x11, 0x02, 0x3b, 0x14, 0x35, 0x3e, 0x27},
+            {0x30, 0x11, 0x02, 0x3b, 0x14, 0x35, 0x3e, 0x27},
+            {0x30, 0x11, 0x02, 0x3b, 0x14, 0x35, 0x3e, 0x07},
+            {0x30, 0x11, 0x02, 0x3b, 0x14, 0x35, 0x3e, 0x07},
+            {0x30, 0x11, 0x02, 0x3b, 0x14, 0x35, 0x3e, 0x07}
+        },
+        {
+            {0x18, 0x21, 0x32, 0x13, 0x24, 0x1d, 0x16, 0x37},
+            {0x18, 0x21, 0x32, 0x13, 0x24, 0x1d, 0x16, 0x37},
+            {0x18, 0x21, 0x32, 0x13, 0x24, 0x1d, 0x16, 0x37},
+            {0x18, 0x21, 0x32, 0x13, 0x24, 0x1d, 0x16, 0x37},
+            {0x38, 0x21, 0x32, 0x13, 0x24, 0x1d, 0x16, 0x37},
+            {0x38, 0x21, 0x32, 0x13, 0x24, 0x1d, 0x16, 0x37},
+            {0x38, 0x21, 0x32, 0x13, 0x24, 0x1d, 0x16, 0x37},
+            {0x38, 0x21, 0x32, 0x13, 0x24, 0x1d, 0x16, 0x37},
+            {0x38, 0x21, 0x32, 0x0b, 0x24, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x32, 0x0b, 0x24, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x32, 0x0b, 0x24, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x32, 0x0b, 0x24, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x2a, 0x0b, 0x24, 0x3d, 0x0e, 0x37},
+            {0x38, 0x01, 0x22, 0x0b, 0x04, 0x3d, 0x0e, 0x37},
+            {0x38, 0x01, 0x22, 0x0b, 0x04, 0x3d, 0x0e, 0x37},
+            {0x38, 0x01, 0x22, 0x0b, 0x04, 0x3d, 0x0e, 0x37},
+            {0x38, 0x01, 0x22, 0x1b, 0x04, 0x2d, 0x0e, 0x27},
+            {0x38, 0x01, 0x22, 0x1b, 0x04, 0x2d, 0x0e, 0x27},
+            {0x38, 0x01, 0x22, 0x1b, 0x04, 0x2d, 0x0e, 0x27},
+            {0x38, 0x01, 0x22, 0x1b, 0x04, 0x2d, 0x0e, 0x27},
+            {0x30, 0x01, 0x22, 0x1b, 0x0c, 0x2d, 0x1e, 0x27},
+            {0x30, 0x01, 0x22, 0x1b, 0x0c, 0x2d, 0x1e, 0x27},
+            {0x30, 0x01, 0x22, 0x1b, 0x0c, 0x2d, 0x1e, 0x27},
+            {0x30, 0x01, 0x22, 0x1b, 0x0c, 0x2d, 0x1e, 0x27},
+            {0x30, 0x11, 0x22, 0x3b, 0x0c, 0x35, 0x1e, 0x27},
+            {0x30, 0x11, 0x22, 0x3b, 0x0c, 0x35, 0x1e, 0x27},
+            {0x30, 0x11, 0x22, 0x3b, 0x0c, 0x35, 0x1e, 0x27},
+            {0x30, 0x11, 0x22, 0x3b, 0x0c, 0x35, 0x1e, 0x27},
+            {0x30, 0x11, 0x02, 0x3b, 0x14, 0x35, 0x3e, 0x27},
+            {0x30, 0x11, 0x02, 0x3b, 0x14, 0x35, 0x3e, 0x27},
+            {0x30, 0x11, 0x02, 0x3b, 0x14, 0x35, 0x3e, 0x27},
+            {0x30, 0x11, 0x02, 0x3b, 0x14, 0x35, 0x3e, 0x27},
+            {0x30, 0x11, 0x02, 0x3b, 0x14, 0x35, 0x3e, 0x07},
+            {0x30, 0x11, 0x02, 0x3b, 0x14, 0x35, 0x3e, 0x07},
+            {0x30, 0x11, 0x02, 0x3b, 0x14, 0x35, 0x3e, 0x07},
+            {0x30, 0x11, 0x02, 0x3b, 0x14, 0x35, 0x3e, 0x07}
+        },
+        {
+            {0x18, 0x21, 0x32, 0x13, 0x24, 0x1d, 0x16, 0x37},
+            {0x18, 0x21, 0x32, 0x13, 0x24, 0x1d, 0x16, 0x37},
+            {0x18, 0x21, 0x32, 0x13, 0x24, 0x1d, 0x16, 0x37},
+            {0x18, 0x21, 0x32, 0x13, 0x24, 0x1d, 0x16, 0x37},
+            {0x18, 0x21, 0x32, 0x13, 0x24, 0x1d, 0x16, 0x37},
+            {0x38, 0x21, 0x32, 0x13, 0x24, 0x1d, 0x16, 0x37},
+            {0x38, 0x21, 0x32, 0x13, 0x24, 0x1d, 0x16, 0x37},
+            {0x38, 0x21, 0x32, 0x0b, 0x24, 0x1d, 0x16, 0x37},
+            {0x38, 0x21, 0x32, 0x0b, 0x24, 0x1d, 0x16, 0x37},
+            {0x38, 0x21, 0x32, 0x0b, 0x24, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x32, 0x0b, 0x24, 0x3d, 0x16, 0x37},
+            {0x38, 0x01, 0x2a, 0x0b, 0x24, 0x3d, 0x0e, 0x37},
+            {0x38, 0x01, 0x2a, 0x0b, 0x24, 0x3d, 0x0e, 0x37},
+            {0x38, 0x01, 0x2a, 0x0b, 0x24, 0x3d, 0x0e, 0x37},
+            {0x38, 0x01, 0x22, 0x0b, 0x04, 0x3d, 0x0e, 0x37},
+            {0x38, 0x01, 0x22, 0x0b, 0x04, 0x2d, 0x0e, 0x2f},
+            {0x38, 0x01, 0x22, 0x0b, 0x04, 0x2d, 0x0e, 0x2f},
+            {0x38, 0x01, 0x22, 0x1b, 0x04, 0x2d, 0x0e, 0x27},
+            {0x38, 0x01, 0x22, 0x1b, 0x04, 0x2d, 0x0e, 0x27},
+            {0x28, 0x01, 0x22, 0x1b, 0x0c, 0x2d, 0x0e, 0x27},
+            {0x28, 0x01, 0x22, 0x1b, 0x0c, 0x2d, 0x0e, 0x27},
+            {0x30, 0x01, 0x22, 0x1b, 0x0c, 0x2d, 0x1e, 0x27},
+            {0x30, 0x01, 0x22, 0x1b, 0x0c, 0x2d, 0x1e, 0x27},
+            {0x30, 0x09, 0x22, 0x3b, 0x0c, 0x2d, 0x1e, 0x27},
+            {0x30, 0x09, 0x22, 0x3b, 0x0c, 0x2d, 0x1e, 0x27},
+            {0x30, 0x11, 0x22, 0x3b, 0x0c, 0x35, 0x1e, 0x27},
+            {0x30, 0x11, 0x22, 0x3b, 0x0c, 0x35, 0x1e, 0x27},
+            {0x30, 0x11, 0x02, 0x3b, 0x0c, 0x35, 0x3e, 0x27},
+            {0x30, 0x11, 0x02, 0x3b, 0x0c, 0x35, 0x3e, 0x27},
+            {0x30, 0x11, 0x02, 0x3b, 0x14, 0x35, 0x3e, 0x27},
+            {0x30, 0x11, 0x02, 0x3b, 0x14, 0x35, 0x3e, 0x27},
+            {0x30, 0x11, 0x02, 0x3b, 0x14, 0x35, 0x3e, 0x07},
+            {0x30, 0x11, 0x02, 0x3b, 0x14, 0x35, 0x3e, 0x07},
+            {0x30, 0x11, 0x02, 0x3b, 0x14, 0x35, 0x3e, 0x07},
+            {0x30, 0x11, 0x02, 0x3b, 0x14, 0x35, 0x3e, 0x07},
+            {0x30, 0x11, 0x02, 0x3b, 0x14, 0x35, 0x3e, 0x07}
+        },
+        {
+            {0x18, 0x21, 0x32, 0x13, 0x24, 0x1d, 0x16, 0x37},
+            {0x18, 0x21, 0x32, 0x13, 0x24, 0x1d, 0x16, 0x37},
+            {0x18, 0x21, 0x32, 0x13, 0x24, 0x1d, 0x16, 0x37},
+            {0x18, 0x21, 0x32, 0x13, 0x24, 0x1d, 0x16, 0x37},
+            {0x18, 0x21, 0x32, 0x13, 0x24, 0x1d, 0x16, 0x37},
+            {0x18, 0x21, 0x32, 0x13, 0x24, 0x1d, 0x16, 0x37},
+            {0x38, 0x21, 0x32, 0x0b, 0x24, 0x1d, 0x16, 0x37},
+            {0x38, 0x21, 0x32, 0x0b, 0x24, 0x1d, 0x16, 0x37},
+            {0x38, 0x21, 0x32, 0x0b, 0x24, 0x1d, 0x16, 0x37},
+            {0x38, 0x21, 0x32, 0x0b, 0x24, 0x1d, 0x16, 0x37},
+            {0x38, 0x21, 0x2a, 0x0b, 0x24, 0x3d, 0x0e, 0x37},
+            {0x38, 0x01, 0x2a, 0x0b, 0x24, 0x3d, 0x0e, 0x37},
+            {0x38, 0x01, 0x2a, 0x0b, 0x24, 0x3d, 0x0e, 0x37},
+            {0x38, 0x01, 0x2a, 0x0b, 0x24, 0x3d, 0x0e, 0x37},
+            {0x38, 0x01, 0x22, 0x0b, 0x04, 0x2d, 0x0e, 0x2f},
+            {0x38, 0x01, 0x22, 0x0b, 0x04, 0x2d, 0x0e, 0x2f},
+            {0x38, 0x01, 0x22, 0x0b, 0x04, 0x2d, 0x0e, 0x2f},
+            {0x38, 0x01, 0x22, 0x0b, 0x04, 0x2d, 0x0e, 0x2f},
+            {0x28, 0x01, 0x22, 0x1b, 0x0c, 0x2d, 0x0e, 0x27},
+            {0x28, 0x01, 0x22, 0x1b, 0x0c, 0x2d, 0x0e, 0x27},
+            {0x28, 0x01, 0x22, 0x1b, 0x0c, 0x2d, 0x0e, 0x27},
+            {0x28, 0x01, 0x22, 0x1b, 0x0c, 0x2d, 0x0e, 0x27},
+            {0x30, 0x09, 0x22, 0x3b, 0x0c, 0x2d, 0x1e, 0x27},
+            {0x30, 0x09, 0x22, 0x3b, 0x0c, 0x2d, 0x1e, 0x27},
+            {0x30, 0x09, 0x22, 0x3b, 0x0c, 0x2d, 0x1e, 0x27},
+            {0x30, 0x09, 0x22, 0x3b, 0x0c, 0x2d, 0x1e, 0x27},
+            {0x30, 0x11, 0x02, 0x3b, 0x0c, 0x35, 0x3e, 0x27},
+            {0x30, 0x11, 0x02, 0x3b, 0x0c, 0x35, 0x3e, 0x27},
+            {0x30, 0x11, 0x02, 0x3b, 0x0c, 0x35, 0x3e, 0x27},
+            {0x30, 0x11, 0x02, 0x3b, 0x0c, 0x35, 0x3e, 0x27},
+            {0x30, 0x11, 0x02, 0x3b, 0x14, 0x35, 0x3e, 0x07},
+            {0x30, 0x11, 0x02, 0x3b, 0x14, 0x35, 0x3e, 0x07},
+            {0x30, 0x11, 0x02, 0x3b, 0x14, 0x35, 0x3e, 0x07},
+            {0x30, 0x11, 0x02, 0x3b, 0x14, 0x35, 0x3e, 0x07},
+            {0x30, 0x11, 0x02, 0x3b, 0x14, 0x35, 0x3e, 0x07},
+            {0x30, 0x11, 0x02, 0x3b, 0x14, 0x35, 0x3e, 0x07}
+        },
+        {
+            {0x18, 0x21, 0x32, 0x13, 0x24, 0x1d, 0x16, 0x37},
+            {0x18, 0x21, 0x32, 0x13, 0x24, 0x1d, 0x16, 0x37},
+            {0x18, 0x21, 0x32, 0x13, 0x24, 0x1d, 0x16, 0x37},
+            {0x18, 0x21, 0x32, 0x13, 0x24, 0x1d, 0x16, 0x37},
+            {0x18, 0x21, 0x32, 0x13, 0x24, 0x1d, 0x16, 0x37},
+            {0x18, 0x21, 0x32, 0x0b, 0x24, 0x1d, 0x16, 0x37},
+            {0x18, 0x21, 0x32, 0x0b, 0x24, 0x1d, 0x16, 0x37},
+            {0x38, 0x21, 0x32, 0x0b, 0x24, 0x1d, 0x16, 0x37},
+            {0x38, 0x21, 0x32, 0x0b, 0x24, 0x1d, 0x16, 0x37},
+            {0x38, 0x21, 0x2a, 0x0b, 0x24, 0x1d, 0x0e, 0x37},
+            {0x38, 0x21, 0x2a, 0x0b, 0x24, 0x1d, 0x0e, 0x37},
+            {0x38, 0x21, 0x2a, 0x0b, 0x24, 0x3d, 0x0e, 0x37},
+            {0x38, 0x01, 0x2a, 0x0b, 0x24, 0x3d, 0x0e, 0x37},
+            {0x38, 0x01, 0x2a, 0x0b, 0x24, 0x2d, 0x0e, 0x2f},
+            {0x38, 0x01, 0x2a, 0x0b, 0x24, 0x2d, 0x0e, 0x2f},
+            {0x38, 0x01, 0x22, 0x0b, 0x04, 0x2d, 0x0e, 0x2f},
+            {0x38, 0x01, 0x22, 0x0b, 0x04, 0x2d, 0x0e, 0x2f},
+            {0x28, 0x01, 0x22, 0x0b, 0x0c, 0x2d, 0x0e, 0x2f},
+            {0x28, 0x01, 0x22, 0x0b, 0x0c, 0x2d, 0x0e, 0x2f},
+            {0x28, 0x01, 0x22, 0x1b, 0x0c, 0x2d, 0x0e, 0x27},
+            {0x28, 0x01, 0x22, 0x1b, 0x0c, 0x2d, 0x0e, 0x27},
+            {0x28, 0x09, 0x22, 0x3b, 0x0c, 0x2d, 0x0e, 0x27},
+            {0x28, 0x09, 0x22, 0x3b, 0x0c, 0x2d, 0x0e, 0x27},
+            {0x30, 0x09, 0x22, 0x3b, 0x0c, 0x2d, 0x1e, 0x27},
+            {0x30, 0x09, 0x22, 0x3b, 0x0c, 0x2d, 0x1e, 0x27},
+            {0x30, 0x09, 0x02, 0x3b, 0x0c, 0x2d, 0x3e, 0x27},
+            {0x30, 0x09, 0x02, 0x3b, 0x0c, 0x2d, 0x3e, 0x27},
+            {0x30, 0x11, 0x02, 0x3b, 0x0c, 0x35, 0x3e, 0x27},
+            {0x30, 0x11, 0x02, 0x3b, 0x0c, 0x35, 0x3e, 0x27},
+            {0x30, 0x11, 0x02, 0x3b, 0x0c, 0x35, 0x3e, 0x07},
+            {0x30, 0x11, 0x02, 0x3b, 0x0c, 0x35, 0x3e, 0x07},
+            {0x30, 0x11, 0x02, 0x3b, 0x14, 0x35, 0x3e, 0x07},
+            {0x30, 0x11, 0x02, 0x3b, 0x14, 0x35, 0x3e, 0x07},
+            {0x30, 0x11, 0x02, 0x3b, 0x14, 0x35, 0x3e, 0x07},
+            {0x30, 0x11, 0x02, 0x3b, 0x14, 0x35, 0x3e, 0x07},
+            {0x30, 0x11, 0x02, 0x3b, 0x14, 0x35, 0x3e, 0x07}
+        },
+        {
+            {0x18, 0x21, 0x32, 0x03, 0x24, 0x1d, 0x06, 0x37},
+            {0x18, 0x21, 0x32, 0x03, 0x24, 0x1d, 0x06, 0x37},
+            {0x18, 0x21, 0x32, 0x03, 0x24, 0x1d, 0x06, 0x37},
+            {0x18, 0x21, 0x32, 0x03, 0x24, 0x1d, 0x06, 0x37},
+            {0x18, 0x21, 0x32, 0x0b, 0x24, 0x1d, 0x06, 0x37},
+            {0x18, 0x21, 0x32, 0x0b, 0x24, 0x1d, 0x06, 0x37},
+            {0x18, 0x21, 0x32, 0x0b, 0x24, 0x1d, 0x06, 0x37},
+            {0x18, 0x21, 0x32, 0x0b, 0x24, 0x1d, 0x06, 0x37},
+            {0x38, 0x21, 0x2a, 0x0b, 0x24, 0x1d, 0x0e, 0x37},
+            {0x38, 0x21, 0x2a, 0x0b, 0x24, 0x1d, 0x0e, 0x37},
+            {0x38, 0x21, 0x2a, 0x0b, 0x24, 0x1d, 0x0e, 0x37},
+            {0x38, 0x21, 0x2a, 0x0b, 0x24, 0x1d, 0x0e, 0x37},
+            {0x38, 0x21, 0x2a, 0x0b, 0x24, 0x2d, 0x0e, 0x2f},
+            {0x38, 0x01, 0x2a, 0x0b, 0x24, 0x2d, 0x0e, 0x2f},
+            {0x38, 0x01, 0x2a, 0x0b, 0x24, 0x2d, 0x0e, 0x2f},
+            {0x38, 0x01, 0x2a, 0x0b, 0x24, 0x2d, 0x0e, 0x2f},
+            {0x28, 0x01, 0x22, 0x0b, 0x0c, 0x2d, 0x0e, 0x2f},
+            {0x28, 0x01, 0x22, 0x0b, 0x0c, 0x2d, 0x0e, 0x2f},
+            {0x28, 0x01, 0x22, 0x0b, 0x0c, 0x2d, 0x0e, 0x2f},
+            {0x28, 0x01, 0x22, 0x0b, 0x0c, 0x2d, 0x0e, 0x2f},
+            {0x28, 0x09, 0x22, 0x3b, 0x0c, 0x2d, 0x0e, 0x27},
+            {0x28, 0x09, 0x22, 0x3b, 0x0c, 0x2d, 0x0e, 0x27},
+            {0x28, 0x09, 0x22, 0x3b, 0x0c, 0x2d, 0x0e, 0x27},
+            {0x28, 0x09, 0x22, 0x3b, 0x0c, 0x2d, 0x0e, 0x27},
+            {0x30, 0x09, 0x02, 0x3b, 0x0c, 0x2d, 0x3e, 0x27},
+            {0x30, 0x09, 0x02, 0x3b, 0x0c, 0x2d, 0x3e, 0x27},
+            {0x30, 0x09, 0x02, 0x3b, 0x0c, 0x2d, 0x3e, 0x27},
+            {0x30, 0x09, 0x02, 0x3b, 0x0c, 0x2d, 0x3e, 0x27},
+            {0x30, 0x11, 0x02, 0x3b, 0x0c, 0x25, 0x3e, 0x07},
+            {0x30, 0x11, 0x02, 0x3b, 0x0c, 0x25, 0x3e, 0x07},
+            {0x30, 0x11, 0x02, 0x3b, 0x0c, 0x25, 0x3e, 0x07},
+            {0x30, 0x11, 0x02, 0x3b, 0x0c, 0x25, 0x3e, 0x07},
+            {0x30, 0x11, 0x02, 0x3b, 0x1c, 0x25, 0x3e, 0x07},
+            {0x30, 0x11, 0x02, 0x3b, 0x1c, 0x25, 0x3e, 0x07},
+            {0x30, 0x11, 0x02, 0x3b, 0x1c, 0x25, 0x3e, 0x07},
+            {0x30, 0x11, 0x02, 0x3b, 0x1c, 0x25, 0x3e, 0x07}
+        },
+        {
+            {0x18, 0x21, 0x32, 0x03, 0x24, 0x1d, 0x06, 0x37},
+            {0x18, 0x21, 0x32, 0x03, 0x24, 0x1d, 0x06, 0x37},
+            {0x18, 0x21, 0x32, 0x03, 0x24, 0x1d, 0x06, 0x37},
+            {0x18, 0x21, 0x32, 0x03, 0x24, 0x1d, 0x06, 0x37},
+            {0x18, 0x21, 0x32, 0x03, 0x24, 0x1d, 0x06, 0x37},
+            {0x18, 0x21, 0x32, 0x0b, 0x24, 0x1d, 0x06, 0x37},
+            {0x18, 0x21, 0x32, 0x0b, 0x24, 0x1d, 0x06, 0x37},
+            {0x18, 0x21, 0x2a, 0x0b, 0x24, 0x1d, 0x06, 0x37},
+            {0x18, 0x21, 0x2a, 0x0b, 0x24, 0x1d, 0x06, 0x37},
+            {0x38, 0x21, 0x2a, 0x0b, 0x24, 0x1d, 0x0e, 0x37},
+            {0x38, 0x21, 0x2a, 0x0b, 0x24, 0x1d, 0x0e, 0x37},
+            {0x38, 0x21, 0x2a, 0x0b, 0x24, 0x0d, 0x0e, 0x2f},
+            {0x38, 0x21, 0x2a, 0x0b, 0x24, 0x0d, 0x0e, 0x2f},
+            {0x38, 0x21, 0x2a, 0x0b, 0x24, 0x2d, 0x0e, 0x2f},
+            {0x38, 0x01, 0x2a, 0x0b, 0x24, 0x2d, 0x0e, 0x2f},
+            {0x28, 0x01, 0x2a, 0x0b, 0x2c, 0x2d, 0x0e, 0x2f},
+            {0x28, 0x01, 0x2a, 0x0b, 0x2c, 0x2d, 0x0e, 0x2f},
+            {0x28, 0x01, 0x22, 0x0b, 0x0c, 0x2d, 0x0e, 0x2f},
+            {0x28, 0x01, 0x22, 0x0b, 0x0c, 0x2d, 0x0e, 0x2f},
+            {0x28, 0x09, 0x22, 0x2b, 0x0c, 0x2d, 0x0e, 0x2f},
+            {0x28, 0x09, 0x22, 0x2b, 0x0c, 0x2d, 0x0e, 0x2f},
+            {0x28, 0x09, 0x22, 0x3b, 0x0c, 0x2d, 0x0e, 0x27},
+            {0x28, 0x09, 0x22, 0x3b, 0x0c, 0x2d, 0x0e, 0x27},
+            {0x28, 0x09, 0x02, 0x3b, 0x0c, 0x2d, 0x2e, 0x27},
+            {0x28, 0x09, 0x02, 0x3b, 0x0c, 0x2d, 0x2e, 0x27},
+            {0x30, 0x09, 0x02, 0x3b, 0x0c, 0x2d, 0x3e, 0x27},
+            {0x30, 0x09, 0x02, 0x3b, 0x0c, 0x2d, 0x3e, 0x27},
+            {0x30, 0x09, 0x02, 0x3b, 0x0c, 0x25, 0x3e, 0x07},
+            {0x30, 0x09, 0x02, 0x3b, 0x0c, 0x25, 0x3e, 0x07},
+            {0x30, 0x11, 0x02, 0x3b, 0x0c, 0x25, 0x3e, 0x07},
+            {0x30, 0x11, 0x02, 0x3b, 0x0c, 0x25, 0x3e, 0x07},
+            {0x30, 0x11, 0x02, 0x3b, 0x1c, 0x25, 0x3e, 0x07},
+            {0x30, 0x11, 0x02, 0x3b, 0x1c, 0x25, 0x3e, 0x07},
+            {0x30, 0x11, 0x02, 0x3b, 0x1c, 0x25, 0x3e, 0x07},
+            {0x30, 0x11, 0x02, 0x3b, 0x1c, 0x25, 0x3e, 0x07},
+            {0x30, 0x11, 0x02, 0x3b, 0x1c, 0x25, 0x3e, 0x07}
+        },
+        {
+            {0x18, 0x21, 0x32, 0x03, 0x24, 0x1d, 0x06, 0x37},
+            {0x18, 0x21, 0x32, 0x03, 0x24, 0x1d, 0x06, 0x37},
+            {0x18, 0x21, 0x32, 0x03, 0x24, 0x1d, 0x06, 0x37},
+            {0x18, 0x21, 0x32, 0x03, 0x24, 0x1d, 0x06, 0x37},
+            {0x18, 0x21, 0x32, 0x03, 0x24, 0x1d, 0x06, 0x37},
+            {0x18, 0x21, 0x32, 0x03, 0x24, 0x1d, 0x06, 0x37},
+            {0x18, 0x21, 0x2a, 0x03, 0x24, 0x1d, 0x06, 0x37},
+            {0x18, 0x21, 0x2a, 0x0b, 0x24, 0x1d, 0x06, 0x37},
+            {0x18, 0x21, 0x2a, 0x0b, 0x24, 0x1d, 0x06, 0x37},
+            {0x18, 0x21, 0x2a, 0x0b, 0x24, 0x1d, 0x06, 0x37},
+            {0x18, 0x21, 0x2a, 0x0b, 0x24, 0x0d, 0x06, 0x2f},
+            {0x38, 0x21, 0x2a, 0x0b, 0x24, 0x0d, 0x0e, 0x2f},
+            {0x38, 0x21, 0x2a, 0x0b, 0x24, 0x0d, 0x0e, 0x2f},
+            {0x38, 0x21, 0x2a, 0x0b, 0x24, 0x0d, 0x0e, 0x2f},
+            {0x28, 0x21, 0x2a, 0x0b, 0x2c, 0x2d, 0x0e, 0x2f},
+            {0x28, 0x01, 0x2a, 0x0b, 0x2c, 0x2d, 0x0e, 0x2f},
+            {0x28, 0x01, 0x2a, 0x0b, 0x2c, 0x2d, 0x0e, 0x2f},
+            {0x28, 0x01, 0x2a, 0x0b, 0x2c, 0x2d, 0x0e, 0x2f},
+            {0x28, 0x09, 0x2a, 0x2b, 0x2c, 0x2d, 0x0e, 0x2f},
+            {0x28, 0x09, 0x22, 0x2b, 0x0c, 0x2d, 0x0e, 0x2f},
+            {0x28, 0x09, 0x22, 0x2b, 0x0c, 0x2d, 0x0e, 0x2f},
+            {0x28, 0x09, 0x22, 0x2b, 0x0c, 0x2d, 0x0e, 0x2f},
+            {0x28, 0x09, 0x02, 0x2b, 0x0c, 0x2d, 0x2e, 0x2f},
+            {0x28, 0x09, 0x02, 0x3b, 0x0c, 0x2d, 0x2e, 0x27},
+            {0x28, 0x09, 0x02, 0x3b, 0x0c, 0x2d, 0x2e, 0x27},
+            {0x28, 0x09, 0x02, 0x3b, 0x0c, 0x2d, 0x2e, 0x27},
+            {0x28, 0x09, 0x02, 0x3b, 0x0c, 0x25, 0x2e, 0x07},
+            {0x30, 0x09, 0x02, 0x3b, 0x0c, 0x25, 0x3e, 0x07},
+            {0x30, 0x09, 0x02, 0x3b, 0x0c, 0x25, 0x3e, 0x07},
+            {0x30, 0x09, 0x02, 0x3b, 0x0c, 0x25, 0x3e, 0x07},
+            {0x30, 0x09, 0x02, 0x3b, 0x1c, 0x25, 0x3e, 0x07},
+            {0x30, 0x11, 0x02, 0x3b, 0x1c, 0x25, 0x3e, 0x07},
+            {0x30, 0x11, 0x02, 0x3b, 0x1c, 0x25, 0x3e, 0x07},
+            {0x30, 0x11, 0x02, 0x3b, 0x1c, 0x25, 0x3e, 0x07},
+            {0x30, 0x11, 0x02, 0x3b, 0x1c, 0x25, 0x3e, 0x07},
+            {0x30, 0x11, 0x02, 0x3b, 0x1c, 0x25, 0x3e, 0x07}
+        },
+        {
+            {0x18, 0x21, 0x32, 0x03, 0x24, 0x1d, 0x06, 0x37},
+            {0x18, 0x21, 0x32, 0x03, 0x24, 0x1d, 0x06, 0x37},
+            {0x18, 0x21, 0x32, 0x03, 0x24, 0x1d, 0x06, 0x37},
+            {0x18, 0x21, 0x32, 0x03, 0x24, 0x1d, 0x06, 0x37},
+            {0x18, 0x21, 0x32, 0x03, 0x24, 0x1d, 0x06, 0x37},
+            {0x18, 0x21, 0x2a, 0x03, 0x24, 0x1d, 0x06, 0x37},
+            {0x18, 0x21, 0x2a, 0x03, 0x24, 0x1d, 0x06, 0x37},
+            {0x18, 0x21, 0x2a, 0x03, 0x24, 0x1d, 0x06, 0x37},
+            {0x18, 0x21, 0x2a, 0x0b, 0x24, 0x1d, 0x06, 0x37},
+            {0x18, 0x21, 0x2a, 0x0b, 0x24, 0x0d, 0x06, 0x2f},
+            {0x18, 0x21, 0x2a, 0x0b, 0x24, 0x0d, 0x06, 0x2f},
+            {0x18, 0x21, 0x2a, 0x0b, 0x24, 0x0d, 0x06, 0x2f},
+            {0x38, 0x21, 0x2a, 0x0b, 0x24, 0x0d, 0x0e, 0x2f},
+            {0x28, 0x21, 0x2a, 0x0b, 0x2c, 0x0d, 0x0e, 0x2f},
+            {0x28, 0x21, 0x2a, 0x0b, 0x2c, 0x0d, 0x0e, 0x2f},
+            {0x28, 0x21, 0x2a, 0x0b, 0x2c, 0x2d, 0x0e, 0x2f},
+            {0x28, 0x01, 0x2a, 0x0b, 0x2c, 0x2d, 0x0e, 0x2f},
+            {0x28, 0x09, 0x2a, 0x2b, 0x2c, 0x2d, 0x0e, 0x2f},
+            {0x28, 0x09, 0x2a, 0x2b, 0x2c, 0x2d, 0x0e, 0x2f},
+            {0x28, 0x09, 0x2a, 0x2b, 0x2c, 0x2d, 0x0e, 0x2f},
+            {0x28, 0x09, 0x22, 0x2b, 0x0c, 0x2d, 0x0e, 0x2f},
+            {0x28, 0x09, 0x02, 0x2b, 0x0c, 0x2d, 0x2e, 0x2f},
+            {0x28, 0x09, 0x02, 0x2b, 0x0c, 0x2d, 0x2e, 0x2f},
+            {0x28, 0x09, 0x02, 0x2b, 0x0c, 0x2d, 0x2e, 0x2f},
+            {0x28, 0x09, 0x02, 0x3b, 0x0c, 0x2d, 0x2e, 0x27},
+            {0x28, 0x09, 0x02, 0x3b, 0x0c, 0x25, 0x2e, 0x07},
+            {0x28, 0x09, 0x02, 0x3b, 0x0c, 0x25, 0x2e, 0x07},
+            {0x28, 0x09, 0x02, 0x3b, 0x0c, 0x25, 0x2e, 0x07},
+            {0x30, 0x09, 0x02, 0x3b, 0x0c, 0x25, 0x3e, 0x07},
+            {0x30, 0x09, 0x02, 0x3b, 0x1c, 0x25, 0x3e, 0x07},
+            {0x30, 0x09, 0x02, 0x3b, 0x1c, 0x25, 0x3e, 0x07},
+            {0x30, 0x09, 0x02, 0x3b, 0x1c, 0x25, 0x3e, 0x07},
+            {0x30, 0x11, 0x02, 0x3b, 0x1c, 0x25, 0x3e, 0x07},
+            {0x30, 0x11, 0x02, 0x3b, 0x1c, 0x25, 0x3e, 0x07},
+            {0x30, 0x11, 0x02, 0x3b, 0x1c, 0x25, 0x3e, 0x07},
+            {0x30, 0x11, 0x02, 0x3b, 0x1c, 0x25, 0x3e, 0x07}
+        },
+        {
+            {0x18, 0x21, 0x3a, 0x03, 0x24, 0x1d, 0x06, 0x3f},
+            {0x18, 0x21, 0x3a, 0x03, 0x24, 0x1d, 0x06, 0x3f},
+            {0x18, 0x21, 0x3a, 0x03, 0x24, 0x1d, 0x06, 0x3f},
+            {0x18, 0x21, 0x3a, 0x03, 0x24, 0x1d, 0x06, 0x3f},
+            {0x18, 0x21, 0x3a, 0x03, 0x24, 0x1d, 0x06, 0x3f},
+            {0x18, 0x21, 0x2a, 0x03, 0x24, 0x1d, 0x06, 0x3f},
+            {0x18, 0x21, 0x2a, 0x03, 0x24, 0x1d, 0x06, 0x3f},
+            {0x18, 0x21, 0x2a, 0x03, 0x24, 0x1d, 0x06, 0x3f},
+            {0x18, 0x21, 0x2a, 0x03, 0x24, 0x0d, 0x06, 0x3f},
+            {0x18, 0x21, 0x2a, 0x0b, 0x24, 0x0d, 0x06, 0x2f},
+            {0x18, 0x21, 0x2a, 0x0b, 0x24, 0x0d, 0x06, 0x2f},
+            {0x18, 0x21, 0x2a, 0x0b, 0x24, 0x0d, 0x06, 0x2f},
+            {0x08, 0x21, 0x2a, 0x0b, 0x2c, 0x0d, 0x06, 0x2f},
+            {0x28, 0x21, 0x2a, 0x0b, 0x2c, 0x0d, 0x0e, 0x2f},
+            {0x28, 0x21, 0x2a, 0x0b, 0x2c, 0x0d, 0x0e, 0x2f},
+            {0x28, 0x21, 0x2a, 0x0b, 0x2c, 0x0d, 0x0e, 0x2f},
+            {0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x0e, 0x2f},
+            {0x28, 0x09, 0x2a, 0x2b, 0x2c, 0x2d, 0x0e, 0x2f},
+            {0x28, 0x09, 0x2a, 0x2b, 0x2c, 0x2d, 0x0e, 0x2f},
+            {0x28, 0x09, 0x2a, 0x2b, 0x2c, 0x2d, 0x0e, 0x2f},
+            {0x28, 0x09, 0x0a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f},
+            {0x28, 0x09, 0x02, 0x2b, 0x0c, 0x2d, 0x2e, 0x2f},
+            {0x28, 0x09, 0x02, 0x2b, 0x0c, 0x2d, 0x2e, 0x2f},
+            {0x28, 0x09, 0x02, 0x2b, 0x0c, 0x2d, 0x2e, 0x2f},
+            {0x28, 0x09, 0x02, 0x2b, 0x0c, 0x25, 0x2e, 0x0f},
+            {0x28, 0x09, 0x02, 0x3b, 0x0c, 0x25, 0x2e, 0x07},
+            {0x28, 0x09, 0x02, 0x3b, 0x0c, 0x25, 0x2e, 0x07},
+            {0x28, 0x09, 0x02, 0x3b, 0x0c, 0x25, 0x2e, 0x07},
+            {0x20, 0x09, 0x02, 0x3b, 0x1c, 0x25, 0x2e, 0x07},
+            {0x20, 0x09, 0x02, 0x3b, 0x1c, 0x25, 0x3e, 0x07},
+            {0x20, 0x09, 0x02, 0x3b, 0x1c, 0x25, 0x3e, 0x07},
+            {0x20, 0x09, 0x02, 0x3b, 0x1c, 0x25, 0x3e, 0x07},
+            {0x20, 0x19, 0x02, 0x3b, 0x1c, 0x25, 0x3e, 0x07},
+            {0x20, 0x19, 0x02, 0x3b, 0x1c, 0x25, 0x3e, 0x07},
+            {0x20, 0x19, 0x02, 0x3b, 0x1c, 0x25, 0x3e, 0x07},
+            {0x20, 0x19, 0x02, 0x3b, 0x1c, 0x25, 0x3e, 0x07}
+        },
+        {
+            {0x18, 0x21, 0x3a, 0x03, 0x24, 0x1d, 0x06, 0x3f},
+            {0x18, 0x21, 0x3a, 0x03, 0x24, 0x1d, 0x06, 0x3f},
+            {0x18, 0x21, 0x3a, 0x03, 0x24, 0x1d, 0x06, 0x3f},
+            {0x18, 0x21, 0x3a, 0x03, 0x24, 0x1d, 0x06, 0x3f},
+            {0x18, 0x21, 0x3a, 0x03, 0x24, 0x1d, 0x06, 0x3f},
+            {0x18, 0x21, 0x3a, 0x03, 0x24, 0x1d, 0x06, 0x3f},
+            {0x18, 0x21, 0x2a, 0x03, 0x24, 0x1d, 0x06, 0x3f},
+            {0x18, 0x21, 0x2a, 0x03, 0x24, 0x0d, 0x06, 0x3f},
+            {0x18, 0x21, 0x2a, 0x03, 0x24, 0x0d, 0x06, 0x3f},
+            {0x18, 0x21, 0x2a, 0x03, 0x24, 0x0d, 0x06, 0x3f},
+            {0x18, 0x21, 0x2a, 0x0b, 0x24, 0x0d, 0x06, 0x2f},
+            {0x08, 0x21, 0x2a, 0x0b, 0x2c, 0x0d, 0x06, 0x2f},
+            {0x08, 0x21, 0x2a, 0x0b, 0x2c, 0x0d, 0x06, 0x2f},
+            {0x08, 0x21, 0x2a, 0x0b, 0x2c, 0x0d, 0x06, 0x2f},
+            {0x28, 0x21, 0x2a, 0x0b, 0x2c, 0x0d, 0x0e, 0x2f},
+            {0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x0d, 0x0e, 0x2f},
+            {0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x0d, 0x0e, 0x2f},
+            {0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x0e, 0x2f},
+            {0x28, 0x09, 0x2a, 0x2b, 0x2c, 0x2d, 0x0e, 0x2f},
+            {0x28, 0x09, 0x0a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f},
+            {0x28, 0x09, 0x0a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f},
+            {0x28, 0x09, 0x0a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f},
+            {0x28, 0x09, 0x02, 0x2b, 0x0c, 0x2d, 0x2e, 0x2f},
+            {0x28, 0x09, 0x02, 0x2b, 0x0c, 0x25, 0x2e, 0x0f},
+            {0x28, 0x09, 0x02, 0x2b, 0x0c, 0x25, 0x2e, 0x0f},
+            {0x28, 0x09, 0x02, 0x2b, 0x0c, 0x25, 0x2e, 0x0f},
+            {0x28, 0x09, 0x02, 0x3b, 0x0c, 0x25, 0x2e, 0x07},
+            {0x20, 0x09, 0x02, 0x3b, 0x1c, 0x25, 0x2e, 0x07},
+            {0x20, 0x09, 0x02, 0x3b, 0x1c, 0x25, 0x2e, 0x07},
+            {0x20, 0x09, 0x02, 0x3b, 0x1c, 0x25, 0x2e, 0x07},
+            {0x20, 0x09, 0x02, 0x3b, 0x1c, 0x25, 0x3e, 0x07},
+            {0x20, 0x19, 0x02, 0x3b, 0x1c, 0x25, 0x3e, 0x07},
+            {0x20, 0x19, 0x02, 0x3b, 0x1c, 0x25, 0x3e, 0x07},
+            {0x20, 0x19, 0x02, 0x3b, 0x1c, 0x25, 0x3e, 0x07},
+            {0x20, 0x19, 0x02, 0x3b, 0x1c, 0x25, 0x3e, 0x07},
+            {0x20, 0x19, 0x02, 0x3b, 0x1c, 0x25, 0x3e, 0x07}
+        },
+        {
+            {0x18, 0x21, 0x3a, 0x03, 0x24, 0x1d, 0x06, 0x3f},
+            {0x18, 0x21, 0x3a, 0x03, 0x24, 0x1d, 0x06, 0x3f},
+            {0x18, 0x21, 0x3a, 0x03, 0x24, 0x1d, 0x06, 0x3f},
+            {0x18, 0x21, 0x3a, 0x03, 0x24, 0x1d, 0x06, 0x3f},
+            {0x18, 0x21, 0x3a, 0x03, 0x24, 0x1d, 0x06, 0x3f},
+            {0x18, 0x21, 0x3a, 0x03, 0x24, 0x1d, 0x06, 0x3f},
+            {0x18, 0x21, 0x3a, 0x03, 0x24, 0x0d, 0x06, 0x3f},
+            {0x18, 0x21, 0x2a, 0x03, 0x24, 0x0d, 0x06, 0x3f},
+            {0x18, 0x21, 0x2a, 0x03, 0x24, 0x0d, 0x06, 0x3f},
+            {0x18, 0x21, 0x2a, 0x03, 0x24, 0x0d, 0x06, 0x3f},
+            {0x08, 0x21, 0x2a, 0x03, 0x2c, 0x0d, 0x06, 0x3f},
+            {0x08, 0x21, 0x2a, 0x0b, 0x2c, 0x0d, 0x06, 0x2f},
+            {0x08, 0x21, 0x2a, 0x0b, 0x2c, 0x0d, 0x06, 0x2f},
+            {0x08, 0x21, 0x2a, 0x0b, 0x2c, 0x0d, 0x06, 0x2f},
+            {0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x0d, 0x0e, 0x2f},
+            {0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x0d, 0x0e, 0x2f},
+            {0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x0d, 0x0e, 0x2f},
+            {0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x0d, 0x0e, 0x2f},
+            {0x28, 0x29, 0x0a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f},
+            {0x28, 0x09, 0x0a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f},
+            {0x28, 0x09, 0x0a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f},
+            {0x28, 0x09, 0x0a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f},
+            {0x28, 0x09, 0x0a, 0x2b, 0x2c, 0x25, 0x2e, 0x0f},
+            {0x28, 0x09, 0x02, 0x2b, 0x0c, 0x25, 0x2e, 0x0f},
+            {0x28, 0x09, 0x02, 0x2b, 0x0c, 0x25, 0x2e, 0x0f},
+            {0x28, 0x09, 0x02, 0x2b, 0x0c, 0x25, 0x2e, 0x0f},
+            {0x20, 0x09, 0x02, 0x2b, 0x1c, 0x25, 0x2e, 0x0f},
+            {0x20, 0x09, 0x02, 0x3b, 0x1c, 0x25, 0x2e, 0x07},
+            {0x20, 0x09, 0x02, 0x3b, 0x1c, 0x25, 0x2e, 0x07},
+            {0x20, 0x09, 0x02, 0x3b, 0x1c, 0x25, 0x2e, 0x07},
+            {0x20, 0x19, 0x02, 0x3b, 0x1c, 0x25, 0x2e, 0x07},
+            {0x20, 0x19, 0x02, 0x3b, 0x1c, 0x25, 0x3e, 0x07},
+            {0x20, 0x19, 0x02, 0x3b, 0x1c, 0x25, 0x3e, 0x07},
+            {0x20, 0x19, 0x02, 0x3b, 0x1c, 0x25, 0x3e, 0x07},
+            {0x20, 0x19, 0x02, 0x3b, 0x1c, 0x25, 0x3e, 0x07},
+            {0x20, 0x19, 0x02, 0x3b, 0x1c, 0x25, 0x3e, 0x07}
+        },
+        {
+            {0x18, 0x21, 0x3a, 0x03, 0x24, 0x1d, 0x06, 0x3f},
+            {0x18, 0x21, 0x3a, 0x03, 0x24, 0x1d, 0x06, 0x3f},
+            {0x18, 0x21, 0x3a, 0x03, 0x24, 0x1d, 0x06, 0x3f},
+            {0x18, 0x21, 0x3a, 0x03, 0x24, 0x1d, 0x06, 0x3f},
+            {0x18, 0x21, 0x3a, 0x03, 0x24, 0x1d, 0x06, 0x3f},
+            {0x18, 0x21, 0x3a, 0x03, 0x24, 0x0d, 0x06, 0x3f},
+            {0x18, 0x21, 0x3a, 0x03, 0x24, 0x0d, 0x06, 0x3f},
+            {0x18, 0x21, 0x3a, 0x03, 0x24, 0x0d, 0x06, 0x3f},
+            {0x18, 0x21, 0x2a, 0x03, 0x24, 0x0d, 0x06, 0x3f},
+            {0x08, 0x21, 0x2a, 0x03, 0x2c, 0x0d, 0x06, 0x3f},
+            {0x08, 0x21, 0x2a, 0x03, 0x2c, 0x0d, 0x06, 0x3f},
+            {0x08, 0x21, 0x2a, 0x03, 0x2c, 0x0d, 0x06, 0x3f},
+            {0x08, 0x21, 0x2a, 0x0b, 0x2c, 0x0d, 0x06, 0x2f},
+            {0x08, 0x29, 0x2a, 0x2b, 0x2c, 0x0d, 0x06, 0x2f},
+            {0x08, 0x29, 0x2a, 0x2b, 0x2c, 0x0d, 0x06, 0x2f},
+            {0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x0d, 0x0e, 0x2f},
+            {0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x0d, 0x0e, 0x2f},
+            {0x28, 0x29, 0x0a, 0x2b, 0x2c, 0x0d, 0x2e, 0x2f},
+            {0x28, 0x29, 0x0a, 0x2b, 0x2c, 0x0d, 0x2e, 0x2f},
+            {0x28, 0x29, 0x0a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f},
+            {0x28, 0x09, 0x0a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f},
+            {0x28, 0x09, 0x0a, 0x2b, 0x2c, 0x25, 0x2e, 0x0f},
+            {0x28, 0x09, 0x0a, 0x2b, 0x2c, 0x25, 0x2e, 0x0f},
+            {0x28, 0x09, 0x0a, 0x2b, 0x2c, 0x25, 0x2e, 0x0f},
+            {0x28, 0x09, 0x02, 0x2b, 0x0c, 0x25, 0x2e, 0x0f},
+            {0x20, 0x09, 0x02, 0x2b, 0x1c, 0x25, 0x2e, 0x0f},
+            {0x20, 0x09, 0x02, 0x2b, 0x1c, 0x25, 0x2e, 0x0f},
+            {0x20, 0x09, 0x02, 0x2b, 0x1c, 0x25, 0x2e, 0x0f},
+            {0x20, 0x09, 0x02, 0x3b, 0x1c, 0x25, 0x2e, 0x07},
+            {0x20, 0x19, 0x02, 0x3b, 0x1c, 0x25, 0x2e, 0x07},
+            {0x20, 0x19, 0x02, 0x3b, 0x1c, 0x25, 0x2e, 0x07},
+            {0x20, 0x19, 0x02, 0x3b, 0x1c, 0x25, 0x2e, 0x07},
+            {0x20, 0x19, 0x02, 0x3b, 0x1c, 0x25, 0x3e, 0x07},
+            {0x20, 0x19, 0x02, 0x3b, 0x1c, 0x25, 0x3e, 0x07},
+            {0x20, 0x19, 0x02, 0x3b, 0x1c, 0x25, 0x3e, 0x07},
+            {0x20, 0x19, 0x02, 0x3b, 0x1c, 0x25, 0x3e, 0x07}
+        },
+        {
+            {0x18, 0x21, 0x3a, 0x03, 0x34, 0x15, 0x06, 0x3f},
+            {0x18, 0x21, 0x3a, 0x03, 0x34, 0x15, 0x06, 0x3f},
+            {0x18, 0x21, 0x3a, 0x03, 0x34, 0x15, 0x06, 0x3f},
+            {0x18, 0x21, 0x3a, 0x03, 0x34, 0x15, 0x06, 0x3f},
+            {0x18, 0x21, 0x3a, 0x03, 0x34, 0x15, 0x06, 0x3f},
+            {0x18, 0x21, 0x3a, 0x03, 0x34, 0x0d, 0x06, 0x3f},
+            {0x18, 0x21, 0x3a, 0x03, 0x34, 0x0d, 0x06, 0x3f},
+            {0x18, 0x21, 0x3a, 0x03, 0x34, 0x0d, 0x06, 0x3f},
+            {0x08, 0x21, 0x3a, 0x03, 0x34, 0x0d, 0x06, 0x3f},
+            {0x08, 0x21, 0x2a, 0x03, 0x2c, 0x0d, 0x06, 0x3f},
+            {0x08, 0x21, 0x2a, 0x03, 0x2c, 0x0d, 0x06, 0x3f},
+            {0x08, 0x21, 0x2a, 0x03, 0x2c, 0x0d, 0x06, 0x3f},
+            {0x08, 0x29, 0x2a, 0x23, 0x2c, 0x0d, 0x06, 0x3f},
+            {0x08, 0x29, 0x2a, 0x2b, 0x2c, 0x0d, 0x06, 0x2f},
+            {0x08, 0x29, 0x2a, 0x2b, 0x2c, 0x0d, 0x06, 0x2f},
+            {0x08, 0x29, 0x2a, 0x2b, 0x2c, 0x0d, 0x06, 0x2f},
+            {0x28, 0x29, 0x0a, 0x2b, 0x2c, 0x0d, 0x2e, 0x2f},
+            {0x28, 0x29, 0x0a, 0x2b, 0x2c, 0x0d, 0x2e, 0x2f},
+            {0x28, 0x29, 0x0a, 0x2b, 0x2c, 0x0d, 0x2e, 0x2f},
+            {0x28, 0x29, 0x0a, 0x2b, 0x2c, 0x0d, 0x2e, 0x2f},
+            {0x28, 0x29, 0x0a, 0x2b, 0x2c, 0x25, 0x2e, 0x0f},
+            {0x28, 0x09, 0x0a, 0x2b, 0x2c, 0x25, 0x2e, 0x0f},
+            {0x28, 0x09, 0x0a, 0x2b, 0x2c, 0x25, 0x2e, 0x0f},
+            {0x28, 0x09, 0x0a, 0x2b, 0x2c, 0x25, 0x2e, 0x0f},
+            {0x20, 0x09, 0x0a, 0x2b, 0x3c, 0x25, 0x2e, 0x0f},
+            {0x20, 0x09, 0x02, 0x2b, 0x1c, 0x25, 0x2e, 0x0f},
+            {0x20, 0x09, 0x02, 0x2b, 0x1c, 0x25, 0x2e, 0x0f},
+            {0x20, 0x09, 0x02, 0x2b, 0x1c, 0x25, 0x2e, 0x0f},
+            {0x20, 0x19, 0x02, 0x33, 0x1c, 0x25, 0x2e, 0x0f},
+            {0x20, 0x19, 0x02, 0x33, 0x1c, 0x25, 0x2e, 0x07},
+            {0x20, 0x19, 0x02, 0x33, 0x1c, 0x25, 0x2e, 0x07},
+            {0x20, 0x19, 0x02, 0x33, 0x1c, 0x25, 0x2e, 0x07},
+            {0x20, 0x19, 0x02, 0x33, 0x1c, 0x25, 0x36, 0x07},
+            {0x20, 0x19, 0x02, 0x33, 0x1c, 0x25, 0x36, 0x07},
+            {0x20, 0x19, 0x02, 0x33, 0x1c, 0x25, 0x36, 0x07},
+            {0x20, 0x19, 0x02, 0x33, 0x1c, 0x25, 0x36, 0x07}
+        },
+        {
+            {0x18, 0x21, 0x3a, 0x03, 0x34, 0x15, 0x06, 0x3f},
+            {0x18, 0x21, 0x3a, 0x03, 0x34, 0x15, 0x06, 0x3f},
+            {0x18, 0x21, 0x3a, 0x03, 0x34, 0x15, 0x06, 0x3f},
+            {0x18, 0x21, 0x3a, 0x03, 0x34, 0x15, 0x06, 0x3f},
+            {0x18, 0x21, 0x3a, 0x03, 0x34, 0x15, 0x06, 0x3f},
+            {0x18, 0x21, 0x3a, 0x03, 0x34, 0x15, 0x06, 0x3f},
+            {0x18, 0x21, 0x3a, 0x03, 0x34, 0x0d, 0x06, 0x3f},
+            {0x08, 0x21, 0x3a, 0x03, 0x34, 0x0d, 0x06, 0x3f},
+            {0x08, 0x21, 0x3a, 0x03, 0x34, 0x0d, 0x06, 0x3f},
+            {0x08, 0x21, 0x3a, 0x03, 0x34, 0x0d, 0x06, 0x3f},
+            {0x08, 0x21, 0x2a, 0x03, 0x2c, 0x0d, 0x06, 0x3f},
+            {0x08, 0x29, 0x2a, 0x23, 0x2c, 0x0d, 0x06, 0x3f},
+            {0x08, 0x29, 0x2a, 0x23, 0x2c, 0x0d, 0x06, 0x3f},
+            {0x08, 0x29, 0x2a, 0x23, 0x2c, 0x0d, 0x06, 0x3f},
+            {0x08, 0x29, 0x2a, 0x2b, 0x2c, 0x0d, 0x06, 0x2f},
+            {0x08, 0x29, 0x0a, 0x2b, 0x2c, 0x0d, 0x26, 0x2f},
+            {0x08, 0x29, 0x0a, 0x2b, 0x2c, 0x0d, 0x26, 0x2f},
+            {0x28, 0x29, 0x0a, 0x2b, 0x2c, 0x0d, 0x2e, 0x2f},
+            {0x28, 0x29, 0x0a, 0x2b, 0x2c, 0x0d, 0x2e, 0x2f},
+            {0x28, 0x29, 0x0a, 0x2b, 0x2c, 0x05, 0x2e, 0x0f},
+            {0x28, 0x29, 0x0a, 0x2b, 0x2c, 0x05, 0x2e, 0x0f},
+            {0x28, 0x29, 0x0a, 0x2b, 0x2c, 0x25, 0x2e, 0x0f},
+            {0x28, 0x09, 0x0a, 0x2b, 0x2c, 0x25, 0x2e, 0x0f},
+            {0x20, 0x09, 0x0a, 0x2b, 0x3c, 0x25, 0x2e, 0x0f},
+            {0x20, 0x09, 0x0a, 0x2b, 0x3c, 0x25, 0x2e, 0x0f},
+            {0x20, 0x09, 0x0a, 0x2b, 0x3c, 0x25, 0x2e, 0x0f},
+            {0x20, 0x09, 0x02, 0x2b, 0x1c, 0x25, 0x2e, 0x0f},
+            {0x20, 0x19, 0x02, 0x33, 0x1c, 0x25, 0x2e, 0x0f},
+            {0x20, 0x19, 0x02, 0x33, 0x1c, 0x25, 0x2e, 0x0f},
+            {0x20, 0x19, 0x02, 0x33, 0x1c, 0x25, 0x2e, 0x0f},
+            {0x20, 0x19, 0x02, 0x33, 0x1c, 0x25, 0x2e, 0x07},
+            {0x20, 0x19, 0x02, 0x33, 0x1c, 0x25, 0x36, 0x07},
+            {0x20, 0x19, 0x02, 0x33, 0x1c, 0x25, 0x36, 0x07},
+            {0x20, 0x19, 0x02, 0x33, 0x1c, 0x25, 0x36, 0x07},
+            {0x20, 0x19, 0x02, 0x33, 0x1c, 0x25, 0x36, 0x07},
+            {0x20, 0x19, 0x02, 0x33, 0x1c, 0x25, 0x36, 0x07}
+        },
+        {
+            {0x18, 0x21, 0x3a, 0x03, 0x34, 0x15, 0x06, 0x3f},
+            {0x18, 0x21, 0x3a, 0x03, 0x34, 0x15, 0x06, 0x3f},
+            {0x18, 0x21, 0x3a, 0x03, 0x34, 0x15, 0x06, 0x3f},
+            {0x18, 0x21, 0x3a, 0x03, 0x34, 0x15, 0x06, 0x3f},
+            {0x18, 0x21, 0x3a, 0x03, 0x34, 0x15, 0x06, 0x3f},
+            {0x18, 0x21, 0x3a, 0x03, 0x34, 0x15, 0x06, 0x3f},
+            {0x08, 0x21, 0x3a, 0x03, 0x34, 0x15, 0x06, 0x3f},
+            {0x08, 0x21, 0x3a, 0x03, 0x34, 0x0d, 0x06, 0x3f},
+            {0x08, 0x21, 0x3a, 0x03, 0x34, 0x0d, 0x06, 0x3f},
+            {0x08, 0x21, 0x3a, 0x03, 0x34, 0x0d, 0x06, 0x3f},
+            {0x08, 0x29, 0x3a, 0x23, 0x34, 0x0d, 0x06, 0x3f},
+            {0x08, 0x29, 0x2a, 0x23, 0x2c, 0x0d, 0x06, 0x3f},
+            {0x08, 0x29, 0x2a, 0x23, 0x2c, 0x0d, 0x06, 0x3f},
+            {0x08, 0x29, 0x2a, 0x23, 0x2c, 0x0d, 0x06, 0x3f},
+            {0x08, 0x29, 0x0a, 0x2b, 0x2c, 0x0d, 0x26, 0x2f},
+            {0x08, 0x29, 0x0a, 0x2b, 0x2c, 0x0d, 0x26, 0x2f},
+            {0x08, 0x29, 0x0a, 0x2b, 0x2c, 0x0d, 0x26, 0x2f},
+            {0x08, 0x29, 0x0a, 0x2b, 0x2c, 0x0d, 0x26, 0x2f},
+            {0x28, 0x29, 0x0a, 0x2b, 0x2c, 0x05, 0x2e, 0x0f},
+            {0x28, 0x29, 0x0a, 0x2b, 0x2c, 0x05, 0x2e, 0x0f},
+            {0x28, 0x29, 0x0a, 0x2b, 0x2c, 0x05, 0x2e, 0x0f},
+            {0x28, 0x29, 0x0a, 0x2b, 0x2c, 0x05, 0x2e, 0x0f},
+            {0x20, 0x29, 0x0a, 0x2b, 0x3c, 0x25, 0x2e, 0x0f},
+            {0x20, 0x09, 0x0a, 0x2b, 0x3c, 0x25, 0x2e, 0x0f},
+            {0x20, 0x09, 0x0a, 0x2b, 0x3c, 0x25, 0x2e, 0x0f},
+            {0x20, 0x09, 0x0a, 0x2b, 0x3c, 0x25, 0x2e, 0x0f},
+            {0x20, 0x19, 0x0a, 0x33, 0x3c, 0x25, 0x2e, 0x0f},
+            {0x20, 0x19, 0x02, 0x33, 0x1c, 0x25, 0x2e, 0x0f},
+            {0x20, 0x19, 0x02, 0x33, 0x1c, 0x25, 0x2e, 0x0f},
+            {0x20, 0x19, 0x02, 0x33, 0x1c, 0x25, 0x2e, 0x0f},
+            {0x20, 0x19, 0x02, 0x33, 0x1c, 0x25, 0x36, 0x0f},
+            {0x20, 0x19, 0x02, 0x33, 0x1c, 0x25, 0x36, 0x07},
+            {0x20, 0x19, 0x02, 0x33, 0x1c, 0x25, 0x36, 0x07},
+            {0x20, 0x19, 0x02, 0x33, 0x1c, 0x25, 0x36, 0x07},
+            {0x20, 0x19, 0x02, 0x33, 0x1c, 0x25, 0x36, 0x07},
+            {0x20, 0x19, 0x02, 0x33, 0x1c, 0x25, 0x36, 0x07}
+        },
+        {
+            {0x18, 0x21, 0x3a, 0x03, 0x34, 0x15, 0x06, 0x3f},
+            {0x18, 0x21, 0x3a, 0x03, 0x34, 0x15, 0x06, 0x3f},
+            {0x18, 0x21, 0x3a, 0x03, 0x34, 0x15, 0x06, 0x3f},
+            {0x18, 0x21, 0x3a, 0x03, 0x34, 0x15, 0x06, 0x3f},
+            {0x18, 0x21, 0x3a, 0x03, 0x34, 0x15, 0x06, 0x3f},
+            {0x08, 0x21, 0x3a, 0x03, 0x34, 0x15, 0x06, 0x3f},
+            {0x08, 0x21, 0x3a, 0x03, 0x34, 0x15, 0x06, 0x3f},
+            {0x08, 0x21, 0x3a, 0x03, 0x34, 0x15, 0x06, 0x3f},
+            {0x08, 0x21, 0x3a, 0x03, 0x34, 0x0d, 0x06, 0x3f},
+            {0x08, 0x29, 0x3a, 0x23, 0x34, 0x0d, 0x06, 0x3f},
+            {0x08, 0x29, 0x3a, 0x23, 0x34, 0x0d, 0x06, 0x3f},
+            {0x08, 0x29, 0x3a, 0x23, 0x34, 0x0d, 0x06, 0x3f},
+            {0x08, 0x29, 0x2a, 0x23, 0x2c, 0x0d, 0x06, 0x3f},
+            {0x08, 0x29, 0x0a, 0x23, 0x2c, 0x0d, 0x26, 0x3f},
+            {0x08, 0x29, 0x0a, 0x23, 0x2c, 0x0d, 0x26, 0x3f},
+            {0x08, 0x29, 0x0a, 0x2b, 0x2c, 0x0d, 0x26, 0x2f},
+            {0x08, 0x29, 0x0a, 0x2b, 0x2c, 0x0d, 0x26, 0x2f},
+            {0x08, 0x29, 0x0a, 0x2b, 0x2c, 0x05, 0x26, 0x0f},
+            {0x08, 0x29, 0x0a, 0x2b, 0x2c, 0x05, 0x26, 0x0f},
+            {0x28, 0x29, 0x0a, 0x2b, 0x2c, 0x05, 0x2e, 0x0f},
+            {0x28, 0x29, 0x0a, 0x2b, 0x2c, 0x05, 0x2e, 0x0f},
+            {0x20, 0x29, 0x0a, 0x2b, 0x3c, 0x05, 0x2e, 0x0f},
+            {0x20, 0x29, 0x0a, 0x2b, 0x3c, 0x05, 0x2e, 0x0f},
+            {0x20, 0x29, 0x0a, 0x2b, 0x3c, 0x25, 0x2e, 0x0f},
+            {0x20, 0x09, 0x0a, 0x2b, 0x3c, 0x25, 0x2e, 0x0f},
+            {0x20, 0x19, 0x0a, 0x33, 0x3c, 0x25, 0x2e, 0x0f},
+            {0x20, 0x19, 0x0a, 0x33, 0x3c, 0x25, 0x2e, 0x0f},
+            {0x20, 0x19, 0x0a, 0x33, 0x3c, 0x25, 0x2e, 0x0f},
+            {0x20, 0x19, 0x02, 0x33, 0x1c, 0x25, 0x2e, 0x0f},
+            {0x20, 0x19, 0x02, 0x33, 0x1c, 0x25, 0x36, 0x0f},
+            {0x20, 0x19, 0x02, 0x33, 0x1c, 0x25, 0x36, 0x0f},
+            {0x20, 0x19, 0x02, 0x33, 0x1c, 0x25, 0x36, 0x0f},
+            {0x20, 0x19, 0x02, 0x33, 0x1c, 0x25, 0x36, 0x07},
+            {0x20, 0x19, 0x02, 0x33, 0x1c, 0x25, 0x36, 0x07},
+            {0x20, 0x19, 0x02, 0x33, 0x1c, 0x25, 0x36, 0x07},
+            {0x20, 0x19, 0x02, 0x33, 0x1c, 0x25, 0x36, 0x07}
+        },
+        {
+            {0x10, 0x31, 0x3a, 0x03, 0x34, 0x15, 0x06, 0x3f},
+            {0x10, 0x31, 0x3a, 0x03, 0x34, 0x15, 0x06, 0x3f},
+            {0x10, 0x31, 0x3a, 0x03, 0x34, 0x15, 0x06, 0x3f},
+            {0x10, 0x31, 0x3a, 0x03, 0x34, 0x15, 0x06, 0x3f},
+            {0x10, 0x31, 0x3a, 0x03, 0x34, 0x15, 0x06, 0x3f},
+            {0x08, 0x31, 0x3a, 0x03, 0x34, 0x15, 0x06, 0x3f},
+            {0x08, 0x31, 0x3a, 0x03, 0x34, 0x15, 0x06, 0x3f},
+            {0x08, 0x31, 0x3a, 0x03, 0x34, 0x15, 0x06, 0x3f},
+            {0x08, 0x31, 0x3a, 0x23, 0x34, 0x15, 0x06, 0x3f},
+            {0x08, 0x29, 0x3a, 0x23, 0x34, 0x0d, 0x06, 0x3f},
+            {0x08, 0x29, 0x3a, 0x23, 0x34, 0x0d, 0x06, 0x3f},
+            {0x08, 0x29, 0x3a, 0x23, 0x34, 0x0d, 0x06, 0x3f},
+            {0x08, 0x29, 0x1a, 0x23, 0x34, 0x0d, 0x26, 0x3f},
+            {0x08, 0x29, 0x0a, 0x23, 0x2c, 0x0d, 0x26, 0x3f},
+            {0x08, 0x29, 0x0a, 0x23, 0x2c, 0x0d, 0x26, 0x3f},
+            {0x08, 0x29, 0x0a, 0x23, 0x2c, 0x0d, 0x26, 0x3f},
+            {0x08, 0x29, 0x0a, 0x2b, 0x2c, 0x05, 0x26, 0x0f},
+            {0x08, 0x29, 0x0a, 0x2b, 0x2c, 0x05, 0x26, 0x0f},
+            {0x08, 0x29, 0x0a, 0x2b, 0x2c, 0x05, 0x26, 0x0f},
+            {0x08, 0x29, 0x0a, 0x2b, 0x2c, 0x05, 0x26, 0x0f},
+            {0x20, 0x29, 0x0a, 0x2b, 0x3c, 0x05, 0x2e, 0x0f},
+            {0x20, 0x29, 0x0a, 0x2b, 0x3c, 0x05, 0x2e, 0x0f},
+            {0x20, 0x29, 0x0a, 0x2b, 0x3c, 0x05, 0x2e, 0x0f},
+            {0x20, 0x29, 0x0a, 0x2b, 0x3c, 0x05, 0x2e, 0x0f},
+            {0x20, 0x39, 0x0a, 0x33, 0x3c, 0x25, 0x2e, 0x0f},
+            {0x20, 0x19, 0x0a, 0x33, 0x3c, 0x25, 0x2e, 0x0f},
+            {0x20, 0x19, 0x0a, 0x33, 0x3c, 0x25, 0x2e, 0x0f},
+            {0x20, 0x19, 0x0a, 0x33, 0x3c, 0x25, 0x2e, 0x0f},
+            {0x20, 0x19, 0x12, 0x33, 0x3c, 0x25, 0x36, 0x0f},
+            {0x20, 0x19, 0x12, 0x33, 0x1c, 0x25, 0x36, 0x0f},
+            {0x20, 0x19, 0x12, 0x33, 0x1c, 0x25, 0x36, 0x0f},
+            {0x20, 0x19, 0x12, 0x33, 0x1c, 0x25, 0x36, 0x0f},
+            {0x20, 0x19, 0x12, 0x33, 0x1c, 0x25, 0x36, 0x17},
+            {0x20, 0x19, 0x12, 0x33, 0x1c, 0x25, 0x36, 0x17},
+            {0x20, 0x19, 0x12, 0x33, 0x1c, 0x25, 0x36, 0x17},
+            {0x20, 0x19, 0x12, 0x33, 0x1c, 0x25, 0x36, 0x17}
+        },
+        {
+            {0x10, 0x31, 0x3a, 0x03, 0x34, 0x15, 0x06, 0x3f},
+            {0x10, 0x31, 0x3a, 0x03, 0x34, 0x15, 0x06, 0x3f},
+            {0x10, 0x31, 0x3a, 0x03, 0x34, 0x15, 0x06, 0x3f},
+            {0x10, 0x31, 0x3a, 0x03, 0x34, 0x15, 0x06, 0x3f},
+            {0x10, 0x31, 0x3a, 0x03, 0x34, 0x15, 0x06, 0x3f},
+            {0x10, 0x31, 0x3a, 0x03, 0x34, 0x15, 0x06, 0x3f},
+            {0x08, 0x31, 0x3a, 0x03, 0x34, 0x15, 0x06, 0x3f},
+            {0x08, 0x31, 0x3a, 0x23, 0x34, 0x15, 0x06, 0x3f},
+            {0x08, 0x31, 0x3a, 0x23, 0x34, 0x15, 0x06, 0x3f},
+            {0x08, 0x31, 0x3a, 0x23, 0x34, 0x15, 0x06, 0x3f},
+            {0x08, 0x29, 0x3a, 0x23, 0x34, 0x0d, 0x06, 0x3f},
+            {0x08, 0x29, 0x1a, 0x23, 0x34, 0x0d, 0x26, 0x3f},
+            {0x08, 0x29, 0x1a, 0x23, 0x34, 0x0d, 0x26, 0x3f},
+            {0x08, 0x29, 0x1a, 0x23, 0x34, 0x0d, 0x26, 0x3f},
+            {0x08, 0x29, 0x0a, 0x23, 0x2c, 0x0d, 0x26, 0x3f},
+            {0x08, 0x29, 0x0a, 0x23, 0x2c, 0x05, 0x26, 0x1f},
+            {0x08, 0x29, 0x0a, 0x23, 0x2c, 0x05, 0x26, 0x1f},
+            {0x08, 0x29, 0x0a, 0x2b, 0x2c, 0x05, 0x26, 0x0f},
+            {0x08, 0x29, 0x0a, 0x2b, 0x2c, 0x05, 0x26, 0x0f},
+            {0x00, 0x29, 0x0a, 0x2b, 0x3c, 0x05, 0x26, 0x0f},
+            {0x00, 0x29, 0x0a, 0x2b, 0x3c, 0x05, 0x26, 0x0f},
+            {0x20, 0x29, 0x0a, 0x2b, 0x3c, 0x05, 0x2e, 0x0f},
+            {0x20, 0x29, 0x0a, 0x2b, 0x3c, 0x05, 0x2e, 0x0f},
+            {0x20, 0x39, 0x0a, 0x33, 0x3c, 0x05, 0x2e, 0x0f},
+            {0x20, 0x39, 0x0a, 0x33, 0x3c, 0x05, 0x2e, 0x0f},
+            {0x20, 0x39, 0x0a, 0x33, 0x3c, 0x25, 0x2e, 0x0f},
+            {0x20, 0x19, 0x0a, 0x33, 0x3c, 0x25, 0x2e, 0x0f},
+            {0x20, 0x19, 0x12, 0x33, 0x3c, 0x25, 0x36, 0x0f},
+            {0x20, 0x19, 0x12, 0x33, 0x3c, 0x25, 0x36, 0x0f},
+            {0x20, 0x19, 0x12, 0x33, 0x3c, 0x25, 0x36, 0x0f},
+            {0x20, 0x19, 0x12, 0x33, 0x1c, 0x25, 0x36, 0x0f},
+            {0x20, 0x19, 0x12, 0x33, 0x1c, 0x25, 0x36, 0x17},
+            {0x20, 0x19, 0x12, 0x33, 0x1c, 0x25, 0x36, 0x17},
+            {0x20, 0x19, 0x12, 0x33, 0x1c, 0x25, 0x36, 0x17},
+            {0x20, 0x19, 0x12, 0x33, 0x1c, 0x25, 0x36, 0x17},
+            {0x20, 0x19, 0x12, 0x33, 0x1c, 0x25, 0x36, 0x17}
+        },
+        {
+            {0x10, 0x31, 0x3a, 0x03, 0x34, 0x15, 0x06, 0x3f},
+            {0x10, 0x31, 0x3a, 0x03, 0x34, 0x15, 0x06, 0x3f},
+            {0x10, 0x31, 0x3a, 0x03, 0x34, 0x15, 0x06, 0x3f},
+            {0x10, 0x31, 0x3a, 0x03, 0x34, 0x15, 0x06, 0x3f},
+            {0x10, 0x31, 0x3a, 0x03, 0x34, 0x15, 0x06, 0x3f},
+            {0x10, 0x31, 0x3a, 0x03, 0x34, 0x15, 0x06, 0x3f},
+            {0x10, 0x31, 0x3a, 0x23, 0x34, 0x15, 0x06, 0x3f},
+            {0x08, 0x31, 0x3a, 0x23, 0x34, 0x15, 0x06, 0x3f},
+            {0x08, 0x31, 0x3a, 0x23, 0x34, 0x15, 0x06, 0x3f},
+            {0x08, 0x31, 0x3a, 0x23, 0x34, 0x15, 0x06, 0x3f},
+            {0x08, 0x31, 0x1a, 0x23, 0x34, 0x15, 0x26, 0x3f},
+            {0x08, 0x29, 0x1a, 0x23, 0x34, 0x0d, 0x26, 0x3f},
+            {0x08, 0x29, 0x1a, 0x23, 0x34, 0x0d, 0x26, 0x3f},
+            {0x08, 0x29, 0x1a, 0x23, 0x34, 0x0d, 0x26, 0x3f},
+            {0x08, 0x29, 0x0a, 0x23, 0x2c, 0x05, 0x26, 0x1f},
+            {0x08, 0x29, 0x0a, 0x23, 0x2c, 0x05, 0x26, 0x1f},
+            {0x08, 0x29, 0x0a, 0x23, 0x2c, 0x05, 0x26, 0x1f},
+            {0x08, 0x29, 0x0a, 0x23, 0x2c, 0x05, 0x26, 0x1f},
+            {0x00, 0x29, 0x0a, 0x2b, 0x3c, 0x05, 0x26, 0x0f},
+            {0x00, 0x29, 0x0a, 0x2b, 0x3c, 0x05, 0x26, 0x0f},
+            {0x00, 0x29, 0x0a, 0x2b, 0x3c, 0x05, 0x26, 0x0f},
+            {0x00, 0x29, 0x0a, 0x2b, 0x3c, 0x05, 0x26, 0x0f},
+            {0x20, 0x39, 0x0a, 0x33, 0x3c, 0x05, 0x2e, 0x0f},
+            {0x20, 0x39, 0x0a, 0x33, 0x3c, 0x05, 0x2e, 0x0f},
+            {0x20, 0x39, 0x0a, 0x33, 0x3c, 0x05, 0x2e, 0x0f},
+            {0x20, 0x39, 0x0a, 0x33, 0x3c, 0x05, 0x2e, 0x0f},
+            {0x20, 0x39, 0x12, 0x33, 0x3c, 0x25, 0x36, 0x0f},
+            {0x20, 0x19, 0x12, 0x33, 0x3c, 0x25, 0x36, 0x0f},
+            {0x20, 0x19, 0x12, 0x33, 0x3c, 0x25, 0x36, 0x0f},
+            {0x20, 0x19, 0x12, 0x33, 0x3c, 0x25, 0x36, 0x0f},
+            {0x20, 0x19, 0x12, 0x33, 0x3c, 0x25, 0x36, 0x17},
+            {0x20, 0x19, 0x12, 0x33, 0x1c, 0x25, 0x36, 0x17},
+            {0x20, 0x19, 0x12, 0x33, 0x1c, 0x25, 0x36, 0x17},
+            {0x20, 0x19, 0x12, 0x33, 0x1c, 0x25, 0x36, 0x17},
+            {0x20, 0x19, 0x12, 0x33, 0x1c, 0x25, 0x36, 0x17},
+            {0x20, 0x19, 0x12, 0x33, 0x1c, 0x25, 0x36, 0x17}
+        },
+        {
+            {0x10, 0x31, 0x3a, 0x03, 0x34, 0x15, 0x06, 0x3f},
+            {0x10, 0x31, 0x3a, 0x03, 0x34, 0x15, 0x06, 0x3f},
+            {0x10, 0x31, 0x3a, 0x03, 0x34, 0x15, 0x06, 0x3f},
+            {0x10, 0x31, 0x3a, 0x03, 0x34, 0x15, 0x06, 0x3f},
+            {0x10, 0x31, 0x3a, 0x03, 0x34, 0x15, 0x06, 0x3f},
+            {0x10, 0x31, 0x3a, 0x23, 0x34, 0x15, 0x06, 0x3f},
+            {0x10, 0x31, 0x3a, 0x23, 0x34, 0x15, 0x06, 0x3f},
+            {0x10, 0x31, 0x3a, 0x23, 0x34, 0x15, 0x06, 0x3f},
+            {0x08, 0x31, 0x3a, 0x23, 0x34, 0x15, 0x06, 0x3f},
+            {0x08, 0x31, 0x1a, 0x23, 0x34, 0x15, 0x26, 0x3f},
+            {0x08, 0x31, 0x1a, 0x23, 0x34, 0x15, 0x26, 0x3f},
+            {0x08, 0x31, 0x1a, 0x23, 0x34, 0x15, 0x26, 0x3f},
+            {0x08, 0x29, 0x1a, 0x23, 0x34, 0x0d, 0x26, 0x3f},
+            {0x08, 0x29, 0x1a, 0x23, 0x34, 0x05, 0x26, 0x1f},
+            {0x08, 0x29, 0x1a, 0x23, 0x34, 0x05, 0x26, 0x1f},
+            {0x08, 0x29, 0x0a, 0x23, 0x2c, 0x05, 0x26, 0x1f},
+            {0x08, 0x29, 0x0a, 0x23, 0x2c, 0x05, 0x26, 0x1f},
+            {0x00, 0x29, 0x0a, 0x23, 0x3c, 0x05, 0x26, 0x1f},
+            {0x00, 0x29, 0x0a, 0x23, 0x3c, 0x05, 0x26, 0x1f},
+            {0x00, 0x29, 0x0a, 0x2b, 0x3c, 0x05, 0x26, 0x0f},
+            {0x00, 0x29, 0x0a, 0x2b, 0x3c, 0x05, 0x26, 0x0f},
+            {0x00, 0x39, 0x0a, 0x33, 0x3c, 0x05, 0x26, 0x0f},
+            {0x00, 0x39, 0x0a, 0x33, 0x3c, 0x05, 0x26, 0x0f},
+            {0x20, 0x39, 0x0a, 0x33, 0x3c, 0x05, 0x2e, 0x0f},
+            {0x20, 0x39, 0x0a, 0x33, 0x3c, 0x05, 0x2e, 0x0f},
+            {0x20, 0x39, 0x12, 0x33, 0x3c, 0x05, 0x36, 0x0f},
+            {0x20, 0x39, 0x12, 0x33, 0x3c, 0x05, 0x36, 0x0f},
+            {0x20, 0x39, 0x12, 0x33, 0x3c, 0x25, 0x36, 0x0f},
+            {0x20, 0x19, 0x12, 0x33, 0x3c, 0x25, 0x36, 0x0f},
+            {0x20, 0x19, 0x12, 0x33, 0x3c, 0x25, 0x36, 0x17},
+            {0x20, 0x19, 0x12, 0x33, 0x3c, 0x25, 0x36, 0x17},
+            {0x20, 0x19, 0x12, 0x33, 0x3c, 0x25, 0x36, 0x17},
+            {0x20, 0x19, 0x12, 0x33, 0x1c, 0x25, 0x36, 0x17},
+            {0x20, 0x19, 0x12, 0x33, 0x1c, 0x25, 0x36, 0x17},
+            {0x20, 0x19, 0x12, 0x33, 0x1c, 0x25, 0x36, 0x17},
+            {0x20, 0x19, 0x12, 0x33, 0x1c, 0x25, 0x36, 0x17}
+        },
+        {
+            {0x10, 0x31, 0x3a, 0x03, 0x34, 0x15, 0x06, 0x3f},
+            {0x10, 0x31, 0x3a, 0x03, 0x34, 0x15, 0x06, 0x3f},
+            {0x10, 0x31, 0x3a, 0x03, 0x34, 0x15, 0x06, 0x3f},
+            {0x10, 0x31, 0x3a, 0x03, 0x34, 0x15, 0x06, 0x3f},
+            {0x10, 0x31, 0x3a, 0x23, 0x34, 0x15, 0x06, 0x3f},
+            {0x10, 0x31, 0x3a, 0x23, 0x34, 0x15, 0x06, 0x3f},
+            {0x10, 0x31, 0x3a, 0x23, 0x34, 0x15, 0x06, 0x3f},
+            {0x10, 0x31, 0x3a, 0x23, 0x34, 0x15, 0x06, 0x3f},
+            {0x10, 0x31, 0x1a, 0x23, 0x34, 0x15, 0x26, 0x3f},
+            {0x08, 0x31, 0x1a, 0x23, 0x34, 0x15, 0x26, 0x3f},
+            {0x08, 0x31, 0x1a, 0x23, 0x34, 0x15, 0x26, 0x3f},
+            {0x08, 0x31, 0x1a, 0x23, 0x34, 0x15, 0x26, 0x3f},
+            {0x08, 0x31, 0x1a, 0x23, 0x34, 0x05, 0x26, 0x1f},
+            {0x08, 0x29, 0x1a, 0x23, 0x34, 0x05, 0x26, 0x1f},
+            {0x08, 0x29, 0x1a, 0x23, 0x34, 0x05, 0x26, 0x1f},
+            {0x08, 0x29, 0x1a, 0x23, 0x34, 0x05, 0x26, 0x1f},
+            {0x00, 0x29, 0x0a, 0x23, 0x3c, 0x05, 0x26, 0x1f},
+            {0x00, 0x29, 0x0a, 0x23, 0x3c, 0x05, 0x26, 0x1f},
+            {0x00, 0x29, 0x0a, 0x23, 0x3c, 0x05, 0x26, 0x1f},
+            {0x00, 0x29, 0x0a, 0x23, 0x3c, 0x05, 0x26, 0x1f},
+            {0x00, 0x39, 0x0a, 0x33, 0x3c, 0x05, 0x26, 0x0f},
+            {0x00, 0x39, 0x0a, 0x33, 0x3c, 0x05, 0x26, 0x0f},
+            {0x00, 0x39, 0x0a, 0x33, 0x3c, 0x05, 0x26, 0x0f},
+            {0x00, 0x39, 0x0a, 0x33, 0x3c, 0x05, 0x26, 0x0f},
+            {0x20, 0x39, 0x12, 0x33, 0x3c, 0x05, 0x36, 0x0f},
+            {0x20, 0x39, 0x12, 0x33, 0x3c, 0x05, 0x36, 0x0f},
+            {0x20, 0x39, 0x12, 0x33, 0x3c, 0x05, 0x36, 0x0f},
+            {0x20, 0x39, 0x12, 0x33, 0x3c, 0x05, 0x36, 0x0f},
+            {0x20, 0x39, 0x12, 0x33, 0x3c, 0x25, 0x36, 0x17},
+            {0x20, 0x19, 0x12, 0x33, 0x3c, 0x25, 0x36, 0x17},
+            {0x20, 0x19, 0x12, 0x33, 0x3c, 0x25, 0x36, 0x17},
+            {0x20, 0x19, 0x12, 0x33, 0x3c, 0x25, 0x36, 0x17},
+            {0x20, 0x19, 0x12, 0x33, 0x3c, 0x25, 0x36, 0x17},
+            {0x20, 0x19, 0x12, 0x33, 0x1c, 0x25, 0x36, 0x17},
+            {0x20, 0x19, 0x12, 0x33, 0x1c, 0x25, 0x36, 0x17},
+            {0x20, 0x19, 0x12, 0x33, 0x1c, 0x25, 0x36, 0x17}
+        },
+        {
+            {0x10, 0x31, 0x3a, 0x03, 0x34, 0x15, 0x06, 0x3f},
+            {0x10, 0x31, 0x3a, 0x03, 0x34, 0x15, 0x06, 0x3f},
+            {0x10, 0x31, 0x3a, 0x03, 0x34, 0x15, 0x06, 0x3f},
+            {0x10, 0x31, 0x3a, 0x23, 0x34, 0x15, 0x06, 0x3f},
+            {0x10, 0x31, 0x3a, 0x23, 0x34, 0x15, 0x06, 0x3f},
+            {0x10, 0x31, 0x3a, 0x23, 0x34, 0x15, 0x06, 0x3f},
+            {0x10, 0x31, 0x3a, 0x23, 0x34, 0x15, 0x06, 0x3f},
+            {0x10, 0x31, 0x1a, 0x23, 0x34, 0x15, 0x26, 0x3f},
+            {0x10, 0x31, 0x1a, 0x23, 0x34, 0x15, 0x26, 0x3f},
+            {0x10, 0x31, 0x1a, 0x23, 0x34, 0x15, 0x26, 0x3f},
+            {0x08, 0x31, 0x1a, 0x23, 0x34, 0x15, 0x26, 0x3f},
+            {0x08, 0x31, 0x1a, 0x23, 0x34, 0x15, 0x26, 0x1f},
+            {0x08, 0x31, 0x1a, 0x23, 0x34, 0x05, 0x26, 0x1f},
+            {0x08, 0x31, 0x1a, 0x23, 0x34, 0x05, 0x26, 0x1f},
+            {0x08, 0x29, 0x1a, 0x23, 0x34, 0x05, 0x26, 0x1f},
+            {0x00, 0x29, 0x1a, 0x23, 0x34, 0x05, 0x26, 0x1f},
+            {0x00, 0x29, 0x1a, 0x23, 0x3c, 0x05, 0x26, 0x1f},
+            {0x00, 0x29, 0x0a, 0x23, 0x3c, 0x05, 0x26, 0x1f},
+            {0x00, 0x29, 0x0a, 0x23, 0x3c, 0x05, 0x26, 0x1f},
+            {0x00, 0x39, 0x0a, 0x23, 0x3c, 0x05, 0x26, 0x1f},
+            {0x00, 0x39, 0x0a, 0x33, 0x3c, 0x05, 0x26, 0x1f},
+            {0x00, 0x39, 0x0a, 0x33, 0x3c, 0x05, 0x26, 0x0f},
+            {0x00, 0x39, 0x0a, 0x33, 0x3c, 0x05, 0x26, 0x0f},
+            {0x00, 0x39, 0x12, 0x33, 0x3c, 0x05, 0x26, 0x0f},
+            {0x00, 0x39, 0x12, 0x33, 0x3c, 0x05, 0x36, 0x0f},
+            {0x20, 0x39, 0x12, 0x33, 0x3c, 0x05, 0x36, 0x0f},
+            {0x20, 0x39, 0x12, 0x33, 0x3c, 0x05, 0x36, 0x0f},
+            {0x20, 0x39, 0x12, 0x33, 0x3c, 0x05, 0x36, 0x17},
+            {0x20, 0x39, 0x12, 0x33, 0x3c, 0x05, 0x36, 0x17},
+            {0x20, 0x39, 0x12, 0x33, 0x3c, 0x25, 0x36, 0x17},
+            {0x20, 0x19, 0x12, 0x33, 0x3c, 0x25, 0x36, 0x17},
+            {0x20, 0x19, 0x12, 0x33, 0x3c, 0x25, 0x36, 0x17},
+            {0x20, 0x19, 0x12, 0x33, 0x3c, 0x25, 0x36, 0x17},
+            {0x20, 0x19, 0x12, 0x33, 0x3c, 0x25, 0x36, 0x17},
+            {0x20, 0x19, 0x12, 0x33, 0x1c, 0x25, 0x36, 0x17},
+            {0x20, 0x19, 0x12, 0x33, 0x1c, 0x25, 0x36, 0x17}
+        },
+        {
+            {0x10, 0x31, 0x3a, 0x03, 0x34, 0x15, 0x06, 0x3f},
+            {0x10, 0x31, 0x3a, 0x03, 0x34, 0x15, 0x06, 0x3f},
+            {0x10, 0x31, 0x3a, 0x23, 0x34, 0x15, 0x06, 0x3f},
+            {0x10, 0x31, 0x3a, 0x23, 0x34, 0x15, 0x06, 0x3f},
+            {0x10, 0x31, 0x3a, 0x23, 0x34, 0x15, 0x06, 0x3f},
+            {0x10, 0x31, 0x3a, 0x23, 0x34, 0x15, 0x06, 0x3f},
+            {0x10, 0x31, 0x1a, 0x23, 0x34, 0x15, 0x26, 0x3f},
+            {0x10, 0x31, 0x1a, 0x23, 0x34, 0x15, 0x26, 0x3f},
+            {0x10, 0x31, 0x1a, 0x23, 0x34, 0x15, 0x26, 0x3f},
+            {0x10, 0x31, 0x1a, 0x23, 0x34, 0x15, 0x26, 0x3f},
+            {0x10, 0x31, 0x1a, 0x23, 0x34, 0x15, 0x26, 0x1f},
+            {0x08, 0x31, 0x1a, 0x23, 0x34, 0x15, 0x26, 0x1f},
+            {0x08, 0x31, 0x1a, 0x23, 0x34, 0x05, 0x26, 0x1f},
+            {0x08, 0x31, 0x1a, 0x23, 0x34, 0x05, 0x26, 0x1f},
+            {0x00, 0x29, 0x1a, 0x23, 0x34, 0x05, 0x26, 0x1f},
+            {0x00, 0x29, 0x1a, 0x23, 0x34, 0x05, 0x26, 0x1f},
+            {0x00, 0x29, 0x1a, 0x23, 0x3c, 0x05, 0x26, 0x1f},
+            {0x00, 0x29, 0x1a, 0x23, 0x3c, 0x05, 0x26, 0x1f},
+            {0x00, 0x39, 0x0a, 0x23, 0x3c, 0x05, 0x26, 0x1f},
+            {0x00, 0x39, 0x0a, 0x23, 0x3c, 0x05, 0x26, 0x1f},
+            {0x00, 0x39, 0x0a, 0x33, 0x3c, 0x05, 0x26, 0x1f},
+            {0x00, 0x39, 0x0a, 0x33, 0x3c, 0x05, 0x26, 0x1f},
+            {0x00, 0x39, 0x12, 0x33, 0x3c, 0x05, 0x26, 0x1f},
+            {0x00, 0x39, 0x12, 0x33, 0x3c, 0x05, 0x26, 0x0f},
+            {0x00, 0x39, 0x12, 0x33, 0x3c, 0x05, 0x36, 0x0f},
+            {0x00, 0x39, 0x12, 0x33, 0x3c, 0x05, 0x36, 0x0f},
+            {0x20, 0x39, 0x12, 0x33, 0x3c, 0x05, 0x36, 0x17},
+            {0x20, 0x39, 0x12, 0x33, 0x3c, 0x05, 0x36, 0x17},
+            {0x20, 0x39, 0x12, 0x33, 0x3c, 0x05, 0x36, 0x17},
+            {0x20, 0x39, 0x12, 0x33, 0x3c, 0x05, 0x36, 0x17},
+            {0x20, 0x39, 0x12, 0x33, 0x3c, 0x25, 0x36, 0x17},
+            {0x20, 0x19, 0x12, 0x33, 0x3c, 0x25, 0x36, 0x17},
+            {0x20, 0x19, 0x12, 0x33, 0x3c, 0x25, 0x36, 0x17},
+            {0x20, 0x19, 0x12, 0x33, 0x3c, 0x25, 0x36, 0x17},
+            {0x20, 0x19, 0x12, 0x33, 0x1c, 0x25, 0x36, 0x17},
+            {0x20, 0x19, 0x12, 0x33, 0x1c, 0x25, 0x36, 0x17}
+        },
+        {
+            {0x10, 0x31, 0x3a, 0x03, 0x34, 0x15, 0x06, 0x3f},
+            {0x10, 0x31, 0x3a, 0x23, 0x34, 0x15, 0x06, 0x3f},
+            {0x10, 0x31, 0x3a, 0x23, 0x34, 0x15, 0x06, 0x3f},
+            {0x10, 0x31, 0x3a, 0x23, 0x34, 0x15, 0x06, 0x3f},
+            {0x10, 0x31, 0x3a, 0x23, 0x34, 0x15, 0x06, 0x3f},
+            {0x10, 0x31, 0x1a, 0x23, 0x34, 0x15, 0x26, 0x3f},
+            {0x10, 0x31, 0x1a, 0x23, 0x34, 0x15, 0x26, 0x3f},
+            {0x10, 0x31, 0x1a, 0x23, 0x34, 0x15, 0x26, 0x3f},
+            {0x10, 0x31, 0x1a, 0x23, 0x34, 0x15, 0x26, 0x3f},
+            {0x10, 0x31, 0x1a, 0x23, 0x34, 0x15, 0x26, 0x1f},
+            {0x10, 0x31, 0x1a, 0x23, 0x34, 0x15, 0x26, 0x1f},
+            {0x10, 0x31, 0x1a, 0x23, 0x34, 0x15, 0x26, 0x1f},
+            {0x08, 0x31, 0x1a, 0x23, 0x34, 0x05, 0x26, 0x1f},
+            {0x00, 0x31, 0x1a, 0x23, 0x34, 0x05, 0x26, 0x1f},
+            {0x00, 0x31, 0x1a, 0x23, 0x34, 0x05, 0x26, 0x1f},
+            {0x00, 0x29, 0x1a, 0x23, 0x34, 0x05, 0x26, 0x1f},
+            {0x00, 0x29, 0x1a, 0x23, 0x3c, 0x05, 0x26, 0x1f},
+            {0x00, 0x39, 0x1a, 0x23, 0x3c, 0x05, 0x26, 0x1f},
+            {0x00, 0x39, 0x1a, 0x23, 0x3c, 0x05, 0x26, 0x1f},
+            {0x00, 0x39, 0x0a, 0x23, 0x3c, 0x05, 0x26, 0x1f},
+            {0x00, 0x39, 0x0a, 0x33, 0x3c, 0x05, 0x26, 0x1f},
+            {0x00, 0x39, 0x12, 0x33, 0x3c, 0x05, 0x26, 0x1f},
+            {0x00, 0x39, 0x12, 0x33, 0x3c, 0x05, 0x26, 0x1f},
+            {0x00, 0x39, 0x12, 0x33, 0x3c, 0x05, 0x26, 0x1f},
+            {0x00, 0x39, 0x12, 0x33, 0x3c, 0x05, 0x36, 0x0f},
+            {0x00, 0x39, 0x12, 0x33, 0x3c, 0x05, 0x36, 0x17},
+            {0x00, 0x39, 0x12, 0x33, 0x3c, 0x05, 0x36, 0x17},
+            {0x20, 0x39, 0x12, 0x33, 0x3c, 0x05, 0x36, 0x17},
+            {0x20, 0x39, 0x12, 0x33, 0x3c, 0x05, 0x36, 0x17},
+            {0x20, 0x39, 0x12, 0x33, 0x3c, 0x05, 0x36, 0x17},
+            {0x20, 0x39, 0x12, 0x33, 0x3c, 0x05, 0x36, 0x17},
+            {0x20, 0x39, 0x12, 0x33, 0x3c, 0x25, 0x36, 0x17},
+            {0x20, 0x19, 0x12, 0x33, 0x3c, 0x25, 0x36, 0x17},
+            {0x20, 0x19, 0x12, 0x33, 0x3c, 0x25, 0x36, 0x17},
+            {0x20, 0x19, 0x12, 0x33, 0x3c, 0x25, 0x36, 0x17},
+            {0x20, 0x19, 0x12, 0x33, 0x1c, 0x25, 0x36, 0x17}
+        },
+        {
+            {0x10, 0x31, 0x3a, 0x23, 0x34, 0x15, 0x06, 0x3f},
+            {0x10, 0x31, 0x3a, 0x23, 0x34, 0x15, 0x06, 0x3f},
+            {0x10, 0x31, 0x3a, 0x23, 0x34, 0x15, 0x06, 0x3f},
+            {0x10, 0x31, 0x3a, 0x23, 0x34, 0x15, 0x06, 0x3f},
+            {0x10, 0x31, 0x1a, 0x23, 0x34, 0x15, 0x26, 0x3f},
+            {0x10, 0x31, 0x1a, 0x23, 0x34, 0x15, 0x26, 0x3f},
+            {0x10, 0x31, 0x1a, 0x23, 0x34, 0x15, 0x26, 0x3f},
+            {0x10, 0x31, 0x1a, 0x23, 0x34, 0x15, 0x26, 0x3f},
+            {0x10, 0x31, 0x1a, 0x23, 0x34, 0x15, 0x26, 0x1f},
+            {0x10, 0x31, 0x1a, 0x23, 0x34, 0x15, 0x26, 0x1f},
+            {0x10, 0x31, 0x1a, 0x23, 0x34, 0x15, 0x26, 0x1f},
+            {0x10, 0x31, 0x1a, 0x23, 0x34, 0x15, 0x26, 0x1f},
+            {0x00, 0x31, 0x1a, 0x23, 0x34, 0x05, 0x26, 0x1f},
+            {0x00, 0x31, 0x1a, 0x23, 0x34, 0x05, 0x26, 0x1f},
+            {0x00, 0x31, 0x1a, 0x23, 0x34, 0x05, 0x26, 0x1f},
+            {0x00, 0x31, 0x1a, 0x23, 0x34, 0x05, 0x26, 0x1f},
+            {0x00, 0x39, 0x1a, 0x23, 0x3c, 0x05, 0x26, 0x1f},
+            {0x00, 0x39, 0x1a, 0x23, 0x3c, 0x05, 0x26, 0x1f},
+            {0x00, 0x39, 0x1a, 0x23, 0x3c, 0x05, 0x26, 0x1f},
+            {0x00, 0x39, 0x1a, 0x23, 0x3c, 0x05, 0x26, 0x1f},
+            {0x00, 0x39, 0x12, 0x33, 0x3c, 0x05, 0x26, 0x1f},
+            {0x00, 0x39, 0x12, 0x33, 0x3c, 0x05, 0x26, 0x1f},
+            {0x00, 0x39, 0x12, 0x33, 0x3c, 0x05, 0x26, 0x1f},
+            {0x00, 0x39, 0x12, 0x33, 0x3c, 0x05, 0x26, 0x1f},
+            {0x00, 0x39, 0x12, 0x33, 0x3c, 0x05, 0x36, 0x17},
+            {0x00, 0x39, 0x12, 0x33, 0x3c, 0x05, 0x36, 0x17},
+            {0x00, 0x39, 0x12, 0x33, 0x3c, 0x05, 0x36, 0x17},
+            {0x00, 0x39, 0x12, 0x33, 0x3c, 0x05, 0x36, 0x17},
+            {0x20, 0x39, 0x12, 0x33, 0x3c, 0x05, 0x36, 0x17},
+            {0x20, 0x39, 0x12, 0x33, 0x3c, 0x05, 0x36, 0x17},
+            {0x20, 0x39, 0x12, 0x33, 0x3c, 0x05, 0x36, 0x17},
+            {0x20, 0x39, 0x12, 0x33, 0x3c, 0x05, 0x36, 0x17},
+            {0x20, 0x39, 0x12, 0x33, 0x3c, 0x25, 0x36, 0x17},
+            {0x20, 0x19, 0x12, 0x33, 0x3c, 0x25, 0x36, 0x17},
+            {0x20, 0x19, 0x12, 0x33, 0x3c, 0x25, 0x36, 0x17},
+            {0x20, 0x19, 0x12, 0x33, 0x3c, 0x25, 0x36, 0x17}
+        },
+        {
+            {0x10, 0x31, 0x3a, 0x23, 0x34, 0x15, 0x06, 0x3f},
+            {0x10, 0x31, 0x3a, 0x23, 0x34, 0x15, 0x06, 0x3f},
+            {0x10, 0x31, 0x3a, 0x23, 0x34, 0x15, 0x06, 0x3f},
+            {0x10, 0x31, 0x1a, 0x23, 0x34, 0x15, 0x26, 0x3f},
+            {0x10, 0x31, 0x1a, 0x23, 0x34, 0x15, 0x26, 0x3f},
+            {0x10, 0x31, 0x1a, 0x23, 0x34, 0x15, 0x26, 0x3f},
+            {0x10, 0x31, 0x1a, 0x23, 0x34, 0x15, 0x26, 0x3f},
+            {0x10, 0x31, 0x1a, 0x23, 0x34, 0x15, 0x26, 0x1f},
+            {0x10, 0x31, 0x1a, 0x23, 0x34, 0x15, 0x26, 0x1f},
+            {0x10, 0x31, 0x1a, 0x23, 0x34, 0x15, 0x26, 0x1f},
+            {0x10, 0x31, 0x1a, 0x23, 0x34, 0x15, 0x26, 0x1f},
+            {0x10, 0x31, 0x1a, 0x23, 0x34, 0x15, 0x26, 0x1f},
+            {0x00, 0x31, 0x1a, 0x23, 0x34, 0x05, 0x26, 0x1f},
+            {0x00, 0x31, 0x1a, 0x23, 0x34, 0x05, 0x26, 0x1f},
+            {0x00, 0x31, 0x1a, 0x23, 0x34, 0x05, 0x26, 0x1f},
+            {0x00, 0x31, 0x1a, 0x23, 0x34, 0x05, 0x26, 0x1f},
+            {0x00, 0x39, 0x1a, 0x23, 0x3c, 0x05, 0x26, 0x1f},
+            {0x00, 0x39, 0x1a, 0x23, 0x3c, 0x05, 0x26, 0x1f},
+            {0x00, 0x39, 0x1a, 0x23, 0x3c, 0x05, 0x26, 0x1f},
+            {0x00, 0x39, 0x1a, 0x23, 0x3c, 0x05, 0x26, 0x1f},
+            {0x00, 0x39, 0x12, 0x33, 0x3c, 0x05, 0x26, 0x1f},
+            {0x00, 0x39, 0x12, 0x33, 0x3c, 0x05, 0x26, 0x1f},
+            {0x00, 0x39, 0x12, 0x33, 0x3c, 0x05, 0x26, 0x1f},
+            {0x00, 0x39, 0x12, 0x33, 0x3c, 0x05, 0x26, 0x1f},
+            {0x00, 0x39, 0x12, 0x33, 0x3c, 0x05, 0x36, 0x17},
+            {0x00, 0x39, 0x12, 0x33, 0x3c, 0x05, 0x36, 0x17},
+            {0x00, 0x39, 0x12, 0x33, 0x3c, 0x05, 0x36, 0x17},
+            {0x00, 0x39, 0x12, 0x33, 0x3c, 0x05, 0x36, 0x17},
+            {0x00, 0x39, 0x12, 0x33, 0x3c, 0x05, 0x36, 0x17},
+            {0x20, 0x39, 0x12, 0x33, 0x3c, 0x05, 0x36, 0x17},
+            {0x20, 0x39, 0x12, 0x33, 0x3c, 0x05, 0x36, 0x17},
+            {0x20, 0x39, 0x12, 0x33, 0x3c, 0x05, 0x36, 0x17},
+            {0x20, 0x39, 0x12, 0x33, 0x3c, 0x05, 0x36, 0x17},
+            {0x20, 0x39, 0x12, 0x33, 0x3c, 0x25, 0x36, 0x17},
+            {0x20, 0x19, 0x12, 0x33, 0x3c, 0x25, 0x36, 0x17},
+            {0x20, 0x19, 0x12, 0x33, 0x3c, 0x25, 0x36, 0x17}
+        },
+        {
+            {0x10, 0x31, 0x3a, 0x23, 0x34, 0x15, 0x06, 0x3f},
+            {0x10, 0x31, 0x3a, 0x23, 0x34, 0x15, 0x06, 0x3f},
+            {0x10, 0x31, 0x1a, 0x23, 0x34, 0x15, 0x26, 0x3f},
+            {0x10, 0x31, 0x1a, 0x23, 0x34, 0x15, 0x26, 0x3f},
+            {0x10, 0x31, 0x1a, 0x23, 0x34, 0x15, 0x26, 0x3f},
+            {0x10, 0x31, 0x1a, 0x23, 0x34, 0x15, 0x26, 0x3f},
+            {0x10, 0x31, 0x1a, 0x23, 0x34, 0x15, 0x26, 0x1f},
+            {0x10, 0x31, 0x1a, 0x23, 0x34, 0x15, 0x26, 0x1f},
+            {0x10, 0x31, 0x1a, 0x23, 0x34, 0x15, 0x26, 0x1f},
+            {0x10, 0x31, 0x1a, 0x23, 0x34, 0x15, 0x26, 0x1f},
+            {0x10, 0x31, 0x1a, 0x23, 0x34, 0x15, 0x26, 0x1f},
+            {0x10, 0x31, 0x1a, 0x23, 0x34, 0x15, 0x26, 0x1f},
+            {0x00, 0x31, 0x1a, 0x23, 0x34, 0x05, 0x26, 0x1f},
+            {0x00, 0x31, 0x1a, 0x23, 0x34, 0x05, 0x26, 0x1f},
+            {0x00, 0x31, 0x1a, 0x23, 0x34, 0x05, 0x26, 0x1f},
+            {0x00, 0x31, 0x1a, 0x23, 0x34, 0x05, 0x26, 0x1f},
+            {0x00, 0x39, 0x1a, 0x23, 0x3c, 0x05, 0x26, 0x1f},
+            {0x00, 0x39, 0x1a, 0x23, 0x3c, 0x05, 0x26, 0x1f},
+            {0x00, 0x39, 0x1a, 0x23, 0x3c, 0x05, 0x26, 0x1f},
+            {0x00, 0x39, 0x1a, 0x23, 0x3c, 0x05, 0x26, 0x1f},
+            {0x00, 0x39, 0x12, 0x33, 0x3c, 0x05, 0x26, 0x1f},
+            {0x00, 0x39, 0x12, 0x33, 0x3c, 0x05, 0x26, 0x1f},
+            {0x00, 0x39, 0x12, 0x33, 0x3c, 0x05, 0x26, 0x1f},
+            {0x00, 0x39, 0x12, 0x33, 0x3c, 0x05, 0x26, 0x1f},
+            {0x00, 0x39, 0x12, 0x33, 0x3c, 0x05, 0x36, 0x17},
+            {0x00, 0x39, 0x12, 0x33, 0x3c, 0x05, 0x36, 0x17},
+            {0x00, 0x39, 0x12, 0x33, 0x3c, 0x05, 0x36, 0x17},
+            {0x00, 0x39, 0x12, 0x33, 0x3c, 0x05, 0x36, 0x17},
+            {0x00, 0x39, 0x12, 0x33, 0x3c, 0x05, 0x36, 0x17},
+            {0x00, 0x39, 0x12, 0x33, 0x3c, 0x05, 0x36, 0x17},
+            {0x00, 0x39, 0x12, 0x33, 0x3c, 0x05, 0x36, 0x17},
+            {0x20, 0x39, 0x12, 0x33, 0x3c, 0x05, 0x36, 0x17},
+            {0x20, 0x39, 0x12, 0x33, 0x3c, 0x05, 0x36, 0x17},
+            {0x20, 0x39, 0x12, 0x33, 0x3c, 0x05, 0x36, 0x17},
+            {0x20, 0x39, 0x12, 0x33, 0x3c, 0x25, 0x36, 0x17},
+            {0x20, 0x19, 0x12, 0x33, 0x3c, 0x25, 0x36, 0x17}
+        },
+        {
+            {0x10, 0x31, 0x3a, 0x23, 0x34, 0x15, 0x06, 0x3f},
+            {0x10, 0x31, 0x1a, 0x23, 0x34, 0x15, 0x26, 0x3f},
+            {0x10, 0x31, 0x1a, 0x23, 0x34, 0x15, 0x26, 0x3f},
+            {0x10, 0x31, 0x1a, 0x23, 0x34, 0x15, 0x26, 0x3f},
+            {0x10, 0x31, 0x1a, 0x23, 0x34, 0x15, 0x26, 0x3f},
+            {0x10, 0x31, 0x1a, 0x23, 0x34, 0x15, 0x26, 0x1f},
+            {0x10, 0x31, 0x1a, 0x23, 0x34, 0x15, 0x26, 0x1f},
+            {0x10, 0x31, 0x1a, 0x23, 0x34, 0x15, 0x26, 0x1f},
+            {0x10, 0x31, 0x1a, 0x23, 0x34, 0x15, 0x26, 0x1f},
+            {0x10, 0x31, 0x1a, 0x23, 0x34, 0x15, 0x26, 0x1f},
+            {0x10, 0x31, 0x1a, 0x23, 0x34, 0x15, 0x26, 0x1f},
+            {0x10, 0x31, 0x1a, 0x23, 0x34, 0x15, 0x26, 0x1f},
+            {0x00, 0x31, 0x1a, 0x23, 0x34, 0x05, 0x26, 0x1f},
+            {0x00, 0x31, 0x1a, 0x23, 0x34, 0x05, 0x26, 0x1f},
+            {0x00, 0x31, 0x1a, 0x23, 0x34, 0x05, 0x26, 0x1f},
+            {0x00, 0x31, 0x1a, 0x23, 0x34, 0x05, 0x26, 0x1f},
+            {0x00, 0x39, 0x1a, 0x23, 0x3c, 0x05, 0x26, 0x1f},
+            {0x00, 0x39, 0x1a, 0x23, 0x3c, 0x05, 0x26, 0x1f},
+            {0x00, 0x39, 0x1a, 0x23, 0x3c, 0x05, 0x26, 0x1f},
+            {0x00, 0x39, 0x1a, 0x23, 0x3c, 0x05, 0x26, 0x1f},
+            {0x00, 0x39, 0x12, 0x33, 0x3c, 0x05, 0x26, 0x1f},
+            {0x00, 0x39, 0x12, 0x33, 0x3c, 0x05, 0x26, 0x1f},
+            {0x00, 0x39, 0x12, 0x33, 0x3c, 0x05, 0x26, 0x1f},
+            {0x00, 0x39, 0x12, 0x33, 0x3c, 0x05, 0x26, 0x1f},
+            {0x00, 0x39, 0x12, 0x33, 0x3c, 0x05, 0x36, 0x17},
+            {0x00, 0x39, 0x12, 0x33, 0x3c, 0x05, 0x36, 0x17},
+            {0x00, 0x39, 0x12, 0x33, 0x3c, 0x05, 0x36, 0x17},
+            {0x00, 0x39, 0x12, 0x33, 0x3c, 0x05, 0x36, 0x17},
+            {0x00, 0x39, 0x12, 0x33, 0x3c, 0x05, 0x36, 0x17},
+            {0x00, 0x39, 0x12, 0x33, 0x3c, 0x05, 0x36, 0x17},
+            {0x00, 0x39, 0x12, 0x33, 0x3c, 0x05, 0x36, 0x17},
+            {0x00, 0x39, 0x12, 0x33, 0x3c, 0x05, 0x36, 0x17},
+            {0x20, 0x39, 0x12, 0x33, 0x3c, 0x05, 0x36, 0x17},
+            {0x20, 0x39, 0x12, 0x33, 0x3c, 0x05, 0x36, 0x17},
+            {0x20, 0x39, 0x12, 0x33, 0x3c, 0x05, 0x36, 0x17},
+            {0x20, 0x39, 0x12, 0x33, 0x3c, 0x25, 0x36, 0x17}
+        }
+    },
+    {   /* 9,600bps map */
+        {
+            {0x10, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x00, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x00, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x00, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x00, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x00, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x00, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x0f},
+            {0x00, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x0f},
+            {0x00, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x0f},
+            {0x00, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x0f},
+            {0x00, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x1e, 0x0f},
+            {0x00, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x1e, 0x0f},
+            {0x00, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x1e, 0x0f},
+            {0x00, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x1e, 0x0f},
+            {0x00, 0x09, 0x12, 0x13, 0x1c, 0x15, 0x1e, 0x0f},
+            {0x00, 0x09, 0x12, 0x13, 0x1c, 0x15, 0x1e, 0x0f},
+            {0x00, 0x09, 0x12, 0x13, 0x1c, 0x15, 0x1e, 0x0f},
+            {0x00, 0x09, 0x12, 0x13, 0x1c, 0x15, 0x1e, 0x0f},
+            {0x00, 0x09, 0x02, 0x13, 0x1c, 0x15, 0x1e, 0x0f},
+            {0x00, 0x09, 0x02, 0x13, 0x1c, 0x15, 0x1e, 0x0f},
+            {0x00, 0x09, 0x02, 0x13, 0x1c, 0x15, 0x1e, 0x0f},
+            {0x00, 0x09, 0x02, 0x13, 0x1c, 0x15, 0x1e, 0x0f},
+            {0x00, 0x09, 0x02, 0x13, 0x1c, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x13, 0x1c, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x13, 0x1c, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x13, 0x1c, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x13, 0x1c, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x13, 0x1c, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x1f}
+        },
+        {
+            {0x10, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x00, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x00, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x00, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x00, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x00, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x0f},
+            {0x00, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x0f},
+            {0x00, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x0f},
+            {0x00, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x0f},
+            {0x00, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x1e, 0x0f},
+            {0x00, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x1e, 0x0f},
+            {0x00, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x1e, 0x0f},
+            {0x00, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x1e, 0x0f},
+            {0x00, 0x09, 0x12, 0x13, 0x1c, 0x15, 0x1e, 0x0f},
+            {0x00, 0x09, 0x12, 0x13, 0x1c, 0x15, 0x1e, 0x0f},
+            {0x00, 0x09, 0x12, 0x13, 0x1c, 0x15, 0x1e, 0x0f},
+            {0x00, 0x09, 0x12, 0x13, 0x1c, 0x15, 0x1e, 0x0f},
+            {0x00, 0x09, 0x02, 0x13, 0x1c, 0x15, 0x1e, 0x0f},
+            {0x00, 0x09, 0x02, 0x13, 0x1c, 0x15, 0x1e, 0x0f},
+            {0x00, 0x09, 0x02, 0x13, 0x1c, 0x15, 0x1e, 0x0f},
+            {0x00, 0x09, 0x02, 0x13, 0x1c, 0x15, 0x1e, 0x0f},
+            {0x00, 0x09, 0x02, 0x13, 0x1c, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x13, 0x1c, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x13, 0x1c, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x13, 0x1c, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x13, 0x1c, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x1f}
+        },
+        {
+            {0x10, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x00, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x00, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x00, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x00, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x0f},
+            {0x00, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x0f},
+            {0x00, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x0f},
+            {0x00, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x0f},
+            {0x00, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x1e, 0x0f},
+            {0x00, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x1e, 0x0f},
+            {0x00, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x1e, 0x0f},
+            {0x00, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x1e, 0x0f},
+            {0x00, 0x09, 0x12, 0x13, 0x1c, 0x15, 0x1e, 0x0f},
+            {0x00, 0x09, 0x12, 0x13, 0x1c, 0x15, 0x1e, 0x0f},
+            {0x00, 0x09, 0x12, 0x13, 0x1c, 0x15, 0x1e, 0x0f},
+            {0x00, 0x09, 0x12, 0x13, 0x1c, 0x15, 0x1e, 0x0f},
+            {0x00, 0x09, 0x02, 0x13, 0x1c, 0x15, 0x1e, 0x0f},
+            {0x00, 0x09, 0x02, 0x13, 0x1c, 0x15, 0x1e, 0x0f},
+            {0x00, 0x09, 0x02, 0x13, 0x1c, 0x15, 0x1e, 0x0f},
+            {0x00, 0x09, 0x02, 0x13, 0x1c, 0x15, 0x1e, 0x0f},
+            {0x00, 0x09, 0x02, 0x13, 0x1c, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x13, 0x1c, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x13, 0x1c, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x13, 0x1c, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x1f}
+        },
+        {
+            {0x10, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x00, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x00, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x00, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x0f},
+            {0x00, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x0f},
+            {0x00, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x0f},
+            {0x00, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x0f},
+            {0x00, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x1e, 0x0f},
+            {0x00, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x1e, 0x0f},
+            {0x00, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x1e, 0x0f},
+            {0x00, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x1e, 0x0f},
+            {0x00, 0x09, 0x12, 0x13, 0x1c, 0x15, 0x1e, 0x0f},
+            {0x00, 0x09, 0x12, 0x13, 0x1c, 0x15, 0x1e, 0x0f},
+            {0x00, 0x09, 0x12, 0x13, 0x1c, 0x15, 0x1e, 0x0f},
+            {0x00, 0x09, 0x12, 0x13, 0x1c, 0x15, 0x1e, 0x0f},
+            {0x00, 0x09, 0x02, 0x13, 0x1c, 0x15, 0x1e, 0x0f},
+            {0x00, 0x09, 0x02, 0x13, 0x1c, 0x15, 0x1e, 0x0f},
+            {0x00, 0x09, 0x02, 0x13, 0x1c, 0x15, 0x1e, 0x0f},
+            {0x00, 0x09, 0x02, 0x13, 0x1c, 0x15, 0x1e, 0x0f},
+            {0x00, 0x09, 0x02, 0x13, 0x1c, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x13, 0x1c, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x13, 0x1c, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x1f}
+        },
+        {
+            {0x10, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x00, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x00, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x0f},
+            {0x00, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x0f},
+            {0x00, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x0f},
+            {0x00, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x0f},
+            {0x00, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x1e, 0x0f},
+            {0x00, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x1e, 0x0f},
+            {0x00, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x1e, 0x0f},
+            {0x00, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x1e, 0x0f},
+            {0x00, 0x09, 0x12, 0x13, 0x1c, 0x15, 0x1e, 0x0f},
+            {0x00, 0x09, 0x12, 0x13, 0x1c, 0x15, 0x1e, 0x0f},
+            {0x00, 0x09, 0x12, 0x13, 0x1c, 0x15, 0x1e, 0x0f},
+            {0x00, 0x09, 0x12, 0x13, 0x1c, 0x15, 0x1e, 0x0f},
+            {0x00, 0x09, 0x02, 0x13, 0x1c, 0x15, 0x1e, 0x0f},
+            {0x00, 0x09, 0x02, 0x13, 0x1c, 0x15, 0x1e, 0x0f},
+            {0x00, 0x09, 0x02, 0x13, 0x1c, 0x15, 0x1e, 0x0f},
+            {0x00, 0x09, 0x02, 0x13, 0x1c, 0x15, 0x1e, 0x0f},
+            {0x00, 0x09, 0x02, 0x13, 0x1c, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x13, 0x1c, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x1f}
+        },
+        {
+            {0x10, 0x01, 0x12, 0x1b, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x00, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x0f},
+            {0x00, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x0f},
+            {0x00, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x0f},
+            {0x00, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x0f},
+            {0x00, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x1e, 0x0f},
+            {0x00, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x1e, 0x0f},
+            {0x00, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x1e, 0x0f},
+            {0x00, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x1e, 0x0f},
+            {0x00, 0x09, 0x12, 0x13, 0x1c, 0x15, 0x1e, 0x0f},
+            {0x00, 0x09, 0x12, 0x13, 0x1c, 0x15, 0x1e, 0x0f},
+            {0x00, 0x09, 0x12, 0x13, 0x1c, 0x15, 0x1e, 0x0f},
+            {0x00, 0x09, 0x12, 0x13, 0x1c, 0x15, 0x1e, 0x0f},
+            {0x00, 0x09, 0x02, 0x13, 0x1c, 0x15, 0x1e, 0x0f},
+            {0x00, 0x09, 0x02, 0x13, 0x1c, 0x15, 0x1e, 0x0f},
+            {0x00, 0x09, 0x02, 0x13, 0x1c, 0x15, 0x1e, 0x0f},
+            {0x00, 0x09, 0x02, 0x13, 0x1c, 0x15, 0x1e, 0x0f},
+            {0x00, 0x09, 0x02, 0x13, 0x1c, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x03, 0x14, 0x15, 0x1e, 0x1f}
+        },
+        {
+            {0x10, 0x01, 0x12, 0x1b, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x1b, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x1b, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x0f},
+            {0x00, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x0f},
+            {0x00, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x0f},
+            {0x00, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x0f},
+            {0x00, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x1e, 0x0f},
+            {0x00, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x1e, 0x0f},
+            {0x00, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x1e, 0x0f},
+            {0x00, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x1e, 0x0f},
+            {0x00, 0x09, 0x12, 0x13, 0x1c, 0x15, 0x1e, 0x0f},
+            {0x00, 0x09, 0x12, 0x13, 0x1c, 0x15, 0x1e, 0x0f},
+            {0x00, 0x09, 0x12, 0x13, 0x1c, 0x15, 0x1e, 0x0f},
+            {0x00, 0x09, 0x12, 0x13, 0x1c, 0x15, 0x1e, 0x0f},
+            {0x00, 0x09, 0x02, 0x13, 0x1c, 0x15, 0x1e, 0x0f},
+            {0x00, 0x09, 0x02, 0x13, 0x1c, 0x15, 0x1e, 0x0f},
+            {0x00, 0x09, 0x02, 0x13, 0x1c, 0x15, 0x1e, 0x0f},
+            {0x00, 0x09, 0x02, 0x13, 0x1c, 0x15, 0x1e, 0x0f},
+            {0x00, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x03, 0x14, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x03, 0x14, 0x15, 0x1e, 0x1f}
+        },
+        {
+            {0x10, 0x01, 0x12, 0x1b, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x1b, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x1b, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x1b, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x0f},
+            {0x10, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x0f},
+            {0x00, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x0f},
+            {0x00, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x0f},
+            {0x00, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x1e, 0x0f},
+            {0x00, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x1e, 0x0f},
+            {0x00, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x1e, 0x0f},
+            {0x00, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x1e, 0x0f},
+            {0x00, 0x09, 0x12, 0x13, 0x1c, 0x15, 0x1e, 0x0f},
+            {0x00, 0x09, 0x12, 0x13, 0x1c, 0x15, 0x1e, 0x0f},
+            {0x00, 0x09, 0x12, 0x13, 0x1c, 0x15, 0x1e, 0x0f},
+            {0x00, 0x09, 0x12, 0x13, 0x1c, 0x15, 0x1e, 0x0f},
+            {0x00, 0x09, 0x02, 0x13, 0x1c, 0x15, 0x1e, 0x0f},
+            {0x00, 0x09, 0x02, 0x13, 0x1c, 0x15, 0x1e, 0x0f},
+            {0x00, 0x09, 0x02, 0x13, 0x1c, 0x15, 0x1e, 0x0f},
+            {0x00, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x0f},
+            {0x00, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x03, 0x14, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x03, 0x14, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x03, 0x14, 0x15, 0x1e, 0x1f}
+        },
+        {
+            {0x10, 0x01, 0x12, 0x1b, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x1b, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x1b, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x1b, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x1b, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x0f},
+            {0x10, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x0f},
+            {0x10, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x0f},
+            {0x00, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x0f},
+            {0x00, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x1e, 0x0f},
+            {0x00, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x1e, 0x0f},
+            {0x00, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x1e, 0x0f},
+            {0x00, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x1e, 0x0f},
+            {0x00, 0x09, 0x12, 0x13, 0x1c, 0x15, 0x1e, 0x0f},
+            {0x00, 0x09, 0x12, 0x13, 0x1c, 0x15, 0x1e, 0x0f},
+            {0x00, 0x09, 0x12, 0x13, 0x1c, 0x15, 0x1e, 0x0f},
+            {0x00, 0x09, 0x12, 0x13, 0x1c, 0x15, 0x1e, 0x0f},
+            {0x00, 0x09, 0x02, 0x13, 0x1c, 0x15, 0x1e, 0x0f},
+            {0x00, 0x09, 0x02, 0x13, 0x1c, 0x15, 0x1e, 0x0f},
+            {0x00, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x0f},
+            {0x00, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x0f},
+            {0x00, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x03, 0x14, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x03, 0x14, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x03, 0x14, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x03, 0x14, 0x15, 0x1e, 0x1f}
+        },
+        {
+            {0x10, 0x01, 0x12, 0x1b, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x1b, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x1b, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x1b, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x1b, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x1b, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x0f},
+            {0x10, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x0f},
+            {0x10, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x0f},
+            {0x10, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x0e, 0x0f},
+            {0x00, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x1e, 0x0f},
+            {0x00, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x1e, 0x0f},
+            {0x00, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x1e, 0x0f},
+            {0x00, 0x01, 0x12, 0x13, 0x1c, 0x1d, 0x1e, 0x0f},
+            {0x00, 0x09, 0x12, 0x13, 0x1c, 0x15, 0x1e, 0x0f},
+            {0x00, 0x09, 0x12, 0x13, 0x1c, 0x15, 0x1e, 0x0f},
+            {0x00, 0x09, 0x12, 0x13, 0x1c, 0x15, 0x1e, 0x0f},
+            {0x00, 0x09, 0x12, 0x13, 0x1c, 0x15, 0x1e, 0x0f},
+            {0x00, 0x09, 0x02, 0x13, 0x1c, 0x15, 0x1e, 0x0f},
+            {0x00, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x0f},
+            {0x00, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x0f},
+            {0x00, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x0f},
+            {0x00, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x03, 0x14, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x03, 0x14, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x03, 0x14, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x03, 0x14, 0x15, 0x1e, 0x1f},
+            {0x00, 0x09, 0x02, 0x03, 0x14, 0x15, 0x1e, 0x1f}
+        },
+        {
+            {0x10, 0x01, 0x12, 0x1b, 0x0c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x1b, 0x0c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x1b, 0x0c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x1b, 0x0c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x1b, 0x0c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x1b, 0x0c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x1b, 0x0c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x13, 0x0c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x13, 0x0c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x13, 0x0c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x13, 0x0c, 0x1d, 0x0e, 0x0f},
+            {0x10, 0x01, 0x12, 0x13, 0x0c, 0x1d, 0x0e, 0x0f},
+            {0x10, 0x01, 0x12, 0x13, 0x0c, 0x1d, 0x0e, 0x0f},
+            {0x10, 0x01, 0x12, 0x13, 0x0c, 0x1d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x12, 0x13, 0x0c, 0x1d, 0x1e, 0x0f},
+            {0x08, 0x01, 0x12, 0x13, 0x0c, 0x1d, 0x1e, 0x0f},
+            {0x08, 0x01, 0x12, 0x13, 0x0c, 0x1d, 0x1e, 0x0f},
+            {0x08, 0x01, 0x12, 0x13, 0x0c, 0x1d, 0x1e, 0x0f},
+            {0x08, 0x09, 0x12, 0x13, 0x0c, 0x15, 0x1e, 0x0f},
+            {0x08, 0x09, 0x12, 0x13, 0x0c, 0x15, 0x1e, 0x0f},
+            {0x08, 0x09, 0x12, 0x13, 0x0c, 0x15, 0x1e, 0x0f},
+            {0x08, 0x09, 0x12, 0x13, 0x0c, 0x15, 0x1e, 0x0f},
+            {0x08, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x0f},
+            {0x08, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x0f},
+            {0x08, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x0f},
+            {0x08, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x0f},
+            {0x08, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x1f},
+            {0x08, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x1f},
+            {0x08, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x1f},
+            {0x08, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x1f},
+            {0x08, 0x09, 0x02, 0x03, 0x14, 0x15, 0x1e, 0x1f},
+            {0x08, 0x09, 0x02, 0x03, 0x14, 0x15, 0x1e, 0x1f},
+            {0x08, 0x09, 0x02, 0x03, 0x14, 0x15, 0x1e, 0x1f},
+            {0x08, 0x09, 0x02, 0x03, 0x14, 0x15, 0x1e, 0x1f},
+            {0x08, 0x09, 0x02, 0x03, 0x14, 0x15, 0x1e, 0x1f},
+            {0x08, 0x09, 0x02, 0x03, 0x14, 0x15, 0x1e, 0x1f}
+        },
+        {
+            {0x10, 0x01, 0x12, 0x1b, 0x0c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x1b, 0x0c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x1b, 0x0c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x1b, 0x0c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x1b, 0x0c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x1b, 0x0c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x1b, 0x0c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x1b, 0x0c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x13, 0x0c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x13, 0x0c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x13, 0x0c, 0x1d, 0x0e, 0x0f},
+            {0x10, 0x01, 0x12, 0x13, 0x0c, 0x1d, 0x0e, 0x0f},
+            {0x10, 0x01, 0x12, 0x13, 0x0c, 0x1d, 0x0e, 0x0f},
+            {0x10, 0x01, 0x12, 0x13, 0x0c, 0x1d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x12, 0x13, 0x0c, 0x1d, 0x1e, 0x0f},
+            {0x08, 0x01, 0x12, 0x13, 0x0c, 0x1d, 0x1e, 0x0f},
+            {0x08, 0x01, 0x12, 0x13, 0x0c, 0x1d, 0x1e, 0x0f},
+            {0x08, 0x01, 0x12, 0x13, 0x0c, 0x1d, 0x1e, 0x0f},
+            {0x08, 0x09, 0x12, 0x13, 0x0c, 0x15, 0x1e, 0x0f},
+            {0x08, 0x09, 0x12, 0x13, 0x0c, 0x15, 0x1e, 0x0f},
+            {0x08, 0x09, 0x12, 0x13, 0x0c, 0x15, 0x1e, 0x0f},
+            {0x08, 0x09, 0x12, 0x13, 0x0c, 0x15, 0x1e, 0x0f},
+            {0x08, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x0f},
+            {0x08, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x0f},
+            {0x08, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x0f},
+            {0x08, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x0f},
+            {0x08, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x1f},
+            {0x08, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x1f},
+            {0x08, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x1f},
+            {0x08, 0x09, 0x02, 0x03, 0x14, 0x15, 0x1e, 0x1f},
+            {0x08, 0x09, 0x02, 0x03, 0x14, 0x15, 0x1e, 0x1f},
+            {0x08, 0x09, 0x02, 0x03, 0x14, 0x15, 0x1e, 0x1f},
+            {0x08, 0x09, 0x02, 0x03, 0x14, 0x15, 0x1e, 0x1f},
+            {0x08, 0x09, 0x02, 0x03, 0x14, 0x15, 0x1e, 0x1f},
+            {0x08, 0x09, 0x02, 0x03, 0x14, 0x15, 0x1e, 0x1f},
+            {0x08, 0x09, 0x02, 0x03, 0x14, 0x15, 0x1e, 0x1f}
+        },
+        {
+            {0x10, 0x01, 0x12, 0x1b, 0x0c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x1b, 0x0c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x1b, 0x0c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x1b, 0x0c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x1b, 0x0c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x1b, 0x0c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x1b, 0x0c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x1b, 0x0c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x1b, 0x0c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x13, 0x0c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x13, 0x0c, 0x1d, 0x0e, 0x0f},
+            {0x10, 0x01, 0x12, 0x13, 0x0c, 0x1d, 0x0e, 0x0f},
+            {0x10, 0x01, 0x12, 0x13, 0x0c, 0x1d, 0x0e, 0x0f},
+            {0x10, 0x01, 0x12, 0x13, 0x0c, 0x1d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x12, 0x13, 0x0c, 0x1d, 0x1e, 0x0f},
+            {0x08, 0x01, 0x12, 0x13, 0x0c, 0x1d, 0x1e, 0x0f},
+            {0x08, 0x01, 0x12, 0x13, 0x0c, 0x1d, 0x1e, 0x0f},
+            {0x08, 0x01, 0x12, 0x13, 0x0c, 0x1d, 0x1e, 0x0f},
+            {0x08, 0x09, 0x12, 0x13, 0x0c, 0x15, 0x1e, 0x0f},
+            {0x08, 0x09, 0x12, 0x13, 0x0c, 0x15, 0x1e, 0x0f},
+            {0x08, 0x09, 0x12, 0x13, 0x0c, 0x15, 0x1e, 0x0f},
+            {0x08, 0x09, 0x12, 0x13, 0x0c, 0x15, 0x1e, 0x0f},
+            {0x08, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x0f},
+            {0x08, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x0f},
+            {0x08, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x0f},
+            {0x08, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x0f},
+            {0x08, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x1f},
+            {0x08, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x1f},
+            {0x08, 0x09, 0x02, 0x03, 0x14, 0x15, 0x1e, 0x1f},
+            {0x08, 0x09, 0x02, 0x03, 0x14, 0x15, 0x1e, 0x1f},
+            {0x08, 0x09, 0x02, 0x03, 0x14, 0x15, 0x1e, 0x1f},
+            {0x08, 0x09, 0x02, 0x03, 0x14, 0x15, 0x1e, 0x1f},
+            {0x08, 0x09, 0x02, 0x03, 0x14, 0x15, 0x1e, 0x1f},
+            {0x08, 0x09, 0x02, 0x03, 0x14, 0x15, 0x1e, 0x1f},
+            {0x08, 0x09, 0x02, 0x03, 0x14, 0x15, 0x1e, 0x1f},
+            {0x08, 0x09, 0x02, 0x03, 0x14, 0x15, 0x1e, 0x1f}
+        },
+        {
+            {0x10, 0x01, 0x12, 0x1b, 0x0c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x1b, 0x0c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x1b, 0x0c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x1b, 0x0c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x1b, 0x0c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x1b, 0x0c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x1b, 0x0c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x1b, 0x0c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x1b, 0x0c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x1b, 0x0c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x01, 0x12, 0x13, 0x0c, 0x1d, 0x0e, 0x0f},
+            {0x10, 0x01, 0x12, 0x13, 0x0c, 0x1d, 0x0e, 0x0f},
+            {0x10, 0x01, 0x12, 0x13, 0x0c, 0x1d, 0x0e, 0x0f},
+            {0x10, 0x01, 0x12, 0x13, 0x0c, 0x1d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x12, 0x13, 0x0c, 0x1d, 0x1e, 0x0f},
+            {0x08, 0x01, 0x12, 0x13, 0x0c, 0x1d, 0x1e, 0x0f},
+            {0x08, 0x01, 0x12, 0x13, 0x0c, 0x1d, 0x1e, 0x0f},
+            {0x08, 0x01, 0x12, 0x13, 0x0c, 0x1d, 0x1e, 0x0f},
+            {0x08, 0x09, 0x12, 0x13, 0x0c, 0x15, 0x1e, 0x0f},
+            {0x08, 0x09, 0x12, 0x13, 0x0c, 0x15, 0x1e, 0x0f},
+            {0x08, 0x09, 0x12, 0x13, 0x0c, 0x15, 0x1e, 0x0f},
+            {0x08, 0x09, 0x12, 0x13, 0x0c, 0x15, 0x1e, 0x0f},
+            {0x08, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x0f},
+            {0x08, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x0f},
+            {0x08, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x0f},
+            {0x08, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x0f},
+            {0x08, 0x09, 0x02, 0x13, 0x14, 0x15, 0x1e, 0x1f},
+            {0x08, 0x09, 0x02, 0x03, 0x14, 0x15, 0x1e, 0x1f},
+            {0x08, 0x09, 0x02, 0x03, 0x14, 0x15, 0x1e, 0x1f},
+            {0x08, 0x09, 0x02, 0x03, 0x14, 0x15, 0x1e, 0x1f},
+            {0x08, 0x09, 0x02, 0x03, 0x14, 0x15, 0x1e, 0x1f},
+            {0x08, 0x09, 0x02, 0x03, 0x14, 0x15, 0x1e, 0x1f},
+            {0x08, 0x09, 0x02, 0x03, 0x14, 0x15, 0x1e, 0x1f},
+            {0x08, 0x09, 0x02, 0x03, 0x14, 0x15, 0x1e, 0x1f},
+            {0x08, 0x09, 0x02, 0x03, 0x14, 0x15, 0x1e, 0x1f},
+            {0x08, 0x09, 0x02, 0x03, 0x14, 0x15, 0x1e, 0x1f}
+        },
+        {
+            {0x10, 0x11, 0x12, 0x1b, 0x0c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x11, 0x12, 0x1b, 0x0c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x11, 0x12, 0x1b, 0x0c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x11, 0x12, 0x1b, 0x0c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x11, 0x12, 0x1b, 0x0c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x11, 0x12, 0x1b, 0x0c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x11, 0x12, 0x1b, 0x0c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x11, 0x12, 0x1b, 0x0c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x11, 0x12, 0x1b, 0x0c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x11, 0x12, 0x1b, 0x0c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x11, 0x12, 0x0b, 0x0c, 0x1d, 0x0e, 0x0f},
+            {0x10, 0x11, 0x12, 0x0b, 0x0c, 0x1d, 0x0e, 0x0f},
+            {0x10, 0x11, 0x12, 0x0b, 0x0c, 0x1d, 0x0e, 0x0f},
+            {0x10, 0x11, 0x12, 0x0b, 0x0c, 0x1d, 0x0e, 0x0f},
+            {0x08, 0x11, 0x12, 0x0b, 0x0c, 0x1d, 0x1e, 0x0f},
+            {0x08, 0x11, 0x12, 0x0b, 0x0c, 0x1d, 0x1e, 0x0f},
+            {0x08, 0x11, 0x12, 0x0b, 0x0c, 0x1d, 0x1e, 0x0f},
+            {0x08, 0x11, 0x12, 0x0b, 0x0c, 0x1d, 0x1e, 0x0f},
+            {0x08, 0x19, 0x12, 0x0b, 0x0c, 0x15, 0x1e, 0x0f},
+            {0x08, 0x19, 0x12, 0x0b, 0x0c, 0x15, 0x1e, 0x0f},
+            {0x08, 0x19, 0x12, 0x0b, 0x0c, 0x15, 0x1e, 0x0f},
+            {0x08, 0x19, 0x12, 0x0b, 0x0c, 0x15, 0x1e, 0x0f},
+            {0x08, 0x19, 0x02, 0x0b, 0x14, 0x15, 0x1e, 0x0f},
+            {0x08, 0x19, 0x02, 0x0b, 0x14, 0x15, 0x1e, 0x0f},
+            {0x08, 0x19, 0x02, 0x0b, 0x14, 0x15, 0x1e, 0x0f},
+            {0x08, 0x19, 0x02, 0x0b, 0x14, 0x15, 0x1e, 0x0f},
+            {0x08, 0x19, 0x02, 0x03, 0x14, 0x15, 0x1e, 0x1f},
+            {0x08, 0x19, 0x02, 0x03, 0x14, 0x15, 0x1e, 0x1f},
+            {0x08, 0x19, 0x02, 0x03, 0x14, 0x15, 0x1e, 0x1f},
+            {0x08, 0x19, 0x02, 0x03, 0x14, 0x15, 0x1e, 0x1f},
+            {0x08, 0x19, 0x02, 0x03, 0x14, 0x15, 0x1e, 0x1f},
+            {0x08, 0x19, 0x02, 0x03, 0x14, 0x15, 0x1e, 0x1f},
+            {0x08, 0x19, 0x02, 0x03, 0x14, 0x15, 0x1e, 0x1f},
+            {0x08, 0x19, 0x02, 0x03, 0x14, 0x15, 0x1e, 0x1f},
+            {0x08, 0x19, 0x02, 0x03, 0x14, 0x15, 0x1e, 0x1f},
+            {0x08, 0x19, 0x02, 0x03, 0x14, 0x15, 0x1e, 0x1f}
+        },
+        {
+            {0x10, 0x11, 0x12, 0x1b, 0x0c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x11, 0x12, 0x1b, 0x0c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x11, 0x12, 0x1b, 0x0c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x11, 0x12, 0x1b, 0x0c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x11, 0x12, 0x1b, 0x0c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x11, 0x12, 0x1b, 0x0c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x11, 0x12, 0x1b, 0x0c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x11, 0x12, 0x1b, 0x0c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x11, 0x12, 0x1b, 0x0c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x11, 0x12, 0x1b, 0x0c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x11, 0x12, 0x0b, 0x0c, 0x1d, 0x0e, 0x0f},
+            {0x10, 0x11, 0x12, 0x0b, 0x0c, 0x1d, 0x0e, 0x0f},
+            {0x10, 0x11, 0x12, 0x0b, 0x0c, 0x1d, 0x0e, 0x0f},
+            {0x10, 0x11, 0x12, 0x0b, 0x0c, 0x1d, 0x0e, 0x0f},
+            {0x08, 0x11, 0x12, 0x0b, 0x0c, 0x1d, 0x1e, 0x0f},
+            {0x08, 0x11, 0x12, 0x0b, 0x0c, 0x1d, 0x1e, 0x0f},
+            {0x08, 0x11, 0x12, 0x0b, 0x0c, 0x1d, 0x1e, 0x0f},
+            {0x08, 0x11, 0x12, 0x0b, 0x0c, 0x1d, 0x1e, 0x0f},
+            {0x08, 0x19, 0x12, 0x0b, 0x0c, 0x15, 0x1e, 0x0f},
+            {0x08, 0x19, 0x12, 0x0b, 0x0c, 0x15, 0x1e, 0x0f},
+            {0x08, 0x19, 0x12, 0x0b, 0x0c, 0x15, 0x1e, 0x0f},
+            {0x08, 0x19, 0x12, 0x0b, 0x0c, 0x15, 0x1e, 0x0f},
+            {0x08, 0x19, 0x02, 0x0b, 0x14, 0x15, 0x1e, 0x0f},
+            {0x08, 0x19, 0x02, 0x0b, 0x14, 0x15, 0x1e, 0x0f},
+            {0x08, 0x19, 0x02, 0x0b, 0x14, 0x15, 0x1e, 0x0f},
+            {0x08, 0x19, 0x02, 0x0b, 0x14, 0x15, 0x1e, 0x0f},
+            {0x08, 0x19, 0x02, 0x03, 0x14, 0x15, 0x1e, 0x1f},
+            {0x08, 0x19, 0x02, 0x03, 0x14, 0x15, 0x1e, 0x1f},
+            {0x08, 0x19, 0x02, 0x03, 0x14, 0x15, 0x1e, 0x1f},
+            {0x08, 0x19, 0x02, 0x03, 0x14, 0x15, 0x1e, 0x1f},
+            {0x08, 0x19, 0x02, 0x03, 0x14, 0x15, 0x1e, 0x1f},
+            {0x08, 0x19, 0x02, 0x03, 0x14, 0x15, 0x1e, 0x1f},
+            {0x08, 0x19, 0x02, 0x03, 0x14, 0x15, 0x1e, 0x1f},
+            {0x08, 0x19, 0x02, 0x03, 0x14, 0x15, 0x1e, 0x1f},
+            {0x08, 0x19, 0x02, 0x03, 0x14, 0x15, 0x1e, 0x1f},
+            {0x08, 0x19, 0x02, 0x03, 0x14, 0x15, 0x1e, 0x1f}
+        },
+        {
+            {0x10, 0x11, 0x12, 0x1b, 0x0c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x11, 0x12, 0x1b, 0x0c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x11, 0x12, 0x1b, 0x0c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x11, 0x12, 0x1b, 0x0c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x11, 0x12, 0x1b, 0x0c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x11, 0x12, 0x1b, 0x0c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x11, 0x12, 0x1b, 0x0c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x11, 0x12, 0x1b, 0x0c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x11, 0x12, 0x1b, 0x0c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x11, 0x12, 0x1b, 0x0c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x11, 0x12, 0x0b, 0x0c, 0x1d, 0x0e, 0x0f},
+            {0x10, 0x11, 0x12, 0x0b, 0x0c, 0x1d, 0x0e, 0x0f},
+            {0x10, 0x11, 0x12, 0x0b, 0x0c, 0x1d, 0x0e, 0x0f},
+            {0x10, 0x11, 0x12, 0x0b, 0x0c, 0x1d, 0x0e, 0x0f},
+            {0x08, 0x11, 0x12, 0x0b, 0x0c, 0x1d, 0x1e, 0x0f},
+            {0x08, 0x11, 0x12, 0x0b, 0x0c, 0x1d, 0x1e, 0x0f},
+            {0x08, 0x11, 0x12, 0x0b, 0x0c, 0x1d, 0x1e, 0x0f},
+            {0x08, 0x11, 0x12, 0x0b, 0x0c, 0x1d, 0x1e, 0x0f},
+            {0x08, 0x19, 0x12, 0x0b, 0x0c, 0x15, 0x1e, 0x0f},
+            {0x08, 0x19, 0x12, 0x0b, 0x0c, 0x15, 0x1e, 0x0f},
+            {0x08, 0x19, 0x12, 0x0b, 0x0c, 0x15, 0x1e, 0x0f},
+            {0x08, 0x19, 0x12, 0x0b, 0x0c, 0x15, 0x1e, 0x0f},
+            {0x08, 0x19, 0x02, 0x0b, 0x14, 0x15, 0x1e, 0x0f},
+            {0x08, 0x19, 0x02, 0x0b, 0x14, 0x15, 0x1e, 0x0f},
+            {0x08, 0x19, 0x02, 0x0b, 0x14, 0x15, 0x1e, 0x0f},
+            {0x08, 0x19, 0x02, 0x0b, 0x14, 0x15, 0x1e, 0x0f},
+            {0x08, 0x19, 0x02, 0x03, 0x14, 0x15, 0x1e, 0x1f},
+            {0x08, 0x19, 0x02, 0x03, 0x14, 0x15, 0x1e, 0x1f},
+            {0x08, 0x19, 0x02, 0x03, 0x14, 0x15, 0x1e, 0x1f},
+            {0x08, 0x19, 0x02, 0x03, 0x14, 0x15, 0x1e, 0x1f},
+            {0x08, 0x19, 0x02, 0x03, 0x14, 0x15, 0x1e, 0x1f},
+            {0x08, 0x19, 0x02, 0x03, 0x14, 0x15, 0x1e, 0x1f},
+            {0x08, 0x19, 0x02, 0x03, 0x14, 0x15, 0x1e, 0x1f},
+            {0x08, 0x19, 0x02, 0x03, 0x14, 0x15, 0x1e, 0x1f},
+            {0x08, 0x19, 0x02, 0x03, 0x14, 0x15, 0x1e, 0x1f},
+            {0x08, 0x19, 0x02, 0x03, 0x14, 0x15, 0x1e, 0x1f}
+        },
+        {
+            {0x10, 0x11, 0x12, 0x1b, 0x0c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x11, 0x12, 0x1b, 0x0c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x11, 0x12, 0x1b, 0x0c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x11, 0x12, 0x1b, 0x0c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x11, 0x12, 0x1b, 0x0c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x11, 0x12, 0x1b, 0x0c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x11, 0x12, 0x1b, 0x0c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x11, 0x12, 0x1b, 0x0c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x11, 0x12, 0x1b, 0x0c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x11, 0x12, 0x1b, 0x0c, 0x1d, 0x0e, 0x07},
+            {0x10, 0x11, 0x12, 0x0b, 0x0c, 0x1d, 0x0e, 0x0f},
+            {0x10, 0x11, 0x12, 0x0b, 0x0c, 0x1d, 0x0e, 0x0f},
+            {0x10, 0x11, 0x12, 0x0b, 0x0c, 0x1d, 0x0e, 0x0f},
+            {0x10, 0x11, 0x12, 0x0b, 0x0c, 0x1d, 0x0e, 0x0f},
+            {0x08, 0x11, 0x12, 0x0b, 0x0c, 0x1d, 0x1e, 0x0f},
+            {0x08, 0x11, 0x12, 0x0b, 0x0c, 0x1d, 0x1e, 0x0f},
+            {0x08, 0x11, 0x12, 0x0b, 0x0c, 0x1d, 0x1e, 0x0f},
+            {0x08, 0x11, 0x12, 0x0b, 0x0c, 0x1d, 0x1e, 0x0f},
+            {0x08, 0x19, 0x12, 0x0b, 0x0c, 0x15, 0x1e, 0x0f},
+            {0x08, 0x19, 0x12, 0x0b, 0x0c, 0x15, 0x1e, 0x0f},
+            {0x08, 0x19, 0x12, 0x0b, 0x0c, 0x15, 0x1e, 0x0f},
+            {0x08, 0x19, 0x12, 0x0b, 0x0c, 0x15, 0x1e, 0x0f},
+            {0x08, 0x19, 0x02, 0x0b, 0x14, 0x15, 0x1e, 0x0f},
+            {0x08, 0x19, 0x02, 0x0b, 0x14, 0x15, 0x1e, 0x0f},
+            {0x08, 0x19, 0x02, 0x0b, 0x14, 0x15, 0x1e, 0x0f},
+            {0x08, 0x19, 0x02, 0x0b, 0x14, 0x15, 0x1e, 0x0f},
+            {0x08, 0x19, 0x02, 0x03, 0x14, 0x15, 0x1e, 0x1f},
+            {0x08, 0x19, 0x02, 0x03, 0x14, 0x15, 0x1e, 0x1f},
+            {0x08, 0x19, 0x02, 0x03, 0x14, 0x15, 0x1e, 0x1f},
+            {0x08, 0x19, 0x02, 0x03, 0x14, 0x15, 0x1e, 0x1f},
+            {0x08, 0x19, 0x02, 0x03, 0x14, 0x15, 0x1e, 0x1f},
+            {0x08, 0x19, 0x02, 0x03, 0x14, 0x15, 0x1e, 0x1f},
+            {0x08, 0x19, 0x02, 0x03, 0x14, 0x15, 0x1e, 0x1f},
+            {0x08, 0x19, 0x02, 0x03, 0x14, 0x15, 0x1e, 0x1f},
+            {0x08, 0x19, 0x02, 0x03, 0x14, 0x15, 0x1e, 0x1f},
+            {0x08, 0x19, 0x02, 0x03, 0x14, 0x15, 0x1e, 0x1f}
+        },
+        {
+            {0x10, 0x11, 0x1a, 0x1b, 0x0c, 0x1d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x0c, 0x1d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x0c, 0x1d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x0c, 0x1d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x0c, 0x1d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x0c, 0x1d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x0c, 0x1d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x0c, 0x1d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x0c, 0x1d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x0c, 0x1d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x0b, 0x0c, 0x1d, 0x06, 0x0f},
+            {0x10, 0x11, 0x1a, 0x0b, 0x0c, 0x1d, 0x06, 0x0f},
+            {0x10, 0x11, 0x1a, 0x0b, 0x0c, 0x1d, 0x06, 0x0f},
+            {0x10, 0x11, 0x1a, 0x0b, 0x0c, 0x1d, 0x06, 0x0f},
+            {0x08, 0x11, 0x1a, 0x0b, 0x0c, 0x1d, 0x16, 0x0f},
+            {0x08, 0x11, 0x1a, 0x0b, 0x0c, 0x1d, 0x16, 0x0f},
+            {0x08, 0x11, 0x1a, 0x0b, 0x0c, 0x1d, 0x16, 0x0f},
+            {0x08, 0x11, 0x1a, 0x0b, 0x0c, 0x1d, 0x16, 0x0f},
+            {0x08, 0x19, 0x1a, 0x0b, 0x0c, 0x15, 0x16, 0x0f},
+            {0x08, 0x19, 0x1a, 0x0b, 0x0c, 0x15, 0x16, 0x0f},
+            {0x08, 0x19, 0x1a, 0x0b, 0x0c, 0x15, 0x16, 0x0f},
+            {0x08, 0x19, 0x1a, 0x0b, 0x0c, 0x15, 0x16, 0x0f},
+            {0x08, 0x19, 0x0a, 0x0b, 0x14, 0x15, 0x16, 0x0f},
+            {0x08, 0x19, 0x0a, 0x0b, 0x14, 0x15, 0x16, 0x0f},
+            {0x08, 0x19, 0x0a, 0x0b, 0x14, 0x15, 0x16, 0x0f},
+            {0x08, 0x19, 0x0a, 0x0b, 0x14, 0x15, 0x16, 0x0f},
+            {0x08, 0x19, 0x0a, 0x03, 0x14, 0x15, 0x16, 0x1f},
+            {0x08, 0x19, 0x0a, 0x03, 0x14, 0x15, 0x16, 0x1f},
+            {0x08, 0x19, 0x0a, 0x03, 0x14, 0x15, 0x16, 0x1f},
+            {0x08, 0x19, 0x0a, 0x03, 0x14, 0x15, 0x16, 0x1f},
+            {0x08, 0x19, 0x0a, 0x03, 0x14, 0x15, 0x16, 0x1f},
+            {0x08, 0x19, 0x0a, 0x03, 0x14, 0x15, 0x16, 0x1f},
+            {0x08, 0x19, 0x0a, 0x03, 0x14, 0x15, 0x16, 0x1f},
+            {0x08, 0x19, 0x0a, 0x03, 0x14, 0x15, 0x16, 0x1f},
+            {0x08, 0x19, 0x0a, 0x03, 0x14, 0x15, 0x16, 0x1f},
+            {0x08, 0x19, 0x0a, 0x03, 0x14, 0x15, 0x16, 0x1f}
+        },
+        {
+            {0x10, 0x11, 0x1a, 0x1b, 0x0c, 0x1d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x0c, 0x1d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x0c, 0x1d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x0c, 0x1d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x0c, 0x1d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x0c, 0x1d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x0c, 0x1d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x0c, 0x1d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x0c, 0x1d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x0c, 0x1d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x0b, 0x0c, 0x1d, 0x06, 0x0f},
+            {0x10, 0x11, 0x1a, 0x0b, 0x0c, 0x1d, 0x06, 0x0f},
+            {0x10, 0x11, 0x1a, 0x0b, 0x0c, 0x1d, 0x06, 0x0f},
+            {0x10, 0x11, 0x1a, 0x0b, 0x0c, 0x1d, 0x06, 0x0f},
+            {0x08, 0x11, 0x1a, 0x0b, 0x0c, 0x1d, 0x16, 0x0f},
+            {0x08, 0x11, 0x1a, 0x0b, 0x0c, 0x1d, 0x16, 0x0f},
+            {0x08, 0x11, 0x1a, 0x0b, 0x0c, 0x1d, 0x16, 0x0f},
+            {0x08, 0x11, 0x1a, 0x0b, 0x0c, 0x1d, 0x16, 0x0f},
+            {0x08, 0x19, 0x1a, 0x0b, 0x0c, 0x15, 0x16, 0x0f},
+            {0x08, 0x19, 0x1a, 0x0b, 0x0c, 0x15, 0x16, 0x0f},
+            {0x08, 0x19, 0x1a, 0x0b, 0x0c, 0x15, 0x16, 0x0f},
+            {0x08, 0x19, 0x1a, 0x0b, 0x0c, 0x15, 0x16, 0x0f},
+            {0x08, 0x19, 0x0a, 0x0b, 0x14, 0x15, 0x16, 0x0f},
+            {0x08, 0x19, 0x0a, 0x0b, 0x14, 0x15, 0x16, 0x0f},
+            {0x08, 0x19, 0x0a, 0x0b, 0x14, 0x15, 0x16, 0x0f},
+            {0x08, 0x19, 0x0a, 0x0b, 0x14, 0x15, 0x16, 0x0f},
+            {0x08, 0x19, 0x0a, 0x03, 0x14, 0x15, 0x16, 0x1f},
+            {0x08, 0x19, 0x0a, 0x03, 0x14, 0x15, 0x16, 0x1f},
+            {0x08, 0x19, 0x0a, 0x03, 0x14, 0x15, 0x16, 0x1f},
+            {0x08, 0x19, 0x0a, 0x03, 0x14, 0x15, 0x16, 0x1f},
+            {0x08, 0x19, 0x0a, 0x03, 0x14, 0x15, 0x16, 0x1f},
+            {0x08, 0x19, 0x0a, 0x03, 0x14, 0x15, 0x16, 0x1f},
+            {0x08, 0x19, 0x0a, 0x03, 0x14, 0x15, 0x16, 0x1f},
+            {0x08, 0x19, 0x0a, 0x03, 0x14, 0x15, 0x16, 0x1f},
+            {0x08, 0x19, 0x0a, 0x03, 0x14, 0x15, 0x16, 0x1f},
+            {0x08, 0x19, 0x0a, 0x03, 0x14, 0x15, 0x16, 0x1f}
+        },
+        {
+            {0x10, 0x11, 0x1a, 0x1b, 0x0c, 0x1d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x0c, 0x1d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x0c, 0x1d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x0c, 0x1d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x0c, 0x1d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x0c, 0x1d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x0c, 0x1d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x0c, 0x1d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x0c, 0x1d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x0c, 0x1d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x0b, 0x0c, 0x1d, 0x06, 0x0f},
+            {0x10, 0x11, 0x1a, 0x0b, 0x0c, 0x1d, 0x06, 0x0f},
+            {0x10, 0x11, 0x1a, 0x0b, 0x0c, 0x1d, 0x06, 0x0f},
+            {0x10, 0x11, 0x1a, 0x0b, 0x0c, 0x1d, 0x06, 0x0f},
+            {0x08, 0x11, 0x1a, 0x0b, 0x0c, 0x1d, 0x16, 0x0f},
+            {0x08, 0x11, 0x1a, 0x0b, 0x0c, 0x1d, 0x16, 0x0f},
+            {0x08, 0x11, 0x1a, 0x0b, 0x0c, 0x1d, 0x16, 0x0f},
+            {0x08, 0x11, 0x1a, 0x0b, 0x0c, 0x1d, 0x16, 0x0f},
+            {0x08, 0x19, 0x1a, 0x0b, 0x0c, 0x15, 0x16, 0x0f},
+            {0x08, 0x19, 0x1a, 0x0b, 0x0c, 0x15, 0x16, 0x0f},
+            {0x08, 0x19, 0x1a, 0x0b, 0x0c, 0x15, 0x16, 0x0f},
+            {0x08, 0x19, 0x1a, 0x0b, 0x0c, 0x15, 0x16, 0x0f},
+            {0x08, 0x19, 0x0a, 0x0b, 0x14, 0x15, 0x16, 0x0f},
+            {0x08, 0x19, 0x0a, 0x0b, 0x14, 0x15, 0x16, 0x0f},
+            {0x08, 0x19, 0x0a, 0x0b, 0x14, 0x15, 0x16, 0x0f},
+            {0x08, 0x19, 0x0a, 0x0b, 0x14, 0x15, 0x16, 0x0f},
+            {0x08, 0x19, 0x0a, 0x03, 0x14, 0x15, 0x16, 0x1f},
+            {0x08, 0x19, 0x0a, 0x03, 0x14, 0x15, 0x16, 0x1f},
+            {0x08, 0x19, 0x0a, 0x03, 0x14, 0x15, 0x16, 0x1f},
+            {0x08, 0x19, 0x0a, 0x03, 0x14, 0x15, 0x16, 0x1f},
+            {0x08, 0x19, 0x0a, 0x03, 0x14, 0x15, 0x16, 0x1f},
+            {0x08, 0x19, 0x0a, 0x03, 0x14, 0x15, 0x16, 0x1f},
+            {0x08, 0x19, 0x0a, 0x03, 0x14, 0x15, 0x16, 0x1f},
+            {0x08, 0x19, 0x0a, 0x03, 0x14, 0x15, 0x16, 0x1f},
+            {0x08, 0x19, 0x0a, 0x03, 0x14, 0x15, 0x16, 0x1f},
+            {0x08, 0x19, 0x0a, 0x03, 0x14, 0x15, 0x16, 0x1f}
+        },
+        {
+            {0x10, 0x11, 0x1a, 0x1b, 0x0c, 0x1d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x0c, 0x1d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x0c, 0x1d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x0c, 0x1d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x0c, 0x1d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x0c, 0x1d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x0c, 0x1d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x0c, 0x1d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x0c, 0x1d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x0c, 0x1d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x0b, 0x0c, 0x1d, 0x06, 0x0f},
+            {0x10, 0x11, 0x1a, 0x0b, 0x0c, 0x1d, 0x06, 0x0f},
+            {0x10, 0x11, 0x1a, 0x0b, 0x0c, 0x1d, 0x06, 0x0f},
+            {0x10, 0x11, 0x1a, 0x0b, 0x0c, 0x1d, 0x06, 0x0f},
+            {0x08, 0x11, 0x1a, 0x0b, 0x0c, 0x1d, 0x16, 0x0f},
+            {0x08, 0x11, 0x1a, 0x0b, 0x0c, 0x1d, 0x16, 0x0f},
+            {0x08, 0x11, 0x1a, 0x0b, 0x0c, 0x1d, 0x16, 0x0f},
+            {0x08, 0x11, 0x1a, 0x0b, 0x0c, 0x1d, 0x16, 0x0f},
+            {0x08, 0x19, 0x1a, 0x0b, 0x0c, 0x15, 0x16, 0x0f},
+            {0x08, 0x19, 0x1a, 0x0b, 0x0c, 0x15, 0x16, 0x0f},
+            {0x08, 0x19, 0x1a, 0x0b, 0x0c, 0x15, 0x16, 0x0f},
+            {0x08, 0x19, 0x1a, 0x0b, 0x0c, 0x15, 0x16, 0x0f},
+            {0x08, 0x19, 0x0a, 0x0b, 0x14, 0x15, 0x16, 0x0f},
+            {0x08, 0x19, 0x0a, 0x0b, 0x14, 0x15, 0x16, 0x0f},
+            {0x08, 0x19, 0x0a, 0x0b, 0x14, 0x15, 0x16, 0x0f},
+            {0x08, 0x19, 0x0a, 0x0b, 0x14, 0x15, 0x16, 0x0f},
+            {0x08, 0x19, 0x0a, 0x03, 0x14, 0x15, 0x16, 0x1f},
+            {0x08, 0x19, 0x0a, 0x03, 0x14, 0x15, 0x16, 0x1f},
+            {0x08, 0x19, 0x0a, 0x03, 0x14, 0x15, 0x16, 0x1f},
+            {0x08, 0x19, 0x0a, 0x03, 0x14, 0x15, 0x16, 0x1f},
+            {0x08, 0x19, 0x0a, 0x03, 0x14, 0x15, 0x16, 0x1f},
+            {0x08, 0x19, 0x0a, 0x03, 0x14, 0x15, 0x16, 0x1f},
+            {0x08, 0x19, 0x0a, 0x03, 0x14, 0x15, 0x16, 0x1f},
+            {0x08, 0x19, 0x0a, 0x03, 0x14, 0x15, 0x16, 0x1f},
+            {0x08, 0x19, 0x0a, 0x03, 0x14, 0x15, 0x16, 0x1f},
+            {0x08, 0x19, 0x0a, 0x03, 0x14, 0x15, 0x16, 0x1f}
+        },
+        {
+            {0x10, 0x11, 0x1a, 0x1b, 0x0c, 0x0d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x0c, 0x0d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x0c, 0x0d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x0c, 0x0d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x0c, 0x0d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x0c, 0x0d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x0c, 0x0d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x0c, 0x0d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x0c, 0x0d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x0c, 0x0d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x0b, 0x0c, 0x0d, 0x06, 0x17},
+            {0x10, 0x11, 0x1a, 0x0b, 0x0c, 0x0d, 0x06, 0x17},
+            {0x10, 0x11, 0x1a, 0x0b, 0x0c, 0x0d, 0x06, 0x17},
+            {0x10, 0x11, 0x1a, 0x0b, 0x0c, 0x0d, 0x06, 0x17},
+            {0x08, 0x11, 0x1a, 0x0b, 0x0c, 0x0d, 0x16, 0x17},
+            {0x08, 0x11, 0x1a, 0x0b, 0x0c, 0x0d, 0x16, 0x17},
+            {0x08, 0x11, 0x1a, 0x0b, 0x0c, 0x0d, 0x16, 0x17},
+            {0x08, 0x11, 0x1a, 0x0b, 0x0c, 0x0d, 0x16, 0x17},
+            {0x08, 0x19, 0x1a, 0x0b, 0x0c, 0x05, 0x16, 0x17},
+            {0x08, 0x19, 0x1a, 0x0b, 0x0c, 0x05, 0x16, 0x17},
+            {0x08, 0x19, 0x1a, 0x0b, 0x0c, 0x05, 0x16, 0x17},
+            {0x08, 0x19, 0x1a, 0x0b, 0x0c, 0x05, 0x16, 0x17},
+            {0x08, 0x19, 0x0a, 0x0b, 0x14, 0x05, 0x16, 0x17},
+            {0x08, 0x19, 0x0a, 0x0b, 0x14, 0x05, 0x16, 0x17},
+            {0x08, 0x19, 0x0a, 0x0b, 0x14, 0x05, 0x16, 0x17},
+            {0x08, 0x19, 0x0a, 0x0b, 0x14, 0x05, 0x16, 0x17},
+            {0x08, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x1f},
+            {0x08, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x1f},
+            {0x08, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x1f},
+            {0x08, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x1f},
+            {0x08, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x1f},
+            {0x08, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x1f},
+            {0x08, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x1f},
+            {0x08, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x1f},
+            {0x08, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x1f},
+            {0x08, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x1f}
+        },
+        {
+            {0x10, 0x11, 0x1a, 0x1b, 0x0c, 0x0d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x0c, 0x0d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x0c, 0x0d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x0c, 0x0d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x0c, 0x0d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x0c, 0x0d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x0c, 0x0d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x0c, 0x0d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x0c, 0x0d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x0c, 0x0d, 0x06, 0x17},
+            {0x10, 0x11, 0x1a, 0x0b, 0x0c, 0x0d, 0x06, 0x17},
+            {0x10, 0x11, 0x1a, 0x0b, 0x0c, 0x0d, 0x06, 0x17},
+            {0x10, 0x11, 0x1a, 0x0b, 0x0c, 0x0d, 0x06, 0x17},
+            {0x10, 0x11, 0x1a, 0x0b, 0x0c, 0x0d, 0x06, 0x17},
+            {0x08, 0x11, 0x1a, 0x0b, 0x0c, 0x0d, 0x16, 0x17},
+            {0x08, 0x11, 0x1a, 0x0b, 0x0c, 0x0d, 0x16, 0x17},
+            {0x08, 0x11, 0x1a, 0x0b, 0x0c, 0x0d, 0x16, 0x17},
+            {0x08, 0x11, 0x1a, 0x0b, 0x0c, 0x0d, 0x16, 0x17},
+            {0x08, 0x19, 0x1a, 0x0b, 0x0c, 0x05, 0x16, 0x17},
+            {0x08, 0x19, 0x1a, 0x0b, 0x0c, 0x05, 0x16, 0x17},
+            {0x08, 0x19, 0x1a, 0x0b, 0x0c, 0x05, 0x16, 0x17},
+            {0x08, 0x19, 0x1a, 0x0b, 0x0c, 0x05, 0x16, 0x17},
+            {0x08, 0x19, 0x0a, 0x0b, 0x14, 0x05, 0x16, 0x17},
+            {0x08, 0x19, 0x0a, 0x0b, 0x14, 0x05, 0x16, 0x17},
+            {0x08, 0x19, 0x0a, 0x0b, 0x14, 0x05, 0x16, 0x17},
+            {0x08, 0x19, 0x0a, 0x0b, 0x14, 0x05, 0x16, 0x17},
+            {0x08, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x17},
+            {0x08, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x1f},
+            {0x08, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x1f},
+            {0x08, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x1f},
+            {0x08, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x1f},
+            {0x08, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x1f},
+            {0x08, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x1f},
+            {0x08, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x1f},
+            {0x08, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x1f},
+            {0x08, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x1f}
+        },
+        {
+            {0x10, 0x11, 0x1a, 0x1b, 0x0c, 0x0d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x0c, 0x0d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x0c, 0x0d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x0c, 0x0d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x0c, 0x0d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x0c, 0x0d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x0c, 0x0d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x0c, 0x0d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x0c, 0x0d, 0x06, 0x17},
+            {0x10, 0x11, 0x1a, 0x1b, 0x0c, 0x0d, 0x06, 0x17},
+            {0x10, 0x11, 0x1a, 0x0b, 0x0c, 0x0d, 0x06, 0x17},
+            {0x10, 0x11, 0x1a, 0x0b, 0x0c, 0x0d, 0x06, 0x17},
+            {0x10, 0x11, 0x1a, 0x0b, 0x0c, 0x0d, 0x06, 0x17},
+            {0x10, 0x11, 0x1a, 0x0b, 0x0c, 0x0d, 0x06, 0x17},
+            {0x08, 0x11, 0x1a, 0x0b, 0x0c, 0x0d, 0x16, 0x17},
+            {0x08, 0x11, 0x1a, 0x0b, 0x0c, 0x0d, 0x16, 0x17},
+            {0x08, 0x11, 0x1a, 0x0b, 0x0c, 0x0d, 0x16, 0x17},
+            {0x08, 0x11, 0x1a, 0x0b, 0x0c, 0x0d, 0x16, 0x17},
+            {0x08, 0x19, 0x1a, 0x0b, 0x0c, 0x05, 0x16, 0x17},
+            {0x08, 0x19, 0x1a, 0x0b, 0x0c, 0x05, 0x16, 0x17},
+            {0x08, 0x19, 0x1a, 0x0b, 0x0c, 0x05, 0x16, 0x17},
+            {0x08, 0x19, 0x1a, 0x0b, 0x0c, 0x05, 0x16, 0x17},
+            {0x08, 0x19, 0x0a, 0x0b, 0x14, 0x05, 0x16, 0x17},
+            {0x08, 0x19, 0x0a, 0x0b, 0x14, 0x05, 0x16, 0x17},
+            {0x08, 0x19, 0x0a, 0x0b, 0x14, 0x05, 0x16, 0x17},
+            {0x08, 0x19, 0x0a, 0x0b, 0x14, 0x05, 0x16, 0x17},
+            {0x08, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x17},
+            {0x08, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x17},
+            {0x08, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x1f},
+            {0x08, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x1f},
+            {0x08, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x1f},
+            {0x08, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x1f},
+            {0x08, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x1f},
+            {0x08, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x1f},
+            {0x08, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x1f},
+            {0x08, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x1f}
+        },
+        {
+            {0x10, 0x11, 0x1a, 0x1b, 0x0c, 0x0d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x0c, 0x0d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x0c, 0x0d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x0c, 0x0d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x0c, 0x0d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x0c, 0x0d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x0c, 0x0d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x0c, 0x0d, 0x06, 0x17},
+            {0x10, 0x11, 0x1a, 0x1b, 0x0c, 0x0d, 0x06, 0x17},
+            {0x10, 0x11, 0x1a, 0x1b, 0x0c, 0x0d, 0x06, 0x17},
+            {0x10, 0x11, 0x1a, 0x0b, 0x0c, 0x0d, 0x06, 0x17},
+            {0x10, 0x11, 0x1a, 0x0b, 0x0c, 0x0d, 0x06, 0x17},
+            {0x10, 0x11, 0x1a, 0x0b, 0x0c, 0x0d, 0x06, 0x17},
+            {0x10, 0x11, 0x1a, 0x0b, 0x0c, 0x0d, 0x06, 0x17},
+            {0x08, 0x11, 0x1a, 0x0b, 0x0c, 0x0d, 0x16, 0x17},
+            {0x08, 0x11, 0x1a, 0x0b, 0x0c, 0x0d, 0x16, 0x17},
+            {0x08, 0x11, 0x1a, 0x0b, 0x0c, 0x0d, 0x16, 0x17},
+            {0x08, 0x11, 0x1a, 0x0b, 0x0c, 0x0d, 0x16, 0x17},
+            {0x08, 0x19, 0x1a, 0x0b, 0x0c, 0x05, 0x16, 0x17},
+            {0x08, 0x19, 0x1a, 0x0b, 0x0c, 0x05, 0x16, 0x17},
+            {0x08, 0x19, 0x1a, 0x0b, 0x0c, 0x05, 0x16, 0x17},
+            {0x08, 0x19, 0x1a, 0x0b, 0x0c, 0x05, 0x16, 0x17},
+            {0x08, 0x19, 0x0a, 0x0b, 0x14, 0x05, 0x16, 0x17},
+            {0x08, 0x19, 0x0a, 0x0b, 0x14, 0x05, 0x16, 0x17},
+            {0x08, 0x19, 0x0a, 0x0b, 0x14, 0x05, 0x16, 0x17},
+            {0x08, 0x19, 0x0a, 0x0b, 0x14, 0x05, 0x16, 0x17},
+            {0x08, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x17},
+            {0x08, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x17},
+            {0x08, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x17},
+            {0x08, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x1f},
+            {0x08, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x1f},
+            {0x08, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x1f},
+            {0x08, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x1f},
+            {0x08, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x1f},
+            {0x08, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x1f},
+            {0x08, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x1f}
+        },
+        {
+            {0x10, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x17},
+            {0x10, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x17},
+            {0x10, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x17},
+            {0x10, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x17},
+            {0x10, 0x11, 0x1a, 0x0b, 0x04, 0x0d, 0x06, 0x17},
+            {0x10, 0x11, 0x1a, 0x0b, 0x04, 0x0d, 0x06, 0x17},
+            {0x10, 0x11, 0x1a, 0x0b, 0x04, 0x0d, 0x06, 0x17},
+            {0x10, 0x11, 0x1a, 0x0b, 0x04, 0x0d, 0x06, 0x17},
+            {0x18, 0x11, 0x1a, 0x0b, 0x04, 0x0d, 0x16, 0x17},
+            {0x18, 0x11, 0x1a, 0x0b, 0x04, 0x0d, 0x16, 0x17},
+            {0x18, 0x11, 0x1a, 0x0b, 0x04, 0x0d, 0x16, 0x17},
+            {0x18, 0x11, 0x1a, 0x0b, 0x04, 0x0d, 0x16, 0x17},
+            {0x18, 0x19, 0x1a, 0x0b, 0x04, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x1a, 0x0b, 0x04, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x1a, 0x0b, 0x04, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x1a, 0x0b, 0x04, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x0b, 0x14, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x0b, 0x14, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x0b, 0x14, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x0b, 0x14, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x1f},
+            {0x18, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x1f},
+            {0x18, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x1f},
+            {0x18, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x1f},
+            {0x18, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x1f},
+            {0x18, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x1f}
+        },
+        {
+            {0x10, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x17},
+            {0x10, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x17},
+            {0x10, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x17},
+            {0x10, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x17},
+            {0x10, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x17},
+            {0x10, 0x11, 0x1a, 0x0b, 0x04, 0x0d, 0x06, 0x17},
+            {0x10, 0x11, 0x1a, 0x0b, 0x04, 0x0d, 0x06, 0x17},
+            {0x10, 0x11, 0x1a, 0x0b, 0x04, 0x0d, 0x06, 0x17},
+            {0x18, 0x11, 0x1a, 0x0b, 0x04, 0x0d, 0x06, 0x17},
+            {0x18, 0x11, 0x1a, 0x0b, 0x04, 0x0d, 0x16, 0x17},
+            {0x18, 0x11, 0x1a, 0x0b, 0x04, 0x0d, 0x16, 0x17},
+            {0x18, 0x11, 0x1a, 0x0b, 0x04, 0x0d, 0x16, 0x17},
+            {0x18, 0x11, 0x1a, 0x0b, 0x04, 0x0d, 0x16, 0x17},
+            {0x18, 0x19, 0x1a, 0x0b, 0x04, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x1a, 0x0b, 0x04, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x1a, 0x0b, 0x04, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x1a, 0x0b, 0x04, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x0b, 0x04, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x0b, 0x14, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x0b, 0x14, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x0b, 0x14, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x1f},
+            {0x18, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x1f},
+            {0x18, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x1f},
+            {0x18, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x1f},
+            {0x18, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x1f}
+        },
+        {
+            {0x10, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x17},
+            {0x10, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x17},
+            {0x10, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x17},
+            {0x10, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x17},
+            {0x10, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x17},
+            {0x10, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x17},
+            {0x10, 0x11, 0x1a, 0x0b, 0x04, 0x0d, 0x06, 0x17},
+            {0x10, 0x11, 0x1a, 0x0b, 0x04, 0x0d, 0x06, 0x17},
+            {0x18, 0x11, 0x1a, 0x0b, 0x04, 0x0d, 0x06, 0x17},
+            {0x18, 0x11, 0x1a, 0x0b, 0x04, 0x0d, 0x06, 0x17},
+            {0x18, 0x11, 0x1a, 0x0b, 0x04, 0x0d, 0x16, 0x17},
+            {0x18, 0x11, 0x1a, 0x0b, 0x04, 0x0d, 0x16, 0x17},
+            {0x18, 0x11, 0x1a, 0x0b, 0x04, 0x0d, 0x16, 0x17},
+            {0x18, 0x11, 0x1a, 0x0b, 0x04, 0x0d, 0x16, 0x17},
+            {0x18, 0x19, 0x1a, 0x0b, 0x04, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x1a, 0x0b, 0x04, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x1a, 0x0b, 0x04, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x1a, 0x0b, 0x04, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x0b, 0x04, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x0b, 0x04, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x0b, 0x14, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x0b, 0x14, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x1f},
+            {0x18, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x1f},
+            {0x18, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x1f},
+            {0x18, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x1f}
+        },
+        {
+            {0x10, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x17},
+            {0x10, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x17},
+            {0x10, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x17},
+            {0x10, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x17},
+            {0x10, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x17},
+            {0x10, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x17},
+            {0x10, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x17},
+            {0x10, 0x11, 0x1a, 0x0b, 0x04, 0x0d, 0x06, 0x17},
+            {0x18, 0x11, 0x1a, 0x0b, 0x04, 0x0d, 0x06, 0x17},
+            {0x18, 0x11, 0x1a, 0x0b, 0x04, 0x0d, 0x06, 0x17},
+            {0x18, 0x11, 0x1a, 0x0b, 0x04, 0x0d, 0x06, 0x17},
+            {0x18, 0x11, 0x1a, 0x0b, 0x04, 0x0d, 0x16, 0x17},
+            {0x18, 0x11, 0x1a, 0x0b, 0x04, 0x0d, 0x16, 0x17},
+            {0x18, 0x11, 0x1a, 0x0b, 0x04, 0x0d, 0x16, 0x17},
+            {0x18, 0x11, 0x1a, 0x0b, 0x04, 0x0d, 0x16, 0x17},
+            {0x18, 0x19, 0x1a, 0x0b, 0x04, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x1a, 0x0b, 0x04, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x1a, 0x0b, 0x04, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x1a, 0x0b, 0x04, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x0b, 0x04, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x0b, 0x04, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x0b, 0x04, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x0b, 0x14, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x1f},
+            {0x18, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x1f},
+            {0x18, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x1f}
+        },
+        {
+            {0x10, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x17},
+            {0x10, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x17},
+            {0x10, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x17},
+            {0x10, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x17},
+            {0x10, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x17},
+            {0x10, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x17},
+            {0x10, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x17},
+            {0x10, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x17},
+            {0x18, 0x11, 0x1a, 0x0b, 0x04, 0x0d, 0x06, 0x17},
+            {0x18, 0x11, 0x1a, 0x0b, 0x04, 0x0d, 0x06, 0x17},
+            {0x18, 0x11, 0x1a, 0x0b, 0x04, 0x0d, 0x06, 0x17},
+            {0x18, 0x11, 0x1a, 0x0b, 0x04, 0x0d, 0x06, 0x17},
+            {0x18, 0x11, 0x1a, 0x0b, 0x04, 0x0d, 0x16, 0x17},
+            {0x18, 0x11, 0x1a, 0x0b, 0x04, 0x0d, 0x16, 0x17},
+            {0x18, 0x11, 0x1a, 0x0b, 0x04, 0x0d, 0x16, 0x17},
+            {0x18, 0x11, 0x1a, 0x0b, 0x04, 0x0d, 0x16, 0x17},
+            {0x18, 0x19, 0x1a, 0x0b, 0x04, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x1a, 0x0b, 0x04, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x1a, 0x0b, 0x04, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x1a, 0x0b, 0x04, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x0b, 0x04, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x0b, 0x04, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x0b, 0x04, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x0b, 0x04, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x1f},
+            {0x18, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x1f}
+        },
+        {
+            {0x10, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x07},
+            {0x10, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x17},
+            {0x10, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x17},
+            {0x10, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x17},
+            {0x10, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x17},
+            {0x10, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x17},
+            {0x10, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x17},
+            {0x10, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x17},
+            {0x10, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x17},
+            {0x18, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x17},
+            {0x18, 0x11, 0x1a, 0x0b, 0x04, 0x0d, 0x06, 0x17},
+            {0x18, 0x11, 0x1a, 0x0b, 0x04, 0x0d, 0x06, 0x17},
+            {0x18, 0x11, 0x1a, 0x0b, 0x04, 0x0d, 0x06, 0x17},
+            {0x18, 0x11, 0x1a, 0x0b, 0x04, 0x0d, 0x06, 0x17},
+            {0x18, 0x11, 0x1a, 0x0b, 0x04, 0x0d, 0x16, 0x17},
+            {0x18, 0x11, 0x1a, 0x0b, 0x04, 0x0d, 0x16, 0x17},
+            {0x18, 0x11, 0x1a, 0x0b, 0x04, 0x0d, 0x16, 0x17},
+            {0x18, 0x11, 0x1a, 0x0b, 0x04, 0x0d, 0x16, 0x17},
+            {0x18, 0x19, 0x1a, 0x0b, 0x04, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x1a, 0x0b, 0x04, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x1a, 0x0b, 0x04, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x1a, 0x0b, 0x04, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x0b, 0x04, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x0b, 0x04, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x0b, 0x04, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x0b, 0x04, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x03, 0x04, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x1f}
+        },
+        {
+            {0x10, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x17},
+            {0x10, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x17},
+            {0x10, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x17},
+            {0x10, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x17},
+            {0x10, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x17},
+            {0x10, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x17},
+            {0x10, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x17},
+            {0x10, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x17},
+            {0x18, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x17},
+            {0x18, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x17},
+            {0x18, 0x11, 0x1a, 0x0b, 0x04, 0x0d, 0x06, 0x17},
+            {0x18, 0x11, 0x1a, 0x0b, 0x04, 0x0d, 0x06, 0x17},
+            {0x18, 0x11, 0x1a, 0x0b, 0x04, 0x0d, 0x06, 0x17},
+            {0x18, 0x11, 0x1a, 0x0b, 0x04, 0x0d, 0x06, 0x17},
+            {0x18, 0x11, 0x1a, 0x0b, 0x04, 0x0d, 0x16, 0x17},
+            {0x18, 0x11, 0x1a, 0x0b, 0x04, 0x0d, 0x16, 0x17},
+            {0x18, 0x11, 0x1a, 0x0b, 0x04, 0x0d, 0x16, 0x17},
+            {0x18, 0x11, 0x1a, 0x0b, 0x04, 0x0d, 0x16, 0x17},
+            {0x18, 0x19, 0x1a, 0x0b, 0x04, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x1a, 0x0b, 0x04, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x1a, 0x0b, 0x04, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x1a, 0x0b, 0x04, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x0b, 0x04, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x0b, 0x04, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x0b, 0x04, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x0b, 0x04, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x03, 0x04, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x03, 0x04, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x17}
+        },
+        {
+            {0x10, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x17},
+            {0x10, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x17},
+            {0x10, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x17},
+            {0x10, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x17},
+            {0x10, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x17},
+            {0x10, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x17},
+            {0x10, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x17},
+            {0x18, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x17},
+            {0x18, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x17},
+            {0x18, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x17},
+            {0x18, 0x11, 0x1a, 0x0b, 0x04, 0x0d, 0x06, 0x17},
+            {0x18, 0x11, 0x1a, 0x0b, 0x04, 0x0d, 0x06, 0x17},
+            {0x18, 0x11, 0x1a, 0x0b, 0x04, 0x0d, 0x06, 0x17},
+            {0x18, 0x11, 0x1a, 0x0b, 0x04, 0x0d, 0x06, 0x17},
+            {0x18, 0x11, 0x1a, 0x0b, 0x04, 0x0d, 0x16, 0x17},
+            {0x18, 0x11, 0x1a, 0x0b, 0x04, 0x0d, 0x16, 0x17},
+            {0x18, 0x11, 0x1a, 0x0b, 0x04, 0x0d, 0x16, 0x17},
+            {0x18, 0x11, 0x1a, 0x0b, 0x04, 0x0d, 0x16, 0x17},
+            {0x18, 0x19, 0x1a, 0x0b, 0x04, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x1a, 0x0b, 0x04, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x1a, 0x0b, 0x04, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x1a, 0x0b, 0x04, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x0b, 0x04, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x0b, 0x04, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x0b, 0x04, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x0b, 0x04, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x03, 0x04, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x03, 0x04, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x03, 0x04, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x17}
+        },
+        {
+            {0x10, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x17},
+            {0x10, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x17},
+            {0x10, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x17},
+            {0x10, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x17},
+            {0x10, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x17},
+            {0x10, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x17},
+            {0x18, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x17},
+            {0x18, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x17},
+            {0x18, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x17},
+            {0x18, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x17},
+            {0x18, 0x11, 0x1a, 0x0b, 0x04, 0x0d, 0x06, 0x17},
+            {0x18, 0x11, 0x1a, 0x0b, 0x04, 0x0d, 0x06, 0x17},
+            {0x18, 0x11, 0x1a, 0x0b, 0x04, 0x0d, 0x06, 0x17},
+            {0x18, 0x11, 0x1a, 0x0b, 0x04, 0x0d, 0x06, 0x17},
+            {0x18, 0x11, 0x1a, 0x0b, 0x04, 0x0d, 0x16, 0x17},
+            {0x18, 0x11, 0x1a, 0x0b, 0x04, 0x0d, 0x16, 0x17},
+            {0x18, 0x11, 0x1a, 0x0b, 0x04, 0x0d, 0x16, 0x17},
+            {0x18, 0x11, 0x1a, 0x0b, 0x04, 0x0d, 0x16, 0x17},
+            {0x18, 0x19, 0x1a, 0x0b, 0x04, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x1a, 0x0b, 0x04, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x1a, 0x0b, 0x04, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x1a, 0x0b, 0x04, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x0b, 0x04, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x0b, 0x04, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x0b, 0x04, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x0b, 0x04, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x03, 0x04, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x03, 0x04, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x03, 0x04, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x03, 0x04, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x03, 0x04, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x17}
+        },
+        {
+            {0x10, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x17},
+            {0x10, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x17},
+            {0x10, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x17},
+            {0x10, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x17},
+            {0x10, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x17},
+            {0x18, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x17},
+            {0x18, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x17},
+            {0x18, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x17},
+            {0x18, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x17},
+            {0x18, 0x11, 0x1a, 0x1b, 0x04, 0x0d, 0x06, 0x17},
+            {0x18, 0x11, 0x1a, 0x0b, 0x04, 0x0d, 0x06, 0x17},
+            {0x18, 0x11, 0x1a, 0x0b, 0x04, 0x0d, 0x06, 0x17},
+            {0x18, 0x11, 0x1a, 0x0b, 0x04, 0x0d, 0x06, 0x17},
+            {0x18, 0x11, 0x1a, 0x0b, 0x04, 0x0d, 0x06, 0x17},
+            {0x18, 0x11, 0x1a, 0x0b, 0x04, 0x0d, 0x16, 0x17},
+            {0x18, 0x11, 0x1a, 0x0b, 0x04, 0x0d, 0x16, 0x17},
+            {0x18, 0x11, 0x1a, 0x0b, 0x04, 0x0d, 0x16, 0x17},
+            {0x18, 0x11, 0x1a, 0x0b, 0x04, 0x0d, 0x16, 0x17},
+            {0x18, 0x19, 0x1a, 0x0b, 0x04, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x1a, 0x0b, 0x04, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x1a, 0x0b, 0x04, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x1a, 0x0b, 0x04, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x0b, 0x04, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x0b, 0x04, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x0b, 0x04, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x0b, 0x04, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x03, 0x04, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x03, 0x04, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x03, 0x04, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x03, 0x04, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x03, 0x04, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x03, 0x04, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x17},
+            {0x18, 0x19, 0x0a, 0x03, 0x14, 0x05, 0x16, 0x17}
+        }
+    },
+    {   /* 7,200bps map */
+        {
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f}
+        },
+        {
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f}
+        },
+        {
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f}
+        },
+        {
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f}
+        },
+        {
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f}
+        },
+        {
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f}
+        },
+        {
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f}
+        },
+        {
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f}
+        },
+        {
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f}
+        },
+        {
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07}
+        },
+        {
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07}
+        },
+        {
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07}
+        },
+        {
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07}
+        },
+        {
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x03, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x04, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07}
+        },
+        {
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07}
+        },
+        {
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07}
+        },
+        {
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07}
+        },
+        {
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x0a, 0x0b, 0x0c, 0x0d, 0x06, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07}
+        },
+        {
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07}
+        },
+        {
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07}
+        },
+        {
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07}
+        },
+        {
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07}
+        },
+        {
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07}
+        },
+        {
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07}
+        },
+        {
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07}
+        },
+        {
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07}
+        },
+        {
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07}
+        },
+        {
+            {0x00, 0x09, 0x0a, 0x03, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x04, 0x0d, 0x0e, 0x07}
+        },
+        {
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07}
+        },
+        {
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07}
+        },
+        {
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07}
+        },
+        {
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07}
+        },
+        {
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07}
+        },
+        {
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07}
+        },
+        {
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07},
+            {0x08, 0x01, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07}
+        },
+        {
+            {0x00, 0x09, 0x0a, 0x0b, 0x0c, 0x05, 0x06, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x0f},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x00, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x05, 0x0e, 0x07},
+            {0x08, 0x09, 0x02, 0x0b, 0x0c, 0x0d, 0x0e, 0x07}
+        }
+    }
+};
+
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/v17tx.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/v17tx.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,405 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * v17tx.c - ITU V.17 modem transmit part
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2004 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: v17tx.c,v 1.62 2008/07/16 14:23:47 steveu Exp $
+ */
+
+/*! \file */
+
+#if defined(HAVE_CONFIG_H)
+#include <config.h>
+#endif
+
+#include <stdio.h>
+#include <inttypes.h>
+#include <stdlib.h>
+#include <string.h>
+#include "floating_fudge.h"
+#if defined(HAVE_TGMATH_H)
+#include <tgmath.h>
+#endif
+#if defined(HAVE_MATH_H)
+#include <math.h>
+#endif
+
+#include "spandsp/telephony.h"
+#include "spandsp/logging.h"
+#include "spandsp/complex.h"
+#include "spandsp/vector_float.h"
+#include "spandsp/complex_vector_float.h"
+#include "spandsp/async.h"
+#include "spandsp/dds.h"
+#include "spandsp/power_meter.h"
+
+#include "spandsp/v17tx.h"
+
+#if defined(SPANDSP_USE_FIXED_POINT)
+#define SPANDSP_USE_FIXED_POINTx
+#endif
+
+#include "v17tx_constellation_maps.h"
+#if defined(SPANDSP_USE_FIXED_POINT)
+#include "v17tx_fixed_rrc.h"
+#else
+#include "v17tx_floating_rrc.h"
+#endif
+
+#define CARRIER_NOMINAL_FREQ        1800.0f
+
+/* Segments of the training sequence */
+#define V17_TRAINING_SEG_TEP_A      0
+#define V17_TRAINING_SEG_TEP_B      (V17_TRAINING_SEG_TEP_A + 480)
+#define V17_TRAINING_SEG_1          (V17_TRAINING_SEG_TEP_B + 48)
+#define V17_TRAINING_SEG_2          (V17_TRAINING_SEG_1 + 256)
+#define V17_TRAINING_SEG_3          (V17_TRAINING_SEG_2 + 2976)
+#define V17_TRAINING_SEG_4          (V17_TRAINING_SEG_3 + 64)
+#define V17_TRAINING_END            (V17_TRAINING_SEG_4 + 48)
+#define V17_TRAINING_SHUTDOWN_A     (V17_TRAINING_END + 32)
+#define V17_TRAINING_SHUTDOWN_END   (V17_TRAINING_SHUTDOWN_A + 48)
+
+#define V17_TRAINING_SHORT_SEG_4    (V17_TRAINING_SEG_2 + 38)
+
+#define V17_BRIDGE_WORD             0x8880
+
+static __inline__ int scramble(v17_tx_state_t *s, int in_bit)
+{
+    int out_bit;
+
+    out_bit = (in_bit ^ (s->scramble_reg >> 17) ^ (s->scramble_reg >> 22)) & 1;
+    s->scramble_reg = (s->scramble_reg << 1) | out_bit;
+    return out_bit;
+}
+/*- End of function --------------------------------------------------------*/
+
+#if defined(SPANDSP_USE_FIXED_POINT)
+static __inline__ complexi16_t training_get(v17_tx_state_t *s)
+#else
+static __inline__ complexf_t training_get(v17_tx_state_t *s)
+#endif
+{
+    static const int cdba_to_abcd[4] =
+    {
+        2, 3, 1, 0
+    };
+    static const int dibit_to_step[4] =
+    {
+        1, 0, 2, 3
+    };
+    int bits;
+    int shift;
+
+    if (++s->training_step <= V17_TRAINING_SEG_3)
+    {
+        if (s->training_step <= V17_TRAINING_SEG_2)
+        {
+            if (s->training_step <= V17_TRAINING_SEG_TEP_B)
+            {
+                /* Optional segment: Unmodulated carrier (talker echo protection) */
+                return v17_abcd_constellation[0];
+            }
+            if (s->training_step <= V17_TRAINING_SEG_1)
+            {
+                /* Optional segment: silence (talker echo protection) */
+#if defined(SPANDSP_USE_FIXED_POINT)
+                return complex_seti16(0, 0);
+#else
+                return complex_setf(0.0f, 0.0f);
+#endif
+            }
+            /* Segment 1: ABAB... */
+            return v17_abcd_constellation[(s->training_step & 1) ^ 1];
+        }
+        /* Segment 2: CDBA... */
+        /* Apply the scrambler */
+        bits = scramble(s, 1);
+        bits = (bits << 1) | scramble(s, 1);
+        s->constellation_state = cdba_to_abcd[bits];
+        if (s->short_train  &&  s->training_step == V17_TRAINING_SHORT_SEG_4)
+        {
+            /* Go straight to the ones test. */
+            s->training_step = V17_TRAINING_SEG_4;
+        }
+        return v17_abcd_constellation[s->constellation_state];
+    }
+    /* Segment 3: Bridge... */
+    shift = ((s->training_step - V17_TRAINING_SEG_3 - 1) & 0x7) << 1;
+    span_log(&s->logging, SPAN_LOG_FLOW, "Seg 3 shift %d\n", shift);
+    bits = scramble(s, V17_BRIDGE_WORD >> shift);
+    bits = (bits << 1) | scramble(s, V17_BRIDGE_WORD >> (shift + 1));
+    s->constellation_state = (s->constellation_state + dibit_to_step[bits]) & 3;
+    return v17_abcd_constellation[s->constellation_state];
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ int diff_and_convolutional_encode(v17_tx_state_t *s, int q)
+{
+    static const int diff_code[16] =
+    {
+        0, 1, 2, 3, 1, 2, 3, 0, 2, 3, 0, 1, 3, 0, 1, 2
+    };
+    int y1;
+    int y2;
+    int this1;
+    int this2;
+
+    /* Differentially encode */
+    s->diff = diff_code[((q & 0x03) << 2) | s->diff];
+
+    /* Convolutionally encode the redundant bit */
+    y2 = s->diff >> 1;
+    y1 = s->diff;
+    this2 = y2 ^ y1 ^ (s->convolution >> 2) ^ ((y2 ^ (s->convolution >> 1)) & s->convolution);
+    this1 = y2 ^ (s->convolution >> 1) ^ (y1 & s->convolution);
+    s->convolution = ((s->convolution & 1) << 2) | ((this2 & 1) << 1) | (this1 & 1);
+    return ((q << 1) & 0x78) | (s->diff << 1) | ((s->convolution >> 2) & 1);
+}
+/*- End of function --------------------------------------------------------*/
+
+static int fake_get_bit(void *user_data)
+{
+    return 1;
+}
+/*- End of function --------------------------------------------------------*/
+
+#if defined(SPANDSP_USE_FIXED_POINT)
+static __inline__ complexi16_t getbaud(v17_tx_state_t *s)
+#else
+static __inline__ complexf_t getbaud(v17_tx_state_t *s)
+#endif
+{
+    int i;
+    int bit;
+    int bits;
+
+    if (s->in_training)
+    {
+        if (s->training_step <= V17_TRAINING_END)
+        {
+            /* Send the training sequence */
+            if (s->training_step < V17_TRAINING_SEG_4)
+                return training_get(s);
+            /* The last step in training is to send some 1's */
+            if (++s->training_step > V17_TRAINING_END)
+            {
+                /* Training finished - commence normal operation. */
+                s->current_get_bit = s->get_bit;
+                s->in_training = FALSE;
+            }
+        }
+        else
+        {
+            if (++s->training_step > V17_TRAINING_SHUTDOWN_A)
+            {
+                /* The shutdown sequence is 32 bauds of all 1's, then 48 bauds
+                   of silence */
+#if defined(SPANDSP_USE_FIXED_POINT)
+                return complex_seti16(0, 0);
+#else
+                return complex_setf(0.0f, 0.0f);
+#endif
+            }
+            if (s->training_step == V17_TRAINING_SHUTDOWN_END)
+            {
+                if (s->status_handler)
+                    s->status_handler(s->status_user_data, MODEM_TX_STATUS_SHUTDOWN_COMPLETE);
+            }
+        }
+    }
+    bits = 0;
+    for (i = 0;  i < s->bits_per_symbol;  i++)
+    {
+        if ((bit = s->current_get_bit(s->get_bit_user_data)) == PUTBIT_END_OF_DATA)
+        {
+            /* End of real data. Switch to the fake get_bit routine, until we
+               have shut down completely. */
+            if (s->status_handler)
+                s->status_handler(s->status_user_data, MODEM_TX_STATUS_DATA_EXHAUSTED);
+            s->current_get_bit = fake_get_bit;
+            s->in_training = TRUE;
+            bit = 1;
+        }
+        bits |= (scramble(s, bit) << i);
+    }
+    return s->constellation[diff_and_convolutional_encode(s, bits)];
+}
+/*- End of function --------------------------------------------------------*/
+
+int v17_tx(v17_tx_state_t *s, int16_t amp[], int len)
+{
+#if defined(SPANDSP_USE_FIXED_POINT)
+    complexi_t x;
+    complexi_t z;
+#else
+    complexf_t x;
+    complexf_t z;
+#endif
+    int i;
+    int sample;
+
+    if (s->training_step >= V17_TRAINING_SHUTDOWN_END)
+    {
+        /* Once we have sent the shutdown sequence, we stop sending completely. */
+        return 0;
+    }
+    for (sample = 0;  sample < len;  sample++)
+    {
+        if ((s->baud_phase += 3) >= 10)
+        {
+            s->baud_phase -= 10;
+            s->rrc_filter[s->rrc_filter_step] =
+            s->rrc_filter[s->rrc_filter_step + V17_TX_FILTER_STEPS] = getbaud(s);
+            if (++s->rrc_filter_step >= V17_TX_FILTER_STEPS)
+                s->rrc_filter_step = 0;
+        }
+        /* Root raised cosine pulse shaping at baseband */
+#if defined(SPANDSP_USE_FIXED_POINT)
+        x = complex_seti(0, 0);
+        for (i = 0;  i < V17_TX_FILTER_STEPS;  i++)
+        {
+            x.re += (int32_t) tx_pulseshaper[TX_PULSESHAPER_COEFF_SETS - 1 - s->baud_phase][i]*(int32_t) s->rrc_filter[i + s->rrc_filter_step].re;
+            x.im += (int32_t) tx_pulseshaper[TX_PULSESHAPER_COEFF_SETS - 1 - s->baud_phase][i]*(int32_t) s->rrc_filter[i + s->rrc_filter_step].im;
+        }
+        /* Now create and modulate the carrier */
+        x.re >>= 4;
+        x.im >>= 4;
+        z = dds_complexi(&(s->carrier_phase), s->carrier_phase_rate);
+        /* Don't bother saturating. We should never clip. */
+        i = (x.re*z.re - x.im*z.im) >> 15;
+        amp[sample] = (int16_t) ((i*s->gain) >> 15);
+#else
+        x = complex_setf(0.0f, 0.0f);
+        for (i = 0;  i < V17_TX_FILTER_STEPS;  i++)
+        {
+            x.re += tx_pulseshaper[TX_PULSESHAPER_COEFF_SETS - 1 - s->baud_phase][i]*s->rrc_filter[i + s->rrc_filter_step].re;
+            x.im += tx_pulseshaper[TX_PULSESHAPER_COEFF_SETS - 1 - s->baud_phase][i]*s->rrc_filter[i + s->rrc_filter_step].im;
+        }
+        /* Now create and modulate the carrier */
+        z = dds_complexf(&(s->carrier_phase), s->carrier_phase_rate);
+        /* Don't bother saturating. We should never clip. */
+        amp[sample] = (int16_t) lrintf((x.re*z.re - x.im*z.im)*s->gain);
+#endif
+    }
+    return sample;
+}
+/*- End of function --------------------------------------------------------*/
+
+void v17_tx_power(v17_tx_state_t *s, float power)
+{
+    /* The constellation design seems to keep the average power the same, regardless
+       of which bit rate is in use. */
+#if defined(SPANDSP_USE_FIXED_POINT)
+    s->gain = 0.223f*powf(10.0f, (power - DBM0_MAX_POWER)/20.0f)*16.0f*(32767.0f/30672.52f)*32768.0f/TX_PULSESHAPER_GAIN;
+#else
+    s->gain = 0.223f*powf(10.0f, (power - DBM0_MAX_POWER)/20.0f)*32768.0f/TX_PULSESHAPER_GAIN;
+#endif
+}
+/*- End of function --------------------------------------------------------*/
+
+void v17_tx_set_get_bit(v17_tx_state_t *s, get_bit_func_t get_bit, void *user_data)
+{
+    if (s->get_bit == s->current_get_bit)
+        s->current_get_bit = get_bit;
+    s->get_bit = get_bit;
+    s->get_bit_user_data = user_data;
+}
+/*- End of function --------------------------------------------------------*/
+
+void v17_tx_set_modem_status_handler(v17_tx_state_t *s, modem_tx_status_func_t handler, void *user_data)
+{
+    s->status_handler = handler;
+    s->status_user_data = user_data;
+}
+/*- End of function --------------------------------------------------------*/
+
+int v17_tx_restart(v17_tx_state_t *s, int bit_rate, int tep, int short_train)
+{
+    switch (bit_rate)
+    {
+    case 14400:
+        s->bits_per_symbol = 6;
+        s->constellation = v17_14400_constellation;
+        break;
+    case 12000:
+        s->bits_per_symbol = 5;
+        s->constellation = v17_12000_constellation;
+        break;
+    case 9600:
+        s->bits_per_symbol = 4;
+        s->constellation = v17_9600_constellation;
+        break;
+    case 7200:
+        s->bits_per_symbol = 3;
+        s->constellation = v17_7200_constellation;
+        break;
+    default:
+        return -1;
+    }
+    /* NB: some modems seem to use 3 instead of 1 for long training */
+    s->diff = (short_train)  ?  0  :  1;
+    s->bit_rate = bit_rate;
+#if defined(SPANDSP_USE_FIXED_POINT)
+    memset(s->rrc_filter, 0, sizeof(s->rrc_filter));
+#else
+    cvec_zerof(s->rrc_filter, sizeof(s->rrc_filter)/sizeof(s->rrc_filter[0]));
+#endif
+    s->rrc_filter_step = 0;
+    s->convolution = 0;
+    s->scramble_reg = 0x2ECDD5;
+    s->in_training = TRUE;
+    s->short_train = short_train;
+    s->training_step = (tep)  ?  V17_TRAINING_SEG_TEP_A  :  V17_TRAINING_SEG_1;
+    s->carrier_phase = 0;
+    s->baud_phase = 0;
+    s->constellation_state = 0;
+    s->current_get_bit = fake_get_bit;
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+v17_tx_state_t *v17_tx_init(v17_tx_state_t *s, int bit_rate, int tep, get_bit_func_t get_bit, void *user_data)
+{
+    if (s == NULL)
+    {
+        if ((s = (v17_tx_state_t *) malloc(sizeof(*s))) == NULL)
+            return NULL;
+    }
+    memset(s, 0, sizeof(*s));
+    span_log_init(&s->logging, SPAN_LOG_NONE, NULL);
+    span_log_set_protocol(&s->logging, "V.17 TX");
+    s->get_bit = get_bit;
+    s->get_bit_user_data = user_data;
+    s->carrier_phase_rate = dds_phase_ratef(CARRIER_NOMINAL_FREQ);
+    v17_tx_power(s, -14.0f);
+    v17_tx_restart(s, bit_rate, tep, FALSE);
+    return s;
+}
+/*- End of function --------------------------------------------------------*/
+
+int v17_tx_free(v17_tx_state_t *s)
+{
+    free(s);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/v17tx_constellation_maps.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/v17tx_constellation_maps.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,313 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * v17tx_constellation_maps.h - ITU V.17 modem transmit part.
+ *                              Constellation mapping.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2004 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: v17tx_constellation_maps.h,v 1.1 2008/05/02 14:44:08 steveu Exp $
+ */
+
+#if defined(SPANDSP_USE_FIXED_POINTx)
+static const complexi16_t v17_abcd_constellation[4] =
+#else
+static const complexf_t v17_abcd_constellation[4] =
+#endif
+{
+    {-6, -2},
+    { 2, -6},
+    { 6,  2},
+    {-2,  6}
+};
+
+#if defined(SPANDSP_USE_FIXED_POINTx)
+static const complexi16_t v17_14400_constellation[128] =
+#else
+static const complexf_t v17_14400_constellation[128] =
+#endif
+{
+    {-8, -3},       /* 0x00 */
+    { 9,  2},       /* 0x01 */
+    { 2, -9},       /* 0x02 */
+    {-3,  8},       /* 0x03 */
+    { 8,  3},       /* 0x04 */
+    {-9, -2},       /* 0x05 */
+    {-2,  9},       /* 0x06 */
+    { 3, -8},       /* 0x07 */
+    {-8,  1},       /* 0x08 */
+    { 9, -2},       /* 0x09 */
+    {-2, -9},       /* 0x0A */
+    { 1,  8},       /* 0x0B */
+    { 8, -1},       /* 0x0C */
+    {-9,  2},       /* 0x0D */
+    { 2,  9},       /* 0x0E */
+    {-1, -8},       /* 0x0F */
+    {-4, -3},       /* 0x10 */
+    { 5,  2},       /* 0x11 */
+    { 2, -5},       /* 0x12 */
+    {-3,  4},       /* 0x13 */
+    { 4,  3},       /* 0x14 */
+    {-5, -2},       /* 0x15 */
+    {-2,  5},       /* 0x16 */
+    { 3, -4},       /* 0x17 */
+    {-4,  1},       /* 0x18 */
+    { 5, -2},       /* 0x19 */
+    {-2, -5},       /* 0x1A */
+    { 1,  4},       /* 0x1B */
+    { 4, -1},       /* 0x1C */
+    {-5,  2},       /* 0x1D */
+    { 2,  5},       /* 0x1E */
+    {-1, -4},       /* 0x1F */
+    { 4, -3},       /* 0x20 */
+    {-3,  2},       /* 0x21 */
+    { 2,  3},       /* 0x22 */
+    {-3, -4},       /* 0x23 */
+    {-4,  3},       /* 0x24 */
+    { 3, -2},       /* 0x25 */
+    {-2, -3},       /* 0x26 */
+    { 3,  4},       /* 0x27 */
+    { 4,  1},       /* 0x28 */
+    {-3, -2},       /* 0x29 */
+    {-2,  3},       /* 0x2A */
+    { 1, -4},       /* 0x2B */
+    {-4, -1},       /* 0x2C */
+    { 3,  2},       /* 0x2D */
+    { 2, -3},       /* 0x2E */
+    {-1,  4},       /* 0x2F */
+    { 0, -3},       /* 0x30 */
+    { 1,  2},       /* 0x31 */
+    { 2, -1},       /* 0x32 */
+    {-3,  0},       /* 0x33 */
+    { 0,  3},       /* 0x34 */
+    {-1, -2},       /* 0x35 */
+    {-2,  1},       /* 0x36 */
+    { 3,  0},       /* 0x37 */
+    { 0,  1},       /* 0x38 */
+    { 1, -2},       /* 0x39 */
+    {-2, -1},       /* 0x3A */
+    { 1,  0},       /* 0x3B */
+    { 0, -1},       /* 0x3C */
+    {-1,  2},       /* 0x3D */
+    { 2,  1},       /* 0x3E */
+    {-1,  0},       /* 0x3F */
+    { 8, -3},       /* 0x40 */
+    {-7,  2},       /* 0x41 */
+    { 2,  7},       /* 0x42 */
+    {-3, -8},       /* 0x43 */
+    {-8,  3},       /* 0x44 */
+    { 7, -2},       /* 0x45 */
+    {-2, -7},       /* 0x46 */
+    { 3,  8},       /* 0x47 */
+    { 8,  1},       /* 0x48 */
+    {-7, -2},       /* 0x49 */
+    {-2,  7},       /* 0x4A */
+    { 1, -8},       /* 0x4B */
+    {-8, -1},       /* 0x4C */
+    { 7,  2},       /* 0x4D */
+    { 2, -7},       /* 0x4E */
+    {-1,  8},       /* 0x4F */
+    {-4, -7},       /* 0x50 */
+    { 5,  6},       /* 0x51 */
+    { 6, -5},       /* 0x52 */
+    {-7,  4},       /* 0x53 */
+    { 4,  7},       /* 0x54 */
+    {-5, -6},       /* 0x55 */
+    {-6,  5},       /* 0x56 */
+    { 7, -4},       /* 0x57 */
+    {-4,  5},       /* 0x58 */
+    { 5, -6},       /* 0x59 */
+    {-6, -5},       /* 0x5A */
+    { 5,  4},       /* 0x5B */
+    { 4, -5},       /* 0x5C */
+    {-5,  6},       /* 0x5D */
+    { 6,  5},       /* 0x5E */
+    {-5, -4},       /* 0x5F */
+    { 4, -7},       /* 0x60 */
+    {-3,  6},       /* 0x61 */
+    { 6,  3},       /* 0x62 */
+    {-7, -4},       /* 0x63 */
+    {-4,  7},       /* 0x64 */
+    { 3, -6},       /* 0x65 */
+    {-6, -3},       /* 0x66 */
+    { 7,  4},       /* 0x67 */
+    { 4,  5},       /* 0x68 */
+    {-3, -6},       /* 0x69 */
+    {-6,  3},       /* 0x6A */
+    { 5, -4},       /* 0x6B */
+    {-4, -5},       /* 0x6C */
+    { 3,  6},       /* 0x6D */
+    { 6, -3},       /* 0x6E */
+    {-5,  4},       /* 0x6F */
+    { 0, -7},       /* 0x70 */
+    { 1,  6},       /* 0x71 */
+    { 6, -1},       /* 0x72 */
+    {-7,  0},       /* 0x73 */
+    { 0,  7},       /* 0x74 */
+    {-1, -6},       /* 0x75 */
+    {-6,  1},       /* 0x76 */
+    { 7,  0},       /* 0x77 */
+    { 0,  5},       /* 0x78 */
+    { 1, -6},       /* 0x79 */
+    {-6, -1},       /* 0x7A */
+    { 5,  0},       /* 0x7B */
+    { 0, -5},       /* 0x7C */
+    {-1,  6},       /* 0x7D */
+    { 6,  1},       /* 0x7E */
+    {-5,  0}        /* 0x7F */
+};
+
+#if defined(SPANDSP_USE_FIXED_POINTx)
+static const complexi16_t v17_12000_constellation[64] =
+#else
+static const complexf_t v17_12000_constellation[64] =
+#endif
+{
+    { 7,  1},       /* 0x00 */
+    {-5, -1},       /* 0x01 */
+    {-1,  5},       /* 0x02 */
+    { 1, -7},       /* 0x03 */
+    {-7, -1},       /* 0x04 */
+    { 5,  1},       /* 0x05 */
+    { 1, -5},       /* 0x06 */
+    {-1,  7},       /* 0x07 */
+    { 3, -3},       /* 0x08 */
+    {-1,  3},       /* 0x09 */
+    { 3,  1},       /* 0x0A */
+    {-3, -3},       /* 0x0B */
+    {-3,  3},       /* 0x0C */
+    { 1, -3},       /* 0x0D */
+    {-3, -1},       /* 0x0E */
+    { 3,  3},       /* 0x0F */
+    { 7, -7},       /* 0x10 */
+    {-5,  7},       /* 0x11 */
+    { 7,  5},       /* 0x12 */
+    {-7, -7},       /* 0x13 */
+    {-7,  7},       /* 0x14 */
+    { 5, -7},       /* 0x15 */
+    {-7, -5},       /* 0x16 */
+    { 7,  7},       /* 0x17 */
+    {-1, -7},       /* 0x18 */
+    { 3,  7},       /* 0x19 */
+    { 7, -3},       /* 0x1A */
+    {-7,  1},       /* 0x1B */
+    { 1,  7},       /* 0x1C */
+    {-3, -7},       /* 0x1D */
+    {-7,  3},       /* 0x1E */
+    { 7, -1},       /* 0x1F */
+    { 3,  5},       /* 0x20 */
+    {-1, -5},       /* 0x21 */
+    {-5,  1},       /* 0x22 */
+    { 5, -3},       /* 0x23 */
+    {-3, -5},       /* 0x24 */
+    { 1,  5},       /* 0x25 */
+    { 5, -1},       /* 0x26 */
+    {-5,  3},       /* 0x27 */
+    {-1,  1},       /* 0x28 */
+    { 3, -1},       /* 0x29 */
+    {-1, -3},       /* 0x2A */
+    { 1,  1},       /* 0x2B */
+    { 1, -1},       /* 0x2C */
+    {-3,  1},       /* 0x2D */
+    { 1,  3},       /* 0x2E */
+    {-1, -1},       /* 0x2F */
+    {-5,  5},       /* 0x30 */
+    { 7, -5},       /* 0x31 */
+    {-5, -7},       /* 0x32 */
+    { 5,  5},       /* 0x33 */
+    { 5, -5},       /* 0x34 */
+    {-7,  5},       /* 0x35 */
+    { 5,  7},       /* 0x36 */
+    {-5, -5},       /* 0x37 */
+    {-5, -3},       /* 0x38 */
+    { 7,  3},       /* 0x39 */
+    { 3, -7},       /* 0x3A */
+    {-3,  5},       /* 0x3B */
+    { 5,  3},       /* 0x3C */
+    {-7, -3},       /* 0x3D */
+    {-3,  7},       /* 0x3E */
+    { 3, -5}        /* 0x3F */
+};
+
+#if defined(SPANDSP_USE_FIXED_POINTx)
+static const complexi16_t v17_9600_constellation[32] =
+#else
+static const complexf_t v17_9600_constellation[32] =
+#endif
+{
+    {-8,  2},       /* 0x00 */
+    {-6, -4},       /* 0x01 */
+    {-4,  6},       /* 0x02 */
+    { 2,  8},       /* 0x03 */
+    { 8, -2},       /* 0x04 */
+    { 6,  4},       /* 0x05 */
+    { 4, -6},       /* 0x06 */
+    {-2, -8},       /* 0x07 */
+    { 0,  2},       /* 0x08 */
+    {-6,  4},       /* 0x09 */
+    { 4,  6},       /* 0x0A */
+    { 2,  0},       /* 0x0B */
+    { 0, -2},       /* 0x0C */
+    { 6, -4},       /* 0x0D */
+    {-4, -6},       /* 0x0E */
+    {-2,  0},       /* 0x0F */
+    { 0, -6},       /* 0x10 */
+    { 2, -4},       /* 0x11 */
+    {-4, -2},       /* 0x12 */
+    {-6,  0},       /* 0x13 */
+    { 0,  6},       /* 0x14 */
+    {-2,  4},       /* 0x15 */
+    { 4,  2},       /* 0x16 */
+    { 6,  0},       /* 0x17 */
+    { 8,  2},       /* 0x18 */
+    { 2,  4},       /* 0x19 */
+    { 4, -2},       /* 0x1A */
+    { 2, -8},       /* 0x1B */
+    {-8, -2},       /* 0x1C */
+    {-2, -4},       /* 0x1D */
+    {-4,  2},       /* 0x1E */
+    {-2,  8}        /* 0x1F */
+};
+
+#if defined(SPANDSP_USE_FIXED_POINTx)
+static const complexi16_t v17_7200_constellation[16] =
+#else
+static const complexf_t v17_7200_constellation[16] =
+#endif
+{
+    { 6, -6},       /* 0x00 */
+    {-2,  6},       /* 0x01 */
+    { 6,  2},       /* 0x02 */
+    {-6, -6},       /* 0x03 */
+    {-6,  6},       /* 0x04 */
+    { 2, -6},       /* 0x05 */
+    {-6, -2},       /* 0x06 */
+    { 6,  6},       /* 0x07 */
+    {-2,  2},       /* 0x08 */
+    { 6, -2},       /* 0x09 */
+    {-2, -6},       /* 0x0A */
+    { 2,  2},       /* 0x0B */
+    { 2, -2},       /* 0x0C */
+    {-6,  2},       /* 0x0D */
+    { 2,  6},       /* 0x0E */
+    {-2, -2}        /* 0x0F */
+};
+
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/v22bis_rx.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/v22bis_rx.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,721 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * v22bis_rx.c - ITU V.22bis modem receive part
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2004 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: v22bis_rx.c,v 1.40 2008/07/25 13:56:54 steveu Exp $
+ */
+
+/*! \file */
+
+/* THIS IS A WORK IN PROGRESS - NOT YET FUNCTIONAL! */
+
+#if defined(HAVE_CONFIG_H)
+#include <config.h>
+#endif
+
+#include <inttypes.h>
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include "floating_fudge.h"
+#if defined(HAVE_TGMATH_H)
+#include <tgmath.h>
+#endif
+#if defined(HAVE_MATH_H)
+#include <math.h>
+#endif
+
+#include "spandsp/telephony.h"
+#include "spandsp/logging.h"
+#include "spandsp/vector_float.h"
+#include "spandsp/async.h"
+#include "spandsp/power_meter.h"
+#include "spandsp/arctan2.h"
+#include "spandsp/complex.h"
+#include "spandsp/dds.h"
+#include "spandsp/complex_filters.h"
+
+#include "spandsp/v29rx.h"
+#include "spandsp/v22bis.h"
+
+#if defined(SPANDSP_USE_FIXED_POINT)
+#include "v22bis_rx_1200_floating_rrc.h"
+#include "v22bis_rx_2400_floating_rrc.h"
+#else
+#include "v22bis_rx_1200_floating_rrc.h"
+#include "v22bis_rx_2400_floating_rrc.h"
+#endif
+
+#define ms_to_symbols(t)        (((t)*600)/1000)
+
+#define EQUALIZER_DELTA         0.25f
+#define PULSESHAPER_COEFF_SETS  12
+
+/*
+The basic method used by the V.22bis receiver is:
+
+    Put each sample into the pulse-shaping and phase shift filter buffer
+
+    At T/2 rate:
+        Filter and demodulate the contents of the input filter buffer, producing a sample
+        in the equalizer filter buffer.
+
+        Tune the symbol timing based on the latest 3 samples in the equalizer buffer. This
+        updates the decision points for taking the T/2 samples.
+
+        Equalize the contents of the equalizer buffer, producing a demodulated constellation
+        point.
+
+        Find the nearest constellation point to the received position. This is our received
+        symbol.
+
+        Tune the local carrier, based on the angular mismatch between the actual signal and
+        the decision.
+        
+        Tune the equalizer, based on the mismatch between the actual signal and the decision.
+
+        Descramble and output the bits represented by the decision.
+*/
+
+enum
+{
+    V22BIS_TRAINING_STAGE_NORMAL_OPERATION,
+    V22BIS_TRAINING_STAGE_SYMBOL_ACQUISITION,
+    V22BIS_TRAINING_STAGE_LOG_PHASE,
+    V22BIS_TRAINING_STAGE_UNSCRAMBLED_ONES,
+    V22BIS_TRAINING_STAGE_UNSCRAMBLED_0011,
+    V22BIS_TRAINING_STAGE_SCRAMBLED_ONES_AT_1200,
+    V22BIS_TRAINING_STAGE_SCRAMBLED_ONES_AT_2400,
+    V22BIS_TRAINING_STAGE_WAIT_FOR_START_1,
+    V22BIS_TRAINING_STAGE_WAIT_FOR_START_2,
+    V22BIS_TRAINING_STAGE_PARKED
+};
+
+static const uint8_t space_map_v22bis[6][6] =
+{
+    {11,  9,  9,  6,  6,  7},
+    {10,  8,  8,  4,  4,  5},
+    {10,  8,  8,  4,  4,  5},
+    {13, 12, 12,  0,  0,  2},
+    {13, 12, 12,  0,  0,  2},
+    {15, 14, 14,  1,  1,  3}
+};
+
+float v22bis_rx_carrier_frequency(v22bis_state_t *s)
+{
+    return dds_frequencyf(s->rx.carrier_phase_rate);
+}
+/*- End of function --------------------------------------------------------*/
+
+float v22bis_symbol_timing_correction(v22bis_state_t *s)
+{
+    return (float) s->rx.total_baud_timing_correction/((float) PULSESHAPER_COEFF_SETS*40.0f/(3.0f*2.0f));
+}
+/*- End of function --------------------------------------------------------*/
+
+float v22bis_rx_signal_power(v22bis_state_t *s)
+{
+    return power_meter_current_dbm0(&s->rx.rx_power);
+}
+/*- End of function --------------------------------------------------------*/
+
+int v22bis_equalizer_state(v22bis_state_t *s, complexf_t **coeffs)
+{
+    *coeffs = s->rx.eq_coeff;
+    return 2*V22BIS_EQUALIZER_LEN + 1;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void equalizer_reset(v22bis_state_t *s)
+{
+    int i;
+
+    /* Start with an equalizer based on everything being perfect */
+    for (i = 0;  i < 2*V22BIS_EQUALIZER_LEN + 1;  i++)
+        s->rx.eq_coeff[i] = complex_setf(0.0f, 0.0f);
+    s->rx.eq_coeff[V22BIS_EQUALIZER_LEN] = complex_setf(3.0f, 0.0f);
+    for (i = 0;  i <= V22BIS_EQUALIZER_MASK;  i++)
+        s->rx.eq_buf[i] = complex_setf(0.0f, 0.0f);
+
+    s->rx.eq_coeff[V22BIS_EQUALIZER_LEN - 6].re = -0.02f;
+    s->rx.eq_coeff[V22BIS_EQUALIZER_LEN - 5].re =  0.035f;
+    s->rx.eq_coeff[V22BIS_EQUALIZER_LEN - 4].re =  0.08f;
+    s->rx.eq_coeff[V22BIS_EQUALIZER_LEN - 3].re = -0.30f;
+    s->rx.eq_coeff[V22BIS_EQUALIZER_LEN - 2].re = -0.37f;
+    s->rx.eq_coeff[V22BIS_EQUALIZER_LEN - 1].re =  0.09f;
+    s->rx.eq_coeff[V22BIS_EQUALIZER_LEN].re     =  3.19f;
+    s->rx.eq_coeff[V22BIS_EQUALIZER_LEN + 1].re =  0.09f;
+    s->rx.eq_coeff[V22BIS_EQUALIZER_LEN + 2].re = -0.37f;
+    s->rx.eq_coeff[V22BIS_EQUALIZER_LEN + 3].re = -0.30f;
+    s->rx.eq_coeff[V22BIS_EQUALIZER_LEN + 5].re =  0.035f;
+    s->rx.eq_coeff[V22BIS_EQUALIZER_LEN + 6].re = -0.02f;
+
+    s->rx.eq_put_step = 20 - 1;
+    s->rx.eq_step = 0;
+    s->rx.eq_delta = EQUALIZER_DELTA/(2*V22BIS_EQUALIZER_LEN + 1);
+}
+/*- End of function --------------------------------------------------------*/
+
+static complexf_t equalizer_get(v22bis_state_t *s)
+{
+    int i;
+    int p;
+    complexf_t z;
+    complexf_t z1;
+
+    /* Get the next equalized value. */
+    z = complex_setf(0.0f, 0.0f);
+    p = s->rx.eq_step - 1;
+    for (i = 0;  i < 2*V22BIS_EQUALIZER_LEN + 1;  i++)
+    {
+        p = (p - 1) & V22BIS_EQUALIZER_MASK;
+        z1 = complex_mulf(&s->rx.eq_coeff[i], &s->rx.eq_buf[p]);
+        z = complex_addf(&z, &z1);
+    }
+    return z;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void tune_equalizer(v22bis_state_t *s, const complexf_t *z, const complexf_t *target)
+{
+    int i;
+    int p;
+    complexf_t ez;
+    complexf_t z1;
+
+    /* Find the x and y mismatch from the exact constellation position. */
+    ez = complex_subf(target, z);
+    ez.re *= s->rx.eq_delta;
+    ez.im *= s->rx.eq_delta;
+
+    p = s->rx.eq_step - 1;
+    for (i = 0;  i < 2*V22BIS_EQUALIZER_LEN + 1;  i++)
+    {
+        p = (p - 1) & V22BIS_EQUALIZER_MASK;
+        z1 = complex_conjf(&s->rx.eq_buf[p]);
+        z1 = complex_mulf(&ez, &z1);
+        s->rx.eq_coeff[i] = complex_addf(&s->rx.eq_coeff[i], &z1);
+        /* If we don't leak a little bit we seem to get some wandering adaption */
+        s->rx.eq_coeff[i].re *= 0.9999f;
+        s->rx.eq_coeff[i].im *= 0.9999f;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ void track_carrier(v22bis_state_t *s, const complexf_t *z, const complexf_t *target)
+{
+    float error;
+
+    /* For small errors the imaginary part of the difference between the actual and the target
+       positions is proportional to the phase error, for any particular target. However, the
+       different amplitudes of the various target positions scale things. */
+    error = z->im*target->re - z->re*target->im;
+    
+    s->rx.carrier_phase_rate += (int32_t) (s->rx.carrier_track_i*error);
+    s->rx.carrier_phase += (int32_t) (s->rx.carrier_track_p*error);
+    //span_log(&s->logging, SPAN_LOG_FLOW, "Im = %15.5f   f = %15.5f\n", error, dds_frequencyf(s->rx.carrier_phase_rate));
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ void put_bit(v22bis_state_t *s, int bit)
+{
+    int out_bit;
+
+    bit &= 1;
+
+    /* Descramble the bit */
+    s->rx.scramble_reg = (s->rx.scramble_reg << 1) | bit;
+    out_bit = (bit ^ (s->rx.scramble_reg >> 15) ^ (s->rx.scramble_reg >> 18)) & 1;
+    if (s->rx.scrambler_pattern_count >= 64)
+    {
+        out_bit ^= 1;
+        s->rx.scrambler_pattern_count = 0;
+    }
+    if (bit)
+        s->rx.scrambler_pattern_count++;
+    else
+        s->rx.scrambler_pattern_count = 0;
+
+    s->put_bit(s->user_data, out_bit);
+}
+/*- End of function --------------------------------------------------------*/
+
+static void decode_baud(v22bis_state_t *s, int nearest)
+{
+    static const uint8_t phase_steps[4] =
+    {
+        1, 0, 2, 3
+    };
+    int raw_bits;
+
+    raw_bits = phase_steps[((nearest - s->rx.constellation_state) >> 2) & 3];
+    /* The first two bits are the quadrant */
+    put_bit(s, raw_bits);
+    put_bit(s, raw_bits >> 1);
+    if (s->bit_rate == 2400)
+    {
+        /* The other two bits are the position within the quadrant */
+        put_bit(s, nearest >> 1);
+        put_bit(s, nearest);
+    }
+    s->rx.constellation_state = nearest;
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ int find_quadrant(const complexf_t *z)
+{
+    int b1;
+    int b2;
+
+#if 0
+    /* Split along the axes, as follows:
+        1  0
+        2  3
+     */
+    b1 = (z->re <= 0.0f);
+    b2 = (z->im <= 0.0f);
+    return (b2 << 1) | (b1 ^ b2);
+#endif    
+    /* Split the space along the two diagonals, as follows:
+         \ 1 /
+        2     0
+         / 3 \
+     */
+    b1 = (z->im > z->re);
+    b2 = (z->im < -z->re);
+    return (b2 << 1) | (b1 ^ b2);
+}
+/*- End of function --------------------------------------------------------*/
+
+static void process_half_baud(v22bis_state_t *s, const complexf_t *sample)
+{
+    complexf_t a;
+    complexf_t b;
+    complexf_t c;
+
+    complexf_t z;
+    complexf_t zz;
+    const complexf_t *target;
+    float p;
+    float q;
+    int re;
+    int im;
+    int nearest;
+
+    z.re = sample->re;
+    z.im = sample->im;
+
+    /* Add a sample to the equalizer's circular buffer, but don't calculate anything
+       at this time. */
+    s->rx.eq_buf[s->rx.eq_step] = z;
+    s->rx.eq_step = (s->rx.eq_step + 1) & V22BIS_EQUALIZER_MASK;
+
+    /* On alternate insertions we have a whole baud and must process it. */
+    if ((s->rx.baud_phase ^= 1))
+        return;
+
+    /* Perform a Gardner test for baud alignment on the three most recent samples. */
+#if 0
+    p = s->rx.eq_buf[(s->rx.eq_step - 3) & V22BIS_EQUALIZER_MASK].re
+      - s->rx.eq_buf[(s->rx.eq_step - 1) & V22BIS_EQUALIZER_MASK].re;
+    p *= s->rx.eq_buf[(s->rx.eq_step - 2) & V22BIS_EQUALIZER_MASK].re;
+
+    q = s->rx.eq_buf[(s->rx.eq_step - 3) & V22BIS_EQUALIZER_MASK].im
+      - s->rx.eq_buf[(s->rx.eq_step - 1) & V22BIS_EQUALIZER_MASK].im;
+    q *= s->rx.eq_buf[(s->rx.eq_step - 2) & V22BIS_EQUALIZER_MASK].im;
+#else
+    if (s->rx.sixteen_way_decisions)
+    {
+        p = s->rx.eq_buf[(s->rx.eq_step - 3) & V22BIS_EQUALIZER_MASK].re
+          - s->rx.eq_buf[(s->rx.eq_step - 1) & V22BIS_EQUALIZER_MASK].re;
+        p *= s->rx.eq_buf[(s->rx.eq_step - 2) & V22BIS_EQUALIZER_MASK].re;
+
+        q = s->rx.eq_buf[(s->rx.eq_step - 3) & V22BIS_EQUALIZER_MASK].im
+        - s->rx.eq_buf[(s->rx.eq_step - 1) & V22BIS_EQUALIZER_MASK].im;
+        q *= s->rx.eq_buf[(s->rx.eq_step - 2) & V22BIS_EQUALIZER_MASK].im;
+    }
+    else
+    {
+        /* Rotate the points to the 45 degree positions, to maximise the effectiveness of the Gardner algorithm */
+        zz = complex_setf(cosf(26.57f*3.14159f/180.0f), sinf(26.57f*3.14159f/180.0f));
+        a = complex_mulf(&s->rx.eq_buf[(s->rx.eq_step - 3) & V22BIS_EQUALIZER_MASK], &zz);
+        b = complex_mulf(&s->rx.eq_buf[(s->rx.eq_step - 2) & V22BIS_EQUALIZER_MASK], &zz);
+        c = complex_mulf(&s->rx.eq_buf[(s->rx.eq_step - 1) & V22BIS_EQUALIZER_MASK], &zz);
+        p = (a.re - c.re)*b.re;
+        q = (a.im - c.im)*b.im;
+    }
+#endif
+
+    p += q;
+    s->rx.gardner_integrate += ((p + q) > 0.0f)  ?  s->rx.gardner_step  :  -s->rx.gardner_step;
+
+    if (abs(s->rx.gardner_integrate) >= 16)
+    {
+        /* This integrate and dump approach avoids rapid changes of the equalizer put step.
+           Rapid changes, without hysteresis, are bad. They degrade the equalizer performance
+           when the true symbol boundary is close to a sample boundary. */
+        s->rx.eq_put_step += (s->rx.gardner_integrate/16);
+        s->rx.total_baud_timing_correction += (s->rx.gardner_integrate/16);
+span_log(&s->logging, SPAN_LOG_FLOW, "Gardner kick %d [total %d]\n", s->rx.gardner_integrate, s->rx.total_baud_timing_correction);
+        if (s->rx.qam_report)
+            s->rx.qam_report(s->rx.qam_user_data, NULL, NULL, s->rx.gardner_integrate);
+        s->rx.gardner_integrate = 0;
+    }
+
+    z = equalizer_get(s);
+printf("VVV %15.5f %15.5f\n", z.re, z.im);
+
+span_log(&s->logging, SPAN_LOG_FLOW, "VVV %p %d\n", s->user_data, s->rx.training);
+    if (s->rx.sixteen_way_decisions)
+    {
+        re = (int) (z.re + 3.0f);
+        if (re > 5)
+            re = 5;
+        else if (re < 0)
+            re = 0;
+        im = (int) (z.im + 3.0f);
+        if (im > 5)
+            im = 5;
+        else if (im < 0)
+            im = 0;
+        nearest = space_map_v22bis[re][im];
+    }
+    else
+    {
+        zz = complex_setf(3.0f/sqrtf(10.0f), -1.0f/sqrtf(10.0f));
+        zz = complex_mulf(&z, &zz);
+        nearest = (find_quadrant(&zz) << 2) | 0x01;
+        printf("Trackit %15.5f %15.5f     %15.5f %15.5f   %d\n", z.re, z.im, zz.re, zz.im, nearest);
+    }
+
+    switch (s->rx.training)
+    {
+    case V22BIS_TRAINING_STAGE_NORMAL_OPERATION:
+        /* Normal operation. */
+        track_carrier(s, &z, &v22bis_constellation[nearest]);
+        tune_equalizer(s, &z, &v22bis_constellation[nearest]);
+        decode_baud(s, nearest);
+        target = &v22bis_constellation[s->rx.constellation_state];
+        break;
+    case V22BIS_TRAINING_STAGE_SYMBOL_ACQUISITION:
+        /* Allow time for the Gardner algorithm to settle the symbol timing. */
+        target = &z;
+        if (++s->rx.training_count >= 40)
+        {
+            s->rx.gardner_step = 4;
+            if (s->caller)
+            {
+                s->rx.training = V22BIS_TRAINING_STAGE_UNSCRAMBLED_ONES;
+            }
+            else
+            {
+                if (s->bit_rate == 2400)
+                    s->rx.training = V22BIS_TRAINING_STAGE_UNSCRAMBLED_0011;
+                else
+                    s->rx.training = V22BIS_TRAINING_STAGE_SCRAMBLED_ONES_AT_1200;
+            }
+            break;
+        }
+
+        /* QAM and Gardner only play nicely with heavy damping, so we need to change to
+           a slow rate of symbol timing adaption. However, it must not be so slow that it
+           cannot track the worst case timing error specified in V.22bis. This should be 0.01%,
+           but since we might be off in the opposite direction from the source, the total
+           error could be higher. */
+        if (s->rx.training_count == 30)
+            s->rx.gardner_step = 32;
+        break;
+    case V22BIS_TRAINING_STAGE_UNSCRAMBLED_ONES:
+        /* The answering modem should initially receive unscrambled ones at 1200bps */
+        track_carrier(s, &z, &v22bis_constellation[nearest]);
+        target = &z;
+        if (nearest == ((s->rx.constellation_state - 4) & 0x0F))
+            s->detected_unscrambled_ones++;
+        if (nearest == ((s->rx.constellation_state + 4) & 0x0F))
+            s->detected_unscrambled_zeros++;
+        s->rx.constellation_state = nearest;
+span_log(&s->logging, SPAN_LOG_FLOW, "TWIDDLING THUMBS - %d\n", s->rx.training_count);
+        if (++s->rx.training_count == ms_to_symbols(155 + 456))
+        {
+            if (s->detected_unscrambled_ones >= 250  ||  s->detected_unscrambled_zeros >= 250)
+                s->detected_unscrambled_ones_or_zeros = TRUE;
+        }
+        if (s->rx.training_count == ms_to_symbols(155 + 457))
+        {
+            /* We should only bother looking for the 2400bps marker if we are allowed to use
+               2400bps */
+            s->rx.training_count = 0;
+            if (s->bit_rate == 2400)
+                s->rx.training = V22BIS_TRAINING_STAGE_UNSCRAMBLED_0011;
+            else
+                s->rx.training = V22BIS_TRAINING_STAGE_SCRAMBLED_ONES_AT_1200;
+        }
+        break;
+    case V22BIS_TRAINING_STAGE_UNSCRAMBLED_0011:
+s->rx.sixteen_way_decisions = TRUE;
+        /* If we can actually find this it means we can use 2400bps. If we find unscrambled ones, it means we
+           we must use 1200bps. */
+        track_carrier(s, &z, &v22bis_constellation[nearest]);
+        tune_equalizer(s, &z, &v22bis_constellation[nearest]);
+        target = &z;
+        //s->rx.carrier_track_i = 1000.0f;
+        //s->rx.carrier_track_p = 1000000.0f;
+#if 0
+        if (++s->rx.training_count > ms_to_symbols(800))
+        {
+            s->detected_unscrambled_0011_ending = TRUE;
+            s->rx.training = V22BIS_TRAINING_STAGE_UNSCRAMBLED_ONES;
+        }
+#else
+        if (++s->rx.training_count == 1)
+        {
+            s->detected_unscrambled_zeros = nearest;
+            s->detected_unscrambled_ones = 0;
+        }
+        else
+        {
+span_log(&s->logging, SPAN_LOG_FLOW, "0x%X 0x%X 0x%X\n", s->detected_unscrambled_zeros, nearest, (s->detected_unscrambled_zeros + nearest) & 0x0F);
+            if ((s->rx.training_count & 1) == 0)
+            {
+span_log(&s->logging, SPAN_LOG_FLOW, "AAA\n");
+                if (((s->detected_unscrambled_zeros + nearest) & 0x0F) == 0x06)
+                    s->detected_unscrambled_ones = 1;
+                else if (((s->detected_unscrambled_zeros + nearest) & 0x0F) == 0x02)
+                    s->detected_unscrambled_ones = -1;
+                else
+                {
+span_log(&s->logging, SPAN_LOG_FLOW, "AAA 1\n");
+                    if (s->detected_unscrambled_ones > 5  ||  s->detected_unscrambled_ones < -5)
+                        s->detected_unscrambled_0011_ending = TRUE;
+                    else
+                        s->bit_rate = 1200;
+                    s->rx.training = V22BIS_TRAINING_STAGE_SCRAMBLED_ONES_AT_1200;
+                }
+            }
+            else
+            {
+span_log(&s->logging, SPAN_LOG_FLOW, "BBB\n");
+                if (((s->detected_unscrambled_zeros + nearest) & 0x0F) == 0x06)
+                    s->detected_unscrambled_ones = 1;
+                else if (((s->detected_unscrambled_zeros + nearest) & 0x0F) == 0x02)
+                    s->detected_unscrambled_ones = -1;
+                else
+                {
+span_log(&s->logging, SPAN_LOG_FLOW, "BBB 1\n");
+                    if (s->detected_unscrambled_ones > 5  ||  s->detected_unscrambled_ones < -5)
+                        s->detected_unscrambled_0011_ending = TRUE;
+                    else
+                        s->bit_rate = 1200;
+                    s->rx.training = V22BIS_TRAINING_STAGE_SCRAMBLED_ONES_AT_1200;
+                }
+            }
+        }
+#endif
+        break;
+    case V22BIS_TRAINING_STAGE_SCRAMBLED_ONES_AT_1200:
+        track_carrier(s, &z, &v22bis_constellation[nearest]);
+        tune_equalizer(s, &z, &v22bis_constellation[nearest]);
+        target = &z;
+span_log(&s->logging, SPAN_LOG_FLOW, "S11 0x%02x\n", nearest);
+        if (++s->rx.training_count > ms_to_symbols(900))
+        {
+            s->detected_scrambled_ones_or_zeros_at_1200bps = TRUE;
+            s->rx.training = V22BIS_TRAINING_STAGE_NORMAL_OPERATION;
+        }
+        if (s->bit_rate == 2400  &&  s->rx.training_count == ms_to_symbols(450))
+            s->rx.sixteen_way_decisions = TRUE;
+        break;
+    case V22BIS_TRAINING_STAGE_SCRAMBLED_ONES_AT_2400:
+        track_carrier(s, &z, &v22bis_constellation[nearest]);
+        tune_equalizer(s, &z, &v22bis_constellation[nearest]);
+        s->rx.sixteen_way_decisions = TRUE;
+        target = &z;
+        break;
+    case V22BIS_TRAINING_STAGE_PARKED:
+    default:
+        /* We failed to train! */
+        /* Park here until the carrier drops. */
+        target = &z;
+        break;
+    }
+    if (s->rx.qam_report)
+        s->rx.qam_report(s->rx.qam_user_data, &z, target, s->rx.constellation_state);
+}
+/*- End of function --------------------------------------------------------*/
+
+int v22bis_rx(v22bis_state_t *s, const int16_t amp[], int len)
+{
+    int i;
+    int j;
+    int step;
+    complexf_t z;
+    complexf_t zz;
+    int32_t power;
+    complexf_t sample;
+    float ii;
+    float qq;
+
+    for (i = 0;  i < len;  i++)
+    {
+        /* Complex bandpass filter the signal, using a pair of FIRs, and RRC coeffs shifted
+           to centre at 1200Hz or 2400Hz. The filters support 12 fractional phase shifts, to 
+           permit signal extraction very close to the middle of a symbol. */
+        s->rx.rrc_filter[s->rx.rrc_filter_step] =
+        s->rx.rrc_filter[s->rx.rrc_filter_step + V22BIS_RX_FILTER_STEPS] = amp[i];
+        if (++s->rx.rrc_filter_step >= V22BIS_RX_FILTER_STEPS)
+            s->rx.rrc_filter_step = 0;
+
+        /* Calculate the I filter, with an arbitrary phase step, just so we can calculate
+           the signal power. */
+        /* TODO: get rid of this */
+        if (s->caller)
+        {
+            ii = rx_pulseshaper_2400[6][0].re*s->rx.rrc_filter[s->rx.rrc_filter_step];
+            for (j = 1;  j < V22BIS_RX_FILTER_STEPS;  j++)
+                ii += rx_pulseshaper_2400[6][j].re*s->rx.rrc_filter[j + s->rx.rrc_filter_step];
+        }
+        else
+        {
+            ii = rx_pulseshaper_1200[6][0].re*s->rx.rrc_filter[s->rx.rrc_filter_step];
+            for (j = 1;  j < V22BIS_RX_FILTER_STEPS;  j++)
+                ii += rx_pulseshaper_1200[6][j].re*s->rx.rrc_filter[j + s->rx.rrc_filter_step];
+        }
+        power = power_meter_update(&(s->rx.rx_power), (int16_t) (ii/10.0f));
+        if (s->rx.signal_present)
+        {
+            /* Look for power below -48dBm0 to turn the carrier off */
+            if (power < s->rx.carrier_off_power)
+            {
+                v22bis_rx_restart(s, s->bit_rate);
+                s->put_bit(s->user_data, PUTBIT_CARRIER_DOWN);
+                continue;
+            }
+        }
+        else
+        {
+            /* Look for power exceeding -43dBm0 to turn the carrier on */
+            if (power < s->rx.carrier_on_power)
+                continue;
+            s->rx.signal_present = TRUE;
+            s->put_bit(s->user_data, PUTBIT_CARRIER_UP);
+        }
+        if (s->rx.training != V22BIS_TRAINING_STAGE_PARKED)
+        {
+            /* Only spend effort processing this data if the modem is not
+               parked, after training failure. */
+            z = dds_complexf(&(s->rx.carrier_phase), s->rx.carrier_phase_rate);
+            if (s->rx.training == V22BIS_TRAINING_STAGE_SYMBOL_ACQUISITION)
+            {
+                /* Only AGC during the initial symbol acquisition, and then lock the gain. */
+                s->rx.agc_scaling = 0.018f*3.60f/sqrtf(power);
+            }
+            /* Put things into the equalization buffer at T/2 rate. The Gardner algorithm
+               will fiddle the step to align this with the symbols. */
+            if ((s->rx.eq_put_step -= PULSESHAPER_COEFF_SETS) <= 0)
+            {
+                /* Pulse shape while still at the carrier frequency, using a quadrature
+                   pair of filters. This results in a properly bandpass filtered complex
+                   signal, which can be brought directly to bandband by complex mixing.
+                   No further filtering, to remove mixer harmonics, is needed. */
+                step = -s->rx.eq_put_step;
+                if (step > PULSESHAPER_COEFF_SETS - 1)
+                    step = PULSESHAPER_COEFF_SETS - 1;
+                s->rx.eq_put_step += PULSESHAPER_COEFF_SETS*40/(3*2);
+                if (s->caller)
+                {
+                    ii = rx_pulseshaper_2400[step][0].re*s->rx.rrc_filter[s->rx.rrc_filter_step];
+                    qq = rx_pulseshaper_2400[step][0].im*s->rx.rrc_filter[s->rx.rrc_filter_step];
+                    for (j = 1;  j < V22BIS_RX_FILTER_STEPS;  j++)
+                    {
+                        ii += rx_pulseshaper_2400[step][j].re*s->rx.rrc_filter[j + s->rx.rrc_filter_step];
+                        qq += rx_pulseshaper_2400[step][j].im*s->rx.rrc_filter[j + s->rx.rrc_filter_step];
+                    }
+                }
+                else
+                {
+                    ii = rx_pulseshaper_1200[step][0].re*s->rx.rrc_filter[s->rx.rrc_filter_step];
+                    qq = rx_pulseshaper_1200[step][0].im*s->rx.rrc_filter[s->rx.rrc_filter_step];
+                    for (j = 1;  j < V22BIS_RX_FILTER_STEPS;  j++)
+                    {
+                        ii += rx_pulseshaper_1200[step][j].re*s->rx.rrc_filter[j + s->rx.rrc_filter_step];
+                        qq += rx_pulseshaper_1200[step][j].im*s->rx.rrc_filter[j + s->rx.rrc_filter_step];
+                    }
+                }
+                sample.re = ii*s->rx.agc_scaling;
+                sample.im = qq*s->rx.agc_scaling;
+                /* Shift to baseband - since this is done in a full complex form, the
+                   result is clean, and requires no further filtering apart from the
+                   equalizer. */
+                zz.re = sample.re*z.re - sample.im*z.im;
+                zz.im = -sample.re*z.im - sample.im*z.re;
+                process_half_baud(s, &zz);
+            }
+        }
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int v22bis_rx_restart(v22bis_state_t *s, int bit_rate)
+{
+    /* If bit_rate is 2400, the real bit rate is negotiated. If bit_rate
+       is 1200, the real bit rate is forced to 1200. */
+    s->bit_rate = bit_rate;
+    vec_zerof(s->rx.rrc_filter, sizeof(s->rx.rrc_filter)/sizeof(s->rx.rrc_filter[0]));
+    s->rx.rrc_filter_step = 0;
+    s->rx.scramble_reg = 0;
+    s->rx.scrambler_pattern_count = 0;
+    s->rx.training = V22BIS_TRAINING_STAGE_SYMBOL_ACQUISITION;
+    s->rx.training_count = 0;
+    s->rx.signal_present = FALSE;
+
+    s->rx.carrier_phase_rate = dds_phase_ratef((s->caller)  ?  2400.0f  :  1200.0f);
+    s->rx.carrier_phase = 0;
+    power_meter_init(&(s->rx.rx_power), 5);
+    s->rx.carrier_on_power = power_meter_level_dbm0(-43);
+    s->rx.carrier_off_power = power_meter_level_dbm0(-48);
+    s->rx.agc_scaling = 0.0005f*0.025f;
+
+    s->rx.constellation_state = 0;
+    s->rx.sixteen_way_decisions = FALSE;
+
+    equalizer_reset(s);
+
+    s->detected_unscrambled_ones = 0;
+    s->detected_unscrambled_zeros = 0;
+    s->rx.gardner_integrate = 0;
+    s->rx.gardner_step = 256;
+    s->rx.baud_phase = 0;
+    s->rx.carrier_track_i = 8000.0f;
+    s->rx.carrier_track_p = 8000000.0f;
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+void v22bis_set_qam_report_handler(v22bis_state_t *s, qam_report_handler_t handler, void *user_data)
+{
+    s->rx.qam_report = handler;
+    s->rx.qam_user_data = user_data;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/v22bis_tx.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/v22bis_tx.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,672 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * v22bis_tx.c - ITU V.22bis modem transmit part
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2004 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: v22bis_tx.c,v 1.43 2008/07/02 14:48:26 steveu Exp $
+ */
+
+/*! \file */
+
+/* THIS IS A WORK IN PROGRESS - NOT YET FUNCTIONAL! */
+
+#if defined(HAVE_CONFIG_H)
+#include <config.h>
+#endif
+
+#include <stdio.h>
+#include <inttypes.h>
+#include <stdlib.h>
+#include <string.h>
+#include "floating_fudge.h"
+#if defined(HAVE_TGMATH_H)
+#include <tgmath.h>
+#endif
+#if defined(HAVE_MATH_H)
+#include <math.h>
+#endif
+
+#include "spandsp/telephony.h"
+#include "spandsp/logging.h"
+#include "spandsp/complex.h"
+#include "spandsp/vector_float.h"
+#include "spandsp/complex_vector_float.h"
+#include "spandsp/async.h"
+#include "spandsp/dds.h"
+#include "spandsp/power_meter.h"
+
+#include "spandsp/v29rx.h"
+#include "spandsp/v22bis.h"
+
+#if defined(SPANDSP_USE_FIXED_POINT)
+#include "v22bis_tx_fixed_rrc.h"
+#else
+#include "v22bis_tx_floating_rrc.h"
+#endif
+
+/* Quoting from the V.22bis spec.
+
+6.3.1.1 Interworking at 2400 bit/s
+
+6.3.1.1.1   Calling modem
+
+a)  On connection to line the calling modem shall be conditioned to receive signals
+    in the high channel at 1200 bit/s and transmit signals in the low channel at 1200 bit/s
+    in accordance with section 2.5.2.2. It shall apply an ON condition to circuit 107 in accordance
+    with Recommendation V.25. The modem shall initially remain silent.
+
+b)  After 155 +-10 ms of unscrambled binary 1 has been detected, the modem shall remain silent
+    for a further 456 +-10 ms then transmit an unscrambled repetitive double dibit pattern of 00
+    and 11 at 1200 bit/s for 100 +-3 ms. Following this signal the modem shall transmit scrambled
+    binary 1 at 1200 bit/s.
+
+c)  If the modem detects scrambled binary 1 in the high channel at 1200 bit/s for 270 +-40 ms,
+    the handshake shall continue in accordance with section 6.3.1.2.1 c) and d). However, if unscrambled
+    repetitive double dibit 00 and 11 at 1200 bit/s is detected in the high channel, then at the
+    end of receipt of this signal the modem shall apply an ON condition to circuit 112.
+
+d)  600 +-10 ms after circuit 112 has been turned ON the modem shall begin transmitting scrambled
+    binary 1 at 2400 bit/s, and 450 +-10 ms after circuit 112 has been turned ON the receiver may
+    begin making 16-way decisions.
+
+e)  Following transmission of scrambled binary 1 at 2400 bit/s for 200 +-10 ms, circuit 106 shall
+    be conditioned to respond to circuit 105 and the modem shall be ready to transmit data at
+    2400 bit/s.
+
+f)  When 32 consecutive bits of scrambled binary 1 at 2400 bit/s have been detected in the high
+    channel the modem shall be ready to receive data at 2400 bit/s and shall apply an ON condition
+    to circuit 109.
+
+6.3.1.1.2   Answering modem
+
+a)  On connection to line the answering modem shall be conditioned to transmit signals in the high
+    channel at 1200 bit/s in accordance with  section 2.5.2.2 and receive signals in the low channel at
+    1200 bit/s. Following transmission of the answer sequence in accordance with Recommendation
+    V.25, the modem shall apply an ON condition to circuit 107 and then transmit unscrambled
+    binary 1 at 1200 bit/s.
+
+b)  If the modem detects scrambled binary 1 or 0 in the low channel at 1200 bit/s for 270 +-40 ms,
+    the handshake shall continue in accordance with section 6.3.1.2.2 b) and c). However, if unscrambled
+    repetitive double dibit 00 and 11 at 1200 bit/s is detected in the low channel, at the end of
+    receipt of this signal the modem shall apply an ON condition to circuit 112 and then transmit
+    an unscrambled repetitive double dibit pattern of 00 and 11 at 1200 bit/s for 100 +-3 ms.
+    Following these signals the modem shall transmit scrambled binary 1 at 1200 bit/s.
+
+c)  600 +-10 ms after circuit 112 has been turned ON the modem shall begin transmitting scrambled
+    binary 1 at 2400 bit/s, and 450 +-10 ms after circuit 112 has been turned ON the receiver may
+    begin making 16-way decisions.
+
+d)  Following transmission of scrambled binary 1 at 2400 bit/s for 200 +-10 ms, circuit 106 shall
+    be conditioned to respond to circuit 105 and the modem shall be ready to transmit data at
+    2400 bit/s.
+
+e)  When 32 consecutive bits of scrambled binary 1 at 2400 bit/s have been detected in the low
+    channel the modem shall be ready to receive data at 2400 bit/s and shall apply an ON
+    condition to circuit 109.
+
+6.3.1.2 Interworking at 1200 bit/s
+
+The following handshake is identical to the Recommendation V.22 alternative A and B handshake.
+
+6.3.1.2.1   Calling modem
+
+a)  On connection to line the calling modem shall be conditioned to receive signals in the high
+    channel at 1200 bit/s and transmit signals in the low channel at 1200 bit/s in accordance
+    with section 2.5.2.2. It shall apply an ON condition to circuit 107 in accordance with
+    Recommendation V.25. The modem shall initially remain silent.
+
+b)  After 155 +-10 ms of unscrambled binary 1 has been detected, the modem shall remain silent
+    for a further 456 +-10 ms then transmit scrambled binary 1 at 1200 bit/s (a preceding V.22 bis
+    signal, as shown in Figure 7/V.22 bis, would not affect the operation of a V.22 answer modem).
+
+c)  On detection of scrambled binary 1 in the high channel at 1200 bit/s for 270 +-40 ms the modem
+    shall be ready to receive data at 1200 bit/s and shall apply an ON condition to circuit 109 and
+    an OFF condition to circuit 112.
+
+d)  765 +-10 ms after circuit 109 has been turned ON, circuit 106 shall be conditioned to respond
+    to circuit 105 and the modem shall be ready to transmit data at 1200 bit/s.
+ 
+6.3.1.2.2   Answering modem
+
+a)  On connection to line the answering modem shall be conditioned to transmit signals in the high
+    channel at 1200 bit/s in accordance with section 2.5.2.2 and receive signals in the low channel at
+    1200 bit/s.
+
+    Following transmission of the answer sequence in accordance with V.25 the modem shall apply
+    an ON condition to circuit 107 and then transmit unscrambled binary 1 at 1200 bit/s.
+
+b)  On detection of scrambled binary 1 or 0 in the low channel at 1200 bit/s for 270 +-40 ms the
+    modem shall apply an OFF condition to circuit 112 and shall then transmit scrambled binary 1
+    at 1200 bit/s.
+
+c)  After scrambled binary 1 has been transmitted at 1200 bit/s for 765 +-10 ms the modem shall be
+    ready to transmit and receive data at 1200 bit/s, shall condition circuit 106 to respond to
+    circuit 105 and shall apply an ON condition to circuit 109.
+
+Note - Manufacturers may wish to note that in certain countries, for national purposes, modems are
+       in service which emit an answering tone of 2225 Hz instead of unscrambled binary 1.
+
+
+V.22bis to V.22bis
+------------------
+Calling party
+                                                           S1       scrambled 1's                  scrambled 1's  data
+                                                                    at 1200bps                     at 2400bps
+|---------------------------------------------------------|XXXXXXXX|XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX|XXXXXXXXXXXXXX|XXXXXXXXXXXXX
+                                      |<155+-10>|<456+-10>|<100+-3>|        |<------600+-10------>|<---200+-10-->|
+                                      ^                            |        ^<----450+-100---->|[16 way decisions begin]
+                                      |                            |        |
+                                      |                            v        |
+                                      |                            |<------450+-100----->|[16 way decisions begin]
+                                      |                            |<----------600+-10-------->|
+  |<2150+-350>|<--3300+-700->|<75+-20>|                            |<100+-3>|                  |<---200+-10-->
+  |-----------|XXXXXXXXXXXXXX|--------|XXXXXXXXXXXXXXXXXXXXXXXXXXXX|XXXXXXXX|XXXXXXXXXXXXXXXXXX|XXXXXXXXXXXXXX|XXXXXXXXXXXXX
+   silence    2100Hz                   unscrambled 1's              S1       scrambled 1's      scrambled 1's  data
+                                       at 1200bps                            at 1200bps         at 2400bps
+Answering party
+
+S1 = Unscrambled double dibit 00 and 11 at 1200bps
+When the 2400bps section starts, both sides should look for 32 bits of continuous ones, as a test of integrity.
+
+
+
+
+V.22 to V.22bis
+---------------
+Calling party
+                                                           scrambled 1's                                 data
+                                                           at 1200bps
+|---------------------------------------------------------|XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX|XXXXXXXXXXXXX
+                                      |<155+-10>|<456+-10>|         |<270+-40>|<--------765+-10-------->|
+                                      ^                   |         ^
+                                      |                   |         |
+                                      |                   |         |
+                                      |                   |         |
+                                      |                   v         |
+  |<2150+-350>|<--3300+-700->|<75+-20>|                   |<270+-40>|<---------765+-10-------->|
+  |-----------|XXXXXXXXXXXXXX|--------|XXXXXXXXXXXXXXXXXXXXXXXXXXXXX|XXXXXXXXXXXXXXXXXXXXXXXXXX|XXXXXXXXXXXXX
+   silence    2100Hz                   unscrambled 1's                scrambled 1's             data
+                                       at 1200bps                     at 1200bps
+Answering party
+
+Both ends should accept unscrambled binary 1 or binary 0 as the preamble.
+
+
+
+
+V.22bis to V.22
+---------------
+Calling party
+                                                           S1      scrambled 1's                                 data
+                                                                   at 1200bps
+|---------------------------------------------------------|XXXXXXXX|XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX|XXXXXXXXXXXXX
+                                      |<155+-10>|<456+-10>|<100+-3>|           |<-270+-40-><------765+-10------>|
+                                      ^                            |           ^
+                                      |                            |           |
+                                      |                            v           |
+                                      |                            |
+                                      |                            |
+  |<2150+-350>|<--3300+-700->|<75+-20>|                            |<-270+-40->|<------765+-10----->|
+  |-----------|XXXXXXXXXXXXXX|--------|XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX|XXXXXXXXXXXXXXXXXXXX|XXXXXXXXXXXXX
+   silence    2100Hz                   unscrambled 1's                          scrambled 1's        data
+                                       at 1200bps                               at 1200bps
+Answering party
+
+Both ends should accept unscrambled binary 1 or binary 0 as the preamble.
+*/
+
+#define ms_to_symbols(t)    (((t)*600)/1000)
+
+/* Segments of the training sequence */
+enum
+{
+    V22BIS_TRAINING_STAGE_NORMAL_OPERATION = 0,
+    V22BIS_TRAINING_STAGE_INITIAL_SILENCE,
+    V22BIS_TRAINING_STAGE_UNSCRAMBLED_ONES,
+    V22BIS_TRAINING_STAGE_UNSCRAMBLED_0011,
+    V22BIS_TRAINING_STAGE_SCRAMBLED_ONES_AT_1200,
+    V22BIS_TRAINING_STAGE_SCRAMBLED_ONES_AT_2400,
+    V22BIS_TRAINING_STAGE_PARKED
+};
+
+static const int phase_steps[4] =
+{
+    1, 0, 2, 3
+};
+
+const complexf_t v22bis_constellation[16] =
+{
+    { 1.0f,  1.0f},
+    { 3.0f,  1.0f},
+    { 1.0f,  3.0f},
+    { 3.0f,  3.0f},
+    {-1.0f,  1.0f},
+    {-1.0f,  3.0f},
+    {-3.0f,  1.0f},
+    {-3.0f,  3.0f},
+    {-1.0f, -1.0f},
+    {-3.0f, -1.0f},
+    {-1.0f, -3.0f},
+    {-3.0f, -3.0f},
+    { 1.0f, -1.0f},
+    { 1.0f, -3.0f},
+    { 3.0f, -1.0f},
+    { 3.0f, -3.0f}
+};
+
+static int fake_get_bit(void *user_data)
+{
+    return 1;
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ int scramble(v22bis_state_t *s, int bit)
+{
+    int out_bit;
+
+    out_bit = (bit ^ (s->tx.scramble_reg >> 14) ^ (s->tx.scramble_reg >> 17)) & 1;
+    if (s->tx.scrambler_pattern_count >= 64)
+    {
+        out_bit ^= 1;
+        s->tx.scrambler_pattern_count = 0;
+    }
+    if (out_bit == 1)
+        s->tx.scrambler_pattern_count++;
+    else
+        s->tx.scrambler_pattern_count = 0;
+    s->tx.scramble_reg = (s->tx.scramble_reg << 1) | out_bit;
+    return out_bit;
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ int get_scrambled_bit(v22bis_state_t *s)
+{
+    int bit;
+
+    if ((bit = s->tx.current_get_bit(s->user_data)) == PUTBIT_END_OF_DATA)
+    {
+        /* Fill out this symbol with ones, and prepare to send
+           the rest of the shutdown sequence. */
+        s->tx.current_get_bit = fake_get_bit;
+        s->tx.shutdown = 1;
+        bit = 1;
+    }
+    return scramble(s, bit);
+}
+/*- End of function --------------------------------------------------------*/
+
+static complexf_t training_get(v22bis_state_t *s)
+{
+    complexf_t z;
+    int bits;
+
+    /* V.22bis training sequence */
+    switch (s->tx.training)
+    {
+    case V22BIS_TRAINING_STAGE_INITIAL_SILENCE:
+        /* Segment 1: silence */
+        s->tx.constellation_state = 0;
+        z = complex_setf(0.0f, 0.0f);
+        if (s->caller)
+        {
+            /* The caller just waits for a signal from the far end, which should be unscrambled ones */
+            if (s->detected_unscrambled_ones_or_zeros)
+            {
+                if (s->bit_rate == 2400)
+                {
+                    /* Try to establish at 2400bps */
+                    span_log(&s->logging, SPAN_LOG_FLOW, "+++ starting unscrambled 0011 at 1200 (S1)\n");
+                    s->tx.training = V22BIS_TRAINING_STAGE_UNSCRAMBLED_0011;
+                }
+                else
+                {
+                    /* Only try at 1200bps */
+                    span_log(&s->logging, SPAN_LOG_FLOW, "+++ starting scrambled ones at 1200 (A)\n");
+                    s->tx.training = V22BIS_TRAINING_STAGE_SCRAMBLED_ONES_AT_1200;
+                }
+                s->tx.training_count = 0;
+            }
+        }
+        else
+        {
+            /* The answerer waits 75ms, then sends unscrambled ones */
+            if (++s->tx.training_count >= ms_to_symbols(75))
+            {
+                /* Inital 75ms of silence is over */
+                span_log(&s->logging, SPAN_LOG_FLOW, "+++ starting unscrambled ones at 1200\n");
+                s->tx.training = V22BIS_TRAINING_STAGE_UNSCRAMBLED_ONES;
+                s->tx.training_count = 0;
+            }
+        }
+        break;
+    case V22BIS_TRAINING_STAGE_UNSCRAMBLED_ONES:
+        /* Segment 2: Continuous unscrambled ones at 1200bps (i.e. reversals). */
+        /* Only the answering modem sends unscrambled ones. It is the first thing exchanged between the modems. */
+        s->tx.constellation_state = (s->tx.constellation_state + phase_steps[3]) & 3;
+        z = v22bis_constellation[(s->tx.constellation_state << 2) | 0x01];
+        if (s->bit_rate == 2400  &&  s->detected_unscrambled_0011_ending)
+        {
+            /* We are allowed to use 2400bps, and the far end is requesting 2400bps. Result: we are going to
+               work at 2400bps */
+            span_log(&s->logging, SPAN_LOG_FLOW, "+++ [2400] starting unscrambled 0011 at 1200 (S1)\n");
+            s->tx.training = V22BIS_TRAINING_STAGE_UNSCRAMBLED_0011;
+            s->tx.training_count = 0;
+            break;
+        }
+        if (s->detected_scrambled_ones_or_zeros_at_1200bps)
+        {
+            /* We are going to work at 1200bps. */
+            span_log(&s->logging, SPAN_LOG_FLOW, "+++ [1200] starting scrambled ones at 1200 (B)\n");
+            s->bit_rate = 1200;
+            s->tx.training = V22BIS_TRAINING_STAGE_SCRAMBLED_ONES_AT_1200;
+            s->tx.training_count = 0;
+            break;
+        }
+        break;
+    case V22BIS_TRAINING_STAGE_UNSCRAMBLED_0011:
+        /* Segment 3: Continuous unscrambled double dibit 00 11 at 1200bps. This is termed the S1 segment in
+           the V.22bis spec. It is only sent to request or accept 2400bps mode, and lasts 100+-3ms. After this
+           timed burst, we unconditionally change to sending scrambled ones at 1200bps. */
+        s->tx.constellation_state = (s->tx.constellation_state + phase_steps[(s->tx.training_count & 1)  ?  3  :  0]) & 3;
+span_log(&s->logging, SPAN_LOG_FLOW, "U0011 Tx 0x%02x\n", s->tx.constellation_state);
+        z = v22bis_constellation[(s->tx.constellation_state << 2) | 0x01];
+        if (++s->tx.training_count >= ms_to_symbols(100))
+        {
+            span_log(&s->logging, SPAN_LOG_FLOW, "+++ starting scrambled ones at 1200 (C)\n");
+            s->tx.training = V22BIS_TRAINING_STAGE_SCRAMBLED_ONES_AT_1200;
+            s->tx.training_count = 0;
+        }
+        break;
+    case V22BIS_TRAINING_STAGE_SCRAMBLED_ONES_AT_1200:
+        /* Segment 4: Scrambled ones at 1200bps. */
+        bits = scramble(s, 1);
+        bits = (bits << 1) | scramble(s, 1);
+        s->tx.constellation_state = (s->tx.constellation_state + phase_steps[bits]) & 3;
+        z = v22bis_constellation[(s->tx.constellation_state << 2) | 0x01];
+        if (s->caller)
+        {
+            if (s->detected_unscrambled_0011_ending)
+            {
+                /* Continue for a further 600+-10ms */
+                if (++s->tx.training_count >= ms_to_symbols(600))
+                {
+                    span_log(&s->logging, SPAN_LOG_FLOW, "+++ starting scrambled ones at 2400 (A)\n");
+                    s->tx.training = V22BIS_TRAINING_STAGE_SCRAMBLED_ONES_AT_2400;
+                    s->tx.training_count = 0;
+                }
+            }
+            else if (s->detected_scrambled_ones_or_zeros_at_1200bps)
+            {
+                if (s->bit_rate == 2400)
+                {
+                    /* Continue for a further 756+-10ms */
+                    if (++s->tx.training_count >= ms_to_symbols(756))
+                    {
+                        span_log(&s->logging, SPAN_LOG_FLOW, "+++ starting scrambled ones at 2400 (B)\n");
+                        s->tx.training = V22BIS_TRAINING_STAGE_SCRAMBLED_ONES_AT_2400;
+                        s->tx.training_count = 0;
+                    }
+                }
+                else
+                {
+                    span_log(&s->logging, SPAN_LOG_FLOW, "+++ finished\n");
+                    s->tx.training = V22BIS_TRAINING_STAGE_NORMAL_OPERATION;
+                    s->tx.training_count = 0;
+                    s->tx.current_get_bit = s->get_bit;
+                }
+            }
+        }
+        else
+        {
+            if (s->bit_rate == 2400)
+            {
+                if (++s->tx.training_count >= ms_to_symbols(500))
+                {
+                    span_log(&s->logging, SPAN_LOG_FLOW, "+++ starting scrambled ones at 2400 (C)\n");
+                    s->tx.training = V22BIS_TRAINING_STAGE_SCRAMBLED_ONES_AT_2400;
+                    s->tx.training_count = 0;
+                }
+            }
+            else
+            {
+                if (++s->tx.training_count >= ms_to_symbols(756))
+                {
+                    span_log(&s->logging, SPAN_LOG_FLOW, "+++ finished\n");
+                    s->tx.training = 0;
+                    s->tx.training_count = 0;
+                }
+            }
+        }
+        break;
+    case V22BIS_TRAINING_STAGE_SCRAMBLED_ONES_AT_2400:
+        /* Segment 4: Scrambled ones at 2400bps. */
+        bits = scramble(s, 1);
+        bits = (bits << 1) | scramble(s, 1);
+        s->tx.constellation_state = (s->tx.constellation_state + phase_steps[bits]) & 3;
+        bits = scramble(s, 1);
+        bits = (bits << 1) | scramble(s, 1);
+        z = v22bis_constellation[(s->tx.constellation_state << 2) | 0x01];
+        if (++s->tx.training_count >= ms_to_symbols(200))
+        {
+            /* We have completed training. Now handle some real work. */
+            span_log(&s->logging, SPAN_LOG_FLOW, "+++ finished\n");
+            s->tx.training = 0;
+            s->tx.training_count = 0;
+            s->tx.current_get_bit = s->get_bit;
+        }
+        break;
+    case V22BIS_TRAINING_STAGE_PARKED:
+    default:
+        z = complex_setf(0.0f, 0.0f);
+        break;
+    }
+    return z;
+}
+/*- End of function --------------------------------------------------------*/
+
+static complexf_t getbaud(v22bis_state_t *s)
+{
+    int bits;
+
+    if (s->tx.training)
+    {
+        /* Send the training sequence */
+        return training_get(s);
+    }
+
+    /* There is no graceful shutdown procedure defined for V.22bis. Just
+       send some ones, to ensure we get the real data bits through, even
+       with bad ISI. */
+    if (s->tx.shutdown)
+    {
+        if (++s->tx.shutdown > 10)
+            return complex_setf(0.0f, 0.0f);
+    }
+    /* The first two bits define the quadrant */
+    bits = get_scrambled_bit(s);
+    bits = (bits << 1) | get_scrambled_bit(s);
+    s->tx.constellation_state = (s->tx.constellation_state + phase_steps[bits]) & 3;
+    if (s->bit_rate == 1200)
+    {
+        bits = 0x01;
+    }
+    else
+    {
+        /* The other two bits define the position within the quadrant */
+        bits = get_scrambled_bit(s);
+        bits = (bits << 1) | get_scrambled_bit(s);
+    }
+    return v22bis_constellation[(s->tx.constellation_state << 2) | bits];
+}
+/*- End of function --------------------------------------------------------*/
+
+int v22bis_tx(v22bis_state_t *s, int16_t amp[], int len)
+{
+    complexf_t x;
+    complexf_t z;
+    int i;
+    int sample;
+    float famp;
+
+    if (s->tx.shutdown > 10)
+        return 0;
+    for (sample = 0;  sample < len;  sample++)
+    {
+        if ((s->tx.baud_phase += 3) >= 40)
+        {
+            s->tx.baud_phase -= 40;
+            s->tx.rrc_filter[s->tx.rrc_filter_step] =
+            s->tx.rrc_filter[s->tx.rrc_filter_step + V22BIS_TX_FILTER_STEPS] = getbaud(s);
+            if (++s->tx.rrc_filter_step >= V22BIS_TX_FILTER_STEPS)
+                s->tx.rrc_filter_step = 0;
+        }
+        /* Root raised cosine pulse shaping at baseband */
+        x = complex_setf(0.0f, 0.0f);
+        for (i = 0;  i < V22BIS_TX_FILTER_STEPS;  i++)
+        {
+            x.re += tx_pulseshaper[39 - s->tx.baud_phase][i]*s->tx.rrc_filter[i + s->tx.rrc_filter_step].re;
+            x.im += tx_pulseshaper[39 - s->tx.baud_phase][i]*s->tx.rrc_filter[i + s->tx.rrc_filter_step].im;
+        }
+        /* Now create and modulate the carrier */
+        z = dds_complexf(&(s->tx.carrier_phase), s->tx.carrier_phase_rate);
+        famp = (x.re*z.re - x.im*z.im)*s->tx.gain;
+        if (s->tx.guard_phase_rate  &&  (s->tx.rrc_filter[s->tx.rrc_filter_step].re != 0.0f  ||  s->tx.rrc_filter[i + s->tx.rrc_filter_step].im != 0.0f))
+        {
+            /* Add the guard tone */
+            famp += dds_modf(&(s->tx.guard_phase), s->tx.guard_phase_rate, s->tx.guard_level, 0);
+        }
+        /* Don't bother saturating. We should never clip. */
+        amp[sample] = (int16_t) lrintf(famp);
+    }
+    return sample;
+}
+/*- End of function --------------------------------------------------------*/
+
+void v22bis_tx_power(v22bis_state_t *s, float power)
+{
+    float l;
+
+    l = 1.6f*powf(10.0f, (power - DBM0_MAX_POWER)/20.0f);
+    s->tx.gain = l*32768.0f/(TX_PULSESHAPER_GAIN*3.0f);
+}
+/*- End of function --------------------------------------------------------*/
+
+static int v22bis_tx_restart(v22bis_state_t *s, int bit_rate)
+{
+    s->bit_rate = bit_rate;
+    cvec_zerof(s->tx.rrc_filter, sizeof(s->tx.rrc_filter)/sizeof(s->tx.rrc_filter[0]));
+    s->tx.rrc_filter_step = 0;
+    s->tx.scramble_reg = 0;
+    s->tx.scrambler_pattern_count = 0;
+    s->tx.training = V22BIS_TRAINING_STAGE_INITIAL_SILENCE;
+    s->tx.training_count = 0;
+    s->tx.carrier_phase = 0;
+    s->tx.guard_phase = 0;
+    s->tx.baud_phase = 0;
+    s->tx.constellation_state = 0;
+    s->tx.current_get_bit = fake_get_bit;
+    s->tx.shutdown = 0;
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+void v22bis_set_get_bit(v22bis_state_t *s, get_bit_func_t get_bit, void *user_data)
+{
+    s->get_bit = get_bit;
+    s->user_data = user_data;
+}
+/*- End of function --------------------------------------------------------*/
+
+void v22bis_set_put_bit(v22bis_state_t *s, put_bit_func_t put_bit, void *user_data)
+{
+    s->put_bit = put_bit;
+    s->user_data = user_data;
+}
+/*- End of function --------------------------------------------------------*/
+
+int v22bis_restart(v22bis_state_t *s, int bit_rate)
+{
+    if (bit_rate != 2400  &&  bit_rate != 1200)
+        return -1;
+    if (v22bis_tx_restart(s, bit_rate))
+        return -1;
+    return v22bis_rx_restart(s, bit_rate);
+}
+/*- End of function --------------------------------------------------------*/
+
+v22bis_state_t *v22bis_init(v22bis_state_t *s,
+                            int bit_rate,
+                            int guard,
+                            int caller,
+                            get_bit_func_t get_bit,
+                            put_bit_func_t put_bit,
+                            void *user_data)
+{
+    if (s == NULL)
+    {
+        if ((s = (v22bis_state_t *) malloc(sizeof(*s))) == NULL)
+            return NULL;
+    }
+    memset(s, 0, sizeof(*s));
+    span_log_init(&s->logging, SPAN_LOG_NONE, NULL);
+    span_log_set_protocol(&s->logging, "V.22bis");
+    s->bit_rate = bit_rate;
+    s->caller = caller;
+
+    s->get_bit = get_bit;
+    s->put_bit = put_bit;
+    s->user_data = user_data;
+
+    if (s->caller)
+    {
+        s->tx.carrier_phase_rate = dds_phase_ratef(1200.0f);
+    }
+    else
+    {
+        s->tx.carrier_phase_rate = dds_phase_ratef(2400.0f);
+        if (guard)
+        {
+            if (guard == 1)
+            {
+                s->tx.guard_phase_rate = dds_phase_ratef(550.0f);
+                s->tx.guard_level = 1500.0f;
+            }
+            else
+            {
+                s->tx.guard_phase_rate = dds_phase_ratef(1800.0f);
+                s->tx.guard_level = 1000.0f;
+            }
+        }
+    }
+    v22bis_tx_power(s, -10.0f);
+    v22bis_restart(s, s->bit_rate);
+    return s;
+}
+/*- End of function --------------------------------------------------------*/
+
+int v22bis_free(v22bis_state_t *s)
+{
+    free(s);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/v27ter_rx.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/v27ter_rx.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,975 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * v27ter_rx.c - ITU V.27ter modem receive part
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2003 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: v27ter_rx.c,v 1.97 2008/08/04 14:03:17 steveu Exp $
+ */
+
+/*! \file */
+
+#if defined(HAVE_CONFIG_H)
+#include <config.h>
+#endif
+
+#include <inttypes.h>
+#include <string.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include "floating_fudge.h"
+#if defined(HAVE_TGMATH_H)
+#include <tgmath.h>
+#endif
+#if defined(HAVE_MATH_H)
+#include <math.h>
+#endif
+
+#include "spandsp/telephony.h"
+#include "spandsp/logging.h"
+#include "spandsp/complex.h"
+#include "spandsp/vector_float.h"
+#include "spandsp/complex_vector_float.h"
+#include "spandsp/async.h"
+#include "spandsp/power_meter.h"
+#include "spandsp/arctan2.h"
+#include "spandsp/dds.h"
+#include "spandsp/complex_filters.h"
+
+#include "spandsp/v29rx.h"
+#include "spandsp/v27ter_rx.h"
+
+#if defined(SPANDSP_USE_FIXED_POINT)
+#include "v27ter_rx_4800_fixed_rrc.h"
+#include "v27ter_rx_2400_fixed_rrc.h"
+#else
+#include "v27ter_rx_4800_floating_rrc.h"
+#include "v27ter_rx_2400_floating_rrc.h"
+#endif
+
+/* V.27ter is a DPSK modem, but this code treats it like QAM. It nails down the
+   signal to a static constellation, even though dealing with differences is all
+   that is necessary. */
+
+#define CARRIER_NOMINAL_FREQ        1800.0f
+#define EQUALIZER_DELTA             0.25f
+
+/* Segments of the training sequence */
+/* V.27ter defines a long and a short sequence. FAX doesn't use the
+   short sequence, so it is not implemented here. */
+#define V27TER_TRAINING_SEG_3_LEN   50
+#define V27TER_TRAINING_SEG_5_LEN   1074
+#define V27TER_TRAINING_SEG_6_LEN   8
+
+enum
+{
+    TRAINING_STAGE_NORMAL_OPERATION = 0,
+    TRAINING_STAGE_SYMBOL_ACQUISITION,
+    TRAINING_STAGE_LOG_PHASE,
+    TRAINING_STAGE_WAIT_FOR_HOP,
+    TRAINING_STAGE_TRAIN_ON_ABAB,
+    TRAINING_STAGE_TEST_ONES,
+    TRAINING_STAGE_PARKED
+};
+
+static const complexf_t v27ter_constellation[8] =
+{
+    { 1.414f,  0.0f},       /*   0deg */
+    { 1.0f,    1.0f},       /*  45deg */
+    { 0.0f,    1.414f},     /*  90deg */
+    {-1.0f,    1.0f},       /* 135deg */
+    {-1.414f,  0.0f},       /* 180deg */
+    {-1.0f,   -1.0f},       /* 225deg */
+    { 0.0f,   -1.414f},     /* 270deg */
+    { 1.0f,   -1.0f}        /* 315deg */
+};
+
+float v27ter_rx_carrier_frequency(v27ter_rx_state_t *s)
+{
+    return dds_frequencyf(s->carrier_phase_rate);
+}
+/*- End of function --------------------------------------------------------*/
+
+float v27ter_rx_symbol_timing_correction(v27ter_rx_state_t *s)
+{
+    int steps_per_symbol;
+    
+    steps_per_symbol = (s->bit_rate == 4800)  ?  RX_PULSESHAPER_4800_COEFF_SETS*5  :  RX_PULSESHAPER_2400_COEFF_SETS*20/3;
+    return (float) s->total_baud_timing_correction/(float) steps_per_symbol;
+}
+/*- End of function --------------------------------------------------------*/
+
+float v27ter_rx_signal_power(v27ter_rx_state_t *s)
+{
+    return power_meter_current_dbm0(&s->power);
+}
+/*- End of function --------------------------------------------------------*/
+
+void v27ter_rx_signal_cutoff(v27ter_rx_state_t *s, float cutoff)
+{
+    /* The 0.4 factor allows for the gain of the DC blocker */
+    s->carrier_on_power = (int32_t) (power_meter_level_dbm0(cutoff + 2.5f)*0.4f);
+    s->carrier_off_power = (int32_t) (power_meter_level_dbm0(cutoff - 2.5f)*0.4f);
+}
+/*- End of function --------------------------------------------------------*/
+
+int v27ter_rx_equalizer_state(v27ter_rx_state_t *s, complexf_t **coeffs)
+{
+    *coeffs = s->eq_coeff;
+    return V27TER_EQUALIZER_PRE_LEN + 1 + V27TER_EQUALIZER_POST_LEN;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void report_status_change(v27ter_rx_state_t *s, int status)
+{
+    if (s->status_handler)
+        s->status_handler(s->status_user_data, status);
+    else if (s->put_bit)
+        s->put_bit(s->put_bit_user_data, status);
+}
+/*- End of function --------------------------------------------------------*/
+
+static void equalizer_save(v27ter_rx_state_t *s)
+{
+    cvec_copyf(s->eq_coeff_save, s->eq_coeff, V27TER_EQUALIZER_PRE_LEN + 1 + V27TER_EQUALIZER_POST_LEN);
+}
+/*- End of function --------------------------------------------------------*/
+
+static void equalizer_restore(v27ter_rx_state_t *s)
+{
+    cvec_copyf(s->eq_coeff, s->eq_coeff_save, V27TER_EQUALIZER_PRE_LEN + 1 + V27TER_EQUALIZER_POST_LEN);
+    cvec_zerof(s->eq_buf, V27TER_EQUALIZER_MASK);
+
+    s->eq_put_step = (s->bit_rate == 4800)  ?  RX_PULSESHAPER_4800_COEFF_SETS*5/2  :  RX_PULSESHAPER_2400_COEFF_SETS*20/(3*2);
+    s->eq_step = 0;
+    s->eq_delta = EQUALIZER_DELTA/(V27TER_EQUALIZER_PRE_LEN + 1 + V27TER_EQUALIZER_POST_LEN);
+}
+/*- End of function --------------------------------------------------------*/
+
+static void equalizer_reset(v27ter_rx_state_t *s)
+{
+    /* Start with an equalizer based on everything being perfect */
+    cvec_zerof(s->eq_coeff, V27TER_EQUALIZER_PRE_LEN + 1 + V27TER_EQUALIZER_POST_LEN);
+    s->eq_coeff[V27TER_EQUALIZER_PRE_LEN] = complex_setf(1.414f, 0.0f);
+    cvec_zerof(s->eq_buf, V27TER_EQUALIZER_MASK);
+
+    s->eq_put_step = (s->bit_rate == 4800)  ?  RX_PULSESHAPER_4800_COEFF_SETS*5/2  :  RX_PULSESHAPER_2400_COEFF_SETS*20/(3*2);
+    s->eq_step = 0;
+    s->eq_delta = EQUALIZER_DELTA/(V27TER_EQUALIZER_PRE_LEN + 1 + V27TER_EQUALIZER_POST_LEN);
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ complexf_t equalizer_get(v27ter_rx_state_t *s)
+{
+    int i;
+    int p;
+    complexf_t z;
+    complexf_t z1;
+
+    /* Get the next equalized value. */
+    z = complex_setf(0.0f, 0.0f);
+    p = s->eq_step - 1;
+    for (i = 0;  i < V27TER_EQUALIZER_PRE_LEN + 1 + V27TER_EQUALIZER_POST_LEN;  i++)
+    {
+        p = (p - 1) & V27TER_EQUALIZER_MASK;
+        z1 = complex_mulf(&s->eq_coeff[i], &s->eq_buf[p]);
+        z = complex_addf(&z, &z1);
+    }
+    return z;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void tune_equalizer(v27ter_rx_state_t *s, const complexf_t *z, const complexf_t *target)
+{
+    int i;
+    int p;
+    complexf_t ez;
+    complexf_t z1;
+
+    /* Find the x and y mismatch from the exact constellation position. */
+    ez = complex_subf(target, z);
+    ez.re *= s->eq_delta;
+    ez.im *= s->eq_delta;
+
+    p = s->eq_step - 1;
+    for (i = 0;  i < V27TER_EQUALIZER_PRE_LEN + 1 + V27TER_EQUALIZER_POST_LEN;  i++)
+    {
+        p = (p - 1) & V27TER_EQUALIZER_MASK;
+        z1 = complex_conjf(&s->eq_buf[p]);
+        z1 = complex_mulf(&ez, &z1);
+        s->eq_coeff[i] = complex_addf(&s->eq_coeff[i], &z1);
+        /* Leak a little to tame uncontrolled wandering */
+        s->eq_coeff[i].re *= 0.9999f;
+        s->eq_coeff[i].im *= 0.9999f;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ void track_carrier(v27ter_rx_state_t *s, const complexf_t *z, const complexf_t *target)
+{
+    float error;
+
+    /* For small errors the imaginary part of the difference between the actual and the target
+       positions is proportional to the phase error, for any particular target. However, the
+       different amplitudes of the various target positions scale things. */
+    error = z->im*target->re - z->re*target->im;
+    
+    s->carrier_phase_rate += (int32_t) (s->carrier_track_i*error);
+    s->carrier_phase += (int32_t) (s->carrier_track_p*error);
+    //span_log(&s->logging, SPAN_LOG_FLOW, "Im = %15.5f   f = %15.5f\n", error, dds_frequencyf(s->carrier_phase_rate));
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ int descramble(v27ter_rx_state_t *s, int in_bit)
+{
+    int out_bit;
+
+    out_bit = (in_bit ^ (s->scramble_reg >> 5) ^ (s->scramble_reg >> 6)) & 1;
+    if (s->scrambler_pattern_count >= 33)
+    {
+        out_bit ^= 1;
+        s->scrambler_pattern_count = 0;
+    }
+    else
+    {
+        if (s->training_stage > TRAINING_STAGE_NORMAL_OPERATION  &&  s->training_stage < TRAINING_STAGE_TEST_ONES)
+        {
+            s->scrambler_pattern_count = 0;
+        }
+        else
+        {
+            if ((((s->scramble_reg >> 7) ^ in_bit) & ((s->scramble_reg >> 8) ^ in_bit) & ((s->scramble_reg >> 11) ^ in_bit) & 1))
+                s->scrambler_pattern_count = 0;
+            else
+                s->scrambler_pattern_count++;
+        }
+    }
+    s->scramble_reg <<= 1;
+    if (s->training_stage > TRAINING_STAGE_NORMAL_OPERATION  &&  s->training_stage < TRAINING_STAGE_TEST_ONES)
+        s->scramble_reg |= out_bit;
+    else
+        s->scramble_reg |= in_bit;
+    return out_bit;
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ void put_bit(v27ter_rx_state_t *s, int bit)
+{
+    int out_bit;
+
+    bit &= 1;
+
+    out_bit = descramble(s, bit);
+
+    /* We need to strip the last part of the training before we let data
+       go to the application. */
+    if (s->training_stage == TRAINING_STAGE_NORMAL_OPERATION)
+    {
+        s->put_bit(s->put_bit_user_data, out_bit);
+    }
+    else
+    {
+        //span_log(&s->logging, SPAN_LOG_FLOW, "Test bit %d\n", out_bit);
+        /* The bits during the final stage of training should be all ones. However,
+           buggy modems mean you cannot rely on this. Therefore we don't bother
+           testing for ones, but just rely on a constellation mismatch measurement. */
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ int find_quadrant(const complexf_t *z)
+{
+    int b1;
+    int b2;
+
+    /* Split the space along the two diagonals. */
+    b1 = (z->im > z->re);
+    b2 = (z->im < -z->re);
+    return (b2 << 1) | (b1 ^ b2);
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ int find_octant(complexf_t *z)
+{
+    float abs_re;
+    float abs_im;
+    int b1;
+    int b2;
+    int bits;
+
+    /* Are we near an axis or a diagonal? */
+    abs_re = fabsf(z->re);
+    abs_im = fabsf(z->im);
+    if (abs_im > abs_re*0.4142136f  &&  abs_im < abs_re*2.4142136f)
+    {
+        /* Split the space along the two axes. */
+        b1 = (z->re < 0.0f);
+        b2 = (z->im < 0.0f);
+        bits = (b2 << 2) | ((b1 ^ b2) << 1) | 1;
+    }
+    else
+    {
+        /* Split the space along the two diagonals. */
+        b1 = (z->im > z->re);
+        b2 = (z->im < -z->re);
+        bits = (b2 << 2) | ((b1 ^ b2) << 1);
+    }
+    return bits;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void decode_baud(v27ter_rx_state_t *s, complexf_t *z)
+{
+    static const uint8_t phase_steps_4800[8] =
+    {
+        4, 0, 2, 6, 7, 3, 1, 5
+    };
+    static const uint8_t phase_steps_2400[4] =
+    {
+        0, 2, 3, 1
+    };
+    int nearest;
+    int raw_bits;
+
+    switch (s->bit_rate)
+    {
+    case 4800:
+    default:
+        nearest = find_octant(z);
+        raw_bits = phase_steps_4800[(nearest - s->constellation_state) & 7];
+        put_bit(s, raw_bits);
+        put_bit(s, raw_bits >> 1);
+        put_bit(s, raw_bits >> 2);
+        s->constellation_state = nearest;
+        break;
+    case 2400:
+        nearest = find_quadrant(z);
+        raw_bits = phase_steps_2400[(nearest - s->constellation_state) & 3];
+        put_bit(s, raw_bits);
+        put_bit(s, raw_bits >> 1);
+        s->constellation_state = nearest;
+        nearest <<= 1;
+        break;
+    }
+    track_carrier(s, z, &v27ter_constellation[nearest]);
+    if (--s->eq_skip <= 0)
+    {
+        /* Once we are in the data the equalization should not need updating.
+           However, the line characteristics may slowly drift. We, therefore,
+           tune up on the occassional sample, keeping the compute down. */
+        s->eq_skip = 100;
+        tune_equalizer(s, z, &v27ter_constellation[nearest]);
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ void process_half_baud(v27ter_rx_state_t *s, const complexf_t *sample)
+{
+    static const int abab_pos[2] =
+    {
+        0, 4
+    };
+    complexf_t z;
+    complexf_t zz;
+    float p;
+    float q;
+    int i;
+    int j;
+    int32_t angle;
+    int32_t ang;
+
+    /* Add a sample to the equalizer's circular buffer, but don't calculate anything
+       at this time. */
+    s->eq_buf[s->eq_step] = *sample;
+    s->eq_step = (s->eq_step + 1) & V27TER_EQUALIZER_MASK;
+        
+    /* On alternate insertions we have a whole baud, and must process it. */
+    if ((s->baud_phase ^= 1))
+    {
+        //span_log(&s->logging, SPAN_LOG_FLOW, "Samp, %f, %f, %f, -1, 0x%X\n", z.re, z.im, sqrtf(z.re*z.re + z.im*z.im), s->eq_put_step);
+        return;
+    }
+    //span_log(&s->logging, SPAN_LOG_FLOW, "Samp, %f, %f, %f, 1, 0x%X\n", z.re, z.im, sqrtf(z.re*z.re + z.im*z.im), s->eq_put_step);
+
+    /* Perform a Gardner test for baud alignment */
+    p = s->eq_buf[(s->eq_step - 3) & V27TER_EQUALIZER_MASK].re
+      - s->eq_buf[(s->eq_step - 1) & V27TER_EQUALIZER_MASK].re;
+    p *= s->eq_buf[(s->eq_step - 2) & V27TER_EQUALIZER_MASK].re;
+
+    q = s->eq_buf[(s->eq_step - 3) & V27TER_EQUALIZER_MASK].im
+      - s->eq_buf[(s->eq_step - 1) & V27TER_EQUALIZER_MASK].im;
+    q *= s->eq_buf[(s->eq_step - 2) & V27TER_EQUALIZER_MASK].im;
+
+    s->gardner_integrate += (p + q > 0.0f)  ?  s->gardner_step  :  -s->gardner_step;
+
+    if (abs(s->gardner_integrate) >= 256)
+    {
+        /* This integrate and dump approach avoids rapid changes of the equalizer put step.
+           Rapid changes, without hysteresis, are bad. They degrade the equalizer performance
+           when the true symbol boundary is close to a sample boundary. */
+        //span_log(&s->logging, SPAN_LOG_FLOW, "Hop %d\n", s->gardner_integrate);
+        s->eq_put_step += (s->gardner_integrate/256);
+        s->total_baud_timing_correction += (s->gardner_integrate/256);
+        if (s->qam_report)
+            s->qam_report(s->qam_user_data, NULL, NULL, s->gardner_integrate);
+        s->gardner_integrate = 0;
+    }
+    //span_log(&s->logging, SPAN_LOG_FLOW, "Gardner=%10.5f 0x%X\n", p, s->eq_put_step);
+
+    z = equalizer_get(s);
+
+    //span_log(&s->logging, SPAN_LOG_FLOW, "Equalized symbol - %15.5f %15.5f\n", z.re, z.im);
+    switch (s->training_stage)
+    {
+    case TRAINING_STAGE_NORMAL_OPERATION:
+        decode_baud(s, &z);
+        break;
+    case TRAINING_STAGE_SYMBOL_ACQUISITION:
+        /* Allow time for the Gardner algorithm to settle the baud timing */
+        /* Don't start narrowing the bandwidth of the Gardner algorithm too early.
+           Some modems are a bit wobbly when they start sending the signal. Also, we start
+           this analysis before our filter buffers have completely filled. */
+        if (++s->training_count >= 30)
+        {
+            s->gardner_step = 32;
+            s->training_stage = TRAINING_STAGE_LOG_PHASE;
+            s->angles[0] =
+            s->start_angles[0] = arctan2(z.im, z.re);
+        }
+        break;
+    case TRAINING_STAGE_LOG_PHASE:
+        /* Record the current alternate phase angle */
+        angle = arctan2(z.im, z.re);
+        s->angles[1] =
+        s->start_angles[1] = angle;
+        s->training_count = 1;
+        s->training_stage = TRAINING_STAGE_WAIT_FOR_HOP;
+        break;
+    case TRAINING_STAGE_WAIT_FOR_HOP:
+        angle = arctan2(z.im, z.re);
+        /* Look for the initial ABAB sequence to display a phase reversal, which will
+           signal the start of the scrambled ABAB segment */
+        ang = angle - s->angles[(s->training_count - 1) & 0xF];
+        s->angles[(s->training_count + 1) & 0xF] = angle;
+        if ((ang > 0x20000000  ||  ang < -0x20000000)  &&  s->training_count >= 3)
+        {
+            /* We seem to have a phase reversal */
+            /* Slam the carrier frequency into line, based on the total phase drift over the last
+               section. Use the shift from the odd bits and the shift from the even bits to get
+               better jitter suppression. We need to scale here, or at the maximum specified
+               frequency deviation we could overflow, and get a silly answer. */
+            /* Step back a few symbols so we don't get ISI distorting things. */
+            i = (s->training_count - 8) & ~1;
+            /* Avoid the possibility of a divide by zero */
+            if (i)
+            {
+                j = i & 0xF;
+                ang = (s->angles[j] - s->start_angles[0])/i
+                    + (s->angles[j | 0x1] - s->start_angles[1])/i;
+                if (s->bit_rate == 4800)
+                    s->carrier_phase_rate += ang/10;
+                else
+                    s->carrier_phase_rate += 3*(ang/40);
+            }
+            span_log(&s->logging, SPAN_LOG_FLOW, "Coarse carrier frequency %7.2f (%d)\n", dds_frequencyf(s->carrier_phase_rate), s->training_count);
+            /* Check if the carrier frequency is plausible */
+            if (s->carrier_phase_rate < dds_phase_ratef(CARRIER_NOMINAL_FREQ - 20.0f)
+                ||
+                s->carrier_phase_rate > dds_phase_ratef(CARRIER_NOMINAL_FREQ + 20.0f))
+            {
+               span_log(&s->logging, SPAN_LOG_FLOW, "Training failed (sequence failed)\n");
+               /* Park this modem */
+               s->training_stage = TRAINING_STAGE_PARKED;
+               report_status_change(s, PUTBIT_TRAINING_FAILED);
+               break;
+            }
+
+            /* Make a step shift in the phase, to pull it into line. We need to rotate the equalizer
+               buffer, as well as the carrier phase, for this to play out nicely. */
+            angle += 0x80000000;
+            p = angle*2.0f*3.14159f/(65536.0f*65536.0f);
+            zz = complex_setf(cosf(p), -sinf(p));
+            for (i = 0;  i <= V27TER_EQUALIZER_MASK;  i++)
+                s->eq_buf[i] = complex_mulf(&s->eq_buf[i], &zz);
+            s->carrier_phase += angle;
+
+            s->gardner_step = 2;
+            /* We have just seen the first element of the scrambled sequence so skip it. */
+            s->training_bc = 1;
+            s->training_bc ^= descramble(s, 1);
+            descramble(s, 1);
+            descramble(s, 1);
+            s->training_count = 1;
+            s->training_stage = TRAINING_STAGE_TRAIN_ON_ABAB;
+            report_status_change(s, PUTBIT_TRAINING_IN_PROGRESS);
+        }
+        else if (++s->training_count > V27TER_TRAINING_SEG_3_LEN)
+        {
+            /* This is bogus. There are not this many bits in this section
+               of a real training sequence. */
+            span_log(&s->logging, SPAN_LOG_FLOW, "Training failed (sequence failed)\n");
+            /* Park this modem */
+            s->training_stage = TRAINING_STAGE_PARKED;
+            report_status_change(s, PUTBIT_TRAINING_FAILED);
+        }
+        break;
+    case TRAINING_STAGE_TRAIN_ON_ABAB:
+        /* Train on the scrambled ABAB section */
+        s->training_bc ^= descramble(s, 1);
+        descramble(s, 1);
+        descramble(s, 1);
+        s->constellation_state = abab_pos[s->training_bc];
+        track_carrier(s, &z, &v27ter_constellation[s->constellation_state]);
+        tune_equalizer(s, &z, &v27ter_constellation[s->constellation_state]);
+
+        if (++s->training_count >= V27TER_TRAINING_SEG_5_LEN)
+        {
+            s->constellation_state = (s->bit_rate == 4800)  ?  4  :  2;
+            s->training_count = 0;
+            s->training_stage = TRAINING_STAGE_TEST_ONES;
+            s->carrier_track_i = 400.0f;
+            s->carrier_track_p = 1000000.0f;
+        }
+        break;
+    case TRAINING_STAGE_TEST_ONES:
+        decode_baud(s, &z);
+        /* Measure the training error */
+        if (s->bit_rate == 4800)
+            zz = complex_subf(&z, &v27ter_constellation[s->constellation_state]);
+        else
+            zz = complex_subf(&z, &v27ter_constellation[s->constellation_state << 1]);
+        s->training_error += powerf(&zz);
+        if (++s->training_count >= V27TER_TRAINING_SEG_6_LEN)
+        {
+            if ((s->bit_rate == 4800  &&  s->training_error < 0.5f)
+                ||
+                (s->bit_rate == 2400  &&  s->training_error < 1.0f))
+            {
+                /* We are up and running */
+                span_log(&s->logging, SPAN_LOG_FLOW, "Training succeeded (constellation mismatch %f)\n", s->training_error);
+                report_status_change(s, PUTBIT_TRAINING_SUCCEEDED);
+                /* Apply some lag to the carrier off condition, to ensure the last few bits get pushed through
+                   the processing. */
+                s->signal_present = (s->bit_rate == 4800)  ?  90  :  120;
+                s->training_stage = TRAINING_STAGE_NORMAL_OPERATION;
+                equalizer_save(s);
+                s->carrier_phase_rate_save = s->carrier_phase_rate;
+                s->agc_scaling_save = s->agc_scaling;
+            }
+            else
+            {
+                /* Training has failed */
+                span_log(&s->logging, SPAN_LOG_FLOW, "Training failed (constellation mismatch %f)\n", s->training_error);
+                /* Park this modem */
+                s->training_stage = TRAINING_STAGE_PARKED;
+                report_status_change(s, PUTBIT_TRAINING_FAILED);
+            }
+        }
+        break;
+    case TRAINING_STAGE_PARKED:
+        /* We failed to train! */
+        /* Park here until the carrier drops. */
+        break;
+    }
+    if (s->qam_report)
+    {
+        s->qam_report(s->qam_user_data,
+                      &z,
+                      &v27ter_constellation[s->constellation_state],
+                      s->constellation_state);
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+int v27ter_rx(v27ter_rx_state_t *s, const int16_t amp[], int len)
+{
+    int i;
+    int j;
+    int step;
+    int16_t x;
+    int32_t diff;
+    complexf_t z;
+    complexf_t zz;
+    complexf_t sample;
+#if defined(SPANDSP_USE_FIXED_POINT)
+    complexi_t zi;
+#endif
+    int32_t power;
+
+    if (s->bit_rate == 4800)
+    {
+        for (i = 0;  i < len;  i++)
+        {
+            s->rrc_filter[s->rrc_filter_step] =
+            s->rrc_filter[s->rrc_filter_step + V27TER_RX_4800_FILTER_STEPS] = amp[i];
+            if (++s->rrc_filter_step >= V27TER_RX_4800_FILTER_STEPS)
+                s->rrc_filter_step = 0;
+
+            /* There should be no DC in the signal, but sometimes there is.
+               We need to measure the power with the DC blocked, but not using
+               a slow to respond DC blocker. Use the most elementary HPF. */
+            x = amp[i] >> 1;
+            diff = x - s->last_sample;
+            power = power_meter_update(&(s->power), diff);
+#if defined(IAXMODEM_STUFF)
+            /* Quick power drop fudge */
+            diff = abs(diff);
+            if (10*diff < s->high_sample)
+            {
+                if (++s->low_samples > 120)
+                {
+                    power_meter_init(&(s->power), 4);
+                    s->high_sample = 0;
+                    s->low_samples = 0;
+                }
+            }
+            else
+            { 
+                s->low_samples = 0;
+                if (diff > s->high_sample)
+                   s->high_sample = diff;
+            }
+#endif
+            s->last_sample = x;
+            //span_log(&s->logging, SPAN_LOG_FLOW, "Power = %f\n", power_meter_current_dbm0(&(s->power)));
+            if (s->signal_present)
+            {
+                /* Look for power below turnoff threshold to turn the carrier off */
+#if defined(IAXMODEM_STUFF)
+                if (s->carrier_drop_pending  ||  power < s->carrier_off_power)
+#else
+                if (power < s->carrier_off_power)
+#endif
+                {
+                    if (--s->signal_present <= 0)
+                    {
+                        /* Count down a short delay, to ensure we push the last
+                           few bits through the filters before stopping. */
+                        v27ter_rx_restart(s, s->bit_rate, FALSE);
+                        report_status_change(s, PUTBIT_CARRIER_DOWN);
+                        continue;
+                    }
+#if defined(IAXMODEM_STUFF)
+                    /* Carrier has dropped, but the put_bit is
+                       pending the signal_present delay. */
+                    s->carrier_drop_pending = TRUE;
+#endif
+                }
+            }
+            else
+            {
+                /* Look for power exceeding turnon threshold to turn the carrier on */
+                if (power < s->carrier_on_power)
+                    continue;
+                s->signal_present = 1;
+#if defined(IAXMODEM_STUFF)
+                s->carrier_drop_pending = FALSE;
+#endif
+                report_status_change(s, PUTBIT_CARRIER_UP);
+            }
+            /* Only spend effort processing this data if the modem is not
+               parked, after training failure. */
+            if (s->training_stage == TRAINING_STAGE_PARKED)
+                continue;
+        
+            /* Put things into the equalization buffer at T/2 rate. The Gardner algorithm
+               will fiddle the step to align this with the symbols. */
+            if ((s->eq_put_step -= RX_PULSESHAPER_4800_COEFF_SETS) <= 0)
+            {
+                if (s->training_stage == TRAINING_STAGE_SYMBOL_ACQUISITION)
+                {
+                    /* Only AGC during the initial training */
+                    s->agc_scaling = (1.0f/RX_PULSESHAPER_4800_GAIN)*1.414f/sqrtf(power);
+                }
+                /* Pulse shape while still at the carrier frequency, using a quadrature
+                   pair of filters. This results in a properly bandpass filtered complex
+                   signal, which can be brought directly to baseband by complex mixing.
+                   No further filtering, to remove mixer harmonics, is needed. */
+                step = -s->eq_put_step;
+                if (step > RX_PULSESHAPER_4800_COEFF_SETS - 1)
+                    step = RX_PULSESHAPER_4800_COEFF_SETS - 1;
+                s->eq_put_step += RX_PULSESHAPER_4800_COEFF_SETS*5/2;
+#if defined(SPANDSP_USE_FIXED_POINT)
+                zi.re = (int32_t) rx_pulseshaper_4800[step][0].re*(int32_t) s->rrc_filter[s->rrc_filter_step];
+                zi.im = (int32_t) rx_pulseshaper_4800[step][0].im*(int32_t) s->rrc_filter[s->rrc_filter_step];
+                for (j = 1;  j < V27TER_RX_4800_FILTER_STEPS;  j++)
+                {
+                    zi.re += (int32_t) rx_pulseshaper_4800[step][j].re*(int32_t) s->rrc_filter[j + s->rrc_filter_step];
+                    zi.im += (int32_t) rx_pulseshaper_4800[step][j].im*(int32_t) s->rrc_filter[j + s->rrc_filter_step];
+                }
+                sample.re = zi.re*s->agc_scaling;
+                sample.im = zi.im*s->agc_scaling;
+#else
+                zz.re = rx_pulseshaper_4800[step][0].re*s->rrc_filter[s->rrc_filter_step];
+                zz.im = rx_pulseshaper_4800[step][0].im*s->rrc_filter[s->rrc_filter_step];
+                for (j = 1;  j < V27TER_RX_4800_FILTER_STEPS;  j++)
+                {
+                    zz.re += rx_pulseshaper_4800[step][j].re*s->rrc_filter[j + s->rrc_filter_step];
+                    zz.im += rx_pulseshaper_4800[step][j].im*s->rrc_filter[j + s->rrc_filter_step];
+                }
+                sample.re = zz.re*s->agc_scaling;
+                sample.im = zz.im*s->agc_scaling;
+#endif
+                /* Shift to baseband - since this is done in a full complex form, the
+                   result is clean, and requires no further filtering, apart from the
+                   equalizer. */
+                z = dds_lookup_complexf(s->carrier_phase);
+                zz.re = sample.re*z.re - sample.im*z.im;
+                zz.im = -sample.re*z.im - sample.im*z.re;
+                process_half_baud(s, &zz);
+            }
+            dds_advancef(&(s->carrier_phase), s->carrier_phase_rate);
+        }
+    }
+    else
+    {
+        for (i = 0;  i < len;  i++)
+        {
+            s->rrc_filter[s->rrc_filter_step] =
+            s->rrc_filter[s->rrc_filter_step + V27TER_RX_2400_FILTER_STEPS] = amp[i];
+            if (++s->rrc_filter_step >= V27TER_RX_2400_FILTER_STEPS)
+                s->rrc_filter_step = 0;
+
+            /* There should be no DC in the signal, but sometimes there is.
+               We need to measure the power with the DC blocked, but not using
+               a slow to respond DC blocker. Use the most elementary HPF. */
+            x = amp[i] >> 1;
+            diff = x - s->last_sample;
+            power = power_meter_update(&(s->power), diff);
+#if defined(IAXMODEM_STUFF)
+            /* Quick power drop fudge */
+            diff = abs(diff);
+            if (10*diff < s->high_sample)
+            {
+                if (++s->low_samples > 120)
+                {
+                    power_meter_init(&(s->power), 4);
+                    s->high_sample = 0;
+                    s->low_samples = 0;
+                }
+            }
+            else
+            { 
+                s->low_samples = 0;
+                if (diff > s->high_sample)
+                   s->high_sample = diff;
+            }
+#endif
+            s->last_sample = x;
+            //span_log(&s->logging, SPAN_LOG_FLOW, "Power = %f\n", power_meter_current_dbm0(&(s->power)));
+            if (s->signal_present)
+            {
+                /* Look for power below turnoff threshold to turn the carrier off */
+#if defined(IAXMODEM_STUFF)
+                if (s->carrier_drop_pending  ||  power < s->carrier_off_power)
+#else
+                if (power < s->carrier_off_power)
+#endif
+                {
+                    if (--s->signal_present <= 0)
+                    {
+                        /* Count down a short delay, to ensure we push the last
+                           few bits through the filters before stopping. */
+                        v27ter_rx_restart(s, s->bit_rate, FALSE);
+                        report_status_change(s, PUTBIT_CARRIER_DOWN);
+                        continue;
+                    }
+#if defined(IAXMODEM_STUFF)
+                    /* Carrier has dropped, but the put_bit is
+                       pending the signal_present delay. */
+                    s->carrier_drop_pending = TRUE;
+#endif
+                }
+            }
+            else
+            {
+                /* Look for power exceeding turnon threshold to turn the carrier on */
+                if (power < s->carrier_on_power)
+                    continue;
+                s->signal_present = 1;
+#if defined(IAXMODEM_STUFF)
+                s->carrier_drop_pending = FALSE;
+#endif
+                report_status_change(s, PUTBIT_CARRIER_UP);
+            }
+            /* Only spend effort processing this data if the modem is not
+               parked, after training failure. */
+            if (s->training_stage == TRAINING_STAGE_PARKED)
+                continue;
+        
+            /* Put things into the equalization buffer at T/2 rate. The Gardner algorithm
+               will fiddle the step to align this with the symbols. */
+            if ((s->eq_put_step -= RX_PULSESHAPER_2400_COEFF_SETS) <= 0)
+            {
+                if (s->training_stage == TRAINING_STAGE_SYMBOL_ACQUISITION)
+                {
+                    /* Only AGC during the initial training */
+                    s->agc_scaling = (1.0f/RX_PULSESHAPER_2400_GAIN)*1.414f/sqrtf(power);
+                }
+                /* Pulse shape while still at the carrier frequency, using a quadrature
+                   pair of filters. This results in a properly bandpass filtered complex
+                   signal, which can be brought directly to bandband by complex mixing.
+                   No further filtering, to remove mixer harmonics, is needed. */
+                step = -s->eq_put_step;
+                if (step > RX_PULSESHAPER_2400_COEFF_SETS - 1)
+                    step = RX_PULSESHAPER_2400_COEFF_SETS - 1;
+                s->eq_put_step += RX_PULSESHAPER_2400_COEFF_SETS*20/(3*2);
+#if defined(SPANDSP_USE_FIXED_POINT)
+                zi.re = (int32_t) rx_pulseshaper_2400[step][0].re*(int32_t) s->rrc_filter[s->rrc_filter_step];
+                zi.im = (int32_t) rx_pulseshaper_2400[step][0].im*(int32_t) s->rrc_filter[s->rrc_filter_step];
+                for (j = 1;  j < V27TER_RX_2400_FILTER_STEPS;  j++)
+                {
+                    zi.re += (int32_t) rx_pulseshaper_2400[step][j].re*(int32_t) s->rrc_filter[j + s->rrc_filter_step];
+                    zi.im += (int32_t) rx_pulseshaper_2400[step][j].im*(int32_t) s->rrc_filter[j + s->rrc_filter_step];
+                }
+                sample.re = zi.re*s->agc_scaling;
+                sample.im = zi.im*s->agc_scaling;
+#else
+                zz.re = rx_pulseshaper_2400[step][0].re*s->rrc_filter[s->rrc_filter_step];
+                zz.im = rx_pulseshaper_2400[step][0].im*s->rrc_filter[s->rrc_filter_step];
+                for (j = 1;  j < V27TER_RX_2400_FILTER_STEPS;  j++)
+                {
+                    zz.re += rx_pulseshaper_2400[step][j].re*s->rrc_filter[j + s->rrc_filter_step];
+                    zz.im += rx_pulseshaper_2400[step][j].im*s->rrc_filter[j + s->rrc_filter_step];
+                }
+                sample.re = zz.re*s->agc_scaling;
+                sample.im = zz.im*s->agc_scaling;
+#endif
+                /* Shift to baseband - since this is done in a full complex form, the
+                   result is clean, and requires no further filtering apart from the
+                   equalizer. */
+                z = dds_lookup_complexf(s->carrier_phase);
+                zz.re = sample.re*z.re - sample.im*z.im;
+                zz.im = -sample.re*z.im - sample.im*z.re;
+                process_half_baud(s, &zz);
+            }
+            dds_advancef(&(s->carrier_phase), s->carrier_phase_rate);
+        }
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+void v27ter_rx_set_put_bit(v27ter_rx_state_t *s, put_bit_func_t put_bit, void *user_data)
+{
+    s->put_bit = put_bit;
+    s->put_bit_user_data = user_data;
+}
+/*- End of function --------------------------------------------------------*/
+
+void v27ter_rx_set_modem_status_handler(v27ter_rx_state_t *s, modem_tx_status_func_t handler, void *user_data)
+{
+    s->status_handler = handler;
+    s->status_user_data = user_data;
+}
+/*- End of function --------------------------------------------------------*/
+
+int v27ter_rx_restart(v27ter_rx_state_t *s, int bit_rate, int old_train)
+{
+    span_log(&s->logging, SPAN_LOG_FLOW, "Restarting V.27ter\n");
+    if (bit_rate != 4800  &&  bit_rate != 2400)
+        return -1;
+    s->bit_rate = bit_rate;
+
+#if defined(SPANDSP_USE_FIXED_POINT)
+    memset(s->rrc_filter, 0, sizeof(s->rrc_filter));
+#else
+    vec_zerof(s->rrc_filter, sizeof(s->rrc_filter)/sizeof(s->rrc_filter[0]));
+#endif
+    s->rrc_filter_step = 0;
+
+    s->scramble_reg = 0x3C;
+    s->scrambler_pattern_count = 0;
+    s->training_stage = TRAINING_STAGE_SYMBOL_ACQUISITION;
+    s->training_bc = 0;
+    s->training_count = 0;
+    s->training_error = 0.0f;
+    s->signal_present = 0;
+#if defined(IAXMODEM_STUFF)
+    s->high_sample = 0;
+    s->low_samples = 0;
+    s->carrier_drop_pending = FALSE;
+#endif
+
+    s->carrier_phase = 0;
+    s->carrier_track_i = 200000.0f;
+    s->carrier_track_p = 10000000.0f;
+    power_meter_init(&(s->power), 4);
+
+    s->constellation_state = 0;
+
+    if (s->old_train)
+    {
+        s->carrier_phase_rate = s->carrier_phase_rate_save;
+        s->agc_scaling = s->agc_scaling_save;
+        equalizer_restore(s);
+    }
+    else
+    {
+        s->carrier_phase_rate = dds_phase_ratef(CARRIER_NOMINAL_FREQ);
+        s->agc_scaling = 0.005f/RX_PULSESHAPER_4800_GAIN;
+        equalizer_reset(s);
+    }
+    s->eq_skip = 0;
+    s->last_sample = 0;
+
+    s->gardner_integrate = 0;
+    s->total_baud_timing_correction = 0;
+    s->gardner_step = 512;
+    s->baud_phase = 0;
+
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+v27ter_rx_state_t *v27ter_rx_init(v27ter_rx_state_t *s, int bit_rate, put_bit_func_t put_bit, void *user_data)
+{
+    if (s == NULL)
+    {
+        if ((s = (v27ter_rx_state_t *) malloc(sizeof(*s))) == NULL)
+            return NULL;
+    }
+    memset(s, 0, sizeof(*s));
+    span_log_init(&s->logging, SPAN_LOG_NONE, NULL);
+    span_log_set_protocol(&s->logging, "V.27ter RX");
+    v27ter_rx_signal_cutoff(s, -45.5f);
+    s->put_bit = put_bit;
+    s->put_bit_user_data = user_data;
+
+    v27ter_rx_restart(s, bit_rate, FALSE);
+    return s;
+}
+/*- End of function --------------------------------------------------------*/
+
+int v27ter_rx_free(v27ter_rx_state_t *s)
+{
+    free(s);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+void v27ter_rx_set_qam_report_handler(v27ter_rx_state_t *s, qam_report_handler_t handler, void *user_data)
+{
+    s->qam_report = handler;
+    s->qam_user_data = user_data;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/v27ter_tx.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/v27ter_tx.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,422 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * v27ter_tx.c - ITU V.27ter modem transmit part
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2003 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: v27ter_tx.c,v 1.64 2008/07/16 14:23:47 steveu Exp $
+ */
+
+/*! \file */
+
+#if defined(HAVE_CONFIG_H)
+#include <config.h>
+#endif
+
+#include <stdio.h>
+#include <inttypes.h>
+#include <stdlib.h>
+#include <string.h>
+#include "floating_fudge.h"
+#if defined(HAVE_TGMATH_H)
+#include <tgmath.h>
+#endif
+#if defined(HAVE_MATH_H)
+#include <math.h>
+#endif
+
+#include "spandsp/telephony.h"
+#include "spandsp/logging.h"
+#include "spandsp/complex.h"
+#include "spandsp/vector_float.h"
+#include "spandsp/complex_vector_float.h"
+#include "spandsp/async.h"
+#include "spandsp/dds.h"
+#include "spandsp/power_meter.h"
+
+#include "spandsp/v27ter_tx.h"
+
+#if defined(SPANDSP_USE_FIXED_POINT)
+#include "v27ter_tx_4800_fixed_rrc.h"
+#include "v27ter_tx_2400_fixed_rrc.h"
+#else
+#include "v27ter_tx_4800_floating_rrc.h"
+#include "v27ter_tx_2400_floating_rrc.h"
+#endif
+
+#define CARRIER_NOMINAL_FREQ            1800.0f
+
+/* Segments of the training sequence */
+/* V.27ter defines a long and a short sequence. FAX doesn't use the
+   short sequence, so it is not implemented here. */
+#define V27TER_TRAINING_SEG_1           0
+#define V27TER_TRAINING_SEG_2           (V27TER_TRAINING_SEG_1 + 320)
+#define V27TER_TRAINING_SEG_3           (V27TER_TRAINING_SEG_2 + 32)
+#define V27TER_TRAINING_SEG_4           (V27TER_TRAINING_SEG_3 + 50)
+#define V27TER_TRAINING_SEG_5           (V27TER_TRAINING_SEG_4 + 1074)
+#define V27TER_TRAINING_END             (V27TER_TRAINING_SEG_5 + 8)
+#define V27TER_TRAINING_SHUTDOWN_END    (V27TER_TRAINING_END + 32)
+
+static int fake_get_bit(void *user_data)
+{
+    return 1;
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ int scramble(v27ter_tx_state_t *s, int in_bit)
+{
+    int out_bit;
+
+    /* This scrambler is really quite messy to implement. There seems to be no efficient shortcut */
+    out_bit = (in_bit ^ (s->scramble_reg >> 5) ^ (s->scramble_reg >> 6)) & 1;
+    if (s->scrambler_pattern_count >= 33)
+    {
+        out_bit ^= 1;
+        s->scrambler_pattern_count = 0;
+    }
+    else
+    {
+        if ((((s->scramble_reg >> 7) ^ out_bit) & ((s->scramble_reg >> 8) ^ out_bit) & ((s->scramble_reg >> 11) ^ out_bit) & 1))
+            s->scrambler_pattern_count = 0;
+        else
+            s->scrambler_pattern_count++;
+    }
+    s->scramble_reg = (s->scramble_reg << 1) | out_bit;
+    return out_bit;
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ int get_scrambled_bit(v27ter_tx_state_t *s)
+{
+    int bit;
+    
+    if ((bit = s->current_get_bit(s->get_bit_user_data)) == PUTBIT_END_OF_DATA)
+    {
+        /* End of real data. Switch to the fake get_bit routine, until we
+           have shut down completely. */
+        if (s->status_handler)
+            s->status_handler(s->status_user_data, MODEM_TX_STATUS_DATA_EXHAUSTED);
+        s->current_get_bit = fake_get_bit;
+        s->in_training = TRUE;
+        bit = 1;
+    }
+    return scramble(s, bit);
+}
+/*- End of function --------------------------------------------------------*/
+
+#if defined(SPANDSP_USE_FIXED_POINT)
+static complexi16_t getbaud(v27ter_tx_state_t *s)
+#else
+static complexf_t getbaud(v27ter_tx_state_t *s)
+#endif
+{
+    static const int phase_steps_4800[8] =
+    {
+        1, 0, 2, 3, 6, 7, 5, 4
+    };
+    static const int phase_steps_2400[4] =
+    {
+        0, 2, 6, 4
+    };
+#if defined(SPANDSP_USE_FIXED_POINT)
+    static const complexi16_t constellation[8] =
+    {
+        { 1414,    0000},       /*   0deg */
+        { 1000,    1000},       /*  45deg */
+        { 0000,    1414},       /*  90deg */
+        {-1000,    1000},       /* 135deg */
+        {-1414,    0000},       /* 180deg */
+        {-1000,   -1000},       /* 225deg */
+        { 0000,   -1414},       /* 270deg */
+        { 1000,   -1000}        /* 315deg */
+    };
+#else
+    static const complexf_t constellation[8] =
+    {
+        { 1.414f,  0.0f},       /*   0deg */
+        { 1.0f,    1.0f},       /*  45deg */
+        { 0.0f,    1.414f},     /*  90deg */
+        {-1.0f,    1.0f},       /* 135deg */
+        {-1.414f,  0.0f},       /* 180deg */
+        {-1.0f,   -1.0f},       /* 225deg */
+        { 0.0f,   -1.414f},     /* 270deg */
+        { 1.0f,   -1.0f}        /* 315deg */
+    };
+#endif
+    int bits;
+
+    if (s->in_training)
+    {
+       	/* Send the training sequence */
+        if (++s->training_step <= V27TER_TRAINING_SEG_5)
+        {
+            if (s->training_step <= V27TER_TRAINING_SEG_4)
+            {
+                if (s->training_step <= V27TER_TRAINING_SEG_2)
+                {
+                    /* Segment 1: Unmodulated carrier (talker echo protection) */
+                    return constellation[0];
+                }
+                if (s->training_step <= V27TER_TRAINING_SEG_3)
+                {
+                    /* Segment 2: Silence */
+#if defined(SPANDSP_USE_FIXED_POINT)
+                    return complex_seti16(0, 0);
+#else
+                    return complex_setf(0.0f, 0.0f);
+#endif
+                }
+                /* Segment 3: Regular reversals... */
+                s->constellation_state = (s->constellation_state + 4) & 7;
+                return constellation[s->constellation_state];
+            }
+            /* Segment 4: Scrambled reversals... */
+            /* Apply the 1 + x^-6 + x^-7 scrambler. We want every third
+               bit from the scrambler. */
+            bits = get_scrambled_bit(s) << 2;
+            get_scrambled_bit(s);
+            get_scrambled_bit(s);
+            s->constellation_state = (s->constellation_state + bits) & 7;
+            return constellation[s->constellation_state];
+        }
+        /* We should be in the block of test ones, or shutdown ones, if we get here. */
+        /* There is no graceful shutdown procedure defined for V.27ter. Just
+           send some ones, to ensure we get the real data bits through, even
+           with bad ISI. */
+        if (s->training_step == V27TER_TRAINING_END + 1)
+        {
+            /* End of the last segment - segment 5: All ones */
+            /* Switch from the fake get_bit routine, to the user supplied real
+               one, and we are up and running. */
+            s->current_get_bit = s->get_bit;
+            s->in_training = FALSE;
+        }
+        if (s->training_step == V27TER_TRAINING_SHUTDOWN_END)
+        {
+            if (s->status_handler)
+                s->status_handler(s->status_user_data, MODEM_TX_STATUS_SHUTDOWN_COMPLETE);
+        }
+    }
+    /* 4800bps uses 8 phases. 2400bps uses 4 phases. */
+    if (s->bit_rate == 4800)
+    {
+        bits = get_scrambled_bit(s);
+        bits = (bits << 1) | get_scrambled_bit(s);
+        bits = (bits << 1) | get_scrambled_bit(s);
+        bits = phase_steps_4800[bits];
+    }
+    else
+    {
+        bits = get_scrambled_bit(s);
+        bits = (bits << 1) | get_scrambled_bit(s);
+        bits = phase_steps_2400[bits];
+    }
+    s->constellation_state = (s->constellation_state + bits) & 7;
+    return constellation[s->constellation_state];
+}
+/*- End of function --------------------------------------------------------*/
+
+int v27ter_tx(v27ter_tx_state_t *s, int16_t amp[], int len)
+{
+#if defined(SPANDSP_USE_FIXED_POINT)
+    complexi_t x;
+    complexi_t z;
+#else
+    complexf_t x;
+    complexf_t z;
+#endif
+    int i;
+    int sample;
+
+    if (s->training_step >= V27TER_TRAINING_SHUTDOWN_END)
+    {
+        /* Once we have sent the shutdown symbols, we stop sending completely. */
+        return 0;
+    }
+    /* The symbol rates for the two bit rates are different. This makes it difficult to
+       merge both generation procedures into a single efficient loop. We do not bother
+       trying. We use two independent loops, filter coefficients, etc. */
+    if (s->bit_rate == 4800)
+    {
+        for (sample = 0;  sample < len;  sample++)
+        {
+            if (++s->baud_phase >= 5)
+            {
+                s->baud_phase -= 5;
+                s->rrc_filter[s->rrc_filter_step] =
+                s->rrc_filter[s->rrc_filter_step + V27TER_TX_FILTER_STEPS] = getbaud(s);
+                if (++s->rrc_filter_step >= V27TER_TX_FILTER_STEPS)
+                    s->rrc_filter_step = 0;
+            }
+            /* Root raised cosine pulse shaping at baseband */
+#if defined(SPANDSP_USE_FIXED_POINT)
+            x = complex_seti(0, 0);
+            for (i = 0;  i < V27TER_TX_FILTER_STEPS;  i++)
+            {
+                x.re += (int32_t) tx_pulseshaper_4800[TX_PULSESHAPER_4800_COEFF_SETS - 1 - s->baud_phase][i]*(int32_t) s->rrc_filter[i + s->rrc_filter_step].re;
+                x.im += (int32_t) tx_pulseshaper_4800[TX_PULSESHAPER_4800_COEFF_SETS - 1 - s->baud_phase][i]*(int32_t) s->rrc_filter[i + s->rrc_filter_step].im;
+            }
+            /* Now create and modulate the carrier */
+            x.re >>= 14;
+            x.im >>= 14;
+            z = dds_complexi(&(s->carrier_phase), s->carrier_phase_rate);
+            /* Don't bother saturating. We should never clip. */
+            i = (x.re*z.re - x.im*z.im) >> 15;
+            amp[sample] = (int16_t) ((i*s->gain_4800) >> 15);
+#else
+            x = complex_setf(0.0f, 0.0f);
+            for (i = 0;  i < V27TER_TX_FILTER_STEPS;  i++)
+            {
+                x.re += tx_pulseshaper_4800[TX_PULSESHAPER_4800_COEFF_SETS - 1 - s->baud_phase][i]*s->rrc_filter[i + s->rrc_filter_step].re;
+                x.im += tx_pulseshaper_4800[TX_PULSESHAPER_4800_COEFF_SETS - 1 - s->baud_phase][i]*s->rrc_filter[i + s->rrc_filter_step].im;
+            }
+            /* Now create and modulate the carrier */
+            z = dds_complexf(&(s->carrier_phase), s->carrier_phase_rate);
+            /* Don't bother saturating. We should never clip. */
+            amp[sample] = (int16_t) lrintf((x.re*z.re - x.im*z.im)*s->gain_4800);
+#endif
+        }
+    }
+    else
+    {
+        for (sample = 0;  sample < len;  sample++)
+        {
+            if ((s->baud_phase += 3) >= 20)
+            {
+                s->baud_phase -= 20;
+                s->rrc_filter[s->rrc_filter_step] =
+                s->rrc_filter[s->rrc_filter_step + V27TER_TX_FILTER_STEPS] = getbaud(s);
+                if (++s->rrc_filter_step >= V27TER_TX_FILTER_STEPS)
+                    s->rrc_filter_step = 0;
+            }
+            /* Root raised cosine pulse shaping at baseband */
+#if defined(SPANDSP_USE_FIXED_POINT)
+            x = complex_seti(0, 0);
+            for (i = 0;  i < V27TER_TX_FILTER_STEPS;  i++)
+            {
+                x.re += (int32_t) tx_pulseshaper_2400[TX_PULSESHAPER_2400_COEFF_SETS - 1 - s->baud_phase][i]*(int32_t) s->rrc_filter[i + s->rrc_filter_step].re;
+                x.im += (int32_t) tx_pulseshaper_2400[TX_PULSESHAPER_2400_COEFF_SETS - 1 - s->baud_phase][i]*(int32_t) s->rrc_filter[i + s->rrc_filter_step].im;
+            }
+            /* Now create and modulate the carrier */
+            x.re >>= 14;
+            x.im >>= 14;
+            z = dds_complexi(&(s->carrier_phase), s->carrier_phase_rate);
+            /* Don't bother saturating. We should never clip. */
+            i = (x.re*z.re - x.im*z.im) >> 15;
+            amp[sample] = (int16_t) ((i*s->gain_2400) >> 15);
+#else
+            x = complex_setf(0.0f, 0.0f);
+            for (i = 0;  i < V27TER_TX_FILTER_STEPS;  i++)
+            {
+                x.re += tx_pulseshaper_2400[TX_PULSESHAPER_2400_COEFF_SETS - 1 - s->baud_phase][i]*s->rrc_filter[i + s->rrc_filter_step].re;
+                x.im += tx_pulseshaper_2400[TX_PULSESHAPER_2400_COEFF_SETS - 1 - s->baud_phase][i]*s->rrc_filter[i + s->rrc_filter_step].im;
+            }
+            /* Now create and modulate the carrier */
+            z = dds_complexf(&(s->carrier_phase), s->carrier_phase_rate);
+            /* Don't bother saturating. We should never clip. */
+            amp[sample] = (int16_t) lrintf((x.re*z.re - x.im*z.im)*s->gain_2400);
+#endif
+        }
+    }
+    return sample;
+}
+/*- End of function --------------------------------------------------------*/
+
+void v27ter_tx_power(v27ter_tx_state_t *s, float power)
+{
+    float l;
+
+    l = powf(10.0f, (power - DBM0_MAX_POWER)/20.0f)*32768.0f;
+#if defined(SPANDSP_USE_FIXED_POINT)
+    s->gain_2400 = 16.0f*1.024f*(32767.0f/28828.51f)*l/TX_PULSESHAPER_2400_GAIN;
+    s->gain_4800 = 16.0f*1.024f*(32767.0f/28828.46f)*l/TX_PULSESHAPER_4800_GAIN;
+#else
+    s->gain_2400 = l/TX_PULSESHAPER_2400_GAIN;
+    s->gain_4800 = l/TX_PULSESHAPER_4800_GAIN;
+#endif
+}
+/*- End of function --------------------------------------------------------*/
+
+void v27ter_tx_set_get_bit(v27ter_tx_state_t *s, get_bit_func_t get_bit, void *user_data)
+{
+    if (s->get_bit == s->current_get_bit)
+        s->current_get_bit = get_bit;
+    s->get_bit = get_bit;
+    s->get_bit_user_data = user_data;
+}
+/*- End of function --------------------------------------------------------*/
+
+void v27ter_tx_set_modem_status_handler(v27ter_tx_state_t *s, modem_tx_status_func_t handler, void *user_data)
+{
+    s->status_handler = handler;
+    s->status_user_data = user_data;
+}
+/*- End of function --------------------------------------------------------*/
+
+int v27ter_tx_restart(v27ter_tx_state_t *s, int bit_rate, int tep)
+{
+    if (bit_rate != 4800  &&  bit_rate != 2400)
+        return -1;
+    s->bit_rate = bit_rate;
+#if defined(SPANDSP_USE_FIXED_POINT)
+    memset(s->rrc_filter, 0, sizeof(s->rrc_filter));
+#else
+    cvec_zerof(s->rrc_filter, sizeof(s->rrc_filter)/sizeof(s->rrc_filter[0]));
+#endif
+    s->rrc_filter_step = 0;
+    s->scramble_reg = 0x3C;
+    s->scrambler_pattern_count = 0;
+    s->in_training = TRUE;
+    s->training_step = (tep)  ?  V27TER_TRAINING_SEG_1  :  V27TER_TRAINING_SEG_2;
+    s->carrier_phase = 0;
+    s->baud_phase = 0;
+    s->constellation_state = 0;
+    s->current_get_bit = fake_get_bit;
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+v27ter_tx_state_t *v27ter_tx_init(v27ter_tx_state_t *s, int bit_rate, int tep, get_bit_func_t get_bit, void *user_data)
+{
+    if (s == NULL)
+    {
+        if ((s = (v27ter_tx_state_t *) malloc(sizeof(*s))) == NULL)
+            return NULL;
+    }
+    memset(s, 0, sizeof(*s));
+    span_log_init(&s->logging, SPAN_LOG_NONE, NULL);
+    span_log_set_protocol(&s->logging, "V.27ter TX");
+    s->get_bit = get_bit;
+    s->get_bit_user_data = user_data;
+    s->carrier_phase_rate = dds_phase_ratef(CARRIER_NOMINAL_FREQ);
+    v27ter_tx_power(s, -14.0f);
+    v27ter_tx_restart(s, bit_rate, tep);
+    return s;
+}
+/*- End of function --------------------------------------------------------*/
+
+int v27ter_tx_free(v27ter_tx_state_t *s)
+{
+    free(s);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/v29rx.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/v29rx.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,935 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * v29rx.c - ITU V.29 modem receive part
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2003, 2004, 2005, 2006, 2007 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: v29rx.c,v 1.124 2008/07/17 19:12:27 steveu Exp $
+ */
+
+/*! \file */
+
+#if defined(HAVE_CONFIG_H)
+#include <config.h>
+#endif
+
+#include <stdlib.h>
+#include <inttypes.h>
+#include <string.h>
+#include <stdio.h>
+#include "floating_fudge.h"
+#if defined(HAVE_TGMATH_H)
+#include <tgmath.h>
+#endif
+#if defined(HAVE_MATH_H)
+#include <math.h>
+#endif
+
+#include "spandsp/telephony.h"
+#include "spandsp/logging.h"
+#include "spandsp/complex.h"
+#include "spandsp/vector_float.h"
+#include "spandsp/complex_vector_float.h"
+#include "spandsp/async.h"
+#include "spandsp/power_meter.h"
+#include "spandsp/arctan2.h"
+#include "spandsp/dds.h"
+#include "spandsp/complex_filters.h"
+
+#include "spandsp/v29rx.h"
+
+#include "v29tx_constellation_maps.h"
+#if defined(SPANDSP_USE_FIXED_POINT)
+#include "v29rx_fixed_rrc.h"
+#else
+#include "v29rx_floating_rrc.h"
+#endif
+
+#define CARRIER_NOMINAL_FREQ        1700.0f
+#define BAUD_RATE                   2400
+#define EQUALIZER_DELTA             0.21f
+
+/* Segments of the training sequence */
+#define V29_TRAINING_SEG_2_LEN      128
+#define V29_TRAINING_SEG_3_LEN      384
+#define V29_TRAINING_SEG_4_LEN      48
+
+enum
+{
+    TRAINING_STAGE_NORMAL_OPERATION = 0,
+    TRAINING_STAGE_SYMBOL_ACQUISITION,
+    TRAINING_STAGE_LOG_PHASE,
+    TRAINING_STAGE_WAIT_FOR_CDCD,
+    TRAINING_STAGE_TRAIN_ON_CDCD,
+    TRAINING_STAGE_TRAIN_ON_CDCD_AND_TEST,
+    TRAINING_STAGE_TEST_ONES,
+    TRAINING_STAGE_PARKED
+};
+
+static const uint8_t space_map_9600[20][20] =
+{
+    {13, 13, 13, 13, 13, 13, 12, 12, 12, 12, 12, 12, 12, 12, 11, 11, 11, 11, 11, 11},
+    {13, 13, 13, 13, 13, 13, 13, 12, 12, 12, 12, 12, 12, 11, 11, 11, 11, 11, 11, 11},
+    {13, 13, 13, 13, 13, 13, 13,  4,  4,  4,  4,  4,  4, 11, 11, 11, 11, 11, 11, 11},
+    {13, 13, 13, 13, 13, 13, 13,  4,  4,  4,  4,  4,  4, 11, 11, 11, 11, 11, 11, 11},
+    {13, 13, 13, 13, 13, 13, 13,  4,  4,  4,  4,  4,  4, 11, 11, 11, 11, 11, 11, 11},
+    {13, 13, 13, 13, 13, 13, 13,  5,  4,  4,  4,  4,  3, 11, 11, 11, 11, 11, 11, 11},
+    {14, 13, 13, 13, 13, 13,  5,  5,  5,  5,  3,  3,  3,  3, 11, 11, 11, 11, 11, 10},
+    {14, 14,  6,  6,  6,  5,  5,  5,  5,  5,  3,  3,  3,  3,  3,  2,  2,  2, 10, 10},
+    {14, 14,  6,  6,  6,  6,  5,  5,  5,  5,  3,  3,  3,  3,  2,  2,  2,  2, 10, 10},
+    {14, 14,  6,  6,  6,  6,  5,  5,  5,  5,  3,  3,  3,  3,  2,  2,  2,  2, 10, 10},
+    {14, 14,  6,  6,  6,  6,  7,  7,  7,  7,  1,  1,  1,  1,  2,  2,  2,  2, 10, 10},
+    {14, 14,  6,  6,  6,  6,  7,  7,  7,  7,  1,  1,  1,  1,  2,  2,  2,  2, 10, 10},
+    {14, 14,  6,  6,  6,  7,  7,  7,  7,  7,  1,  1,  1,  1,  1,  2,  2,  2, 10, 10},
+    {14, 15, 15, 15, 15, 15,  7,  7,  7,  7,  1,  1,  1,  1,  9,  9,  9,  9,  9, 10},
+    {15, 15, 15, 15, 15, 15, 15,  7,  0,  0,  0,  0,  1,  9,  9,  9,  9,  9,  9,  9},
+    {15, 15, 15, 15, 15, 15, 15,  0,  0,  0,  0,  0,  0,  9,  9,  9,  9,  9,  9,  9},
+    {15, 15, 15, 15, 15, 15, 15,  0,  0,  0,  0,  0,  0,  9,  9,  9,  9,  9,  9,  9},
+    {15, 15, 15, 15, 15, 15, 15,  0,  0,  0,  0,  0,  0,  9,  9,  9,  9,  9,  9,  9},
+    {15, 15, 15, 15, 15, 15, 15,  8,  8,  8,  8,  8,  8,  9,  9,  9,  9,  9,  9,  9},
+    {15, 15, 15, 15, 15, 15,  8,  8,  8,  8,  8,  8,  8,  8,  9,  9,  9,  9,  9,  9}
+};
+
+/* Coefficients for the band edge symbol timing synchroniser (alpha = 0.99) */
+#define SYNC_LOW_BAND_EDGE_COEFF_0       1.829281f    /* 2*alpha*cos(low_edge) */
+#define SYNC_LOW_BAND_EDGE_COEFF_1      -0.980100f    /* -alpha^2 */
+#define SYNC_HIGH_BAND_EDGE_COEFF_0     -1.285907f    /* 2*alpha*cos(high_edge) */
+#define SYNC_HIGH_BAND_EDGE_COEFF_1     -0.980100f    /* -alpha^2 */
+#define SYNC_CROSS_CORR_COEFF_A         -0.932131f    /* -alpha^2*sin(freq_diff) */
+#define SYNC_CROSS_CORR_COEFF_B          0.752802f    /* alpha*sin(high_edge) */
+#define SYNC_CROSS_CORR_COEFF_C         -0.378857f    /* -alpha*sin(low_edge) */
+
+float v29_rx_carrier_frequency(v29_rx_state_t *s)
+{
+    return dds_frequencyf(s->carrier_phase_rate);
+}
+/*- End of function --------------------------------------------------------*/
+
+float v29_rx_symbol_timing_correction(v29_rx_state_t *s)
+{
+    return (float) s->total_baud_timing_correction/((float) RX_PULSESHAPER_COEFF_SETS*10.0f/3.0f);
+}
+/*- End of function --------------------------------------------------------*/
+
+void v29_rx_signal_cutoff(v29_rx_state_t *s, float cutoff)
+{
+    /* The 0.4 factor allows for the gain of the DC blocker */
+    s->carrier_on_power = (int32_t) (power_meter_level_dbm0(cutoff + 2.5f)*0.4f);
+    s->carrier_off_power = (int32_t) (power_meter_level_dbm0(cutoff - 2.5f)*0.4f);
+}
+/*- End of function --------------------------------------------------------*/
+
+float v29_rx_signal_power(v29_rx_state_t *s)
+{
+    return power_meter_current_dbm0(&s->power);
+}
+/*- End of function --------------------------------------------------------*/
+
+int v29_rx_equalizer_state(v29_rx_state_t *s, complexf_t **coeffs)
+{
+    *coeffs = s->eq_coeff;
+    return V29_EQUALIZER_PRE_LEN + 1 + V29_EQUALIZER_POST_LEN;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void equalizer_save(v29_rx_state_t *s)
+{
+    cvec_copyf(s->eq_coeff_save, s->eq_coeff, V29_EQUALIZER_PRE_LEN + 1 + V29_EQUALIZER_POST_LEN);
+}
+/*- End of function --------------------------------------------------------*/
+
+static void report_status_change(v29_rx_state_t *s, int status)
+{
+    if (s->status_handler)
+        s->status_handler(s->status_user_data, status);
+    else if (s->put_bit)
+        s->put_bit(s->put_bit_user_data, status);
+}
+/*- End of function --------------------------------------------------------*/
+
+static void equalizer_restore(v29_rx_state_t *s)
+{
+    cvec_copyf(s->eq_coeff, s->eq_coeff_save, V29_EQUALIZER_PRE_LEN + 1 + V29_EQUALIZER_POST_LEN);
+    cvec_zerof(s->eq_buf, V29_EQUALIZER_MASK);
+
+    s->eq_put_step = RX_PULSESHAPER_COEFF_SETS*10/(3*2) - 1;
+    s->eq_step = 0;
+    s->eq_delta = EQUALIZER_DELTA/(V29_EQUALIZER_PRE_LEN + 1 + V29_EQUALIZER_POST_LEN);
+}
+/*- End of function --------------------------------------------------------*/
+
+static void equalizer_reset(v29_rx_state_t *s)
+{
+    /* Start with an equalizer based on everything being perfect */
+    cvec_zerof(s->eq_coeff, V29_EQUALIZER_PRE_LEN + 1 + V29_EQUALIZER_POST_LEN);
+    s->eq_coeff[V29_EQUALIZER_PRE_LEN] = complex_setf(3.0f, 0.0f);
+    cvec_zerof(s->eq_buf, V29_EQUALIZER_MASK);
+
+    s->eq_put_step = RX_PULSESHAPER_COEFF_SETS*10/(3*2) - 1;
+    s->eq_step = 0;
+    s->eq_delta = EQUALIZER_DELTA/(V29_EQUALIZER_PRE_LEN + 1 + V29_EQUALIZER_POST_LEN);
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ complexf_t equalizer_get(v29_rx_state_t *s)
+{
+    int i;
+    int p;
+    complexf_t z;
+    complexf_t z1;
+
+    /* Get the next equalized value. */
+    z = complex_setf(0.0f, 0.0f);
+    p = s->eq_step - 1;
+    for (i = 0;  i < V29_EQUALIZER_PRE_LEN + 1 + V29_EQUALIZER_POST_LEN;  i++)
+    {
+        p = (p - 1) & V29_EQUALIZER_MASK;
+        z1 = complex_mulf(&s->eq_coeff[i], &s->eq_buf[p]);
+        z = complex_addf(&z, &z1);
+    }
+    return z;
+}
+/*- End of function --------------------------------------------------------*/
+
+#if defined(SPANDSP_USE_FIXED_POINTy)
+static void tune_equalizer(v29_rx_state_t *s, const complexi16_t *z, const complexi16_t *target)
+#else
+static void tune_equalizer(v29_rx_state_t *s, const complexf_t *z, const complexf_t *target)
+#endif
+{
+    int i;
+    int p;
+    complexf_t ez;
+    complexf_t z1;
+
+    /* Find the x and y mismatch from the exact constellation position. */
+    ez = complex_subf(target, z);
+    ez.re *= s->eq_delta;
+    ez.im *= s->eq_delta;
+
+    p = s->eq_step - 1;
+    for (i = 0;  i < V29_EQUALIZER_PRE_LEN + 1 + V29_EQUALIZER_POST_LEN;  i++)
+    {
+        p = (p - 1) & V29_EQUALIZER_MASK;
+        z1 = complex_conjf(&s->eq_buf[p]);
+        z1 = complex_mulf(&ez, &z1);
+        s->eq_coeff[i] = complex_addf(&s->eq_coeff[i], &z1);
+        /* Leak a little to tame uncontrolled wandering */
+        s->eq_coeff[i].re *= 0.9999f;
+        s->eq_coeff[i].im *= 0.9999f;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static int scrambled_training_bit(v29_rx_state_t *s)
+{
+    int bit;
+
+    /* Segment 3 of the training sequence - the scrambled CDCD part. */
+    /* Apply the 1 + x^-6 + x^-7 scrambler */
+    bit = s->training_scramble_reg & 1;
+    s->training_scramble_reg >>= 1;
+    if (bit ^ (s->training_scramble_reg & 1))
+        s->training_scramble_reg |= 0x40;
+    return bit;
+}
+/*- End of function --------------------------------------------------------*/
+
+#if defined(SPANDSP_USE_FIXED_POINTy)
+static __inline__ int find_quadrant(const complexi16_t *z)
+#else
+static __inline__ int find_quadrant(const complexf_t *z)
+#endif
+{
+    int b1;
+    int b2;
+
+    /* Split the space along the two diagonals. */
+    b1 = (z->im > z->re);
+    b2 = (z->im < -z->re);
+    return (b2 << 1) | (b1 ^ b2);
+}
+/*- End of function --------------------------------------------------------*/
+
+#if defined(SPANDSP_USE_FIXED_POINTy)
+static __inline__ void track_carrier(v29_rx_state_t *s, const complexi16_t *z, const complexi16_t *target)
+#else
+static __inline__ void track_carrier(v29_rx_state_t *s, const complexf_t *z, const complexf_t *target)
+#endif
+{
+    float error;
+
+    /* The initial coarse carrier frequency and phase estimation should have
+       got us in the right ballpark. Now we need to fine tune fairly quickly,
+       to get the receovered carrier more precisely on target. Then we need to
+       fine tune in a more damped way to keep us on target. The goal is to have
+       things running really well by the time the training is complete. 
+       We assume the frequency of the oscillators at the two ends drift only
+       very slowly. The PSTN has rather limited doppler problems. :-) Any
+       remaining FDM in the network should also drift slowly. */
+    /* For small errors the imaginary part of the difference between the actual and the target
+       positions is proportional to the phase error, for any particular target. However, the
+       different amplitudes of the various target positions scale things. This isn't all bad,
+       as the angular error for the larger amplitude constellation points is probably
+       a more reliable indicator, and we are weighting it as such. */
+    error = z->im*target->re - z->re*target->im;
+
+    /* Use a proportional-integral approach to tracking the carrier. The PI
+       parameters are coarser at first, until we get precisely on target. Then,
+       the filter will be damped more to keep us on target. */
+    s->carrier_phase_rate += (int32_t) (s->carrier_track_i*error);
+    s->carrier_phase += (int32_t) (s->carrier_track_p*error);
+    //span_log(&s->logging, SPAN_LOG_FLOW, "Im = %15.5f   f = %15.5f\n", error, dds_frequencyf(s->carrier_phase_rate));
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ void put_bit(v29_rx_state_t *s, int bit)
+{
+    int out_bit;
+
+    bit &= 1;
+
+    /* Descramble the bit */
+    out_bit = (bit ^ (s->scramble_reg >> 17) ^ (s->scramble_reg >> 22)) & 1;
+    s->scramble_reg = (s->scramble_reg << 1) | bit;
+
+    /* We need to strip the last part of the training - the test period of all 1s -
+       before we let data go to the application. */
+    if (s->training_stage == TRAINING_STAGE_NORMAL_OPERATION)
+    {
+        s->put_bit(s->put_bit_user_data, out_bit);
+    }
+    else
+    {
+        /* The bits during the final stage of training should be all ones. However,
+           buggy modems mean you cannot rely on this. Therefore we don't bother
+           testing for ones, but just rely on a constellation mismatch measurement. */
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static void decode_baud(v29_rx_state_t *s, complexf_t *z)
+{
+    static const uint8_t phase_steps_9600[8] =
+    {
+        4, 0, 2, 6, 7, 3, 1, 5
+    };
+    static const uint8_t phase_steps_4800[4] =
+    {
+        0, 2, 3, 1
+    };
+    int nearest;
+    int raw_bits;
+    int i;
+    int re;
+    int im;
+
+    switch (s->bit_rate)
+    {
+    case 9600:
+    default:
+        re = (int) ((z->re + 5.0f)*2.0f);
+        if (re > 19)
+            re = 19;
+        else if (re < 0)
+            re = 0;
+        im = (int) ((z->im + 5.0f)*2.0f);
+        if (im > 19)
+            im = 19;
+        else if (im < 0)
+            im = 0;
+        nearest = space_map_9600[re][im];
+        /* Deal with the amplitude bit */
+        put_bit(s, nearest >> 3);
+        raw_bits = phase_steps_9600[(nearest - s->constellation_state) & 7];
+        for (i = 0;  i < 3;  i++)
+        {
+            put_bit(s, raw_bits);
+            raw_bits >>= 1;
+        }
+        break;
+    case 7200:
+        /* We can reuse the space map for 9600, but drop the top bit */
+        re = (int) ((z->re + 5.0f)*2.0f);
+        if (re > 19)
+            re = 19;
+        else if (re < 0)
+            re = 0;
+        im = (int) ((z->im + 5.0f)*2.0f);
+        if (im > 19)
+            im = 19;
+        else if (im < 0)
+            im = 0;
+        nearest = space_map_9600[re][im] & 7;
+        raw_bits = phase_steps_9600[(nearest - s->constellation_state) & 7];
+        for (i = 0;  i < 3;  i++)
+        {
+            put_bit(s, raw_bits);
+            raw_bits >>= 1;
+        }
+        break;
+    case 4800:
+        nearest = find_quadrant(z) << 1;
+        raw_bits = phase_steps_4800[((nearest - s->constellation_state) >> 1) & 3];
+        put_bit(s, raw_bits);
+        put_bit(s, raw_bits >> 1);
+        break;
+    }
+    track_carrier(s, z, &v29_9600_constellation[nearest]);
+    if (--s->eq_skip <= 0)
+    {
+        /* Once we are in the data the equalization should not need updating.
+           However, the line characteristics may slowly drift. We, therefore,
+           tune up on the occassional sample, keeping the compute down. */
+        s->eq_skip = 10;
+        tune_equalizer(s, z, &v29_9600_constellation[nearest]);
+    }
+    s->constellation_state = nearest;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void process_half_baud(v29_rx_state_t *s, complexf_t *sample)
+{
+    static const int cdcd_pos[6] =
+    {
+        0, 11,
+        0,  3,
+        0,  2
+    };
+    complexf_t z;
+    complexf_t zz;
+#if defined(SPANDSP_USE_FIXED_POINTy)
+    const complexi16_t *target;
+#else
+    const complexf_t *target;
+#endif
+    float v;
+    float p;
+    int bit;
+    int i;
+    int j;
+    int32_t angle;
+    int32_t ang;
+
+    /* This routine processes every half a baud, as we put things into the equalizer at the T/2 rate.
+       This routine adapts the position of the half baud samples, which the caller takes. */
+
+    /* Add a sample to the equalizer's circular buffer, but don't calculate anything
+       at this time. */
+    s->eq_buf[s->eq_step] = *sample;
+    s->eq_step = (s->eq_step + 1) & V29_EQUALIZER_MASK;
+
+    /* On alternate insertions we have a whole baud, and must process it. */
+    if ((s->baud_half ^= 1))
+        return;
+
+    /* Symbol timing synchronisation */
+    /* Cross correlate */
+    v = s->symbol_sync_low[1]*s->symbol_sync_high[1]*SYNC_CROSS_CORR_COEFF_A
+      + s->symbol_sync_low[0]*s->symbol_sync_high[1]*SYNC_CROSS_CORR_COEFF_B
+      + s->symbol_sync_low[1]*s->symbol_sync_high[0]*SYNC_CROSS_CORR_COEFF_C;
+
+    /* Filter away any DC component  */
+    p = v - s->symbol_sync_dc_filter[1];
+    s->symbol_sync_dc_filter[1] = s->symbol_sync_dc_filter[0];
+    s->symbol_sync_dc_filter[0] = v;
+    /* A little integration will now filter away much of the noise */
+    s->baud_phase -= p;
+
+    if (fabsf(s->baud_phase) > 30.0f)
+    {
+        if (s->baud_phase > 0.0f)
+            i = (s->baud_phase > 1000.0f)  ?  5  :  1;
+        else
+            i = (s->baud_phase < -1000.0f)  ?  -5  :  -1;
+
+        //printf("v = %10.5f %5d - %f %f %d %d\n", v, i, p, s->baud_phase, s->total_baud_timing_correction);
+        s->eq_put_step += i;
+        s->total_baud_timing_correction += i;
+    }
+
+    z = equalizer_get(s);
+
+    switch (s->training_stage)
+    {
+    case TRAINING_STAGE_NORMAL_OPERATION:
+        /* Normal operation. */
+        decode_baud(s, &z);
+        target = &v29_9600_constellation[s->constellation_state];
+        break;
+    case TRAINING_STAGE_SYMBOL_ACQUISITION:
+        /* Allow time for symbol synchronisation to settle the symbol timing. */
+        target = &z;
+        if (++s->training_count >= 60)
+        {
+            /* Record the current phase angle */
+            s->training_stage = TRAINING_STAGE_LOG_PHASE;
+            s->angles[0] =
+            s->start_angles[0] = arctan2(z.im, z.re);
+        }
+        break;
+    case TRAINING_STAGE_LOG_PHASE:
+        /* Record the current alternate phase angle */
+        target = &z;
+        s->angles[1] =
+        s->start_angles[1] = arctan2(z.im, z.re);
+        s->training_count = 1;
+        s->training_stage = TRAINING_STAGE_WAIT_FOR_CDCD;
+        break;
+    case TRAINING_STAGE_WAIT_FOR_CDCD:
+        target = &z;
+        angle = arctan2(z.im, z.re);
+        /* Look for the initial ABAB sequence to display a phase reversal, which will
+           signal the start of the scrambled CDCD segment */
+        ang = angle - s->angles[(s->training_count - 1) & 0xF];
+        s->angles[(s->training_count + 1) & 0xF] = angle;
+        if ((ang > 0x20000000  ||  ang < -0x20000000)  &&  s->training_count >= 13)
+        {
+            /* We seem to have a phase reversal */
+            /* Slam the carrier frequency into line, based on the total phase drift over the last
+               section. Use the shift from the odd bits and the shift from the even bits to get
+               better jitter suppression. We need to scale here, or at the maximum specified
+               frequency deviation we could overflow, and get a silly answer. */
+            /* Step back a few symbols so we don't get ISI distorting things. */
+            i = (s->training_count - 8) & ~1;
+            /* Avoid the possibility of a divide by zero */
+            if (i)
+            {
+                j = i & 0xF;
+                ang = (s->angles[j] - s->start_angles[0])/i
+                    + (s->angles[j | 0x1] - s->start_angles[1])/i;
+                s->carrier_phase_rate += 3*(ang/20);
+            }
+            span_log(&s->logging, SPAN_LOG_FLOW, "Coarse carrier frequency %7.2f\n", dds_frequencyf(s->carrier_phase_rate));
+            /* Check if the carrier frequency is plausible */
+            if (s->carrier_phase_rate < dds_phase_ratef(CARRIER_NOMINAL_FREQ - 20.0f)
+                ||
+                s->carrier_phase_rate > dds_phase_ratef(CARRIER_NOMINAL_FREQ + 20.0f))
+            {
+               span_log(&s->logging, SPAN_LOG_FLOW, "Training failed (sequence failed)\n");
+               /* Park this modem */
+               s->training_stage = TRAINING_STAGE_PARKED;
+               report_status_change(s, PUTBIT_TRAINING_FAILED);
+               break;
+            }
+            /* Make a step shift in the phase, to pull it into line. We need to rotate the equalizer
+               buffer, as well as the carrier phase, for this to play out nicely. */
+            p = angle*2.0f*3.14159f/(65536.0f*65536.0f);
+            zz = complex_setf(cosf(p), -sinf(p));
+            for (i = 0;  i <= V29_EQUALIZER_MASK;  i++)
+                s->eq_buf[i] = complex_mulf(&s->eq_buf[i], &zz);
+            s->carrier_phase += angle;
+            /* We have just seen the first bit of the scrambled sequence, so skip it. */
+            bit = scrambled_training_bit(s);
+            s->training_count = 1;
+            s->training_stage = TRAINING_STAGE_TRAIN_ON_CDCD;
+            report_status_change(s, PUTBIT_TRAINING_IN_PROGRESS);
+            break;
+        }
+        if (++s->training_count > V29_TRAINING_SEG_2_LEN)
+        {
+            /* This is bogus. There are not this many bauds in this section
+               of a real training sequence. */
+            span_log(&s->logging, SPAN_LOG_FLOW, "Training failed (sequence failed)\n");
+            /* Park this modem */
+            s->training_stage = TRAINING_STAGE_PARKED;
+            report_status_change(s, PUTBIT_TRAINING_FAILED);
+        }
+        break;
+    case TRAINING_STAGE_TRAIN_ON_CDCD:
+        /* Train on the scrambled CDCD section. */
+        bit = scrambled_training_bit(s);
+        //span_log(&s->logging, SPAN_LOG_FLOW, "%5d %15.5f, %15.5f     %15.5f, %15.5f\n", s->training_count, z.re, z.im, v29_9600_constellation[cdcd_pos[s->training_cd + bit]].re, v29_9600_constellation[cdcd_pos[s->training_cd + bit]].im);
+        s->constellation_state = cdcd_pos[s->training_cd + bit];
+        target = &v29_9600_constellation[s->constellation_state];
+        track_carrier(s, &z, target);
+        tune_equalizer(s, &z, target);
+        if (++s->training_count >= V29_TRAINING_SEG_3_LEN - 48)
+        {
+            s->training_stage = TRAINING_STAGE_TRAIN_ON_CDCD_AND_TEST;
+            s->training_error = 0.0f;
+            s->carrier_track_i = 200.0f;
+            s->carrier_track_p = 1000000.0f;
+        }
+        break;
+    case TRAINING_STAGE_TRAIN_ON_CDCD_AND_TEST:
+        /* Continue training on the scrambled CDCD section, but measure the quality of training too. */
+        bit = scrambled_training_bit(s);
+        //span_log(&s->logging, SPAN_LOG_FLOW, "%5d %15.5f, %15.5f     %15.5f, %15.5f\n", s->training_count, z.re, z.im, v29_9600_constellation[cdcd_pos[s->training_cd + bit]].re, v29_9600_constellation[cdcd_pos[s->training_cd + bit]].im);
+        s->constellation_state = cdcd_pos[s->training_cd + bit];
+        target = &v29_9600_constellation[s->constellation_state];
+        track_carrier(s, &z, target);
+        tune_equalizer(s, &z, target);
+        /* Measure the training error */
+        zz = complex_subf(&z, target);
+        s->training_error += powerf(&zz);
+        if (++s->training_count >= V29_TRAINING_SEG_3_LEN)
+        {
+            span_log(&s->logging, SPAN_LOG_FLOW, "Constellation mismatch %f\n", s->training_error);
+            if (s->training_error < 100.0f)
+            {
+                s->training_count = 0;
+                s->training_error = 0.0f;
+                s->constellation_state = 0;
+                s->training_stage = TRAINING_STAGE_TEST_ONES;
+            }
+            else
+            {
+                span_log(&s->logging, SPAN_LOG_FLOW, "Training failed (convergence failed)\n");
+                /* Park this modem */
+                s->training_stage = TRAINING_STAGE_PARKED;
+                report_status_change(s, PUTBIT_TRAINING_FAILED);
+            }
+        }
+        break;
+    case TRAINING_STAGE_TEST_ONES:
+        /* We are in the test phase, where we check that we can receive reliably.
+           We should get a run of 1's, 48 symbols (192 bits at 9600bps) long. */
+        //span_log(&s->logging, SPAN_LOG_FLOW, "%5d %15.5f, %15.5f\n", s->training_count, z.re, z.im);
+        decode_baud(s, &z);
+        target = &v29_9600_constellation[s->constellation_state];
+        /* Measure the training error */
+        zz = complex_subf(&z, target);
+        s->training_error += powerf(&zz);
+        if (++s->training_count >= V29_TRAINING_SEG_4_LEN)
+        {
+            if (s->training_error < 50.0f)
+            {
+                /* We are up and running */
+                span_log(&s->logging, SPAN_LOG_FLOW, "Training succeeded (constellation mismatch %f)\n", s->training_error);
+                report_status_change(s, PUTBIT_TRAINING_SUCCEEDED);
+                /* Apply some lag to the carrier off condition, to ensure the last few bits get pushed through
+                   the processing. */
+                s->signal_present = 60;
+                s->training_stage = TRAINING_STAGE_NORMAL_OPERATION;
+                equalizer_save(s);
+                s->carrier_phase_rate_save = s->carrier_phase_rate;
+                s->agc_scaling_save = s->agc_scaling;
+            }
+            else
+            {
+                /* Training has failed */
+                span_log(&s->logging, SPAN_LOG_FLOW, "Training failed (constellation mismatch %f)\n", s->training_error);
+                /* Park this modem */
+                report_status_change(s, PUTBIT_TRAINING_FAILED);
+                s->training_stage = TRAINING_STAGE_PARKED;
+            }
+        }
+        break;
+    case TRAINING_STAGE_PARKED:
+    default:
+        /* We failed to train! */
+        /* Park here until the carrier drops. */
+        target = &z;
+        break;
+    }
+    if (s->qam_report)
+        s->qam_report(s->qam_user_data, &z, target, s->constellation_state);
+}
+/*- End of function --------------------------------------------------------*/
+
+int v29_rx(v29_rx_state_t *s, const int16_t amp[], int len)
+{
+    int i;
+    int j;
+    int step;
+    int16_t x;
+    int32_t diff;
+    complexf_t z;
+    complexf_t zz;
+    complexf_t sample;
+#if defined(SPANDSP_USE_FIXED_POINT)
+    complexi_t zi;
+#endif
+    int32_t power;
+    float v;
+
+    for (i = 0;  i < len;  i++)
+    {
+        s->rrc_filter[s->rrc_filter_step] =
+        s->rrc_filter[s->rrc_filter_step + V29_RX_FILTER_STEPS] = amp[i];
+        if (++s->rrc_filter_step >= V29_RX_FILTER_STEPS)
+            s->rrc_filter_step = 0;
+
+        /* There should be no DC in the signal, but sometimes there is.
+           We need to measure the power with the DC blocked, but not using
+           a slow to respond DC blocker. Use the most elementary HPF. */
+        x = amp[i] >> 1;
+        diff = x - s->last_sample;
+        power = power_meter_update(&(s->power), diff);
+#if defined(IAXMODEM_STUFF)
+        /* Quick power drop fudge */
+        diff = abs(diff);
+        if (10*diff < s->high_sample)
+        {
+            if (++s->low_samples > 120)
+            {
+                power_meter_init(&(s->power), 4);
+                s->high_sample = 0;
+                s->low_samples = 0;
+            }
+        }
+        else
+        { 
+            s->low_samples = 0;
+            if (diff > s->high_sample)
+               s->high_sample = diff;
+        }
+#endif
+        s->last_sample = x;
+        if (s->signal_present)
+        {
+            /* Look for power below turn-off threshold to turn the carrier off */
+#if defined(IAXMODEM_STUFF)
+            if (s->carrier_drop_pending  ||  power < s->carrier_off_power)
+#else
+            if (power < s->carrier_off_power)
+#endif
+            {
+                if (--s->signal_present <= 0)
+                {
+                    /* Count down a short delay, to ensure we push the last
+                       few bits through the filters before stopping. */
+                    v29_rx_restart(s, s->bit_rate, FALSE);
+                    report_status_change(s, PUTBIT_CARRIER_DOWN);
+                    continue;
+                }
+#if defined(IAXMODEM_STUFF)
+                /* Carrier has dropped, but the put_bit is
+                   pending the signal_present delay. */
+                s->carrier_drop_pending = TRUE;
+#endif
+            }
+        }
+        else
+        {
+            /* Look for power exceeding turn-on threshold to turn the carrier on */
+            if (power < s->carrier_on_power)
+                continue;
+            s->signal_present = 1;
+#if defined(IAXMODEM_STUFF)
+            s->carrier_drop_pending = FALSE;
+#endif
+            report_status_change(s, PUTBIT_CARRIER_UP);
+        }
+        if (s->training_stage == TRAINING_STAGE_PARKED)
+            continue;
+        /* Only spend effort processing this data if the modem is not
+           parked, after training failure. */
+        s->eq_put_step -= RX_PULSESHAPER_COEFF_SETS;
+        step = -s->eq_put_step;
+        if (step > RX_PULSESHAPER_COEFF_SETS - 1)
+            step = RX_PULSESHAPER_COEFF_SETS - 1;
+        if (step < 0)
+            step += RX_PULSESHAPER_COEFF_SETS;
+#if defined(SPANDSP_USE_FIXED_POINT)
+        zi.re = (int32_t) rx_pulseshaper[step][0].re*(int32_t) s->rrc_filter[s->rrc_filter_step];
+        for (j = 1;  j < V29_RX_FILTER_STEPS;  j++)
+            zi.re += (int32_t) rx_pulseshaper[step][j].re*(int32_t) s->rrc_filter[j + s->rrc_filter_step];
+        sample.re = zi.re*s->agc_scaling;
+#else
+        zz.re = rx_pulseshaper[step][0].re*s->rrc_filter[s->rrc_filter_step];
+        for (j = 1;  j < V29_RX_FILTER_STEPS;  j++)
+            zz.re += rx_pulseshaper[step][j].re*s->rrc_filter[j + s->rrc_filter_step];
+        sample.re = zz.re*s->agc_scaling;
+#endif
+
+        /* Symbol timing synchronisation band edge filters */
+        /* Low Nyquist band edge filter */
+        v = s->symbol_sync_low[0]*SYNC_LOW_BAND_EDGE_COEFF_0 + s->symbol_sync_low[1]*SYNC_LOW_BAND_EDGE_COEFF_1 + sample.re;
+        s->symbol_sync_low[1] = s->symbol_sync_low[0];
+        s->symbol_sync_low[0] = v;
+        /* High Nyquist band edge filter */
+        v = s->symbol_sync_high[0]*SYNC_HIGH_BAND_EDGE_COEFF_0 + s->symbol_sync_high[1]*SYNC_HIGH_BAND_EDGE_COEFF_1 + sample.re;
+        s->symbol_sync_high[1] = s->symbol_sync_high[0];
+        s->symbol_sync_high[0] = v;
+
+        /* Put things into the equalization buffer at T/2 rate. The symbol synchronisation
+           will fiddle the step to align this with the symbols. */
+        if (s->eq_put_step <= 0)
+        {
+            if (s->training_stage == TRAINING_STAGE_SYMBOL_ACQUISITION)
+            {
+                /* Only AGC during the initial training */
+                s->agc_scaling = (1.0f/RX_PULSESHAPER_GAIN)*5.0f*0.25f/sqrtf(power);
+            }
+            /* Pulse shape while still at the carrier frequency, using a quadrature
+               pair of filters. This results in a properly bandpass filtered complex
+               signal, which can be brought directly to baseband by complex mixing.
+               No further filtering, to remove mixer harmonics, is needed. */
+            step = -s->eq_put_step;
+            if (step > RX_PULSESHAPER_COEFF_SETS - 1)
+                step = RX_PULSESHAPER_COEFF_SETS - 1;
+#if defined(SPANDSP_USE_FIXED_POINT)
+            zi.im = (int32_t) rx_pulseshaper[step][0].im*(int32_t) s->rrc_filter[s->rrc_filter_step];
+            for (j = 1;  j < V29_RX_FILTER_STEPS;  j++)
+                zi.im += (int32_t) rx_pulseshaper[step][j].im*(int32_t) s->rrc_filter[j + s->rrc_filter_step];
+            sample.im = zi.im*s->agc_scaling;
+#else
+            zz.im = rx_pulseshaper[step][0].im*s->rrc_filter[s->rrc_filter_step];
+            for (j = 1;  j < V29_RX_FILTER_STEPS;  j++)
+                zz.im += rx_pulseshaper[step][j].im*s->rrc_filter[j + s->rrc_filter_step];
+            sample.im = zz.im*s->agc_scaling;
+#endif
+            s->eq_put_step += RX_PULSESHAPER_COEFF_SETS*10/(3*2);
+            /* Shift to baseband - since this is done in a full complex form, the
+               result is clean, and requires no further filtering, apart from the
+               equalizer. */
+            z = dds_lookup_complexf(s->carrier_phase);
+            zz.re = sample.re*z.re - sample.im*z.im;
+            zz.im = -sample.re*z.im - sample.im*z.re;
+            process_half_baud(s, &zz);
+        }
+        dds_advancef(&(s->carrier_phase), s->carrier_phase_rate);
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+void v29_rx_set_put_bit(v29_rx_state_t *s, put_bit_func_t put_bit, void *user_data)
+{
+    s->put_bit = put_bit;
+    s->put_bit_user_data = user_data;
+}
+/*- End of function --------------------------------------------------------*/
+
+void v29_rx_set_modem_status_handler(v29_rx_state_t *s, modem_tx_status_func_t handler, void *user_data)
+{
+    s->status_handler = handler;
+    s->status_user_data = user_data;
+}
+/*- End of function --------------------------------------------------------*/
+
+int v29_rx_restart(v29_rx_state_t *s, int bit_rate, int old_train)
+{
+    switch (bit_rate)
+    {
+    case 9600:
+        s->training_cd = 0;
+        break;
+    case 7200:
+        s->training_cd = 2;
+        break;
+    case 4800:
+        s->training_cd = 4;
+        break;
+    default:
+        return -1;
+    }
+    s->bit_rate = bit_rate;
+
+#if defined(SPANDSP_USE_FIXED_POINT)
+    memset(s->rrc_filter, 0, sizeof(s->rrc_filter));
+#else
+    vec_zerof(s->rrc_filter, sizeof(s->rrc_filter)/sizeof(s->rrc_filter[0]));
+#endif
+    s->rrc_filter_step = 0;
+
+    s->scramble_reg = 0;
+    s->training_scramble_reg = 0x2A;
+    s->training_stage = TRAINING_STAGE_SYMBOL_ACQUISITION;
+    s->training_count = 0;
+    s->signal_present = 0;
+#if defined(IAXMODEM_STUFF)
+    s->high_sample = 0;
+    s->low_samples = 0;
+    s->carrier_drop_pending = FALSE;
+#endif
+    s->old_train = old_train;
+
+    s->carrier_phase = 0;
+    s->carrier_track_i = 8000.0f;
+    s->carrier_track_p = 8000000.0f;
+
+    power_meter_init(&(s->power), 4);
+
+    s->constellation_state = 0;
+
+    if (s->old_train)
+    {
+        s->carrier_phase_rate = s->carrier_phase_rate_save;
+        s->agc_scaling = s->agc_scaling_save;
+        equalizer_restore(s);
+    }
+    else
+    {
+        s->carrier_phase_rate = dds_phase_ratef(CARRIER_NOMINAL_FREQ);
+        s->agc_scaling = 0.0017f/RX_PULSESHAPER_GAIN;
+        equalizer_reset(s);
+    }
+    s->eq_skip = 0;
+    s->last_sample = 0;
+
+    /* Initialise the working data for symbol timing synchronisation */
+    s->symbol_sync_low[0] = 0.0f;
+    s->symbol_sync_low[1] = 0.0f;
+    s->symbol_sync_high[0] = 0.0f;
+    s->symbol_sync_high[1] = 0.0f;
+    s->symbol_sync_dc_filter[0] = 0.0f;
+    s->symbol_sync_dc_filter[1] = 0.0f;
+    s->baud_phase = 0.0f;
+    s->baud_half = 0;
+
+    s->total_baud_timing_correction = 0;
+
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+v29_rx_state_t *v29_rx_init(v29_rx_state_t *s, int rate, put_bit_func_t put_bit, void *user_data)
+{
+    if (s == NULL)
+    {
+        if ((s = (v29_rx_state_t *) malloc(sizeof(*s))) == NULL)
+            return NULL;
+    }
+
+    memset(s, 0, sizeof(*s));
+    span_log_init(&s->logging, SPAN_LOG_NONE, NULL);
+    span_log_set_protocol(&s->logging, "V.29 RX");
+    s->put_bit = put_bit;
+    s->put_bit_user_data = user_data;
+    /* The V.29 spec says the thresholds should be -31dBm and -26dBm, but that makes little
+       sense. V.17 uses -48dBm and -43dBm, and there seems no good reason to cut off at a
+       higher level (though at 9600bps and 7200bps, TCM should put V.17 sensitivity several
+       dB ahead of V.29). */
+    /* The thresholds should be on at -26dBm0 and off at -31dBm0 */
+    v29_rx_signal_cutoff(s, -28.5f);
+
+    v29_rx_restart(s, rate, FALSE);
+    return s;
+}
+/*- End of function --------------------------------------------------------*/
+
+int v29_rx_free(v29_rx_state_t *s)
+{
+    free(s);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+void v29_rx_set_qam_report_handler(v29_rx_state_t *s, qam_report_handler_t handler, void *user_data)
+{
+    s->qam_report = handler;
+    s->qam_user_data = user_data;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/v29tx.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/v29tx.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,379 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * v29tx.c - ITU V.29 modem transmit part
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2003 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: v29tx.c,v 1.76 2008/07/16 14:23:47 steveu Exp $
+ */
+
+/*! \file */
+
+#if defined(HAVE_CONFIG_H)
+#include <config.h>
+#endif
+
+#include <stdio.h>
+#include <inttypes.h>
+#include <stdlib.h>
+#include <string.h>
+#include "floating_fudge.h"
+#if defined(HAVE_TGMATH_H)
+#include <tgmath.h>
+#endif
+#if defined(HAVE_MATH_H)
+#include <math.h>
+#endif
+
+#include "spandsp/telephony.h"
+#include "spandsp/logging.h"
+#include "spandsp/complex.h"
+#include "spandsp/vector_float.h"
+#include "spandsp/complex_vector_float.h"
+#include "spandsp/async.h"
+#include "spandsp/dds.h"
+#include "spandsp/power_meter.h"
+
+#include "spandsp/v29tx.h"
+
+#if defined(SPANDSP_USE_FIXED_POINT)
+#define SPANDSP_USE_FIXED_POINTx
+#endif
+
+#include "v29tx_constellation_maps.h"
+#if defined(SPANDSP_USE_FIXED_POINT)
+#include "v29tx_fixed_rrc.h"
+#else
+#include "v29tx_floating_rrc.h"
+#endif
+
+#define CARRIER_NOMINAL_FREQ        1700.0f
+
+/* Segments of the training sequence */
+#define V29_TRAINING_SEG_TEP        0
+#define V29_TRAINING_SEG_1          (V29_TRAINING_SEG_TEP + 480)
+#define V29_TRAINING_SEG_2          (V29_TRAINING_SEG_1 + 48)
+#define V29_TRAINING_SEG_3          (V29_TRAINING_SEG_2 + 128)
+#define V29_TRAINING_SEG_4          (V29_TRAINING_SEG_3 + 384)
+#define V29_TRAINING_END            (V29_TRAINING_SEG_4 + 48)
+#define V29_TRAINING_SHUTDOWN_END   (V29_TRAINING_END + 32)
+
+static int fake_get_bit(void *user_data)
+{
+    return 1;
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ int get_scrambled_bit(v29_tx_state_t *s)
+{
+    int bit;
+    int out_bit;
+
+    if ((bit = s->current_get_bit(s->get_bit_user_data)) == PUTBIT_END_OF_DATA)
+    {
+        /* End of real data. Switch to the fake get_bit routine, until we
+           have shut down completely. */
+        if (s->status_handler)
+            s->status_handler(s->status_user_data, MODEM_TX_STATUS_DATA_EXHAUSTED);
+        s->current_get_bit = fake_get_bit;
+        s->in_training = TRUE;
+        bit = 1;
+    }
+    out_bit = (bit ^ (s->scramble_reg >> 17) ^ (s->scramble_reg >> 22)) & 1;
+    s->scramble_reg = (s->scramble_reg << 1) | out_bit;
+    return out_bit;
+}
+/*- End of function --------------------------------------------------------*/
+
+#if defined(SPANDSP_USE_FIXED_POINT)
+static __inline__ complexi16_t getbaud(v29_tx_state_t *s)
+#else
+static __inline__ complexf_t getbaud(v29_tx_state_t *s)
+#endif
+{
+    static const int phase_steps_9600[8] =
+    {
+        1, 0, 2, 3, 6, 7, 5, 4
+    };
+    static const int phase_steps_4800[4] =
+    {
+        0, 2, 6, 4
+    };
+    int bits;
+    int amp;
+    int bit;
+
+    if (s->in_training)
+    {
+        /* Send the training sequence */
+        if (++s->training_step <= V29_TRAINING_SEG_4)
+        {
+            if (s->training_step <= V29_TRAINING_SEG_3)
+            {
+                if (s->training_step <= V29_TRAINING_SEG_1)
+                {
+                    /* Optional segment: Unmodulated carrier (talker echo protection) */
+                    return v29_9600_constellation[0];
+                }
+                if (s->training_step <= V29_TRAINING_SEG_2)
+                {
+                    /* Segment 1: silence */
+#if defined(SPANDSP_USE_FIXED_POINT)
+                    return complex_seti16(0, 0);
+#else
+                    return complex_setf(0.0f, 0.0f);
+#endif
+                }
+                /* Segment 2: ABAB... */
+                return v29_abab_constellation[(s->training_step & 1) + s->training_offset];
+            }
+            /* Segment 3: CDCD... */
+            /* Apply the 1 + x^-6 + x^-7 training scrambler */
+            bit = s->training_scramble_reg & 1;
+            s->training_scramble_reg >>= 1;
+            s->training_scramble_reg |= (((bit ^ s->training_scramble_reg) & 1) << 6);
+            return v29_cdcd_constellation[bit + s->training_offset];
+        }
+        /* We should be in the block of test ones, or shutdown ones, if we get here. */
+        /* There is no graceful shutdown procedure defined for V.29. Just
+           send some ones, to ensure we get the real data bits through, even
+           with bad ISI. */
+        if (s->training_step == V29_TRAINING_END + 1)
+        {
+            /* Switch from the fake get_bit routine, to the user supplied real
+               one, and we are up and running. */
+            s->current_get_bit = s->get_bit;
+            s->in_training = FALSE;
+        }
+        if (s->training_step == V29_TRAINING_SHUTDOWN_END)
+        {
+            if (s->status_handler)
+                s->status_handler(s->status_user_data, MODEM_TX_STATUS_SHUTDOWN_COMPLETE);
+        }
+    }
+    /* 9600bps uses the full constellation.
+       7200bps uses only the first half of the full constellation.
+       4800bps uses the smaller constellation. */
+    amp = 0;
+    /* We only use an amplitude bit at 9600bps */
+    if (s->bit_rate == 9600  &&  get_scrambled_bit(s))
+        amp = 8;
+    /*endif*/
+    bits = get_scrambled_bit(s);
+    bits = (bits << 1) | get_scrambled_bit(s);
+    if (s->bit_rate == 4800)
+    {
+        bits = phase_steps_4800[bits];
+    }
+    else
+    {
+        bits = (bits << 1) | get_scrambled_bit(s);
+        bits = phase_steps_9600[bits];
+    }
+    s->constellation_state = (s->constellation_state + bits) & 7;
+    return v29_9600_constellation[amp | s->constellation_state];
+}
+/*- End of function --------------------------------------------------------*/
+
+int v29_tx(v29_tx_state_t *s, int16_t amp[], int len)
+{
+#if defined(SPANDSP_USE_FIXED_POINT)
+    complexi_t x;
+    complexi_t z;
+#else
+    complexf_t x;
+    complexf_t z;
+#endif
+    int i;
+    int sample;
+
+    if (s->training_step >= V29_TRAINING_SHUTDOWN_END)
+    {
+        /* Once we have sent the shutdown symbols, we stop sending completely. */
+        return 0;
+    }
+    for (sample = 0;  sample < len;  sample++)
+    {
+        if ((s->baud_phase += 3) >= 10)
+        {
+            s->baud_phase -= 10;
+            s->rrc_filter[s->rrc_filter_step] =
+            s->rrc_filter[s->rrc_filter_step + V29_TX_FILTER_STEPS] = getbaud(s);
+            if (++s->rrc_filter_step >= V29_TX_FILTER_STEPS)
+                s->rrc_filter_step = 0;
+        }
+        /* Root raised cosine pulse shaping at baseband */
+#if defined(SPANDSP_USE_FIXED_POINT)
+        x = complex_seti(0, 0);
+        for (i = 0;  i < V29_TX_FILTER_STEPS;  i++)
+        {
+            x.re += (int32_t) tx_pulseshaper[TX_PULSESHAPER_COEFF_SETS - 1 - s->baud_phase][i]*(int32_t) s->rrc_filter[i + s->rrc_filter_step].re;
+            x.im += (int32_t) tx_pulseshaper[TX_PULSESHAPER_COEFF_SETS - 1 - s->baud_phase][i]*(int32_t) s->rrc_filter[i + s->rrc_filter_step].im;
+        }
+        /* Now create and modulate the carrier */
+        x.re >>= 4;
+        x.im >>= 4;
+        z = dds_complexi(&(s->carrier_phase), s->carrier_phase_rate);
+        /* Don't bother saturating. We should never clip. */
+        i = (x.re*z.re - x.im*z.im) >> 15;
+        amp[sample] = (int16_t) ((i*s->gain) >> 15);
+#else
+        x = complex_setf(0.0f, 0.0f);
+        for (i = 0;  i < V29_TX_FILTER_STEPS;  i++)
+        {
+            x.re += tx_pulseshaper[TX_PULSESHAPER_COEFF_SETS - 1 - s->baud_phase][i]*s->rrc_filter[i + s->rrc_filter_step].re;
+            x.im += tx_pulseshaper[TX_PULSESHAPER_COEFF_SETS - 1 - s->baud_phase][i]*s->rrc_filter[i + s->rrc_filter_step].im;
+        }
+        /* Now create and modulate the carrier */
+        z = dds_complexf(&(s->carrier_phase), s->carrier_phase_rate);
+        /* Don't bother saturating. We should never clip. */
+        amp[sample] = (int16_t) lrintf((x.re*z.re - x.im*z.im)*s->gain);
+#endif
+    }
+    return sample;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void set_working_gain(v29_tx_state_t *s)
+{
+#if defined(SPANDSP_USE_FIXED_POINT)
+    switch (s->bit_rate)
+    {
+    case 9600:
+        s->gain = 0.387f*s->base_gain*16.0f*32767.0f/30672.52f;
+        break;
+    case 7200:
+        s->gain = 0.605f*s->base_gain*16.0f*32767.0f/30672.52f;
+        break;
+    case 4800:
+        s->gain = 0.470f*s->base_gain*16.0f*32767.0f/30672.52f;
+        break;
+    default:
+        break;
+    }
+#else
+    switch (s->bit_rate)
+    {
+    case 9600:
+        s->gain = 0.387f*s->base_gain;
+        break;
+    case 7200:
+        s->gain = 0.605f*s->base_gain;
+        break;
+    case 4800:
+        s->gain = 0.470f*s->base_gain;
+        break;
+    default:
+        break;
+    }
+#endif
+}
+/*- End of function --------------------------------------------------------*/
+
+void v29_tx_power(v29_tx_state_t *s, float power)
+{
+    /* The constellation does not maintain constant average power as we change bit rates.
+       We need to scale the gain we get here by a bit rate specific scaling factor each
+       time we restart the modem. */
+    s->base_gain = powf(10.0f, (power - DBM0_MAX_POWER)/20.0f)*32768.0f/TX_PULSESHAPER_GAIN;
+    set_working_gain(s);
+}
+/*- End of function --------------------------------------------------------*/
+
+void v29_tx_set_get_bit(v29_tx_state_t *s, get_bit_func_t get_bit, void *user_data)
+{
+    if (s->get_bit == s->current_get_bit)
+        s->current_get_bit = get_bit;
+    s->get_bit = get_bit;
+    s->get_bit_user_data = user_data;
+}
+/*- End of function --------------------------------------------------------*/
+
+void v29_tx_set_modem_status_handler(v29_tx_state_t *s, modem_tx_status_func_t handler, void *user_data)
+{
+    s->status_handler = handler;
+    s->status_user_data = user_data;
+}
+/*- End of function --------------------------------------------------------*/
+
+int v29_tx_restart(v29_tx_state_t *s, int bit_rate, int tep)
+{
+    span_log(&s->logging, SPAN_LOG_FLOW, "Restarting V.29\n");
+    s->bit_rate = bit_rate;
+    set_working_gain(s);
+    switch (s->bit_rate)
+    {
+    case 9600:
+        s->training_offset = 0;
+        break;
+    case 7200:
+        s->training_offset = 2;
+        break;
+    case 4800:
+        s->training_offset = 4;
+        break;
+    default:
+        return -1;
+    }
+#if defined(SPANDSP_USE_FIXED_POINT)
+    memset(s->rrc_filter, 0, sizeof(s->rrc_filter));
+#else
+    cvec_zerof(s->rrc_filter, sizeof(s->rrc_filter)/sizeof(s->rrc_filter[0]));
+#endif
+    s->rrc_filter_step = 0;
+    s->scramble_reg = 0;
+    s->training_scramble_reg = 0x2A;
+    s->in_training = TRUE;
+    s->training_step = (tep)  ?  V29_TRAINING_SEG_TEP  :  V29_TRAINING_SEG_1;
+    s->carrier_phase = 0;
+    s->baud_phase = 0;
+    s->constellation_state = 0;
+    s->current_get_bit = fake_get_bit;
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+v29_tx_state_t *v29_tx_init(v29_tx_state_t *s, int bit_rate, int tep, get_bit_func_t get_bit, void *user_data)
+{
+    if (s == NULL)
+    {
+        if ((s = (v29_tx_state_t *) malloc(sizeof(*s))) == NULL)
+            return NULL;
+    }
+    memset(s, 0, sizeof(*s));
+    span_log_init(&s->logging, SPAN_LOG_NONE, NULL);
+    span_log_set_protocol(&s->logging, "V.29 TX");
+    s->get_bit = get_bit;
+    s->get_bit_user_data = user_data;
+    s->carrier_phase_rate = dds_phase_ratef(CARRIER_NOMINAL_FREQ);
+    v29_tx_power(s, -14.0f);
+    v29_tx_restart(s, bit_rate, tep);
+    return s;
+}
+/*- End of function --------------------------------------------------------*/
+
+int v29_tx_free(v29_tx_state_t *s)
+{
+    free(s);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/v29tx_constellation_maps.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/v29tx_constellation_maps.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,81 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * v29tx_constellation_maps.h - ITU V.29 modem transmit part.
+ *                              Constellation mapping.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2008 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: v29tx_constellation_maps.h,v 1.1 2008/07/10 13:34:01 steveu Exp $
+ */
+
+#if defined(SPANDSP_USE_FIXED_POINTx)
+static const complexi16_t v29_abab_constellation[6] =
+#else
+static const complexf_t v29_abab_constellation[6] =
+#endif
+{
+    { 3, -3},           /* 315deg high 9600 */
+    {-3,  0},           /* 180deg low       */
+    { 1, -1},           /* 315deg low 7200  */
+    {-3,  0},           /* 180deg low       */
+    { 0, -3},           /* 270deg low 4800  */
+    {-3,  0}            /* 180deg low       */
+};
+
+#if defined(SPANDSP_USE_FIXED_POINTx)
+static const complexi16_t v29_cdcd_constellation[6] =
+#else
+static const complexf_t v29_cdcd_constellation[6] =
+#endif
+{
+    { 3,  0},           /*   0deg low 9600  */
+    {-3,  3},           /* 135deg high      */
+    { 3,  0},           /*   0deg low 7200  */
+    {-1,  1},           /* 135deg low       */
+    { 3,  0},           /*   0deg low 4800  */
+    { 0,  3}            /*  90deg low       */
+};
+
+#if defined(SPANDSP_USE_FIXED_POINTx)
+static const complexi16_t v29_9600_constellation[16] =
+#else
+static const complexf_t v29_9600_constellation[16] =
+#endif
+{
+    { 3,  0},           /*   0deg low  */
+    { 1,  1},           /*  45deg low  */
+    { 0,  3},           /*  90deg low  */
+    {-1,  1},           /* 135deg low  */
+    {-3,  0},           /* 180deg low  */
+    {-1, -1},           /* 225deg low  */
+    { 0, -3},           /* 270deg low  */
+    { 1, -1},           /* 315deg low  */
+    { 5,  0},           /*   0deg high */
+    { 3,  3},           /*  45deg high */
+    { 0,  5},           /*  90deg high */
+    {-3,  3},           /* 135deg high */
+    {-5,  0},           /* 180deg high */
+    {-3, -3},           /* 225deg high */
+    { 0, -5},           /* 270deg high */
+    { 3, -3}            /* 315deg high */
+};
+
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/v42.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/v42.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,1457 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * v42.c
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2004 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: v42.c,v 1.42 2008/05/13 13:17:25 steveu Exp $
+ */
+
+/* THIS IS A WORK IN PROGRESS. IT IS NOT FINISHED. */
+
+/*! \file */
+
+#if defined(HAVE_CONFIG_H)
+#include <config.h>
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <inttypes.h>
+#include <string.h>
+#include <errno.h>
+
+#include "spandsp/telephony.h"
+#include "spandsp/logging.h"
+#include "spandsp/async.h"
+#include "spandsp/hdlc.h"
+#include "spandsp/schedule.h"
+#include "spandsp/queue.h"
+#include "spandsp/v42.h"
+
+#if !defined(FALSE)
+#define FALSE   0
+#endif
+#if !defined(TRUE)
+#define TRUE    (!FALSE)
+#endif
+
+#define LAPM_FRAMETYPE_MASK         0x03
+
+#define LAPM_FRAMETYPE_I            0x00
+#define LAPM_FRAMETYPE_I_ALT        0x02
+#define LAPM_FRAMETYPE_S            0x01
+#define LAPM_FRAMETYPE_U            0x03
+
+/* Timer values */
+
+#define T_WAIT_MIN                  2000
+#define T_WAIT_MAX                  10000
+/* Detection phase timer */
+#define T_400                       750000
+/* Acknowledgement timer - 1 second between SABME's */
+#define T_401                       1000000
+/* Replay delay timer (optional) */
+#define T_402                       1000000
+/* Inactivity timer (optional). No default - use 10 seconds with no packets */
+#define T_403                       10000000
+/* Max retries */
+#define N_400                       3
+/* Max octets in an information field */
+#define N_401                       128
+
+#define LAPM_DLCI_DTE_TO_DTE        0
+#define LAPM_DLCI_LAYER2_MANAGEMENT 63
+
+static void t401_expired(span_sched_state_t *s, void *user_data);
+static void t403_expired(span_sched_state_t *s, void *user_data);
+
+void lapm_reset(lapm_state_t *s);
+void lapm_restart(lapm_state_t *s);
+
+static void lapm_link_down(lapm_state_t *s);
+
+static __inline__ void lapm_init_header(uint8_t *frame, int command)
+{
+    /* Data link connection identifier (0) */
+    /* Command/response (0 if answerer, 1 if originator) */
+    /* Extended address (1) */
+    frame[0] = (LAPM_DLCI_DTE_TO_DTE << 2) | ((command)  ?  0x02  :  0x00) | 0x01;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int lapm_tx_frame(lapm_state_t *s, uint8_t *frame, int len)
+{
+    if ((s->debug & LAPM_DEBUG_LAPM_DUMP))
+        lapm_dump(s, frame, len, s->debug & LAPM_DEBUG_LAPM_RAW, TRUE);
+    /*endif*/
+    hdlc_tx_frame(&s->hdlc_tx, frame, len);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void t400_expired(span_sched_state_t *ss, void *user_data)
+{
+    v42_state_t *s;
+    
+    /* Give up trying to detect a V.42 capable peer. */
+    s = (v42_state_t *) user_data;
+    s->t400_timer = -1;
+    s->lapm.state = LAPM_UNSUPPORTED;
+    if (s->lapm.status_callback)
+        s->lapm.status_callback(s->lapm.status_callback_user_data, s->lapm.state);
+    /*endif*/
+}
+/*- End of function --------------------------------------------------------*/
+
+static void lapm_send_ua(lapm_state_t *s, int pfbit)
+{
+    uint8_t frame[3];
+
+    lapm_init_header(frame, !s->we_are_originator);
+    frame[1] = (uint8_t) (0x63 | (pfbit << 4));
+    frame[2] = 0;
+    span_log(&s->logging, SPAN_LOG_FLOW, "Sending unnumbered acknowledgement\n");
+    lapm_tx_frame(s, frame, 3);
+}
+/*- End of function --------------------------------------------------------*/
+
+static void lapm_send_sabme(span_sched_state_t *ss, void *user_data)
+{
+    lapm_state_t *s;
+    uint8_t frame[3];
+
+    s = (lapm_state_t *) user_data;
+    if (s->t401_timer >= 0)
+    {
+fprintf(stderr, "Deleting T401 q [%p] %d\n", (void *) s, s->t401_timer);
+        span_schedule_del(&s->sched, s->t401_timer);
+        s->t401_timer = -1;
+    }
+    /*endif*/
+    if (++s->retransmissions > N_400)
+    {
+        /* 8.3.2.2 Too many retries */
+        s->state = LAPM_RELEASE;
+        if (s->status_callback)
+            s->status_callback(s->status_callback_user_data, s->state);
+        /*endif*/
+        return;
+    }
+    /*endif*/
+fprintf(stderr, "Setting T401 a1 [%p]\n", (void *) s);
+    s->t401_timer = span_schedule_event(&s->sched, T_401, lapm_send_sabme, s);
+    lapm_init_header(frame, s->we_are_originator);
+    frame[1] = 0x7F;
+    frame[2] = 0;
+    span_log(&s->logging, SPAN_LOG_FLOW, "Sending SABME (set asynchronous balanced mode extended)\n");
+    lapm_tx_frame(s, frame, 3);
+}
+/*- End of function --------------------------------------------------------*/
+
+static int lapm_ack_packet(lapm_state_t *s, int num)
+{
+    lapm_frame_queue_t *f;
+    lapm_frame_queue_t *prev;
+
+    for (prev = NULL, f = s->txqueue;  f;  prev = f, f = f->next)
+    {
+        if ((f->frame[1] >> 1) == num)
+        {
+            /* Cancel each packet, as necessary */
+            if (prev)
+                prev->next = f->next;
+            else
+                s->txqueue = f->next;
+            /*endif*/
+            span_log(&s->logging,
+                     SPAN_LOG_FLOW,
+                     "-- ACKing packet %d. New txqueue is %d (-1 means empty)\n",
+                     (f->frame[1] >> 1),
+                     (s->txqueue)  ?  (s->txqueue->frame[1] >> 1)  :  -1);
+            s->last_frame_peer_acknowledged = num;
+            free(f);
+            /* Reset retransmission count if we actually acked something */
+            s->retransmissions = 0;
+            return 1;
+        }
+        /*endif*/
+    }
+    /*endfor*/
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void lapm_ack_rx(lapm_state_t *s, int ack)
+{
+    int i;
+    int cnt;
+
+    /* This might not be acking anything new */
+    if (s->last_frame_peer_acknowledged == ack)
+        return;
+    /*endif*/
+    /* It should be acking something that is actually outstanding */
+    if ((s->last_frame_peer_acknowledged < s->next_tx_frame  &&  (ack < s->last_frame_peer_acknowledged  ||  ack > s->next_tx_frame))
+        ||
+        (s->last_frame_peer_acknowledged > s->next_tx_frame  &&  (ack > s->last_frame_peer_acknowledged  ||  ack < s->next_tx_frame)))
+    {
+        /* ACK was outside our window --- ignore */
+        span_log(&s->logging, SPAN_LOG_FLOW, "ACK received outside window, ignoring\n");
+        return;
+    }
+    /*endif*/
+    
+    /* Cancel each packet, as necessary */
+    span_log(&s->logging,
+             SPAN_LOG_FLOW,
+             "-- ACKing all packets from %d to (but not including) %d\n",
+             s->last_frame_peer_acknowledged,
+             ack);
+    for (cnt = 0, i = s->last_frame_peer_acknowledged;  i != ack;  i = (i + 1) & 0x7F) 
+        cnt += lapm_ack_packet(s, i);
+    /*endfor*/
+    s->last_frame_peer_acknowledged = ack;
+    if (s->txqueue == NULL)
+    {
+        span_log(&s->logging, SPAN_LOG_FLOW, "-- Since there was nothing left, stopping timer T_401\n");
+        /* Something was ACK'd.  Stop timer T_401. */
+fprintf(stderr, "T401 a2 is %d [%p]\n", s->t401_timer, (void *) s);
+        if (s->t401_timer >= 0)
+        {
+fprintf(stderr, "Deleting T401 a3 [%p] %d\n", (void *) s, s->t401_timer);
+            span_schedule_del(&s->sched, s->t401_timer);
+            s->t401_timer = -1;
+        }
+        /*endif*/
+    }
+    /*endif*/
+    if (s->t403_timer >= 0)
+    {
+        span_log(&s->logging, SPAN_LOG_FLOW, "-- Stopping timer T_403, since we got an ACK\n");
+        if (s->t403_timer >= 0)
+        {
+fprintf(stderr, "Deleting T403 b %d\n", s->t403_timer);
+            span_schedule_del(&s->sched, s->t403_timer);
+            s->t403_timer = -1;
+        }
+        /*endif*/
+    }
+    /*endif*/
+    if (s->txqueue)
+    {
+        /* Something left to transmit. Start timer T_401 again if it is stopped */
+        span_log(&s->logging,
+                 SPAN_LOG_FLOW,
+                 "-- Something left to transmit (%d). Restarting timer T_401\n",
+                 s->txqueue->frame[1] >> 1);
+        if (s->t401_timer < 0)
+        {
+fprintf(stderr, "Setting T401 b [%p]\n", (void *) s);
+            s->t401_timer = span_schedule_event(&s->sched, T_401, t401_expired, s);
+        }
+        /*endif*/
+    }
+    else
+    {
+        span_log(&s->logging, SPAN_LOG_FLOW, "-- Nothing left, starting timer T_403\n");
+        /* Nothing to transmit. Start timer T_403. */
+fprintf(stderr, "Setting T403 c\n");
+        s->t403_timer = span_schedule_event(&s->sched, T_403, t403_expired, s);
+    }
+    /*endif*/
+}
+/*- End of function --------------------------------------------------------*/
+
+static void lapm_reject(lapm_state_t *s)
+{
+    uint8_t frame[4];
+
+    lapm_init_header(frame, !s->we_are_originator);
+    frame[1] = (uint8_t) (0x00 | 0x08 | LAPM_FRAMETYPE_S);
+    /* Where to start retransmission */
+    frame[2] = (uint8_t) ((s->next_expected_frame << 1) | 0x01);
+    span_log(&s->logging, SPAN_LOG_FLOW, "Sending REJ (reject (%d)\n", s->next_expected_frame);
+    lapm_tx_frame(s, frame, 4);
+}
+/*- End of function --------------------------------------------------------*/
+
+static void lapm_rr(lapm_state_t *s, int pfbit)
+{
+    uint8_t frame[4];
+
+    lapm_init_header(frame, !s->we_are_originator);
+    frame[1] = (uint8_t) (0x00 | 0x00 | LAPM_FRAMETYPE_S);
+    frame[2] = (uint8_t) ((s->next_expected_frame << 1) | pfbit);
+    /* Note that we have already ACKed this */
+    s->last_frame_we_acknowledged = s->next_expected_frame;
+    span_log(&s->logging, SPAN_LOG_FLOW, "Sending RR (receiver ready) (%d)\n", s->next_expected_frame);
+    lapm_tx_frame(s, frame, 4);
+}
+/*- End of function --------------------------------------------------------*/
+
+static void t401_expired(span_sched_state_t *ss, void *user_data)
+{
+    lapm_state_t *s;
+    
+    s = (lapm_state_t *) user_data;
+fprintf(stderr, "Expiring T401 a4 [%p]\n", (void *) s);
+    s->t401_timer = -1;
+    if (s->txqueue)
+    {
+        /* Retransmit first packet in the queue, setting the poll bit */
+        span_log(&s->logging, SPAN_LOG_FLOW, "-- Timer T_401 expired, What to do...\n");
+        /* Update N(R), and set the poll bit */
+        s->txqueue->frame[2] = (uint8_t)((s->next_expected_frame << 1) | 0x01);
+        s->last_frame_we_acknowledged = s->next_expected_frame;
+        s->solicit_f_bit = TRUE;
+        if (++s->retransmissions <= N_400)
+        {
+            /* Reschedule timer T401 */
+            span_log(&s->logging, SPAN_LOG_FLOW, "-- Retransmitting %d bytes\n", s->txqueue->len);
+            lapm_tx_frame(s, s->txqueue->frame, s->txqueue->len);
+            span_log(&s->logging, SPAN_LOG_FLOW, "-- Scheduling retransmission (%d)\n", s->retransmissions);
+fprintf(stderr, "Setting T401 d [%p]\n", (void *) s);
+            s->t401_timer = span_schedule_event(&s->sched, T_401, t401_expired, s);
+        }
+        else
+        {
+            span_log(&s->logging, SPAN_LOG_FLOW, "-- Timeout occured\n");
+            s->state = LAPM_RELEASE;
+            if (s->status_callback)
+                s->status_callback(s->status_callback_user_data, s->state);
+            lapm_link_down(s);
+            lapm_restart(s);
+        }
+        /*endif*/
+    }
+    else
+    {
+        span_log(&s->logging, SPAN_LOG_FLOW, "Timer T_401 expired. Nothing to send...\n");
+    }
+    /*endif*/
+}
+/*- End of function --------------------------------------------------------*/
+
+const char *lapm_status_to_str(int status)
+{
+    switch (status)
+    {
+    case LAPM_DETECT:
+        return "LAPM_DETECT";
+    case LAPM_ESTABLISH:
+        return "LAPM_ESTABLISH";
+    case LAPM_DATA:
+        return "LAPM_DATA";
+    case LAPM_RELEASE:
+        return "LAPM_RELEASE";
+    case LAPM_SIGNAL:
+        return "LAPM_SIGNAL";
+    case LAPM_SETPARM:
+        return "LAPM_SETPARM";
+    case LAPM_TEST:
+        return "LAPM_TEST";
+    case LAPM_UNSUPPORTED:
+        return "LAPM_UNSUPPORTED";
+    }
+    /*endswitch*/
+    return "???";
+}
+/*- End of function --------------------------------------------------------*/
+
+int lapm_tx(lapm_state_t *s, const void *buf, int len)
+{
+    return queue_write(s->tx_queue, buf, len);
+}
+/*- End of function --------------------------------------------------------*/
+
+int lapm_release(lapm_state_t *s)
+{
+    s->state = LAPM_RELEASE;
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int lapm_loopback(lapm_state_t *s, int enable)
+{
+    s->state = LAPM_TEST;
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int lapm_break(lapm_state_t *s, int enable)
+{
+    s->state = LAPM_SIGNAL;
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int lapm_tx_iframe(lapm_state_t *s, const void *buf, int len, int cr)
+{
+    lapm_frame_queue_t *f;
+
+    if ((f = malloc(sizeof(*f) + len + 4)) == NULL)
+    {
+        span_log(&s->logging, SPAN_LOG_FLOW, "Out of memory\n");
+        return -1;
+    }
+    /*endif*/
+
+    lapm_init_header(f->frame, (s->peer_is_originator)  ?  cr  :  !cr);
+    f->next = NULL;
+    f->len = len + 4;
+    f->frame[1] = (uint8_t) (s->next_tx_frame << 1);
+    f->frame[2] = (uint8_t) (s->next_expected_frame << 1);
+    memcpy(f->frame + 3, buf, len);
+    s->next_tx_frame = (s->next_tx_frame + 1) & 0x7F;
+    s->last_frame_we_acknowledged = s->next_expected_frame;
+    /* Clear poll bit */
+    f->frame[2] &= ~0x01;
+    if (s->tx_last)
+        s->tx_last->next = f;
+    else
+        s->txqueue = f;
+    /*endif*/
+    s->tx_last = f;
+    /* Immediately transmit unless we're in a recovery state */
+    if (s->retransmissions == 0)
+        lapm_tx_frame(s, f->frame, f->len);
+    /*endif*/
+    if (s->t403_timer >= 0)
+    {
+        span_log(&s->logging, SPAN_LOG_FLOW, "Stopping T_403 timer\n");
+fprintf(stderr, "Deleting T403 c %d\n", s->t403_timer);
+        span_schedule_del(&s->sched, s->t403_timer);
+        s->t403_timer = -1;
+    }
+    /*endif*/
+    if (s->t401_timer < 0)
+    {
+        span_log(&s->logging, SPAN_LOG_FLOW, "Starting timer T_401\n");
+        s->t401_timer = span_schedule_event(&s->sched, T_401, t401_expired, s);
+fprintf(stderr, "Setting T401 e %d [%p]\n", s->t401_timer, (void *) s);
+    }
+    else
+    {
+        span_log(&s->logging, SPAN_LOG_FLOW, "Timer T_401 already running (%d)\n", s->t401_timer);
+    }
+    /*endif*/
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void t403_expired(span_sched_state_t *ss, void *user_data)
+{
+    lapm_state_t *s;
+
+    s = (lapm_state_t *) user_data;
+    span_log(&s->logging, SPAN_LOG_FLOW, "Timer T_403 expired. Sending RR and scheduling T_403 again\n");
+    s->t403_timer = -1;
+    s->retransmissions = 0;
+    /* Solicit an F-bit in the other end's RR */
+    s->solicit_f_bit = TRUE;
+    lapm_rr(s, 1);
+    /* Restart ourselves */
+fprintf(stderr, "Setting T403 f\n");
+    s->t401_timer = span_schedule_event(&s->sched, T_401, t401_expired, s);
+}
+/*- End of function --------------------------------------------------------*/
+
+void lapm_dump(lapm_state_t *s, const uint8_t *frame, int len, int showraw, int txrx)
+{
+    const char *type;
+    char direction_tag[2];
+    
+    direction_tag[0] = txrx  ?  '>'  :  '<';
+    direction_tag[1] = '\0';
+    if (showraw)
+        span_log_buf(&s->logging, SPAN_LOG_FLOW, direction_tag, frame, len);
+    /*endif*/
+
+    switch ((frame[1] & LAPM_FRAMETYPE_MASK))
+    {
+    case LAPM_FRAMETYPE_I:
+    case LAPM_FRAMETYPE_I_ALT:
+        span_log(&s->logging, SPAN_LOG_FLOW, "%c Information frame:\n", direction_tag[0]);
+        break;
+    case LAPM_FRAMETYPE_S:
+        span_log(&s->logging, SPAN_LOG_FLOW, "%c Supervisory frame:\n", direction_tag[0]);
+        break;
+    case LAPM_FRAMETYPE_U:
+        span_log(&s->logging, SPAN_LOG_FLOW, "%c Unnumbered frame:\n", direction_tag[0]);
+        break;
+    }
+    /*endswitch*/
+    
+    span_log(&s->logging,
+             SPAN_LOG_FLOW,
+             "%c DLCI: %2d  C/R: %d  EA: %d\n",
+             direction_tag[0],
+             (frame[0] >> 2),
+             (frame[0] & 0x02)  ?  1  :  0,
+             (frame[0] & 0x01),
+             direction_tag[0]);
+    switch ((frame[1] & LAPM_FRAMETYPE_MASK))
+    {
+    case LAPM_FRAMETYPE_I:
+    case LAPM_FRAMETYPE_I_ALT:
+        /* Information frame */
+        span_log(&s->logging,
+                 SPAN_LOG_FLOW, 
+                 "%c N(S): %03d\n",
+                 direction_tag[0],
+                 (frame[1] >> 1));
+        span_log(&s->logging,
+                 SPAN_LOG_FLOW, 
+                 "%c N(R): %03d   P: %d\n",
+                 direction_tag[0],
+                 (frame[2] >> 1),
+                 (frame[2] & 0x01));
+        span_log(&s->logging,
+                 SPAN_LOG_FLOW, 
+                 "%c %d bytes of data\n",
+                 direction_tag[0],
+                 len - 4);
+        break;
+    case LAPM_FRAMETYPE_S:
+        /* Supervisory frame */
+        switch (frame[1] & 0x0C)
+        {
+        case 0x00:
+            type = "RR (receive ready)";
+            break;
+        case 0x04:
+            type = "RNR (receive not ready)";
+            break;
+        case 0x08:
+            type = "REJ (reject)";
+            break;
+        case 0x0C:
+            type = "SREJ (selective reject)";
+            break;
+        default:
+            type = "???";
+            break;
+        }
+        /*endswitch*/
+        span_log(&s->logging,
+                 SPAN_LOG_FLOW,
+                 "%c S: %03d [ %s ]\n",
+                 direction_tag[0],
+                 frame[1],
+                 type);
+        span_log(&s->logging,
+                 SPAN_LOG_FLOW,
+                 "%c N(R): %03d P/F: %d\n",
+                 direction_tag[0],
+                 frame[2] >> 1,
+                 frame[2] & 0x01);
+        span_log(&s->logging,
+                 SPAN_LOG_FLOW,
+                 "%c %d bytes of data\n",
+                 direction_tag[0],
+                 len - 4);
+        break;
+    case LAPM_FRAMETYPE_U:
+        /* Unnumbered frame */
+        switch (frame[1] & 0xEC)
+        {
+        case 0x00:
+            type = "UI (unnumbered information)";
+            break;
+        case 0x0C:
+            type = "DM (disconnect mode)";
+            break;
+        case 0x40:
+            type = "DISC (disconnect)";
+            break;
+        case 0x60:
+            type = "UA (unnumbered acknowledgement)";
+            break;
+        case 0x6C:
+            type = "SABME (set asynchronous balanced mode extended)";
+            break;
+        case 0x84:
+            type = "FRMR (frame reject)";
+            break;
+        case 0xAC:
+            type = "XID (exchange identification)";
+            break;
+        case 0xE0:
+            type = "TEST (test)";
+            break;
+        default:
+            type = "???";
+            break;
+        }
+        /*endswitch*/
+        span_log(&s->logging,
+                 SPAN_LOG_FLOW,
+                 "%c   M: %03d [ %s ] P/F: %d\n",
+                 direction_tag[0],
+                 frame[1],
+                 type,
+                 (frame[1] >> 4) & 1);
+        span_log(&s->logging,
+                 SPAN_LOG_FLOW,
+                 "%c %d bytes of data\n",
+                 direction_tag[0],
+                 len - 3);
+        break;
+    }
+    /*endswitch*/
+}
+/*- End of function --------------------------------------------------------*/
+
+static void lapm_link_up(lapm_state_t *s)
+{
+    uint8_t buf[1024];
+    int len;
+
+    lapm_reset(s);
+    /* Go into connection established state */
+    s->state = LAPM_DATA;
+    if (s->status_callback)
+        s->status_callback(s->status_callback_user_data, s->state);
+    /*endif*/
+    if (!queue_empty(s->tx_queue))
+    {
+        if ((len = queue_read(s->tx_queue, buf, s->n401)) > 0)
+            lapm_tx_iframe(s, buf, len, TRUE);
+        /*endif*/
+    }
+    /*endif*/
+    if (s->t401_timer >= 0)
+    {
+fprintf(stderr, "Deleting T401 x [%p] %d\n", (void *) s, s->t401_timer);
+        span_schedule_del(&s->sched, s->t401_timer);
+        s->t401_timer = -1;
+    }
+    /*endif*/
+    /* Start the T403 timer */
+fprintf(stderr, "Setting T403 g\n");
+    s->t403_timer = span_schedule_event(&s->sched, T_403, t403_expired, s);
+}
+/*- End of function --------------------------------------------------------*/
+
+static void lapm_link_down(lapm_state_t *s)
+{
+    lapm_reset(s);
+
+    if (s->status_callback)
+        s->status_callback(s->status_callback_user_data, s->state);
+    /*endif*/
+}
+/*- End of function --------------------------------------------------------*/
+
+void lapm_reset(lapm_state_t *s)
+{
+    lapm_frame_queue_t *f;
+    lapm_frame_queue_t *p;
+
+    /* Having received a SABME, we need to reset our entire state */
+    s->next_tx_frame = 0;
+    s->last_frame_peer_acknowledged = 0;
+    s->next_expected_frame = 0;
+    s->last_frame_we_acknowledged = 0;
+    s->window_size_k = 15;
+    s->n401 = 128;
+    if (s->t401_timer >= 0)
+    {
+fprintf(stderr, "Deleting T401 d [%p] %d\n", (void *) s, s->t401_timer);
+        span_schedule_del(&s->sched, s->t401_timer);
+        s->t401_timer = -1;
+    }
+    /*endif*/
+    if (s->t403_timer >= 0)
+    {
+fprintf(stderr, "Deleting T403 e %d\n", s->t403_timer);
+        span_schedule_del(&s->sched, s->t403_timer);
+        s->t403_timer = -1;
+    }
+    /*endif*/
+    s->busy = FALSE;
+    s->solicit_f_bit = FALSE;
+    s->state = LAPM_RELEASE;
+    s->retransmissions = 0;
+    /* Discard anything waiting to go out */
+    for (f = s->txqueue;  f;  )
+    {
+        p = f;
+        f = f->next;
+        free(p);
+    }
+    /*endfor*/
+    s->txqueue = NULL;
+}
+/*- End of function --------------------------------------------------------*/
+
+void lapm_receive(void *user_data, const uint8_t *frame, int len, int ok)
+{
+    lapm_state_t *s;
+    lapm_frame_queue_t *f;
+    int sendnow;
+    int octet;
+    int s_field;
+    int m_field;
+
+fprintf(stderr, "LAPM receive %d %d\n", ok, len);
+    if (!ok  ||  len == 0)
+        return;
+    /*endif*/
+    s = (lapm_state_t *) user_data;
+    if (len < 0)
+    {
+        /* Special conditions */
+        switch (len)
+        {
+        case PUTBIT_TRAINING_FAILED:
+            span_log(&s->logging, SPAN_LOG_DEBUG, "Training failed\n");
+            break;
+        case PUTBIT_TRAINING_SUCCEEDED:
+            span_log(&s->logging, SPAN_LOG_DEBUG, "Training succeeded\n");
+            break;
+        case PUTBIT_CARRIER_UP:
+            span_log(&s->logging, SPAN_LOG_DEBUG, "Carrier up\n");
+            break;
+        case PUTBIT_CARRIER_DOWN:
+            span_log(&s->logging, SPAN_LOG_DEBUG, "Carrier down\n");
+            break;
+        case PUTBIT_FRAMING_OK:
+            span_log(&s->logging, SPAN_LOG_DEBUG, "Framing OK\n");
+            break;
+        case PUTBIT_ABORT:
+            span_log(&s->logging, SPAN_LOG_DEBUG, "Abort\n");
+            break;
+        default:
+            span_log(&s->logging, SPAN_LOG_DEBUG, "Eh!\n");
+            break;
+        }
+        /*endswitch*/
+        return;
+    }
+    /*endif*/
+
+    if ((s->debug & LAPM_DEBUG_LAPM_DUMP))
+        lapm_dump(s, frame, len, s->debug & LAPM_DEBUG_LAPM_RAW, FALSE);
+    /*endif*/
+    octet = 0;
+    /* We do not expect extended addresses */
+    if ((frame[octet] & 0x01) == 0)
+        return;
+    /*endif*/
+    /* Check for DLCIs we do not recognise */
+    if ((frame[octet] >> 2) != LAPM_DLCI_DTE_TO_DTE)
+        return;
+    /*endif*/
+    octet++;
+    switch (frame[octet] & LAPM_FRAMETYPE_MASK)
+    {
+    case LAPM_FRAMETYPE_I:
+    case LAPM_FRAMETYPE_I_ALT:
+        if (s->state != LAPM_DATA)
+        {
+            span_log(&s->logging, SPAN_LOG_FLOW, "!! Got an I-frame while link state is %d\n", s->state);
+            break;
+        }
+        /*endif*/
+        /* Information frame */
+        if (len < 4)
+        {
+            span_log(&s->logging, SPAN_LOG_FLOW, "!! Received short I-frame (expected 4, got %d)\n", len);
+            break;
+        }
+        /*endif*/
+        /* Make sure this is a valid packet */
+        if ((frame[1] >> 1) == s->next_expected_frame)
+        {
+            /* Increment next expected I-frame */
+            s->next_expected_frame = (s->next_expected_frame + 1) & 0x7F;
+            /* Handle their ACK */
+            lapm_ack_rx(s, frame[2] >> 1);
+            if ((frame[2] & 0x01))
+            {
+                /* If the Poll/Final bit is set, send the RR immediately */
+                lapm_rr(s, 1);
+            }
+            /*endif*/
+            s->iframe_receive(s->iframe_receive_user_data, frame + 3, len - 4);
+            /* Send an RR if one wasn't sent already */
+            if (s->last_frame_we_acknowledged != s->next_expected_frame) 
+                lapm_rr(s, 0);
+            /*endif*/
+        }
+        else
+        {
+            if (((s->next_expected_frame - (frame[1] >> 1)) & 127) < s->window_size_k)
+            {
+                /* It's within our window -- send back an RR */
+                lapm_rr(s, 0);
+            }
+            else
+            {
+                lapm_reject(s);
+            }
+            /*endif*/
+        }
+        /*endif*/
+        break;
+    case LAPM_FRAMETYPE_S:
+        if (s->state != LAPM_DATA)
+        {
+            span_log(&s->logging, SPAN_LOG_FLOW, "!! Got S-frame while link down\n");
+            break;
+        }
+        /*endif*/
+        if (len < 4)
+        {
+            span_log(&s->logging, SPAN_LOG_FLOW, "!! Received short S-frame (expected 4, got %d)\n", len);
+            break;
+        }
+        /*endif*/
+        s_field = frame[octet] & 0xEC;
+        switch (s_field)
+        {
+        case 0x00:
+            /* RR (receive ready) */
+            s->busy = FALSE;
+            /* Acknowledge frames as necessary */
+            lapm_ack_rx(s, frame[2] >> 1);
+            if ((frame[2] & 0x01))
+            {
+                /* If P/F is one, respond with an RR with the P/F bit set */
+                if (s->solicit_f_bit)
+                {
+                    span_log(&s->logging, SPAN_LOG_FLOW, "-- Got RR response to our frame\n");
+                }
+                else
+                {
+                    span_log(&s->logging, SPAN_LOG_FLOW, "-- Unsolicited RR with P/F bit, responding\n");
+                    lapm_rr(s, 1);
+                }
+                /*endif*/
+                s->solicit_f_bit = FALSE;
+            }
+            /*endif*/
+            break;
+        case 0x04:
+            /* RNR (receive not ready) */
+            span_log(&s->logging, SPAN_LOG_FLOW, "-- Got receiver not ready\n");
+            s->busy = TRUE;
+            break;   
+        case 0x08:
+            /* REJ (reject) */
+            /* Just retransmit */
+            span_log(&s->logging, SPAN_LOG_FLOW, "-- Got reject requesting packet %d...  Retransmitting.\n", frame[2] >> 1);
+            if ((frame[2] & 0x01))
+            {
+                /* If it has the poll bit set, send an appropriate supervisory response */
+                lapm_rr(s, 1);
+            }
+            /*endif*/
+            sendnow = FALSE;
+            /* Resend the appropriate I-frame */
+            for (f = s->txqueue;  f;  f = f->next)
+            {
+                if (sendnow  ||  (f->frame[1] >> 1) == (frame[2] >> 1))
+                {
+                    /* Matches the request, or follows in our window */
+                    sendnow = TRUE;
+                    span_log(&s->logging,
+                             SPAN_LOG_FLOW,
+                             "!! Got reject for frame %d, retransmitting frame %d now, updating n_r!\n",
+                             frame[2] >> 1,
+                             f->frame[1] >> 1);
+                    f->frame[2] = (uint8_t) (s->next_expected_frame << 1);
+                    lapm_tx_frame(s, f->frame, f->len);
+                }
+                /*endif*/
+            }
+            /*endfor*/
+            if (!sendnow)
+            {
+                if (s->txqueue)
+                {
+                    /* This should never happen */
+                    if ((frame[2] & 0x01) == 0  ||  (frame[2] >> 1))
+                    {
+                        span_log(&s->logging,
+                                 SPAN_LOG_FLOW,
+                                 "!! Got reject for frame %d, but we only have others!\n",
+                                 frame[2] >> 1);
+                    }
+                    /*endif*/
+                }
+                else
+                {
+                    /* Hrm, we have nothing to send, but have been REJ'd.  Reset last_frame_peer_acknowledged, next_tx_frame, etc */
+                    span_log(&s->logging, SPAN_LOG_FLOW, "!! Got reject for frame %d, but we have nothing -- resetting!\n", frame[2] >> 1);
+                    s->last_frame_peer_acknowledged =
+                    s->next_tx_frame = frame[2] >> 1;
+                    /* Reset t401 timer if it was somehow going */
+                    if (s->t401_timer >= 0)
+                    {
+fprintf(stderr, "Deleting T401 f [%p] %d\n", (void *) s, s->t401_timer);
+                        span_schedule_del(&s->sched, s->t401_timer);
+                        s->t401_timer = -1;
+                    }
+                    /*endif*/
+                    /* Reset and restart t403 timer */
+                    if (s->t403_timer >= 0)
+                    {
+fprintf(stderr, "Deleting T403 g %d\n", s->t403_timer);
+                        span_schedule_del(&s->sched, s->t403_timer);
+                        s->t403_timer = -1;
+                    }
+                    /*endif*/
+fprintf(stderr, "Setting T403 h\n");
+                    s->t403_timer = span_schedule_event(&s->sched, T_403, t403_expired, s);
+                }
+                /*endif*/
+            }
+            /*endif*/
+            break;
+        case 0x0C:
+            /* SREJ (selective reject) */
+            break;
+        default:
+            span_log(&s->logging,
+                     SPAN_LOG_FLOW,
+                     "!! XXX Unknown Supervisory frame sd=0x%02x,pf=%02xnr=%02x vs=%02x, va=%02x XXX\n",
+                     s_field,
+                     frame[2] & 0x01,
+                     frame[2] >> 1,
+                     s->next_tx_frame,
+                     s->last_frame_peer_acknowledged);
+            break;
+        }
+        /*endswitch*/
+        break;
+    case LAPM_FRAMETYPE_U:
+        if (len < 3)
+        {
+            span_log(&s->logging, SPAN_LOG_FLOW, "!! Received too short unnumbered frame\n");
+            break;
+        }
+        /*endif*/
+        m_field = frame[octet] & 0xEC;
+        switch (m_field)
+        {
+        case 0x00:
+            /* UI (unnumbered information) */
+            switch (frame[++octet] & 0x7F)
+            {
+            case 0x40:
+                /* BRK */
+                span_log(&s->logging, SPAN_LOG_FLOW, "BRK - option %d, length %d\n", (frame[octet] >> 5), frame[octet + 1]);
+                octet += 2;
+                break;
+            case 0x60:
+                /* BRKACK */
+                span_log(&s->logging, SPAN_LOG_FLOW, "BRKACK\n");
+                break;
+            default:
+                /* Unknown */
+                span_log(&s->logging, SPAN_LOG_FLOW, "Unknown UI type\n");
+                break;
+            }
+            /*endswitch*/
+            break;
+        case 0x0C:
+            /* DM (disconnect mode) */
+            if ((frame[octet] & 0x10))
+            {
+                span_log(&s->logging, SPAN_LOG_FLOW, "-- Got Unconnected Mode from peer.\n");
+                /* Disconnected mode, try again */
+                lapm_link_down(s);
+                lapm_restart(s);
+            }
+            else
+            {
+                span_log(&s->logging, SPAN_LOG_FLOW, "-- DM (disconnect mode) requesting SABME, starting.\n");
+                /* Requesting that we start */
+                lapm_restart(s);
+            }
+            /*endif*/
+            break;
+        case 0x40:
+            /* DISC (disconnect) */
+            span_log(&s->logging, SPAN_LOG_FLOW, "-- Got DISC (disconnect) from peer.\n");
+            /* Acknowledge */
+            lapm_send_ua(s, (frame[octet] & 0x10));
+            lapm_link_down(s);
+            break;
+        case 0x60:
+            /* UA (unnumbered acknowledgement) */
+            if (s->state == LAPM_ESTABLISH)
+            {
+                span_log(&s->logging, SPAN_LOG_FLOW, "-- Got UA (unnumbered acknowledgement) from %s peer. Link up.\n", (frame[0] & 0x02)  ?  "xxx"  :  "yyy");
+                lapm_link_up(s);
+            }
+            else
+            {
+                span_log(&s->logging, SPAN_LOG_FLOW, "!! Got a UA (unnumbered acknowledgement) in state %d\n", s->state);
+            }
+            /*endif*/
+            break;
+        case 0x6C:
+            /* SABME (set asynchronous balanced mode extended) */
+            span_log(&s->logging, SPAN_LOG_FLOW, "-- Got SABME (set asynchronous balanced mode extended) from %s peer.\n", (frame[0] & 0x02)  ?  "yyy"  :  "xxx");
+            if ((frame[0] & 0x02))
+            {
+                s->peer_is_originator = TRUE;
+                if (s->we_are_originator)
+                {
+                    /* We can't both be originators */
+                    span_log(&s->logging, SPAN_LOG_FLOW, "We think we are the originator, but they think so too.");
+                    break;
+                }
+                /*endif*/
+            }
+            else
+            {
+                s->peer_is_originator = FALSE;
+                if (!s->we_are_originator)
+                {
+                    /* We can't both be answerers */
+                    span_log(&s->logging, SPAN_LOG_FLOW, "We think we are the answerer, but they think so too.\n");
+                    break;
+                }
+                /*endif*/
+            }
+            /*endif*/
+            /* Send unnumbered acknowledgement */
+            lapm_send_ua(s, (frame[octet] & 0x10));
+            lapm_link_up(s);
+            break;
+        case 0x84:
+            /* FRMR (frame reject) */
+            span_log(&s->logging, SPAN_LOG_FLOW, "!! FRMR (frame reject).\n");
+            break;
+        case 0xAC:
+            /* XID (exchange identification) */
+            span_log(&s->logging, SPAN_LOG_FLOW, "!! XID (exchange identification) frames not supported\n");
+            break;
+        case 0xE0:
+            /* TEST (test) */
+            span_log(&s->logging, SPAN_LOG_FLOW, "!! TEST (test) frames not supported\n");
+            break;
+        default:
+            span_log(&s->logging, SPAN_LOG_FLOW, "!! Don't know what to do with M=%X u-frames\n", m_field);
+            break;
+        }
+        /*endswitch*/
+        break;
+    }
+    /*endswitch*/
+}
+/*- End of function --------------------------------------------------------*/
+
+static void lapm_hdlc_underflow(void *user_data)
+{
+    lapm_state_t *s;
+    uint8_t buf[1024];
+    int len;
+
+    s = (lapm_state_t *) user_data;
+    span_log(&s->logging, SPAN_LOG_FLOW, "HDLC underflow\n");
+    if (s->state == LAPM_DATA)
+    {
+        if (!queue_empty(s->tx_queue))
+        {
+            if ((len = queue_read(s->tx_queue, buf, s->n401)) > 0)
+                lapm_tx_iframe(s, buf, len, TRUE);
+            /*endif*/
+        }
+        /*endif*/
+    }
+    /*endif*/
+}
+/*- End of function --------------------------------------------------------*/
+
+void lapm_restart(lapm_state_t *s)
+{
+#if 0
+    if (s->state != LAPM_RELEASE)
+    {
+        span_log(&s->logging, SPAN_LOG_FLOW, "!! lapm_restart: Not in 'Link Connection Released' state\n");
+        return;
+    }
+    /*endif*/
+#endif
+    span_log_init(&s->logging, SPAN_LOG_NONE, NULL);
+    span_log_set_protocol(&s->logging, "LAP.M");
+    hdlc_tx_init(&s->hdlc_tx, FALSE, 1, TRUE, lapm_hdlc_underflow, s);
+    hdlc_rx_init(&s->hdlc_rx, FALSE, FALSE, 1, lapm_receive, s);
+    /* TODO: This is a bodge! */
+    s->t401_timer = -1;
+    s->t402_timer = -1;
+    s->t403_timer = -1;
+    lapm_reset(s);
+    /* TODO: Maybe we should implement T_WAIT? */
+    lapm_send_sabme(NULL, s);
+}
+/*- End of function --------------------------------------------------------*/
+
+#if 0
+static void lapm_init(lapm_state_t *s)
+{
+    lapm_restart(s);
+}
+/*- End of function --------------------------------------------------------*/
+#endif
+
+static void negotiation_rx_bit(v42_state_t *s, int new_bit)
+{
+    /* DC1 with even parity, 8-16 ones, DC1 with odd parity, 8-16 ones */
+    //uint8_t odp = "0100010001 11111111 0100010011 11111111";
+    /* V.42 OK E , 8-16 ones, C, 8-16 ones */
+    //uint8_t adp_v42 = "0101000101  11111111  0110000101  11111111";
+    /* V.42 disabled E, 8-16 ones, NULL, 8-16 ones */
+    //uint8_t adp_nov42 = "0101000101  11111111  0000000001  11111111";
+
+    /* There may be no negotiation, so we need to process this data through the
+       HDLC receiver as well */
+    if (new_bit < 0)
+    {
+        /* Special conditions */
+        switch (new_bit)
+        {
+        case PUTBIT_CARRIER_UP:
+            break;
+        case PUTBIT_CARRIER_DOWN:
+        case PUTBIT_TRAINING_SUCCEEDED:
+        case PUTBIT_TRAINING_FAILED:
+            break;
+        default:
+            span_log(&s->logging, SPAN_LOG_WARNING, "Unexpected special 'bit' code %d\n", new_bit);
+            break;
+        }
+        /*endswitch*/
+        return;
+    }
+    /*endif*/
+    new_bit &= 1;
+    s->rxstream = (s->rxstream << 1) | new_bit;
+    switch (s->rx_negotiation_step)
+    {
+    case 0:
+        /* Look for some ones */
+        if (new_bit)
+            break;
+        /*endif*/
+        s->rx_negotiation_step = 1;
+        s->rxbits = 0;
+        s->rxstream = ~1;
+        s->rxoks = 0;
+        break;
+    case 1:
+        /* Look for the first character */
+        if (++s->rxbits < 9)
+            break;
+        /*endif*/
+        s->rxstream &= 0x3FF;
+        if (s->caller  &&  s->rxstream == 0x145)
+        {
+            s->rx_negotiation_step++;
+        }
+        else if (!s->caller  &&  s->rxstream == 0x111)
+        {
+            s->rx_negotiation_step++;
+        }
+        else
+        {
+            s->rx_negotiation_step = 0;
+        }
+        /*endif*/
+        s->rxbits = 0;
+        s->rxstream = ~0;
+        break;
+    case 2:
+        /* Look for 8 to 16 ones */
+        s->rxbits++;
+        if (new_bit)
+            break;
+        /*endif*/
+        if (s->rxbits >= 8  &&  s->rxbits <= 16)
+            s->rx_negotiation_step++;
+        else
+            s->rx_negotiation_step = 0;
+        /*endif*/
+        s->rxbits = 0;
+        s->rxstream = ~1;
+        break;
+    case 3:
+        /* Look for the second character */
+        if (++s->rxbits < 9)
+            break;
+        /*endif*/
+        s->rxstream &= 0x3FF;
+        if (s->caller  &&  s->rxstream == 0x185)
+        {
+            s->rx_negotiation_step++;
+        }
+        else if (s->caller  &&  s->rxstream == 0x001)
+        {
+            s->rx_negotiation_step++;
+        }
+        else if (!s->caller  &&  s->rxstream == 0x113)
+        {
+            s->rx_negotiation_step++;
+        }
+        else
+        {
+            s->rx_negotiation_step = 0;
+        }
+        /*endif*/
+        s->rxbits = 0;
+        s->rxstream = ~0;
+        break;
+    case 4:
+        /* Look for 8 to 16 ones */
+        s->rxbits++;
+        if (new_bit)
+            break;
+        /*endif*/
+        if (s->rxbits >= 8  &&  s->rxbits <= 16)
+        {
+            if (++s->rxoks >= 2)
+            {
+                /* HIT */
+                s->rx_negotiation_step++;
+                if (s->caller)
+                {
+                    if (s->t400_timer >= 0)
+                    {
+fprintf(stderr, "Deleting T400 h %d\n", s->t400_timer);
+                        span_schedule_del(&s->lapm.sched, s->t400_timer);
+                        s->t400_timer = -1;
+                    }
+                    /*endif*/
+                    s->lapm.state = LAPM_ESTABLISH;
+                    if (s->lapm.status_callback)
+                        s->lapm.status_callback(s->lapm.status_callback_user_data, s->lapm.state);
+                    /*endif*/
+                }
+                else
+                {
+                    s->odp_seen = TRUE;
+                }
+                /*endif*/
+                break;
+            }
+            /*endif*/
+            s->rx_negotiation_step = 1;
+            s->rxbits = 0;
+            s->rxstream = ~1;
+        }
+        else
+        {
+            s->rx_negotiation_step = 0;
+            s->rxbits = 0;
+            s->rxstream = ~0;
+        }
+        /*endif*/
+        break;
+    case 5:
+        /* Parked */
+        break;
+    }
+    /*endswitch*/
+}
+/*- End of function --------------------------------------------------------*/
+
+static int v42_support_negotiation_tx_bit(v42_state_t *s)
+{
+    int bit;
+
+    if (s->caller)
+    {
+        if (s->txbits <= 0)
+        {
+            s->txstream = 0x3FE22;
+            s->txbits = 36;
+        }
+        else if (s->txbits == 18)
+        {
+            s->txstream = 0x3FF22;
+        }
+        /*endif*/
+        bit = s->txstream & 1;
+        s->txstream >>= 1;
+        s->txbits--;
+    }
+    else
+    {
+        if (s->odp_seen  &&  s->txadps < 10)
+        {
+            if (s->txbits <= 0)
+            {
+                if (++s->txadps >= 10)
+                {
+                    if (s->t400_timer >= 0)
+                    {
+fprintf(stderr, "Deleting T400 i %d\n", s->t400_timer);
+                        span_schedule_del(&s->lapm.sched, s->t400_timer);
+                        s->t400_timer = -1;
+                    }
+                    /*endif*/
+                    s->lapm.state = LAPM_ESTABLISH;
+                    if (s->lapm.status_callback)
+                        s->lapm.status_callback(s->lapm.status_callback_user_data, s->lapm.state);
+                    /*endif*/
+                    s->txstream = 1;
+                }
+                else
+                {
+                    s->txstream = 0x3FE8A;
+                    s->txbits = 36;
+                }
+                /*endif*/
+            }
+            else if (s->txbits == 18)
+            {
+                s->txstream = 0x3FE86;
+            }
+            /*endif*/
+            bit = s->txstream & 1;
+            s->txstream >>= 1;
+            s->txbits--;
+        }
+        else
+        {
+            bit = 1;
+        }
+        /*endif*/
+    }
+    /*endif*/
+    return bit;
+}
+/*- End of function --------------------------------------------------------*/
+
+void v42_rx_bit(void *user_data, int bit)
+{
+    v42_state_t *s;
+
+    s = (v42_state_t *) user_data;
+    if (s->lapm.state == LAPM_DETECT)
+        negotiation_rx_bit(s, bit);
+    else
+        hdlc_rx_put_bit(&s->lapm.hdlc_rx, bit);
+    /*endif*/
+}
+/*- End of function --------------------------------------------------------*/
+
+int v42_tx_bit(void *user_data)
+{
+    v42_state_t *s;
+    int bit;
+
+    s = (v42_state_t *) user_data;
+    if (s->lapm.state == LAPM_DETECT)
+        bit = v42_support_negotiation_tx_bit(s);
+    else
+        bit = hdlc_tx_get_bit(&s->lapm.hdlc_tx);
+    /*endif*/
+    return bit;
+}
+/*- End of function --------------------------------------------------------*/
+
+void v42_set_status_callback(v42_state_t *s, v42_status_func_t callback, void *user_data)
+{
+    s->lapm.status_callback = callback;
+    s->lapm.status_callback_user_data = user_data;
+}
+/*- End of function --------------------------------------------------------*/
+
+void v42_restart(v42_state_t *s)
+{
+    span_schedule_init(&s->lapm.sched);
+
+    s->lapm.we_are_originator = s->caller;
+    lapm_restart(&s->lapm);
+    if (s->detect)
+    {
+        s->txstream = ~0;
+        s->txbits = 0;
+        s->rxstream = ~0;
+        s->rxbits = 0;
+        s->rxoks = 0;
+        s->txadps = 0;
+        s->rx_negotiation_step = 0;
+        s->odp_seen = FALSE;
+fprintf(stderr, "Setting T400 i\n");
+        s->t400_timer = span_schedule_event(&s->lapm.sched, T_400, t400_expired, s);
+        s->lapm.state = LAPM_DETECT;
+    }
+    else
+    {
+        s->lapm.state = LAPM_ESTABLISH;
+    }
+    /*endif*/
+}
+/*- End of function --------------------------------------------------------*/
+
+v42_state_t *v42_init(v42_state_t *s, int caller, int detect, v42_frame_handler_t frame_handler, void *user_data)
+{
+    int alloced;
+    
+    if (frame_handler == NULL)
+        return NULL;
+    /*endif*/
+    alloced = FALSE;
+    if (s == NULL)
+    {
+        if ((s = (v42_state_t *) malloc(sizeof(*s))) == NULL)
+            return NULL;
+        alloced = TRUE;
+    }
+    memset(s, 0, sizeof(*s));
+    s->caller = caller;
+    s->detect = detect;
+    s->lapm.iframe_receive = frame_handler;
+    s->lapm.iframe_receive_user_data = user_data;
+    s->lapm.debug |= (LAPM_DEBUG_LAPM_RAW | LAPM_DEBUG_LAPM_DUMP | LAPM_DEBUG_LAPM_STATE);
+    s->lapm.t401_timer =
+    s->lapm.t402_timer =
+    s->lapm.t403_timer = -1;
+
+    if ((s->lapm.tx_queue = queue_init(NULL, 16384, 0)) == NULL)
+    {
+        if (alloced)
+            free(s);
+        return NULL;
+    }
+    /*endif*/
+    span_log_init(&s->logging, SPAN_LOG_NONE, NULL);
+    span_log_set_protocol(&s->logging, "V.42");
+    v42_restart(s);
+    return s;
+}
+/*- End of function --------------------------------------------------------*/
+
+int v42_release(v42_state_t *s)
+{
+    free(s);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/v42bis.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/v42bis.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,693 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * v42bis.c
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2005 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: v42bis.c,v 1.32 2008/05/13 13:17:25 steveu Exp $
+ */
+
+/* THIS IS A WORK IN PROGRESS. IT IS NOT FINISHED. 
+   Currently it performs the core compression and decompression functions OK.
+   However, a number of the bells and whistles in V.42bis are incomplete. */
+
+/*! \file */
+
+#if defined(HAVE_CONFIG_H)
+#include <config.h>
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <inttypes.h>
+#include <string.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <ctype.h>
+#include <assert.h>
+
+#include "spandsp/telephony.h"
+#include "spandsp/logging.h"
+#include "spandsp/bit_operations.h"
+#include "spandsp/v42bis.h"
+
+/* Fixed parameters from the spec. */
+#define V42BIS_N3               8   /* Character size (bits) */
+#define V42BIS_N4               256 /* Number of characters in the alphabet */
+#define V42BIS_N5               (V42BIS_N4 + V42BIS_N6)  /* Index number of first dictionary entry used to store a string */
+#define V42BIS_N6               3   /* Number of control codewords */
+
+/* Control code words in compressed mode */
+enum
+{
+    V42BIS_ETM = 0,         /* Enter transparent mode */
+    V42BIS_FLUSH = 1,       /* Flush data */
+    V42BIS_STEPUP = 2       /* Step up codeword size */
+};
+
+/* Command codes in transparent mode */
+enum
+{
+    V42BIS_ECM = 0,         /* Enter compression mode */
+    V42BIS_EID = 1,         /* Escape character in data */
+    V42BIS_RESET = 2        /* Force reinitialisation */
+};
+
+static __inline__ void push_compressed_raw_octet(v42bis_compress_state_t *ss, int octet)
+{
+    ss->output_buf[ss->output_octet_count++] = (uint8_t) octet;
+    if (ss->output_octet_count >= ss->max_len)
+    {
+        ss->handler(ss->user_data, ss->output_buf, ss->output_octet_count);
+        ss->output_octet_count = 0;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ void push_compressed_code(v42bis_compress_state_t *ss, int code)
+{
+    ss->output_bit_buffer |= code << (32 - ss->v42bis_parm_c2 - ss->output_bit_count);
+    ss->output_bit_count += ss->v42bis_parm_c2;
+    while (ss->output_bit_count >= 8)
+    {
+        push_compressed_raw_octet(ss, ss->output_bit_buffer >> 24);
+        ss->output_bit_buffer <<= 8;
+        ss->output_bit_count -= 8;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ void push_compressed_octet(v42bis_compress_state_t *ss, int code)
+{
+    ss->output_bit_buffer |= code << (32 - 8 - ss->output_bit_count);
+    ss->output_bit_count += 8;
+    while (ss->output_bit_count >= 8)
+    {
+        push_compressed_raw_octet(ss, ss->output_bit_buffer >> 24);
+        ss->output_bit_buffer <<= 8;
+        ss->output_bit_count -= 8;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+int v42bis_compress(v42bis_state_t *s, const uint8_t *buf, int len)
+{
+    int ptr;
+    int i;
+    uint32_t octet;
+    uint32_t code;
+    v42bis_compress_state_t *ss;
+
+    ss = &s->compress;
+    if ((s->v42bis_parm_p0 & 2) == 0)
+    {
+        /* Compression is off - just push the incoming data out */
+        for (i = 0;  i < len - ss->max_len;  i += ss->max_len)
+            ss->handler(ss->user_data, buf + i, ss->max_len);
+        if (i < len)
+            ss->handler(ss->user_data, buf + i, len - i);
+        return 0;
+    }
+    ptr = 0;
+    if (ss->first  &&  len > 0)
+    {
+        octet = buf[ptr++];
+        ss->string_code = octet + V42BIS_N6;
+        if (ss->transparent)
+            push_compressed_octet(ss, octet);
+        ss->first = FALSE;
+    }
+    while (ptr < len)
+    {
+        octet = buf[ptr++];
+        if ((ss->dict[ss->string_code].children[octet >> 5] & (1 << (octet & 0x1F))))
+        {
+            /* The leaf exists. Now find it in the table. */
+            /* TODO: This is a brute force scan for a match. We need something better. */
+            for (code = 0;  code < ss->v42bis_parm_c3;  code++)
+            {
+                if (ss->dict[code].parent_code == ss->string_code  &&  ss->dict[code].node_octet == octet)
+                    break;
+            }
+        }
+        else
+        {
+            /* The leaf does not exist. */
+            code = s->v42bis_parm_n2;
+        }
+        /* 6.3(b) If the string matches a dictionary entry, and the entry is not that entry
+                  created by the last invocation of the string matching procedure, then the
+                  next character shall be read and appended to the string and this step
+                  repeated. */
+        if (code < ss->v42bis_parm_c3  &&  code != ss->latest_code)
+        {
+            /* The string was found */
+            ss->string_code = code;
+            ss->string_length++;
+        }
+        else
+        {
+            /* The string is not in the table. */
+            if (!ss->transparent)
+            {
+                /* 7.4 Encoding - we now have the longest matchable string, and will need to output the code for it. */
+                while (ss->v42bis_parm_c1 >= ss->v42bis_parm_c3  &&  ss->v42bis_parm_c3 <= s->v42bis_parm_n2)
+                {
+                    /* We need to increase the codeword size */
+                    /* 7.4(a) */
+                    push_compressed_code(ss, V42BIS_STEPUP);
+                    /* 7.4(b) */
+                    ss->v42bis_parm_c2++;
+                    /* 7.4(c) */
+                    ss->v42bis_parm_c3 <<= 1;
+                    /* 7.4(d) this might need to be repeated, so we loop */
+                }
+                /* 7.5 Transfer - output the last state of the string */
+                push_compressed_code(ss, ss->string_code);
+            }
+            /* 7.6    Dictionary updating */
+            /* 6.4    Add the string to the dictionary */
+            /* 6.4(b) The string is not in the table. */
+            if (code != ss->latest_code  &&  ss->string_length < s->v42bis_parm_n7)
+            {
+                ss->latest_code = ss->v42bis_parm_c1;
+                /* 6.4(a) The length of the string is in range for adding to the dictionary */
+                /* If the last code was a leaf, it no longer is */
+                ss->dict[ss->string_code].leaves++;
+                ss->dict[ss->string_code].children[octet >> 5] |= (1 << (octet & 0x1F));
+                /* The new one is definitely a leaf */
+                ss->dict[ss->v42bis_parm_c1].parent_code = (uint16_t) ss->string_code;
+                ss->dict[ss->v42bis_parm_c1].leaves = 0;
+                ss->dict[ss->v42bis_parm_c1].node_octet = (uint8_t) octet;
+                /* 7.7    Node recovery */
+                /* 6.5    Recovering a dictionary entry to use next */
+                for (;;)
+                {
+                    /* 6.5(a) and (b) */
+                    if ((int) (++ss->v42bis_parm_c1) >= s->v42bis_parm_n2)
+                        ss->v42bis_parm_c1 = V42BIS_N5;
+                    /* 6.5(c) We need to reuse a leaf node */
+                    if (ss->dict[ss->v42bis_parm_c1].leaves)
+                        continue;
+                    if (ss->dict[ss->v42bis_parm_c1].parent_code == 0xFFFF)
+                        break;
+                    /* 6.5(d) Detach the leaf node from its parent, and re-use it */
+                    /* Possibly make the parent a leaf node again */
+                    ss->dict[ss->dict[ss->v42bis_parm_c1].parent_code].leaves--;
+                    ss->dict[ss->dict[ss->v42bis_parm_c1].parent_code].children[ss->dict[ss->v42bis_parm_c1].node_octet >> 5] &= ~(1 << (ss->dict[ss->v42bis_parm_c1].node_octet & 0x1F));
+                    ss->dict[ss->v42bis_parm_c1].parent_code = 0xFFFF;
+                    break;
+                }
+            }
+            else
+            {
+                ss->latest_code = 0xFFFFFFFF;
+            }
+            /* 7.8 Data compressibility test */
+            /* Filter on the balance of what went into the compressor, and what came out */
+            ss->compressibility_filter += ((((8*ss->string_length - ss->v42bis_parm_c2) << 20) - ss->compressibility_filter) >> 10);
+            if (ss->compression_mode == V42BIS_COMPRESSION_MODE_DYNAMIC)
+            {
+                /* Work out if it is appropriate to change between transparent and
+                   compressed mode. */
+                if (ss->transparent)
+                {
+                    if (ss->compressibility_filter > 0)
+                    {
+                        if (++ss->compressibility_persistence > 1000)
+                        {
+                            /* Schedule a switch to compressed mode */
+                            ss->change_transparency = -1;
+                            ss->compressibility_persistence = 0;
+                        }
+                    }
+                    else
+                    {
+                        ss->compressibility_persistence = 0;
+                    }
+                }
+                else
+                {
+                    if (ss->compressibility_filter < 0)
+                    {
+                        if (++ss->compressibility_persistence > 1000)
+                        {
+                            /* Schedule a switch to transparent mode */
+                            ss->change_transparency = 1;
+                            ss->compressibility_persistence = 0;
+                        }
+                    }
+                    else
+                    {
+                        ss->compressibility_persistence = 0;
+                    }
+                }
+            }
+            if (ss->change_transparency)
+            {
+                if (ss->change_transparency < 0)
+                {
+                    if (ss->transparent)
+                    {
+                        printf("Going compressed\n");
+                        /* 7.8.1 Transition to compressed mode */
+                        /* Switch out of transparent now, between codes. We need to send the octet which did not
+                        match, just before switching. */
+                        if (octet == ss->escape_code)
+                        {
+                            push_compressed_octet(ss, ss->escape_code++);
+                            push_compressed_octet(ss, V42BIS_EID);
+                        }
+                        else
+                        {
+                            push_compressed_octet(ss, octet);
+                        }
+                        push_compressed_octet(ss, ss->escape_code++);
+                        push_compressed_octet(ss, V42BIS_ECM);
+                        ss->transparent = FALSE;
+                    }
+                }
+                else
+                {
+                    if (!ss->transparent)
+                    {
+                        printf("Going transparent\n");
+                        /* 7.8.2 Transition to transparent mode */
+                        /* Switch into transparent now, between codes, and the unmatched octet should
+                           go out in transparent mode, just below */
+                        push_compressed_code(ss, V42BIS_ETM);
+                        ss->transparent = TRUE;
+                    }
+                }
+                ss->change_transparency = 0;
+            }
+            /* 7.8.3 Reset function - TODO */
+            ss->string_code = octet + V42BIS_N6;
+            ss->string_length = 1;
+        }
+        if (ss->transparent)
+        {
+            if (octet == ss->escape_code)
+            {
+                push_compressed_octet(ss, ss->escape_code++);
+                push_compressed_octet(ss, V42BIS_EID);
+            }
+            else
+            {
+                push_compressed_octet(ss, octet);
+            }
+        }
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int v42bis_compress_flush(v42bis_state_t *s)
+{
+    v42bis_compress_state_t *ss;
+
+    ss = &s->compress;
+    if (!ss->transparent)
+    {
+        /* Output the last state of the string */
+        push_compressed_code(ss, ss->string_code);
+        /* TODO: We use a positive FLUSH at all times. It is really needed, if the
+           previous step resulted in no leftover bits. */
+        push_compressed_code(ss, V42BIS_FLUSH);
+    }
+    while (ss->output_bit_count > 0)
+    {
+        push_compressed_raw_octet(ss, ss->output_bit_buffer >> 24);
+        ss->output_bit_buffer <<= 8;
+        ss->output_bit_count -= 8;
+    }
+    /* Now push out anything remaining. */
+    if (ss->output_octet_count > 0)
+    {
+        ss->handler(ss->user_data, ss->output_buf, ss->output_octet_count);
+        ss->output_octet_count = 0;
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+#if 0
+int v42bis_compress_dump(v42bis_state_t *s)
+{
+    int i;
+    
+    for (i = 0;  i < V42BIS_MAX_CODEWORDS;  i++)
+    {
+        if (s->compress.dict[i].parent_code != 0xFFFF)
+        {
+            printf("Entry %4x, prior %4x, leaves %d, octet %2x\n", i, s->compress.dict[i].parent_code, s->compress.dict[i].leaves, s->compress.dict[i].node_octet);
+        }
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+#endif
+
+int v42bis_decompress(v42bis_state_t *s, const uint8_t *buf, int len)
+{
+    int ptr;
+    int i;
+    int this_length;
+    uint8_t *string;
+    uint32_t code;
+    uint32_t new_code;
+    int code_len;
+    v42bis_decompress_state_t *ss;
+    uint8_t decode_buf[V42BIS_MAX_STRING_SIZE];
+
+    ss = &s->decompress;
+    if ((s->v42bis_parm_p0 & 1) == 0)
+    {
+        /* Compression is off - just push the incoming data out */
+        for (i = 0;  i < len - ss->max_len;  i += ss->max_len)
+            ss->handler(ss->user_data, buf + i, ss->max_len);
+        if (i < len)
+            ss->handler(ss->user_data, buf + i, len - i);
+        return 0;
+    }
+    ptr = 0;
+    code_len = (ss->transparent)  ?  8  :  ss->v42bis_parm_c2;
+    for (;;)
+    {
+        /* Fill up the bit buffer. */
+        while (ss->input_bit_count < 32 - 8  &&  ptr < len)
+        {
+            ss->input_bit_count += 8;
+            ss->input_bit_buffer |= (uint32_t) buf[ptr++] << (32 - ss->input_bit_count);
+        }
+        if (ss->input_bit_count < code_len)
+            break;
+        new_code = ss->input_bit_buffer >> (32 - code_len);
+        ss->input_bit_count -= code_len;
+        ss->input_bit_buffer <<= code_len;
+        if (ss->transparent)
+        {
+            code = new_code;
+            if (ss->escaped)
+            {
+                ss->escaped = FALSE;
+                if (code == V42BIS_ECM)
+                {
+                    printf("Hit V42BIS_ECM\n");
+                    ss->transparent = FALSE;
+                    code_len = ss->v42bis_parm_c2;
+                }
+                else if (code == V42BIS_EID)
+                {
+                    printf("Hit V42BIS_EID\n");
+                    ss->output_buf[ss->output_octet_count++] = ss->escape_code - 1;
+                    if (ss->output_octet_count >= ss->max_len - s->v42bis_parm_n7)
+                    {
+                        ss->handler(ss->user_data, ss->output_buf, ss->output_octet_count);
+                        ss->output_octet_count = 0;
+                    }
+                }
+                else if (code == V42BIS_RESET)
+                {
+                    printf("Hit V42BIS_RESET\n");
+                }
+                else
+                {
+                    printf("Hit V42BIS_???? - %" PRIu32 "\n", code);
+                }
+            }
+            else if (code == ss->escape_code)
+            {
+                ss->escape_code++;
+                ss->escaped = TRUE;
+            }
+            else
+            {
+                ss->output_buf[ss->output_octet_count++] = (uint8_t) code;
+                if (ss->output_octet_count >= ss->max_len - s->v42bis_parm_n7)
+                {
+                    ss->handler(ss->user_data, ss->output_buf, ss->output_octet_count);
+                    ss->output_octet_count = 0;
+                }
+            }
+        }
+        else
+        {
+            if (new_code < V42BIS_N6)
+            {
+                /* We have a control code. */
+                switch (new_code)
+                {
+                case V42BIS_ETM:
+                    printf("Hit V42BIS_ETM\n");
+                    ss->transparent = TRUE;
+                    code_len = 8;
+                    break;
+                case V42BIS_FLUSH:
+                    printf("Hit V42BIS_FLUSH\n");
+                    v42bis_decompress_flush(s);
+                    break;
+                case V42BIS_STEPUP:
+                    /* We need to increase the codeword size */
+                    printf("Hit V42BIS_STEPUP\n");
+                    if (ss->v42bis_parm_c3 >= s->v42bis_parm_n2)
+                    {
+                        /* Invalid condition */
+                        return -1;
+                    }
+                    code_len = ++ss->v42bis_parm_c2;
+                    ss->v42bis_parm_c3 <<= 1;
+                    break;
+                }
+                continue;
+            }
+            if (ss->first)
+            {
+                ss->first = FALSE;
+                ss->octet = new_code - V42BIS_N6;
+                ss->output_buf[0] = (uint8_t) ss->octet;
+                ss->output_octet_count = 1;
+                if (ss->output_octet_count >= ss->max_len - s->v42bis_parm_n7)
+                {
+                    ss->handler(ss->user_data, ss->output_buf, ss->output_octet_count);
+                    ss->output_octet_count = 0;
+                }
+                ss->old_code = new_code;
+                continue;
+            }
+            /* Start at the end of the buffer, and decode backwards */
+            string = &decode_buf[V42BIS_MAX_STRING_SIZE - 1];
+            /* Check the received code is valid. It can't be too big, as we pulled only the expected number
+            of bits from the input stream. It could, however, be unknown. */
+            if (ss->dict[new_code].parent_code == 0xFFFF)
+                return -1;
+            /* Otherwise we do a straight decode of the new code. */
+            code = new_code;
+            /* Trace back through the octets which form the string, and output them. */
+            while (code >= V42BIS_N5)
+            {
+if (code > 4095) {printf("Code is 0x%" PRIu32 "\n", code); exit(2);}
+                *string-- = ss->dict[code].node_octet;
+                code = ss->dict[code].parent_code;
+            }
+            *string = (uint8_t) (code - V42BIS_N6);
+            ss->octet = code - V42BIS_N6;
+            /* Output the decoded string. */
+            this_length = V42BIS_MAX_STRING_SIZE - (int) (string - decode_buf);
+            memcpy(ss->output_buf + ss->output_octet_count, string, this_length);
+            ss->output_octet_count += this_length;
+            if (ss->output_octet_count >= ss->max_len - s->v42bis_parm_n7)
+            {
+                ss->handler(ss->user_data, ss->output_buf, ss->output_octet_count);
+                ss->output_octet_count = 0;
+            }
+            /* 6.4 Add the string to the dictionary */
+            if (ss->last_length < s->v42bis_parm_n7)
+            {
+                /* 6.4(a) The string does not exceed N7 in length */
+                if (ss->last_old_code != ss->old_code
+                    ||
+                    ss->last_extra_octet != *string)
+                {
+                    /* 6.4(b) The string is not in the table. */
+                    ss->dict[ss->old_code].leaves++;
+                    /* The new one is definitely a leaf */
+                    ss->dict[ss->v42bis_parm_c1].parent_code = (uint16_t) ss->old_code;
+                    ss->dict[ss->v42bis_parm_c1].leaves = 0;
+                    ss->dict[ss->v42bis_parm_c1].node_octet = (uint8_t) ss->octet;
+                    /* 6.5 Recovering a dictionary entry to use next */
+                    for (;;)
+                    {
+                        /* 6.5(a) and (b) */
+                        if (++ss->v42bis_parm_c1 >= s->v42bis_parm_n2)
+                            ss->v42bis_parm_c1 = V42BIS_N5;
+                        /* 6.5(c) We need to reuse a leaf node */
+                        if (ss->dict[ss->v42bis_parm_c1].leaves)
+                            continue;
+                        /* 6.5(d) This is a leaf node, so re-use it */
+                        /* Possibly make the parent a leaf node again */
+                        if (ss->dict[ss->v42bis_parm_c1].parent_code != 0xFFFF)
+                            ss->dict[ss->dict[ss->v42bis_parm_c1].parent_code].leaves--;
+                        ss->dict[ss->v42bis_parm_c1].parent_code = 0xFFFF;
+                        break;
+                    }
+                }
+            }
+            /* Record the addition to the dictionary, so we can check for repeat attempts
+               at the next code - see II.4.3 */
+            ss->last_old_code = ss->old_code;
+            ss->last_extra_octet = *string;
+
+            ss->old_code = new_code;
+            ss->last_length = this_length;
+        }
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int v42bis_decompress_flush(v42bis_state_t *s)
+{
+    v42bis_decompress_state_t *ss;
+
+    ss = &s->decompress;
+    /* Push out anything remaining. */
+    if (ss->output_octet_count > 0)
+    {
+        ss->handler(ss->user_data, ss->output_buf, ss->output_octet_count);
+        ss->output_octet_count = 0;
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+#if 0
+int v42bis_decompress_dump(v42bis_state_t *s)
+{
+    int i;
+    
+    for (i = 0;  i < V42BIS_MAX_CODEWORDS;  i++)
+    {
+        if (s->decompress.dict[i].parent_code != 0xFFFF)
+        {
+            printf("Entry %4x, prior %4x, leaves %d, octet %2x\n", i, s->decompress.dict[i].parent_code, s->decompress.dict[i].leaves, s->decompress.dict[i].node_octet);
+        }
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+#endif
+
+void v42bis_compression_control(v42bis_state_t *s, int mode)
+{
+    s->compress.compression_mode = mode;
+    switch (mode)
+    {
+    case V42BIS_COMPRESSION_MODE_ALWAYS:
+        s->compress.change_transparency = -1;
+        break;
+    case V42BIS_COMPRESSION_MODE_NEVER:
+        s->compress.change_transparency = 1;
+        break;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+v42bis_state_t *v42bis_init(v42bis_state_t *s,
+                            int negotiated_p0,
+                            int negotiated_p1,
+                            int negotiated_p2,
+                            v42bis_frame_handler_t frame_handler,
+                            void *frame_user_data,
+                            int max_frame_len,
+                            v42bis_data_handler_t data_handler,
+                            void *data_user_data,
+                            int max_data_len)
+{
+    int i;
+
+    if (negotiated_p1 < 512  ||  negotiated_p1 > 65535)
+        return NULL;
+    if (negotiated_p2 < 6  ||  negotiated_p2 > V42BIS_MAX_STRING_SIZE)
+        return NULL;
+    if (s == NULL)
+    {
+        if ((s = (v42bis_state_t *) malloc(sizeof(*s))) == NULL)
+            return NULL;
+    }
+    memset(s, 0, sizeof(*s));
+
+    s->compress.handler = frame_handler;
+    s->compress.user_data = frame_user_data;
+    s->compress.max_len = (max_frame_len < 1024)  ?  max_frame_len  :  1024;
+
+    s->decompress.handler = data_handler;
+    s->decompress.user_data = data_user_data;
+    s->decompress.max_len = (max_data_len < 1024)  ?  max_data_len  :  1024;
+
+    s->v42bis_parm_p0 = negotiated_p0;  /* default is both ways off */
+
+    s->v42bis_parm_n1 = top_bit(negotiated_p1 - 1) + 1;
+    s->v42bis_parm_n2 = negotiated_p1;
+    s->v42bis_parm_n7 = negotiated_p2;
+
+    /* 6.5 */
+    s->compress.v42bis_parm_c1 =
+    s->decompress.v42bis_parm_c1 = V42BIS_N5;
+
+    s->compress.v42bis_parm_c2 =
+    s->decompress.v42bis_parm_c2 = V42BIS_N3 + 1;
+
+    s->compress.v42bis_parm_c3 =
+    s->decompress.v42bis_parm_c3 = 2*V42BIS_N4;
+
+    s->compress.first =
+    s->decompress.first = TRUE;
+    for (i = 0;  i < V42BIS_MAX_CODEWORDS;  i++)
+    {
+        s->compress.dict[i].parent_code =
+        s->decompress.dict[i].parent_code = 0xFFFF;
+        s->compress.dict[i].leaves =
+        s->decompress.dict[i].leaves = 0;
+    }
+    /* Point the root nodes for decompression to themselves. It doesn't matter much what
+       they are set to, as long as they are considered "known" codes. */
+    for (i = 0;  i < V42BIS_N5;  i++)
+        s->decompress.dict[i].parent_code = (uint16_t) i;
+    s->compress.string_code = 0xFFFFFFFF;
+    s->compress.latest_code = 0xFFFFFFFF;
+    
+    s->decompress.last_old_code = 0xFFFFFFFF;
+    s->decompress.last_extra_octet = -1;
+
+    s->compress.compression_mode = V42BIS_COMPRESSION_MODE_DYNAMIC;
+
+    return s;
+}
+/*- End of function --------------------------------------------------------*/
+
+int v42bis_release(v42bis_state_t *s)
+{
+    free(s);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/v8.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/v8.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,887 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * v8.c - V.8 modem negotiation processing.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2004 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: v8.c,v 1.30 2008/07/02 14:48:26 steveu Exp $
+ */
+ 
+/*! \file */
+
+#if defined(HAVE_CONFIG_H)
+#include <config.h>
+#endif
+
+#include <inttypes.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <memory.h>
+#include "floating_fudge.h"
+#if defined(HAVE_TGMATH_H)
+#include <tgmath.h>
+#endif
+#if defined(HAVE_MATH_H)
+#include <math.h>
+#endif
+
+#include "spandsp/telephony.h"
+#include "spandsp/logging.h"
+#include "spandsp/queue.h"
+#include "spandsp/async.h"
+#include "spandsp/complex.h"
+#include "spandsp/dds.h"
+#include "spandsp/tone_detect.h"
+#include "spandsp/tone_generate.h"
+#include "spandsp/super_tone_rx.h"
+#include "spandsp/power_meter.h"
+#include "spandsp/fsk.h"
+#include "spandsp/modem_connect_tones.h"
+#include "spandsp/v8.h"
+
+#define ms_to_samples(t)    (((t)*SAMPLE_RATE)/1000)
+
+enum
+{
+    V8_WAIT_1S,
+    V8_CI,
+    V8_CI_ON,
+    V8_CI_OFF,
+    V8_HEARD_ANSAM,
+    V8_CM_ON,
+    V8_CJ_ON,
+    V8_CM_WAIT,
+
+    V8_SIGC,
+    V8_WAIT_200MS,
+    V8_JM_ON,
+    V8_SIGA,
+
+    V8_PARKED
+} v8_states_e;
+
+enum
+{
+    V8_SYNC_UNKNOWN = 0,
+    V8_SYNC_CI,
+    V8_SYNC_CM_JM,
+    V8_SYNC_V92
+} v8_sync_types_e;
+
+const char *v8_call_function_to_str(int call_function)
+{
+    switch (call_function)
+    {
+    case V8_CALL_TBS:
+        return "TBS";
+    case V8_CALL_H324:
+        return "H.324 PSTN multimedia terminal";
+    case V8_CALL_V18:
+        return "V.18 textphone";
+    case V8_CALL_T101:
+        return "T.101 videotext";
+    case V8_CALL_T30_TX:
+        return "T.30 Tx FAX";
+    case V8_CALL_T30_RX:
+        return "T.30 Rx FAX";
+    case V8_CALL_V_SERIES:
+        return "V series modem data";
+    case V8_CALL_FUNCTION_EXTENSION:
+        return "Call function is in extention octet";
+    }
+    return "???";
+}
+/*- End of function --------------------------------------------------------*/
+
+const char *v8_modulation_to_str(int modulation_scheme)
+{
+    switch (modulation_scheme)
+    {
+    case V8_MOD_V17:
+        return "V.17 half-duplex";
+    case V8_MOD_V21:
+        return "V.21 duplex";
+    case V8_MOD_V22:
+        return "V.22/V22.bis duplex";
+    case V8_MOD_V23HALF:
+        return "V.23 half-duplex";
+    case V8_MOD_V23:
+        return "V.23 duplex";
+    case V8_MOD_V26BIS:
+        return "V.23 duplex";
+    case V8_MOD_V26TER:
+        return "V.23 duplex";
+    case V8_MOD_V27TER:
+        return "V.23 duplex";
+    case V8_MOD_V29:
+        return "V.29 half-duplex";
+    case V8_MOD_V32:
+        return "V.32/V32.bis duplex";
+    case V8_MOD_V34HALF:
+        return "V.34 half-duplex";
+    case V8_MOD_V34:
+        return "V.34 duplex";
+    case V8_MOD_V90:
+        return "V.90 duplex";
+    case V8_MOD_V92:
+        return "V.92 duplex";
+    case V8_MOD_FAILED:
+        return "negotiation failed";
+    }
+    return "???";
+}
+/*- End of function --------------------------------------------------------*/
+
+const char *v8_protocol_to_str(int protocol)
+{
+    switch (protocol)
+    {
+    case V8_PROTOCOL_NONE:
+        return "None";
+    case V8_PROTOCOL_LAPM_V42:
+        return "LAPM";
+    case V8_PROTOCOL_EXTENSION:
+        return "Extension";
+    }
+    return "Undefined";
+}
+/*- End of function --------------------------------------------------------*/
+
+const char *v8_pstn_access_to_str(int pstn_access)
+{
+    return "???";
+}
+/*- End of function --------------------------------------------------------*/
+
+const char *v8_pcm_modem_availability_to_str(int pcm_modem_availability)
+{
+    return "???";
+}
+/*- End of function --------------------------------------------------------*/
+
+void v8_log_supported_modulations(v8_state_t *s, int modulation_schemes)
+{
+    const char *comma;
+    int i;
+    
+    comma = "";
+    span_log(&s->logging, SPAN_LOG_FLOW, "");
+    for (i = 0;  i < 32;  i++)
+    {
+        if ((modulation_schemes & (1 << i)))
+        {
+            span_log(&s->logging, SPAN_LOG_FLOW | SPAN_LOG_SUPPRESS_LABELLING, "%s%s", comma, v8_modulation_to_str(modulation_schemes & (1 << i)));
+            comma = ", ";
+        }
+    }
+    span_log(&s->logging, SPAN_LOG_FLOW | SPAN_LOG_SUPPRESS_LABELLING, " supported\n");
+}
+/*- End of function --------------------------------------------------------*/
+
+static const uint8_t *process_call_function(v8_state_t *s, const uint8_t *p)
+{
+    int call_function;
+
+    call_function = (*p >> 5) & 0x07;
+    span_log(&s->logging, SPAN_LOG_FLOW, "%s\n", v8_call_function_to_str(call_function));
+    s->call_function = call_function;
+    return ++p;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const uint8_t *process_modulation_mode(v8_state_t *s, const uint8_t *p)
+{
+    int far_end_modulations;
+
+    /* Modulation mode octet */
+    far_end_modulations = 0;
+    if (*p & 0x80)
+        far_end_modulations |= V8_MOD_V34HALF;
+    if (*p & 0x40)
+        far_end_modulations |= V8_MOD_V34;
+    if (*p & 0x20)
+        far_end_modulations |= V8_MOD_V90;
+
+    if ((*++p & 0x38) == 0x10)
+    {
+        if (*p & 0x80)
+            far_end_modulations |= V8_MOD_V27TER;
+        if (*p & 0x40)
+            far_end_modulations |= V8_MOD_V29;
+        if (*p & 0x04)
+            far_end_modulations |= V8_MOD_V17;
+        if (*p & 0x02)
+            far_end_modulations |= V8_MOD_V22;
+        if (*p & 0x01)
+            far_end_modulations |= V8_MOD_V32;
+
+        if ((*++p & 0x38) == 0x10)
+        {
+            if (*p & 0x80)
+                far_end_modulations |= V8_MOD_V21;
+            if (*p & 0x40)
+                far_end_modulations |= V8_MOD_V23HALF;
+            if (*p & 0x04)
+                far_end_modulations |= V8_MOD_V23;
+            if (*p & 0x02)
+                far_end_modulations |= V8_MOD_V26BIS;
+            if (*p & 0x01)
+                far_end_modulations |= V8_MOD_V26TER;
+            /* Skip any future extensions we do not understand */
+            while  ((*++p & 0x38) == 0x10)
+                /* dummy loop */;
+        }
+    }
+    s->far_end_modulations = far_end_modulations;
+    v8_log_supported_modulations(s, s->far_end_modulations);
+    return ++p;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const uint8_t *process_protocols(v8_state_t *s, const uint8_t *p)
+{
+    int protocol;
+
+    protocol = (*p >> 5) & 0x07;
+    span_log(&s->logging, SPAN_LOG_FLOW, "%s\n", v8_protocol_to_str(protocol));
+    s->protocol = protocol;
+    return ++p;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const uint8_t *process_pstn_access(v8_state_t *s, const uint8_t *p)
+{
+    int pstn_access;
+
+    pstn_access = (*p >> 5) & 0x07;
+    if (pstn_access & V8_PSTN_ACCESS_DCE_ON_DIGTIAL)
+        span_log(&s->logging, SPAN_LOG_FLOW, "DCE on digital network connection\n");
+    else
+        span_log(&s->logging, SPAN_LOG_FLOW, "DCE on analogue network connection\n");
+    if (pstn_access & V8_PSTN_ACCESS_ANSWER_DCE_CELLULAR)
+        span_log(&s->logging, SPAN_LOG_FLOW, "Answer DCE on cellular connection\n");
+    if (pstn_access & V8_PSTN_ACCESS_CALL_DCE_CELLULAR)
+        span_log(&s->logging, SPAN_LOG_FLOW, "Call DCE on cellular connection\n");
+    return ++p;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const uint8_t *process_non_standard_facilities(v8_state_t *s, const uint8_t *p)
+{
+    ++p;
+    p += *p;
+    return p;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const uint8_t *process_pcm_modem_availability(v8_state_t *s, const uint8_t *p)
+{
+    int pcm_availability;
+
+    pcm_availability = (*p >> 5) & 0x07;
+    if (pcm_availability & V8_PSTN_PCM_MODEM_V91)
+        span_log(&s->logging, SPAN_LOG_FLOW, "V.91 available\n");
+    if (pcm_availability & V8_PSTN_PCM_MODEM_V90_V92_DIGITAL)
+        span_log(&s->logging, SPAN_LOG_FLOW, "V.90 or V.92 digital modem available\n");
+    if (pcm_availability & V8_PSTN_PCM_MODEM_V90_V92_ANALOGUE)
+        span_log(&s->logging, SPAN_LOG_FLOW, "V.90 or V.92 analogue modem available\n");
+    return ++p;
+}
+/*- End of function --------------------------------------------------------*/
+
+static const uint8_t *process_t66(v8_state_t *s, const uint8_t *p)
+{
+    return ++p;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void ci_decode(v8_state_t *s)
+{
+    if ((s->rx_data[0] & 0x1F) == 0x01)
+        process_call_function(s, &s->rx_data[0]);
+}
+/*- End of function --------------------------------------------------------*/
+
+static void cm_jm_decode(v8_state_t *s)
+{
+    const uint8_t *p;
+
+    if (s->got_cm_jm)
+        return;
+
+    /* We must receive two consecutive identical CM or JM sequences to accept it. */
+    if (s->cm_jm_count <= 0
+        ||
+        s->cm_jm_count != s->rx_data_ptr
+        ||
+        memcmp(s->cm_jm_data, s->rx_data, s->rx_data_ptr))
+    {
+        /* Save the current CM or JM sequence */
+        s->cm_jm_count = s->rx_data_ptr;
+        memcpy(s->cm_jm_data, s->rx_data, s->rx_data_ptr);
+        return;
+    }
+    /* We have a pair of matching CMs or JMs */
+    s->got_cm_jm = TRUE;
+
+    span_log(&s->logging, SPAN_LOG_FLOW, "Decoding\n");
+
+    /* Zero indicates the end */
+    s->cm_jm_data[s->cm_jm_count] = 0;
+
+    s->far_end_modulations = 0;
+    p = s->cm_jm_data;
+
+    while (*p)
+    {
+        switch (*p & 0x1F)
+        {
+        case 0x01:
+            p = process_call_function(s, p);
+            break;
+        case 0x05:
+            p = process_modulation_mode(s, p);
+            break;
+        case 0x0A:
+            p = process_protocols(s, p);
+            break;
+        case 0x0D:
+            p = process_pstn_access(s, p);
+            break;
+        case 0x0F:
+            p = process_non_standard_facilities(s, p);
+            break;
+        case 0x07:
+            p = process_pcm_modem_availability(s, p);
+            break;
+        case 0x0E:
+            p = process_t66(s, p);
+            break;
+        default:
+            p++;
+            break;
+        }
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static void put_bit(void *user_data, int bit)
+{
+    v8_state_t *s;
+    int new_preamble_type;
+    const char *tag;
+    uint8_t data;
+
+    s = user_data;
+    if (bit < 0)
+    {
+        /* Special conditions */
+        switch (bit)
+        {
+        case PUTBIT_CARRIER_UP:
+        case PUTBIT_CARRIER_DOWN:
+        case PUTBIT_TRAINING_SUCCEEDED:
+        case PUTBIT_TRAINING_FAILED:
+            break;
+        default:
+            break;
+        }
+        return;
+    }
+    /* Wait until we sync. */
+    s->bit_stream = (s->bit_stream >> 1) | (bit << 19);
+    if (s->bit_stream == 0x803FF)
+        new_preamble_type = V8_SYNC_CI;
+    else if (s->bit_stream == 0xF03FF)
+        new_preamble_type = V8_SYNC_CM_JM;
+    else if (s->bit_stream == 0xAABFF)
+        new_preamble_type = V8_SYNC_V92;
+    else
+        new_preamble_type = V8_SYNC_UNKNOWN;
+    if (new_preamble_type)
+    {
+        /* Debug */
+        if (span_log_test(&s->logging, SPAN_LOG_FLOW))
+        {
+            if (s->preamble_type == V8_SYNC_CI)
+            {
+                tag = "CI: ";
+            }
+            else if (s->preamble_type == V8_SYNC_CM_JM)
+            {
+                if (s->caller)
+                    tag = "JM: ";
+                else
+                    tag = "CM: ";
+            }
+            else if (s->preamble_type == V8_SYNC_V92)
+            {
+                tag = "V92: ";
+            }
+            else
+            {
+                tag = "??: ";
+            }
+            span_log_buf(&s->logging, SPAN_LOG_FLOW, tag, s->rx_data, s->rx_data_ptr);
+        }
+        /* Decode previous sequence */
+        switch (s->preamble_type)
+        {
+        case V8_SYNC_CI:
+            ci_decode(s);
+            break;
+        case V8_SYNC_CM_JM:
+            cm_jm_decode(s);
+            break;
+        }
+        s->preamble_type = new_preamble_type;
+        s->bit_cnt = 0;
+        s->rx_data_ptr = 0;
+    }
+    
+    /* Parse octets with 1 bit start, 1 bit stop */
+    if (s->preamble_type)
+    {
+        s->bit_cnt++;
+        /* Start, stop? */
+        if ((s->bit_stream & 0x80400) == 0x80000  &&  s->bit_cnt >= 10)
+        {
+            /* Store the available data */
+            data = (uint8_t) ((s->bit_stream >> 11) & 0xFF);
+            /* CJ detection */
+            if (data == 0)
+            {
+                if (++s->zero_byte_count == 3)
+                    s->got_cj = TRUE;
+            }
+            else
+            {
+                s->zero_byte_count = 0;
+            }
+
+            if (s->rx_data_ptr < (int) (sizeof(s->rx_data) - 1))
+                s->rx_data[s->rx_data_ptr++] = data;
+            s->bit_cnt = 0;
+        }
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static void v8_decode_init(v8_state_t *s)
+{
+    if (s->caller)
+        fsk_rx_init(&s->v21rx, &preset_fsk_specs[FSK_V21CH2], FALSE, put_bit, s);
+    else
+        fsk_rx_init(&s->v21rx, &preset_fsk_specs[FSK_V21CH1], FALSE, put_bit, s);
+    s->preamble_type = 0;
+    s->bit_stream = 0;
+    s->cm_jm_count = 0;
+    s->got_cm_jm = FALSE;
+    s->got_cj = FALSE;
+    s->zero_byte_count = 0;
+    s->rx_data_ptr = 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int get_bit(void *user_data)
+{
+    v8_state_t *s;
+    uint8_t bit;
+
+    s = user_data;
+    if (queue_read(s->tx_queue, &bit, 1) <= 0)
+        bit = 1;
+    return bit;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void v8_put_byte(v8_state_t *s, int data)
+{
+    int i;
+    uint8_t bits[10];
+
+    /* Insert start & stop bits */
+    bits[0] = 0;
+    for (i = 1;  i < 9;  i++)
+    {
+        bits[i] = (uint8_t) (data & 1);
+        data >>= 1;
+    }
+    bits[9] = 1;
+    queue_write(s->tx_queue, bits, 10);
+}
+/*- End of function --------------------------------------------------------*/
+
+static void send_cm_jm(v8_state_t *s, int mod_mask)
+{
+    int val;
+    static const uint8_t preamble[20] =
+    {
+        /* 10 1's (0x3FF), then 10 bits of CM sync (0x00F) */
+        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1
+    };
+
+    /* Send a CM, or a JM as appropriate */
+    queue_write(s->tx_queue, preamble, 20);
+    
+    /* Data call */
+    v8_put_byte(s, (V8_CALL_V_SERIES << 5) | 0x01);
+    
+    /* Supported modulations */
+    val = 0x05;
+    if (mod_mask & V8_MOD_V90)
+        val |= 0x20;
+    if (mod_mask & V8_MOD_V34)
+        val |= 0x40;
+    v8_put_byte(s, val);
+
+    val = 0x10;
+    if (mod_mask & V8_MOD_V32)
+        val |= 0x01;
+    if (mod_mask & V8_MOD_V22)
+        val |= 0x02;
+    if (mod_mask & V8_MOD_V17)
+        val |= 0x04;
+    if (mod_mask & V8_MOD_V29)
+        val |= 0x40;
+    if (mod_mask & V8_MOD_V27TER)
+        val |= 0x80;
+    v8_put_byte(s, val);
+
+    val = 0x10;
+    if (mod_mask & V8_MOD_V26TER)
+        val |= 0x01;
+    if (mod_mask & V8_MOD_V26BIS)
+        val |= 0x02;
+    if (mod_mask & V8_MOD_V23)
+        val |= 0x04;
+    if (mod_mask & V8_MOD_V23HALF)
+        val |= 0x40;
+    if (mod_mask & V8_MOD_V21)
+        val |= 0x80;
+    v8_put_byte(s, val);
+
+    v8_put_byte(s, (0 << 5) | 0x07);
+
+    v8_put_byte(s, (V8_PROTOCOL_LAPM_V42 << 5) | 0x0A);
+
+    /* No cellular right now */    
+    v8_put_byte(s, (0 << 5) | 0x0D);
+}
+/*- End of function --------------------------------------------------------*/
+
+static int select_modulation(int mask)
+{
+    if (mask & V8_MOD_V90)
+        return V8_MOD_V90;
+    if (mask & V8_MOD_V34)
+        return V8_MOD_V34;
+    if (mask & V8_MOD_V32)
+        return V8_MOD_V32;
+    if (mask & V8_MOD_V23)
+        return V8_MOD_V23;
+    if (mask & V8_MOD_V21)
+        return V8_MOD_V21;
+    return V8_MOD_FAILED;
+}
+/*- End of function --------------------------------------------------------*/
+
+int v8_tx(v8_state_t *s, int16_t *amp, int max_len)
+{
+    int len;
+
+    //span_log(&s->logging, SPAN_LOG_FLOW, "v8_tx state %d\n", s->state);
+    len = 0;
+    switch (s->state)
+    {
+    case V8_CI_ON:
+    case V8_CM_ON:
+    case V8_JM_ON:
+    case V8_CJ_ON:
+        len = fsk_tx(&s->v21tx, amp, max_len);
+        break;
+    case V8_CM_WAIT:
+        /* Send the ANSam tone */
+        len = modem_connect_tones_tx(&s->ansam_tx, amp, max_len);
+        break;
+    }
+    return len;
+}
+/*- End of function --------------------------------------------------------*/
+
+int v8_rx(v8_state_t *s, const int16_t *amp, int len)
+{
+    int i;
+    int residual_samples;
+    v8_result_t result;
+    static const uint8_t preamble[20] =
+    {
+        /* 10 1's (0x3FF), then 10 bits of CI sync (0x001) */
+        1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1
+    };
+
+    //span_log(&s->logging, SPAN_LOG_FLOW, "v8_rx state %d\n", s->state);
+    residual_samples = 0;
+    switch (s->state)
+    {
+    case V8_WAIT_1S:
+        /* Wait 1 second before sending the first CI packet */
+        if ((s->negotiation_timer -= len) > 0)
+            break;
+        s->state = V8_CI;
+        s->ci_count = 0;
+        modem_connect_tones_rx_init(&s->ansam_rx, MODEM_CONNECT_TONES_ANS_PR, NULL, NULL);
+        fsk_tx_init(&s->v21tx, &preset_fsk_specs[FSK_V21CH1], get_bit, s);
+        /* Fall through to the next state */
+    case V8_CI:
+        residual_samples = modem_connect_tones_rx(&s->ansam_rx, amp, len);
+        /* Send 4 CI packets in a burst (the spec says at least 3) */
+        for (i = 0;  i < 4;  i++)
+        {
+            /* 10 1's (0x3FF), then CI sync (0x001) */
+            queue_write(s->tx_queue, preamble, 20);
+            v8_put_byte(s, (V8_CALL_V_SERIES << 5) | 0x01);
+        }
+        s->state = V8_CI_ON;
+        break;
+    case V8_CI_ON:
+        residual_samples = modem_connect_tones_rx(&s->ansam_rx, amp, len);
+        if (queue_empty(s->tx_queue))
+        {
+            s->state = V8_CI_OFF;
+            s->ci_timer = ms_to_samples(500);
+        }
+        break;
+    case V8_CI_OFF:
+        residual_samples = modem_connect_tones_rx(&s->ansam_rx, amp, len);
+        /* Check if an ANSam tone has been detected */
+        if (modem_connect_tones_rx_get(&s->ansam_rx))
+        {
+            /* Set the Te interval. The spec. says 500ms is the minimum,
+               but gives reasons why 1 second is a better value. */
+            s->ci_timer = ms_to_samples(1000);
+            s->state = V8_HEARD_ANSAM;
+            break;
+        }
+        if ((s->ci_timer -= len) <= 0)
+        {
+            if (++s->ci_count >= 10)
+            {
+                /* The spec says we should give up now. */
+                s->state = V8_PARKED;
+                if (s->result_handler)
+                    s->result_handler(s->result_handler_user_data, NULL);
+            }
+            else
+            {
+                /* Try again */
+                s->state = V8_CI;
+            }
+        }
+        break;
+    case V8_HEARD_ANSAM:
+        /* We have heard the ANSam signal, but we still need to wait for the
+           end of the Te timeout period to comply with the spec. */
+        if ((s->ci_timer -= len) <= 0)
+        {
+            v8_decode_init(s);
+            s->state = V8_CM_ON;
+            s->negotiation_timer = ms_to_samples(5000);
+            send_cm_jm(s, s->available_modulations);
+        }
+        break;
+    case V8_CM_ON:
+        residual_samples = fsk_rx(&s->v21rx, amp, len);
+        if (s->got_cm_jm)
+        {
+            /* Now JM has been detected we send CJ and wait for 75 ms
+               before finishing the V.8 analysis. */
+            s->negotiated_modulation = select_modulation(s->far_end_modulations);
+
+            queue_flush(s->tx_queue);
+            for (i = 0;  i < 9;  i++)
+                v8_put_byte(s, 0);
+            s->state = V8_CJ_ON;
+            break;
+        }
+        if ((s->negotiation_timer -= len) <= 0)
+        {
+            /* Timeout */
+            s->state = V8_PARKED;
+            if (s->result_handler)
+                s->result_handler(s->result_handler_user_data, NULL);
+        }
+        if (queue_empty(s->tx_queue))
+        {
+            /* Send CM again */
+            send_cm_jm(s, s->available_modulations);
+        }
+        break;
+    case V8_CJ_ON:
+        residual_samples = fsk_rx(&s->v21rx, amp, len);
+        if (queue_empty(s->tx_queue))
+        {
+            s->negotiation_timer = ms_to_samples(75);
+            s->state = V8_SIGC;
+        }
+        break;
+    case V8_SIGC:
+        if ((s->negotiation_timer -= len) <= 0)
+        {
+            /* The V.8 negotiation has succeeded. */
+            s->state = V8_PARKED;
+            if (s->result_handler)
+            {
+                result.call_function = s->call_function;
+                result.available_modulations = s->far_end_modulations;
+                result.negotiated_modulation = s->negotiated_modulation;
+                result.protocol = s->protocol;
+                result.pstn_access = s->pstn_access;
+                result.nsf_seen = s->nsf_seen;
+                result.pcm_modem_availability = s->pcm_modem_availability;
+                result.t66_seen = s->t66_seen;
+                s->result_handler(s->result_handler_user_data, &result);
+            }
+        }
+        break;
+    case V8_WAIT_200MS:
+        if ((s->negotiation_timer -= len) <= 0)
+        {
+            /* Send the ANSam tone */
+            modem_connect_tones_tx_init(&s->ansam_tx, MODEM_CONNECT_TONES_ANSAM_PR);
+                
+            v8_decode_init(s);
+            s->state = V8_CM_WAIT;
+            s->negotiation_timer = ms_to_samples(5000);
+        }
+        break;
+    case V8_CM_WAIT:
+        residual_samples = fsk_rx(&s->v21rx, amp, len);
+        if (s->got_cm_jm)
+        {
+            /* Stop sending ANSam and send JM instead */
+            fsk_tx_init(&s->v21tx, &preset_fsk_specs[FSK_V21CH2], get_bit, s);
+            /* Set the timeout for JM */
+            s->negotiation_timer = ms_to_samples(5000); 
+            s->state = V8_JM_ON;
+            s->common_modulations = s->available_modulations & s->far_end_modulations;
+            s->negotiated_modulation = select_modulation(s->common_modulations);
+            send_cm_jm(s, s->common_modulations);
+            break;
+        }
+        if ((s->negotiation_timer -= len) <= 0)
+        {
+            /* Timeout */
+            s->state = V8_PARKED;
+            if (s->result_handler)
+                s->result_handler(s->result_handler_user_data, NULL);
+        }
+        break;
+    case V8_JM_ON:
+        residual_samples = fsk_rx(&s->v21rx, amp, len);
+        if (s->got_cj)
+        {
+            /* Stop sending JM, and wait 75 ms */
+            s->negotiation_timer = ms_to_samples(75);
+            s->state = V8_SIGA;
+            break;
+        }
+        if ((s->negotiation_timer -= len) <= 0)
+        {
+            /* Timeout */
+            s->state = V8_PARKED;
+            if (s->result_handler)
+                s->result_handler(s->result_handler_user_data, NULL);
+            break;
+        }
+        if (queue_empty(s->tx_queue))
+        {
+            /* Send JM */
+            send_cm_jm(s, s->common_modulations);
+        }
+        break;
+    case V8_SIGA:
+        if ((s->negotiation_timer -= len) <= 0)
+        {
+            /* The V.8 negotiation has succeeded. */
+            s->state = V8_PARKED;
+            if (s->result_handler)
+            {
+                result.call_function = s->call_function;
+                result.available_modulations = s->far_end_modulations;
+                result.negotiated_modulation = s->negotiated_modulation;
+                result.protocol = s->protocol;
+                result.pstn_access = s->pstn_access;
+                result.nsf_seen = s->nsf_seen;
+                result.pcm_modem_availability = s->pcm_modem_availability;
+                result.t66_seen = s->t66_seen;
+                s->result_handler(s->result_handler_user_data, &result);
+            }
+        }
+        break;
+    case V8_PARKED:
+        residual_samples = len;
+        break;
+    }
+    return residual_samples;
+}
+/*- End of function --------------------------------------------------------*/
+
+v8_state_t *v8_init(v8_state_t *s,
+                    int caller,
+                    int available_modulations,
+                    v8_result_handler_t *result_handler,
+                    void *user_data)
+{
+    if (s == NULL)
+    {
+        if ((s = (v8_state_t *) malloc(sizeof(*s))) == NULL)
+            return NULL;
+    }
+    memset(s, 0, sizeof(*s));
+    s->caller = caller;
+    s->available_modulations = available_modulations;
+    s->result_handler = result_handler;
+    s->result_handler_user_data = user_data;
+
+    s->ci_timer = 0;
+    if (s->caller)
+    {
+        s->state = V8_WAIT_1S;
+        s->negotiation_timer = ms_to_samples(1000);
+    }
+    else
+    {
+        s->state = V8_WAIT_200MS;
+        s->negotiation_timer = ms_to_samples(200);
+    }
+    if ((s->tx_queue = queue_init(NULL, 1024, 0)) == NULL)
+        return NULL;
+    return s;
+}
+/*- End of function --------------------------------------------------------*/
+
+int v8_release(v8_state_t *s)
+{
+    return queue_free(s->tx_queue);
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/vector_float.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/vector_float.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,346 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * vector_float.c - Floating vector arithmetic routines.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2006 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: vector_float.c,v 1.11 2008/07/02 14:48:26 steveu Exp $
+ */
+
+/*! \file */
+
+#if defined(HAVE_CONFIG_H)
+#include <config.h>
+#endif
+
+#include <inttypes.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include "floating_fudge.h"
+#if defined(HAVE_TGMATH_H)
+#include <tgmath.h>
+#endif
+#if defined(HAVE_MATH_H)
+#include <math.h>
+#endif
+#include <assert.h>
+
+#include "spandsp/telephony.h"
+#include "spandsp/vector_float.h"
+
+void vec_copyf(float z[], const float x[], int n)
+{
+    int i;
+    
+    for (i = 0;  i < n;  i++)
+        z[i] = x[i];
+}
+/*- End of function --------------------------------------------------------*/
+
+void vec_copy(double z[], const double x[], int n)
+{
+    int i;
+    
+    for (i = 0;  i < n;  i++)
+        z[i] = x[i];
+}
+/*- End of function --------------------------------------------------------*/
+
+#if defined(HAVE_LONG_DOUBLE)
+void vec_copyl(long double z[], const long double x[], int n)
+{
+    int i;
+    
+    for (i = 0;  i < n;  i++)
+        z[i] = x[i];
+}
+/*- End of function --------------------------------------------------------*/
+#endif
+
+void vec_zerof(float z[], int n)
+{
+    int i;
+    
+    for (i = 0;  i < n;  i++)
+        z[i] = 0.0f;
+}
+/*- End of function --------------------------------------------------------*/
+
+void vec_zero(double z[], int n)
+{
+    int i;
+    
+    for (i = 0;  i < n;  i++)
+        z[i] = 0.0;
+}
+/*- End of function --------------------------------------------------------*/
+
+#if defined(HAVE_LONG_DOUBLE)
+void vec_zerol(long double z[], int n)
+{
+    int i;
+    
+    for (i = 0;  i < n;  i++)
+        z[i] = 0.0;
+}
+/*- End of function --------------------------------------------------------*/
+#endif
+
+void vec_setf(float z[], float x, int n)
+{
+    int i;
+    
+    for (i = 0;  i < n;  i++)
+        z[i] = x;
+}
+/*- End of function --------------------------------------------------------*/
+
+void vec_set(double z[], double x, int n)
+{
+    int i;
+    
+    for (i = 0;  i < n;  i++)
+        z[i] = x;
+}
+/*- End of function --------------------------------------------------------*/
+
+#if defined(HAVE_LONG_DOUBLE)
+void vec_setl(long double z[], long double x, int n)
+{
+    int i;
+    
+    for (i = 0;  i < n;  i++)
+        z[i] = x;
+}
+/*- End of function --------------------------------------------------------*/
+#endif
+
+void vec_addf(float z[], const float x[], const float y[], int n)
+{
+    int i;
+
+    for (i = 0;  i < n;  i++)
+        z[i] = x[i] + y[i];
+}
+/*- End of function --------------------------------------------------------*/
+
+void vec_add(double z[], const double x[], const double y[], int n)
+{
+    int i;
+
+    for (i = 0;  i < n;  i++)
+        z[i] = x[i] + y[i];
+}
+/*- End of function --------------------------------------------------------*/
+
+void vec_addl(long double z[], const long double x[], const long double y[], int n)
+{
+    int i;
+
+    for (i = 0;  i < n;  i++)
+        z[i] = x[i] + y[i];
+}
+/*- End of function --------------------------------------------------------*/
+
+void vec_scaled_addf(float z[], const float x[], float x_scale, const float y[], float y_scale, int n)
+{
+    int i;
+
+    for (i = 0;  i < n;  i++)
+        z[i] = x[i]*x_scale + y[i]*y_scale;
+}
+/*- End of function --------------------------------------------------------*/
+
+void vec_scaled_add(double z[], const double x[], double x_scale, const double y[], double y_scale, int n)
+{
+    int i;
+
+    for (i = 0;  i < n;  i++)
+        z[i] = x[i]*x_scale + y[i]*y_scale;
+}
+/*- End of function --------------------------------------------------------*/
+
+#if defined(HAVE_LONG_DOUBLE)
+void vec_scaled_addl(long double z[], const long double x[], long double x_scale, const long double y[], long double y_scale, int n)
+{
+    int i;
+
+    for (i = 0;  i < n;  i++)
+        z[i] = x[i]*x_scale + y[i]*y_scale;
+}
+/*- End of function --------------------------------------------------------*/
+#endif
+
+void vec_subf(float z[], const float x[], const float y[], int n)
+{
+    int i;
+
+    for (i = 0;  i < n;  i++)
+        z[i] = x[i] - y[i];
+}
+/*- End of function --------------------------------------------------------*/
+
+void vec_sub(double z[], const double x[], const double y[], int n)
+{
+    int i;
+
+    for (i = 0;  i < n;  i++)
+        z[i] = x[i] - y[i];
+}
+/*- End of function --------------------------------------------------------*/
+
+#if defined(HAVE_LONG_DOUBLE)
+void vec_subl(long double z[], const long double x[], const long double y[], int n)
+{
+    int i;
+
+    for (i = 0;  i < n;  i++)
+        z[i] = x[i] - y[i];
+}
+/*- End of function --------------------------------------------------------*/
+#endif
+
+void vec_scaled_subf(float z[], const float x[], float x_scale, const float y[], float y_scale, int n)
+{
+    int i;
+
+    for (i = 0;  i < n;  i++)
+        z[i] = x[i]*x_scale - y[i]*y_scale;
+}
+/*- End of function --------------------------------------------------------*/
+
+void vec_scaled_sub(double z[], const double x[], double x_scale, const double y[], double y_scale, int n)
+{
+    int i;
+
+    for (i = 0;  i < n;  i++)
+        z[i] = x[i]*x_scale - y[i]*y_scale;
+}
+/*- End of function --------------------------------------------------------*/
+
+#if defined(HAVE_LONG_DOUBLE)
+void vec_scaled_subl(long double z[], const long double x[], long double x_scale, const long double y[], long double y_scale, int n)
+{
+    int i;
+
+    for (i = 0;  i < n;  i++)
+        z[i] = x[i]*x_scale - y[i]*y_scale;
+}
+/*- End of function --------------------------------------------------------*/
+#endif
+
+void vec_scalar_mulf(float z[], const float x[], float y, int n)
+{
+    int i;
+
+    for (i = 0;  i < n;  i++)
+        z[i] = x[i]*y;
+}
+/*- End of function --------------------------------------------------------*/
+
+void vec_scalar_mul(double z[], const double x[], double y, int n)
+{
+    int i;
+
+    for (i = 0;  i < n;  i++)
+        z[i] = x[i]*y;
+}
+/*- End of function --------------------------------------------------------*/
+
+#if defined(HAVE_LONG_DOUBLE)
+void vec_scalar_mull(long double z[], const long double x[], long double y, int n)
+{
+    int i;
+
+    for (i = 0;  i < n;  i++)
+        z[i] = x[i]*y;
+}
+/*- End of function --------------------------------------------------------*/
+#endif
+
+void vec_mulf(float z[], const float x[], const float y[], int n)
+{
+    int i;
+
+    for (i = 0;  i < n;  i++)
+        z[i] = x[i]*y[i];
+}
+/*- End of function --------------------------------------------------------*/
+
+void vec_mul(double z[], const double x[], const double y[], int n)
+{
+    int i;
+
+    for (i = 0;  i < n;  i++)
+        z[i] = x[i]*y[i];
+}
+/*- End of function --------------------------------------------------------*/
+
+#if defined(HAVE_LONG_DOUBLE)
+void vec_mull(long double z[], const long double x[], const long double y[], int n)
+{
+    int i;
+
+    for (i = 0;  i < n;  i++)
+        z[i] = x[i]*y[i];
+}
+/*- End of function --------------------------------------------------------*/
+#endif
+
+float vec_dot_prodf(const float x[], const float y[], int n)
+{
+    int i;
+    float z;
+
+    z = 0.0f;
+    for (i = 0;  i < n;  i++)
+        z += x[i]*y[i];
+    return z;
+}
+/*- End of function --------------------------------------------------------*/
+
+double vec_dot_prod(const double x[], const double y[], int n)
+{
+    int i;
+    double z;
+
+    z = 0.0;
+    for (i = 0;  i < n;  i++)
+        z += x[i]*y[i];
+    return z;
+}
+/*- End of function --------------------------------------------------------*/
+
+#if defined(HAVE_LONG_DOUBLE)
+long double vec_dot_prodl(const long double x[], const long double y[], int n)
+{
+    int i;
+    long double z;
+
+    z = 0.0;
+    for (i = 0;  i < n;  i++)
+        z += x[i]*y[i];
+    return z;
+}
+/*- End of function --------------------------------------------------------*/
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/src/vector_int.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/src/vector_int.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,348 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * vector_int.c - Integer vector arithmetic
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2006 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: vector_int.c,v 1.11 2008/07/02 14:48:26 steveu Exp $
+ */
+
+/*! \file */
+
+#if defined(HAVE_CONFIG_H)
+#include <config.h>
+#endif
+
+#include <inttypes.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include "floating_fudge.h"
+#if defined(HAVE_TGMATH_H)
+#include <tgmath.h>
+#endif
+#if defined(HAVE_MATH_H)
+#include <math.h>
+#endif
+#include <assert.h>
+
+#include "spandsp/telephony.h"
+#include "spandsp/vector_int.h"
+
+int32_t vec_dot_prodi16(const int16_t x[], const int16_t y[], int n)
+{
+    int32_t z;
+
+#if defined(__GNUC__)  &&  defined(__i386__)
+    __asm__ __volatile__(
+        " emms;\n"
+        " pxor %%mm0,%%mm0;\n"
+        " leal -32(%%esi,%%eax,2),%%edx;\n"     /* edx = top - 32 */
+
+        " cmpl %%edx,%%esi;\n"
+        " ja 1f;\n"
+
+        /* Work in blocks of 16 int16_t's until we are near the end */
+        " .p2align 2;\n"
+        "2:\n"
+        " movq (%%edi),%%mm1;\n"
+        " movq (%%esi),%%mm2;\n"
+        " pmaddwd %%mm2,%%mm1;\n"
+        " paddd %%mm1,%%mm0;\n"
+        " movq 8(%%edi),%%mm1;\n"
+        " movq 8(%%esi),%%mm2;\n"
+        " pmaddwd %%mm2,%%mm1;\n"
+        " paddd %%mm1,%%mm0;\n"
+        " movq 16(%%edi),%%mm1;\n"
+        " movq 16(%%esi),%%mm2;\n"
+        " pmaddwd %%mm2,%%mm1;\n"
+        " paddd %%mm1,%%mm0;\n"
+        " movq 24(%%edi),%%mm1;\n"
+        " movq 24(%%esi),%%mm2;\n"
+        " pmaddwd %%mm2,%%mm1;\n"
+        " paddd %%mm1,%%mm0;\n"
+
+        " addl $32,%%esi;\n"
+        " addl $32,%%edi;\n"
+        " cmpl %%edx,%%esi;\n"
+        " jbe 2b;\n"
+
+        " .p2align 2;\n"
+        "1:\n"
+        " addl $24,%%edx;\n"                  /* now edx = top - 8 */
+        " cmpl %%edx,%%esi;\n"
+        " ja 3f;\n"
+
+        /* Work in blocks of 4 int16_t's until we are near the end */
+        " .p2align 2;\n"
+        "4:\n"
+        " movq (%%edi),%%mm1;\n"
+        " movq (%%esi),%%mm2;\n"
+        " pmaddwd %%mm2,%%mm1;\n"
+        " paddd %%mm1,%%mm0;\n"
+
+        " addl $8,%%esi;\n"
+        " addl $8,%%edi;\n"
+        " cmpl %%edx,%%esi;"
+        " jbe 4b;\n"
+
+        " .p2align 2;\n"
+        "3:\n"
+        " addl $4,%%edx;\n"                  /* now edx = top - 4 */
+        " cmpl %%edx,%%esi;\n"
+        " ja 5f;\n"
+
+        /* Work in a block of 2 int16_t's */
+        " movd (%%edi),%%mm1;\n"
+        " movd (%%esi),%%mm2;\n"
+        " pmaddwd %%mm2,%%mm1;\n"
+        " paddd %%mm1,%%mm0;\n"
+
+        " addl $4,%%esi;\n"
+        " addl $4,%%edi;\n"
+
+        " .p2align 2;\n"
+        "5:\n"
+        " addl $2,%%edx;\n"                  /* now edx = top - 2 */
+        " cmpl %%edx,%%esi;\n"
+        " ja 6f;\n"
+
+        /* Deal with the very last int16_t, when n is odd */
+        " movswl (%%edi),%%eax;\n"
+        " andl $65535,%%eax;\n"
+        " movd %%eax,%%mm1;\n"
+        " movswl (%%esi),%%eax;\n"
+        " andl $65535,%%eax;\n"
+        " movd %%eax,%%mm2;\n"
+        " pmaddwd %%mm2,%%mm1;\n"
+        " paddd %%mm1,%%mm0;\n"
+
+        " .p2align 2;\n"
+        "6:\n"
+        /* Merge the pieces of the answer */
+        " movq %%mm0,%%mm1;\n"
+        " punpckhdq %%mm0,%%mm1;\n"
+        " paddd %%mm1,%%mm0;\n"
+        /* et voila, eax has the final result */
+        " movd %%mm0,%%eax;\n"
+
+        " emms;\n"
+        : "=a" (z)
+        : "S" (x), "D" (y), "a" (n)
+        : "cc"
+    );
+#else
+    int i;
+
+    z = 0;
+    for (i = 0;  i < n;  i++)
+        z += (int32_t) x[i]*(int32_t) y[i];
+#endif
+    return  z;
+}
+/*- End of function --------------------------------------------------------*/
+
+int32_t vec_min_maxi16(const int16_t x[], int n, int16_t out[])
+{
+#if defined(__GNUC__)  &&  defined(__i386__)
+    static const int32_t lower_bound = 0x80008000;
+    static const int32_t upper_bound = 0x7FFF7FFF;
+    int32_t max;
+
+    __asm__ __volatile__(
+        " emms;\n"
+        " pushl %%edx;\n"
+        " leal -8(%%esi,%%eax,2),%%edx;\n"
+
+        " cmpl %%edx,%%esi;\n"
+        " jbe 2f;\n"
+        " movd %[lower],%%mm0;\n"
+        " movd %[upper],%%mm1;\n"
+        " jmp 1f;\n"
+
+        " .p2align 2;\n"
+        "2:\n"
+        " movq (%%esi),%%mm0;\n"   /* mm0 will be max's */
+        " movq %%mm0,%%mm1;\n"     /* mm1 will be min's */
+        " addl $8,%%esi;\n"
+        " cmpl %%edx,%%esi;\n"
+        " ja 4f;\n"
+
+        " .p2align 2;\n"
+        "3:\n"
+        " movq (%%esi),%%mm2;\n"
+
+        " movq %%mm2,%%mm3;\n"
+        " pcmpgtw %%mm0,%%mm3;\n"  /* mm3 is bitmask for words where mm2 > mm0 */ 
+        " movq %%mm3,%%mm4;\n"
+        " pand %%mm2,%%mm3;\n"     /* mm3 is mm2 masked to new max's */
+        " pandn %%mm0,%%mm4;\n"    /* mm4 is mm0 masked to its max's */
+        " por %%mm3,%%mm4;\n"
+        " movq %%mm4,%%mm0;\n"     /* Now mm0 is updated max's */
+        
+        " movq %%mm1,%%mm3;\n"
+        " pcmpgtw %%mm2,%%mm3;\n"  /* mm3 is bitmask for words where mm2 < mm1 */ 
+        " pand %%mm3,%%mm2;\n"     /* mm2 is mm2 masked to new min's */
+        " pandn %%mm1,%%mm3;\n"    /* mm3 is mm1 masked to its min's */
+        " por %%mm3,%%mm2;\n"
+        " movq %%mm2,%%mm1;\n"     /* now mm1 is updated min's */
+
+        " addl $8,%%esi;\n"
+        " cmpl %%edx,%%esi;\n"
+        " jbe 3b;\n"
+
+        " .p2align 2;\n"
+        "4:\n"
+        /* Merge down the 4-word max/mins to lower 2 words */
+        " movq %%mm0,%%mm2;\n"
+        " psrlq $32,%%mm2;\n"
+        " movq %%mm2,%%mm3;\n"
+        " pcmpgtw %%mm0,%%mm3;\n"  /* mm3 is bitmask for words where mm2 > mm0 */ 
+        " pand %%mm3,%%mm2;\n"     /* mm2 is mm2 masked to new max's */
+        " pandn %%mm0,%%mm3;\n"    /* mm3 is mm0 masked to its max's */
+        " por %%mm3,%%mm2;\n"
+        " movq %%mm2,%%mm0;\n"     /* now mm0 is updated max's */
+
+        " movq %%mm1,%%mm2;\n"
+        " psrlq $32,%%mm2;\n"
+        " movq %%mm1,%%mm3;\n"
+        " pcmpgtw %%mm2,%%mm3;\n"  /* mm3 is bitmask for words where mm2 < mm1 */ 
+        " pand %%mm3,%%mm2;\n"     /* mm2 is mm2 masked to new min's */
+        " pandn %%mm1,%%mm3;\n"    /* mm3 is mm1 masked to its min's */
+        " por %%mm3,%%mm2;\n"
+        " movq %%mm2,%%mm1;\n"     /* now mm1 is updated min's */
+
+        " .p2align 2;\n"
+        "1:\n"
+        " addl $4,%%edx;\n"        /* now dx = top-4 */
+        " cmpl %%edx,%%esi;\n"
+        " ja 5f;\n"
+        /* Here, there are >= 2 words of input remaining */
+        " movd (%%esi),%%mm2;\n"
+
+        " movq %%mm2,%%mm3;\n"
+        " pcmpgtw %%mm0,%%mm3;\n"  /* mm3 is bitmask for words where mm2 > mm0 */ 
+        " movq %%mm3,%%mm4;\n"
+        " pand %%mm2,%%mm3;\n"     /* mm3 is mm2 masked to new max's */
+        " pandn %%mm0,%%mm4;\n"    /* mm4 is mm0 masked to its max's */
+        " por %%mm3,%%mm4;\n"
+        " movq %%mm4,%%mm0;\n"     /* now mm0 is updated max's */
+
+        " movq %%mm1,%%mm3;\n"
+        " pcmpgtw %%mm2,%%mm3;\n"  /* mm3 is bitmask for words where mm2 < mm1 */ 
+        " pand %%mm3,%%mm2;\n"     /* mm2 is mm2 masked to new min's */
+        " pandn %%mm1,%%mm3;\n"    /* mm3 is mm1 masked to its min's */
+        " por %%mm3,%%mm2;\n"
+        " movq %%mm2,%%mm1;\n"     /* now mm1 is updated min's */
+
+        " addl $4,%%esi;\n"
+
+        " .p2align 2;\n"
+        "5:\n"
+        /* Merge down the 2-word max/mins to 1 word */
+        " movq %%mm0,%%mm2;\n"
+        " psrlq $16,%%mm2;\n"
+        " movq %%mm2,%%mm3;\n"
+        " pcmpgtw %%mm0,%%mm3;\n"  /* mm3 is bitmask for words where mm2 > mm0 */ 
+        " pand %%mm3,%%mm2;\n"     /* mm2 is mm2 masked to new max's */
+        " pandn %%mm0,%%mm3;\n"    /* mm3 is mm0 masked to its max's */
+        " por %%mm3,%%mm2;\n"
+        " movd %%mm2,%%ecx;\n"     /* cx is max so far */
+
+        " movq %%mm1,%%mm2;\n"
+        " psrlq $16,%%mm2;\n"
+        " movq %%mm1,%%mm3;\n"
+        " pcmpgtw %%mm2,%%mm3;\n"  /* mm3 is bitmask for words where mm2 < mm1 */ 
+        " pand %%mm3,%%mm2;\n"     /* mm2 is mm2 masked to new min's */
+        " pandn %%mm1,%%mm3;\n"    /* mm3 is mm1 masked to its min's */
+        " por %%mm3,%%mm2;\n"
+        " movd %%mm2,%%eax;\n"     /* ax is min so far */
+        
+        " addl $2,%%edx;\n"        /* now dx = top-2 */
+        " cmpl %%edx,%%esi;\n"
+        " ja 6f;\n"
+
+        /* Here, there is one word of input left */
+        " cmpw (%%esi),%%cx;\n"
+        " jge 9f;\n"
+        " movw (%%esi),%%cx;\n"
+        " .p2align 2;\n"
+        "9:\n"
+        " cmpw (%%esi),%%ax;\n"
+        " jle 6f;\n"
+        " movw (%%esi),%%ax;\n"
+
+        " .p2align 2;\n"
+        "6:\n"
+        /* (finally!) cx is the max, ax the min */
+        " movswl %%cx,%%ecx;\n"
+        " movswl %%ax,%%eax;\n"
+
+        " popl %%edx;\n"            /* ptr to output max,min vals */
+        " andl %%edx,%%edx;\n"
+        " jz 7f;\n"
+        " movw %%cx,(%%edx);\n"    /* max */
+        " movw %%ax,2(%%edx);\n"   /* min */
+        " .p2align 2;\n"
+        "7:\n"
+        /* Now calculate max absolute value */
+        " negl %%eax;\n"
+        " cmpl %%ecx,%%eax;\n"
+        " jge 8f;\n"
+        " movl %%ecx,%%eax;\n"
+        " .p2align 2;\n"
+        "8:\n"
+        " emms;\n"
+        : "=a" (max)
+        : "S" (x), "a" (n), "d" (out), [lower] "m" (lower_bound), [upper] "m" (upper_bound)
+        : "ecx"
+    );
+    return  max;
+#else
+    int i;
+    int16_t min;
+    int16_t max;
+    int16_t temp;
+    int32_t z;
+    
+    max = INT16_MIN;
+    min = INT16_MAX;
+    for (i = 0;  i < n;  i++)
+    {
+        temp = x[i];
+        if (temp > max)
+            max = temp;
+        /*endif*/
+        if (temp < min)
+            min = temp;
+        /*endif*/
+    }
+    /*endfor*/
+    out[0] = max;
+    out[1] = min;
+    z = abs(min);
+    if (z > max)
+        return z;
+    return max;
+#endif
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/test-data/Makefile.am
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/test-data/Makefile.am	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,28 @@
+##
+## SpanDSP - a series of DSP components for telephony
+##
+## Makefile.am - Process this file with automake to produce Makefile.in
+##
+## This program is free software; you can redistribute it and/or modify
+## it under the terms of the GNU Lesser General Public License version 2.1,
+## as published by the Free Software Foundation.
+##
+## This program is distributed in the hope that it will be useful,
+## but WITHOUT ANY WARRANTY; without even the implied warranty of
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+## GNU Lesser General Public License for more details.
+##
+## You should have received a copy of the GNU Lesser General Public
+## License along with this program; if not, write to the Free Software
+## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+##
+## $Id: Makefile.am,v 1.3 2008/07/10 12:44:54 steveu Exp $
+
+SUBDIRS = itu etsi
+
+DIST_SUBDIRS = itu etsi local
+
+all: 
+
+clean:
+

Added: freeswitch/trunk/libs/spandsp/test-data/etsi/Makefile.am
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/test-data/etsi/Makefile.am	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,28 @@
+##
+## SpanDSP - a series of DSP components for telephony
+##
+## Makefile.am -- Process this file with automake to produce Makefile.in
+##
+## This program is free software; you can redistribute it and/or modify
+## it under the terms of the GNU Lesser General Public License version 2.1,
+## as published by the Free Software Foundation.
+##
+## This program is distributed in the hope that it will be useful,
+## but WITHOUT ANY WARRANTY; without even the implied warranty of
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+## GNU Lesser General Public License for more details.
+##
+## You should have received a copy of the GNU Lesser General Public
+## License along with this program; if not, write to the Free Software
+## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+##
+## $Id: Makefile.am,v 1.4 2008/07/10 12:44:54 steveu Exp $
+
+SUBDIRS = fax
+
+DIST_SUBDIRS = fax
+
+all: 
+
+clean:
+

Added: freeswitch/trunk/libs/spandsp/test-data/etsi/fax/Makefile.am
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/test-data/etsi/fax/Makefile.am	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,55 @@
+##
+## SpanDSP - a series of DSP components for telephony
+##
+## Makefile.am -- Process this file with automake to produce Makefile.in
+##
+## This program is free software; you can redistribute it and/or modify
+## it under the terms of the GNU General Public License version 2, as
+## published by the Free Software Foundation.
+##
+## This program is distributed in the hope that it will be useful,
+## but WITHOUT ANY WARRANTY; without even the implied warranty of
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+## GNU General Public License for more details.
+##
+## You should have received a copy of the GNU General Public License
+## along with this program; if not, write to the Free Software
+## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+##
+## $Id: Makefile.am,v 1.1 2008/07/10 12:44:54 steveu Exp $
+
+AM_CFLAGS = $(COMP_VENDOR_CFLAGS)
+
+PBM2G3 = pbmtog3
+FAX2TIFF = fax2tiff
+
+EXTRA_DIST = 
+
+nobase_data_DATA = etsi_300_242_white.tif \
+                   etsi_300_242_stairstep.tif \
+                   etsi_300_242_diago1.tif \
+                   etsi_300_242_diago2.tif \
+                   etsi_300_242_impress.tif \
+                   etsi_300_242_error.tif
+
+noinst_PROGRAMS =   generate_etsi_300_242_pages
+
+generate_etsi_300_242_pages_SOURCES = generate_etsi_300_242_pages.c
+generate_etsi_300_242_pages_LDADD = -ltiff
+
+clean:
+	rm -f *.tif *.g3
+
+.g3.tif:
+	${FAX2TIFF} -M -o $*.tif $*.g3
+
+.pbm.g3:
+	${PBM2G3} $*.pbm >$*.g3
+
+etsi_300_242_white.tif \
+etsi_300_242_stairstep.tif \
+etsi_300_242_diago1.tif \
+etsi_300_242_diago2.tif \
+etsi_300_242_impress.tif \
+etsi_300_242_error.tif: generate_etsi_300_242_pages$(EXEEXT)
+	./generate_etsi_300_242_pages$(EXEEXT)

Added: freeswitch/trunk/libs/spandsp/test-data/etsi/fax/generate_etsi_300_242_pages.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/test-data/etsi/fax/generate_etsi_300_242_pages.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,611 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * generate_etsi_300_242_pages.c - Create the test pages defined in ETSI ETS 300 242.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2006 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: generate_etsi_300_242_pages.c,v 1.1 2008/07/10 12:44:54 steveu Exp $
+ */
+
+/*! \file */
+
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
+
+#include <stdio.h>
+#include <inttypes.h>
+#include <limits.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <stdlib.h>
+#include <time.h>
+#include <memory.h>
+#include <string.h>
+#if defined(HAVE_TGMATH_H)
+#include <tgmath.h>
+#endif
+#if defined(HAVE_MATH_H)
+#include <math.h>
+#endif
+
+#include "spandsp.h"
+
+struct
+{
+    const char *name;
+    int x_res;
+    int y_res;
+    int width;
+    int length;
+    int compression;
+    int type;
+} sequence[] =
+{
+    {
+        "etsi_300_242_white.tif",
+        T4_X_RESOLUTION_R8,
+        T4_Y_RESOLUTION_STANDARD,
+        T4_WIDTH_R8_A4,
+        1100,
+        COMPRESSION_CCITT_T4,
+        0
+    },
+    {
+        "etsi_300_242_stairstep.tif",
+        T4_X_RESOLUTION_R8,
+        T4_Y_RESOLUTION_STANDARD,
+        T4_WIDTH_R8_A4,
+        1100,
+        COMPRESSION_CCITT_T4,
+        1
+    },
+    {
+        "etsi_300_242_diago1.tif",
+        T4_X_RESOLUTION_R8,
+        T4_Y_RESOLUTION_STANDARD,
+        T4_WIDTH_R8_A4,
+        1100,
+        COMPRESSION_CCITT_T4,
+        2
+    },
+    {
+        "etsi_300_242_diago2.tif",
+        T4_X_RESOLUTION_R8,
+        T4_Y_RESOLUTION_STANDARD,
+        T4_WIDTH_R8_A4,
+        1100,
+        COMPRESSION_CCITT_T4,
+        3
+    },
+    {
+        "etsi_300_242_impress.tif",
+        T4_X_RESOLUTION_R8,
+        T4_Y_RESOLUTION_STANDARD,
+        T4_WIDTH_R8_A4,
+        1100,
+        COMPRESSION_CCITT_T4,
+        4
+    },
+    {
+        "etsi_300_242_duration1.tif",
+        T4_X_RESOLUTION_R8,
+        T4_Y_RESOLUTION_STANDARD,
+        T4_WIDTH_R8_A4,
+        1100,
+        COMPRESSION_CCITT_T4,
+        5
+    },
+    {
+        "etsi_300_242_duration2.tif",
+        T4_X_RESOLUTION_R8,
+        T4_Y_RESOLUTION_STANDARD,
+        T4_WIDTH_R8_A4,
+        1100,
+        COMPRESSION_CCITT_T4,
+        6
+    },
+    {
+        "etsi_300_242_error.tif",
+        T4_X_RESOLUTION_R8,
+        T4_Y_RESOLUTION_STANDARD,
+        T4_WIDTH_R8_A4,
+        1100,
+        COMPRESSION_CCITT_T4,
+        7
+    },
+    {
+        NULL,
+        0,
+        0,
+        0,
+        0
+    },
+};
+
+static void clear_row(uint8_t buf[], int width)
+{
+    memset(buf, 0, width/8 + 1);
+}
+/*- End of function --------------------------------------------------------*/
+
+static void set_row(uint8_t buf[], int width)
+{
+    memset(buf, 0xFF, width/8 + 1);
+}
+/*- End of function --------------------------------------------------------*/
+
+static void set_pixel(uint8_t buf[], int row, int pixel)
+{
+    row--;
+    buf[row*1728/8 + pixel/8] |= (0x80 >> (pixel & 0x07));
+}
+/*- End of function --------------------------------------------------------*/
+
+static void clear_pixel(uint8_t buf[], int row, int pixel)
+{
+    row--;
+    buf[row*1728/8 + pixel/8] &= ~(0x80 >> (pixel & 0x07));
+}
+/*- End of function --------------------------------------------------------*/
+
+static void set_pixel_range(uint8_t buf[], int row, int start, int end)
+{
+    int i;
+
+    for (i = start;  i <= end;  i++)
+        set_pixel(buf, row, i);
+}
+/*- End of function --------------------------------------------------------*/
+
+static void clear_pixel_range(uint8_t buf[], int row, int start, int end)
+{
+    int i;
+    
+    for (i = start;  i <= end;  i++)
+        clear_pixel(buf, row, i);
+}
+/*- End of function --------------------------------------------------------*/
+
+static int create_white_page(TIFF *tiff_file)
+{
+    uint8_t image_buffer[8192];
+    int row;
+
+    /* TSB-85 WHITE page. */
+    for (row = 0;  row < 1100;  row++)
+    {
+        clear_row(image_buffer, 1728);
+        if (TIFFWriteScanline(tiff_file, image_buffer, row, 0) < 0)
+        {
+            printf("Write error at row %d.\n", row);
+            exit(2);
+        }
+    }
+    return 1100;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int create_stairstep_page(TIFF *tiff_file)
+{
+    uint8_t image_buffer[8192];
+    int row;
+    int start_pixel;
+
+    /* TSB-85 STAIRSTEP page. */
+    start_pixel = 0;
+    for (row = 0;  row < 1728;  row++)
+    {
+        clear_row(image_buffer, 1728);
+        set_pixel_range(image_buffer, 1, start_pixel, start_pixel + 63);
+        if (TIFFWriteScanline(tiff_file, image_buffer, row, 0) < 0)
+        {
+            printf("Write error at row %d.\n", row);
+            exit(2);
+        }
+        start_pixel += 64;
+        if (start_pixel >= 1728)
+            start_pixel = 0;
+    }
+    return 1728;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int create_diago1_page(TIFF *tiff_file)
+{
+    uint8_t image_buffer[1728/8];
+    int row;
+
+    /* ETSI ETS 300 242 B.5.1 One dimensional coding test chart - the DIAGO1 page. */
+    for (row = 0;  row < 1001;  row++)
+    {
+        clear_row(image_buffer, 1728);
+        set_pixel_range(image_buffer, 1, row, 1727);
+        if (TIFFWriteScanline(tiff_file, image_buffer, row, 0) < 0)
+        {
+            printf("Write error at row %d.\n", row);
+            exit(2);
+        }
+    }
+    clear_row(image_buffer, 1728);
+    set_pixel_range(image_buffer, 1, 0, 1727);
+    if (TIFFWriteScanline(tiff_file, image_buffer, row, 0) < 0)
+    {
+        printf("Write error at row %d.\n", row);
+        exit(2);
+    }
+    return 1002;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int create_diago2_page(TIFF *tiff_file)
+{
+    uint8_t image_buffer[1728/8];
+    int row;
+
+    /* ETSI ETS 300 242 B.5.1 One dimensional coding test chart - the DIAGO2 page. */
+    for (row = 0;  row < 1001;  row++)
+    {
+        clear_row(image_buffer, 1728);
+        set_pixel_range(image_buffer, 1, row + 728, 1727);
+        if (TIFFWriteScanline(tiff_file, image_buffer, row, 0) < 0)
+        {
+            printf("Write error at row %d.\n", row);
+            exit(2);
+        }
+    }
+    clear_row(image_buffer, 1728);
+    set_pixel_range(image_buffer, 1, 0, 1727);
+    if (TIFFWriteScanline(tiff_file, image_buffer, row, 0) < 0)
+    {
+        printf("Write error at row %d.\n", row);
+        exit(2);
+    }
+    return 1002;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int create_impress_page(TIFF *tiff_file)
+{
+    int j;
+    int row;
+    uint8_t *page;
+
+    /* ETSI ETS 300 242 B.5.2 Printing resolution - the IMPRESS page */
+    if ((page = malloc(1079*1728/8)) == NULL)
+        return 0;
+    memset(page, 0, 1079*1728/8);
+
+    set_pixel_range(page, 1, 0, 1727);
+    for (row = 2;  row <= 78;  row++)
+    {
+        set_pixel_range(page, row, 850, 850 + 27);
+        set_pixel_range(page, row, 850 + 27 + 745, 850 + 27 + 745 + 26);
+    }
+    for (row = 80;  row <= 117;  row++)
+    {
+        for (j = 0;  j < 1728;  j += 2)
+            set_pixel(page, row, j);
+    }
+    for (row = 118;  row <= 155;  row++)
+    {
+        for (j = 1;  j < 1728;  j += 2)
+            set_pixel(page, row, j);
+    }
+    for (row = 194;  row <= 231;  row += 2)
+        set_pixel_range(page, row, 0, 1727);
+    for (row = 270;  row <= 276;  row++)
+        set_pixel_range(page, row, 60, 60 + 1607);
+    for (j = 0;  j < 1728;  j += 27)
+        set_pixel(page, 315, j);
+    for (row = 354;  row <= 480;  row++)
+        set_pixel_range(page, row, 209, 768);
+    for (row = 358;  row <= 476;  row++)
+        clear_pixel_range(page, row, 488, 489);
+    clear_pixel_range(page, 417, 217, 760);
+
+    for (row = 354;  row <= 357;  row++)
+        set_pixel_range(page, row, 962, 1521);
+    for (row = 477;  row <= 480;  row++)
+        set_pixel_range(page, row, 962, 1521);
+    for (row = 358;  row <= 476;  row++)
+        set_pixel_range(page, row, 962, 969);
+    for (row = 358;  row <= 476;  row++)
+        set_pixel_range(page, row, 1514, 1521);
+    for (row = 358;  row <= 476;  row++)
+        set_pixel(page, row, 1241);
+    set_pixel_range(page, 417, 970, 1513);
+
+    for (row = 354;  row <= 1079;  row++)
+        set_pixel(page, row, 864);
+    for (row = 157;  row <= 926;  row++)
+        set_pixel_range(page, row, 884, 899);
+    for (row = 0;  row < 1079;  row++)
+    {
+        if (TIFFWriteScanline(tiff_file, page + row*1728/8, row, 0) < 0)
+        {
+            printf("Write error at row %d.\n", row);
+            exit(2);
+        }
+    }
+    free(page);
+    return 1079;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int create_duration1_page(TIFF *tiff_file)
+{
+    uint8_t image_buffer[1728/8];
+    int row;
+    int i;
+
+    /* ETSI ETS 300 242 B.5.3 Acceptance of total coded scan line duration - the DURATION1 page */
+    row = 0;
+    clear_row(image_buffer, 1728);
+    set_pixel_range(image_buffer, 1, 0, 1727);
+    if (TIFFWriteScanline(tiff_file, image_buffer, row++, 0) < 0)
+    {
+        printf("Write error at row %d.\n", row);
+        exit(2);
+    }
+    for (  ;  row < 117;  row++)
+    {
+        clear_row(image_buffer, 1728);
+        if (TIFFWriteScanline(tiff_file, image_buffer, row, 0) < 0)
+        {
+            printf("Write error at row %d.\n", row);
+            exit(2);
+        }
+    }
+    clear_row(image_buffer, 1728);
+    set_pixel_range(image_buffer, 1, 0, 1727);
+    if (TIFFWriteScanline(tiff_file, image_buffer, row++, 0) < 0)
+    {
+        printf("Write error at row %d.\n", row);
+        exit(2);
+    }
+    clear_row(image_buffer, 1728);
+    for (i = 1;  i < 1728;  i += 2)
+        set_pixel(image_buffer, 1, i);
+    if (TIFFWriteScanline(tiff_file, image_buffer, row++, 0) < 0)
+    {
+        printf("Write error at row %d.\n", row);
+        exit(2);
+    }
+    clear_row(image_buffer, 1728);
+    set_pixel_range(image_buffer, 1, 0, 1727);
+    if (TIFFWriteScanline(tiff_file, image_buffer, row++, 0) < 0)
+    {
+        printf("Write error at row %d.\n", row);
+        exit(2);
+    }
+    for (  ;  row < 236;  row++)
+    {
+        clear_row(image_buffer, 1728);
+        if (TIFFWriteScanline(tiff_file, image_buffer, row, 0) < 0)
+        {
+            printf("Write error at row %d.\n", row);
+            exit(2);
+        }
+    }
+    clear_row(image_buffer, 1728);
+    set_pixel_range(image_buffer, 1, 0, 1727);
+    if (TIFFWriteScanline(tiff_file, image_buffer, row, 0) < 0)
+    {
+        printf("Write error at row %d.\n", row);
+        exit(2);
+    }
+    return 237;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int create_duration2_page(TIFF *tiff_file)
+{
+    return create_duration1_page(tiff_file);
+}
+/*- End of function --------------------------------------------------------*/
+
+static int create_error_page(TIFF *tiff_file)
+{
+    uint8_t image_buffer[1728/8];
+    int row;
+    int start_pixel;
+    int i;
+
+    /* ETSI ETS 300 242 B.5.4 Copy quality criteria - the ERROR page. */
+    for (row = 0;  row < 68;  row++)
+    {
+        clear_row(image_buffer, 1728);
+        if (TIFFWriteScanline(tiff_file, image_buffer, row, 0) < 0)
+        {
+            printf("Write error at row %d.\n", row);
+            exit(2);
+        }
+    }
+
+    clear_row(image_buffer, 1728);
+    set_pixel_range(image_buffer, 1, 0, 1727);
+    if (TIFFWriteScanline(tiff_file, image_buffer, row++, 0) < 0)
+    {
+        printf("Write error at row %d.\n", row);
+        exit(2);
+    }
+
+    clear_row(image_buffer, 1728);
+    if (TIFFWriteScanline(tiff_file, image_buffer, row++, 0) < 0)
+    {
+        printf("Write error at row %d.\n", row);
+        exit(2);
+    }
+
+    for (i = 0;  i < 10;  i++)
+    {
+        for (start_pixel = 16;  start_pixel <= 1616;  start_pixel += 64)
+        {
+            clear_row(image_buffer, 1728);
+            set_pixel_range(image_buffer, 1, start_pixel, start_pixel + 63);
+            if (TIFFWriteScanline(tiff_file, image_buffer, row, 0) < 0)
+            {
+                printf("Write error at row %d.\n", row);
+                exit(2);
+            }
+            row++;
+        }
+    }
+
+    clear_row(image_buffer, 1728);
+    if (TIFFWriteScanline(tiff_file, image_buffer, row++, 0) < 0)
+    {
+        printf("Write error at row %d.\n", row);
+        exit(2);
+    }
+
+    clear_row(image_buffer, 1728);
+    set_pixel_range(image_buffer, 1, 0, 1727);
+    if (TIFFWriteScanline(tiff_file, image_buffer, row++, 0) < 0)
+    {
+        printf("Write error at row %d.\n", row);
+        exit(2);
+    }
+
+    for (row = 332;  row < 400;  row++)
+    {
+        clear_row(image_buffer, 1728);
+        if (TIFFWriteScanline(tiff_file, image_buffer, row, 0) < 0)
+        {
+            printf("Write error at row %d.\n", row);
+            exit(2);
+        }
+    }
+
+    return 400;
+}
+/*- End of function --------------------------------------------------------*/
+
+int main(int argc, char *argv[])
+{
+    int row;
+    TIFF *tiff_file;
+    struct tm *tm;
+    time_t now;
+    char buf[133];
+    float x_resolution;
+    float y_resolution;
+    int i;
+    int image_length;
+
+    for (i = 0;  sequence[i].name;  i++)
+    {
+        if ((tiff_file = TIFFOpen(sequence[i].name, "w")) == NULL)
+            exit(2);
+
+        /* Prepare the directory entry fully before writing the image, or libtiff complains */
+        TIFFSetField(tiff_file, TIFFTAG_COMPRESSION, sequence[i].compression);
+        if (sequence[i].compression == COMPRESSION_CCITT_T4)
+        {
+            TIFFSetField(tiff_file, TIFFTAG_T4OPTIONS, GROUP3OPT_FILLBITS); // | GROUP3OPT_2DENCODING);
+            TIFFSetField(tiff_file, TIFFTAG_FAXMODE, FAXMODE_CLASSF);
+        }
+        TIFFSetField(tiff_file, TIFFTAG_IMAGEWIDTH, sequence[i].width);
+        TIFFSetField(tiff_file, TIFFTAG_BITSPERSAMPLE, 1);
+        TIFFSetField(tiff_file, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
+        TIFFSetField(tiff_file, TIFFTAG_SAMPLESPERPIXEL, 1);
+        TIFFSetField(tiff_file, TIFFTAG_ROWSPERSTRIP, -1L);
+        TIFFSetField(tiff_file, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
+        TIFFSetField(tiff_file, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISWHITE);
+        TIFFSetField(tiff_file, TIFFTAG_FILLORDER, FILLORDER_LSB2MSB);
+    
+        x_resolution = sequence[i].x_res/100.0f;
+        y_resolution = sequence[i].y_res/100.0f;
+        TIFFSetField(tiff_file, TIFFTAG_XRESOLUTION, floorf(x_resolution*2.54f + 0.5f));
+        TIFFSetField(tiff_file, TIFFTAG_YRESOLUTION, floorf(y_resolution*2.54f + 0.5f));
+        TIFFSetField(tiff_file, TIFFTAG_RESOLUTIONUNIT, RESUNIT_INCH);
+    
+        TIFFSetField(tiff_file, TIFFTAG_SOFTWARE, "spandsp");
+        if (gethostname(buf, sizeof(buf)) == 0)
+            TIFFSetField(tiff_file, TIFFTAG_HOSTCOMPUTER, buf);
+    
+        TIFFSetField(tiff_file, TIFFTAG_IMAGEDESCRIPTION, "Blank test image");
+        TIFFSetField(tiff_file, TIFFTAG_MAKE, "soft-switch.org");
+        TIFFSetField(tiff_file, TIFFTAG_MODEL, "test data");
+
+        time(&now);
+        tm = localtime(&now);
+        sprintf(buf,
+    	        "%4d/%02d/%02d %02d:%02d:%02d",
+                tm->tm_year + 1900,
+                tm->tm_mon + 1,
+                tm->tm_mday,
+                tm->tm_hour,
+                tm->tm_min,
+                tm->tm_sec);
+        TIFFSetField(tiff_file, TIFFTAG_DATETIME, buf);
+        image_length = sequence[i].length;
+        
+        /* Write the image first.... */
+        row = 0;
+        switch (sequence[i].type)
+        {
+        case 0:
+            /* A white A4 page */
+            image_length = create_white_page(tiff_file);
+            break;
+        case 1:
+            /* A stairstep of 64 pixel dashes */
+            image_length = create_stairstep_page(tiff_file);
+            break;
+        case 2:
+            /* The DIAGO1 page */
+            image_length = create_diago1_page(tiff_file);
+            break;
+        case 3:
+            /* The DIAGO2 page */
+            image_length = create_diago2_page(tiff_file);
+            break;
+        case 4:
+            /* The IMPRESS page */
+            image_length = create_impress_page(tiff_file);
+            break;
+        case 5:
+            /* The DURATION1 page */
+            image_length = create_duration1_page(tiff_file);
+            break;
+        case 6:
+            /* The DURATION2 page */
+            image_length = create_duration2_page(tiff_file);
+            break;
+        case 7:
+            /* The ERROR page */
+            image_length = create_error_page(tiff_file);
+            break;
+        }
+        /* ....then the directory entry, and libtiff is happy. */
+        TIFFSetField(tiff_file, TIFFTAG_IMAGELENGTH, image_length);
+        TIFFSetField(tiff_file, TIFFTAG_PAGENUMBER, 0, 1);
+        TIFFSetField(tiff_file, TIFFTAG_CLEANFAXDATA, CLEANFAXDATA_CLEAN);
+
+        TIFFWriteDirectory(tiff_file);
+        TIFFClose(tiff_file);
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/test-data/itu/Makefile.am
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/test-data/itu/Makefile.am	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,28 @@
+##
+## SpanDSP - a series of DSP components for telephony
+##
+## Makefile.am - Process this file with automake to produce Makefile.in
+##
+## This program is free software; you can redistribute it and/or modify
+## it under the terms of the GNU Lesser General Public License version 2.1,
+## as published by the Free Software Foundation.
+##
+## This program is distributed in the hope that it will be useful,
+## but WITHOUT ANY WARRANTY; without even the implied warranty of
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+## GNU Lesser General Public License for more details.
+##
+## You should have received a copy of the GNU Lesser General Public
+## License along with this program; if not, write to the Free Software
+## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+##
+## $Id: Makefile.am,v 1.3 2008/05/03 10:26:15 steveu Exp $
+
+SUBDIRS = fax
+
+DIST_SUBDIRS = fax
+
+all: 
+
+clean:
+

Added: freeswitch/trunk/libs/spandsp/test-data/itu/fax/Makefile.am
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/test-data/itu/fax/Makefile.am	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,155 @@
+##
+## SpanDSP - a series of DSP components for telephony
+##
+## Makefile.am - Process this file with automake to produce Makefile.in
+##
+## This program is free software; you can redistribute it and/or modify
+## it under the terms of the GNU General Public License version 2, as
+## as published by the Free Software Foundation.
+##
+## This program is distributed in the hope that it will be useful,
+## but WITHOUT ANY WARRANTY; without even the implied warranty of
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+## GNU General Public License for more details.
+##
+## You should have received a copy of the GNU General Public License
+## License along with this program; if not, write to the Free Software
+## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+##
+## $Id: Makefile.am,v 1.3 2008/05/03 13:05:04 steveu Exp $
+
+AM_CFLAGS = $(COMP_VENDOR_CFLAGS)
+AM_LDFLAGS = $(COMP_VENDOR_LDFLAGS)
+
+PBM2G3 = pbmtog3
+FAX2TIFF = fax2tiff
+
+EXTRA_DIST = itu1.pbm \
+             itu2.pbm \
+             itu3.pbm \
+             itu4.pbm \
+             itu5.pbm \
+             itu6.pbm \
+             itu7.pbm \
+             itu8.pbm \
+             test1.pbm \
+             test2.pbm \
+             test3.pbm \
+             test4.pbm
+
+nobase_data_DATA =  itutests.tif \
+                    itu1.tif \
+                    itu2.tif \
+                    itu3.tif \
+                    itu4.tif \
+                    itu5.tif \
+                    itu6.tif \
+                    itu7.tif \
+                    itu8.tif \
+                    test1.tif \
+                    test2.tif \
+                    test3.tif \
+                    test4.tif \
+                    dithered.tif \
+                    100pages.tif \
+                    R1200_1200_A4.tif \
+                    R1200_1200_B4.tif \
+                    R1200_1200_A3.tif \
+                    R600_1200_A4.tif \
+                    R600_1200_B4.tif \
+                    R600_1200_A3.tif \
+                    R600_600_A4.tif \
+                    R600_600_B4.tif \
+                    R600_600_A3.tif \
+                    R16_800_A4.tif \
+                    R16_800_B4.tif \
+                    R16_800_A3.tif \
+                    R16_154_A3.tif \
+                    R16_154_A4.tif \
+                    R16_154_B4.tif \
+                    R300_600_A4.tif \
+                    R300_600_B4.tif \
+                    R300_600_A3.tif \
+                    R300_300_A4.tif \
+                    R300_300_B4.tif \
+                    R300_300_A3.tif \
+                    R8_154_A3.tif \
+                    R8_154_A4.tif \
+                    R8_154_B4.tif \
+                    R8_77_A3.tif \
+                    R8_77_A4.tif \
+                    R8_77_B4.tif \
+                    R8_385_A3.tif \
+                    R8_385_A4.tif \
+                    R8_385_B4.tif
+
+noinst_PROGRAMS =   generate_dithered_tif \
+                    generate_sized_pages
+
+generate_dithered_tif_SOURCES = generate_dithered_tif.c
+generate_dithered_tif_LDADD = -ltiff
+
+generate_sized_pages_SOURCES = generate_sized_pages.c
+generate_sized_pages_LDADD = -ltiff
+
+clean:
+	rm -f *.tif *.g3
+
+itutests.tif: itu1.g3 itu2.g3 itu3.g3 itu4.g3 itu5.g3 itu6.g3 itu7.g3 itu8.g3 \
+              test1.g3 test2.g3 test3.g3 test4.g3
+	$(FAX2TIFF) -M -o $@ itu1.g3 itu2.g3 itu3.g3 itu4.g3 itu5.g3 itu6.g3 itu7.g3 itu8.g3 \
+                test1.g3 test2.g3 test3.g3 test4.g3
+
+100pages.tif: itu1.g3 itu2.g3 itu3.g3 itu4.g3 itu5.g3 itu6.g3 itu7.g3 itu8.g3 \
+              test1.g3 test2.g3 test3.g3 test4.g3
+	$(FAX2TIFF) -M -o $@ \
+        itu1.g3 itu2.g3 itu3.g3 itu4.g3 itu5.g3 itu6.g3 itu7.g3 itu8.g3 test1.g3 test2.g3 test3.g3 test4.g3 \
+        itu1.g3 itu2.g3 itu3.g3 itu4.g3 itu5.g3 itu6.g3 itu7.g3 itu8.g3 test1.g3 test2.g3 test3.g3 test4.g3 \
+        itu1.g3 itu2.g3 itu3.g3 itu4.g3 itu5.g3 itu6.g3 itu7.g3 itu8.g3 test1.g3 test2.g3 test3.g3 test4.g3 \
+        itu1.g3 itu2.g3 itu3.g3 itu4.g3 itu5.g3 itu6.g3 itu7.g3 itu8.g3 test1.g3 test2.g3 test3.g3 test4.g3 \
+        itu1.g3 itu2.g3 itu3.g3 itu4.g3 itu5.g3 itu6.g3 itu7.g3 itu8.g3 test1.g3 test2.g3 test3.g3 test4.g3 \
+        itu1.g3 itu2.g3 itu3.g3 itu4.g3 itu5.g3 itu6.g3 itu7.g3 itu8.g3 test1.g3 test2.g3 test3.g3 test4.g3 \
+        itu1.g3 itu2.g3 itu3.g3 itu4.g3 itu5.g3 itu6.g3 itu7.g3 itu8.g3 test1.g3 test2.g3 test3.g3 test4.g3 \
+        itu1.g3 itu2.g3 itu3.g3 itu4.g3 itu5.g3 itu6.g3 itu7.g3 itu8.g3 test1.g3 test2.g3 test3.g3 test4.g3 \
+        itu1.g3 itu2.g3 itu3.g3 itu4.g3
+
+.g3.tif:
+	${FAX2TIFF} -M -o $*.tif $*.g3
+
+.pbm.g3:
+	${PBM2G3} $*.pbm >$*.g3
+    
+dithered.tif: generate_dithered_tif$(EXEEXT)
+	./generate_dithered_tif$(EXEEXT)
+
+R1200_1200_A4.tif \
+R1200_1200_B4.tif \
+R1200_1200_A3.tif \
+R600_1200_A4.tif \
+R600_1200_B4.tif \
+R600_1200_A3.tif \
+R600_600_A4.tif \
+R600_600_B4.tif \
+R600_600_A3.tif \
+R16_800_A4.tif \
+R16_800_B4.tif \
+R16_800_A3.tif \
+R16_154_A3.tif \
+R16_154_A4.tif \
+R16_154_B4.tif \
+R300_600_A4.tif \
+R300_600_B4.tif \
+R300_600_A3.tif \
+R300_300_A4.tif \
+R300_300_B4.tif \
+R300_300_A3.tif \
+R8_154_A3.tif \
+R8_154_A4.tif \
+R8_154_B4.tif \
+R8_77_A3.tif \
+R8_77_A4.tif \
+R8_77_B4.tif \
+R8_385_A3.tif \
+R8_385_A4.tif \
+R8_385_B4.tif: generate_sized_pages$(EXEEXT)
+	./generate_sized_pages$(EXEEXT)

Added: freeswitch/trunk/libs/spandsp/test-data/itu/fax/generate_dithered_tif.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/test-data/itu/fax/generate_dithered_tif.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,162 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * generate_dithered_tif.c - Create a fine checkerboard TIFF file for test purposes.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2006 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: generate_dithered_tif.c,v 1.2 2008/07/10 13:34:01 steveu Exp $
+ */
+
+/*! \file */
+
+/*
+    This program generates an A4 sized FAX image of a fine checkerboard. This doesn't
+    compress well, so it results in a rather large file for a single page. This is
+    good for testing the handling of extreme pages.
+    
+    Note that due to a bug in FAX image handling, versions of libtiff up to 3.8.2 fail
+    to handle this complex image properly, if 2-D compression is used. The bug should
+    be fixed in CVS at the time of writing, and so should be fixed in released versions
+    after 3.8.2. This code uses 1-D compression to avoid the issue.
+ */
+
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
+
+#include <stdio.h>
+#include <inttypes.h>
+#include <limits.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <stdlib.h>
+#include <time.h>
+#include <memory.h>
+#include <string.h>
+#if defined(HAVE_TGMATH_H)
+#include <tgmath.h>
+#endif
+#if defined(HAVE_MATH_H)
+#include <math.h>
+#endif
+#include <tiffio.h>
+
+#include "spandsp.h"
+
+int main(int argc, char *argv[])
+{
+    int image_width;
+    int row;
+    int resunit;
+    int output_compression;
+    int output_t4_options;
+    uint8_t image_buffer[1024];
+    TIFF *tiff_file;
+    struct tm *tm;
+    time_t now;
+    char buf[133];
+    float x_resolution;
+    float y_resolution;
+    int x_res;
+    int y_res;
+    int image_length;
+
+    if ((tiff_file = TIFFOpen("dithered.tif", "w")) == NULL)
+        exit(2);
+
+    output_compression = COMPRESSION_CCITT_T4;
+    /* Use 1-D compression until a fixed libtiff is the norm. */
+    //output_t4_options = GROUP3OPT_FILLBITS | GROUP3OPT_2DENCODING;
+    output_t4_options = GROUP3OPT_FILLBITS;
+
+    x_res = T4_X_RESOLUTION_R8;
+    y_res = T4_Y_RESOLUTION_FINE;
+    image_width = 1728;
+    image_length = 2200;
+
+    /* Prepare the directory entry fully before writing the image, or libtiff complains */
+    TIFFSetField(tiff_file, TIFFTAG_COMPRESSION, output_compression);
+    if (output_compression == COMPRESSION_CCITT_T4)
+    {
+        TIFFSetField(tiff_file, TIFFTAG_T4OPTIONS, output_t4_options);
+        TIFFSetField(tiff_file, TIFFTAG_FAXMODE, FAXMODE_CLASSF);
+    }
+    TIFFSetField(tiff_file, TIFFTAG_IMAGEWIDTH, image_width);
+    TIFFSetField(tiff_file, TIFFTAG_BITSPERSAMPLE, 1);
+    TIFFSetField(tiff_file, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
+    TIFFSetField(tiff_file, TIFFTAG_SAMPLESPERPIXEL, 1);
+    TIFFSetField(tiff_file, TIFFTAG_ROWSPERSTRIP, -1L);
+    TIFFSetField(tiff_file, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
+    TIFFSetField(tiff_file, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISWHITE);
+    TIFFSetField(tiff_file, TIFFTAG_FILLORDER, FILLORDER_LSB2MSB);
+
+    x_resolution = x_res/100.0f;
+    y_resolution = y_res/100.0f;
+    TIFFSetField(tiff_file, TIFFTAG_XRESOLUTION, floorf(x_resolution*2.54f + 0.5f));
+    TIFFSetField(tiff_file, TIFFTAG_YRESOLUTION, floorf(y_resolution*2.54f + 0.5f));
+    resunit = RESUNIT_INCH;
+    TIFFSetField(tiff_file, TIFFTAG_RESOLUTIONUNIT, resunit);
+
+    TIFFSetField(tiff_file, TIFFTAG_SOFTWARE, "spandsp");
+    if (gethostname(buf, sizeof(buf)) == 0)
+        TIFFSetField(tiff_file, TIFFTAG_HOSTCOMPUTER, buf);
+
+    TIFFSetField(tiff_file, TIFFTAG_IMAGEDESCRIPTION, "Checkerboard or dithered ones");
+    TIFFSetField(tiff_file, TIFFTAG_MAKE, "soft-switch.org");
+    TIFFSetField(tiff_file, TIFFTAG_MODEL, "test data");
+
+    time(&now);
+    tm = localtime(&now);
+    sprintf(buf,
+    	    "%4d/%02d/%02d %02d:%02d:%02d",
+            tm->tm_year + 1900,
+            tm->tm_mon + 1,
+            tm->tm_mday,
+            tm->tm_hour,
+            tm->tm_min,
+            tm->tm_sec);
+    TIFFSetField(tiff_file, TIFFTAG_DATETIME, buf);
+
+    TIFFSetField(tiff_file, TIFFTAG_IMAGELENGTH, image_length);
+    TIFFSetField(tiff_file, TIFFTAG_PAGENUMBER, 0, 1);
+    TIFFSetField(tiff_file, TIFFTAG_CLEANFAXDATA, CLEANFAXDATA_CLEAN);
+    TIFFSetField(tiff_file, TIFFTAG_IMAGEWIDTH, image_width);
+
+    /* Write the image first.... */
+    for (row = 0;  row < image_length;  row++)
+    {
+        if ((row & 1) == 0)
+            memset(image_buffer, 0xAA, image_width/8 + 1);
+        else
+            memset(image_buffer, 0x55, image_width/8 + 1);
+        if (TIFFWriteScanline(tiff_file, image_buffer, row, 0) < 0)
+        {
+            printf("Write error at row %d.\n", row);
+            exit(2);
+        }
+    }
+    /* ....then the directory entry, and libtiff is happy. */
+    TIFFWriteDirectory(tiff_file);
+    TIFFClose(tiff_file);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/test-data/itu/fax/generate_sized_pages.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/test-data/itu/fax/generate_sized_pages.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,357 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * generate_sized_pages.c - Create a series of TIFF files in the various page sizes
+ *                        and resolutions.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2006 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: generate_sized_pages.c,v 1.2 2008/07/10 13:34:01 steveu Exp $
+ */
+
+/*! \file */
+
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
+
+#include <stdio.h>
+#include <inttypes.h>
+#include <limits.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <stdlib.h>
+#include <time.h>
+#include <memory.h>
+#include <string.h>
+#if defined(HAVE_TGMATH_H)
+#include <tgmath.h>
+#endif
+#if defined(HAVE_MATH_H)
+#include <math.h>
+#endif
+
+#include "spandsp.h"
+
+struct
+{
+    const char *name;
+    int x_res;
+    int y_res;
+    int width;
+    int length;
+} sequence[] =
+{
+    {
+        "R8_385_A4.tif",
+        T4_X_RESOLUTION_R8,
+        T4_Y_RESOLUTION_STANDARD,
+        T4_WIDTH_R8_A4,
+        1100
+    },
+    {
+        "R8_385_B4.tif",
+        T4_X_RESOLUTION_R8,
+        T4_Y_RESOLUTION_STANDARD,
+        T4_WIDTH_R8_B4,
+        1200
+    },
+    {
+        "R8_385_A3.tif",
+        T4_X_RESOLUTION_R8,
+        T4_Y_RESOLUTION_STANDARD,
+        T4_WIDTH_R8_A3,
+        1556
+    },
+    {
+        "R8_77_A4.tif",
+        T4_X_RESOLUTION_R8,
+        T4_Y_RESOLUTION_FINE,
+        T4_WIDTH_R8_A4,
+        2200
+    },
+    {
+        "R8_77_B4.tif",
+        T4_X_RESOLUTION_R8,
+        T4_Y_RESOLUTION_FINE,
+        T4_WIDTH_R8_B4,
+        2400
+    },
+    {
+        "R8_77_A3.tif",
+        T4_X_RESOLUTION_R8,
+        T4_Y_RESOLUTION_FINE,
+        T4_WIDTH_R8_A3,
+        3111
+    },
+    {
+        "R8_154_A4.tif",
+        T4_X_RESOLUTION_R8,
+        T4_Y_RESOLUTION_SUPERFINE,
+        T4_WIDTH_R8_A4,
+        4400
+    },
+    {
+        "R8_154_B4.tif",
+        T4_X_RESOLUTION_R8,
+        T4_Y_RESOLUTION_SUPERFINE,
+        T4_WIDTH_R8_B4,
+        4800
+    },
+    {
+        "R8_154_A3.tif",
+        T4_X_RESOLUTION_R8,
+        T4_Y_RESOLUTION_SUPERFINE,
+        T4_WIDTH_R8_A3,
+        6222
+    },
+    {
+        "R300_300_A4.tif",
+        T4_X_RESOLUTION_300,
+        T4_Y_RESOLUTION_300,
+        T4_WIDTH_300_A4,
+        4400
+    },
+    {
+        "R300_300_B4.tif",
+        T4_X_RESOLUTION_300,
+        T4_Y_RESOLUTION_300,
+        T4_WIDTH_300_B4,
+        4800
+    },
+    {
+        "R300_300_A3.tif",
+        T4_X_RESOLUTION_300,
+        T4_Y_RESOLUTION_300,
+        T4_WIDTH_300_A3,
+        6222
+    },
+    {
+        "R300_600_A4.tif",
+        T4_X_RESOLUTION_300,
+        T4_Y_RESOLUTION_600,
+        T4_WIDTH_300_A4,
+        4400
+    },
+    {
+        "R300_600_B4.tif",
+        T4_X_RESOLUTION_300,
+        T4_Y_RESOLUTION_600,
+        T4_WIDTH_300_B4,
+        4800
+    },
+    {
+        "R300_600_A3.tif",
+        T4_X_RESOLUTION_300,
+        T4_Y_RESOLUTION_600,
+        T4_WIDTH_300_A3,
+        6222
+    },
+    {
+        "R16_154_A4.tif",
+        T4_X_RESOLUTION_R16,
+        T4_Y_RESOLUTION_SUPERFINE,
+        T4_WIDTH_R16_A4,
+        4400
+    },
+    {
+        "R16_154_B4.tif",
+        T4_X_RESOLUTION_R16,
+        T4_Y_RESOLUTION_SUPERFINE,
+        T4_WIDTH_R16_B4,
+        4800
+    },
+    {
+        "R16_154_A3.tif",
+        T4_X_RESOLUTION_R16,
+        T4_Y_RESOLUTION_SUPERFINE,
+        T4_WIDTH_R16_A3,
+        6222
+    },
+    {
+        "R16_800_A4.tif",
+        T4_X_RESOLUTION_R16,
+        T4_Y_RESOLUTION_800,
+        T4_WIDTH_R16_A4,
+        4400
+    },
+    {
+        "R16_800_B4.tif",
+        T4_X_RESOLUTION_R16,
+        T4_Y_RESOLUTION_800,
+        T4_WIDTH_R16_B4,
+        4800
+    },
+    {
+        "R16_800_A3.tif",
+        T4_X_RESOLUTION_R16,
+        T4_Y_RESOLUTION_800,
+        T4_WIDTH_R16_A3,
+        6222
+    },
+    {
+        "R600_600_A4.tif",
+        T4_X_RESOLUTION_600,
+        T4_Y_RESOLUTION_600,
+        T4_WIDTH_600_A4,
+        4400
+    },
+    {
+        "R600_600_B4.tif",
+        T4_X_RESOLUTION_600,
+        T4_Y_RESOLUTION_600,
+        T4_WIDTH_600_B4,
+        4800
+    },
+    {
+        "R600_600_A3.tif",
+        T4_X_RESOLUTION_600,
+        T4_Y_RESOLUTION_600,
+        T4_WIDTH_600_A3,
+        6222
+    },
+    {
+        "R600_1200_A4.tif",
+        T4_X_RESOLUTION_600,
+        T4_Y_RESOLUTION_1200,
+        T4_WIDTH_600_A4,
+        4400
+    },
+    {
+        "R600_1200_B4.tif",
+        T4_X_RESOLUTION_600,
+        T4_Y_RESOLUTION_1200,
+        T4_WIDTH_600_B4,
+        4800
+    },
+    {
+        "R600_1200_A3.tif",
+        T4_X_RESOLUTION_600,
+        T4_Y_RESOLUTION_1200,
+        T4_WIDTH_600_A3,
+        6222
+    },
+    {
+        "R1200_1200_A4.tif",
+        T4_X_RESOLUTION_1200,
+        T4_Y_RESOLUTION_1200,
+        T4_WIDTH_1200_A4,
+        4400
+    },
+    {
+        "R1200_1200_B4.tif",
+        T4_X_RESOLUTION_1200,
+        T4_Y_RESOLUTION_1200,
+        T4_WIDTH_1200_B4,
+        4800
+    },
+    {
+        "R1200_1200_A3.tif",
+        T4_X_RESOLUTION_1200,
+        T4_Y_RESOLUTION_1200,
+        T4_WIDTH_1200_A3,
+        6222
+    },
+    {
+        NULL,
+        0,
+        0,
+        0,
+        0
+    },
+};
+
+int main(int argc, char *argv[])
+{
+    int row;
+    uint8_t image_buffer[8192];
+    TIFF *tiff_file;
+    struct tm *tm;
+    time_t now;
+    char buf[133];
+    float x_resolution;
+    float y_resolution;
+    int i;
+
+    for (i = 0;  sequence[i].name;  i++)
+    {
+        if ((tiff_file = TIFFOpen(sequence[i].name, "w")) == NULL)
+            exit(2);
+
+        /* Prepare the directory entry fully before writing the image, or libtiff complains */
+        TIFFSetField(tiff_file, TIFFTAG_COMPRESSION, COMPRESSION_CCITT_T6);
+        TIFFSetField(tiff_file, TIFFTAG_IMAGEWIDTH, sequence[i].width);
+        TIFFSetField(tiff_file, TIFFTAG_BITSPERSAMPLE, 1);
+        TIFFSetField(tiff_file, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
+        TIFFSetField(tiff_file, TIFFTAG_SAMPLESPERPIXEL, 1);
+        TIFFSetField(tiff_file, TIFFTAG_ROWSPERSTRIP, -1L);
+        TIFFSetField(tiff_file, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
+        TIFFSetField(tiff_file, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISWHITE);
+        TIFFSetField(tiff_file, TIFFTAG_FILLORDER, FILLORDER_LSB2MSB);
+    
+        x_resolution = sequence[i].x_res/100.0f;
+        y_resolution = sequence[i].y_res/100.0f;
+        TIFFSetField(tiff_file, TIFFTAG_XRESOLUTION, floorf(x_resolution*2.54f + 0.5f));
+        TIFFSetField(tiff_file, TIFFTAG_YRESOLUTION, floorf(y_resolution*2.54f + 0.5f));
+        TIFFSetField(tiff_file, TIFFTAG_RESOLUTIONUNIT, RESUNIT_INCH);
+    
+        TIFFSetField(tiff_file, TIFFTAG_SOFTWARE, "spandsp");
+        if (gethostname(buf, sizeof(buf)) == 0)
+            TIFFSetField(tiff_file, TIFFTAG_HOSTCOMPUTER, buf);
+    
+        TIFFSetField(tiff_file, TIFFTAG_IMAGEDESCRIPTION, "Blank test image");
+        TIFFSetField(tiff_file, TIFFTAG_MAKE, "soft-switch.org");
+        TIFFSetField(tiff_file, TIFFTAG_MODEL, "test data");
+
+        time(&now);
+        tm = localtime(&now);
+        sprintf(buf,
+    	        "%4d/%02d/%02d %02d:%02d:%02d",
+                tm->tm_year + 1900,
+                tm->tm_mon + 1,
+                tm->tm_mday,
+                tm->tm_hour,
+                tm->tm_min,
+                tm->tm_sec);
+        TIFFSetField(tiff_file, TIFFTAG_DATETIME, buf);
+    
+        TIFFSetField(tiff_file, TIFFTAG_IMAGELENGTH, sequence[i].length);
+        TIFFSetField(tiff_file, TIFFTAG_PAGENUMBER, 0, 1);
+        TIFFSetField(tiff_file, TIFFTAG_CLEANFAXDATA, CLEANFAXDATA_CLEAN);
+        TIFFSetField(tiff_file, TIFFTAG_IMAGEWIDTH, sequence[i].width);
+
+        /* Write the image first.... */
+        for (row = 0;  row < sequence[i].length;  row++)
+        {
+            memset(image_buffer, 0, sequence[i].width/8 + 1);
+            if (TIFFWriteScanline(tiff_file, image_buffer, row, 0) < 0)
+            {
+                printf("Write error at row %d.\n", row);
+                exit(2);
+            }
+        }
+        /* ....then the directory entry, and libtiff is happy. */
+        TIFFWriteDirectory(tiff_file);
+        TIFFClose(tiff_file);
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/test-data/itu/fax/itu1.pbm
==============================================================================
Binary file. No diff available.

Added: freeswitch/trunk/libs/spandsp/test-data/itu/fax/itu2.pbm
==============================================================================
Binary file. No diff available.

Added: freeswitch/trunk/libs/spandsp/test-data/itu/fax/itu3.pbm
==============================================================================
Binary file. No diff available.

Added: freeswitch/trunk/libs/spandsp/test-data/itu/fax/itu4.pbm
==============================================================================
Binary file. No diff available.

Added: freeswitch/trunk/libs/spandsp/test-data/itu/fax/itu5.pbm
==============================================================================
Binary file. No diff available.

Added: freeswitch/trunk/libs/spandsp/test-data/itu/fax/itu6.pbm
==============================================================================
Binary file. No diff available.

Added: freeswitch/trunk/libs/spandsp/test-data/itu/fax/itu7.pbm
==============================================================================
Binary file. No diff available.

Added: freeswitch/trunk/libs/spandsp/test-data/itu/fax/itu8.pbm
==============================================================================
Binary file. No diff available.

Added: freeswitch/trunk/libs/spandsp/test-data/itu/fax/test1.pbm
==============================================================================
Binary file. No diff available.

Added: freeswitch/trunk/libs/spandsp/test-data/itu/fax/test2.pbm
==============================================================================
Binary file. No diff available.

Added: freeswitch/trunk/libs/spandsp/test-data/itu/fax/test3.pbm
==============================================================================
Binary file. No diff available.

Added: freeswitch/trunk/libs/spandsp/test-data/itu/fax/test4.pbm
==============================================================================
Binary file. No diff available.

Added: freeswitch/trunk/libs/spandsp/test-data/local/Makefile.am
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/test-data/local/Makefile.am	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,32 @@
+##
+## SpanDSP - a series of DSP components for telephony
+##
+## Makefile.am - Process this file with automake to produce Makefile.in
+##
+## This program is free software; you can redistribute it and/or modify
+## it under the terms of the GNU Lesser General Public License version 2.1,
+## as published by the Free Software Foundation.
+##
+## This program is distributed in the hope that it will be useful,
+## but WITHOUT ANY WARRANTY; without even the implied warranty of
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+## GNU Lesser General Public License for more details.
+##
+## You should have received a copy of the GNU Lesser General Public
+## License along with this program; if not, write to the Free Software
+## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+##
+## $Id: Makefile.am,v 1.1 2008/05/03 07:30:19 steveu Exp $
+
+SUBDIRS =
+
+DIST_SUBDIRS =
+
+EXTRA_DIST = dam9_lpc55.wav \
+             dam9.wav \
+             short_nb_voice.wav \
+             short_wb_voice.wav
+
+all: 
+
+clean:

Added: freeswitch/trunk/libs/spandsp/test-data/local/dam9.wav
==============================================================================
Binary file. No diff available.

Added: freeswitch/trunk/libs/spandsp/test-data/local/dam9_lpc55.wav
==============================================================================
Binary file. No diff available.

Added: freeswitch/trunk/libs/spandsp/test-data/local/short_nb_voice.wav
==============================================================================
Binary file. No diff available.

Added: freeswitch/trunk/libs/spandsp/test-data/local/short_wb_voice.wav
==============================================================================
Binary file. No diff available.

Added: freeswitch/trunk/libs/spandsp/tests/Makefile.am
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/tests/Makefile.am	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,318 @@
+##
+## SpanDSP - a series of DSP components for telephony
+##
+## Makefile.am - Process this file with automake to produce Makefile.in
+##
+## This program is free software; you can redistribute it and/or modify
+## it under the terms of the GNU General Public License version 2, as
+## published by the Free Software Foundation.
+##
+## This program is distributed in the hope that it will be useful,
+## but WITHOUT ANY WARRANTY; without even the implied warranty of
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+## GNU General Public License for more details.
+##
+## You should have received a copy of the GNU General Public License
+## along with this program; if not, write to the Free Software
+## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+##
+## $Id: Makefile.am,v 1.105 2008/08/16 15:45:45 steveu Exp $
+
+AM_CFLAGS = $(COMP_VENDOR_CFLAGS)
+AM_LDFLAGS = $(COMP_VENDOR_LDFLAGS)
+
+LIBS += $(TESTLIBS)
+
+noinst_DATA = sound_c1_8k.wav sound_c3_8k.wav
+
+EXTRA_DIST = regression_tests.sh v42bis_tests.sh fax_tests.sh tsb85_tests.sh
+ 
+MAINTAINERCLEANFILES = Makefile.in
+
+INCLUDES = -I$(top_builddir)/src -I$(top_builddir)/spandsp-sim -DDATADIR="\"$(pkgdatadir)\""
+
+LIBDIR = -L$(top_builddir)/src
+
+noinst_PROGRAMS =   adsi_tests \
+                    async_tests \
+                    at_interpreter_tests \
+                    awgn_tests \
+                    bell_mf_rx_tests \
+                    bell_mf_tx_tests \
+                    bert_tests \
+                    bit_operations_tests \
+                    crc_tests \
+                    dc_restore_tests \
+                    dds_tests \
+                    dtmf_rx_tests \
+                    dtmf_tx_tests \
+                    echo_tests \
+                    fax_decode \
+                    fax_tests \
+                    fsk_tests \
+                    g1050_tests \
+                    g168_tests \
+                    g711_tests \
+                    g722_tests \
+                    g726_tests \
+                    gsm0610_tests \
+                    hdlc_tests \
+                    ima_adpcm_tests \
+                    line_model_tests \
+                    logging_tests \
+                    lpc10_tests \
+                    make_g168_css \
+                    modem_echo_tests \
+                    modem_connect_tones_tests \
+                    noise_tests \
+                    oki_adpcm_tests \
+                    playout_tests \
+                    plc_tests \
+                    power_meter_tests \
+                    queue_tests \
+                    r2_mf_rx_tests \
+                    r2_mf_tx_tests \
+                    rfc2198_sim_tests \
+                    schedule_tests \
+                    sig_tone_tests \
+                    super_tone_rx_tests \
+                    super_tone_tx_tests \
+                    t4_tests \
+                    t31_tests \
+                    t38_core_tests \
+                    t38_gateway_tests \
+                    t38_gateway_to_terminal_tests \
+                    t38_non_ecm_buffer_tests \
+                    t38_terminal_tests \
+                    t38_terminal_to_gateway_tests \
+                    time_scale_tests \
+                    tone_detect_tests \
+                    tone_generate_tests \
+                    tsb85_tests \
+                    v17_tests \
+                    v22bis_tests \
+                    v27ter_tests \
+                    v29_tests \
+                    v42_tests \
+                    v42bis_tests \
+                    v8_tests \
+                    vector_float_tests \
+                    vector_int_tests \
+                    testadsi \
+                    testfax \
+                    tsb85_tests
+
+noinst_HEADERS =    echo_monitor.h \
+                    fax_tester.h \
+                    line_model_monitor.h \
+                    media_monitor.h \
+                    modem_monitor.h
+
+adsi_tests_SOURCES = adsi_tests.c
+adsi_tests_LDADD = -L$(top_builddir)/spandsp-sim -lspandsp-sim $(LIBDIR) -lspandsp
+
+async_tests_SOURCES = async_tests.c
+async_tests_LDADD = $(LIBDIR) -lspandsp
+
+at_interpreter_tests_SOURCES = at_interpreter_tests.c
+at_interpreter_tests_LDADD = $(LIBDIR) -lspandsp
+
+awgn_tests_SOURCES = awgn_tests.c
+awgn_tests_LDADD = $(LIBDIR) -lspandsp
+
+bell_mf_rx_tests_SOURCES = bell_mf_rx_tests.c
+bell_mf_rx_tests_LDADD = $(LIBDIR) -lspandsp
+
+bell_mf_tx_tests_SOURCES = bell_mf_tx_tests.c
+bell_mf_tx_tests_LDADD = -L$(top_builddir)/spandsp-sim -lspandsp-sim $(LIBDIR) -lspandsp
+
+bert_tests_SOURCES = bert_tests.c
+bert_tests_LDADD = $(LIBDIR) -lspandsp
+
+bit_operations_tests_SOURCES = bit_operations_tests.c
+bit_operations_tests_LDADD = $(LIBDIR) -lspandsp
+
+crc_tests_SOURCES = crc_tests.c
+crc_tests_LDADD = $(LIBDIR) -lspandsp
+
+dc_restore_tests_SOURCES = dc_restore_tests.c
+dc_restore_tests_LDADD = $(LIBDIR) -lspandsp
+
+dds_tests_SOURCES = dds_tests.c
+dds_tests_LDADD = $(LIBDIR) -lspandsp
+
+dtmf_rx_tests_SOURCES = dtmf_rx_tests.c
+dtmf_rx_tests_LDADD = -L$(top_builddir)/spandsp-sim -lspandsp-sim $(LIBDIR) -lspandsp
+
+dtmf_tx_tests_SOURCES = dtmf_tx_tests.c
+dtmf_tx_tests_LDADD = -L$(top_builddir)/spandsp-sim -lspandsp-sim $(LIBDIR) -lspandsp
+
+echo_tests_SOURCES = echo_tests.c echo_monitor.cpp
+echo_tests_LDADD = -L$(top_builddir)/spandsp-sim -lspandsp-sim $(LIBDIR) -lspandsp
+
+fax_decode_SOURCES = fax_decode.c
+fax_decode_LDADD = $(LIBDIR) -lspandsp
+
+fax_tests_SOURCES = fax_tests.c
+fax_tests_LDADD = -L$(top_builddir)/spandsp-sim -lspandsp-sim $(LIBDIR) -lspandsp
+
+fsk_tests_SOURCES = fsk_tests.c
+fsk_tests_LDADD = -L$(top_builddir)/spandsp-sim -lspandsp-sim $(LIBDIR) -lspandsp
+
+g1050_tests_SOURCES = g1050_tests.c media_monitor.cpp
+g1050_tests_LDADD = -L$(top_builddir)/spandsp-sim -lspandsp-sim $(LIBDIR) -lspandsp
+
+g168_tests_SOURCES = g168_tests.c
+g168_tests_LDADD = $(LIBDIR) -lspandsp
+
+g711_tests_SOURCES = g711_tests.c
+g711_tests_LDADD = -L$(top_builddir)/spandsp-sim -lspandsp-sim $(LIBDIR) -lspandsp
+
+g722_tests_SOURCES = g722_tests.c
+g722_tests_LDADD = $(LIBDIR) -lspandsp
+
+g726_tests_SOURCES = g726_tests.c
+g726_tests_LDADD = -L$(top_builddir)/spandsp-sim -lspandsp-sim $(LIBDIR) -lspandsp
+
+gsm0610_tests_SOURCES = gsm0610_tests.c
+gsm0610_tests_LDADD = -L$(top_builddir)/spandsp-sim -lspandsp-sim $(LIBDIR) -lspandsp
+
+hdlc_tests_SOURCES = hdlc_tests.c
+hdlc_tests_LDADD = $(LIBDIR) -lspandsp
+
+ima_adpcm_tests_SOURCES = ima_adpcm_tests.c
+ima_adpcm_tests_LDADD = -L$(top_builddir)/spandsp-sim -lspandsp-sim $(LIBDIR) -lspandsp
+
+line_model_tests_SOURCES = line_model_tests.c
+line_model_tests_LDADD = -L$(top_builddir)/spandsp-sim -lspandsp-sim $(LIBDIR) -lspandsp
+
+logging_tests_SOURCES = logging_tests.c
+logging_tests_LDADD = $(LIBDIR) -lspandsp
+
+lpc10_tests_SOURCES = lpc10_tests.c
+lpc10_tests_LDADD = -L$(top_builddir)/spandsp-sim -lspandsp-sim $(LIBDIR) -lspandsp
+
+make_g168_css_SOURCES = make_g168_css.c
+make_g168_css_LDADD = $(LIBDIR) -lspandsp
+
+modem_echo_tests_SOURCES = modem_echo_tests.c echo_monitor.cpp
+modem_echo_tests_LDADD = -L$(top_builddir)/spandsp-sim -lspandsp-sim $(LIBDIR) -lspandsp
+
+modem_connect_tones_tests_SOURCES = modem_connect_tones_tests.c
+modem_connect_tones_tests_LDADD = -L$(top_builddir)/spandsp-sim -lspandsp-sim $(LIBDIR) -lspandsp
+
+noise_tests_SOURCES = noise_tests.c
+noise_tests_LDADD = -L$(top_builddir)/spandsp-sim -lspandsp-sim $(LIBDIR) -lspandsp
+
+oki_adpcm_tests_SOURCES = oki_adpcm_tests.c
+oki_adpcm_tests_LDADD = -L$(top_builddir)/spandsp-sim -lspandsp-sim $(LIBDIR) -lspandsp
+
+playout_tests_SOURCES = playout_tests.c media_monitor.cpp
+playout_tests_LDADD = -L$(top_builddir)/spandsp-sim -lspandsp-sim $(LIBDIR) -lspandsp
+
+plc_tests_SOURCES = plc_tests.c
+plc_tests_LDADD = $(LIBDIR) -lspandsp
+
+power_meter_tests_SOURCES = power_meter_tests.c
+power_meter_tests_LDADD = $(LIBDIR) -lspandsp
+
+queue_tests_SOURCES = queue_tests.c
+queue_tests_LDADD = $(LIBDIR) -lspandsp
+
+r2_mf_rx_tests_SOURCES = r2_mf_rx_tests.c
+r2_mf_rx_tests_LDADD = $(LIBDIR) -lspandsp
+
+r2_mf_tx_tests_SOURCES = r2_mf_tx_tests.c
+r2_mf_tx_tests_LDADD = -L$(top_builddir)/spandsp-sim -lspandsp-sim $(LIBDIR) -lspandsp
+
+rfc2198_sim_tests_SOURCES = rfc2198_sim_tests.c media_monitor.cpp
+rfc2198_sim_tests_LDADD = -L$(top_builddir)/spandsp-sim -lspandsp-sim $(LIBDIR) -lspandsp
+
+schedule_tests_SOURCES = schedule_tests.c
+schedule_tests_LDADD = $(LIBDIR) -lspandsp 
+
+sig_tone_tests_SOURCES = sig_tone_tests.c
+sig_tone_tests_LDADD = -L$(top_builddir)/spandsp-sim -lspandsp-sim $(LIBDIR) -lspandsp 
+
+super_tone_rx_tests_SOURCES = super_tone_rx_tests.c
+super_tone_rx_tests_LDADD = $(LIBDIR) -lspandsp
+
+super_tone_tx_tests_SOURCES = super_tone_tx_tests.c
+super_tone_tx_tests_LDADD = -L$(top_builddir)/spandsp-sim -lspandsp-sim $(LIBDIR) -lspandsp
+
+t4_tests_SOURCES = t4_tests.c
+t4_tests_LDADD = $(LIBDIR) -lspandsp
+
+t31_tests_SOURCES = t31_tests.c media_monitor.cpp
+t31_tests_LDADD = -L$(top_builddir)/spandsp-sim -lspandsp-sim $(LIBDIR) -lspandsp
+
+t38_core_tests_SOURCES = t38_core_tests.c
+t38_core_tests_LDADD = $(LIBDIR) -lspandsp
+
+t38_gateway_tests_SOURCES = t38_gateway_tests.c media_monitor.cpp
+t38_gateway_tests_LDADD = -L$(top_builddir)/spandsp-sim -lspandsp-sim $(LIBDIR) -lspandsp
+
+t38_gateway_to_terminal_tests_SOURCES = t38_gateway_to_terminal_tests.c media_monitor.cpp
+t38_gateway_to_terminal_tests_LDADD = -L$(top_builddir)/spandsp-sim -lspandsp-sim $(LIBDIR) -lspandsp
+
+t38_non_ecm_buffer_tests_SOURCES = t38_non_ecm_buffer_tests.c
+t38_non_ecm_buffer_tests_LDADD = $(LIBDIR) -lspandsp
+
+t38_terminal_tests_SOURCES = t38_terminal_tests.c media_monitor.cpp
+t38_terminal_tests_LDADD = -L$(top_builddir)/spandsp-sim -lspandsp-sim $(LIBDIR) -lspandsp
+
+t38_terminal_to_gateway_tests_SOURCES = t38_terminal_to_gateway_tests.c media_monitor.cpp
+t38_terminal_to_gateway_tests_LDADD = -L$(top_builddir)/spandsp-sim -lspandsp-sim $(LIBDIR) -lspandsp
+
+time_scale_tests_SOURCES = time_scale_tests.c
+time_scale_tests_LDADD = $(LIBDIR) -lspandsp
+
+tone_detect_tests_SOURCES = tone_detect_tests.c
+tone_detect_tests_LDADD = $(LIBDIR) -lspandsp
+
+tone_generate_tests_SOURCES = tone_generate_tests.c
+tone_generate_tests_LDADD = -L$(top_builddir)/spandsp-sim -lspandsp-sim $(LIBDIR) -lspandsp
+
+tsb85_tests_SOURCES = tsb85_tests.c fax_tester.c
+tsb85_tests_LDADD = $(LIBDIR) -lspandsp
+
+v17_tests_SOURCES = v17_tests.c line_model_monitor.cpp modem_monitor.cpp
+v17_tests_LDADD = -L$(top_builddir)/spandsp-sim -lspandsp-sim $(LIBDIR) -lspandsp
+
+v22bis_tests_SOURCES = v22bis_tests.c line_model_monitor.cpp modem_monitor.cpp
+v22bis_tests_LDADD = -L$(top_builddir)/spandsp-sim -lspandsp-sim $(LIBDIR) -lspandsp
+
+v27ter_tests_SOURCES = v27ter_tests.c line_model_monitor.cpp modem_monitor.cpp
+v27ter_tests_LDADD = -L$(top_builddir)/spandsp-sim -lspandsp-sim $(LIBDIR) -lspandsp
+
+v29_tests_SOURCES = v29_tests.c line_model_monitor.cpp modem_monitor.cpp
+v29_tests_LDADD = -L$(top_builddir)/spandsp-sim -lspandsp-sim $(LIBDIR) -lspandsp
+
+v42_tests_SOURCES = v42_tests.c
+v42_tests_LDADD = $(LIBDIR) -lspandsp
+
+v42bis_tests_SOURCES = v42bis_tests.c
+v42bis_tests_LDADD = $(LIBDIR) -lspandsp
+
+v8_tests_SOURCES = v8_tests.c
+v8_tests_LDADD = -L$(top_builddir)/spandsp-sim -lspandsp-sim $(LIBDIR) -lspandsp
+
+vector_float_tests_SOURCES = vector_float_tests.c
+vector_float_tests_LDADD = $(LIBDIR) -lspandsp
+
+vector_int_tests_SOURCES = vector_int_tests.c
+vector_int_tests_LDADD = $(LIBDIR) -lspandsp
+
+testadsi_SOURCES = testadsi.c
+testadsi_LDADD = $(LIBDIR) -lspandsp
+
+testfax_SOURCES = testfax.c
+testfax_LDADD = $(LIBDIR) -lspandsp
+
+# We need to create the CSS files for echo cancellation tests.
+
+sound_c1_8k.wav sound_c3_8k.wav: make_g168_css$(EXEEXT)
+	./make_g168_css$(EXEEXT)
+	sox sound_c1.wav -r8000 sound_c1_8k.wav
+	sox sound_c3.wav -r8000 sound_c3_8k.wav
+	rm sound_c1.wav sound_c3.wav

Added: freeswitch/trunk/libs/spandsp/tests/adsi_tests.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/tests/adsi_tests.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,770 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * adsi_tests.c - tests for analogue display service handling.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2003 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: adsi_tests.c,v 1.45 2008/08/29 09:28:13 steveu Exp $
+ */
+
+/*! \page adsi_tests_page ADSI tests
+\section adsi_tests_page_sec_1 What does it do?
+These tests exercise the ADSI module, for all supported standards. A transmit
+and a receive instance of the ADSI module are connected together. A quantity
+of messages is passed between these instances, and checked for accuracy at
+the receiver. Since the FSK modems used for this are exercised fully by other
+tests, these tests do not include line modelling.
+
+\section adsi_tests_page_sec_2 How does it work?
+*/
+
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
+
+#include <unistd.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <strings.h>
+#include <assert.h>
+#include <audiofile.h>
+
+#include "spandsp.h"
+#include "spandsp-sim.h"
+
+#define OUT_FILE_NAME   "adsi.wav"
+
+#define BLOCK_LEN       160
+
+char *decode_test_file = NULL;
+
+int errors = 0;
+
+adsi_rx_state_t rx_adsi;
+adsi_tx_state_t tx_adsi;
+
+int current_standard = 0;
+int good_message_received;
+
+static int adsi_create_message(adsi_tx_state_t *s, uint8_t *msg)
+{
+    const char *t;
+    int len;
+    static int cycle = 0;
+
+    len = 0;
+    switch (current_standard)
+    {
+    case ADSI_STANDARD_CLASS:
+        if (cycle > 3)
+            cycle = 0;
+        switch (cycle)
+        {
+        case 0:
+            len = adsi_add_field(s, msg, -1, CLASS_MDMF_CALLERID, NULL, 0);
+            /* Date and time as MMDDHHMM */
+            len = adsi_add_field(s, msg, len, MCLASS_DATETIME, (uint8_t *) "10011750", 8);
+            len = adsi_add_field(s, msg, len, MCLASS_CALLER_NUMBER, (uint8_t *) "12345678", 8);
+            len = adsi_add_field(s, msg, len, MCLASS_DIALED_NUMBER, (uint8_t *) "87654321", 8);
+            len = adsi_add_field(s, msg, len, MCLASS_CALLER_NAME, (uint8_t *) "Chan Dai Man", 15);
+            break;
+        case 1:
+            len = adsi_add_field(s, msg, -1, CLASS_SDMF_MSG_WAITING, NULL, 0);
+            /* Active */
+            len = adsi_add_field(s, msg, len, 0, (uint8_t *) "\x42", 1);
+            len = adsi_add_field(s, msg, len, 0, (uint8_t *) "\x42", 1);
+            len = adsi_add_field(s, msg, len, 0, (uint8_t *) "\x42", 1);
+            break;
+        case 2:
+            len = adsi_add_field(s, msg, -1, CLASS_SDMF_MSG_WAITING, NULL, 0);
+            /* Inactive */
+            len = adsi_add_field(s, msg, len, 0, (uint8_t *) "\x6F", 1);
+            len = adsi_add_field(s, msg, len, 0, (uint8_t *) "\x6F", 1);
+            len = adsi_add_field(s, msg, len, 0, (uint8_t *) "\x6F", 1);
+            break;
+        case 3:
+            len = adsi_add_field(s, msg, -1, CLASS_SDMF_CALLERID, NULL, 0);
+            /* Date and time as MMDDHHMM */
+            len = adsi_add_field(s, msg, len, 0, (uint8_t *) "10011750", 8);
+            len = adsi_add_field(s, msg, len, 0, (uint8_t *) "6095551212", 10);
+            break;
+        }
+        break;
+    case ADSI_STANDARD_CLIP:
+        if (cycle > 4)
+            cycle = 0;
+        switch (cycle)
+        {
+        case 0:
+            len = adsi_add_field(s, msg, -1, CLIP_MDMF_CALLERID, NULL, 0);
+            len = adsi_add_field(s, msg, len, CLIP_CALLTYPE, (uint8_t *) "\x81", 1);
+            /* Date and time as MMDDHHMM */
+            len = adsi_add_field(s, msg, len, CLIP_DATETIME, (uint8_t *) "10011750", 8);
+            len = adsi_add_field(s, msg, len, CLIP_DIALED_NUMBER, (uint8_t *) "12345678", 8);
+            len = adsi_add_field(s, msg, len, CLIP_CALLER_NUMBER, (uint8_t *) "87654321", 8);
+            len = adsi_add_field(s, msg, len, CLIP_CALLER_NAME, (uint8_t *) "Chan Dai Man", 15);
+            break;
+        case 1:
+            len = adsi_add_field(s, msg, -1, CLIP_MDMF_MSG_WAITING, NULL, 0);
+            /* Inactive */
+            len = adsi_add_field(s, msg, len, CLIP_VISUAL_INDICATOR, (uint8_t *) "\x00", 1);
+            break;
+        case 2:
+            len = adsi_add_field(s, msg, -1, CLIP_MDMF_MSG_WAITING, NULL, 0);
+            /* Active */
+            len = adsi_add_field(s, msg, len, CLIP_VISUAL_INDICATOR, (uint8_t *) "\xFF", 1);
+            len = adsi_add_field(s, msg, len, CLIP_NUM_MSG, (uint8_t *) "\x05", 1);
+            break;
+        case 3:
+            len = adsi_add_field(s, msg, -1, CLIP_MDMF_SMS, NULL, 0);
+            /* Active */
+            len = adsi_add_field(s, msg, len, CLIP_DISPLAY_INFO, (uint8_t *) "\x00" "ABC", 4);
+            break;
+        case 4:
+            len = adsi_add_field(s, msg, -1, CLIP_MDMF_CALLERID, NULL, 0);
+            len = adsi_add_field(s, msg, len, CLIP_NUM_MSG, (uint8_t *) "\x03", 1);
+            break;
+        }
+        break;
+    case ADSI_STANDARD_ACLIP:
+        if (cycle > 0)
+            cycle = 0;
+        switch (cycle)
+        {
+        case 0:
+            len = adsi_add_field(s, msg, -1, ACLIP_MDMF_CALLERID, NULL, 0);
+            /* Date and time as MMDDHHMM */
+            len = adsi_add_field(s, msg, len, ACLIP_DATETIME, (uint8_t *) "10011750", 8);
+            len = adsi_add_field(s, msg, len, ACLIP_DIALED_NUMBER, (uint8_t *) "12345678", 8);
+            len = adsi_add_field(s, msg, len, ACLIP_CALLER_NUMBER, (uint8_t *) "87654321", 8);
+            len = adsi_add_field(s, msg, len, ACLIP_CALLER_NAME, (uint8_t *) "Chan Dai Man", 15);
+            break;
+        }
+        break;
+    case ADSI_STANDARD_JCLIP:
+        len = adsi_add_field(s, msg, -1, JCLIP_MDMF_CALLERID, NULL, 0);
+        len = adsi_add_field(s, msg, len, JCLIP_CALLER_NUMBER, (uint8_t *) "12345678", 8);
+        len = adsi_add_field(s, msg, len, JCLIP_CALLER_NUM_DES, (uint8_t *) "215", 3);
+        len = adsi_add_field(s, msg, len, JCLIP_DIALED_NUMBER, (uint8_t *) "87654321", 8);
+        len = adsi_add_field(s, msg, len, JCLIP_DIALED_NUM_DES, (uint8_t *) "215", 3);
+        break;
+    case ADSI_STANDARD_CLIP_DTMF:
+        if (cycle > 4)
+            cycle = 0;
+        switch (cycle)
+        {
+        case 0:
+            len = adsi_add_field(s, msg, -1, CLIP_DTMF_C_TERMINATED, NULL, 0);
+            len = adsi_add_field(s, msg, len, CLIP_DTMF_C_CALLER_NUMBER, (uint8_t *) "12345678", 8);
+            len = adsi_add_field(s, msg, len, CLIP_DTMF_C_ABSENCE, (uint8_t *) "10", 2);
+            len = adsi_add_field(s, msg, len, CLIP_DTMF_C_REDIRECT_NUMBER, (uint8_t *) "87654321", 8);
+            break;
+        case 1:
+            len = adsi_add_field(s, msg, -1, CLIP_DTMF_HASH_TERMINATED, NULL, 0);
+            len = adsi_add_field(s, msg, len, CLIP_DTMF_HASH_CALLER_NUMBER, (uint8_t *) "12345678", 8);
+            break;
+        case 2:
+            len = adsi_add_field(s, msg, -1, CLIP_DTMF_HASH_TERMINATED, NULL, 0);
+            len = adsi_add_field(s, msg, len, CLIP_DTMF_HASH_ABSENCE, (uint8_t *) "1", 1);
+            break;
+        case 3:
+            /* Test the D<number>C format, used in Taiwan and Kuwait */
+            len = adsi_add_field(s, msg, -1, CLIP_DTMF_HASH_TERMINATED, NULL, 0);
+            len = adsi_add_field(s, msg, len, CLIP_DTMF_HASH_ABSENCE, (uint8_t *) "12345678", 8);
+            break;
+        case 4:
+            /* Test the <number># format, with no header */
+            len = adsi_add_field(s, msg, -1, CLIP_DTMF_HASH_TERMINATED, NULL, 0);
+            len = adsi_add_field(s, msg, len, CLIP_DTMF_HASH_UNSPECIFIED, (uint8_t *) "12345678", 8);
+            break;
+        }
+        break;
+    case ADSI_STANDARD_TDD:
+        t = "The quick Brown Fox Jumps Over The Lazy dog 0123456789!@#$%^&*()";
+        len = adsi_add_field(s, msg, -1, 0, (uint8_t *) t, strlen(t));
+        break;
+    }
+    cycle++;
+    return len;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void put_adsi_msg(void *user_data, const uint8_t *msg, int len)
+{
+    int i;
+    int l;
+    uint8_t field_type;
+    const uint8_t *field_body;
+    int field_len;
+    int message_type;
+    uint8_t body[256];
+    
+    printf("Good message received (%d bytes)\n", len);
+    good_message_received = TRUE;
+    for (i = 0;  i < len;  i++)
+    {
+        printf("%02x ", msg[i]);
+        if ((i & 0xF) == 0xF)
+            printf("\n");
+    }
+    printf("\n");
+    l = -1;
+    message_type = -1;
+    printf("Message breakdown\n");
+    do
+    {
+        l = adsi_next_field(&rx_adsi, msg, len, l, &field_type, &field_body, &field_len);
+        if (l > 0)
+        {
+            if (field_body)
+            {
+                memcpy(body, field_body, field_len);
+                body[field_len] = '\0';
+                printf("Field type 0x%x, len %d, '%s' - ", field_type, field_len, body);
+                switch (current_standard)
+                {
+                case ADSI_STANDARD_CLASS:
+                    switch (message_type)
+                    {
+                    case CLASS_SDMF_CALLERID:
+                        break;
+                    case CLASS_MDMF_CALLERID:
+                        switch (field_type)
+                        {
+                        case MCLASS_DATETIME:
+                            printf("Date and time (MMDDHHMM)");
+                            break;
+                        case MCLASS_CALLER_NUMBER:
+                            printf("Caller's number");
+                            break;
+                        case MCLASS_DIALED_NUMBER:
+                            printf("Dialed number");
+                            break;
+                        case MCLASS_ABSENCE1:
+                            printf("Caller's number absent: 'O' or 'P'");
+                            break;
+                        case MCLASS_REDIRECT:
+                            printf("Call forward: universal ('0'), on busy ('1'), or on unanswered ('2')");
+                            break;
+                        case MCLASS_QUALIFIER:
+                            printf("Long distance: 'L'");
+                            break;
+                        case MCLASS_CALLER_NAME:
+                            printf("Caller's name");
+                            break;
+                        case MCLASS_ABSENCE2:
+                            printf("Caller's name absent: 'O' or 'P'");
+                            break;
+                        }
+                        break;
+                    case CLASS_SDMF_MSG_WAITING:
+                        break;
+                    case CLASS_MDMF_MSG_WAITING:
+                        switch (field_type)
+                        {
+                        case MCLASS_VISUAL_INDICATOR:
+                            printf("Message waiting/not waiting");
+                            break;
+                        }
+                        break;
+                    }
+                    break;
+                case ADSI_STANDARD_CLIP:
+                    switch (message_type)
+                    {
+                    case CLIP_MDMF_CALLERID:
+                    case CLIP_MDMF_MSG_WAITING:
+                    case CLIP_MDMF_CHARGE_INFO:
+                    case CLIP_MDMF_SMS:
+                        switch (field_type)
+                        {
+                        case CLIP_DATETIME:
+                            printf("Date and time (MMDDHHMM)");
+                            break;
+                        case CLIP_CALLER_NUMBER:
+                            printf("Caller's number");
+                            break;
+                        case CLIP_DIALED_NUMBER:
+                            printf("Dialed number");
+                            break;
+                        case CLIP_ABSENCE1:
+                            printf("Caller's number absent");
+                            break;
+                        case CLIP_CALLER_NAME:
+                            printf("Caller's name");
+                            break;
+                        case CLIP_ABSENCE2:
+                            printf("Caller's name absent");
+                            break;
+                        case CLIP_VISUAL_INDICATOR:
+                            printf("Visual indicator");
+                            break;
+                        case CLIP_MESSAGE_ID:
+                            printf("Message ID");
+                            break;
+                        case CLIP_CALLTYPE:
+                            printf("Voice call, ring-back-when-free call, or msg waiting call");
+                            break;
+                        case CLIP_NUM_MSG:
+                            printf("Number of messages");
+                            break;
+#if 0
+                        case CLIP_REDIR_NUMBER:
+                            printf("Redirecting number");
+                            break;
+#endif
+                        case CLIP_CHARGE:
+                            printf("Charge");
+                            break;
+                        case CLIP_DURATION:
+                            printf("Duration of the call");
+                            break;
+                        case CLIP_ADD_CHARGE:
+                            printf("Additional charge");
+                            break;
+                        case CLIP_DISPLAY_INFO:
+                            printf("Display information");
+                            break;
+                        case CLIP_SERVICE_INFO:
+                            printf("Service information");
+                            break;
+                        }
+                        break;
+                    }
+                    break;
+                case ADSI_STANDARD_ACLIP:
+                    switch (message_type)
+                    {
+                    case ACLIP_SDMF_CALLERID:
+                        break;
+                    case ACLIP_MDMF_CALLERID:
+                        switch (field_type)
+                        {
+                        case ACLIP_DATETIME:
+                            printf("Date and time (MMDDHHMM)");
+                            break;
+                        case ACLIP_CALLER_NUMBER:
+                            printf("Caller's number");
+                            break;
+                        case ACLIP_DIALED_NUMBER:
+                            printf("Dialed number");
+                            break;
+                        case ACLIP_NUMBER_ABSENCE:
+                            printf("Caller's number absent: 'O' or 'P'");
+                            break;
+                        case ACLIP_REDIRECT:
+                            printf("Call forward: universal, on busy, or on unanswered");
+                            break;
+                        case ACLIP_QUALIFIER:
+                            printf("Long distance call: 'L'");
+                            break;
+                        case ACLIP_CALLER_NAME:
+                            printf("Caller's name");
+                            break;
+                        case ACLIP_NAME_ABSENCE:
+                            printf("Caller's name absent: 'O' or 'P'");
+                            break;
+                        }
+                        break;
+                    }
+                    break;
+                case ADSI_STANDARD_JCLIP:
+                    switch (message_type)
+                    {
+                    case JCLIP_MDMF_CALLERID:
+                        switch (field_type)
+                        {
+                        case JCLIP_CALLER_NUMBER:
+                            printf("Caller's number");
+                            break;
+                        case JCLIP_CALLER_NUM_DES:
+                            printf("Caller's number data extension signal");
+                            break;
+                        case JCLIP_DIALED_NUMBER:
+                            printf("Dialed number");
+                            break;
+                        case JCLIP_DIALED_NUM_DES:
+                            printf("Dialed number data extension signal");
+                            break;
+                        case JCLIP_ABSENCE:
+                            printf("Caller's number absent: 'C', 'O', 'P' or 'S'");
+                            break;
+                        }
+                        break;
+                    }
+                    break;
+                case ADSI_STANDARD_CLIP_DTMF:
+                    switch (message_type)
+                    {
+                    case CLIP_DTMF_HASH_TERMINATED:
+                        switch (field_type)
+                        {
+                        case CLIP_DTMF_HASH_CALLER_NUMBER:
+                            printf("Caller's number");
+                            break;
+                        case CLIP_DTMF_HASH_ABSENCE:
+                            printf("Caller's number absent: private (1), overseas (2) or not available (3)");
+                            break;
+                        case CLIP_DTMF_HASH_UNSPECIFIED:
+                            printf("Unspecified");
+                            break;
+                        }
+                        break;
+                    case CLIP_DTMF_C_TERMINATED:
+                        switch (field_type)
+                        {
+                        case CLIP_DTMF_C_CALLER_NUMBER:
+                            printf("Caller's number");
+                            break;
+                        case CLIP_DTMF_C_REDIRECT_NUMBER:
+                            printf("Redirect number");
+                            break;
+                        case CLIP_DTMF_C_ABSENCE:
+                            printf("Caller's number absent");
+                            break;
+                        }
+                        break;
+                    }
+                    break;
+                case ADSI_STANDARD_TDD:
+                    if (len != 59
+                        ||
+                        memcmp(msg, "THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG 0123456789#$*()", 59))
+                    {
+                        printf("\n");
+                        printf("String error\n");
+                        exit(2);
+                    }
+                    break;
+                }
+            }
+            else
+            {
+                printf("Message type 0x%x - ", field_type);
+                message_type = field_type;
+                switch (current_standard)
+                {
+                case ADSI_STANDARD_CLASS:
+                    switch (message_type)
+                    {
+                    case CLASS_SDMF_CALLERID:
+                        printf("Single data message caller ID");
+                        break;
+                    case CLASS_MDMF_CALLERID:
+                        printf("Multiple data message caller ID");
+                        break;
+                    case CLASS_SDMF_MSG_WAITING:
+                        printf("Single data message message waiting");
+                        break;
+                    case CLASS_MDMF_MSG_WAITING:
+                        printf("Multiple data message message waiting");
+                        break;
+                    default:
+                        printf("Unknown");
+                        break;
+                    }
+                    break;
+                case ADSI_STANDARD_CLIP:
+                    switch (message_type)
+                    {
+                    case CLIP_MDMF_CALLERID:
+                        printf("Multiple data message caller ID");
+                        break;
+                    case CLIP_MDMF_MSG_WAITING:
+                        printf("Multiple data message message waiting");
+                        break;
+                    case CLIP_MDMF_CHARGE_INFO:
+                        printf("Multiple data message charge info");
+                        break;
+                    case CLIP_MDMF_SMS:
+                        printf("Multiple data message SMS");
+                        break;
+                    default:
+                        printf("Unknown");
+                        break;
+                    }
+                    break;
+                case ADSI_STANDARD_ACLIP:
+                    switch (message_type)
+                    {
+                    case ACLIP_SDMF_CALLERID:
+                        printf("Single data message caller ID frame");
+                        break;
+                    case ACLIP_MDMF_CALLERID:
+                        printf("Multiple data message caller ID frame");
+                        break;
+                    default:
+                        printf("Unknown");
+                        break;
+                    }
+                    break;
+                case ADSI_STANDARD_JCLIP:
+                    switch (message_type)
+                    {
+                    case JCLIP_MDMF_CALLERID:
+                        printf("Multiple data message caller ID frame");
+                        break;
+                    default:
+                        printf("Unknown");
+                        break;
+                    }
+                    break;
+                case ADSI_STANDARD_CLIP_DTMF:
+                    switch (message_type)
+                    {
+                    case CLIP_DTMF_HASH_TERMINATED:
+                        printf("# terminated");
+                        break;
+                    case CLIP_DTMF_C_TERMINATED:
+                        printf("C terminated");
+                        break;
+                    default:
+                        printf("Unknown");
+                        break;
+                    }
+                    break;
+                case ADSI_STANDARD_TDD:
+                    printf("Unknown");
+                    break;
+                }
+            }
+            printf("\n");
+        }
+    }
+    while (l > 0);
+    if (l < -1)
+    {
+        /* This message appears corrupt */
+        printf("Bad message contents\n");
+        exit(2);
+    }
+    printf("\n");
+}
+/*- End of function --------------------------------------------------------*/
+
+int main(int argc, char *argv[])
+{
+    int16_t amp[BLOCK_LEN];
+    uint8_t adsi_msg[256 + 42];
+    int adsi_msg_len;
+    AFfilehandle inhandle;
+    AFfilehandle outhandle;
+    int outframes;
+    int len;
+    int i;
+    int push;
+    int log_audio;
+    int short_preamble;
+    int test_standard;
+    int first_standard;
+    int last_standard;
+    int opt;
+
+    log_audio = FALSE;
+    decode_test_file = NULL;
+    test_standard = -1;
+    short_preamble = FALSE;
+    while ((opt = getopt(argc, argv, "d:lps:")) != -1)
+    {
+        switch (opt)
+        {
+        case 'd':
+            decode_test_file = optarg;
+            break;
+        case 'l':
+            log_audio = TRUE;
+            break;
+        case 'p':
+            short_preamble = TRUE;
+            break;
+        case 's':
+            if (strcasecmp("CLASS", optarg) == 0)
+                test_standard = ADSI_STANDARD_CLASS;
+            else if (strcasecmp("CLIP", optarg) == 0)
+                test_standard = ADSI_STANDARD_CLIP;
+            else if (strcasecmp("A-CLIP", optarg) == 0)
+                test_standard = ADSI_STANDARD_ACLIP;
+            else if (strcasecmp("J-CLIP", optarg) == 0)
+                test_standard = ADSI_STANDARD_JCLIP;
+            else if (strcasecmp("CLIP-DTMF", optarg) == 0)
+                test_standard = ADSI_STANDARD_CLIP_DTMF;
+            else if (strcasecmp("TDD", optarg) == 0)
+                test_standard = ADSI_STANDARD_TDD;
+            else
+                test_standard = atoi(optarg);
+            break;
+        default:
+            //usage();
+            exit(2);
+            break;
+        }
+    }
+    outhandle = AF_NULL_FILEHANDLE;
+    
+#if 0
+    /* This part tests internal static routines in the ADSI module. It can
+       only be run with a modified version of the ADSI module, which makes
+       the routines visible. */
+    /* Check the character encode/decode cycle */
+    current_standard = ADSI_STANDARD_TDD;
+    adsi_tx_init(&tx_adsi, ADSI_STANDARD_TDD);
+    adsi_rx_init(&rx_adsi, ADSI_STANDARD_TDD, put_adsi_msg, NULL);
+    s = "The quick Brown Fox Jumps Over The Lazy dog 0123456789!@#$%^&*()";
+    while ((ch = *s++))
+    {
+        xx = adsi_encode_baudot(&tx_adsi, ch);
+        if ((xx & 0x3E0))
+        {
+            yy = adsi_decode_baudot(&rx_adsi, (xx >> 5) & 0x1F);
+            if (yy)
+                printf("%c", yy);
+        }
+        yy = adsi_decode_baudot(&rx_adsi, xx & 0x1F);
+        if (yy)
+            printf("%c", yy);
+    }
+    printf("\n");
+#endif
+
+    if (decode_test_file)
+    {
+        /* We will decode the audio from a wave file. */
+        if ((inhandle = afOpenFile_telephony_read(decode_test_file, 1)) == AF_NULL_FILEHANDLE)
+        {
+            fprintf(stderr, "    Cannot open wave file '%s'\n", decode_test_file);
+            exit(2);
+        }
+        if (test_standard < 0)
+            current_standard = ADSI_STANDARD_CLASS;
+        else
+            current_standard = test_standard;
+
+        adsi_rx_init(&rx_adsi, current_standard, put_adsi_msg, NULL);
+        span_log_set_level(&rx_adsi.logging, SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_FLOW);
+        span_log_set_tag(&rx_adsi.logging, "ADSI");
+        for (;;)
+        {
+            len = afReadFrames(inhandle,
+                               AF_DEFAULT_TRACK,
+                               amp,
+                               BLOCK_LEN);
+            if (len == 0)
+                break;
+            adsi_rx(&rx_adsi, amp, len);
+        }
+        if (afCloseFile(inhandle) != 0)
+        {
+            fprintf(stderr, "    Cannot close wave file '%s'\n", decode_test_file);
+            exit(2);
+        }
+    }
+    else
+    {
+        if (log_audio)
+        {
+            if ((outhandle = afOpenFile_telephony_write(OUT_FILE_NAME, 1)) == AF_NULL_FILEHANDLE)
+            {
+                fprintf(stderr, "    Cannot create wave file '%s'\n", OUT_FILE_NAME);
+                exit(2);
+            }
+        }
+        /* Go through all the standards */
+        /* This assumes standard 0 is NULL, and TDD is the last in the list */
+        if (test_standard < 0)
+        {
+            first_standard = ADSI_STANDARD_CLASS;
+            last_standard = ADSI_STANDARD_TDD;
+        }
+        else
+        {
+            first_standard =
+            last_standard = test_standard;
+        }
+        for (current_standard = first_standard;  current_standard <= last_standard;  current_standard++)
+        {
+            printf("Testing %s\n", adsi_standard_to_str(current_standard));
+            adsi_tx_init(&tx_adsi, current_standard);
+            if (short_preamble)
+                adsi_tx_set_preamble(&tx_adsi, 50, 20, 5, -1);
+            adsi_rx_init(&rx_adsi, current_standard, put_adsi_msg, NULL);
+
+            /* Fake an OK condition for the first message test */
+            good_message_received = TRUE;
+            push = 0;
+            for (i = 0;  i < 100000;  i++)
+            {
+                if (push == 0)
+                {
+                    if ((len = adsi_tx(&tx_adsi, amp, BLOCK_LEN)) == 0)
+                        push = 10;
+                }
+                else
+                {
+                    len = 0;
+                    /* Push a little silence through, to flush things out */
+                    if (--push == 0)
+                    {
+                        if (!good_message_received)
+                        {
+                            printf("No message received %s (%d)\n", adsi_standard_to_str(current_standard), i);
+                            exit(2);
+                        }
+                        good_message_received = FALSE;
+                        adsi_msg_len = adsi_create_message(&tx_adsi, adsi_msg);
+                        adsi_msg_len = adsi_tx_put_message(&tx_adsi, adsi_msg, adsi_msg_len);
+                    }
+                }
+                if (len < BLOCK_LEN)
+                {
+                    memset(&amp[len], 0, sizeof(int16_t)*(BLOCK_LEN - len));
+                    len = BLOCK_LEN;
+                }
+                if (log_audio)
+                {
+                    outframes = afWriteFrames(outhandle,
+                                              AF_DEFAULT_TRACK,
+                                              amp,
+                                              len);
+                    if (outframes != len)
+                    {
+                        fprintf(stderr, "    Error writing wave file\n");
+                        exit(2);
+                    }
+                }
+                adsi_rx(&rx_adsi, amp, len);
+            }
+        }
+        if (log_audio)
+        {
+            if (afCloseFile(outhandle) != 0)
+            {
+                fprintf(stderr, "    Cannot close wave file '%s'\n", OUT_FILE_NAME);
+                exit(2);
+            }
+        }
+    }
+    
+    printf("Tests passed.\n");
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/tests/async_tests.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/tests/async_tests.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,246 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * async_tests.c - Tests for asynchronous serial processing.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2004 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: async_tests.c,v 1.17 2008/05/13 13:17:25 steveu Exp $
+ */
+
+/*! \file */
+
+/*! \page async_tests_page Asynchronous bit stream tests
+\section async_tests_page_sec_1 What does it do?
+*/
+
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <audiofile.h>
+
+#include "spandsp.h"
+
+async_rx_state_t rx_async;
+async_tx_state_t tx_async;
+
+int full_len;
+uint8_t old_buf[1000];
+uint8_t new_buf[1000];
+
+volatile int tx_async_chars;
+volatile int rx_async_chars;
+volatile int rx_async_char_mask;
+
+int v14_test_async_tx_get_bit(void *user_data);
+
+int v14_test_async_tx_get_bit(void *user_data)
+{
+    async_tx_state_t *s;
+    int bit;
+    static int destuff = 0;
+    
+    /* Special routine to test V.14 rate adaption, by randomly skipping
+       stop bits. */
+    s = (async_tx_state_t *) user_data;
+    if (s->bitpos == 0)
+    {
+        /* Start bit */
+        bit = 0;
+        s->byte_in_progress = s->get_byte(s->user_data);
+        s->parity_bit = 0;
+        s->bitpos++;
+    }
+    else if (s->bitpos <= s->data_bits)
+    {
+        bit = s->byte_in_progress & 1;
+        s->byte_in_progress >>= 1;
+        s->parity_bit ^= bit;
+        s->bitpos++;
+        if (!s->parity  &&  s->bitpos == s->data_bits + 1)
+        {
+            /* Drop the stop bit on every fourth character for V.14 simulation*/
+            if ((++destuff & 3) == 0)
+                s->bitpos = 0;
+        }
+    }
+    else if (s->parity  &&  s->bitpos == s->data_bits + 1)
+    {
+        if (s->parity == ASYNC_PARITY_ODD)
+            s->parity_bit ^= 1;
+        bit = s->parity_bit;
+        s->bitpos++;
+        /* Drop the stop bit on every fourth character for V.14 simulation */
+        if ((++destuff & 3) == 0)
+            s->bitpos = 0;
+    }
+    else
+    {
+        /* Stop bit(s) */
+        bit = 1;
+        s->bitpos++;
+        if (s->bitpos > s->data_bits + s->stop_bits)
+            s->bitpos = 0;
+    }
+    return bit;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int test_get_async_byte(void *user_data)
+{
+    int byte;
+    
+    byte = tx_async_chars & 0xFF;
+    tx_async_chars++;
+    return byte;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void test_put_async_byte(void *user_data, int byte)
+{
+    if ((rx_async_chars & rx_async_char_mask) != byte)
+        printf("Received byte is 0x%X (expected 0x%X)\n", byte, rx_async_chars);
+    rx_async_chars++;
+}
+/*- End of function --------------------------------------------------------*/
+
+int main(int argc, char *argv[])
+{
+    int bit;
+
+    printf("Test with async 8N1\n");
+    async_tx_init(&tx_async, 8, ASYNC_PARITY_NONE, 1, FALSE, test_get_async_byte, NULL);
+    async_rx_init(&rx_async, 8, ASYNC_PARITY_NONE, 1, FALSE, test_put_async_byte, NULL);
+    tx_async_chars = 0;
+    rx_async_chars = 0;
+    rx_async_char_mask = 0xFF;
+    while (rx_async_chars < 1000)
+    {
+        bit = async_tx_get_bit(&tx_async);
+        async_rx_put_bit(&rx_async, bit);
+    }
+    printf("Chars=%d/%d, PE=%d, FE=%d\n", tx_async_chars, rx_async_chars, rx_async.parity_errors, rx_async.framing_errors);
+    if (tx_async_chars != rx_async_chars
+        ||
+        rx_async.parity_errors
+        ||
+        rx_async.framing_errors)
+    {
+        printf("Test failed.\n");
+        exit(2);
+    }
+    
+    printf("Test with async 7E1\n");
+    async_tx_init(&tx_async, 7, ASYNC_PARITY_EVEN, 1, FALSE, test_get_async_byte, NULL);
+    async_rx_init(&rx_async, 7, ASYNC_PARITY_EVEN, 1, FALSE, test_put_async_byte, NULL);
+    tx_async_chars = 0;
+    rx_async_chars = 0;
+    rx_async_char_mask = 0x7F;
+    while (rx_async_chars < 1000)
+    {
+        bit = async_tx_get_bit(&tx_async);
+        async_rx_put_bit(&rx_async, bit);
+    }
+    printf("Chars=%d/%d, PE=%d, FE=%d\n", tx_async_chars, rx_async_chars, rx_async.parity_errors, rx_async.framing_errors);
+    if (tx_async_chars != rx_async_chars
+        ||
+        rx_async.parity_errors
+        ||
+        rx_async.framing_errors)
+    {
+        printf("Test failed.\n");
+        exit(2);
+    }
+
+    printf("Test with async 8O1\n");
+    async_tx_init(&tx_async, 8, ASYNC_PARITY_ODD, 1, FALSE, test_get_async_byte, NULL);
+    async_rx_init(&rx_async, 8, ASYNC_PARITY_ODD, 1, FALSE, test_put_async_byte, NULL);
+    tx_async_chars = 0;
+    rx_async_chars = 0;
+    rx_async_char_mask = 0xFF;
+    while (rx_async_chars < 1000)
+    {
+        bit = async_tx_get_bit(&tx_async);
+        async_rx_put_bit(&rx_async, bit);
+    }
+    printf("Chars=%d/%d, PE=%d, FE=%d\n", tx_async_chars, rx_async_chars, rx_async.parity_errors, rx_async.framing_errors);
+    if (tx_async_chars != rx_async_chars
+        ||
+        rx_async.parity_errors
+        ||
+        rx_async.framing_errors)
+    {
+        printf("Test failed.\n");
+        exit(2);
+    }
+
+    printf("Test with async 8O1 and V.14\n");
+    async_tx_init(&tx_async, 8, ASYNC_PARITY_ODD, 1, TRUE, test_get_async_byte, NULL);
+    async_rx_init(&rx_async, 8, ASYNC_PARITY_ODD, 1, TRUE, test_put_async_byte, NULL);
+    tx_async_chars = 0;
+    rx_async_chars = 0;
+    rx_async_char_mask = 0xFF;
+    while (rx_async_chars < 1000)
+    {
+        bit = v14_test_async_tx_get_bit(&tx_async);
+        async_rx_put_bit(&rx_async, bit);
+    }
+    printf("Chars=%d/%d, PE=%d, FE=%d\n", tx_async_chars, rx_async_chars, rx_async.parity_errors, rx_async.framing_errors);
+    if (tx_async_chars != rx_async_chars + 1
+        ||
+        rx_async.parity_errors
+        ||
+        rx_async.framing_errors)
+    {
+        printf("Test failed.\n");
+        exit(2);
+    }
+
+    printf("Test with async 5N2\n");
+    async_tx_init(&tx_async, 5, ASYNC_PARITY_NONE, 2, FALSE, test_get_async_byte, NULL);
+    async_rx_init(&rx_async, 5, ASYNC_PARITY_NONE, 2, FALSE, test_put_async_byte, NULL);
+    tx_async_chars = 0;
+    rx_async_chars = 0;
+    rx_async_char_mask = 0x1F;
+    while (rx_async_chars < 1000)
+    {
+        bit = async_tx_get_bit(&tx_async);
+        async_rx_put_bit(&rx_async, bit);
+    }
+    printf("Chars=%d/%d, PE=%d, FE=%d\n", tx_async_chars, rx_async_chars, rx_async.parity_errors, rx_async.framing_errors);
+    if (tx_async_chars != rx_async_chars
+        ||
+        rx_async.parity_errors
+        ||
+        rx_async.framing_errors)
+    {
+        printf("Test failed.\n");
+        exit(2);
+    }
+
+    printf("Tests passed.\n");
+    return  0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/tests/at_interpreter_tests.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/tests/at_interpreter_tests.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,557 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * at_interpreter_tests.c - Tests for the AT interpreter.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2004, 2005, 2006 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: at_interpreter_tests.c,v 1.18 2008/08/17 16:25:52 steveu Exp $
+ */
+
+/*! \file */
+
+/*! \page at_interpreter_tests_page AT interpreter tests
+\section at_interpreter_tests_page_sec_1 What does it do?
+These tests exercise all the commands which should be understood by the AT interpreter.
+*/
+
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <string.h>
+#include <assert.h>
+#include <audiofile.h>
+
+#include "spandsp.h"
+
+#define DLE 0x10
+#define ETX 0x03
+#define SUB 0x1A
+
+#define MANUFACTURER            "www.soft-switch.org"
+
+struct command_response_s
+{
+    const char *command;
+    const char *response;
+};
+
+static const struct command_response_s general_test_seq[] =
+{
+    /* Try the various cases for "AT" */
+    {"atE1\r", "atE1\r\r\nOK\r\n"},
+    {"AtE1\r", "AtE1\r\r\nOK\r\n"},
+    {"aTE1\r", "aTE1\r\r\nOK\r\n"},
+    {"ATE1\r", "ATE1\r\r\nOK\r\n"},
+    {"ATE0\r", "ATE0\r\r\nOK\r\n"},
+
+    /* Try the various command formats */
+    {"ATS8?\r", "\r\n005\r\n\r\nOK\r\n"},
+    {"ATS8=1\r", "\r\nOK\r\n"},
+    {"ATS8.5?\r", "\r\n0\r\n\r\nOK\r\n"},
+    {"ATS8.5=1\r", "\r\nOK\r\n"},
+    {"ATS8.5?\r", "\r\n1\r\n\r\nOK\r\n"},
+    {"ATS8?\r", "\r\n033\r\n\r\nOK\r\n"},
+    {"AT+FCLASS=1\r", "\r\nOK\r\n"},
+    {"AT+FCLASS?\r", "\r\n1\r\n\r\nOK\r\n"},
+    {"AT+FCLASS=?\r", "\r\n0,1,1.0\r\n\r\nOK\r\n"},
+
+    /* Try all the commands */
+    {"AT&C\r", "\r\nOK\r\n"},                                       /* V.250 6.2.8 - Circuit 109 (received line signal detector), behaviour */ 
+    {"AT&D\r", "\r\nOK\r\n"},                                       /* V.250 6.2.9 - Circuit 108 (data terminal ready) behaviour */ 
+    {"AT&F\r", "\r\nOK\r\n"},                                       /* V.250 6.1.2 - Set to factory-defined configuration */ 
+    {"ATE0\r", "ATE0\r\r\nOK\r\n"},                                 /* Counteract the effects of the above */
+    {"AT+A8E=?\r", "\r\n+A8E:(0-6),(0-5),(00-FF)\r\n\r\nOK\r\n"},   /* V.251 5.1 - V.8 and V.8bis operation controls */
+    {"AT+A8M\r", "\r\nOK\r\n"},                                     /* V.251 5.2 - Send V.8 menu signals */
+    {"AT+A8T=?\r", "\r\n+A8T:(0-10)\r\n\r\nOK\r\n"},                /* V.251 5.3 - Send V.8bis signal and/or message(s) */
+    {"AT+ASTO=?\r", "\r\n+ASTO:\r\n\r\nOK\r\n"},                    /* V.250 6.3.15 - Store telephone number */ 
+    {"AT+CAAP=?\r", "\r\n+CAAP:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 7.25 - Automatic answer for eMLPP Service */
+    {"AT+CACM=?\r", "\r\n+CACM:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 8.25 - Accumulated call meter */
+    {"AT+CACSP=?\r", "\r\n+CACSP:\r\n\r\nOK\r\n"},                  /* 3GPP TS 27.007 11.1.7 - Voice Group or Voice Broadcast Call State Attribute Presentation */
+    {"AT+CAEMLPP=?\r", "\r\n+CAEMLPP:\r\n\r\nOK\r\n"},              /* 3GPP TS 27.007 7.22 - eMLPP Priority Registration and Interrogation */
+    {"AT+CAHLD=?\r", "\r\n+CAHLD:\r\n\r\nOK\r\n"},                  /* 3GPP TS 27.007 11.1.3 - Leave an ongoing Voice Group or Voice Broadcast Call */
+    {"AT+CAJOIN=?\r", "\r\n+CAJOIN:\r\n\r\nOK\r\n"},                /* 3GPP TS 27.007 11.1.1 - Accept an incoming Voice Group or Voice Broadcast Call */
+    {"AT+CALA=?\r", "\r\n+CALA:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 8.16 - Alarm */
+    {"AT+CALCC=?\r", "\r\n+CALCC:\r\n\r\nOK\r\n"},                  /* 3GPP TS 27.007 11.1.6 - List current Voice Group and Voice Broadcast Calls */
+    {"AT+CALD=?\r", "\r\n+CALD:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 8.38 - Delete alar m */
+    {"AT+CALM=?\r", "\r\n+CALM:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 8.20 - Alert sound mode */
+    {"AT+CAMM=?\r", "\r\n+CAMM:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 8.26 - Accumulated call meter maximum */
+    {"AT+CANCHEV=?\r", "\r\n+CANCHEV:\r\n\r\nOK\r\n"},              /* 3GPP TS 27.007 11.1.8 - NCH Support Indication */
+    {"AT+CAOC=?\r", "\r\n+CAOC:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 7.16 - Advice of Charge */
+    {"AT+CAPD=?\r", "\r\n+CAPD:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 8.39 - Postpone or dismiss an alarm */
+    {"AT+CAPTT=?\r", "\r\n+CAPTT:\r\n\r\nOK\r\n"},                  /* 3GPP TS 27.007 11.1.4 - Talker Access for Voice Group Call */
+    {"AT+CAREJ=?\r", "\r\n+CAREJ:\r\n\r\nOK\r\n"},                  /* 3GPP TS 27.007 11.1.2 - Reject an incoming Voice Group or Voice Broadcast Call */
+    {"AT+CAULEV=?\r", "\r\n+CAULEV:\r\n\r\nOK\r\n"},                /* 3GPP TS 27.007 11.1.5 - Voice Group Call Uplink Status Presentation */
+    {"AT+CBC=?\r", "\r\n+CBC:\r\n\r\nOK\r\n"},                      /* 3GPP TS 27.007 8.4 - Battery charge */
+    {"AT+CBCS=?\r", "\r\n+CBCS:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 11.3.2 - VBS subscriptions and GId status */
+    {"AT+CBST=?\r", "\r\n+CBST:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 6.7 - Select bearer service type */
+    {"AT+CCFC=?\r", "\r\n+CCFC:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 7.11 - Call forwarding number and conditions */
+    {"AT+CCLK=?\r", "\r\n+CCLK:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 8.15 - Clock */
+    {"AT+CCUG=?\r", "\r\n+CCUG:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 7.10 - Closed user group */
+    {"AT+CCWA=?\r", "\r\n+CCWA:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 7.12 - Call waiting */
+    {"AT+CCWE=?\r", "\r\n+CCWE:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 8.28 - Call Meter maximum event */
+    {"AT+CDIP=?\r", "\r\n+CDIP:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 7.9 - Called line identification presentation */
+    {"AT+CDIS=?\r", "\r\n+CDIS:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 8.8 - Display control */
+    {"AT+CEER=?\r", "\r\n+CEER:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 6.10 - Extended error report */
+    {"AT+CFCS=?\r", "\r\n+CFCS:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 7.24 - Fast call setup conditions */
+    {"AT+CFUN=?\r", "\r\n+CFUN:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 8.2 - Set phone functionality */
+    {"AT+CGACT=?\r", "\r\n+CGACT:\r\n\r\nOK\r\n"},                  /* 3GPP TS 27.007 10.1.10 - PDP context activate or deactivate */
+    {"AT+CGANS=?\r", "\r\n+CGANS:\r\n\r\nOK\r\n"},                  /* 3GPP TS 27.007 10.1.16 - Manual response to a network request for PDP context activation */
+    {"AT+CGATT=?\r", "\r\n+CGATT:\r\n\r\nOK\r\n"},                  /* 3GPP TS 27.007 10.1.9 - PS attach or detach */
+    {"AT+CGAUTO=?\r", "\r\n+CGAUTO:\r\n\r\nOK\r\n"},                /* 3GPP TS 27.007 10.1.15 - Automatic response to a network request for PDP context activation */
+    {"AT+CGCLASS=?\r", "\r\n+CGCLASS:\r\n\r\nOK\r\n"},              /* 3GPP TS 27.007 10.1.17 - GPRS mobile station class (GPRS only) */
+    {"AT+CGCLOSP=?\r", "\r\n+CGCLOSP:\r\n\r\nOK\r\n"},              /* 3GPP TS 27.007 10.1.13 - Configure local octet stream PAD parameters (Obsolete) */
+    {"AT+CGCLPAD=?\r", "\r\n+CGCLPAD:\r\n\r\nOK\r\n"},              /* 3GPP TS 27.007 10.1.12 - Configure local triple-X PAD parameters (GPRS only) (Obsolete) */
+    {"AT+CGCMOD=?\r", "\r\n+CGCMOD:\r\n\r\nOK\r\n"},                /* 3GPP TS 27.007 10.1.11 - PDP Context Modify */
+    {"AT+CGCS=?\r", "\r\n+CGCS:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 11.3.1 - VGCS subscriptions and GId status */
+    {"AT+CGDATA=?\r", "\r\n+CGDATA:\r\n\r\nOK\r\n"},                /* 3GPP TS 27.007 10.1.12 - Enter data state */
+    {"AT+CGDCONT=?\r", "\r\n+CGDCONT:\r\n\r\nOK\r\n"},              /* 3GPP TS 27.007 10.1.1 - Define PDP Context */
+    {"AT+CGDSCONT=?\r", "\r\n+CGDSCONT:\r\n\r\nOK\r\n"},            /* 3GPP TS 27.007 10.1.2 - Define Secondary PDP Context */
+    {"AT+CGEQMIN=?\r", "\r\n+CGEQMIN:\r\n\r\nOK\r\n"},              /* 3GPP TS 27.007 10.1.7 - 3G Quality of Service Profile (Minimum acceptable) */
+    {"AT+CGEQNEG=?\r", "\r\n+CGEQNEG:\r\n\r\nOK\r\n"},              /* 3GPP TS 27.007 10.1.8 - 3G Quality of Service Profile (Negotiated) */
+    {"AT+CGEQREQ=?\r", "\r\n+CGEQREQ:\r\n\r\nOK\r\n"},              /* 3GPP TS 27.007 10.1.6 - 3G Quality of Service Profile (Requested) */
+    {"AT+CGEREP=?\r", "\r\n+CGEREP:\r\n\r\nOK\r\n"},                /* 3GPP TS 27.007 10.1.18 - Packet Domain event reporting */
+    {"AT+CGMI=?\r", "\r\n+CGMI:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 5.1 - Request manufacturer identification */
+    {"AT+CGMM=?\r", "\r\n+CGMM:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 5.2 - Request model identification */
+    {"AT+CGMR=?\r", "\r\n+CGMR:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 5.3 - Request revision identification */
+    {"AT+CGPADDR=?\r", "\r\n+CGPADDR:\r\n\r\nOK\r\n"},              /* 3GPP TS 27.007 10.1.14 - Show PDP address */
+    {"AT+CGQMIN=?\r", "\r\n+CGQMIN:\r\n\r\nOK\r\n"},                /* 3GPP TS 27.007 10.1.5 - Quality of Service Profile (Minimum acceptable) */
+    {"AT+CGQREQ=?\r", "\r\n+CGQREQ:\r\n\r\nOK\r\n"},                /* 3GPP TS 27.007 10.1.4 - Quality of Service Profile (Requested) */
+    {"AT+CGREG=?\r", "\r\n+CGREG:\r\n\r\nOK\r\n"},                  /* 3GPP TS 27.007 10.1.19 - GPRS network registration status */
+    {"AT+CGSMS=?\r", "\r\n+CGSMS:\r\n\r\nOK\r\n"},                  /* 3GPP TS 27.007 10.1.20 - Select service for MO SMS messages */
+    {"AT+CGSN=?\r", "\r\n+CGSN:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 5.4 - Request product serial number identification */
+    {"AT+CGTFT=?\r", "\r\n+CGTFT:\r\n\r\nOK\r\n"},                  /* 3GPP TS 27.007 10.1.3 - Traffic Flow Template */
+    {"AT+CHLD=?\r", "\r\n+CHLD:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 7.13 - Call related supplementary services */
+    {"AT+CHSA=?\r", "\r\n+CHSA:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 6.18 - HSCSD non-transparent asymmetry configuration */
+    {"AT+CHSC=?\r", "\r\n+CHSC:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 6.15 - HSCSD current call parameters */
+    {"AT+CHSD=?\r", "\r\n+CHSD:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 6.12 - HSCSD device parameters */
+    {"AT+CHSN=?\r", "\r\n+CHSN:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 6.14 - HSCSD non-transparent call configuration */
+    {"AT+CHSR=?\r", "\r\n+CHSR:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 6.16 - HSCSD parameters report */
+    {"AT+CHST=?\r", "\r\n+CHST:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 6.13 - HSCSD transparent call configuration */
+    {"AT+CHSU=?\r", "\r\n+CHSU:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 6.17 - HSCSD automatic user initiated upgrading */
+    {"AT+CHUP=?\r", "\r\n+CHUP:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 6.5 - Hangup call */
+    {"AT+CIMI=?\r", "\r\n+CIMI:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 5.6 - Request international mobile subscriber identity */
+    {"AT+CIND=?\r", "\r\n+CIND:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 8.9 - Indicator control */
+    {"AT+CKPD=?\r", "\r\n+CKPD:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 8.7 - Keypad control */
+    {"AT+CLAC=?\r", "\r\n+CLAC:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 8.37 - List all available AT commands */
+    {"AT+CLAE=?\r", "\r\n+CLAE:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 8.31 - Language Event */
+    {"AT+CLAN=?\r", "\r\n+CLAN:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 8.30 - Set Language */
+    {"AT+CLCC=?\r", "\r\n+CLCC:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 7.18 - List current calls */
+    {"AT+CLCK=?\r", "\r\n+CLCK:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 7.4 - Facility lock */
+    {"AT+CLIP=?\r", "\r\n+CLIP:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 7.6 - Calling line identification presentation */
+    {"AT+CLIR=?\r", "\r\n+CLIR:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 7.7 - Calling line identification restriction */
+    {"AT+CLVL=?\r", "\r\n+CLVL:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 8.23 - Loudspeaker volume level */
+    {"AT+CMAR=?\r", "\r\n+CMAR:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 8.36 - Master Reset */
+    {"AT+CMEC=?\r", "\r\n+CMEC:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 8.6 - Mobile Termination control mode */
+    {"AT+CMER=?\r", "\r\n+CMER:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 8.10 - Mobile Termination event reporting */
+    {"AT+CMOD=?\r", "\r\n+CMOD:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 6.4 - Call mode */
+    {"AT+CMUT=?\r", "\r\n+CMUT:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 8.24 - Mute control */
+    {"AT+CMUX=?\r", "\r\n+CMUX:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 5.7 - Multiplexing mode */
+    {"AT+CNUM=?\r", "\r\n+CNUM:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 7.1 - Subscriber number */
+    {"AT+COLP=?\r", "\r\n+COLP:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 7.8 - Connected line identification presentation */
+    {"AT+COPN=?\r", "\r\n+COPN:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 7.21 - Read operator names */
+    {"AT+COPS=?\r", "\r\n+COPS:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 7.3 - PLMN selection */
+    {"AT+COTDI=?\r", "\r\n+COTDI:\r\n\r\nOK\r\n"},                  /* 3GPP TS 27.007 11.1.9 - Originator to Dispatcher Information */
+    {"AT+CPAS=?\r", "\r\n+CPAS:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 8.1 - Phone activity status */
+    {"AT+CPBF=?\r", "\r\n+CPBF:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 8.13 - Find phonebook entries */
+    {"AT+CPBR=?\r", "\r\n+CPBR:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 8.12 - Read phonebook entries */
+    {"AT+CPBS=?\r", "\r\n+CPBS:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 8.11 - Select phonebook memory storage */
+    {"AT+CPBW=?\r", "\r\n+CPBW:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 8.14 - Write phonebook entry */
+    {"AT+CPIN=?\r", "\r\n+CPIN:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 8.3 - Enter PIN */
+    {"AT+CPLS=?\r", "\r\n+CPLS:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 7.20 - Selection of preferred PLMN list */
+    {"AT+CPOL=?\r", "\r\n+CPOL:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 7.19 - Preferred PLMN list */
+    {"AT+CPPS=?\r", "\r\n+CPPS:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 7.23 - eMLPP subscriptions */
+    {"AT+CPROT=?\r", "\r\n+CPROT:\r\n\r\nOK\r\n"},                  /* 3GPP TS 27.007 8.42 - Enter protocol mode */
+    {"AT+CPUC=?\r", "\r\n+CPUC:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 8.27 - Price per unit and currency table */
+    {"AT+CPWC=?\r", "\r\n+CPWC:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 8.29 - Power class */
+    {"AT+CPWD=?\r", "\r\n+CPWD:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 7.5 - Change password */
+    {"AT+CR=?\r", "\r\n+CR:\r\n\r\nOK\r\n"},                        /* 3GPP TS 27.007 6.9 - Service reporting control */
+    {"AT+CRC=?\r", "\r\n+CRC:\r\n\r\nOK\r\n"},                      /* 3GPP TS 27.007 6.11 - Cellular result codes */
+    {"AT+CREG=?\r", "\r\n+CREG:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 7.2 - Network registration */
+    {"AT+CRLP=?\r", "\r\n+CRLP:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 6.8 - Radio link protocol */
+    {"AT+CRMC=?\r", "\r\n+CRMC:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 8.34 - Ring Melody Control */
+    {"AT+CRMP=?\r", "\r\n+CRMP:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 8.35 - Ring Melody Playback */
+    {"AT+CRSL=?\r", "\r\n+CRSL:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 8.21 - Ringer sound level */
+    {"AT+CRSM=?\r", "\r\n+CRSM:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 8.18 - Restricted SIM access */
+    {"AT+CSCC=?\r", "\r\n+CSCC:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 8.19 - Secure control command */
+    {"AT+CSCS=?\r", "\r\n+CSCS:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 5.5 - Select TE character set */
+    {"AT+CSDF=?\r", "\r\n+CSDF:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 6.22 - Settings date format */
+    {"AT+CSGT=?\r", "\r\n+CSGT:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 8.32 - Set Greeting Text */
+    {"AT+CSIL=?\r", "\r\n+CSIL:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 6.23 - Silence Command */
+    {"AT+CSIM=?\r", "\r\n+CSIM:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 8.17 - Generic SIM access */
+    {"AT+CSNS=?\r", "\r\n+CSNS:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 6.19 - Single numbering scheme */
+    {"AT+CSQ=?\r", "\r\n+CSQ:\r\n\r\nOK\r\n"},                      /* 3GPP TS 27.007 8.5 - Signal quality */
+    {"AT+CSSN=?\r", "\r\n+CSSN:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 7.17 - Supplementary service notifications */
+    {"AT+CSTA=?\r", "\r\n+CSTA:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 6.1 - Select type of address */
+    {"AT+CSTF=?\r", "\r\n+CSTF:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 6.24 - Settings time format */
+    {"AT+CSVM=?\r", "\r\n+CSVM:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 8.33 - Set Voice Mail Number */
+    {"AT+CTFR=?\r", "\r\n+CTFR:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 7.14 - Call deflection */
+    {"AT+CTZR=?\r", "\r\n+CTZR:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 8.41 - Time Zone Reporting */
+    {"AT+CTZU=?\r", "\r\n+CTZU:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 8.40 - Automatic Time Zone Update */
+    {"AT+CUSD=?\r", "\r\n+CUSD:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 7.15 - Unstructured supplementary service data */
+    {"AT+CUUS1=?\r", "\r\n+CUUS1:\r\n\r\nOK\r\n"},                  /* 3GPP TS 27.007 7.26 - User to User Signalling Service 1 */
+    {"AT+CV120=?\r", "\r\n+CV120:\r\n\r\nOK\r\n"},                  /* 3GPP TS 27.007 6.21 - V.120 rate adaption protocol */
+    {"AT+CVHU=?\r", "\r\n+CVHU:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 6.20 - Voice Hangup Control */
+    {"AT+CVIB=?\r", "\r\n+CVIB:\r\n\r\nOK\r\n"},                    /* 3GPP TS 27.007 8.22 - Vibrator mode */
+    {"AT+DR=?\r", "\r\n+DR:\r\n\r\nOK\r\n"},                        /* V.250 6.6.2 - Data compression reporting */ 
+    {"AT+DS=?\r", "\r\n+DS:\r\n\r\nOK\r\n"},                        /* V.250 6.6.1 - Data compression */ 
+    {"AT+EB=?\r", "\r\n+EB:\r\n\r\nOK\r\n"},                        /* V.250 6.5.2 - Break handling in error control operation */ 
+    {"AT+EFCS=?\r", "\r\n+EFCS:(0-2)\r\n\r\nOK\r\n"},               /* V.250 6.5.4 - 32-bit frame check sequence */ 
+    {"AT+EFCS?\r", "\r\n+EFCS:0\r\n\r\nOK\r\n"},
+    {"AT+EFRAM=?\r", "\r\n+EFRAM:(1-65535),(1-65535)\r\n\r\nOK\r\n"},     /* V.250 6.5.8 - Frame length */ 
+    {"AT+ER=?\r", "\r\n+ER:(0,1)\r\n\r\nOK\r\n"},                   /* V.250 6.5.5 - Error control reporting */ 
+    {"AT+ES\r", "\r\nOK\r\n"},                                      /* V.250 6.5.1 - Error control selection */ 
+    {"AT+ESR\r", "\r\nOK\r\n"},                                     /* V.250 6.5.3 - Selective repeat */ 
+    {"AT+ETBM=?\r", "\r\n+ETBM:(0-2),(0-2),(0-30)\r\n\r\nOK\r\n"},  /* T.31 8.5.1 - Adaptive reception control */ 
+    {"AT+ETBM?\r", "\r\n+ETBM:0,0\r\n\r\nOK\r\n"},
+    {"AT+EWIND=?\r", "\r\n+EWIND:(1-127),(1-127)\r\n\r\nOK\r\n"},   /* V.250 6.5.7 - Window size */ 
+    {"AT+EWIND?\r", "\r\n+EWIND:0,0\r\n\r\nOK\r\n"},
+    {"AT+FAR=?\r", "\r\n0,1\r\n\r\nOK\r\n"},                        /* T.31 8.5.1 - Adaptive reception control */ 
+    {"AT+FAR?\r", "\r\n0\r\n\r\nOK\r\n"},
+    {"AT+FCL=?\r", "\r\n(0-255)\r\n\r\nOK\r\n"},                    /* T.31 8.5.2 - Carrier loss timeout */ 
+    {"AT+FCLASS=?\r", "\r\n0,1,1.0\r\n\r\nOK\r\n"},                 /* T.31 8.2 - Capabilities identification and control */ 
+    {"AT+FCLASS?\r", "\r\n1\r\n\r\nOK\r\n"},
+    {"AT+FDD=?\r", "\r\n(0,1)\r\n\r\nOK\r\n"},                      /* T.31 8.5.3 - Double escape character replacement */ 
+    {"AT+FDD?\r", "\r\n0\r\n\r\nOK\r\n"},
+    {"AT+FIT=?\r", "\r\n+FIT:(0-255),(0-1)\r\n\r\nOK\r\n"},         /* T.31 8.5.4 - DTE inactivity timeout */ 
+    {"AT+FIT?\r", "\r\n+FIT:0,0\r\n\r\nOK\r\n"},
+    {"AT+FLO\r", "\r\nOK\r\n"},                                     /* T.31 says to implement something similar to +IFC */ 
+    {"AT+FMI?\r", "\r\n" MANUFACTURER "\r\n\r\nOK\r\n"},            /* T.31 says to duplicate +GMI */ 
+    {"AT+FMM?\r", "\r\n" PACKAGE "\r\n\r\nOK\r\n"},                 /* T.31 says to duplicate +GMM */ 
+    {"AT+FMR?\r", "\r\n" VERSION "\r\n\r\nOK\r\n"},                 /* T.31 says to duplicate +GMR */ 
+    {"AT+FPR=?\r", "\r\n115200\r\n\r\nOK\r\n"},                     /* T.31 says to implement something similar to +IPR */ 
+    {"AT+FPR?\r", "\r\n0\r\n\r\nOK\r\n"},
+    {"AT+FRH=?\r", "\r\n3\r\n\r\nOK\r\n"},                          /* T.31 8.3.6 - HDLC receive */ 
+    {"AT+FRH?\r", "\r\n-1\r\n\r\nOK\r\n"},
+    {"AT+FRM=?\r", "\r\n24,48,72,73,74,96,97,98,121,122,145,146\r\n\r\nOK\r\n"}, /* T.31 8.3.4 - Facsimile receive */ 
+    {"AT+FRM?\r", "\r\n-1\r\n\r\nOK\r\n"},
+    {"AT+FRS=?\r", "\r\n0-255\r\n\r\nOK\r\n"},                      /* T.31 8.3.2 - Receive silence */ 
+    {"AT+FRS?\r", "\r\n-1\r\n\r\nOK\r\n"},
+    {"AT+FTH=?\r", "\r\n3\r\n\r\nOK\r\n"},                          /* T.31 8.3.5 - HDLC transmit */ 
+    {"AT+FTH?\r", "\r\n-1\r\n\r\nOK\r\n"},
+    {"AT+FTM=?\r", "\r\n24,48,72,73,74,96,97,98,121,122,145,146\r\n\r\nOK\r\n"}, /* T.31 8.3.3 - Facsimile transmit */ 
+    {"AT+FTM?\r", "\r\n-1\r\n\r\nOK\r\n"},
+    {"AT+FTS=?\r", "\r\n0-255\r\n\r\nOK\r\n"},                      /* T.31 8.3.1 - Transmit silence */ 
+    {"AT+FTS?\r", "\r\n-1\r\n\r\nOK\r\n"},
+    {"AT+GCAP\r", "\r\nOK\r\n"},                                    /* V.250 6.1.9 - Request complete capabilities list */ 
+    {"AT+GCI=?\r", "\r\n+GCI:(00-FF)\r\n\r\nOK\r\n"},               /* V.250 6.1.10 - Country of installation, */ 
+    {"AT+GCI?\r", "\r\n+GCI:00\r\n\r\nOK\r\n"},
+    {"AT+GMI?\r", "\r\n" MANUFACTURER "\r\n\r\nOK\r\n"},            /* V.250 6.1.4 - Request manufacturer identification */ 
+    {"AT+GMM?\r", "\r\n" PACKAGE "\r\n\r\nOK\r\n"},                 /* V.250 6.1.5 - Request model identification */ 
+    {"AT+GMR?\r", "\r\n" VERSION "\r\n\r\nOK\r\n"},                 /* V.250 6.1.6 - Request revision identification */ 
+    {"AT+GOI\r", "\r\nOK\r\n"},                                     /* V.250 6.1.8 - Request global object identification */ 
+    {"AT+GSN?\r", "\r\n42\r\n\r\nOK\r\n"},                          /* V.250 6.1.7 - Request product serial number identification */ 
+    {"AT+ICF?\r", "\r\n+ICF:0,0\r\n\r\nOK\r\n"},                    /* V.250 6.2.11 - DTE-DCE character framing */ 
+    {"AT+ICLOK?\r", "\r\n+ICLOK:0\r\n\r\nOK\r\n"},                  /* V.250 6.2.14 - Select sync transmit clock source */ 
+    {"AT+IDSR?\r", "\r\n+IDSR:0\r\n\r\nOK\r\n"},                    /* V.250 6.2.16 - Select data set ready option */ 
+    {"AT+IFC\r", "\r\nOK\r\n"},                                     /* V.250 6.2.12 - DTE-DCE local flow control */ 
+    {"AT+ILRR\r", "\r\nOK\r\n"},                                    /* V.250 6.2.13 - DTE-DCE local rate reporting */ 
+    {"AT+ILSD=?\r", "\r\n+ILSD:(0,1)\r\n\r\nOK\r\n"},               /* V.250 6.2.15 - Select long space disconnect option */ 
+    {"AT+ILSD?\r", "\r\n+ILSD:0\r\n\r\nOK\r\n"},
+    {"AT+IPR=?\r", "\r\n+IPR:(115200),(115200)\r\n\r\nOK\r\n"},     /* V.250 6.2.10 - Fixed DTE rate */ 
+    {"AT+IPR?\r", "\r\n+IPR:0\r\n\r\nOK\r\n"},
+    {"AT+IRTS=?\r", "\r\n+IRTS:(0,1)\r\n\r\nOK\r\n"},               /* V.250 6.2.17 - Select synchronous mode RTS option */ 
+    {"AT+IRTS?\r", "\r\n+IRTS:0\r\n\r\nOK\r\n"},
+    {"AT+MA\r", "\r\nOK\r\n"},                                      /* V.250 6.4.2 - Modulation automode control */ 
+    {"AT+MR=?\r", "\r\n+MR:(0,1)\r\n\r\nOK\r\n"},                   /* V.250 6.4.3 - Modulation reporting control */ 
+    {"AT+MR?\r", "\r\n+MR:0\r\n\r\nOK\r\n"},
+    {"AT+MS\r", "\r\nOK\r\n"},                                      /* V.250 6.4.1 - Modulation selection */ 
+    {"AT+MSC=?\r", "\r\n+MSC:(0,1)\r\n\r\nOK\r\n"},                 /* V.250 6.4.8 - Seamless rate change enable */ 
+    {"AT+MSC?\r", "\r\n+MSC:0\r\n\r\nOK\r\n"},
+    {"AT+MV18AM\r", "\r\nOK\r\n"},                                  /* V.250 6.4.6 - V.18 answering message editing */ 
+    {"AT+MV18P=?\r", "\r\n+MV18P:(2-7)\r\n\r\nOK\r\n"},             /* V.250 6.4.7 - Order of probes */ 
+    {"AT+MV18P?\r", "\r\n+MV18P:0\r\n\r\nOK\r\n"},
+    {"AT+MV18R=?\r", "\r\n+MV18R:(0,1)\r\n\r\nOK\r\n"},             /* V.250 6.4.5 - V.18 reporting control */ 
+    {"AT+MV18R?\r", "\r\n+MV18R:0\r\n\r\nOK\r\n"},
+    {"AT+MV18S\r", "\r\nOK\r\n"},                                   /* V.250 6.4.4 - V.18 selection */ 
+    {"AT+TADR\r", "\r\nOK\r\n"},                                    /* V.250 6.7.2.9 - Local V.54 address */ 
+    {"AT+TAL=?\r", "\r\n+TAL:(0,1),(0,1)\r\n\r\nOK\r\n"},           /* V.250 6.7.2.15 - Local analogue loop */ 
+    {"AT+TAL?\r", "\r\n+TAL:0,0\r\n\r\nOK\r\n"},
+    {"AT+TALS=?\r", "\r\n+TALS:(0-3)\r\n\r\nOK\r\n"},               /* V.250 6.7.2.6 - Analogue loop status */ 
+    {"AT+TALS?\r", "\r\n+TALS:0\r\n\r\nOK\r\n"},
+    {"AT+TDLS=?\r", "\r\n+TDLS:(0-4)\r\n\r\nOK\r\n"},               /* V.250 6.7.2.7 - Local digital loop status */ 
+    {"AT+TDLS?\r", "\r\n+TDLS:0\r\n\r\nOK\r\n"},
+    {"AT+TE140=?\r", "\r\n+TE140:(0,1)\r\n\r\nOK\r\n"},             /* V.250 6.7.2.1 - Enable ckt 140 */ 
+    {"AT+TE140?\r", "\r\n+TE140:0\r\n\r\nOK\r\n"},
+    {"AT+TE141=?\r", "\r\n+TE141:(0,1)\r\n\r\nOK\r\n"},             /* V.250 6.7.2.2 - Enable ckt 141 */ 
+    {"AT+TE141?\r", "\r\n+TE141:0\r\n\r\nOK\r\n"},
+    {"AT+TEPAL=?\r", "\r\n+TEPAL:(0,1)\r\n\r\nOK\r\n"},             /* V.250 6.7.2.5 - Enable front panel analogue loop */ 
+    {"AT+TEPAL?\r", "\r\n+TEPAL:0\r\n\r\nOK\r\n"},
+    {"AT+TEPDL=?\r", "\r\n+TEPDL:(0,1)\r\n\r\nOK\r\n"},             /* V.250 6.7.2.4 - Enable front panel RDL */ 
+    {"AT+TEPDL?\r", "\r\n+TEPDL:0\r\n\r\nOK\r\n"},
+    {"AT+TERDL=?\r", "\r\n+TERDL:(0,1)\r\n\r\nOK\r\n"},             /* V.250 6.7.2.3 - Enable RDL from remote */ 
+    {"AT+TERDL?\r", "\r\n+TERDL:0\r\n\r\nOK\r\n"},
+    {"AT+TLDL=?\r", "\r\n+TLDL:(0,1)\r\n\r\nOK\r\n"},               /* V.250 6.7.2.13 - Local digital loop */ 
+    {"AT+TLDL?\r", "\r\n+TLDL:0\r\n\r\nOK\r\n"},
+    {"AT+TMODE=?\r", "\r\n+TMODE:(0,1)\r\n\r\nOK\r\n"},             /* V.250 6.7.2.10 - Set V.54 mode */ 
+    {"AT+TMODE?\r", "\r\n+TMODE:0\r\n\r\nOK\r\n"},
+    {"AT+TNUM\r", "\r\nOK\r\n"},                                    /* V.250 6.7.2.12 - Errored bit and block counts */ 
+    {"AT+TRDL=?\r", "\r\n+TRDL:(0,1)\r\n\r\nOK\r\n"},               /* V.250 6.7.2.14 - Request remote digital loop */ 
+    {"AT+TRDL?\r", "\r\n+TRDL:0\r\n\r\nOK\r\n"},
+    {"AT+TRDLS\r", "\r\nOK\r\n"},                                   /* V.250 6.7.2.8 - Remote digital loop status */ 
+    {"AT+TRES=?\r", "\r\n+TRES:(0-2)\r\n\r\nOK\r\n"},               /* V.250 6.7.2.17 - Self test result */ 
+    {"AT+TRES?\r", "\r\n+TRES:0\r\n\r\nOK\r\n"},
+    {"AT+TSELF=?\r", "\r\n+TSELF:(0,1)\r\n\r\nOK\r\n"},             /* V.250 6.7.2.16 - Self test */ 
+    {"AT+TSELF?\r", "\r\n+TSELF:0\r\n\r\nOK\r\n"},
+    {"AT+TTER=?\r", "\r\n+TTER:(0-65535),(0-65535)\r\n\r\nOK\r\n"}, /* V.250 6.7.2.11 - Test error rate */ 
+    {"AT+TTER?\r", "\r\n+TTER:0,0\r\n\r\nOK\r\n"},
+    {"AT+VBT\r", "\r\nOK\r\n"},                                     /* 3GPP TS 27.007 C.2.2 - Buffer threshold setting */
+    {"AT+VCID=?\r", "\r\n0,1\r\n\r\nOK\r\n"},                       /* 3GPP TS 27.007 C.2.3 - Calling number ID presentation */
+    {"AT+VCID?\r", "\r\n0\r\n\r\nOK\r\n"},
+    {"AT+VDR\r", "\r\nOK\r\n"},                                     /* V.253 10.3.1 - Distinctive ring (ring cadence reporting) */
+    {"AT+VDT\r", "\r\nOK\r\n"},                                     /* V.253 10.3.2 - Control tone cadence reporting */
+    {"AT+VDX\r", "\r\nOK\r\n"},                                     /* V.253 10.5.6 - Speakerphone duplex mode */
+    {"AT+VEM\r", "\r\nOK\r\n"},                                     /* V.253 10.5.7 - Deliver event reports */
+    {"AT+VGM\r", "\r\nOK\r\n"},                                     /* V.253 10.5.2 - Microphone gain */
+    {"AT+VGR\r", "\r\nOK\r\n"},                                     /* V.253 10.2.1 - Receive gain selection */
+    {"AT+VGS\r", "\r\nOK\r\n"},                                     /* V.253 10.5.3 - Speaker gain */
+    {"AT+VGT\r", "\r\nOK\r\n"},                                     /* V.253 10.2.2 - Volume selection */
+    {"AT+VIP\r", "\r\nOK\r\n"},                                     /* V.253 10.1.1 - Initialize voice parameters */
+    {"AT+VIT\r", "\r\nOK\r\n"},                                     /* V.253 10.2.3 - DTE/DCE inactivity timer */
+    {"AT+VLS\r", "\r\nOK\r\n"},                                     /* V.253 10.2.4 - Analogue source/destination selection */
+    {"AT+VPP\r", "\r\nOK\r\n"},                                     /* V.253 10.4.2 - Voice packet protocol */
+    {"AT+VRA\r", "\r\nOK\r\n"},                                     /* V.253 10.2.5 - Ringing tone goes away timer */
+    {"AT+VRID?\r", "\r\n0\r\n\r\nOK\r\n"},                          /* Extension - Find the originating and destination numbers */
+    {"AT+VRL\r", "\r\nOK\r\n"},                                     /* V.253 10.1.2 - Ring local phone */
+    {"AT+VRN\r", "\r\nOK\r\n"},                                     /* V.253 10.2.6 - Ringing tone never appeared timer */
+    {"AT+VRX\r", "\r\nOK\r\n"},                                     /* V.253 10.1.3 - Voice receive state */
+    {"AT+VSD\r", "\r\nOK\r\n"},                                     /* V.253 10.2.7 - Silence detection (QUIET and SILENCE) */
+    {"AT+VSID=12345\r", "\r\nOK\r\n"},                              /* Extension - Set the originating number */
+    {"AT+VSID?\r", "\r\n12345\r\n\r\nOK\r\n"},
+    {"AT+VSM\r", "\r\nOK\r\n"},                                     /* V.253 10.2.8 - Compression method selection */
+    {"AT+VSP\r", "\r\nOK\r\n"},                                     /* V.253 10.5.1 - Voice speakerphone state */
+    {"AT+VTA\r", "\r\nOK\r\n"},                                     /* V.253 10.5.4 - Train acoustic echo-canceller */ 
+    {"AT+VTD\r", "\r\nOK\r\n"},                                     /* V.253 10.2.9 - Beep tone duration timer */
+    {"AT+VTH\r", "\r\nOK\r\n"},                                     /* V.253 10.5.5 - Train line echo-canceller */ 
+    {"AT+VTR\r", "\r\nOK\r\n"},                                     /* V.253 10.1.4 - Voice duplex state */
+    {"AT+VTS\r", "\r\nOK\r\n"},                                     /* V.253 10.1.5 - DTMF and tone generation in voice */
+    {"AT+VTX\r", "\r\nOK\r\n"},                                     /* V.253 10.1.6 - Transmit data state */
+    {"AT+WS46\r", "\r\nOK\r\n"},                                    /* 3GPP TS 27.007 5.9 - PCCA STD-101 [17] select wireless network */
+    {"ATA\r", "\r\nERROR\r\n"},                                     /* V.250 6.3.5 - Answer */ 
+    {"ATDT -1234567890ABCDPSTW*#+,!@\r", ""},                       /* V.250 6.3.1 - Dial */ 
+    {"ATE1\r", "\r\nOK\r\n"},                                       /* V.250 6.2.4 - Command echo */ 
+    {"ATE0\r", "ATE0\r\r\nOK\r\n"},                                 /* V.250 6.2.4 - Command echo */ 
+    {"ATH\r", "\r\nOK\r\n"},                                        /* V.250 6.3.6 - Hook control */ 
+    {"ATI\r", "\r\n" PACKAGE "\r\n\r\nOK\r\n"},                     /* V.250 6.1.3 - Request identification information */ 
+    {"ATL\r", "\r\nOK\r\n"},                                        /* V.250 6.3.13 - Monitor speaker loudness */ 
+    {"ATM\r", "\r\nOK\r\n"},                                        /* V.250 6.3.14 - Monitor speaker mode */ 
+    {"ATO\r", "\r\nCONNECT\r\n\r\nOK\r\n"},                         /* V.250 6.3.7 - Return to online data state */ 
+    {"ATP\r", "\r\nOK\r\n"},                                        /* V.250 6.3.3 - Select pulse dialling (command) */ 
+    {"ATQ\r", "\r\nOK\r\n"},                                        /* V.250 6.2.5 - Result code suppression */ 
+    {"ATS0=?\r", "\r\n000\r\n\r\nOK\r\n"},                          /* V.250 6.3.8 - Automatic answer */ 
+    {"ATS0?\r", "\r\n000\r\n\r\nOK\r\n"},
+    {"ATS10=?\r", "\r\n000\r\n\r\nOK\r\n"},                         /* V.250 6.3.12 - Automatic disconnect delay */ 
+    {"ATS10?\r", "\r\n000\r\n\r\nOK\r\n"},
+    {"ATS3=?\r", "\r\n000\r\n\r\nOK\r\n"},                          /* V.250 6.2.1 - Command line termination character */ 
+    {"ATS3?\r", "\r\n013\r\n\r\nOK\r\n"},
+    {"ATS4=?\r", "\r\n000\r\n\r\nOK\r\n"},                          /* V.250 6.2.2 - Response formatting character */ 
+    {"ATS4?\r", "\r\n010\r\n\r\nOK\r\n"},
+    {"ATS5=?\r", "\r\n000\r\n\r\nOK\r\n"},                          /* V.250 6.2.3 - Command line editing character */ 
+    {"ATS5?\r", "\r\n008\r\n\r\nOK\r\n"},
+    {"ATS6=?\r", "\r\n000\r\n\r\nOK\r\n"},                          /* V.250 6.3.9 - Pause before blind dialling */ 
+    {"ATS6?\r", "\r\n001\r\n\r\nOK\r\n"},
+    {"ATS7=?\r", "\r\n000\r\n\r\nOK\r\n"},                          /* V.250 6.3.10 - Connection completion timeout */ 
+    {"ATS7?\r", "\r\n060\r\n\r\nOK\r\n"},
+    {"ATS8=?\r", "\r\n000\r\n\r\nOK\r\n"},                          /* V.250 6.3.11 - Comma dial modifier time */ 
+    {"ATS8?\r", "\r\n005\r\n\r\nOK\r\n"},
+    {"ATT\r", "\r\nOK\r\n"},                                        /* V.250 6.3.2 - Select tone dialling (command) */ 
+    {"ATV0\r", "0\r"},                                              /* V.250 6.2.6 - DCE response format */ 
+    {"ATV1\r", "\r\nOK\r\n"},
+    {"ATX4\r", "\r\nOK\r\n"},                                       /* V.250 6.2.7 - Result code selection and call progress monitoring control */ 
+    {"ATZ\r", "\r\nOK\r\n"},                                        /* V.250 6.1.1 - Reset to default configuration */
+    {"", ""}
+};
+
+char *decode_test_file = NULL;
+int countdown = 0;
+int command_response_test_step = -1;
+char response_buf[1000];
+int response_buf_ptr = 0;
+
+static int at_rx(at_state_t *s, const char *t, int len)
+{
+    at_interpreter(s, t, len);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int at_send(at_state_t *s, const char *t)
+{
+    printf("%s", t);
+    at_rx(s, t, strlen(t));
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+#if 0
+static int at_send_at(at_state_t *s, const char *t)
+{
+    uint8_t buf[500];
+
+    sprintf((char *) buf, "AT%s\r", t);
+    printf("%s", t);
+    at_rx(s, t, strlen(t));
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int at_expect(at_state_t *s, const char *t)
+{
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int at_send_hdlc(at_state_t *s, uint8_t *t, int len)
+{
+    uint8_t buf[100];
+    int i;
+    int j;
+
+    for (i = 0, j = 0;  i < len;  i++)
+    {
+        if (*t == DLE)
+            buf[j++] = DLE;
+        buf[j++] = *t++;
+    }
+    buf[j++] = DLE;
+    buf[j++] = ETX;
+    at_rx(s, (char *) buf, j);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+#endif
+
+static int general_test(at_state_t *s)
+{
+    int i;
+    
+    for (i = 0;  general_test_seq[i].command[0];  i++)
+    {
+        response_buf_ptr = 0;
+        response_buf[0] = '\0';
+        command_response_test_step = i;
+        at_send(s, general_test_seq[i].command);    
+        if (strcmp(general_test_seq[command_response_test_step].response, response_buf) != 0)
+        {
+            printf("Incorrect response\n");
+            printf("Expected: '%s'\n", general_test_seq[command_response_test_step].response);
+            printf("Received: '%s'\n", response_buf);
+            return -1;
+        }
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int modem_call_control(at_state_t *s, void *user_data, int op, const char *num)
+{
+    switch (op)
+    {
+    case AT_MODEM_CONTROL_CALL:
+        printf("\nModem control - Dialing '%s'\n", num);
+        break;
+    case AT_MODEM_CONTROL_ANSWER:
+        printf("\nModem control - Answering\n");
+        /* Force an error response, so we get something well defined for the test. */
+        return -1;
+    case AT_MODEM_CONTROL_HANGUP:
+        printf("\nModem control - Hanging up\n");
+        break;
+    case AT_MODEM_CONTROL_OFFHOOK:
+        printf("\nModem control - Going off hook\n");
+        break;
+    case AT_MODEM_CONTROL_ONHOOK:
+        printf("\nModem control - Going on hook\n");
+        break;
+    case AT_MODEM_CONTROL_DTR:
+        printf("\nModem control - DTR %d\n", (int) (intptr_t) num);
+        break;
+    case AT_MODEM_CONTROL_RTS:
+        printf("\nModem control - RTS %d\n", (int) (intptr_t) num);
+        break;
+    case AT_MODEM_CONTROL_CTS:
+        printf("\nModem control - CTS %d\n", (int) (intptr_t) num);
+        break;
+    case AT_MODEM_CONTROL_CAR:
+        printf("\nModem control - CAR %d\n", (int) (intptr_t) num);
+        break;
+    case AT_MODEM_CONTROL_RNG:
+        printf("\nModem control - RNG %d\n", (int) (intptr_t) num);
+        break;
+    case AT_MODEM_CONTROL_DSR:
+        printf("\nModem control - DSR %d\n", (int) (intptr_t) num);
+        break;
+    case AT_MODEM_CONTROL_SETID:
+        printf("\nModem control - Set ID '%s'\n", num);
+        break;
+    case AT_MODEM_CONTROL_RESTART:
+        printf("\nModem control - Restart %d\n", (int) (intptr_t) num);
+        break;
+    case AT_MODEM_CONTROL_DTE_TIMEOUT:
+        printf("\nModem control - Set DTE timeout %d\n", (int) (intptr_t) num);
+        break;
+    default:
+        printf("\nModem control - operation %d\n", op);
+        break;
+    }
+    /*endswitch*/
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int at_tx_handler(at_state_t *s, void *user_data, const uint8_t *buf, size_t len)
+{
+    int i;
+
+    for (i = 0;  i < len;  i++)
+    {
+        response_buf[response_buf_ptr++] = buf[i];
+        putchar(buf[i]);
+    }
+    response_buf[response_buf_ptr] = '\0';
+    
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int main(int argc, char *argv[])
+{
+    at_state_t at_state;
+    
+    if (at_init(&at_state, at_tx_handler, NULL, modem_call_control, NULL) == NULL)
+    {
+        fprintf(stderr, "Cannot start the AT interpreter\n");
+        exit(2);
+    }
+    if (general_test(&at_state))
+    {
+        printf("Tests failed.\n");
+        exit(2);
+    }
+    printf("Tests passed.\n");
+    return  0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/tests/awgn_tests.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/tests/awgn_tests.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,142 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * awgn_tests.c
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2001 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: awgn_tests.c,v 1.17 2008/05/13 13:17:25 steveu Exp $
+ */
+
+/*! \page awgn_tests_page AWGN tests
+\section awgn_tests_page_sec_1 What does it do?
+*/
+
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "spandsp.h"
+
+#if !defined(M_PI)
+# define M_PI           3.14159265358979323846  /* pi */
+#endif
+
+#define OUT_FILE_NAME   "awgn.wav"
+
+/* Some simple sanity tests for the Gaussian noise generation routines */
+
+int main (int argc, char *argv[])
+{
+    int i;
+    int j;
+    int clip_high;
+    int clip_low;
+    int total_samples;
+    int idum = 1234567;
+    int16_t value;
+    double total;
+    double x;
+    double p;
+    double o;
+    double error;
+    int bins[65536];
+    awgn_state_t noise_source;
+
+    /* Generate noise at several RMS levels between -50dBm and 0dBm. Noise is
+       generated for a large number of samples (1,000,000), and the RMS value
+       of the noise is calculated along the way. If the resulting level is
+       close to the requested RMS level, at least the scaling of the noise
+       should be Ok. At high level some clipping may distort the result a
+       little. */
+    for (j = -50;  j <= 0;  j += 5)
+    {
+        clip_high = 0;
+        clip_low = 0;
+        total = 0.0;
+        awgn_init_dbm0(&noise_source, idum, (float) j);
+        total_samples = 1000000;
+        for (i = 0;  i < total_samples;  i++)
+        {
+            value = awgn(&noise_source);
+            if (value == 32767)
+                clip_high++;
+            else if (value == -32768)
+                clip_low++;
+            total += ((double) value)*((double) value);
+        }
+        error = 100.0*(1.0 - sqrt(total/total_samples)/noise_source.rms);
+        printf("RMS = %.3f (expected %d) %.2f%% error [clipped samples %d+%d]\n",
+               log10(sqrt(total/total_samples)/32768.0)*20.0 + DBM0_MAX_POWER,
+               j,
+               error,
+               clip_low,
+               clip_high);
+        /* We don't check the result at 0dBm0, as there will definitely be a lot of error due to clipping */
+        if (j < 0  &&  fabs(error) > 0.2)
+        {
+            printf("Test failed.\n");
+            exit(2);
+        }
+    }
+    /* Now look at the statistical spread of the results, by collecting data in
+       bins from a large number of samples. Use a fairly high noise level, but
+       low enough to avoid significant clipping. Use the Gaussian model to
+       predict the real probability, and present the results for graphing. */
+    memset(bins, 0, sizeof(bins));
+    clip_high = 0;
+    clip_low = 0;
+    awgn_init_dbm0(&noise_source, idum, -15);
+    total_samples = 10000000;
+    for (i = 0;  i < total_samples;  i++)
+    {
+        value = awgn(&noise_source);
+        if (value == 32767)
+            clip_high++;
+        else if (value == -32768)
+            clip_low++;
+        bins[value + 32768]++;
+    }
+    o = noise_source.rms;
+    for (i = 0;  i < 65536 - 10;  i++)
+    {
+        x = i - 32768;
+        /* Find the real probability for this bin */
+        p = (1.0/(o*sqrt(2.0*M_PI)))*exp(-(x*x)/(2.0*o*o));
+        /* Now do a little smoothing on the real data to get a reasonably
+           steady answer */
+        x = 0;
+        for (j = 0;  j < 10;  j++)
+            x += bins[i + j];
+        x /= 10.0;
+        x /= total_samples;
+        /* Now send it out for graphing. */
+        printf("%6d %.7f %.7f\n", i - 32768, x, p);
+    }
+    
+    printf("Tests passed.\n");
+    return  0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/tests/bell_mf_rx_tests.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/tests/bell_mf_rx_tests.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,549 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * bell_mf_tests.c - Test the Bell MF detector against the spec., whatever the
+ *                   spec. may be :)
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2003 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: bell_mf_rx_tests.c,v 1.12 2008/05/13 13:17:25 steveu Exp $
+ */
+
+/*! \file */
+
+/*! \page bell_mf_tests_page Bell MF tone generation and detection tests
+\section bell_mf_tests_page_sec_1 What does it do?
+These tests are fashioned after those on the CM7291 test tape from
+Mitel. Those tests are for DTMF, rather than Bell MF, but make a
+fair starting point for a set of meaningful tests of Bell MF.
+
+These tests include conversion to and from A-law. It is assumed the
+distortion this produces is comparable to u-law, so it should be
+a fair test of performance in a real PSTN channel.
+*/
+
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <string.h>
+#include <time.h>
+#include <audiofile.h>
+
+#include "spandsp.h"
+
+/* Basic Bell MF specs:
+ *
+ * Signal generation:
+ *   Tone on time = KP: 100+-7ms. All other signals: 68+-7ms
+ *   Tone off time (between digits) = 68+-7ms
+ *   Frequency tolerance +- 1.5%
+ *   Signal level -7+-1dBm per frequency
+ *
+ * Signal reception:
+ *   Frequency tolerance +- 1.5% +-10Hz
+ *   Signal level -14dBm to 0dBm
+ *   Perform a "two and only two tones present" test.
+ *   Twist <= 6dB accepted
+ *   Receiver sensitive to signals above -22dBm per frequency
+ *   Test for a minimum of 55ms if KP, or 30ms of other signals.
+ *   Signals to be recognised if the two tones arrive within 8ms of each other.
+ *   Invalid signals result in the return of the re-order tone.
+ */
+
+#define MF_DURATION                 (68*8)
+#define MF_PAUSE                    (68*8)
+#define MF_CYCLE                    (MF_DURATION + MF_PAUSE)
+
+typedef struct
+{
+    float       f1;         /* First freq */
+    float       f2;         /* Second freq */
+    int8_t      level1;     /* Level of the first freq (dB) */
+    int8_t      level2;     /* Level of the second freq (dB) */
+    uint8_t     on_time;    /* Tone on time (ms) */
+    uint8_t     off_time;   /* Minimum post tone silence (ms) */
+} mf_digit_tones_t;
+
+static const mf_digit_tones_t bell_mf_tones[] =
+{
+    { 700.0,  900.0, -7, -7,  68, 68},
+    { 700.0, 1100.0, -7, -7,  68, 68},
+    { 900.0, 1100.0, -7, -7,  68, 68},
+    { 700.0, 1300.0, -7, -7,  68, 68},
+    { 900.0, 1300.0, -7, -7,  68, 68},
+    {1100.0, 1300.0, -7, -7,  68, 68},
+    { 700.0, 1500.0, -7, -7,  68, 68},
+    { 900.0, 1500.0, -7, -7,  68, 68},
+    {1100.0, 1500.0, -7, -7,  68, 68},
+    {1300.0, 1500.0, -7, -7,  68, 68},
+    { 700.0, 1700.0, -7, -7,  68, 68}, /* ST''' - use 'C' */
+    { 900.0, 1700.0, -7, -7,  68, 68}, /* ST'   - use 'A' */
+    {1100.0, 1700.0, -7, -7, 100, 68}, /* KP    - use '*' */
+    {1300.0, 1700.0, -7, -7,  68, 68}, /* ST''  - use 'B' */
+    {1500.0, 1700.0, -7, -7,  68, 68}, /* ST    - use '#' */
+    {0.0, 0.0, 0, 0, 0, 0}
+};
+
+static tone_gen_descriptor_t my_mf_digit_tones[16];
+
+static char bell_mf_tone_codes[] = "1234567890CA*B#";
+
+int callback_ok;
+int callback_roll;
+
+static void my_mf_gen_init(float low_fudge,
+                           int low_level,
+                           float high_fudge,
+                           int high_level,
+                           int duration,
+                           int gap)
+{
+    int i;
+
+    /* The fiddle factor on the tone duration is to make KP consistently
+       50% longer than the other digits, as the digit durations are varied
+       for the tests. This is an approximation, as the real scaling should
+       be 100/68 */
+    for (i = 0;  i < 15;  i++)
+    {
+        make_tone_gen_descriptor(&my_mf_digit_tones[i],
+                                 bell_mf_tones[i].f1*(1.0 + low_fudge),
+                                 low_level,
+                                 bell_mf_tones[i].f2*(1.0 + high_fudge),
+                                 high_level,
+                                 (i == 12)  ?  3*duration/2  :  duration,
+                                 gap,
+                                 0,
+                                 0,
+                                 FALSE);
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static int my_mf_generate(int16_t amp[], const char *digits)
+{
+    int len;
+    char *cp;
+    tone_gen_state_t tone;
+
+    len = 0;
+    while (*digits)
+    {
+        if ((cp = strchr(bell_mf_tone_codes, *digits)))
+        {
+            tone_gen_init(&tone, &my_mf_digit_tones[cp - bell_mf_tone_codes]);
+            len += tone_gen(&tone, amp + len, 9999);
+        }
+        digits++;
+    }
+    return len;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void codec_munge(int16_t amp[], int len)
+{
+    int i;
+    uint8_t alaw;
+    
+    for (i = 0;  i < len;  i++)
+    {
+        alaw = linear_to_alaw (amp[i]);
+        amp[i] = alaw_to_linear (alaw);
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+#define ALL_POSSIBLE_DIGITS     "1234567890CA*B#"
+
+static void digit_delivery(void *data, const char *digits, int len)
+{
+    int i;
+    int seg;
+    const char *s = ALL_POSSIBLE_DIGITS;
+    const char *t;
+
+    if (data != (void *) 0x12345678)
+    {
+        callback_ok = FALSE;
+        return;
+    }
+    callback_ok = TRUE;
+    t = s + callback_roll;
+    seg = 15 - callback_roll;
+    for (i = 0;  i < len;  i += seg, seg = 15)
+    {
+        if (i + seg > len)
+            seg = len - i;
+        if (memcmp(digits + i, t, seg))
+        {
+            callback_ok = FALSE;
+            printf("Fail at %d %d\n", i, seg);
+            break;
+        }
+        t = s;
+        callback_roll = (callback_roll + seg)%15;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static int16_t amp[1000000];
+
+int main(int argc, char *argv[])
+{
+    int duration;
+    int i;
+    int j;
+    int len;
+    int sample;
+    const char *s;
+    char digit[2];
+    char buf[MAX_BELL_MF_DIGITS + 1];
+    int actual;
+    int nplus;
+    int nminus;
+    float rrb;
+    float rcfo;
+    time_t now;
+    bell_mf_rx_state_t mf_state;
+    awgn_state_t noise_source;
+
+    time(&now);
+    bell_mf_rx_init(&mf_state, NULL, NULL);
+
+    /* Test 1: Mitel's test 1 isn't really a test. Its a calibration step,
+       which has no meaning here. */
+    printf ("Test 1: Calibration\n");
+    printf ("    Passed\n");
+
+    /* Test 2: Decode check
+       This is a sanity check, that all digits are reliably detected
+       under ideal conditions.  Each possible digit is repeated 10 times,
+       with 68ms bursts. The level of each tone is about 6dB down from clip */
+    printf ("Test 2: Decode check\n");
+    my_mf_gen_init(0.0, -3, 0.0, -3, 68, 68);
+    s = ALL_POSSIBLE_DIGITS;
+    digit[1] = '\0';
+    while (*s)
+    {
+        digit[0] = *s++;
+        for (i = 0;  i < 10;  i++)
+        {
+            len = my_mf_generate(amp, digit);
+            codec_munge(amp, len);
+            bell_mf_rx(&mf_state, amp, len);
+            actual = bell_mf_rx_get(&mf_state, buf, 128);
+            if (actual != 1  ||  buf[0] != digit[0])
+            {
+                printf ("    Sent     '%s'\n", digit);
+                printf ("    Received '%s' [%d]\n", buf, actual);
+                printf ("    Failed\n");
+                exit (2);
+            }
+        }
+    }
+    printf ("    Passed\n");
+
+    /* Test 3: Recognition bandwidth and channel centre frequency check.
+       Use all digits. Each digit types requires four tests to complete
+       the check. Each section contains 40 pulses of 68ms duration,
+       with an amplitude of -20dB from clip per frequency.
+
+       Four sections covering the tests for one tone (1 digit) are:
+       a. H frequency at 0% deviation from center, L frequency at +0.1%.
+          L frequency is then increments in +01.% steps up to +4%. The
+          number of tone bursts is noted and designated N+.
+       b. H frequency at 0% deviation, L frequency at -0.1%. L frequency
+          is then incremental in -0.1% steps, up to -4%. The number of
+          tone bursts is noted and designated N-.
+       c. The test in (a) is repeated with the L frequency at 0% and the
+          H frequency varied up to +4%.
+       d. The test in (b) is repeated with the L frequency and 0% and the
+          H frequency varied to -4%.
+
+       Receiver Recognition Bandwidth (RRB) is calculated as follows:
+            RRB% = (N+ + N-)/10
+       Receiver Center Frequency Offset (RCFO) is calculated as follows:
+            RCFO% = X + (N+ - N-)/20
+            
+       Note that this test doesn't test what it says it is testing at all,
+       and the results are quite inaccurate, if not a downright lie! However,
+       it follows the Mitel procedure, so how can it be bad? :)
+       
+       The spec calls for +-1.5% +-10Hz of bandwidth.
+    */
+    printf ("Test 3: Recognition bandwidth and channel centre frequency check\n");
+    s = ALL_POSSIBLE_DIGITS;
+    digit[1] = '\0';
+    j = 0;
+    while (*s)
+    {
+        digit[0] = *s++;
+        for (nplus = 0, i = 1;  i <= 60;  i++)
+        {
+            my_mf_gen_init((float) i/1000.0, -17, 0.0, -17, 68, 68);
+            len = my_mf_generate(amp, digit);
+            codec_munge(amp, len);
+            bell_mf_rx(&mf_state, amp, len);
+            nplus += bell_mf_rx_get(&mf_state, buf, 128);
+        }
+        for (nminus = 0, i = -1;  i >= -60;  i--)
+        {
+            my_mf_gen_init((float) i/1000.0, -17, 0.0, -17, 68, 68);
+            len = my_mf_generate(amp, digit);
+            codec_munge(amp, len);
+            bell_mf_rx(&mf_state, amp, len);
+            nminus += bell_mf_rx_get(&mf_state, buf, 128);
+        }
+        rrb = (float) (nplus + nminus)/10.0;
+        rcfo = (float) (nplus - nminus)/10.0;
+        printf ("    %c (low)  rrb = %5.2f%%, rcfo = %5.2f%%, max -ve = %5.2f, max +ve = %5.2f\n",
+                digit[0],
+                rrb,
+                rcfo,
+                (float) nminus/10.0,
+                (float) nplus/10.0);
+
+        if (rrb < 3.0 + rcfo + (2.0*100.0*10.0/bell_mf_tones[j].f1)  ||  rrb >= 15.0 + rcfo)
+        {
+            printf ("    Failed\n");
+            exit (2);
+        }
+
+        for (nplus = 0, i = 1;  i <= 60;  i++)
+        {
+            my_mf_gen_init(0.0, -17, (float) i/1000.0, -17, 68, 68);
+            len = my_mf_generate(amp, digit);
+            codec_munge(amp, len);
+            bell_mf_rx(&mf_state, amp, len);
+            nplus += bell_mf_rx_get(&mf_state, buf, 128);
+        }
+        for (nminus = 0, i = -1;  i >= -60;  i--)
+        {
+            my_mf_gen_init(0.0, -17, (float) i/1000.0, -17, 68, 68);
+            len = my_mf_generate(amp, digit);
+            codec_munge(amp, len);
+            bell_mf_rx(&mf_state, amp, len);
+            nminus += bell_mf_rx_get(&mf_state, buf, 128);
+        }
+        rrb = (float) (nplus + nminus)/10.0;
+        rcfo = (float) (nplus - nminus)/10.0;
+        printf ("    %c (high) rrb = %5.2f%%, rcfo = %5.2f%%, max -ve = %5.2f, max +ve = %5.2f\n",
+                digit[0],
+                rrb,
+                rcfo,
+                (float) nminus/10.0,
+                (float) nplus/10.0);
+        if (rrb < 3.0 + rcfo + (2.0*100.0*10.0/bell_mf_tones[j].f2)  ||  rrb >= 15.0 + rcfo)
+        {
+            printf ("    Failed\n");
+            exit (2);
+        }
+        j++;
+    }
+    printf ("    Passed\n");
+
+    /* Test 4: Acceptable amplitude ratio (twist).
+       Twist all digits in both directions, and check the maximum twist
+       we can accept. The way this is done is styled after the Mitel DTMF
+       test, and has good and bad points. */
+
+    printf ("Test 4: Acceptable amplitude ratio (twist)\n");
+    s = ALL_POSSIBLE_DIGITS;
+    digit[1] = '\0';
+    while (*s)
+    {
+        digit[0] = *s++;
+        for (nplus = 0, i = -50;  i >= -250;  i--)
+        {
+            my_mf_gen_init(0.0, -5, 0.0, i/10, 68, 68);
+
+            len = my_mf_generate(amp, digit);
+            codec_munge(amp, len);
+            bell_mf_rx(&mf_state, amp, len);
+            nplus += bell_mf_rx_get(&mf_state, buf, 128);
+        }
+        printf("    %c normal twist  = %.2fdB\n", digit[0], (float) nplus/10.0);
+        if (nplus < 60)
+        {
+            printf("    Failed\n");
+            exit(2);
+        }
+        for (nminus = 0, i = -50;  i >= -250;  i--)
+        {
+            my_mf_gen_init(0.0, i/10, 0.0, -5, 68, 68);
+
+            len = my_mf_generate(amp, digit);
+            codec_munge(amp, len);
+            bell_mf_rx(&mf_state, amp, len);
+            nminus += bell_mf_rx_get(&mf_state, buf, 128);
+        }
+        printf("    %c reverse twist = %.2fdB\n", digit[0], (float) nminus/10.0);
+        if (nminus < 60)
+        {
+            printf("    Failed\n");
+            exit(2);
+        }
+    }
+    printf("    Passed\n");
+
+    /* Test 5: Dynamic range
+       This test sends all possible digits, with gradually increasing 
+       amplitude. We determine the span over which we achieve reliable
+       detection. The spec says we should detect between -14dBm and 0dBm,
+       but the tones clip above -3dBm, so this cannot really work. */
+       
+    printf("Test 5: Dynamic range\n");
+    for (nplus = nminus = -1000, i = -50;  i <= 3;  i++)
+    {
+        my_mf_gen_init(0.0, i, 0.0, i, 68, 68);
+        for (j = 0;  j < 100;  j++)
+        {
+            len = my_mf_generate(amp, ALL_POSSIBLE_DIGITS);
+            codec_munge(amp, len);
+            bell_mf_rx(&mf_state, amp, len);
+            if (bell_mf_rx_get(&mf_state, buf, 128) != 15)
+                break;
+            if (strcmp(buf, ALL_POSSIBLE_DIGITS) != 0)
+                break;
+        }
+        if (j == 100)
+        {
+            if (nplus == -1000)
+                nplus = i;
+        }
+        else
+        {
+            if (nplus != -1000  &&  nminus == -1000)
+                nminus = i;
+        }
+    }
+    printf("    Dynamic range = %ddB to %ddB\n", nplus, nminus - 1);
+    if (nplus > -22  ||  nminus <= -3)
+    {
+        printf("    Failed\n");
+        exit(2);
+    }
+    printf("    Passed\n");
+
+    /* Test 6: Guard time
+       This test sends all possible digits, with a gradually reducing 
+       duration. The spec defines a narrow range of tone duration
+       times we can expect, so as long as we detect reliably at the
+       specified minimum we should be OK. However, the spec also says
+       we should detect on a minimum of 55ms of KP, or 30ms of other
+       digits. */
+
+    printf("Test 6: Guard time\n");
+    for (i = 30;  i < 62;  i++)
+    {
+        my_mf_gen_init(0.0, -5, 0.0, -3, i, 68);
+        for (j = 0;  j < 500;  j++)
+        {
+            len = my_mf_generate(amp, ALL_POSSIBLE_DIGITS);
+            codec_munge(amp, len);
+            bell_mf_rx(&mf_state, amp, len);
+
+            if (bell_mf_rx_get(&mf_state, buf, 128) != 15)
+                break;
+            if (strcmp(buf, ALL_POSSIBLE_DIGITS) != 0)
+                break;
+        }
+        if (j == 500)
+            break;
+    }
+    printf("    Guard time = %dms\n", i);
+    if (i > 61)
+    {
+        printf("    Failed\n");
+        exit(2);
+    }
+    printf("    Passed\n");
+
+    /* Test 7: Acceptable signal to noise ratio
+       We send all possible digits at -6dBm from clip, mixed with AWGN.
+       We gradually reduce the noise until we get clean detection. */
+
+    printf("Test 7: Acceptable signal to noise ratio\n");
+    my_mf_gen_init(0.0, -3, 0.0, -3, 68, 68);
+    for (i = -10;  i > -50;  i--)
+    {
+        awgn_init_dbm0(&noise_source, 1234567, (float) i);
+        for (j = 0;  j < 500;  j++)
+        {
+            len = my_mf_generate(amp, ALL_POSSIBLE_DIGITS);
+            for (sample = 0;  sample < len;  sample++)
+                amp[sample] = saturate(amp[sample] + awgn(&noise_source));
+            codec_munge(amp, len);
+            bell_mf_rx(&mf_state, amp, len);
+
+            if (bell_mf_rx_get(&mf_state, buf, 128) != 15)
+                break;
+            if (strcmp(buf, ALL_POSSIBLE_DIGITS) != 0)
+                break;
+        }
+        if (j == 500)
+            break;
+    }
+    printf("    Acceptable S/N ratio is %ddB\n", -3 - i);
+    if (-3 - i > 26)
+    {
+        printf("    Failed\n");
+        exit(2);
+    }
+    printf("    Passed\n");
+
+    /* The remainder of the Mitel tape is the talk-off test. This is
+       meaningless for Bell MF. However the decoder's tolerance of
+       out of band noise is significant. */
+    /* TODO: add a OOB tolerance test. */
+
+    /* Test the callback mode for delivering detected digits */
+
+    printf("Test: Callback digit delivery mode.\n");
+    callback_ok = FALSE;
+    callback_roll = 0;
+    bell_mf_rx_init(&mf_state, digit_delivery, (void *) 0x12345678);
+    my_mf_gen_init(0.0, -10, 0.0, -10, 68, 68);
+    for (i = 1;  i < 10;  i++)
+    {
+        len = 0;
+        for (j = 0;  j < i;  j++)
+            len += my_mf_generate(amp + len, ALL_POSSIBLE_DIGITS);
+        bell_mf_rx(&mf_state, amp, len);
+        if (!callback_ok)
+            break;
+    }
+    if (!callback_ok)
+    {
+        printf("    Failed\n");
+     	exit (2);
+    }
+    printf("    Passed\n");
+
+    duration = time (NULL) - now;
+    printf ("Tests passed in %ds\n", duration);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/tests/bell_mf_tx_tests.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/tests/bell_mf_tx_tests.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,210 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * bell_mf_tx_tests.c - Test the Bell MF generator.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2003 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: bell_mf_tx_tests.c,v 1.12 2008/08/16 15:24:15 steveu Exp $
+ */
+
+/*! \file */
+
+/*! \page bell_mf_tx_tests_page Bell MF generation tests
+\section bell_mf_tx_tests_page_sec_1 What does it do?
+???.
+
+\section bell_mf_tx_tests_page_sec_2 How does it work?
+???.
+*/
+
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <string.h>
+#include <time.h>
+#include <audiofile.h>
+
+#include "spandsp.h"
+#include "spandsp-sim.h"
+
+#define OUTPUT_FILE_NAME    "bell_mf.wav"
+
+int main(int argc, char *argv[])
+{
+    bell_mf_tx_state_t gen;
+    int16_t amp[16384];
+    int len;
+    AFfilehandle outhandle;
+    int outframes;
+    int add_digits;
+
+    if ((outhandle = afOpenFile_telephony_write(OUTPUT_FILE_NAME, 1)) == AF_NULL_FILEHANDLE)
+    {
+        fprintf(stderr, "    Cannot open wave file '%s'\n", OUTPUT_FILE_NAME);
+        exit(2);
+    }
+
+    bell_mf_tx_init(&gen);
+    len = bell_mf_tx(&gen, amp, 16384);
+    printf("Generated %d samples\n", len);
+    outframes = afWriteFrames(outhandle,
+                              AF_DEFAULT_TRACK,
+                              amp,
+                              len);
+    if (bell_mf_tx_put(&gen, "123", -1))
+        printf("Ooops\n");
+    len = bell_mf_tx(&gen, amp, 16384);
+    printf("Generated %d samples\n", len);
+    outframes = afWriteFrames(outhandle,
+                              AF_DEFAULT_TRACK,
+                              amp,
+                              len);
+    if (bell_mf_tx_put(&gen, "456", -1))
+        printf("Ooops\n");
+    len = bell_mf_tx(&gen, amp, 160);
+    printf("Generated %d samples\n", len);
+    outframes = afWriteFrames(outhandle,
+                              AF_DEFAULT_TRACK,
+                              amp,
+                              len);
+    if (bell_mf_tx_put(&gen, "789", -1))
+        printf("Ooops\n");
+    len = bell_mf_tx(&gen, amp, 160);
+    printf("Generated %d samples\n", len);
+    outframes = afWriteFrames(outhandle,
+                              AF_DEFAULT_TRACK,
+                              amp,
+                              len);
+    if (bell_mf_tx_put(&gen, "*#", -1))
+        printf("Ooops\n");
+    len = bell_mf_tx(&gen, amp, 160);
+    printf("Generated %d samples\n", len);
+    outframes = afWriteFrames(outhandle,
+                              AF_DEFAULT_TRACK,
+                              amp,
+                              len);
+    add_digits = 1;
+    do
+    {
+        len = bell_mf_tx(&gen, amp, 160);
+        printf("Generated %d samples\n", len);
+        if (len > 0)
+        {
+            outframes = afWriteFrames(outhandle,
+                                      AF_DEFAULT_TRACK,
+                                      amp,
+                                      len);
+        }
+        if (add_digits)
+        {
+            if (bell_mf_tx_put(&gen, "1234567890", -1))
+            {
+                printf("Digit buffer full\n");
+                add_digits = 0;
+            }
+        }
+    }
+    while (len > 0);
+
+    bell_mf_tx_init(&gen);
+    len = bell_mf_tx(&gen, amp, 16384);
+    printf("Generated %d samples\n", len);
+    outframes = afWriteFrames(outhandle,
+                              AF_DEFAULT_TRACK,
+                              amp,
+                              len);
+    if (bell_mf_tx_put(&gen, "123", -1))
+        printf("Ooops\n");
+    len = bell_mf_tx(&gen, amp, 16384);
+    printf("Generated %d samples\n", len);
+    outframes = afWriteFrames(outhandle,
+                              AF_DEFAULT_TRACK,
+                              amp,
+                              len);
+    if (bell_mf_tx_put(&gen, "456", -1))
+        printf("Ooops\n");
+    len = bell_mf_tx(&gen, amp, 160);
+    printf("Generated %d samples\n", len);
+    outframes = afWriteFrames(outhandle,
+                              AF_DEFAULT_TRACK,
+                              amp,
+                              len);
+    if (bell_mf_tx_put(&gen, "789", -1))
+        printf("Ooops\n");
+    len = bell_mf_tx(&gen, amp, 160);
+    printf("Generated %d samples\n", len);
+    outframes = afWriteFrames(outhandle,
+                              AF_DEFAULT_TRACK,
+                              amp,
+                              len);
+    if (bell_mf_tx_put(&gen, "0*#", -1))
+        printf("Ooops\n");
+    len = bell_mf_tx(&gen, amp, 160);
+    printf("Generated %d samples\n", len);
+    outframes = afWriteFrames(outhandle,
+                              AF_DEFAULT_TRACK,
+                              amp,
+                              len);
+    if (bell_mf_tx_put(&gen, "ABC", -1))
+        printf("Ooops\n");
+    len = bell_mf_tx(&gen, amp, 160);
+    printf("Generated %d samples\n", len);
+    outframes = afWriteFrames(outhandle,
+                              AF_DEFAULT_TRACK,
+                              amp,
+                              len);
+    add_digits = 1;
+    do
+    {
+        len = bell_mf_tx(&gen, amp, 160);
+        printf("Generated %d samples\n", len);
+        if (len > 0)
+        {
+            outframes = afWriteFrames(outhandle,
+                                      AF_DEFAULT_TRACK,
+                                      amp,
+                                      len);
+        }
+        if (add_digits)
+        {
+            if (bell_mf_tx_put(&gen, "1234567890", -1))
+            {
+                printf("Digit buffer full\n");
+                add_digits = 0;
+            }
+        }
+    }
+    while (len > 0);
+
+    if (afCloseFile(outhandle) != 0)
+    {
+        fprintf(stderr, "    Cannot close wave file '%s'\n", OUTPUT_FILE_NAME);
+        exit (2);
+    }
+
+    return  0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/tests/bert_tests.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/tests/bert_tests.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,455 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * bert_tests.c - Tests for the BER tester.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2004 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: bert_tests.c,v 1.24 2008/05/13 13:17:25 steveu Exp $
+ */
+
+/*! \file */
+
+/*! \page bert_tests_page BERT tests
+\section bert_tests_page_sec_1 What does it do?
+These tests exercise each of the BERT standards supported by the BERT module.
+*/
+
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <assert.h>
+#include <audiofile.h>
+
+#include "spandsp.h"
+
+/* Use a local random generator, so the results are consistent across platforms */
+static int my_rand(void)
+{
+    static int rndnum = 1234567;
+
+    return (rndnum = 1664525U*rndnum + 1013904223U);
+}
+/*- End of function --------------------------------------------------------*/
+
+static void reporter(void *user_data, int reason, bert_results_t *results)
+{
+    int channel;
+
+    channel = (int) (intptr_t) user_data;
+    printf("BERT report '%s' ", bert_event_to_str(reason));
+    switch (reason)
+    {
+    case BERT_REPORT_REGULAR:
+        printf("%d bits, %d bad bits, %d resyncs", results->total_bits, results->bad_bits, results->resyncs);
+        break;
+    }
+    printf("\r");
+}
+/*- End of function --------------------------------------------------------*/
+
+int8_t test[0x800000];
+
+int main(int argc, char *argv[])
+{
+    bert_state_t tx_bert;
+    bert_state_t rx_bert;
+    bert_state_t bert;
+    bert_results_t bert_results;
+    int i;
+    int bit;
+    int zeros;
+    int max_zeros;
+    int failed;
+
+    bert_init(&tx_bert, 0, BERT_PATTERN_ZEROS, 300, 20);
+    bert_init(&rx_bert, 0, BERT_PATTERN_ZEROS, 300, 20);
+    for (i = 0;  i < 511*2;  i++)
+    {
+        bit = bert_get_bit(&tx_bert);
+        bert_put_bit(&rx_bert, bit);        
+    }
+    bert_result(&rx_bert, &bert_results);
+    printf("Zeros:     Bad bits %d/%d\n", bert_results.bad_bits, bert_results.total_bits);
+    if (bert_results.bad_bits  ||  bert_results.total_bits != 950)
+    {
+        printf("Test failed.\n");
+        exit(2);
+    }
+
+    bert_init(&tx_bert, 0, BERT_PATTERN_ONES, 300, 20);
+    bert_init(&rx_bert, 0, BERT_PATTERN_ONES, 300, 20);
+    for (i = 0;  i < 511*2;  i++)
+    {
+        bit = bert_get_bit(&tx_bert);
+        bert_put_bit(&rx_bert, bit);        
+    }
+    bert_result(&rx_bert, &bert_results);
+    printf("Ones:      Bad bits %d/%d\n", bert_results.bad_bits, bert_results.total_bits);
+    if (bert_results.bad_bits  ||  bert_results.total_bits != 950)
+    {
+        printf("Test failed.\n");
+        exit(2);
+    }
+
+    bert_init(&tx_bert, 0, BERT_PATTERN_1_TO_7, 300, 20);
+    bert_init(&rx_bert, 0, BERT_PATTERN_1_TO_7, 300, 20);
+    for (i = 0;  i < 511*2;  i++)
+    {
+        bit = bert_get_bit(&tx_bert);
+        bert_put_bit(&rx_bert, bit);
+    }
+    bert_result(&rx_bert, &bert_results);
+    printf("1 to 7:    Bad bits %d/%d\n", bert_results.bad_bits, bert_results.total_bits);
+    if (bert_results.bad_bits  ||  bert_results.total_bits != 950)
+    {
+        printf("Test failed.\n");
+        exit(2);
+    }
+
+    bert_init(&tx_bert, 0, BERT_PATTERN_1_TO_3, 300, 20);
+    bert_init(&rx_bert, 0, BERT_PATTERN_1_TO_3, 300, 20);
+    for (i = 0;  i < 511*2;  i++)
+    {
+        bit = bert_get_bit(&tx_bert);
+        bert_put_bit(&rx_bert, bit);        
+    }
+    bert_result(&rx_bert, &bert_results);
+    printf("1 to 3:    Bad bits %d/%d\n", bert_results.bad_bits, bert_results.total_bits);
+    if (bert_results.bad_bits  ||  bert_results.total_bits != 950)
+    {
+        printf("Test failed.\n");
+        exit(2);
+    }
+
+    bert_init(&tx_bert, 0, BERT_PATTERN_1_TO_1, 300, 20);
+    bert_init(&rx_bert, 0, BERT_PATTERN_1_TO_1, 300, 20);
+    for (i = 0;  i < 511*2;  i++)
+    {
+        bit = bert_get_bit(&tx_bert);
+        bert_put_bit(&rx_bert, bit);
+    }
+    bert_result(&rx_bert, &bert_results);
+    printf("1 to 1:    Bad bits %d/%d\n", bert_results.bad_bits, bert_results.total_bits);
+    if (bert_results.bad_bits  ||  bert_results.total_bits != 950)
+    {
+        printf("Test failed.\n");
+        exit(2);
+    }
+
+    bert_init(&tx_bert, 0, BERT_PATTERN_3_TO_1, 300, 20);
+    bert_init(&rx_bert, 0, BERT_PATTERN_3_TO_1, 300, 20);
+    for (i = 0;  i < 511*2;  i++)
+    {
+        bit = bert_get_bit(&tx_bert);
+        bert_put_bit(&rx_bert, bit);
+    }
+    bert_result(&rx_bert, &bert_results);
+    printf("3 to 1:    Bad bits %d/%d\n", bert_results.bad_bits, bert_results.total_bits);
+    if (bert_results.bad_bits  ||  bert_results.total_bits != 950)
+    {
+        printf("Test failed.\n");
+        exit(2);
+    }
+    
+    bert_init(&tx_bert, 0, BERT_PATTERN_7_TO_1, 300, 20);
+    bert_init(&rx_bert, 0, BERT_PATTERN_7_TO_1, 300, 20);
+    for (i = 0;  i < 511*2;  i++)
+    {
+        bit = bert_get_bit(&tx_bert);
+        bert_put_bit(&rx_bert, bit);
+    }
+    bert_result(&rx_bert, &bert_results);
+    printf("7 to 1:    Bad bits %d/%d\n", bert_results.bad_bits, bert_results.total_bits);
+    if (bert_results.bad_bits  ||  bert_results.total_bits != 950)
+    {
+        printf("Test failed.\n");
+        exit(2);
+    }
+
+    bert_init(&tx_bert, 0, BERT_PATTERN_ITU_O153_9, 300, 20);
+    bert_init(&rx_bert, 0, BERT_PATTERN_ITU_O153_9, 300, 20);
+    for (i = 0;  i < 0x200;  i++)
+        test[i] = 0;
+    max_zeros = 0;
+    zeros = 0;
+    for (i = 0;  i < 511*2;  i++)
+    {
+        bit = bert_get_bit(&tx_bert);
+        if (bit)
+        {
+            if (zeros > max_zeros)
+                max_zeros = zeros;
+            zeros = 0;
+        }
+        else
+        {
+            zeros++;
+        }
+        bert_put_bit(&rx_bert, bit);        
+        test[tx_bert.tx_reg]++;
+    }
+    failed = FALSE;
+    if (test[0] != 0)
+    {
+        printf("XXX %d %d\n", 0, test[0]);
+        failed = TRUE;
+    }
+    for (i = 1;  i < 0x200;  i++)
+    {
+        if (test[i] != 2)
+        {
+            printf("XXX %d %d\n", i, test[i]);
+            failed = TRUE;
+        }
+    }
+    bert_result(&rx_bert, &bert_results);
+    printf("O.153(9):  Bad bits %d/%d, max zeros %d\n", bert_results.bad_bits, bert_results.total_bits, max_zeros);
+    if (bert_results.bad_bits  ||  bert_results.total_bits != 986  ||  failed)
+    {
+        printf("Test failed.\n");
+        exit(2);
+    }
+
+    bert_init(&tx_bert, 0, BERT_PATTERN_ITU_O152_11, 300, 20);
+    bert_init(&rx_bert, 0, BERT_PATTERN_ITU_O152_11, 300, 20);
+    for (i = 0;  i < 0x800;  i++)
+        test[i] = 0;
+    max_zeros = 0;
+    zeros = 0;
+    for (i = 0;  i < 2047*2;  i++)
+    {
+        bit = bert_get_bit(&tx_bert);
+        if (bit)
+        {
+            if (zeros > max_zeros)
+                max_zeros = zeros;
+            zeros = 0;
+        }
+        else
+        {
+            zeros++;
+        }
+        bert_put_bit(&rx_bert, bit);        
+        test[tx_bert.tx_reg]++;
+    }
+    failed = FALSE;
+    if (test[0] != 0)
+    {
+        printf("XXX %d %d\n", 0, test[0]);
+        failed = TRUE;
+    }
+    for (i = 1;  i < 0x800;  i++)
+    {
+        if (test[i] != 2)
+        {
+            printf("XXX %d %d\n", i, test[i]);
+            failed = TRUE;
+        }
+    }
+    bert_result(&rx_bert, &bert_results);
+    printf("O.152(11): Bad bits %d/%d, max zeros %d\n", bert_results.bad_bits, bert_results.total_bits, max_zeros);
+    if (bert_results.bad_bits  ||  bert_results.total_bits != 4052  ||  failed)
+    {
+        printf("Test failed.\n");
+        exit(2);
+    }
+
+    bert_init(&tx_bert, 0, BERT_PATTERN_ITU_O151_15, 300, 20);
+    bert_init(&rx_bert, 0, BERT_PATTERN_ITU_O151_15, 300, 20);
+    for (i = 0;  i < 0x8000;  i++)
+        test[i] = 0;
+    max_zeros = 0;
+    zeros = 0;
+    for (i = 0;  i < 32767*2;  i++)
+    {
+        bit = bert_get_bit(&tx_bert);
+        if (bit)
+        {
+            if (zeros > max_zeros)
+                max_zeros = zeros;
+            zeros = 0;
+        }
+        else
+        {
+            zeros++;
+        }
+        bert_put_bit(&rx_bert, bit);        
+        test[tx_bert.tx_reg]++;
+    }
+    failed = FALSE;
+    if (test[0] != 0)
+    {
+        printf("XXX %d %d\n", 0, test[0]);
+        failed = TRUE;
+    }
+    for (i = 1;  i < 0x8000;  i++)
+    {
+        if (test[i] != 2)
+        {
+            printf("XXX %d %d\n", i, test[i]);
+            failed = TRUE;
+        }
+    }
+    bert_result(&rx_bert, &bert_results);
+    printf("O.151(15): Bad bits %d/%d, max zeros %d\n", bert_results.bad_bits, bert_results.total_bits, max_zeros);
+    if (bert_results.bad_bits  ||  bert_results.total_bits != 65480  ||  failed)
+    {
+        printf("Test failed.\n");
+        exit(2);
+    }
+
+    bert_init(&tx_bert, 0, BERT_PATTERN_ITU_O151_20, 300, 20);
+    bert_init(&rx_bert, 0, BERT_PATTERN_ITU_O151_20, 300, 20);
+    for (i = 0;  i < 0x100000;  i++)
+        test[i] = 0;    
+    max_zeros = 0;
+    zeros = 0;
+    for (i = 0;  i < 1048575*2;  i++)
+    {
+        bit = bert_get_bit(&tx_bert);
+        if (bit)
+        {
+            if (zeros > max_zeros)
+                max_zeros = zeros;
+            zeros = 0;
+        }
+        else
+        {
+            zeros++;
+        }
+        bert_put_bit(&rx_bert, bit);        
+        test[tx_bert.tx_reg]++;
+    }
+    failed = FALSE;
+    if (test[0] != 0)
+    {
+        printf("XXX %d %d\n", 0, test[0]);
+        failed = TRUE;
+    }
+    for (i = 1;  i < 0x100000;  i++)
+    {
+        if (test[i] != 2)
+            printf("XXX %d %d\n", i, test[i]);
+    }
+    bert_result(&rx_bert, &bert_results);
+    printf("O.151(20): Bad bits %d/%d, max zeros %d\n", bert_results.bad_bits, bert_results.total_bits, max_zeros);
+    if (bert_results.bad_bits  ||  bert_results.total_bits != 2097066  ||  failed)
+    {
+        printf("Test failed.\n");
+        exit(2);
+    }
+
+    bert_init(&tx_bert, 0, BERT_PATTERN_ITU_O151_23, 300, 20);
+    bert_init(&rx_bert, 0, BERT_PATTERN_ITU_O151_23, 300, 20);
+    for (i = 0;  i < 0x800000;  i++)
+        test[i] = 0;    
+    max_zeros = 0;
+    zeros = 0;
+    for (i = 0;  i < 8388607*2;  i++)
+    {
+        bit = bert_get_bit(&tx_bert);
+        if (bit)
+        {
+            if (zeros > max_zeros)
+                max_zeros = zeros;
+            zeros = 0;
+        }
+        else
+        {
+            zeros++;
+        }
+        bert_put_bit(&rx_bert, bit);        
+        test[tx_bert.tx_reg]++;
+    }
+    failed = FALSE;
+    if (test[0] != 0)
+    {
+        printf("XXX %d %d\n", 0, test[0]);
+        failed = TRUE;
+    }
+    for (i = 1;  i < 0x800000;  i++)
+    {
+        if (test[i] != 2)
+            printf("XXX %d %d\n", i, test[i]);
+    }
+    bert_result(&rx_bert, &bert_results);
+    printf("O.151(23): Bad bits %d/%d, max zeros %d\n", bert_results.bad_bits, bert_results.total_bits, max_zeros);
+    if (bert_results.bad_bits  ||  bert_results.total_bits != 16777136  ||  failed)
+    {
+        printf("Test failed.\n");
+        exit(2);
+    }
+
+    bert_init(&tx_bert, 0, BERT_PATTERN_QBF, 300, 20);
+    bert_init(&rx_bert, 0, BERT_PATTERN_QBF, 300, 20);
+    for (i = 0;  i < 100000;  i++)
+    {
+        bit = bert_get_bit(&tx_bert);
+        bert_put_bit(&rx_bert, bit);        
+    }
+    bert_result(&rx_bert, &bert_results);
+    printf("QBF:       Bad bits %d/%d\n", bert_results.bad_bits, bert_results.total_bits);
+    if (bert_results.bad_bits  ||  bert_results.total_bits != 100000)
+    {
+        printf("Test failed.\n");
+        exit(2);
+    }
+
+    /* Test the mechanism for categorising the error rate into <10^x bands */
+    /* TODO: The result of this test is not checked automatically */
+    bert_init(&bert, 15000000, BERT_PATTERN_ITU_O152_11, 300, 20);
+    bert_set_report(&bert, 100000, reporter, (intptr_t) 0);
+    for (;;)
+    {
+        if ((bit = bert_get_bit(&bert)) == PUTBIT_END_OF_DATA)
+        {
+            bert_result(&bert, &bert_results);
+            printf("Rate test: %d bits, %d bad bits, %d resyncs\n", bert_results.total_bits, bert_results.bad_bits, bert_results.resyncs);
+            if (bert_results.total_bits != 15000000 - 42
+                ||
+                bert_results.bad_bits != 58
+                ||
+                bert_results.resyncs != 0)
+            {
+                printf("Tests failed\n");
+                exit(2);
+            }
+            break;
+            //bert_init(&bert, 15000000, BERT_PATTERN_ITU_O152_11, 300, 20);
+            //bert_set_report(&bert, 100000, reporter, (intptr_t) 0);
+            //continue;
+        }
+        if ((my_rand() & 0x3FFFF) == 0)
+            bit ^= 1;
+        //if ((my_rand() & 0xFFF) == 0)
+        //    bert_put_bit(&bert, bit);
+        bert_put_bit(&bert, bit);
+    }
+    
+    printf("Tests passed.\n");
+    return  0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/tests/bit_operations_tests.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/tests/bit_operations_tests.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,295 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * bit_operations_tests.c
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2006 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: bit_operations_tests.c,v 1.14 2008/05/13 13:17:25 steveu Exp $
+ */
+
+/*! \page bit_operations_tests_page Bit operations tests
+\section bit_operations_tests_page_sec_1 What does it do?
+These tests check the operation of efficient bit manipulation routines, by comparing
+their operation with very dumb brute force versions of the same functionality.
+
+\section bit_operations_tests_page_sec_2 How is it used?
+*/
+
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <string.h>
+#include <assert.h>
+
+#include "spandsp.h"
+
+uint8_t from[1000000];
+uint8_t to[1000000];
+
+static __inline__ int top_bit_dumb(unsigned int data)
+{
+    int i;
+    
+    if (data == 0)
+        return -1;
+    for (i = 31;  i >= 0;  i--)
+    {
+        if ((data & (1 << i)))
+            return i;
+    }
+    return -1;
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ int bottom_bit_dumb(unsigned int data)
+{
+    int i;
+    
+    if (data == 0)
+        return -1;
+    for (i = 0;  i < 32;  i++)
+    {
+        if ((data & (1 << i)))
+            return i;
+    }
+    return -1;
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ uint8_t bit_reverse8_dumb(uint8_t data)
+{
+    int i;
+    int result;
+    
+    result = 0;
+    for (i = 0;  i < 8;  i++)
+    {
+        result = (result << 1) | (data & 1); 
+        data >>= 1;
+    }
+    return result;
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ uint32_t bit_reverse_4bytes_dumb(uint32_t data)
+{
+    int i;
+    uint32_t result;
+    
+    result = 0;
+    for (i = 0;  i < 8;  i++)
+    {
+        result = (result << 1) | (data & 0x01010101); 
+        data >>= 1;
+    }
+    return result;
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ uint16_t bit_reverse16_dumb(uint16_t data)
+{
+    int i;
+    uint16_t result;
+    
+    result = 0;
+    for (i = 0;  i < 16;  i++)
+    {
+        result = (result << 1) | (data & 1); 
+        data >>= 1;
+    }
+    return result;
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ uint32_t bit_reverse32_dumb(uint32_t data)
+{
+    int i;
+    uint32_t result;
+    
+    result = 0;
+    for (i = 0;  i < 32;  i++)
+    {
+        result = (result << 1) | (data & 1); 
+        data >>= 1;
+    }
+    return result;
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ int parity8_dumb(uint8_t x)
+{
+    uint8_t y;
+    int i;
+
+    for (y = 0, i = 0;  i < 8;  i++)
+    {
+        y ^= (x & 1);
+        x >>= 1;
+    }
+    return y;
+}
+/*- End of function --------------------------------------------------------*/
+
+static __inline__ int one_bits32_dumb(uint32_t x)
+{
+    int i;
+    int bits;
+    
+    bits = 0;
+    for (i = 0;  i < 32;  i++)
+    {
+        if (x & 1)
+            bits++;
+        x >>= 1;
+    }
+    return bits;
+}
+/*- End of function --------------------------------------------------------*/
+    
+int main(int argc, char *argv[])
+{
+    int i;
+    uint32_t x;
+    uint8_t ax;
+    uint8_t bx;
+    uint16_t ax16;
+    uint16_t bx16;
+    uint32_t ax32;
+    uint32_t bx32;
+
+    for (i = 0, x = 0;  i < 100000;  i++)
+    {
+        ax = top_bit_dumb(x);
+        bx = top_bit(x);
+        if (ax != bx)
+        {
+            printf("Test failed: top bit mismatch 0x%" PRIx32 " -> %u %u\n", x, ax, bx);
+            exit(2);
+        }
+        ax = bottom_bit_dumb(x);
+        bx = bottom_bit(x);
+        if (ax != bx)
+        {
+            printf("Test failed: bottom bit mismatch 0x%" PRIx32 " -> %u %u\n", x, ax, bx);
+            exit(2);
+        }
+        x = rand();
+    }
+    for (i = 0;  i < 256;  i++)
+    {
+        ax = bit_reverse8_dumb(i);
+        bx = bit_reverse8(i);
+        if (ax != bx)
+        {
+            printf("Test failed: bit reverse 8 - %02x %02x %02x\n", i, ax, bx);
+            exit(2);
+        }
+    }
+    for (i = 0;  i < 1000000;  i++)
+        from[i] = rand();
+    bit_reverse(to, from, 1000000);
+    for (i = 0;  i < 1000000;  i++)
+    {
+        if (bit_reverse8_dumb(from[i]) != to[i])
+        {
+            printf("Test failed: bit reverse - at %d, %02x %02x %02x\n", i, from[i], bit_reverse8(from[i]), to[i]);
+            exit(2);
+        }
+    }
+    for (i = 0;  i < 256;  i++)
+    {
+        x = i | (((i + 1) & 0xFF) << 8) | (((i + 2) & 0xFF) << 16) | (((i + 3) & 0xFF) << 24);
+        ax32 = bit_reverse_4bytes_dumb(x);
+        bx32 = bit_reverse_4bytes(x);
+        if (ax32 != bx32)
+        {
+            printf("Test failed: bit reverse 4 bytes - %" PRIx32 " %" PRIx32 " %" PRIx32 "\n", x, ax32, bx32);
+            exit(2);
+        }
+    }
+    for (i = 0;  i < 65536;  i++)
+    {
+        ax16 = bit_reverse16_dumb(i);
+        bx16 = bit_reverse16(i);
+        if (ax16 != bx16)
+        {
+            printf("Test failed: bit reverse 16 - %x %x %x\n", i, ax16, bx16);
+            exit(2);
+        }
+    }
+    for (i = 0;  i < 0x7FFFFF00;  i += 127)
+    {
+        ax32 = bit_reverse32_dumb(i);
+        bx32 = bit_reverse32(i);
+        if (ax32 != bx32)
+        {
+            printf("Test failed: bit reverse 32 - %d %" PRIx32 " %" PRIx32 "\n", i, ax32, bx32);
+            exit(2);
+        }
+    }
+
+    for (i = 0;  i < 256;  i++)
+    {
+        ax = parity8(i);
+        bx = parity8_dumb(i);
+        if (ax != bx)
+        {
+            printf("Test failed: parity 8 - %x %x %x\n", i, ax, bx);
+            exit(2);
+        }
+    }
+
+    for (i = -1;  i < 32;  i++)
+    {
+        ax32 = most_significant_one32(1 << i);
+        if (ax32 != (1 << i))
+        {
+            printf("Test failed: most significant one 32 - %x %" PRIx32 " %x\n", i, ax32, (1 << i));
+            exit(2);
+        }
+        ax32 = least_significant_one32(1 << i);
+        if (ax32 != (1 << i))
+        {
+            printf("Test failed: least significant one 32 - %x %" PRIx32 " %x\n", i, ax32, (1 << i));
+            exit(2);
+        }
+    }
+
+    for (i = 0x80000000;  i < 0x800FFFFF;  i++)
+    {
+        ax = one_bits32_dumb(i);
+        bx = one_bits32(i);
+        if (ax != bx)
+        {
+            printf("Test failed: one bits - %d, %x %x\n", i, ax, bx);
+            exit(2);
+        }
+    }
+
+    printf("Tests passed.\n");
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/tests/crc_tests.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/tests/crc_tests.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,111 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * crc_tests.c
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2003 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: crc_tests.c,v 1.6 2008/05/13 13:17:25 steveu Exp $
+ */
+
+/*! \file */
+
+/*! \page crc_tests_page CRC tests
+\section crc_tests_page_sec_1 What does it do?
+The CRC tests exercise the ITU-16 and ITU-32 CRC module, and verifies
+correct operation.
+*/
+
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "spandsp.h"
+
+int ref_len;
+uint8_t buf[1000];
+
+/* Use a local random generator, so the results are consistent across platforms. We have hard coded
+   correct results for a message sequence generated by this particular PRNG. */
+static int my_rand(void)
+{
+    static int rndnum = 1234567;
+
+    return (rndnum = 1664525U*rndnum + 1013904223U) >> 8;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int cook_up_msg(uint8_t *buf)
+{
+    int i;
+    int len;
+
+    /* Use medium length messages, with some randomised length variation. */
+    /* TODO: this doesn't exercise extremely short or long messages. */
+    len = (my_rand() & 0x3F) + 100;
+    for (i = 0;  i < len;  i++)
+        buf[i] = my_rand();
+    return len;
+}
+/*- End of function --------------------------------------------------------*/
+
+int main(int argc, char *argv[])
+{
+    int i;
+    int len;
+
+    printf("HDLC module tests\n");
+
+    /* TODO: This doesn't check every function in the module */
+    
+    /* Try a few random messages through the CRC logic. */
+    printf("Testing the CRC-16 routines\n");
+    for (i = 0;  i < 100;  i++)
+    {
+        ref_len = cook_up_msg(buf);
+        len = crc_itu16_append(buf, ref_len);
+        if (!crc_itu16_check(buf, len))
+        {
+            printf("CRC-16 failure\n");
+            exit(2);
+        }
+    }
+    printf("Test passed.\n\n");
+    
+    printf("Testing the CRC-32 routines\n");
+    for (i = 0;  i < 100;  i++)
+    {
+        ref_len = cook_up_msg(buf);
+        len = crc_itu32_append(buf, ref_len);
+        if (!crc_itu32_check(buf, len))
+        {
+            printf("CRC-32 failure\n");
+            exit(2);
+        }
+    }
+    printf("Test passed.\n");
+    return  0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/tests/dc_restore_tests.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/tests/dc_restore_tests.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,94 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * dc_restore_tests.c - Tests for the dc_restore functions.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2001 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: dc_restore_tests.c,v 1.23 2008/05/13 13:17:25 steveu Exp $
+ */
+
+/*! \page dc_restore_tests_page DC restoration tests
+\section dc_restore_tests_page_sec_1 What does it do?
+*/
+
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <memory.h>
+#include <time.h>
+
+#include "spandsp.h"
+    
+int main (int argc, char *argv[])
+{
+    awgn_state_t noise_source;
+    dc_restore_state_t dc_state;
+    int i;
+    int idum = 1234567;
+    int16_t dirty;
+    int16_t clean;
+    int estimate;
+    int min;
+    int max;
+    int dc_offset;
+
+    dc_offset = 5000;
+    awgn_init_dbm0(&noise_source, idum, -10.0);
+    dc_restore_init(&dc_state);
+    for (i = 0;  i < 100000;  i++)
+    {
+        dirty = awgn(&noise_source) + dc_offset;
+        clean = dc_restore(&dc_state, dirty);
+        if ((i % 1000) == 0)
+        {
+            printf("Sample %6d: %d (expect %d)\n",
+                   i,
+                   dc_restore_estimate(&dc_state),
+                   dc_offset);
+        }
+    }
+    /* We should have settled by now. Look at the variation we get */
+    min = 99999;
+    max = -99999;
+    for (i = 0;  i < 100000;  i++)
+    {
+        dirty = awgn(&noise_source) + dc_offset;
+        clean = dc_restore(&dc_state, dirty);
+        estimate = dc_restore_estimate(&dc_state);
+        if (estimate < min)
+            min = estimate;
+        if (estimate > max)
+            max = estimate;
+    }
+    printf("Spread of DC estimate for an offset of %d was %d to %d\n", dc_offset, min, max);
+    if (min < dc_offset - 50  ||  max > dc_offset + 50)
+    {
+        printf("Test failed.\n");
+        exit(2);
+    }
+    printf("Test passed.\n");
+    return  0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/tests/dds_tests.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/tests/dds_tests.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,256 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * dds_tests.c
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2003 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: dds_tests.c,v 1.24 2008/05/13 13:17:25 steveu Exp $
+ */
+
+/*! \file */
+
+/*! \page dds_tests_page Direct digital synthesis tests
+\section dds_tests_page_sec_1 What does it do?
+???.
+
+\section dds_tests_page_sec_2 How does it work?
+???.
+*/
+
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <memory.h>
+#include <audiofile.h>
+
+#include "spandsp.h"
+
+#define OUTPUT_FILE_NAME            "dds.wav"
+#define OUTPUT_FILE_NAME_COMPLEX    "complex_dds.wav"
+
+#define SAMPLES_PER_CHUNK           8000
+
+int main(int argc, char *argv[])
+{
+    int i;
+    uint32_t phase;
+    int32_t phase_inc;
+    int outframes;
+    complexf_t camp;
+    int16_t buf[2*SAMPLES_PER_CHUNK];
+    AFfilehandle outhandle;
+    AFfilesetup filesetup;
+    power_meter_t meter;
+    power_meter_t meter_i;
+    power_meter_t meter_q;
+    int scale;
+
+    power_meter_init(&meter, 10);
+
+    printf("Non-complex DDS tests.\n");
+    if ((filesetup = afNewFileSetup()) == AF_NULL_FILESETUP)
+    {
+        fprintf(stderr, "    Failed to create file setup\n");
+        exit(2);
+    }
+    afInitSampleFormat(filesetup, AF_DEFAULT_TRACK, AF_SAMPFMT_TWOSCOMP, 16);
+    afInitRate(filesetup, AF_DEFAULT_TRACK, (float) SAMPLE_RATE);
+    afInitFileFormat(filesetup, AF_FILE_WAVE);
+    afInitChannels(filesetup, AF_DEFAULT_TRACK, 1);
+
+    if ((outhandle = afOpenFile(OUTPUT_FILE_NAME, "w", filesetup)) == AF_NULL_FILEHANDLE)
+    {
+        fprintf(stderr, "    Cannot create wave file '%s'\n", OUTPUT_FILE_NAME);
+        exit(2);
+    }
+
+    phase = 0;
+
+    printf("Test with 123.456789Hz.\n");
+    phase_inc = dds_phase_rate(123.456789f);
+    scale = dds_scaling_dbm0(-10.0f);
+    for (i = 0;  i < SAMPLES_PER_CHUNK;  i++)
+    {
+        buf[i] = alaw_to_linear(linear_to_alaw((dds(&phase, phase_inc)*scale) >> 15));
+        power_meter_update(&meter, buf[i]);
+    }
+    outframes = afWriteFrames(outhandle,
+                              AF_DEFAULT_TRACK,
+                              buf,
+                              SAMPLES_PER_CHUNK);
+    if (outframes != SAMPLES_PER_CHUNK)
+    {
+        fprintf(stderr, "    Error writing wave file\n");
+        exit(2);
+    }
+    printf("Level is %fdBOv/%fdBm0\n", power_meter_current_dbov(&meter), power_meter_current_dbm0(&meter));
+    if (fabs(power_meter_current_dbm0(&meter) + 10.0f) > 0.05f)
+    {
+        printf("Test failed.\n");
+        exit(2);
+    }
+
+    printf("Test with 12.3456789Hz.\n");
+    phase_inc = dds_phase_rate(12.3456789f);
+    for (i = 0;  i < SAMPLES_PER_CHUNK;  i++)
+    {
+        buf[i] = alaw_to_linear(linear_to_alaw(dds(&phase, phase_inc)));
+        power_meter_update(&meter, buf[i]);
+    }
+    outframes = afWriteFrames(outhandle,
+                              AF_DEFAULT_TRACK,
+                              buf,
+                              SAMPLES_PER_CHUNK);
+    if (outframes != SAMPLES_PER_CHUNK)
+    {
+        fprintf(stderr, "    Error writing wave file\n");
+        exit(2);
+    }
+    printf("Level is %fdBOv/%fdBm0\n", power_meter_current_dbov(&meter), power_meter_current_dbm0(&meter));
+    /* Use a wider tolerance for this very low frequency - the power meter will ripple */
+    if (fabs(power_meter_current_dbov(&meter) + 3.02f) > 0.2f)
+    {
+        printf("Test failed.\n");
+        exit(2);
+    }
+
+    printf("Test with 2345.6789Hz.\n");
+    phase_inc = dds_phase_rate(2345.6789f);
+    scale = dds_scaling_dbov(-10.0f);
+    for (i = 0;  i < SAMPLES_PER_CHUNK;  i++)
+    {
+        buf[i] = alaw_to_linear(linear_to_alaw((dds(&phase, phase_inc)*scale) >> 15));
+        power_meter_update(&meter, buf[i]);
+    }
+    outframes = afWriteFrames(outhandle,
+                              AF_DEFAULT_TRACK,
+                              buf,
+                              SAMPLES_PER_CHUNK);
+    if (outframes != SAMPLES_PER_CHUNK)
+    {
+        fprintf(stderr, "    Error writing wave file\n");
+        exit(2);
+    }
+    printf("Level is %fdBOv/%fdBm0\n", power_meter_current_dbov(&meter), power_meter_current_dbm0(&meter));
+    if (fabs(power_meter_current_dbov(&meter) + 10.0f) > 0.05f)
+    {
+        printf("Test failed.\n");
+        exit(2);
+    }
+
+    printf("Test with 3456.789Hz.\n");
+    phase_inc = dds_phase_rate(3456.789f);
+    for (i = 0;  i < SAMPLES_PER_CHUNK;  i++)
+    {
+        buf[i] = alaw_to_linear(linear_to_alaw(dds(&phase, phase_inc)));
+        power_meter_update(&meter, buf[i]);
+    }
+    outframes = afWriteFrames(outhandle,
+                              AF_DEFAULT_TRACK,
+                              buf,
+                              SAMPLES_PER_CHUNK);
+    if (outframes != SAMPLES_PER_CHUNK)
+    {
+        fprintf(stderr, "    Error writing wave file\n");
+        exit(2);
+    }
+    printf("Level is %fdBOv/%fdBm0\n", power_meter_current_dbov(&meter), power_meter_current_dbm0(&meter));
+    if (fabs(power_meter_current_dbov(&meter) + 3.02f) > 0.05f)
+    {
+        printf("Test failed.\n");
+        exit(2);
+    }
+
+    if (afCloseFile(outhandle) != 0)
+    {
+        fprintf(stderr, "    Cannot close wave file '%s'\n", OUTPUT_FILE_NAME);
+        exit(2);
+    }
+    afFreeFileSetup(filesetup);
+
+    printf("Complex DDS tests,\n");
+    if ((filesetup = afNewFileSetup()) == AF_NULL_FILESETUP)
+    {
+        fprintf(stderr, "    Failed to create file setup\n");
+        exit(2);
+    }
+    afInitSampleFormat(filesetup, AF_DEFAULT_TRACK, AF_SAMPFMT_TWOSCOMP, 16);
+    afInitRate(filesetup, AF_DEFAULT_TRACK, (float) SAMPLE_RATE);
+    afInitFileFormat(filesetup, AF_FILE_WAVE);
+    afInitChannels(filesetup, AF_DEFAULT_TRACK, 2);
+
+    if ((outhandle = afOpenFile(OUTPUT_FILE_NAME_COMPLEX, "w", filesetup)) == AF_NULL_FILEHANDLE)
+    {
+        fprintf(stderr, "    Cannot create wave file '%s'\n", OUTPUT_FILE_NAME_COMPLEX);
+        exit(2);
+    }
+
+    power_meter_init(&meter_i, 7);
+    power_meter_init(&meter_q, 7);
+
+    phase = 0;
+    phase_inc = dds_phase_ratef(123.456789f);
+    for (i = 0;  i < SAMPLES_PER_CHUNK;  i++)
+    {
+        camp = dds_complexf(&phase, phase_inc);
+        buf[2*i] = camp.re*10000.0f;
+        buf[2*i + 1] = camp.im*10000.0f;
+        power_meter_update(&meter_i, buf[2*i]);
+        power_meter_update(&meter_q, buf[2*i]);
+    }
+    outframes = afWriteFrames(outhandle,
+                              AF_DEFAULT_TRACK,
+                              buf,
+                              SAMPLES_PER_CHUNK);
+    if (outframes != SAMPLES_PER_CHUNK)
+    {
+        fprintf(stderr, "    Error writing wave file\n");
+        exit(2);
+    }
+    printf("Level is %fdBOv/%fdBm0, %fdBOv/%fdBm0\n",
+           power_meter_current_dbov(&meter_i),
+           power_meter_current_dbm0(&meter_i),
+           power_meter_current_dbov(&meter_q),
+           power_meter_current_dbm0(&meter_q));
+    if (fabs(power_meter_current_dbov(&meter_i) + 13.42f) > 0.05f
+        ||
+        fabs(power_meter_current_dbov(&meter_q) + 13.42f) > 0.05f)
+    {
+        printf("Test failed.\n");
+        exit(2);
+    }
+
+    if (afCloseFile(outhandle) != 0)
+    {
+        fprintf(stderr, "    Cannot close wave file '%s'\n", OUTPUT_FILE_NAME_COMPLEX);
+        exit(2);
+    }
+    afFreeFileSetup(filesetup);
+
+    printf("Tests passed.\n");
+    return  0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/tests/dtmf_rx_tests.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/tests/dtmf_rx_tests.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,886 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * dtmf_rx_tests.c - Test the DTMF detector against the spec., whatever the spec.
+ *                   may be :)
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2001, 2006 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: dtmf_rx_tests.c,v 1.42 2008/08/29 09:28:13 steveu Exp $
+ */
+
+/*
+ * These tests include conversion to and from A-law. I assume the
+ * distortion this produces is comparable to u-law, so it should be
+ * a fair test.
+ *
+ * These tests mirror those on the CM7291 test tape from Mitel.
+ * Many of these tests are highly questionable, but they are a
+ * well accepted industry standard.
+ *
+ * However standard these tests might be, Mitel appears to have stopped
+ * selling copies of their tape.
+ *
+ * For the talk-off test the Bellcore tapes may be used. However, they are
+ * copyright material, so the test data files produced from the Bellcore
+ * tapes cannot be distributed as a part of this package.
+ *
+ */
+
+/*! \page dtmf_rx_tests_page DTMF receiver tests
+\section dtmf_rx_tests_page_sec_1 What does it do?
+
+The DTMF detection test suite performs similar tests to the Mitel test tape,
+traditionally used for testing DTMF receivers. Mitel seem to have discontinued
+this product, but all it not lost. 
+
+The first side of the Mitel tape consists of a number of tone and tone+noise
+based tests. The test suite synthesizes equivalent test data. Being digitally
+generated, this data is rather more predictable than the test data on the nasty
+old stretchy cassette tapes which Mitel sold. 
+
+The second side of the Mitel tape contains fragments of real speech from real
+phone calls captured from the North American telephone network. These are
+considered troublesome for DTMF detectors. A good detector is expected to
+achieve a reasonably low number of false detections on this data. Fresh clean
+copies of this seem to be unobtainable. However, Bellcore produce a much more
+aggressive set of three cassette tapes. All six side (about 30 minutes each) are
+filled with much tougher fragments of real speech from the North American
+telephone network. If you can do well in this test, nobody cares about your
+results against the Mitel test tape. 
+
+A fresh set of tapes was purchased for these tests, and digitised, producing 6
+wave files of 16 bit signed PCM data, sampled at 8kHz. They were transcribed
+using a speed adjustable cassette player. The test tone at the start of the
+tapes is pretty accurate, and the new tapes should not have had much opportunity
+to stretch. It is believed these transcriptions are about as good as the source
+material permits. 
+
+PLEASE NOTE
+
+These transcriptions may be freely used by anyone who has a legitimate copy of
+the original tapes. However, if you don't have a legitimate copy of those tapes,
+you also have no right to use this data. The original tapes are the copyright
+material of BellCore, and they charge over US$200 for a set. I doubt they sell
+enough copies to consider this much of a business. However, it is their data,
+and it is their right to do as they wish with it. Currently I see no indication
+they wish to give it away for free. 
+*/
+
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <time.h>
+#include <audiofile.h>
+
+#include "spandsp.h"
+#include "spandsp-sim.h"
+
+/* Basic DTMF specs:
+ *
+ * Minimum tone on = 40ms
+ * Minimum tone off = 50ms
+ * Maximum digit rate = 10 per second
+ * Normal twist <= 8dB accepted
+ * Reverse twist <= 4dB accepted
+ * S/N >= 15dB will detect OK
+ * Attenuation <= 26dB will detect OK
+ * Frequency tolerance +- 1.5% will detect, +-3.5% will reject
+ */
+
+#define DEFAULT_DTMF_TX_LEVEL       -10
+#define DEFAULT_DTMF_TX_ON_TIME     50
+#define DEFAULT_DTMF_TX_OFF_TIME    50
+
+#define SAMPLES_PER_CHUNK           160
+
+#define ALL_POSSIBLE_DIGITS         "123A456B789C*0#D"
+
+#define MITEL_DIR                   "../test-data/mitel/"
+#define BELLCORE_DIR                "../test-data/bellcore/"
+
+const char *bellcore_files[] =
+{
+    MITEL_DIR    "mitel-cm7291-talkoff.wav",
+    BELLCORE_DIR "tr-tsy-00763-1.wav",
+    BELLCORE_DIR "tr-tsy-00763-2.wav",
+    BELLCORE_DIR "tr-tsy-00763-3.wav",
+    BELLCORE_DIR "tr-tsy-00763-4.wav",
+    BELLCORE_DIR "tr-tsy-00763-5.wav",
+    BELLCORE_DIR "tr-tsy-00763-6.wav",
+    ""
+};
+
+static tone_gen_descriptor_t my_dtmf_digit_tones[16];
+
+float dtmf_row[] =
+{
+     697.0f,  770.0f,  852.0f,  941.0f
+};
+float dtmf_col[] =
+{
+    1209.0f, 1336.0f, 1477.0f, 1633.0f
+};
+
+char dtmf_positions[] = "123A" "456B" "789C" "*0#D";
+
+int callback_hit;
+int callback_ok;
+int callback_roll;
+int step;
+
+int use_dialtone_filter = FALSE;
+
+char *decode_test_file = NULL;
+
+static int16_t amp[1000000];
+static int16_t amp2[1000000];
+
+codec_munge_state_t *munge = NULL;
+
+static void my_dtmf_gen_init(float low_fudge,
+                             int low_level,
+                             float high_fudge,
+                             int high_level,
+                             int duration,
+                             int gap)
+{
+    int row;
+    int col;
+
+    for (row = 0;  row < 4;  row++)
+    {
+        for (col = 0;  col < 4;  col++)
+        {
+            make_tone_gen_descriptor(&my_dtmf_digit_tones[row*4 + col],
+                                     dtmf_row[row]*(1.0f + low_fudge),
+                                     low_level,
+                                     dtmf_col[col]*(1.0f + high_fudge),
+                                     high_level,
+                                     duration,
+                                     gap,
+                                     0,
+                                     0,
+                                     FALSE);
+        }
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static int my_dtmf_generate(int16_t amp[], const char *digits)
+{
+    int len;
+    char *cp;
+    tone_gen_state_t tone;
+
+    len = 0;
+    while (*digits)
+    {
+        cp = strchr(dtmf_positions, *digits);
+        if (cp)
+        {
+            tone_gen_init(&tone, &my_dtmf_digit_tones[cp - dtmf_positions]);
+            len += tone_gen(&tone, amp + len, 1000);
+        }
+        digits++;
+    }
+    return len;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void digit_delivery(void *data, const char *digits, int len)
+{
+    int i;
+    int seg;
+    const char *s = ALL_POSSIBLE_DIGITS;
+    const char *t;
+
+    callback_hit = TRUE;
+    if (data == (void *) 0x12345678)
+    {
+        t = s + callback_roll;
+        seg = 16 - callback_roll;
+        for (i = 0;  i < len;  i += seg, seg = 16)
+        {
+            if (i + seg > len)
+                seg = len - i;
+            if (memcmp(digits + i, t, seg))
+            {
+                callback_ok = FALSE;
+                printf("Fail at %d %d\n", i, seg);
+                break;
+            }
+            t = s;
+            callback_roll = (callback_roll + seg)%16;
+        }
+    }
+    else
+    {
+        callback_ok = FALSE;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static void digit_status(void *data, int signal, int level, int delay)
+{
+    const char *s = ALL_POSSIBLE_DIGITS;
+    int len;
+    static int last_step = 0;
+    static int first = TRUE;
+
+    callback_hit = TRUE;
+    len = step - last_step;
+    if (data == (void *) 0x12345678)
+    {
+        if (len < 320  ||  len > 480)
+        {
+            if (first)
+            {
+                /* At the beginning the apparent duration is expected to be wrong */
+                first = FALSE;
+            }
+            else
+            {
+                printf("Failed for signal %s length %d at %d\n", (callback_roll & 1)  ?  "on"  :  "off", len, step);
+                callback_ok = FALSE;
+            }
+        }
+        if (callback_roll & 1)
+        {
+            if (signal != 0)
+            {
+                printf("Failed for signal 0x%X instead of 0\n", signal);
+                callback_ok = FALSE;
+            }
+        }
+        else
+        {
+            if (signal != s[callback_roll >> 1])
+            {
+                printf("Failed for signal 0x%X instead of 0x%X\n", signal, s[callback_roll >> 1]);
+                callback_ok = FALSE;
+            }
+            if (level < DEFAULT_DTMF_TX_LEVEL + 3 - 1  ||  level > DEFAULT_DTMF_TX_LEVEL + 3 + 1)
+            {
+                printf("Failed for level %d instead of %d\n", level, DEFAULT_DTMF_TX_LEVEL + 3);
+                callback_ok = FALSE;
+            }
+        }
+        if (++callback_roll >= 32)
+            callback_roll = 0;
+    }
+    else
+    {
+        callback_ok = FALSE;
+    }
+    last_step = step;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void mitel_cm7291_side_1_tests(void)
+{
+    int i;
+    int j;
+    int len;
+    int sample;
+    const char *s;
+    char digit[2];
+    char buf[128 + 1];
+    int actual;
+    int nplus;
+    int nminus;
+    float rrb;
+    float rcfo;
+    dtmf_rx_state_t dtmf_state;
+    awgn_state_t noise_source;
+
+    dtmf_rx_init(&dtmf_state, NULL, NULL);
+    if (use_dialtone_filter)
+        dtmf_rx_parms(&dtmf_state, TRUE, -1, -1, -99);
+
+    /* Test 1: Mitel's test 1 isn't really a test. Its a calibration step,
+       which has no meaning here. */
+    printf("Test 1: Calibration\n");
+    printf("    Passed\n");
+
+    /* Test 2: Decode check
+       This is a sanity check, that all digits are reliably detected
+       under ideal conditions.  Each possible digit repeated 10 times,
+       with 50ms bursts. The level of each tone is about 6dB down from clip.
+       6dB down actually causes trouble with G.726, so we use 7dB down. */
+    printf("Test 2: Decode check\n");
+    my_dtmf_gen_init(0.0f, -4, 0.0f, -4, 50, 50);
+    s = ALL_POSSIBLE_DIGITS;
+    digit[1] = '\0';
+    while (*s)
+    {
+        digit[0] = *s++;
+        for (i = 0;  i < 10;  i++)
+        {
+            len = my_dtmf_generate(amp, digit);
+            codec_munge(munge, amp, len);
+            dtmf_rx(&dtmf_state, amp, len);
+
+            actual = dtmf_rx_get(&dtmf_state, buf, 128);
+
+            if (actual != 1  ||  buf[0] != digit[0])
+            {
+                printf("    Sent     '%s'\n", digit);
+                printf("    Received '%s'\n", buf);
+                printf("    Failed\n");
+                exit(2);
+            }
+        }
+    }
+    printf("    Passed\n");
+
+    /* Test 3: Recognition bandwidth and channel centre frequency check.
+       Use only the diagonal pairs of tones (digits 1, 5, 9 and D). Each
+       tone pair requires four test to complete the check, making 16
+       sections overall. Each section contains 40 pulses of
+       50ms duration, with an amplitude of -20dB from clip per
+       frequency.
+
+       Four sections covering the tests for one tone (1 digit) are:
+       a. H frequency at 0% deviation from center, L frequency at +0.1%.
+          L frequency is then increments in +01.% steps up to +4%. The
+          number of tone bursts is noted and designated N+.
+       b. H frequency at 0% deviation, L frequency at -0.1%. L frequency
+          is then incremental in -0.1% steps, up to -4%. The number of
+          tone bursts is noted and designated N-.
+       c. The test in (a) is repeated with the L frequency at 0% and the
+          H frequency varied up to +4%.
+       d. The test in (b) is repeated with the L frequency and 0% and the
+          H frequency varied to -4%.
+
+       Receiver Recognition Bandwidth (RRB) is calculated as follows:
+            RRB% = (N+ + N-)/10
+       Receiver Center Frequency Offset (RCFO) is calculated as follows:
+            RCFO% = X + (N+ - N-)/20
+
+       Note that this test doesn't test what it says it is testing at all,
+       and the results are quite inaccurate, if not a downright lie! However,
+       it follows the Mitel procedure, so how can it be bad? :)
+    */
+    printf("Test 3: Recognition bandwidth and channel centre frequency check\n");
+    s = "159D";
+    digit[1] = '\0';
+    while (*s)
+    {
+        digit[0] = *s++;
+        for (nplus = 0, i = 1;  i <= 60;  i++)
+        {
+            my_dtmf_gen_init((float) i/1000.0f, -17, 0.0f, -17, 50, 50);
+            len = my_dtmf_generate(amp, digit);
+            codec_munge(munge, amp, len);
+            dtmf_rx(&dtmf_state, amp, len);
+            nplus += dtmf_rx_get(&dtmf_state, buf, 128);
+        }
+        for (nminus = 0, i = -1;  i >= -60;  i--)
+        {
+            my_dtmf_gen_init((float) i/1000.0f, -17, 0.0f, -17, 50, 50);
+            len = my_dtmf_generate(amp, digit);
+            codec_munge(munge, amp, len);
+            dtmf_rx(&dtmf_state, amp, len);
+            nminus += dtmf_rx_get(&dtmf_state, buf, 128);
+        }
+        rrb = (float) (nplus + nminus)/10.0f;
+        rcfo = (float) (nplus - nminus)/10.0f;
+        printf("    %c (low)  rrb = %5.2f%%, rcfo = %5.2f%%, max -ve = %5.2f, max +ve = %5.2f\n",
+               digit[0],
+               rrb,
+               rcfo,
+               (float) nminus/10.0f,
+               (float) nplus/10.0f);
+        if (rrb < 3.0f + rcfo  ||  rrb >= 15.0f + rcfo)
+        {
+            printf("    Failed\n");
+            exit(2);
+        }
+
+        for (nplus = 0, i = 1;  i <= 60;  i++)
+        {
+            my_dtmf_gen_init(0.0f, -17, (float) i/1000.0f, -17, 50, 50);
+            len = my_dtmf_generate(amp, digit);
+            codec_munge(munge, amp, len);
+            dtmf_rx(&dtmf_state, amp, len);
+            nplus += dtmf_rx_get(&dtmf_state, buf, 128);
+        }
+        for (nminus = 0, i = -1;  i >= -60;  i--)
+        {
+            my_dtmf_gen_init(0.0f, -17, (float) i/1000.0f, -17, 50, 50);
+            len = my_dtmf_generate(amp, digit);
+            codec_munge(munge, amp, len);
+            dtmf_rx(&dtmf_state, amp, len);
+            nminus += dtmf_rx_get(&dtmf_state, buf, 128);
+        }
+        rrb = (float) (nplus + nminus)/10.0f;
+        rcfo = (float) (nplus - nminus)/10.0f;
+        printf("    %c (high) rrb = %5.2f%%, rcfo = %5.2f%%, max -ve = %5.2f, max +ve = %5.2f\n",
+               digit[0],
+               rrb,
+               rcfo,
+               (float) nminus/10.0f,
+               (float) nplus/10.0f);
+        if (rrb < 3.0f + rcfo  ||  rrb >= 15.0f + rcfo)
+        {
+            printf("    Failed\n");
+            exit(2);
+        }
+    }
+    printf("    Passed\n");
+
+    /* Test 4: Acceptable amplitude ratio (twist).
+       Use only the diagonal pairs of tones (digits 1, 5, 9 and D). 
+       There are eight sections to the test. Each section contains 200
+       pulses with a 50ms duration for each pulse. Initially the amplitude
+       of both tones is 6dB down from clip. The two sections to test one
+       tone pair are:
+
+       a. Standard Twist: H tone amplitude is maintained at -6dB from clip,
+          L tone amplitude is attenuated gradually until the amplitude ratio
+          L/H is -20dB. Note the number of responses from the receiver.
+       b. Reverse Twist: L tone amplitude is maintained at -6dB from clip,
+          H tone amplitude is attenuated gradually until the amplitude ratio
+          is 20dB. Note the number of responses from the receiver.
+
+       All tone bursts are of 50ms duration.
+
+       The Acceptable Amplitude Ratio in dB is equal to the number of
+       responses registered in (a) or (b), divided by 10.
+       
+       TODO: This is supposed to work in 1/10dB steps, but here I used 1dB
+             steps, as the current tone generator has its amplitude set in
+             1dB steps.
+    */
+    printf("Test 4: Acceptable amplitude ratio (twist)\n");
+    s = "159D";
+    digit[1] = '\0';
+    while (*s)
+    {
+        digit[0] = *s++;
+        for (nplus = 0, i = -30;  i >= -230;  i--)
+        {
+            my_dtmf_gen_init(0.0f, -3, 0.0f, i/10, 50, 50);
+
+            len = my_dtmf_generate(amp, digit);
+            codec_munge(munge, amp, len);
+            dtmf_rx(&dtmf_state, amp, len);
+            nplus += dtmf_rx_get(&dtmf_state, buf, 128);
+        }
+        printf("    %c normal twist  = %.2fdB\n", digit[0], (float) nplus/10.0);
+        if (nplus < 80)
+        {
+            printf("    Failed\n");
+            exit(2);
+        }
+        for (nminus = 0, i = -30;  i >= -230;  i--)
+        {
+            my_dtmf_gen_init(0.0f, i/10, 0.0f, -3, 50, 50);
+
+            len = my_dtmf_generate(amp, digit);
+            codec_munge(munge, amp, len);
+            dtmf_rx(&dtmf_state, amp, len);
+            nminus += dtmf_rx_get(&dtmf_state, buf, 128);
+        }
+        printf("    %c reverse twist = %.2fdB\n", digit[0], (float) nminus/10.0);
+        if (nminus < 40)
+        {
+            printf("    Failed\n");
+            exit(2);
+        }
+    }
+    printf("    Passed\n");
+
+    /* Test 5: Dynamic range
+       This test utilizes tone pair L1 H1 (digit 1). Thirty-five tone pair
+       pulses are transmitted, with both frequencies stating at -6dB from
+       clip. The amplitude of each is gradually attenuated by -35dB at a
+       rate of 1dB per pulse. The Dynamic Range in dB is equal to the
+       number of responses from the receiver during the test.
+       
+       Well not really, but that is the Mitel test. Lets sweep a bit further,
+       and see what the real range is */
+    printf("Test 5: Dynamic range\n");
+    for (nplus = 0, i = +3;  i >= -50;  i--)
+    {
+        my_dtmf_gen_init(0.0f, i, 0.0f, i, 50, 50);
+
+        len = my_dtmf_generate(amp, "1");
+        codec_munge(munge, amp, len);
+        dtmf_rx(&dtmf_state, amp, len);
+        nplus += dtmf_rx_get(&dtmf_state, buf, 128);
+    }
+    printf("    Dynamic range = %ddB\n", nplus);
+    /* We ought to set some pass/fail condition, even if Mitel did not. If
+       we don't, regression testing is weakened. */
+    if (nplus < 35)
+    {
+        printf("    Failed\n");
+        exit(2);
+    }
+    printf("    Passed\n");
+
+    /* Test 6: Guard time
+       This test utilizes tone pair L1 H1 (digit 1). Four hundred pulses
+       are transmitted at an amplitude of -6dB from clip per frequency.
+       Pulse duration starts at 49ms and is gradually reduced to 10ms.
+       Guard time in ms is equal to (500 - number of responses)/10.
+       
+       That is the Mitel test, and we will follow it. Its totally bogus,
+       though. Just what the heck is a pass or fail here? */
+
+    printf("Test 6: Guard time\n");
+    for (nplus = 0, i = 490;  i >= 100;  i--)
+    {
+        my_dtmf_gen_init(0.0f, -3, 0.0f, -3, i/10, 50);
+
+        len = my_dtmf_generate(amp, "1");
+        codec_munge(munge, amp, len);
+        dtmf_rx(&dtmf_state, amp, len);
+        nplus += dtmf_rx_get(&dtmf_state, buf, 128);
+    }
+    printf("    Guard time = %dms\n", (500 - nplus)/10);
+    printf("    Passed\n");
+
+    /* Test 7: Acceptable signal to noise ratio
+       This test utilizes tone pair L1 H1, transmitted on a noise background.
+       The test consists of three sections in which the tone pair is
+       transmitted 1000 times at an amplitude -6dB from clip per frequency,
+       but with a different white noise level for each section. The first
+       level is -24dBV, the second -18dBV and the third -12dBV.. The
+       acceptable signal to noise ratio is the lowest ratio of signal
+       to noise in the test where the receiver responds to all 1000 pulses.
+       
+       Well, that is the Mitel test, but it doesn't tell you what the
+       decoder can really do. Lets do a more comprehensive test */
+
+    printf("Test 7: Acceptable signal to noise ratio\n");
+    my_dtmf_gen_init(0.0f, -4, 0.0f, -4, 50, 50);
+
+    for (j = -13;  j > -50;  j--)
+    {
+        awgn_init_dbm0(&noise_source, 1234567, (float) j);
+        for (i = 0;  i < 1000;  i++)
+        {
+            len = my_dtmf_generate(amp, "1");
+
+            // TODO: Clip
+            for (sample = 0;  sample < len;  sample++)
+                amp[sample] = saturate(amp[sample] + awgn(&noise_source));
+            
+            codec_munge(munge, amp, len);
+            dtmf_rx(&dtmf_state, amp, len);
+
+            if (dtmf_rx_get(&dtmf_state, buf, 128) != 1)
+                break;
+        }
+        if (i == 1000)
+            break;
+    }
+    printf("    Acceptable S/N ratio is %ddB\n", -4 - j);
+    if (-4 - j > 26)
+    {
+        printf("    Failed\n");
+        exit(2);
+    }
+    printf("    Passed\n");
+}
+/*- End of function --------------------------------------------------------*/
+
+static void mitel_cm7291_side_2_and_bellcore_tests(void)
+{
+    int i;
+    int j;
+    int len;
+    int hits;
+    int hit_types[256];
+    char buf[128 + 1];
+    AFfilehandle inhandle;
+    int frames;
+    dtmf_rx_state_t dtmf_state;
+
+    dtmf_rx_init(&dtmf_state, NULL, NULL);
+    if (use_dialtone_filter)
+        dtmf_rx_parms(&dtmf_state, TRUE, -1, -1, -99);
+
+    /* The remainder of the Mitel tape is the talk-off test */
+    /* Here we use the Bellcore test tapes (much tougher), in six
+       wave files - 1 from each side of the original 3 cassette tapes */
+    /* Bellcore say you should get no more than 470 false detections with
+       a good receiver. Dialogic claim 20. Of course, we can do better than
+       that, eh? */
+    printf("Test 8: Talk-off test\n");
+    memset(hit_types, '\0', sizeof(hit_types));
+    for (j = 0;  bellcore_files[j][0];  j++)
+    {
+        if ((inhandle = afOpenFile_telephony_read(bellcore_files[j], 1)) == AF_NULL_FILEHANDLE)
+        {
+            printf("    Cannot open speech file '%s'\n", bellcore_files[j]);
+            exit(2);
+        }
+        hits = 0;
+        while ((frames = afReadFrames(inhandle, AF_DEFAULT_TRACK, amp, SAMPLE_RATE)))
+        {
+            dtmf_rx(&dtmf_state, amp, frames);
+            len = dtmf_rx_get(&dtmf_state, buf, 128);
+            if (len > 0)
+            {
+                for (i = 0;  i < len;  i++)
+                    hit_types[(int) buf[i]]++;
+                hits += len;
+            }
+        }
+        if (afCloseFile(inhandle) != 0)
+        {
+            printf("    Cannot close speech file '%s'\n", bellcore_files[j]);
+            exit(2);
+        }
+        printf("    File %d gave %d false hits.\n", j + 1, hits);
+    }
+    for (i = 0, j = 0;  i < 256;  i++)
+    {
+        if (hit_types[i])
+        {
+            printf("    Digit %c had %d false hits\n", i, hit_types[i]);
+            j += hit_types[i];
+        }
+    }
+    printf("    %d hits in total\n", j);
+    if (j > 470)
+    {
+        printf("    Failed\n");
+        exit(2);
+    }
+    printf("    Passed\n");
+}
+/*- End of function --------------------------------------------------------*/
+
+static void dial_tone_tolerance_tests(void)
+{
+    int i;
+    int j;
+    int len;
+    int sample;
+    char buf[128 + 1];
+    dtmf_rx_state_t dtmf_state;
+    tone_gen_descriptor_t dial_tone_desc;
+    tone_gen_state_t dial_tone;
+
+    dtmf_rx_init(&dtmf_state, NULL, NULL);
+    if (use_dialtone_filter)
+        dtmf_rx_parms(&dtmf_state, TRUE, -1, -1, -99);
+
+    /* Test dial tone tolerance */
+    printf("Test: Dial tone tolerance.\n");
+    my_dtmf_gen_init(0.0f, -15, 0.0f, -15, DEFAULT_DTMF_TX_ON_TIME, DEFAULT_DTMF_TX_OFF_TIME);
+
+    for (j = -30;  j < -3;  j++)
+    {
+        make_tone_gen_descriptor(&dial_tone_desc, 350, j, 440, j, 1, 0, 0, 0, TRUE);
+        tone_gen_init(&dial_tone, &dial_tone_desc);
+        for (i = 0;  i < 10;  i++)
+        {
+            len = my_dtmf_generate(amp, ALL_POSSIBLE_DIGITS);
+            tone_gen(&dial_tone, amp2, len);
+
+            for (sample = 0;  sample < len;  sample++)
+                amp[sample] = saturate(amp[sample] + amp2[sample]);
+            codec_munge(munge, amp, len);
+            dtmf_rx(&dtmf_state, amp, len);
+
+            if (dtmf_rx_get(&dtmf_state, buf, 128) != strlen(ALL_POSSIBLE_DIGITS))
+                break;
+        }
+        if (i != 10)
+            break;
+    }
+    printf("    Acceptable signal to dial tone ratio is %ddB\n", -15 - j);
+    if ((use_dialtone_filter  &&  (-15 - j) > -12)
+        ||
+        (!use_dialtone_filter  &&  (-15 - j) > 10)) 
+    {
+        printf("    Failed\n");
+        exit(2); 
+    }
+    printf("    Passed\n");
+}
+/*- End of function --------------------------------------------------------*/
+
+static void callback_function_tests(void)
+{
+    int i;
+    int j;
+    int len;
+    int sample;
+    dtmf_rx_state_t dtmf_state;
+
+    /* Test the callback mode for delivering detected digits */
+    printf("Test: Callback digit delivery mode.\n");
+    callback_hit = FALSE;
+    callback_ok = TRUE;
+    callback_roll = 0;
+    dtmf_rx_init(&dtmf_state, digit_delivery, (void *) 0x12345678);
+    if (use_dialtone_filter)
+        dtmf_rx_parms(&dtmf_state, TRUE, -1, -1, -99);
+    my_dtmf_gen_init(0.0f, DEFAULT_DTMF_TX_LEVEL, 0.0f, DEFAULT_DTMF_TX_LEVEL, DEFAULT_DTMF_TX_ON_TIME, DEFAULT_DTMF_TX_OFF_TIME);
+    for (i = 1;  i < 10;  i++)
+    {
+        len = 0;
+        for (j = 0;  j < i;  j++)
+            len += my_dtmf_generate(amp + len, ALL_POSSIBLE_DIGITS);
+        dtmf_rx(&dtmf_state, amp, len);
+        if (!callback_hit  ||  !callback_ok)
+            break;
+    }
+    if (!callback_hit  ||  !callback_ok)
+    {
+        printf("    Failed\n");
+        exit(2);
+    }
+    printf("    Passed\n");
+
+    /* Test the realtime callback mode for reporting detected digits */
+    printf("Test: Realtime callback digit delivery mode.\n");
+    callback_hit = FALSE;
+    callback_ok = TRUE;
+    callback_roll = 0;
+    dtmf_rx_init(&dtmf_state, NULL, NULL);
+    dtmf_rx_set_realtime_callback(&dtmf_state, digit_status, (void *) 0x12345678);
+    if (use_dialtone_filter)
+        dtmf_rx_parms(&dtmf_state, TRUE, -1, -1, -99);
+    my_dtmf_gen_init(0.0f, DEFAULT_DTMF_TX_LEVEL, 0.0f, DEFAULT_DTMF_TX_LEVEL, DEFAULT_DTMF_TX_ON_TIME, DEFAULT_DTMF_TX_OFF_TIME);
+    step = 0;
+    for (i = 1;  i < 10;  i++)
+    {
+        len = 0;
+        for (j = 0;  j < i;  j++)
+            len += my_dtmf_generate(amp + len, ALL_POSSIBLE_DIGITS);
+        for (sample = 0, j = SAMPLES_PER_CHUNK;  sample < len;  sample += SAMPLES_PER_CHUNK, j = ((len - sample) >= SAMPLES_PER_CHUNK)  ?  SAMPLES_PER_CHUNK  :  (len - sample))
+        {
+            dtmf_rx(&dtmf_state, &amp[sample], j);
+            if (!callback_ok)
+                break;
+            step += j;
+        }
+        if (!callback_hit  ||  !callback_ok)
+            break;
+    }
+    if (!callback_hit  ||  !callback_ok)
+    {
+        printf("    Failed\n");
+        exit(2);
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static void decode_test(const char *test_file)
+{
+    int16_t amp[SAMPLES_PER_CHUNK];
+    AFfilehandle inhandle;
+    dtmf_rx_state_t dtmf_state;
+    char buf[128 + 1];
+    int actual;
+    int samples;
+    int total;
+
+    dtmf_rx_init(&dtmf_state, NULL, NULL);
+    if (use_dialtone_filter)
+        dtmf_rx_parms(&dtmf_state, TRUE, -1, -1, -99);
+
+    /* We will decode the audio from a wave file. */
+    
+    if ((inhandle = afOpenFile_telephony_read(decode_test_file, 1)) == AF_NULL_FILEHANDLE)
+    {
+        fprintf(stderr, "    Cannot open wave file '%s'\n", decode_test_file);
+        exit(2);
+    }
+    
+    total = 0;
+    while ((samples = afReadFrames(inhandle, AF_DEFAULT_TRACK, amp, SAMPLES_PER_CHUNK)) > 0)
+    {
+        codec_munge(munge, amp, samples);
+        dtmf_rx(&dtmf_state, amp, samples);
+        //printf("Status 0x%X\n", dtmf_rx_status(&dtmf_state));
+        if ((actual = dtmf_rx_get(&dtmf_state, buf, 128)) > 0)
+            printf("Received '%s'\n", buf);
+        total += actual;
+    }
+    printf("%d digits received\n", total);
+}
+/*- End of function --------------------------------------------------------*/
+
+int main(int argc, char *argv[])
+{
+    int duration;
+    time_t now;
+    int channel_codec;
+    int opt;
+
+    use_dialtone_filter = FALSE;
+    channel_codec = MUNGE_CODEC_NONE;
+    decode_test_file = NULL;
+    while ((opt = getopt(argc, argv, "c:d:f")) != -1)
+    {
+        switch (opt)
+        {
+        case 'c':
+            channel_codec = atoi(optarg);
+            break;
+        case 'd':
+            decode_test_file = optarg;
+            break;
+        case 'f':
+            use_dialtone_filter = TRUE;
+            break;
+        default:
+            //usage();
+            exit(2);
+            break;
+        }
+    }
+    munge = codec_munge_init(channel_codec, 0);
+
+    if (decode_test_file)
+    {
+        decode_test(decode_test_file);
+    }
+    else
+    {
+        time(&now);
+        mitel_cm7291_side_1_tests();
+        mitel_cm7291_side_2_and_bellcore_tests();
+        dial_tone_tolerance_tests();
+        callback_function_tests();
+        printf("    Passed\n");
+        duration = time(NULL) - now;
+        printf("Tests passed in %ds\n", duration);
+    }
+
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/tests/dtmf_tx_tests.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/tests/dtmf_tx_tests.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,192 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * dtmf_tx_tests.c - Test the DTMF generator.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2003 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: dtmf_tx_tests.c,v 1.20 2008/08/16 15:24:15 steveu Exp $
+ */
+
+/*! \file */
+
+/*! \page dtmf_tx_tests_page DTMF generation tests
+\section dtmf_tx_tests_page_sec_1 What does it do?
+???.
+
+\section dtmf_tx_tests_page_sec_2 How does it work?
+???.
+*/
+
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <time.h>
+#include <audiofile.h>
+
+#include "spandsp.h"
+#include "spandsp-sim.h"
+
+#define OUTPUT_FILE_NAME    "dtmf.wav"
+
+int main(int argc, char *argv[])
+{
+    dtmf_tx_state_t gen;
+    int16_t amp[16384];
+    int len;
+    AFfilehandle outhandle;
+    int outframes;
+    int add_digits;
+
+    if ((outhandle = afOpenFile_telephony_write(OUTPUT_FILE_NAME, 1)) == AF_NULL_FILEHANDLE)
+    {
+        fprintf(stderr, "    Cannot open wave file '%s'\n", OUTPUT_FILE_NAME);
+        exit(2);
+    }
+
+    dtmf_tx_init(&gen);
+    len = dtmf_tx(&gen, amp, 16384);
+    printf("Generated %d samples\n", len);
+    outframes = afWriteFrames(outhandle, AF_DEFAULT_TRACK, amp, len);
+    if (dtmf_tx_put(&gen, "123", -1))
+        printf("Ooops\n");
+    len = dtmf_tx(&gen, amp, 16384);
+    printf("Generated %d samples\n", len);
+    outframes = afWriteFrames(outhandle, AF_DEFAULT_TRACK, amp, len);
+    if (dtmf_tx_put(&gen, "456", -1))
+        printf("Ooops\n");
+    len = dtmf_tx(&gen, amp, 160);
+    printf("Generated %d samples\n", len);
+    outframes = afWriteFrames(outhandle, AF_DEFAULT_TRACK, amp, len);
+    if (dtmf_tx_put(&gen, "789", -1))
+        printf("Ooops\n");
+    len = dtmf_tx(&gen, amp, 160);
+    printf("Generated %d samples\n", len);
+    outframes = afWriteFrames(outhandle, AF_DEFAULT_TRACK, amp, len);
+    if (dtmf_tx_put(&gen, "*#", -1))
+        printf("Ooops\n");
+    len = dtmf_tx(&gen, amp, 160);
+    printf("Generated %d samples\n", len);
+    outframes = afWriteFrames(outhandle, AF_DEFAULT_TRACK, amp, len);
+    add_digits = 1;
+    do
+    {
+        len = dtmf_tx(&gen, amp, 160);
+        printf("Generated %d samples\n", len);
+        if (len > 0)
+        {
+            outframes = afWriteFrames(outhandle, AF_DEFAULT_TRACK, amp, len);
+        }
+        if (add_digits)
+        {
+            if (dtmf_tx_put(&gen, "1234567890", -1))
+            {
+                printf("Digit buffer full\n");
+                add_digits = 0;
+            }
+        }
+    }
+    while (len > 0);
+
+    dtmf_tx_init(&gen);
+    len = dtmf_tx(&gen, amp, 16384);
+    printf("Generated %d samples\n", len);
+    outframes = afWriteFrames(outhandle, AF_DEFAULT_TRACK, amp, len);
+    if (dtmf_tx_put(&gen, "123", -1))
+        printf("Ooops\n");
+    len = dtmf_tx(&gen, amp, 16384);
+    printf("Generated %d samples\n", len);
+    outframes = afWriteFrames(outhandle, AF_DEFAULT_TRACK, amp, len);
+    if (dtmf_tx_put(&gen, "456", -1))
+        printf("Ooops\n");
+    len = dtmf_tx(&gen, amp, 160);
+    printf("Generated %d samples\n", len);
+    outframes = afWriteFrames(outhandle, AF_DEFAULT_TRACK, amp, len);
+    if (dtmf_tx_put(&gen, "789", -1))
+        printf("Ooops\n");
+    len = dtmf_tx(&gen, amp, 160);
+    printf("Generated %d samples\n", len);
+    outframes = afWriteFrames(outhandle, AF_DEFAULT_TRACK, amp, len);
+    if (dtmf_tx_put(&gen, "0*#", -1))
+        printf("Ooops\n");
+    len = dtmf_tx(&gen, amp, 160);
+    printf("Generated %d samples\n", len);
+    outframes = afWriteFrames(outhandle, AF_DEFAULT_TRACK, amp, len);
+    if (dtmf_tx_put(&gen, "ABCD", -1))
+        printf("Ooops\n");
+    len = dtmf_tx(&gen, amp, 160);
+    printf("Generated %d samples\n", len);
+    outframes = afWriteFrames(outhandle, AF_DEFAULT_TRACK, amp, len);
+
+    /* Try modifying the level and length of the digits */
+    printf("Try different levels and timing\n");
+    dtmf_tx_set_level(&gen, -20, 5);
+    dtmf_tx_set_timing(&gen, 100, 200);
+    if (dtmf_tx_put(&gen, "123", -1))
+        printf("Ooops\n");
+    do
+    {
+        len = dtmf_tx(&gen, amp, 160);
+        printf("Generated %d samples\n", len);
+        if (len > 0)
+            outframes = afWriteFrames(outhandle, AF_DEFAULT_TRACK, amp, len);
+    }
+    while (len > 0);
+    printf("Restore normal levels and timing\n");
+    dtmf_tx_set_level(&gen, -10, 0);
+    dtmf_tx_set_timing(&gen, 50, 55);
+    if (dtmf_tx_put(&gen, "A", -1))
+        printf("Ooops\n");
+
+    add_digits = TRUE;
+    do
+    {
+        len = dtmf_tx(&gen, amp, 160);
+        printf("Generated %d samples\n", len);
+        if (len > 0)
+        {
+            outframes = afWriteFrames(outhandle, AF_DEFAULT_TRACK, amp, len);
+        }
+        if (add_digits)
+        {
+            if (dtmf_tx_put(&gen, "1234567890", -1))
+            {
+                printf("Digit buffer full\n");
+                add_digits = FALSE;
+            }
+        }
+    }
+    while (len > 0);
+
+    if (afCloseFile(outhandle) != 0)
+    {
+        fprintf(stderr, "    Cannot close wave file '%s'\n", OUTPUT_FILE_NAME);
+        exit (2);
+    }
+
+    return  0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/tests/echo_monitor.cpp
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/tests/echo_monitor.cpp	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,451 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * echo_monitor.cpp - Display echo canceller status, using the FLTK toolkit.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2004 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: echo_monitor.cpp,v 1.12 2008/05/27 15:08:21 steveu Exp $
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#if defined(HAVE_FL_FL_H)  &&  defined(HAVE_FL_FL_CARTESIAN_H)
+
+#define __STDC_LIMIT_MACROS
+
+#include <inttypes.h>
+#include <stdio.h>
+#include <math.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/select.h>
+#if defined(HAVE_FFTW3_H)
+#include <fftw3.h>
+#else
+#include <fftw.h>
+#endif
+
+#include <FL/Fl.H>
+#include <FL/Fl_Overlay_Window.H>
+#include <FL/Fl_Light_Button.H>
+#include <FL/Fl_Cartesian.H>
+#include <FL/Fl_Audio_Meter.H>
+#include <FL/fl_draw.H>
+
+#include "spandsp.h"
+#include "echo_monitor.h"
+
+Fl_Double_Window *w;
+
+Fl_Audio_Meter *audio_meter;
+
+Fl_Group *c_spec;
+Fl_Group *c_right;
+Fl_Group *c_can;
+Fl_Group *c_line_model;
+
+Ca_Canvas *canvas_spec;
+Ca_X_Axis *spec_freq;
+Ca_Y_Axis *spec_amp;
+Ca_Line *spec_re = NULL;
+double spec_re_plot[2*512];
+
+Ca_Canvas *canvas_can;
+Ca_X_Axis *can_x;
+Ca_Y_Axis *can_y;
+Ca_Line *can_re = NULL;
+double can_re_plot[512];
+
+Ca_Canvas *canvas_line_model;
+Ca_X_Axis *line_model_x;
+Ca_Y_Axis *line_model_y;
+Ca_Line *line_model_re = NULL;
+double line_model_re_plot[512];
+
+static int skip = 0;
+
+int in_ptr;
+#if defined(HAVE_FFTW3_H)
+double in[1024][2];
+double out[1024][2];
+#else
+fftw_complex in[1024];
+fftw_complex out[1024];
+#endif
+fftw_plan p;
+
+int echo_can_monitor_can_update(const int16_t *coeffs, int len)
+{
+    int i;
+    float min;
+    float max;
+
+    if (can_re)
+        delete can_re;
+
+    canvas_can->current(canvas_can);
+    i = 0;
+    min = coeffs[i];
+    max = coeffs[i];
+    for (i = 0;  i < len;  i++)
+    {
+        can_re_plot[2*i] = i;
+        can_re_plot[2*i + 1] = coeffs[i];
+        if (min > coeffs[i])
+            min = coeffs[i];
+        if (max < coeffs[i])
+            max = coeffs[i];
+    }
+    can_y->maximum((max == min)  ?  max + 0.2  :  max);
+    can_y->minimum(min);
+    can_re = new Ca_Line(len, can_re_plot, 0, 0, FL_BLUE, CA_NO_POINT);
+    if (++skip >= 100)
+    {
+        skip = 0;
+        Fl::check();
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int echo_can_monitor_line_model_update(const int32_t *coeffs, int len)
+{
+    int i;
+    float min;
+    float max;
+
+    if (line_model_re)
+        delete line_model_re;
+
+    canvas_line_model->current(canvas_line_model);
+    i = 0;
+    min = coeffs[i];
+    max = coeffs[i];
+    for (i = 0;  i < len;  i++)
+    {
+        line_model_re_plot[2*i] = i;
+        line_model_re_plot[2*i + 1] = coeffs[i];
+        if (min > coeffs[i])
+            min = coeffs[i];
+        if (max < coeffs[i])
+            max = coeffs[i];
+    }
+    line_model_y->maximum((max == min)  ?  max + 0.2  :  max);
+    line_model_y->minimum(min);
+    line_model_re = new Ca_Line(len, line_model_re_plot, 0, 0, FL_BLUE, CA_NO_POINT);
+    if (++skip >= 100)
+    {
+        skip = 0;
+        Fl::check();
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int echo_can_monitor_line_spectrum_update(const int16_t amp[], int len)
+{
+    int i;
+    int x;
+
+    for (i = 0;  i < len;  i++)
+        audio_meter->sample(amp[i]/32768.0);
+
+    if (in_ptr + len < 512)
+    {
+        /* Just add this fragment to the buffer. */
+        for (i = 0;  i < len;  i++)
+#if defined(HAVE_FFTW3_H)
+            in[in_ptr + i][0] = amp[i];
+#else
+            in[in_ptr + i].re = amp[i];
+#endif
+        in_ptr += len;
+        return 0;
+    }
+    if (len >= 512)
+    {
+        /* We have enough for a whole block. Use the last 512 samples
+           we have. */
+        x = len - 512;
+        for (i = 0;  i < 512;  i++)
+#if defined(HAVE_FFTW3_H)
+            in[i][0] = amp[x + i];
+#else
+            in[i].re = amp[x + i];
+#endif
+    }
+    else
+    {
+        /* We want the last 512 samples. */
+        x = 512 - len;
+        for (i = 0;  i < x;  i++)
+#if defined(HAVE_FFTW3_H)
+            in[i][0] = in[in_ptr - x + i][0];
+#else
+            in[i].re = in[in_ptr - x + i].re;
+#endif
+        for (i = x;  i < 512;  i++)
+#if defined(HAVE_FFTW3_H)
+            in[i][0] = amp[i - x];
+#else
+            in[i].re = amp[i - x];
+#endif
+    }
+    in_ptr = 0;
+#if defined(HAVE_FFTW3_H)    
+    fftw_execute(p);
+#else
+    fftw_one(p, in, out);
+#endif
+    if (spec_re)
+        delete spec_re;
+    canvas_spec->current(canvas_spec);
+    for (i = 0;  i < 512;  i++)
+    {
+        spec_re_plot[2*i] = i*4000.0/512.0;
+#if defined(HAVE_FFTW3_H)    
+        spec_re_plot[2*i + 1] = 20.0*log10(sqrt(out[i][0]*out[i][0] + out[i][1]*out[i][1])/(256.0*32768)) + 3.14;
+#else
+        spec_re_plot[2*i + 1] = 20.0*log10(sqrt(out[i].re*out[i].re + out[i].im*out[i].im)/(256.0*32768)) + 3.14;
+#endif
+    }
+    spec_re = new Ca_Line(512, spec_re_plot, 0, 0, FL_BLUE, CA_NO_POINT);
+    Fl::check();
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int start_echo_can_monitor(int len)
+{
+    char buf[132 + 1];
+    float x;
+    float y;
+    int i;
+
+    w = new Fl_Double_Window(850, 400, "Echo canceller monitor");
+
+    c_spec = new Fl_Group(0, 0, 380, 400);
+    c_spec->box(FL_DOWN_BOX);
+    c_spec->align(FL_ALIGN_TOP | FL_ALIGN_INSIDE);
+
+    canvas_spec = new Ca_Canvas(60, 30, 300, 300, "Spectrum");
+    canvas_spec->box(FL_PLASTIC_DOWN_BOX);
+    canvas_spec->color(7);
+    canvas_spec->align(FL_ALIGN_TOP);
+    canvas_spec->border(15);
+
+    spec_freq = new Ca_X_Axis(65, 330, 290, 30, "Freq (Hz)");
+    spec_freq->align(FL_ALIGN_BOTTOM);
+    spec_freq->minimum(0);
+    spec_freq->maximum(4000);
+    spec_freq->label_format("%g");
+    spec_freq->minor_grid_color(fl_gray_ramp(20));
+    spec_freq->major_grid_color(fl_gray_ramp(15));
+    spec_freq->label_grid_color(fl_gray_ramp(10));
+    spec_freq->grid_visible(CA_LABEL_GRID | CA_ALWAYS_VISIBLE);
+    spec_freq->minor_grid_style(FL_DOT);
+    spec_freq->major_step(5);
+    spec_freq->label_step(1);
+    spec_freq->axis_color(FL_BLACK);
+    spec_freq->axis_align(CA_BOTTOM | CA_LINE);
+
+    spec_amp = new Ca_Y_Axis(20, 35, 40, 290, "Amp (dBmO)");
+    spec_amp->align(FL_ALIGN_LEFT);
+    spec_amp->minimum(-80.0);
+    spec_amp->maximum(10.0);
+    spec_amp->minor_grid_color(fl_gray_ramp(20));
+    spec_amp->major_grid_color(fl_gray_ramp(15));
+    spec_amp->label_grid_color(fl_gray_ramp(10));
+    //spec_amp->grid_visible(CA_MINOR_TICK | CA_MAJOR_TICK | CA_LABEL_GRID | CA_ALWAYS_VISIBLE);
+    spec_amp->grid_visible(CA_LABEL_GRID | CA_ALWAYS_VISIBLE);
+    spec_amp->minor_grid_style(FL_DOT);
+    spec_amp->major_step(5);
+    spec_amp->label_step(1);
+    spec_amp->axis_color(FL_BLACK);
+
+    spec_amp->current();
+
+    c_spec->end();
+
+    c_right = new Fl_Group(440, 0, 465, 405);
+
+    c_can = new Fl_Group(380, 0, 415, 200);
+    c_can->box(FL_DOWN_BOX);
+    c_can->align(FL_ALIGN_TOP | FL_ALIGN_INSIDE);
+    c_can->current();
+
+    canvas_can = new Ca_Canvas(460, 35, 300, 100, "Canceller coefficients");
+    canvas_can->box(FL_PLASTIC_DOWN_BOX);
+    canvas_can->color(7);
+    canvas_can->align(FL_ALIGN_TOP);
+    Fl_Group::current()->resizable(canvas_can);
+    canvas_can->border(15);
+
+    can_x = new Ca_X_Axis(465, 135, 290, 30, "Tap");
+    can_x->align(FL_ALIGN_BOTTOM);
+    can_x->minimum(0.0);
+    can_x->maximum((float) len);
+    can_x->label_format("%g");
+    can_x->minor_grid_color(fl_gray_ramp(20));
+    can_x->major_grid_color(fl_gray_ramp(15));
+    can_x->label_grid_color(fl_gray_ramp(10));
+    can_x->grid_visible(CA_LABEL_GRID | CA_ALWAYS_VISIBLE);
+    can_x->minor_grid_style(FL_DOT);
+    can_x->major_step(5);
+    can_x->label_step(1);
+    can_x->axis_align(CA_BOTTOM | CA_LINE);
+    can_x->axis_color(FL_BLACK);
+    can_x->current();
+
+    can_y = new Ca_Y_Axis(420, 40, 40, 90, "Amp");
+    can_y->align(FL_ALIGN_LEFT);
+    can_y->minimum(-0.1);
+    can_y->maximum(0.1);
+    can_y->minor_grid_color(fl_gray_ramp(20));
+    can_y->major_grid_color(fl_gray_ramp(15));
+    can_y->label_grid_color(fl_gray_ramp(10));
+    can_y->grid_visible(CA_LABEL_GRID | CA_ALWAYS_VISIBLE);
+    can_y->minor_grid_style(FL_DOT);
+    can_y->major_step(5);
+    can_y->label_step(1);
+    can_y->axis_color(FL_BLACK);
+    can_y->current();
+
+    c_can->end();
+
+    c_line_model = new Fl_Group(380, 200, 415, 200);
+    c_line_model->box(FL_DOWN_BOX);
+    c_line_model->align(FL_ALIGN_TOP | FL_ALIGN_INSIDE);
+    c_line_model->current();
+
+    canvas_line_model = new Ca_Canvas(460, 235, 300, 100, "Line impulse response model");
+    canvas_line_model->box(FL_PLASTIC_DOWN_BOX);
+    canvas_line_model->color(7);
+    canvas_line_model->align(FL_ALIGN_TOP);
+    Fl_Group::current()->resizable(canvas_line_model);
+    canvas_line_model->border(15);
+
+    line_model_x = new Ca_X_Axis(465, 335, 290, 30, "Tap");
+    line_model_x->align(FL_ALIGN_BOTTOM);
+    line_model_x->minimum(0.0);
+    line_model_x->maximum((float) len);
+    line_model_x->label_format("%g");
+    line_model_x->minor_grid_color(fl_gray_ramp(20));
+    line_model_x->major_grid_color(fl_gray_ramp(15));
+    line_model_x->label_grid_color(fl_gray_ramp(10));
+    line_model_x->grid_visible(CA_LABEL_GRID | CA_ALWAYS_VISIBLE);
+    line_model_x->minor_grid_style(FL_DOT);
+    line_model_x->major_step(5);
+    line_model_x->label_step(1);
+    line_model_x->axis_align(CA_BOTTOM | CA_LINE);
+    line_model_x->axis_color(FL_BLACK);
+    line_model_x->current();
+
+    line_model_y = new Ca_Y_Axis(420, 240, 40, 90, "Amp");
+    line_model_y->align(FL_ALIGN_LEFT);
+    line_model_y->minimum(-0.1);
+    line_model_y->maximum(0.1);
+    line_model_y->minor_grid_color(fl_gray_ramp(20));
+    line_model_y->major_grid_color(fl_gray_ramp(15));
+    line_model_y->label_grid_color(fl_gray_ramp(10));
+    line_model_y->grid_visible(CA_LABEL_GRID | CA_ALWAYS_VISIBLE);
+    line_model_y->minor_grid_style(FL_DOT);
+    line_model_y->major_step(5);
+    line_model_y->label_step(1);
+    line_model_y->axis_color(FL_BLACK);
+    line_model_y->current();
+
+    c_line_model->end();
+
+    audio_meter = new Fl_Audio_Meter(810, 40, 10, 250, "");
+    audio_meter->box(FL_PLASTIC_UP_BOX);
+    audio_meter->type(FL_VERT_AUDIO_METER);
+
+    c_right->end();
+
+    Fl_Group::current()->resizable(c_right);
+    w->end();
+    w->show();
+
+#if defined(HAVE_FFTW3_H)    
+    p = fftw_plan_dft_1d(1024, in, out, FFTW_BACKWARD, FFTW_ESTIMATE);
+    for (i = 0;  i < 1024;  i++)
+    {
+        in[i][0] = 0.0;
+        in[i][1] = 0.0;
+    }
+#else
+    p = fftw_create_plan(1024, FFTW_BACKWARD, FFTW_ESTIMATE);
+    for (i = 0;  i < 1024;  i++)
+    {
+        in[i].re = 0.0;
+        in[i].im = 0.0;
+    }
+#endif
+    in_ptr = 0;
+
+    Fl::check();
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+void echo_can_monitor_wait_to_end(void) 
+{
+    fd_set rfds;
+    int res;
+    struct timeval tv;
+
+    fprintf(stderr, "Processing complete.  Press the <enter> key to end\n");
+    do
+    {
+        usleep(100000);
+        Fl::check();
+        FD_ZERO(&rfds);
+        FD_SET(0, &rfds);
+        tv.tv_usec = 100000;
+        tv.tv_sec = 0;
+        res = select(1, &rfds, NULL, NULL, &tv);
+    }
+    while (res <= 0);
+}
+/*- End of function --------------------------------------------------------*/
+
+void echo_can_monitor_update_display(void) 
+{
+    Fl::check();
+    Fl::check();
+    Fl::check();
+    Fl::check();
+    Fl::check();
+    Fl::check();
+    Fl::check();
+    Fl::check();
+    Fl::check();
+    Fl::check();
+    Fl::check();
+    Fl::check();
+}
+/*- End of function --------------------------------------------------------*/
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/tests/echo_monitor.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/tests/echo_monitor.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,62 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * echo_monitor.h - Display echo canceller status, using the FLTK toolkit.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2004 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: echo_monitor.h,v 1.12 2008/04/26 13:39:16 steveu Exp $
+ */
+
+/*! \page echo_monitor_page Echo canceller performance monitoring
+\section echo_monitor_page_sec_1 What does it do?
+This code controls a GUI window, which provides monitoring of the internal status
+of a time domain echo canceller. It shows, graphically:
+
+    - the spectrum of the received signal.
+    - the line model in use (when a known line model is being used).
+    - the adapted coefficients of the canceller.
+
+\section echo_monitor_page_sec_2 How does it work?
+This code uses the FLTK cross platform GUI toolkit. It works on X11 and Windows platforms.
+In addition to the basic FLTK toolkit, fltk_cartesian is also required.
+*/
+
+#if !defined(_ECHO_MONITOR_H_)
+#define _ECHO_MONITOR_H_
+
+#if defined(__cplusplus)
+extern "C"
+{
+#endif
+
+int start_echo_can_monitor(int len);
+int echo_can_monitor_can_update(const int16_t *coeffs, int len);
+int echo_can_monitor_line_model_update(const int32_t *coeffs, int len);
+int echo_can_monitor_line_spectrum_update(const int16_t amp[], int len);
+void echo_can_monitor_wait_to_end(void);
+void echo_can_monitor_update_display(void);
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/tests/echo_tests.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/tests/echo_tests.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,1739 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * echo_tests.c
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2001 Steve Underwood
+ *
+ * Based on a bit from here, a bit from there, eye of toad,
+ * ear of bat, etc - plus, of course, my own 2 cents.
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: echo_tests.c,v 1.35 2008/08/29 09:28:13 steveu Exp $
+ */
+
+/*! \page echo_can_tests_page Line echo cancellation for voice tests
+
+\section echo_can_tests_page_sec_1 What does it do?
+The echo cancellation tests test the echo cancellor against the G.168 spec. Not
+all the tests in G.168 are fully implemented at this time.
+
+\section echo_can_tests_page_sec_2 How does it work?
+
+\section echo_can_tests_page_sec_2 How do I use it?
+
+*/
+
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
+
+#if defined(HAVE_FL_FL_H)  &&  defined(HAVE_FL_FL_CARTESIAN_H)  &&  defined(HAVE_FL_FL_AUDIO_METER_H)
+#define ENABLE_GUI
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <string.h>
+#include <strings.h>
+#include <assert.h>
+#include <audiofile.h>
+
+#define GEN_CONST
+#include <math.h>
+
+#include "spandsp.h"
+#include "spandsp/g168models.h"
+#include "spandsp-sim.h"
+#if defined(ENABLE_GUI)
+#include "echo_monitor.h"
+#endif
+
+#if !defined(NULL)
+#define NULL (void *) 0
+#endif
+
+#define TEST_EC_TAPS            256
+
+#define RESIDUE_FILE_NAME       "residue_sound.wav"
+
+/*
+    The key signal names, as defined in G.168
+
+          +--------------+       +------------+
+          |              |  Sin  |            |
+Sgen -->--|   Echoey     |--->---|  Echo      |-->-- Sout
+          |              |       |            |
+          |   World      |  Rout |  Canceller |
+     --<--|              |---<---|            |--<-- Rin
+          |              |       |            |
+          +--------------+       +------------+
+
+    Echoey world model. Munge means linear->PCM->linear distortion.
+          +-------------------------+
+          |                         |  Sin
+Sgen -->--|-->munge--->sum-->munge--|--->---
+          |        +-->             |
+          |       FIR               |  Rout
+     --<--|--------+--------munge<--|---<---
+          |                         |
+          +-------------------------+
+*/
+
+typedef struct
+{
+    const char *name;
+    int max;
+    int cur;
+    float gain;
+    AFfilehandle handle;
+    int16_t signal[SAMPLE_RATE];
+} signal_source_t;
+
+/* Level measurement device, specified in G.168 section 6.4.1.2.1 */
+typedef struct
+{
+    int type;
+    fir_float_state_t *fir;
+    float history[35*8];
+    int pos;
+    float factor; 
+    float power;
+    float peak;
+} level_measurement_device_t;
+
+typedef struct
+{
+    int model_no;
+    float erl;
+    fir32_state_t impulse;
+    float gain;
+    int munging_codec;
+} channel_model_state_t;
+
+channel_model_state_t chan_model;
+
+signal_source_t local_css;
+signal_source_t far_css;
+awgn_state_t local_noise_source;
+awgn_state_t far_noise_source;
+
+AFfilehandle residue_handle;
+int16_t residue_sound[SAMPLE_RATE];
+int residue_cur = 0;
+
+level_measurement_device_t *power_meter_1;
+level_measurement_device_t *power_meter_2;
+
+int line_model_no;
+int supp_line_model_no;
+int munger;
+
+level_measurement_device_t *rin_power_meter;    /* Also known as Lrin */
+level_measurement_device_t *rout_power_meter;
+level_measurement_device_t *sin_power_meter;
+level_measurement_device_t *sout_power_meter;   /* Also known as Lret (pre NLP value is known as Lres) */
+level_measurement_device_t *sgen_power_meter;
+
+#define RESULT_CHANNELS 7
+AFfilehandle result_handle;
+int16_t result_sound[SAMPLE_RATE*RESULT_CHANNELS];
+int result_cur;
+
+const char *test_name;
+int quiet;
+int use_gui;
+
+float erl;
+
+/* Dump estimated echo response */
+static void dump_ec_state(echo_can_state_t *ctx)
+{
+    int i;
+    FILE *f;
+
+    if ((f = fopen("echo_tests_state.txt", "wt")) == NULL)
+        return;
+    for (i = 0;  i < TEST_EC_TAPS;  i++)
+        fprintf(f, "%f\n", (float) ctx->fir_taps16[0][i]/(1 << 15));
+    fclose(f);
+}
+/*- End of function --------------------------------------------------------*/
+
+static inline void put_residue(int16_t amp)
+{
+    int outframes;
+
+    residue_sound[residue_cur++] = amp;
+    if (residue_cur >= SAMPLE_RATE)
+    {
+        outframes = afWriteFrames(residue_handle,
+                                  AF_DEFAULT_TRACK,
+                                  residue_sound,
+                                  residue_cur);
+        if (outframes != residue_cur)
+        {
+            fprintf(stderr, "    Error writing residue sound\n");
+            exit(2);
+        }
+        residue_cur = 0;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static void signal_load(signal_source_t *sig, const char *name)
+{
+    sig->handle = afOpenFile_telephony_read(name, 1);
+    sig->name = name;
+    sig->max = afReadFrames(sig->handle, AF_DEFAULT_TRACK, sig->signal, SAMPLE_RATE);
+    if (sig->max < 0)
+    {
+        fprintf(stderr, "    Error reading sound file '%s'\n", sig->name);
+        exit(2);
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static void signal_free(signal_source_t *sig)
+{
+    if (afCloseFile(sig->handle) != 0)
+    {
+        fprintf(stderr, "    Cannot close sound file '%s'\n", sig->name);
+        exit(2);
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static void signal_restart(signal_source_t *sig, float gain)
+{
+    sig->cur = 0;
+    sig->gain = powf(10.0f, gain/20.0f);
+}
+/*- End of function --------------------------------------------------------*/
+
+static int16_t signal_amp(signal_source_t *sig)
+{
+    int16_t tx;
+
+    tx = sig->signal[sig->cur++]*sig->gain;
+    if (sig->cur >= sig->max)
+        sig->cur = 0;
+    return tx;
+}
+/*- End of function --------------------------------------------------------*/
+
+static level_measurement_device_t *level_measurement_device_create(int type)
+{
+    level_measurement_device_t *dev;
+    int i;
+
+    dev = (level_measurement_device_t *) malloc(sizeof(level_measurement_device_t));
+    dev->fir = (fir_float_state_t *) malloc(sizeof(fir_float_state_t));
+    fir_float_create(dev->fir,
+                     level_measurement_bp_coeffs,
+                     sizeof(level_measurement_bp_coeffs)/sizeof(float));
+    for (i = 0;  i < 35*8;  i++)
+        dev->history[i] = 0.0f;
+    dev->pos = 0;
+    dev->factor = expf(-1.0f/((float) SAMPLE_RATE*0.035f));
+    dev->power = 0;
+    dev->type = type;
+    return  dev;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void level_measurement_device_reset(level_measurement_device_t *dev)
+{
+    int i;
+
+    for (i = 0;  i < 35*8;  i++)
+        dev->history[i] = 0.0f;
+    dev->pos = 0;
+    dev->power = 0;
+    dev->peak = 0.0f;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int level_measurement_device_release(level_measurement_device_t *s)
+{
+    fir_float_free(s->fir);
+    free(s->fir);
+    free(s);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static float level_measurement_device_get_peak(level_measurement_device_t *dev)
+{
+    return dev->peak;
+}
+/*- End of function --------------------------------------------------------*/
+
+static float level_measurement_device_reset_peak(level_measurement_device_t *dev)
+{
+    float power;
+
+    power = dev->peak;
+    dev->peak = -99.0f;
+    return power;
+}
+/*- End of function --------------------------------------------------------*/
+
+static float level_measurement_device(level_measurement_device_t *dev, int16_t amp)
+{
+    float signal;
+    float power;
+
+    /* Level measurement device(s), specified in G.168 section 6.4.1.2.1 and 6.4.1.2.2 */
+    signal = fir_float(dev->fir, amp);
+    signal *= signal;
+    if (dev->type == 0)
+    {
+        /* Level measurement device, specified in G.168 section 6.4.1.2.1 -
+           level measurement device. This version uses a single pole
+           estimator.*/
+        dev->power = dev->power*dev->factor + signal*(1.0f - dev->factor);
+        signal = sqrtf(dev->power);
+    }
+    else
+    {
+        /* Level measurement device, specified in G.168 section 6.4.1.2.2 -
+           level measurement device for peaks. This version uses a sliding
+           window estimator. */
+        dev->power += (signal - dev->history[dev->pos]);
+        dev->history[dev->pos++] = signal;
+        signal = sqrtf(dev->power/(35.8f*8.0f));
+    }
+    if (signal <= 0.0f)
+        return -99.0f;
+    power = DBM0_MAX_POWER + 20.0f*log10f(signal/32767.0f);
+    if (power > dev->peak)
+        dev->peak = power;
+    return power;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void level_measurements_create(int type)
+{
+    rin_power_meter = level_measurement_device_create(type);
+    rout_power_meter = level_measurement_device_create(type);
+    sin_power_meter = level_measurement_device_create(type);
+    sout_power_meter = level_measurement_device_create(type);
+    sgen_power_meter = level_measurement_device_create(type);
+}
+/*- End of function --------------------------------------------------------*/
+
+static void level_measurements_update(int16_t rin, int16_t sin, int16_t rout, int16_t sout, int16_t sgen)
+{
+    level_measurement_device(rin_power_meter, rin);
+    level_measurement_device(rout_power_meter, rout);
+    level_measurement_device(sin_power_meter, sin);
+    level_measurement_device(sout_power_meter, sout);
+    level_measurement_device(sgen_power_meter, sgen);
+}
+/*- End of function --------------------------------------------------------*/
+
+static void level_measurements_reset_peaks(void)
+{
+    level_measurement_device_reset_peak(rin_power_meter);
+    level_measurement_device_reset_peak(rout_power_meter);
+    level_measurement_device_reset_peak(sin_power_meter);
+    level_measurement_device_reset_peak(sout_power_meter);
+    level_measurement_device_reset_peak(sgen_power_meter);
+}
+/*- End of function --------------------------------------------------------*/
+
+static void print_results(void)
+{
+    if (!quiet)
+        printf("test  model  ERL   time     Max Rin  Max Rout Max Sgen Max Sin  Max Sout\n");
+    printf("%-4s  %-1d      %-5.1f%6.2fs%9.2f%9.2f%9.2f%9.2f%9.2f\n", 
+           test_name,
+           chan_model.model_no,
+           20.0f*log10f(-chan_model.erl), 
+           0.0f, //test_clock,
+           level_measurement_device_get_peak(rin_power_meter),
+           level_measurement_device_get_peak(rout_power_meter),
+           level_measurement_device_get_peak(sgen_power_meter),
+           level_measurement_device_get_peak(sin_power_meter),
+           level_measurement_device_get_peak(sout_power_meter));
+}
+/*- End of function --------------------------------------------------------*/
+
+static int channel_model_create(channel_model_state_t *chan, int model, float erl, int codec)
+{
+    static const int32_t line_model_clear_coeffs[] =
+    {
+        32768
+    };
+    static const int32_t *line_models[] =
+    {
+        line_model_clear_coeffs,
+        line_model_d2_coeffs,
+        line_model_d3_coeffs,
+        line_model_d4_coeffs,
+        line_model_d5_coeffs,
+        line_model_d6_coeffs,
+        line_model_d7_coeffs,
+        line_model_d8_coeffs,
+        line_model_d9_coeffs
+    };
+    static const int line_model_sizes[] =
+    {
+        sizeof(line_model_clear_coeffs)/sizeof(line_model_clear_coeffs[0]),
+        sizeof(line_model_d2_coeffs)/sizeof(line_model_d2_coeffs[0]),
+        sizeof(line_model_d3_coeffs)/sizeof(line_model_d3_coeffs[0]),
+        sizeof(line_model_d4_coeffs)/sizeof(line_model_d4_coeffs[0]),
+        sizeof(line_model_d5_coeffs)/sizeof(line_model_d5_coeffs[0]),
+        sizeof(line_model_d6_coeffs)/sizeof(line_model_d6_coeffs[0]),
+        sizeof(line_model_d7_coeffs)/sizeof(line_model_d7_coeffs[0]),
+        sizeof(line_model_d8_coeffs)/sizeof(line_model_d8_coeffs[0]),
+        sizeof(line_model_d9_coeffs)/sizeof(line_model_d9_coeffs[0])
+    };
+    static const float ki[] = 
+    {
+        3.05e-5f,
+        LINE_MODEL_D2_GAIN,
+        LINE_MODEL_D3_GAIN,
+        LINE_MODEL_D4_GAIN,
+        LINE_MODEL_D5_GAIN,
+        LINE_MODEL_D6_GAIN,
+        LINE_MODEL_D7_GAIN,
+        LINE_MODEL_D8_GAIN,
+        LINE_MODEL_D9_GAIN
+    };
+
+    if (model < 0  ||  model >= (int) (sizeof(line_model_sizes)/sizeof(line_model_sizes[0])))
+        return -1;
+    fir32_create(&chan->impulse, line_models[model], line_model_sizes[model]);
+    chan->gain = 32768.0f*powf(10.0f, erl/20.0f)*ki[model];
+    chan->munging_codec = codec;
+    chan->model_no = model;
+    chan->erl = erl;
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int16_t channel_model(channel_model_state_t *chan, int16_t rout, int16_t sgen)
+{
+    int16_t echo;
+    int16_t sin;
+
+    /* Channel modelling is merely simulating the effects of A-law or u-law distortion
+       and using one of the echo models from G.168. Simulating the codec is very important,
+       as this is usually the limiting factor in how much echo reduction is achieved. */
+
+    /* The far end signal will have been through codec munging. */
+    switch (chan->munging_codec)
+    {
+    case G711_ALAW:
+        sgen = alaw_to_linear(linear_to_alaw(sgen));
+        break;
+    case G711_ULAW:
+        sgen = ulaw_to_linear(linear_to_ulaw(sgen));
+        break;
+    }
+
+    /* The local tx signal will usually have gone through codec munging before
+       it reached the line's analogue area, where the echo occurs. */
+    switch (chan->munging_codec)
+    {
+    case G711_ALAW:
+        rout = alaw_to_linear(linear_to_alaw(rout));
+        break;
+    case G711_ULAW:
+        rout = ulaw_to_linear(linear_to_ulaw(rout));
+        break;
+    }
+    /* Now we need to model the echo. We only model a single analogue segment, as per
+       the G.168 spec. However, there will generally be near end and far end analogue/echoey
+       segments in the real world, unless an end is purely digital. */
+    echo = fir32(&chan->impulse, rout*chan->gain);
+    sin = saturate(echo + sgen);
+
+    /* This mixed echo and far end signal will have been through codec munging
+       when it came back into the digital network. */
+    switch (chan->munging_codec)
+    {
+    case G711_ALAW:
+        sin = alaw_to_linear(linear_to_alaw(sin));
+        break;
+    case G711_ULAW:
+        sin = ulaw_to_linear(linear_to_ulaw(sin));
+        break;
+    }
+    return sin;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void write_log_files(int16_t rout, int16_t sin)
+{
+#if 0
+    fprintf(flevel, "%f\t%f\t%f\t%f\n", LRin, LSin, LSout, LSgen);
+    fprintf(fdump, "%d %d %d", ctx->tx, ctx->rx, ctx->clean);
+    fprintf(fdump,
+            " %d %d %d %d %d %d %d %d %d %d\n",
+            ctx->clean_nlp,
+            ctx->Ltx, 
+            ctx->Lrx,
+            ctx->Lclean, 
+            (ctx->nonupdate_dwell > 0),
+            ctx->adapt,
+            ctx->Lclean_bg,
+            ctx->Pstates, 
+            ctx->Lbgn_upper,
+            ctx->Lbgn);
+#endif
+}
+/*- End of function --------------------------------------------------------*/
+
+static int16_t silence(void)
+{
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int16_t local_css_signal(void)
+{
+    return signal_amp(&local_css);
+}
+/*- End of function --------------------------------------------------------*/
+
+static int16_t far_css_signal(void)
+{
+    return signal_amp(&far_css);
+}
+/*- End of function --------------------------------------------------------*/
+
+static int16_t local_noise_signal(void)
+{
+    return awgn(&local_noise_source);
+}
+/*- End of function --------------------------------------------------------*/
+
+static int16_t far_noise_signal(void)
+{
+    return awgn(&far_noise_source);
+}
+/*- End of function --------------------------------------------------------*/
+
+static int16_t local_hoth_noise_signal(void)
+{
+    static float hoth_noise = 0.0;
+
+    hoth_noise = hoth_noise*0.625 + awgn(&local_noise_source)*0.375;
+    return (int16_t) hoth_noise;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int16_t far_hoth_noise_signal(void)
+{
+    static float hoth_noise = 0.0;
+
+    hoth_noise = hoth_noise*0.625 + awgn(&far_noise_source)*0.375;
+    return (int16_t) hoth_noise;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void run_test(echo_can_state_t *ctx, int16_t (*tx_source)(void), int16_t (*rx_source)(void), int period)
+{
+    int i;
+    int16_t rin;
+    int16_t rout;
+    int16_t sin;
+    int16_t sout;
+    int16_t sgen;
+    int outframes;
+
+    for (i = 0;  i < period*SAMPLE_RATE/1000;  i++)
+    {
+        rin = tx_source();
+        sgen = rx_source();
+
+        rout = echo_can_hpf_tx(ctx, rin);
+        sin = channel_model(&chan_model, rout, sgen);
+        sout = echo_can_update(ctx, rout, sin);
+
+        level_measurements_update(rin, sin, rout, sout, sgen);
+        //residue = 100.0f*pp1/pp2;
+        //put_residue(residue);
+
+        //put_residue(clean - rx);
+#if defined(ENABLE_GUI)
+        if (use_gui)
+            echo_can_monitor_can_update(ctx->fir_taps16[0], TEST_EC_TAPS);
+#endif
+        result_sound[result_cur++] = rin;
+        result_sound[result_cur++] = sgen;
+        result_sound[result_cur++] = sin;
+        result_sound[result_cur++] = rout;
+        result_sound[result_cur++] = sout;
+        result_sound[result_cur++] = sout - sgen;
+        result_sound[result_cur++] = 0; // TODO: insert the EC's internal status here
+        if (result_cur >= RESULT_CHANNELS*SAMPLE_RATE)
+        {
+            outframes = afWriteFrames(result_handle,
+                                      AF_DEFAULT_TRACK,
+                                      result_sound,
+                                      result_cur/RESULT_CHANNELS);
+            if (outframes != result_cur/RESULT_CHANNELS)
+            {
+                fprintf(stderr, "    Error writing result sound\n");
+                exit(2);
+            }
+            result_cur = 0;
+        }
+    }
+#if defined(ENABLE_GUI)
+    if (use_gui)
+        echo_can_monitor_can_update(ctx->fir_taps16[0], TEST_EC_TAPS);
+#endif
+    if (result_cur >= 0)
+    {
+        outframes = afWriteFrames(result_handle,
+                                  AF_DEFAULT_TRACK,
+                                  result_sound,
+                                  result_cur/RESULT_CHANNELS);
+        if (outframes != result_cur/RESULT_CHANNELS)
+        {
+            fprintf(stderr, "    Error writing result sound\n");
+            exit(2);
+        }
+        result_cur = 0;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static void print_test_title(const char *title)
+{
+    if (quiet == FALSE) 
+        printf(title);
+}
+/*- End of function --------------------------------------------------------*/
+
+static int perform_test_sanity(void)
+{
+    echo_can_state_t *ctx;
+    int i;
+    int16_t rx;
+    int16_t tx;
+    int16_t clean;
+    int far_tx;
+    int16_t far_sound[SAMPLE_RATE];
+    int16_t result_sound[64000];
+    int result_cur;
+    int outframes;
+    int local_cur;
+    int far_cur;
+    //int32_t coeffs[200][128];
+    //int coeff_index;
+
+    print_test_title("Performing basic sanity test\n");
+    ctx = echo_can_create(TEST_EC_TAPS, 0);
+
+    local_cur = 0;
+    far_cur = 0;
+    result_cur = 0;
+
+    echo_can_flush(ctx);
+    /* Converge the canceller */
+    signal_restart(&local_css, 0.0f);
+    run_test(ctx, silence, silence, 200);
+    run_test(ctx, local_css_signal, silence, 5000);
+    echo_can_adaption_mode(ctx, ECHO_CAN_USE_ADAPTION | ECHO_CAN_USE_NLP | ECHO_CAN_USE_CNG);
+    run_test(ctx, local_css_signal, silence, 5000);
+    echo_can_adaption_mode(ctx, ECHO_CAN_USE_ADAPTION);
+    
+    for (i = 0;  i < SAMPLE_RATE*10;  i++)
+    {
+        tx = local_css_signal();
+#if 0
+        if ((i/10000)%10 == 9)
+        {
+            /* Inject a burst of far sound */
+            if (far_cur >= far_max)
+            {
+                far_max = afReadFrames(farhandle, AF_DEFAULT_TRACK, far_sound, SAMPLE_RATE);
+                if (far_max < 0)
+                {
+                    fprintf(stderr, "    Error reading far sound\n");
+                    exit(2);
+                }
+                if (far_max == 0)
+                    break;
+                far_cur = 0;
+            }
+            far_tx = far_sound[far_cur++];
+        }
+        else
+        {
+            far_tx = 0;
+        }
+#else
+        far_sound[0] = 0;
+        far_tx = 0;
+#endif
+        rx = channel_model(&chan_model, tx, far_tx);
+        //rx += awgn(&far_noise_source);
+        //tx += awgn(&far_noise_source);
+        clean = echo_can_update(ctx, tx, rx);
+
+#if defined(XYZZY)
+        if (i%SAMPLE_RATE == 0)
+        {
+            if (coeff_index < 200)
+            {
+                for (j = 0;  j < ctx->taps;  j++)
+                    coeffs[coeff_index][j] = ctx->fir_taps32[j];
+                coeff_index++;
+            }
+        }
+#endif
+        result_sound[result_cur++] = tx;
+        result_sound[result_cur++] = rx;
+        result_sound[result_cur++] = clean - far_tx;
+        //result_sound[result_cur++] = ctx->tx_power[2];
+        //result_sound[result_cur++] = ctx->tx_power[1];
+        ////result_sound[result_cur++] = (ctx->tx_power[1] > 64)  ?  SAMPLE_RATE  :  -SAMPLE_RATE;
+        //result_sound[result_cur++] = ctx->tap_set*SAMPLE_RATE;
+        //result_sound[result_cur++] = (ctx->nonupdate_dwell > 0)  ?  SAMPLE_RATE  :  -SAMPLE_RATE;
+        //result_sound[result_cur++] = ctx->latest_correction >> 8;
+        //result_sound[result_cur++] = level_measurement_device(tx)/(16.0*65536.0);
+        //result_sound[result_cur++] = level_measurement_device(tx)/4096.0;
+        ////result_sound[result_cur++] = (ctx->tx_power[1] > ctx->rx_power[0])  ?  SAMPLE_RATE  :  -SAMPLE_RATE;
+        //result_sound[result_cur++] = (ctx->tx_power[1] > ctx->rx_power[0])  ?  SAMPLE_RATE  :  -SAMPLE_RATE;
+        //result_sound[result_cur++] = (ctx->narrowband_score)*5; //  ?  SAMPLE_RATE  :  -SAMPLE_RATE;
+        //result_sound[result_cur++] = ctx->tap_rotate_counter*10;
+        ////result_sound[result_cur++] = ctx->vad;
+        
+        put_residue(clean - far_tx);
+        if (result_cur >= RESULT_CHANNELS*SAMPLE_RATE)
+        {
+            outframes = afWriteFrames(result_handle,
+                                      AF_DEFAULT_TRACK,
+                                      result_sound,
+                                      result_cur/RESULT_CHANNELS);
+            if (outframes != result_cur/RESULT_CHANNELS)
+            {
+                fprintf(stderr, "    Error writing result sound\n");
+                exit(2);
+            }
+            result_cur = 0;
+        }
+    }
+    if (result_cur > 0)
+    {
+        outframes = afWriteFrames(result_handle,
+                                  AF_DEFAULT_TRACK,
+                                  result_sound,
+                                  result_cur/RESULT_CHANNELS);
+        if (outframes != result_cur/RESULT_CHANNELS)
+        {
+            fprintf(stderr, "    Error writing result sound\n");
+            exit(2);
+        }
+    }
+#if defined(XYZZY)
+    for (j = 0;  j < ctx->taps;  j++)
+    {
+        for (i = 0;  i < coeff_index;  i++)
+            fprintf(stderr, "%d ", coeffs[i][j]);
+        fprintf(stderr, "\n");
+    }
+#endif
+
+    echo_can_free(ctx);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int perform_test_2a(void)
+{
+    echo_can_state_t *ctx;
+
+    /* Test 2 - Convergence and steady state residual and returned echo level test */
+    /* Test 2A - Convergence and reconvergence test with NLP enabled */
+    print_test_title("Performing test 2A - Convergence and reconvergence test with NLP enabled\n");
+    ctx = echo_can_create(TEST_EC_TAPS, 0);
+
+    echo_can_flush(ctx);
+    echo_can_adaption_mode(ctx, ECHO_CAN_USE_ADAPTION | ECHO_CAN_USE_NLP);
+
+    /* Test 2A (a) - Convergence test with NLP enabled */
+
+    /* Converge the canceller. */
+    run_test(ctx, silence, silence, 200);
+    signal_restart(&local_css, 0.0f);
+    run_test(ctx, local_css_signal, silence, 1000);
+    level_measurements_reset_peaks();
+    run_test(ctx, local_css_signal, silence, 9000);
+    print_results();
+    if (level_measurement_device_get_peak(sout_power_meter) > -65.0f)
+        printf("Test failed\n");
+    else
+        printf("Test passed\n");
+
+    /* Test 2A (b) - Reconvergence test with NLP enabled */
+
+    /* Make an abrupt change of channel characteristic, to another of the channel echo models. */
+    if (channel_model_create(&chan_model, supp_line_model_no, erl, munger))
+    {
+        fprintf(stderr, "    Failed to create line model\n");
+        exit(2);
+    }
+    signal_restart(&local_css, 0.0f);
+    run_test(ctx, local_css_signal, silence, 1000);
+    level_measurements_reset_peaks();
+    run_test(ctx, local_css_signal, silence, 9000);
+    print_results();
+    if (level_measurement_device_get_peak(sout_power_meter) > -65.0f)
+        printf("Test failed\n");
+    else
+        printf("Test passed\n");
+
+    echo_can_free(ctx);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int perform_test_2b(void)
+{
+    echo_can_state_t *ctx;
+
+    /* Test 2 - Convergence and steady state residual and returned echo level test */
+    /* Test 2B - Convergence and reconverge with NLP disabled */
+    print_test_title("Performing test 2B - Convergence and reconverge with NLP disabled\n");
+    ctx = echo_can_create(TEST_EC_TAPS, 0);
+
+    echo_can_flush(ctx);
+    echo_can_adaption_mode(ctx, ECHO_CAN_USE_ADAPTION);
+    signal_restart(&local_css, 0.0f);
+    
+    /* Test 2B (a) - Convergence test with NLP disabled */
+
+    /* Converge the canceller */
+    run_test(ctx, silence, silence, 200);
+    run_test(ctx, local_css_signal, silence, 1000);
+    level_measurements_reset_peaks();
+    run_test(ctx, local_css_signal, silence, 9000);
+    print_results();
+    level_measurements_reset_peaks();
+    run_test(ctx, local_css_signal, silence, 170000);
+    print_results();
+    if (level_measurement_device_get_peak(sout_power_meter) > -65.0)
+        printf("Test failed\n");
+    else
+        printf("Test passed\n");
+
+    /* Test 2B (b) - Reconvergence test with NLP disabled */
+
+    /* Make an abrupt change of channel characteristic, to another of the channel echo models. */
+    if (channel_model_create(&chan_model, supp_line_model_no, erl, munger))
+    {
+        fprintf(stderr, "    Failed to create line model\n");
+        exit(2);
+    }
+    run_test(ctx, local_css_signal, silence, 1000);
+    level_measurements_reset_peaks();
+    run_test(ctx, local_css_signal, silence, 9000);
+    print_results();
+    level_measurements_reset_peaks();
+    run_test(ctx, local_css_signal, silence, 170000);
+    print_results();
+    if (level_measurement_device_get_peak(sout_power_meter) > -65.0)
+        printf("Test failed\n");
+    else
+        printf("Test passed\n");
+
+    echo_can_free(ctx);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int perform_test_2ca(void)
+{
+    echo_can_state_t *ctx;
+
+    /* Test 2 - Convergence and steady state residual and returned echo level test */
+    /* Test 2C(a) - Convergence with background noise present */
+    print_test_title("Performing test 2C(a) - Convergence with background noise present\n");
+    ctx = echo_can_create(TEST_EC_TAPS, 0);
+    awgn_init_dbm0(&far_noise_source, 7162534, -50.0f);
+    
+    echo_can_flush(ctx);
+    echo_can_adaption_mode(ctx, ECHO_CAN_USE_ADAPTION);
+    
+    /* Converge a canceller */
+    signal_restart(&local_css, 0.0f);
+    run_test(ctx, silence, silence, 200);
+    
+    awgn_init_dbm0(&far_noise_source, 7162534, -40.0f);
+    run_test(ctx, local_css_signal, far_hoth_noise_signal, 5000);
+    
+    /* Now freeze adaption, and measure the echo. */
+    echo_can_adaption_mode(ctx, 0);
+    level_measurements_reset_peaks();
+    run_test(ctx, local_css_signal, silence, 5000);
+    print_results();
+    if (level_measurement_device_get_peak(sout_power_meter) > level_measurement_device_get_peak(sgen_power_meter))
+        printf("Test failed\n");
+    else
+        printf("Test passed\n");
+
+    echo_can_adaption_mode(ctx, ECHO_CAN_USE_ADAPTION);
+    echo_can_free(ctx);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int perform_test_3a(void)
+{
+    echo_can_state_t *ctx;
+
+    /* Test 3 - Performance under double talk conditions */
+    /* Test 3A - Double talk test with low cancelled-end levels */
+    print_test_title("Performing test 3A - Double talk test with low cancelled-end levels\n");
+    ctx = echo_can_create(TEST_EC_TAPS, 0);
+
+    echo_can_flush(ctx);
+    echo_can_adaption_mode(ctx, ECHO_CAN_USE_ADAPTION);
+    
+    run_test(ctx, silence, silence, 200);
+    signal_restart(&local_css, 0.0f);
+    signal_restart(&far_css, -20.0f);
+
+    /* Apply double talk, with a weak far end signal */
+    run_test(ctx, local_css_signal, far_css_signal, 5000);
+    
+    /* Now freeze adaption. */
+    echo_can_adaption_mode(ctx, 0);
+    run_test(ctx, local_css_signal, silence, 500);
+    
+    /* Now measure the echo */
+    level_measurements_reset_peaks();
+    run_test(ctx, local_css_signal, silence, 5000);
+    print_results();
+    if (level_measurement_device_get_peak(sout_power_meter) > level_measurement_device_get_peak(sgen_power_meter))
+        printf("Test failed\n");
+    else
+        printf("Test passed\n");
+
+    echo_can_adaption_mode(ctx, ECHO_CAN_USE_ADAPTION);
+    echo_can_free(ctx);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int perform_test_3ba(void)
+{
+    echo_can_state_t *ctx;
+
+    /* Test 3 - Performance under double talk conditions */
+    /* Test 3B(a) - Double talk stability test with high cancelled-end levels */
+    print_test_title("Performing test 3B(b) - Double talk stability test with high cancelled-end levels\n");
+    ctx = echo_can_create(TEST_EC_TAPS, 0);
+
+    echo_can_flush(ctx);
+    echo_can_adaption_mode(ctx, ECHO_CAN_USE_ADAPTION);
+
+    run_test(ctx, silence, silence, 200);
+    signal_restart(&local_css, 0.0f);
+    signal_restart(&far_css, 0.0f);
+    
+    /* Converge the canceller */
+    run_test(ctx, local_css_signal, silence, 5000);
+    
+    /* Apply double talk */
+    run_test(ctx, local_css_signal, far_css_signal, 5000);
+    
+    /* Now freeze adaption. */
+    echo_can_adaption_mode(ctx, 0);
+    run_test(ctx, local_css_signal, far_css_signal, 1000);
+    
+    /* Turn off the double talk. */
+    run_test(ctx, local_css_signal, silence, 500);
+    
+    /* Now measure the echo */
+    level_measurements_reset_peaks();
+    run_test(ctx, local_css_signal, silence, 5000);
+    print_results();
+
+    echo_can_adaption_mode(ctx, ECHO_CAN_USE_ADAPTION);
+    echo_can_free(ctx);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int perform_test_3bb(void)
+{
+    echo_can_state_t *ctx;
+
+    /* Test 3 - Performance under double talk conditions */
+    /* Test 3B(b) - Double talk stability test with low cancelled-end levels */
+    print_test_title("Performing test 3B(b) - Double talk stability test with low cancelled-end levels\n");
+    ctx = echo_can_create(TEST_EC_TAPS, 0);
+
+    echo_can_flush(ctx);
+    echo_can_adaption_mode(ctx, ECHO_CAN_USE_ADAPTION);
+
+    run_test(ctx, silence, silence, 200);
+    signal_restart(&local_css, 0.0f);
+    signal_restart(&far_css, -15.0f);
+    
+    /* Converge the canceller */
+    run_test(ctx, local_css_signal, silence, 5000);
+    
+    /* Apply double talk */
+    run_test(ctx, local_css_signal, far_css_signal, 5000);
+    
+    /* Now freeze adaption. */
+    echo_can_adaption_mode(ctx, 0);
+    run_test(ctx, local_css_signal, silence, 1000);
+    
+    /* Turn off the double talk. */
+    run_test(ctx, local_css_signal, silence, 500);
+    
+    /* Now measure the echo */
+    level_measurements_reset_peaks();
+    run_test(ctx, local_css_signal, silence, 5000);
+    print_results();
+
+    echo_can_adaption_mode(ctx, ECHO_CAN_USE_ADAPTION);
+    echo_can_free(ctx);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int perform_test_3c(void)
+{
+    echo_can_state_t *ctx;
+
+    /* Test 3 - Performance under double talk conditions */
+    /* Test 3C - Double talk test with simulated conversation */
+    print_test_title("Performing test 3C - Double talk test with simulated conversation\n");
+    ctx = echo_can_create(TEST_EC_TAPS, 0);
+
+    echo_can_flush(ctx);
+    echo_can_adaption_mode(ctx, ECHO_CAN_USE_ADAPTION);
+    run_test(ctx, silence, silence, 200);
+
+    signal_restart(&local_css, 0.0f);
+    signal_restart(&far_css, -15.0f);
+
+    /* Apply double talk */
+    run_test(ctx, local_css_signal, far_css_signal, 5600);
+
+    /* Stop the far signal, and measure the echo. */
+    level_measurements_reset_peaks();
+    run_test(ctx, local_css_signal, silence, 1400);
+    print_results();
+
+    /* Continue measuring the resulting echo */
+    run_test(ctx, local_css_signal, silence, 5000);
+
+    /* Reapply double talk */
+    signal_restart(&far_css, 0.0f);
+    run_test(ctx, local_css_signal, far_css_signal, 5600);
+
+    /* Now the far signal only */
+    run_test(ctx, silence, far_css_signal, 5600);
+
+    echo_can_free(ctx);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int perform_test_4(void)
+{
+    echo_can_state_t *ctx;
+
+    /* Test 4 - Leak rate test */
+    print_test_title("Performing test 4 - Leak rate test\n");
+    ctx = echo_can_create(TEST_EC_TAPS, 0);
+
+    echo_can_flush(ctx);
+    echo_can_adaption_mode(ctx, ECHO_CAN_USE_ADAPTION);
+
+    run_test(ctx, silence, silence, 200);
+
+    /* Converge the canceller */
+    signal_restart(&local_css, 0.0f);
+    run_test(ctx, local_css_signal, silence, 5000);
+
+    /* Put 2 minutes of silence through it */
+    run_test(ctx, silence, silence, 120000);
+
+    /* Now freeze it, and check if it is still well adapted. */
+    echo_can_adaption_mode(ctx, 0);
+    level_measurements_reset_peaks();
+    run_test(ctx, local_css_signal, silence, 5000);
+    print_results();
+
+    echo_can_adaption_mode(ctx, ECHO_CAN_USE_ADAPTION);
+    echo_can_free(ctx);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int perform_test_5(void)
+{
+    echo_can_state_t *ctx;
+    
+    /* Test 5 - Infinite return loss convergence test */
+    print_test_title("Performing test 5 - Infinite return loss convergence test\n");
+    ctx = echo_can_create(TEST_EC_TAPS, 0);
+
+    echo_can_flush(ctx);
+    echo_can_adaption_mode(ctx, ECHO_CAN_USE_ADAPTION);
+
+    /* Converge the canceller */
+    signal_restart(&local_css, 0.0f);
+    run_test(ctx, local_css_signal, silence, 5000);
+
+    /* Now stop echoing, and see we don't do anything unpleasant as the
+       echo path is open looped. */
+    run_test(ctx, local_css_signal, silence, 5000);
+    print_results();
+
+    echo_can_free(ctx);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int perform_test_6(void)
+{
+    echo_can_state_t *ctx;
+    int i;
+    int j;
+    int k;
+    int16_t rx;
+    int16_t tx;
+    int16_t clean;
+    int local_max;
+    tone_gen_descriptor_t tone_desc;
+    tone_gen_state_t tone_state;
+    int16_t local_sound[40000];
+
+    /* Test 6 - Non-divergence on narrow-band signals */
+    print_test_title("Performing test 6 - Non-divergence on narrow-band signals\n");
+    ctx = echo_can_create(TEST_EC_TAPS, 0);
+
+    echo_can_flush(ctx);
+    echo_can_adaption_mode(ctx, ECHO_CAN_USE_ADAPTION);
+
+    /* Converge the canceller */
+    signal_restart(&local_css, 0.0f);
+    run_test(ctx, local_css_signal, silence, 5000);
+
+    /* Now put 5s bursts of a list of tones through the converged canceller, and check
+       that nothing unpleasant happens. */
+    for (k = 0;  tones_6_4_2_7[k][0];  k++)
+    {
+        make_tone_gen_descriptor(&tone_desc,
+                                 tones_6_4_2_7[k][0],
+                                 -11,
+                                 tones_6_4_2_7[k][1],
+                                 -9,
+                                 1,
+                                 0,
+                                 0,
+                                 0,
+                                 1);
+        tone_gen_init(&tone_state, &tone_desc);
+        j = 0;
+        for (i = 0;  i < 5;  i++)
+        {
+            local_max = tone_gen(&tone_state, local_sound, SAMPLE_RATE);
+            for (j = 0;  j < SAMPLE_RATE;  j++)
+            {
+                tx = local_sound[j];
+                rx = channel_model(&chan_model, tx, 0);
+                clean = echo_can_update(ctx, tx, rx);
+                put_residue(clean);
+            }
+#if defined(ENABLE_GUI)
+            if (use_gui)
+            {
+                echo_can_monitor_can_update(ctx->fir_taps16[0], TEST_EC_TAPS);
+                echo_can_monitor_update_display();
+                usleep(100000);
+            }
+#endif
+        }
+    }
+#if defined(ENABLE_GUI)
+    if (use_gui)
+        echo_can_monitor_can_update(ctx->fir_taps16[0], TEST_EC_TAPS);
+#endif
+    echo_can_free(ctx);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int perform_test_7(void)
+{
+    echo_can_state_t *ctx;
+    int i;
+    int j;
+    int16_t rx;
+    int16_t tx;
+    int16_t clean;
+    int local_max;
+    tone_gen_descriptor_t tone_desc;
+    tone_gen_state_t tone_state;
+    int16_t local_sound[40000];
+
+    /* Test 7 - Stability */
+    print_test_title("Performing test 7 - Stability\n");
+    ctx = echo_can_create(TEST_EC_TAPS, 0);
+
+    /* Put tones through an unconverged canceller, and check nothing unpleasant
+       happens. */
+    echo_can_flush(ctx);
+    echo_can_adaption_mode(ctx, ECHO_CAN_USE_ADAPTION);
+    make_tone_gen_descriptor(&tone_desc,
+                             tones_6_4_2_7[0][0],
+                             -11,
+                             tones_6_4_2_7[0][1],
+                             -9,
+                             1,
+                             0,
+                             0,
+                             0,
+                             1);
+    tone_gen_init(&tone_state, &tone_desc);
+    j = 0;
+    for (i = 0;  i < 120;  i++)
+    {
+        local_max = tone_gen(&tone_state, local_sound, SAMPLE_RATE);
+        for (j = 0;  j < SAMPLE_RATE;  j++)
+        {
+            tx = local_sound[j];
+            rx = channel_model(&chan_model, tx, 0);
+            clean = echo_can_update(ctx, tx, rx);
+            put_residue(clean);
+        }
+#if defined(ENABLE_GUI)
+        if (use_gui)
+        {
+            echo_can_monitor_can_update(ctx->fir_taps16[0], TEST_EC_TAPS);
+            echo_can_monitor_update_display();
+            usleep(100000);
+        }
+#endif
+    }
+#if defined(ENABLE_GUI)
+    if (use_gui)
+        echo_can_monitor_can_update(ctx->fir_taps16[0], TEST_EC_TAPS);
+#endif
+    echo_can_free(ctx);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int perform_test_8(void)
+{
+    echo_can_state_t *ctx;
+
+    /* Test 8 - Non-convergence on No 5, 6, and 7 in-band signalling */
+    print_test_title("Performing test 8 - Non-convergence on No 5, 6, and 7 in-band signalling\n");
+    ctx = echo_can_create(TEST_EC_TAPS, 0);
+
+    fprintf(stderr, "Test 8 not yet implemented\n");
+
+    echo_can_free(ctx);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int perform_test_9(void)
+{
+    echo_can_state_t *ctx;
+    awgn_state_t local_noise_source;
+    awgn_state_t far_noise_source;
+
+    /* Test 9 - Comfort noise test */
+    print_test_title("Performing test 9 - Comfort noise test\n");
+    ctx = echo_can_create(TEST_EC_TAPS, 0);
+    awgn_init_dbm0(&far_noise_source, 7162534, -50.0f);
+
+    echo_can_flush(ctx);
+    echo_can_adaption_mode(ctx,
+                           ECHO_CAN_USE_ADAPTION 
+                         | ECHO_CAN_USE_NLP 
+                         | ECHO_CAN_USE_CNG);
+
+    /* Test 9 part 1 - matching */
+    /* Converge the canceller */
+    signal_restart(&local_css, 0.0f);
+    run_test(ctx, local_css_signal, silence, 5000);
+
+    echo_can_adaption_mode(ctx, ECHO_CAN_USE_ADAPTION | ECHO_CAN_USE_NLP | ECHO_CAN_USE_CNG);
+    awgn_init_dbm0(&far_noise_source, 7162534, -45.0f);
+    run_test(ctx, silence, far_noise_signal, 30000);
+
+    awgn_init_dbm0(&local_noise_source, 1234567, -10.0f);
+    run_test(ctx, local_noise_signal, far_noise_signal, 2000);
+
+    /* Test 9 part 2 - adjust down */
+    awgn_init_dbm0(&far_noise_source, 7162534, -55.0f);
+    run_test(ctx, silence, far_noise_signal, 10000);
+    run_test(ctx, local_noise_signal, far_noise_signal, 2000);
+
+    echo_can_free(ctx);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int perform_test_10a(void)
+{
+    echo_can_state_t *ctx;
+
+    /* Test 10 - FAX test during call establishment phase */
+    /* Test 10A - Canceller operation on the calling station side */
+    print_test_title("Performing test 10A - Canceller operation on the calling station side\n");
+    ctx = echo_can_create(TEST_EC_TAPS, 0);
+
+    fprintf(stderr, "Test 10A not yet implemented\n");
+
+    echo_can_free(ctx);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int perform_test_10b(void)
+{
+    echo_can_state_t *ctx;
+
+    /* Test 10 - FAX test during call establishment phase */
+    /* Test 10B - Canceller operation on the called station side */
+    print_test_title("Performing test 10B - Canceller operation on the called station side\n");
+    ctx = echo_can_create(TEST_EC_TAPS, 0);
+
+    fprintf(stderr, "Test 10B not yet implemented\n");
+
+    echo_can_free(ctx);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int perform_test_10c(void)
+{
+    echo_can_state_t *ctx;
+
+    /* Test 10 - FAX test during call establishment phase */
+    /* Test 10C - Canceller operation on the calling station side during page
+                  transmission and page breaks (for further study) */
+    print_test_title("Performing test 10C - Canceller operation on the calling station side during page\n"
+                     "transmission and page breaks (for further study)\n");
+    ctx = echo_can_create(TEST_EC_TAPS, 0);
+
+    fprintf(stderr, "Test 10C not yet implemented\n");
+
+    echo_can_free(ctx);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int perform_test_11(void)
+{
+    echo_can_state_t *ctx;
+
+    /* Test 11 - Tandem echo canceller test (for further study) */
+    print_test_title("Performing test 11 - Tandem echo canceller test (for further study)\n");
+    ctx = echo_can_create(TEST_EC_TAPS, 0);
+
+    fprintf(stderr, "Test 11 not yet implemented\n");
+
+    echo_can_free(ctx);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int perform_test_12(void)
+{
+    echo_can_state_t *ctx;
+
+    /* Test 12 - Residual acoustic echo test (for further study) */
+    print_test_title("Performing test 12 - Residual acoustic echo test (for further study)\n");
+    ctx = echo_can_create(TEST_EC_TAPS, 0);
+
+    fprintf(stderr, "Test 12 not yet implemented\n");
+
+    echo_can_free(ctx);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int perform_test_13(void)
+{
+    echo_can_state_t *ctx;
+
+    /* Test 13 - Performance with ITU-T low-bit rate coders in echo path
+                 (Optional, under study) */
+    print_test_title("Performing test 13 - Performance with ITU-T low-bit rate coders in echo path (Optional, under study)\n");
+    ctx = echo_can_create(TEST_EC_TAPS, 0);
+
+    fprintf(stderr, "Test 13 not yet implemented\n");
+
+    echo_can_free(ctx);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int perform_test_14(void)
+{
+    echo_can_state_t *ctx;
+
+    /* Test 14 - Performance with V-series low-speed data modems */
+    print_test_title("Performing test 14 - Performance with V-series low-speed data modems\n");
+    ctx = echo_can_create(TEST_EC_TAPS, 0);
+
+    fprintf(stderr, "Test 14 not yet implemented\n");
+
+    echo_can_free(ctx);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int perform_test_15(void)
+{
+    echo_can_state_t *ctx;
+
+    /* Test 15 - PCM offset test (Optional) */
+    print_test_title("Performing test 15 - PCM offset test (Optional)\n");
+    ctx = echo_can_create(TEST_EC_TAPS, 0);
+
+    fprintf(stderr, "Test 15 not yet implemented\n");
+
+    echo_can_free(ctx);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int match_test_name(const char *name)
+{
+    const struct
+    {
+        const char *name;
+        int (*func)(void);
+    } tests[] =
+    {
+        {"sanity", perform_test_sanity},
+        {"2a", perform_test_2a},
+        {"2b", perform_test_2b},
+        {"2ca", perform_test_2ca},
+        {"3a", perform_test_3a},
+        {"3ba", perform_test_3ba},
+        {"3bb", perform_test_3bb},
+        {"3c", perform_test_3c},
+        {"4", perform_test_4},
+        {"5", perform_test_5},
+        {"6", perform_test_6},
+        {"7", perform_test_7},
+        {"8", perform_test_8},
+        {"9", perform_test_9},
+        {"10a", perform_test_10a},
+        {"10b", perform_test_10b},
+        {"10c", perform_test_10c},
+        {"11", perform_test_11},
+        {"12", perform_test_12},
+        {"13", perform_test_13},
+        {"14", perform_test_14},
+        {"15", perform_test_15},
+        {NULL, NULL}
+    };
+    int i;
+
+    for (i = 0;  tests[i].name;  i++)
+    {
+        if (strcasecmp(name, tests[i].name) == 0)
+        {
+            test_name = name;
+            tests[i].func();
+            return 0;
+        }
+    }
+    printf("Unknown test name '%s' specified. The known test names are ", name);
+    for (i = 0;  tests[i].name;  i++)
+    {
+        printf("%s", tests[i].name);
+        if (tests[i + 1].name)
+            printf(", ");
+    }
+    printf("\n");
+    return -1;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void simulate_ec(const char *argv[], int two_channel_file, int mode)
+{
+    echo_can_state_t *ctx;
+    AFfilehandle txfile;
+    AFfilehandle rxfile;
+    AFfilehandle rxtxfile;
+    AFfilehandle ecfile;
+    int ntx;
+    int nrx;
+    int nec;
+    int16_t buf[2];
+    int16_t rin;
+    int16_t rout;
+    int16_t sin;
+    int16_t sout;
+    int32_t samples;
+
+    mode |= ECHO_CAN_USE_ADAPTION;
+    txfile = NULL;
+    rxfile = NULL;
+    rxtxfile = NULL;
+    ecfile = NULL;
+    if (two_channel_file)
+    {
+        txfile = afOpenFile_telephony_read(argv[0], 1);
+        rxfile = afOpenFile_telephony_read(argv[1], 1);      
+        ecfile = afOpenFile_telephony_write(argv[2], 1);
+    }
+    else
+    {
+        rxtxfile = afOpenFile_telephony_read(argv[0], 2);
+        ecfile = afOpenFile_telephony_write(argv[1], 1);
+    }
+
+    ctx = echo_can_create(TEST_EC_TAPS, 0);
+    echo_can_adaption_mode(ctx, mode);
+    samples = 0;
+    do
+    {
+        if (two_channel_file)
+        {
+            if ((ntx = afReadFrames(rxtxfile, AF_DEFAULT_TRACK, buf, 1)) < 0)
+            {
+                fprintf(stderr, "    Error reading tx sound file\n");
+                exit(2);
+            }
+            rin = buf[0];
+            sin = buf[1];
+            nrx = ntx;
+        }
+        else
+        {
+            if ((ntx = afReadFrames(txfile, AF_DEFAULT_TRACK, &rin, 1)) < 0)
+            {
+                fprintf(stderr, "    Error reading tx sound file\n");
+                exit(2);
+            }
+            if ((nrx = afReadFrames(rxfile, AF_DEFAULT_TRACK, &sin, 1)) < 0)
+            {           
+                fprintf(stderr, "    Error reading rx sound file\n");
+                exit(2);
+            }
+        }
+        rout = echo_can_hpf_tx(ctx, rin);
+        sout = echo_can_update(ctx, rout, sin);
+
+        if ((nec = afWriteFrames(ecfile, AF_DEFAULT_TRACK, &sout, 1)) != 1)
+        {
+            fprintf(stderr, "    Error writing ec sound file\n");
+            exit(2);
+        }
+        level_measurements_update(rin, sin, rout, sout, 0);
+        write_log_files(rin, sin);
+#if defined(ENABLE_GUI)
+        if (use_gui)
+            echo_can_monitor_can_update(ctx->fir_taps16[0], TEST_EC_TAPS);
+#endif
+    }
+    while (ntx  &&  nrx);
+    dump_ec_state(ctx);
+
+    echo_can_free(ctx);
+
+    if (two_channel_file)
+    {
+        afCloseFile(rxtxfile);
+    }
+    else
+    {
+        afCloseFile(txfile);
+        afCloseFile(rxfile);
+    }
+    afCloseFile(ecfile);
+}
+/*- End of function --------------------------------------------------------*/
+
+int main(int argc, char *argv[])
+{
+    int i;
+    time_t now;
+    int simulate;
+    int cng;
+    int hpf;
+    int two_channel_file;
+    int opt;
+    int mode;
+
+    /* Check which tests we should run */
+    if (argc < 2)
+        fprintf(stderr, "Usage: echo tests [-g] [-m <model number>] [-s] <list of test numbers>\n");
+    line_model_no = 0;
+    supp_line_model_no = 0;
+    cng = FALSE;
+    hpf = FALSE;
+    use_gui = FALSE;
+    simulate = FALSE;
+    munger = -1;
+    two_channel_file = FALSE;
+    erl = -12.0f;
+
+    while ((opt = getopt(argc, argv, "2ace:ghm:M:su")) != -1)
+    {
+        switch (opt)
+        {
+        case '2':
+            two_channel_file = TRUE;
+            break;
+        case 'a':
+            munger = G711_ALAW;
+            break;
+        case 'c':
+            cng = TRUE;
+            break;
+        case 'e':
+            /* Allow for ERL being entered as x or -x */
+            erl = -fabs(atof(optarg));
+            break;
+        case 'g':
+#if defined(ENABLE_GUI)
+            use_gui = TRUE;
+#else
+            fprintf(stderr, "Graphical monitoring not available\n");
+            exit(2);
+#endif
+            break;
+        case 'h':
+            hpf = TRUE;
+            break;
+        case 'm':
+            line_model_no = atoi(optarg);
+            break;
+        case 'M':
+            supp_line_model_no = atoi(optarg);
+            break;
+        case 's':
+            simulate = TRUE;
+            break;
+        case 'u':
+            munger = G711_ULAW;
+            break;
+        default:
+            //usage();
+            exit(2);
+            break;
+        }
+    }
+    argc -= optind;
+    argv += optind;
+
+#if defined(ENABLE_GUI)
+    if (use_gui)
+        start_echo_can_monitor(TEST_EC_TAPS);
+#endif
+    if (simulate)
+    {
+        /* Process a pair of transmitted and received wave files, and produce
+           an echo cancelled wave file. */
+        if (argc < ((two_channel_file)  ?  2  :  3))
+        {
+            printf("not enough arguments for a simulation\n");
+            exit(2);
+        }
+        mode = ECHO_CAN_USE_NLP;
+        mode |= ((cng)  ?  ECHO_CAN_USE_CNG  :  ECHO_CAN_USE_CLIP);
+        if (hpf)
+        {
+            mode |= ECHO_CAN_USE_TX_HPF;
+            mode |= ECHO_CAN_USE_RX_HPF;
+        }
+        simulate_ec(argv, two_channel_file, mode);
+    }
+    else
+    {
+        /* Run some G.168 tests */
+#if defined(ENABLE_GUI)
+        if (use_gui)
+            echo_can_monitor_line_model_update(chan_model.impulse.coeffs, chan_model.impulse.taps);
+#endif
+        signal_load(&local_css, "sound_c1_8k.wav");
+        signal_load(&far_css, "sound_c3_8k.wav");
+
+        if ((residue_handle = afOpenFile_telephony_write(RESIDUE_FILE_NAME, 1)) == AF_NULL_FILEHANDLE)
+        {
+            fprintf(stderr, "    Failed to open '%s'\n", RESIDUE_FILE_NAME);
+            exit(2);
+        }
+        if (argc <= 0)
+        {
+            printf("No tests specified\n");
+        }
+        else
+        {
+            time(&now);
+            if ((result_handle = afOpenFile_telephony_write("echo_tests_result.wav", RESULT_CHANNELS)) == AF_NULL_FILEHANDLE)
+            {
+                fprintf(stderr, "    Failed to open result file\n");
+                exit(2);
+            }
+            result_cur = 0;
+            level_measurements_create(0);
+            for (i = 0;  i < argc;  i++)
+            {
+                if (channel_model_create(&chan_model, line_model_no, erl, munger))
+                {
+                    fprintf(stderr, "    Failed to create line model\n");
+                    exit(2);
+                }
+                match_test_name(argv[i]);
+            }
+            if (afCloseFile(result_handle) != 0)
+            {
+                fprintf(stderr, "    Cannot close speech file '%s'\n", "result_sound.wav");
+                exit(2);
+            }
+            printf("Run time %lds\n", time(NULL) - now);
+        }
+        signal_free(&local_css);
+        signal_free(&far_css);
+        if (afCloseFile(residue_handle) != 0)
+        {
+            fprintf(stderr, "    Cannot close speech file '%s'\n", RESIDUE_FILE_NAME);
+            exit(2);
+        }
+    }
+#if defined(ENABLE_GUI)
+    if (use_gui)
+        echo_can_monitor_wait_to_end();
+#endif
+
+    printf("Tests passed.\n");
+    return  0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/tests/fax_decode.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/tests/fax_decode.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,587 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * fax_decode.c - a simple FAX audio decoder
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2005 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: fax_decode.c,v 1.45 2008/08/13 00:11:30 steveu Exp $
+ */
+
+/*! \page fax_decode_page FAX decoder
+\section fax_decode_page_sec_1 What does it do?
+???.
+
+\section fax_decode_tests_page_sec_2 How does it work?
+???.
+*/
+
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <assert.h>
+#include <audiofile.h>
+
+#include "spandsp.h"
+
+#define SAMPLES_PER_CHUNK   160
+
+#define DISBIT1     0x01
+#define DISBIT2     0x02
+#define DISBIT3     0x04
+#define DISBIT4     0x08
+#define DISBIT5     0x10
+#define DISBIT6     0x20
+#define DISBIT7     0x40
+#define DISBIT8     0x80
+
+enum
+{
+    FAX_NONE,
+    FAX_V27TER_RX,
+    FAX_V29_RX,
+    FAX_V17_RX
+};
+
+static const struct
+{
+    int bit_rate;
+    int modem_type;
+    int which;
+    uint8_t dcs_code;
+} fallback_sequence[] =
+{
+    {14400, T30_MODEM_V17,    T30_SUPPORT_V17,    DISBIT6},
+    {12000, T30_MODEM_V17,    T30_SUPPORT_V17,    (DISBIT6 | DISBIT4)},
+    { 9600, T30_MODEM_V17,    T30_SUPPORT_V17,    (DISBIT6 | DISBIT3)},
+    { 9600, T30_MODEM_V29,    T30_SUPPORT_V29,    DISBIT3},
+    { 7200, T30_MODEM_V17,    T30_SUPPORT_V17,    (DISBIT6 | DISBIT4 | DISBIT3)},
+    { 7200, T30_MODEM_V29,    T30_SUPPORT_V29,    (DISBIT4 | DISBIT3)},
+    { 4800, T30_MODEM_V27TER, T30_SUPPORT_V27TER, DISBIT4},
+    { 2400, T30_MODEM_V27TER, T30_SUPPORT_V27TER, 0},
+    {    0, 0, 0, 0}
+};
+
+int decode_test = FALSE;
+int rx_bits = 0;
+
+t30_state_t t30_dummy;
+t4_state_t t4_state;
+int t4_up = FALSE;
+
+hdlc_rx_state_t hdlcrx;
+
+int fast_trained = FAX_NONE;
+
+uint8_t ecm_data[256][260];
+int16_t ecm_len[256];
+
+int line_encoding = T4_COMPRESSION_ITU_T4_1D;
+int x_resolution = T4_X_RESOLUTION_R8;
+int y_resolution = T4_Y_RESOLUTION_STANDARD;
+int image_width = 1728;
+int octets_per_ecm_frame = 256;
+int error_correcting_mode = FALSE;
+int current_fallback = 0;
+
+static void print_frame(const char *io, const uint8_t *fr, int frlen)
+{
+    int i;
+    int type;
+    const char *country;
+    const char *vendor;
+    const char *model;
+    
+    fprintf(stderr, "%s %s:", io, t30_frametype(fr[2]));
+    for (i = 2;  i < frlen;  i++)
+        fprintf(stderr, " %02x", fr[i]);
+    fprintf(stderr, "\n");
+    type = fr[2] & 0xFE;
+    if (type == T30_DIS  ||  type == T30_DTC  ||  type == T30_DCS)
+        t30_decode_dis_dtc_dcs(&t30_dummy, fr, frlen);
+    if (type == T30_NSF)
+    {
+        if (t35_decode(&fr[3], frlen - 3, &country, &vendor, &model))
+        {
+            if (country)
+                fprintf(stderr, "The remote was made in '%s'\n", country);
+            if (vendor)
+                fprintf(stderr, "The remote was made by '%s'\n", vendor);
+            if (model)
+                fprintf(stderr, "The remote is a '%s'\n", model);
+        }
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static int find_fallback_entry(int dcs_code)
+{
+    int i;
+
+    /* The table is short, and not searched often, so a brain-dead linear scan seems OK */
+    for (i = 0;  fallback_sequence[i].bit_rate;  i++)
+    {
+        if (fallback_sequence[i].dcs_code == dcs_code)
+            break;
+    }
+    if (fallback_sequence[i].bit_rate == 0)
+        return -1;
+    return i;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int check_rx_dcs(const uint8_t *msg, int len)
+{
+    static const int widths[3][4] =
+    {
+        { 864, 1024, 1216, -1}, /* R4 resolution - no longer used in recent versions of T.30 */
+        {1728, 2048, 2432, -1}, /* R8 resolution */
+        {3456, 4096, 4864, -1}  /* R16 resolution */
+    };
+    uint8_t dcs_frame[T30_MAX_DIS_DTC_DCS_LEN];
+
+    /* Check DCS frame from remote */
+    if (len < 6)
+    {
+        printf("Short DCS frame\n");
+        return -1;
+    }
+
+    /* Make a local copy of the message, padded to the maximum possible length with zeros. This allows
+       us to simply pick out the bits, without worrying about whether they were set from the remote side. */
+    if (len > T30_MAX_DIS_DTC_DCS_LEN)
+    {
+        memcpy(dcs_frame, msg, T30_MAX_DIS_DTC_DCS_LEN);
+    }
+    else
+    {
+        memcpy(dcs_frame, msg, len);
+        if (len < T30_MAX_DIS_DTC_DCS_LEN)
+            memset(dcs_frame + len, 0, T30_MAX_DIS_DTC_DCS_LEN - len);
+    }
+
+    octets_per_ecm_frame = (dcs_frame[6] & DISBIT4)  ?  256  :  64;
+    if ((dcs_frame[8] & DISBIT1))
+        y_resolution = T4_Y_RESOLUTION_SUPERFINE;
+    else if (dcs_frame[4] & DISBIT7)
+        y_resolution = T4_Y_RESOLUTION_FINE;
+    else
+        y_resolution = T4_Y_RESOLUTION_STANDARD;
+    image_width = widths[(dcs_frame[8] & DISBIT3)  ?  2  :  1][dcs_frame[5] & (DISBIT2 | DISBIT1)];
+
+    /* Check which compression we will use. */
+    if ((dcs_frame[6] & DISBIT7))
+        line_encoding = T4_COMPRESSION_ITU_T6;
+    else if ((dcs_frame[4] & DISBIT8))
+        line_encoding = T4_COMPRESSION_ITU_T4_2D;
+    else
+        line_encoding = T4_COMPRESSION_ITU_T4_1D;
+    fprintf(stderr, "Selected compression %d\n", line_encoding);
+
+    if ((current_fallback = find_fallback_entry(dcs_frame[4] & (DISBIT6 | DISBIT5 | DISBIT4 | DISBIT3))) < 0)
+        printf("Remote asked for a modem standard we do not support\n");
+    error_correcting_mode = ((dcs_frame[6] & DISBIT3) != 0);
+
+    //v17_rx_restart(&v17, fallback_sequence[fallback_entry].bit_rate, FALSE);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void hdlc_accept(void *user_data, const uint8_t *msg, int len, int ok)
+{
+    int type;
+    int frame_no;
+    int i;
+
+    if (len < 0)
+    {
+        /* Special conditions */
+        switch (len)
+        {
+        case PUTBIT_CARRIER_UP:
+            fprintf(stderr, "HDLC carrier up\n");
+            break;
+        case PUTBIT_CARRIER_DOWN:
+            fprintf(stderr, "HDLC carrier down\n");
+            break;
+        case PUTBIT_FRAMING_OK:
+            fprintf(stderr, "HDLC framing OK\n");
+            break;
+        case PUTBIT_ABORT:
+            /* Just ignore these */
+            break;
+        default:
+            fprintf(stderr, "Unexpected HDLC special length - %d!\n", len);
+            break;
+        }
+        return;
+    }
+    
+    if (ok)
+    {
+        if (msg[0] != 0xFF  ||  !(msg[1] == 0x03  ||  msg[1] == 0x13))
+        {
+            fprintf(stderr, "Bad frame header - %02x %02x", msg[0], msg[1]);
+            return;
+        }
+        print_frame("HDLC: ", msg, len);
+        type = msg[2] & 0xFE;
+        switch (type)
+        {
+        case T4_FCD:
+            if (len <= 4 + 256)
+            {
+                frame_no = msg[3];
+                /* Just store the actual image data, and record its length */
+                memcpy(&ecm_data[frame_no][0], &msg[4], len - 4);
+                ecm_len[frame_no] = (int16_t) (len - 4);
+            }
+            break;
+        case T30_DCS:
+            check_rx_dcs(msg, len);
+            break;
+        }
+    }
+    else
+    {
+        fprintf(stderr, "Bad HDLC frame ");
+        for (i = 0;  i < len;  i++)
+            fprintf(stderr, " %02x", msg[i]);
+        fprintf(stderr, "\n");
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static void t4_begin(void)
+{
+    int i;
+
+    //printf("Begin T.4 - %d %d %d %d\n", line_encoding, x_resolution, y_resolution, image_width);
+    t4_rx_set_rx_encoding(&t4_state, line_encoding);
+    t4_rx_set_x_resolution(&t4_state, x_resolution);
+    t4_rx_set_y_resolution(&t4_state, y_resolution);
+    t4_rx_set_image_width(&t4_state, image_width);
+
+    t4_rx_start_page(&t4_state);
+    t4_up = TRUE;
+
+    for (i = 0;  i < 256;  i++)
+        ecm_len[i] = -1;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void t4_end(void)
+{
+    t4_stats_t stats;
+    int i;
+    int j;
+    int k;
+
+    if (!t4_up)
+        return;
+    if (error_correcting_mode)
+    {
+        for (i = 0;  i < 256;  i++)
+        {
+            for (j = 0;  j < ecm_len[i];  j++)
+            {
+                for (k = 0;  k < 8;  k++)
+                    t4_rx_put_bit(&t4_state, (ecm_data[i][j] >> k) & 1);
+            }
+            fprintf(stderr, "%d", (ecm_len[i] < 0)  ?  0  :  1);
+        }
+        fprintf(stderr, "\n");
+    }
+    t4_rx_end_page(&t4_state);
+    t4_get_transfer_statistics(&t4_state, &stats);
+    fprintf(stderr, "Pages = %d\n", stats.pages_transferred);
+    fprintf(stderr, "Image size = %dx%d\n", stats.width, stats.length);
+    fprintf(stderr, "Image resolution = %dx%d\n", stats.x_resolution, stats.y_resolution);
+    fprintf(stderr, "Bad rows = %d\n", stats.bad_rows);
+    fprintf(stderr, "Longest bad row run = %d\n", stats.longest_bad_row_run);
+    t4_up = FALSE;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void v21_put_bit(void *user_data, int bit)
+{
+    if (bit < 0)
+    {
+        /* Special conditions */
+        switch (bit)
+        {
+        case PUTBIT_TRAINING_FAILED:
+            fprintf(stderr, "V.21 Training failed\n");
+            break;
+        case PUTBIT_TRAINING_SUCCEEDED:
+            fprintf(stderr, "V.21 Training succeeded\n");
+            t4_begin();
+            break;
+        case PUTBIT_CARRIER_UP:
+            fprintf(stderr, "V.21 Carrier up\n");
+            break;
+        case PUTBIT_CARRIER_DOWN:
+            fprintf(stderr, "V.21 Carrier down\n");
+            t4_end();
+            break;
+        default:
+            fprintf(stderr, "V.21 Eh!\n");
+            break;
+        }
+        return;
+    }
+    if (fast_trained == FAX_NONE)
+        hdlc_rx_put_bit(&hdlcrx, bit);
+    //printf("V.21 Rx bit %d - %d\n", rx_bits++, bit);
+}
+/*- End of function --------------------------------------------------------*/
+
+static void v17_put_bit(void *user_data, int bit)
+{
+    if (bit < 0)
+    {
+        /* Special conditions */
+        switch (bit)
+        {
+        case PUTBIT_TRAINING_FAILED:
+            fprintf(stderr, "V.17 Training failed\n");
+            break;
+        case PUTBIT_TRAINING_SUCCEEDED:
+            fprintf(stderr, "V.17 Training succeeded\n");
+            fast_trained = FAX_V17_RX;
+            t4_begin();
+            break;
+        case PUTBIT_CARRIER_UP:
+            fprintf(stderr, "V.17 Carrier up\n");
+            break;
+        case PUTBIT_CARRIER_DOWN:
+            fprintf(stderr, "V.17 Carrier down\n");
+            t4_end();
+            if (fast_trained == FAX_V17_RX)
+                fast_trained = FAX_NONE;
+            break;
+        default:
+            fprintf(stderr, "V.17 Eh!\n");
+            break;
+        }
+        return;
+    }
+    if (error_correcting_mode)
+    {
+        hdlc_rx_put_bit(&hdlcrx, bit);
+    }
+    else
+    {
+        if (t4_rx_put_bit(&t4_state, bit))
+        {
+            t4_end();
+            fprintf(stderr, "End of page detected\n");
+        }
+    }
+    //printf("V.17 Rx bit %d - %d\n", rx_bits++, bit);
+}
+/*- End of function --------------------------------------------------------*/
+
+static void v29_put_bit(void *user_data, int bit)
+{
+    if (bit < 0)
+    {
+        /* Special conditions */
+        switch (bit)
+        {
+        case PUTBIT_TRAINING_FAILED:
+            //fprintf(stderr, "V.29 Training failed\n");
+            break;
+        case PUTBIT_TRAINING_SUCCEEDED:
+            fprintf(stderr, "V.29 Training succeeded\n");
+            fast_trained = FAX_V29_RX;
+            t4_begin();
+            break;
+        case PUTBIT_CARRIER_UP:
+            //fprintf(stderr, "V.29 Carrier up\n");
+            break;
+        case PUTBIT_CARRIER_DOWN:
+            //fprintf(stderr, "V.29 Carrier down\n");
+            t4_end();
+            if (fast_trained == FAX_V29_RX)
+                fast_trained = FAX_NONE;
+            break;
+        default:
+            fprintf(stderr, "V.29 Eh!\n");
+            break;
+        }
+        return;
+    }
+    if (error_correcting_mode)
+    {
+        hdlc_rx_put_bit(&hdlcrx, bit);
+    }
+    else
+    {
+        if (t4_rx_put_bit(&t4_state, bit))
+        {
+            t4_end();
+            fprintf(stderr, "End of page detected\n");
+        }
+    }
+    //printf("V.29 Rx bit %d - %d\n", rx_bits++, bit);
+}
+/*- End of function --------------------------------------------------------*/
+
+static void v27ter_put_bit(void *user_data, int bit)
+{
+    if (bit < 0)
+    {
+        /* Special conditions */
+        switch (bit)
+        {
+        case PUTBIT_TRAINING_FAILED:
+            //fprintf(stderr, "V.27ter Training failed\n");
+            break;
+        case PUTBIT_TRAINING_SUCCEEDED:
+            fprintf(stderr, "V.27ter Training succeeded\n");
+            fast_trained = FAX_V27TER_RX;
+            t4_begin();
+            break;
+        case PUTBIT_CARRIER_UP:
+            //fprintf(stderr, "V.27ter Carrier up\n");
+            break;
+        case PUTBIT_CARRIER_DOWN:
+            //fprintf(stderr, "V.27ter Carrier down\n");
+            if (fast_trained == FAX_V27TER_RX)
+                fast_trained = FAX_NONE;
+            break;
+        default:
+            fprintf(stderr, "V.27ter Eh!\n");
+            break;
+        }
+        return;
+    }
+    if (error_correcting_mode)
+    {
+        hdlc_rx_put_bit(&hdlcrx, bit);
+    }
+    else
+    {
+        if (t4_rx_put_bit(&t4_state, bit))
+        {
+            t4_end();
+            fprintf(stderr, "End of page detected\n");
+        }
+    }
+    //printf("V.27ter Rx bit %d - %d\n", rx_bits++, bit);
+}
+/*- End of function --------------------------------------------------------*/
+
+int main(int argc, char *argv[])
+{
+    fsk_rx_state_t fsk;
+    v17_rx_state_t v17;
+    v29_rx_state_t v29;
+    v27ter_rx_state_t v27ter;
+    int16_t amp[SAMPLES_PER_CHUNK];
+    AFfilehandle inhandle;
+    int len;
+    const char *filename;
+    float x;
+
+    filename = "fax_samp.wav";
+
+    if (argc > 1)
+        filename = argv[1];
+
+    if ((inhandle = afOpenFile(filename, "r", NULL)) == AF_NULL_FILEHANDLE)
+    {
+        fprintf(stderr, "    Cannot open wave file '%s'\n", filename);
+        exit(2);
+    }
+    if ((x = afGetFrameSize(inhandle, AF_DEFAULT_TRACK, 1)) != 2.0)
+    {
+        printf("    Unexpected frame size in speech file '%s' (%f)\n", filename, x);
+        exit(2);
+    }
+    if ((x = afGetRate(inhandle, AF_DEFAULT_TRACK)) != (float) SAMPLE_RATE)
+    {
+        printf("    Unexpected sample rate in speech file '%s' (%f)\n", filename, x);
+        exit(2);
+    }
+    if ((x = afGetChannels(inhandle, AF_DEFAULT_TRACK)) != 1.0)
+    {
+        printf("    Unexpected number of channels in speech file '%s' (%f)\n", filename, x);
+        exit(2);
+    }
+    memset(&t30_dummy, 0, sizeof(t30_dummy));
+    span_log_init(&t30_dummy.logging, SPAN_LOG_FLOW, NULL);
+    span_log_set_protocol(&t30_dummy.logging, "T.30");
+
+    hdlc_rx_init(&hdlcrx, FALSE, TRUE, 5, hdlc_accept, NULL);
+    fsk_rx_init(&fsk, &preset_fsk_specs[FSK_V21CH2], TRUE, v21_put_bit, NULL);
+    v17_rx_init(&v17, 14400, v17_put_bit, NULL);
+    v29_rx_init(&v29, 9600, v29_put_bit, NULL);
+    //v29_rx_init(&v29, 7200, v29_put_bit, NULL);
+    v27ter_rx_init(&v27ter, 4800, v27ter_put_bit, NULL);
+    fsk_rx_signal_cutoff(&fsk, -45.5);
+    v17_rx_signal_cutoff(&v17, -45.5);
+    v29_rx_signal_cutoff(&v29, -45.5);
+    v27ter_rx_signal_cutoff(&v27ter, -40.0);
+
+    span_log_init(&v17.logging, SPAN_LOG_FLOW, NULL);
+    span_log_set_protocol(&v17.logging, "V.17");
+    span_log_set_level(&v17.logging, SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_SHOW_TAG | SPAN_LOG_FLOW);
+
+    span_log_init(&v29.logging, SPAN_LOG_FLOW, NULL);
+    span_log_set_protocol(&v29.logging, "V.29");
+    span_log_set_level(&v29.logging, SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_SHOW_TAG | SPAN_LOG_FLOW);
+
+    span_log_init(&v27ter.logging, SPAN_LOG_FLOW, NULL);
+    span_log_set_protocol(&v27ter.logging, "V.27ter");
+    span_log_set_level(&v27ter.logging, SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_SHOW_TAG | SPAN_LOG_FLOW);
+
+    if (t4_rx_init(&t4_state, "fax_decode.tif", T4_COMPRESSION_ITU_T4_2D) == NULL)
+    {
+        fprintf(stderr, "Failed to init\n");
+        exit(0);
+    }
+        
+    for (;;)
+    {
+        len = afReadFrames(inhandle, AF_DEFAULT_TRACK, amp, SAMPLES_PER_CHUNK);
+        if (len < SAMPLES_PER_CHUNK)
+            break;
+        fsk_rx(&fsk, amp, len);
+        v17_rx(&v17, amp, len);
+        v29_rx(&v29, amp, len);
+        v27ter_rx(&v27ter, amp, len);
+    }
+    t4_rx_end(&t4_state);
+
+    if (afCloseFile(inhandle) != 0)
+    {
+        fprintf(stderr, "    Cannot close wave file '%s'\n", filename);
+        exit(2);
+    }
+    return  0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/tests/fax_tester.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/tests/fax_tester.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,759 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * faxtester_tests.c
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2008 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: fax_tester.c,v 1.13 2008/08/13 00:11:30 steveu Exp $
+ */
+
+/*! \file */
+
+#if defined(HAVE_CONFIG_H)
+#include <config.h>
+#endif
+
+#include <inttypes.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include "floating_fudge.h"
+#if defined(HAVE_TGMATH_H)
+#include <tgmath.h>
+#endif
+#if defined(HAVE_MATH_H)
+#include <math.h>
+#endif
+#include <assert.h>
+#include <fcntl.h>
+#include <time.h>
+#include <unistd.h>
+
+#if defined(HAVE_LIBXML_XMLMEMORY_H)
+#include <libxml/xmlmemory.h>
+#endif
+#if defined(HAVE_LIBXML_PARSER_H)
+#include <libxml/parser.h>
+#endif
+#if defined(HAVE_LIBXML_XINCLUDE_H)
+#include <libxml/xinclude.h>
+#endif
+
+#include "spandsp.h"
+#include "fax_tester.h"
+
+#define HDLC_FRAMING_OK_THRESHOLD       5
+
+static void timer_update(faxtester_state_t *s, int len)
+{
+    s->timer += len;
+    if (s->timer > s->timeout)
+    {
+        s->timeout = 0x7FFFFFFFFFFFFFFFLL;
+        if (s->front_end_step_timeout_handler)
+            s->front_end_step_timeout_handler(s, s->front_end_step_timeout_user_data);
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static void front_end_step_complete(faxtester_state_t *s)
+{
+    if (s->front_end_step_complete_handler)
+        s->front_end_step_complete_handler(s, s->front_end_step_complete_user_data);
+}
+/*- End of function --------------------------------------------------------*/
+
+void faxtester_send_hdlc_flags(faxtester_state_t *s, int flags)
+{
+    hdlc_tx_flags(&(s->modems.hdlc_tx), flags);
+}
+/*- End of function --------------------------------------------------------*/
+
+void faxtester_send_hdlc_msg(faxtester_state_t *s, const uint8_t *msg, int len, int crc_ok)
+{
+    hdlc_tx_frame(&(s->modems.hdlc_tx), msg, len);
+    if (!crc_ok)
+        hdlc_tx_corrupt_frame(&(s->modems.hdlc_tx));
+}
+/*- End of function --------------------------------------------------------*/
+
+static void hdlc_underflow_handler(void *user_data)
+{
+    faxtester_state_t *s;
+    uint8_t buf[400];
+
+    s = (faxtester_state_t *) user_data;
+    
+    if (s->image_buffer)
+    {
+        /* We are sending an ECM image */
+        if (s->image_ptr < s->image_len)
+        {
+            buf[0] = 0xFF;
+            buf[1] = 0x03;
+            buf[2] = 0x06;
+            buf[3] = s->image_ptr/s->ecm_frame_size;
+            memcpy(buf + 4, &s->image_buffer[s->image_ptr], s->ecm_frame_size);
+            hdlc_tx_frame(&(s->modems.hdlc_tx), buf, 4 + s->ecm_frame_size);
+            if (s->corrupt_crc >= 0  &&  s->corrupt_crc == s->image_ptr/s->ecm_frame_size)
+                hdlc_tx_corrupt_frame(&(s->modems.hdlc_tx));
+            s->image_ptr += s->ecm_frame_size;
+            return;
+        }
+        else
+        {
+            /* The actual image is over. We are sending the final RCP frames. */
+            if (s->image_bit_ptr > 2)
+            {
+                s->image_bit_ptr--;
+                buf[0] = 0xFF;
+                buf[1] = 0x03;
+                buf[2] = 0x86;
+                hdlc_tx_frame(&(s->modems.hdlc_tx), buf, 3);
+                return;
+            }
+            else
+            {
+                /* All done. */
+                s->image_buffer = NULL;
+            }
+        }
+    }
+    front_end_step_complete(s);
+}
+/*- End of function --------------------------------------------------------*/
+
+static int modem_tx_status(void *user_data, int status)
+{
+    faxtester_state_t *s;
+
+    s = (faxtester_state_t *) user_data;
+    switch (status)
+    {
+    case MODEM_TX_STATUS_DATA_EXHAUSTED:
+        span_log(&s->logging, SPAN_LOG_FLOW, "Tx data exhausted\n");
+        break;
+    case MODEM_TX_STATUS_SHUTDOWN_COMPLETE:
+        span_log(&s->logging, SPAN_LOG_FLOW, "Tx shutdown complete\n");
+        front_end_step_complete(s);
+        break;
+    default:
+        span_log(&s->logging, SPAN_LOG_FLOW, "Tx status is %d\n", status);
+        break;
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void tone_detected(void *user_data, int tone, int level, int delay)
+{
+    faxtester_state_t *s;
+
+    s = (faxtester_state_t *) user_data;
+    span_log(&s->logging,
+             SPAN_LOG_FLOW,
+             "%s (%d) declared (%ddBm0)\n",
+             modem_connect_tone_to_str(tone),
+             tone,
+             level);
+    if (tone != MODEM_CONNECT_TONES_NONE)
+    {
+        s->tone_on_time = s->timer;
+    }
+    else
+    {
+        span_log(&s->logging,
+                 SPAN_LOG_FLOW,
+                 "Tone was on for %fs\n",
+                 (float) (s->timer - s->tone_on_time)/SAMPLE_RATE + 0.55);
+    }    
+    s->tone_state = tone;
+    if (tone == MODEM_CONNECT_TONES_NONE)
+        front_end_step_complete(s);
+}
+/*- End of function --------------------------------------------------------*/
+
+static int non_ecm_get_bit(void *user_data)
+{
+    faxtester_state_t *s;
+    int bit;
+
+    s = (faxtester_state_t *) user_data;
+    if (s->image_bit_ptr == 0)
+    {
+        if (s->image_ptr >= s->image_len)
+        {
+            s->image_buffer = NULL;
+            return PUTBIT_END_OF_DATA;
+        }
+        s->image_bit_ptr = 8;
+        s->image_ptr++;
+    }
+    s->image_bit_ptr--;
+    bit = (s->image_buffer[s->image_ptr] >> (7 - s->image_bit_ptr)) & 0x01;
+    //printf("Rx bit - %d\n", bit);
+    return bit;
+}
+/*- End of function --------------------------------------------------------*/
+
+void faxtester_set_non_ecm_image_buffer(faxtester_state_t *s, const uint8_t *buf, int len)
+{
+    s->image_ptr = 0;
+    s->image_bit_ptr = 8;
+    s->image_len = len;
+    s->image_buffer = buf;
+}
+/*- End of function --------------------------------------------------------*/
+
+void faxtester_set_ecm_image_buffer(faxtester_state_t *s, const uint8_t *buf, int len, int frame_size, int crc_hit)
+{
+    s->ecm_frame_size = frame_size;
+    s->image_ptr = 0;
+    s->image_bit_ptr = 8;
+    s->image_len = len;
+    s->image_buffer = buf;
+    s->corrupt_crc = crc_hit;
+    /* Send the first frame */
+    hdlc_underflow_handler(s);
+}
+/*- End of function --------------------------------------------------------*/
+
+static void non_ecm_rx_status(void *user_data, int status)
+{
+    faxtester_state_t *s;
+
+    s = (faxtester_state_t *) user_data;
+    switch (status)
+    {
+    case PUTBIT_TRAINING_IN_PROGRESS:
+        break;
+    case PUTBIT_TRAINING_FAILED:
+        span_log(&s->logging, SPAN_LOG_FLOW, "Non-ECM carrier training failed\n");
+        s->modems.rx_trained = FALSE;
+        break;
+    case PUTBIT_TRAINING_SUCCEEDED:
+        /* The modem is now trained */
+        span_log(&s->logging, SPAN_LOG_FLOW, "Non-ECM carrier trained\n");
+        s->modems.rx_trained = TRUE;
+        break;
+    case PUTBIT_CARRIER_UP:
+        span_log(&s->logging, SPAN_LOG_FLOW, "Non-ECM carrier up\n");
+        s->modems.rx_signal_present = TRUE;
+        break;
+    case PUTBIT_CARRIER_DOWN:
+        span_log(&s->logging, SPAN_LOG_FLOW, "Non-ECM carrier down\n");
+        if (s->modems.rx_trained)
+        {
+            if (s->real_time_frame_handler)
+                s->real_time_frame_handler(s, s->real_time_frame_user_data, TRUE, NULL, 0);
+        }
+        s->modems.rx_signal_present = FALSE;
+        s->modems.rx_trained = FALSE;
+        break;
+    default:
+        span_log(&s->logging, SPAN_LOG_WARNING, "Unexpected non-ECM rx status - %d!\n", status);
+        break;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static void non_ecm_put_bit(void *user_data, int bit)
+{
+    faxtester_state_t *s;
+
+    if (bit < 0)
+    {
+        non_ecm_rx_status(user_data, bit);
+        return;
+    }
+    s = (faxtester_state_t *) user_data;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void hdlc_rx_status(void *user_data, int status)
+{
+    faxtester_state_t *s;
+
+    s = (faxtester_state_t *) user_data;
+    switch (status)
+    {
+    case PUTBIT_TRAINING_IN_PROGRESS:
+        break;
+    case PUTBIT_TRAINING_FAILED:
+        span_log(&s->logging, SPAN_LOG_FLOW, "HDLC carrier training failed\n");
+        s->modems.rx_trained = FALSE;
+        break;
+    case PUTBIT_TRAINING_SUCCEEDED:
+        /* The modem is now trained */
+        span_log(&s->logging, SPAN_LOG_FLOW, "HDLC carrier trained\n");
+        s->modems.rx_trained = TRUE;
+        break;
+    case PUTBIT_CARRIER_UP:
+        span_log(&s->logging, SPAN_LOG_FLOW, "HDLC carrier up\n");
+        s->modems.rx_signal_present = TRUE;
+        break;
+    case PUTBIT_CARRIER_DOWN:
+        span_log(&s->logging, SPAN_LOG_FLOW, "HDLC carrier down\n");
+        s->modems.rx_signal_present = FALSE;
+        s->modems.rx_trained = FALSE;
+        break;
+    case PUTBIT_FRAMING_OK:
+        span_log(&s->logging, SPAN_LOG_FLOW, "HDLC framing OK\n");
+        break;
+    case PUTBIT_ABORT:
+        /* Just ignore these */
+        break;
+    default:
+        span_log(&s->logging, SPAN_LOG_FLOW, "Unexpected HDLC special length - %d!\n", status);
+        break;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static void hdlc_accept(void *user_data, const uint8_t *msg, int len, int ok)
+{
+    faxtester_state_t *s;
+
+    if (len < 0)
+    {
+        hdlc_rx_status(user_data, len);
+        return;
+    }
+    s = (faxtester_state_t *) user_data;
+    if (s->real_time_frame_handler)
+        s->real_time_frame_handler(s, s->real_time_frame_user_data, TRUE, msg, len);
+}
+/*- End of function --------------------------------------------------------*/
+
+static int v17_v21_rx(void *user_data, const int16_t amp[], int len)
+{
+    faxtester_state_t *t;
+    fax_modems_state_t *s;
+
+    t = (faxtester_state_t *) user_data;
+    s = &t->modems;
+    v17_rx(&s->v17_rx, amp, len);
+    fsk_rx(&s->v21_rx, amp, len);
+    if (s->rx_trained)
+    {
+        /* The fast modem has trained, so we no longer need to run the slow
+           one in parallel. */
+        span_log(&t->logging, SPAN_LOG_FLOW, "Switching from V.17 + V.21 to V.17 (%.2fdBm0)\n", v17_rx_signal_power(&s->v17_rx));
+        s->rx_handler = (span_rx_handler_t *) &v17_rx;
+        s->rx_user_data = &s->v17_rx;
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int v27ter_v21_rx(void *user_data, const int16_t amp[], int len)
+{
+    faxtester_state_t *t;
+    fax_modems_state_t *s;
+
+    t = (faxtester_state_t *) user_data;
+    s = &t->modems;
+    v27ter_rx(&s->v27ter_rx, amp, len);
+    fsk_rx(&s->v21_rx, amp, len);
+    if (s->rx_trained)
+    {
+        /* The fast modem has trained, so we no longer need to run the slow
+           one in parallel. */
+        span_log(&t->logging, SPAN_LOG_FLOW, "Switching from V.27ter + V.21 to V.27ter (%.2fdBm0)\n", v27ter_rx_signal_power(&s->v27ter_rx));
+        s->rx_handler = (span_rx_handler_t *) &v27ter_rx;
+        s->rx_user_data = &s->v27ter_rx;
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int v29_v21_rx(void *user_data, const int16_t amp[], int len)
+{
+    faxtester_state_t *t;
+    fax_modems_state_t *s;
+
+    t = (faxtester_state_t *) user_data;
+    s = &t->modems;
+    v29_rx(&s->v29_rx, amp, len);
+    fsk_rx(&s->v21_rx, amp, len);
+    if (s->rx_trained)
+    {
+        /* The fast modem has trained, so we no longer need to run the slow
+           one in parallel. */
+        span_log(&t->logging, SPAN_LOG_FLOW, "Switching from V.29 + V.21 to V.29 (%.2fdBm0)\n", v29_rx_signal_power(&s->v29_rx));
+        s->rx_handler = (span_rx_handler_t *) &v29_rx;
+        s->rx_user_data = &s->v29_rx;
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int faxtester_rx(faxtester_state_t *s, int16_t *amp, int len)
+{
+    int i;
+
+    for (i = 0;  i < len;  i++)
+        amp[i] = dc_restore(&(s->modems.dc_restore), amp[i]);
+    s->modems.rx_handler(s->modems.rx_user_data, amp, len);
+    timer_update(s, len);
+    if (s->wait_for_silence)
+    {
+        if (!s->modems.rx_signal_present)
+        {
+            s->wait_for_silence = FALSE;
+            front_end_step_complete(s);
+        }
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int faxtester_tx(faxtester_state_t *s, int16_t *amp, int max_len)
+{
+    int len;
+    int required_len;
+    
+    required_len = max_len;
+    len = 0;
+    if (s->transmit)
+    {
+        while ((len += s->modems.tx_handler(s->modems.tx_user_data, amp + len, max_len - len)) < max_len)
+        {
+            /* Allow for a change of tx handler within a block */
+            front_end_step_complete(s);
+            if (!s->transmit)
+            {
+                if (s->modems.transmit_on_idle)
+                {
+                    /* Pad to the requested length with silence */
+                    memset(amp + len, 0, (max_len - len)*sizeof(int16_t));
+                    len = max_len;        
+                }
+                break;
+            }
+        }
+    }
+    else
+    {
+        if (s->modems.transmit_on_idle)
+        {
+            /* Pad to the requested length with silence */
+            memset(amp, 0, max_len*sizeof(int16_t));
+            len = max_len;        
+        }
+    }
+    return len;
+}
+/*- End of function --------------------------------------------------------*/
+
+void faxtest_set_rx_silence(faxtester_state_t *s)
+{
+    s->wait_for_silence = TRUE;
+}
+/*- End of function --------------------------------------------------------*/
+
+void faxtester_set_rx_type(void *user_data, int type, int bit_rate, int short_train, int use_hdlc)
+{
+    faxtester_state_t *s;
+    put_bit_func_t put_bit_func;
+    void *put_bit_user_data;
+    fax_modems_state_t *t;
+    int tone;
+
+    s = (faxtester_state_t *) user_data;
+    t = &s->modems;
+    span_log(&s->logging, SPAN_LOG_FLOW, "Set rx type %d\n", type);
+    if (s->current_rx_type == type)
+        return;
+    s->current_rx_type = type;
+    if (use_hdlc)
+    {
+        put_bit_func = (put_bit_func_t) hdlc_rx_put_bit;
+        put_bit_user_data = (void *) &t->hdlc_rx;
+        hdlc_rx_init(&t->hdlc_rx, FALSE, FALSE, HDLC_FRAMING_OK_THRESHOLD, hdlc_accept, s);
+    }
+    else
+    {
+        put_bit_func = non_ecm_put_bit;
+        put_bit_user_data = (void *) s;
+    }
+    switch (type)
+    {
+    case T30_MODEM_CED:
+    case T30_MODEM_CNG:
+        if (type == T30_MODEM_CED)
+            tone = MODEM_CONNECT_TONES_FAX_CED;
+        else
+            tone = MODEM_CONNECT_TONES_FAX_CNG;
+        modem_connect_tones_rx_init(&t->connect_rx,
+                                    tone,
+                                    tone_detected,
+                                    (void *) s);
+        t->rx_handler = (span_rx_handler_t *) &modem_connect_tones_rx;
+        t->rx_user_data = &t->connect_rx;
+        s->tone_state = MODEM_CONNECT_TONES_NONE;
+        break;
+    case T30_MODEM_V21:
+        if (s->flush_handler)
+            s->flush_handler(s, s->flush_user_data, 3);
+        fsk_rx_init(&t->v21_rx, &preset_fsk_specs[FSK_V21CH2], TRUE, (put_bit_func_t) hdlc_rx_put_bit, put_bit_user_data);
+        fsk_rx_signal_cutoff(&t->v21_rx, -45.5);
+        t->rx_handler = (span_rx_handler_t *) &fsk_rx;
+        t->rx_user_data = &t->v21_rx;
+        break;
+    case T30_MODEM_V27TER:
+        v27ter_rx_restart(&t->v27ter_rx, bit_rate, FALSE);
+        v27ter_rx_set_put_bit(&t->v27ter_rx, put_bit_func, put_bit_user_data);
+        t->rx_handler = (span_rx_handler_t *) &v27ter_v21_rx;
+        t->rx_user_data = s;
+        break;
+    case T30_MODEM_V29:
+        v29_rx_restart(&t->v29_rx, bit_rate, FALSE);
+        v29_rx_set_put_bit(&t->v29_rx, put_bit_func, put_bit_user_data);
+        t->rx_handler = (span_rx_handler_t *) &v29_v21_rx;
+        t->rx_user_data = s;
+        break;
+    case T30_MODEM_V17:
+        v17_rx_restart(&t->v17_rx, bit_rate, short_train);
+        v17_rx_set_put_bit(&t->v17_rx, put_bit_func, put_bit_user_data);
+        t->rx_handler = (span_rx_handler_t *) &v17_v21_rx;
+        t->rx_user_data = s;
+        break;
+    case T30_MODEM_DONE:
+        span_log(&s->logging, SPAN_LOG_FLOW, "FAX exchange complete\n");
+    default:
+        t->rx_handler = (span_rx_handler_t *) &span_dummy_rx;
+        t->rx_user_data = s;
+        break;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+void faxtester_set_tx_type(void *user_data, int type, int bit_rate, int short_train, int use_hdlc)
+{
+    faxtester_state_t *s;
+    get_bit_func_t get_bit_func;
+    void *get_bit_user_data;
+    fax_modems_state_t *t;
+    int tone;
+
+    s = (faxtester_state_t *) user_data;
+    t = &s->modems;
+    span_log(&s->logging, SPAN_LOG_FLOW, "Set tx type %d\n", type);
+    if (s->current_tx_type == type)
+        return;
+    if (use_hdlc)
+    {
+        get_bit_func = (get_bit_func_t) hdlc_tx_get_bit;
+        get_bit_user_data = (void *) &t->hdlc_tx;
+    }
+    else
+    {
+        get_bit_func = non_ecm_get_bit;
+        get_bit_user_data = (void *) s;
+    }
+    switch (type)
+    {
+    case T30_MODEM_PAUSE:
+        silence_gen_alter(&t->silence_gen, ms_to_samples(short_train));
+        t->tx_handler = (span_tx_handler_t *) &silence_gen;
+        t->tx_user_data = &t->silence_gen;
+        s->transmit = TRUE;
+        break;
+    case T30_MODEM_CED:
+    case T30_MODEM_CNG:
+        if (type == T30_MODEM_CED)
+            tone = MODEM_CONNECT_TONES_FAX_CED;
+        else
+            tone = MODEM_CONNECT_TONES_FAX_CNG;
+        modem_connect_tones_tx_init(&t->connect_tx, tone);
+        t->tx_handler = (span_tx_handler_t *) &modem_connect_tones_tx;
+        t->tx_user_data = &t->connect_tx;
+        s->transmit = TRUE;
+        break;
+    case T30_MODEM_V21:
+        fsk_tx_init(&t->v21_tx, &preset_fsk_specs[FSK_V21CH2], get_bit_func, get_bit_user_data);
+        fsk_tx_set_modem_status_handler(&t->v21_tx, modem_tx_status, (void *) s);
+        t->tx_handler = (span_tx_handler_t *) &fsk_tx;
+        t->tx_user_data = &t->v21_tx;
+        s->transmit = TRUE;
+        break;
+    case T30_MODEM_V27TER:
+        v27ter_tx_restart(&t->v27ter_tx, bit_rate, t->use_tep);
+        v27ter_tx_set_get_bit(&t->v27ter_tx, get_bit_func, get_bit_user_data);
+        v27ter_tx_set_modem_status_handler(&t->v27ter_tx, modem_tx_status, (void *) s);
+        t->tx_handler = (span_tx_handler_t *) &v27ter_tx;
+        t->tx_user_data = &t->v27ter_tx;
+        /* For any fast modem, set 200ms of preamble flags */
+        hdlc_tx_flags(&t->hdlc_tx, bit_rate/(8*5));
+        s->transmit = TRUE;
+        break;
+    case T30_MODEM_V29:
+        v29_tx_restart(&t->v29_tx, bit_rate, t->use_tep);
+        v29_tx_set_get_bit(&t->v29_tx, get_bit_func, get_bit_user_data);
+        v29_tx_set_modem_status_handler(&t->v29_tx, modem_tx_status, (void *) s);
+        t->tx_handler = (span_tx_handler_t *) &v29_tx;
+        t->tx_user_data = &t->v29_tx;
+        /* For any fast modem, set 200ms of preamble flags */
+        hdlc_tx_flags(&t->hdlc_tx, bit_rate/(8*5));
+        s->transmit = TRUE;
+        break;
+    case T30_MODEM_V17:
+        v17_tx_restart(&t->v17_tx, bit_rate, t->use_tep, short_train);
+        v17_tx_set_get_bit(&t->v17_tx, get_bit_func, get_bit_user_data);
+        v17_tx_set_modem_status_handler(&t->v17_tx, modem_tx_status, (void *) s);
+        t->tx_handler = (span_tx_handler_t *) &v17_tx;
+        t->tx_user_data = &t->v17_tx;
+        /* For any fast modem, set 200ms of preamble flags */
+        hdlc_tx_flags(&t->hdlc_tx, bit_rate/(8*5));
+        s->transmit = TRUE;
+        break;
+    case T30_MODEM_DONE:
+        span_log(&s->logging, SPAN_LOG_FLOW, "FAX exchange complete\n");
+        /* Fall through */
+    default:
+        silence_gen_alter(&t->silence_gen, 0);
+        t->tx_handler = (span_tx_handler_t *) &silence_gen;
+        t->tx_user_data = &t->silence_gen;
+        s->transmit = FALSE;
+        break;
+    }
+    s->current_tx_type = type;
+}
+/*- End of function --------------------------------------------------------*/
+
+void faxtester_set_timeout(faxtester_state_t *s, int timeout)
+{
+    if (timeout >= 0)
+        s->timeout = s->timer + timeout*SAMPLE_RATE/1000;
+    else
+        s->timeout = 0x7FFFFFFFFFFFFFFFLL;
+}
+/*- End of function --------------------------------------------------------*/
+
+void faxtester_set_transmit_on_idle(faxtester_state_t *s, int transmit_on_idle)
+{
+    s->modems.transmit_on_idle = transmit_on_idle;
+}
+/*- End of function --------------------------------------------------------*/
+
+void faxtester_set_tep_mode(faxtester_state_t *s, int use_tep)
+{
+    s->modems.use_tep = use_tep;
+}
+/*- End of function --------------------------------------------------------*/
+
+void faxtester_set_real_time_frame_handler(faxtester_state_t *s, faxtester_real_time_frame_handler_t *handler, void *user_data)
+{
+    s->real_time_frame_handler = handler;
+    s->real_time_frame_user_data = user_data;
+}
+/*- End of function --------------------------------------------------------*/
+
+void faxtester_set_front_end_step_complete_handler(faxtester_state_t *s, faxtester_front_end_step_complete_handler_t *handler, void *user_data)
+{
+    s->front_end_step_complete_handler = handler;
+    s->front_end_step_complete_user_data = user_data;
+}
+/*- End of function --------------------------------------------------------*/
+
+void faxtester_set_front_end_step_timeout_handler(faxtester_state_t *s, faxtester_front_end_step_complete_handler_t *handler, void *user_data)
+{
+    s->front_end_step_timeout_handler = handler;
+    s->front_end_step_timeout_user_data = user_data;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void faxtester_fax_modems_init(fax_modems_state_t *s, int use_tep, void *user_data)
+{
+    s->use_tep = use_tep;
+
+    hdlc_rx_init(&s->hdlc_rx, FALSE, FALSE, HDLC_FRAMING_OK_THRESHOLD, hdlc_accept, user_data);
+    hdlc_tx_init(&s->hdlc_tx, FALSE, 2, FALSE, hdlc_underflow_handler, user_data);
+    fsk_rx_init(&s->v21_rx, &preset_fsk_specs[FSK_V21CH2], TRUE, (put_bit_func_t) hdlc_rx_put_bit, &s->hdlc_rx);
+    fsk_rx_signal_cutoff(&s->v21_rx, -45.5);
+    fsk_tx_init(&s->v21_tx, &preset_fsk_specs[FSK_V21CH2], (get_bit_func_t) hdlc_tx_get_bit, &s->hdlc_tx);
+    fsk_tx_set_modem_status_handler(&s->v21_tx, modem_tx_status, user_data);
+    v17_rx_init(&s->v17_rx, 14400, non_ecm_put_bit, user_data);
+    v17_tx_init(&s->v17_tx, 14400, s->use_tep, non_ecm_get_bit, user_data);
+    v17_tx_set_modem_status_handler(&s->v17_tx, modem_tx_status, user_data);
+    v29_rx_init(&s->v29_rx, 9600, non_ecm_put_bit, user_data);
+    v29_rx_signal_cutoff(&s->v29_rx, -45.5);
+    v29_tx_init(&s->v29_tx, 9600, s->use_tep, non_ecm_get_bit, user_data);
+    v29_tx_set_modem_status_handler(&s->v29_tx, modem_tx_status, user_data);
+    v27ter_rx_init(&s->v27ter_rx, 4800, non_ecm_put_bit, user_data);
+    v27ter_tx_init(&s->v27ter_tx, 4800, s->use_tep, non_ecm_get_bit, user_data);
+    v27ter_tx_set_modem_status_handler(&s->v27ter_tx, modem_tx_status, user_data);
+    silence_gen_init(&s->silence_gen, 0);
+    modem_connect_tones_tx_init(&s->connect_tx, MODEM_CONNECT_TONES_FAX_CNG);
+    modem_connect_tones_rx_init(&s->connect_rx,
+                                MODEM_CONNECT_TONES_FAX_CNG,
+                                tone_detected,
+                                user_data);
+    dc_restore_init(&s->dc_restore);
+
+    s->rx_signal_present = FALSE;
+    s->rx_handler = (span_rx_handler_t *) &span_dummy_rx;
+    s->rx_user_data = NULL;
+    s->tx_handler = (span_tx_handler_t *) &silence_gen;
+    s->tx_user_data = &s->silence_gen;
+}
+/*- End of function --------------------------------------------------------*/
+
+faxtester_state_t *faxtester_init(faxtester_state_t *s, int calling_party)
+{
+    if (s == NULL)
+    {
+        if ((s = (faxtester_state_t *) malloc(sizeof(*s))) == NULL)
+            return NULL;
+    }
+
+    memset(s, 0, sizeof(*s));
+    span_log_init(&s->logging, SPAN_LOG_NONE, NULL);
+    span_log_set_protocol(&s->logging, "TST");
+    faxtester_fax_modems_init(&s->modems, FALSE, s);
+    faxtester_set_timeout(s, -1);
+    faxtester_set_tx_type(s, T30_MODEM_NONE, 0, FALSE, FALSE);
+
+    return s;
+}
+/*- End of function --------------------------------------------------------*/
+
+int faxtester_release(faxtester_state_t *s)
+{
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int faxtester_free(faxtester_state_t *s)
+{
+    free(s);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+void faxtester_set_flush_handler(faxtester_state_t *s, faxtester_flush_handler_t *handler, void *user_data)
+{
+    s->flush_handler = handler;
+    s->flush_user_data = user_data;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/tests/fax_tester.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/tests/fax_tester.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,210 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * fax_tester.h
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2008 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: fax_tester.h,v 1.8 2008/08/13 00:11:30 steveu Exp $
+ */
+
+/*! \file */
+
+#if !defined(_SPANDSP_FAX_TESTER_H_)
+#define _SPANDSP_FAX_TESTER_H_
+
+/*! \page fax_tester_page FAX over analogue modem handling
+
+\section fax_tester_page_sec_1 What does it do?
+
+\section fax_tester_page_sec_2 How does it work?
+*/
+
+typedef struct faxtester_state_s faxtester_state_t;
+
+typedef void (faxtester_flush_handler_t)(faxtester_state_t *s, void *user_data, int which);
+
+/*!
+    FAX tester real time frame handler.
+    \brief FAX tester real time frame handler.
+    \param s The FAX tester context.
+    \param user_data An opaque pointer.
+    \param direction TRUE for incoming, FALSE for outgoing.
+    \param msg The HDLC message.
+    \param len The length of the message.
+*/
+typedef void (faxtester_real_time_frame_handler_t)(faxtester_state_t *s,
+                                                   void *user_data,
+                                                   int direction,
+                                                   const uint8_t *msg,
+                                                   int len);
+
+typedef void (faxtester_front_end_step_complete_handler_t)(faxtester_state_t *s, void *user_data);
+
+/*!
+    FAX tester descriptor.
+*/
+struct faxtester_state_s
+{
+    /*! \brief Pointer to our current step in the test. */
+    xmlNodePtr cur;
+
+    faxtester_flush_handler_t *flush_handler;
+    void *flush_user_data;
+
+    /*! \brief A pointer to a callback routine to be called when frames are
+        exchanged. */
+    faxtester_real_time_frame_handler_t *real_time_frame_handler;
+    /*! \brief An opaque pointer supplied in real time frame callbacks. */
+    void *real_time_frame_user_data;
+
+    faxtester_front_end_step_complete_handler_t *front_end_step_complete_handler;
+    void *front_end_step_complete_user_data;
+
+    faxtester_front_end_step_complete_handler_t *front_end_step_timeout_handler;
+    void *front_end_step_timeout_user_data;
+
+    const uint8_t *image_buffer;
+    int image_len;
+    int image_ptr;
+    int image_bit_ptr;
+    
+    int ecm_frame_size;
+    int corrupt_crc;
+    
+    int final_delayed;
+
+    fax_modems_state_t modems;
+    
+    /*! If TRUE, transmission is in progress */
+    int transmit;
+
+    /*! \brief TRUE is the short training sequence should be used. */
+    int short_train;
+
+    /*! \brief The currently select receiver type */
+    int current_rx_type;
+    /*! \brief The currently select transmitter type */
+    int current_tx_type;
+
+    int wait_for_silence;
+    
+    int tone_state;
+    int64_t tone_on_time;
+
+    int64_t timer;
+    int64_t timeout;
+
+    /*! \brief Error and flow logging control */
+    logging_state_t logging;
+};
+
+#if defined(__cplusplus)
+extern "C"
+{
+#endif
+
+/*! Apply T.30 receive processing to a block of audio samples.
+    \brief Apply T.30 receive processing to a block of audio samples.
+    \param s The FAX tester context.
+    \param amp The audio sample buffer.
+    \param len The number of samples in the buffer.
+    \return The number of samples unprocessed. This should only be non-zero if
+            the software has reached the end of the FAX call.
+*/
+int faxtester_rx(faxtester_state_t *s, int16_t *amp, int len);
+
+/*! Apply T.30 transmit processing to generate a block of audio samples.
+    \brief Apply T.30 transmit processing to generate a block of audio samples.
+    \param s The FAX tester context.
+    \param amp The audio sample buffer.
+    \param max_len The number of samples to be generated.
+    \return The number of samples actually generated. This will be zero when
+            there is nothing to send.
+*/
+int faxtester_tx(faxtester_state_t *s, int16_t *amp, int max_len);
+
+void faxtester_set_tx_type(void *user_data, int type, int bit_rate, int short_train, int use_hdlc);
+
+void faxtester_set_rx_type(void *user_data, int type, int bit_rate, int short_train, int use_hdlc);
+
+void faxtest_set_rx_silence(faxtester_state_t *s);
+
+void faxtester_send_hdlc_flags(faxtester_state_t *s, int flags);
+
+void faxtester_send_hdlc_msg(faxtester_state_t *s, const uint8_t *msg, int len, int crc_ok);
+
+void faxtester_set_flush_handler(faxtester_state_t *s, faxtester_flush_handler_t *handler, void *user_data);
+
+/*! Select whether silent audio will be sent when FAX transmit is idle.
+    \brief Select whether silent audio will be sent when FAX transmit is idle.
+    \param s The FAX tester context.
+    \param transmit_on_idle TRUE if silent audio should be output when the FAX transmitter is
+           idle. FALSE to transmit zero length audio when the FAX transmitter is idle. The default
+           behaviour is FALSE.
+*/
+void faxtester_set_transmit_on_idle(faxtester_state_t *s, int transmit_on_idle);
+
+/*! Select whether talker echo protection tone will be sent for the image modems.
+    \brief Select whether TEP will be sent for the image modems.
+    \param s The FAX tester context.
+    \param use_tep TRUE if TEP should be sent.
+*/
+void faxtester_set_tep_mode(faxtester_state_t *s, int use_tep);
+
+void faxtester_set_real_time_frame_handler(faxtester_state_t *s, faxtester_real_time_frame_handler_t *handler, void *user_data);
+
+void faxtester_set_front_end_step_complete_handler(faxtester_state_t *s, faxtester_front_end_step_complete_handler_t *handler, void *user_data);
+
+void faxtester_set_front_end_step_timeout_handler(faxtester_state_t *s, faxtester_front_end_step_complete_handler_t *handler, void *user_data);
+
+void faxtester_set_timeout(faxtester_state_t *s, int timeout);
+
+void faxtester_set_non_ecm_image_buffer(faxtester_state_t *s, const uint8_t *buf, int len);
+
+void faxtester_set_ecm_image_buffer(faxtester_state_t *s, const uint8_t *buf, int len, int frame_size, int crc_hit);
+
+/*! Initialise a FAX context.
+    \brief Initialise a FAX context.
+    \param s The FAX tester context.
+    \param calling_party TRUE if the context is for a calling party. FALSE if the
+           context is for an answering party.
+    \return A pointer to the FAX context, or NULL if there was a problem.
+*/
+faxtester_state_t *faxtester_init(faxtester_state_t *s, int calling_party);
+
+/*! Release a FAX context.
+    \brief Release a FAX context.
+    \param s The FAX tester context.
+    \return 0 for OK, else -1. */
+int faxtester_release(faxtester_state_t *s);
+
+/*! Free a FAX context.
+    \brief Free a FAX context.
+    \param s The FAX tester context.
+    \return 0 for OK, else -1. */
+int faxtester_free(faxtester_state_t *s);
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/tests/fax_tests.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/tests/fax_tests.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,524 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * fax_tests.c
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2003 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: fax_tests.c,v 1.95 2008/08/29 09:28:13 steveu Exp $
+ */
+
+/*! \page fax_tests_page FAX tests
+\section fax_tests_page_sec_1 What does it do?
+\section fax_tests_page_sec_2 How does it work?
+*/
+
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <string.h>
+#include <assert.h>
+#include <audiofile.h>
+
+#include "spandsp.h"
+#include "spandsp-sim.h"
+
+#define SAMPLES_PER_CHUNK       160
+
+#define INPUT_TIFF_FILE_NAME    "../test-data/itu/fax/itutests.tif"
+
+#define OUTPUT_FILE_NAME_WAVE   "fax_tests.wav"
+
+#define FAX_MACHINES            2
+
+struct machine_s
+{
+    int chan;
+    int16_t amp[SAMPLES_PER_CHUNK];
+    int len;
+    fax_state_t fax;
+    int done;
+    int succeeded;
+    char tag[50];
+    int error_delay;
+    int total_audio_time;
+} machines[FAX_MACHINES];
+
+int use_receiver_not_ready = FALSE;
+int test_local_interrupt = FALSE;
+int t30_state_to_wreck = -1;
+
+static int phase_b_handler(t30_state_t *s, void *user_data, int result)
+{
+    int i;
+    const char *u;
+    
+    i = (intptr_t) user_data;
+    if ((u = t30_get_rx_ident(s)))
+        printf("%d: Phase B: remote ident '%s'\n", i, u);
+    if ((u = t30_get_rx_sub_address(s)))
+        printf("%d: Phase B: remote sub-address '%s'\n", i, u);
+    if ((u = t30_get_rx_polled_sub_address(s)))
+        printf("%d: Phase B: remote polled sub-address '%s'\n", i, u);
+    if ((u = t30_get_rx_selective_polling_address(s)))
+        printf("%d: Phase B: remote selective polling address '%s'\n", i, u);
+    if ((u = t30_get_rx_sender_ident(s)))
+        printf("%d: Phase B: remote sender ident '%s'\n", i, u);
+    if ((u = t30_get_rx_password(s)))
+        printf("%d: Phase B: remote password '%s'\n", i, u);
+    printf("%d: Phase B handler on channel %d - (0x%X) %s\n", i, i, result, t30_frametype(result));
+    return T30_ERR_OK;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int phase_d_handler(t30_state_t *s, void *user_data, int result)
+{
+    int i;
+    t30_stats_t t;
+    const char *u;
+
+    i = (intptr_t) user_data;
+    t30_get_transfer_statistics(s, &t);
+
+    printf("%d: Phase D handler on channel %d - (0x%X) %s\n", i, i, result, t30_frametype(result));
+    printf("%d: Phase D: bit rate %d\n", i, t.bit_rate);
+    printf("%d: Phase D: ECM %s\n", i, (t.error_correcting_mode)  ?  "on"  :  "off");
+    printf("%d: Phase D: pages transferred %d\n", i, t.pages_transferred);
+    printf("%d: Phase D: pages in the file %d\n", i, t.pages_in_file);
+    printf("%d: Phase D: image size %d x %d\n", i, t.width, t.length);
+    printf("%d: Phase D: image resolution %d x %d\n", i, t.x_resolution, t.y_resolution);
+    printf("%d: Phase D: bad rows %d\n", i, t.bad_rows);
+    printf("%d: Phase D: longest bad row run %d\n", i, t.longest_bad_row_run);
+    printf("%d: Phase D: compression type %d\n", i, t.encoding);
+    printf("%d: Phase D: image size %d bytes\n", i, t.image_size);
+    if ((u = t30_get_tx_ident(s)))
+        printf("%d: Phase D: local ident '%s'\n", i, u);
+    if ((u = t30_get_rx_ident(s)))
+        printf("%d: Phase D: remote ident '%s'\n", i, u);
+    printf("%d: Phase D: bits per row - min %d, max %d\n", i, s->t4.min_row_bits, s->t4.max_row_bits);
+
+    if (use_receiver_not_ready)
+        t30_set_receiver_not_ready(s, 3);
+
+    if (test_local_interrupt)
+    {
+        if (i == 0)
+        {
+            printf("%d: Initiating interrupt request\n", i);
+            t30_local_interrupt_request(s, TRUE);
+        }
+        else
+        {
+            switch (result)
+            {
+            case T30_PIP:
+            case T30_PRI_MPS:
+            case T30_PRI_EOM:
+            case T30_PRI_EOP:
+                printf("%d: Accepting interrupt request\n", i);
+                t30_local_interrupt_request(s, TRUE);
+                break;
+            case T30_PIN:
+                break;
+            }
+        }
+    }
+    return T30_ERR_OK;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void phase_e_handler(t30_state_t *s, void *user_data, int result)
+{
+    int i;
+    t30_stats_t t;
+    const char *u;
+    
+    i = (intptr_t) user_data;
+    printf("%d: Phase E handler on channel %d - (%d) %s\n", i, i, result, t30_completion_code_to_str(result));    
+    t30_get_transfer_statistics(s, &t);
+    printf("%d: Phase E: bit rate %d\n", i, t.bit_rate);
+    printf("%d: Phase E: ECM %s\n", i, (t.error_correcting_mode)  ?  "on"  :  "off");
+    printf("%d: Phase E: pages transferred %d\n", i, t.pages_transferred);
+    printf("%d: Phase E: pages in the file %d\n", i, t.pages_in_file);
+    printf("%d: Phase E: image size %d x %d\n", i, t.width, t.length);
+    printf("%d: Phase E: image resolution %d x %d\n", i, t.x_resolution, t.y_resolution);
+    printf("%d: Phase E: bad rows %d\n", i, t.bad_rows);
+    printf("%d: Phase E: longest bad row run %d\n", i, t.longest_bad_row_run);
+    printf("%d: Phase E: coding method %s\n", i, t4_encoding_to_str(t.encoding));
+    printf("%d: Phase E: image size %d bytes\n", i, t.image_size);
+    //printf("%d: Phase E: local ident '%s'\n", i, info->ident);
+    if ((u = t30_get_rx_ident(s)))
+        printf("%d: Phase E: remote ident '%s'\n", i, u);
+    if ((u = t30_get_rx_country(s)))
+        printf("%d: Phase E: Remote was made in '%s'\n", i, u);
+    if ((u = t30_get_rx_vendor(s)))
+        printf("%d: Phase E: Remote was made by '%s'\n", i, u);
+    if ((u = t30_get_rx_model(s)))
+        printf("%d: Phase E: Remote is model '%s'\n", i, u);
+    machines[i].succeeded = (result == T30_ERR_OK)  &&  (t.pages_transferred == 12);
+    machines[i].done = TRUE;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void real_time_frame_handler(t30_state_t *s,
+                                    void *user_data,
+                                    int direction,
+                                    const uint8_t *msg,
+                                    int len)
+{
+    int i;
+    
+    i = (intptr_t) user_data;
+    printf("%d: Real time frame handler on channel %d - %s, %s, length = %d\n",
+           i,
+           i,
+           (direction)  ?  "line->T.30"  : "T.30->line",
+           t30_frametype(msg[2]),
+           len);
+}
+/*- End of function --------------------------------------------------------*/
+
+static int document_handler(t30_state_t *s, void *user_data, int event)
+{
+    int i;
+    
+    i = (intptr_t) user_data;
+    printf("%d: Document handler on channel %d - event %d\n", i, i, event);
+    return FALSE;
+}
+/*- End of function --------------------------------------------------------*/
+
+int main(int argc, char *argv[])
+{
+    AFfilehandle wave_handle;
+    AFfilehandle input_wave_handle;
+    int i;
+    int j;
+    int k;
+    struct machine_s *mc;
+    int outframes;
+    char buf[128 + 1];
+    int16_t silence[SAMPLES_PER_CHUNK];
+    int16_t out_amp[2*SAMPLES_PER_CHUNK];
+    int alldone;
+    const char *input_tiff_file_name;
+    const char *input_audio_file_name;
+    int log_audio;
+    int use_ecm;
+    int use_tep;
+    int use_transmit_on_idle;
+    int use_line_hits;
+    int polled_mode;
+    int reverse_flow;
+    int use_page_limits;
+    int supported_modems;
+    time_t start_time;
+    time_t end_time;
+    char *page_header_info;
+    int opt;
+    t30_state_t *t30;
+
+    log_audio = FALSE;
+    input_tiff_file_name = INPUT_TIFF_FILE_NAME;
+    input_audio_file_name = NULL;
+    use_ecm = FALSE;
+    use_line_hits = FALSE;
+    use_tep = FALSE;
+    polled_mode = FALSE;
+    page_header_info = NULL;
+    reverse_flow = FALSE;
+    use_transmit_on_idle = TRUE;
+    use_receiver_not_ready = FALSE;
+    use_page_limits = FALSE;
+    supported_modems = T30_SUPPORT_V27TER | T30_SUPPORT_V29 | T30_SUPPORT_V17;
+    while ((opt = getopt(argc, argv, "ehH:i:I:lm:prRtTw:")) != -1)
+    {
+        switch (opt)
+        {
+        case 'e':
+            use_ecm = TRUE;
+            break;
+        case 'h':
+            use_line_hits = TRUE;
+            break;
+        case 'H':
+            page_header_info = optarg;
+            break;
+        case 'i':
+            input_tiff_file_name = optarg;
+            break;
+        case 'I':
+            input_audio_file_name = optarg;
+            break;
+        case 'l':
+            log_audio = TRUE;
+            break;
+        case 'm':
+            supported_modems = atoi(optarg);
+            break;
+        case 'p':
+            polled_mode = TRUE;
+            break;
+        case 'r':
+            reverse_flow = TRUE;
+            break;
+        case 'R':
+            use_receiver_not_ready = TRUE;
+            break;
+        case 't':
+            use_tep = TRUE;
+            break;
+        case 'T':
+            use_page_limits = TRUE;
+            break;
+        case 'w':
+            t30_state_to_wreck = atoi(optarg);
+            break;
+        default:
+            //usage();
+            exit(2);
+            break;
+        }
+    }
+
+    input_wave_handle = AF_NULL_FILEHANDLE;
+    if (input_audio_file_name)
+    {
+        if ((input_wave_handle = afOpenFile_telephony_read(input_audio_file_name, 1)) == AF_NULL_FILEHANDLE)
+        {
+            fprintf(stderr, "    Cannot open wave file '%s'\n", input_audio_file_name);
+            exit(2);
+        }
+    }
+
+    wave_handle = AF_NULL_FILEHANDLE;
+    if (log_audio)
+    {
+        if ((wave_handle = afOpenFile_telephony_write(OUTPUT_FILE_NAME_WAVE, 2)) == AF_NULL_FILEHANDLE)
+        {
+            fprintf(stderr, "    Cannot create wave file '%s'\n", OUTPUT_FILE_NAME_WAVE);
+            exit(2);
+        }
+    }
+
+    memset(silence, 0, sizeof(silence));
+    for (j = 0;  j < FAX_MACHINES;  j++)
+    {
+        machines[j].chan = j;
+        mc = &machines[j];
+
+        i = mc->chan + 1;
+        sprintf(buf, "%d%d%d%d%d%d%d%d", i, i, i, i, i, i, i, i);
+        if (reverse_flow)
+            fax_init(&mc->fax, (mc->chan & 1)  ?  TRUE  :  FALSE);
+        else
+            fax_init(&mc->fax, (mc->chan & 1)  ?  FALSE  :  TRUE);
+        fax_set_transmit_on_idle(&mc->fax, use_transmit_on_idle);
+        fax_set_tep_mode(&mc->fax, use_tep);
+        t30 = fax_get_t30_state(&mc->fax);
+        t30_set_tx_ident(t30, buf);
+        t30_set_tx_sub_address(t30, "Sub-address");
+        t30_set_tx_sender_ident(t30, "Sender ID");
+        t30_set_tx_password(t30, "Password");
+        t30_set_tx_polled_sub_address(t30, "Polled sub-address");
+        t30_set_tx_selective_polling_address(t30, "Selective polling address");
+        t30_set_tx_page_header_info(t30, page_header_info);
+        t30_set_tx_nsf(t30, (const uint8_t *) "\x50\x00\x00\x00Spandsp\x00", 12);
+        t30_set_ecm_capability(t30, use_ecm);
+        t30_set_supported_t30_features(t30,
+                                       T30_SUPPORT_IDENTIFICATION
+                                     | T30_SUPPORT_SELECTIVE_POLLING
+                                     | T30_SUPPORT_SUB_ADDRESSING);
+
+        if ((mc->chan & 1))
+            t30_set_minimum_scan_line_time(t30, 40);
+        t30_set_supported_image_sizes(t30,
+                                      T30_SUPPORT_US_LETTER_LENGTH
+                                    | T30_SUPPORT_US_LEGAL_LENGTH
+                                    | T30_SUPPORT_UNLIMITED_LENGTH
+                                    | T30_SUPPORT_215MM_WIDTH
+                                    | T30_SUPPORT_255MM_WIDTH
+                                    | T30_SUPPORT_303MM_WIDTH);
+        t30_set_supported_resolutions(t30,
+                                      T30_SUPPORT_STANDARD_RESOLUTION
+                                    | T30_SUPPORT_FINE_RESOLUTION
+                                    | T30_SUPPORT_SUPERFINE_RESOLUTION
+                                    | T30_SUPPORT_R8_RESOLUTION
+                                    | T30_SUPPORT_R16_RESOLUTION
+                                    | T30_SUPPORT_300_300_RESOLUTION
+                                    | T30_SUPPORT_400_400_RESOLUTION
+                                    | T30_SUPPORT_600_600_RESOLUTION
+                                    | T30_SUPPORT_1200_1200_RESOLUTION
+                                    | T30_SUPPORT_300_600_RESOLUTION
+                                    | T30_SUPPORT_400_800_RESOLUTION
+                                    | T30_SUPPORT_600_1200_RESOLUTION);
+        t30_set_supported_modems(t30, supported_modems);
+        if (use_ecm)
+            t30_set_supported_compressions(t30, T30_SUPPORT_T4_1D_COMPRESSION | T30_SUPPORT_T4_2D_COMPRESSION | T30_SUPPORT_T6_COMPRESSION);
+        if ((mc->chan & 1))
+        {
+            if (polled_mode)
+            {
+                if (use_page_limits)
+                    t30_set_tx_file(t30, input_tiff_file_name, 3, 6);
+                else
+                    t30_set_tx_file(t30, input_tiff_file_name, -1, -1);
+            }
+            else
+            {
+                sprintf(buf, "fax_tests_%d.tif", (mc->chan + 1)/2);
+                t30_set_rx_file(t30, buf, -1);
+            }
+        }
+        else
+        {
+            if (polled_mode)
+            {
+                sprintf(buf, "fax_tests_%d.tif", (mc->chan + 1)/2);
+                t30_set_rx_file(t30, buf, -1);
+            }
+            else
+            {
+                if (use_page_limits)
+                    t30_set_tx_file(t30, input_tiff_file_name, 3, 6);
+                else
+                    t30_set_tx_file(t30, input_tiff_file_name, -1, -1);
+            }
+        }
+        t30_set_phase_b_handler(t30, phase_b_handler, (void *) (intptr_t) mc->chan);
+        t30_set_phase_d_handler(t30, phase_d_handler, (void *) (intptr_t) mc->chan);
+        t30_set_phase_e_handler(t30, phase_e_handler, (void *) (intptr_t) mc->chan);
+        t30_set_real_time_frame_handler(t30, real_time_frame_handler, (void *) (intptr_t) mc->chan);
+        t30_set_document_handler(t30, document_handler, (void *) (intptr_t) mc->chan);
+        sprintf(mc->tag, "FAX-%d", j + 1);
+        span_log_set_level(&t30->logging, SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME | SPAN_LOG_FLOW);
+        span_log_set_tag(&t30->logging, mc->tag);
+        span_log_set_level(&mc->fax.modems.v29_rx.logging, SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME | SPAN_LOG_FLOW);
+        span_log_set_tag(&mc->fax.modems.v29_rx.logging, mc->tag);
+        span_log_set_level(&mc->fax.logging, SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME | SPAN_LOG_FLOW);
+        span_log_set_tag(&mc->fax.logging, mc->tag);
+        memset(mc->amp, 0, sizeof(mc->amp));
+        mc->total_audio_time = 0;
+        mc->done = FALSE;
+    }
+    time(&start_time);
+    for (;;)
+    {
+        alldone = TRUE;
+        for (j = 0;  j < FAX_MACHINES;  j++)
+        {
+            mc = &machines[j];
+
+            if ((j & 1) == 0  &&  input_audio_file_name)
+            {
+                mc->len = afReadFrames(input_wave_handle, AF_DEFAULT_TRACK, mc->amp, SAMPLES_PER_CHUNK);
+                if (mc->len == 0)
+                    break;
+            }
+            else
+            {
+                mc->len = fax_tx(&mc->fax, mc->amp, SAMPLES_PER_CHUNK);
+            }
+            mc->total_audio_time += SAMPLES_PER_CHUNK;
+            if (!use_transmit_on_idle)
+            {
+                /* The receive side always expects a full block of samples, but the
+                   transmit side may not be sending any when it doesn't need to. We
+                   may need to pad with some silence. */
+                if (mc->len < SAMPLES_PER_CHUNK)
+                {
+                    memset(mc->amp + mc->len, 0, sizeof(int16_t)*(SAMPLES_PER_CHUNK - mc->len));
+                    mc->len = SAMPLES_PER_CHUNK;
+                }
+            }
+            span_log_bump_samples(&mc->fax.t30.logging, mc->len);
+            span_log_bump_samples(&mc->fax.modems.v29_rx.logging, mc->len);
+            span_log_bump_samples(&mc->fax.logging, mc->len);
+
+            if (log_audio)
+            {
+                for (k = 0;  k < mc->len;  k++)
+                    out_amp[2*k + j] = mc->amp[k];
+            }
+            if (machines[j ^ 1].len < SAMPLES_PER_CHUNK)
+                memset(machines[j ^ 1].amp + machines[j ^ 1].len, 0, sizeof(int16_t)*(SAMPLES_PER_CHUNK - machines[j ^ 1].len));
+            if (use_line_hits)
+            {
+                /* TODO: This applies very crude line hits. improve it */
+                if (mc->fax.t30.state == 22)
+                {
+                    if (++mc->error_delay == 100)
+                    {
+                        fprintf(stderr, "HIT %d!\n", j);
+                        mc->error_delay = 0;
+                        for (k = 0;  k < 5;  k++)
+                            mc->amp[k] = 0;
+                    }
+                }    
+            }
+            if (mc->fax.t30.state == t30_state_to_wreck)
+                memset(machines[j ^ 1].amp, 0, sizeof(int16_t)*SAMPLES_PER_CHUNK);
+            if (fax_rx(&mc->fax, machines[j ^ 1].amp, SAMPLES_PER_CHUNK))
+                break;
+            if (!mc->done)
+                alldone = FALSE;
+        }
+
+        if (log_audio)
+        {
+            outframes = afWriteFrames(wave_handle, AF_DEFAULT_TRACK, out_amp, SAMPLES_PER_CHUNK);
+            if (outframes != SAMPLES_PER_CHUNK)
+                break;
+        }
+
+        if (alldone  ||  j < FAX_MACHINES)
+            break;
+    }
+    time(&end_time);
+    for (j = 0;  j < FAX_MACHINES;  j++)
+    {
+        mc = &machines[j];
+        fax_release(&mc->fax);
+    }
+    if (log_audio)
+    {
+        if (afCloseFile(wave_handle))
+        {
+            fprintf(stderr, "    Cannot close wave file '%s'\n", OUTPUT_FILE_NAME_WAVE);
+            exit(2);
+        }
+    }
+    if (input_audio_file_name)
+    {
+        if (afCloseFile(input_wave_handle))
+        {
+            fprintf(stderr, "    Cannot close wave file '%s'\n", input_audio_file_name);
+            exit(2);
+        }
+    }
+    printf("Total audio time = %ds (wall time %ds)\n", machines[0].total_audio_time/8000, (int) (end_time - start_time));
+    return  0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/tests/fax_tests.sh
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/tests/fax_tests.sh	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,206 @@
+#!/bin/bash
+#
+# spandsp fax tests
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU Lesser General Public License version 2.1,
+# as published by the Free Software Foundation.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this program; if not, write to the Free Software
+# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+#
+# $Id: fax_tests.sh,v 1.5 2008/03/30 18:33:31 steveu Exp $
+#
+
+run_fax_test()
+{
+    rm -f fax_tests_1.tif
+    echo -i ${FILE} ${OPTS}
+    ./fax_tests -i ${FILE} ${OPTS} >xyzzy 2>xyzzy2
+    RETVAL=$?
+    if [ $RETVAL != 0 ]
+    then
+        echo fax_tests failed!
+        exit $RETVAL
+    fi
+    # Now use tiffcmp to check the results. It will return non-zero if any page images differ. The -t
+    # option means the normal differences in tags will be ignored.
+    tiffcmp -t ${FILE} fax_tests_1.tif #>/dev/null
+    RETVAL=$?
+    if [ $RETVAL != 0 ]
+    then
+        echo fax_tests failed!
+        exit $RETVAL
+    fi
+    rm -f fax_tests_1.tif
+    echo tested ${FILE}
+}
+
+OPTS="-e"
+
+FILE="../itutests/fax/R8_385_A4.tif"
+run_fax_test
+
+FILE="../itutests/fax/R8_385_B4.tif"
+run_fax_test
+
+FILE="../itutests/fax/R8_385_A3.tif"
+run_fax_test
+
+
+FILE="../itutests/fax/R8_77_A4.tif"
+run_fax_test
+
+FILE="../itutests/fax/R8_77_B4.tif"
+run_fax_test
+
+FILE="../itutests/fax/R8_77_A3.tif"
+run_fax_test
+
+
+FILE="../itutests/fax/R8_154_A4.tif"
+run_fax_test
+
+FILE="../itutests/fax/R8_154_B4.tif"
+run_fax_test
+
+FILE="../itutests/fax/R8_154_A3.tif"
+run_fax_test
+
+
+FILE="../itutests/fax/R300_300_A4.tif"
+run_fax_test
+
+FILE="../itutests/fax/R300_300_B4.tif"
+run_fax_test
+
+FILE="../itutests/fax/R300_300_A3.tif"
+run_fax_test
+
+
+FILE="../itutests/fax/R300_600_A4.tif"
+run_fax_test
+
+FILE="../itutests/fax/R300_600_B4.tif"
+run_fax_test
+
+FILE="../itutests/fax/R300_600_A3.tif"
+run_fax_test
+
+
+FILE="../itutests/fax/R16_154_A4.tif"
+run_fax_test
+
+FILE="../itutests/fax/R16_154_B4.tif"
+run_fax_test
+
+FILE="../itutests/fax/R16_154_A3.tif"
+run_fax_test
+
+
+FILE="../itutests/fax/R16_800_A4.tif"
+run_fax_test
+
+FILE="../itutests/fax/R16_800_B4.tif"
+run_fax_test
+
+FILE="../itutests/fax/R16_800_A3.tif"
+run_fax_test
+
+
+FILE="../itutests/fax/R600_600_A4.tif"
+run_fax_test
+
+FILE="../itutests/fax/R600_600_B4.tif"
+run_fax_test
+
+FILE="../itutests/fax/R600_600_A3.tif"
+run_fax_test
+
+
+FILE="../itutests/fax/R600_1200_A4.tif"
+run_fax_test
+
+FILE="../itutests/fax/R600_1200_B4.tif"
+run_fax_test
+
+FILE="../itutests/fax/R600_1200_A3.tif"
+run_fax_test
+
+
+FILE="../itutests/fax/R1200_1200_A4.tif"
+run_fax_test
+
+FILE="../itutests/fax/R1200_1200_B4.tif"
+run_fax_test
+
+FILE="../itutests/fax/R1200_1200_A3.tif"
+run_fax_test
+
+
+OPTS=""
+
+FILE="../itutests/fax/R8_385_A4.tif"
+run_fax_test
+
+FILE="../itutests/fax/R8_385_B4.tif"
+run_fax_test
+
+FILE="../itutests/fax/R8_385_A3.tif"
+run_fax_test
+
+
+FILE="../itutests/fax/R8_77_A4.tif"
+run_fax_test
+
+FILE="../itutests/fax/R8_77_B4.tif"
+run_fax_test
+
+FILE="../itutests/fax/R8_77_A3.tif"
+run_fax_test
+
+
+FILE="../itutests/fax/R8_154_A4.tif"
+run_fax_test
+
+FILE="../itutests/fax/R8_154_B4.tif"
+run_fax_test
+
+FILE="../itutests/fax/R8_154_A3.tif"
+run_fax_test
+
+
+FILE="../itutests/fax/R16_385_A4.tif"
+#run_fax_test
+
+FILE="../itutests/fax/R16_385_B4.tif"
+#run_fax_test
+
+FILE="../itutests/fax/R16_385_A3.tif"
+#run_fax_test
+
+
+FILE="../itutests/fax/R16_77_A4.tif"
+#run_fax_test
+
+FILE="../itutests/fax/R16_77_B4.tif"
+#run_fax_test
+
+FILE="../itutests/fax/R16_77_A3.tif"
+#run_fax_test
+
+
+FILE="../itutests/fax/R16_154_A4.tif"
+run_fax_test
+
+FILE="../itutests/fax/R16_154_B4.tif"
+run_fax_test
+
+FILE="../itutests/fax/R16_154_A3.tif"
+run_fax_test

Added: freeswitch/trunk/libs/spandsp/tests/fsk_tests.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/tests/fsk_tests.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,565 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * fsk_tests.c - Tests for the low speed FSK modem code (V.21, V.23, etc.).
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2003 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: fsk_tests.c,v 1.48 2008/08/29 09:28:13 steveu Exp $
+ */
+
+/*! \page fsk_tests_page FSK modem tests
+\section fsk_tests_page_sec_1 What does it do?
+These tests allow either:
+
+ - An FSK transmit modem to feed an FSK receive modem, of the same type,
+   through a telephone line model. BER testing is then used to evaluate
+   performance under various line conditions. This is effective for testing
+   the basic performance of the receive modem. It is also the only test mode
+   provided for evaluating the transmit modem.
+
+ - An FSK receive modem is used to decode FSK audio, stored in a wave file.
+   This is good way to evaluate performance with audio recorded from other
+   models of modem, and with real world problematic telephone lines.
+
+\section fsk_tests_page_sec_2 How does it work?
+*/
+
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <string.h>
+#include <assert.h>
+#include <audiofile.h>
+
+#include "spandsp.h"
+#include "spandsp-sim.h"
+
+#define BLOCK_LEN           160
+
+#define OUTPUT_FILE_NAME    "fsk.wav"
+
+char *decode_test_file = NULL;
+both_ways_line_model_state_t *model;
+int rx_bits = 0;
+int cutoff_test_carrier = FALSE;
+
+static int rx_status(void *user_data, int status)
+{
+    printf("FSK rx status is %d\n", status);
+    switch (status)
+    {
+    case PUTBIT_TRAINING_FAILED:
+        printf("Training failed\n");
+        break;
+    case PUTBIT_TRAINING_SUCCEEDED:
+        printf("Training succeeded\n");
+        break;
+    case PUTBIT_CARRIER_UP:
+        printf("Carrier up\n");
+        break;
+    case PUTBIT_CARRIER_DOWN:
+        printf("Carrier down\n");
+        break;
+    default:
+        printf("Eh! - %d\n", status);
+        break;
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int tx_status(void *user_data, int status)
+{
+    switch (status)
+    {
+    case MODEM_TX_STATUS_DATA_EXHAUSTED:
+        printf("FSK tx data exhausted\n");
+        break;
+    case MODEM_TX_STATUS_SHUTDOWN_COMPLETE:
+        printf("FSK tx shutdown complete\n");
+        break;
+    default:
+        printf("FSK tx status is %d\n", status);
+        break;
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void put_bit(void *user_data, int bit)
+{
+    if (bit < 0)
+    {
+        rx_status(user_data, bit);
+        return;
+    }
+
+    printf("Rx bit %d - %d\n", rx_bits++, bit);
+}
+/*- End of function --------------------------------------------------------*/
+
+static int cutoff_test_rx_status(void *user_data, int status)
+{
+    printf("FSK rx status is %d\n", status);
+    switch (status)
+    {
+    case PUTBIT_TRAINING_FAILED:
+        printf("Training failed\n");
+        break;
+    case PUTBIT_TRAINING_SUCCEEDED:
+        printf("Training succeeded\n");
+        break;
+    case PUTBIT_CARRIER_UP:
+        //printf("Carrier up\n");
+        cutoff_test_carrier = TRUE;
+        break;
+    case PUTBIT_CARRIER_DOWN:
+        //printf("Carrier down\n");
+        cutoff_test_carrier = FALSE;
+        break;
+    default:
+        printf("Eh! - %d\n", status);
+        break;
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void cutoff_test_put_bit(void *user_data, int bit)
+{
+    if (bit < 0)
+    {
+        cutoff_test_rx_status(user_data, bit);
+        return;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static void reporter(void *user_data, int reason, bert_results_t *results)
+{
+    int channel;
+
+    channel = (int) (intptr_t) user_data;
+    switch (reason)
+    {
+    case BERT_REPORT_SYNCED:
+        printf("%d: BERT report synced\n", channel);
+        break;
+    case BERT_REPORT_UNSYNCED:
+        printf("%d: BERT report unsync'ed\n", channel);
+        break;
+    case BERT_REPORT_REGULAR:
+        printf("%d: BERT report regular - %d bits, %d bad bits, %d resyncs\n", channel, results->total_bits, results->bad_bits, results->resyncs);
+        break;
+    case BERT_REPORT_GT_10_2:
+        printf("%d: BERT report > 1 in 10^2\n", channel);
+        break;
+    case BERT_REPORT_LT_10_2:
+        printf("%d: BERT report < 1 in 10^2\n", channel);
+        break;
+    case BERT_REPORT_LT_10_3:
+        printf("%d: BERT report < 1 in 10^3\n", channel);
+        break;
+    case BERT_REPORT_LT_10_4:
+        printf("%d: BERT report < 1 in 10^4\n", channel);
+        break;
+    case BERT_REPORT_LT_10_5:
+        printf("%d: BERT report < 1 in 10^5\n", channel);
+        break;
+    case BERT_REPORT_LT_10_6:
+        printf("%d: BERT report < 1 in 10^6\n", channel);
+        break;
+    case BERT_REPORT_LT_10_7:
+        printf("%d: BERT report < 1 in 10^7\n", channel);
+        break;
+    default:
+        printf("%d: BERT report reason %d\n", channel, reason);
+        break;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+int main(int argc, char *argv[])
+{
+    fsk_tx_state_t caller_tx;
+    fsk_rx_state_t caller_rx;
+    fsk_tx_state_t answerer_tx;
+    fsk_rx_state_t answerer_rx;
+    bert_state_t caller_bert;
+    bert_state_t answerer_bert;
+    bert_results_t bert_results;
+    power_meter_t caller_meter;
+    power_meter_t answerer_meter;
+    int16_t caller_amp[BLOCK_LEN];
+    int16_t answerer_amp[BLOCK_LEN];
+    int16_t caller_model_amp[BLOCK_LEN];
+    int16_t answerer_model_amp[BLOCK_LEN];
+    int16_t out_amp[2*BLOCK_LEN];
+    AFfilehandle inhandle;
+    AFfilehandle outhandle;
+    int outframes;    
+    int i;
+    int j;
+    int samples;
+    int test_bps;
+    int noise_level;
+    int noise_sweep;
+    int bits_per_test;
+    int line_model_no;
+    int modem_under_test_1;
+    int modem_under_test_2;
+    int modems_set;
+    int log_audio;
+    int channel_codec;
+    int rbs_pattern;
+    int on_at;
+    int off_at;
+    tone_gen_descriptor_t tone_desc;
+    tone_gen_state_t tone_tx;
+    int opt;
+
+    channel_codec = MUNGE_CODEC_NONE;
+    rbs_pattern = 0;
+    line_model_no = 0;
+    decode_test_file = NULL;
+    noise_sweep = FALSE;
+    modem_under_test_1 = FSK_V21CH1;
+    modem_under_test_2 = FSK_V21CH2;
+    log_audio = FALSE;
+    modems_set = 0;
+    while ((opt = getopt(argc, argv, "c:dlm:nr:s:")) != -1)
+    {
+        switch (opt)
+        {
+        case 'c':
+            channel_codec = atoi(optarg);
+            break;
+        case 'd':
+            decode_test_file = optarg;
+            break;
+        case 'l':
+            log_audio = TRUE;
+            break;
+        case 'm':
+            line_model_no = atoi(optarg);
+            break;
+        case 'n':
+            noise_sweep = TRUE;
+            break;
+        case 'r':
+            rbs_pattern = atoi(optarg);
+            break;
+        case 's':
+            switch (modems_set++)
+            {
+            case 0:
+                modem_under_test_1 = atoi(optarg);
+                break;
+            case 1:
+                modem_under_test_2 = atoi(optarg);
+                break;
+            }
+            break;
+        default:
+            //usage();
+            exit(2);
+            break;
+        }
+    }
+
+    if (modem_under_test_1 >= 0)
+        printf("Modem channel 1 is '%s'\n", preset_fsk_specs[modem_under_test_1].name);
+    if (modem_under_test_2 >= 0)
+        printf("Modem channel 2 is '%s'\n", preset_fsk_specs[modem_under_test_2].name);
+
+    outhandle = AF_NULL_FILEHANDLE;
+
+    if (log_audio)
+    {
+        if ((outhandle = afOpenFile_telephony_write(OUTPUT_FILE_NAME, 2)) == AF_NULL_FILEHANDLE)
+        {
+            fprintf(stderr, "    Cannot create wave file '%s'\n", OUTPUT_FILE_NAME);
+            exit(2);
+        }
+    }
+    noise_level = -200;
+    bits_per_test = 0;
+    inhandle = NULL;
+
+    memset(caller_amp, 0, sizeof(*caller_amp));
+    memset(answerer_amp, 0, sizeof(*answerer_amp));
+    memset(caller_model_amp, 0, sizeof(*caller_model_amp));
+    memset(answerer_model_amp, 0, sizeof(*answerer_model_amp));
+    power_meter_init(&caller_meter, 7);
+    power_meter_init(&answerer_meter, 7);
+
+    if (decode_test_file)
+    {
+        if ((inhandle = afOpenFile_telephony_read(decode_test_file, 1)) == AF_NULL_FILEHANDLE)
+        {
+            fprintf(stderr, "    Cannot open wave file '%s'\n", decode_test_file);
+            exit(2);
+        }
+        fsk_rx_init(&caller_rx, &preset_fsk_specs[modem_under_test_1], TRUE, put_bit, NULL);
+        fsk_rx_set_modem_status_handler(&caller_rx, rx_status, (void *) &caller_rx);
+        test_bps = preset_fsk_specs[modem_under_test_1].baud_rate;
+
+        for (;;)
+        {
+            samples = afReadFrames(inhandle,
+                                   AF_DEFAULT_TRACK,
+                                   caller_model_amp,
+                                   BLOCK_LEN);
+            if (samples < BLOCK_LEN)
+                break;
+            for (i = 0;  i < samples;  i++)
+                power_meter_update(&caller_meter, caller_model_amp[i]);
+            fsk_rx(&caller_rx, caller_model_amp, samples);
+        }
+
+        if (afCloseFile(inhandle) != 0)
+        {
+            fprintf(stderr, "    Cannot close wave file '%s'\n", decode_test_file);
+            exit(2);
+        }
+    }
+    else
+    {
+        printf("Test cutoff level\n");
+        fsk_rx_init(&caller_rx, &preset_fsk_specs[modem_under_test_1], TRUE, cutoff_test_put_bit, NULL);
+        fsk_rx_signal_cutoff(&caller_rx, -30.0f);
+        fsk_rx_set_modem_status_handler(&caller_rx, cutoff_test_rx_status, (void *) &caller_rx);
+        on_at = 0;
+        for (i = -40;  i < -25;  i++)
+        {
+            make_tone_gen_descriptor(&tone_desc,
+                                     1500,
+                                     i,
+                                     0,
+                                     0,
+                                     1,
+                                     0,
+                                     0,
+                                     0,
+                                     TRUE);
+            tone_gen_init(&tone_tx, &tone_desc);
+            for (j = 0;  j < 10;  j++)
+            {
+                samples = tone_gen(&tone_tx, caller_model_amp, 160);
+                fsk_rx(&caller_rx, caller_model_amp, samples);
+            }
+            if (cutoff_test_carrier)
+               break;
+        }
+        on_at = i;
+        off_at = 0;
+        for (  ;  i > -40;  i--)
+        {
+            make_tone_gen_descriptor(&tone_desc,
+                                     1500,
+                                     i,
+                                     0,
+                                     0,
+                                     1,
+                                     0,
+                                     0,
+                                     0,
+                                     TRUE);
+            tone_gen_init(&tone_tx, &tone_desc);
+            for (j = 0;  j < 10;  j++)
+            {
+                samples = tone_gen(&tone_tx, caller_model_amp, 160);
+                fsk_rx(&caller_rx, caller_model_amp, samples);
+            }
+            if (!cutoff_test_carrier)
+                break;
+        }
+        off_at = i;
+        printf("Carrier on at %d, off at %d\n", on_at, off_at);
+        if (on_at < -29  ||  on_at > -26  
+            ||
+            off_at < -35  ||  off_at > -31)
+        {
+            printf("Tests failed.\n");
+            exit(2);
+        }
+                
+        printf("Test with BERT\n");
+        test_bps = preset_fsk_specs[modem_under_test_1].baud_rate;
+        if (modem_under_test_1 >= 0)
+        {
+            fsk_tx_init(&caller_tx, &preset_fsk_specs[modem_under_test_1], (get_bit_func_t) bert_get_bit, &caller_bert);
+            fsk_tx_set_modem_status_handler(&caller_tx, tx_status, (void *) &caller_tx);
+            fsk_rx_init(&answerer_rx, &preset_fsk_specs[modem_under_test_1], TRUE, (put_bit_func_t) bert_put_bit, &answerer_bert);
+            fsk_rx_set_modem_status_handler(&answerer_rx, rx_status, (void *) &answerer_rx);
+        }
+        if (modem_under_test_2 >= 0)
+        {
+            fsk_tx_init(&answerer_tx, &preset_fsk_specs[modem_under_test_2], (get_bit_func_t) bert_get_bit, &answerer_bert);
+            fsk_tx_set_modem_status_handler(&answerer_tx, tx_status, (void *) &answerer_tx);
+            fsk_rx_init(&caller_rx, &preset_fsk_specs[modem_under_test_2], TRUE, (put_bit_func_t) bert_put_bit, &caller_bert);
+            fsk_rx_set_modem_status_handler(&caller_rx, rx_status, (void *) &caller_rx);
+        }
+        test_bps = preset_fsk_specs[modem_under_test_1].baud_rate;
+
+        bits_per_test = 500000;
+        noise_level = -24;
+
+        bert_init(&caller_bert, bits_per_test, BERT_PATTERN_ITU_O152_11, test_bps, 20);
+        bert_set_report(&caller_bert, 100000, reporter, (void *) (intptr_t) 1);
+        bert_init(&answerer_bert, bits_per_test, BERT_PATTERN_ITU_O152_11, test_bps, 20);
+        bert_set_report(&answerer_bert, 100000, reporter, (void *) (intptr_t) 2);
+        if ((model = both_ways_line_model_init(line_model_no, (float) noise_level, line_model_no, noise_level, channel_codec, rbs_pattern)) == NULL)
+        {
+            fprintf(stderr, "    Failed to create line model\n");
+            exit(2);
+        }
+
+        for (;;)
+        {
+            samples = fsk_tx(&caller_tx, caller_amp, BLOCK_LEN);
+            for (i = 0;  i < samples;  i++)
+                power_meter_update(&caller_meter, caller_amp[i]);
+            samples = fsk_tx(&answerer_tx, answerer_amp, BLOCK_LEN);
+            for (i = 0;  i < samples;  i++)
+                power_meter_update(&answerer_meter, answerer_amp[i]);
+            both_ways_line_model(model,
+                                 caller_model_amp,
+                                 caller_amp,
+                                 answerer_model_amp,
+                                 answerer_amp,
+                                 samples);
+
+            //printf("Powers %10.5fdBm0 %10.5fdBm0\n", power_meter_current_dbm0(&caller_meter), power_meter_current_dbm0(&answerer_meter));
+
+            fsk_rx(&answerer_rx, caller_model_amp, samples);
+            for (i = 0;  i < samples;  i++)
+                out_amp[2*i] = caller_model_amp[i];
+            for (  ;  i < BLOCK_LEN;  i++)
+                out_amp[2*i] = 0;
+
+            fsk_rx(&caller_rx, answerer_model_amp, samples);
+            for (i = 0;  i < samples;  i++)
+                out_amp[2*i + 1] = answerer_model_amp[i];
+            for (  ;  i < BLOCK_LEN;  i++)
+                out_amp[2*i + 1] = 0;
+        
+            if (log_audio)
+            {
+                outframes = afWriteFrames(outhandle,
+                                          AF_DEFAULT_TRACK,
+                                          out_amp,
+                                          BLOCK_LEN);
+                if (outframes != BLOCK_LEN)
+                {
+                    fprintf(stderr, "    Error writing wave file\n");
+                    exit(2);
+                }
+            }
+
+            if (samples < BLOCK_LEN)
+            {
+                bert_result(&caller_bert, &bert_results);
+                fprintf(stderr, "%ddB AWGN, %d bits, %d bad bits, %d resyncs\n", noise_level, bert_results.total_bits, bert_results.bad_bits, bert_results.resyncs);
+                if (!noise_sweep)
+                {
+                    if (bert_results.total_bits != bits_per_test - 43
+                        ||
+                        bert_results.bad_bits != 0
+                        ||
+                        bert_results.resyncs != 0)
+                    {
+                        printf("Tests failed.\n");
+                        exit(2);
+                    }
+                }
+                bert_result(&answerer_bert, &bert_results);
+                fprintf(stderr, "%ddB AWGN, %d bits, %d bad bits, %d resyncs\n", noise_level, bert_results.total_bits, bert_results.bad_bits, bert_results.resyncs);
+                if (!noise_sweep)
+                {
+                    if (bert_results.total_bits != bits_per_test - 43
+                        ||
+                        bert_results.bad_bits != 0
+                        ||
+                        bert_results.resyncs != 0)
+                    {
+                        printf("Tests failed.\n");
+                        exit(2);
+                    }
+                    break;
+                }
+    
+                /* Put a little silence between the chunks in the file. */
+                memset(out_amp, 0, sizeof(out_amp));
+                if (log_audio)
+                {
+                    for (i = 0;  i < 200;  i++)
+                    {
+                        outframes = afWriteFrames(outhandle,
+                                                  AF_DEFAULT_TRACK,
+                                                  out_amp,
+                                                  BLOCK_LEN);
+                    }
+                }
+                if (modem_under_test_1 >= 0)
+                {
+                    fsk_tx_init(&caller_tx, &preset_fsk_specs[modem_under_test_1], (get_bit_func_t) bert_get_bit, &caller_bert);
+                    fsk_tx_set_modem_status_handler(&caller_tx, tx_status, (void *) &caller_tx);
+                    fsk_rx_init(&answerer_rx, &preset_fsk_specs[modem_under_test_1], TRUE, (put_bit_func_t) bert_put_bit, &answerer_bert);
+                    fsk_rx_set_modem_status_handler(&answerer_rx, rx_status, (void *) &answerer_rx);
+                }
+                if (modem_under_test_2 >= 0)
+                {
+                    fsk_tx_init(&answerer_tx, &preset_fsk_specs[modem_under_test_2], (get_bit_func_t) bert_get_bit, &answerer_bert);
+                    fsk_tx_set_modem_status_handler(&answerer_tx, tx_status, (void *) &answerer_tx);
+                    fsk_rx_init(&caller_rx, &preset_fsk_specs[modem_under_test_2], TRUE, (put_bit_func_t) bert_put_bit, &caller_bert);
+                    fsk_rx_set_modem_status_handler(&caller_rx, rx_status, (void *) &caller_rx);
+                }
+                noise_level++;
+                if ((model = both_ways_line_model_init(line_model_no, (float) noise_level, line_model_no, noise_level, channel_codec, 0)) == NULL)
+                {
+                    fprintf(stderr, "    Failed to create line model\n");
+                    exit(2);
+                }
+                bert_init(&caller_bert, bits_per_test, BERT_PATTERN_ITU_O152_11, test_bps, 20);
+                bert_set_report(&caller_bert, 100000, reporter, (void *) (intptr_t) 1);
+                bert_init(&answerer_bert, bits_per_test, BERT_PATTERN_ITU_O152_11, test_bps, 20);
+                bert_set_report(&answerer_bert, 100000, reporter, (void *) (intptr_t) 2);
+            }
+        }
+        printf("Tests passed.\n");
+    }
+    if (log_audio)
+    {
+        if (afCloseFile(outhandle) != 0)
+        {
+            fprintf(stderr, "    Cannot close wave file '%s'\n", OUTPUT_FILE_NAME);
+            exit(2);
+        }
+    }
+    return  0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/tests/g1050_tests.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/tests/g1050_tests.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,237 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * g1050_tests.c - Tests for the G.1050/TIA-921 model.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2007 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: g1050_tests.c,v 1.16 2008/05/13 13:17:25 steveu Exp $
+ */
+
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
+
+#if defined(HAVE_FL_FL_H)  &&  defined(HAVE_FL_FL_CARTESIAN_H)  &&  defined(HAVE_FL_FL_AUDIO_METER_H)
+#define ENABLE_GUI
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <string.h>
+#include <time.h>
+#include <audiofile.h>
+#if defined(HAVE_MATH_H)
+#define GEN_CONST
+#endif
+
+#include "spandsp.h"
+#include "spandsp-sim.h"
+
+#if defined(ENABLE_GUI)
+#include "media_monitor.h"
+#endif
+
+#define PACKET_SIZE                 256
+#define PACKET_INTERVAL             20
+#define SIMULATION_TIME             300
+
+#define MODEL_NO                    8
+#define SPEED_PATTERN_NO            133
+
+int main(int argc, char *argv[])
+{
+    g1050_state_t *s;
+    double *packet_arrival_times;
+    int packets_per_sec;
+    int num_packets;
+    int model_no;
+    int speed_pattern_no;
+    int use_gui;
+    int simulation_time;
+    int i;
+    int len;
+    uint8_t put_pkt[256];
+    uint8_t get_pkt[256];
+    int put_pkt_len;
+    int get_pkt_len;
+    int get_seq_no;
+    double get_departure_time;
+    double get_arrival_time;
+    int packets_put;
+    int packets_really_put;
+    int packets_got;
+    int oos_packets_got;
+    int missing_packets_got;
+    int highest_seq_no_got;
+    int opt;
+    FILE *out_file;
+
+    model_no = MODEL_NO;
+    speed_pattern_no = SPEED_PATTERN_NO;
+    simulation_time = SIMULATION_TIME;
+    use_gui = FALSE;
+    while ((opt = getopt(argc, argv, "gm:s:t:")) != -1)
+    {
+        switch (opt)
+        {
+        case 'g':
+#if defined(ENABLE_GUI)
+            use_gui = TRUE;
+#else
+            fprintf(stderr, "Graphical monitoring not available\n");
+            exit(2);
+#endif
+            break;
+        case 'm':
+            model_no = optarg[0] - 'A' + 1;
+            if (model_no < 0  ||  model_no > 8)
+            {
+                fprintf(stderr, "Bad model ID '%s'\n", optarg);
+                exit(2);
+            }
+            break;
+        case 's':
+            speed_pattern_no = atoi(optarg);
+            if (speed_pattern_no < 1  ||  speed_pattern_no > 133)
+            {
+                fprintf(stderr, "Bad link speed pattern %s\n", optarg);
+                exit(2);
+            }
+            break;
+        case 't':
+            simulation_time = atoi(optarg);
+            break;
+        default:
+            //usage();
+            exit(2);
+            break;
+        }
+    }
+    argc -= optind;
+    argv += optind;
+
+    if ((out_file = fopen("g1050_tests.txt", "w")) == NULL)
+    {
+        fprintf(stderr, "Can't open %s\n", "g1050_tests.txt");
+        return 2;
+    }
+    packets_per_sec = 1000/PACKET_INTERVAL;
+    num_packets = packets_per_sec*simulation_time;
+
+    if ((packet_arrival_times = calloc(num_packets, sizeof(double))) == NULL)
+    {
+        fprintf(stderr, "Can't allocate the data buffers\n");
+        return 2;
+    }
+    for (i = 0;  i < num_packets;  i++)
+        packet_arrival_times[i] = 0.0;
+
+    /* If we don't initialise this random number generator it gives endless zeros on some systems. */
+    /* Use a fixed seed to produce identical results in successive runs of the simulation, for debug purposes. */
+    srand48(0x1234567);
+
+    if ((s = g1050_init(model_no, speed_pattern_no, PACKET_SIZE, packets_per_sec)) == NULL)
+    {
+        fprintf(stderr, "Failed to start the G.1050 model\n");
+        exit(2);
+    }
+    g1050_dump_parms(model_no, speed_pattern_no);
+
+#if defined(ENABLE_GUI)
+    if (use_gui)
+        start_media_monitor();
+#endif
+
+    for (i = 0;  i < 256;  i++)
+        put_pkt[i] = i;
+    put_pkt_len = 256;
+    get_pkt_len = -1;
+    get_seq_no = -1;
+    get_arrival_time = -1;
+    packets_put = 0;
+    packets_really_put = 0;
+    packets_got = 0;
+    oos_packets_got = 0;
+    missing_packets_got = 0;
+    highest_seq_no_got = -1;
+    for (i = 0;  i < num_packets;  i++)
+    {
+        if ((len = g1050_put(s, put_pkt, put_pkt_len, i, (double) i*0.001*PACKET_INTERVAL)) > 0)
+            packets_really_put++;
+        packets_put++;
+        if (i == 5)
+            g1050_queue_dump(s);
+        if (i >= 5)
+        {
+            do
+            {
+                get_pkt_len = g1050_get(s, get_pkt, 256, (double) i*0.001*PACKET_INTERVAL, &get_seq_no, &get_departure_time, &get_arrival_time);
+                if (get_pkt_len >= 0)
+                {
+#if defined(ENABLE_GUI)
+                    if (use_gui)
+                        media_monitor_rx(get_seq_no, get_departure_time, get_arrival_time);
+#endif
+                    packets_got++;
+                    if (get_seq_no < highest_seq_no_got)
+                        oos_packets_got++;
+                    else if (get_seq_no > highest_seq_no_got + 1)
+                        missing_packets_got += (get_seq_no - highest_seq_no_got - 1);
+                    if (get_seq_no > highest_seq_no_got)
+                        highest_seq_no_got = get_seq_no;
+                    fprintf(out_file, "%d, %.3f, %.8f\n", get_seq_no, get_seq_no*0.001*PACKET_INTERVAL, get_arrival_time);
+                }
+            }
+            while (get_pkt_len >= 0);
+        }
+#if defined(ENABLE_GUI)
+        if (use_gui)
+            media_monitor_update_display();
+#endif
+    }
+    /* Clear out anything remaining in the queue, by jumping forwards in time */
+    do
+    {
+        get_pkt_len = g1050_get(s, get_pkt, 256, (double) i*0.001*PACKET_INTERVAL + 5.0, &get_seq_no, &get_departure_time, &get_arrival_time);
+        if (get_pkt_len >= 0)
+        {
+            packets_got++;
+            fprintf(out_file, "%d, %.3f, %.8f\n", get_seq_no, get_seq_no*0.001*PACKET_INTERVAL, get_arrival_time);
+        }
+    }
+    while (get_pkt_len >= 0);
+
+    fclose(out_file);
+
+    printf("Put %d packets. Really put %d packets. Got %d packets.\n", packets_put, packets_really_put, packets_got);
+    printf("%d OOS packets, %d missing packets\n", oos_packets_got, missing_packets_got - oos_packets_got);
+    if (packets_really_put != packets_got)
+    {
+        printf("%d packets queued, but only %d received\n", packets_really_put, packets_got);
+        exit(2);
+    }
+    printf("%.3f%% of packets lost\n", 100.0*(packets_put - packets_really_put)/packets_put);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/tests/g168_tests.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/tests/g168_tests.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,373 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * g168tests.c - Tests of the "test equipment" (filters, etc.) specified
+ *               in G.168. This code is only for checking out the tools,
+ *               not for testing an echo cancellor.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2001 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: g168_tests.c,v 1.16 2008/08/29 09:28:13 steveu Exp $
+ */
+
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <audiofile.h>
+
+#include "spandsp.h"
+#include "spandsp/g168models.h"
+
+#define FALSE 0
+#define TRUE (!FALSE)
+
+typedef struct
+{
+    const char *name;
+    int max;
+    int cur;
+    float gain;
+    AFfilehandle handle;
+    int16_t signal[SAMPLE_RATE];
+} signal_source_t;
+
+signal_source_t local_css;
+signal_source_t far_css;
+
+AFfilehandle afOpenFile_telephony_read(const char *name, int channels)
+{
+    float x;
+    AFfilehandle handle;
+
+    if ((handle = afOpenFile(name, "r", 0)) == AF_NULL_FILEHANDLE)
+    {
+        fprintf(stderr, "    Cannot open wave file '%s'\n", name);
+        exit(2);
+    }
+    if ((x = afGetFrameSize(handle, AF_DEFAULT_TRACK, 1)) != 2.0)
+    {
+        fprintf(stderr, "    Unexpected frame size in wave file '%s'\n", name);
+        exit(2);
+    }
+    if ((x = afGetRate(handle, AF_DEFAULT_TRACK)) != (float) SAMPLE_RATE)
+    {
+        printf("    Unexpected sample rate in wave file '%s'\n", name);
+        exit(2);
+    }
+    if ((x = afGetChannels(handle, AF_DEFAULT_TRACK)) != (float) channels)
+    {
+        printf("    Unexpected number of channels in wave file '%s'\n", name);
+        exit(2);
+    }
+
+    return handle;
+}
+/*- End of function --------------------------------------------------------*/
+
+AFfilehandle afOpenFile_telephony_write(const char *name, int channels)
+{
+    AFfilesetup setup;
+    AFfilehandle handle;
+
+    if ((setup = afNewFileSetup()) == AF_NULL_FILESETUP)
+    {
+        fprintf(stderr, "    %s: Failed to create file setup\n", name);
+        exit(2);
+    }
+    afInitSampleFormat(setup, AF_DEFAULT_TRACK, AF_SAMPFMT_TWOSCOMP, 16);
+    afInitRate(setup, AF_DEFAULT_TRACK, (float) SAMPLE_RATE);
+    afInitFileFormat(setup, AF_FILE_WAVE);
+    afInitChannels(setup, AF_DEFAULT_TRACK, channels);
+
+    if ((handle = afOpenFile(name, "w", setup)) == AF_NULL_FILEHANDLE)
+    {
+        fprintf(stderr, "    Failed to open result file\n");
+        exit(2);
+    }
+    afFreeFileSetup(setup);
+
+    return handle;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void signal_load(signal_source_t *sig, const char *name)
+{
+    sig->handle = afOpenFile_telephony_read(name, 1);
+    sig->name = name;
+    sig->max = afReadFrames(sig->handle, AF_DEFAULT_TRACK, sig->signal, SAMPLE_RATE);
+    if (sig->max < 0)
+    {
+        fprintf(stderr, "    Error reading sound file '%s'\n", sig->name);
+        exit(2);
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static void signal_free(signal_source_t *sig)
+{
+    if (afCloseFile(sig->handle) != 0)
+    {
+        fprintf(stderr, "    Cannot close sound file '%s'\n", sig->name);
+        exit(2);
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static void signal_restart(signal_source_t *sig, float gain)
+{
+    sig->cur = 0;
+    sig->gain = powf(10.0f, gain/20.0f);
+}
+/*- End of function --------------------------------------------------------*/
+
+static int16_t signal_amp(signal_source_t *sig)
+{
+    int16_t tx;
+
+    tx = sig->signal[sig->cur++]*sig->gain;
+    if (sig->cur >= sig->max)
+        sig->cur = 0;
+    return tx;
+}
+/*- End of function --------------------------------------------------------*/
+
+int main(int argc, char *argv[])
+{
+    int f;
+    int i;
+    int16_t amp[8000];
+    int16_t value;
+    int signal;
+    float power[10];
+    tone_gen_descriptor_t tone_desc;
+    tone_gen_state_t tone_state;
+    awgn_state_t noise_source;
+    fir32_state_t line_model_d2;
+    fir32_state_t line_model_d3;
+    fir32_state_t line_model_d4;
+    fir32_state_t line_model_d5;
+    fir32_state_t line_model_d6;
+    fir32_state_t line_model_d7;
+    fir32_state_t line_model_d8;
+    fir32_state_t line_model_d9;
+    fir_float_state_t level_measurement_bp;
+
+    signal_load(&local_css, "sound_c1_8k.wav");
+    signal_load(&far_css, "sound_c3_8k.wav");
+
+    fir32_create(&line_model_d2,
+                 line_model_d2_coeffs,
+                 sizeof(line_model_d2_coeffs)/sizeof(int32_t));
+    fir32_create(&line_model_d3,
+                 line_model_d3_coeffs,
+                 sizeof(line_model_d3_coeffs)/sizeof(int32_t));
+    fir32_create(&line_model_d4,
+                 line_model_d4_coeffs,
+                 sizeof(line_model_d4_coeffs)/sizeof(int32_t));
+    fir32_create(&line_model_d5,
+                 line_model_d5_coeffs,
+                 sizeof(line_model_d5_coeffs)/sizeof(int32_t));
+    fir32_create(&line_model_d6,
+                 line_model_d6_coeffs,
+                 sizeof(line_model_d6_coeffs)/sizeof(int32_t));
+    fir32_create(&line_model_d7,
+                 line_model_d7_coeffs,
+                 sizeof(line_model_d7_coeffs)/sizeof(int32_t));
+    fir32_create(&line_model_d8,
+                 line_model_d8_coeffs,
+                 sizeof(line_model_d8_coeffs)/sizeof(int32_t));
+    fir32_create(&line_model_d9,
+                 line_model_d9_coeffs,
+                 sizeof(line_model_d9_coeffs)/sizeof(int32_t));
+    fir_float_create(&level_measurement_bp,
+                     level_measurement_bp_coeffs,
+                     sizeof(level_measurement_bp_coeffs)/sizeof(float));
+
+    for (f = 10;  f < 4000;  f++)
+    {
+         make_tone_gen_descriptor(&tone_desc,
+                                  f,
+                                  -10,
+                                  0,
+                                  0,
+                                  1,
+                                  0,
+                                  0,
+                                  0,
+                                  TRUE);
+        tone_gen_init(&tone_state, &tone_desc);
+        tone_gen(&tone_state, amp, 8000);
+        for (i = 0;  i < 10;  i++)
+            power[i] = 0.0f;
+        for (i = 0;  i < 800;  i++)
+        {
+            signal = fir32(&line_model_d2, amp[i]);
+            power[0] += ((signal*signal - power[0])/32.0f);
+            signal = fir32(&line_model_d3, amp[i]);
+            power[1] += ((signal*signal - power[1])/32.0f);
+            signal = fir32(&line_model_d4, amp[i]);
+            power[2] += ((signal*signal - power[2])/32.0f);
+            signal = fir32(&line_model_d5, amp[i]);
+            power[3] += ((signal*signal - power[3])/32.0f);
+            signal = fir32(&line_model_d6, amp[i]);
+            power[4] += ((signal*signal - power[4])/32.0f);
+            signal = fir32(&line_model_d7, amp[i]);
+            power[5] += ((signal*signal - power[5])/32.0f);
+            signal = fir32(&line_model_d8, amp[i]);
+            power[6] += ((signal*signal - power[6])/32.0f);
+            signal = fir32(&line_model_d9, amp[i]);
+            power[7] += ((signal*signal - power[7])/32.0f);
+            signal = fir_float(&level_measurement_bp, amp[i]);
+            power[8] += ((signal*signal - power[8])/32.0f);
+            signal = amp[i];
+            power[9] += ((signal*signal - power[9])/32.0f);
+        }
+        printf("%d %f %f %f %f %f %f %f %f %f %f\n",
+               f,
+               sqrt(power[0])*LINE_MODEL_D2_GAIN,
+               sqrt(power[1])*LINE_MODEL_D3_GAIN,
+               sqrt(power[2])*LINE_MODEL_D4_GAIN,
+               sqrt(power[3])*LINE_MODEL_D5_GAIN,
+               sqrt(power[4])*LINE_MODEL_D6_GAIN,
+               sqrt(power[5])*LINE_MODEL_D7_GAIN,
+               sqrt(power[6])*LINE_MODEL_D8_GAIN,
+               sqrt(power[7])*LINE_MODEL_D9_GAIN,
+               sqrt(power[8]),
+               sqrt(power[9]));
+    }
+    awgn_init_dbm0(&noise_source, 1234567, -20.0f);
+    for (i = 0;  i < 10;  i++)
+        power[i] = 0.0f;
+    signal_restart(&local_css, 0.0f);
+    signal_restart(&far_css, 0.0f);
+    for (i = 0;  i < SAMPLE_RATE;  i++)
+    {
+        value = signal_amp(&local_css);
+        //value = awgn(&noise_source);
+        signal = fir32(&line_model_d2, value);
+        power[0] += ((signal*signal - power[0])/32.0f);
+        signal = fir32(&line_model_d3, value);
+        power[1] += ((signal*signal - power[1])/32.0f);
+        signal = fir32(&line_model_d4, value);
+        power[2] += ((signal*signal - power[2])/32.0f);
+        signal = fir32(&line_model_d5, value);
+        power[3] += ((signal*signal - power[3])/32.0f);
+        signal = fir32(&line_model_d6, value);
+        power[4] += ((signal*signal - power[4])/32.0f);
+        signal = fir32(&line_model_d7, value);
+        power[5] += ((signal*signal - power[5])/32.0f);
+        signal = fir32(&line_model_d8, value);
+        power[6] += ((signal*signal - power[6])/32.0f);
+        signal = fir32(&line_model_d9, value);
+        power[7] += ((signal*signal - power[7])/32.0f);
+        signal = fir_float(&level_measurement_bp, value);
+        power[8] += ((signal*signal - power[8])/32.0f);
+        signal = value;
+        power[9] += ((signal*signal - power[9])/32.0f);
+    }
+    for (i = 0;  i < 10;  i++)
+        power[i] = 0.0f;
+    for (i = 0;  i < SAMPLE_RATE;  i++)
+    {
+        value = signal_amp(&local_css);
+        //value = awgn(&noise_source);
+        signal = fir32(&line_model_d2, value);
+        power[0] += ((signal*signal - power[0])/32.0f);
+        signal = fir32(&line_model_d3, value);
+        power[1] += ((signal*signal - power[1])/32.0f);
+        signal = fir32(&line_model_d4, value);
+        power[2] += ((signal*signal - power[2])/32.0f);
+        signal = fir32(&line_model_d5, value);
+        power[3] += ((signal*signal - power[3])/32.0f);
+        signal = fir32(&line_model_d6, value);
+        power[4] += ((signal*signal - power[4])/32.0f);
+        signal = fir32(&line_model_d7, value);
+        power[5] += ((signal*signal - power[5])/32.0f);
+        signal = fir32(&line_model_d8, value);
+        power[6] += ((signal*signal - power[6])/32.0f);
+        signal = fir32(&line_model_d9, value);
+        power[7] += ((signal*signal - power[7])/32.0f);
+        signal = fir_float(&level_measurement_bp, value);
+        power[8] += ((signal*signal - power[8])/32.0f);
+        signal = value;
+        power[9] += ((signal*signal - power[9])/32.0f);
+    }
+    printf("%d %f %f %f %f %f %f %f %f %f %f\n",
+           0,
+           sqrt(power[0])*LINE_MODEL_D2_GAIN,
+           sqrt(power[1])*LINE_MODEL_D3_GAIN,
+           sqrt(power[2])*LINE_MODEL_D4_GAIN,
+           sqrt(power[3])*LINE_MODEL_D5_GAIN,
+           sqrt(power[4])*LINE_MODEL_D6_GAIN,
+           sqrt(power[5])*LINE_MODEL_D7_GAIN,
+           sqrt(power[6])*LINE_MODEL_D8_GAIN,
+           sqrt(power[7])*LINE_MODEL_D9_GAIN,
+           sqrt(power[8]),
+           sqrt(power[9]));
+    printf("%d %f %f %f %f %f %f %f %f %f %f\n",
+           0,
+           sqrt(power[0]),
+           sqrt(power[1]),
+           sqrt(power[2]),
+           sqrt(power[3]),
+           sqrt(power[4]),
+           sqrt(power[5]),
+           sqrt(power[6]),
+           sqrt(power[7]),
+           sqrt(power[8]),
+           sqrt(power[9]));
+    printf("%d %f %f %f %f %f %f %f %f %f %f\n",
+           0,
+           sqrt(power[0])/sqrt(power[9]),
+           sqrt(power[1])/sqrt(power[9]),
+           sqrt(power[2])/sqrt(power[9]),
+           sqrt(power[3])/sqrt(power[9]),
+           sqrt(power[4])/sqrt(power[9]),
+           sqrt(power[5])/sqrt(power[9]),
+           sqrt(power[6])/sqrt(power[9]),
+           sqrt(power[7])/sqrt(power[9]),
+           sqrt(power[8]),
+           sqrt(power[9]));
+    printf("%d %f %f %f %f %f %f %f %f %f %f\n",
+           0,
+           sqrt(power[0])*LINE_MODEL_D2_GAIN/sqrt(power[9]),
+           sqrt(power[1])*LINE_MODEL_D3_GAIN/sqrt(power[9]),
+           sqrt(power[2])*LINE_MODEL_D4_GAIN/sqrt(power[9]),
+           sqrt(power[3])*LINE_MODEL_D5_GAIN/sqrt(power[9]),
+           sqrt(power[4])*LINE_MODEL_D6_GAIN/sqrt(power[9]),
+           sqrt(power[5])*LINE_MODEL_D7_GAIN/sqrt(power[9]),
+           sqrt(power[6])*LINE_MODEL_D8_GAIN/sqrt(power[9]),
+           sqrt(power[7])*LINE_MODEL_D9_GAIN/sqrt(power[9]),
+           sqrt(power[8]),
+           sqrt(power[9]));
+
+    for (i = 0;  i < (int) (sizeof(css_c1)/sizeof(css_c1[0]));  i++)
+        printf("%d\n", css_c1[i]);
+    printf("\n");
+    for (i = 0;  i < (int) (sizeof(css_c1)/sizeof(css_c3[0]));  i++)
+        printf("%d\n", css_c3[i]);
+    return  0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/tests/g711_tests.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/tests/g711_tests.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,324 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * g711_tests.c
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2006 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: g711_tests.c,v 1.14 2008/08/16 15:24:15 steveu Exp $
+ */
+
+/*! \page g711_tests_page A-law and u-law conversion tests
+\section g711_tests_page_sec_1 What does it do?
+
+\section g711_tests_page_sec_2 How is it used?
+*/
+
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <string.h>
+#include <audiofile.h>
+
+#include "spandsp.h"
+#include "spandsp-sim.h"
+
+#define OUT_FILE_NAME   "g711.wav"
+
+int16_t amp[65536];
+uint8_t ulaw_data[65536];
+uint8_t alaw_data[65536];
+
+const uint8_t alaw_1khz_sine[] = {0x34, 0x21, 0x21, 0x34, 0xB4, 0xA1, 0xA1, 0xB4};
+const uint8_t ulaw_1khz_sine[] = {0x1E, 0x0B, 0x0B, 0x1E, 0x9E, 0x8B, 0x8B, 0x9E};
+
+int main(int argc, char *argv[])
+{
+    AFfilehandle outhandle;
+    power_meter_t power_meter;
+    int outframes;
+    int i;
+    int block;
+    int pre;
+    int post;
+    int post_post;
+    int alaw_failures;
+    int ulaw_failures;
+    float worst_alaw;
+    float worst_ulaw;
+    float tmp;
+    int len;
+    g711_state_t *encode;
+    g711_state_t *transcode;
+    g711_state_t *decode;
+    
+    if ((outhandle = afOpenFile_telephony_write(OUT_FILE_NAME, 1)) == AF_NULL_FILEHANDLE)
+    {
+        fprintf(stderr, "    Cannot create wave file '%s'\n", OUT_FILE_NAME);
+        exit(2);
+    }
+
+    printf("Conversion accuracy tests.\n");
+    alaw_failures = 0;
+    ulaw_failures = 0;
+    worst_alaw = 0.0;
+    worst_ulaw = 0.0;
+    for (block = 0;  block < 1;  block++)
+    {
+        for (i = 0;  i < 65536;  i++)
+        {
+            pre = i - 32768;
+            post = alaw_to_linear(linear_to_alaw(pre));
+            if (abs(pre) > 140)
+            {
+                tmp = (float) abs(post - pre)/(float) abs(pre);
+                if (tmp > 0.10)
+                {
+                    printf("A-law: Excessive error at %d (%d)\n", pre, post);
+                    alaw_failures++;
+                }
+                if (tmp > worst_alaw)
+                    worst_alaw = tmp;
+            }
+            else
+            {
+                /* Small values need different handling for sensible measurement */
+                if (abs(post - pre) > 15)
+                {
+                    printf("A-law: Excessive error at %d (%d)\n", pre, post);
+                    alaw_failures++;
+                }
+            }
+            amp[i] = post;
+        }
+        outframes = afWriteFrames(outhandle,
+                                  AF_DEFAULT_TRACK,
+                                  amp,
+                                  65536);
+        if (outframes != 65536)
+        {
+            fprintf(stderr, "    Error writing wave file\n");
+            exit(2);
+        }
+        for (i = 0;  i < 65536;  i++)
+        {
+            pre = i - 32768;
+            post = ulaw_to_linear(linear_to_ulaw(pre));
+            if (abs(pre) > 40)
+            {
+                tmp = (float) abs(post - pre)/(float) abs(pre);
+                if (tmp > 0.10)
+                {
+                    printf("u-law: Excessive error at %d (%d)\n", pre, post);
+                    ulaw_failures++;
+                }
+                if (tmp > worst_ulaw)
+                    worst_ulaw = tmp;
+            }
+            else
+            {
+                /* Small values need different handling for sensible measurement */
+                if (abs(post - pre) > 4)
+                {
+                    printf("u-law: Excessive error at %d (%d)\n", pre, post);
+                    ulaw_failures++;
+                }
+            }
+            amp[i] = post;
+        }
+        outframes = afWriteFrames(outhandle,
+                                  AF_DEFAULT_TRACK,
+                                  amp,
+                                  65536);
+        if (outframes != 65536)
+        {
+            fprintf(stderr, "    Error writing wave file\n");
+            exit(2);
+        }
+    }
+    printf("Worst A-law error (ignoring small values) %f%%\n", worst_alaw*100.0);
+    printf("Worst u-law error (ignoring small values) %f%%\n", worst_ulaw*100.0);
+    if (alaw_failures  ||  ulaw_failures)
+    {
+        printf("%d A-law values with excessive error\n", alaw_failures);
+        printf("%d u-law values with excessive error\n", ulaw_failures);
+        printf("Tests failed\n");
+        exit(2);
+    }
+    
+    printf("Cyclic conversion repeatability tests.\n");
+    /* Find what happens to every possible linear value after a round trip. */
+    for (i = 0;  i < 65536;  i++)
+    {
+        pre = i - 32768;
+        /* Make a round trip */
+        post = alaw_to_linear(linear_to_alaw(pre));
+        /* A second round trip should cause no further change */
+        post_post = alaw_to_linear(linear_to_alaw(post));
+        if (post_post != post)
+        {
+            printf("A-law second round trip mismatch - at %d, %d != %d\n", pre, post, post_post);
+            printf("Tests failed\n");
+            exit(2);
+        }
+        /* Make a round trip */
+        post = ulaw_to_linear(linear_to_ulaw(pre));
+        /* A second round trip should cause no further change */
+        post_post = ulaw_to_linear(linear_to_ulaw(post));
+        if (post_post != post)
+        {
+            printf("u-law round trip mismatch - at %d, %d != %d\n", pre, post, post_post);
+            printf("Tests failed\n");
+            exit(2);
+        }
+    }
+    
+    printf("Reference power level tests.\n");
+    power_meter_init(&power_meter, 7);
+
+    for (i = 0;  i < 8000;  i++)
+    {
+        amp[i] = ulaw_to_linear(ulaw_1khz_sine[i & 7]);
+        power_meter_update(&power_meter, amp[i]);
+    }
+    printf("Reference u-law 1kHz tone is %fdBm0\n", power_meter_current_dbm0(&power_meter));
+    outframes = afWriteFrames(outhandle,
+                              AF_DEFAULT_TRACK,
+                              amp,
+                              8000);
+    if (outframes != 8000)
+    {
+        fprintf(stderr, "    Error writing wave file\n");
+        exit(2);
+    }
+    if (0.1f < fabs(power_meter_current_dbm0(&power_meter)))
+    {
+        printf("Test failed.\n");
+        exit(2);
+    }
+
+    for (i = 0;  i < 8000;  i++)
+    {
+        amp[i] = alaw_to_linear(alaw_1khz_sine[i & 7]);
+        power_meter_update(&power_meter, amp[i]);
+    }
+    printf("Reference A-law 1kHz tone is %fdBm0\n", power_meter_current_dbm0(&power_meter));
+    outframes = afWriteFrames(outhandle,
+                              AF_DEFAULT_TRACK,
+                              amp,
+                              8000);
+    if (outframes != 8000)
+    {
+        fprintf(stderr, "    Error writing wave file\n");
+        exit(2);
+    }
+    if (0.1f < fabs(power_meter_current_dbm0(&power_meter)))
+    {
+        printf("Test failed.\n");
+        exit(2);
+    }
+
+    /* Check the transcoding functions. */
+    printf("Testing transcoding A-law -> u-law -> A-law\n");
+    for (i = 0;  i < 256;  i++)
+    {
+        if (alaw_to_ulaw(ulaw_to_alaw(i)) != i)
+        {
+            if (abs(alaw_to_ulaw(ulaw_to_alaw(i)) - i) > 1)
+            {
+                printf("u-law -> A-law -> u-law gave %d -> %d\n", i, alaw_to_ulaw(ulaw_to_alaw(i)));
+                printf("Test failed\n");
+                exit(2);
+            }
+        }
+    }
+
+    printf("Testing transcoding u-law -> A-law -> u-law\n");
+    for (i = 0;  i < 256;  i++)
+    {
+        if (ulaw_to_alaw(alaw_to_ulaw(i)) != i)
+        {
+            if (abs(alaw_to_ulaw(ulaw_to_alaw(i)) - i) > 1)
+            {
+                printf("A-law -> u-law -> A-law gave %d -> %d\n", i, ulaw_to_alaw(alaw_to_ulaw(i)));
+                printf("Test failed\n");
+                exit(2);
+            }
+        }
+    }
+    
+    encode = g711_init(NULL, G711_ALAW);
+    transcode = g711_init(NULL, G711_ALAW);
+    decode = g711_init(NULL, G711_ULAW);
+
+    len = 65536;
+    for (i = 0;  i < len;  i++)
+        amp[i] = i - 32768;
+    len = g711_encode(encode, alaw_data, amp, len);
+    len = g711_transcode(transcode, ulaw_data, alaw_data, len);
+    len = g711_decode(decode, amp, ulaw_data, len);
+    if (len != 65536)
+    {
+        printf("Block coding gave the wrong length - %d instead of %d\n", len, 65536);
+        printf("Test failed\n");
+        exit(2);
+    }
+    for (i = 0;  i < len;  i++)
+    {
+        pre = i - 32768;
+        post = amp[i];
+        if (abs(pre) > 140)
+        {
+            tmp = (float) abs(post - pre)/(float) abs(pre);
+            if (tmp > 0.10)
+            {
+                printf("Block: Excessive error at %d (%d)\n", pre, post);
+                exit(2);
+            }
+        }
+        else
+        {
+            /* Small values need different handling for sensible measurement */
+            if (abs(post - pre) > 15)
+            {
+                printf("Block: Excessive error at %d (%d)\n", pre, post);
+                exit(2);
+            }
+        }
+    }
+    g711_release(encode);
+    g711_release(transcode);
+    g711_release(decode);
+
+    if (afCloseFile(outhandle))
+    {
+        fprintf(stderr, "    Cannot close wave file '%s'\n", OUT_FILE_NAME);
+        exit(2);
+    }
+
+    printf("Tests passed.\n");
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/tests/g722_tests.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/tests/g722_tests.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,521 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * g722_tests.c - Test G.722 encode and decode.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2005 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: g722_tests.c,v 1.26 2008/05/13 13:17:25 steveu Exp $
+ */
+
+/*! \file */
+
+/*! \page g722_tests_page G.722 tests
+\section g722_tests_page_sec_1 What does it do?
+This modules implements two sets of tests:
+    - The tests defined in the G.722 specification, using the test data files supplied
+      with the specification.
+    - A generally audio quality test, consisting of compressing and decompressing a speeech
+      file for audible comparison.
+
+The speech file should be recorded at 16 bits/sample, 16000 samples/second, and named
+"pre_g722.wav".
+
+The ITU tests use the codec in a special mode, in which the QMFs, which split and recombine the
+sub-bands, are disabled. This means they do not test 100% of the codec. This is the reason for
+including the additional listening test.
+
+\section g722_tests_page_sec_2 How is it used?
+To perform the tests in the G.722 specification you need to obtain the test data files from the
+specification. These are copyright material, and so cannot be distributed with this test software.
+
+The files, containing test vectors, which are supplied with the G.722 specification, should be
+copied to itutests/g722. The ITU tests can then be run by executing g722_tests without
+any parameters.
+
+To perform a general audio quality test, g722_tests should be run with a parameter specifying
+the required bit rate for compression. The valid parameters are "-48", "-56", and "-64".
+The file ../test-data/local/short_wb_voice.wav will be compressed to the specified bit rate, decompressed,
+and the resulting audio stored in post_g722.wav.
+*/
+
+#if defined(HAVE_CONFIG_H)
+#include <config.h>
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <memory.h>
+#include <ctype.h>
+#include <audiofile.h>
+
+#include "spandsp.h"
+
+#define G722_SAMPLE_RATE    16000
+
+#define BLOCK_LEN           320
+
+#define MAX_TEST_VECTOR_LEN 40000
+
+#define TESTDATA_DIR        "../test-data/itu/g722/"
+
+#define EIGHTK_IN_FILE_NAME "../test-data/local/short_nb_voice.wav"
+#define IN_FILE_NAME        "../test-data/local/short_wb_voice.wav"
+#define OUT_FILE_NAME       "post_g722.wav"
+
+#if 0
+static const char *itu_test_files[] =
+{
+    TESTDATA_DIR "T1C1.XMT",        /* 69973 bytes */
+    TESTDATA_DIR "T1C2.XMT",        /* 3605 bytes */
+    TESTDATA_DIR "T1D3.COD",        /* 69973 bytes */
+
+    TESTDATA_DIR "T2R1.COD",        /* 69973 bytes */
+    TESTDATA_DIR "T2R2.COD",        /* 3605 bytes */
+
+    TESTDATA_DIR "T3L1.RC1",        /* 69973 bytes */
+    TESTDATA_DIR "T3L1.RC2",        /* 69973 bytes */
+    TESTDATA_DIR "T3L1.RC3",        /* 69973 bytes */
+    TESTDATA_DIR "T3H1.RC0",        /* 69973 bytes */
+    TESTDATA_DIR "T3L2.RC1",        /* 3605 bytes */
+    TESTDATA_DIR "T3L2.RC2",        /* 3605 bytes */
+    TESTDATA_DIR "T3L2.RC3",        /* 3605 bytes */
+    TESTDATA_DIR "T3H2.RC0",        /* 3605 bytes */
+    TESTDATA_DIR "T3L3.RC1",        /* 69973 bytes */
+    TESTDATA_DIR "T3L3.RC2",        /* 69973 bytes */
+    TESTDATA_DIR "T3L3.RC3",        /* 69973 bytes */
+    TESTDATA_DIR "T3H3.RC0"         /* 69973 bytes */
+};
+#endif
+
+static const char *encode_test_files[] =
+{
+    TESTDATA_DIR "T1C1.XMT",    TESTDATA_DIR "T2R1.COD",
+    TESTDATA_DIR "T1C2.XMT",    TESTDATA_DIR "T2R2.COD",
+    NULL
+};
+
+static const char *decode_test_files[] =
+{
+    TESTDATA_DIR "T2R1.COD",
+    TESTDATA_DIR "T3L1.RC1",
+    TESTDATA_DIR "T3L1.RC2",
+    TESTDATA_DIR "T3L1.RC3",
+    TESTDATA_DIR "T3H1.RC0",
+
+    TESTDATA_DIR "T2R2.COD",
+    TESTDATA_DIR "T3L2.RC1",
+    TESTDATA_DIR "T3L2.RC2",
+    TESTDATA_DIR "T3L2.RC3",
+    TESTDATA_DIR "T3H2.RC0",
+
+    TESTDATA_DIR "T1D3.COD",
+    TESTDATA_DIR "T3L3.RC1",
+    TESTDATA_DIR "T3L3.RC2",
+    TESTDATA_DIR "T3L3.RC3",
+    TESTDATA_DIR "T3H3.RC0",
+    
+    NULL
+};
+
+int16_t itu_data[MAX_TEST_VECTOR_LEN];
+uint16_t itu_ref[MAX_TEST_VECTOR_LEN];
+uint16_t itu_ref_upper[MAX_TEST_VECTOR_LEN];
+uint8_t compressed[MAX_TEST_VECTOR_LEN];
+int16_t decompressed[MAX_TEST_VECTOR_LEN];
+
+static int hex_get(char *s)
+{
+    int i;
+    int value;
+    int x;
+
+    for (value = i = 0;  i < 4;  i++)
+    {
+        x = *s++ - 0x30;
+        if (x > 9)
+            x -= 0x07;
+        if (x > 15)
+            x -= 0x20;
+        if (x < 0  ||  x > 15)
+            return -1;
+        value <<= 4;
+        value |= x;
+    }
+    return value;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int get_vector(FILE *file, uint16_t vec[])
+{
+    char buf[132 + 1];
+    char *s;
+    int i;
+    int value;
+
+    while (fgets(buf, 133, file))
+    {
+        if (buf[0] == '/'  &&  buf[1] == '*')
+            continue;
+        s = buf;
+        i = 0;
+        while ((value = hex_get(s)) >= 0)
+        {
+            vec[i++] = value;
+            s += 4;
+        }
+        return i;
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int get_test_vector(const char *file, uint16_t buf[], int max_len)
+{
+    int octets;
+    int i;
+    FILE *infile;
+    
+    if ((infile = fopen(file, "r")) == NULL)
+    {
+        fprintf(stderr, "    Failed to open '%s'\n", file);
+        exit(2);
+    }
+    octets = 0;  
+    while ((i = get_vector(infile, buf + octets)) > 0)
+        octets += i;
+    fclose(infile);
+    return octets;
+}
+/*- End of function --------------------------------------------------------*/
+
+int main(int argc, char *argv[])
+{
+    g722_encode_state_t enc_state;
+    g722_decode_state_t dec_state;
+    int len;
+    int len_comp;
+    int len_comp_upper;
+    int len_data;
+    int len2;
+    int len3;
+    int i;
+    int j;
+    int k;
+    int file;
+    AFfilehandle inhandle;
+    AFfilehandle outhandle;
+    AFfilesetup filesetup;
+    int outframes;
+    int samples;
+    int mode;
+    int itutests;
+    int bit_rate;
+    int eight_k_in;
+    int eight_k_out;
+    float x;
+    int16_t indata[BLOCK_LEN];
+    int16_t outdata[BLOCK_LEN];
+    uint8_t adpcmdata[BLOCK_LEN];
+
+    i = 1;
+    bit_rate = 64000;
+    eight_k_in = FALSE;
+    eight_k_out = FALSE;
+    itutests = TRUE;
+    while (argc > i)
+    {
+        if (strcmp(argv[i], "-48") == 0)
+        {
+            bit_rate = 48000;
+            itutests = FALSE;
+            i++;
+        }
+        else if (strcmp(argv[i], "-56") == 0)
+        {
+            bit_rate = 56000;
+            itutests = FALSE;
+            i++;
+        }
+        else if (strcmp(argv[i], "-64") == 0)
+        {
+            bit_rate = 64000;
+            itutests = FALSE;
+            i++;
+        }
+        else if (strcmp(argv[i], "-8k8k") == 0)
+        {
+            eight_k_in = TRUE;
+            eight_k_out = TRUE;
+            i++;
+        }
+        else if (strcmp(argv[i], "-8k16k") == 0)
+        {
+            eight_k_in = TRUE;
+            eight_k_out = FALSE;
+            i++;
+        }
+        else if (strcmp(argv[i], "-16k8k") == 0)
+        {
+            eight_k_in = FALSE;
+            eight_k_out = TRUE;
+            i++;
+        }
+        else if (strcmp(argv[i], "-16k16k") == 0)
+        {
+            eight_k_in = FALSE;
+            eight_k_out = FALSE;
+            i++;
+        }
+        else
+        {
+            fprintf(stderr, "Unknown parameter %s specified.\n", argv[i]);
+            exit(2);
+        }
+    }
+
+    if (itutests)
+    {
+        /* ITU G.722 encode tests, using configuration 1. The QMF is bypassed */
+        for (file = 0;  encode_test_files[file];  file += 2)
+        {
+            printf("Testing %s -> %s\n", encode_test_files[file], encode_test_files[file + 1]);
+    
+            /* Get the input data */
+            len_data = get_test_vector(encode_test_files[file], (uint16_t *) itu_data, MAX_TEST_VECTOR_LEN);
+
+            /* Get the reference output data */
+            len_comp = get_test_vector(encode_test_files[file + 1], itu_ref, MAX_TEST_VECTOR_LEN);
+
+            /* Process the input data */
+            /* Skip the reset stuff at each end of the data */
+            for (i = 0;  i < len_data;  i++)
+            {
+                if ((itu_data[i] & 1) == 0)
+                    break;
+            }
+            for (j = i;  j < len_data;  j++)
+            {
+                if ((itu_data[j] & 1))
+                    break;
+            }
+            len = j - i;
+            g722_encode_init(&enc_state, 64000, 0);
+            enc_state.itu_test_mode = TRUE;
+            len2 = g722_encode(&enc_state, compressed, itu_data + i, len);
+
+            /* Check the result against the ITU's reference output data */
+            j = 0;
+            for (k = 0;  k < len2;  k++)
+            {
+                if ((compressed[k] & 0xFF) != ((itu_ref[k + i] >> 8) & 0xFF))
+                {
+                    printf(">>> %6d %4x %4x\n", k, compressed[k] & 0xFF, itu_ref[k + i] & 0xFFFF);
+                    j++;
+                }
+            }
+            printf("%d bad samples, out of %d/%d samples\n", j, len, len_data);
+            if (j)
+            {
+                printf("Test failed\n");
+                exit(2);
+            }
+            printf("Test passed\n");
+        }
+
+        /* ITU G.722 decode tests, using configuration 2. The QMF is bypassed */
+        /* Run each of the tests for each of the modes - 48kbps, 56kbps and 64kbps. */
+        for (mode = 1;  mode <= 3;  mode++)
+        {
+            for (file = 0;  decode_test_files[file];  file += 5)
+            {
+                printf("Testing mode %d, %s -> %s + %s\n",
+                       mode,
+                       decode_test_files[file],
+                       decode_test_files[file + mode],
+                       decode_test_files[file + 4]);
+
+                /* Get the input data */
+                len_data = get_test_vector(decode_test_files[file], (uint16_t *) itu_data, MAX_TEST_VECTOR_LEN);
+        
+                /* Get the lower reference output data */
+                len_comp = get_test_vector(decode_test_files[file + mode], itu_ref, MAX_TEST_VECTOR_LEN);
+        
+                /* Get the upper reference output data */
+                len_comp_upper = get_test_vector(decode_test_files[file + 4], itu_ref_upper, MAX_TEST_VECTOR_LEN);
+    
+                /* Process the input data */
+                /* Skip the reset stuff at each end of the data */
+                for (i = 0;  i < len_data;  i++)
+                {
+                    if ((itu_data[i] & 1) == 0)
+                        break;
+                }
+                for (j = i;  j < len_data;  j++)
+                {
+                    if ((itu_data[j] & 1))
+                        break;
+                }
+                len = j - i;
+                for (k = 0;  k < len;  k++)
+                    compressed[k] = itu_data[k + i] >> ((mode == 3)  ?  10  :  (mode == 2)  ?  9  :  8);
+        
+                g722_decode_init(&dec_state, (mode == 3)  ?  48000  :  (mode == 2)  ?  56000  :  64000, 0);
+                dec_state.itu_test_mode = TRUE;
+                len2 = g722_decode(&dec_state, decompressed, compressed, len);
+        
+                /* Check the result against the ITU's reference output data */
+                j = 0;
+                for (k = 0;  k < len2;  k += 2)
+                {
+                    if ((decompressed[k] & 0xFFFF) != (itu_ref[(k >> 1) + i] & 0xFFFF)
+                        ||
+                        (decompressed[k + 1] & 0xFFFF) != (itu_ref_upper[(k >> 1) + i] & 0xFFFF))
+                    {
+                        printf(">>> %6d %4x %4x %4x %4x\n", k >> 1, decompressed[k] & 0xFFFF, decompressed[k + 1] & 0xFFFF, itu_ref[(k >> 1) + i] & 0xFFFF, itu_ref_upper[(k >> 1) + i] & 0xFFFF);
+                        j++;
+                    }
+                }
+                printf("%d bad samples, out of %d/%d samples\n", j, len, len_data);
+                if (j)
+                {
+                    printf("Test failed\n");
+                    exit(2);
+                }
+                printf("Test passed\n");
+            }
+        }
+
+        printf("Tests passed.\n");
+    }
+    else
+    {
+        if (eight_k_in)
+        {
+            if ((inhandle = afOpenFile(EIGHTK_IN_FILE_NAME, "r", NULL)) == AF_NULL_FILEHANDLE)
+            {
+                fprintf(stderr, "    Cannot open wave file '%s'\n", EIGHTK_IN_FILE_NAME);
+                exit(2);
+            }
+            if ((x = afGetFrameSize(inhandle, AF_DEFAULT_TRACK, 1)) != 2.0)
+            {
+                fprintf(stderr, "    Unexpected frame size in wave file '%s'\n", EIGHTK_IN_FILE_NAME);
+                exit(2);
+            }
+            if ((x = afGetRate(inhandle, AF_DEFAULT_TRACK)) != (float) SAMPLE_RATE)
+            {
+                fprintf(stderr, "    Unexpected sample rate %f in wave file '%s'\n", x, EIGHTK_IN_FILE_NAME);
+                exit(2);
+            }
+            if ((x = afGetChannels(inhandle, AF_DEFAULT_TRACK)) != 1.0)
+            {
+                fprintf(stderr, "    Unexpected number of channels in wave file '%s'\n", EIGHTK_IN_FILE_NAME);
+                exit(2);
+            }
+        }
+        else
+        {
+            if ((inhandle = afOpenFile(IN_FILE_NAME, "r", NULL)) == AF_NULL_FILEHANDLE)
+            {
+                fprintf(stderr, "    Cannot open wave file '%s'\n", IN_FILE_NAME);
+                exit(2);
+            }
+            if ((x = afGetFrameSize(inhandle, AF_DEFAULT_TRACK, 1)) != 2.0)
+            {
+                fprintf(stderr, "    Unexpected frame size in wave file '%s'\n", IN_FILE_NAME);
+                exit(2);
+            }
+            if ((x = afGetRate(inhandle, AF_DEFAULT_TRACK)) != (float) G722_SAMPLE_RATE)
+            {
+                fprintf(stderr, "    Unexpected sample rate %f in wave file '%s'\n", x, IN_FILE_NAME);
+                exit(2);
+            }
+            if ((x = afGetChannels(inhandle, AF_DEFAULT_TRACK)) != 1.0)
+            {
+                fprintf(stderr, "    Unexpected number of channels in wave file '%s'\n", IN_FILE_NAME);
+                exit(2);
+            }
+        }
+        
+        if ((filesetup = afNewFileSetup()) == AF_NULL_FILESETUP)
+        {
+            fprintf(stderr, "    Failed to create file setup\n");
+            exit(2);
+        }
+        afInitSampleFormat(filesetup, AF_DEFAULT_TRACK, AF_SAMPFMT_TWOSCOMP, 16);
+        if (eight_k_out)
+            afInitRate(filesetup, AF_DEFAULT_TRACK, (float) SAMPLE_RATE);
+        else
+            afInitRate(filesetup, AF_DEFAULT_TRACK, (float) G722_SAMPLE_RATE);
+        afInitFileFormat(filesetup, AF_FILE_WAVE);
+        afInitChannels(filesetup, AF_DEFAULT_TRACK, 1);
+        if ((outhandle = afOpenFile(OUT_FILE_NAME, "w", filesetup)) == AF_NULL_FILEHANDLE)
+        {
+            fprintf(stderr, "    Cannot create wave file '%s'\n", OUT_FILE_NAME);
+            exit(2);
+        }
+        if (eight_k_in)
+            g722_encode_init(&enc_state, bit_rate, G722_PACKED | G722_SAMPLE_RATE_8000);
+        else
+            g722_encode_init(&enc_state, bit_rate, G722_PACKED);
+        if (eight_k_out)
+            g722_decode_init(&dec_state, bit_rate, G722_PACKED | G722_SAMPLE_RATE_8000);
+        else
+            g722_decode_init(&dec_state, bit_rate, G722_PACKED);
+        for (;;)
+        {
+            samples = afReadFrames(inhandle,
+                                   AF_DEFAULT_TRACK,
+                                   indata,
+                                   BLOCK_LEN);
+            if (samples <= 0)
+                break;
+            len2 = g722_encode(&enc_state, adpcmdata, indata, samples);
+            len3 = g722_decode(&dec_state, outdata, adpcmdata, len2);
+            outframes = afWriteFrames(outhandle,
+                                      AF_DEFAULT_TRACK,
+                                      outdata,
+                                      len3);
+            if (outframes != len3)
+            {
+                fprintf(stderr, "    Error writing wave file\n");
+                exit(2);
+            }
+        }
+        if (afCloseFile(inhandle))
+        {
+            fprintf(stderr, "    Cannot close wave file '%s'\n", IN_FILE_NAME);
+            exit(2);
+        }
+        if (afCloseFile(outhandle))
+        {
+            fprintf(stderr, "    Cannot close wave file '%s'\n", OUT_FILE_NAME);
+            exit(2);
+        }
+        afFreeFileSetup(filesetup);
+
+        printf("'%s' transcoded to '%s' at %dbps.\n", IN_FILE_NAME, OUT_FILE_NAME, bit_rate);
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/tests/g726_tests.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/tests/g726_tests.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,1306 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * g726_tests.c - Test G.726 encode and decode.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2006 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: g726_tests.c,v 1.27 2008/08/29 09:28:13 steveu Exp $
+ */
+
+/*! \file */
+
+/*! \page g726_tests_page G.726 tests
+\section g726_tests_page_sec_1 What does it do?
+Two sets of tests are performed:
+    - The tests defined in the G.726 specification, using the test data files supplied with
+      the specification.
+    - A generally audio quality test, consisting of compressing and decompressing a speeech
+      file for audible comparison.
+
+The speech file should be recorded at 16 bits/sample, 8000 samples/second, and named
+"pre_g726.wav".
+
+\section g726_tests_page_sec_2 How is it used?
+To perform the tests in the G.726 specification you need to obtain the test data files from the
+specification. These are copyright material, and so cannot be distributed with this test software.
+
+The files, containing test vectors, which are supplied with the G.726 specification, should be
+copied to itutests/g726 so the files are arranged in the same directory heirarchy in which they
+are supplied. That is, you should have file names like
+
+    - itutests/g726/DISK1/INPUT/NRM.M
+    - itutests/g726/DISK1/INPUT/OVR.M
+    - itutests/g726/DISK2/INPUT/NRM.A
+    - itutests/g726/DISK2/INPUT/OVR.A
+
+in your source tree. The ITU tests can then be run by executing g726_tests without
+any parameters.
+
+To perform a general audio quality test, g726_tests should be run with a parameter specifying
+the required bit rate for compression. The valid parameters are "-16", "-24", "-32", and "-40".
+The test file ../test-data/local/short_nb_voice.wav will be compressed to the specified bit rate,
+decompressed, and the resulting audio stored in post_g726.wav.
+*/
+
+#if defined(HAVE_CONFIG_H)
+#include <config.h>
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <memory.h>
+#include <ctype.h>
+#include <audiofile.h>
+
+#include "spandsp.h"
+#include "spandsp-sim.h"
+
+#define BLOCK_LEN           320
+#define MAX_TEST_VECTOR_LEN 40000
+
+#define TESTDATA_DIR    "../test-data/itu/g726/"
+
+#define IN_FILE_NAME    "../test-data/local/short_nb_voice.wav"
+#define OUT_FILE_NAME   "post_g726.wav"
+
+int16_t outdata[MAX_TEST_VECTOR_LEN];
+uint8_t adpcmdata[MAX_TEST_VECTOR_LEN];
+
+int16_t itudata[MAX_TEST_VECTOR_LEN];
+uint8_t itu_ref[MAX_TEST_VECTOR_LEN];
+uint8_t unpacked[MAX_TEST_VECTOR_LEN];
+uint8_t xlaw[MAX_TEST_VECTOR_LEN];
+
+/*
+Table 4 - V Reset and homing sequences for u-law
+            Normal                              I-input     Overload
+Algorithm   Input   Intermediate    Output      Input       Output      Input   Intermediate    Output
+            (PCM)   (ADPCM)         (PCM)       (ADPCM)     (PCM)       (PCM)   (ADPCM)         (PCM)
+
+16F         NRM.M   RN16FM.I        RN16FM.O    I16         RI16FM.O    OVR.M   RV16FM.I        RV16FM.O
+                    HN16FM.I        HN16FM.O                HI16FM.O            HV16FM.I        HV16FM.O
+
+24F         NRM.M   RN24FM.I        RN24FM.O    I24         RI24FM.O    OVR.M   RV24FM.I        RV24FM.O
+                    HN24FM.I        HN24FM.O                HI24FM.O            HV24FM.I        HV24FM.O
+
+32F         NRM.M   RN32FM.I        RN32FM.O    I32         RI32FM.O    OVR.M   RV32FM.I        RV32FM.O
+                    HN32FM.I        HN32FM.O                HI32FM.O            HV32FM.I        HV32FM.O
+
+40F         NRM.M   RN40FM.I        RN40FM.O    I40         RI40FM.O    OVR.M   RV40FM.I        RV40FM.O
+                    HN40FM.I        HN40FM.O                HI40FM.O            HV40FM.I        HV40FM.O
+
+
+Table 5 - V Reset and homing sequences for A-law
+            Normal                              I-input     Overload
+Algorithm   Input   Intermediate    Output      Input       Output      Input   Intermediate    Output
+            (PCM)   (ADPCM)         (PCM)       (ADPCM)     (PCM)       (PCM)   (ADPCM)         (PCM)
+16F         NRM.A   RN16FA.I        RN16FA.O    I16         RI16FA.O    OVR.A   RV16FA.I        RV16FA.O
+                    HN16FA.I        HN16FA.O                HI16FA.O            HV16FA.I        HV16FA.O
+
+24F         NRM.A   RN24FA.I        RN24FA.O    I24         RI24FA.O    OVR.A   RV24FA.I        RV24FA.O
+                    HN24FA.I        HN24FA.O                HI24FA.O            HV24FA.I        HV24FA.O
+
+32F         NRM.A   RN32FA.I        RN32FA.O    I32         RI32FA.O    OVR.A   RV32FA.I        RV32FA.O
+                    HN32FA.I        HN32FA.O                HI32FA.O            HV32FA.I        HV32FA.O
+
+40F         NRM.A   RN40FA.I        RN40FA.O    I40         RI40FA.O    OVR.A   RV40FA.I        RV40FA.O
+                    HN40FA.I        HN40FA.O                HI40FA.O            HV40FA.I        HV40FA.O
+
+Table 6 ¡V Reset and homing cross sequences for u-law -> A-law
+            Normal                              Overload
+Algorithm   Input   Intermediate    Output      Input   Intermediate    Output
+            (PCM)   (ADPCM)         (PCM)       (PCM)   (ADPCM)         (PCM)
+16F         NRM.M   RN16FM.I        RN16FC.O    OVR.M   RV16FM.I        RV16FC.O
+                    HN16FM.I        HN16FC.O            HV16FM.I        HV16FC.O
+
+24F         NRM.M   RN24FM.I        RN24FC.O    OVR.M   RV24FM.I        RV24FC.O
+                    HN24FM.I        HN24FC.O            HV24FM.I        HV24FC.O
+
+32F         NRM.M   RN32FM.I        RN32FC.O    OVR.M   RV32FM.I        RV32FC.O
+                    HN32FM.I        HN32FC.O            HV32FM.I        HV32FC.O
+
+40F         NRM.M   RN40FM.I        RN40FC.O    OVR.M   RV40FM.I        RV40FC.O
+                    HN40FM.I        HN40FC.O            HV40FM.I        HV40FC.O
+
+Table 7 ¡V Reset and homing cross sequences for A-law -> u-law
+            Normal                              Overload
+Algorithm   Input   Intermediate    Output      Input   Intermediate    Output
+            (PCM)   (ADPCM)         (PCM)       (PCM)   (ADPCM)         (PCM)
+16F         NRM.A   RN16FA.I        RN16FX.O    OVR.A   RV16FA.I        RV16FX.O
+                    HN16FA.I        HN16FX.O            HV16FA.I        HV16FX.O
+
+24F         NRM.A   RN24FA.I        RN24FX.O    OVR.A   RV24FA.I        RV24FX.O
+                    HN24FA.I        HN24FX.O            HV24FA.I        HV24FX.O
+
+32F         NRM.A   RN32FA.I        RN32FX.O    OVR.A   RV32FA.I        RV32FX.O
+                    HN32FA.I        HN32FX.O            HV32FA.I        HV32FX.O
+
+40F         NRM.A   RN40FA.I        RN40FX.O    OVR.A   RV40FA.I        RV40FX.O
+                    HN40FA.I        HN40FX.O            HV40FA.I        HV40FX.O
+*/
+
+#define	G726_ENCODING_NONE          9999
+
+typedef struct
+{
+    const char *conditioning_pcm_file;
+    const char *pcm_file;
+    const char *conditioning_adpcm_file;
+    const char *adpcm_file;
+    const char *output_file;
+    int rate;
+    int compression_law;
+    int decompression_law;
+} test_set_t;
+
+static test_set_t itu_test_sets[] =
+{
+    /* u-law to u-law tests */
+    {
+        "",
+        TESTDATA_DIR "DISK1/INPUT/NRM.M",
+        "",
+        TESTDATA_DIR "DISK1/RESET/16/RN16FM.I",
+        TESTDATA_DIR "DISK1/RESET/16/RN16FM.O",
+        16000,
+        G726_ENCODING_ULAW,
+        G726_ENCODING_ULAW
+    },
+    {
+        "",
+        "",
+        "",
+        TESTDATA_DIR "DISK1/INPUT/I16",
+        TESTDATA_DIR "DISK1/RESET/16/RI16FM.O",
+        16000,
+        G726_ENCODING_NONE,
+        G726_ENCODING_ULAW
+    },
+    {
+        "",
+        TESTDATA_DIR "DISK1/INPUT/OVR.M",
+        "",
+        TESTDATA_DIR "DISK1/RESET/16/RV16FM.I",
+        TESTDATA_DIR "DISK1/RESET/16/RV16FM.O",
+        16000,
+        G726_ENCODING_ULAW,
+        G726_ENCODING_ULAW
+    },
+    {
+        "",
+        TESTDATA_DIR "DISK1/INPUT/NRM.M",
+        "",
+        TESTDATA_DIR "DISK1/RESET/24/RN24FM.I",
+        TESTDATA_DIR "DISK1/RESET/24/RN24FM.O",
+        24000,
+        G726_ENCODING_ULAW,
+        G726_ENCODING_ULAW
+    },
+    {
+        "",
+        "",
+        "",
+        TESTDATA_DIR "DISK1/INPUT/I24",
+        TESTDATA_DIR "DISK1/RESET/24/RI24FM.O",
+        24000,
+        G726_ENCODING_NONE,
+        G726_ENCODING_ULAW
+    },
+    {
+        "",
+        TESTDATA_DIR "DISK1/INPUT/OVR.M",
+        "",
+        TESTDATA_DIR "DISK1/RESET/24/RV24FM.I",
+        TESTDATA_DIR "DISK1/RESET/24/RV24FM.O",
+        24000,
+        G726_ENCODING_ULAW,
+        G726_ENCODING_ULAW
+    },
+    {
+        "",
+        TESTDATA_DIR "DISK1/INPUT/NRM.M",
+        "",
+        TESTDATA_DIR "DISK1/RESET/32/RN32FM.I",
+        TESTDATA_DIR "DISK1/RESET/32/RN32FM.O",
+        32000,
+        G726_ENCODING_ULAW,
+        G726_ENCODING_ULAW
+    },
+    {
+        "",
+        "",
+        "",
+        TESTDATA_DIR "DISK1/INPUT/I32",
+        TESTDATA_DIR "DISK1/RESET/32/RI32FM.O",
+        32000,
+        G726_ENCODING_NONE,
+        G726_ENCODING_ULAW
+    },
+    {
+        "",
+        TESTDATA_DIR "DISK1/INPUT/OVR.M",
+        "",
+        TESTDATA_DIR "DISK1/RESET/32/RV32FM.I",
+        TESTDATA_DIR "DISK1/RESET/32/RV32FM.O",
+        32000,
+        G726_ENCODING_ULAW,
+        G726_ENCODING_ULAW
+    },
+    {
+        "",
+        TESTDATA_DIR "DISK1/INPUT/NRM.M",
+        "",
+        TESTDATA_DIR "DISK1/RESET/40/RN40FM.I",
+        TESTDATA_DIR "DISK1/RESET/40/RN40FM.O",
+        40000,
+        G726_ENCODING_ULAW,
+        G726_ENCODING_ULAW
+    },
+    {
+        "",
+        "",
+        "",
+        TESTDATA_DIR "DISK1/INPUT/I40",
+        TESTDATA_DIR "DISK1/RESET/40/RI40FM.O",
+        40000,
+        G726_ENCODING_NONE,
+        G726_ENCODING_ULAW
+    },
+    {
+        "",
+        TESTDATA_DIR "DISK1/INPUT/OVR.M",
+        "",
+        TESTDATA_DIR "DISK1/RESET/40/RV40FM.I",
+        TESTDATA_DIR "DISK1/RESET/40/RV40FM.O",
+        40000,
+        G726_ENCODING_ULAW,
+        G726_ENCODING_ULAW
+    },
+    /* A-law to A-law tests */
+    {
+        "",
+        TESTDATA_DIR "DISK2/INPUT/NRM.A",
+        "",
+        TESTDATA_DIR "DISK2/RESET/16/RN16FA.I",
+        TESTDATA_DIR "DISK2/RESET/16/RN16FA.O",
+        16000,
+        G726_ENCODING_ALAW,
+        G726_ENCODING_ALAW
+    },
+    {
+        "",
+        "",
+        "",
+        TESTDATA_DIR "DISK2/INPUT/I16",
+        TESTDATA_DIR "DISK2/RESET/16/RI16FA.O",
+        16000,
+        G726_ENCODING_NONE,
+        G726_ENCODING_ALAW
+    },
+    {
+        "",
+        TESTDATA_DIR "DISK2/INPUT/OVR.A",
+        "",
+        TESTDATA_DIR "DISK2/RESET/16/RV16FA.I",
+        TESTDATA_DIR "DISK2/RESET/16/RV16FA.O",
+        16000,
+        G726_ENCODING_ALAW,
+        G726_ENCODING_ALAW
+    },
+    {
+        "",
+        TESTDATA_DIR "DISK2/INPUT/NRM.A",
+        "",
+        TESTDATA_DIR "DISK2/RESET/24/RN24FA.I",
+        TESTDATA_DIR "DISK2/RESET/24/RN24FA.O",
+        24000,
+        G726_ENCODING_ALAW,
+        G726_ENCODING_ALAW
+    },
+    {
+        "",
+        "",
+        "",
+        TESTDATA_DIR "DISK2/INPUT/I24",
+        TESTDATA_DIR "DISK2/RESET/24/RI24FA.O",
+        24000,
+        G726_ENCODING_NONE,
+        G726_ENCODING_ALAW
+    },
+    {
+        "",
+        TESTDATA_DIR "DISK2/INPUT/OVR.A",
+        "",
+        TESTDATA_DIR "DISK2/RESET/24/RV24FA.I",
+        TESTDATA_DIR "DISK2/RESET/24/RV24FA.O",
+        24000,
+        G726_ENCODING_ALAW,
+        G726_ENCODING_ALAW
+    },
+    {
+        "",
+        TESTDATA_DIR "DISK2/INPUT/NRM.A",
+        "",
+        TESTDATA_DIR "DISK2/RESET/32/RN32FA.I",
+        TESTDATA_DIR "DISK2/RESET/32/RN32FA.O",
+        32000,
+        G726_ENCODING_ALAW,
+        G726_ENCODING_ALAW
+    },
+    {
+        "",
+        "",
+        "",
+        TESTDATA_DIR "DISK2/INPUT/I32",
+        TESTDATA_DIR "DISK2/RESET/32/RI32FA.O",
+        32000,
+        G726_ENCODING_NONE,
+        G726_ENCODING_ALAW
+    },
+    {
+        "",
+        TESTDATA_DIR "DISK2/INPUT/OVR.A",
+        "",
+        TESTDATA_DIR "DISK2/RESET/32/RV32FA.I",
+        TESTDATA_DIR "DISK2/RESET/32/RV32FA.O",
+        32000,
+        G726_ENCODING_ALAW,
+        G726_ENCODING_ALAW
+    },
+    {
+        "",
+        TESTDATA_DIR "DISK2/INPUT/NRM.A",
+        "",
+        TESTDATA_DIR "DISK2/RESET/40/RN40FA.I",
+        TESTDATA_DIR "DISK2/RESET/40/RN40FA.O",
+        40000,
+        G726_ENCODING_ALAW,
+        G726_ENCODING_ALAW
+    },
+    {
+        "",
+        "",
+        "",
+        TESTDATA_DIR "DISK2/INPUT/I40",
+        TESTDATA_DIR "DISK2/RESET/40/RI40FA.O",
+        40000,
+        G726_ENCODING_NONE,
+        G726_ENCODING_ALAW
+    },
+    {
+        "",
+        TESTDATA_DIR "DISK2/INPUT/OVR.A",
+        "",
+        TESTDATA_DIR "DISK2/RESET/40/RV40FA.I",
+        TESTDATA_DIR "DISK2/RESET/40/RV40FA.O",
+        40000,
+        G726_ENCODING_ALAW,
+        G726_ENCODING_ALAW
+    },
+    /* u-law to A-law tests */
+    {
+        "",
+        TESTDATA_DIR "DISK1/INPUT/NRM.M",
+        "",
+        TESTDATA_DIR "DISK1/RESET/16/RN16FM.I",
+        TESTDATA_DIR "DISK1/RESET/16/RN16FC.O",
+        16000,
+        G726_ENCODING_ULAW,
+        G726_ENCODING_ALAW
+    },
+    {
+        "",
+        TESTDATA_DIR "DISK1/INPUT/OVR.M",
+        "",
+        TESTDATA_DIR "DISK1/RESET/16/RV16FM.I",
+        TESTDATA_DIR "DISK1/RESET/16/RV16FC.O",
+        16000,
+        G726_ENCODING_ULAW,
+        G726_ENCODING_ALAW
+    },
+    {
+        "",
+        TESTDATA_DIR "DISK1/INPUT/NRM.M",
+        "",
+        TESTDATA_DIR "DISK1/RESET/24/RN24FM.I",
+        TESTDATA_DIR "DISK1/RESET/24/RN24FC.O",
+        24000,
+        G726_ENCODING_ULAW,
+        G726_ENCODING_ALAW
+    },
+    {
+        "",
+        TESTDATA_DIR "DISK1/INPUT/OVR.M",
+        "",
+        TESTDATA_DIR "DISK1/RESET/24/RV24FM.I",
+        TESTDATA_DIR "DISK1/RESET/24/RV24FC.O",
+        24000,
+        G726_ENCODING_ULAW,
+        G726_ENCODING_ALAW
+    },
+    {
+        "",
+        TESTDATA_DIR "DISK1/INPUT/NRM.M",
+        "",
+        TESTDATA_DIR "DISK1/RESET/32/RN32FM.I",
+        TESTDATA_DIR "DISK1/RESET/32/RN32FC.O",
+        32000,
+        G726_ENCODING_ULAW,
+        G726_ENCODING_ALAW
+    },
+    {
+        "",
+        TESTDATA_DIR "DISK1/INPUT/OVR.M",
+        "",
+        TESTDATA_DIR "DISK1/RESET/32/RV32FM.I",
+        TESTDATA_DIR "DISK1/RESET/32/RV32FC.O",
+        32000,
+        G726_ENCODING_ULAW,
+        G726_ENCODING_ALAW
+    },
+    {
+        "",
+        TESTDATA_DIR "DISK1/INPUT/NRM.M",
+        "",
+        TESTDATA_DIR "DISK1/RESET/40/RN40FM.I",
+        TESTDATA_DIR "DISK1/RESET/40/RN40FC.O",
+        40000,
+        G726_ENCODING_ULAW,
+        G726_ENCODING_ALAW
+    },
+    {
+        "",
+        TESTDATA_DIR "DISK1/INPUT/OVR.M",
+        "",
+        TESTDATA_DIR "DISK1/RESET/40/RV40FM.I",
+        TESTDATA_DIR "DISK1/RESET/40/RV40FC.O",
+        40000,
+        G726_ENCODING_ULAW,
+        G726_ENCODING_ALAW
+    },
+    /* A-law to u-law tests */
+    {
+        "",
+        TESTDATA_DIR "DISK2/INPUT/NRM.A",
+        "",
+        TESTDATA_DIR "DISK2/RESET/16/RN16FA.I",
+        TESTDATA_DIR "DISK2/RESET/16/RN16FX.O",
+        16000,
+        G726_ENCODING_ALAW,
+        G726_ENCODING_ULAW
+    },
+    {
+        "",
+        TESTDATA_DIR "DISK2/INPUT/OVR.A",
+        "",
+        TESTDATA_DIR "DISK2/RESET/16/RV16FA.I",
+        TESTDATA_DIR "DISK2/RESET/16/RV16FX.O",
+        16000,
+        G726_ENCODING_ALAW,
+        G726_ENCODING_ULAW
+    },
+    {
+        "",
+        TESTDATA_DIR "DISK2/INPUT/NRM.A",
+        "",
+        TESTDATA_DIR "DISK2/RESET/24/RN24FA.I",
+        TESTDATA_DIR "DISK2/RESET/24/RN24FX.O",
+        24000,
+        G726_ENCODING_ALAW,
+        G726_ENCODING_ULAW
+    },
+    {
+        "",
+        TESTDATA_DIR "DISK2/INPUT/OVR.A",
+        "",
+        TESTDATA_DIR "DISK2/RESET/24/RV24FA.I",
+        TESTDATA_DIR "DISK2/RESET/24/RV24FX.O",
+        24000,
+        G726_ENCODING_ALAW,
+        G726_ENCODING_ULAW
+    },
+    {
+        "",
+        TESTDATA_DIR "DISK2/INPUT/NRM.A",
+        "",
+        TESTDATA_DIR "DISK2/RESET/32/RN32FA.I",
+        TESTDATA_DIR "DISK2/RESET/32/RN32FX.O",
+        32000,
+        G726_ENCODING_ALAW,
+        G726_ENCODING_ULAW
+    },
+    {
+        "",
+        TESTDATA_DIR "DISK2/INPUT/OVR.A",
+        "",
+        TESTDATA_DIR "DISK2/RESET/32/RV32FA.I",
+        TESTDATA_DIR "DISK2/RESET/32/RV32FX.O",
+        32000,
+        G726_ENCODING_ALAW,
+        G726_ENCODING_ULAW
+    },
+    {
+        "",
+        TESTDATA_DIR "DISK2/INPUT/NRM.A",
+        "",
+        TESTDATA_DIR "DISK2/RESET/40/RN40FA.I",
+        TESTDATA_DIR "DISK2/RESET/40/RN40FX.O",
+        40000,
+        G726_ENCODING_ALAW,
+        G726_ENCODING_ULAW
+    },
+    {
+        "",
+        TESTDATA_DIR "DISK2/INPUT/OVR.A",
+        "",
+        TESTDATA_DIR "DISK2/RESET/40/RV40FA.I",
+        TESTDATA_DIR "DISK2/RESET/40/RV40FX.O",
+        40000,
+        G726_ENCODING_ALAW,
+        G726_ENCODING_ULAW
+    },
+    /* u-law to u-law tests */
+    {
+        TESTDATA_DIR "DISK1/PCM_INIT.M",
+        TESTDATA_DIR "DISK1/INPUT/NRM.M",
+        TESTDATA_DIR "DISK1/HOMING/16/I_INI_16.M",
+        TESTDATA_DIR "DISK1/HOMING/16/HN16FM.I",
+        TESTDATA_DIR "DISK1/HOMING/16/HN16FM.O",
+        16000,
+        G726_ENCODING_ULAW,
+        G726_ENCODING_ULAW
+    },
+    {
+        "",
+        "",
+        TESTDATA_DIR "DISK1/HOMING/16/I_INI_16.M",
+        TESTDATA_DIR "DISK1/INPUT/I16",
+        TESTDATA_DIR "DISK1/HOMING/16/HI16FM.O",
+        16000,
+        G726_ENCODING_NONE,
+        G726_ENCODING_ULAW
+    },
+    {
+        TESTDATA_DIR "DISK1/PCM_INIT.M",
+        TESTDATA_DIR "DISK1/INPUT/OVR.M",
+        TESTDATA_DIR "DISK1/HOMING/16/I_INI_16.M",
+        TESTDATA_DIR "DISK1/HOMING/16/HV16FM.I",
+        TESTDATA_DIR "DISK1/HOMING/16/HV16FM.O",
+        16000,
+        G726_ENCODING_ULAW,
+        G726_ENCODING_ULAW
+    },
+    {
+        TESTDATA_DIR "DISK1/PCM_INIT.M",
+        TESTDATA_DIR "DISK1/INPUT/NRM.M",
+        TESTDATA_DIR "DISK1/HOMING/24/I_INI_24.M",
+        TESTDATA_DIR "DISK1/HOMING/24/HN24FM.I",
+        TESTDATA_DIR "DISK1/HOMING/24/HN24FM.O",
+        24000,
+        G726_ENCODING_ULAW,
+        G726_ENCODING_ULAW
+    },
+    {
+        "",
+        "",
+        TESTDATA_DIR "DISK1/HOMING/24/I_INI_24.M",
+        TESTDATA_DIR "DISK1/INPUT/I24",
+        TESTDATA_DIR "DISK1/HOMING/24/HI24FM.O",
+        24000,
+        G726_ENCODING_NONE,
+        G726_ENCODING_ULAW
+    },
+    {
+        TESTDATA_DIR "DISK1/PCM_INIT.M",
+        TESTDATA_DIR "DISK1/INPUT/OVR.M",
+        TESTDATA_DIR "DISK1/HOMING/24/I_INI_24.M",
+        TESTDATA_DIR "DISK1/HOMING/24/HV24FM.I",
+        TESTDATA_DIR "DISK1/HOMING/24/HV24FM.O",
+        24000,
+        G726_ENCODING_ULAW,
+        G726_ENCODING_ULAW
+    },
+    {
+        TESTDATA_DIR "DISK1/PCM_INIT.M",
+        TESTDATA_DIR "DISK1/INPUT/NRM.M",
+        TESTDATA_DIR "DISK1/HOMING/32/I_INI_32.M",
+        TESTDATA_DIR "DISK1/HOMING/32/HN32FM.I",
+        TESTDATA_DIR "DISK1/HOMING/32/HN32FM.O",
+        32000,
+        G726_ENCODING_ULAW,
+        G726_ENCODING_ULAW
+    },
+    {
+        "",
+        "",
+        TESTDATA_DIR "DISK1/HOMING/32/I_INI_32.M",
+        TESTDATA_DIR "DISK1/INPUT/I32",
+        TESTDATA_DIR "DISK1/HOMING/32/HI32FM.O",
+        32000,
+        G726_ENCODING_NONE,
+        G726_ENCODING_ULAW
+    },
+    {
+        TESTDATA_DIR "DISK1/PCM_INIT.M",
+        TESTDATA_DIR "DISK1/INPUT/OVR.M",
+        TESTDATA_DIR "DISK1/HOMING/32/I_INI_32.M",
+        TESTDATA_DIR "DISK1/HOMING/32/HV32FM.I",
+        TESTDATA_DIR "DISK1/HOMING/32/HV32FM.O",
+        32000,
+        G726_ENCODING_ULAW,
+        G726_ENCODING_ULAW
+    },
+    {
+        TESTDATA_DIR "DISK1/PCM_INIT.M",
+        TESTDATA_DIR "DISK1/INPUT/NRM.M",
+        TESTDATA_DIR "DISK1/HOMING/40/I_INI_40.M",
+        TESTDATA_DIR "DISK1/HOMING/40/HN40FM.I",
+        TESTDATA_DIR "DISK1/HOMING/40/HN40FM.O",
+        40000,
+        G726_ENCODING_ULAW,
+        G726_ENCODING_ULAW
+    },
+    {
+        "",
+        "",
+        TESTDATA_DIR "DISK1/HOMING/40/I_INI_40.M",
+        TESTDATA_DIR "DISK1/INPUT/I40",
+        TESTDATA_DIR "DISK1/HOMING/40/HI40FM.O",
+        40000,
+        G726_ENCODING_NONE,
+        G726_ENCODING_ULAW
+    },
+    {
+        TESTDATA_DIR "DISK1/PCM_INIT.M",
+        TESTDATA_DIR "DISK1/INPUT/OVR.M",
+        TESTDATA_DIR "DISK1/HOMING/40/I_INI_40.M",
+        TESTDATA_DIR "DISK1/HOMING/40/HV40FM.I",
+        TESTDATA_DIR "DISK1/HOMING/40/HV40FM.O",
+        40000,
+        G726_ENCODING_ULAW,
+        G726_ENCODING_ULAW
+    },
+    /* A-law to A-law tests */
+    {
+        TESTDATA_DIR "DISK2/PCM_INIT.A",
+        TESTDATA_DIR "DISK2/INPUT/NRM.A",
+        TESTDATA_DIR "DISK2/HOMING/16/I_INI_16.A",
+        TESTDATA_DIR "DISK2/HOMING/16/HN16FA.I",
+        TESTDATA_DIR "DISK2/HOMING/16/HN16FA.O",
+        16000,
+        G726_ENCODING_ALAW,
+        G726_ENCODING_ALAW
+    },
+    {
+        "",
+        "",
+        TESTDATA_DIR "DISK2/HOMING/16/I_INI_16.A",
+        TESTDATA_DIR "DISK2/INPUT/I16",
+        TESTDATA_DIR "DISK2/HOMING/16/HI16FA.O",
+        16000,
+        G726_ENCODING_NONE,
+        G726_ENCODING_ALAW
+    },
+    {
+        TESTDATA_DIR "DISK2/PCM_INIT.A",
+        TESTDATA_DIR "DISK2/INPUT/OVR.A",
+        TESTDATA_DIR "DISK2/HOMING/16/I_INI_16.A",
+        TESTDATA_DIR "DISK2/HOMING/16/HV16FA.I",
+        TESTDATA_DIR "DISK2/HOMING/16/HV16FA.O",
+        16000,
+        G726_ENCODING_ALAW,
+        G726_ENCODING_ALAW
+    },
+    {
+        TESTDATA_DIR "DISK2/PCM_INIT.A",
+        TESTDATA_DIR "DISK2/INPUT/NRM.A",
+        TESTDATA_DIR "DISK2/HOMING/24/I_INI_24.A",
+        TESTDATA_DIR "DISK2/HOMING/24/HN24FA.I",
+        TESTDATA_DIR "DISK2/HOMING/24/HN24FA.O",
+        24000,
+        G726_ENCODING_ALAW,
+        G726_ENCODING_ALAW
+    },
+    {
+        "",
+        "",
+        TESTDATA_DIR "DISK2/HOMING/24/I_INI_24.A",
+        TESTDATA_DIR "DISK2/INPUT/I24",
+        TESTDATA_DIR "DISK2/HOMING/24/HI24FA.O",
+        24000,
+        G726_ENCODING_NONE,
+        G726_ENCODING_ALAW
+    },
+    {
+        TESTDATA_DIR "DISK2/PCM_INIT.A",
+        TESTDATA_DIR "DISK2/INPUT/OVR.A",
+        TESTDATA_DIR "DISK2/HOMING/24/I_INI_24.A",
+        TESTDATA_DIR "DISK2/HOMING/24/HV24FA.I",
+        TESTDATA_DIR "DISK2/HOMING/24/HV24FA.O",
+        24000,
+        G726_ENCODING_ALAW,
+        G726_ENCODING_ALAW
+    },
+    {
+        TESTDATA_DIR "DISK2/PCM_INIT.A",
+        TESTDATA_DIR "DISK2/INPUT/NRM.A",
+        TESTDATA_DIR "DISK2/HOMING/32/I_INI_32.A",
+        TESTDATA_DIR "DISK2/HOMING/32/HN32FA.I",
+        TESTDATA_DIR "DISK2/HOMING/32/HN32FA.O",
+        32000,
+        G726_ENCODING_ALAW,
+        G726_ENCODING_ALAW
+    },
+    {
+        "",
+        "",
+        TESTDATA_DIR "DISK2/HOMING/32/I_INI_32.A",
+        TESTDATA_DIR "DISK2/INPUT/I32",
+        TESTDATA_DIR "DISK2/HOMING/32/HI32FA.O",
+        32000,
+        G726_ENCODING_NONE,
+        G726_ENCODING_ALAW
+    },
+    {
+        TESTDATA_DIR "DISK2/PCM_INIT.A",
+        TESTDATA_DIR "DISK2/INPUT/OVR.A",
+        TESTDATA_DIR "DISK2/HOMING/32/I_INI_32.A",
+        TESTDATA_DIR "DISK2/HOMING/32/HV32FA.I",
+        TESTDATA_DIR "DISK2/HOMING/32/HV32FA.O",
+        32000,
+        G726_ENCODING_ALAW,
+        G726_ENCODING_ALAW
+    },
+    {
+        TESTDATA_DIR "DISK2/PCM_INIT.A",
+        TESTDATA_DIR "DISK2/INPUT/NRM.A",
+        TESTDATA_DIR "DISK2/HOMING/40/I_INI_40.A",
+        TESTDATA_DIR "DISK2/HOMING/40/HN40FA.I",
+        TESTDATA_DIR "DISK2/HOMING/40/HN40FA.O",
+        40000,
+        G726_ENCODING_ALAW,
+        G726_ENCODING_ALAW
+    },
+    {
+        "",
+        "",
+        TESTDATA_DIR "DISK2/HOMING/40/I_INI_40.A",
+        TESTDATA_DIR "DISK2/INPUT/I40",
+        TESTDATA_DIR "DISK2/HOMING/40/HI40FA.O",
+        40000,
+        G726_ENCODING_NONE,
+        G726_ENCODING_ALAW
+    },
+    {
+        TESTDATA_DIR "DISK2/PCM_INIT.A",
+        TESTDATA_DIR "DISK2/INPUT/OVR.A",
+        TESTDATA_DIR "DISK2/HOMING/40/I_INI_40.A",
+        TESTDATA_DIR "DISK2/HOMING/40/HV40FA.I",
+        TESTDATA_DIR "DISK2/HOMING/40/HV40FA.O",
+        40000,
+        G726_ENCODING_ALAW,
+        G726_ENCODING_ALAW
+    },
+    /* u-law to A-law tests */
+    {
+        TESTDATA_DIR "DISK1/PCM_INIT.M",
+        TESTDATA_DIR "DISK1/INPUT/NRM.M",
+        TESTDATA_DIR "DISK2/HOMING/16/I_INI_16.A",
+        TESTDATA_DIR "DISK1/HOMING/16/HN16FM.I",
+        TESTDATA_DIR "DISK1/HOMING/16/HN16FC.O",
+        16000,
+        G726_ENCODING_ULAW,
+        G726_ENCODING_ALAW
+    },
+    {
+        TESTDATA_DIR "DISK1/PCM_INIT.M",
+        TESTDATA_DIR "DISK1/INPUT/OVR.M",
+        TESTDATA_DIR "DISK2/HOMING/16/I_INI_16.A",
+        TESTDATA_DIR "DISK1/HOMING/16/HV16FM.I",
+        TESTDATA_DIR "DISK1/HOMING/16/HV16FC.O",
+        16000,
+        G726_ENCODING_ULAW,
+        G726_ENCODING_ALAW
+    },
+    {
+        TESTDATA_DIR "DISK1/PCM_INIT.M",
+        TESTDATA_DIR "DISK1/INPUT/NRM.M",
+        TESTDATA_DIR "DISK2/HOMING/24/I_INI_24.A",
+        TESTDATA_DIR "DISK1/HOMING/24/HN24FM.I",
+        TESTDATA_DIR "DISK1/HOMING/24/HN24FC.O",
+        24000,
+        G726_ENCODING_ULAW,
+        G726_ENCODING_ALAW
+    },
+    {
+        TESTDATA_DIR "DISK1/PCM_INIT.M",
+        TESTDATA_DIR "DISK1/INPUT/OVR.M",
+        TESTDATA_DIR "DISK2/HOMING/24/I_INI_24.A",
+        TESTDATA_DIR "DISK1/HOMING/24/HV24FM.I",
+        TESTDATA_DIR "DISK1/HOMING/24/HV24FC.O",
+        24000,
+        G726_ENCODING_ULAW,
+        G726_ENCODING_ALAW
+    },
+    {
+        TESTDATA_DIR "DISK1/PCM_INIT.M",
+        TESTDATA_DIR "DISK1/INPUT/NRM.M",
+        TESTDATA_DIR "DISK2/HOMING/32/I_INI_32.A",
+        TESTDATA_DIR "DISK1/HOMING/32/HN32FM.I",
+        TESTDATA_DIR "DISK1/HOMING/32/HN32FC.O",
+        32000,
+        G726_ENCODING_ULAW,
+        G726_ENCODING_ALAW
+    },
+    {
+        TESTDATA_DIR "DISK1/PCM_INIT.M",
+        TESTDATA_DIR "DISK1/INPUT/OVR.M",
+        TESTDATA_DIR "DISK2/HOMING/32/I_INI_32.A",
+        TESTDATA_DIR "DISK1/HOMING/32/HV32FM.I",
+        TESTDATA_DIR "DISK1/HOMING/32/HV32FC.O",
+        32000,
+        G726_ENCODING_ULAW,
+        G726_ENCODING_ALAW
+    },
+    {
+        TESTDATA_DIR "DISK1/PCM_INIT.M",
+        TESTDATA_DIR "DISK1/INPUT/NRM.M",
+        TESTDATA_DIR "DISK2/HOMING/40/I_INI_40.A",
+        TESTDATA_DIR "DISK1/HOMING/40/HN40FM.I",
+        TESTDATA_DIR "DISK1/HOMING/40/HN40FC.O",
+        40000,
+        G726_ENCODING_ULAW,
+        G726_ENCODING_ALAW
+    },
+    {
+        TESTDATA_DIR "DISK1/PCM_INIT.M",
+        TESTDATA_DIR "DISK1/INPUT/OVR.M",
+        TESTDATA_DIR "DISK2/HOMING/40/I_INI_40.A",
+        TESTDATA_DIR "DISK1/HOMING/40/HV40FM.I",
+        TESTDATA_DIR "DISK1/HOMING/40/HV40FC.O",
+        40000,
+        G726_ENCODING_ULAW,
+        G726_ENCODING_ALAW
+    },
+    /* A-law to u-law tests */
+    {
+        TESTDATA_DIR "DISK2/PCM_INIT.A",
+        TESTDATA_DIR "DISK2/INPUT/NRM.A",
+        TESTDATA_DIR "DISK1/HOMING/16/I_INI_16.M",
+        TESTDATA_DIR "DISK2/HOMING/16/HN16FA.I",
+        TESTDATA_DIR "DISK2/HOMING/16/HN16FX.O",
+        16000,
+        G726_ENCODING_ALAW,
+        G726_ENCODING_ULAW
+    },
+    {
+        TESTDATA_DIR "DISK2/PCM_INIT.A",
+        TESTDATA_DIR "DISK2/INPUT/OVR.A",
+        TESTDATA_DIR "DISK1/HOMING/16/I_INI_16.M",
+        TESTDATA_DIR "DISK2/HOMING/16/HV16FA.I",
+        TESTDATA_DIR "DISK2/HOMING/16/HV16FX.O",
+        16000,
+        G726_ENCODING_ALAW,
+        G726_ENCODING_ULAW
+    },
+    {
+        TESTDATA_DIR "DISK2/PCM_INIT.A",
+        TESTDATA_DIR "DISK2/INPUT/NRM.A",
+        TESTDATA_DIR "DISK1/HOMING/24/I_INI_24.M",
+        TESTDATA_DIR "DISK2/HOMING/24/HN24FA.I",
+        TESTDATA_DIR "DISK2/HOMING/24/HN24FX.O",
+        24000,
+        G726_ENCODING_ALAW,
+        G726_ENCODING_ULAW
+    },
+    {
+        TESTDATA_DIR "DISK2/PCM_INIT.A",
+        TESTDATA_DIR "DISK2/INPUT/OVR.A",
+        TESTDATA_DIR "DISK1/HOMING/24/I_INI_24.M",
+        TESTDATA_DIR "DISK2/HOMING/24/HV24FA.I",
+        TESTDATA_DIR "DISK2/HOMING/24/HV24FX.O",
+        24000,
+        G726_ENCODING_ALAW,
+        G726_ENCODING_ULAW
+    },
+    {
+        TESTDATA_DIR "DISK2/PCM_INIT.A",
+        TESTDATA_DIR "DISK2/INPUT/NRM.A",
+        TESTDATA_DIR "DISK1/HOMING/32/I_INI_32.M",
+        TESTDATA_DIR "DISK2/HOMING/32/HN32FA.I",
+        TESTDATA_DIR "DISK2/HOMING/32/HN32FX.O",
+        32000,
+        G726_ENCODING_ALAW,
+        G726_ENCODING_ULAW
+    },
+    {
+        TESTDATA_DIR "DISK2/PCM_INIT.A",
+        TESTDATA_DIR "DISK2/INPUT/OVR.A",
+        TESTDATA_DIR "DISK1/HOMING/32/I_INI_32.M",
+        TESTDATA_DIR "DISK2/HOMING/32/HV32FA.I",
+        TESTDATA_DIR "DISK2/HOMING/32/HV32FX.O",
+        32000,
+        G726_ENCODING_ALAW,
+        G726_ENCODING_ULAW
+    },
+    {
+        TESTDATA_DIR "DISK2/PCM_INIT.A",
+        TESTDATA_DIR "DISK2/INPUT/NRM.A",
+        TESTDATA_DIR "DISK1/HOMING/40/I_INI_40.M",
+        TESTDATA_DIR "DISK2/HOMING/40/HN40FA.I",
+        TESTDATA_DIR "DISK2/HOMING/40/HN40FX.O",
+        40000,
+        G726_ENCODING_ALAW,
+        G726_ENCODING_ULAW
+    },
+    {
+        TESTDATA_DIR "DISK2/PCM_INIT.A",
+        TESTDATA_DIR "DISK2/INPUT/OVR.A",
+        TESTDATA_DIR "DISK1/HOMING/40/I_INI_40.M",
+        TESTDATA_DIR "DISK2/HOMING/40/HV40FA.I",
+        TESTDATA_DIR "DISK2/HOMING/40/HV40FX.O",
+        40000,
+        G726_ENCODING_ALAW,
+        G726_ENCODING_ULAW
+    },
+    {
+        NULL,
+        NULL,
+        NULL,
+        NULL,
+        NULL,
+        0,
+        0,
+        0
+    }
+};
+
+static int hex_get(char *s)
+{
+    int i;
+    int value;
+    int x;
+
+    for (value = i = 0;  i < 2;  i++)
+    {
+        x = *s++ - 0x30;
+        if (x > 9)
+            x -= 0x07;
+        if (x > 15)
+            x -= 0x20;
+        if (x < 0  ||  x > 15)
+            return -1;
+        value <<= 4;
+        value |= x;
+    }
+    return value;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int get_vector(FILE *file, uint8_t vec[])
+{
+    char buf[132 + 1];
+    char *s;
+    int i;
+    int value;
+
+    while (fgets(buf, 133, file))
+    {
+        s = buf;
+        i = 0;
+        while ((value = hex_get(s)) >= 0)
+        {
+            vec[i++] = value;
+            s += 2;
+        }
+        return i;
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int get_test_vector(const char *file, uint8_t buf[], int max_len)
+{
+    int octets;
+    int i;
+    int sum;
+    FILE *infile;
+    
+    if ((infile = fopen(file, "r")) == NULL)
+    {
+        fprintf(stderr, "    Failed to open '%s'\n", file);
+        exit(2);
+    }
+    octets = 0;  
+    while ((i = get_vector(infile, buf + octets)) > 0)
+        octets += i;
+    fclose(infile);
+    /* The last octet is a sumcheck, so the real data octets are one less than
+       the total we have */
+    octets--;
+    /* Test the checksum */
+    for (sum = i = 0;  i < octets;  i++)
+        sum += buf[i];
+    if (sum%255 != (int) buf[i])
+    {
+        fprintf(stderr, "    Sumcheck failed in '%s' - %x %x\n", file, sum%255, buf[i]);
+        exit(2);
+    }
+    return octets;
+}
+/*- End of function --------------------------------------------------------*/
+
+int main(int argc, char *argv[])
+{
+    g726_state_t enc_state;
+    g726_state_t dec_state;
+    int len2;
+    int len3;
+    int i;
+    int test;
+    int bits_per_code;
+    int itutests;
+    int bit_rate;
+    int bad_samples;
+    AFfilehandle inhandle;
+    AFfilehandle outhandle;
+    int16_t amp[1024];
+    int frames;
+    int outframes;
+    int conditioning_samples;
+    int samples;
+    int conditioning_adpcm;
+    int adpcm;
+    int packing;
+
+    i = 1;
+    bit_rate = 32000;
+    itutests = TRUE;
+    packing = G726_PACKING_NONE;
+    while (argc > i)
+    {
+        if (strcmp(argv[i], "-16") == 0)
+        {
+            bit_rate = 16000;
+            itutests = FALSE;
+            i++;
+        }
+        else if (strcmp(argv[i], "-24") == 0)
+        {
+            bit_rate = 24000;
+            itutests = FALSE;
+            i++;
+        }
+        else if (strcmp(argv[i], "-32") == 0)
+        {
+            bit_rate = 32000;
+            itutests = FALSE;
+            i++;
+        }
+        else if (strcmp(argv[i], "-40") == 0)
+        {
+            bit_rate = 40000;
+            itutests = FALSE;
+            i++;
+        }
+        else if (strcmp(argv[i], "-l") == 0)
+        {
+            packing = G726_PACKING_LEFT;
+            i++;
+        }
+        else if (strcmp(argv[i], "-r") == 0)
+        {
+            packing = G726_PACKING_RIGHT;
+            i++;
+        }
+        else
+        {
+            fprintf(stderr, "Unknown parameter %s specified.\n", argv[i]);
+            exit(2);
+        }
+    }
+
+    len2 = 0;
+    conditioning_samples = 0;
+    if (itutests)
+    {
+        for (test = 0;  itu_test_sets[test].rate;  test++)
+        {
+            printf("Test %2d: '%s' + '%s'\n"
+                   "      -> '%s' + '%s'\n"
+                   "      -> '%s' [%d, %d, %d]\n",
+                   test,
+                   itu_test_sets[test].conditioning_pcm_file,
+                   itu_test_sets[test].pcm_file,
+                   itu_test_sets[test].conditioning_adpcm_file,
+                   itu_test_sets[test].adpcm_file,
+                   itu_test_sets[test].output_file,
+                   itu_test_sets[test].rate,
+                   itu_test_sets[test].compression_law,
+                   itu_test_sets[test].decompression_law);
+            switch (itu_test_sets[test].rate)
+            {
+            case 16000:
+                bits_per_code = 2;
+                break;
+            case 24000:
+                bits_per_code = 3;
+                break;
+            case 32000:
+            default:
+                bits_per_code = 4;
+                break;
+            case 40000:
+                bits_per_code = 5;
+                break;
+            }
+            if (itu_test_sets[test].compression_law != G726_ENCODING_NONE)
+            {
+                /* Test the encode side */
+                g726_init(&enc_state, itu_test_sets[test].rate, itu_test_sets[test].compression_law, G726_PACKING_NONE);
+                if (itu_test_sets[test].conditioning_pcm_file[0])
+                {
+                    conditioning_samples = get_test_vector(itu_test_sets[test].conditioning_pcm_file, xlaw, MAX_TEST_VECTOR_LEN);
+                    printf("Test %d: Homing %d samples at %dbps\n", test, conditioning_samples, itu_test_sets[test].rate);
+                }
+                else
+                {
+                    conditioning_samples = 0;
+                }
+                samples = get_test_vector(itu_test_sets[test].pcm_file, xlaw + conditioning_samples, MAX_TEST_VECTOR_LEN);
+                memcpy(itudata, xlaw, samples + conditioning_samples);
+                printf("Test %d: Compressing %d samples at %dbps\n", test, samples, itu_test_sets[test].rate);
+                len2 = g726_encode(&enc_state, adpcmdata, itudata, conditioning_samples + samples);
+            }
+            /* Test the decode side */
+            g726_init(&dec_state, itu_test_sets[test].rate, itu_test_sets[test].decompression_law, G726_PACKING_NONE);
+            if (itu_test_sets[test].conditioning_adpcm_file[0])
+            {
+                conditioning_adpcm = get_test_vector(itu_test_sets[test].conditioning_adpcm_file, unpacked, MAX_TEST_VECTOR_LEN);
+                printf("Test %d: Homing %d octets at %dbps\n", test, conditioning_adpcm, itu_test_sets[test].rate);
+            }
+            else
+            {
+                conditioning_adpcm = 0;
+            }
+            adpcm = get_test_vector(itu_test_sets[test].adpcm_file, unpacked + conditioning_adpcm, MAX_TEST_VECTOR_LEN);
+            if (itu_test_sets[test].compression_law != G726_ENCODING_NONE)
+            {
+                /* Test our compressed version against the reference compressed version */
+                printf("Test %d: Compressed data check - %d/%d octets\n", test, conditioning_adpcm + adpcm, len2);
+                if (conditioning_adpcm + adpcm == len2)
+                {
+                    for (bad_samples = 0, i = conditioning_samples;  i < len2;  i++)
+                    {
+                        if (adpcmdata[i] != unpacked[i])
+                        {
+                            bad_samples++;
+                            printf("Test %d: Compressed mismatch %d %x %x\n", test, i, adpcmdata[i], unpacked[i]);
+                        }
+                    }
+                    if (bad_samples > 0)
+                    {
+                        printf("Test failed\n");
+                        exit(2);
+                    }
+                    printf("Test passed\n");
+                }
+                else
+                {
+                    printf("Test %d: Length mismatch - ref = %d, processed = %d\n", test, conditioning_adpcm + adpcm, len2);
+                    exit(2);
+                }
+            }
+            
+            len3 = g726_decode(&dec_state, outdata, unpacked, conditioning_adpcm + adpcm);
+
+            /* Get the output reference data */
+            samples = get_test_vector(itu_test_sets[test].output_file, xlaw, MAX_TEST_VECTOR_LEN);
+            memcpy(itu_ref, xlaw, samples);
+            /* Test our decompressed version against the reference decompressed version */
+            printf("Test %d: Decompressed data check - %d/%d samples\n", test, samples, len3 - conditioning_adpcm);
+            if (samples == len3 - conditioning_adpcm)
+            {
+                for (bad_samples = 0, i = 0;  i < len3;  i++)
+                {
+                    if (itu_ref[i] != ((uint8_t *) outdata)[i + conditioning_adpcm])
+                    {
+                        bad_samples++;
+                        printf("Test %d: Decompressed mismatch %d %x %x\n", test, i, itu_ref[i], ((uint8_t *) outdata)[i + conditioning_adpcm]);
+                    }
+                }
+                if (bad_samples > 0)
+                {
+                    printf("Test failed\n");
+                    exit(2);
+                }
+                printf("Test passed\n");
+            }
+            else
+            {
+                printf("Test %d: Length mismatch - ref = %d, processed = %d\n", test, samples, len3 - conditioning_adpcm);
+                exit(2);
+            }
+        }
+
+        printf("Tests passed.\n");
+    }
+    else
+    {
+        if ((inhandle = afOpenFile_telephony_read(IN_FILE_NAME, 1)) == AF_NULL_FILEHANDLE)
+        {
+            fprintf(stderr, "    Cannot open wave file '%s'\n", IN_FILE_NAME);
+            exit(2);
+        }
+        if ((outhandle = afOpenFile_telephony_write(OUT_FILE_NAME, 1)) == AF_NULL_FILEHANDLE)
+        {
+            fprintf(stderr, "    Cannot create wave file '%s'\n", OUT_FILE_NAME);
+            exit(2);
+        }
+
+        printf("ADPCM packing is %d\n", packing);
+        g726_init(&enc_state, bit_rate, G726_ENCODING_LINEAR, packing);
+        g726_init(&dec_state, bit_rate, G726_ENCODING_LINEAR, packing);
+            
+        while ((frames = afReadFrames(inhandle, AF_DEFAULT_TRACK, amp, 159)))
+        {
+            adpcm = g726_encode(&enc_state, adpcmdata, amp, frames);
+            frames = g726_decode(&dec_state, amp, adpcmdata, adpcm);
+            outframes = afWriteFrames(outhandle, AF_DEFAULT_TRACK, amp, frames);
+        }
+        if (afCloseFile(inhandle) != 0)
+        {
+            printf("    Cannot close wave file '%s'\n", IN_FILE_NAME);
+            exit(2);
+        }
+        if (afCloseFile(outhandle) != 0)
+        {
+            printf("    Cannot close wave file '%s'\n", OUT_FILE_NAME);
+            exit(2);
+        }
+        printf("'%s' transcoded to '%s' at %dbps.\n", IN_FILE_NAME, OUT_FILE_NAME, bit_rate);
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/tests/gsm0610_tests.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/tests/gsm0610_tests.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,621 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * gsm0610_tests.c - Test the GSM 06.10 FR codec.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2006 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: gsm0610_tests.c,v 1.19 2008/08/29 09:28:13 steveu Exp $
+ */
+
+/*! \file */
+
+/*! \page gsm0610_tests_page GSM 06.10 full rate codec tests
+\section gsm0610_tests_page_sec_1 What does it do?
+Two sets of tests are performed:
+    - The tests defined in the GSM 06.10 specification, using the test data files supplied with
+      the specification.
+    - A generally audio quality test, consisting of compressing and decompressing a speeech
+      file for audible comparison.
+
+The speech file should be recorded at 16 bits/sample, 8000 samples/second, and named
+"pre_gsm0610.wav".
+
+\section gsm0610_tests_page_sec_2 How is it used?
+To perform the tests in the GSM 06.10 specification you need to obtain the test data files from the
+specification. These are copyright material, and so cannot be distributed with this test software.
+They can, however, be freely downloaded from the ETSI web site.
+
+The files, containing test vectors, which are supplied with the GSM 06.10 specification, should be
+copied to etsitests/gsm0610/unpacked so the files are arranged in the following directories.
+
+./fr_A:
+    Seq01-A.cod Seq01-A.inp Seq01-A.out
+    Seq02-A.cod Seq02-A.inp Seq02-A.out
+    Seq03-A.cod Seq03-A.inp Seq03-A.out
+    Seq04-A.cod Seq04-A.inp Seq04-A.out
+    Seq05-A.out
+
+./fr_L:
+    Seq01.cod   Seq01.inp   Seq01.out
+    Seq02.cod   Seq02.inp   Seq02.out
+    Seq03.cod   Seq03.inp   Seq03.out
+    Seq04.cod   Seq04.inp   Seq04.out
+    Seq05.cod   Seq05.out
+
+./fr_U:
+    Seq01-U.cod Seq01-U.inp Seq01-U.out
+    Seq02-U.cod Seq02-U.inp Seq02-U.out
+    Seq03-U.cod Seq03-U.inp Seq03-U.out
+    Seq04-U.cod Seq04-U.inp Seq04-U.out
+    Seq05-U.out
+
+./fr_homing_A:
+    Homing01_A.out
+    Seq01H_A.cod    Seq01H_A.inp    Seq01H_A.out
+    Seq02H_A.cod    Seq02H_A.inp    Seq02H_A.out
+    Seq03H_A.cod    Seq03H_A.inp    Seq03H_A.out
+    Seq04H_A.cod    Seq04H_A.inp    Seq04H_A.out
+    Seq05H_A.out
+    Seq06H_A.cod    Seq06H_A.inp
+
+./fr_homing_L:
+    Homing01.cod    Homing01.out
+    Seq01h.cod      Seq01h.inp      Seq01h.out
+    Seq02h.cod      Seq02h.inp      Seq02h.out
+    Seq03h.cod      Seq03h.inp      Seq03h.out
+    Seq04h.cod      Seq04h.inp      Seq04h.out
+    Seq05h.cod      Seq05h.out
+    Seq06h.cod      Seq06h.inp
+
+./fr_homing_U:
+    Homing01_U.out
+    Seq01H_U.cod    Seq01H_U.inp    Seq01H_U.out
+    Seq02H_U.cod    Seq02H_U.inp    Seq02H_U.out
+    Seq03H_U.cod    Seq03H_U.inp    Seq03H_U.out
+    Seq04H_U.cod    Seq04H_U.inp    Seq04H_U.out
+    Seq05H_U.out
+    Seq06H_U.cod    Seq06H_U.inp
+
+./fr_sync_A:
+    Seqsync_A.inp
+    Sync000_A.cod   --to--          Sync159_A.cod
+    
+./fr_sync_L:
+    Bitsync.inp
+    Seqsync.inp
+    Sync000.cod     --to--          Sync159.cod
+
+./fr_sync_U:
+    Seqsync_U.inp
+    Sync000_U.cod   --to--          Sync159_U.cod
+
+This is different from the directory structure in which they are supplied. Also, the files names are a little
+different. The supplied names are messy, and inconsistent across the sets. The names required by these tests
+just clean up these inconsistencies. Note that you will need a Windows machine to unpack some of the supplied
+files.
+
+To perform a general audio quality test, gsm0610_tests should be run. The file ../test-data/local/short_nb_voice.wav
+will be compressed to GSM 06.10 data, decompressed, and the resulting audio stored in post_gsm0610.wav.
+*/
+
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <string.h>
+#include <ctype.h>
+#include <audiofile.h>
+
+#include "spandsp.h"
+#include "spandsp-sim.h"
+
+#define BLOCK_LEN       160
+
+#define TESTDATA_DIR "../test-data/etsi/gsm0610/unpacked/fr_"
+
+#define IN_FILE_NAME    "../test-data/local/short_nb_voice.wav"
+#define OUT_FILE_NAME   "post_gsm0610.wav"
+
+#define HIST_LEN        1000
+
+uint8_t law_in_vector[1000000];
+int16_t in_vector[1000000];
+uint16_t code_vector_buf[1000000];
+uint8_t code_vector[1000000];
+uint8_t ref_code_vector[1000000];
+uint8_t decoder_code_vector[1000000];
+uint8_t law_out_vector[1000000];
+int16_t out_vector[1000000];
+int16_t ref_out_vector[1000000];
+uint8_t ref_law_out_vector[1000000];
+int vector_len;
+
+static int get_test_vector(int full, int disk, const char *name)
+{
+    char buf[500];
+    int in;
+    int len;
+    int i;
+    
+    if (full)
+    {
+        sprintf(buf, "%s%c/%s.inp", TESTDATA_DIR, 'L', name);
+        if ((in = open(buf, O_RDONLY)) < 0)
+        {
+            fprintf(stderr, "Cannot open %s\n", buf);
+            exit(2);
+        }
+        len = read(in, in_vector, 1000000);
+        close(in);
+        len /= sizeof(int16_t);
+        vector_len = len;
+    }
+
+    sprintf(buf, "%s%c/%s.out", TESTDATA_DIR, 'L', name);
+    if ((in = open(buf, O_RDONLY)) < 0)
+    {
+        fprintf(stderr, "Cannot open %s\n", buf);
+        exit(2);
+    }
+    len = read(in, ref_out_vector, 1000000);
+    close(in);
+    len /= sizeof(int16_t);
+    if (full)
+    {
+        if (len != vector_len)
+        {
+            fprintf(stderr, "Input and reference vector lengths do not match - %d %d\n", vector_len, len);
+            exit(2);
+        }
+    }
+    else
+    {
+        vector_len = len;
+    }
+    
+    sprintf(buf, "%s%c/%s.cod", TESTDATA_DIR, 'L', name);
+    if ((in = open(buf, O_RDONLY)) < 0)
+    {
+        fprintf(stderr, "Cannot open %s\n", buf);
+        exit(2);
+    }
+    len = read(in, code_vector_buf, 1000000);
+    close(in);
+    len /= sizeof(int16_t);
+    for (i = 0;  i < len;  i++)
+    {
+        ref_code_vector[i] = code_vector_buf[i];
+        decoder_code_vector[i] = code_vector_buf[i];
+    }
+    if (len*BLOCK_LEN != vector_len*76)
+    {
+        fprintf(stderr, "Input and code vector lengths do not match - %d %d\n", vector_len, len);
+        exit(2);
+    }
+
+    return len;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int get_law_test_vector(int full, int law, const char *name)
+{
+    char buf[500];
+    int in;
+    int len;
+    int i;
+    int law_uc;
+    
+    law_uc = toupper(law);
+    
+    if (full)
+    {
+        sprintf(buf, "%s%c/%s-%c.inp", TESTDATA_DIR, law_uc, name, law_uc);
+        if ((in = open(buf, O_RDONLY)) < 0)
+        {
+            fprintf(stderr, "Cannot open %s\n", buf);
+            exit(2);
+        }
+        len = read(in, law_in_vector, 1000000);
+        close(in);
+        vector_len = len;
+
+        sprintf(buf, "%s%c/%s-%c.cod", TESTDATA_DIR, law_uc, name, law_uc);
+        if ((in = open(buf, O_RDONLY)) < 0)
+        {
+            fprintf(stderr, "Cannot open %s\n", buf);
+            exit(2);
+        }
+        len = read(in, code_vector_buf, 1000000);
+        close(in);
+        len /= sizeof(int16_t);
+        for (i = 0;  i < len;  i++)
+            ref_code_vector[i] = code_vector_buf[i];
+        if (len*BLOCK_LEN != vector_len*76)
+        {
+            fprintf(stderr, "Input and code vector lengths do not match - %d %d\n", vector_len, len);
+            exit(2);
+        }
+    }
+
+    sprintf(buf, "%s%c/%s-%c.out", TESTDATA_DIR, law_uc, name, law_uc);
+    if ((in = open(buf, O_RDONLY)) < 0)
+    {
+        fprintf(stderr, "Cannot open %s\n", buf);
+        exit(2);
+    }
+    len = read(in, ref_law_out_vector, 1000000);
+    close(in);
+    if (full)
+    {
+        if (len != vector_len)
+        {
+            fprintf(stderr, "Input and reference vector lengths do not match - %d %d\n", vector_len, len);
+            exit(2);
+        }
+    }
+    else
+    {
+        vector_len = len;
+    }
+
+    sprintf(buf, "%s%c/%s.cod", TESTDATA_DIR, 'L', name);
+    if ((in = open(buf, O_RDONLY)) < 0)
+    {
+        fprintf(stderr, "Cannot open %s\n", buf);
+        exit(2);
+    }
+    len = read(in, code_vector_buf, 1000000);
+    close(in);
+    len /= sizeof(int16_t);
+    for (i = 0;  i < len;  i++)
+        decoder_code_vector[i] = code_vector_buf[i];
+    
+    return len;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int perform_linear_test(int full, int disk, const char *name)
+{
+    gsm0610_state_t *gsm0610_enc_state;
+    gsm0610_state_t *gsm0610_dec_state;
+    int i;
+    int xxx;
+    int mismatches;
+
+    printf("Performing linear test '%s' from disk %d\n", name, disk);
+
+    get_test_vector(full, disk, name);
+
+    if (full)
+    {
+        if ((gsm0610_enc_state = gsm0610_init(NULL, GSM0610_PACKING_NONE)) == NULL)
+        {
+            fprintf(stderr, "    Cannot create encoder\n");
+            exit(2);
+        }
+        xxx = gsm0610_encode(gsm0610_enc_state, code_vector, in_vector, vector_len);
+
+        printf("Check code vector of length %d\n", xxx);
+        for (i = 0, mismatches = 0;  i < xxx;  i++)
+        {
+            if (code_vector[i] != ref_code_vector[i])
+            {
+                printf("%8d/%3d: %6d %6d\n", i/76, i%76, code_vector[i], ref_code_vector[i]);
+                mismatches++;
+            }
+        }
+        gsm0610_release(gsm0610_enc_state);
+        if (mismatches)
+        {
+            printf("Test failed: %d of %d samples mismatch\n", mismatches, xxx);
+            exit(2);
+        }
+        printf("Test passed\n");
+    }
+
+    if ((gsm0610_dec_state = gsm0610_init(NULL, GSM0610_PACKING_NONE)) == NULL)
+    {
+        fprintf(stderr, "    Cannot create decoder\n");
+        exit(2);
+    }
+    xxx = gsm0610_decode(gsm0610_dec_state, out_vector, decoder_code_vector, vector_len);
+    printf("Check output vector of length %d\n", vector_len);
+    for (i = 0, mismatches = 0;  i < vector_len;  i++)
+    {
+        if (out_vector[i] != ref_out_vector[i])
+        {
+            printf("%8d: %6d %6d\n", i, out_vector[i], ref_out_vector[i]);
+            mismatches++;
+        }
+    }
+    if (mismatches)
+    {
+        printf("Test failed: %d of %d samples mismatch\n", mismatches, vector_len);
+        exit(2);
+    }
+    gsm0610_release(gsm0610_dec_state);
+    printf("Test passed\n");
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int perform_law_test(int full, int law, const char *name)
+{
+    gsm0610_state_t *gsm0610_enc_state;
+    gsm0610_state_t *gsm0610_dec_state;
+    int i;
+    int xxx;
+    int mismatches;
+
+    if (law == 'a')
+        printf("Performing A-law test '%s'\n", name);
+    else
+        printf("Performing u-law test '%s'\n", name);
+        
+    get_law_test_vector(full, law, name);
+
+    if (full)
+    {
+        if ((gsm0610_enc_state = gsm0610_init(NULL, GSM0610_PACKING_NONE)) == NULL)
+        {
+            fprintf(stderr, "    Cannot create encoder\n");
+            exit(2);
+        }
+        if (law == 'a')
+        {
+            for (i = 0;  i < vector_len;  i++)
+                in_vector[i] = alaw_to_linear(law_in_vector[i]);
+        }
+        else
+        {
+            for (i = 0;  i < vector_len;  i++)
+                in_vector[i] = ulaw_to_linear(law_in_vector[i]);
+        }
+        xxx = gsm0610_encode(gsm0610_enc_state, code_vector, in_vector, vector_len);
+
+        printf("Check code vector of length %d\n", xxx);
+        for (i = 0, mismatches = 0;  i < xxx;  i++)
+        {
+            if (code_vector[i] != ref_code_vector[i])
+            {
+                printf("%8d/%3d: %6d %6d %6d\n", i/76, i%76, code_vector[i], ref_code_vector[i], decoder_code_vector[i]);
+                mismatches++;
+            }
+        }
+        if (mismatches)
+        {
+            printf("Test failed: %d of %d samples mismatch\n", mismatches, xxx);
+            exit(2);
+        }
+        printf("Test passed\n");
+        gsm0610_release(gsm0610_enc_state);
+    }
+
+    if ((gsm0610_dec_state = gsm0610_init(NULL, GSM0610_PACKING_NONE)) == NULL)
+    {
+        fprintf(stderr, "    Cannot create decoder\n");
+        exit(2);
+    }
+    xxx = gsm0610_decode(gsm0610_dec_state, out_vector, decoder_code_vector, vector_len);
+    if (law == 'a')
+    {
+        for (i = 0;  i < vector_len;  i++)
+            law_out_vector[i] = linear_to_alaw(out_vector[i]);
+    }
+    else
+    {
+        for (i = 0;  i < vector_len;  i++)
+            law_out_vector[i] = linear_to_ulaw(out_vector[i]);
+    }
+    printf("Check output vector of length %d\n", vector_len);
+    for (i = 0, mismatches = 0;  i < vector_len;  i++)
+    {
+        if (law_out_vector[i] != ref_law_out_vector[i])
+        {
+            printf("%8d: %6d %6d\n", i, law_out_vector[i], ref_law_out_vector[i]);
+            mismatches++;
+        }
+    }
+    if (mismatches)
+    {
+        printf("Test failed: %d of %d samples mismatch\n", mismatches, vector_len);
+        exit(2);
+    }
+    gsm0610_release(gsm0610_dec_state);
+    printf("Test passed\n");
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int repack_gsm0610_voip_to_wav49(uint8_t c[], const uint8_t d[])
+{
+    gsm0610_frame_t frame[2];
+    int n;
+ 
+	n = gsm0610_unpack_voip(&frame[0], d);
+	gsm0610_unpack_voip(&frame[1], d + n);
+    n = gsm0610_pack_wav49(c, frame);
+    return n;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int repack_gsm0610_wav49_to_voip(uint8_t d[], const uint8_t c[])
+{
+    gsm0610_frame_t frame[2];
+    int n[2];
+
+    gsm0610_unpack_wav49(frame, c);
+    n[0] = gsm0610_pack_voip(d, &frame[0]);
+    n[1] = gsm0610_pack_voip(d + n[0], &frame[1]);
+    return n[0] + n[1];
+}
+/*- End of function --------------------------------------------------------*/
+
+static int perform_pack_unpack_test(void)
+{
+    uint8_t a[66];
+    uint8_t b[66];
+    uint8_t c[66];
+    int i;
+    int j;
+
+    printf("Performing packing/unpacking tests (not part of the ETSI conformance tests).\n");
+    /* Try trans-packing a lot of random data looking for before/after mismatch. */
+    for (j = 0;  j < 1000;  j++)
+    {        
+        for (i = 0;  i < 65;  i++)
+            a[i] = rand();
+        repack_gsm0610_wav49_to_voip(b, a);
+        repack_gsm0610_voip_to_wav49(c, b);
+        if (memcmp(a, c, 65))
+        {
+            printf("Test failed: data mismatch\n");
+            exit(2);
+        }
+
+        for (i = 0;  i < 66;  i++)
+            a[i] = rand();
+        /* Insert the magic code */
+        a[0] = (a[0] & 0xF) | 0xD0;
+        a[33] = (a[33] & 0xF) | 0xD0;
+        repack_gsm0610_voip_to_wav49(b, a);
+        repack_gsm0610_wav49_to_voip(c, b);
+        //for (i = 0;  i < 66;  i++)
+        //    printf("%2d: 0x%02X 0x%02X\n", i, a[i], c[i]);
+        if (memcmp(a, c, 66))
+        {
+            printf("Test failed: data mismatch\n");
+            exit(2);
+        }
+    }
+    printf("Test passed\n");
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int main(int argc, char *argv[])
+{
+    AFfilehandle inhandle;
+    AFfilehandle outhandle;
+    int frames;
+    int outframes;
+    int16_t pre_amp[HIST_LEN];
+    int16_t post_amp[HIST_LEN];
+    uint8_t gsm0610_data[HIST_LEN];
+    gsm0610_state_t *gsm0610_enc_state;
+    gsm0610_state_t *gsm0610_dec_state;
+    int etsitests;
+    int packing;
+    int i;
+
+    etsitests = TRUE;
+    packing = GSM0610_PACKING_NONE;
+    for (i = 1;  i < argc;  i++)
+    {
+        if (strcmp(argv[i], "-l") == 0)
+        {
+            etsitests = FALSE;
+            continue;
+        }
+        if (strcmp(argv[i], "-p") == 0)
+        {
+            packing = atoi(argv[++i]);
+            continue;
+        }
+        fprintf(stderr, "Unknown parameter %s specified.\n", argv[i]);
+        exit(2);
+    }
+
+    if (etsitests)
+    {
+        perform_linear_test(TRUE, 1, "Seq01");
+        perform_linear_test(TRUE, 1, "Seq02");
+        perform_linear_test(TRUE, 1, "Seq03");
+        perform_linear_test(TRUE, 1, "Seq04");
+        perform_linear_test(FALSE, 1, "Seq05");
+        perform_law_test(TRUE, 'a', "Seq01");
+        perform_law_test(TRUE, 'a', "Seq02");
+        perform_law_test(TRUE, 'a', "Seq03");
+        perform_law_test(TRUE, 'a', "Seq04");
+        perform_law_test(FALSE, 'a', "Seq05");
+        perform_law_test(TRUE, 'u', "Seq01");
+        perform_law_test(TRUE, 'u', "Seq02");
+        perform_law_test(TRUE, 'u', "Seq03");
+        perform_law_test(TRUE, 'u', "Seq04");
+        perform_law_test(FALSE, 'u', "Seq05");
+        /* This is not actually an ETSI test */
+        perform_pack_unpack_test();
+
+        printf("Tests passed.\n");
+    }
+    else
+    {
+        if ((inhandle = afOpenFile_telephony_read(IN_FILE_NAME, 1)) == AF_NULL_FILEHANDLE)
+        {
+            fprintf(stderr, "    Cannot open wave file '%s'\n", IN_FILE_NAME);
+            exit(2);
+        }
+        if ((outhandle = afOpenFile_telephony_write(OUT_FILE_NAME, 1)) == AF_NULL_FILEHANDLE)
+        {
+            fprintf(stderr, "    Cannot create wave file '%s'\n", OUT_FILE_NAME);
+            exit(2);
+        }
+    
+        if ((gsm0610_enc_state = gsm0610_init(NULL, packing)) == NULL)
+        {
+            fprintf(stderr, "    Cannot create encoder\n");
+            exit(2);
+        }
+            
+        if ((gsm0610_dec_state = gsm0610_init(NULL, packing)) == NULL)
+        {
+            fprintf(stderr, "    Cannot create decoder\n");
+            exit(2);
+        }
+
+        while ((frames = afReadFrames(inhandle, AF_DEFAULT_TRACK, pre_amp, 2*BLOCK_LEN)))
+        {
+            gsm0610_encode(gsm0610_enc_state, gsm0610_data, pre_amp, (packing == GSM0610_PACKING_WAV49)  ?  BLOCK_LEN  :  2*BLOCK_LEN);
+            gsm0610_decode(gsm0610_dec_state, post_amp, gsm0610_data, (packing == GSM0610_PACKING_WAV49)  ?  33  :  65);
+            outframes = afWriteFrames(outhandle, AF_DEFAULT_TRACK, post_amp, frames);
+        }
+    
+        if (afCloseFile(inhandle) != 0)
+        {
+            fprintf(stderr, "    Cannot close wave file '%s'\n", IN_FILE_NAME);
+            exit(2);
+        }
+        if (afCloseFile(outhandle) != 0)
+        {
+            fprintf(stderr, "    Cannot close wave file '%s'\n", OUT_FILE_NAME);
+            exit(2);
+        }
+        gsm0610_release(gsm0610_enc_state);
+        gsm0610_release(gsm0610_dec_state);
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/tests/hdlc_tests.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/tests/hdlc_tests.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,817 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * hdlc_tests.c
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2003 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: hdlc_tests.c,v 1.43 2008/05/13 13:17:25 steveu Exp $
+ */
+
+/*! \file */
+
+/*! \page hdlc_tests_page HDLC tests
+\section hdlc_tests_page_sec_1 What does it do?
+The HDLC tests exercise the HDLC module, and verifies correct operation
+using both 16 and 32 bit CRCs.
+*/
+
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "spandsp.h"
+
+int ref_len;
+uint8_t buf[1000];
+
+int abort_reported;
+int frame_handled;
+int frame_failed;
+int frame_len_errors;
+int frame_data_errors;
+int underflow_reported;
+int framing_ok_reported;
+int framing_ok_reports;
+
+hdlc_rx_state_t rx;
+hdlc_tx_state_t tx;
+
+int frames_sent;
+int bytes_sent;
+uint64_t start;
+uint64_t end;
+
+/* Use a local random generator, so the results are consistent across platforms. We have hard coded
+   correct results for a message sequence generated by this particular PRNG. */
+static int my_rand(void)
+{
+    static int rndnum = 1234567;
+
+    return (rndnum = 1664525U*rndnum + 1013904223U) >> 8;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int cook_up_msg(uint8_t *buf)
+{
+    int i;
+    int len;
+
+    /* Use medium length messages, with some randomised length variation. */
+    /* TODO: this doesn't exercise extremely short or long messages. */
+    len = (my_rand() & 0x3F) + 100;
+    for (i = 0;  i < len;  i++)
+        buf[i] = my_rand();
+    return len;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void frame_handler(void *user_data, const uint8_t *pkt, int len, int ok)
+{
+    if (len < 0)
+    {
+        /* Special conditions */
+        switch (len)
+        {
+        case PUTBIT_TRAINING_IN_PROGRESS:
+            printf("Training in progress\n");
+            break;
+        case PUTBIT_TRAINING_FAILED:
+            printf("Training failed\n");
+            break;
+        case PUTBIT_TRAINING_SUCCEEDED:
+            printf("Training succeeded\n");
+            break;
+        case PUTBIT_CARRIER_UP:
+            printf("Carrier up\n");
+            break;
+        case PUTBIT_CARRIER_DOWN:
+            printf("Carrier down\n");
+            break;
+        case PUTBIT_FRAMING_OK:
+            framing_ok_reported = TRUE;
+            framing_ok_reports++;
+            //printf("Framing OK\n");
+            break;
+        case PUTBIT_END_OF_DATA:
+            printf("End of data\n");
+            break;
+        case PUTBIT_ABORT:
+            abort_reported = TRUE;
+            //printf("Abort\n");
+            break;
+        case PUTBIT_BREAK:
+            printf("Break\n");
+            break;
+        case PUTBIT_OCTET_REPORT:
+            printf("Octet report\n");
+            break;
+        default:
+            printf("Eh!\n");
+            break;
+        }
+        return;
+    }
+    if (ok)
+    {
+        if (len != ref_len)
+        {
+            printf("Len error - %d %d\n", len, ref_len);
+            frame_len_errors++;
+            return;
+        }
+        if (memcmp(pkt, buf, len))
+        {
+            printf("Frame data error\n");
+            frame_data_errors++;
+            return;
+        }
+        frame_handled = TRUE;
+    }
+    else
+    {
+        frame_failed = TRUE;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static void underflow_handler(void *user_data)
+{
+    //printf("Underflow reported\n");
+    underflow_reported = TRUE;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void check_result(void)
+{
+    hdlc_rx_stats_t rx_stats;
+
+    hdlc_rx_get_stats(&rx, &rx_stats);
+    printf("%lu bytes\n", rx_stats.bytes);
+    printf("%lu good frames\n", rx_stats.good_frames);
+    printf("%lu CRC errors\n", rx_stats.crc_errors);
+    printf("%lu length errors\n", rx_stats.length_errors);
+    printf("%lu aborts\n", rx_stats.aborts);
+    printf("%d frame length errors\n", frame_len_errors);
+    printf("%d frame data errors\n", frame_data_errors);
+    printf("Test duration %" PRIu64 " ticks\n", end - start);
+    if (rx_stats.bytes != bytes_sent
+        ||
+        rx_stats.good_frames != frames_sent
+        ||
+        rx_stats.crc_errors != 0
+        ||
+        rx_stats.length_errors != 0
+        ||
+        rx_stats.aborts != 0
+        ||
+        frame_len_errors != 0
+        ||
+        frame_data_errors != 0)
+    {
+        printf("Tests failed.\n");
+        exit(2);
+    }
+    printf("Test passed.\n\n");
+}
+/*- End of function --------------------------------------------------------*/
+
+static int test_hdlc_modes(void)
+{
+    int i;
+    int j;
+    int len;
+    int nextbyte;
+    int progress;
+    int progress_delay;
+    uint8_t bufx[100];
+
+    /* Try sending HDLC messages with CRC-16 */
+    printf("Testing with CRC-16 (byte by byte)\n");
+    frame_len_errors = 0;
+    frame_data_errors = 0;
+    hdlc_tx_init(&tx, FALSE, 1, FALSE, underflow_handler, NULL);
+    hdlc_rx_init(&rx, FALSE, FALSE, 5, frame_handler, NULL);
+    underflow_reported = FALSE;
+
+    start = rdtscll();
+    hdlc_tx_flags(&tx, 40);
+    /* Push an initial message so we should NOT get an underflow after the preamble. */
+    ref_len = cook_up_msg(buf);
+    hdlc_tx_frame(&tx, buf, ref_len);
+    frame_handled = FALSE;
+    frame_failed = FALSE;
+    frames_sent = 0;
+    bytes_sent = 0;
+    for (i = 0;  i < 1000000;  i++)
+    {
+        nextbyte = hdlc_tx_get_byte(&tx);
+        hdlc_rx_put_byte(&rx, nextbyte);
+        if (underflow_reported)
+        {
+            underflow_reported = FALSE;
+            nextbyte = hdlc_tx_get_byte(&tx);
+            hdlc_rx_put_byte(&rx, nextbyte);
+            frames_sent++;
+            bytes_sent += ref_len;
+            if (!frame_handled)
+            {
+                printf("Frame not received.\n");
+                return -1;
+            }
+            ref_len = cook_up_msg(buf);
+            hdlc_tx_frame(&tx, buf, ref_len);
+            frame_handled = FALSE;
+        }
+    }
+    end = rdtscll();
+    check_result();
+
+    /* Now try sending HDLC messages with CRC-16 */
+    printf("Testing with CRC-16 (chunk by chunk)\n");
+    frame_len_errors = 0;
+    frame_data_errors = 0;
+    hdlc_tx_init(&tx, FALSE, 1, FALSE, underflow_handler, NULL);
+    hdlc_rx_init(&rx, FALSE, FALSE, 5, frame_handler, NULL);
+    underflow_reported = FALSE;
+
+    start = rdtscll();
+    hdlc_tx_flags(&tx, 40);
+    /* Push an initial message so we should NOT get an underflow after the preamble. */
+    ref_len = cook_up_msg(buf);
+    hdlc_tx_frame(&tx, buf, ref_len);
+    frame_handled = FALSE;
+    frame_failed = FALSE;
+    frames_sent = 0;
+    bytes_sent = 0;
+    for (i = 0;  i < 10000;  i++)
+    {
+        len = hdlc_tx_get(&tx, bufx, 100);
+        hdlc_rx_put(&rx, bufx, len);
+        if (underflow_reported)
+        {
+            underflow_reported = FALSE;
+            len = hdlc_tx_get(&tx, bufx, 100);
+            hdlc_rx_put(&rx, bufx, len);
+            frames_sent++;
+            bytes_sent += ref_len;
+            if (!frame_handled)
+            {
+                printf("Frame not received.\n");
+                return -1;
+            }
+            ref_len = cook_up_msg(buf);
+            hdlc_tx_frame(&tx, buf, ref_len);
+            frame_handled = FALSE;
+        }
+    }
+    end = rdtscll();
+    check_result();
+
+    /* Now try sending HDLC messages with CRC-16 bit by bit */
+    printf("Testing with CRC-16 (bit by bit)\n");
+    frame_len_errors = 0;
+    frame_data_errors = 0;
+    hdlc_tx_init(&tx, FALSE, 2, FALSE, underflow_handler, NULL);
+    hdlc_rx_init(&rx, FALSE, FALSE, 5, frame_handler, NULL);
+    underflow_reported = FALSE;
+
+    start = rdtscll();
+    hdlc_tx_flags(&tx, 40);
+    /* Don't push an initial message so we should get an underflow after the preamble. */
+    /* Lie for the first message, as there isn't really one */
+    frame_handled = TRUE;
+    frame_failed = FALSE;
+    frames_sent = 0;
+    bytes_sent = 0;
+    ref_len = 0;
+    for (i = 0;  i < 8*1000000;  i++)
+    {
+        nextbyte = hdlc_tx_get_bit(&tx);
+        hdlc_rx_put_bit(&rx, nextbyte);
+        if (underflow_reported)
+        {
+            underflow_reported = FALSE;
+            for (j = 0;  j < 20;  j++)
+            {
+                nextbyte = hdlc_tx_get_bit(&tx);
+                hdlc_rx_put_bit(&rx, nextbyte);
+            }
+            if (ref_len)
+            {
+                frames_sent++;
+                bytes_sent += ref_len;
+            }
+            if (!frame_handled)
+            {
+                printf("Frame not received.\n");
+                return -1;
+            }
+            ref_len = cook_up_msg(buf);
+            hdlc_tx_frame(&tx, buf, ref_len);
+            frame_handled = FALSE;
+        }
+    }
+    end = rdtscll();
+    check_result();
+
+    /* Now try sending HDLC messages with CRC-32 */
+    printf("Testing with CRC-32 (byte by byte)\n");
+    frame_len_errors = 0;
+    frame_data_errors = 0;
+    hdlc_tx_init(&tx, TRUE, 1, FALSE, underflow_handler, NULL);
+    hdlc_rx_init(&rx, TRUE, FALSE, 1, frame_handler, NULL);
+    underflow_reported = FALSE;
+
+    start = rdtscll();
+    hdlc_tx_flags(&tx, 40);
+    /* Don't push an initial message so we should get an underflow after the preamble. */
+    /* Lie for the first message, as there isn't really one */
+    frame_handled = TRUE;
+    frame_failed = FALSE;
+    frames_sent = 0;
+    bytes_sent = 0;
+    ref_len = 0;
+    for (i = 0;  i < 1000000;  i++)
+    {
+        nextbyte = hdlc_tx_get_byte(&tx);
+        hdlc_rx_put_byte(&rx, nextbyte);
+        if (underflow_reported)
+        {
+            underflow_reported = FALSE;
+            nextbyte = hdlc_tx_get_byte(&tx);
+            hdlc_rx_put_byte(&rx, nextbyte);
+            if (ref_len)
+            {
+                frames_sent++;
+                bytes_sent += ref_len;
+            }
+            if (!frame_handled)
+            {
+                printf("Frame not received.\n");
+                return -1;
+            }
+            ref_len = cook_up_msg(buf);
+            hdlc_tx_frame(&tx, buf, ref_len);
+            frame_handled = FALSE;
+        }
+    }
+    end = rdtscll();
+    check_result();
+
+    /* Now try progressive mode with CRC-16 */
+    printf("Testing progressive mode with CRC-16 (byte by byte)\n");
+    frame_len_errors = 0;
+    frame_data_errors = 0;
+    hdlc_tx_init(&tx, TRUE, 1, TRUE, underflow_handler, NULL);
+    hdlc_rx_init(&rx, TRUE, FALSE, 1, frame_handler, NULL);
+    underflow_reported = FALSE;
+
+    start = rdtscll();
+    hdlc_tx_flags(&tx, 40);
+    /* Don't push an initial message so we should get an underflow after the preamble. */
+    /* Lie for the first message, as there isn't really one */
+    frame_handled = TRUE;
+    frame_failed = FALSE;
+    progress = 9999;
+    progress_delay = 9999;
+    frames_sent = 0;
+    bytes_sent = 0;
+    ref_len = 0;
+    for (i = 0;  i < 1000000;  i++)
+    {
+        nextbyte = hdlc_tx_get_byte(&tx);
+        hdlc_rx_put_byte(&rx, nextbyte);
+        if (underflow_reported)
+        {
+            underflow_reported = FALSE;
+            nextbyte = hdlc_tx_get_byte(&tx);
+            hdlc_rx_put_byte(&rx, nextbyte);
+            if (ref_len)
+            {
+                frames_sent++;
+                bytes_sent += ref_len;
+            }
+            if (!frame_handled)
+            {
+                printf("Frame not received.\n");
+                return -1;
+            }
+            ref_len = cook_up_msg(buf);
+            hdlc_tx_frame(&tx, buf, 10);
+            progress = 10;
+            progress_delay = 8;
+            frame_handled = FALSE;
+        }
+        if (progress < ref_len  &&  progress_delay-- <= 0)
+        {
+            if (hdlc_tx_frame(&tx, buf + progress, (progress + 10 <= ref_len)  ?  10  :  ref_len - progress) < 0)
+            {
+                printf("Failed to add progressively\n");
+                return -1;
+            }
+            progress += 10;
+            progress_delay = 8;
+        }
+    }
+    end = rdtscll();
+    check_result();
+
+    printf("Tests passed.\n");
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int test_hdlc_frame_length_error_handling(void)
+{
+    int i;
+    int j;
+    int nextbyte;
+
+    printf("Testing frame length error handling using CRC-16 (bit by bit)\n");
+    frame_len_errors = 0;
+    frame_data_errors = 0;
+    hdlc_tx_init(&tx, FALSE, 2, FALSE, underflow_handler, NULL);
+    hdlc_rx_init(&rx, FALSE, TRUE, 5, frame_handler, NULL);
+    hdlc_rx_set_max_frame_len(&rx, 100);
+    underflow_reported = FALSE;
+    framing_ok_reported = FALSE;
+    framing_ok_reports = 0;
+
+    hdlc_tx_flags(&tx, 10);
+    /* Don't push an initial message so we should get an underflow after the preamble. */
+    /* Lie for the first message, as there isn't really one */
+    frame_handled = TRUE;
+    frame_failed = FALSE;
+    frames_sent = 0;
+    bytes_sent = 0;
+    ref_len = 0;
+    for (i = 0;  i < 8*1000000;  i++)
+    {
+        nextbyte = hdlc_tx_get_bit(&tx);
+        hdlc_rx_put_bit(&rx, nextbyte);
+        if (framing_ok_reported)
+        {
+            printf("Framing OK reported at bit %d (%d)\n", i, framing_ok_reports);
+            framing_ok_reported = FALSE;
+        }
+        if (underflow_reported)
+        {
+            underflow_reported = FALSE;
+            for (j = 0;  j < 20;  j++)
+            {
+                nextbyte = hdlc_tx_get_bit(&tx);
+                hdlc_rx_put_bit(&rx, nextbyte);
+                if (framing_ok_reported)
+                {
+                    printf("Framing OK reported at bit %d (%d) - %d\n", i, framing_ok_reports, frame_handled);
+                    framing_ok_reported = FALSE;
+                }
+            }
+            if (ref_len)
+            {
+                frames_sent++;
+                bytes_sent += ref_len;
+            }
+            if (!frame_handled)
+            {
+                /* We should get a failure when the length reaches 101 */
+                if (ref_len == 101)
+                {
+                    printf("Tests passed.\n");
+                    return 0;
+                }
+                if (frame_failed)
+                    printf("Frame failed.\n");
+                printf("Frame not received at length %d.\n", ref_len);
+                return -1;
+            }
+            else
+            {
+                /* We should get a failure when the length reaches 101 */
+                if (ref_len > 100)
+                {
+                    printf("Tests failed.\n");
+                    return -1;
+                }
+            }
+            ref_len++;
+            hdlc_tx_frame(&tx, buf, ref_len);
+            frame_handled = FALSE;
+        }
+    }
+    /* We shouldn't reach here */
+    printf("Tests failed.\n");
+    return -1;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int test_hdlc_crc_error_handling(void)
+{
+    int i;
+    int j;
+    int nextbyte;
+    int corrupt;
+
+    printf("Testing CRC error handling using CRC-16 (bit by bit)\n");
+    frame_len_errors = 0;
+    frame_data_errors = 0;
+    hdlc_tx_init(&tx, FALSE, 2, FALSE, underflow_handler, NULL);
+    hdlc_rx_init(&rx, FALSE, TRUE, 5, frame_handler, NULL);
+    underflow_reported = FALSE;
+    framing_ok_reported = FALSE;
+    framing_ok_reports = 0;
+
+    hdlc_tx_flags(&tx, 10);
+    /* Don't push an initial message so we should get an underflow after the preamble. */
+    /* Lie for the first message, as there isn't really one */
+    frame_handled = TRUE;
+    frame_failed = FALSE;
+    frames_sent = 0;
+    bytes_sent = 0;
+    ref_len = 100;
+    corrupt = FALSE;
+    for (i = 0;  i < 8*1000000;  i++)
+    {
+        nextbyte = hdlc_tx_get_bit(&tx);
+        hdlc_rx_put_bit(&rx, nextbyte);
+        if (framing_ok_reported)
+        {
+            printf("Framing OK reported at bit %d (%d)\n", i, framing_ok_reports);
+            framing_ok_reported = FALSE;
+        }
+        if (underflow_reported)
+        {
+            underflow_reported = FALSE;
+            for (j = 0;  j < 20;  j++)
+            {
+                nextbyte = hdlc_tx_get_bit(&tx);
+                hdlc_rx_put_bit(&rx, nextbyte);
+                if (framing_ok_reported)
+                {
+                    printf("Framing OK reported at bit %d (%d) - %d\n", i, framing_ok_reports, frame_handled);
+                    framing_ok_reported = FALSE;
+                }
+            }
+            if (ref_len)
+            {
+                frames_sent++;
+                bytes_sent += ref_len;
+            }
+            if (!frame_handled)
+            {
+                if (!corrupt)
+                {
+                    printf("Frame not received when it should be correct.\n");
+                    return -1;
+                }
+            }
+            else
+            {
+                if (corrupt)
+                {
+                    printf("Frame received when it should be corrupt.\n");
+                    return -1;
+                }
+            }
+            ref_len = cook_up_msg(buf);
+            hdlc_tx_frame(&tx, buf, ref_len);
+            if ((corrupt = rand() & 1))
+                hdlc_tx_corrupt_frame(&tx);
+            frame_handled = FALSE;
+        }
+    }
+
+    printf("Tests passed.\n");
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int test_hdlc_abort_handling(void)
+{
+    int i;
+    int j;
+    int nextbyte;
+    int abort;
+
+    printf("Testing abort handling using CRC-16 (bit by bit)\n");
+    frame_len_errors = 0;
+    frame_data_errors = 0;
+    hdlc_tx_init(&tx, FALSE, 2, FALSE, underflow_handler, NULL);
+    hdlc_rx_init(&rx, FALSE, TRUE, 0, frame_handler, NULL);
+    underflow_reported = FALSE;
+    framing_ok_reported = FALSE;
+    framing_ok_reports = 0;
+
+    hdlc_tx_flags(&tx, 10);
+    /* Don't push an initial message so we should get an underflow after the preamble. */
+    /* Lie for the first message, as there isn't really one */
+    frame_handled = TRUE;
+    frame_failed = FALSE;
+    frames_sent = 0;
+    bytes_sent = 0;
+    ref_len = 0;
+    abort = FALSE;
+    abort_reported = FALSE;
+    for (i = 0;  i < 8*1000000;  i++)
+    {
+        nextbyte = hdlc_tx_get_bit(&tx);
+        hdlc_rx_put_bit(&rx, nextbyte);
+        if (framing_ok_reported)
+        {
+            printf("Framing OK reported at bit %d (%d)\n", i, framing_ok_reports);
+            framing_ok_reported = FALSE;
+        }
+        if (underflow_reported)
+        {
+            underflow_reported = FALSE;
+            for (j = 0;  j < 20;  j++)
+            {
+                nextbyte = hdlc_tx_get_bit(&tx);
+                hdlc_rx_put_bit(&rx, nextbyte);
+                if (framing_ok_reported)
+                {
+                    printf("Framing OK reported at bit %d (%d) - %d\n", i, framing_ok_reports, frame_handled);
+                    framing_ok_reported = FALSE;
+                }
+            }
+            if (ref_len)
+            {
+                frames_sent++;
+                bytes_sent += ref_len;
+            }
+            if (abort)
+            {
+                if (abort  &&  !abort_reported)
+                {
+                    printf("Abort not received when sent\n");
+                    return -1;
+                }
+                if (frame_handled)
+                {
+                    if (frame_failed)
+                        printf("Frame failed.\n");
+                    printf("Frame received when abort sent.\n");
+                    return -1;
+                }
+            }
+            else
+            {
+                if (abort_reported)
+                {
+                    printf("Abort received when not sent\n");
+                    return -1;
+                }
+                if (!frame_handled)
+                {
+                    if (frame_failed)
+                        printf("Frame failed.\n");
+                    printf("Frame not received.\n");
+                    return -1;
+                }
+            }
+            ref_len = cook_up_msg(buf);
+            hdlc_tx_frame(&tx, buf, ref_len);
+            if ((abort = rand() & 1))
+                hdlc_tx_abort(&tx);
+            frame_handled = FALSE;
+            abort_reported = FALSE;
+        }
+    }
+
+    printf("Tests passed.\n");
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+#if 0
+static int test_hdlc_octet_count_handling(void)
+{
+    int i;
+    int j;
+    int nextbyte;
+
+    printf("Testing the octet_counting handling using CRC-16 (bit by bit)\n");
+    frame_len_errors = 0;
+    frame_data_errors = 0;
+    hdlc_tx_init(&tx, FALSE, 2, FALSE, underflow_handler, NULL);
+    hdlc_rx_init(&rx, FALSE, TRUE, 0, frame_handler, NULL);
+    //hdlc_rx_set_max_frame_len(&rx, 50);
+    hdlc_rx_set_octet_counting_report_interval(&rx, 16);
+    underflow_reported = FALSE;
+    framing_ok_reported = FALSE;
+    framing_ok_reports = 0;
+
+    hdlc_tx_flags(&tx, 10);
+    /* Don't push an initial message so we should get an underflow after the preamble. */
+    /* Lie for the first message, as there isn't really one */
+    frame_handled = TRUE;
+    frame_failed = FALSE;
+    frames_sent = 0;
+    bytes_sent = 0;
+    ref_len = 0;
+    for (i = 0;  i < 8*1000000;  i++)
+    {
+        nextbyte = hdlc_tx_get_bit(&tx);
+        hdlc_rx_put_bit(&rx, nextbyte);
+        if (framing_ok_reported)
+        {
+            printf("Framing OK reported at bit %d (%d)\n", i, framing_ok_reports);
+            framing_ok_reported = FALSE;
+        }
+        if (underflow_reported)
+        {
+            underflow_reported = FALSE;
+            for (j = 0;  j < 20;  j++)
+            {
+                nextbyte = hdlc_tx_get_bit(&tx);
+                hdlc_rx_put_bit(&rx, nextbyte);
+                if (framing_ok_reported)
+                {
+                    printf("Framing OK reported at bit %d (%d) - %d\n", i, framing_ok_reports, frame_handled);
+                    framing_ok_reported = FALSE;
+                }
+            }
+            if (ref_len)
+            {
+                frames_sent++;
+                bytes_sent += ref_len;
+            }
+            if (!frame_handled)
+            {
+                if (frame_failed)
+                    printf("Frame failed.\n");
+                printf("Frame not received.\n");
+                return -1;
+            }
+            ref_len = cook_up_msg(buf);
+            hdlc_tx_frame(&tx, buf, ref_len);
+            hdlc_tx_abort(&tx);
+            //hdlc_tx_corrupt_frame(&tx);
+            frame_handled = FALSE;
+        }
+    }
+
+    printf("Tests passed.\n");
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+#endif
+
+int main(int argc, char *argv[])
+{
+    printf("HDLC module tests\n");
+
+    if (test_hdlc_modes())
+    {
+        printf("Tests failed\n");
+        exit(2);
+    }
+    if (test_hdlc_frame_length_error_handling())
+    {
+        printf("Tests failed\n");
+        exit(2);
+    }
+    if (test_hdlc_crc_error_handling())
+    {
+        printf("Tests failed\n");
+        exit(2);
+    }
+    if (test_hdlc_abort_handling())
+    {
+        printf("Tests failed\n");
+        exit(2);
+    }
+#if 0
+    if (test_hdlc_octet_count_handling())
+    {
+        printf("Tests failed\n");
+        exit(2);
+    }
+#endif
+    printf("Tests passed.\n");
+    return  0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/tests/ima_adpcm_tests.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/tests/ima_adpcm_tests.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,214 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * ima_adpcm_tests.c - Test the IMA/DVI/Intel ADPCM encode and decode
+ *                     software.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2004 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: ima_adpcm_tests.c,v 1.34 2008/08/29 09:28:13 steveu Exp $
+ */
+
+/*! \file */
+
+/*! \page ima_adpcm_tests_page IMA ADPCM tests
+\section ima_adpcm_tests_page_sec_1 What does it do?
+To perform a general audio quality test, ima_adpcm_tests should be run. The test file
+../test-data/local/short_nb_voice.wav will be compressed to the specified bit rate,
+decompressed, and the resulting audio stored in post_ima_adpcm.wav. A simple SNR test
+is automatically performed. Listening tests may be used for a more detailed evaluation
+of the degradation in quality caused by the compression.
+
+\section ima_adpcm_tests_page_sec_2 How is it used?
+*/
+
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <string.h>
+#include <time.h>
+#include <audiofile.h>
+
+#include "spandsp.h"
+#include "spandsp-sim.h"
+
+#define IN_FILE_NAME    "../test-data/local/short_nb_voice.wav"
+#define OUT_FILE_NAME   "post_ima_adpcm.wav"
+
+#define HIST_LEN        2000
+
+int main(int argc, char *argv[])
+{
+    int i;
+    AFfilehandle inhandle;
+    AFfilehandle outhandle;
+    int frames;
+    int dec_frames;
+    int outframes;
+    int ima_bytes;
+    double pre_energy;
+    double post_energy;
+    double diff_energy;
+    int16_t pre_amp[HIST_LEN];
+    int16_t post_amp[HIST_LEN];
+    uint8_t ima_data[HIST_LEN];
+    int16_t history[HIST_LEN];
+    int hist_in;
+    int hist_out;
+    ima_adpcm_state_t *ima_enc_state;
+    ima_adpcm_state_t *ima_dec_state;
+    int xx;
+    int total_pre_samples;
+    int total_compressed_bytes;
+    int total_post_samples;
+    const char *in_file_name;
+    int variant;
+    int chunk_size;
+    int enc_chunk_size;
+    int log_encoded_data;
+    int opt;
+
+    variant = IMA_ADPCM_DVI4;
+    in_file_name = IN_FILE_NAME;
+    chunk_size = 160;
+    enc_chunk_size = 0;
+    log_encoded_data = FALSE;
+    while ((opt = getopt(argc, argv, "ac:i:lv")) != -1)
+    {
+        switch (opt)
+        {
+        case 'a':
+            variant = IMA_ADPCM_IMA4;
+            chunk_size = 505;
+            break;
+        case 'c':
+            enc_chunk_size = atoi(optarg);
+            break;
+        case 'i':
+            in_file_name = optarg;
+            break;
+        case 'l':
+            log_encoded_data = TRUE;
+            break;
+        case 'v':
+            variant = IMA_ADPCM_VDVI;
+            break;
+        default:
+            //usage();
+            exit(2);
+            break;
+        }
+    }
+
+    if ((inhandle = afOpenFile_telephony_read(in_file_name, 1)) == AF_NULL_FILEHANDLE)
+    {
+        fprintf(stderr, "    Cannot open wave file '%s'\n", in_file_name);
+        exit(2);
+    }
+
+    if ((outhandle = afOpenFile_telephony_write(OUT_FILE_NAME, 1)) == AF_NULL_FILEHANDLE)
+    {
+        fprintf(stderr, "    Cannot create wave file '%s'\n", OUT_FILE_NAME);
+        exit(2);
+    }
+
+    if ((ima_enc_state = ima_adpcm_init(NULL, variant, enc_chunk_size)) == NULL)
+    {
+        fprintf(stderr, "    Cannot create encoder\n");
+        exit(2);
+    }
+        
+    if ((ima_dec_state = ima_adpcm_init(NULL, variant, enc_chunk_size)) == NULL)
+    {
+        fprintf(stderr, "    Cannot create decoder\n");
+        exit(2);
+    }
+
+    hist_in = 0;
+    hist_out = 0;
+    pre_energy = 0.0;
+    post_energy = 0.0;
+    diff_energy = 0.0;
+    total_pre_samples = 0;
+    total_compressed_bytes = 0;
+    total_post_samples = 0;
+    while ((frames = afReadFrames(inhandle, AF_DEFAULT_TRACK, pre_amp, chunk_size)))
+    {
+        total_pre_samples += frames;
+        ima_bytes = ima_adpcm_encode(ima_enc_state, ima_data, pre_amp, frames);
+        if (log_encoded_data)
+            write(1, ima_data, ima_bytes);
+        total_compressed_bytes += ima_bytes;
+        dec_frames = ima_adpcm_decode(ima_dec_state, post_amp, ima_data, ima_bytes);
+        total_post_samples += dec_frames;
+        for (i = 0;  i < frames;  i++)
+        {
+            history[hist_in++] = pre_amp[i];
+            if (hist_in >= HIST_LEN)
+                hist_in = 0;
+            pre_energy += (double) pre_amp[i] * (double) pre_amp[i];
+        }
+        for (i = 0;  i < dec_frames;  i++)
+        {
+            post_energy += (double) post_amp[i] * (double) post_amp[i];
+            xx = post_amp[i] - history[hist_out++];
+            if (hist_out >= HIST_LEN)
+                hist_out = 0;
+            diff_energy += (double) xx * (double) xx;
+        }
+        outframes = afWriteFrames(outhandle, AF_DEFAULT_TRACK, post_amp, dec_frames);
+    }
+    if (afCloseFile(inhandle) != 0)
+    {
+        fprintf(stderr, "    Cannot close wave file '%s'\n", in_file_name);
+        exit(2);
+    }
+    if (afCloseFile(outhandle) != 0)
+    {
+        fprintf(stderr, "    Cannot close wave file '%s'\n", OUT_FILE_NAME);
+        exit(2);
+    }
+    ima_adpcm_release(ima_enc_state);
+    ima_adpcm_release(ima_dec_state);
+
+    printf("Pre samples: %d\n", total_pre_samples);
+    printf("Compressed bytes: %d\n", total_compressed_bytes);
+    printf("Post samples: %d\n", total_post_samples);
+
+    printf("Output energy is %f%% of input energy.\n", 100.0*post_energy/pre_energy);
+    printf("Residual energy is %f%% of the total.\n", 100.0*diff_energy/post_energy);
+    if (fabs(1.0 - post_energy/pre_energy) > 0.05
+        ||
+        fabs(diff_energy/post_energy) > 0.03)
+    {
+        printf("Tests failed.\n");
+        exit(2);
+    }
+    
+    printf("Tests passed.\n");
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/tests/line_model_monitor.cpp
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/tests/line_model_monitor.cpp	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,451 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * line_model_monitor.cpp - Model activity in a telephone line model, using the FLTK toolkit.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2006 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: line_model_monitor.cpp,v 1.4 2008/05/27 15:08:21 steveu Exp $
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#if defined(HAVE_FL_FL_H)  &&  defined(HAVE_FL_FL_CARTESIAN_H)
+
+#define __STDC_LIMIT_MACROS
+
+#include <inttypes.h>
+#include <stdio.h>
+#include <math.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/select.h>
+#if defined(HAVE_FFTW3_H)
+#include <fftw3.h>
+#else
+#include <fftw.h>
+#endif
+
+#include <FL/Fl.H>
+#include <FL/Fl_Overlay_Window.H>
+#include <FL/Fl_Light_Button.H>
+#include <FL/Fl_Cartesian.H>
+#include <FL/Fl_Audio_Meter.H>
+#include <FL/fl_draw.H>
+
+#include "spandsp.h"
+#include "line_model_monitor.h"
+
+Fl_Double_Window *w;
+
+Fl_Audio_Meter *audio_meter;
+
+Fl_Group *c_spec;
+Fl_Group *c_right;
+Fl_Group *c_can;
+Fl_Group *c_line_model;
+
+Ca_Canvas *canvas_spec;
+Ca_X_Axis *spec_freq;
+Ca_Y_Axis *spec_amp;
+Ca_Line *spec_re = NULL;
+double spec_re_plot[2*512];
+
+Ca_Canvas *canvas_can;
+Ca_X_Axis *can_x;
+Ca_Y_Axis *can_y;
+Ca_Line *can_re = NULL;
+double can_re_plot[512];
+
+Ca_Canvas *canvas_line_model;
+Ca_X_Axis *line_model_x;
+Ca_Y_Axis *line_model_y;
+Ca_Line *line_model_re = NULL;
+double line_model_re_plot[512];
+
+static int skip = 0;
+
+int in_ptr;
+#if defined(HAVE_FFTW3_H)
+double in[1024][2];
+double out[1024][2];
+#else
+fftw_complex in[1024];
+fftw_complex out[1024];
+#endif
+fftw_plan p;
+
+int line_model_monitor_can_update(const float *coeffs, int len)
+{
+    int i;
+    float min;
+    float max;
+
+    if (can_re)
+        delete can_re;
+
+    canvas_can->current(canvas_can);
+    i = 0;
+    min = coeffs[i];
+    max = coeffs[i];
+    for (i = 0;  i < len;  i++)
+    {
+        can_re_plot[2*i] = i;
+        can_re_plot[2*i + 1] = coeffs[i];
+        if (min > coeffs[i])
+            min = coeffs[i];
+        if (max < coeffs[i])
+            max = coeffs[i];
+    }
+    can_y->maximum((max == min)  ?  max + 0.2  :  max);
+    can_y->minimum(min);
+    can_re = new Ca_Line(len, can_re_plot, 0, 0, FL_BLUE, CA_NO_POINT);
+    if (++skip >= 100)
+    {
+        skip = 0;
+        Fl::check();
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int line_model_monitor_line_model_update(const float *coeffs, int len)
+{
+    int i;
+    float min;
+    float max;
+
+    if (line_model_re)
+        delete line_model_re;
+
+    canvas_line_model->current(canvas_line_model);
+    i = 0;
+    min = coeffs[i];
+    max = coeffs[i];
+    for (i = 0;  i < len;  i++)
+    {
+        line_model_re_plot[2*i] = i;
+        line_model_re_plot[2*i + 1] = coeffs[i];
+        if (min > coeffs[i])
+            min = coeffs[i];
+        if (max < coeffs[i])
+            max = coeffs[i];
+    }
+    line_model_y->maximum((max == min)  ?  max + 0.2  :  max);
+    line_model_y->minimum(min);
+    line_model_re = new Ca_Line(len, line_model_re_plot, 0, 0, FL_BLUE, CA_NO_POINT);
+    if (++skip >= 100)
+    {
+        skip = 0;
+        Fl::check();
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int line_model_monitor_line_spectrum_update(const int16_t amp[], int len)
+{
+    int i;
+    int x;
+
+    for (i = 0;  i < len;  i++)
+        audio_meter->sample(amp[i]/32768.0);
+
+    if (in_ptr + len < 512)
+    {
+        /* Just add this fragment to the buffer. */
+        for (i = 0;  i < len;  i++)
+#if defined(HAVE_FFTW3_H)
+            in[in_ptr + i][0] = amp[i];
+#else
+            in[in_ptr + i].re = amp[i];
+#endif
+        in_ptr += len;
+        return 0;
+    }
+    if (len >= 512)
+    {
+        /* We have enough for a whole block. Use the last 512 samples
+           we have. */
+        x = len - 512;
+        for (i = 0;  i < 512;  i++)
+#if defined(HAVE_FFTW3_H)
+            in[i][0] = amp[x + i];
+#else
+            in[i].re = amp[x + i];
+#endif
+    }
+    else
+    {
+        /* We want the last 512 samples. */
+        x = 512 - len;
+        for (i = 0;  i < x;  i++)
+#if defined(HAVE_FFTW3_H)
+            in[i][0] = in[in_ptr - x + i][0];
+#else
+            in[i].re = in[in_ptr - x + i].re;
+#endif
+        for (i = x;  i < 512;  i++)
+#if defined(HAVE_FFTW3_H)
+            in[i][0] = amp[i - x];
+#else
+            in[i].re = amp[i - x];
+#endif
+    }
+    in_ptr = 0;
+#if defined(HAVE_FFTW3_H)    
+    fftw_execute(p);
+#else
+    fftw_one(p, in, out);
+#endif
+    if (spec_re)
+        delete spec_re;
+    canvas_spec->current(canvas_spec);
+    for (i = 0;  i < 512;  i++)
+    {
+        spec_re_plot[2*i] = i*4000.0/512.0;
+#if defined(HAVE_FFTW3_H)    
+        spec_re_plot[2*i + 1] = 20.0*log10(sqrt(out[i][0]*out[i][0] + out[i][1]*out[i][1])/(256.0*32768)) + 3.14;
+#else
+        spec_re_plot[2*i + 1] = 20.0*log10(sqrt(out[i].re*out[i].re + out[i].im*out[i].im)/(256.0*32768)) + 3.14;
+#endif
+    }
+    spec_re = new Ca_Line(512, spec_re_plot, 0, 0, FL_BLUE, CA_NO_POINT);
+    Fl::check();
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int start_line_model_monitor(int len)
+{
+    char buf[132 + 1];
+    float x;
+    float y;
+    int i;
+
+    w = new Fl_Double_Window(850, 400, "Telephone line model monitor");
+
+    c_spec = new Fl_Group(0, 0, 380, 400);
+    c_spec->box(FL_DOWN_BOX);
+    c_spec->align(FL_ALIGN_TOP | FL_ALIGN_INSIDE);
+
+    canvas_spec = new Ca_Canvas(60, 30, 300, 300, "Spectrum");
+    canvas_spec->box(FL_PLASTIC_DOWN_BOX);
+    canvas_spec->color(7);
+    canvas_spec->align(FL_ALIGN_TOP);
+    canvas_spec->border(15);
+
+    spec_freq = new Ca_X_Axis(65, 330, 290, 30, "Freq (Hz)");
+    spec_freq->align(FL_ALIGN_BOTTOM);
+    spec_freq->minimum(0);
+    spec_freq->maximum(4000);
+    spec_freq->label_format("%g");
+    spec_freq->minor_grid_color(fl_gray_ramp(20));
+    spec_freq->major_grid_color(fl_gray_ramp(15));
+    spec_freq->label_grid_color(fl_gray_ramp(10));
+    spec_freq->grid_visible(CA_LABEL_GRID | CA_ALWAYS_VISIBLE);
+    spec_freq->minor_grid_style(FL_DOT);
+    spec_freq->major_step(5);
+    spec_freq->label_step(1);
+    spec_freq->axis_color(FL_BLACK);
+    spec_freq->axis_align(CA_BOTTOM | CA_LINE);
+
+    spec_amp = new Ca_Y_Axis(20, 35, 40, 290, "Amp (dBmO)");
+    spec_amp->align(FL_ALIGN_LEFT);
+    spec_amp->minimum(-80.0);
+    spec_amp->maximum(10.0);
+    spec_amp->minor_grid_color(fl_gray_ramp(20));
+    spec_amp->major_grid_color(fl_gray_ramp(15));
+    spec_amp->label_grid_color(fl_gray_ramp(10));
+    //spec_amp->grid_visible(CA_MINOR_TICK | CA_MAJOR_TICK | CA_LABEL_GRID | CA_ALWAYS_VISIBLE);
+    spec_amp->grid_visible(CA_LABEL_GRID | CA_ALWAYS_VISIBLE);
+    spec_amp->minor_grid_style(FL_DOT);
+    spec_amp->major_step(5);
+    spec_amp->label_step(1);
+    spec_amp->axis_color(FL_BLACK);
+
+    spec_amp->current();
+
+    c_spec->end();
+
+    c_right = new Fl_Group(440, 0, 465, 405);
+
+    c_can = new Fl_Group(380, 0, 415, 200);
+    c_can->box(FL_DOWN_BOX);
+    c_can->align(FL_ALIGN_TOP | FL_ALIGN_INSIDE);
+    c_can->current();
+
+    canvas_can = new Ca_Canvas(460, 35, 300, 100, "??? coefficients");
+    canvas_can->box(FL_PLASTIC_DOWN_BOX);
+    canvas_can->color(7);
+    canvas_can->align(FL_ALIGN_TOP);
+    Fl_Group::current()->resizable(canvas_can);
+    canvas_can->border(15);
+
+    can_x = new Ca_X_Axis(465, 135, 290, 30, "Tap");
+    can_x->align(FL_ALIGN_BOTTOM);
+    can_x->minimum(0.0);
+    can_x->maximum((float) len);
+    can_x->label_format("%g");
+    can_x->minor_grid_color(fl_gray_ramp(20));
+    can_x->major_grid_color(fl_gray_ramp(15));
+    can_x->label_grid_color(fl_gray_ramp(10));
+    can_x->grid_visible(CA_LABEL_GRID | CA_ALWAYS_VISIBLE);
+    can_x->minor_grid_style(FL_DOT);
+    can_x->major_step(5);
+    can_x->label_step(1);
+    can_x->axis_align(CA_BOTTOM | CA_LINE);
+    can_x->axis_color(FL_BLACK);
+    can_x->current();
+
+    can_y = new Ca_Y_Axis(420, 40, 40, 90, "Amp");
+    can_y->align(FL_ALIGN_LEFT);
+    can_y->minimum(-0.1);
+    can_y->maximum(0.1);
+    can_y->minor_grid_color(fl_gray_ramp(20));
+    can_y->major_grid_color(fl_gray_ramp(15));
+    can_y->label_grid_color(fl_gray_ramp(10));
+    can_y->grid_visible(CA_LABEL_GRID | CA_ALWAYS_VISIBLE);
+    can_y->minor_grid_style(FL_DOT);
+    can_y->major_step(5);
+    can_y->label_step(1);
+    can_y->axis_color(FL_BLACK);
+    can_y->current();
+
+    c_can->end();
+
+    c_line_model = new Fl_Group(380, 200, 415, 200);
+    c_line_model->box(FL_DOWN_BOX);
+    c_line_model->align(FL_ALIGN_TOP | FL_ALIGN_INSIDE);
+    c_line_model->current();
+
+    canvas_line_model = new Ca_Canvas(460, 235, 300, 100, "Line impulse response model");
+    canvas_line_model->box(FL_PLASTIC_DOWN_BOX);
+    canvas_line_model->color(7);
+    canvas_line_model->align(FL_ALIGN_TOP);
+    Fl_Group::current()->resizable(canvas_line_model);
+    canvas_line_model->border(15);
+
+    line_model_x = new Ca_X_Axis(465, 335, 290, 30, "Tap");
+    line_model_x->align(FL_ALIGN_BOTTOM);
+    line_model_x->minimum(0.0);
+    line_model_x->maximum((float) len);
+    line_model_x->label_format("%g");
+    line_model_x->minor_grid_color(fl_gray_ramp(20));
+    line_model_x->major_grid_color(fl_gray_ramp(15));
+    line_model_x->label_grid_color(fl_gray_ramp(10));
+    line_model_x->grid_visible(CA_LABEL_GRID | CA_ALWAYS_VISIBLE);
+    line_model_x->minor_grid_style(FL_DOT);
+    line_model_x->major_step(5);
+    line_model_x->label_step(1);
+    line_model_x->axis_align(CA_BOTTOM | CA_LINE);
+    line_model_x->axis_color(FL_BLACK);
+    line_model_x->current();
+
+    line_model_y = new Ca_Y_Axis(420, 240, 40, 90, "Amp");
+    line_model_y->align(FL_ALIGN_LEFT);
+    line_model_y->minimum(-0.1);
+    line_model_y->maximum(0.1);
+    line_model_y->minor_grid_color(fl_gray_ramp(20));
+    line_model_y->major_grid_color(fl_gray_ramp(15));
+    line_model_y->label_grid_color(fl_gray_ramp(10));
+    line_model_y->grid_visible(CA_LABEL_GRID | CA_ALWAYS_VISIBLE);
+    line_model_y->minor_grid_style(FL_DOT);
+    line_model_y->major_step(5);
+    line_model_y->label_step(1);
+    line_model_y->axis_color(FL_BLACK);
+    line_model_y->current();
+
+    c_line_model->end();
+
+    audio_meter = new Fl_Audio_Meter(810, 40, 10, 250, "");
+    audio_meter->box(FL_PLASTIC_UP_BOX);
+    audio_meter->type(FL_VERT_AUDIO_METER);
+
+    c_right->end();
+
+    Fl_Group::current()->resizable(c_right);
+    w->end();
+    w->show();
+
+#if defined(HAVE_FFTW3_H)    
+    p = fftw_plan_dft_1d(1024, in, out, FFTW_BACKWARD, FFTW_ESTIMATE);
+    for (i = 0;  i < 1024;  i++)
+    {
+        in[i][0] = 0.0;
+        in[i][1] = 0.0;
+    }
+#else
+    p = fftw_create_plan(1024, FFTW_BACKWARD, FFTW_ESTIMATE);
+    for (i = 0;  i < 1024;  i++)
+    {
+        in[i].re = 0.0;
+        in[i].im = 0.0;
+    }
+#endif
+    in_ptr = 0;
+
+    Fl::check();
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+void line_model_monitor_wait_to_end(void) 
+{
+    fd_set rfds;
+    int res;
+    struct timeval tv;
+
+    fprintf(stderr, "Processing complete.  Press the <enter> key to end\n");
+    do
+    {
+        usleep(100000);
+        Fl::check();
+        FD_ZERO(&rfds);
+        FD_SET(0, &rfds);
+        tv.tv_usec = 100000;
+        tv.tv_sec = 0;
+        res = select(1, &rfds, NULL, NULL, &tv);
+    }
+    while (res <= 0);
+}
+/*- End of function --------------------------------------------------------*/
+
+void line_model_monitor_update_display(void) 
+{
+    Fl::check();
+    Fl::check();
+    Fl::check();
+    Fl::check();
+    Fl::check();
+    Fl::check();
+    Fl::check();
+    Fl::check();
+    Fl::check();
+    Fl::check();
+    Fl::check();
+    Fl::check();
+}
+/*- End of function --------------------------------------------------------*/
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/tests/line_model_monitor.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/tests/line_model_monitor.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,62 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * line_model_monitor.h - Model activity in a telephone line model, using the FLTK toolkit.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2004 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: line_model_monitor.h,v 1.7 2008/04/26 13:39:17 steveu Exp $
+ */
+
+/*! \page line_model_monitor_page Telephone line model monitoring
+\section line_model_monitor_page_sec_1 What does it do?
+This code controls a GUI window, which provides monitoring of the internal status
+of a telephone line modem. It shows, graphically:
+
+    - the spectrum of the received signal.
+    - the line model in use (when a known line model is being used).
+    - the adapted coefficients of the canceller.
+
+\section line_model_monitor_page_sec_2 How does it work?
+This code uses the FLTK cross platform GUI toolkit. It works on X11 and Windows platforms.
+In addition to the basic FLTK toolkit, fltk_cartesian is also required.
+*/
+
+#if !defined(_LINE_MODEL_MONITOR_H_)
+#define _LINE_MODEL_MONITOR_H_
+
+#if defined(__cplusplus)
+extern "C"
+{
+#endif
+
+int start_line_model_monitor(int len);
+int line_model_monitor_can_update(const float *coeffs, int len);
+int line_model_monitor_line_model_update(const float *coeffs, int len);
+int line_model_monitor_line_spectrum_update(const int16_t amp[], int len);
+void line_model_monitor_wait_to_end(void);
+void line_model_monitor_update_display(void);
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/tests/line_model_tests.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/tests/line_model_tests.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,381 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * line_model_tests.c - Tests for the telephone line model.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2004 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: line_model_tests.c,v 1.24 2008/05/13 13:17:26 steveu Exp $
+ */
+
+/*! \page line_model_tests_page Telephony line model tests
+\section line_model_tests_page_sec_1 What does it do?
+???.
+
+\section line_model_tests_page_sec_2 How does it work?
+???.
+*/
+
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <string.h>
+#include <time.h>
+#include <audiofile.h>
+
+#include "spandsp.h"
+#include "spandsp-sim.h"
+
+#if !defined(NULL)
+#define NULL (void *) 0
+#endif
+
+#define BLOCK_LEN           160
+
+#define OUT_FILE_COMPLEXIFY "complexify.wav"
+#define IN_FILE_NAME1       "line_model_test_in1.wav"
+#define IN_FILE_NAME2       "line_model_test_in2.wav"
+#define OUT_FILE_NAME1      "line_model_one_way_test_out.wav"
+#define OUT_FILE_NAME       "line_model_test_out.wav"
+
+int channel_codec;
+int rbs_pattern;
+
+static void complexify_tests(void)
+{
+    complexify_state_t *s;
+    complexf_t cc;
+    int16_t amp;
+    int i;
+    AFfilehandle outhandle;
+    AFfilesetup filesetup;
+    int outframes;
+    int16_t out[40000];
+    awgn_state_t noise1;
+    
+    if ((filesetup = afNewFileSetup()) == AF_NULL_FILESETUP)
+    {
+        fprintf(stderr, "    Failed to create file setup\n");
+        exit(2);
+    }
+    afInitSampleFormat(filesetup, AF_DEFAULT_TRACK, AF_SAMPFMT_TWOSCOMP, 16);
+    afInitRate(filesetup, AF_DEFAULT_TRACK, (float) SAMPLE_RATE);
+    afInitFileFormat(filesetup, AF_FILE_WAVE);
+    afInitChannels(filesetup, AF_DEFAULT_TRACK, 2);
+
+    if ((outhandle = afOpenFile(OUT_FILE_COMPLEXIFY, "w", filesetup)) == AF_NULL_FILEHANDLE)
+    {
+        fprintf(stderr, "    Cannot create wave file '%s'\n", OUT_FILE_COMPLEXIFY);
+        exit(2);
+    }
+    awgn_init_dbm0(&noise1, 1234567, -10.0f);
+    s = complexify_init();
+    for (i = 0;  i < 20000;  i++)
+    {
+        amp = awgn(&noise1);
+        cc = complexify(s, amp);
+        out[2*i] = cc.re;
+        out[2*i + 1] = cc.im;
+    }
+    outframes = afWriteFrames(outhandle,
+                              AF_DEFAULT_TRACK,
+                              out,
+                              20000);
+    if (outframes != 20000)
+    {
+        fprintf(stderr, "    Error writing wave file\n");
+        exit(2);
+    }
+    if (afCloseFile(outhandle))
+    {
+        fprintf(stderr, "    Cannot close wave file '%s'\n", OUT_FILE_COMPLEXIFY);
+        exit(2);
+    }
+    afFreeFileSetup(filesetup);
+}
+/*- End of function --------------------------------------------------------*/
+
+static void test_one_way_model(int line_model_no, int speech_test)
+{
+    one_way_line_model_state_t *model;
+    int16_t input1[BLOCK_LEN];
+    int16_t output1[BLOCK_LEN];
+    int16_t amp[2*BLOCK_LEN];
+    AFfilehandle inhandle1;
+    AFfilehandle outhandle;
+    AFfilesetup filesetup;
+    int outframes;
+    int samples;
+    int i;
+    int j;
+    awgn_state_t noise1;
+    
+    if ((model = one_way_line_model_init(line_model_no, -50, channel_codec, rbs_pattern)) == NULL)
+    {
+        fprintf(stderr, "    Failed to create line model\n");
+        exit(2);
+    }
+    
+    awgn_init_dbm0(&noise1, 1234567, -10.0f);
+
+    if ((filesetup = afNewFileSetup()) == AF_NULL_FILESETUP)
+    {
+        fprintf(stderr, "    Failed to create file setup\n");
+        exit(2);
+    }
+    afInitSampleFormat(filesetup, AF_DEFAULT_TRACK, AF_SAMPFMT_TWOSCOMP, 16);
+    afInitRate(filesetup, AF_DEFAULT_TRACK, (float) SAMPLE_RATE);
+    afInitFileFormat(filesetup, AF_FILE_WAVE);
+    afInitChannels(filesetup, AF_DEFAULT_TRACK, 1);
+
+    if ((inhandle1 = afOpenFile(IN_FILE_NAME1, "r", NULL)) == AF_NULL_FILEHANDLE)
+    {
+        fprintf(stderr, "    Cannot open wave file '%s'\n", IN_FILE_NAME1);
+        exit(2);
+    }
+    if ((outhandle = afOpenFile(OUT_FILE_NAME1, "w", filesetup)) == AF_NULL_FILEHANDLE)
+    {
+        fprintf(stderr, "    Cannot create wave file '%s'\n", OUT_FILE_NAME1);
+        exit(2);
+    }
+    for (i = 0;  i < 10000;  i++)
+    {
+        if (speech_test)
+        {
+            samples = afReadFrames(inhandle1,
+                                   AF_DEFAULT_TRACK,
+                                   input1,
+                                   BLOCK_LEN);
+            if (samples == 0)
+                break;
+        }
+        else
+        {
+            for (j = 0;  j < BLOCK_LEN;  j++)
+                input1[j] = awgn(&noise1);
+            samples = BLOCK_LEN;
+        }
+        for (j = 0;  j < samples;  j++)
+        {
+            one_way_line_model(model, 
+                               &output1[j],
+                               &input1[j],
+                               1);
+            amp[j] = output1[j];
+        }
+        outframes = afWriteFrames(outhandle,
+                                  AF_DEFAULT_TRACK,
+                                  amp,
+                                  samples);
+        if (outframes != samples)
+        {
+            fprintf(stderr, "    Error writing wave file\n");
+            exit(2);
+        }
+    }
+    if (afCloseFile(inhandle1))
+    {
+        fprintf(stderr, "    Cannot close wave file '%s'\n", IN_FILE_NAME1);
+        exit(2);
+    }
+    if (afCloseFile(outhandle))
+    {
+        fprintf(stderr, "    Cannot close wave file '%s'\n", OUT_FILE_NAME1);
+        exit(2);
+    }
+    afFreeFileSetup(filesetup);
+    one_way_line_model_release(model);
+}
+/*- End of function --------------------------------------------------------*/
+
+static void test_both_ways_model(int line_model_no, int speech_test)
+{
+    both_ways_line_model_state_t *model;
+    int16_t input1[BLOCK_LEN];
+    int16_t input2[BLOCK_LEN];
+    int16_t output1[BLOCK_LEN];
+    int16_t output2[BLOCK_LEN];
+    int16_t amp[2*BLOCK_LEN];
+    AFfilehandle inhandle1;
+    AFfilehandle inhandle2;
+    AFfilehandle outhandle;
+    AFfilesetup filesetup;
+    int outframes;
+    int samples;
+    int i;
+    int j;
+    awgn_state_t noise1;
+    awgn_state_t noise2;
+    
+    if ((model = both_ways_line_model_init(line_model_no, -50, line_model_no + 1, -35, channel_codec, rbs_pattern)) == NULL)
+    {
+        fprintf(stderr, "    Failed to create line model\n");
+        exit(2);
+    }
+    
+    awgn_init_dbm0(&noise1, 1234567, -10.0f);
+    awgn_init_dbm0(&noise2, 1234567, -10.0f);
+
+    filesetup = afNewFileSetup();
+    if (filesetup == AF_NULL_FILESETUP)
+    {
+        fprintf(stderr, "    Failed to create file setup\n");
+        exit(2);
+    }
+    afInitSampleFormat(filesetup, AF_DEFAULT_TRACK, AF_SAMPFMT_TWOSCOMP, 16);
+    afInitRate(filesetup, AF_DEFAULT_TRACK, (float) SAMPLE_RATE);
+    afInitFileFormat(filesetup, AF_FILE_WAVE);
+    afInitChannels(filesetup, AF_DEFAULT_TRACK, 2);
+
+    inhandle1 = afOpenFile(IN_FILE_NAME1, "r", NULL);
+    if (inhandle1 == AF_NULL_FILEHANDLE)
+    {
+        fprintf(stderr, "    Cannot open wave file '%s'\n", IN_FILE_NAME1);
+        exit(2);
+    }
+    inhandle2 = afOpenFile(IN_FILE_NAME2, "r", NULL);
+    if (inhandle2 == AF_NULL_FILEHANDLE)
+    {
+        fprintf(stderr, "    Cannot open wave file '%s'\n", IN_FILE_NAME2);
+        exit(2);
+    }
+    outhandle = afOpenFile(OUT_FILE_NAME, "w", filesetup);
+    if (outhandle == AF_NULL_FILEHANDLE)
+    {
+        fprintf(stderr, "    Cannot create wave file '%s'\n", OUT_FILE_NAME);
+        exit(2);
+    }
+    for (i = 0;  i < 10000;  i++)
+    {
+        if (speech_test)
+        {
+            samples = afReadFrames(inhandle1,
+                                   AF_DEFAULT_TRACK,
+                                   input1,
+                                   BLOCK_LEN);
+            if (samples == 0)
+                break;
+            samples = afReadFrames(inhandle2,
+                                   AF_DEFAULT_TRACK,
+                                   input2,
+                                   samples);
+            if (samples == 0)
+                break;
+        }
+        else
+        {
+            for (j = 0;  j < BLOCK_LEN;  j++)
+            {
+                input1[j] = awgn(&noise1);
+                input2[j] = awgn(&noise2);
+            }
+            samples = BLOCK_LEN;
+        }
+        for (j = 0;  j < samples;  j++)
+        {
+            both_ways_line_model(model, 
+                                 &output1[j],
+                                 &input1[j],
+                                 &output2[j],
+                                 &input2[j],
+                                 1);
+            amp[2*j] = output1[j];
+            amp[2*j + 1] = output2[j];
+        }
+        outframes = afWriteFrames(outhandle,
+                                  AF_DEFAULT_TRACK,
+                                  amp,
+                                  samples);
+        if (outframes != samples)
+        {
+            fprintf(stderr, "    Error writing wave file\n");
+            exit(2);
+        }
+    }
+    if (afCloseFile(inhandle1))
+    {
+        fprintf(stderr, "    Cannot close wave file '%s'\n", IN_FILE_NAME1);
+        exit(2);
+    }
+    if (afCloseFile(inhandle2))
+    {
+        fprintf(stderr, "    Cannot close wave file '%s'\n", IN_FILE_NAME2);
+        exit(2);
+    }
+    if (afCloseFile(outhandle))
+    {
+        fprintf(stderr, "    Cannot close wave file '%s'\n", OUT_FILE_NAME);
+        exit(2);
+    }
+    afFreeFileSetup(filesetup);
+    both_ways_line_model_release(model);
+}
+/*- End of function --------------------------------------------------------*/
+
+int main(int argc, char *argv[])
+{
+    int line_model_no;
+    int speech_test;
+    int log_audio;
+    int i;
+
+    line_model_no = 0;
+    speech_test = FALSE;
+    log_audio = FALSE;
+    channel_codec = MUNGE_CODEC_NONE;
+    for (i = 1;  i < argc;  i++)
+    {
+        if (strcmp(argv[i], "-c") == 0)
+        {
+            channel_codec = atoi(argv[++i]);
+            continue;
+        }
+        if (strcmp(argv[i], "-l") == 0)
+        {
+            log_audio = TRUE;
+            continue;
+        }
+        if (strcmp(argv[i], "-m") == 0)
+        {
+            line_model_no = atoi(argv[++i]);
+            continue;
+        }
+        if (strcmp(argv[i], "-r") == 0)
+        {
+            rbs_pattern = atoi(argv[++i]);
+            continue;
+        }
+        if (strcmp(argv[i], "-s") == 0)
+        {
+            speech_test = TRUE;
+            continue;
+        }
+    }
+    complexify_tests();
+    test_one_way_model(line_model_no, speech_test);
+    test_both_ways_model(line_model_no, speech_test);
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/tests/logging_tests.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/tests/logging_tests.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,231 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * logging_tests.c - Tests for the logging functions.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2006 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: logging_tests.c,v 1.14 2008/05/13 13:17:26 steveu Exp $
+ */
+
+/*! \page logging_tests_page Logging tests
+\section logging_tests_page_sec_1 What does it do?
+*/
+
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
+
+#define _POSIX_SOURCE
+#define _POSIX_C_SOURCE 200112L
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <memory.h>
+#include <time.h>
+
+#include "spandsp.h"
+
+static int tests_failed = FALSE;
+
+static int msg_step = 0;
+static int msg2_step = 0;
+static int error_step = 0;
+static int msg_done = FALSE;
+static int msg2_done = FALSE;
+static int error_done = FALSE;
+
+static void message_handler(int level, const char *text)
+{
+    const char *ref[] =
+    {
+        "TAG Log with tag 1 2 3\n",
+        "Log with protocol 1 2 3\n",
+        "FLOW NewTag Log with new tag 1 2 3\n",
+        "FLOW Protocol NewTag Log with protocol 1 2 3\n",
+        "FLOW Protocol NewTag Buf 00 01 02 03 04 05 06 07 08 09\n",
+        "FLOW Protocol NewTag Buf 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f 20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f 30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f 60 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f 70 71 72 73 74 75 76 77 78 79 7a 7b 7c 7d 7e 7f 80 81 82 83 84 85 86 87 88 89 8a 8b 8c 8d 8e 8f 90 91 92 93 94 95 96 97 98 99 9a 9b 9c 9d 9e 9f a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 ba bb bc bd be bf c0 c1 c2 c3 c4 c5 c6 c7 c8 c9 ca cb cc cd ce cf d0 d1 d2 d3 d4 d5 d6 d7 d8 d9 da db dc dd de df e0 e1 e2 e3 e4 e5 e6 e7 e8 e9 ea eb ec ed ee ef f0 f1 f2 f3 f4 f5 f6 f7 f8 f9 fa fb fc fd fe ff 00 01 02 03 04 05 06 07 08 09\n",
+        "00:00:00.000 FLOW Protocol NewTag Time tagged log 1 2 3\n",
+        "00:00:00.020 FLOW Protocol NewTag Time tagged log 1 2 3\n",
+        "00:00:00.040 FLOW Protocol NewTag Time tagged log 1 2 3\n",
+        "00:00:00.060 FLOW Protocol NewTag Time tagged log 1 2 3\n",
+        "00:00:00.080 FLOW Protocol NewTag Time tagged log 1 2 3\n",
+        "00:00:00.100 FLOW Protocol NewTag Time tagged log 1 2 3\n",
+        "00:00:00.120 FLOW Protocol NewTag Time tagged log 1 2 3\n",
+        "00:00:00.140 FLOW Protocol NewTag Time tagged log 1 2 3\n",
+        "00:00:00.160 FLOW Protocol NewTag Time tagged log 1 2 3\n",
+        "00:00:00.180 FLOW Protocol NewTag Time tagged log 1 2 3\n",
+        ""
+    };
+
+    if (strcmp(ref[msg_step], text))
+    {
+        printf(">>>: %s", ref[msg_step]);
+        tests_failed = TRUE;
+    }
+    if (ref[++msg_step][0] == '\0')
+        msg_done = TRUE;
+    printf("MSG: %s", text);
+}
+/*- End of function --------------------------------------------------------*/
+
+static void message_handler2(int level, const char *text)
+{
+    /* TODO: This doesn't check if the date/time field makes sense */
+    if (strcmp(" FLOW Protocol NewTag Date/time tagged log 1 2 3\n", text + 23))
+    {
+        printf(">>>: %s", text + 23);
+        tests_failed = TRUE;
+    }
+    if (++msg2_step == 10)
+        msg2_done = TRUE;
+    printf("MSG: %s", text);
+}
+/*- End of function --------------------------------------------------------*/
+
+static void error_handler(const char *text)
+{
+    const char *ref[] =
+    {
+        "ERROR Log with severity log 1 2 3\n",
+        ""
+    };
+    
+    if (strcmp(ref[error_step], text))
+    {
+        printf(">>>: %s", ref[error_step]);
+        tests_failed = TRUE;
+    }
+    if (ref[++error_step][0] == '\0')
+        error_done = TRUE;
+    printf("ERR: %s", text);
+}
+/*- End of function --------------------------------------------------------*/
+
+int main(int argc, char *argv[])
+{
+    logging_state_t log;
+    int i;
+    uint8_t buf[1000];
+    struct timespec delay;
+
+    /* Set up a logger */
+    if (span_log_init(&log, 123, "TAG"))
+    {
+        fprintf(stderr, "Failed to initialise log.\n");
+        exit(2);
+    }
+    /* Try it */
+    span_log_set_level(&log, SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_SHOW_TAG | SPAN_LOG_FLOW);
+    if (span_log(&log, SPAN_LOG_FLOW, "Logging to fprintf, as simple as %d %d %d\n", 1, 2, 3))
+        fprintf(stderr, "Logged.\n");
+    else
+        fprintf(stderr, "Not logged.\n");
+
+    /* Now set a custom log handler */
+    span_log_set_message_handler(&log, &message_handler);
+    span_log_set_error_handler(&log, &error_handler);
+    span_log_set_sample_rate(&log, 44100);
+
+    /* Try the different logging elements */
+    span_log_set_level(&log, SPAN_LOG_SHOW_TAG | SPAN_LOG_FLOW);
+    if (span_log(&log, SPAN_LOG_FLOW, "Log with tag %d %d %d\n", 1, 2, 3))
+        fprintf(stderr, "Logged.\n");
+    else
+        fprintf(stderr, "Not logged.\n");
+    span_log_set_level(&log, SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_FLOW);
+    if (span_log(&log, SPAN_LOG_FLOW, "Log with protocol %d %d %d\n", 1, 2, 3))
+        fprintf(stderr, "Logged.\n");
+    else
+        fprintf(stderr, "Not logged.\n");
+    span_log_set_level(&log, SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_FLOW);
+    if (span_log(&log, SPAN_LOG_ERROR, "Log with severity log %d %d %d\n", 1, 2, 3))
+        fprintf(stderr, "Logged.\n");
+    else
+        fprintf(stderr, "Not logged.\n");
+
+    span_log_set_level(&log, SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_SHOW_TAG | SPAN_LOG_FLOW);
+    span_log_set_tag(&log, "NewTag");
+    if (span_log(&log, SPAN_LOG_FLOW, "Log with new tag %d %d %d\n", 1, 2, 3))
+        fprintf(stderr, "Logged.\n");
+    else
+        fprintf(stderr, "Not logged.\n");
+
+    span_log_set_protocol(&log, "Protocol");
+    if (span_log(&log, SPAN_LOG_FLOW, "Log with protocol %d %d %d\n", 1, 2, 3))
+        fprintf(stderr, "Logged.\n");
+    else
+        fprintf(stderr, "Not logged.\n");
+    
+    /* Test logging of buffer contents */
+    for (i = 0;  i < 1000;  i++)
+        buf[i] = i;
+    if (span_log_buf(&log, SPAN_LOG_FLOW, "Buf", buf, 10))
+        fprintf(stderr, "Logged.\n");
+    else
+        fprintf(stderr, "Not logged.\n");
+    if (span_log_buf(&log, SPAN_LOG_FLOW, "Buf", buf, 1000))
+        fprintf(stderr, "Logged.\n");
+    else
+        fprintf(stderr, "Not logged.\n");
+
+    /* Test the correct severities will be logged */
+    for (i = 0;  i < 10;  i++)
+    {
+        if (!span_log_test(&log, i))
+        {
+            if (i != 6)
+                tests_failed = TRUE;
+            break;
+        }
+    }
+    
+    /* Check timestamping by samples */
+    span_log_set_level(&log, SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_SHOW_TAG | SPAN_LOG_FLOW | SPAN_LOG_SHOW_SAMPLE_TIME);
+    for (i = 0;  i < 10;  i++)
+    {
+        span_log(&log, SPAN_LOG_FLOW, "Time tagged log %d %d %d\n", 1, 2, 3);
+        span_log_bump_samples(&log, 441*2);
+    }
+
+    /* Check timestamping by current date and time */
+    span_log_set_message_handler(&log, &message_handler2);
+    span_log_set_level(&log, SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_SHOW_TAG | SPAN_LOG_FLOW | SPAN_LOG_SHOW_DATE);
+    for (i = 0;  i < 10;  i++)
+    {
+        span_log(&log, SPAN_LOG_FLOW, "Date/time tagged log %d %d %d\n", 1, 2, 3);
+        delay.tv_sec = 0;
+        delay.tv_nsec = 20000000;
+        nanosleep(&delay, NULL);
+    }
+    if (tests_failed  ||  !msg_done  ||  !error_done)
+    {
+        printf("Tests failed - %d %d %d.\n", tests_failed, msg_done, error_done);
+        return 2;
+    }
+    
+    span_log_set_message_handler(&log, &message_handler);
+
+    printf("Tests passed.\n");
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/tests/lpc10_tests.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/tests/lpc10_tests.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,262 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * lpc10_tests.c - Test the LPC10 low bit rate speech codec.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2006 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: lpc10_tests.c,v 1.21 2008/08/29 09:28:13 steveu Exp $
+ */
+
+/*! \file */
+
+/*! \page lpc10_tests_page LPC10 codec tests
+\section lpc10_tests_page_sec_1 What does it do?
+
+\section lpc10_tests_page_sec_2 How is it used?
+To perform a general audio quality test, lpc10 should be run. The file ../test-data/local/short_nb_voice.wav
+will be compressed to LPC10 data, decompressed, and the resulting audio stored in post_lpc10.wav.
+*/
+
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <string.h>
+#include <assert.h>
+#include <ctype.h>
+#include <audiofile.h>
+
+#include "spandsp.h"
+#include "spandsp-sim.h"
+
+#define BLOCK_LEN       180
+
+#define BLOCKS_PER_READ 5
+
+#define IN_FILE_NAME            "../test-data/local/dam9.wav"
+#define REF_FILE_NAME           "../test-data/local/dam9_lpc55.wav"
+#define COMPRESS_FILE_NAME      "lpc10_out.lpc10"
+#define DECOMPRESS_FILE_NAME    "lpc10_in.lpc10"
+#define OUT_FILE_NAME           "post_lpc10.wav"
+
+int main(int argc, char *argv[])
+{
+    AFfilehandle inhandle;
+    AFfilehandle refhandle;
+    AFfilehandle outhandle;
+    int frames;
+    int outframes;
+    double pre_energy;
+    double post_energy;
+    double ref_energy;
+    double diff_energy;
+    int16_t pre_amp[BLOCKS_PER_READ*BLOCK_LEN];
+    int16_t post_amp[BLOCKS_PER_READ*BLOCK_LEN];
+    int16_t ref_amp[BLOCKS_PER_READ*BLOCK_LEN];
+    int16_t log_amp[BLOCKS_PER_READ*BLOCK_LEN*3];
+    uint8_t lpc10_data[BLOCKS_PER_READ*7];
+    double xx;
+    lpc10_encode_state_t *lpc10_enc_state;
+    lpc10_decode_state_t *lpc10_dec_state;
+    int i;
+    int block_no;
+    int log_error;
+    int compress;
+    int decompress;
+    const char *in_file_name;
+    int compress_file;
+    int decompress_file;
+    int len;
+    int enc_len;
+    int dec_len;
+
+    compress = FALSE;
+    decompress = FALSE;
+    log_error = TRUE;
+    in_file_name = IN_FILE_NAME;
+    for (i = 1;  i < argc;  i++)
+    {
+        if (strcmp(argv[i], "-c") == 0)
+        {
+            compress = TRUE;
+            continue;
+        }
+        if (strcmp(argv[i], "-d") == 0)
+        {
+            decompress = TRUE;
+            continue;
+        }
+        if (strcmp(argv[i], "-i") == 0)
+        {
+            in_file_name = argv[++i];
+            continue;
+        }
+        if (strcmp(argv[i], "-l") == 0)
+        {
+            log_error = FALSE;
+            continue;
+        }
+    }
+
+    compress_file = -1;
+    decompress_file = -1;
+    inhandle = AF_NULL_FILEHANDLE;
+    refhandle = AF_NULL_FILEHANDLE;
+    outhandle = AF_NULL_FILEHANDLE;
+    if (!decompress)
+    {
+        if ((inhandle = afOpenFile_telephony_read(in_file_name, 1)) == AF_NULL_FILEHANDLE)
+        {
+            fprintf(stderr, "    Cannot open wave file '%s'\n", in_file_name);
+            exit(2);
+        }
+
+        if ((refhandle = afOpenFile_telephony_read(REF_FILE_NAME, 1)) == AF_NULL_FILEHANDLE)
+        {
+            fprintf(stderr, "    Cannot open wave file '%s'\n", REF_FILE_NAME);
+            exit(2);
+        }
+    }
+    else
+    {
+        if ((decompress_file = open(DECOMPRESS_FILE_NAME, O_RDONLY)) < 0)
+        {
+            fprintf(stderr, "    Cannot open decompressed data file '%s'\n", DECOMPRESS_FILE_NAME);
+            exit(2);
+        }
+    }
+
+    if ((outhandle = afOpenFile_telephony_write(OUT_FILE_NAME, 1)) == AF_NULL_FILEHANDLE)
+    {
+        fprintf(stderr, "    Cannot create wave file '%s'\n", OUT_FILE_NAME);
+        exit(2);
+    }
+    
+    if ((lpc10_enc_state = lpc10_encode_init(NULL, TRUE)) == NULL)
+    {
+        fprintf(stderr, "    Cannot create encoder\n");
+        exit(2);
+    }
+            
+    if ((lpc10_dec_state = lpc10_decode_init(NULL, TRUE)) == NULL)
+    {
+        fprintf(stderr, "    Cannot create decoder\n");
+        exit(2);
+    }
+
+    if (compress)
+    {
+        if ((compress_file = open(COMPRESS_FILE_NAME, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0)
+        {
+            fprintf(stderr, "    Cannot create compressed data file '%s'\n", COMPRESS_FILE_NAME);
+            exit(2);
+        }
+    }
+
+    pre_energy = 0.0;
+    post_energy = 0.0;
+    ref_energy = 0.0;
+    diff_energy = 0.0;
+
+    if (decompress)
+    {
+        while ((len = read(decompress_file, lpc10_data, BLOCKS_PER_READ*7)) > 0)
+        {
+            lpc10_decode(lpc10_dec_state, post_amp, lpc10_data, len/7);
+            outframes = afWriteFrames(outhandle, AF_DEFAULT_TRACK, post_amp, BLOCK_LEN*len/7);
+        }
+    }
+    else
+    {
+        block_no = 0;
+        while ((frames = afReadFrames(inhandle, AF_DEFAULT_TRACK, pre_amp, BLOCKS_PER_READ*BLOCK_LEN)) == BLOCKS_PER_READ*BLOCK_LEN
+                &&
+                (frames = afReadFrames(refhandle, AF_DEFAULT_TRACK, ref_amp, BLOCKS_PER_READ*BLOCK_LEN)) == BLOCKS_PER_READ*BLOCK_LEN)
+        {
+            enc_len = lpc10_encode(lpc10_enc_state, lpc10_data, pre_amp, BLOCKS_PER_READ*BLOCK_LEN);
+            if (compress)
+                write(compress_file, lpc10_data, enc_len);
+            dec_len = lpc10_decode(lpc10_dec_state, post_amp, lpc10_data, enc_len);
+            for (i = 0;  i < dec_len;  i++)
+            {
+                pre_energy += (double) pre_amp[i]*(double) pre_amp[i];
+                post_energy += (double) post_amp[i]*(double) post_amp[i];
+                ref_energy += (double) ref_amp[i]*(double) ref_amp[i];
+                /* The reference file has some odd clipping, so eliminate this from the
+                   energy measurement. */
+                if (ref_amp[i] == 32767  ||  ref_amp[i] == -32768)
+                    xx = 0.0;
+                else
+                    xx = post_amp[i] - ref_amp[i];
+                diff_energy += (double) xx*(double) xx;
+                log_amp[i] = xx;
+            }
+            block_no++;
+            if (log_error)
+                outframes = afWriteFrames(outhandle, AF_DEFAULT_TRACK, log_amp, dec_len);
+            else
+                outframes = afWriteFrames(outhandle, AF_DEFAULT_TRACK, post_amp, dec_len);
+        }
+        if (afCloseFile(inhandle) != 0)
+        {
+            fprintf(stderr, "    Cannot close wave file '%s'\n", in_file_name);
+            exit(2);
+        }
+        if (afCloseFile(refhandle) != 0)
+        {
+            fprintf(stderr, "    Cannot close wave file '%s'\n", REF_FILE_NAME);
+            exit(2);
+        }
+    }
+    
+    if (afCloseFile(outhandle) != 0)
+    {
+        fprintf(stderr, "    Cannot close wave file '%s'\n", OUT_FILE_NAME);
+        exit(2);
+    }
+    if (compress)
+        close(compress_file);
+    if (decompress)
+        close(decompress_file);
+    lpc10_encode_release(lpc10_enc_state);
+    lpc10_decode_release(lpc10_dec_state);
+
+    if (!decompress)
+    {
+        printf("Output energy is %f%% of input energy.\n", 100.0*post_energy/pre_energy);
+        printf("Difference energy is %f%% of the total.\n", 100.0*diff_energy/ref_energy);
+        if (fabs(1.0 - post_energy/pre_energy) > 0.05
+            ||
+            fabs(diff_energy/post_energy) > 0.03)
+        {
+            printf("Tests failed.\n");
+            exit(2);
+        }
+        printf("Tests passed.\n");
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/tests/make_g168_css.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/tests/make_g168_css.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,404 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * makecss.c - Create the composite source signal (CSS) for G.168 testing.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2003 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: make_g168_css.c,v 1.15 2008/08/29 09:28:13 steveu Exp $
+ */
+
+/*! \page makecss_page CSS construction for G.168 testing
+\section makecss_page_sec_1 What does it do?
+???.
+
+\section makecss_page_sec_2 How does it work?
+???.
+*/
+
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <unistd.h>
+#include <string.h>
+#include <time.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <audiofile.h>
+#if defined(HAVE_FFTW3_H)
+#include <fftw3.h>
+#else
+#include <fftw.h>
+#endif
+#if defined(HAVE_MATH_H)
+#define GEN_CONST
+#endif
+
+#include "spandsp.h"
+#include "spandsp/g168models.h"
+
+#if !defined(NULL)
+#define NULL (void *) 0
+#endif
+
+#define FAST_SAMPLE_RATE    44100.0
+
+#define C1_VOICED_SAMPLES   2144    /* 48.62ms at 44100 samples/second => 2144.142 */
+#define C1_NOISE_SAMPLES    8820    /* 200ms at 44100 samples/second => 8820.0 */
+#define C1_SILENCE_SAMPLES  4471    /* 101.38ms at 44100 samples/second => 4470.858 */
+
+#define C3_VOICED_SAMPLES   3206    /* 72.69ms at 44100 samples/second => 3205.629 */
+#define C3_NOISE_SAMPLES    8820    /* 200ms at 44100 samples/second => 8820.0 */
+#define C3_SILENCE_SAMPLES  5614    /* 127.31ms at 44100 samples/second => 5614.371 */
+
+static double scaling(double f, double start, double end, double start_gain, double end_gain)
+{
+    double scale;
+
+    scale = start_gain + (f - start)*(end_gain - start_gain)/(end - start);
+    return scale;
+}
+/*- End of function --------------------------------------------------------*/
+
+static double peak(const int16_t amp[], int len)
+{
+    int16_t peak;
+    int i;
+
+    peak = 0;
+    for (i = 0;  i < len;  i++)
+    {
+        if (abs(amp[i]) > peak)
+            peak = abs(amp[i]);
+    }
+    return peak/32767.0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static double rms(const int16_t amp[], int len)
+{
+    double ms;
+    int i;
+
+    ms = 0.0;
+    for (i = 0;  i < len;  i++)
+        ms += amp[i]*amp[i];
+    return sqrt(ms/len)/32767.0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static double rms_to_dbm0(double rms)
+{
+    return 20.0*log10(rms) + DBM0_MAX_POWER;
+}
+/*- End of function --------------------------------------------------------*/
+
+static double rms_to_db(double rms)
+{
+    return 20.0*log10(rms);
+}
+/*- End of function --------------------------------------------------------*/
+
+int main(int argc, char *argv[])
+{
+#if defined(HAVE_FFTW3_H)
+    double in[8192][2];
+    double out[8192][2];
+#else
+    fftw_complex in[8192];
+    fftw_complex out[8192];
+#endif
+    fftw_plan p;
+    int16_t voiced_sound[8192];
+    int16_t noise_sound[8830];
+    int16_t silence_sound[8192];
+    int i;
+    int outframes;
+    int voiced_length;
+    double f;
+    double pk;
+    double ms;
+    double scale;
+    AFfilehandle filehandle;
+    AFfilesetup filesetup;
+    awgn_state_t noise_source;
+
+    if ((filesetup = afNewFileSetup()) == AF_NULL_FILESETUP)
+    {
+        fprintf(stderr, "    Failed to create file setup\n");
+        exit(2);
+    }
+    afInitSampleFormat(filesetup, AF_DEFAULT_TRACK, AF_SAMPFMT_TWOSCOMP, 16);
+    afInitRate(filesetup, AF_DEFAULT_TRACK, FAST_SAMPLE_RATE);
+    afInitFileFormat(filesetup, AF_FILE_WAVE);
+    afInitChannels(filesetup, AF_DEFAULT_TRACK, 1);
+    if ((filehandle = afOpenFile("sound_c1.wav", "w", filesetup)) == AF_NULL_FILEHANDLE)
+    {
+        fprintf(stderr, "    Failed to open result file\n");
+        exit(2);
+    }
+
+    printf("Generate C1\n");
+    /* The set of C1 voice samples is ready for use in the output file. */
+    voiced_length = sizeof(css_c1)/sizeof(css_c1[0]);
+    for (i = 0;  i < voiced_length;  i++)
+        voiced_sound[i] = css_c1[i];
+    pk = peak(voiced_sound, voiced_length);
+    ms = rms(voiced_sound, voiced_length);
+    printf("Voiced level = %.2fdB, crest factor = %.2fdB\n", rms_to_dbm0(ms), rms_to_db(pk/ms));
+
+#if defined(HAVE_FFTW3_H)
+    p = fftw_plan_dft_1d(8192, in, out, FFTW_BACKWARD, FFTW_ESTIMATE);
+#else
+    p = fftw_create_plan(8192, FFTW_BACKWARD, FFTW_ESTIMATE);
+#endif
+    for (i = 0;  i < 8192;  i++)
+    {
+#if defined(HAVE_FFTW3_H)
+        in[i][0] = 0.0;
+        in[i][1] = 0.0;
+#else
+        in[i].re = 0.0;
+        in[i].im = 0.0;
+#endif
+    }
+    for (i = 1;  i <= 3715;  i++)
+    {
+        f = FAST_SAMPLE_RATE*i/8192.0;
+
+#if 1
+        if (f < 50.0)
+            scale = -60.0;
+        else if (f < 100.0)
+            scale = scaling(f, 50.0, 100.0, -25.8, -12.8);
+        else if (f < 200.0)
+            scale = scaling(f, 100.0, 200.0, -12.8, 17.4);
+        else if (f < 215.0)
+            scale = scaling(f, 200.0, 215.0, 17.4, 17.8);
+        else if (f < 500.0)
+            scale = scaling(f, 215.0, 500.0, 17.8, 12.2);
+        else if (f < 1000.0)
+            scale = scaling(f, 500.0, 1000.0, 12.2, 7.2);
+        else if (f < 2850.0)
+            scale = scaling(f, 1000.0, 2850.0, 7.2, 0.0);
+        else if (f < 3600.0)
+            scale = scaling(f, 2850.0, 3600.0, 0.0, -2.0);
+        else if (f < 3660.0)
+            scale = scaling(f, 3600.0, 3660.0, -2.0, -20.0);
+        else if (f < 3680.0)
+            scale = scaling(f, 3600.0, 3680.0, -20.0, -30.0);
+        else
+            scale = -60.0;
+#else
+        scale = 0.0;
+#endif
+#if defined(HAVE_FFTW3_H)
+        in[i][0] = ((rand() >> 10) & 0x1)  ?  1.0  :  -1.0;
+        in[i][0] *= pow(10.0, scale/20.0)*35.0; //305360
+        in[8192 - i][0] = -in[i][0];
+#else
+        in[i].re = ((rand() >> 10) & 0x1)  ?  1.0  :  -1.0;
+        in[i].re *= pow(10.0, scale/20.0)*35.0; //305360
+        in[8192 - i].re = -in[i].re;
+#endif
+    }
+#if defined(HAVE_FFTW3_H)
+    fftw_execute(p);
+#else
+    fftw_one(p, in, out);
+#endif
+    for (i = 0;  i < 8192;  i++)
+    {
+#if defined(HAVE_FFTW3_H)
+        noise_sound[i] = out[i][1];
+#else
+        noise_sound[i] = out[i].im;
+#endif
+    }
+    pk = peak(noise_sound, 8192);
+    ms = rms(noise_sound, 8192);
+    printf("Noise level = %.2fdB, crest factor = %.2fdB\n", rms_to_dbm0(ms), rms_to_db(pk/ms));
+    
+    for (i = 0;  i < 8192;  i++)
+        silence_sound[i] = 0.0;
+
+    for (i = 0;  i < 16;  i++)
+    {
+        outframes = afWriteFrames(filehandle,
+                                  AF_DEFAULT_TRACK,
+                                  voiced_sound,
+                                  voiced_length);
+    }
+    printf("%d samples of voice\n", 16*voiced_length);
+    outframes = afWriteFrames(filehandle,
+                              AF_DEFAULT_TRACK,
+                              noise_sound,
+                              8192);
+    outframes = afWriteFrames(filehandle,
+                              AF_DEFAULT_TRACK,
+                              noise_sound,
+                              C1_NOISE_SAMPLES - 8192);
+    printf("%d samples of noise\n", C1_NOISE_SAMPLES);
+    outframes = afWriteFrames(filehandle,
+                              AF_DEFAULT_TRACK,
+                              silence_sound,
+                              C1_SILENCE_SAMPLES);
+    printf("%d samples of silence\n", C1_SILENCE_SAMPLES);
+
+    /* Now phase invert the C1 set of voice samples. */
+    voiced_length = sizeof(css_c1)/sizeof(css_c1[0]);
+    for (i = 0;  i < voiced_length;  i++)
+        voiced_sound[i] = -css_c1[i];
+    pk = peak(voiced_sound, voiced_length);
+    ms = rms(voiced_sound, voiced_length);
+    printf("Voiced level = %.2fdB, crest factor = %.2fdB\n", rms_to_dbm0(ms), rms_to_db(pk/ms));
+
+    for (i = 0;  i < 8192;  i++)
+        noise_sound[i] = -noise_sound[i];
+
+    for (i = 0;  i < 16;  i++)
+    {
+        outframes = afWriteFrames(filehandle,
+                                  AF_DEFAULT_TRACK,
+                                  voiced_sound,
+                                  voiced_length);
+    }
+    printf("%d samples of voice\n", 16*voiced_length);
+    outframes = afWriteFrames(filehandle,
+                              AF_DEFAULT_TRACK,
+                              noise_sound,
+                              8192);
+    outframes = afWriteFrames(filehandle,
+                              AF_DEFAULT_TRACK,
+                              noise_sound,
+                              C1_NOISE_SAMPLES - 8192);
+    printf("%d samples of noise\n", C1_NOISE_SAMPLES);
+    outframes = afWriteFrames(filehandle,
+                              AF_DEFAULT_TRACK,
+                              silence_sound,
+                              C1_SILENCE_SAMPLES);
+    printf("%d samples of silence\n", C1_SILENCE_SAMPLES);
+
+    if (afCloseFile(filehandle) != 0)
+    {
+        fprintf(stderr, "    Cannot close speech file '%s'\n", "sound_c1.wav");
+        exit(2);
+    }
+
+    afInitSampleFormat(filesetup, AF_DEFAULT_TRACK, AF_SAMPFMT_TWOSCOMP, 16);
+    afInitRate(filesetup, AF_DEFAULT_TRACK, FAST_SAMPLE_RATE);
+    afInitFileFormat(filesetup, AF_FILE_WAVE);
+    afInitChannels(filesetup, AF_DEFAULT_TRACK, 1);
+    filehandle = afOpenFile("sound_c3.wav", "w", filesetup);
+    if (filehandle == AF_NULL_FILEHANDLE)
+    {
+        fprintf(stderr, "    Failed to open result file\n");
+        exit(2);
+    }
+
+    printf("Generate C3\n");
+    /* Take the supplied set of C3 voice samples. */
+    voiced_length = (sizeof(css_c3)/sizeof(css_c3[0]));
+    for (i = 0;  i < voiced_length;  i++)
+        voiced_sound[i] = css_c3[i];
+    pk = peak(voiced_sound, voiced_length);
+    ms = rms(voiced_sound, voiced_length);
+    printf("Voiced level = %.2fdB, crest factor = %.2fdB\n", rms_to_dbm0(ms), rms_to_db(pk/ms));
+
+    awgn_init_dbm0(&noise_source, 7162534, rms_to_dbm0(ms));
+    for (i = 0;  i < 8192;  i++)
+        noise_sound[i] = awgn(&noise_source);
+    pk = peak(noise_sound, 8192);
+    ms = rms(noise_sound, 8192);
+    printf("Noise level = %.2fdB, crest factor = %.2fdB\n", rms_to_dbm0(ms), rms_to_db(pk/ms));
+
+    for (i = 0;  i < 14;  i++)
+    {
+        outframes = afWriteFrames(filehandle,
+                                  AF_DEFAULT_TRACK,
+                                  voiced_sound,
+                                  voiced_length);
+    }
+    printf("%d samples of voice\n", 14*voiced_length);
+
+    outframes = afWriteFrames(filehandle,
+                              AF_DEFAULT_TRACK,
+                              noise_sound,
+                              8192);
+    outframes = afWriteFrames(filehandle,
+                              AF_DEFAULT_TRACK,
+                              noise_sound,
+                              C3_NOISE_SAMPLES - 8192);
+    printf("%d samples of noise\n", C3_NOISE_SAMPLES);
+    outframes = afWriteFrames(filehandle,
+                              AF_DEFAULT_TRACK,
+                              silence_sound,
+                              C3_SILENCE_SAMPLES);
+    printf("%d samples of silence\n", C3_SILENCE_SAMPLES);
+
+    /* Now phase invert the set of voice samples. */
+    voiced_length = (sizeof(css_c3)/sizeof(css_c3[0]));
+    for (i = 0;  i < voiced_length;  i++)
+        voiced_sound[i] = -css_c3[i];
+    pk = peak(voiced_sound, voiced_length);
+    ms = rms(voiced_sound, voiced_length);
+    printf("Voiced level = %.2fdB, crest factor = %.2fdB\n", rms_to_dbm0(ms), rms_to_db(pk/ms));
+
+    /* Now phase invert the set of noise samples. */
+    for (i = 0;  i < 8192;  i++)
+        noise_sound[i] = -noise_sound[i];
+
+    for (i = 0;  i < 14;  i++)
+    {
+        outframes = afWriteFrames(filehandle,
+                                  AF_DEFAULT_TRACK,
+                                  voiced_sound,
+                                  voiced_length);
+    }
+    printf("%d samples of voice\n", 14*i);
+    outframes = afWriteFrames(filehandle,
+                              AF_DEFAULT_TRACK,
+                              noise_sound,
+                              8192);
+    outframes = afWriteFrames(filehandle,
+                              AF_DEFAULT_TRACK,
+                              noise_sound,
+                              C3_NOISE_SAMPLES - 8192);
+    printf("%d samples of noise\n", C3_NOISE_SAMPLES);
+    outframes = afWriteFrames(filehandle,
+                              AF_DEFAULT_TRACK,
+                              silence_sound,
+                              C3_SILENCE_SAMPLES);
+    printf("%d samples of silence\n", C3_SILENCE_SAMPLES);
+
+    if (afCloseFile(filehandle) != 0)
+    {
+        fprintf(stderr, "    Cannot close speech file '%s'\n", "sound_c3.wav");
+        exit(2);
+    }
+    afFreeFileSetup(filesetup);
+
+    fftw_destroy_plan(p);
+    return  0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/tests/media_monitor.cpp
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/tests/media_monitor.cpp	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,316 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * media_monitor.cpp - Display IP streaming media status, using the FLTK toolkit.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2007 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: media_monitor.cpp,v 1.4 2008/05/27 15:08:21 steveu Exp $
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#if defined(HAVE_FL_FL_H)  &&  defined(HAVE_FL_FL_CARTESIAN_H)
+
+#define __STDC_LIMIT_MACROS
+
+#include <inttypes.h>
+#include <stdio.h>
+#include <math.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/select.h>
+
+#include <FL/Fl.H>
+#include <FL/Fl_Overlay_Window.H>
+#include <FL/Fl_Light_Button.H>
+#include <FL/Fl_Cartesian.H>
+#include <FL/fl_draw.H>
+
+#include "spandsp.h"
+#include "media_monitor.h"
+
+Fl_Double_Window *w;
+
+Fl_Group *c_right;
+Fl_Group *c_sent;
+Fl_Group *c_received;
+
+Ca_Canvas *canvas_sent;
+Ca_X_Axis *sent_x;
+Ca_Y_Axis *sent_y;
+Ca_Line *sent_re = NULL;
+double sent_re_plot[1000];
+double sent_re_plot_min;
+double sent_re_plot_max;
+
+Ca_Canvas *canvas_received;
+Ca_X_Axis *received_x;
+Ca_Y_Axis *received_y;
+Ca_Line *received_delays = NULL;
+double received_delays_plot[4000];
+double received_delays_plot_max;
+int min_diff;
+int max_diff;
+
+int highest_seq_no_seen = -1;
+
+static int skip = 0;
+
+int in_ptr;
+
+void media_monitor_rx(int seq_no, double departure_time, double arrival_time)
+{
+    double fdiff;
+    int diff;
+    int i;
+
+    if (received_delays)
+        delete received_delays;
+
+    canvas_received->current(canvas_received);
+    fdiff = (arrival_time - departure_time)*1000.0;
+    diff = (int) fdiff;
+    if (diff < 0)
+        diff = 0;
+    else if (diff > 1999)
+        diff = 1999;
+    received_delays_plot[2*diff + 1]++;
+    if (received_delays_plot[2*diff + 1] > received_delays_plot_max)
+    {
+        received_delays_plot_max = received_delays_plot[2*diff + 1];
+        received_y->maximum(received_delays_plot_max);
+    }
+    if (diff > max_diff)
+    {
+        max_diff = diff;
+        received_x->maximum((double) max_diff);
+    }
+    if (diff < min_diff)
+    {
+        min_diff = diff - 1;
+        received_x->minimum((double) min_diff);
+    }
+
+    received_delays = new Ca_Line(2000, received_delays_plot, 0, 0, FL_BLUE, CA_NO_POINT);
+    
+    if (sent_re)
+        delete sent_re;
+
+    canvas_sent->current(canvas_sent);
+
+    if (seq_no > highest_seq_no_seen + 1)
+    {
+        for (i = highest_seq_no_seen + 1;  i < seq_no;  i++)
+            sent_re_plot[2*(i%500) + 1] = 0.0;
+    }
+    sent_re_plot[2*(seq_no%500) + 1] = fdiff;
+    
+    if (fdiff > sent_re_plot_max)
+    {
+        sent_re_plot_max = fdiff;
+        sent_y->maximum(sent_re_plot_max);
+    }
+    if (fdiff < sent_re_plot_min)
+    {
+        sent_re_plot_min = fdiff - 1.0;
+        sent_y->minimum(sent_re_plot_min);
+    }
+    sent_re = new Ca_Line(500, sent_re_plot, 0, 0, FL_BLUE, CA_NO_POINT);
+
+    if (seq_no > highest_seq_no_seen)
+        highest_seq_no_seen = seq_no;
+
+    if (++skip >= 100)
+    {
+        skip = 0;
+        Fl::check();
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+int start_media_monitor(void)
+{
+    char buf[132 + 1];
+    float x;
+    float y;
+    int i;
+    int len;
+    
+    len = 128;
+
+    w = new Fl_Double_Window(465, 400, "IP streaming media monitor");
+
+    c_right = new Fl_Group(0, 0, 465, 405);
+
+    c_sent = new Fl_Group(0, 0, 465, 200);
+    c_sent->box(FL_DOWN_BOX);
+    c_sent->align(FL_ALIGN_TOP | FL_ALIGN_INSIDE);
+    c_sent->current();
+
+    canvas_sent = new Ca_Canvas(110, 35, 300, 100, "Packet delays");
+    canvas_sent->box(FL_PLASTIC_DOWN_BOX);
+    canvas_sent->color(7);
+    canvas_sent->align(FL_ALIGN_TOP);
+    Fl_Group::current()->resizable(canvas_sent);
+    canvas_sent->border(15);
+
+    sent_x = new Ca_X_Axis(115, 135, 290, 30, "Packet");
+    sent_x->align(FL_ALIGN_BOTTOM);
+    sent_x->minimum(0.0);
+    sent_x->maximum(500.0);
+    sent_x->label_format("%g");
+    sent_x->minor_grid_color(fl_gray_ramp(20));
+    sent_x->major_grid_color(fl_gray_ramp(15));
+    sent_x->label_grid_color(fl_gray_ramp(10));
+    sent_x->grid_visible(CA_LABEL_GRID | CA_ALWAYS_VISIBLE);
+    sent_x->minor_grid_style(FL_DOT);
+    sent_x->major_step(5);
+    sent_x->label_step(1);
+    sent_x->axis_align(CA_BOTTOM | CA_LINE);
+    sent_x->axis_color(FL_BLACK);
+    sent_x->current();
+
+    sent_y = new Ca_Y_Axis(60, 40, 50, 90, "Delay\n(ms)");
+    sent_y->align(FL_ALIGN_LEFT);
+    sent_y->minimum(0.0);
+    sent_y->maximum(2000.0);
+    sent_y->minor_grid_color(fl_gray_ramp(20));
+    sent_y->major_grid_color(fl_gray_ramp(15));
+    sent_y->label_grid_color(fl_gray_ramp(10));
+    sent_y->grid_visible(CA_LABEL_GRID | CA_ALWAYS_VISIBLE);
+    sent_y->minor_grid_style(FL_DOT);
+    sent_y->major_step(5);
+    sent_y->label_step(1);
+    sent_y->axis_color(FL_BLACK);
+    sent_y->current();
+
+    c_sent->end();
+
+    c_received = new Fl_Group(0, 200, 465, 200);
+    c_received->box(FL_DOWN_BOX);
+    c_received->align(FL_ALIGN_TOP | FL_ALIGN_INSIDE);
+    c_received->current();
+
+    canvas_received = new Ca_Canvas(110, 235, 300, 100, "Delay spread");
+    canvas_received->box(FL_PLASTIC_DOWN_BOX);
+    canvas_received->color(7);
+    canvas_received->align(FL_ALIGN_TOP);
+    Fl_Group::current()->resizable(canvas_received);
+    canvas_received->border(15);
+
+    received_x = new Ca_X_Axis(115, 335, 290, 30, "Delay (ms)");
+    received_x->align(FL_ALIGN_BOTTOM);
+    received_x->minimum(0.0);
+    received_x->maximum(2000.0);
+    received_x->label_format("%g");
+    received_x->minor_grid_color(fl_gray_ramp(20));
+    received_x->major_grid_color(fl_gray_ramp(15));
+    received_x->label_grid_color(fl_gray_ramp(10));
+    received_x->grid_visible(CA_LABEL_GRID | CA_ALWAYS_VISIBLE);
+    received_x->minor_grid_style(FL_DOT);
+    received_x->major_step(5);
+    received_x->label_step(1);
+    received_x->axis_align(CA_BOTTOM | CA_LINE);
+    received_x->axis_color(FL_BLACK);
+    received_x->current();
+
+    received_y = new Ca_Y_Axis(60, 240, 50, 90, "Freq");
+    received_y->align(FL_ALIGN_LEFT);
+    received_y->minimum(0.0);
+    received_y->maximum(50.0);
+    received_y->minor_grid_color(fl_gray_ramp(20));
+    received_y->major_grid_color(fl_gray_ramp(15));
+    received_y->label_grid_color(fl_gray_ramp(10));
+    received_y->grid_visible(CA_LABEL_GRID | CA_ALWAYS_VISIBLE);
+    received_y->minor_grid_style(FL_DOT);
+    received_y->major_step(5);
+    received_y->label_step(1);
+    received_y->axis_color(FL_BLACK);
+    received_y->current();
+
+    for (i = 0;  i < 2000;  i++)
+        received_delays_plot[2*i] = i;
+    received_delays_plot_max = 0.0;
+    min_diff = 2000;
+    max_diff = 0;
+    
+    for (i = 0;  i < 500;  i++)
+        sent_re_plot[2*i] = i;
+    sent_re_plot_min = 99999.0;
+    sent_re_plot_max = 0.0;
+
+    c_received->end();
+
+    c_right->end();
+
+    Fl_Group::current()->resizable(c_right);
+    w->end();
+    w->show();
+
+    in_ptr = 0;
+
+    Fl::check();
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+void media_monitor_wait_to_end(void) 
+{
+    fd_set rfds;
+    int res;
+    struct timeval tv;
+
+    fprintf(stderr, "Processing complete.  Press the <enter> key to end\n");
+    do
+    {
+        usleep(100000);
+        Fl::check();
+        FD_ZERO(&rfds);
+        FD_SET(0, &rfds);
+        tv.tv_usec = 100000;
+        tv.tv_sec = 0;
+        res = select(1, &rfds, NULL, NULL, &tv);
+    }
+    while (res <= 0);
+}
+/*- End of function --------------------------------------------------------*/
+
+void media_monitor_update_display(void) 
+{
+    Fl::check();
+    Fl::check();
+    Fl::check();
+    Fl::check();
+    Fl::check();
+    Fl::check();
+    Fl::check();
+    Fl::check();
+    Fl::check();
+    Fl::check();
+    Fl::check();
+    Fl::check();
+}
+/*- End of function --------------------------------------------------------*/
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/tests/media_monitor.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/tests/media_monitor.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,56 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * media_monitor.h - Display IP streaming media status, using the FLTK toolkit.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2007 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: media_monitor.h,v 1.6 2008/04/26 13:39:17 steveu Exp $
+ */
+
+/*! \page media_monitor_page IP streaming media performance monitoring
+\section media_monitor_page_sec_1 What does it do?
+This code controls a GUI window, which provides monitoring of the status
+of an IP media stream. It shows, graphically:
+
+\section media_monitor_page_sec_2 How does it work?
+This code uses the FLTK cross platform GUI toolkit. It works on X11 and Windows platforms.
+In addition to the basic FLTK toolkit, fltk_cartesian is also required.
+*/
+
+#if !defined(_MEDIA_MONITOR_H_)
+#define _MEDIA_MONITOR_H_
+
+#if defined(__cplusplus)
+extern "C"
+{
+#endif
+
+int start_media_monitor(void);
+void media_monitor_rx(int seq_no, double departure_time, double arrival_time);
+void media_monitor_wait_to_end(void);
+void media_monitor_update_display(void);
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/tests/modem_connect_tones_tests.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/tests/modem_connect_tones_tests.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,1018 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * modem_connect_tones_tests.c
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2006 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: modem_connect_tones_tests.c,v 1.27 2008/08/29 09:28:13 steveu Exp $
+ */
+
+/*! \page modem_connect_tones_tests_page Modem connect tones tests
+\section modem_connect_tones_rx_tests_page_sec_1 What does it do?
+These tests...
+*/
+
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <string.h>
+#include <audiofile.h>
+
+#include "spandsp.h"
+#include "spandsp-sim.h"
+
+#define SAMPLES_PER_CHUNK           160
+
+#define OUTPUT_FILE_NAME            "modem_connect_tones.wav"
+
+#define MITEL_DIR                   "../test-data/mitel/"
+#define BELLCORE_DIR                "../test-data/bellcore/"
+
+#define FALSE 0
+#define TRUE (!FALSE)
+
+#define LEVEL_MAX                   0
+#define LEVEL_MIN                   -48
+#define LEVEL_MIN_ACCEPT            -43
+#define LEVEL_MIN_REJECT            -44
+
+const char *bellcore_files[] =
+{
+    MITEL_DIR    "mitel-cm7291-talkoff.wav",
+    BELLCORE_DIR "tr-tsy-00763-1.wav",
+    BELLCORE_DIR "tr-tsy-00763-2.wav",
+    BELLCORE_DIR "tr-tsy-00763-3.wav",
+    BELLCORE_DIR "tr-tsy-00763-4.wav",
+    BELLCORE_DIR "tr-tsy-00763-5.wav",
+    BELLCORE_DIR "tr-tsy-00763-6.wav",
+    ""
+};
+
+enum
+{
+    PERFORM_TEST_1A = (1 << 1),
+    PERFORM_TEST_1B = (1 << 2),
+    PERFORM_TEST_1C = (1 << 3),
+    PERFORM_TEST_1D = (1 << 4),
+    PERFORM_TEST_1E = (1 << 5),
+    PERFORM_TEST_2A = (1 << 6),
+    PERFORM_TEST_2B = (1 << 7),
+    PERFORM_TEST_2C = (1 << 8),
+    PERFORM_TEST_3A = (1 << 9),
+    PERFORM_TEST_3B = (1 << 10),
+    PERFORM_TEST_3C = (1 << 11),
+    PERFORM_TEST_4 = (1 << 12),
+    PERFORM_TEST_5A = (1 << 13),
+    PERFORM_TEST_5B = (1 << 14),
+    PERFORM_TEST_6 = (1 << 15)
+};
+
+int preamble_count = 0;
+int preamble_on_at = -1;
+int preamble_off_at = -1;
+int hits = 0;
+int when = 0;
+
+static int preamble_get_bit(void *user_data)
+{
+    static int bit_no = 0;
+    int bit;
+    
+    /* Generate a section of HDLC flag octet preamble. Then generate some random
+       bits, which should not look like preamble. */
+    if (++preamble_count < 255)
+    {
+        bit = (bit_no < 2)  ?  0  :  1;
+        if (++bit_no >= 8)
+            bit_no = 0;
+#if 0
+        /* Inject some bad bits */
+        if (rand()%15 == 0)
+            return bit ^ 1;
+#endif
+    }
+    else
+    {
+        bit = rand() & 1;
+    }
+    return bit;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void preamble_detected(void *user_data, int on, int level, int delay)
+{
+    printf("%s (%d) declared at bit %d (%ddBm0)\n", modem_connect_tone_to_str(on), on, preamble_count, level);
+    if (on)
+        preamble_on_at = preamble_count;
+    else
+        preamble_off_at = preamble_count;
+    /*endif*/
+}
+/*- End of function --------------------------------------------------------*/
+
+static void ced_detected(void *user_data, int on, int level, int delay)
+{
+    printf("%s (%d) declared at %fs, delay %d (%ddBm0)\n", modem_connect_tone_to_str(on), on, (float) when/SAMPLE_RATE, delay, level);
+    if (on)
+        hits++;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void ans_pr_detected(void *user_data, int on, int level, int delay)
+{
+    printf("%s (%d) declared at %fs, delay %d (%ddBm0)\n", modem_connect_tone_to_str(on), on, (float) when/SAMPLE_RATE, delay, level);
+    if (on == MODEM_CONNECT_TONES_ANS_PR)
+        hits++;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void cng_detected(void *user_data, int on, int level, int delay)
+{
+    printf("%s (%d) declared at %fs, delay %d (%ddBm0)\n", modem_connect_tone_to_str(on), on, (float) when/SAMPLE_RATE, delay, level);
+    if (on)
+        hits++;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void ec_dis_detected(void *user_data, int on, int level, int delay)
+{
+    printf("%s (%d) declared at %fs, delay %d (%ddBm0)\n", modem_connect_tone_to_str(on), on, (float) when/SAMPLE_RATE, delay, level);
+    if (on)
+        hits++;
+}
+/*- End of function --------------------------------------------------------*/
+
+int main(int argc, char *argv[])
+{
+    int i;
+    int j;
+    int pitch;
+    int level;
+    int interval;
+    int cycle;
+    int16_t amp[8000];
+    modem_connect_tones_rx_state_t cng_rx;
+    modem_connect_tones_rx_state_t ced_rx;
+    modem_connect_tones_rx_state_t ans_pr_rx;
+    modem_connect_tones_tx_state_t modem_tone_tx;
+    awgn_state_t chan_noise_source;
+    AFfilehandle inhandle;
+    AFfilehandle outhandle;
+    int outframes;
+    int frames;
+    int samples;
+    int hit;
+    int false_hit;
+    int false_miss;
+    tone_gen_descriptor_t tone_desc;
+    tone_gen_state_t tone_tx;
+    power_meter_t power_state;
+    int power;
+    int max_power;
+    int level2;
+    int max_level2;
+    int test_list;
+    int opt;
+    char *decode_test_file;
+    fsk_tx_state_t preamble_tx;
+
+    test_list = 0;
+    decode_test_file = NULL;
+    while ((opt = getopt(argc, argv, "d:")) != -1)
+    {
+        switch (opt)
+        {
+        case 'd':
+            decode_test_file = optarg;
+            break;
+        default:
+            //usage();
+            exit(2);
+            break;
+        }
+    }
+    argc -= optind;
+    argv += optind;
+    for (i = 0;  i < argc;  i++)
+    {
+        if (strcasecmp(argv[i], "1a") == 0)
+            test_list |= PERFORM_TEST_1A;
+        else if (strcasecmp(argv[i], "1b") == 0)
+            test_list |= PERFORM_TEST_1B;
+        else if (strcasecmp(argv[i], "1c") == 0)
+            test_list |= PERFORM_TEST_1C;
+        else if (strcasecmp(argv[i], "1d") == 0)
+            test_list |= PERFORM_TEST_1D;
+        else if (strcasecmp(argv[i], "1e") == 0)
+            test_list |= PERFORM_TEST_1E;
+        else if (strcasecmp(argv[i], "2a") == 0)
+            test_list |= PERFORM_TEST_2A;
+        else if (strcasecmp(argv[i], "2b") == 0)
+            test_list |= PERFORM_TEST_2B;
+        else if (strcasecmp(argv[i], "2c") == 0)
+            test_list |= PERFORM_TEST_2C;
+        else if (strcasecmp(argv[i], "3a") == 0)
+            test_list |= PERFORM_TEST_3A;
+        else if (strcasecmp(argv[i], "3b") == 0)
+            test_list |= PERFORM_TEST_3B;
+        else if (strcasecmp(argv[i], "3c") == 0)
+            test_list |= PERFORM_TEST_3C;
+        else if (strcasecmp(argv[i], "4") == 0)
+            test_list |= PERFORM_TEST_4;
+        else if (strcasecmp(argv[i], "5a") == 0)
+            test_list |= PERFORM_TEST_5A;
+        else if (strcasecmp(argv[i], "5b") == 0)
+            test_list |= PERFORM_TEST_5B;
+        else if (strcasecmp(argv[i], "6") == 0)
+            test_list |= PERFORM_TEST_6;
+        else
+        {
+            fprintf(stderr, "Unknown test '%s' specified\n", argv[i]);
+            exit(2);
+        }
+    }
+    if (decode_test_file == NULL  &&  test_list == 0)
+        test_list = 0xFFFFFFFF;
+
+    if ((outhandle = afOpenFile_telephony_write(OUTPUT_FILE_NAME, 1)) == AF_NULL_FILEHANDLE)
+    {
+        fprintf(stderr, "    Cannot create wave file '%s'\n", OUTPUT_FILE_NAME);
+        exit(2);
+    }
+
+    if ((test_list & PERFORM_TEST_1A))
+    {
+        printf("Test 1a: CNG generation to a file\n");
+        modem_connect_tones_tx_init(&modem_tone_tx, MODEM_CONNECT_TONES_FAX_CNG);
+        for (i = 0;  i < 20*SAMPLE_RATE;  i += SAMPLES_PER_CHUNK)
+        {
+            samples = modem_connect_tones_tx(&modem_tone_tx, amp, SAMPLES_PER_CHUNK);
+            outframes = afWriteFrames(outhandle,
+                                      AF_DEFAULT_TRACK,
+                                      amp,
+                                      samples);
+            if (outframes != samples)
+            {
+                fprintf(stderr, "    Error writing wave file\n");
+                exit(2);
+            }
+            /*endif*/
+        }
+        /*endfor*/
+    }
+    /*endif*/
+    
+    if ((test_list & PERFORM_TEST_1B))
+    {
+        printf("Test 1b: CED/ANS generation to a file\n");
+        modem_connect_tones_tx_init(&modem_tone_tx, MODEM_CONNECT_TONES_FAX_CED);
+        for (i = 0;  i < 20*SAMPLE_RATE;  i += SAMPLES_PER_CHUNK)
+        {
+            samples = modem_connect_tones_tx(&modem_tone_tx, amp, SAMPLES_PER_CHUNK);
+            outframes = afWriteFrames(outhandle,
+                                      AF_DEFAULT_TRACK,
+                                      amp,
+                                      samples);
+            if (outframes != samples)
+            {
+                fprintf(stderr, "    Error writing wave file\n");
+                exit(2);
+            }
+            /*endif*/
+        }
+        /*endfor*/
+    }
+    /*endif*/
+
+    if ((test_list & PERFORM_TEST_1C))
+    {
+        printf("Test 1c: ANSAM (Modulated ANS) generation to a file\n");
+        /* Some with modulation */
+        modem_connect_tones_tx_init(&modem_tone_tx, MODEM_CONNECT_TONES_ANSAM);
+        for (i = 0;  i < 20*SAMPLE_RATE;  i += SAMPLES_PER_CHUNK)
+        {
+            samples = modem_connect_tones_tx(&modem_tone_tx, amp, SAMPLES_PER_CHUNK);
+            outframes = afWriteFrames(outhandle,
+                                      AF_DEFAULT_TRACK,
+                                      amp,
+                                      samples);
+            if (outframes != samples)
+            {
+                fprintf(stderr, "    Error writing wave file\n");
+                exit(2);
+            }
+            /*endif*/
+        }
+        /*endfor*/
+    }
+    /*endif*/
+
+    if ((test_list & PERFORM_TEST_1D))
+    {
+        printf("Test 1d: ANS PR (EC-disable) generation to a file\n");
+        /* Some without modulation */
+        modem_connect_tones_tx_init(&modem_tone_tx, MODEM_CONNECT_TONES_ANS_PR);
+        for (i = 0;  i < 20*SAMPLE_RATE;  i += SAMPLES_PER_CHUNK)
+        {
+            samples = modem_connect_tones_tx(&modem_tone_tx, amp, SAMPLES_PER_CHUNK);
+            outframes = afWriteFrames(outhandle,
+                                      AF_DEFAULT_TRACK,
+                                      amp,
+                                      samples);
+            if (outframes != samples)
+            {
+                fprintf(stderr, "    Error writing wave file\n");
+                exit(2);
+            }
+            /*endif*/
+        }
+        /*endfor*/
+    }
+    /*endif*/
+    
+    if ((test_list & PERFORM_TEST_1E))
+    {
+        printf("Test 1e: ANSAM PR (Modulated EC-disable) generation to a file\n");
+        /* Some with modulation */
+        modem_connect_tones_tx_init(&modem_tone_tx, MODEM_CONNECT_TONES_ANSAM_PR);
+        for (i = 0;  i < 20*SAMPLE_RATE;  i += SAMPLES_PER_CHUNK)
+        {
+            samples = modem_connect_tones_tx(&modem_tone_tx, amp, SAMPLES_PER_CHUNK);
+            outframes = afWriteFrames(outhandle,
+                                      AF_DEFAULT_TRACK,
+                                      amp,
+                                      samples);
+            if (outframes != samples)
+            {
+                fprintf(stderr, "    Error writing wave file\n");
+                exit(2);
+            }
+            /*endif*/
+        }
+        /*endfor*/
+    }
+    /*endif*/
+
+    if (afCloseFile(outhandle) != 0)
+    {
+        printf("    Cannot close wave file '%s'\n", OUTPUT_FILE_NAME);
+        exit(2);
+    }
+    /*endif*/
+    
+    if ((test_list & PERFORM_TEST_2A))
+    {
+        printf("Test 2a: CNG detection with frequency\n");
+        awgn_init_dbm0(&chan_noise_source, 7162534, -50.0f);
+        false_hit = FALSE;
+        false_miss = FALSE;
+        for (pitch = 600;  pitch < 1600;  pitch++)
+        {
+            make_tone_gen_descriptor(&tone_desc,
+                                     pitch,
+                                     -11,
+                                     0,
+                                     0,
+                                     425,
+                                     3000,
+                                     0,
+                                     0,
+                                     TRUE);
+            tone_gen_init(&tone_tx, &tone_desc);
+
+            modem_connect_tones_rx_init(&cng_rx, MODEM_CONNECT_TONES_FAX_CNG, NULL, NULL);
+            power_meter_init(&power_state, 5);
+            power = 0;
+            max_power = 0;
+            level2 = 0;
+            max_level2 = 0;
+            for (i = 0;  i < 10*SAMPLE_RATE;  i += SAMPLES_PER_CHUNK)
+            {
+                samples = tone_gen(&tone_tx, amp, SAMPLES_PER_CHUNK);
+                for (j = 0;  j < samples;  j++)
+                {
+                    amp[j] += awgn(&chan_noise_source);
+                    power = power_meter_update(&power_state, amp[j]);
+                    if (power > max_power)
+                        max_power = power;
+                    /*endif*/
+                    level2 += ((abs(amp[j]) - level2) >> 5);
+                    if (level2 > max_level2)
+                        max_level2 = level2;
+                }
+                /*endfor*/
+                modem_connect_tones_rx(&cng_rx, amp, samples);
+            }
+            /*endfor*/
+            hit = modem_connect_tones_rx_get(&cng_rx);
+            if (pitch < (1100 - 70)  ||  pitch > (1100 + 70))
+            {
+                if (hit == MODEM_CONNECT_TONES_FAX_CNG)
+                    false_hit = TRUE;
+                /*endif*/
+            }
+            else if (pitch > (1100 - 50)  &&  pitch < (1100 + 50))
+            {
+                if (hit != MODEM_CONNECT_TONES_FAX_CNG)
+                    false_miss = TRUE;
+                /*endif*/
+            }
+            /*endif*/
+            if (hit)
+                printf("Detected at %5dHz %12d %12d %d\n", pitch, cng_rx.channel_level, cng_rx.notch_level, hit);
+            /*endif*/
+        }
+        /*endfor*/
+        if (false_hit  ||  false_miss)
+        {
+            printf("Test failed.\n");
+            exit(2);
+        }
+        /*endif*/
+        printf("Test passed.\n");
+    }
+    /*endif*/
+    
+    if ((test_list & PERFORM_TEST_2B))
+    {
+        printf("Test 2b: CED/ANS detection with frequency\n");
+        awgn_init_dbm0(&chan_noise_source, 7162534, -50.0f);
+        false_hit = FALSE;
+        false_miss = FALSE;
+        for (pitch = 1600;  pitch < 2600;  pitch++)
+        {
+            make_tone_gen_descriptor(&tone_desc,
+                                     pitch,
+                                     -11,
+                                     0,
+                                     0,
+                                     2600,
+                                     0,
+                                     0,
+                                     0,
+                                     FALSE);
+            tone_gen_init(&tone_tx, &tone_desc);
+
+            modem_connect_tones_rx_init(&ced_rx, MODEM_CONNECT_TONES_FAX_CED_OR_PREAMBLE, NULL, NULL);
+            for (i = 0;  i < 10*SAMPLE_RATE;  i += SAMPLES_PER_CHUNK)
+            {
+                samples = tone_gen(&tone_tx, amp, SAMPLES_PER_CHUNK);
+                for (j = 0;  j < samples;  j++)
+                    amp[j] += awgn(&chan_noise_source);
+                /*endfor*/
+                modem_connect_tones_rx(&ced_rx, amp, samples);
+            }
+            /*endfor*/
+            hit = modem_connect_tones_rx_get(&ced_rx);
+            if (pitch < (2100 - 70)  ||  pitch > (2100 + 70))
+            {
+                if (hit == MODEM_CONNECT_TONES_FAX_CED)
+                    false_hit = TRUE;
+            }
+            else if (pitch > (2100 - 50)  &&  pitch < (2100 + 50))
+            {
+                if (hit != MODEM_CONNECT_TONES_FAX_CED)
+                    false_miss = TRUE;
+            }
+            /*endif*/
+            if (hit)
+                printf("Detected at %5dHz %12d %12d %d\n", pitch, ced_rx.channel_level, ced_rx.notch_level, hit);
+            /*endif*/
+        }
+        if (false_hit  ||  false_miss)
+        {
+            printf("Test failed.\n");
+            exit(2);
+        }
+        /*endif*/
+        printf("Test passed.\n");
+    }
+    /*endif*/
+
+    if ((test_list & PERFORM_TEST_2C))
+    {
+        printf("Test 2c: EC disable detection with frequency\n");
+        awgn_init_dbm0(&chan_noise_source, 7162534, -50.0f);
+        false_hit = FALSE;
+        false_miss = FALSE;
+        for (pitch = 2000;  pitch < 2200;  pitch++)
+        {
+            /* Use the transmitter to test the receiver */
+            modem_connect_tones_tx_init(&modem_tone_tx, MODEM_CONNECT_TONES_ANS_PR);
+            /* Fudge things for the test */
+            modem_tone_tx.tone_phase_rate = dds_phase_rate(pitch);
+            modem_tone_tx.level = dds_scaling_dbm0(-25);
+            modem_connect_tones_rx_init(&ans_pr_rx, MODEM_CONNECT_TONES_ANS_PR, NULL, NULL);
+            for (i = 0;  i < 10*SAMPLE_RATE;  i += SAMPLES_PER_CHUNK)
+            {
+                samples = modem_connect_tones_tx(&modem_tone_tx, amp, SAMPLES_PER_CHUNK);
+                for (j = 0;  j < samples;  j++)
+                    amp[j] += awgn(&chan_noise_source);
+                /*endfor*/
+                modem_connect_tones_rx(&ans_pr_rx, amp, samples);
+            }
+            /*endfor*/
+            hit = modem_connect_tones_rx_get(&ans_pr_rx);
+            if (pitch < (2100 - 70)  ||  pitch > (2100 + 70))
+            {
+                if (hit == MODEM_CONNECT_TONES_ANS_PR)
+                    false_hit = TRUE;
+                /*endif*/
+            }
+            else if (pitch > (2100 - 50)  &&  pitch < (2100 + 50))
+            {
+                if (hit != MODEM_CONNECT_TONES_ANS_PR)
+                    false_miss = TRUE;
+                /*endif*/
+            }
+            /*endif*/
+            if (hit)
+                printf("Detected at %5dHz %12d %12d %d\n", pitch, ans_pr_rx.channel_level, ans_pr_rx.notch_level, hit);
+            /*endif*/
+        }
+        /*endfor*/
+        if (false_hit  ||  false_miss)
+        {
+            printf("Test failed.\n");
+            exit(2);
+        }
+        /*endif*/
+        printf("Test passed.\n");
+    }
+    /*endif*/
+
+    if ((test_list & PERFORM_TEST_3A))
+    {
+        printf("Test 3a: CNG detection with level\n");
+        awgn_init_dbm0(&chan_noise_source, 7162534, -60.0f);
+        false_hit = FALSE;
+        false_miss = FALSE;
+        for (pitch = 1062;  pitch <= 1138;  pitch += 2*38)
+        {
+            for (level = LEVEL_MAX;  level >= LEVEL_MIN;  level--)
+            {
+                make_tone_gen_descriptor(&tone_desc,
+                                         pitch,
+                                         level,
+                                         0,
+                                         0,
+                                         500,
+                                         3000,
+                                         0,
+                                         0,
+                                         TRUE);
+                tone_gen_init(&tone_tx, &tone_desc);
+
+                modem_connect_tones_rx_init(&cng_rx, MODEM_CONNECT_TONES_FAX_CNG, NULL, NULL);
+                for (i = 0;  i < 10*SAMPLE_RATE;  i += SAMPLES_PER_CHUNK)
+                {
+                    samples = tone_gen(&tone_tx, amp, SAMPLES_PER_CHUNK);
+                    for (j = 0;  j < samples;  j++)
+                        amp[j] += awgn(&chan_noise_source);
+                    /*endfor*/
+                    modem_connect_tones_rx(&cng_rx, amp, samples);
+                }
+                /*endfor*/
+                hit = modem_connect_tones_rx_get(&cng_rx);
+                if (level < LEVEL_MIN_REJECT)
+                {
+                    if (hit == MODEM_CONNECT_TONES_FAX_CNG)
+                    {
+                        printf("False hit %d at %ddB\n", hit, level);
+                        false_hit = TRUE;
+                    }
+                    /*endif*/
+                }
+                else if (level > LEVEL_MIN_ACCEPT)
+                {
+                    if (hit != MODEM_CONNECT_TONES_FAX_CNG)
+                    {
+                        printf("False miss %d at %ddB\n", hit, level);
+                        false_miss = TRUE;
+                    }
+                    /*endif*/
+                }
+                /*endif*/
+                if (hit)
+                    printf("Detected at %5dHz %4ddB %12d %12d %d\n", pitch, level, cng_rx.channel_level, cng_rx.notch_level, hit);
+                /*endif*/
+            }
+            /*endfor*/
+        }
+        /*endfor*/
+        if (false_hit  ||  false_miss)
+        {
+            printf("Test failed.\n");
+            exit(2);
+        }
+        /*endif*/
+        printf("Test passed.\n");
+    }
+    /*endif*/
+
+    if ((test_list & PERFORM_TEST_3B))
+    {
+        printf("Test 3b: CED/ANS detection with level\n");
+        awgn_init_dbm0(&chan_noise_source, 7162534, -60.0f);
+        false_hit = FALSE;
+        false_miss = FALSE;
+        for (pitch = 2062;  pitch <= 2138;  pitch += 2*38)
+        {
+            for (level = LEVEL_MAX;  level >= LEVEL_MIN;  level--)
+            {
+                make_tone_gen_descriptor(&tone_desc,
+                                         pitch,
+                                         level,
+                                         0,
+                                         0,
+                                         2600,
+                                         0,
+                                         0,
+                                         0,
+                                         FALSE);
+                tone_gen_init(&tone_tx, &tone_desc);
+                modem_connect_tones_rx_init(&ced_rx, MODEM_CONNECT_TONES_FAX_CED_OR_PREAMBLE, NULL, NULL);
+                for (i = 0;  i < 10*SAMPLE_RATE;  i += SAMPLES_PER_CHUNK)
+                {
+                    samples = tone_gen(&tone_tx, amp, SAMPLES_PER_CHUNK);
+                    for (j = 0;  j < samples;  j++)
+                        amp[j] += awgn(&chan_noise_source);
+                    /*endfor*/
+                    modem_connect_tones_rx(&ced_rx, amp, samples);
+                }
+                /*endfor*/
+                hit = modem_connect_tones_rx_get(&ced_rx);
+                if (level < LEVEL_MIN_REJECT)
+                {
+                    if (hit == MODEM_CONNECT_TONES_FAX_CED)
+                        false_hit = TRUE;
+                    /*endif*/
+                }
+                else if (level > LEVEL_MIN_ACCEPT)
+                {
+                    if (hit != MODEM_CONNECT_TONES_FAX_CED)
+                        false_miss = TRUE;
+                    /*endif*/
+                }
+                /*endif*/
+                if (hit)
+                    printf("Detected at %5dHz %4ddB %12d %12d %d\n", pitch, level, ced_rx.channel_level, ced_rx.notch_level, hit);
+                /*endif*/
+            }
+            /*endfor*/
+        }
+        /*endfor*/
+        if (false_hit  ||  false_miss)
+        {
+            printf("Test failed.\n");
+            exit(2);
+        }
+        /*endif*/
+        printf("Test passed.\n");
+    }
+    /*endif*/
+
+    if ((test_list & PERFORM_TEST_3C))
+    {
+        printf("Test 3c: EC disable detection with level\n");
+        awgn_init_dbm0(&chan_noise_source, 7162534, -60.0f);
+        false_hit = FALSE;
+        false_miss = FALSE;
+        for (pitch = 2062;  pitch <= 2138;  pitch += 2*38)
+        {
+            for (level = LEVEL_MAX;  level >= LEVEL_MIN;  level--)
+            {
+                /* Use the transmitter to test the receiver */
+                modem_connect_tones_tx_init(&modem_tone_tx, MODEM_CONNECT_TONES_ANS_PR);
+                /* Fudge things for the test */
+                modem_tone_tx.tone_phase_rate = dds_phase_rate(pitch);
+                modem_tone_tx.level = dds_scaling_dbm0(level);
+                modem_connect_tones_rx_init(&ans_pr_rx, MODEM_CONNECT_TONES_ANS_PR, NULL, NULL);
+                for (i = 0;  i < 10*SAMPLE_RATE;  i += SAMPLES_PER_CHUNK)
+                {
+                    samples = modem_connect_tones_tx(&modem_tone_tx, amp, SAMPLES_PER_CHUNK);
+                    for (j = 0;  j < samples;  j++)
+                        amp[j] += awgn(&chan_noise_source);
+                    /*endfor*/
+                    modem_connect_tones_rx(&ans_pr_rx, amp, samples);
+                }
+                /*endfor*/
+                hit = modem_connect_tones_rx_get(&ans_pr_rx);
+                if (level < LEVEL_MIN_REJECT)
+                {
+                    if (hit == MODEM_CONNECT_TONES_ANS_PR)
+                        false_hit = TRUE;
+                    /*endif*/
+                }
+                else if (level > LEVEL_MIN_ACCEPT)
+                {
+                    if (hit != MODEM_CONNECT_TONES_ANS_PR)
+                        false_miss = TRUE;
+                    /*endif*/
+                }
+                /*endif*/
+                if (hit)
+                    printf("Detected at %5dHz %4ddB %12d %12d %d\n", pitch, level, ans_pr_rx.channel_level, ans_pr_rx.notch_level, hit);
+                /*endif*/
+            }
+            /*endfor*/
+        }
+        /*endfor*/
+        if (false_hit  ||  false_miss)
+        {
+            printf("Test failed.\n");
+            exit(2);
+        }
+        /*endif*/
+        printf("Test passed.\n");
+    }
+    /*endif*/
+
+    if ((test_list & PERFORM_TEST_4))
+    {
+        printf("Test 4: CED detection, when stimulated with V.21 preamble\n");
+        false_hit = FALSE;
+        false_miss = FALSE;
+
+        /* Send 255 bits of preamble (0.85s, the minimum specified preamble for T.30), and then
+           some random bits. Check the preamble detector comes on, and goes off at reasonable times. */
+        fsk_tx_init(&preamble_tx, &preset_fsk_specs[FSK_V21CH2], preamble_get_bit, NULL);
+        modem_connect_tones_rx_init(&ced_rx, MODEM_CONNECT_TONES_FAX_CED_OR_PREAMBLE, preamble_detected, NULL);
+        for (i = 0;  i < 2*SAMPLE_RATE;  i += SAMPLES_PER_CHUNK)
+        {
+            samples = fsk_tx(&preamble_tx, amp, SAMPLES_PER_CHUNK);
+            modem_connect_tones_rx(&ced_rx, amp, samples);
+        }
+        /*endfor*/
+        for (i = 0;  i < SAMPLE_RATE/10;  i += SAMPLES_PER_CHUNK)
+        {
+            memset(amp, 0, sizeof(int16_t)*SAMPLES_PER_CHUNK);
+            modem_connect_tones_rx(&ced_rx, amp, SAMPLES_PER_CHUNK);
+        }
+        /*endfor*/
+        if (preamble_on_at < 40  ||  preamble_on_at > 50
+            ||
+            preamble_off_at < 580  ||  preamble_off_at > 620)
+        {
+            printf("Test failed.\n");
+            exit(2);
+        }
+        /*endif*/
+        printf("Test passed.\n");
+    }
+    /*endif*/
+
+    if ((test_list & PERFORM_TEST_5A))
+    {
+        printf("Test 5A: ANS/ANS PR detection with reversal interval\n");
+        awgn_init_dbm0(&chan_noise_source, 7162534, -60.0f);
+        false_hit = FALSE;
+        false_miss = FALSE;
+        pitch = 2100;
+        level = -15;
+        for (interval = 400;  interval < 800;  interval++)
+        {
+            printf("Reversal interval = %d\n", interval);
+            /* Use the transmitter to test the receiver */
+            modem_connect_tones_tx_init(&modem_tone_tx, MODEM_CONNECT_TONES_ANS_PR);
+            /* Fudge things for the test */
+            modem_tone_tx.tone_phase_rate = dds_phase_rate(pitch);
+            modem_tone_tx.level = dds_scaling_dbm0(level);
+            modem_connect_tones_rx_init(&ans_pr_rx, MODEM_CONNECT_TONES_ANS_PR, ans_pr_detected, NULL);
+            hits = 0;
+            for (i = 0;  i < 10*SAMPLE_RATE;  i += SAMPLES_PER_CHUNK)
+            {
+                when = i;
+                samples = SAMPLES_PER_CHUNK;
+                for (j = 0;  j < samples;  j++)
+                {
+                    if (--modem_tone_tx.hop_timer <= 0)
+                    {
+                        modem_tone_tx.hop_timer = ms_to_samples(interval);
+                        modem_tone_tx.tone_phase += 0x80000000;
+                    }
+                    /*endif*/
+                    amp[j] = dds_mod(&modem_tone_tx.tone_phase, modem_tone_tx.tone_phase_rate, modem_tone_tx.level, 0);
+                }
+                for (j = 0;  j < samples;  j++)
+                    amp[j] += awgn(&chan_noise_source);
+                /*endfor*/
+                modem_connect_tones_rx(&ans_pr_rx, amp, samples);
+            }
+            /*endfor*/
+            if (interval < (450 - 25)  ||  interval > (450 + 25))
+            {
+                if (hits != 0)
+                    false_hit = TRUE;
+                /*endif*/
+            }
+            else if (interval > (450 - 25)  &&  interval < (450 + 25))
+            {
+                if (hits == 0)
+                    false_miss = TRUE;
+                /*endif*/
+            }
+            /*endif*/
+            if (hits)
+                printf("Detected at %5dHz %4ddB %dms %12d %12d %d\n", pitch, level, interval, ans_pr_rx.channel_level, ans_pr_rx.notch_level, hits);
+            /*endif*/
+        }
+        /*endfor*/
+        if (false_hit  ||  false_miss)
+        {
+            printf("Test failed.\n");
+            exit(2);
+        }
+        /*endif*/
+        printf("Test passed.\n");
+    }
+    /*endif*/
+
+    if ((test_list & PERFORM_TEST_5B))
+    {
+        printf("Test 5B: ANS/ANS PR detection with mixed reversal intervals\n");
+        awgn_init_dbm0(&chan_noise_source, 7162534, -60.0f);
+        false_hit = FALSE;
+        false_miss = FALSE;
+        pitch = 2100;
+        level = -15;
+        interval = 450;
+        printf("Reversal interval = %d\n", interval);
+        /* Use the transmitter to test the receiver */
+        modem_connect_tones_tx_init(&modem_tone_tx, MODEM_CONNECT_TONES_ANS_PR);
+        /* Fudge things for the test */
+        modem_tone_tx.tone_phase_rate = dds_phase_rate(pitch);
+        modem_tone_tx.level = dds_scaling_dbm0(level);
+        modem_connect_tones_rx_init(&ans_pr_rx, MODEM_CONNECT_TONES_ANS_PR, ans_pr_detected, NULL);
+        hits = 0;
+        cycle = 0;
+        for (i = 0;  i < 60*SAMPLE_RATE;  i += SAMPLES_PER_CHUNK)
+        {
+            when = i;
+            samples = SAMPLES_PER_CHUNK;
+            for (j = 0;  j < samples;  j++)
+            {
+                if (--modem_tone_tx.hop_timer <= 0)
+                {
+                    if (++cycle == 10)
+                        interval = 1000;
+                    if (cycle == 20)
+                        interval = 450;
+                    modem_tone_tx.hop_timer = ms_to_samples(interval);
+                    modem_tone_tx.tone_phase += 0x80000000;
+                }
+                amp[j] = dds_mod(&modem_tone_tx.tone_phase, modem_tone_tx.tone_phase_rate, modem_tone_tx.level, 0);
+            }
+            /*endfor*/
+            for (j = 0;  j < samples;  j++)
+                amp[j] += awgn(&chan_noise_source);
+            /*endfor*/
+            modem_connect_tones_rx(&ans_pr_rx, amp, samples);
+        }
+        /*endfor*/
+        if (false_hit  ||  false_miss)
+        {
+            printf("Test failed.\n");
+            exit(2);
+        }
+        /*endif*/
+        printf("Test passed.\n");
+    }
+    /*endif*/
+
+    if ((test_list & PERFORM_TEST_6))
+    {
+        /* Talk-off test */
+        /* Here we use the BellCore and Mitel talk off test tapes, intended for DTMF
+           detector testing. Presumably they should also have value here, but I am not
+           sure. If those voice snippets were chosen to be tough on DTMF detectors, they
+           might go easy on detectors looking for different pitches. However, the
+           Mitel DTMF test tape is known (the hard way) to exercise 2280Hz tone
+           detectors quite well. */
+        printf("Test 6: Talk-off test\n");
+        modem_connect_tones_rx_init(&cng_rx, MODEM_CONNECT_TONES_FAX_CNG, NULL, NULL);
+        modem_connect_tones_rx_init(&ced_rx, MODEM_CONNECT_TONES_FAX_CED_OR_PREAMBLE, NULL, NULL);
+        modem_connect_tones_rx_init(&ans_pr_rx, MODEM_CONNECT_TONES_ANS_PR, NULL, NULL);
+        hits = 0;
+        for (j = 0;  bellcore_files[j][0];  j++)
+        {
+            if ((inhandle = afOpenFile_telephony_read(bellcore_files[j], 1)) == AF_NULL_FILEHANDLE)
+            {
+                fprintf(stderr, "    Cannot open speech file '%s'\n", bellcore_files[j]);
+                exit (2);
+            }
+            /*endif*/
+
+            when = 0;
+            hits = 0;
+            while ((frames = afReadFrames(inhandle, AF_DEFAULT_TRACK, amp, 8000)))
+            {
+                when++;
+                modem_connect_tones_rx(&cng_rx, amp, frames);
+                modem_connect_tones_rx(&ced_rx, amp, frames);
+                modem_connect_tones_rx(&ans_pr_rx, amp, frames);
+                if (modem_connect_tones_rx_get(&cng_rx) != MODEM_CONNECT_TONES_NONE)
+                {
+                    /* This is not a true measure of hits, as there might be more
+                       than one in a block of data. However, since the only good
+                       result is no hits, this approximation is OK. */
+                    printf("Hit CNG at %ds\n", when);
+                    hits++;
+                    modem_connect_tones_rx_init(&cng_rx, MODEM_CONNECT_TONES_FAX_CNG, NULL, NULL);
+                }
+                /*endif*/
+                if (modem_connect_tones_rx_get(&ced_rx) != MODEM_CONNECT_TONES_NONE)
+                {
+                    printf("Hit CED at %ds\n", when);
+                    hits++;
+                    modem_connect_tones_rx_init(&ced_rx, MODEM_CONNECT_TONES_FAX_CED_OR_PREAMBLE, NULL, NULL);
+                }
+                /*endif*/
+                if (modem_connect_tones_rx_get(&ans_pr_rx) != MODEM_CONNECT_TONES_NONE)
+                {
+                    printf("Hit EC disable at %ds\n", when);
+                    hits++;
+                    modem_connect_tones_rx_init(&ans_pr_rx, MODEM_CONNECT_TONES_ANS_PR, NULL, NULL);
+                }
+                /*endif*/
+            }
+            /*endwhile*/
+            if (afCloseFile(inhandle) != 0)
+            {
+                fprintf(stderr, "    Cannot close speech file '%s'\n", bellcore_files[j]);
+                exit(2);
+            }
+            /*endif*/
+            printf("    File %d gave %d false hits.\n", j + 1, hits);
+        }
+        /*endfor*/
+        if (hits > 0)
+        {
+            printf("Test failed.\n");
+            exit(2);
+        }
+        /*endif*/
+        printf("Test passed.\n");
+    }
+    /*endif*/
+
+    if (decode_test_file)
+    {
+        printf("Decode file '%s'\n", decode_test_file);
+        modem_connect_tones_rx_init(&cng_rx, MODEM_CONNECT_TONES_FAX_CNG, cng_detected, NULL);
+        modem_connect_tones_rx_init(&ced_rx, MODEM_CONNECT_TONES_FAX_CED_OR_PREAMBLE, ced_detected, NULL);
+        modem_connect_tones_rx_init(&ans_pr_rx, MODEM_CONNECT_TONES_ANS_PR, ec_dis_detected, NULL);
+        hits = 0;
+        if ((inhandle = afOpenFile_telephony_read(decode_test_file, 1)) == AF_NULL_FILEHANDLE)
+        {
+            fprintf(stderr, "    Cannot open speech file '%s'\n", decode_test_file);
+            exit (2);
+        }
+        /*endif*/
+
+        when = 0;
+        hits = 0;
+        while ((frames = afReadFrames(inhandle, AF_DEFAULT_TRACK, amp, 8000)))
+        {
+            when++;
+            modem_connect_tones_rx(&cng_rx, amp, frames);
+            modem_connect_tones_rx(&ced_rx, amp, frames);
+            modem_connect_tones_rx(&ans_pr_rx, amp, frames);
+        }
+        /*endwhile*/
+        if (afCloseFile(inhandle) != 0)
+        {
+            fprintf(stderr, "    Cannot close speech file '%s'\n", decode_test_file);
+            exit(2);
+        }
+        /*endif*/
+        printf("    File gave %d hits.\n", hits);
+    }
+    printf("Tests passed.\n");
+    return  0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/tests/modem_echo_tests.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/tests/modem_echo_tests.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,404 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * modem_echo_tests.c
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2004 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: modem_echo_tests.c,v 1.30 2008/08/29 09:28:13 steveu Exp $
+ */
+
+/*! \page modem_echo_can_tests_page Line echo cancellation for modems tests
+\section modem_echo_can_tests_page_sec_1 What does it do?
+Currently the echo cancellation tests only provide simple exercising of the
+cancellor in the way it might be used for line echo cancellation. The test code
+is in echotests.c. 
+
+The goal is to test the echo cancellor again the G.16X specs. Clearly, that also
+means the goal for the cancellor itself is to comply with those specs. Right
+now, the only aspect of these tests implemented is the line impulse response
+models in g168tests.c. 
+
+\section modem_echo_can_tests_page_sec_2 How does it work?
+The current test consists of feeding a wave file of real speech to the echo
+cancellor as the transmit signal. A very simple model of a telephone line is
+used to simulate a simple echo from the transmit signal. A second wave file of
+real speech is also used to simulate a signal received form the far end of the
+line. This is gated so it is only placed for one second every 10 seconds,
+simulating the double talk condition. The resulting echo cancelled signal can
+either be store in a file for further analysis, or played back as the data is
+processed. 
+
+A number of modified versions of this test have been performed. The signal level
+of the two speech sources has been varied. Several simple models of the
+telephone line have been used. Although the current cancellor design has known
+limitations, it seems stable for all these test conditions. No instability has
+been observed in the current version due to arithmetic overflow when the speech
+is very loud (with earlier versions, well, ....:) ). The lack of saturating
+arithmetic in general purpose CPUs is a huge disadvantage here, as software
+saturation logic would cause a major slow down. Floating point would be good,
+but is not usable in the Linux kernel. Anyway, the bottom line seems to be the
+current design is genuinely useful, if imperfect. 
+
+\section modem_echo_can_tests_page_sec_2 How do I use it?
+
+Build the tests with the command "./build". Currently there is no proper make
+setup, or way to build individual tests. "./build" will built all the tests
+which currently exist for the DSP functions. The echo cancellation test assumes
+there are two wave files containing mono, 16 bit signed PCM speech data, sampled
+at 8kHz. These should be called local_sound.wav and far_sound.wav. A third wave
+file will be produced. This very crudely starts with the first 256 bytes from
+the local_sound.wav file, followed by the results of the echo cancellation. The
+resulting audio is also played to the /dev/dsp device. A printf near the end of
+echo_tests.c is commented out with a \#if. If this is enabled, detailed
+information about the results of the echo cancellation will be written to
+stdout. By saving this into a file, Grace (recommended), GnuPlot, or some other
+plotting package may be used to graphically display the functioning of the
+cancellor.  
+*/
+
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
+
+#if defined(HAVE_FL_FL_H)  &&  defined(HAVE_FL_FL_CARTESIAN_H)  &&  defined(HAVE_FL_FL_AUDIO_METER_H)
+#define ENABLE_GUI
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <string.h>
+#include <time.h>
+#include <audiofile.h>
+#if defined(HAVE_MATH_H)
+#define GEN_CONST
+#endif
+
+#include "spandsp.h"
+#include "spandsp/g168models.h"
+#include "spandsp-sim.h"
+#if defined(ENABLE_GUI)
+#include "echo_monitor.h"
+#endif
+
+#if !defined(NULL)
+#define NULL (void *) 0
+#endif
+
+typedef struct
+{
+    const char *name;
+    int max;
+    int cur;
+    AFfilehandle handle;
+    int16_t signal[8000];
+} signal_source_t;
+
+signal_source_t local_css;
+
+fir32_state_t line_model;
+
+AFfilehandle resulthandle;
+int16_t residue_sound[8000];
+int residue_cur = 0;
+int do_codec_munge = TRUE;
+int use_gui = FALSE;
+
+static const int16_t tone_1khz[] = {10362, 7327, 0, -7327, -10362, -7327, 0, 7327};
+
+static inline void put_residue(int16_t tx, int16_t residue)
+{
+    int outframes;
+
+    residue_sound[residue_cur++] = tx;
+    residue_sound[residue_cur++] = residue;
+    if (residue_cur >= 8000)
+    {
+        residue_cur >>= 1;
+        outframes = afWriteFrames(resulthandle,
+                                  AF_DEFAULT_TRACK,
+                                  residue_sound,
+                                  residue_cur);
+        if (outframes != residue_cur)
+        {
+            fprintf(stderr, "    Error writing residue sound\n");
+            exit(2);
+        }
+        residue_cur = 0;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static void signal_load(signal_source_t *sig, const char *name)
+{
+    sig->name = name;
+    if ((sig->handle = afOpenFile_telephony_read(sig->name, 1)) == AF_NULL_FILEHANDLE)
+    {
+        fprintf(stderr, "    Cannot open sound file '%s'\n", sig->name);
+        exit(2);
+    }
+    sig->max = afReadFrames(sig->handle, AF_DEFAULT_TRACK, sig->signal, 8000);
+    if (sig->max < 0)
+    {
+        fprintf(stderr, "    Error reading sound file '%s'\n", sig->name);
+        exit(2);
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static void signal_free(signal_source_t *sig)
+{
+    if (afCloseFile(sig->handle) != 0)
+    {
+        fprintf(stderr, "    Cannot close sound file '%s'\n", sig->name);
+        exit(2);
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static void signal_restart(signal_source_t *sig)
+{
+    sig->cur = 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int16_t signal_amp(signal_source_t *sig)
+{
+    int16_t tx;
+
+    tx = sig->signal[sig->cur++];
+    if (sig->cur >= sig->max)
+        sig->cur = 0;
+    return tx;
+}
+/*- End of function --------------------------------------------------------*/
+
+static inline int16_t codec_munger(int16_t amp)
+{
+    if (do_codec_munge)
+        return alaw_to_linear(linear_to_alaw(amp));
+    return amp;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void channel_model_create(int model)
+{
+    static const int32_t *line_models[] =
+    {
+        line_model_d2_coeffs,
+        line_model_d3_coeffs,
+        line_model_d4_coeffs,
+        line_model_d5_coeffs,
+        line_model_d6_coeffs,
+        line_model_d7_coeffs,
+        line_model_d8_coeffs,
+        line_model_d9_coeffs
+    };
+
+    static int line_model_sizes[] =
+    {
+        sizeof(line_model_d2_coeffs)/sizeof(int32_t),
+        sizeof(line_model_d3_coeffs)/sizeof(int32_t),
+        sizeof(line_model_d4_coeffs)/sizeof(int32_t),
+        sizeof(line_model_d5_coeffs)/sizeof(int32_t),
+        sizeof(line_model_d6_coeffs)/sizeof(int32_t),
+        sizeof(line_model_d7_coeffs)/sizeof(int32_t),
+        sizeof(line_model_d8_coeffs)/sizeof(int32_t),
+        sizeof(line_model_d9_coeffs)/sizeof(int32_t)
+    };
+
+    fir32_create(&line_model, line_models[model], line_model_sizes[model]);
+}
+/*- End of function --------------------------------------------------------*/
+
+static int16_t channel_model(int16_t local, int16_t far)
+{
+    int16_t echo;
+    int16_t rx;
+
+    /* Channel modelling is merely simulating the effects of A-law distortion
+       and using one of the echo models from G.168 */
+
+    /* The local tx signal will have gone through an A-law munging before
+       it reached the line's analogue area where the echo occurs. */
+    echo = fir32(&line_model, codec_munger(local/8));
+    /* The far end signal will have been through an A-law munging, although
+       this should not affect things. */
+    rx = echo + codec_munger(far);
+    /* This mixed echo and far end signal will have been through an A-law munging when it came back into
+       the digital network. */
+    rx = codec_munger(rx);
+    return  rx;
+}
+/*- End of function --------------------------------------------------------*/
+
+int main(int argc, char *argv[])
+{
+    modem_echo_can_state_t *ctx;
+    //awgn_state_t local_noise_source;
+    awgn_state_t far_noise_source;
+    int i;
+    int clean;
+    int16_t rx;
+    int16_t tx;
+    int local_cur;
+    int far_cur;
+    int result_cur;
+    int line_model_no;
+    time_t now;
+    power_meter_t power_before;
+    power_meter_t power_after;
+    float unadapted_output_power;
+    float unadapted_echo_power;
+    float adapted_output_power;
+    float adapted_echo_power;
+#if defined(ENABLE_GUI)
+    int16_t amp[2];
+#endif
+    
+    line_model_no = 0;
+    use_gui = FALSE;
+    for (i = 1;  i < argc;  i++)
+    {
+        if (strcmp(argv[i], "-g") == 0)
+        {
+            use_gui = TRUE;
+            continue;
+        }
+        line_model_no = atoi(argv[1]);
+    }
+    time(&now);
+    ctx = modem_echo_can_create(256);
+    awgn_init_dbm0(&far_noise_source, 7162534, -50.0f);
+
+    signal_load(&local_css, "sound_c1_8k.wav");
+
+    if ((resulthandle = afOpenFile_telephony_write("modem_echo.wav", 2)) == AF_NULL_FILEHANDLE)
+    {
+        fprintf(stderr, "    Failed to open result file\n");
+        exit(2);
+    }
+
+#if defined(ENABLE_GUI)
+    if (use_gui)
+        start_echo_can_monitor(256);
+#endif
+
+    local_cur = 0;
+    far_cur = 0;
+    result_cur = 0;
+    channel_model_create(line_model_no);
+#if defined(ENABLE_GUI)
+    if (use_gui)
+        echo_can_monitor_line_model_update(line_model.coeffs, line_model.taps);
+#endif
+
+    modem_echo_can_flush(ctx);
+
+    power_meter_init(&power_before, 5);
+    power_meter_init(&power_after, 5);
+    
+    /* Measure the echo power before adaption */
+    modem_echo_can_adaption_mode(ctx, FALSE);
+    for (i = 0;  i < 8000*5;  i++)
+    {
+        tx = tone_1khz[i & 7];
+        rx = channel_model(tx, 0);
+        clean = modem_echo_can_update(ctx, tx, rx);
+        power_meter_update(&power_before, rx);
+        power_meter_update(&power_after, clean);
+    }
+    unadapted_output_power = power_meter_current_dbm0(&power_before);
+    unadapted_echo_power = power_meter_current_dbm0(&power_after);
+    printf("Pre-adaption: output power %10.5fdBm0, echo power %10.5fdBm0\n", unadapted_output_power, unadapted_echo_power);
+    
+    /* Converge the canceller */
+    signal_restart(&local_css);
+    modem_echo_can_adaption_mode(ctx, TRUE);
+    for (i = 0;  i < 800*2;  i++)
+    {
+        clean = modem_echo_can_update(ctx, 0, 0);
+        put_residue(0, clean);
+    }
+
+    for (i = 0;  i < 8000*50;  i++)
+    {
+        tx = signal_amp(&local_css);
+        rx = channel_model(tx, 0);
+        clean = modem_echo_can_update(ctx, tx, rx);
+        power_meter_update(&power_before, rx);
+        power_meter_update(&power_after, clean);
+#if 0
+        if (i%800 == 0)
+            printf("Powers %10.5fdBm0 %10.5fdBm0\n", power_meter_current_dbm0(&power_before), power_meter_current_dbm0(&power_after));
+#endif
+        put_residue(tx, clean);
+#if defined(ENABLE_GUI)
+        if (use_gui)
+        {
+            echo_can_monitor_can_update(ctx->fir_taps16, 256);
+            amp[0] = tx;
+            echo_can_monitor_line_spectrum_update(amp, 1);
+        }
+#endif
+    }
+
+    /* Now lets see how well adapted we are */
+    modem_echo_can_adaption_mode(ctx, FALSE);
+    for (i = 0;  i < 8000*5;  i++)
+    {
+        tx = tone_1khz[i & 7];
+        rx = channel_model(tx, 0);
+        clean = modem_echo_can_update(ctx, tx, rx);
+        power_meter_update(&power_before, rx);
+        power_meter_update(&power_after, clean);
+    }
+    adapted_output_power = power_meter_current_dbm0(&power_before);
+    adapted_echo_power = power_meter_current_dbm0(&power_after);
+    printf("Post-adaption: output power %10.5fdBm0, echo power %10.5fdBm0\n", adapted_output_power, adapted_echo_power);
+    
+    if (fabsf(adapted_output_power - unadapted_output_power) > 0.1f
+        ||
+        adapted_echo_power > unadapted_echo_power - 30.0f)
+    {
+        printf("Tests failed.\n");
+        exit(2);
+    }
+
+    modem_echo_can_free(ctx);
+    signal_free(&local_css);
+
+    if (afCloseFile(resulthandle) != 0)
+    {
+        fprintf(stderr, "    Cannot close speech file '%s'\n", "result_sound.wav");
+        exit(2);
+    }
+
+#if defined(ENABLE_GUI)
+    if (use_gui)
+        echo_can_monitor_wait_to_end();
+#endif
+
+    printf("Tests passed.\n");
+    return  0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/tests/modem_monitor.cpp
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/tests/modem_monitor.cpp	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,543 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * modem_monitor.cpp - Display QAM constellations, using the FLTK toolkit.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2004 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: modem_monitor.cpp,v 1.15 2008/05/27 15:08:21 steveu Exp $
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#if defined(HAVE_FL_FL_H)  &&  defined(HAVE_FL_FL_CARTESIAN_H)
+
+#define __STDC_LIMIT_MACROS
+
+#include <inttypes.h>
+#include <stdio.h>
+#include <math.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/select.h>
+
+#include <FL/Fl.H>
+#include <FL/Fl_Overlay_Window.H>
+#include <FL/Fl_Light_Button.H>
+#include <FL/Fl_Cartesian.H>
+#include <FL/Fl_Audio_Meter.H>
+#include <FL/Fl_Output.H>
+#include <FL/fl_draw.H>
+
+#include "spandsp.h"
+
+#define SYMBOL_TRACKER_POINTS   12000
+#define CARRIER_TRACKER_POINTS  12000
+
+struct qam_monitor_s
+{
+    Fl_Double_Window *w;
+    Fl_Group *c_const;
+    Fl_Group *c_right;
+    Fl_Group *c_eq;
+    Fl_Group *c_symbol_track;
+    
+    /* Constellation stuff */
+    Ca_Canvas *canvas_const;
+    Ca_X_Axis *sig_i;
+    Ca_Y_Axis *sig_q;
+
+    Ca_Point *constel_point[100000];
+    int constel_window;
+    int next_constel_point;
+    int skip;
+
+    /* Equalizer stuff */
+    Ca_Canvas *canvas_eq;
+
+    Ca_X_Axis *eq_x;
+    Ca_Y_Axis *eq_y;
+
+    Ca_Line *eq_re;
+    Ca_Line *eq_im;
+
+    double eq_re_plot[200];
+    double eq_im_plot[200];
+
+    /* Carrier and symbol tracking stuff */
+    Ca_Canvas *canvas_track;
+
+    Ca_X_Axis *track_x;
+    Ca_Y_Axis *symbol_track_y;
+    Ca_Y_Axis *carrier_y;
+
+    Ca_Line *symbol_track;
+    Ca_Line *carrier;
+
+    float symbol_tracker[SYMBOL_TRACKER_POINTS];
+    double symbol_track_plot[SYMBOL_TRACKER_POINTS*2];
+    int symbol_track_points;
+    int symbol_track_ptr;
+    int symbol_track_window;
+
+    float carrier_tracker[CARRIER_TRACKER_POINTS];
+    double carrier_plot[CARRIER_TRACKER_POINTS*2];
+    int carrier_points;
+    int carrier_ptr;
+    int carrier_window;
+
+    /* Audio meter stuff */
+    Fl_Audio_Meter *audio_meter;
+    Fl_Output *audio_level;
+    int32_t power_reading;
+};
+
+#include "modem_monitor.h"
+
+int qam_monitor_clear_constel(qam_monitor_t *s)
+{
+    int i;
+
+    s->canvas_const->current(s->canvas_const);
+    for (i = 0;  i < s->constel_window;  i++)
+    {
+        if (s->constel_point[i])
+        {
+            delete s->constel_point[i];
+            s->constel_point[i] = NULL;
+        }
+    }
+    s->next_constel_point = 0;
+    Fl::check();
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int qam_monitor_update_constel(qam_monitor_t *s, const complexf_t *pt)
+{
+    int i;
+
+    s->canvas_const->current(s->canvas_const);
+    if (s->constel_point[s->next_constel_point])
+        delete s->constel_point[s->next_constel_point];
+    s->constel_point[s->next_constel_point++] = new Ca_Point(pt->re, pt->im, FL_BLACK);
+    if (s->next_constel_point >= s->constel_window)
+        s->next_constel_point = 0;
+    if (++s->skip >= 100)
+    {
+        s->skip = 0;
+        Fl::check();
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int qam_monitor_update_equalizer(qam_monitor_t *s, const complexf_t *coeffs, int len)
+{
+    int i;
+    float min;
+    float max;
+
+    /* Protect against screwy values */
+    for (i = 0;  i < len;  i++)
+    {
+        if (isnan(coeffs[i].re)  ||  isinf(coeffs[i].re))
+            break;
+        if (isnan(coeffs[i].im)  ||  isinf(coeffs[i].im))
+            break;
+        if (coeffs[i].re < -20.0  ||  coeffs[i].re > 20.0)
+            break;
+        if (coeffs[i].im < -20.0  ||  coeffs[i].im > 20.0)
+            break;
+    }
+    if (i != len)
+        return -1;
+
+    if (s->eq_re)
+        delete s->eq_re;
+    if (s->eq_im)
+        delete s->eq_im;
+
+    s->canvas_eq->current(s->canvas_eq);
+    i = 0;
+    min = coeffs[i].re;
+    if (min > coeffs[i].im)
+        min = coeffs[i].im;
+    max = coeffs[i].re;
+    if (max < coeffs[i].im)
+        max = coeffs[i].im;
+    for (i = 0;  i < len;  i++)
+    {
+        s->eq_re_plot[2*i] = (i - len/2)/2.0;
+        s->eq_re_plot[2*i + 1] = coeffs[i].re;
+        if (min > coeffs[i].re)
+            min = coeffs[i].re;
+        if (max < coeffs[i].re)
+            max = coeffs[i].re;
+
+        s->eq_im_plot[2*i] = (i - len/2)/2.0;
+        s->eq_im_plot[2*i + 1] = coeffs[i].im;
+        if (min > coeffs[i].im)
+            min = coeffs[i].im;
+        if (max < coeffs[i].im)
+            max = coeffs[i].im;
+    }
+    
+    s->eq_x->minimum(-len/4.0);
+    s->eq_x->maximum(len/4.0);
+    s->eq_y->maximum((max == min)  ?  max + 0.2  :  max);
+    s->eq_y->minimum(min);
+    s->eq_re = new Ca_Line(len, s->eq_re_plot, 0, 0, FL_BLUE, CA_NO_POINT);
+    s->eq_im = new Ca_Line(len, s->eq_im_plot, 0, 0, FL_RED, CA_NO_POINT);
+    Fl::check();
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int qam_monitor_update_symbol_tracking(qam_monitor_t *s, float total_correction)
+{
+    int i;
+    int j;
+    float min;
+    float max;
+
+    s->symbol_tracker[s->symbol_track_ptr++] = total_correction;
+    if (s->symbol_track_points < SYMBOL_TRACKER_POINTS)
+        s->symbol_track_points++;
+    if (s->symbol_track_ptr >= SYMBOL_TRACKER_POINTS)
+        s->symbol_track_ptr = 0;
+
+    s->canvas_track->current(s->canvas_track);
+    if (s->symbol_track)
+        delete s->symbol_track;
+    s->track_x->current();
+    s->symbol_track_y->current();
+
+    min =
+    max = s->symbol_tracker[0];
+    for (i = s->symbol_track_ptr, j = 0;  i < s->symbol_track_points;  i++, j++)
+    {
+        s->symbol_track_plot[2*j] = j;
+        s->symbol_track_plot[2*j + 1] = s->symbol_tracker[i];
+        if (min > s->symbol_tracker[i])
+            min = s->symbol_tracker[i];
+        if (max < s->symbol_tracker[i])
+            max = s->symbol_tracker[i];
+    }
+    for (i = 0;  i < s->symbol_track_ptr;  i++, j++)
+    {
+        s->symbol_track_plot[2*j] = j;
+        s->symbol_track_plot[2*j + 1] = s->symbol_tracker[i];
+        if (min > s->symbol_tracker[i])
+            min = s->symbol_tracker[i];
+        if (max < s->symbol_tracker[i])
+            max = s->symbol_tracker[i];
+    }
+    s->symbol_track_y->maximum((fabs(max - min) < 0.05)  ?  max + 0.05  :  max);
+    s->symbol_track_y->minimum(min);
+
+    s->symbol_track = new Ca_Line(s->symbol_track_points, s->symbol_track_plot, 0, 0, FL_RED, CA_NO_POINT);
+    //Fl::check();
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int qam_monitor_update_audio_level(qam_monitor_t *s, const int16_t amp[], int len)
+{
+    int i;
+    char buf[11];
+    double val;
+    
+    for (i = 0;  i < len;  i++)
+    {
+        s->audio_meter->sample(amp[i]/32768.0);
+        s->power_reading += ((amp[i]*amp[i] - s->power_reading) >> 10);
+    }
+    if (s->power_reading <= 0)
+        val = -90.0;
+    else
+        val = log10((double) s->power_reading/(32767.0f*32767.0f))*10.0f + 3.14 + 3.02;
+
+    snprintf(buf, sizeof(buf), "%5.1fdBm0", val);
+    s->audio_level->value(buf);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int qam_monitor_update_carrier_tracking(qam_monitor_t *s, float carrier_freq)
+{
+    int i;
+    int j;
+    float min;
+    float max;
+
+    s->carrier_tracker[s->carrier_ptr++] = carrier_freq;
+    if (s->carrier_points < CARRIER_TRACKER_POINTS)
+        s->carrier_points++;
+    if (s->carrier_ptr >= CARRIER_TRACKER_POINTS)
+        s->carrier_ptr = 0;
+
+    s->canvas_track->current(s->canvas_track);
+    if (s->carrier)
+        delete s->carrier;
+    s->track_x->current();
+    s->carrier_y->current();
+
+    min =
+    max = s->carrier_tracker[0];
+    for (i = s->carrier_ptr, j = 0;  i < s->carrier_points;  i++, j++)
+    {
+        s->carrier_plot[2*j] = j;
+        s->carrier_plot[2*j + 1] = s->carrier_tracker[i];
+        if (min > s->carrier_tracker[i])
+            min = s->carrier_tracker[i];
+        if (max < s->carrier_tracker[i])
+            max = s->carrier_tracker[i];
+    }
+    for (i = 0;  i < s->carrier_ptr;  i++, j++)
+    {
+        s->carrier_plot[2*j] = j;
+        s->carrier_plot[2*j + 1] = s->carrier_tracker[i];
+        if (min > s->carrier_tracker[i])
+            min = s->carrier_tracker[i];
+        if (max < s->carrier_tracker[i])
+            max = s->carrier_tracker[i];
+    }
+
+    s->carrier_y->maximum((fabs(max - min) < 0.05)  ?  max + 0.05  :  max);
+    s->carrier_y->minimum(min);
+
+    s->carrier = new Ca_Line(s->carrier_points, s->carrier_plot, 0, 0, FL_BLUE, CA_NO_POINT);
+    //Fl::check();
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+qam_monitor_t *qam_monitor_init(float constel_width, const char *tag)
+{
+    char buf[132 + 1];
+    float x;
+    float y;
+    qam_monitor_t *s;
+    
+    if ((s = (qam_monitor_t *) malloc(sizeof(*s))) == NULL)
+        return NULL;
+    
+    s->w = new Fl_Double_Window(905, 400, (tag)  ?  tag  :  "QAM monitor");
+
+    s->c_const = new Fl_Group(0, 0, 380, 400);
+    s->c_const->box(FL_DOWN_BOX);
+    s->c_const->align(FL_ALIGN_TOP | FL_ALIGN_INSIDE);
+
+    s->canvas_const = new Ca_Canvas(60, 30, 300, 300, "Constellation");
+    s->canvas_const->box(FL_PLASTIC_DOWN_BOX);
+    s->canvas_const->color(7);
+    s->canvas_const->align(FL_ALIGN_TOP);
+    s->canvas_const->border(15);
+
+    s->sig_i = new Ca_X_Axis(65, 330, 290, 30, "I");
+    s->sig_i->align(FL_ALIGN_BOTTOM);
+    s->sig_i->minimum(-constel_width);
+    s->sig_i->maximum(constel_width);
+    s->sig_i->label_format("%g");
+    s->sig_i->minor_grid_color(fl_gray_ramp(20));
+    s->sig_i->major_grid_color(fl_gray_ramp(15));
+    s->sig_i->label_grid_color(fl_gray_ramp(10));
+    s->sig_i->grid_visible(CA_LABEL_GRID | CA_ALWAYS_VISIBLE);
+    s->sig_i->minor_grid_style(FL_DOT);
+    s->sig_i->major_step(5);
+    s->sig_i->label_step(1);
+    s->sig_i->axis_color(FL_BLACK);
+    s->sig_i->axis_align(CA_BOTTOM | CA_LINE);
+
+    s->sig_q = new Ca_Y_Axis(20, 35, 40, 290, "Q");
+    s->sig_q->align(FL_ALIGN_LEFT);
+    s->sig_q->minimum(-constel_width);
+    s->sig_q->maximum(constel_width);
+    s->sig_q->minor_grid_color(fl_gray_ramp(20));
+    s->sig_q->major_grid_color(fl_gray_ramp(15));
+    s->sig_q->label_grid_color(fl_gray_ramp(10));
+    //s->sig_q->grid_visible(CA_MINOR_TICK | CA_MAJOR_TICK | CA_LABEL_GRID | CA_ALWAYS_VISIBLE);
+    s->sig_q->grid_visible(CA_LABEL_GRID | CA_ALWAYS_VISIBLE);
+    s->sig_q->minor_grid_style(FL_DOT);
+    s->sig_q->major_step(5);
+    s->sig_q->label_step(1);
+    s->sig_q->axis_color(FL_BLACK);
+
+    s->sig_q->current();
+
+    s->c_const->end();
+
+    s->c_right = new Fl_Group(440, 0, 465, 405);
+
+    s->c_eq = new Fl_Group(380, 0, 265, 200);
+    s->c_eq->box(FL_DOWN_BOX);
+    s->c_eq->align(FL_ALIGN_TOP | FL_ALIGN_INSIDE);
+    s->c_eq->current();
+    s->canvas_eq = new Ca_Canvas(460, 35, 150, 100, "Equalizer");
+    s->canvas_eq->box(FL_PLASTIC_DOWN_BOX);
+    s->canvas_eq->color(7);
+    s->canvas_eq->align(FL_ALIGN_TOP);
+    Fl_Group::current()->resizable(s->canvas_eq);
+    s->canvas_eq->border(15);
+
+    s->eq_x = new Ca_X_Axis(465, 135, 140, 30, "Symbol");
+    s->eq_x->align(FL_ALIGN_BOTTOM);
+    s->eq_x->minimum(-8.0);
+    s->eq_x->maximum(8.0);
+    s->eq_x->label_format("%g");
+    s->eq_x->minor_grid_color(fl_gray_ramp(20));
+    s->eq_x->major_grid_color(fl_gray_ramp(15));
+    s->eq_x->label_grid_color(fl_gray_ramp(10));
+    s->eq_x->grid_visible(CA_LABEL_GRID | CA_ALWAYS_VISIBLE);
+    s->eq_x->minor_grid_style(FL_DOT);
+    s->eq_x->major_step(5);
+    s->eq_x->label_step(1);
+    s->eq_x->axis_align(CA_BOTTOM | CA_LINE);
+    s->eq_x->axis_color(FL_BLACK);
+    s->eq_x->current();
+
+    s->eq_y = new Ca_Y_Axis(420, 40, 40, 90, "Amp");
+    s->eq_y->align(FL_ALIGN_LEFT);
+    s->eq_y->minimum(-0.1);
+    s->eq_y->maximum(0.1);
+    s->eq_y->minor_grid_color(fl_gray_ramp(20));
+    s->eq_y->major_grid_color(fl_gray_ramp(15));
+    s->eq_y->label_grid_color(fl_gray_ramp(10));
+    s->eq_y->grid_visible(CA_LABEL_GRID | CA_ALWAYS_VISIBLE);
+    s->eq_y->minor_grid_style(FL_DOT);
+    s->eq_y->major_step(5);
+    s->eq_y->label_step(1);
+    s->eq_y->axis_color(FL_BLACK);
+    s->eq_y->current();
+
+    s->c_eq->end();
+
+    s->c_symbol_track = new Fl_Group(380, 200, 525, 200);
+    s->c_symbol_track->box(FL_DOWN_BOX);
+    s->c_symbol_track->align(FL_ALIGN_TOP | FL_ALIGN_INSIDE);
+    s->c_symbol_track->current();
+
+    s->canvas_track = new Ca_Canvas(490, 235, 300, 100, "Symbol and carrier tracking");
+    s->canvas_track->box(FL_PLASTIC_DOWN_BOX);
+    s->canvas_track->color(7);
+    s->canvas_track->align(FL_ALIGN_TOP);
+    Fl_Group::current()->resizable(s->canvas_track);
+    s->canvas_track->border(15);
+
+    s->track_x = new Ca_X_Axis(495, 335, 290, 30, "Time (symbols)");
+    s->track_x->align(FL_ALIGN_BOTTOM);
+    s->track_x->minimum(0.0);
+    s->track_x->maximum(2400.0*5.0);
+    s->track_x->label_format("%g");
+    s->track_x->minor_grid_color(fl_gray_ramp(20));
+    s->track_x->major_grid_color(fl_gray_ramp(15));
+    s->track_x->label_grid_color(fl_gray_ramp(10));
+    s->track_x->grid_visible(CA_LABEL_GRID | CA_ALWAYS_VISIBLE);
+    s->track_x->minor_grid_style(FL_DOT);
+    s->track_x->major_step(5);
+    s->track_x->label_step(1);
+    s->track_x->axis_align(CA_BOTTOM | CA_LINE);
+    s->track_x->axis_color(FL_BLACK);
+    s->track_x->current();
+
+    s->symbol_track_y = new Ca_Y_Axis(420, 240, 70, 90, "Cor");
+    s->symbol_track_y->align(FL_ALIGN_LEFT);
+    s->symbol_track_y->minimum(-0.1);
+    s->symbol_track_y->maximum(0.1);
+    s->symbol_track_y->minor_grid_color(fl_gray_ramp(20));
+    s->symbol_track_y->major_grid_color(fl_gray_ramp(15));
+    s->symbol_track_y->label_grid_color(fl_gray_ramp(10));
+    s->symbol_track_y->grid_visible(CA_LABEL_GRID | CA_ALWAYS_VISIBLE);
+    s->symbol_track_y->minor_grid_style(FL_DOT);
+    s->symbol_track_y->major_step(5);
+    s->symbol_track_y->label_step(1);
+    s->symbol_track_y->axis_color(FL_RED);
+    s->symbol_track_y->current();
+
+    s->carrier_y = new Ca_Y_Axis(790, 240, 70, 90, "Freq");
+    s->carrier_y->align(FL_ALIGN_RIGHT);
+    s->carrier_y->minimum(-0.1);
+    s->carrier_y->maximum(0.1);
+    s->carrier_y->minor_grid_color(fl_gray_ramp(20));
+    s->carrier_y->major_grid_color(fl_gray_ramp(15));
+    s->carrier_y->label_grid_color(fl_gray_ramp(10));
+    s->carrier_y->grid_visible(CA_LABEL_GRID | CA_ALWAYS_VISIBLE);
+    s->carrier_y->minor_grid_style(FL_DOT);
+    s->carrier_y->major_step(5);
+    s->carrier_y->label_step(1);
+    s->carrier_y->axis_align(CA_RIGHT);
+    s->carrier_y->axis_color(FL_BLUE);
+    s->carrier_y->current();
+
+    s->c_symbol_track->end();
+
+    s->audio_meter = new Fl_Audio_Meter(672, 10, 16, 150, "");
+    s->audio_meter->box(FL_PLASTIC_UP_BOX);
+    s->audio_meter->type(FL_VERT_AUDIO_METER);
+
+    s->audio_level = new Fl_Output(650, 170, 60, 20, "");
+    s->audio_level->textsize(10);
+
+    s->c_right->end();
+
+    Fl_Group::current()->resizable(s->c_right);
+    s->w->end();
+    s->w->show();
+
+    s->next_constel_point = 0;
+    s->constel_window = 10000;
+
+    s->carrier_points = 0;
+    s->carrier_ptr = 0;
+    s->carrier_window = 100;
+
+    s->symbol_track_points = 0;
+    s->symbol_track_window = 10000;
+    Fl::check();
+    return s;
+}
+/*- End of function --------------------------------------------------------*/
+
+void qam_wait_to_end(qam_monitor_t *s) 
+{
+    fd_set rfds;
+    int res;
+    struct timeval tv;
+
+    fprintf(stderr, "Processing complete.  Press the <enter> key to end\n");
+    do
+    {
+        usleep(100000);
+        Fl::check();
+        FD_ZERO(&rfds);
+        FD_SET(0, &rfds);
+        tv.tv_usec = 100000;
+        tv.tv_sec = 0;
+        res = select(1, &rfds, NULL, NULL, &tv);
+    }
+    while (res <= 0);
+}
+/*- End of function --------------------------------------------------------*/
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/tests/modem_monitor.h
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/tests/modem_monitor.h	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,69 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * constel.h - Display QAM constellations, using the FLTK toolkit.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2004 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: modem_monitor.h,v 1.15 2008/04/26 13:39:17 steveu Exp $
+ */
+
+/*! \page constel_page Modem performance monitoring
+\section constel_page_sec_1 What does it do?
+This code controls a GUI window, which provides monitoring of the internal status
+of a modem. It shows, graphically:
+
+    - the constellation, for PSK, QAM and other similar modulations.
+    - the equalizer status, for modems with adaptive equalizers.
+    - the carrier frequency.
+    - the symbol timing correction.
+
+\section constel_page_sec_2 How does it work?
+This code uses the FLTK cross platform GUI toolkit. It works on X11 and Windows platforms.
+In addition to the basic FLTK toolkit, fltk_cartesian is also required.
+*/
+
+#if !defined(_MODEM_MONITOR_H_)
+#define _MODEM_MONITOR_H_
+
+struct qam_monitor_s;
+
+typedef struct qam_monitor_s qam_monitor_t;
+
+#if defined(__cplusplus)
+extern "C"
+{
+#endif
+
+qam_monitor_t *qam_monitor_init(float constel_width, const char *tag);
+int qam_monitor_clear_constel(qam_monitor_t *s);
+int qam_monitor_update_constel(qam_monitor_t *s, const complexf_t *pt);
+int qam_monitor_update_equalizer(qam_monitor_t *s, const complexf_t *coeffs, int len);
+int qam_monitor_update_symbol_tracking(qam_monitor_t *s, float total_correction);
+int qam_monitor_update_carrier_tracking(qam_monitor_t *s, float carrier);
+int qam_monitor_update_audio_level(qam_monitor_t *s, const int16_t amp[], int len);
+void qam_wait_to_end(qam_monitor_t *s);
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/tests/noise_tests.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/tests/noise_tests.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,240 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * noise_tests.c
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2005 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: noise_tests.c,v 1.16 2008/08/17 14:21:26 steveu Exp $
+ */
+
+/*! \page noise_tests_page Noise generator tests
+\section noise_tests_page_sec_1 What does it do?
+*/
+
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <audiofile.h>
+
+#include "spandsp.h"
+#include "spandsp-sim.h"
+
+#if !defined(M_PI)
+# define M_PI           3.14159265358979323846  /* pi */
+#endif
+
+#define OUT_FILE_NAME   "noise.wav"
+
+/* Some simple sanity tests for the noise generation routines */
+
+int main (int argc, char *argv[])
+{
+    int i;
+    int j;
+    int level;
+    int clip_high;
+    int clip_low;
+    int quality;
+    int total_samples;
+    int seed = 1234567;
+    int outframes;
+    int16_t value;
+    double total;
+    double x;
+    double p;
+    double o;
+    int bins[65536];
+    int16_t amp[1024];
+    noise_state_t noise_source;
+    AFfilehandle outhandle;
+
+    if ((outhandle = afOpenFile_telephony_write(OUT_FILE_NAME, 1)) == AF_NULL_FILEHANDLE)
+    {
+        fprintf(stderr, "    Cannot create wave file '%s'\n", OUT_FILE_NAME);
+        exit(2);
+    }
+
+    for (quality = 7;  quality <= 20;  quality += (20 - 7))
+    {
+        /* Generate AWGN at several RMS levels between -50dBOv and 0dBOv. Noise is
+           generated for a large number of samples (1,000,000), and the RMS value
+           of the noise is calculated along the way. If the resulting level is
+           close to the requested RMS level, at least the scaling of the noise
+           should be Ok. At high levels some clipping may distort the result a
+           little. */
+        printf("Testing AWGN power, with quality %d\n", quality);
+        for (level = -50;  level <= 0;  level += 5)
+        {
+            clip_high = 0;
+            clip_low = 0;
+            total = 0.0;
+            noise_init_dbov(&noise_source, seed, (float) level, NOISE_CLASS_AWGN, quality);
+            total_samples = 1000000;
+            for (i = 0;  i < total_samples;  i++)
+            {
+                value = noise(&noise_source);
+                if (value == 32767)
+                    clip_high++;
+                else if (value == -32768)
+                    clip_low++;
+                total += ((double) value)*((double) value);
+            }
+            printf ("RMS = %.3f (expected %d) %.2f%% error [clipped samples %d+%d]\n",
+                    log10(sqrt(total/total_samples)/32768.0)*20.0,
+                    level,
+                    100.0*(1.0 - sqrt(total/total_samples)/(pow(10.0, level/20.0)*32768.0)),
+                    clip_low,
+                    clip_high);
+            if (level < -5  &&  fabs(log10(sqrt(total/total_samples)/32768.0)*20.0 - level) > 0.2)
+            {
+                printf("Test failed\n");
+                exit(2);
+            }
+        }
+    }
+
+    /* Now look at the statistical spread of the results, by collecting data in
+       bins from a large number of samples. Use a fairly high noise level, but
+       low enough to avoid significant clipping. Use the Gaussian model to
+       predict the real probability, and present the results for graphing. */
+    quality = 7;
+    printf("Testing the statistical spread of AWGN, with quality %d\n", quality);
+    memset(bins, 0, sizeof(bins));
+    clip_high = 0;
+    clip_low = 0;
+    level = -15;
+    noise_init_dbov(&noise_source, seed, (float) level, NOISE_CLASS_AWGN, quality);
+    total_samples = 10000000;
+    for (i = 0;  i < total_samples;  i++)
+    {
+        value = noise(&noise_source);
+        if (value == 32767)
+            clip_high++;
+        else if (value == -32768)
+            clip_low++;
+        bins[value + 32768]++;
+    }
+    /* Find the RMS power level to expect */
+    o = pow(10.0, level/20.0)*(32768.0*0.70711);
+    for (i = 0;  i < 65536 - 10;  i++)
+    {
+        x = i - 32768;
+        /* Find the real probability for this bin */
+        p = (1.0/(o*sqrt(2.0*M_PI)))*exp(-(x*x)/(2.0*o*o));
+        /* Now do a little smoothing on the real data to get a reasonably
+           steady answer */
+        x = 0;
+        for (j = 0;  j < 10;  j++)
+            x += bins[i + j];
+        x /= 10.0;
+        x /= total_samples;
+        /* Now send it out for graphing. */
+        if (p > 0.0000001)
+            printf("%6d %.7f %.7f\n", i - 32768, x, p);
+    }
+
+    printf("Generating AWGN at -15dBOv to file\n");
+    for (j = 0;  j < 50;  j++)
+    {
+        for (i = 0;  i < 1024;  i++)
+            amp[i] = noise(&noise_source);
+        outframes = afWriteFrames(outhandle,
+                                  AF_DEFAULT_TRACK,
+                                  amp,
+                                  1024);
+        if (outframes != 1024)
+        {
+            fprintf(stderr, "    Error writing wave file\n");
+            exit(2);
+        }
+    }
+
+    /* Generate Hoth noise at several RMS levels between -50dBm and 0dBm. Noise
+       is generated for a large number of samples (1,000,000), and the RMS value
+       of the noise is calculated along the way. If the resulting level is
+       close to the requested RMS level, at least the scaling of the noise
+       should be Ok. At high levels some clipping may distort the result a
+       little. */
+    quality = 7;
+    printf("Testing Hoth noise power, with quality %d\n", quality);
+    for (level = -50;  level <= 0;  level += 5)
+    {
+        clip_high = 0;
+        clip_low = 0;
+        total = 0.0;
+        noise_init_dbov(&noise_source, seed, (float) level, NOISE_CLASS_HOTH, quality);
+        total_samples = 1000000;
+        for (i = 0;  i < total_samples;  i++)
+        {
+            value = noise(&noise_source);
+            if (value == 32767)
+                clip_high++;
+            else if (value == -32768)
+                clip_low++;
+            total += ((double) value)*((double) value);
+        }
+        printf ("RMS = %.3f (expected %d) %.2f%% error [clipped samples %d+%d]\n",
+                log10(sqrt(total/total_samples)/32768.0)*20.0,
+                level,
+                100.0*(1.0 - sqrt(total/total_samples)/(pow(10.0, level/20.0)*32768.0)),
+                clip_low,
+                clip_high);
+        if (level < -5  &&  fabs(log10(sqrt(total/total_samples)/32768.0)*20.0 - level) > 0.2)
+        {
+            printf("Test failed\n");
+            exit(2);
+        }
+    }
+    
+    quality = 7;
+    printf("Generating Hoth noise at -15dBOv to file\n");
+    level = -15;
+    noise_init_dbov(&noise_source, seed, (float) level, NOISE_CLASS_HOTH, quality);
+    for (j = 0;  j < 50;  j++)
+    {
+        for (i = 0;  i < 1024;  i++)
+            amp[i] = noise(&noise_source);
+        outframes = afWriteFrames(outhandle,
+                                  AF_DEFAULT_TRACK,
+                                  amp,
+                                  1024);
+        if (outframes != 1024)
+        {
+            fprintf(stderr, "    Error writing wave file\n");
+            exit(2);
+        }
+    }
+
+    if (afCloseFile(outhandle))
+    {
+        fprintf(stderr, "    Cannot close wave file '%s'\n", OUT_FILE_NAME);
+        exit(2);
+    }
+    
+    printf("Tests passed.\n");
+    return  0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/tests/oki_adpcm_tests.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/tests/oki_adpcm_tests.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,222 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * oki_adpcm_tests.c - Test the Oki (Dialogic) ADPCM encode and decode
+ *                     software at 24kbps and 32kbps.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2004 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: oki_adpcm_tests.c,v 1.34 2008/08/29 09:28:13 steveu Exp $
+ */
+
+/*! \file */
+
+/*! \page oki_adpcm_tests_page OKI (Dialogic) ADPCM tests
+\section oki_adpcm_tests_page_sec_1 What does it do?
+To perform a general audio quality test, oki_adpcm_tests should be run. The test file
+../test-data/local/short_nb_voice.wav will be compressed to the specified bit rate,
+decompressed, and the resulting audio stored in post_oki_adpcm.wav. A simple SNR test
+is automatically performed. Listening tests may be used for a more detailed evaluation
+of the degradation in quality caused by the compression. Both 32k bps and 24k bps
+compression may be tested.
+
+\section oki_adpcm_tests_page_sec_2 How is it used?
+*/
+
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <string.h>
+#include <time.h>
+#include <audiofile.h>
+
+#include "spandsp.h"
+#include "spandsp-sim.h"
+
+#define IN_FILE_NAME    "../test-data/local/short_nb_voice.wav"
+#define OUT_FILE_NAME   "post_oki_adpcm.wav"
+
+#define HIST_LEN        1000
+
+int main(int argc, char *argv[])
+{
+    int i;
+    AFfilehandle inhandle;
+    AFfilehandle outhandle;
+    int frames;
+    int dec_frames;
+    int outframes;
+    int oki_bytes;
+    int bit_rate;
+    float x;
+    double pre_energy;
+    double post_energy;
+    double diff_energy;
+    int16_t pre_amp[HIST_LEN];
+    int16_t post_amp[HIST_LEN];
+    uint8_t oki_data[HIST_LEN];
+    int16_t history[HIST_LEN];
+    int hist_in;
+    int hist_out;
+    oki_adpcm_state_t *oki_enc_state;
+    oki_adpcm_state_t *oki_dec_state;
+    int xx;
+    int total_pre_samples;
+    int total_compressed_bytes;
+    int total_post_samples;
+    const char *in_file_name;
+    int log_encoded_data;
+    int opt;
+
+    bit_rate = 32000;
+    in_file_name = IN_FILE_NAME;
+    log_encoded_data = FALSE;
+    while ((opt = getopt(argc, argv, "2i:l")) != -1)
+    {
+        switch (opt)
+        {
+        case '2':
+            bit_rate = 24000;
+            break;
+        case 'i':
+            in_file_name = optarg;
+            break;
+        case 'l':
+            log_encoded_data = TRUE;
+            break;
+        default:
+            //usage();
+            exit(2);
+            break;
+        }
+    }
+
+    if ((inhandle = afOpenFile_telephony_read(in_file_name, 1)) == AF_NULL_FILEHANDLE)
+    {
+        fprintf(stderr, "    Cannot open wave file '%s'\n", in_file_name);
+        exit(2);
+    }
+    if ((outhandle = afOpenFile_telephony_write(OUT_FILE_NAME, 1)) == AF_NULL_FILEHANDLE)
+    {
+        fprintf(stderr, "    Cannot create wave file '%s'\n", OUT_FILE_NAME);
+        exit(2);
+    }
+
+    if ((oki_enc_state = oki_adpcm_init(NULL, bit_rate)) == NULL)
+    {
+        fprintf(stderr, "    Cannot create encoder\n");
+        exit(2);
+    }
+        
+    if ((oki_dec_state = oki_adpcm_init(NULL, bit_rate)) == NULL)
+    {
+        fprintf(stderr, "    Cannot create decoder\n");
+        exit(2);
+    }
+
+    hist_in = 0;
+    if (bit_rate == 32000)
+        hist_out = 0;
+    else
+        hist_out = HIST_LEN - 27;
+    memset(history, 0, sizeof(history));
+    pre_energy = 0.0;
+    post_energy = 0.0;
+    diff_energy = 0.0;
+    total_pre_samples = 0;
+    total_compressed_bytes = 0;
+    total_post_samples = 0;
+    while ((frames = afReadFrames(inhandle, AF_DEFAULT_TRACK, pre_amp, 159)))
+    {
+        total_pre_samples += frames;
+        oki_bytes = oki_adpcm_encode(oki_enc_state, oki_data, pre_amp, frames);
+        if (log_encoded_data)
+            write(1, oki_data, oki_bytes);
+        total_compressed_bytes += oki_bytes;
+        dec_frames = oki_adpcm_decode(oki_dec_state, post_amp, oki_data, oki_bytes);
+        total_post_samples += dec_frames;
+        for (i = 0;  i < frames;  i++)
+        {
+            history[hist_in++] = pre_amp[i];
+            if (hist_in >= HIST_LEN)
+                hist_in = 0;
+            pre_energy += (double) pre_amp[i] * (double) pre_amp[i];
+        }
+        for (i = 0;  i < dec_frames;  i++)
+        {
+            post_energy += (double) post_amp[i] * (double) post_amp[i];
+            xx = post_amp[i] - history[hist_out++];
+            if (hist_out >= HIST_LEN)
+                hist_out = 0;
+            diff_energy += (double) xx * (double) xx;
+            //post_amp[i] = xx;
+        }
+        outframes = afWriteFrames(outhandle, AF_DEFAULT_TRACK, post_amp, dec_frames);
+    }
+    if (afCloseFile(inhandle) != 0)
+    {
+        fprintf(stderr, "    Cannot close wave file '%s'\n", in_file_name);
+        exit(2);
+    }
+    if (afCloseFile(outhandle) != 0)
+    {
+        fprintf(stderr, "    Cannot close wave file '%s'\n", OUT_FILE_NAME);
+        exit(2);
+    }
+    oki_adpcm_release(oki_enc_state);
+    oki_adpcm_release(oki_dec_state);
+
+    printf("Pre samples: %d\n", total_pre_samples);
+    printf("Compressed bytes: %d\n", total_compressed_bytes);
+    printf("Post samples: %d\n", total_post_samples);
+
+    printf("Output energy is %f%% of input energy.\n", 100.0*post_energy/pre_energy);
+    printf("Residual energy is %f%% of the total.\n", 100.0*diff_energy/post_energy);
+    if (bit_rate == 32000)
+    {
+        if (fabs(1.0 - post_energy/pre_energy) > 0.05
+            ||
+            fabs(diff_energy/post_energy) > 0.03)
+        {
+            printf("Tests failed.\n");
+            exit(2);
+        }
+    }
+    else
+    {
+        if (fabs(1.0 - post_energy/pre_energy) > 0.20
+            ||
+            fabs(diff_energy/post_energy) > 0.10)
+        {
+            printf("Tests failed.\n");
+            exit(2);
+        }
+    }
+
+    printf("Tests passed.\n");
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/tests/playout_tests.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/tests/playout_tests.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,355 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * playout_tests.c
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2005 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: playout_tests.c,v 1.26 2008/07/28 15:14:30 steveu Exp $
+ */
+
+/*! \page playout_tests_page Playout (jitter buffering) tests
+\section playout_tests_page_sec_1 What does it do?
+These tests simulate timing jitter and packet loss in an audio stream, and see
+how well the playout module copes.
+*/
+
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <audiofile.h>
+
+#include "spandsp.h"
+
+#define INPUT_FILE_NAME     "playout_in.wav"
+#define OUTPUT_FILE_NAME    "playout_out.wav"
+
+#define BLOCK_LEN           160
+
+static void dynamic_buffer_tests(void)
+{
+    playout_state_t *s;
+    playout_frame_t frame;
+    playout_frame_t *p;
+    plc_state_t plc;
+    time_scale_state_t ts;
+    int16_t *amp;
+    int16_t fill[BLOCK_LEN];
+    int16_t buf[20*BLOCK_LEN];
+    int16_t out[10*BLOCK_LEN];
+    timestamp_t time_stamp;
+    timestamp_t next_actual_receive;
+    timestamp_t next_scheduled_receive;
+    int near_far_time_offset;
+    int rng;
+    int i;
+    int j;
+    int ret;
+    int len;
+    int inframes;
+    int outframes;
+    AFfilehandle inhandle;
+    AFfilehandle outhandle;
+    AFfilesetup filesetup;
+
+    filesetup = afNewFileSetup();
+    if (filesetup == AF_NULL_FILESETUP)
+    {
+        fprintf(stderr, "    Failed to create file setup\n");
+        exit(2);
+    }
+    afInitSampleFormat(filesetup, AF_DEFAULT_TRACK, AF_SAMPFMT_TWOSCOMP, 16);
+    afInitRate(filesetup, AF_DEFAULT_TRACK, (float) SAMPLE_RATE);
+    afInitFileFormat(filesetup, AF_FILE_WAVE);
+    afInitChannels(filesetup, AF_DEFAULT_TRACK, 2);
+
+    inhandle = afOpenFile(INPUT_FILE_NAME, "r", NULL);
+    if (inhandle == AF_NULL_FILEHANDLE)
+    {
+        fprintf(stderr, "    Failed to open wave file '%s'\n", INPUT_FILE_NAME);
+        exit(2);
+    }
+    outhandle = afOpenFile(OUTPUT_FILE_NAME, "w", filesetup);
+    if (outhandle == AF_NULL_FILEHANDLE)
+    {
+        fprintf(stderr, "    Failed to create wave file '%s'\n", OUTPUT_FILE_NAME);
+        exit(2);
+    }
+
+    near_far_time_offset = 54321;
+    time_stamp = 12345;
+    next_actual_receive = time_stamp + near_far_time_offset;
+    next_scheduled_receive = 0;
+    for (i = 0;  i < BLOCK_LEN;  i++)
+        fill[i] = 32767;
+
+    if ((s = playout_new(2*BLOCK_LEN, 15*BLOCK_LEN)) == NULL)
+        return;
+    plc_init(&plc);
+    time_scale_init(&ts, SAMPLE_RATE, 1.0);
+    for (i = 0;  i < 1000000;  i++)
+    {
+        if (i >= next_actual_receive)
+        {
+            amp = malloc(BLOCK_LEN*sizeof(int16_t));
+            inframes = afReadFrames(inhandle,
+                                    AF_DEFAULT_TRACK,
+                                    amp,
+                                    BLOCK_LEN);
+            if (inframes < BLOCK_LEN)
+                break;
+            ret = playout_put(s,
+                              amp,
+                              PLAYOUT_TYPE_SPEECH,
+                              inframes,
+                              time_stamp,
+                              next_actual_receive);
+#if 0
+            switch (ret)
+            {
+            case PLAYOUT_OK:
+                printf("<< Record\n");
+                break;
+            case PLAYOUT_ERROR:
+                printf("<< Error\n");
+                break;
+            default:
+                printf("<< Eh?\n");
+                break;
+            }
+#endif
+            rng = rand() & 0xFF;
+            if (i < 100000)
+                rng = (rng*rng) >> 7;
+            else if (i < 200000)
+                rng = (rng*rng) >> 6;
+            else if (i < 300000)
+                rng = (rng*rng) >> 5;
+            else if (i < 400000)
+                rng = (rng*rng) >> 7;
+            time_stamp += BLOCK_LEN;
+            next_actual_receive = time_stamp + near_far_time_offset + rng;
+        }
+        if (i >= next_scheduled_receive)
+        {
+            do
+            {
+                ret = playout_get(s, &frame, next_scheduled_receive);
+                if (ret == PLAYOUT_DROP)
+                    printf(">> Drop %d\n", next_scheduled_receive);
+            }
+            while (ret == PLAYOUT_DROP);
+            switch (ret)
+            {
+            case PLAYOUT_OK:
+                printf(">> Play %d\n", next_scheduled_receive);
+                plc_rx(&plc, frame.data, frame.sender_len);
+                len = time_scale(&ts, out, ((int16_t *) frame.data), frame.sender_len);
+printf("len = %d\n", len);
+                for (j = 0;  j < len;  j++)
+                {
+                    buf[2*j] = out[j];
+                    buf[2*j + 1] = 10*playout_current_length(s);
+                }
+                outframes = afWriteFrames(outhandle, AF_DEFAULT_TRACK, buf, len);
+                if (outframes != len)
+                {
+                    fprintf(stderr, "    Error writing out sound\n");
+                    exit(2);
+                }
+                free(frame.data);
+                next_scheduled_receive += BLOCK_LEN;
+                break;
+            case PLAYOUT_FILLIN:
+                printf(">> Fill %d\n", next_scheduled_receive);
+                plc_fillin(&plc, fill, BLOCK_LEN);
+                time_scale_rate(&ts, 0.5);
+                len = time_scale(&ts, out, fill, BLOCK_LEN);
+                time_scale_rate(&ts, 1.0);
+printf("len = %d\n", len);
+                for (j = 0;  j < len;  j++)
+                {
+                    buf[2*j] = out[j];
+                    buf[2*j + 1] = 10*playout_current_length(s);
+                }
+                outframes = afWriteFrames(outhandle, AF_DEFAULT_TRACK, buf, len);
+                if (outframes != len)
+                {
+                    fprintf(stderr, "    Error writing out sound\n");
+                    exit(2);
+                }
+                next_scheduled_receive += BLOCK_LEN;
+                break;
+            case PLAYOUT_DROP:
+                printf(">> Drop %d\n", next_scheduled_receive);
+                break;
+            case PLAYOUT_NOFRAME:
+                printf(">> No frame %d %d %d %d\n", next_scheduled_receive, playout_next_due(s), s->last_speech_sender_stamp, s->last_speech_sender_len);
+                next_scheduled_receive += BLOCK_LEN;
+                break;
+            case PLAYOUT_EMPTY:
+                printf(">> Empty %d\n", next_scheduled_receive);
+                next_scheduled_receive += BLOCK_LEN;
+                break;
+            case PLAYOUT_ERROR:
+                printf(">> Error %d\n", next_scheduled_receive);
+                next_scheduled_receive += BLOCK_LEN;
+                break;
+            default:
+                printf(">> Eh? %d\n", next_scheduled_receive);
+                break;
+            }
+        }
+    }
+    if (afCloseFile(inhandle) != 0)
+    {
+        fprintf(stderr, "    Cannot close wave file '%s'\n", INPUT_FILE_NAME);
+        exit(2);
+    }
+    if (afCloseFile(outhandle) != 0)
+    {
+        fprintf(stderr, "    Cannot close wave file '%s'\n", OUTPUT_FILE_NAME);
+        exit(2);
+    }
+    afFreeFileSetup(filesetup);
+
+    printf("%10" PRId32 " %10" PRId32 " %10d\n", s->state_just_in_time, s->state_late, playout_current_length(s));
+
+    /* Clear everything from the queue */
+    while ((p = playout_get_unconditional(s)))
+        /*free(p->data)*/;
+    /* Now free the context itself */
+    playout_free(s);
+}
+/*- End of function --------------------------------------------------------*/
+
+static void static_buffer_tests(void)
+{
+    playout_state_t *s;
+    playout_frame_t frame;
+    playout_frame_t *p;
+    int type;
+    uint8_t fr[BLOCK_LEN];
+    timestamp_t next_scheduled_send;
+    int transit_time;
+    timestamp_t next_actual_receive;
+    timestamp_t next_scheduled_receive;
+    int len;
+    int i;
+    int ret;
+
+    next_scheduled_send = 0;
+    transit_time = 320;
+    next_actual_receive = next_scheduled_send + transit_time;
+    next_scheduled_receive = 960;
+
+    memset(fr, 0, sizeof(fr));
+    type = PLAYOUT_TYPE_SPEECH;
+    len = BLOCK_LEN;
+
+    if ((s = playout_new(2*BLOCK_LEN, 2*BLOCK_LEN)) == NULL)
+        return;
+    for (i = 0;  i < 1000000;  i++)
+    {
+        if (i >= next_actual_receive)
+        {
+            ret = playout_put(s,
+                              fr,
+                              type,
+                              len,
+                              next_scheduled_send, 
+                              next_actual_receive);
+            switch (ret)
+            {
+            case PLAYOUT_OK:
+                printf("<< Record\n");
+                break;
+            case PLAYOUT_ERROR:
+                printf("<< Error\n");
+                break;
+            default:
+                printf("<< Eh?\n");
+                break;
+            }
+            next_scheduled_send += BLOCK_LEN;
+            ret = rand() & 0xFF;
+            ret = (ret*ret) >> 7;
+            transit_time = 320 + ret;
+            next_actual_receive = next_scheduled_send + transit_time;
+        }
+        if (i >= next_scheduled_receive)
+        {
+            do
+            {
+                ret = playout_get(s, &frame, next_scheduled_receive);
+            }
+            while (ret == PLAYOUT_DROP);
+            switch (ret)
+            {
+            case PLAYOUT_OK:
+                printf(">> Play\n");
+                next_scheduled_receive += BLOCK_LEN;
+                break;
+            case PLAYOUT_FILLIN:
+                printf(">> Fill\n");
+                next_scheduled_receive += BLOCK_LEN;
+                break;
+            case PLAYOUT_DROP:
+                printf(">> Drop\n");
+                break;
+            case PLAYOUT_NOFRAME:
+                printf(">> No frame\n");
+                next_scheduled_receive += BLOCK_LEN;
+                break;
+            case PLAYOUT_EMPTY:
+                printf(">> Empty\n");
+                next_scheduled_receive += BLOCK_LEN;
+                break;
+            case PLAYOUT_ERROR:
+                printf(">> Error\n");
+                next_scheduled_receive += BLOCK_LEN;
+                break;
+            default:
+                printf(">> Eh?\n");
+                break;
+            }
+        }
+    }
+    /* Clear everything from the queue */
+    while ((p = playout_get_unconditional(s)))
+        /*free(p->data)*/;
+    /* Now free the context itself */
+    playout_free(s);
+}
+/*- End of function --------------------------------------------------------*/
+
+int main(int argc, char *argv[])
+{
+    printf("Dynamic buffering tests\n");
+    dynamic_buffer_tests();
+    printf("Static buffering tests\n");
+    static_buffer_tests();
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/tests/plc_tests.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/tests/plc_tests.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,201 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * plc_tests.c
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2004 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: plc_tests.c,v 1.25 2008/05/13 13:17:26 steveu Exp $
+ */
+
+/*! \page plc_tests_page Packet loss concealment tests
+\section plc_tests_page_sec_1 What does it do?
+These tests run a speech file through the packet loss concealment routines.
+The loss rate, in percent, and the packet size, in samples, may be specified
+on the command line.
+
+\section plc_tests_page_sec_2 How are the tests run?
+These tests process a speech file called pre_plc.wav. This file should contain
+8000 sample/second 16 bits/sample linear audio. The tests read this file in
+blocks, of a size specified on the command line. Some of these blocks are
+dropped, to simulate packet loss. The rate of loss is also specified on the
+command line. The PLC module is then used to reconstruct an acceptable
+approximation to the original signal. The resulting audio is written to a new
+audio file, called post_plc.wav. This file contains 8000 sample/second
+16 bits/sample linear audio.
+*/
+
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <string.h>
+
+#include <audiofile.h>
+
+#include "spandsp.h"
+
+#define INPUT_FILE_NAME     "../test-data/local/short_nb_voice.wav"
+#define OUTPUT_FILE_NAME    "post_plc.wav"
+
+int main(int argc, char *argv[])
+{
+    AFfilehandle inhandle;
+    AFfilehandle outhandle;
+    AFfilesetup filesetup;
+    plc_state_t plc;
+    int inframes;
+    int outframes;
+    int16_t amp[1024];
+    int block_no;
+    int lost_blocks;
+    int block_len;
+    int loss_rate;
+    int dropit;
+    int block_real;
+    int block_synthetic;
+    int tone;
+    int i;
+    uint32_t phase_acc;
+    int32_t phase_rate;
+    int opt;
+
+    loss_rate = 25;
+    block_len = 160;
+    block_real = FALSE;
+    block_synthetic = FALSE;
+    tone = -1;
+    while ((opt = getopt(argc, argv, "b:l:rst:")) != -1)
+    {
+        switch (opt)
+        {
+        case 'b':
+            block_len = atoi(optarg);
+            break;
+        case 'l':
+            loss_rate = atoi(optarg);
+            break;
+        case 'r':
+            block_real = TRUE;
+            break;
+        case 's':
+            block_synthetic = TRUE;
+            break;
+        case 't':
+            tone = atoi(optarg);
+            break;
+        }
+    }
+    if ((filesetup = afNewFileSetup()) == AF_NULL_FILESETUP)
+    {
+        fprintf(stderr, "    Failed to create file setup\n");
+        exit(2);
+    }
+    afInitSampleFormat(filesetup, AF_DEFAULT_TRACK, AF_SAMPFMT_TWOSCOMP, 16);
+    afInitRate(filesetup, AF_DEFAULT_TRACK, (float) SAMPLE_RATE);
+    afInitFileFormat(filesetup, AF_FILE_WAVE);
+    afInitChannels(filesetup, AF_DEFAULT_TRACK, 1);
+
+    phase_rate = 0;
+    inhandle = NULL;
+    if (tone < 0)
+    {
+        if ((inhandle = afOpenFile(INPUT_FILE_NAME, "r", NULL)) == AF_NULL_FILEHANDLE)
+        {
+            fprintf(stderr, "    Failed to open wave file '%s'\n", INPUT_FILE_NAME);
+            exit(2);
+        }
+    }
+    else
+    {
+        phase_rate = dds_phase_ratef((float) tone);
+    }
+    if ((outhandle = afOpenFile(OUTPUT_FILE_NAME, "w", filesetup)) == AF_NULL_FILEHANDLE)
+    {
+        fprintf(stderr, "    Failed to open wave file '%s'\n", OUTPUT_FILE_NAME);
+        exit(2);
+    }
+    plc_init(&plc);
+    lost_blocks = 0;
+    for (block_no = 0;  ;  block_no++)
+    {
+        if (tone < 0)
+        {
+            inframes = afReadFrames(inhandle,
+                                    AF_DEFAULT_TRACK,
+                                    amp,
+                                    block_len);
+            if (inframes != block_len)
+                break;
+        }
+        else
+        {
+            if (block_no > 10000)
+                break;
+            for (i = 0;  i < block_len;  i++)
+                amp[i] = (int16_t) dds_modf(&phase_acc, phase_rate, 10000.0, 0);
+            inframes = block_len;
+        }
+        dropit = rand()/(RAND_MAX/100);
+        if (dropit > loss_rate)
+        {
+            plc_rx(&plc, amp, inframes);
+            if (block_real)
+                memset(amp, 0, sizeof(int16_t)*inframes);
+        }
+        else
+        {
+            lost_blocks++;
+            plc_fillin(&plc, amp, inframes);
+            if (block_synthetic)
+                memset(amp, 0, sizeof(int16_t)*inframes);
+        }
+        outframes = afWriteFrames(outhandle,
+                                  AF_DEFAULT_TRACK,
+                                  amp,
+                                  inframes);
+        if (outframes != inframes)
+        {
+            fprintf(stderr, "    Error writing out sound\n");
+            exit(2);
+        }
+    }
+    printf("Dropped %d of %d blocks\n", lost_blocks, block_no);
+    if (tone < 0)
+    {
+        if (afCloseFile(inhandle) != 0)
+        {
+            fprintf(stderr, "    Cannot close wave file '%s'\n", INPUT_FILE_NAME);
+            exit(2);
+        }
+    }
+    if (afCloseFile(outhandle) != 0)
+    {
+        fprintf(stderr, "    Cannot close wave file '%s'\n", OUTPUT_FILE_NAME);
+        exit(2);
+    }
+    afFreeFileSetup(filesetup);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/tests/power_meter_tests.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/tests/power_meter_tests.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,162 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * power_meter_tests.c
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2003 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: power_meter_tests.c,v 1.21 2008/05/13 13:17:26 steveu Exp $
+ */
+
+/*! \page power_meter_tests_page Power meter tests
+\section power_meter_tests_page_sec_1 What does it do?
+These tests assess the accuracy of power meters built from the power meter module.
+Both tones and noise are used to check the meter's behaviour.
+
+\section power_meter_tests_page_sec_2 How does it work?
+???.
+*/
+
+#if defined(HAVE_CONFIG_H)
+#include <config.h>
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <memory.h>
+#include <time.h>
+
+#include "spandsp.h"
+
+int main(int argc, char *argv[])
+{
+    awgn_state_t noise_source;
+    power_meter_t meter;
+    tone_gen_descriptor_t tone_desc;
+    tone_gen_state_t gen;
+    int i;
+    int idum = 1234567;
+    int16_t amp[1000];
+    int len;
+    int32_t level;
+
+    power_meter_init(&meter, 7);
+
+    printf("Testing with a square wave 10dB from maximum\n");
+    for (i = 0;  i < 1000;  i++)
+    {
+        amp[i] = (i & 1)  ?  10362  :  -10362;
+        level = power_meter_update(&meter, amp[i]);
+        //printf("%12d %fdBm0 %fdBov\n", level, power_meter_current_dbm0(&meter), power_meter_current_dbov(&meter));
+    }
+    printf("Level: expected %" PRId32 "/%" PRId32 ", got %" PRId32 "\n", power_meter_level_dbov(-10.0f), power_meter_level_dbm0(-10.0f + DBM0_MAX_POWER), level);
+    printf("Power: expected %fdBm0, got %fdBm0\n", -10.0f + DBM0_MAX_POWER, power_meter_current_dbm0(&meter));
+    printf("Power: expected %fdBOv, got %fdBOv\n", -10.0f, power_meter_current_dbov(&meter));
+    if (level < power_meter_level_dbov(-10.0f)*0.99f
+        ||
+        level > power_meter_level_dbov(-10.0f)*1.01f)
+    {
+        printf("Test failed (level)\n");
+        exit(2);
+    }
+    if (0.1f < fabsf(power_meter_current_dbm0(&meter) + 10.0f - DBM0_MAX_POWER))
+    {
+        printf("Test failed (dBm0)\n");
+        exit(2);
+    }
+    if (0.1f < fabsf(power_meter_current_dbov(&meter) + 10.0))
+    {
+        printf("Test failed (dBOv)\n");
+        exit(2);
+    }
+
+    printf("Testing with a sine wave tone 10dB from maximum\n");
+    make_tone_gen_descriptor(&tone_desc,
+                             1000,
+                             -4,
+                             0,
+                             1,
+                             1,
+                             0,
+                             0,
+                             0,
+                             TRUE);
+    tone_gen_init(&gen, &tone_desc);
+    len = tone_gen(&gen, amp, 1000);
+    for (i = 0;  i < len;  i++)
+    {
+        level = power_meter_update(&meter, amp[i]);
+        //printf("%12d %fdBm0 %fdBov\n", level, power_meter_current_dbm0(&meter), power_meter_current_dbov(&meter));
+    }
+    printf("Level: expected %" PRId32 "/%" PRId32 ", got %" PRId32 "\n", power_meter_level_dbov(-10.0f), power_meter_level_dbm0(-10.0f + DBM0_MAX_POWER), level);
+    printf("Power: expected %fdBm0, got %fdBm0\n", -10.0f + DBM0_MAX_POWER, power_meter_current_dbm0(&meter));
+    printf("Power: expected %fdBOv, got %fdBOv\n", -10.0f, power_meter_current_dbov(&meter));
+    if (level < power_meter_level_dbov(-10.0f)*0.95f
+        ||
+        level > power_meter_level_dbov(-10.0f)*1.05f)
+    {
+        printf("Test failed (level)\n");
+        exit(2);
+    }
+    if (0.2f < fabsf(power_meter_current_dbm0(&meter) + 10.0f - DBM0_MAX_POWER))
+    {
+        printf("Test failed (dBm0)\n");
+        exit(2);
+    }
+    if (0.2f < fabsf(power_meter_current_dbov(&meter) + 10.0))
+    {
+        printf("Test failed (dBOv)\n");
+        exit(2);
+    }
+
+    printf("Testing with AWGN 10dB from maximum\n");
+    awgn_init_dbov(&noise_source, idum, -10.0f);
+    for (i = 0;  i < 1000;  i++)
+        amp[i] = awgn(&noise_source);
+    for (i = 0;  i < 1000;  i++)
+    {
+        level = power_meter_update(&meter, amp[i]);
+        //printf("%12d %fdBm0 %fdBov\n", level, power_meter_current_dbm0(&meter), power_meter_current_dbov(&meter));
+    }
+    printf("Level: expected %" PRId32 "/%" PRId32 ", got %" PRId32 "\n", power_meter_level_dbov(-10.0f), power_meter_level_dbm0(-10.0f + DBM0_MAX_POWER), level);
+    printf("Power: expected %fdBm0, got %fdBm0\n", -10.0f + DBM0_MAX_POWER, power_meter_current_dbm0(&meter));
+    printf("Power: expected %fdBOv, got %fdBOv\n", -10.0f, power_meter_current_dbov(&meter));
+    if (level < power_meter_level_dbov(-10.0f)*0.95f
+        ||
+        level > power_meter_level_dbov(-10.0f)*1.05f)
+    {
+        printf("Test failed (level)\n");
+        exit(2);
+    }
+    if (0.2f < fabsf(power_meter_current_dbm0(&meter) + 10.0f - DBM0_MAX_POWER))
+    {
+        printf("Test failed (dBm0)\n");
+        exit(2);
+    }
+    if (0.2f < fabsf(power_meter_current_dbov(&meter) + 10.0f))
+    {
+        printf("Test failed (dBOv)\n");
+        exit(2);
+    }
+    printf("Tests passed\n");
+    return  0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/tests/queue_tests.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/tests/queue_tests.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,736 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * queue_tests.c
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2007 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: queue_tests.c,v 1.11 2008/08/10 03:42:38 steveu Exp $
+ */
+
+/* THIS IS A WORK IN PROGRESS. IT IS NOT FINISHED. */
+
+/*! \page queue_tests_page Queue tests
+\section queue_tests_page_sec_1 What does it do?
+*/
+
+#if defined(HAVE_CONFIG_H)
+#include <config.h>
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <string.h>
+#include <assert.h>
+#include <pthread.h>
+#include <sched.h>
+
+#include "spandsp.h"
+
+#define BUF_LEN     10000
+#define MSG_LEN     17
+
+pthread_t thread[2];
+queue_state_t *queue;
+volatile int put_oks;
+volatile int put_misses;
+volatile int got_oks;
+volatile int got_misses;
+
+int total_in;
+int total_out;
+
+static void tests_failed(void)
+{
+    printf("Tests failed\n");
+    tests_failed();
+}
+/*- End of function --------------------------------------------------------*/
+
+static void display_queue_pointers(void)
+{
+    printf("Pointers %d %d %d\n", queue->iptr, queue->optr, queue->len);
+}
+/*- End of function --------------------------------------------------------*/
+
+static void *run_stream_write(void *arg)
+{
+    uint8_t buf[MSG_LEN];
+    int i;
+    int next;
+
+    printf("Write thread\n");
+    next = 0;
+    for (i = 0;  i < MSG_LEN;  i++)
+        buf[i] = next;
+    next = (next + 1) & 0xFF;
+    put_oks = 0;
+    put_misses = 0;
+    for (;;)
+    {
+        if (queue_write(queue, buf, MSG_LEN) == MSG_LEN)
+        {
+            for (i = 0;  i < MSG_LEN;  i++)
+                buf[i] = next;
+            next = (next + 1) & 0xFF;
+            put_oks++;
+            if (put_oks%1000000 == 0)
+                printf("%d puts, %d misses\n", put_oks, put_misses);
+        }
+        else
+        {
+            sched_yield();
+            put_misses++;
+        }
+    }
+    return NULL;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void *run_stream_read(void *arg)
+{
+    uint8_t buf[MSG_LEN];
+    int i;
+    int len;
+    int next;
+
+    printf("Read thread\n");
+    next = 0;
+    got_oks = 0;
+    got_misses = 0;
+    for (;;)
+    {
+        if ((len = queue_read(queue, buf, MSG_LEN)) >= 0)
+        {
+            if (len != MSG_LEN)
+            {
+                printf("AHH! - len %d\n", len);
+                tests_failed();
+            }
+            for (i = 0;  i < len;  i++)
+            {
+                if (buf[i] != next)
+                {
+                    printf("AHH! - 0x%X 0x%X\n", buf[i], next);
+                    tests_failed();
+                }
+            }
+            next = (next + 1) & 0xFF;
+            got_oks++;
+            if (got_oks%1000000 == 0)
+                printf("%d gots, %d misses\n", got_oks, got_misses);
+        }
+        else
+        {
+            sched_yield();
+            got_misses++;
+        }
+    }
+    return NULL;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void threaded_stream_tests(void)
+{
+    pthread_attr_t attr;
+
+    if ((queue = queue_init(NULL, BUF_LEN, QUEUE_READ_ATOMIC | QUEUE_WRITE_ATOMIC)) == NULL)
+    {
+        printf("Failed to create the queue\n");
+        tests_failed();
+    }
+    pthread_attr_init(&attr);
+    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
+    if (pthread_create(&thread[0], &attr, run_stream_write, NULL))
+    {
+        printf("Failed to create thread\n");
+        tests_failed();
+    }
+    if (pthread_create(&thread[1], &attr, run_stream_read, NULL))
+    {
+        printf("Failed to create thread\n");
+        tests_failed();
+    }
+    for (;;)
+    {
+        sleep(5);
+        printf("Main thread - %d %d\n", put_oks, got_oks);
+    }
+    queue_free(queue);
+}
+/*- End of function --------------------------------------------------------*/
+
+static void *run_message_write(void *arg)
+{
+    uint8_t buf[MSG_LEN];
+    int i;
+    int next;
+
+    printf("Write thread\n");
+    next = 0;
+    for (i = 0;  i < MSG_LEN;  i++)
+        buf[i] = next;
+    next = (next + 1) & 0xFF;
+    put_oks = 0;
+    put_misses = 0;
+    for (;;)
+    {
+        if (queue_write_msg(queue, buf, MSG_LEN) == MSG_LEN)
+        {
+            for (i = 0;  i < MSG_LEN;  i++)
+                buf[i] = next;
+            next = (next + 1) & 0xFF;
+            put_oks++;
+            if (put_oks%1000000 == 0)
+                printf("%d puts, %d misses\n", put_oks, put_misses);
+        }
+        else
+        {
+            sched_yield();
+            put_misses++;
+        }
+    }
+    return NULL;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void *run_message_read(void *arg)
+{
+    uint8_t buf[1024];
+    int i;
+    int len;
+    int next;
+
+    printf("Read thread\n");
+    next = 0;
+    got_oks = 0;
+    got_misses = 0;
+    for (;;)
+    {
+        if ((len = queue_read_msg(queue, buf, 1024)) >= 0)
+        {
+            if (len != MSG_LEN)
+            {
+                printf("AHH! - len %d\n", len);
+                tests_failed();
+            }
+            for (i = 0;  i < len;  i++)
+            {
+                if (buf[i] != next)
+                {
+                    printf("AHH! - 0x%X 0x%X\n", buf[i], next);
+                    tests_failed();
+                }
+            }
+            next = (next + 1) & 0xFF;
+            got_oks++;
+            if (got_oks%1000000 == 0)
+                printf("%d gots, %d misses\n", got_oks, got_misses);
+        }
+        else
+        {
+            sched_yield();
+            got_misses++;
+        }
+    }
+    return NULL;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void threaded_message_tests(void)
+{
+    pthread_attr_t attr;
+
+    if ((queue = queue_init(NULL, BUF_LEN, QUEUE_READ_ATOMIC | QUEUE_WRITE_ATOMIC)) == NULL)
+    {
+        printf("Failed to create the queue\n");
+        tests_failed();
+    }
+    pthread_attr_init(&attr);
+    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
+    if (pthread_create(&thread[0], &attr, run_message_write, NULL))
+    {
+        printf("Failed to create thread\n");
+        tests_failed();
+    }
+    if (pthread_create(&thread[1], &attr, run_message_read, NULL))
+    {
+        printf("Failed to create thread\n");
+        tests_failed();
+    }
+    for (;;)
+    {
+        sleep(5);
+        printf("Main thread - %d %d\n", put_oks, got_oks);
+    }
+    queue_free(queue);
+}
+/*- End of function --------------------------------------------------------*/
+
+static void check_contents(int total_in, int total_out)
+{
+    if (queue_contents(queue) != (total_in - total_out))
+    {
+        printf("Contents = %d (%d)\n", queue_contents(queue), (total_in - total_out));
+        display_queue_pointers();
+        tests_failed();
+    }
+    if (queue_free_space(queue) != BUF_LEN - (total_in - total_out))
+    {
+        printf("Free space = %d (%d)\n", queue_free_space(queue), BUF_LEN - (total_in - total_out));
+        display_queue_pointers();
+        tests_failed();
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static int monitored_queue_write(const uint8_t buf[], int len)
+{
+    int lenx;
+    
+    lenx = queue_write(queue, buf, len);
+    if (lenx >= 0)
+        total_in += lenx;
+    check_contents(total_in, total_out);
+    return lenx;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int monitored_queue_write_byte(const uint8_t buf)
+{
+    int res;
+    
+    if ((res = queue_write_byte(queue, buf)) >= 0)
+        total_in++;
+    check_contents(total_in, total_out);
+    return res;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int monitored_queue_read(uint8_t buf[], int len)
+{
+    int lenx;
+    
+    lenx = queue_read(queue, buf, len);
+    if (lenx >= 0)
+        total_out += lenx;
+    check_contents(total_in, total_out);
+    return lenx;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int monitored_queue_read_byte(void)
+{
+    int res;
+    
+    if ((res = queue_read_byte(queue)) >= 0)
+        total_out++;
+    check_contents(total_in, total_out);
+    return res;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void functional_stream_tests(void)
+{
+    uint8_t buf[MSG_LEN];
+    int i;
+    int res;
+    
+    total_in = 0;
+    total_out = 0;
+
+    for (i = 0;  i < MSG_LEN;  i++)
+        buf[i] = i;
+    if ((queue = queue_init(NULL, BUF_LEN, QUEUE_READ_ATOMIC | QUEUE_WRITE_ATOMIC)) == NULL)
+    {
+        printf("Failed to create the queue\n");
+        tests_failed();
+    }
+    check_contents(total_in, total_out);
+    /* Half fill the buffer, and check we can get out what we put in. */
+    for (i = 1;  i < 5000;  i++)
+    {
+        if (monitored_queue_write_byte(i & 0xFF) != 1)
+        {
+            printf("Byte by byte full at %d/%d\n", i, BUF_LEN);
+            tests_failed();
+        }
+    }
+    for (i = 1;  i < 5001;  i++)
+    {
+        if ((res = monitored_queue_read_byte()) != (i & 0xFF))
+            break;
+    }
+    printf("Byte by byte read breaks at %d (expected %d) - %d\n", i, 5000, res);
+    if (i != 5000)
+        tests_failed();
+    /* Now completely fill the buffer, and we should roll around the end. Check we can
+       get out what we put in. */
+    for (i = 1;  i < 20000;  i++)
+    {
+        if (monitored_queue_write_byte(i & 0xFF) != 1)
+            break;
+    }
+    printf("Byte by byte full at %d (expected %d)\n", i, 10001);
+    if (i != 10001)
+        tests_failed();
+    for (i = 1;  i < 20000;  i++)
+    {
+        if ((res = monitored_queue_read_byte()) != (i & 0xFF))
+            break;
+    }
+    printf("Byte by byte read breaks at %d (expected %d) - %d\n", i, 10001, res);
+    if (i != 10001)
+        tests_failed();
+    /* Fill the buffer, checking the contents grow correctly */
+    for (i = 1;  i < 1000;  i++)
+    {
+        if (monitored_queue_write(buf, MSG_LEN) != MSG_LEN)
+            break;
+    }
+    printf("Full at chunk %d (expected %d)\n", i, BUF_LEN/MSG_LEN + 1);
+    if (i != BUF_LEN/MSG_LEN + 1)
+        tests_failed();
+    if (monitored_queue_write(buf, 5) == 5)
+    {
+        printf("Write of 5 succeeded\n");
+        tests_failed();
+    }
+    if (monitored_queue_write(buf, 4) != 4)
+    {
+        printf("Write of 4 failed\n");
+        tests_failed();
+    }
+    /* Now full. Empty a little, and refill around the end */
+    if (monitored_queue_read(buf, MSG_LEN) != MSG_LEN)
+    {
+        printf("Read failed\n");
+        tests_failed();
+    }
+    if (monitored_queue_write(buf, MSG_LEN) != MSG_LEN)
+    {
+        printf("Write failed\n");
+        tests_failed();
+    }
+    /* Empty completely, checking the contents shrink correctly */
+    for (;;)
+    {
+        if (monitored_queue_read(buf, MSG_LEN) != MSG_LEN)
+            break;
+    }
+    if (monitored_queue_read(buf, 4) != 4)
+    {
+        printf("Read failed\n");
+        tests_failed();
+    }
+    /* Nudge around the buffer */
+    for (i = 1;  i < 588;  i++)
+    {
+        if (monitored_queue_write(buf, MSG_LEN) != MSG_LEN)
+        {
+            printf("Write failed\n");
+            tests_failed();
+        }
+        if (monitored_queue_read(buf, MSG_LEN) != MSG_LEN)
+        {
+            printf("Read failed\n");
+            tests_failed();
+        }
+    }
+    /* Fill the buffer, checking the contents grow correctly */
+    for (i = 1;  i < 1000;  i++)
+    {
+        if (monitored_queue_write(buf, MSG_LEN) != MSG_LEN)
+            break;
+    }
+    printf("Full at chunk %d (expected %d)\n", i, BUF_LEN/MSG_LEN + 1);
+    if (i != BUF_LEN/MSG_LEN + 1)
+        tests_failed();
+    display_queue_pointers();
+    if (monitored_queue_read(buf, MSG_LEN) != MSG_LEN)
+    {
+        printf("Read failed\n");
+        tests_failed();
+    }
+    if (monitored_queue_write(buf, MSG_LEN) != MSG_LEN)
+    {
+        printf("Write failed\n");
+        tests_failed();
+    }
+    display_queue_pointers();
+    for (i = 1;  i < 5000;  i++)
+    {
+        if (monitored_queue_read(buf, MSG_LEN) != MSG_LEN)
+        {
+            printf("Read failed\n");
+            tests_failed();
+        }
+        if (monitored_queue_write(buf, MSG_LEN) != MSG_LEN)
+        {
+            printf("Write failed\n");
+            tests_failed();
+        }
+    }
+    display_queue_pointers();
+    if (monitored_queue_write(buf, 5) == 5)
+    {
+        printf("Write of 5 succeeded\n");
+        tests_failed();
+    }
+    if (monitored_queue_write(buf, 4) != 4)
+    {
+        printf("Write of 4 failed\n");
+        tests_failed();
+    }
+    display_queue_pointers();
+    for (i = 1;  i < 5000;  i++)
+    {
+        if (monitored_queue_read(buf, MSG_LEN) != MSG_LEN)
+        {
+            printf("Read failed\n");
+            tests_failed();
+        }
+        if (monitored_queue_write(buf, MSG_LEN) != MSG_LEN)
+        {
+            printf("Write failed\n");
+            tests_failed();
+        }
+    }
+    display_queue_pointers();
+    queue_free(queue);
+}
+/*- End of function --------------------------------------------------------*/
+
+static int monitored_queue_write_msg(const uint8_t buf[], int len)
+{
+    int lenx;
+
+    lenx = queue_write_msg(queue, buf, len);
+    if (lenx >= 0)
+        total_in += lenx + sizeof(uint16_t);
+    check_contents(total_in, total_out);
+    return lenx;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int monitored_queue_read_msg(uint8_t buf[], int len)
+{
+    int lenx;
+
+    lenx = queue_read_msg(queue, buf, len);
+    if (lenx >= 0)
+        total_out += lenx + sizeof(uint16_t);
+    check_contents(total_in, total_out);
+    return lenx;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void functional_message_tests(void)
+{
+    uint8_t buf[MSG_LEN];
+    int i;
+    int len;
+    
+    total_in = 0;
+    total_out = 0;
+
+    for (i = 0;  i < MSG_LEN;  i++)
+        buf[i] = i;
+    if ((queue = queue_init(NULL, BUF_LEN, QUEUE_READ_ATOMIC | QUEUE_WRITE_ATOMIC)) == NULL)
+    {
+        printf("Failed to create the queue\n");
+        tests_failed();
+    }
+    check_contents(total_in, total_out);
+    /* Fill the buffer, checking the contents grow correctly */
+    for (i = 1;  i < 1000;  i++)
+    {
+        if (monitored_queue_write_msg(buf, MSG_LEN) != MSG_LEN)
+            break;
+    }
+    printf("Full at chunk %d (expected %u)\n", i, BUF_LEN/(MSG_LEN + sizeof(uint16_t)) + 1);
+    if (i != BUF_LEN/(MSG_LEN + sizeof(uint16_t)) + 1)
+        tests_failed();
+    if ((len = monitored_queue_write_msg(buf, 5)) == 5)
+    {
+        printf("Write of 5 succeeded\n");
+        tests_failed();
+    }
+    if ((len = monitored_queue_write_msg(buf, 4)) != 4)
+    {
+        printf("Write of 4 failed\n");
+        tests_failed();
+    }
+    /* Now full. Empty a little, and refill around the end */
+    if ((len = monitored_queue_read_msg(buf, MSG_LEN)) != MSG_LEN)
+    {
+        printf("Read failed - %d\n", len);
+        tests_failed();
+    }
+    if ((len = monitored_queue_write_msg(buf, MSG_LEN)) != MSG_LEN)
+    {
+        printf("Write failed - %d\n", len);
+        tests_failed();
+    }
+    /* Empty completely, checking the contents shrink correctly */
+    for (;;)
+    {
+        if ((len = monitored_queue_read_msg(buf, MSG_LEN)) != MSG_LEN)
+            break;
+    }
+    if (len != 4)
+    {
+        printf("Read failed - %d\n", len);
+        tests_failed();
+    }
+    /* We should now have one MSG_LEN message in the buffer */
+    /* Nudge around the buffer */
+    for (i = 1;  i < 527;  i++)
+    {
+        if ((len = monitored_queue_write_msg(buf, MSG_LEN)) != MSG_LEN)
+        {
+            printf("Write failed - %d\n", len);
+            tests_failed();
+        }
+        if ((len = monitored_queue_read_msg(buf, MSG_LEN)) != MSG_LEN)
+        {
+            printf("Read failed - %d\n", len);
+            tests_failed();
+        }
+    }
+    /* Fill the buffer, checking the contents grow correctly */
+    for (i = 1;  i < 1000;  i++)
+    {
+        if ((len = monitored_queue_write_msg(buf, MSG_LEN)) != MSG_LEN)
+            break;
+    }
+    printf("Free space = %d (%d)\n", queue_free_space(queue), BUF_LEN - (total_in - total_out));
+    display_queue_pointers();
+    printf("Full at chunk %d (expected %u)\n", i, BUF_LEN/(MSG_LEN + sizeof(uint16_t)));
+    if (i != BUF_LEN/(MSG_LEN + sizeof(uint16_t)))
+        tests_failed();
+    display_queue_pointers();
+
+    if ((len = monitored_queue_read_msg(buf, MSG_LEN)) != MSG_LEN)
+    {
+        printf("Read failed - %d\n", len);
+        tests_failed();
+    }
+    if ((len = monitored_queue_write_msg(buf, MSG_LEN)) != MSG_LEN)
+    {
+        printf("Write failed - %d\n", len);
+        tests_failed();
+    }
+    display_queue_pointers();
+    for (i = 1;  i < 5000;  i++)
+    {
+        if ((len = monitored_queue_read_msg(buf, MSG_LEN)) != MSG_LEN)
+        {
+            printf("Read failed - %d\n", len);
+            tests_failed();
+        }
+        if ((len = monitored_queue_write_msg(buf, MSG_LEN)) != MSG_LEN)
+        {
+            printf("Write failed - %d\n", len);
+            tests_failed();
+        }
+    }
+    display_queue_pointers();
+    if ((len = monitored_queue_write_msg(buf, 5)) == 5)
+    {
+        printf("Write of 5 succeeded\n");
+        tests_failed();
+    }
+    if ((len = monitored_queue_write_msg(buf, 4)) != 4)
+    {
+        printf("Write of 4 failed\n");
+        tests_failed();
+    }
+    display_queue_pointers();
+    for (i = 1;  i < 5000;  i++)
+    {
+        if (i == 527)
+        {
+            if ((len = monitored_queue_read_msg(buf, MSG_LEN)) != 4)
+            {
+                printf("Read failed - %d\n", len);
+                tests_failed();
+            }
+        }
+        if ((len = monitored_queue_read_msg(buf, MSG_LEN)) != MSG_LEN)
+        {
+            printf("Read failed - %d\n", len);
+            tests_failed();
+        }
+        if ((len = monitored_queue_write_msg(buf, MSG_LEN)) != MSG_LEN)
+        {
+            printf("Write failed - %d\n", len);
+            tests_failed();
+        }
+    }
+    display_queue_pointers();
+    queue_free(queue);
+}
+/*- End of function --------------------------------------------------------*/
+
+int main(int argc, char *argv[])
+{
+    int threaded_messages;
+    int threaded_streams;
+    int opt;
+
+    threaded_messages = FALSE;
+    threaded_streams = FALSE;
+    while ((opt = getopt(argc, argv, "ms")) != -1)
+    {
+        switch (opt)
+        {
+        case 'm':
+            threaded_messages = TRUE;
+            break;
+        case 's':
+            threaded_streams = TRUE;
+            break;
+        }
+    }
+
+    /* Test the basic functionality of the queueing code in stream and message modes */
+    printf("Stream mode functional tests\n");
+    functional_stream_tests();
+    printf("Message mode functional tests\n");
+    functional_message_tests();
+
+    /* Run separate write and read threads for a while, to verify there are no locking
+       issues. */
+    if (threaded_streams)
+    {
+        printf("Stream mode threaded tests\n");
+        threaded_stream_tests();
+    }
+    if (threaded_messages)
+    {
+        printf("Message mode threaded tests\n");
+        threaded_message_tests();
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/tests/r2_mf_rx_tests.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/tests/r2_mf_rx_tests.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,596 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * r2_mf_tests.c - Test the R2 MF detector against the spec., whatever the
+ *                 spec. may be :)
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2003 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: r2_mf_rx_tests.c,v 1.10 2008/05/13 13:17:26 steveu Exp $
+ */
+
+/*! \file */
+
+/*! \page r2_mf_tests_page R2 MF tone generation and detection tests
+\section r2_mf_tests_page_sec_1 What does it do?
+These tests are fashioned after those on the CM7291 test tape from
+Mitel. Those tests are for DTMF, rather than R2 MF, but make a
+fair starting point for a set of meaningful tests of R2 MF.
+
+These tests include conversion to and from A-law. It is assumed the
+distortion this produces is comparable to u-law, so it should be
+a fair test of performance in a real PSTN channel.
+*/
+
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <string.h>
+#include <time.h>
+#include <audiofile.h>
+
+#include "spandsp.h"
+
+/* R2 tone generation specs.
+ *  Power: -11.5dBm +- 1dB
+ *  Frequency: within +-4Hz
+ *  Mismatch between the start time of a pair of tones: <=1ms.
+ *  Mismatch between the end time of a pair of tones: <=1ms.
+ */
+/* Basic MFC/R2 tone detection specs:
+ *  Receiver response range: -5dBm to -35dBm
+ *  Difference in level for a pair of frequencies
+ *      Adjacent tones: <5dB
+ *      Non-adjacent tones: <7dB
+ *  Receiver not to detect a signal of 2 frequencies of level -5dB and
+ *  duration <7ms.
+ *  Receiver not to recognise a signal of 2 frequencies having a difference
+ *  in level >=20dB.
+ *  Max received signal frequency error: +-10Hz
+ *  The sum of the operate and release times of a 2 frequency signal not to
+ *  exceed 80ms (there are no individual specs for the operate and release
+ *  times).
+ *  Receiver not to release for signal interruptions <=7ms.
+ *  System malfunction due to signal interruptions >7ms (typically 20ms) is
+ *  prevented by further logic elements.
+ */
+
+#define MF_DURATION                 (68*8)
+#define MF_PAUSE                    (68*8)
+#define MF_CYCLE                    (MF_DURATION + MF_PAUSE)
+
+typedef struct
+{
+    float       f1;         /* First freq */
+    float       f2;         /* Second freq */
+    int8_t      level1;     /* Level of the first freq (dB) */
+    int8_t      level2;     /* Level of the second freq (dB) */
+    uint8_t     on_time;    /* Tone on time (ms) */
+    uint8_t     off_time;   /* Minimum post tone silence (ms) */
+} mf_digit_tones_t;
+
+static const mf_digit_tones_t r2_mf_fwd_tones[] =
+{
+    {1380.0, 1500.0, -11, -11, 1, 0},
+    {1380.0, 1620.0, -11, -11, 1, 0},
+    {1500.0, 1620.0, -11, -11, 1, 0},
+    {1380.0, 1740.0, -11, -11, 1, 0},
+    {1500.0, 1740.0, -11, -11, 1, 0},
+    {1620.0, 1740.0, -11, -11, 1, 0},
+    {1380.0, 1860.0, -11, -11, 1, 0},
+    {1500.0, 1860.0, -11, -11, 1, 0},
+    {1620.0, 1860.0, -11, -11, 1, 0},
+    {1740.0, 1860.0, -11, -11, 1, 0},
+    {1380.0, 1980.0, -11, -11, 1, 0},
+    {1500.0, 1980.0, -11, -11, 1, 0},
+    {1620.0, 1980.0, -11, -11, 1, 0},
+    {1740.0, 1980.0, -11, -11, 1, 0},
+    {1860.0, 1980.0, -11, -11, 1, 0},
+    {0.0, 0.0, 0, 0, 0, 0}
+};
+
+static const mf_digit_tones_t r2_mf_back_tones[] =
+{
+    {1140.0, 1020.0, -11, -11, 1, 0},
+    {1140.0,  900.0, -11, -11, 1, 0},
+    {1020.0,  900.0, -11, -11, 1, 0},
+    {1140.0,  780.0, -11, -11, 1, 0},
+    {1020.0,  780.0, -11, -11, 1, 0},
+    { 900.0,  780.0, -11, -11, 1, 0},
+    {1140.0,  660.0, -11, -11, 1, 0},
+    {1020.0,  660.0, -11, -11, 1, 0},
+    { 900.0,  660.0, -11, -11, 1, 0},
+    { 780.0,  660.0, -11, -11, 1, 0},
+    {1140.0,  540.0, -11, -11, 1, 0},
+    {1020.0,  540.0, -11, -11, 1, 0},
+    { 900.0,  540.0, -11, -11, 1, 0},
+    { 780.0,  540.0, -11, -11, 1, 0},
+    { 660.0,  540.0, -11, -11, 1, 0},
+    {0.0, 0.0, 0, 0, 0, 0}
+};
+
+static tone_gen_descriptor_t my_mf_digit_tones[16];
+
+static char r2_mf_tone_codes[] = "1234567890BCDEF";
+
+int callback_ok;
+int callback_roll;
+
+static void my_mf_gen_init(float low_fudge,
+                           int low_level,
+                           float high_fudge,
+                           int high_level,
+                           int duration,
+                           int fwd)
+{
+    const mf_digit_tones_t *tone;
+    int i;
+    
+    for (i = 0;  i < 15;  i++)
+    {
+        if (fwd)
+            tone = &r2_mf_fwd_tones[i];
+        else
+            tone = &r2_mf_back_tones[i];
+        make_tone_gen_descriptor(&my_mf_digit_tones[i],
+                                 tone->f1*(1.0 + low_fudge),
+                                 low_level,
+                                 tone->f2*(1.0 + high_fudge),
+                                 high_level,
+                                 duration,
+                                 0,
+                                 0,
+                                 0,
+                                 FALSE);
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static int my_mf_generate(int16_t amp[], char digit)
+{
+    int len;
+    char *cp;
+    tone_gen_state_t tone;
+
+    len = 0;
+    if ((cp = strchr(r2_mf_tone_codes, digit)))
+    {
+        tone_gen_init(&tone, &my_mf_digit_tones[cp - r2_mf_tone_codes]);
+        len += tone_gen(&tone, amp + len, 9999);
+    }
+    return len;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void codec_munge(int16_t amp[], int len)
+{
+    int i;
+    uint8_t alaw;
+    
+    for (i = 0;  i < len;  i++)
+    {
+        alaw = linear_to_alaw (amp[i]);
+        amp[i] = alaw_to_linear (alaw);
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static void digit_delivery(void *data, int digit, int level, int delay)
+{
+    char ch;
+
+    if (data != (void *) 0x12345678)
+    {
+        callback_ok = FALSE;
+        return;
+    }
+    if ((callback_roll & 1))
+        ch = 0;
+    else
+        ch = r2_mf_tone_codes[callback_roll >> 1];
+    if (ch == digit)
+        callback_ok = TRUE;
+    else
+        callback_ok = FALSE;
+    if (r2_mf_tone_codes[callback_roll >> 1])
+        callback_roll++;
+    else
+        callback_ok = FALSE;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int test_a_tone_set(int fwd)
+{
+    int i;
+    int j;
+    int len;
+    int sample;
+    const char *s;
+    char digit;
+    int actual;
+    int nplus;
+    int nminus;
+    float rrb;
+    float rcfo;
+    int16_t amp[100000];
+    r2_mf_rx_state_t mf_state;
+    awgn_state_t noise_source;
+    const mf_digit_tones_t *tone;
+
+    if (fwd)
+        tone = &r2_mf_fwd_tones[0];
+    else
+        tone = &r2_mf_back_tones[0];
+    r2_mf_rx_init(&mf_state, fwd, NULL, NULL);
+
+    /* Test 1: Mitel's test 1 isn't really a test. Its a calibration step,
+       which has no meaning here. */
+
+    printf ("Test 1: Calibration\n");
+    printf ("    Passed\n");
+
+    /* Test 2: Decode check
+       This is a sanity check, that all digits are reliably detected
+       under ideal conditions.  Each possible digit is repeated 10 times,
+       with 68ms bursts. The level of each tone is about 6dB down from clip */
+
+    printf ("Test 2: Decode check\n");
+    my_mf_gen_init(0.0, -3, 0.0, -3, 68, fwd);
+    s = r2_mf_tone_codes;
+    while (*s)
+    {
+        digit = *s++;
+        for (i = 0;  i < 10;  i++)
+        {
+            len = my_mf_generate(amp, digit);
+            codec_munge (amp, len);
+            r2_mf_rx(&mf_state, amp, len);
+            actual = r2_mf_rx_get(&mf_state);
+            if (actual != digit)
+            {
+                printf ("    Sent     '%c'\n", digit);
+                printf ("    Received 0x%X\n", actual);
+                printf ("    Failed\n");
+                exit (2);
+            }
+        }
+    }
+    printf ("    Passed\n");
+
+    /* Test 3: Recognition bandwidth and channel centre frequency check.
+       Use all digits. Each digit types requires four tests to complete
+       the check. Each section contains 40 pulses of 68ms duration,
+       with an amplitude of -20dB from clip per frequency.
+
+       Four sections covering the tests for one tone (1 digit) are:
+       a. H frequency at 0% deviation from center, L frequency at +0.1%.
+          L frequency is then increments in +01.% steps up to +4%. The
+          number of tone bursts is noted and designated N+.
+       b. H frequency at 0% deviation, L frequency at -0.1%. L frequency
+          is then incremental in -0.1% steps, up to -4%. The number of
+          tone bursts is noted and designated N-.
+       c. The test in (a) is repeated with the L frequency at 0% and the
+          H frequency varied up to +4%.
+       d. The test in (b) is repeated with the L frequency and 0% and the
+          H frequency varied to -4%.
+
+       Receiver Recognition Bandwidth (RRB) is calculated as follows:
+            RRB% = (N+ + N-)/10
+       Receiver Center Frequency Offset (RCFO) is calculated as follows:
+            RCFO% = X + (N+ - N-)/20
+            
+       Note that this test doesn't test what it says it is testing at all,
+       and the results are quite inaccurate, if not a downright lie! However,
+       it follows the Mitel procedure, so how can it be bad? :)
+       
+       The spec calls for +-4 +-10Hz (ie +-14Hz) of bandwidth. */
+
+    printf ("Test 3: Recognition bandwidth and channel centre frequency check\n");
+    s = r2_mf_tone_codes;
+    j = 0;
+    while (*s)
+    {
+        digit = *s++;
+        for (nplus = 0, i = 1;  i <= 60;  i++)
+        {
+            my_mf_gen_init((float) i/1000.0, -17, 0.0, -17, 68, fwd);
+            len = my_mf_generate(amp, digit);
+            codec_munge(amp, len);
+            r2_mf_rx(&mf_state, amp, len);
+            if (r2_mf_rx_get(&mf_state) == digit)
+                nplus++;
+        }
+        for (nminus = 0, i = -1;  i >= -60;  i--)
+        {
+            my_mf_gen_init((float) i/1000.0, -17, 0.0, -17, 68, fwd);
+            len = my_mf_generate(amp, digit);
+            codec_munge(amp, len);
+            r2_mf_rx(&mf_state, amp, len);
+            if (r2_mf_rx_get(&mf_state) == digit)
+                nminus++;
+        }
+        rrb = (float) (nplus + nminus)/10.0;
+        rcfo = (float) (nplus - nminus)/10.0;
+        printf ("    %c (low)  rrb = %5.2f%%, rcfo = %5.2f%%, max -ve = %5.2f, max +ve = %5.2f\n",
+                digit,
+                rrb,
+                rcfo,
+                (float) nminus/10.0,
+                (float) nplus/10.0);
+
+        if (rrb < rcfo + (2.0*100.0*14.0/r2_mf_fwd_tones[j].f1)  ||  rrb >= 15.0 + rcfo)
+        {
+            printf ("    Failed\n");
+            exit (2);
+        }
+
+        for (nplus = 0, i = 1;  i <= 60;  i++)
+        {
+            my_mf_gen_init(0.0, -17, (float) i/1000.0, -17, 68, fwd);
+            len = my_mf_generate(amp, digit);
+            codec_munge(amp, len);
+            r2_mf_rx(&mf_state, amp, len);
+            if (r2_mf_rx_get(&mf_state) == digit)
+                nplus++;
+        }
+        for (nminus = 0, i = -1;  i >= -60;  i--)
+        {
+            my_mf_gen_init(0.0, -17, (float) i/1000.0, -17, 68, fwd);
+            len = my_mf_generate(amp, digit);
+            codec_munge(amp, len);
+            r2_mf_rx(&mf_state, amp, len);
+            if (r2_mf_rx_get(&mf_state) == digit)
+                nminus++;
+        }
+        rrb = (float) (nplus + nminus)/10.0;
+        rcfo = (float) (nplus - nminus)/10.0;
+        printf ("    %c (high) rrb = %5.2f%%, rcfo = %5.2f%%, max -ve = %5.2f, max +ve = %5.2f\n",
+                digit,
+                rrb,
+                rcfo,
+                (float) nminus/10.0,
+                (float) nplus/10.0);
+        if (rrb < rcfo + (2.0*100.0*14.0/r2_mf_fwd_tones[j].f2)  ||  rrb >= 15.0 + rcfo)
+        {
+            printf ("    Failed\n");
+            exit (2);
+        }
+        j++;
+    }
+    printf ("    Passed\n");
+
+    /* Test 4: Acceptable amplitude ratio (twist).
+       Twist all digits in both directions, and check the maximum twist
+       we can accept. The way this is done is styled after the Mitel DTMF
+       test, and has good and bad points. */
+
+    printf ("Test 4: Acceptable amplitude ratio (twist)\n");
+    s = r2_mf_tone_codes;
+    while (*s)
+    {
+        digit = *s++;
+        for (nplus = 0, i = -50;  i >= -250;  i--)
+        {
+            my_mf_gen_init(0.0, -5, 0.0, i/10, 68, fwd);
+
+            len = my_mf_generate(amp, digit);
+            codec_munge (amp, len);
+            r2_mf_rx(&mf_state, amp, len);
+            if (r2_mf_rx_get(&mf_state) == digit)
+                nplus++;
+        }
+        printf ("    %c normal twist  = %.2fdB\n", digit, (float) nplus/10.0);
+        if (nplus < 70)
+        {
+            printf ("    Failed\n");
+            exit (2);
+        }
+        for (nminus = 0, i = -50;  i >= -250;  i--)
+        {
+            my_mf_gen_init(0.0, i/10, 0.0, -5, 68, fwd);
+
+            len = my_mf_generate(amp, digit);
+            codec_munge(amp, len);
+            r2_mf_rx(&mf_state, amp, len);
+            if (r2_mf_rx_get(&mf_state) == digit)
+                nminus++;
+        }
+        printf ("    %c reverse twist = %.2fdB\n", digit, (float) nminus/10.0);
+        if (nminus < 70)
+        {
+            printf ("    Failed\n");
+            exit (2);
+        }
+    }
+    printf ("    Passed\n");
+
+    /* Test 5: Dynamic range
+       This test sends all possible digits, with gradually increasing 
+       amplitude. We determine the span over which we achieve reliable
+       detection. */
+       
+    printf ("Test 5: Dynamic range\n");
+    for (nplus = nminus = -1000, i = -50;  i <= 3;  i++)
+    {
+        s = r2_mf_tone_codes;
+        while (*s)
+        {
+            digit = *s++;
+            my_mf_gen_init(0.0, i, 0.0, i, 68, fwd);
+            for (j = 0;  j < 100;  j++)
+            {
+                len = my_mf_generate(amp, digit);
+                codec_munge(amp, len);
+                r2_mf_rx(&mf_state, amp, len);
+                if (r2_mf_rx_get(&mf_state) != digit)
+                    break;
+            }
+            if (j < 100)
+                break;
+        }
+        if (j == 100)
+        {
+            if (nplus == -1000)
+                nplus = i;
+        }
+        else
+        {
+            if (nplus != -1000  &&  nminus == -1000)
+                nminus = i;
+        }
+    }
+    printf ("    Dynamic range = %ddB to %ddB\n", nplus, nminus - 1);
+    if (nplus > -35  ||  nminus <= -5)
+    {
+        printf("    Failed\n");
+        exit(2);
+    }
+    printf ("    Passed\n");
+
+    /* Test 6: Guard time
+       This test sends all possible digits, with a gradually reducing 
+       duration. */
+
+    printf ("Test 6: Guard time\n");
+    for (i = 30;  i < 62;  i++)
+    {
+        s = r2_mf_tone_codes;
+        j = 0;
+        while (*s)
+        {
+            digit = *s++;
+            my_mf_gen_init(0.0, -5, 0.0, -3, i, fwd);
+            for (j = 0;  j < 500;  j++)
+            {
+                len = my_mf_generate(amp, digit);
+                codec_munge(amp, len);
+                r2_mf_rx(&mf_state, amp, len);
+                if (r2_mf_rx_get(&mf_state) != digit)
+                    break;
+            }
+            if (j < 500)
+                break;
+        }
+        if (j == 500)
+            break;
+    }
+    printf ("    Guard time = %dms\n", i);
+    if (i > 61)
+    {
+        printf("    Failed\n");
+        exit(2);
+    }
+    printf ("    Passed\n");
+
+    /* Test 7: Acceptable signal to noise ratio
+       We send all possible digits at -6dBm from clip, mixed with AWGN.
+       We gradually reduce the noise until we get clean detection. */
+
+    printf ("Test 7: Acceptable signal to noise ratio\n");
+    my_mf_gen_init(0.0, -3, 0.0, -3, 68, fwd);
+    for (i = -3;  i > -50;  i--)
+    {
+        s = r2_mf_tone_codes;
+        while (*s)
+        {
+            digit = *s++;
+            awgn_init_dbm0(&noise_source, 1234567, (float) i);
+            for (j = 0;  j < 500;  j++)
+            {
+                len = my_mf_generate(amp, digit);
+                for (sample = 0;  sample < len;  sample++)
+                    amp[sample] = saturate(amp[sample] + awgn(&noise_source));
+                codec_munge(amp, len);
+                r2_mf_rx(&mf_state, amp, len);
+                if (r2_mf_rx_get(&mf_state) != digit)
+                    break;
+            }
+            if (j < 500)
+                break;
+        }
+        if (j == 500)
+            break;
+    }
+    printf("    Acceptable S/N ratio is %ddB\n", -3 - i);
+    if (-3 - i > 26)
+    {
+        printf("    Failed\n");
+        exit(2);
+    }
+    printf("    Passed\n");
+
+    printf("Test 8: Callback digit delivery mode.\n");
+    callback_ok = FALSE;
+    callback_roll = 0;
+    r2_mf_rx_init(&mf_state, fwd, digit_delivery, (void *) 0x12345678);
+    my_mf_gen_init(0.0, -3, 0.0, -3, 68, fwd);
+    s = r2_mf_tone_codes;
+    awgn_init_dbm0(&noise_source, 1234567, -40.0f);
+    while (*s)
+    {
+        digit = *s++;
+        len = my_mf_generate(amp, digit);
+        for (sample = 0;  sample < len;  sample++)
+            amp[sample] = saturate(amp[sample] + awgn(&noise_source));
+        codec_munge(amp, len);
+        r2_mf_rx(&mf_state, amp, len);
+        len = 160;
+        memset(amp, '\0', len*sizeof(int16_t));
+        for (sample = 0;  sample < len;  sample++)
+            amp[sample] = saturate(amp[sample] + awgn(&noise_source));
+        codec_munge(amp, len);
+        r2_mf_rx(&mf_state, amp, len);
+    }
+    if (!callback_ok)
+    {
+        printf("    Failed\n");
+     	exit (2);
+    }
+    printf("    Passed\n");
+
+    /* The remainder of the Mitel tape is the talk-off test. This is
+       meaningless for R2 MF. However the decoder's tolerance of
+       out of band noise is significant. */
+    /* TODO: add a OOB tolerance test. */
+    
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int main(int argc, char *argv[])
+{
+    time_t now;
+    time_t duration;
+
+    now = time(NULL);
+    printf("R2 forward tones\n");
+    test_a_tone_set(TRUE);
+    printf("R2 backward tones\n");
+    test_a_tone_set(FALSE);
+    duration = time(NULL) - now;
+    printf ("Tests passed in %lds\n", duration);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/tests/r2_mf_tx_tests.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/tests/r2_mf_tx_tests.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,129 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * r2_mf_tx_tests.c - Test the Bell MF generator.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2003 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: r2_mf_tx_tests.c,v 1.13 2008/08/16 15:24:16 steveu Exp $
+ */
+
+/*! \file */
+
+/*! \page r2_mf_tx_tests_page R2 MF generation tests
+\section r2_mf_tx_tests_page_sec_1 What does it do?
+???.
+
+\section r2_mf_tx_tests_page_sec_2 How does it work?
+???.
+*/
+
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <string.h>
+#include <time.h>
+#include <audiofile.h>
+
+#include "spandsp.h"
+#include "spandsp-sim.h"
+
+#define OUTPUT_FILE_NAME    "r2_mf.wav"
+
+int main(int argc, char *argv[])
+{
+    r2_mf_tx_state_t gen;
+    int16_t amp[1000];
+    int len;
+    AFfilehandle outhandle;
+    int outframes;
+    int digit;
+    const char *digits = "0123456789BCDEF";
+
+    if ((outhandle = afOpenFile_telephony_write(OUTPUT_FILE_NAME, 1)) == AF_NULL_FILEHANDLE)
+    {
+        fprintf(stderr, "    Cannot open wave file '%s'\n", OUTPUT_FILE_NAME);
+        exit(2);
+    }
+
+    r2_mf_tx_init(&gen, FALSE);
+    for (digit = 0;  digits[digit];  digit++)
+    {
+        r2_mf_tx_put(&gen, digits[digit]);
+        len = r2_mf_tx(&gen, amp, 1000);
+        printf("Generated %d samples of %c\n", len, digits[digit]);
+        if (len > 0)
+        {
+            outframes = afWriteFrames(outhandle,
+                                      AF_DEFAULT_TRACK,
+                                      amp,
+                                      len);
+        }
+        r2_mf_tx_put(&gen, 0);
+        len = r2_mf_tx(&gen, amp, 1000);
+        printf("Generated %d samples\n", len);
+        if (len > 0)
+        {
+            outframes = afWriteFrames(outhandle,
+                                      AF_DEFAULT_TRACK,
+                                      amp,
+                                      len);
+        }
+    }
+
+    r2_mf_tx_init(&gen, TRUE);
+    for (digit = 0;  digits[digit];  digit++)
+    {
+        r2_mf_tx_put(&gen, digits[digit]);
+        len = r2_mf_tx(&gen, amp, 1000);
+        printf("Generated %d samples of %c\n", len, digits[digit]);
+        if (len > 0)
+        {
+            outframes = afWriteFrames(outhandle,
+                                      AF_DEFAULT_TRACK,
+                                      amp,
+                                      len);
+        }
+        r2_mf_tx_put(&gen, 0);
+        len = r2_mf_tx(&gen, amp, 1000);
+        printf("Generated %d samples\n", len);
+        if (len > 0)
+        {
+            outframes = afWriteFrames(outhandle,
+                                      AF_DEFAULT_TRACK,
+                                      amp,
+                                      len);
+        }
+    }
+
+    if (afCloseFile(outhandle) != 0)
+    {
+        fprintf(stderr, "    Cannot close wave file '%s'\n", OUTPUT_FILE_NAME);
+        exit (2);
+    }
+
+    return  0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/tests/regression_tests.sh
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/tests/regression_tests.sh	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,758 @@
+#!/bin/sh
+#
+# SpanDSP - a series of DSP components for telephony
+#
+# regression_tests.sh
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU Lesser General Public License version 2.1,
+# as published by the Free Software Foundation.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this program; if not, write to the Free Software
+# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+#
+# $Id: regression_tests.sh,v 1.51 2008/05/03 07:37:06 steveu Exp $
+#
+
+ITUTESTS_TIF=../test-data/itu/fax/itutests.tif
+STDOUT_DEST=xyzzy
+STDERR_DEST=xyzzy2
+
+echo Performing basic spandsp regression tests
+echo
+
+./adsi_tests >$STDOUT_DEST 2>$STDERR_DEST
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    echo adsi_tests failed!
+    exit $RETVAL
+fi
+echo adsi_tests completed OK
+
+./async_tests >$STDOUT_DEST 2>$STDERR_DEST
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    echo async_tests failed!
+    exit $RETVAL
+fi
+echo async_tests completed OK
+
+./at_interpreter_tests >$STDOUT_DEST 2>$STDERR_DEST
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    echo at_interpreter_tests failed!
+    exit $RETVAL
+fi
+echo at_interpreter_tests completed OK
+
+./awgn_tests >$STDOUT_DEST 2>$STDERR_DEST
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    echo awgn_tests failed!
+    exit $RETVAL
+fi
+echo awgn_tests completed OK
+
+./bell_mf_rx_tests >$STDOUT_DEST 2>$STDERR_DEST
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    echo bell_mf_rx_tests failed!
+    exit $RETVAL
+fi
+echo bell_mf_rx_tests completed OK
+
+./bell_mf_tx_tests >$STDOUT_DEST 2>$STDERR_DEST
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    echo bell_mf_tx_tests failed!
+    exit $RETVAL
+fi
+echo bell_mf_tx_tests completed OK
+
+./bert_tests >$STDOUT_DEST 2>$STDERR_DEST
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    echo bert_tests failed!
+    exit $RETVAL
+fi
+echo bert_tests completed OK
+
+./bit_operations_tests >$STDOUT_DEST 2>$STDERR_DEST
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    echo bit_operations_tests failed!
+    exit $RETVAL
+fi
+echo bit_operations_tests completed OK
+
+./crc_tests >$STDOUT_DEST 2>$STDERR_DEST
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    echo crc_tests failed!
+    exit $RETVAL
+fi
+echo crc_tests completed OK
+
+./dc_restore_tests >$STDOUT_DEST 2>$STDERR_DEST
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    echo dc_restore_tests failed!
+    exit $RETVAL
+fi
+echo dc_restore_tests completed OK
+
+./dds_tests >$STDOUT_DEST 2>$STDERR_DEST
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    echo dds_tests failed!
+    exit $RETVAL
+fi
+echo dds_tests completed OK
+
+./dtmf_rx_tests >$STDOUT_DEST 2>$STDERR_DEST
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    echo dtmf_rx_tests failed!
+    exit $RETVAL
+fi
+echo dtmf_rx_tests completed OK
+
+./dtmf_tx_tests >$STDOUT_DEST 2>$STDERR_DEST
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    echo dtmf_tx_tests failed!
+    exit $RETVAL
+fi
+echo dtmf_tx_tests completed OK
+
+#./echo_tests >$STDOUT_DEST 2>$STDERR_DEST
+#RETVAL=$?
+#if [ $RETVAL != 0 ]
+#then
+#    echo echo_tests failed!
+#    exit $RETVAL
+#fi
+#echo echo_tests completed OK
+echo echo_tests not enabled
+
+rm -f fax_tests_1.tif
+./fax_tests >$STDOUT_DEST 2>$STDERR_DEST
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    echo fax_tests failed!
+    exit $RETVAL
+fi
+# Now use tiffcmp to check the results. It will return non-zero if any page images differ. The -t
+# option means the normal differences in tags will be ignored.
+tiffcmp -t ${ITUTESTS_TIF} fax_tests_1.tif >/dev/null
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    echo fax_tests failed!
+    exit $RETVAL
+fi
+rm -f fax_tests_1.tif
+./fax_tests -e >$STDOUT_DEST 2>$STDERR_DEST
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    echo fax_tests -e failed!
+    exit $RETVAL
+fi
+# Now use tiffcmp to check the results. It will return non-zero if any page images differ. The -t
+# option means the normal differences in tags will be ignored.
+tiffcmp -t ${ITUTESTS_TIF} fax_tests_1.tif >/dev/null
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    echo fax_tests -e failed!
+    exit $RETVAL
+fi
+echo fax_tests completed OK
+
+./fsk_tests >$STDOUT_DEST 2>$STDERR_DEST
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    echo fsk_tests failed!
+    exit $RETVAL
+fi
+echo fsk_tests completed OK
+
+#./g1050_tests >$STDOUT_DEST 2>$STDERR_DEST
+#RETVAL=$?
+#if [ $RETVAL != 0 ]
+#then
+#    echo g1050_tests failed!
+#    exit $RETVAL
+#fi
+#echo g1050_tests completed OK
+
+./g711_tests >$STDOUT_DEST 2>$STDERR_DEST
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    echo g711_tests failed!
+    exit $RETVAL
+fi
+echo g711_tests completed OK
+
+./g722_tests >$STDOUT_DEST 2>$STDERR_DEST
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    echo g722_tests failed!
+    exit $RETVAL
+fi
+echo g722_tests completed OK
+
+./g726_tests >$STDOUT_DEST 2>$STDERR_DEST
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    echo g726_tests failed!
+    exit $RETVAL
+fi
+echo g726_tests completed OK
+
+./gsm0610_tests >$STDOUT_DEST 2>$STDERR_DEST
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    echo gsm0610_tests failed!
+    exit $RETVAL
+fi
+echo gsm0610_tests completed OK
+
+./hdlc_tests >$STDOUT_DEST 2>$STDERR_DEST
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    echo hdlc_tests failed!
+    exit $RETVAL
+fi
+echo hdlc_tests completed OK
+
+./ima_adpcm_tests >$STDOUT_DEST 2>$STDERR_DEST
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    echo ima_adpcm_tests failed!
+    exit $RETVAL
+fi
+echo ima_adpcm_tests completed OK
+
+./logging_tests >$STDOUT_DEST 2>$STDERR_DEST
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    echo logging_tests failed!
+    exit $RETVAL
+fi
+echo logging_tests completed OK
+
+./lpc10_tests >$STDOUT_DEST 2>$STDERR_DEST
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    echo lpc10_tests failed!
+    exit $RETVAL
+fi
+echo lpc10_tests completed OK
+
+./modem_echo_tests >$STDOUT_DEST 2>$STDERR_DEST
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    echo modem_echo_tests failed!
+    exit $RETVAL
+fi
+echo modem_echo_tests completed OK
+
+./modem_connect_tones_tests >$STDOUT_DEST 2>$STDERR_DEST
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    echo modem_connect_tones_tests failed!
+    exit $RETVAL
+fi
+echo modem_connect_tones_tests completed OK
+
+./noise_tests >$STDOUT_DEST 2>$STDERR_DEST
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    echo noise_tests failed!
+    exit $RETVAL
+fi
+echo noise_tests completed OK
+
+./oki_adpcm_tests >$STDOUT_DEST 2>$STDERR_DEST
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    echo oki_adpcm_tests failed!
+    exit $RETVAL
+fi
+echo oki_adpcm_tests completed OK
+
+#./playout_tests >$STDOUT_DEST 2>$STDERR_DEST
+#RETVAL=$?
+#if [ $RETVAL != 0 ]
+#then
+#    echo playout_tests failed!
+#    exit $RETVAL
+#fi
+#echo playout_tests completed OK
+echo playout_tests not enabled
+
+#./plc_tests >$STDOUT_DEST 2>$STDERR_DEST
+#RETVAL=$?
+#if [ $RETVAL != 0 ]
+#then
+#    echo plc_tests failed!
+#    exit $RETVAL
+#fi
+#echo plc_tests completed OK
+echo plc_tests not enabled
+
+./power_meter_tests >$STDOUT_DEST 2>$STDERR_DEST
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    echo power_meter_tests failed!
+    exit $RETVAL
+fi
+echo power_meter_tests completed OK
+
+./queue_tests >$STDOUT_DEST 2>$STDERR_DEST
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    echo queue_tests failed!
+    exit $RETVAL
+fi
+echo queue_tests completed OK
+
+./r2_mf_rx_tests >$STDOUT_DEST 2>$STDERR_DEST
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    echo r2_mf_rx_tests failed!
+    exit $RETVAL
+fi
+echo r2_mf_rx_tests completed OK
+
+./r2_mf_tx_tests >$STDOUT_DEST 2>$STDERR_DEST
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    echo r2_mf_tx_tests failed!
+    exit $RETVAL
+fi
+echo r2_mf_tx_tests completed OK
+
+#./rfc2198_sim_tests >$STDOUT_DEST 2>$STDERR_DEST
+#RETVAL=$?
+#if [ $RETVAL != 0 ]
+#then
+#    echo rfc2198_sim_tests failed!
+#    exit $RETVAL
+#fi
+#echo rfc2198_sim_tests completed OK
+
+./schedule_tests >$STDOUT_DEST 2>$STDERR_DEST
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    echo schedule_tests failed!
+    exit $RETVAL
+fi
+echo schedule_tests completed OK
+
+#./sig_tone_tests >$STDOUT_DEST 2>$STDERR_DEST
+#RETVAL=$?
+#if [ $RETVAL != 0 ]
+#then
+#    echo sig_tone_tests failed!
+#    exit $RETVAL
+#fi
+#echo sig_tone_tests completed OK
+echo sig_tone_tests not enabled
+
+#./super_tone_rx_tests >$STDOUT_DEST 2>$STDERR_DEST
+#RETVAL=$?
+#if [ $RETVAL != 0 ]
+#then
+#    echo super_tone_rx_tests failed!
+#    exit $RETVAL
+#fi
+#echo super_tone_rx_tests completed OK
+echo super_tone_rx_tests not enabled
+
+#./super_tone_tx_tests >$STDOUT_DEST 2>$STDERR_DEST
+#RETVAL=$?
+#if [ $RETVAL != 0 ]
+#then
+#    echo super_tone_tx_tests failed!
+#    exit $RETVAL
+#fi
+#echo super_tone_tx_tests completed OK
+echo super_tone_tx_tests not enabled
+
+./t31_tests -r >$STDOUT_DEST 2>$STDERR_DEST
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    echo t31_tests -r failed!
+    exit $RETVAL
+fi
+./t31_tests -s >$STDOUT_DEST 2>$STDERR_DEST
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    echo t31_tests -s failed!
+    exit $RETVAL
+fi
+echo t31_tests completed OK
+
+./t38_core_tests >$STDOUT_DEST 2>$STDERR_DEST
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    echo t38_core_tests failed!
+    exit $RETVAL
+fi
+echo t38_core_tests completed OK
+
+rm -f t38.tif
+./t38_gateway_tests >$STDOUT_DEST 2>$STDERR_DEST
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    echo t38_gateway_tests failed!
+    exit $RETVAL
+fi
+# Now use tiffcmp to check the results. It will return non-zero if any page images differ. The -t
+# option means the normal differences in tags will be ignored.
+tiffcmp -t ${ITUTESTS_TIF} t38.tif >/dev/null
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    echo t38_gateway_tests failed!
+    exit $RETVAL
+fi
+rm -f t38.tif
+./t38_gateway_tests -e >$STDOUT_DEST 2>$STDERR_DEST
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    echo t38_gateway_tests -e failed!
+    exit $RETVAL
+fi
+# Now use tiffcmp to check the results. It will return non-zero if any page images differ. The -t
+# option means the normal differences in tags will be ignored.
+tiffcmp -t ${ITUTESTS_TIF} t38.tif >/dev/null
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    echo t38_gateway_tests -e failed!
+    exit $RETVAL
+fi
+echo t38_gateway_tests completed OK
+
+rm -f t38.tif
+./t38_gateway_to_terminal_tests >$STDOUT_DEST 2>$STDERR_DEST
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    echo t38_gateway_to_terminal_tests failed!
+    exit $RETVAL
+fi
+# Now use tiffcmp to check the results. It will return non-zero if any page images differ. The -t
+# option means the normal differences in tags will be ignored.
+tiffcmp -t ${ITUTESTS_TIF} t38.tif >/dev/null
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    echo t38_gateway_to_terminal_tests failed!
+    exit $RETVAL
+fi
+rm -f t38.tif
+./t38_gateway_to_terminal_tests -e >$STDOUT_DEST 2>$STDERR_DEST
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    echo t38_gateway_to_terminal_tests -e failed!
+    exit $RETVAL
+fi
+# Now use tiffcmp to check the results. It will return non-zero if any page images differ. The -t
+# option means the normal differences in tags will be ignored.
+tiffcmp -t ${ITUTESTS_TIF} t38.tif >/dev/null
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    echo t38_gateway_to_terminal_tests -e failed!
+    exit $RETVAL
+fi
+echo t38_gateway_to_terminal_tests completed OK
+
+rm -f t38.tif
+./t38_terminal_to_gateway_tests >$STDOUT_DEST 2>$STDERR_DEST
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    echo t38_terminal_to_gateway_tests failed!
+    exit $RETVAL
+fi
+# Now use tiffcmp to check the results. It will return non-zero if any page images differ. The -t
+# option means the normal differences in tags will be ignored.
+tiffcmp -t ${ITUTESTS_TIF} t38.tif >/dev/null
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    echo t38_terminal_to_gateway_tests failed!
+    exit $RETVAL
+fi
+rm -f t38.tif
+./t38_terminal_to_gateway_tests -e >$STDOUT_DEST 2>$STDERR_DEST
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    echo t38_terminal_to_gateway_tests -e failed!
+    exit $RETVAL
+fi
+# Now use tiffcmp to check the results. It will return non-zero if any page images differ. The -t
+# option means the normal differences in tags will be ignored.
+tiffcmp -t ${ITUTESTS_TIF} t38.tif >/dev/null
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    echo t38_terminal_to_gateway_tests -e failed!
+    exit $RETVAL
+fi
+echo t38_terminal_to_gateway_tests completed OK
+
+rm -f t38.tif
+./t38_terminal_tests >$STDOUT_DEST 2>$STDERR_DEST
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    echo t38_terminal_tests failed!
+    exit $RETVAL
+fi
+# Now use tiffcmp to check the results. It will return non-zero if any page images differ. The -t
+# option means the normal differences in tags will be ignored.
+tiffcmp -t ${ITUTESTS_TIF} t38.tif >/dev/null
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    echo t38_terminal_tests failed!
+    exit $RETVAL
+fi
+rm -f t38.tif
+./t38_terminal_tests -e >$STDOUT_DEST 2>$STDERR_DEST
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    echo t38_terminal_tests -e failed!
+    exit $RETVAL
+fi
+# Now use tiffcmp to check the results. It will return non-zero if any page images differ. The -t
+# option means the normal differences in tags will be ignored.
+tiffcmp -t ${ITUTESTS_TIF} t38.tif >/dev/null
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    echo t38_terminal_tests -e failed!
+    exit $RETVAL
+fi
+echo t38_terminal_tests completed OK
+
+rm -f t4_tests_receive.tif
+./t4_tests >$STDOUT_DEST 2>$STDERR_DEST
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    echo t4_tests failed!
+    exit $RETVAL
+fi
+echo t4_tests completed OK
+
+#./time_scale_tests >$STDOUT_DEST 2>$STDERR_DEST
+#RETVAL=$?
+#if [ $RETVAL != 0 ]
+#then
+#    echo time_scale_tests failed!
+#    exit $RETVAL
+#fi
+#echo time_scale_tests completed OK
+echo time_scale_tests not enabled
+
+#./tone_detect_tests >$STDOUT_DEST 2>$STDERR_DEST
+#RETVAL=$?
+#if [ $RETVAL != 0 ]
+#then
+#    echo tone_detect_tests failed!
+#    exit $RETVAL
+#fi
+#echo tone_detect_tests completed OK
+echo tone_detect_tests not enabled
+
+#./tone_generate_tests >$STDOUT_DEST 2>$STDERR_DEST
+#RETVAL=$?
+#if [ $RETVAL != 0 ]
+#then
+#    echo tone_generate_tests failed!
+#    exit $RETVAL
+#fi
+#echo tone_generate_tests completed OK
+echo tone_generate_tests not enabled
+
+./v17_tests -s -42 14400 >$STDOUT_DEST 2>$STDERR_DEST
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    echo v17_tests failed!
+    exit $RETVAL
+fi
+./v17_tests -s -42 12000 >$STDOUT_DEST 2>$STDERR_DEST
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    echo v17_tests failed!
+    exit $RETVAL
+fi
+./v17_tests -s -42 9600 >$STDOUT_DEST 2>$STDERR_DEST
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    echo v17_tests failed!
+    exit $RETVAL
+fi
+./v17_tests -s -42 7200 >$STDOUT_DEST 2>$STDERR_DEST
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    echo v17_tests failed!
+    exit $RETVAL
+fi
+echo v17_tests completed OK
+
+#./v22bis_tests >$STDOUT_DEST 2>$STDERR_DEST
+#RETVAL=$?
+#if [ $RETVAL != 0 ]
+#then
+#    echo v22bis_tests failed!
+#    exit $RETVAL
+#fi
+#echo v22bis_tests completed OK
+echo v22bis_tests not enabled
+
+./v27ter_tests -s -42 2400 >$STDOUT_DEST 2>$STDERR_DEST
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    echo v27ter_tests failed!
+    exit $RETVAL
+fi
+./v27ter_tests -s -42 4800 >$STDOUT_DEST 2>$STDERR_DEST
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    echo v27ter_tests failed!
+    exit $RETVAL
+fi
+echo v27ter_tests completed OK
+
+./v29_tests -s -42 4800 >$STDOUT_DEST 2>$STDERR_DEST
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    echo v29_tests failed!
+    exit $RETVAL
+fi
+./v29_tests -s -42 7200 >$STDOUT_DEST 2>$STDERR_DEST
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    echo v29_tests failed!
+    exit $RETVAL
+fi
+./v29_tests -s -42 9600 >$STDOUT_DEST 2>$STDERR_DEST
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    echo v29_tests failed!
+    exit $RETVAL
+fi
+echo v29_tests completed OK
+
+#./v42_tests >$STDOUT_DEST 2>$STDERR_DEST
+#RETVAL=$?
+#if [ $RETVAL != 0 ]
+#then
+#    echo v42_tests failed!
+#    exit $RETVAL
+#fi
+#echo v42_tests completed OK
+echo v42_tests not enabled
+
+./v42bis_tests.sh >/dev/null
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    echo v42bis_tests failed!
+    exit $RETVAL
+fi
+echo v42bis_tests completed OK
+
+./v8_tests >$STDOUT_DEST 2>$STDERR_DEST
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    echo v8_tests failed!
+    exit $RETVAL
+fi
+echo v8_tests completed OK
+
+./vector_float_tests >$STDOUT_DEST 2>$STDERR_DEST
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    echo vector_float_tests failed!
+    exit $RETVAL
+fi
+echo vector_float_tests completed OK
+
+./vector_int_tests >$STDOUT_DEST 2>$STDERR_DEST
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    echo vector_int_tests failed!
+    exit $RETVAL
+fi
+echo vector_int_tests completed OK
+
+echo
+echo All regression tests successfully completed

Added: freeswitch/trunk/libs/spandsp/tests/rfc2198_sim_tests.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/tests/rfc2198_sim_tests.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,238 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * rfc2198_sim_tests.c - Tests for the G.1050/TIA-921 model
+ *                       with redundant transmission in the
+ *                       style of UDPTL or RFC2198..
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2007 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: rfc2198_sim_tests.c,v 1.5 2008/05/13 13:17:26 steveu Exp $
+ */
+
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
+
+#if defined(HAVE_FL_FL_H)  &&  defined(HAVE_FL_FL_CARTESIAN_H)  &&  defined(HAVE_FL_FL_AUDIO_METER_H)
+#define ENABLE_GUI
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <string.h>
+#include <time.h>
+#include <audiofile.h>
+#if defined(HAVE_MATH_H)
+#define GEN_CONST
+#endif
+
+#include "spandsp.h"
+#include "spandsp-sim.h"
+
+#if defined(ENABLE_GUI)
+#include "media_monitor.h"
+#endif
+
+#define PACKET_SIZE                 256
+#define PACKET_INTERVAL             20
+#define SIMULATION_TIME             300
+
+#define MODEL_NO                    8
+#define SPEED_PATTERN_NO            133
+
+int main(int argc, char *argv[])
+{
+    rfc2198_sim_state_t *s;
+    double *packet_arrival_times;
+    int packets_per_sec;
+    int num_packets;
+    int model_no;
+    int speed_pattern_no;
+    int use_gui;
+    int simulation_time;
+    int i;
+    int len;
+    uint8_t put_pkt[256];
+    uint8_t get_pkt[256];
+    int put_pkt_len;
+    int get_pkt_len;
+    int get_seq_no;
+    double get_departure_time;
+    double get_arrival_time;
+    int packets_put;
+    int packets_really_put;
+    int packets_got;
+    int oos_packets_got;
+    int missing_packets_got;
+    int highest_seq_no_got;
+    int opt;
+    FILE *out_file;
+
+    model_no = MODEL_NO;
+    speed_pattern_no = SPEED_PATTERN_NO;
+    simulation_time = SIMULATION_TIME;
+    use_gui = FALSE;
+    while ((opt = getopt(argc, argv, "gm:s:t:")) != -1)
+    {
+        switch (opt)
+        {
+        case 'g':
+#if defined(ENABLE_GUI)
+            use_gui = TRUE;
+#else
+            fprintf(stderr, "Graphical monitoring not available\n");
+            exit(2);
+#endif
+            break;
+        case 'm':
+            model_no = optarg[0] - 'A' + 1;
+            if (model_no < 0  ||  model_no > 8)
+            {
+                fprintf(stderr, "Bad model ID '%s'\n", optarg);
+                exit(2);
+            }
+            break;
+        case 's':
+            speed_pattern_no = atoi(optarg);
+            if (speed_pattern_no < 1  ||  speed_pattern_no > 133)
+            {
+                fprintf(stderr, "Bad link speed pattern %s\n", optarg);
+                exit(2);
+            }
+            break;
+        case 't':
+            simulation_time = atoi(optarg);
+            break;
+        default:
+            //usage();
+            exit(2);
+            break;
+        }
+    }
+    argc -= optind;
+    argv += optind;
+
+    if ((out_file = fopen("rfc2198_sim_tests.txt", "w")) == NULL)
+    {
+        fprintf(stderr, "Can't open %s\n", "rfc2198_sim_tests.txt");
+        return 2;
+    }
+    packets_per_sec = 1000/PACKET_INTERVAL;
+    num_packets = packets_per_sec*simulation_time;
+
+    if ((packet_arrival_times = calloc(num_packets, sizeof(double))) == NULL)
+    {
+        fprintf(stderr, "Can't allocate the data buffers\n");
+        return 2;
+    }
+    for (i = 0;  i < num_packets;  i++)
+        packet_arrival_times[i] = 0.0;
+
+    /* If we don't initialise this random number generator it gives endless zeros on some systems. */
+    /* Use a fixed seed to produce identical results in successive runs of the simulation, for debug purposes. */
+    srand48(0x1234567);
+
+    if ((s = rfc2198_sim_init(model_no, speed_pattern_no, PACKET_SIZE, packets_per_sec, 3)) == NULL)
+    {
+        fprintf(stderr, "Failed to start the G.1050 model\n");
+        exit(2);
+    }
+    //rfc2198_sim_dump_parms(model_no, speed_pattern_no);
+
+#if defined(ENABLE_GUI)
+    if (use_gui)
+        start_media_monitor();
+#endif
+
+    for (i = 0;  i < 256;  i++)
+        put_pkt[i] = i;
+    put_pkt_len = 256;
+    get_pkt_len = -1;
+    get_seq_no = -1;
+    get_arrival_time = -1;
+    packets_put = 0;
+    packets_really_put = 0;
+    packets_got = 0;
+    oos_packets_got = 0;
+    missing_packets_got = 0;
+    highest_seq_no_got = -1;
+    for (i = 0;  i < num_packets;  i++)
+    {
+        if ((len = rfc2198_sim_put(s, put_pkt, put_pkt_len, i, (double) i*0.001*PACKET_INTERVAL)) > 0)
+            packets_really_put++;
+        packets_put++;
+#if 0        
+        if (i == 5)
+            rfc2198_sim_queue_dump(s);
+#endif
+        if (i >= 5)
+        {
+            do
+            {
+                get_pkt_len = rfc2198_sim_get(s, get_pkt, 256, (double) i*0.001*PACKET_INTERVAL, &get_seq_no, &get_departure_time, &get_arrival_time);
+                if (get_pkt_len >= 0)
+                {
+#if defined(ENABLE_GUI)
+                    if (use_gui)
+                        media_monitor_rx(get_seq_no, get_departure_time, get_arrival_time);
+#endif
+                    packets_got++;
+                    if (get_seq_no < highest_seq_no_got)
+                        oos_packets_got++;
+                    else if (get_seq_no > highest_seq_no_got + 1)
+                        missing_packets_got += (get_seq_no - highest_seq_no_got - 1);
+                    if (get_seq_no > highest_seq_no_got)
+                        highest_seq_no_got = get_seq_no;
+                    fprintf(out_file, "%d, %.3f, %.8f\n", get_seq_no, get_seq_no*0.001*PACKET_INTERVAL, get_arrival_time);
+                }
+            }
+            while (get_pkt_len >= 0);
+        }
+#if defined(ENABLE_GUI)
+        if (use_gui)
+            media_monitor_update_display();
+#endif
+    }
+    /* Clear out anything remaining in the queue, by jumping forwards in time */
+    do
+    {
+        get_pkt_len = rfc2198_sim_get(s, get_pkt, 256, (double) i*0.001*PACKET_INTERVAL + 5.0, &get_seq_no, &get_departure_time, &get_arrival_time);
+        if (get_pkt_len >= 0)
+        {
+            packets_got++;
+            fprintf(out_file, "%d, %.3f, %.8f\n", get_seq_no, get_seq_no*0.001*PACKET_INTERVAL, get_arrival_time);
+        }
+    }
+    while (get_pkt_len >= 0);
+
+    fclose(out_file);
+
+    printf("Put %d packets. Really put %d packets. Got %d packets.\n", packets_put, packets_really_put, packets_got);
+    printf("%d OOS packets, %d missing packets\n", oos_packets_got, missing_packets_got - oos_packets_got);
+    printf("%d packets queued, %d received\n", packets_really_put, packets_got);
+    printf("%.3f%% of packets lost before redundancy\n", 100.0*(packets_put - packets_really_put)/packets_put);
+    printf("%.3f%% of packets lost after redundancy\n", 100.0*(packets_put - packets_got)/packets_put);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/tests/schedule_tests.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/tests/schedule_tests.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,117 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * schedule_tests.c
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2004 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: schedule_tests.c,v 1.19 2008/05/13 13:17:26 steveu Exp $
+ */
+
+/*! \page schedule_tests_page Event scheduler tests
+\section schedule_tests_page_sec_1 What does it do?
+???.
+
+\section schedule_tests_page_sec_2 How does it work?
+???.
+*/
+
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "spandsp.h"
+
+uint64_t when1;
+uint64_t when2;
+
+static void callback1(span_sched_state_t *s, void *user_data)
+{
+    int id;
+    uint64_t when;
+
+    when = span_schedule_time(s);
+    printf("1: Callback at %f %" PRId64 "\n", (float) when/1000000.0, when - when1);
+    if ((when - when1))
+    {
+        printf("Callback occured at the wrong time.\n");
+        exit(2);
+    }
+    id = span_schedule_event(s, 500000, callback1, NULL);
+    when1 = when + 500000;
+    when = span_schedule_next(s);
+    printf("1: Event %d, earliest is %" PRId64 "\n", id, when);
+}
+
+static void callback2(span_sched_state_t *s, void *user_data)
+{
+    int id;
+    uint64_t when;
+
+    when = span_schedule_time(s);
+    printf("2: Callback at %f %" PRId64 "\n", (float) when/1000000.0, when - when2);
+    id = span_schedule_event(s, 550000, callback2, NULL);
+    if ((when - when2) != 10000)
+    {
+        printf("Callback occured at the wrong time.\n");
+        exit(2);
+    }
+    when2 = when + 550000;
+    when = span_schedule_next(s);
+    printf("2: Event %d, earliest is %" PRId64 "\n", id, when);
+}
+
+int main(int argc, char *argv[])
+{
+    int i;
+    int id1;
+    int id2;
+    span_sched_state_t sched;
+    uint64_t when;
+    
+    span_schedule_init(&sched);
+
+    id1 = span_schedule_event(&sched, 500000, callback1, NULL);
+    id2 = span_schedule_event(&sched, 550000, callback2, NULL);
+    when1 = span_schedule_time(&sched) + 500000;
+    when2 = span_schedule_time(&sched) + 550000;
+    //span_schedule_del(&sched, id);
+    
+    for (i = 0;  i < 100000000;  i += 20000)
+    {
+        span_schedule_update(&sched, 20000);
+    }
+    when = span_schedule_time(&sched);
+    if ((when1 - when) < 0  ||  (when1 - when) > 500000  ||  (when2 - when) < 0  ||  (when2 - when) > 550000)
+    {
+        printf("Callback failed to occur.\n");
+        exit(2);
+    }
+    span_schedule_release(&sched);
+
+    printf("Tests passed.\n");
+    return  0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/tests/sig_tone_tests.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/tests/sig_tone_tests.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,264 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * sig_tone_tests.c
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2004 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: sig_tone_tests.c,v 1.23 2008/08/16 14:59:50 steveu Exp $
+ */
+
+/*! \file */
+
+/*! \page sig_tone_tests_page The signaling tone processor tests
+\section sig_tone_tests_sec_1 What does it do?
+???.
+
+\section sig_tone_tests_sec_2 How does it work?
+???.
+*/
+
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <memory.h>
+#include <audiofile.h>
+
+#include "spandsp.h"
+#include "spandsp-sim.h"
+
+#define OUT_FILE_NAME               "sig_tone.wav"
+
+#define SAMPLES_PER_CHUNK           160
+
+static int sampleno = 0;
+static int tone_1_present = 0;
+static int tone_2_present = 0;
+static int ping = 0;
+
+void map_frequency_response(sig_tone_rx_state_t *s);
+
+static int tx_handler(void *user_data, int what)
+{
+    //printf("What - %d\n", what);
+    if ((what & SIG_TONE_UPDATE_REQUEST))
+    {
+        printf("Tx: update request\n");
+        /* The signaling processor wants to know what to do next */
+        if (sampleno < ms_to_samples(100))
+        {
+            /* 100ms off-hook */
+            printf("100ms off-hook - %d samples\n", 800 - sampleno);
+            return 0x02 | ((ms_to_samples(100) - sampleno) << 16);
+        }
+        else if (sampleno < ms_to_samples(600))
+        {
+            /* 500ms idle */
+            printf("500ms idle - %d samples\n", ms_to_samples(600) - sampleno);
+            return 0x02 | SIG_TONE_1_PRESENT | ((ms_to_samples(600) - sampleno) << 16);
+        }
+        else if (sampleno < ms_to_samples(700))
+        {
+            /* 100ms seize */
+            printf("100ms seize - %d samples\n", ms_to_samples(700) - sampleno);
+            return 0x02 | ((ms_to_samples(700) - sampleno) << 16);
+        }
+        else if (sampleno < ms_to_samples(1700))
+        {
+            if (ping)
+            {
+                printf("33ms break - %d samples\n", ms_to_samples(33));
+                ping = !ping;
+                return 0x02 | (ms_to_samples(33) << 16);
+            }
+            else
+            {
+                printf("67ms make - %d samples\n", ms_to_samples(67));
+                ping = !ping;
+                return 0x02 | SIG_TONE_1_PRESENT | (ms_to_samples(67) << 16);
+            }
+            /*endif*/
+        }
+        else
+        {
+            return 0x02 | SIG_TONE_1_PRESENT | ((ms_to_samples(700) - sampleno) << 16) | SIG_TONE_TX_PASSTHROUGH;
+        }
+        /*endif*/
+    }
+    /*endif*/
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int rx_handler(void *user_data, int what)
+{
+    //printf("What - %d\n", what);
+    if ((what & SIG_TONE_1_CHANGE))
+    {
+        tone_1_present = what & SIG_TONE_1_PRESENT;
+        printf("Rx: tone 1 is %s after %d samples\n", (tone_1_present)  ?  "on"  : "off", (what >> 16) & 0xFFFF);
+    }
+    /*endif*/
+    if ((what & SIG_TONE_2_CHANGE))
+    {
+        tone_2_present = what & SIG_TONE_2_PRESENT;
+        printf("Rx: tone 2 is %s after %d samples\n", (tone_2_present)  ?  "on"  : "off", (what >> 16) & 0xFFFF);
+    }
+    /*endif*/
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+void map_frequency_response(sig_tone_rx_state_t *s)
+{
+    int16_t buf[8192];
+    awgn_state_t noise_source;
+    int i;
+    int f;
+    uint32_t phase_acc;
+    int32_t phase_rate;
+    int32_t scaling;
+    double sum;
+    
+    /* Things like noise don't highlight the frequency response of the high Q notch
+       very well. We use a slowly swept frequency to check it. */
+    awgn_init_dbm0(&noise_source, 1234567, -10.0f);
+    for (f = 1;  f < 4000;  f++)
+    {
+        phase_rate = dds_phase_rate(f);
+        scaling = dds_scaling_dbm0(-10);
+        phase_acc = 0;
+        for (i = 0;  i < 8192;  i++)
+            buf[i] = dds_mod(&phase_acc, phase_rate, scaling, 0);
+        /*endfor*/
+        sig_tone_rx(s, buf, 8192);
+        sum = 0.0;
+        for (i = 1000;  i < 8192;  i++)
+            sum += (double) buf[i]*(double) buf[i];
+        /*endfor*/
+        sum = sqrt(sum);
+        printf("%7d %f\n", f, sum);
+    }
+    /*endfor*/
+}
+/*- End of function --------------------------------------------------------*/
+
+int main(int argc, char *argv[])
+{
+    int16_t amp[SAMPLES_PER_CHUNK];
+    int16_t out_amp[2*SAMPLES_PER_CHUNK];
+    AFfilehandle outhandle;
+    int outframes;
+    int i;
+    int type;
+    int rx_samples;
+    int tx_samples;
+    sig_tone_tx_state_t tx_state;
+    sig_tone_rx_state_t rx_state;
+    awgn_state_t noise_source;
+    codec_munge_state_t *munge;
+
+    if ((outhandle = afOpenFile_telephony_write(OUT_FILE_NAME, 2)) == AF_NULL_FILEHANDLE)
+    {
+        fprintf(stderr, "    Cannot create wave file '%s'\n", OUT_FILE_NAME);
+        exit(2);
+    }
+    /*endif*/
+
+    awgn_init_dbm0(&noise_source, 1234567, -10.0f);
+
+    for (type = 1;  type <= 3;  type++)
+    {
+        sampleno = 0;
+        tone_1_present = 0;
+        tone_2_present = 0;
+        ping = 0;
+        munge = NULL;
+        switch (type)
+        {
+        case 1:
+            printf("2280Hz tests.\n");
+            munge = codec_munge_init(MUNGE_CODEC_ALAW, 0);
+            sig_tone_tx_init(&tx_state, SIG_TONE_2280HZ, tx_handler, NULL);
+            sig_tone_rx_init(&rx_state, SIG_TONE_2280HZ, rx_handler, NULL);
+            rx_state.current_rx_tone |= SIG_TONE_RX_PASSTHROUGH;
+            break;
+        case 2:
+            printf("26000Hz tests.\n");
+            munge = codec_munge_init(MUNGE_CODEC_ULAW, 0);
+            sig_tone_tx_init(&tx_state, SIG_TONE_2600HZ, tx_handler, NULL);
+            sig_tone_rx_init(&rx_state, SIG_TONE_2600HZ, rx_handler, NULL);
+            rx_state.current_rx_tone |= SIG_TONE_RX_PASSTHROUGH;
+            break;
+        case 3:
+            printf("2400Hz/26000Hz tests.\n");
+            munge = codec_munge_init(MUNGE_CODEC_ULAW, 0);
+            sig_tone_tx_init(&tx_state, SIG_TONE_2400HZ_2600HZ, tx_handler, NULL);
+            sig_tone_rx_init(&rx_state, SIG_TONE_2400HZ_2600HZ, rx_handler, NULL);
+            rx_state.current_rx_tone |= SIG_TONE_RX_PASSTHROUGH;
+            break;
+        }
+        /*endswitch*/
+    
+        //map_frequency_response(&rx_state);
+
+        for (sampleno = 0;  sampleno < 20000;  sampleno += SAMPLES_PER_CHUNK)
+        {
+            for (i = 0;  i < SAMPLES_PER_CHUNK;  i++)
+                amp[i] = awgn(&noise_source);
+            /*endfor*/
+            tx_samples = sig_tone_tx(&tx_state, amp, SAMPLES_PER_CHUNK);
+            for (i = 0;  i < tx_samples;  i++)
+                out_amp[2*i] = amp[i];
+            /*endfor*/
+            codec_munge(munge, amp, tx_samples);
+            rx_samples = sig_tone_rx(&rx_state, amp, tx_samples);
+            for (i = 0;  i < rx_samples;  i++)
+                out_amp[2*i + 1] = amp[i];
+            /*endfor*/
+            outframes = afWriteFrames(outhandle,
+                                      AF_DEFAULT_TRACK,
+                                      out_amp,
+                                      rx_samples);
+            if (outframes != rx_samples)
+            {
+                fprintf(stderr, "    Error writing wave file\n");
+                exit(2);
+            }
+            /*endif*/
+        }
+        /*endfor*/
+    }
+    /*endfor*/
+    if (afCloseFile(outhandle) != 0)
+    {
+        fprintf(stderr, "    Cannot close wave file '%s'\n", OUT_FILE_NAME);
+        exit(2);
+    }
+    /*endif*/
+    
+    printf("Tests completed.\n");
+    return  0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/tests/super_tone_rx_tests.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/tests/super_tone_rx_tests.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,471 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * super_tone_detect_tests.c
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2003 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: super_tone_rx_tests.c,v 1.28 2008/05/13 13:17:26 steveu Exp $
+ */
+
+/*! \file */
+
+/*! \page super_tone_rx_tests_page Supervisory tone detection tests
+\section super_tone_rx_tests_page_sec_1 What does it do?
+*/
+
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <string.h>
+#include <strings.h>
+#include <ctype.h>
+#include <time.h>
+#include <sys/socket.h>
+#include <audiofile.h>
+
+#if defined(HAVE_LIBXML_XMLMEMORY_H)
+#include <libxml/xmlmemory.h>
+#endif
+#if defined(HAVE_LIBXML_PARSER_H)
+#include <libxml/parser.h>
+#endif
+#if defined(HAVE_LIBXML_XINCLUDE_H)
+#include <libxml/xinclude.h>
+#endif
+
+#include "spandsp.h"
+
+#define IN_FILE_NAME    "super_tone.wav"
+
+#define MITEL_DIR       "../test-data/mitel/"
+#define BELLCORE_DIR	"../test-data/bellcore/"
+
+const char *bellcore_files[] =
+{
+    MITEL_DIR    "mitel-cm7291-talkoff.wav",
+    BELLCORE_DIR "tr-tsy-00763-1.wav",
+    BELLCORE_DIR "tr-tsy-00763-2.wav",
+    BELLCORE_DIR "tr-tsy-00763-3.wav",
+    BELLCORE_DIR "tr-tsy-00763-4.wav",
+    BELLCORE_DIR "tr-tsy-00763-5.wav",
+    BELLCORE_DIR "tr-tsy-00763-6.wav",
+    ""
+};
+
+const char *tone_names[20] = {NULL};
+
+AFfilehandle inhandle;
+
+super_tone_rx_segment_t tone_segments[20][10];
+
+super_tone_tx_step_t *dialtone_tree = NULL;
+super_tone_tx_step_t *ringback_tree = NULL;
+super_tone_tx_step_t *busytone_tree = NULL;
+super_tone_tx_step_t *nutone_tree = NULL;
+super_tone_tx_step_t *congestiontone_tree = NULL;
+super_tone_tx_step_t *waitingtone_tree = NULL;
+
+#if defined(HAVE_LIBXML2)
+static int parse_tone(super_tone_rx_descriptor_t *desc, int tone_id, super_tone_tx_step_t **tree, xmlDocPtr doc, xmlNsPtr ns, xmlNodePtr cur)
+{
+    xmlChar *x;
+    float f1;
+    float f2;
+    float f_tol;
+    float l1;
+    float l2;
+    float length;
+    float length_tol;
+    float recognition_length;
+    float recognition_length_tol;
+    int cycles;
+    super_tone_tx_step_t *treep;
+    int min_duration;
+    int max_duration;
+
+    cur = cur->xmlChildrenNode;
+    while (cur)
+    {
+        if (xmlStrcmp(cur->name, (const xmlChar *) "step") == 0)
+        {
+            printf("Step - ");
+            /* Set some defaults */
+            f1 = 0.0;
+            f2 = 0.0;
+            f_tol = 1.0;
+            l1 = -11.0;
+            l2 = -11.0;
+            length = 0.0;
+            length_tol = 10.0;
+            recognition_length = 0.0;
+            recognition_length_tol = 10.0;
+            cycles = 1;
+            if ((x = xmlGetProp(cur, (const xmlChar *) "freq")))
+            {
+                sscanf((char *) x, "%f [%f%%]", &f1, &f_tol);
+                sscanf((char *) x, "%f+%f [%f%%]", &f1, &f2, &f_tol);
+                printf(" Frequency=%.2f+%.2f [%.2f%%]", f1, f2, f_tol);
+            }
+            if ((x = xmlGetProp(cur, (const xmlChar *) "level")))
+            {
+                if (sscanf((char *) x, "%f+%f", &l1, &l2) < 2)
+                    l2 = l1;
+                printf(" Level=%.2f+%.2f", l1, l2);
+            }
+            if ((x = xmlGetProp(cur, (const xmlChar *) "length")))
+            {
+                sscanf((char *) x, "%f [%f%%]", &length, &length_tol);
+                printf(" Length=%.2f [%.2f%%]", length, length_tol);
+            }
+            if ((x = xmlGetProp(cur, (const xmlChar *) "recognition-length")))
+            {
+                sscanf((char *) x, "%f [%f%%]", &recognition_length, &recognition_length_tol);
+                printf(" Recognition length=%.2f [%.2f%%]", recognition_length, recognition_length_tol);
+            }
+            if ((x = xmlGetProp(cur, (const xmlChar *) "cycles")))
+            {
+                if (strcasecmp((char *) x, "endless") == 0)
+                    cycles = 0;
+                else
+                    cycles = atoi((char *) x);
+                printf(" Cycles='%d' ", cycles);
+            }
+            if ((x = xmlGetProp(cur, (const xmlChar *) "recorded-announcement")))
+                printf(" Recorded announcement='%s'", x);
+            printf("\n");
+            if (f1  ||  f2  ||  length)
+            {
+                /* TODO: This cannot handle cycling patterns */
+                if (length == 0.0)
+                {
+                    if (recognition_length)
+                        min_duration = recognition_length*1000.0 + 0.5;
+                    else
+                        min_duration = 700;
+                    max_duration = 0;
+                }
+                else
+                {
+                    if (recognition_length)
+                        min_duration = recognition_length*1000.0 + 0.5;
+                    else
+                        min_duration = (length*1000.0 + 0.5)*(1.0 - length_tol/100.0) - 30;
+                    max_duration = (length*1000.0 + 0.5)*(1.0 + length_tol/100.0) + 30;
+                }
+                printf(">>>Detector element %10d %10d %10d %10d\n", (int) (f1 + 0.5), (int) (f2 + 0.5), min_duration, max_duration);
+                super_tone_rx_add_element(desc, tone_id, f1 + 0.5, f2 + 0.5, min_duration, max_duration);
+            }
+            treep = super_tone_tx_make_step(NULL,
+                                            f1,
+                                            l1,
+                                            f2,
+                                            l2,
+                                            length*1000.0 + 0.5,
+                                            cycles);
+            *tree = treep;
+            tree = &(treep->next);
+            parse_tone(desc, tone_id, &(treep->nest), doc, ns, cur);
+        }
+        /*endif*/
+        cur = cur->next;
+    }
+    /*endwhile*/
+    return  0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void parse_tone_set(super_tone_rx_descriptor_t *desc, xmlDocPtr doc, xmlNsPtr ns, xmlNodePtr cur)
+{
+    int tone_id;
+
+    printf("Parsing tone set\n");
+    cur = cur->xmlChildrenNode;
+    while (cur)
+    {
+        if (strcmp((char *) cur->name, "dial-tone") == 0)
+        {
+            printf("Hit %s\n", cur->name);
+            tone_id = super_tone_rx_add_tone(desc);
+            dialtone_tree = NULL;
+            parse_tone(desc, tone_id, &dialtone_tree, doc, ns, cur);
+            tone_names[tone_id] = "Dial tone";
+        }
+        else if (strcmp((char *) cur->name, "ringback-tone") == 0)
+        {
+            printf("Hit %s\n", cur->name);
+            tone_id = super_tone_rx_add_tone(desc);
+            ringback_tree = NULL;
+            parse_tone(desc, tone_id, &ringback_tree, doc, ns, cur);
+            tone_names[tone_id] = "Ringback tone";
+        }
+        else if (strcmp((char *) cur->name, "busy-tone") == 0)
+        {
+            printf("Hit %s\n", cur->name);
+            tone_id = super_tone_rx_add_tone(desc);
+            busytone_tree = NULL;
+            parse_tone(desc, tone_id, &busytone_tree, doc, ns, cur);
+            tone_names[tone_id] = "Busy tone";
+        }
+        else if (strcmp((char *) cur->name, "number-unobtainable-tone") == 0)
+        {
+            printf("Hit %s\n", cur->name);
+            tone_id = super_tone_rx_add_tone(desc);
+            nutone_tree = NULL;
+            parse_tone(desc, tone_id, &nutone_tree, doc, ns, cur);
+            tone_names[tone_id] = "NU tone";
+        }
+        else if (strcmp((char *) cur->name, "congestion-tone") == 0)
+        {
+            printf("Hit %s\n", cur->name);
+            tone_id = super_tone_rx_add_tone(desc);
+            congestiontone_tree = NULL;
+            parse_tone(desc, tone_id, &congestiontone_tree, doc, ns, cur);
+            tone_names[tone_id] = "Congestion tone";
+        }
+        else if (strcmp((char *) cur->name, "waiting-tone") == 0)
+        {
+            printf("Hit %s\n", cur->name);
+            tone_id = super_tone_rx_add_tone(desc);
+            waitingtone_tree = NULL;
+            parse_tone(desc, tone_id, &waitingtone_tree, doc, ns, cur);
+            tone_names[tone_id] = "Waiting tone";
+        }
+        /*endif*/
+        cur = cur->next;
+    }
+    /*endwhile*/
+}
+/*- End of function --------------------------------------------------------*/
+
+static void get_tone_set(super_tone_rx_descriptor_t *desc, const char *tone_file, const char *set_id)
+{
+    xmlDocPtr doc;
+    xmlNsPtr ns;
+    xmlNodePtr cur;
+#if 0
+    xmlValidCtxt valid;
+#endif
+    xmlChar *x;
+    
+    ns = NULL;
+    xmlKeepBlanksDefault(0);
+    xmlCleanupParser();
+    doc = xmlParseFile(tone_file);
+    if (doc == NULL)
+    {
+        fprintf(stderr, "No document\n");
+        exit(2);
+    }
+    /*endif*/
+    xmlXIncludeProcess(doc);
+#if 0
+    if (!xmlValidateDocument(&valid, doc))
+    {
+        fprintf(stderr, "Invalid document\n");
+        exit(2);
+    }
+    /*endif*/
+#endif
+    /* Check the document is of the right kind */
+    if ((cur = xmlDocGetRootElement(doc)) == NULL)
+    {
+        fprintf(stderr, "Empty document\n");
+        xmlFreeDoc(doc);
+        exit(2);
+    }
+    /*endif*/
+    if (xmlStrcmp(cur->name, (const xmlChar *) "global-tones"))
+    {
+        fprintf(stderr, "Document of the wrong type, root node != global-tones");
+        xmlFreeDoc(doc);
+        exit(2);
+    }
+    /*endif*/
+    cur = cur->xmlChildrenNode;
+    while (cur  &&  xmlIsBlankNode (cur))
+        cur = cur->next;
+    /*endwhile*/
+    if (cur == NULL)
+        exit(2);
+    /*endif*/
+    while (cur)
+    {
+        if (xmlStrcmp(cur->name, (const xmlChar *) "tone-set") == 0)
+        {
+            if ((x = xmlGetProp(cur, (const xmlChar *) "uncode")))
+            {
+                if (strcmp((char *) x, set_id) == 0)
+                    parse_tone_set(desc, doc, ns, cur);
+            }
+            /*endif*/
+        }
+        /*endif*/
+        cur = cur->next;
+    }
+    /*endwhile*/
+    xmlFreeDoc(doc);
+}
+/*- End of function --------------------------------------------------------*/
+#endif
+
+static void super_tone_rx_fill_descriptor(super_tone_rx_descriptor_t *desc)
+{
+    int tone_id;
+    
+    tone_id = super_tone_rx_add_tone(desc);
+    super_tone_rx_add_element(desc, tone_id, 400, 0, 700, 0);
+    tone_names[tone_id] = "XXX";
+
+    tone_id = super_tone_rx_add_tone(desc);
+    super_tone_rx_add_element(desc, tone_id, 1100, 0, 400, 600);
+    super_tone_rx_add_element(desc, tone_id, 0, 0, 2800, 3200);
+    tone_names[tone_id] = "FAX tone";
+}
+/*- End of function --------------------------------------------------------*/
+
+static void wakeup(void *data, int code, int level, int delay)
+{
+    if (code >= 0)
+        printf("Current tone is %d '%s' '%s'\n", code, (tone_names[code])  ?  tone_names[code]  :  "???", (char *) data);
+    else
+        printf("Tone off '%s'\n", (char *) data);
+}
+/*- End of function --------------------------------------------------------*/
+
+static void tone_segment(void *data, int f1, int f2, int duration)
+{
+    if (f1 < 0)
+        printf("Result %5d silence\n", duration);
+    else if (f2 < 0)
+        printf("Result %5d %4d\n", duration, f1);
+    else
+        printf("Result %5d %4d + %4d\n", duration, f1, f2);
+}
+/*- End of function --------------------------------------------------------*/
+
+int main(int argc, char *argv[])
+{
+    int x;
+    int16_t amp[8000];
+    int sample;
+    int frames;
+    awgn_state_t noise_source;
+    super_tone_rx_state_t *super;
+    super_tone_rx_descriptor_t desc;
+
+    if ((inhandle = afOpenFile(IN_FILE_NAME, "r", 0)) == AF_NULL_FILEHANDLE)
+    {
+        fprintf(stderr, "    Cannot open wave file '%s'\n", IN_FILE_NAME);
+        exit(2);
+    }
+    if ((x = afGetFrameSize(inhandle, AF_DEFAULT_TRACK, 1)) != 2.0)
+    {
+        printf("    Unexpected frame size in wave file '%s'\n", IN_FILE_NAME);
+        exit(2);
+    }
+    if ((x = afGetRate(inhandle, AF_DEFAULT_TRACK)) != (float) SAMPLE_RATE)
+    {
+        printf("    Unexpected sample rate in wave file '%s'\n", IN_FILE_NAME);
+        exit(2);
+    }
+    if ((x = afGetChannels(inhandle, AF_DEFAULT_TRACK)) != 1.0)
+    {
+        printf("    Unexpected number of channels in wave file '%s'\n", IN_FILE_NAME);
+        exit(2);
+    }
+    super_tone_rx_make_descriptor(&desc);
+#if defined(HAVE_LIBXML2)
+    get_tone_set(&desc, "../spandsp/global-tones.xml", (argc > 1)  ?  argv[1]  :  "hk");
+#endif
+    super_tone_rx_fill_descriptor(&desc);
+    if ((super = super_tone_rx_init(NULL, &desc, wakeup, (void *) "test")) == NULL)
+    {
+        printf("    Failed to create detector.\n");
+        exit(2);
+    }
+    super_tone_rx_segment_callback(super, tone_segment);
+    awgn_init_dbm0(&noise_source, 1234567, -30.0f);
+    printf("Processing file\n");
+    while ((frames = afReadFrames(inhandle, AF_DEFAULT_TRACK, amp, 8000)))
+    {
+        /* Add some noise to the signal for a more meaningful test. */
+        //for (sample = 0;  sample < frames;  sample++)
+        //    amp[sample] += saturate(amp[sample] + awgn (&noise_source));
+        for (sample = 0;  sample < frames;  )
+        {
+            x = super_tone_rx(super, amp + sample, frames - sample);
+            sample += x;
+        }
+    }
+    if (afCloseFile(inhandle))
+    {
+        fprintf(stderr, "    Cannot close audio file '%s'\n", IN_FILE_NAME);
+        exit(2);
+    }
+#if 0
+    /* Test for voice immunity */
+    for (j = 0;  bellcore_files[j][0];  j++)
+    {
+        if ((inhandle = afOpenFile(bellcore_files[j], "r", 0)) == AF_NULL_FILEHANDLE)
+        {
+            printf("    Cannot open wave file '%s'\n", bellcore_files[j]);
+            exit(2);
+        }
+        if ((x = afGetFrameSize(inhandle, AF_DEFAULT_TRACK, 1)) != 2.0)
+        {
+            printf("    Unexpected frame size in wave file '%s'\n", bellcore_files[j]);
+            exit(2);
+        }
+        if ((x = afGetRate(inhandle, AF_DEFAULT_TRACK)) != (float) SAMPLE_RATE)
+        {
+            printf("    Unexpected sample rate in wave file '%s'\n", bellcore_files[j]);
+            exit(2);
+        }
+        if ((x = afGetChannels(inhandle, AF_DEFAULT_TRACK)) != 1.0)
+        {
+            printf("    Unexpected number of channels in wave file '%s'\n", bellcore_files[j]);
+            exit(2);
+        }
+        while ((frames = afReadFrames(inhandle, AF_DEFAULT_TRACK, amp, 8000)))
+        {
+            for (sample = 0;  sample < frames;  )
+            {
+                x = super_tone_rx(super, amp + sample, frames - sample);
+                sample += x;
+            }
+    	}
+        if (afCloseFile(inhandle) != 0)
+    	{
+    	    printf("    Cannot close speech file '%s'\n", bellcore_files[j]);
+            exit(2);
+    	}
+    }
+#endif
+    super_tone_rx_free(super);
+    printf("Done\n");
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/tests/super_tone_tx_tests.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/tests/super_tone_tx_tests.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,302 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * super_tone_generate_tests.c
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2003 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: super_tone_tx_tests.c,v 1.23 2008/08/16 15:24:16 steveu Exp $
+ */
+
+/*! \file */
+
+/*! \page super_tone_tx_tests_page Supervisory tone generation tests
+\section super_tone_tx_tests_page_sec_1 What does it do?
+*/
+
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <string.h>
+#include <strings.h>
+#include <ctype.h>
+#include <time.h>
+#include <inttypes.h>
+#include <sys/socket.h>
+#include <audiofile.h>
+
+#if defined(HAVE_LIBXML_XMLMEMORY_H)
+#include <libxml/xmlmemory.h>
+#endif
+#if defined(HAVE_LIBXML_PARSER_H)
+#include <libxml/parser.h>
+#endif
+#if defined(HAVE_LIBXML_XINCLUDE_H)
+#include <libxml/xinclude.h>
+#endif
+
+#include "spandsp.h"
+#include "spandsp-sim.h"
+
+#define OUT_FILE_NAME   "super_tone.wav"
+
+AFfilehandle outhandle;
+
+super_tone_tx_step_t *tone_tree = NULL;
+
+static void play_tones(super_tone_tx_state_t *tone, int max_samples)
+{
+    int16_t amp[8000];
+    int len;
+    int outframes;
+    int i;
+    int total_length;
+
+    i = 500;
+    total_length = 0;
+    do
+    {
+        len = super_tone_tx(tone, amp, 160);
+        outframes = afWriteFrames(outhandle,
+                                  AF_DEFAULT_TRACK,
+                                  amp,
+                                  len);
+        if (outframes != len)
+        {
+            fprintf(stderr, "    Error writing wave file\n");
+            exit(2);
+        }
+        total_length += len;
+    }
+    while (len > 0  &&  --i > 0);
+    printf("Tone length = %d samples (%dms)\n", total_length, total_length/8);
+}
+/*- End of function --------------------------------------------------------*/
+
+#if defined(HAVE_LIBXML2)
+static int parse_tone(super_tone_tx_step_t **tree, xmlDocPtr doc, xmlNsPtr ns, xmlNodePtr cur)
+{
+    xmlChar *x;
+    float f1;
+    float f2;
+    float f_tol;
+    float l1;
+    float l2;
+    float length;
+    float length_tol;
+    int cycles;
+    super_tone_tx_step_t *treep;
+
+    cur = cur->xmlChildrenNode;
+    while (cur)
+    {
+        if (xmlStrcmp(cur->name, (const xmlChar *) "step") == 0)
+        {
+            printf("Step - ");
+            /* Set some defaults */
+            f1 = 0.0;
+            f2 = 0.0;
+            f_tol = 1.0;
+            l1 = -11.0;
+            l2 = -11.0;
+            length = 0.0;
+            length_tol = 10.0;
+            cycles = 1;
+            if ((x = xmlGetProp(cur, (const xmlChar *) "freq")))
+            {
+                sscanf((char *) x, "%f [%f%%]", &f1, &f_tol);
+                sscanf((char *) x, "%f+%f [%f%%]", &f1, &f2, &f_tol);
+                printf("Frequency=%.2f+%.2f [%.2f%%]", f1, f2, f_tol);
+                xmlFree(x);
+            }
+            if ((x = xmlGetProp(cur, (const xmlChar *) "level")))
+            {
+                if (sscanf((char *) x, "%f+%f", &l1, &l2) < 2)
+                    l2 = l1;
+                printf("Level=%.2f+%.2f", l1, l2);
+                xmlFree(x);
+            }
+            if ((x = xmlGetProp(cur, (const xmlChar *) "length")))
+            {
+                sscanf((char *) x, "%f [%f%%]", &length, &length_tol);
+                printf("Length=%.2f [%.2f%%]", length, length_tol);
+                xmlFree(x);
+            }
+            if ((x = xmlGetProp(cur, (const xmlChar *) "recognition-length")))
+            {
+                printf("Recognition length='%s'", x);
+                xmlFree(x);
+            }
+            if ((x = xmlGetProp(cur, (const xmlChar *) "cycles")))
+            {
+                if (strcasecmp((char *) x, "endless") == 0)
+                    cycles = 0;
+                else
+                    cycles = atoi((char *) x);
+                printf("Cycles=%d ", cycles);
+                xmlFree(x);
+            }
+            if ((x = xmlGetProp(cur, (const xmlChar *) "recorded-announcement")))
+            {
+                printf("Recorded announcement='%s'", x);
+                xmlFree(x);
+            }
+            printf("\n");
+            treep = super_tone_tx_make_step(NULL,
+                                            f1,
+                                            l1,
+                                            f2,
+                                            l2,
+                                            length*1000.0 + 0.5,
+                                            cycles);
+            *tree = treep;
+            tree = &(treep->next);
+            parse_tone(&(treep->nest), doc, ns, cur);
+        }
+        /*endif*/
+        cur = cur->next;
+    }
+    /*endwhile*/
+    return  0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void parse_tone_set(xmlDocPtr doc, xmlNsPtr ns, xmlNodePtr cur)
+{
+    super_tone_tx_state_t tone;
+
+    printf("Parsing tone set\n");
+    cur = cur->xmlChildrenNode;
+    while (cur)
+    {
+        if (strcmp((char *) cur->name + strlen((char *) cur->name) - 5, "-tone") == 0)
+        {
+            printf("Hit %s\n", cur->name);
+            tone_tree = NULL;
+            parse_tone(&tone_tree, doc, ns, cur);
+            super_tone_tx_init(&tone, tone_tree);
+//printf("Len %p %p %d %d\n", (void *) tone.levels[0], (void *) tone_tree, tone_tree->length, tone_tree->tone);
+            play_tones(&tone, 99999999);
+            super_tone_tx_free(tone_tree);
+        }
+        /*endif*/
+        cur = cur->next;
+    }
+    /*endwhile*/
+}
+/*- End of function --------------------------------------------------------*/
+
+static void get_tone_set(const char *tone_file, const char *set_id)
+{
+    xmlDocPtr doc;
+    xmlNsPtr ns;
+    xmlNodePtr cur;
+#if 0
+    xmlValidCtxt valid;
+#endif
+    xmlChar *x;
+
+    ns = NULL;    
+    xmlKeepBlanksDefault(0);
+    xmlCleanupParser();
+    doc = xmlParseFile(tone_file);
+    if (doc == NULL)
+    {
+        fprintf(stderr, "No document\n");
+        exit(2);
+    }
+    /*endif*/
+    xmlXIncludeProcess(doc);
+#if 0
+    if (!xmlValidateDocument(&valid, doc))
+    {
+        fprintf(stderr, "Invalid document\n");
+        exit(2);
+    }
+    /*endif*/
+#endif
+    /* Check the document is of the right kind */
+    if ((cur = xmlDocGetRootElement(doc)) == NULL)
+    {
+        fprintf(stderr, "Empty document\n");
+        xmlFreeDoc(doc);
+        exit(2);
+    }
+    /*endif*/
+    if (xmlStrcmp(cur->name, (const xmlChar *) "global-tones"))
+    {
+        fprintf(stderr, "Document of the wrong type, root node != global-tones");
+        xmlFreeDoc(doc);
+        exit(2);
+    }
+    /*endif*/
+    cur = cur->xmlChildrenNode;
+    while (cur  &&  xmlIsBlankNode(cur))
+        cur = cur->next;
+    /*endwhile*/
+    if (cur == NULL)
+        exit(2);
+    /*endif*/
+    while (cur)
+    {
+        if (xmlStrcmp(cur->name, (const xmlChar *) "tone-set") == 0)
+        {
+            if ((x = xmlGetProp(cur, (const xmlChar *) "uncode")))
+            {
+                if (strcmp((char *) x, set_id) == 0)
+                    parse_tone_set(doc, ns, cur);
+                /*endif*/
+                xmlFree(x);
+            }
+            /*endif*/
+        }
+        /*endif*/
+        cur = cur->next;
+    }
+    /*endwhile*/
+    xmlFreeDoc(doc);
+}
+/*- End of function --------------------------------------------------------*/
+#endif
+
+int main(int argc, char *argv[])
+{
+    if ((outhandle = afOpenFile_telephony_write(OUT_FILE_NAME, 1)) == AF_NULL_FILEHANDLE)
+    {
+        fprintf(stderr, "    Cannot open audio file '%s'\n", OUT_FILE_NAME);
+        exit(2);
+    }
+#if defined(HAVE_LIBXML2)
+    get_tone_set("../spandsp/global-tones.xml", (argc > 1)  ?  argv[1]  :  "hk");
+#endif
+    if (afCloseFile (outhandle) != 0)
+    {
+        fprintf(stderr, "    Cannot close audio file '%s'\n", OUT_FILE_NAME);
+        exit(2);
+    }
+    printf("Done\n");
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/tests/t31_tests.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/tests/t31_tests.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,736 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * t31_tests.c - Tests for the T.31 modem.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2004 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: t31_tests.c,v 1.62 2008/08/29 09:28:13 steveu Exp $
+ */
+
+/*! \file */
+
+/*! \page t31_tests_page T.31 tests
+\section t31_tests_page_sec_1 What does it do?
+*/
+
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <string.h>
+#include <assert.h>
+#include <audiofile.h>
+
+#include "spandsp.h"
+#include "spandsp/t30_fcf.h"
+#include "spandsp-sim.h"
+
+#if defined(ENABLE_GUI)
+#include "media_monitor.h"
+#endif
+
+#define INPUT_FILE_NAME         "../test-data/itu/fax/itu1.tif"
+#define OUTPUT_FILE_NAME        "t31.tif"
+#define OUTPUT_WAVE_FILE_NAME   "t31_tests.wav"
+
+#define DLE 0x10
+#define ETX 0x03
+#define SUB 0x1A
+
+#define MANUFACTURER            "www.soft-switch.org"
+
+#define SAMPLES_PER_CHUNK 160
+
+struct command_response_s
+{
+    const char *command;
+    int len_command;
+    const char *response;
+    int len_response;
+};
+
+g1050_state_t *path_a_to_b;
+g1050_state_t *path_b_to_a;
+
+double when = 0.0;
+
+#define EXCHANGE(a,b) {a, sizeof(a) - 1, b, sizeof(b) - 1}
+#define RESPONSE(b) {"", 0, b, sizeof(b) - 1}
+#define FAST_RESPONSE(b) {NULL, -1, b, sizeof(b) - 1}
+#define FAST_SEND(b) {(const char *) 1, -2, b, sizeof(b) - 1}
+
+static const struct command_response_s fax_send_test_seq[] =
+{
+    EXCHANGE("ATE0\r", "ATE0\r\r\nOK\r\n"),
+    EXCHANGE("AT+FCLASS=1\r", "\r\nOK\r\n"),
+    EXCHANGE("ATD123456789\r", "\r\nCONNECT\r\n"),
+    //<NSF frame>         AT+FRH=3 is implied when dialing in the AT+FCLASS=1 state
+    //RESPONSE("\xFF\x03\x10\x03"),
+    //RESPONSE("\r\nOK\r\n"),
+    //EXCHANGE("AT+FRH=3\r", "\r\nCONNECT\r\n"),
+    //<CSI frame data>
+    RESPONSE("\xFF\x03\x40\x31\x31\x31\x31\x31\x31\x31\x31\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x1e\x46\x10\x03"),
+    RESPONSE("\r\nOK\r\n"),
+    EXCHANGE("AT+FRH=3\r", "\r\nCONNECT\r\n"),
+    //<DIS frame data>
+    RESPONSE("\xFF\x13\x80\x00\xEE\xF8\x80\x80\x91\x80\x80\x80\x18\x78\x57\x10\x03"),
+    RESPONSE("\r\nOK\r\n"),
+    //EXCHANGE("AT+FRH=3\r", "\r\nNO CARRIER\r\n"),
+    EXCHANGE("AT+FTH=3\r", "\r\nCONNECT\r\n"),
+    //<TSI frame data>
+    EXCHANGE("\xFF\x03\x43\x32\x32\x32\x32\x32\x32\x32\x32\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x10\x03", "\r\nCONNECT\r\n"),
+    //<DCS frame data>
+    EXCHANGE("\xFF\x13\x83\x01\xC6\x80\x80\x80\x80\x01\xFD\x13\x10\x03", "\r\nOK\r\n"),
+    //Do a wait for timed silence at this point, or there won't be one in the tests
+    EXCHANGE("AT+FRS=7\r", "\r\nOK\r\n"),
+    //EXCHANGE("AT+FTS=8;+FTM=96\r", "\r\nCONNECT\r\n"),
+    EXCHANGE("AT+FTS=8\r", "\r\nOK\r\n"),
+    EXCHANGE("AT+FTM=96\r", "\r\nCONNECT\r\n"),
+    //<TCF data pattern>
+    FAST_SEND("\r\nOK\r\n"),
+    EXCHANGE("AT+FRH=3\r", "\r\nCONNECT\r\n"),
+    //<CFR frame data>
+    RESPONSE("\xFF\x13\x84\xEA\x7D\x10\x03"),
+    RESPONSE("\r\nOK\r\n"),
+    EXCHANGE("AT+FTM=96\r", "\r\nCONNECT\r\n"),
+    //<page image data>
+    FAST_SEND("\r\nOK\r\n"),
+    //EXCHANGE("AT+FTS=8;+FTH=3\r", "\r\nCONNECT\r\n"),
+    EXCHANGE("AT+FTS=8\r", "\r\nOK\r\n"),
+    EXCHANGE("AT+FTH=3\r", "\r\nCONNECT\r\n"),
+    //<EOP frame data>
+    EXCHANGE("\xFF\x13\x2E\x10\x03", "\r\nOK\r\n"),
+    EXCHANGE("AT+FRH=3\r", "\r\nCONNECT\r\n"),
+    //<MCF frame data>
+    RESPONSE("\xFF\x13\x8C\xA2\xF1\x10\x03"),
+    RESPONSE("\r\nOK\r\n"),
+    EXCHANGE("AT+FTH=3\r", "\r\nCONNECT\r\n"),
+    // <DCN frame data>
+    EXCHANGE("\xFF\x13\xFB\x10\x03", "\r\nOK\r\n"),
+    EXCHANGE("ATH0\r", "\r\nOK\r\n")
+};
+
+static const struct command_response_s fax_receive_test_seq[] =
+{
+    EXCHANGE("ATE0\r", "ATE0\r\r\nOK\r\n"),
+    EXCHANGE("AT+FCLASS=1\r", "\r\nOK\r\n"),
+    RESPONSE("\r\nRING\r\n"),
+    EXCHANGE("ATA\r", "\r\nCONNECT\r\n"),
+    //<CSI frame data>
+    EXCHANGE("\xFF\x03\x40\x32\x32\x32\x32\x32\x32\x32\x32\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x10\x03", "\r\nCONNECT\r\n"),
+    //<DIS frame data>
+    EXCHANGE("\xFF\x13\x80\x01\xCE\xF4\x80\x80\x81\x80\x80\x80\x18\x10\x03", "\r\nOK\r\n"),
+    EXCHANGE("AT+FRH=3\r", "\r\nCONNECT\r\n"),
+    //<TSI frame data>
+    RESPONSE("\xFF\x03\x43\x31\x31\x31\x31\x31\x31\x31\x31\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\x20\xAA\x1F\x10\x03"),
+    RESPONSE("\r\nOK\r\n"),
+    EXCHANGE("AT+FRH=3\r", "\r\nCONNECT\r\n"),
+    //<DCS frame data>
+    RESPONSE("\xFF\x13\x83\x00\xC6\x74\x53\x00\x10\x03"),
+    RESPONSE("\r\nOK\r\n"),
+    EXCHANGE("AT+FRM=96\r", "\r\nCONNECT\r\n"),
+    //<TCF data>
+    FAST_RESPONSE(NULL),
+    RESPONSE("\r\nNO CARRIER\r\n"),
+    EXCHANGE("AT+FTH=3\r", "\r\nCONNECT\r\n"),
+    //<CFR frame data>
+    EXCHANGE("\xFF\x13\x84\x10\x03", "\r\nOK\r\n"),
+    EXCHANGE("AT+FRM=96\r", "\r\nCONNECT\r\n"),
+    //<page image data>
+    FAST_RESPONSE(NULL),
+    RESPONSE("\r\nNO CARRIER\r\n"),
+    EXCHANGE("AT+FRH=3\r", "\r\nCONNECT\r\n"),
+    //<EOP frame data>
+    RESPONSE("\xFF\x13\x2F\x33\x66\x10\x03"),
+    RESPONSE("\r\nOK\r\n"),
+    EXCHANGE("AT+FTH=3\r", "\r\nCONNECT\r\n"),
+    //<MCF frame data>
+    EXCHANGE("\xFF\x13\x8C\x10\x03", "\r\nOK\r\n"),
+    EXCHANGE("AT+FRH=3\r", "\r\nCONNECT\r\n"),
+    //<DCN frame data>
+    RESPONSE("\xFF\x13\xfb\x9a\xf6\x10\x03"),
+    RESPONSE("\r\nOK\r\n"),
+    EXCHANGE("ATH0\r", "\r\nOK\r\n")
+};
+
+char *decode_test_file = NULL;
+int countdown = 0;
+int command_response_test_step = -1;
+char response_buf[1000];
+int response_buf_ptr = 0;
+int answered = FALSE;
+int kick = FALSE;
+int dled = FALSE;
+int done = FALSE;
+
+static const struct command_response_s *fax_test_seq;
+
+int test_seq_ptr = 0;
+
+t31_state_t t31_state;
+
+static int phase_b_handler(t30_state_t *s, void *user_data, int result)
+{
+    int i;
+    
+    i = (intptr_t) user_data;
+    printf("Phase B handler on channel %d - (0x%X) %s\n", i, result, t30_frametype(result));
+    return T30_ERR_OK;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int phase_d_handler(t30_state_t *s, void *user_data, int result)
+{
+    int i;
+    t30_stats_t t;
+    const char *u;
+
+    i = (int) (intptr_t) user_data;
+    printf("%c: Phase D handler on channel %c - (0x%X) %s\n", i, i, result, t30_frametype(result));
+    t30_get_transfer_statistics(s, &t);
+    printf("%c: Phase D: bit rate %d\n", i, t.bit_rate);
+    printf("%c: Phase D: ECM %s\n", i, (t.error_correcting_mode)  ?  "on"  :  "off");
+    printf("%c: Phase D: pages transferred %d\n", i, t.pages_transferred);
+    printf("%c: Phase D: image size %d x %d\n", i, t.width, t.length);
+    printf("%c: Phase D: image resolution %d x %d\n", i, t.x_resolution, t.y_resolution);
+    printf("%c: Phase D: bad rows %d\n", i, t.bad_rows);
+    printf("%c: Phase D: longest bad row run %d\n", i, t.longest_bad_row_run);
+    printf("%c: Phase D: coding method %s\n", i, t4_encoding_to_str(t.encoding));
+    printf("%c: Phase D: image size %d\n", i, t.image_size);
+    if ((u = t30_get_tx_ident(s)))
+        printf("%d: Phase D: local ident '%s'\n", i, u);
+    if ((u = t30_get_rx_ident(s)))
+        printf("%d: Phase D: remote ident '%s'\n", i, u);
+    printf("%c: Phase D: bits per row - min %d, max %d\n", i, s->t4.min_row_bits, s->t4.max_row_bits);
+    return T30_ERR_OK;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void phase_e_handler(t30_state_t *s, void *user_data, int result)
+{
+    int i;
+    
+    i = (intptr_t) user_data;
+    printf("Phase E handler on channel %d\n", i);
+    //exit(0);
+}
+/*- End of function --------------------------------------------------------*/
+
+static int modem_call_control(t31_state_t *s, void *user_data, int op, const char *num)
+{
+    switch (op)
+    {
+    case AT_MODEM_CONTROL_ANSWER:
+        printf("\nModem control - Answering\n");
+        answered = TRUE;
+        break;
+    case AT_MODEM_CONTROL_CALL:
+        printf("\nModem control - Dialing '%s'\n", num);
+        t31_call_event(&t31_state, AT_CALL_EVENT_CONNECTED);
+        break;
+    case AT_MODEM_CONTROL_HANGUP:
+        printf("\nModem control - Hanging up\n");
+        done = TRUE;
+        break;
+    case AT_MODEM_CONTROL_OFFHOOK:
+        printf("\nModem control - Going off hook\n");
+        break;
+    case AT_MODEM_CONTROL_DTR:
+        printf("\nModem control - DTR %d\n", (int) (intptr_t) num);
+        break;
+    case AT_MODEM_CONTROL_RTS:
+        printf("\nModem control - RTS %d\n", (int) (intptr_t) num);
+        break;
+    case AT_MODEM_CONTROL_CTS:
+        printf("\nModem control - CTS %d\n", (int) (intptr_t) num);
+        break;
+    case AT_MODEM_CONTROL_CAR:
+        printf("\nModem control - CAR %d\n", (int) (intptr_t) num);
+        break;
+    case AT_MODEM_CONTROL_RNG:
+        printf("\nModem control - RNG %d\n", (int) (intptr_t) num);
+        break;
+    case AT_MODEM_CONTROL_DSR:
+        printf("\nModem control - DSR %d\n", (int) (intptr_t) num);
+        break;
+    default:
+        printf("\nModem control - operation %d\n", op);
+        break;
+    }
+    /*endswitch*/
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int at_tx_handler(at_state_t *s, void *user_data, const uint8_t *buf, size_t len)
+{
+    size_t i;
+
+    i = 0;
+    if (fax_test_seq[test_seq_ptr].command == NULL)
+    {
+        /* TCF or non-ECM image data expected */
+        for (  ;  i < len;  i++)
+        {
+            if (dled)
+            {
+                if (buf[i] == ETX)
+                {
+                    printf("\nFast data ended\n");
+                    response_buf_ptr = 0;
+                    response_buf[response_buf_ptr] = '\0';
+                    test_seq_ptr++;
+                    if (fax_test_seq[test_seq_ptr].command)
+                        kick = TRUE;
+                    break;
+                }
+                dled = FALSE;
+            }
+            else
+            {
+                if (buf[i] == DLE)
+                    dled = TRUE;
+            }
+        }
+        i++;
+        if (i >= len)
+            return 0;
+    }
+    for (  ;  i < len;  i++)
+    {
+        response_buf[response_buf_ptr++] = buf[i];
+        putchar(buf[i]);
+    }
+    response_buf[response_buf_ptr] = '\0';
+    printf("Expected ");
+    for (i = 0;  i < response_buf_ptr;  i++)
+        printf("%02x ", fax_test_seq[test_seq_ptr].response[i] & 0xFF);
+    printf("\n");
+    printf("Response ");
+    for (i = 0;  i < response_buf_ptr;  i++)
+        printf("%02x ", response_buf[i] & 0xFF);
+    printf("\n");
+printf("Match %d against %d\n", response_buf_ptr, fax_test_seq[test_seq_ptr].len_response);
+    if (response_buf_ptr >= fax_test_seq[test_seq_ptr].len_response
+        &&
+        memcmp(fax_test_seq[test_seq_ptr].response, response_buf, fax_test_seq[test_seq_ptr].len_response) == 0)
+    {
+        printf("\nMatched\n");
+        test_seq_ptr++;
+        response_buf_ptr = 0;
+        response_buf[response_buf_ptr] = '\0';
+        if (fax_test_seq[test_seq_ptr].command)
+            kick = TRUE;
+        else
+            dled = FALSE;
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int tx_packet_handler(t38_core_state_t *s, void *user_data, const uint8_t *buf, int len, int count)
+{
+    t38_terminal_state_t *t;
+    int i;
+
+    /* This routine queues messages between two instances of T.38 processing */
+    t = (t38_terminal_state_t *) user_data;
+    span_log(&s->logging, SPAN_LOG_FLOW, "Send seq %d, len %d, count %d\n", s->tx_seq_no, len, count);
+
+    for (i = 0;  i < count;  i++)
+    {
+        if (g1050_put(path_a_to_b, buf, len, s->tx_seq_no, when) < 0)
+            printf("Lost packet %d\n", s->tx_seq_no);
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int t38_tests(int use_gui, int test_sending)
+{
+    t38_terminal_state_t t38_state;
+    int fast_send;
+    int fast_blocks;
+    uint8_t fast_buf[1000];
+    t30_state_t *t30;
+
+    if (t31_init(&t31_state, at_tx_handler, NULL, modem_call_control, NULL, NULL, NULL) == NULL)
+    {
+        fprintf(stderr, "    Cannot start the T.31 FAX modem\n");
+        exit(2);
+    }
+    span_log_set_level(&t31_state.logging, SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_SHOW_TAG | SPAN_LOG_FLOW);
+    span_log_set_tag(&t31_state.logging, "T.31");
+
+
+    if (t38_terminal_init(&t38_state, TRUE, tx_packet_handler, &t31_state) == NULL)
+    {
+        fprintf(stderr, "Cannot start the T.38 channel\n");
+        exit(2);
+    }
+    t30 = t38_terminal_get_t30_state(&t38_state);
+    span_log_set_level(&t38_state.logging, SPAN_LOG_DEBUG | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME);
+    span_log_set_tag(&t38_state.logging, "T.38");
+    span_log_set_level(&t38_state.t38_fe.t38.logging, SPAN_LOG_DEBUG | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME);
+    span_log_set_tag(&t38_state.t38_fe.t38.logging, "T.38-A");
+    span_log_set_level(&t30->logging, SPAN_LOG_DEBUG | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME);
+    span_log_set_tag(&t30->logging, "T.38");
+
+    t30_set_tx_ident(t30, "11111111");
+    t30_set_tx_nsf(t30, (const uint8_t *) "\x50\x00\x00\x00Spandsp\x00", 12);
+    t30_set_tx_file(t30, INPUT_FILE_NAME, -1, -1);
+    t30_set_phase_b_handler(t30, phase_b_handler, (void *) (intptr_t) 'A');
+    t30_set_phase_d_handler(t30, phase_d_handler, (void *) (intptr_t) 'A');
+    t30_set_phase_e_handler(t30, phase_e_handler, (void *) (intptr_t) 'A');
+    //t30_set_ecm_capability(t30, use_ecm);
+    //if (use_ecm)
+    //    t30_set_supported_compressions(t30, T30_SUPPORT_T4_1D_COMPRESSION | T30_SUPPORT_T4_2D_COMPRESSION | T30_SUPPORT_T6_COMPRESSION);
+
+    fast_send = FALSE;
+    fast_blocks = 0;
+    kick = TRUE;
+#if defined(ENABLE_GUI)
+    if (use_gui)
+        start_media_monitor();
+#endif
+    while (!done)
+    {
+        if (kick)
+        {
+            kick = FALSE;
+            if (fax_test_seq[test_seq_ptr].command > (const char *) 1)
+            {
+                if (fax_test_seq[test_seq_ptr].command[0])
+                {
+                    printf("%s\n", fax_test_seq[test_seq_ptr].command);
+                    t31_at_rx(&t31_state, fax_test_seq[test_seq_ptr].command, fax_test_seq[test_seq_ptr].len_command);
+                }
+            }
+            else
+            {
+                printf("Fast send\n");
+                fast_send = TRUE;
+                fast_blocks = 100;
+            }
+        }
+        if (fast_send)
+        {
+            /* Send fast modem data */
+            memset(fast_buf, 0, 36);
+            if (fast_blocks == 1)
+            {
+                /* Insert EOLs */
+                fast_buf[35] = ETX;
+                fast_buf[34] = DLE;
+                fast_buf[31] =
+                fast_buf[28] =
+                fast_buf[25] =
+                fast_buf[22] =
+                fast_buf[19] =
+                fast_buf[16] = 1;
+            }
+            t31_at_rx(&t31_state, (char *) fast_buf, 36);
+            if (--fast_blocks == 0)
+                fast_send = FALSE;
+        }
+        //t30_len = fax_tx(&fax_state, t30_amp, SAMPLES_PER_CHUNK);
+        //if (t31_rx(&t31_state, t30_amp, t30_len))
+        //    break;
+        if (countdown)
+        {
+            if (answered)
+            {
+                countdown = 0;
+                t31_call_event(&t31_state, AT_CALL_EVENT_ANSWERED);
+            }
+            else if (--countdown == 0)
+            {
+                t31_call_event(&t31_state, AT_CALL_EVENT_ALERTING);
+                countdown = 250;
+            }
+        }
+
+        //t31_len = t31_tx(&t31_state, t31_amp, SAMPLES_PER_CHUNK);
+        //if (fax_rx(&fax_state, t31_amp, SAMPLES_PER_CHUNK))
+        //    break;
+#if defined(ENABLE_GUI)
+        if (use_gui)
+            media_monitor_update_display();
+#endif
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int t30_tests(int log_audio, int test_sending)
+{
+    int k;
+    int outframes;
+    fax_state_t fax_state;
+    int16_t t30_amp[SAMPLES_PER_CHUNK];
+    int16_t t31_amp[SAMPLES_PER_CHUNK];
+    int16_t silence[SAMPLES_PER_CHUNK];
+    int16_t out_amp[2*SAMPLES_PER_CHUNK];
+    int t30_len;
+    int t31_len;
+    AFfilehandle wave_handle;
+    AFfilehandle in_handle;
+    int fast_send;
+    int fast_blocks;
+    uint8_t fast_buf[1000];
+    t30_state_t *t30;
+
+    wave_handle = AF_NULL_FILEHANDLE;
+    if (log_audio)
+    {
+        if ((wave_handle = afOpenFile_telephony_write(OUTPUT_WAVE_FILE_NAME, 2)) == AF_NULL_FILEHANDLE)
+        {
+            fprintf(stderr, "    Cannot create wave file '%s'\n", OUTPUT_WAVE_FILE_NAME);
+            exit(2);
+        }
+    }
+
+    memset(silence, 0, sizeof(silence));
+ 
+    in_handle = NULL;
+    if (decode_test_file)
+    {
+        if ((in_handle = afOpenFile_telephony_read(decode_test_file, 1)) == AF_NULL_FILEHANDLE)
+        {
+            fprintf(stderr, "    Cannot create wave file '%s'\n", decode_test_file);
+            exit(2);
+        }
+    }
+
+    if (t31_init(&t31_state, at_tx_handler, NULL, modem_call_control, NULL, NULL, NULL) == NULL)
+    {
+        fprintf(stderr, "    Cannot start the T.31 FAX modem\n");
+        exit(2);
+    }
+    span_log_set_level(&t31_state.logging, SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_SHOW_TAG | SPAN_LOG_FLOW);
+    span_log_set_tag(&t31_state.logging, "T.31");
+
+    if (test_sending)
+    {
+        fax_init(&fax_state, FALSE);
+        t30 = fax_get_t30_state(&fax_state);
+        t30_set_rx_file(t30, OUTPUT_FILE_NAME, -1);
+        fax_test_seq = fax_send_test_seq;
+        countdown = 0;
+    }
+    else
+    {
+        fax_init(&fax_state, TRUE);
+        t30 = fax_get_t30_state(&fax_state);
+        t30_set_tx_file(t30, INPUT_FILE_NAME, -1, -1);
+        fax_test_seq = fax_receive_test_seq;
+        countdown = 250;
+    }
+    
+    t30_set_tx_ident(t30, "11111111");
+    t30_set_supported_modems(t30, T30_SUPPORT_V27TER | T30_SUPPORT_V29 | T30_SUPPORT_V17);
+    t30_set_phase_b_handler(t30, phase_b_handler, (void *) 0);
+    t30_set_phase_d_handler(t30, phase_d_handler, (void *) 0);
+    t30_set_phase_e_handler(t30, phase_e_handler, (void *) 0);
+    memset(t30_amp, 0, sizeof(t30_amp));
+    
+    span_log_set_level(&t30->logging, SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_SHOW_TAG | SPAN_LOG_FLOW);
+    span_log_set_tag(&t30->logging, "FAX");
+    span_log_set_level(&fax_state.logging, SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_SHOW_TAG | SPAN_LOG_FLOW);
+    span_log_set_tag(&fax_state.logging, "FAX");
+
+    fast_send = FALSE;
+    fast_blocks = 0;
+    kick = TRUE;
+    while (!done)
+    {
+        if (kick)
+        {
+            kick = FALSE;
+            if (fax_test_seq[test_seq_ptr].command > (const char *) 1)
+            {
+                if (fax_test_seq[test_seq_ptr].command[0])
+                {
+                    printf("%s\n", fax_test_seq[test_seq_ptr].command);
+                    t31_at_rx(&t31_state, fax_test_seq[test_seq_ptr].command, fax_test_seq[test_seq_ptr].len_command);
+                }
+            }
+            else
+            {
+                printf("Fast send\n");
+                fast_send = TRUE;
+                fast_blocks = 100;
+            }
+        }
+        if (fast_send)
+        {
+            /* Send fast modem data */
+            memset(fast_buf, 0, 36);
+            if (fast_blocks == 1)
+            {
+                /* Insert EOLs */
+                fast_buf[35] = ETX;
+                fast_buf[34] = DLE;
+                fast_buf[31] =
+                fast_buf[28] =
+                fast_buf[25] =
+                fast_buf[22] =
+                fast_buf[19] =
+                fast_buf[16] = 1;
+            }
+            t31_at_rx(&t31_state, (char *) fast_buf, 36);
+            if (--fast_blocks == 0)
+                fast_send = FALSE;
+        }
+        t30_len = fax_tx(&fax_state, t30_amp, SAMPLES_PER_CHUNK);
+        /* The receive side always expects a full block of samples, but the
+           transmit side may not be sending any when it doesn't need to. We
+           may need to pad with some silence. */
+        if (t30_len < SAMPLES_PER_CHUNK)
+        {
+            memset(t30_amp + t30_len, 0, sizeof(int16_t)*(SAMPLES_PER_CHUNK - t30_len));
+            t30_len = SAMPLES_PER_CHUNK;
+        }
+        if (log_audio)
+        {
+            for (k = 0;  k < t30_len;  k++)
+                out_amp[2*k] = t30_amp[k];
+        }
+        if (t31_rx(&t31_state, t30_amp, t30_len))
+            break;
+        if (countdown)
+        {
+            if (answered)
+            {
+                countdown = 0;
+                t31_call_event(&t31_state, AT_CALL_EVENT_ANSWERED);
+            }
+            else if (--countdown == 0)
+            {
+                t31_call_event(&t31_state, AT_CALL_EVENT_ALERTING);
+                countdown = 250;
+            }
+        }
+
+        t31_len = t31_tx(&t31_state, t31_amp, SAMPLES_PER_CHUNK);
+        if (t31_len < SAMPLES_PER_CHUNK)
+        {
+            memset(t31_amp + t31_len, 0, sizeof(int16_t)*(SAMPLES_PER_CHUNK - t31_len));
+            t31_len = SAMPLES_PER_CHUNK;
+        }
+        if (log_audio)
+        {
+            for (k = 0;  k < t31_len;  k++)
+                out_amp[2*k + 1] = t31_amp[k];
+        }
+        if (fax_rx(&fax_state, t31_amp, SAMPLES_PER_CHUNK))
+            break;
+
+        if (log_audio)
+        {
+            outframes = afWriteFrames(wave_handle, AF_DEFAULT_TRACK, out_amp, SAMPLES_PER_CHUNK);
+            if (outframes != SAMPLES_PER_CHUNK)
+                break;
+        }
+    }
+    if (decode_test_file)
+    {
+        if (afCloseFile(in_handle) != 0)
+        {
+            fprintf(stderr, "    Cannot close wave file '%s'\n", decode_test_file);
+            exit(2);
+        }
+    }
+    if (log_audio)
+    {
+        if (afCloseFile(wave_handle) != 0)
+        {
+            fprintf(stderr, "    Cannot close wave file '%s'\n", OUTPUT_WAVE_FILE_NAME);
+            exit(2);
+        }
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int main(int argc, char *argv[])
+{
+    int log_audio;
+    int t38_mode;
+    int test_sending;
+    int use_gui;
+    int opt;
+
+    decode_test_file = NULL;
+    log_audio = FALSE;
+    test_sending = FALSE;
+    t38_mode = FALSE;
+    use_gui = FALSE;
+    while ((opt = getopt(argc, argv, "d:glrst")) != -1)
+    {
+        switch (opt)
+        {
+        case 'd':
+            decode_test_file = optarg;
+            break;
+        case 'g':
+#if defined(ENABLE_GUI)
+            use_gui = TRUE;
+#else
+            fprintf(stderr, "Graphical monitoring not available\n");
+            exit(2);
+#endif
+            break;
+        case 'l':
+            log_audio = TRUE;
+            break;
+        case 'r':
+            test_sending = FALSE;
+            break;
+        case 's':
+            test_sending = TRUE;
+            break;
+        case 't':
+            t38_mode = TRUE;
+            break;
+        default:
+            //usage();
+            exit(2);
+            break;
+        }
+    }
+
+    if (t38_mode)
+        t38_tests(use_gui, test_sending);
+    else
+        t30_tests(log_audio, test_sending);
+    if (done)
+    {
+        printf("Tests passed\n");
+    }
+    return  0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/tests/t38_core_tests.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/tests/t38_core_tests.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,309 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * t38_core_tests.c - Tests for the T.38 FoIP core module.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2007 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: t38_core_tests.c,v 1.13 2008/05/13 13:17:26 steveu Exp $
+ */
+
+/*! \file */
+
+/*! \page t38_core_tests_page T.38 core tests
+\section t38_core_tests_page_sec_1 What does it do?
+These tests exercise the T.38 core ASN.1 processing code.
+*/
+
+#if defined(HAVE_CONFIG_H)
+#include <config.h>
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <string.h>
+#include <assert.h>
+#include <errno.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <netinet/tcp.h>
+#if !defined(__USE_MISC)
+#define __USE_MISC
+#endif
+#include <arpa/inet.h>
+#include <sys/select.h>
+#include <sys/time.h>
+#include <audiofile.h>
+
+#include "spandsp.h"
+
+#define MAX_FIELDS      42
+#define MAX_FIELD_LEN   8192
+
+int t38_version;
+int succeeded = TRUE;
+int ok_indicator_packets;
+int bad_indicator_packets;
+int ok_data_packets;
+int bad_data_packets;
+int missing_packets;
+
+int current_indicator;
+int current_data_type;
+int current_field_type;
+int skip;
+
+uint8_t field_body[MAX_FIELDS][MAX_FIELD_LEN];
+int field_len[MAX_FIELDS];
+
+static int rx_missing_handler(t38_core_state_t *s, void *user_data, int rx_seq_no, int expected_seq_no)
+{
+    missing_packets++;
+    //printf("Hit missing\n");
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int rx_indicator_handler(t38_core_state_t *s, void *user_data, int indicator)
+{
+    if (indicator == current_indicator)
+        ok_indicator_packets++;
+    else
+        bad_indicator_packets++;
+    //printf("Hit indicator %d\n", indicator);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int rx_data_handler(t38_core_state_t *s, void *user_data, int data_type, int field_type, const uint8_t *buf, int len)
+{
+    if (--skip >= 0)
+    {
+        if (data_type == current_data_type  &&  field_type == current_field_type)
+            ok_data_packets++;
+        else
+            bad_data_packets++;
+    }
+    else
+    {
+        if (data_type == current_data_type  &&  field_type == T38_FIELD_T4_NON_ECM_SIG_END)
+            ok_data_packets++;
+        else
+            bad_data_packets++;
+    }
+    //printf("Hit data %d, field %d\n", data_type, field_type);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int tx_packet_handler(t38_core_state_t *s, void *user_data, const uint8_t *buf, int len, int count)
+{
+    t38_core_state_t *t;
+    static int seq_no = 0;
+    
+    t = (t38_core_state_t *) user_data;
+    span_log(&s->logging, SPAN_LOG_FLOW, "Send seq %d, len %d, count %d\n", s->tx_seq_no, len, count);
+    if (t38_core_rx_ifp_packet(t, buf, len, seq_no) < 0)
+        succeeded = FALSE;
+    seq_no++;
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int encode_decode_tests(t38_core_state_t *a, t38_core_state_t *b)
+{
+    t38_data_field_t field[MAX_FIELDS];
+    int i;
+    int j;
+    
+    ok_indicator_packets = 0;
+    bad_indicator_packets = 0;
+    ok_data_packets = 0;
+    bad_data_packets = 0;
+    missing_packets = 0;
+
+    /* Try all the indicator types */
+    for (i = 0;  i < 100;  i++)
+    {
+        current_indicator = i;
+        if (t38_core_send_indicator(a, i, 3) < 0)
+            break;
+    }
+
+    /* Try all the data types, as single field messages with no data */
+    for (i = 0;  i < 100;  i++)
+    {
+        for (j = 0;  j < 100;  j++)
+        {
+            current_data_type = i;
+            current_field_type = j;
+            skip = 99;
+            if (t38_core_send_data(a, i, j, (uint8_t *) "", 0, 1) < 0)
+                break;
+        }
+        if (j == 0)
+            break;
+    }
+
+    /* Try all the data types and field types, as single field messages with data */
+    for (i = 0;  i < 100;  i++)
+    {
+        for (j = 0;  j < 100;  j++)
+        {
+            current_data_type = i;
+            current_field_type = j;
+            skip = 99;
+            if (t38_core_send_data(a, i, j, (uint8_t *) "ABCD", 4, 1) < 0)
+                break;
+        }
+        if (j == 0)
+            break;
+    }
+
+    /* Try all the data types and field types, as multi-field messages */
+    for (i = 0;  i < 100;  i++)
+    {
+        for (j = 0;  j < 100;  j++)
+        {
+            current_data_type = i;
+            current_field_type = j;
+            skip = 1;
+
+            field_len[0] = 444;
+            field_len[1] = 333;
+
+            field[0].field_type = j;
+            field[0].field = field_body[0];
+            field[0].field_len = field_len[0];
+            field[1].field_type = T38_FIELD_T4_NON_ECM_SIG_END;
+            field[1].field = field_body[1];
+            field[1].field_len = field_len[1];
+            if (t38_core_send_data_multi_field(a, i, field, 2, 1) < 0)
+                break;
+        }
+        if (j == 0)
+            break;
+    }
+    printf("Indicator packets: OK = %d, bad = %d\n", ok_indicator_packets, bad_indicator_packets);
+    printf("Data packets: OK = %d, bad = %d\n", ok_data_packets, bad_data_packets);
+    printf("Missing packets = %d\n", missing_packets);
+    if (t38_version == 0)
+    {
+        if (ok_indicator_packets != 16  ||  bad_indicator_packets != 0)
+        {
+            printf("Tests failed\n");
+            return -1;
+        }
+        if (ok_data_packets != 288  ||  bad_data_packets != 0)
+        {
+            printf("Tests failed\n");
+            return -1;
+        }
+    }
+    else
+    {
+        if (ok_indicator_packets != 23  ||  bad_indicator_packets != 0)
+        {
+            printf("Tests failed\n");
+            return -1;
+        }
+        if (ok_data_packets != 720  ||  bad_data_packets != 0)
+        {
+            printf("Tests failed\n");
+            return -1;
+        }
+    }
+    if (missing_packets > 0)
+    {
+        printf("Tests failed\n");
+        return -1;
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int attack_tests(t38_core_state_t *s)
+{
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int main(int argc, char *argv[])
+{
+    t38_core_state_t t38_core_a;
+    t38_core_state_t t38_core_b;
+
+    for (t38_version = 0;  t38_version < 2;  t38_version++)
+    {
+        printf("Using T.38 version %d\n", t38_version);
+
+        if (t38_core_init(&t38_core_a,
+                          rx_indicator_handler,
+                          rx_data_handler,
+                          rx_missing_handler,
+                          &t38_core_b,
+                          tx_packet_handler,
+                          &t38_core_b) == NULL)
+        {
+            fprintf(stderr, "Cannot start the T.38 core\n");
+            exit(2);
+        }
+        if (t38_core_init(&t38_core_b,
+                          rx_indicator_handler,
+                          rx_data_handler,
+                          rx_missing_handler,
+                          &t38_core_a,
+                          tx_packet_handler,
+                          &t38_core_a) == NULL)
+        {
+            fprintf(stderr, "Cannot start the T.38 core\n");
+            exit(2);
+        }
+
+        t38_set_t38_version(&t38_core_a, t38_version);
+        t38_set_t38_version(&t38_core_b, t38_version);
+
+        span_log_set_level(&t38_core_a.logging, SPAN_LOG_DEBUG | SPAN_LOG_SHOW_TAG);
+        span_log_set_tag(&t38_core_a.logging, "T.38-A");
+        span_log_set_level(&t38_core_b.logging, SPAN_LOG_DEBUG | SPAN_LOG_SHOW_TAG);
+        span_log_set_tag(&t38_core_b.logging, "T.38-B");
+
+        if (encode_decode_tests(&t38_core_a, &t38_core_b))
+        {
+            printf("Encode/decode tests failed\n");
+            exit(2);
+        }
+        if (attack_tests(&t38_core_a))
+        {
+            printf("Attack tests failed\n");
+            exit(2);
+        }
+    }
+    if (!succeeded)
+    {
+        printf("Tests failed\n");
+        exit(2);
+    }
+    printf("Tests passed\n");
+    return  0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/tests/t38_gateway_tests.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/tests/t38_gateway_tests.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,622 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * t38_gateway_tests.c - Tests for the T.38 FoIP gateway module.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2005, 2006 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: t38_gateway_tests.c,v 1.76 2008/08/16 15:24:16 steveu Exp $
+ */
+
+/*! \file */
+
+/*! \page t38_gateway_tests_page T.38 gateway tests
+\section t38_gateway_tests_page_sec_1 What does it do?
+These tests exercise the path
+
+    FAX machine <-> T.38 gateway <-> T.38 gateway <-> FAX machine
+*/
+
+#if defined(HAVE_CONFIG_H)
+#include <config.h>
+#endif
+
+#if defined(HAVE_FL_FL_H)  &&  defined(HAVE_FL_FL_CARTESIAN_H)  &&  defined(HAVE_FL_FL_AUDIO_METER_H)
+#define ENABLE_GUI
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <string.h>
+#include <assert.h>
+#include <errno.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <netinet/tcp.h>
+#if !defined(__USE_MISC)
+#define __USE_MISC
+#endif
+#include <arpa/inet.h>
+#include <sys/select.h>
+#include <sys/time.h>
+#include <audiofile.h>
+
+#include "spandsp.h"
+#include "spandsp-sim.h"
+
+#if defined(ENABLE_GUI)
+#include "media_monitor.h"
+#endif
+
+#define SAMPLES_PER_CHUNK       160
+
+#define INPUT_FILE_NAME         "../test-data/itu/fax/itutests.tif"
+#define OUTPUT_FILE_NAME        "t38.tif"
+#define OUTPUT_FILE_NAME_WAVE   "t38_gateway.wav"
+#define OUTPUT_FILE_NAME_T30A   "t38_gateway_t30a.wav"
+#define OUTPUT_FILE_NAME_T38A   "t38_gateway_t38a.wav"
+#define OUTPUT_FILE_NAME_T30B   "t38_gateway_t30b.wav"
+#define OUTPUT_FILE_NAME_T38B   "t38_gateway_t38b.wav"
+
+fax_state_t fax_state_a;
+t38_gateway_state_t t38_state_a;
+t38_gateway_state_t t38_state_b;
+fax_state_t fax_state_b;
+
+g1050_state_t *path_a_to_b;
+g1050_state_t *path_b_to_a;
+
+double when = 0.0;
+
+int done[2] = {FALSE, FALSE};
+int succeeded[2] = {FALSE, FALSE};
+
+int simulate_incrementing_repeats = FALSE;
+
+static int phase_b_handler(t30_state_t *s, void *user_data, int result)
+{
+    int i;
+    
+    i = (int) (intptr_t) user_data;
+    printf("%c: Phase B handler on channel %c - (0x%X) %s\n", i, i, result, t30_frametype(result));
+    return T30_ERR_OK;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int phase_d_handler(t30_state_t *s, void *user_data, int result)
+{
+    int i;
+    t30_stats_t t;
+    const char *u;
+
+    i = (int) (intptr_t) user_data;
+    printf("%c: Phase D handler on channel %c - (0x%X) %s\n", i, i, result, t30_frametype(result));
+    t30_get_transfer_statistics(s, &t);
+    printf("%c: Phase D: bit rate %d\n", i, t.bit_rate);
+    printf("%c: Phase D: ECM %s\n", i, (t.error_correcting_mode)  ?  "on"  :  "off");
+    printf("%c: Phase D: pages transferred %d\n", i, t.pages_transferred);
+    printf("%c: Phase D: image size %d x %d\n", i, t.width, t.length);
+    printf("%c: Phase D: image resolution %d x %d\n", i, t.x_resolution, t.y_resolution);
+    printf("%c: Phase D: bad rows %d\n", i, t.bad_rows);
+    printf("%c: Phase D: longest bad row run %d\n", i, t.longest_bad_row_run);
+    printf("%c: Phase D: coding method %s\n", i, t4_encoding_to_str(t.encoding));
+    printf("%c: Phase D: image size %d bytes\n", i, t.image_size);
+    if ((u = t30_get_tx_ident(s)))
+        printf("%c: Phase D: local ident '%s'\n", i, u);
+    if ((u = t30_get_rx_ident(s)))
+        printf("%c: Phase D: remote ident '%s'\n", i, u);
+    printf("%c: Phase D: bits per row - min %d, max %d\n", i, s->t4.min_row_bits, s->t4.max_row_bits);
+    return T30_ERR_OK;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void phase_e_handler(t30_state_t *s, void *user_data, int result)
+{
+    int i;
+    t30_stats_t t;
+    const char *u;
+    
+    i = (int) (intptr_t) user_data;
+    printf("%c: Phase E handler on channel %c - (%d) %s\n", i, i, result, t30_completion_code_to_str(result));
+    t30_get_transfer_statistics(s, &t);
+    printf("%c: Phase E: bit rate %d\n", i, t.bit_rate);
+    printf("%c: Phase E: ECM %s\n", i, (t.error_correcting_mode)  ?  "on"  :  "off");
+    printf("%c: Phase E: pages transferred %d\n", i, t.pages_transferred);
+    printf("%c: Phase E: image size %d x %d\n", i, t.width, t.length);
+    printf("%c: Phase E: image resolution %d x %d\n", i, t.x_resolution, t.y_resolution);
+    printf("%c: Phase E: bad rows %d\n", i, t.bad_rows);
+    printf("%c: Phase E: longest bad row run %d\n", i, t.longest_bad_row_run);
+    printf("%c: Phase E: coding method %s\n", i, t4_encoding_to_str(t.encoding));
+    printf("%c: Phase E: image size %d bytes\n", i, t.image_size);
+    if ((u = t30_get_tx_ident(s)))
+        printf("%c: Phase E: local ident '%s'\n", i, u);
+    if ((u = t30_get_rx_ident(s)))
+        printf("%c: Phase E: remote ident '%s'\n", i, u);
+    succeeded[i - 'A'] = (result == T30_ERR_OK)  &&  (t.pages_transferred == 12);
+    done[i - 'A'] = TRUE;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void real_time_frame_handler(t38_gateway_state_t *s,
+                                    void *user_data,
+                                    int direction,
+                                    const uint8_t *msg,
+                                    int len)
+{
+    int i;
+    
+    i = (intptr_t) user_data;
+    printf("%d: Real time frame handler on channel %d - %s, %s, length = %d\n",
+           i,
+           i,
+           (direction)  ?  "PSTN->T.38"  : "T.38->PSTN",
+           t30_frametype(msg[2]),
+           len);
+}
+/*- End of function --------------------------------------------------------*/
+
+static int tx_packet_handler_a(t38_core_state_t *s, void *user_data, const uint8_t *buf, int len, int count)
+{
+    t38_terminal_state_t *t;
+    int i;
+    static int subst_seq = 0;
+
+    /* This routine queues messages between two instances of T.38 processing */
+    t = (t38_terminal_state_t *) user_data;
+    if (simulate_incrementing_repeats)
+    {
+        for (i = 0;  i < count;  i++)
+        {
+            span_log(&s->logging, SPAN_LOG_FLOW, "Send seq %d, len %d\n", subst_seq, len);
+
+            g1050_put(path_a_to_b, buf, len, subst_seq, when);
+            subst_seq = (subst_seq + 1) & 0xFFFF;
+        }
+    }
+    else
+    {
+        span_log(&s->logging, SPAN_LOG_FLOW, "Send seq %d, len %d, count %d\n", s->tx_seq_no, len, count);
+
+        for (i = 0;  i < count;  i++)
+            g1050_put(path_a_to_b, buf, len, s->tx_seq_no, when);
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int tx_packet_handler_b(t38_core_state_t *s, void *user_data, const uint8_t *buf, int len, int count)
+{
+    t38_terminal_state_t *t;
+    int i;
+    static int subst_seq = 0;
+
+    /* This routine queues messages between two instances of T.38 processing */
+    t = (t38_terminal_state_t *) user_data;
+    if (simulate_incrementing_repeats)
+    {
+        for (i = 0;  i < count;  i++)
+        {
+            span_log(&s->logging, SPAN_LOG_FLOW, "Send seq %d, len %d\n", subst_seq, len);
+
+            g1050_put(path_b_to_a, buf, len, subst_seq, when);
+            subst_seq = (subst_seq + 1) & 0xFFFF;
+        }
+    }
+    else
+    {
+        span_log(&s->logging, SPAN_LOG_FLOW, "Send seq %d, len %d, count %d\n", s->tx_seq_no, len, count);
+
+        for (i = 0;  i < count;  i++)
+            g1050_put(path_b_to_a, buf, len, s->tx_seq_no, when);
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int main(int argc, char *argv[])
+{
+    int16_t silence[SAMPLES_PER_CHUNK];
+    int16_t t30_amp_a[SAMPLES_PER_CHUNK];
+    int16_t t38_amp_a[SAMPLES_PER_CHUNK];
+    int16_t t38_amp_hist_a[8][SAMPLES_PER_CHUNK];
+    int16_t t38_amp_b[SAMPLES_PER_CHUNK];
+    int16_t t38_amp_hist_b[8][SAMPLES_PER_CHUNK];
+    int16_t t30_amp_b[SAMPLES_PER_CHUNK];
+    int16_t out_amp[SAMPLES_PER_CHUNK*4];
+    int t30_len_a;
+    int t38_len_a;
+    int t38_len_b;
+    int t30_len_b;
+    int hist_ptr;
+    int log_audio;
+    int msg_len;
+    uint8_t msg[1024];
+    int outframes;
+    AFfilehandle wave_handle;
+    int use_ecm;
+    int use_tep;
+    int feedback_audio;
+    int use_transmit_on_idle;
+    int t38_version;
+    const char *input_file_name;
+    int i;
+    int seq_no;
+    int model_no;
+    int speed_pattern_no;
+    double tx_when;
+    double rx_when;
+    int supported_modems;
+    int fill_removal;
+    int use_gui;
+    int opt;
+    t38_stats_t stats;
+    t30_state_t *t30;
+
+    log_audio = FALSE;
+    use_ecm = FALSE;
+    t38_version = 1;
+    input_file_name = INPUT_FILE_NAME;
+    simulate_incrementing_repeats = FALSE;
+    model_no = 0;
+    speed_pattern_no = 1;
+    fill_removal = FALSE;
+    use_gui = FALSE;
+    use_tep = FALSE;
+    feedback_audio = FALSE;
+    use_transmit_on_idle = TRUE;
+    supported_modems = T30_SUPPORT_V27TER | T30_SUPPORT_V29 | T30_SUPPORT_V17;
+    while ((opt = getopt(argc, argv, "efFgi:Ilm:M:s:tv:")) != -1)
+    {
+        switch (opt)
+        {
+        case 'e':
+            use_ecm = TRUE;
+            break;
+        case 'f':
+            feedback_audio = TRUE;
+            break;
+        case 'F':
+            fill_removal = TRUE;
+            break;
+        case 'g':
+#if defined(ENABLE_GUI)
+            use_gui = TRUE;
+#else
+            fprintf(stderr, "Graphical monitoring not available\n");
+            exit(2);
+#endif
+            break;
+        case 'i':
+            input_file_name = optarg;
+            break;
+        case 'I':
+            simulate_incrementing_repeats = TRUE;
+            break;
+        case 'l':
+            log_audio = TRUE;
+            break;
+        case 'm':
+            supported_modems = atoi(optarg);
+            break;
+        case 'M':
+            model_no = optarg[0] - 'A' + 1;
+            break;
+        case 's':
+            speed_pattern_no = atoi(optarg);
+            break;
+        case 't':
+            use_tep = TRUE;
+            break;
+        case 'v':
+            t38_version = atoi(optarg);
+            break;
+        default:
+            //usage();
+            exit(2);
+            break;
+        }
+    }
+
+    printf("Using T.38 version %d\n", t38_version);
+    if (use_ecm)
+        printf("Using ECM\n");
+
+    wave_handle = AF_NULL_FILEHANDLE;
+    if (log_audio)
+    {
+        if ((wave_handle = afOpenFile_telephony_write(OUTPUT_FILE_NAME_WAVE, 4)) == AF_NULL_FILEHANDLE)
+        {
+            fprintf(stderr, "    Cannot create wave file '%s'\n", OUTPUT_FILE_NAME_WAVE);
+            exit(2);
+        }
+    }
+    memset(silence, 0, sizeof(silence));
+ 
+    srand48(0x1234567);
+    if ((path_a_to_b = g1050_init(model_no, speed_pattern_no, 100, 33)) == NULL)
+    {
+        fprintf(stderr, "Failed to start IP network path model\n");
+        exit(2);
+    }
+    if ((path_b_to_a = g1050_init(model_no, speed_pattern_no, 100, 33)) == NULL)
+    {
+        fprintf(stderr, "Failed to start IP network path model\n");
+        exit(2);
+    }
+
+    if (fax_init(&fax_state_a, TRUE) == NULL)
+    {
+        fprintf(stderr, "Cannot start FAX\n");
+        exit(2);
+    }
+    t30 = fax_get_t30_state(&fax_state_a);
+    fax_set_transmit_on_idle(&fax_state_a, use_transmit_on_idle);
+    fax_set_tep_mode(&fax_state_a, use_tep);
+    t30_set_supported_modems(t30, supported_modems);
+    t30_set_tx_ident(t30, "11111111");
+    t30_set_tx_nsf(t30, (const uint8_t *) "\x50\x00\x00\x00Spandsp\x00", 12);
+    t30_set_tx_file(t30, input_file_name, -1, -1);
+    t30_set_phase_b_handler(t30, phase_b_handler, (void *) (intptr_t) 'A');
+    t30_set_phase_d_handler(t30, phase_d_handler, (void *) (intptr_t) 'A');
+    t30_set_phase_e_handler(t30, phase_e_handler, (void *) (intptr_t) 'A');
+    t30_set_ecm_capability(t30, use_ecm);
+    if (use_ecm)
+        t30_set_supported_compressions(t30, T30_SUPPORT_T4_1D_COMPRESSION | T30_SUPPORT_T4_2D_COMPRESSION | T30_SUPPORT_T6_COMPRESSION);
+    t30_set_minimum_scan_line_time(t30, 40);
+    //t30_set_iaf_mode(t30, T30_IAF_MODE_NO_FILL_BITS);
+    span_log_set_level(&fax_state_a.logging, SPAN_LOG_DEBUG | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME);
+    span_log_set_tag(&fax_state_a.logging, "FAX-A ");
+    span_log_set_level(&t30->logging, SPAN_LOG_DEBUG | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME);
+    span_log_set_tag(&t30->logging, "FAX-A ");
+    memset(t30_amp_a, 0, sizeof(t30_amp_a));
+    memset(t38_amp_hist_a, 0, sizeof(t38_amp_hist_a));
+    memset(t38_amp_hist_b, 0, sizeof(t38_amp_hist_b));
+
+    if (t38_gateway_init(&t38_state_a, tx_packet_handler_a, &t38_state_b) == NULL)
+    {
+        fprintf(stderr, "Cannot start the T.38 channel\n");
+        exit(2);
+    }
+    t38_gateway_set_transmit_on_idle(&t38_state_a, use_transmit_on_idle);
+    t38_gateway_set_supported_modems(&t38_state_a, supported_modems);
+    //t38_gateway_set_nsx_suppression(&t38_state_a, NULL, 0, NULL, 0);
+    t38_gateway_set_fill_bit_removal(&t38_state_a, fill_removal);
+    t38_gateway_set_real_time_frame_handler(&t38_state_a, real_time_frame_handler, NULL);
+    t38_set_t38_version(&t38_state_a.t38x.t38, t38_version);
+    t38_gateway_set_ecm_capability(&t38_state_a, use_ecm);
+    span_log_set_level(&t38_state_a.logging, SPAN_LOG_DEBUG | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME);
+    span_log_set_tag(&t38_state_a.logging, "T.38-A");
+    span_log_set_level(&t38_state_a.t38x.t38.logging, SPAN_LOG_DEBUG | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME);
+    span_log_set_tag(&t38_state_a.t38x.t38.logging, "T.38-A");
+    memset(t38_amp_a, 0, sizeof(t38_amp_a));
+
+    if (t38_gateway_init(&t38_state_b, tx_packet_handler_b, &t38_state_a) == NULL)
+    {
+        fprintf(stderr, "Cannot start the T.38 channel\n");
+        exit(2);
+    }
+    t38_gateway_set_transmit_on_idle(&t38_state_b, use_transmit_on_idle);
+    t38_gateway_set_supported_modems(&t38_state_b, supported_modems);
+    //t38_gateway_set_nsx_suppression(&t38_state_b, FALSE);
+    t38_gateway_set_fill_bit_removal(&t38_state_b, fill_removal);
+    t38_set_t38_version(&t38_state_b.t38x.t38, t38_version);
+    t38_gateway_set_ecm_capability(&t38_state_b, use_ecm);
+    span_log_set_level(&t38_state_b.logging, SPAN_LOG_DEBUG | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME);
+    span_log_set_tag(&t38_state_b.logging, "T.38-B");
+    span_log_set_level(&t38_state_b.t38x.t38.logging, SPAN_LOG_DEBUG | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME);
+    span_log_set_tag(&t38_state_b.t38x.t38.logging, "T.38-B");
+    memset(t38_amp_b, 0, sizeof(t38_amp_b));
+
+    if (fax_init(&fax_state_b, FALSE) == NULL)
+    {
+        fprintf(stderr, "Cannot start FAX\n");
+        exit(2);
+    }
+    t30 = fax_get_t30_state(&fax_state_b);
+    fax_set_transmit_on_idle(&fax_state_b, use_transmit_on_idle);
+    fax_set_tep_mode(&fax_state_b, use_tep);
+    t30_set_supported_modems(t30, supported_modems);
+    t30_set_tx_ident(t30, "22222222");
+    t30_set_tx_nsf(t30, (const uint8_t *) "\x50\x00\x00\x00Spandsp\x00", 12);
+    t30_set_rx_file(t30, OUTPUT_FILE_NAME, -1);
+    t30_set_phase_b_handler(t30, phase_b_handler, (void *) (intptr_t) 'B');
+    t30_set_phase_d_handler(t30, phase_d_handler, (void *) (intptr_t) 'B');
+    t30_set_phase_e_handler(t30, phase_e_handler, (void *) (intptr_t) 'B');
+    t30_set_ecm_capability(t30, use_ecm);
+    if (use_ecm)
+        t30_set_supported_compressions(t30, T30_SUPPORT_T4_1D_COMPRESSION | T30_SUPPORT_T4_2D_COMPRESSION | T30_SUPPORT_T6_COMPRESSION);
+    t30_set_minimum_scan_line_time(t30, 40);
+    span_log_set_level(&fax_state_b.logging, SPAN_LOG_DEBUG | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME);
+    span_log_set_tag(&fax_state_b.logging, "FAX-B ");
+    span_log_set_level(&t30->logging, SPAN_LOG_DEBUG | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME);
+    span_log_set_tag(&t30->logging, "FAX-B ");
+    memset(t30_amp_b, 0, sizeof(t30_amp_b));
+
+#if defined(ENABLE_GUI)
+    if (use_gui)
+        start_media_monitor();
+#endif
+    hist_ptr = 0;
+    for (;;)
+    {
+        t30 = fax_get_t30_state(&fax_state_a);
+        span_log_bump_samples(&fax_state_a.logging, SAMPLES_PER_CHUNK);
+        span_log_bump_samples(&t30->logging, SAMPLES_PER_CHUNK);
+        span_log_bump_samples(&t38_state_a.logging, SAMPLES_PER_CHUNK);
+        span_log_bump_samples(&t38_state_a.t38x.t38.logging, SAMPLES_PER_CHUNK);
+        span_log_bump_samples(&t38_state_b.logging, SAMPLES_PER_CHUNK);
+        span_log_bump_samples(&t38_state_b.t38x.t38.logging, SAMPLES_PER_CHUNK);
+        t30 = fax_get_t30_state(&fax_state_b);
+        span_log_bump_samples(&fax_state_b.logging, SAMPLES_PER_CHUNK);
+        span_log_bump_samples(&t30->logging, SAMPLES_PER_CHUNK);
+        memset(out_amp, 0, sizeof(out_amp));
+
+        t30_len_a = fax_tx(&fax_state_a, t30_amp_a, SAMPLES_PER_CHUNK);
+        if (!use_transmit_on_idle)
+        {
+            /* The receive side always expects a full block of samples, but the
+               transmit side may not be sending any when it doesn't need to. We
+               may need to pad with some silence. */
+            if (t30_len_a < SAMPLES_PER_CHUNK)
+            {
+                memset(t30_amp_a + t30_len_a, 0, sizeof(int16_t)*(SAMPLES_PER_CHUNK - t30_len_a));
+                t30_len_a = SAMPLES_PER_CHUNK;
+            }
+        }
+        if (log_audio)
+        {
+            for (i = 0;  i < t30_len_a;  i++)
+                out_amp[i*4] = t30_amp_a[i];
+        }
+        if (feedback_audio)
+        {
+            for (i = 0;  i < t30_len_a;  i++)
+                t30_amp_a[i] += t38_amp_hist_a[hist_ptr][i] >> 1;
+            memcpy(t38_amp_hist_a[hist_ptr], t38_amp_a, sizeof(int16_t)*SAMPLES_PER_CHUNK);
+        }
+        if (t38_gateway_rx(&t38_state_a, t30_amp_a, t30_len_a))
+            break;
+
+        t38_len_a = t38_gateway_tx(&t38_state_a, t38_amp_a, SAMPLES_PER_CHUNK);
+        if (!use_transmit_on_idle)
+        {
+            if (t38_len_a < SAMPLES_PER_CHUNK)
+            {
+                memset(t38_amp_a + t38_len_a, 0, sizeof(int16_t)*(SAMPLES_PER_CHUNK - t38_len_a));
+                t38_len_a = SAMPLES_PER_CHUNK;
+            }
+        }
+        if (log_audio)
+        {
+            for (i = 0;  i < t38_len_a;  i++)
+                out_amp[i*4 + 1] = t38_amp_a[i];
+        }
+        if (fax_rx(&fax_state_a, t38_amp_a, SAMPLES_PER_CHUNK))
+            break;
+
+        t30_len_b = fax_tx(&fax_state_b, t30_amp_b, SAMPLES_PER_CHUNK);
+        if (!use_transmit_on_idle)
+        {
+            /* The receive side always expects a full block of samples, but the
+               transmit side may not be sending any when it doesn't need to. We
+               may need to pad with some silence. */
+            if (t30_len_b < SAMPLES_PER_CHUNK)
+            {
+                memset(t30_amp_b + t30_len_b, 0, sizeof(int16_t)*(SAMPLES_PER_CHUNK - t30_len_b));
+                t30_len_b = SAMPLES_PER_CHUNK;
+            }
+        }
+        if (log_audio)
+        {
+            for (i = 0;  i < t30_len_b;  i++)
+                out_amp[i*4 + 3] = t30_amp_b[i];
+        }
+        if (feedback_audio)
+        {
+            for (i = 0;  i < t30_len_b;  i++)
+                t30_amp_b[i] += t38_amp_hist_b[hist_ptr][i] >> 1;
+            memcpy(t38_amp_hist_b[hist_ptr], t38_amp_b, sizeof(int16_t)*SAMPLES_PER_CHUNK);
+        }
+        if (t38_gateway_rx(&t38_state_b, t30_amp_b, t30_len_b))
+            break;
+
+        t38_len_b = t38_gateway_tx(&t38_state_b, t38_amp_b, SAMPLES_PER_CHUNK);
+        if (!use_transmit_on_idle)
+        {
+            if (t38_len_b < SAMPLES_PER_CHUNK)
+            {
+                memset(t38_amp_b + t38_len_b, 0, sizeof(int16_t)*(SAMPLES_PER_CHUNK - t38_len_b));
+                t38_len_b = SAMPLES_PER_CHUNK;
+            }
+        }
+        if (log_audio)
+        {
+            for (i = 0;  i < t38_len_b;  i++)
+                out_amp[i*4 + 2] = t38_amp_b[i];
+        }
+        if (fax_rx(&fax_state_b, t38_amp_b, SAMPLES_PER_CHUNK))
+            break;
+
+        when += 0.02;
+
+        while ((msg_len = g1050_get(path_a_to_b, msg, 1024, when, &seq_no, &tx_when, &rx_when)) >= 0)
+        {
+#if defined(ENABLE_GUI)
+            if (use_gui)
+                media_monitor_rx(seq_no, tx_when, rx_when);
+#endif
+            t38_core_rx_ifp_packet(&t38_state_b.t38x.t38, msg, msg_len, seq_no);
+        }
+        while ((msg_len = g1050_get(path_b_to_a, msg, 1024, when, &seq_no, &tx_when, &rx_when)) >= 0)
+        {
+#if defined(ENABLE_GUI)
+            if (use_gui)
+                media_monitor_rx(seq_no, tx_when, rx_when);
+#endif
+            t38_core_rx_ifp_packet(&t38_state_a.t38x.t38, msg, msg_len, seq_no);
+        }
+        if (log_audio)
+        {
+            outframes = afWriteFrames(wave_handle, AF_DEFAULT_TRACK, out_amp, SAMPLES_PER_CHUNK);
+            if (outframes != SAMPLES_PER_CHUNK)
+                break;
+        }
+
+        if (done[0]  &&  done[1])
+            break;
+#if defined(ENABLE_GUI)
+        if (use_gui)
+            media_monitor_update_display();
+#endif
+        if (++hist_ptr > 3)
+            hist_ptr = 0;
+    }
+    t38_gateway_get_transfer_statistics(&t38_state_a, &stats);
+    printf("A side exchanged %d pages at %dbps, in %s mode\n",
+           stats.pages_transferred,
+           stats.bit_rate,
+           (stats.error_correcting_mode)  ?  "ECM"  :  "non-ECM");
+    t38_gateway_get_transfer_statistics(&t38_state_a, &stats);
+    printf("B side exchanged %d pages at %dbps, in %s mode\n",
+           stats.pages_transferred,
+           stats.bit_rate,
+           (stats.error_correcting_mode)  ?  "ECM"  :  "non-ECM");
+    fax_release(&fax_state_a);
+    fax_release(&fax_state_b);
+    if (log_audio)
+    {
+        if (afCloseFile(wave_handle) != 0)
+        {
+            fprintf(stderr, "    Cannot close wave file '%s'\n", OUTPUT_FILE_NAME_WAVE);
+            exit(2);
+        }
+    }
+    if (!succeeded[0]  ||  !succeeded[1])
+    {
+        printf("Tests failed\n");
+        exit(2);
+    }
+    printf("Tests passed\n");
+    return  0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/tests/t38_gateway_to_terminal_tests.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/tests/t38_gateway_to_terminal_tests.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,500 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * t38_gateway_to_terminal_tests.c - Joint tests for the T.38 FoIP terminal and gateway modules.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2005, 2006 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: t38_gateway_to_terminal_tests.c,v 1.60 2008/08/16 15:24:16 steveu Exp $
+ */
+
+/*! \file */
+
+/*! \page t38_gateway_to_terminal_tests_page T.38 mixed gateway and termination tests
+\section t38_gateway_to_terminal_tests_page_sec_1 What does it do?
+These tests exercise the path
+
+    FAX machine -> T.38 gateway -> T.38 termination
+*/
+
+#if defined(HAVE_CONFIG_H)
+#include <config.h>
+#endif
+
+#if defined(HAVE_FL_FL_H)  &&  defined(HAVE_FL_FL_CARTESIAN_H)  &&  defined(HAVE_FL_FL_AUDIO_METER_H)
+#define ENABLE_GUI
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <string.h>
+#include <assert.h>
+#include <errno.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <netinet/tcp.h>
+#if !defined(__USE_MISC)
+#define __USE_MISC
+#endif
+#include <arpa/inet.h>
+#include <sys/select.h>
+#include <sys/time.h>
+#include <audiofile.h>
+
+#include "spandsp.h"
+#include "spandsp-sim.h"
+
+#if defined(ENABLE_GUI)
+#include "media_monitor.h"
+#endif
+
+#define SAMPLES_PER_CHUNK       160
+
+#define INPUT_FILE_NAME         "../test-data/itu/fax/itutests.tif"
+#define OUTPUT_FILE_NAME        "t38.tif"
+#define OUTPUT_FILE_NAME_WAVE   "t38_gateway_to_terminal.wav"
+
+fax_state_t fax_state_a;
+t38_gateway_state_t t38_state_a;
+t38_terminal_state_t t38_state_b;
+
+g1050_state_t *path_a_to_b;
+g1050_state_t *path_b_to_a;
+
+double when = 0.0;
+
+int done[2] = {FALSE, FALSE};
+int succeeded[2] = {FALSE, FALSE};
+
+int simulate_incrementing_repeats = FALSE;
+
+static int phase_b_handler(t30_state_t *s, void *user_data, int result)
+{
+    int i;
+    
+    i = (int) (intptr_t) user_data;
+    printf("%c: Phase B handler on channel %c - (0x%X) %s\n", i, i, result, t30_frametype(result));
+    return T30_ERR_OK;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int phase_d_handler(t30_state_t *s, void *user_data, int result)
+{
+    int i;
+    t30_stats_t t;
+    const char *u;
+
+    i = (int) (intptr_t) user_data;
+    printf("%c: Phase D handler on channel %c - (0x%X) %s\n", i, i, result, t30_frametype(result));
+    t30_get_transfer_statistics(s, &t);
+    printf("%c: Phase D: bit rate %d\n", i, t.bit_rate);
+    printf("%c: Phase D: ECM %s\n", i, (t.error_correcting_mode)  ?  "on"  :  "off");
+    printf("%c: Phase D: pages transferred %d\n", i, t.pages_transferred);
+    printf("%c: Phase D: image size %d x %d\n", i, t.width, t.length);
+    printf("%c: Phase D: image resolution %d x %d\n", i, t.x_resolution, t.y_resolution);
+    printf("%c: Phase D: bad rows %d\n", i, t.bad_rows);
+    printf("%c: Phase D: longest bad row run %d\n", i, t.longest_bad_row_run);
+    printf("%c: Phase D: coding method %s\n", i, t4_encoding_to_str(t.encoding));
+    printf("%c: Phase D: image size %d bytes\n", i, t.image_size);
+    if ((u = t30_get_tx_ident(s)))
+        printf("%c: Phase D: local ident '%s'\n", i, u);
+    if ((u = t30_get_rx_ident(s)))
+        printf("%c: Phase D: remote ident '%s'\n", i, u);
+    printf("%c: Phase D: bits per row - min %d, max %d\n", i, s->t4.min_row_bits, s->t4.max_row_bits);
+    return T30_ERR_OK;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void phase_e_handler(t30_state_t *s, void *user_data, int result)
+{
+    int i;
+    t30_stats_t t;
+    const char *u;
+    
+    i = (int) (intptr_t) user_data;
+    printf("%c: Phase E handler on channel %c - (%d) %s\n", i, i, result, t30_completion_code_to_str(result));
+    t30_get_transfer_statistics(s, &t);
+    printf("%c: Phase E: bit rate %d\n", i, t.bit_rate);
+    printf("%c: Phase E: ECM %s\n", i, (t.error_correcting_mode)  ?  "on"  :  "off");
+    printf("%c: Phase E: pages transferred %d\n", i, t.pages_transferred);
+    printf("%c: Phase E: image size %d x %d\n", i, t.width, t.length);
+    printf("%c: Phase E: image resolution %d x %d\n", i, t.x_resolution, t.y_resolution);
+    printf("%c: Phase E: bad rows %d\n", i, t.bad_rows);
+    printf("%c: Phase E: longest bad row run %d\n", i, t.longest_bad_row_run);
+    printf("%c: Phase E: coding method %s\n", i, t4_encoding_to_str(t.encoding));
+    printf("%c: Phase E: image size %d bytes\n", i, t.image_size);
+    if ((u = t30_get_tx_ident(s)))
+        printf("%c: Phase E: local ident '%s'\n", i, u);
+    if ((u = t30_get_rx_ident(s)))
+        printf("%c: Phase E: remote ident '%s'\n", i, u);
+    succeeded[i - 'A'] = (result == T30_ERR_OK)  &&  (t.pages_transferred == 12);
+    done[i - 'A'] = TRUE;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int tx_packet_handler_a(t38_core_state_t *s, void *user_data, const uint8_t *buf, int len, int count)
+{
+    t38_terminal_state_t *t;
+    int i;
+    static int subst_seq = 0;
+
+    /* This routine queues messages between two instances of T.38 processing */
+    t = (t38_terminal_state_t *) user_data;
+    if (simulate_incrementing_repeats)
+    {
+        for (i = 0;  i < count;  i++)
+        {
+            span_log(&s->logging, SPAN_LOG_FLOW, "Send seq %d, len %d\n", subst_seq, len);
+
+            g1050_put(path_a_to_b, buf, len, subst_seq, when);
+            subst_seq = (subst_seq + 1) & 0xFFFF;
+        }
+    }
+    else
+    {
+        span_log(&s->logging, SPAN_LOG_FLOW, "Send seq %d, len %d, count %d\n", s->tx_seq_no, len, count);
+
+        for (i = 0;  i < count;  i++)
+            g1050_put(path_a_to_b, buf, len, s->tx_seq_no, when);
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int tx_packet_handler_b(t38_core_state_t *s, void *user_data, const uint8_t *buf, int len, int count)
+{
+    t38_terminal_state_t *t;
+    int i;
+    static int subst_seq = 0;
+
+    /* This routine queues messages between two instances of T.38 processing */
+    t = (t38_terminal_state_t *) user_data;
+    if (simulate_incrementing_repeats)
+    {
+        for (i = 0;  i < count;  i++)
+        {
+            span_log(&s->logging, SPAN_LOG_FLOW, "Send seq %d, len %d\n", subst_seq, len);
+
+            g1050_put(path_b_to_a, buf, len, subst_seq, when);
+            subst_seq = (subst_seq + 1) & 0xFFFF;
+        }
+    }
+    else
+    {
+        span_log(&s->logging, SPAN_LOG_FLOW, "Send seq %d, len %d, count %d\n", s->tx_seq_no, len, count);
+
+        for (i = 0;  i < count;  i++)
+            g1050_put(path_b_to_a, buf, len, s->tx_seq_no, when);
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int main(int argc, char *argv[])
+{
+    int16_t t38_amp_a[SAMPLES_PER_CHUNK];
+    int16_t t30_amp_a[SAMPLES_PER_CHUNK];
+    int16_t out_amp[2*SAMPLES_PER_CHUNK];
+    int t38_len_a;
+    int t30_len_a;
+    int msg_len;
+    uint8_t msg[1024];
+    int log_audio;
+    int outframes;
+    int t38_version;
+    int use_ecm;
+    int use_tep;
+    int feedback_audio;
+    int use_transmit_on_idle;
+    AFfilehandle wave_handle;
+    const char *input_file_name;
+    int i;
+    int seq_no;
+    int model_no;
+    int speed_pattern_no;
+    double tx_when;
+    double rx_when;
+    int supported_modems;
+    int use_gui;
+    int opt;
+    t30_state_t *t30;
+
+    log_audio = FALSE;
+    t38_version = 1;
+    use_ecm = FALSE;
+    input_file_name = INPUT_FILE_NAME;
+    simulate_incrementing_repeats = FALSE;
+    model_no = 0;
+    speed_pattern_no = 1;
+    use_gui = FALSE;
+    use_tep = FALSE;
+    feedback_audio = FALSE;
+    use_transmit_on_idle = TRUE;
+    supported_modems = T30_SUPPORT_V27TER | T30_SUPPORT_V29 | T30_SUPPORT_V17;
+    while ((opt = getopt(argc, argv, "efgi:Ilm:M:s:tv:")) != -1)
+    {
+        switch (opt)
+        {
+        case 'e':
+            use_ecm = TRUE;
+            break;
+        case 'f':
+            feedback_audio = TRUE;
+            break;
+        case 'g':
+#if defined(ENABLE_GUI)
+            use_gui = TRUE;
+#else
+            fprintf(stderr, "Graphical monitoring not available\n");
+            exit(2);
+#endif
+            break;
+        case 'i':
+            input_file_name = optarg;
+            break;
+        case 'I':
+            simulate_incrementing_repeats = TRUE;
+            break;
+        case 'l':
+            log_audio = TRUE;
+            break;
+        case 'm':
+            supported_modems = atoi(optarg);
+            break;
+        case 'M':
+            model_no = optarg[0] - 'A' + 1;
+            break;
+        case 's':
+            speed_pattern_no = atoi(optarg);
+            break;
+        case 't':
+            use_tep = TRUE;
+            break;
+        case 'v':
+            t38_version = atoi(optarg);
+            break;
+        default:
+            //usage();
+            exit(2);
+            break;
+        }
+    }
+
+    printf("Using T.38 version %d\n", t38_version);
+    if (use_ecm)
+        printf("Using ECM\n");
+
+    wave_handle = AF_NULL_FILEHANDLE;
+    if (log_audio)
+    {
+        if ((wave_handle = afOpenFile_telephony_write(OUTPUT_FILE_NAME_WAVE, 2)) == AF_NULL_FILEHANDLE)
+        {
+            fprintf(stderr, "    Cannot create wave file '%s'\n", OUTPUT_FILE_NAME_WAVE);
+            exit(2);
+        }
+    }
+
+    srand48(0x1234567);
+    if ((path_a_to_b = g1050_init(model_no, speed_pattern_no, 100, 33)) == NULL)
+    {
+        fprintf(stderr, "Failed to start IP network path model\n");
+        exit(2);
+    }
+    if ((path_b_to_a = g1050_init(model_no, speed_pattern_no, 100, 33)) == NULL)
+    {
+        fprintf(stderr, "Failed to start IP network path model\n");
+        exit(2);
+    }
+
+    if (fax_init(&fax_state_a, TRUE) == NULL)
+    {
+        fprintf(stderr, "Cannot start FAX\n");
+        exit(2);
+    }
+    t30 = fax_get_t30_state(&fax_state_a);
+    fax_set_transmit_on_idle(&fax_state_a, use_transmit_on_idle);
+    fax_set_tep_mode(&fax_state_a, use_tep);
+    t30_set_supported_modems(t30, supported_modems);
+    t30_set_tx_ident(t30, "11111111");
+    t30_set_tx_nsf(t30, (const uint8_t *) "\x50\x00\x00\x00Spandsp\x00", 12);
+    t30_set_tx_file(t30, input_file_name, -1, -1);
+    t30_set_phase_b_handler(t30, phase_b_handler, (void *) (intptr_t) 'A');
+    t30_set_phase_d_handler(t30, phase_d_handler, (void *) (intptr_t) 'A');
+    t30_set_phase_e_handler(t30, phase_e_handler, (void *) (intptr_t) 'A');
+    t30_set_ecm_capability(t30, use_ecm);
+    if (use_ecm)
+        t30_set_supported_compressions(t30, T30_SUPPORT_T4_1D_COMPRESSION | T30_SUPPORT_T4_2D_COMPRESSION | T30_SUPPORT_T6_COMPRESSION);
+    span_log_set_level(&fax_state_a.logging, SPAN_LOG_DEBUG | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME);
+    span_log_set_tag(&fax_state_a.logging, "FAX-A ");
+    span_log_set_level(&t30->logging, SPAN_LOG_DEBUG | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME);
+    span_log_set_tag(&t30->logging, "FAX-A ");
+    memset(t30_amp_a, 0, sizeof(t30_amp_a));
+
+    if (t38_gateway_init(&t38_state_a, tx_packet_handler_a, &t38_state_b) == NULL)
+    {
+        fprintf(stderr, "Cannot start the T.38 channel\n");
+        exit(2);
+    }
+    t38_gateway_set_transmit_on_idle(&t38_state_a, use_transmit_on_idle);
+    t38_set_t38_version(&t38_state_a.t38x.t38, t38_version);
+    t38_gateway_set_ecm_capability(&t38_state_a, use_ecm);
+    span_log_set_level(&t38_state_a.logging, SPAN_LOG_DEBUG | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME);
+    span_log_set_tag(&t38_state_a.logging, "T.38-A");
+    span_log_set_level(&t38_state_a.t38x.t38.logging, SPAN_LOG_DEBUG | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME);
+    span_log_set_tag(&t38_state_a.t38x.t38.logging, "T.38-A");
+    memset(t38_amp_a, 0, sizeof(t38_amp_a));
+
+    if (t38_terminal_init(&t38_state_b, FALSE, tx_packet_handler_b, &t38_state_a) == NULL)
+    {
+        fprintf(stderr, "Cannot start the T.38 channel\n");
+        exit(2);
+    }
+    t30 = t38_terminal_get_t30_state(&t38_state_b);
+    t38_set_t38_version(&t38_state_b.t38_fe.t38, t38_version);
+    span_log_set_level(&t38_state_b.logging, SPAN_LOG_DEBUG | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME);
+    span_log_set_tag(&t38_state_b.logging, "T.38-B");
+    span_log_set_level(&t38_state_b.t38_fe.t38.logging, SPAN_LOG_DEBUG | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME);
+    span_log_set_tag(&t38_state_b.t38_fe.t38.logging, "T.38-B");
+    span_log_set_level(&t30->logging, SPAN_LOG_DEBUG | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME);
+    span_log_set_tag(&t30->logging, "T.38-B");
+
+    t30_set_supported_modems(t30, supported_modems);
+    t30_set_tx_ident(t30, "22222222");
+    t30_set_rx_file(t30, OUTPUT_FILE_NAME, -1);
+    t30_set_phase_b_handler(t30, phase_b_handler, (void *) (intptr_t) 'B');
+    t30_set_phase_d_handler(t30, phase_d_handler, (void *) (intptr_t) 'B');
+    t30_set_phase_e_handler(t30, phase_e_handler, (void *) (intptr_t) 'B');
+    t30_set_ecm_capability(t30, use_ecm);
+    if (use_ecm)
+        t30_set_supported_compressions(t30, T30_SUPPORT_T4_1D_COMPRESSION | T30_SUPPORT_T4_2D_COMPRESSION | T30_SUPPORT_T6_COMPRESSION);
+
+#if defined(ENABLE_GUI)
+    if (use_gui)
+        start_media_monitor();
+#endif
+    for (;;)
+    {
+        span_log_bump_samples(&fax_state_a.logging, SAMPLES_PER_CHUNK);
+        span_log_bump_samples(&fax_state_a.t30.logging, SAMPLES_PER_CHUNK);
+        span_log_bump_samples(&t38_state_a.logging, SAMPLES_PER_CHUNK);
+        span_log_bump_samples(&t38_state_a.t38x.t38.logging, SAMPLES_PER_CHUNK);
+        span_log_bump_samples(&t38_state_b.logging, SAMPLES_PER_CHUNK);
+        span_log_bump_samples(&t38_state_b.t38_fe.t38.logging, SAMPLES_PER_CHUNK);
+        span_log_bump_samples(&t38_state_b.t30.logging, SAMPLES_PER_CHUNK);
+        memset(out_amp, 0, sizeof(out_amp));
+
+        t38_terminal_send_timeout(&t38_state_b, SAMPLES_PER_CHUNK);
+
+        t30_len_a = fax_tx(&fax_state_a, t30_amp_a, SAMPLES_PER_CHUNK);
+        if (!use_transmit_on_idle)
+        {
+            /* The receive side always expects a full block of samples, but the
+               transmit side may not be sending any when it doesn't need to. We
+               may need to pad with some silence. */
+            if (t30_len_a < SAMPLES_PER_CHUNK)
+            {
+                memset(t30_amp_a + t30_len_a, 0, sizeof(int16_t)*(SAMPLES_PER_CHUNK - t30_len_a));
+                t30_len_a = SAMPLES_PER_CHUNK;
+            }
+        }
+        if (log_audio)
+        {
+            for (i = 0;  i < t30_len_a;  i++)
+                out_amp[2*i] = t30_amp_a[i];
+        }
+        if (feedback_audio)
+        {
+            for (i = 0;  i < t30_len_a;  i++)
+                t30_amp_a[i] += t38_amp_a[i] >> 1;
+        }
+        if (t38_gateway_rx(&t38_state_a, t30_amp_a, t30_len_a))
+            break;
+    
+        t38_len_a = t38_gateway_tx(&t38_state_a, t38_amp_a, SAMPLES_PER_CHUNK);
+        if (!use_transmit_on_idle)
+        {
+            if (t38_len_a < SAMPLES_PER_CHUNK)
+            {
+                memset(t38_amp_a + t38_len_a, 0, sizeof(int16_t)*(SAMPLES_PER_CHUNK - t38_len_a));
+                t38_len_a = SAMPLES_PER_CHUNK;
+            }
+        }
+        if (log_audio)
+        {
+            for (i = 0;  i < t38_len_a;  i++)
+                out_amp[2*i + 1] = t38_amp_a[i];
+        }
+        if (fax_rx(&fax_state_a, t38_amp_a, SAMPLES_PER_CHUNK))
+            break;
+
+        when += 0.02;
+
+        while ((msg_len = g1050_get(path_a_to_b, msg, 1024, when, &seq_no, &tx_when, &rx_when)) >= 0)
+        {
+#if defined(ENABLE_GUI)
+            if (use_gui)
+                media_monitor_rx(seq_no, tx_when, rx_when);
+#endif
+            t38_core_rx_ifp_packet(&t38_state_b.t38_fe.t38, msg, msg_len, seq_no);
+        }
+        while ((msg_len = g1050_get(path_b_to_a, msg, 1024, when, &seq_no, &tx_when, &rx_when)) >= 0)
+        {
+#if defined(ENABLE_GUI)
+            if (use_gui)
+                media_monitor_rx(seq_no, tx_when, rx_when);
+#endif
+            t38_core_rx_ifp_packet(&t38_state_a.t38x.t38, msg, msg_len, seq_no);
+        }
+        if (log_audio)
+        {
+            outframes = afWriteFrames(wave_handle, AF_DEFAULT_TRACK, out_amp, SAMPLES_PER_CHUNK);
+            if (outframes != SAMPLES_PER_CHUNK)
+                break;
+        }
+
+        if (done[0]  &&  done[1])
+            break;
+#if defined(ENABLE_GUI)
+        if (use_gui)
+            media_monitor_update_display();
+#endif
+    }
+    fax_release(&fax_state_a);
+    t38_terminal_release(&t38_state_b);
+    if (log_audio)
+    {
+        if (afCloseFile(wave_handle) != 0)
+        {
+            fprintf(stderr, "    Cannot close wave file '%s'\n", OUTPUT_FILE_NAME_WAVE);
+            exit(2);
+        }
+    }
+    if (!succeeded[0]  ||  !succeeded[1])
+    {
+        printf("Tests failed\n");
+        exit(2);
+    }
+    printf("Tests passed\n");
+    return  0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/tests/t38_non_ecm_buffer_tests.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/tests/t38_non_ecm_buffer_tests.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,123 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * t38_non_ecm_buffer_tests.c - Tests for the T.38 non-ECM image data buffer module.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2008 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: t38_non_ecm_buffer_tests.c,v 1.1 2008/08/14 14:06:06 steveu Exp $
+ */
+
+/*! \file */
+
+/*! \page t38_non_ecm_buffer_tests_page T.38 non-ECM buffer tests
+\section t38_non_ecm_buffer_tests_page_sec_1 What does it do?
+These tests exercise the flow controlling non-ECM image data buffer
+module, used for T.38 gateways.
+*/
+
+#if defined(HAVE_CONFIG_H)
+#include <config.h>
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <string.h>
+#include <assert.h>
+#include <errno.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <netinet/tcp.h>
+#if !defined(__USE_MISC)
+#define __USE_MISC
+#endif
+#include <arpa/inet.h>
+#include <sys/select.h>
+#include <sys/time.h>
+#include <audiofile.h>
+
+#include "spandsp.h"
+
+int main(int argc, char *argv[])
+{
+    t38_non_ecm_buffer_state_t buffer;
+    logging_state_t logging;
+    uint8_t buf[1024];
+    int bit;
+    int n;
+
+    span_log_init(&logging, SPAN_LOG_FLOW, NULL);
+    span_log_set_protocol(&logging, "Buffer");
+
+    t38_non_ecm_buffer_init(&buffer, TRUE, 0);
+    memset(buf, 0, sizeof(buf));
+    memset(buf + 10, 0x55, 10);
+    buf[25] = 0x80;
+    memset(buf + 30, 0x55, 10);
+    t38_non_ecm_buffer_inject(&buffer, buf, 50);
+    t38_non_ecm_buffer_push(&buffer);
+    n = 0;
+    do
+    {
+        bit = t38_non_ecm_buffer_get_bit((void *) &buffer);
+        printf("Rx bit %d - %d\n", n++, bit);
+    }
+    while (bit >= 0);
+    t38_non_ecm_buffer_report_status(&buffer, &logging);
+
+    t38_non_ecm_buffer_init(&buffer, TRUE, 0);
+    memset(buf, 0, sizeof(buf));
+    memset(buf + 10, 0x55, 10);
+    buf[25] = 0x40;
+    memset(buf + 30, 0x55, 10);
+    t38_non_ecm_buffer_inject(&buffer, buf, 50);
+    t38_non_ecm_buffer_push(&buffer);
+    n = 0;
+    do
+    {
+        bit = t38_non_ecm_buffer_get_bit((void *) &buffer);
+        printf("Rx bit %d - %d\n", n++, bit);
+    }
+    while (bit >= 0);
+    t38_non_ecm_buffer_report_status(&buffer, &logging);
+
+    t38_non_ecm_buffer_init(&buffer, TRUE, 400);
+    memset(buf, 0, sizeof(buf));
+    memset(buf + 10, 0x55, 10);
+    buf[25] = 0x01;
+    memset(buf + 30, 0x55, 10);
+    t38_non_ecm_buffer_inject(&buffer, buf, 50);
+    t38_non_ecm_buffer_push(&buffer);
+    n = 0;
+    do
+    {
+        bit = t38_non_ecm_buffer_get_bit((void *) &buffer);
+        printf("Rx bit %d - %d\n", n++, bit);
+    }
+    while (bit >= 0);
+    t38_non_ecm_buffer_report_status(&buffer, &logging);
+
+    printf("Tests passed\n");
+    return  0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/tests/t38_terminal_tests.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/tests/t38_terminal_tests.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,411 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * t38_terminal_tests.c - Tests for the T.38 FoIP terminal module.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2005, 2006 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: t38_terminal_tests.c,v 1.61 2008/08/16 14:59:50 steveu Exp $
+ */
+
+/*! \file */
+
+/*! \page t38_terminal_tests_page T.38 termination tests
+\section t38_terminal_tests_page_sec_1 What does it do?
+These tests exercise the path
+
+    T.38 termination <-> T.38 termination
+*/
+
+#if defined(HAVE_CONFIG_H)
+#include <config.h>
+#endif
+
+#if defined(HAVE_FL_FL_H)  &&  defined(HAVE_FL_FL_CARTESIAN_H)  &&  defined(HAVE_FL_FL_AUDIO_METER_H)
+#define ENABLE_GUI
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <string.h>
+#include <assert.h>
+#include <errno.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <netinet/tcp.h>
+#if !defined(__USE_MISC)
+#define __USE_MISC
+#endif
+#include <arpa/inet.h>
+#include <sys/select.h>
+#include <sys/time.h>
+#include <audiofile.h>
+
+#include "spandsp.h"
+#include "spandsp-sim.h"
+
+#if defined(ENABLE_GUI)
+#include "media_monitor.h"
+#endif
+
+#define SAMPLES_PER_CHUNK 160
+
+#define INPUT_FILE_NAME         "../test-data/itu/fax/itutests.tif"
+#define OUTPUT_FILE_NAME        "t38.tif"
+
+t38_terminal_state_t t38_state_a;
+t38_terminal_state_t t38_state_b;
+
+g1050_state_t *path_a_to_b;
+g1050_state_t *path_b_to_a;
+
+double when = 0.0;
+
+int done[2] = {FALSE, FALSE};
+int succeeded[2] = {FALSE, FALSE};
+
+int simulate_incrementing_repeats = FALSE;
+
+static int phase_b_handler(t30_state_t *s, void *user_data, int result)
+{
+    int i;
+    
+    i = (int) (intptr_t) user_data;
+    printf("%c: Phase B handler on channel %c - (0x%X) %s\n", i, i, result, t30_frametype(result));
+    return T30_ERR_OK;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int phase_d_handler(t30_state_t *s, void *user_data, int result)
+{
+    int i;
+    t30_stats_t t;
+    const char *u;
+
+    i = (int) (intptr_t) user_data;
+    printf("%c: Phase D handler on channel %c - (0x%X) %s\n", i, i, result, t30_frametype(result));
+    t30_get_transfer_statistics(s, &t);
+    printf("%c: Phase D: bit rate %d\n", i, t.bit_rate);
+    printf("%c: Phase D: ECM %s\n", i, (t.error_correcting_mode)  ?  "on"  :  "off");
+    printf("%c: Phase D: pages transferred %d\n", i, t.pages_transferred);
+    printf("%c: Phase D: image size %d x %d\n", i, t.width, t.length);
+    printf("%c: Phase D: image resolution %d x %d\n", i, t.x_resolution, t.y_resolution);
+    printf("%c: Phase D: bad rows %d\n", i, t.bad_rows);
+    printf("%c: Phase D: longest bad row run %d\n", i, t.longest_bad_row_run);
+    printf("%c: Phase D: coding method %s\n", i, t4_encoding_to_str(t.encoding));
+    printf("%c: Phase D: image size %d bytes\n", i, t.image_size);
+    if ((u = t30_get_tx_ident(s)))
+        printf("%c: Phase D: local ident '%s'\n", i, u);
+    if ((u = t30_get_rx_ident(s)))
+        printf("%c: Phase D: remote ident '%s'\n", i, u);
+    printf("%c: Phase D: bits per row - min %d, max %d\n", i, s->t4.min_row_bits, s->t4.max_row_bits);
+    return T30_ERR_OK;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void phase_e_handler(t30_state_t *s, void *user_data, int result)
+{
+    int i;
+    t30_stats_t t;
+    const char *u;
+    
+    i = (int) (intptr_t) user_data;
+    printf("%c: Phase E handler on channel %c - (%d) %s\n", i, i, result, t30_completion_code_to_str(result));
+    t30_get_transfer_statistics(s, &t);
+    printf("%c: Phase E: bit rate %d\n", i, t.bit_rate);
+    printf("%c: Phase E: ECM %s\n", i, (t.error_correcting_mode)  ?  "on"  :  "off");
+    printf("%c: Phase E: pages transferred %d\n", i, t.pages_transferred);
+    printf("%c: Phase E: image size %d x %d\n", i, t.width, t.length);
+    printf("%c: Phase E: image resolution %d x %d\n", i, t.x_resolution, t.y_resolution);
+    printf("%c: Phase E: bad rows %d\n", i, t.bad_rows);
+    printf("%c: Phase E: longest bad row run %d\n", i, t.longest_bad_row_run);
+    printf("%c: Phase E: coding method %s\n", i, t4_encoding_to_str(t.encoding));
+    printf("%c: Phase E: image size %d bytes\n", i, t.image_size);
+    if ((u = t30_get_tx_ident(s)))
+        printf("%c: Phase E: local ident '%s'\n", i, u);
+    if ((u = t30_get_rx_ident(s)))
+        printf("%c: Phase E: remote ident '%s'\n", i, u);
+    succeeded[i - 'A'] = (result == T30_ERR_OK)  &&  (t.pages_transferred == 12);
+    //done[i - 'A'] = TRUE;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int tx_packet_handler_a(t38_core_state_t *s, void *user_data, const uint8_t *buf, int len, int count)
+{
+    t38_terminal_state_t *t;
+    int i;
+    static int subst_seq = 0;
+
+    /* This routine queues messages between two instances of T.38 processing */
+    t = (t38_terminal_state_t *) user_data;
+    if (simulate_incrementing_repeats)
+    {
+        for (i = 0;  i < count;  i++)
+        {
+            span_log(&s->logging, SPAN_LOG_FLOW, "Send seq %d, len %d\n", subst_seq, len);
+
+            if (g1050_put(path_a_to_b, buf, len, subst_seq, when) < 0)
+                printf("Lost packet %d\n", subst_seq);
+            subst_seq = (subst_seq + 1) & 0xFFFF;
+        }
+    }
+    else
+    {
+        span_log(&s->logging, SPAN_LOG_FLOW, "Send seq %d, len %d, count %d\n", s->tx_seq_no, len, count);
+
+        for (i = 0;  i < count;  i++)
+        {
+            if (g1050_put(path_a_to_b, buf, len, s->tx_seq_no, when) < 0)
+                printf("Lost packet %d\n", s->tx_seq_no);
+        }
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int tx_packet_handler_b(t38_core_state_t *s, void *user_data, const uint8_t *buf, int len, int count)
+{
+    t38_terminal_state_t *t;
+    int i;
+    static int subst_seq = 0;
+
+    /* This routine queues messages between two instances of T.38 processing */
+    t = (t38_terminal_state_t *) user_data;
+    if (simulate_incrementing_repeats)
+    {
+        for (i = 0;  i < count;  i++)
+        {
+            span_log(&s->logging, SPAN_LOG_FLOW, "Send seq %d, len %d\n", subst_seq, len);
+
+            g1050_put(path_b_to_a, buf, len, subst_seq, when);
+            subst_seq = (subst_seq + 1) & 0xFFFF;
+        }
+    }
+    else
+    {
+        span_log(&s->logging, SPAN_LOG_FLOW, "Send seq %d, len %d, count %d\n", s->tx_seq_no, len, count);
+
+        for (i = 0;  i < count;  i++)
+            g1050_put(path_b_to_a, buf, len, s->tx_seq_no, when);
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int main(int argc, char *argv[])
+{
+    int msg_len;
+    uint8_t msg[1024];
+    int t38_version;
+    int seq_no;
+    int use_ecm;
+    int without_pacing;
+    int use_tep;
+    int model_no;
+    int speed_pattern_no;
+    const char *input_file_name;
+    double tx_when;
+    double rx_when;
+    int use_gui;
+    int supported_modems;
+    int opt;
+    t30_state_t *t30;
+
+    t38_version = 1;
+    without_pacing = FALSE;
+    use_tep = FALSE;
+    input_file_name = INPUT_FILE_NAME;
+    use_ecm = FALSE;
+    simulate_incrementing_repeats = FALSE;
+    model_no = 0;
+    speed_pattern_no = 1;
+    use_gui = FALSE;
+    supported_modems = T30_SUPPORT_V27TER | T30_SUPPORT_V29 | T30_SUPPORT_V17;
+    while ((opt = getopt(argc, argv, "efgi:Im:M:ps:tv:")) != -1)
+    {
+        switch (opt)
+        {
+        case 'e':
+            use_ecm = TRUE;
+            break;
+        case 'g':
+#if defined(ENABLE_GUI)
+            use_gui = TRUE;
+#else
+            fprintf(stderr, "Graphical monitoring not available\n");
+            exit(2);
+#endif
+            break;
+        case 'i':
+            input_file_name = optarg;
+            break;
+        case 'I':
+            simulate_incrementing_repeats = TRUE;
+            break;
+        case 'm':
+            supported_modems = atoi(optarg);
+            break;
+        case 'M':
+            model_no = optarg[0] - 'A' + 1;
+            break;
+        case 'p':
+            without_pacing = TRUE;
+            break;
+        case 's':
+            speed_pattern_no = atoi(optarg);
+            break;
+        case 't':
+            use_tep = TRUE;
+            break;
+        case 'v':
+            t38_version = atoi(optarg);
+            break;
+        default:
+            //usage();
+            exit(2);
+            break;
+        }
+    }
+
+    printf("Using T.38 version %d\n", t38_version);
+    if (use_ecm)
+        printf("Using ECM\n");
+
+    srand48(0x1234567);
+    if ((path_a_to_b = g1050_init(model_no, speed_pattern_no, 100, 33)) == NULL)
+    {
+        fprintf(stderr, "Failed to start IP network path model\n");
+        exit(2);
+    }
+    if ((path_b_to_a = g1050_init(model_no, speed_pattern_no, 100, 33)) == NULL)
+    {
+        fprintf(stderr, "Failed to start IP network path model\n");
+        exit(2);
+    }
+
+    if (t38_terminal_init(&t38_state_a, TRUE, tx_packet_handler_a, &t38_state_b) == NULL)
+    {
+        fprintf(stderr, "Cannot start the T.38 channel\n");
+        exit(2);
+    }
+    t30 = t38_terminal_get_t30_state(&t38_state_a);
+    t38_set_t38_version(&t38_state_a.t38_fe.t38, t38_version);
+    t38_terminal_set_config(&t38_state_a, without_pacing);
+    t38_terminal_set_tep_mode(&t38_state_a, use_tep);
+    span_log_set_level(&t38_state_a.logging, SPAN_LOG_DEBUG | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME);
+    span_log_set_tag(&t38_state_a.logging, "T.38-A");
+    span_log_set_level(&t38_state_a.t38_fe.t38.logging, SPAN_LOG_DEBUG | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME);
+    span_log_set_tag(&t38_state_a.t38_fe.t38.logging, "T.38-A");
+    span_log_set_level(&t30->logging, SPAN_LOG_DEBUG | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME);
+    span_log_set_tag(&t30->logging, "T.38-A");
+
+    t30_set_supported_modems(t30, supported_modems);
+    t30_set_tx_ident(t30, "11111111");
+    t30_set_tx_nsf(t30, (const uint8_t *) "\x50\x00\x00\x00Spandsp\x00", 12);
+    t30_set_tx_file(t30, input_file_name, -1, -1);
+    t30_set_phase_b_handler(t30, phase_b_handler, (void *) (intptr_t) 'A');
+    t30_set_phase_d_handler(t30, phase_d_handler, (void *) (intptr_t) 'A');
+    t30_set_phase_e_handler(t30, phase_e_handler, (void *) (intptr_t) 'A');
+    t30_set_ecm_capability(t30, use_ecm);
+    if (use_ecm)
+        t30_set_supported_compressions(t30, T30_SUPPORT_T4_1D_COMPRESSION | T30_SUPPORT_T4_2D_COMPRESSION | T30_SUPPORT_T6_COMPRESSION);
+
+    if (t38_terminal_init(&t38_state_b, FALSE, tx_packet_handler_b, &t38_state_a) == NULL)
+    {
+        fprintf(stderr, "Cannot start the T.38 channel\n");
+        exit(2);
+    }
+    t30 = t38_terminal_get_t30_state(&t38_state_b);
+    t38_set_t38_version(&t38_state_b.t38_fe.t38, t38_version);
+    t38_terminal_set_config(&t38_state_b, without_pacing);
+    t38_terminal_set_tep_mode(&t38_state_b, use_tep);
+    span_log_set_level(&t38_state_b.logging, SPAN_LOG_DEBUG | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME);
+    span_log_set_tag(&t38_state_b.logging, "T.38-B");
+    span_log_set_level(&t38_state_b.t38_fe.t38.logging, SPAN_LOG_DEBUG | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME);
+    span_log_set_tag(&t38_state_b.t38_fe.t38.logging, "T.38-B");
+    span_log_set_level(&t30->logging, SPAN_LOG_DEBUG | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME);
+    span_log_set_tag(&t30->logging, "T.38-B");
+
+    t30_set_supported_modems(t30, supported_modems);
+    t30_set_tx_ident(t30, "22222222");
+    t30_set_tx_nsf(t30, (const uint8_t *) "\x50\x00\x00\x00Spandsp\x00", 12);
+    t30_set_rx_file(t30, OUTPUT_FILE_NAME, -1);
+    t30_set_phase_b_handler(t30, phase_b_handler, (void *) (intptr_t) 'B');
+    t30_set_phase_d_handler(t30, phase_d_handler, (void *) (intptr_t) 'B');
+    t30_set_phase_e_handler(t30, phase_e_handler, (void *) (intptr_t) 'B');
+    t30_set_ecm_capability(t30, use_ecm);
+    if (use_ecm)
+        t30_set_supported_compressions(t30, T30_SUPPORT_T4_1D_COMPRESSION | T30_SUPPORT_T4_2D_COMPRESSION | T30_SUPPORT_T6_COMPRESSION);
+
+#if defined(ENABLE_GUI)
+    if (use_gui)
+        start_media_monitor();
+#endif
+    for (;;)
+    {
+        span_log_bump_samples(&t38_state_a.logging, SAMPLES_PER_CHUNK);
+        span_log_bump_samples(&t38_state_a.t38_fe.t38.logging, SAMPLES_PER_CHUNK);
+        span_log_bump_samples(&t38_state_a.t30.logging, SAMPLES_PER_CHUNK);
+        span_log_bump_samples(&t38_state_b.logging, SAMPLES_PER_CHUNK);
+        span_log_bump_samples(&t38_state_b.t38_fe.t38.logging, SAMPLES_PER_CHUNK);
+        span_log_bump_samples(&t38_state_b.t30.logging, SAMPLES_PER_CHUNK);
+
+        done[0] = t38_terminal_send_timeout(&t38_state_a, SAMPLES_PER_CHUNK);
+        done[1] = t38_terminal_send_timeout(&t38_state_b, SAMPLES_PER_CHUNK);
+
+        when += 0.02;
+
+        while ((msg_len = g1050_get(path_a_to_b, msg, 1024, when, &seq_no, &tx_when, &rx_when)) >= 0)
+        {
+#if defined(ENABLE_GUI)
+            if (use_gui)
+                media_monitor_rx(seq_no, tx_when, rx_when);
+#endif
+            t38_core_rx_ifp_packet(&t38_state_b.t38_fe.t38, msg, msg_len, seq_no);
+        }
+        while ((msg_len = g1050_get(path_b_to_a, msg, 1024, when, &seq_no, &tx_when, &rx_when)) >= 0)
+        {
+#if defined(ENABLE_GUI)
+            if (use_gui)
+                media_monitor_rx(seq_no, tx_when, rx_when);
+#endif
+            t38_core_rx_ifp_packet(&t38_state_a.t38_fe.t38, msg, msg_len, seq_no);
+        }
+        if (done[0]  &&  done[1])
+            break;
+#if defined(ENABLE_GUI)
+        if (use_gui)
+            media_monitor_update_display();
+#endif
+    }
+    t38_terminal_release(&t38_state_a);
+    t38_terminal_release(&t38_state_b);
+    if (!succeeded[0]  ||  !succeeded[1])
+    {
+        printf("Tests failed\n");
+        exit(2);
+    }
+    printf("Tests passed\n");
+    return  0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/tests/t38_terminal_to_gateway_tests.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/tests/t38_terminal_to_gateway_tests.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,500 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * t38_terminal_to_gateway_tests.c - Joint tests for the T.38 FoIP terminal and gateway modules.
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2005, 2006 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: t38_terminal_to_gateway_tests.c,v 1.59 2008/08/16 15:24:16 steveu Exp $
+ */
+
+/*! \file */
+
+/*! \page t38_terminal_to_gateway_tests_page T.38 mixed gateway and termination tests
+\section t38_terminal_to_gateway_tests_page_sec_1 What does it do?
+These tests exercise the path
+
+    T.38 termination -> T.38 gateway -> FAX machine
+*/
+
+#if defined(HAVE_CONFIG_H)
+#include <config.h>
+#endif
+
+#if defined(HAVE_FL_FL_H)  &&  defined(HAVE_FL_FL_CARTESIAN_H)  &&  defined(HAVE_FL_FL_AUDIO_METER_H)
+#define ENABLE_GUI
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <string.h>
+#include <assert.h>
+#include <errno.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <netinet/tcp.h>
+#if !defined(__USE_MISC)
+#define __USE_MISC
+#endif
+#include <arpa/inet.h>
+#include <sys/select.h>
+#include <sys/time.h>
+#include <audiofile.h>
+
+#include "spandsp.h"
+#include "spandsp-sim.h"
+
+#if defined(ENABLE_GUI)
+#include "media_monitor.h"
+#endif
+
+#define SAMPLES_PER_CHUNK       160
+
+#define INPUT_FILE_NAME         "../test-data/itu/fax/itutests.tif"
+#define OUTPUT_FILE_NAME        "t38.tif"
+#define OUTPUT_FILE_NAME_WAVE   "t38_terminal_to_gateway.wav"
+
+t38_terminal_state_t t38_state_a;
+t38_gateway_state_t t38_state_b;
+fax_state_t fax_state_b;
+
+g1050_state_t *path_a_to_b;
+g1050_state_t *path_b_to_a;
+
+double when = 0.0;
+
+int done[2] = {FALSE, FALSE};
+int succeeded[2] = {FALSE, FALSE};
+
+int simulate_incrementing_repeats = FALSE;
+
+static int phase_b_handler(t30_state_t *s, void *user_data, int result)
+{
+    int i;
+    
+    i = (int) (intptr_t) user_data;
+    printf("%c: Phase B handler on channel %c - (0x%X) %s\n", i, i, result, t30_frametype(result));
+    return T30_ERR_OK;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int phase_d_handler(t30_state_t *s, void *user_data, int result)
+{
+    int i;
+    t30_stats_t t;
+    const char *u;
+
+    i = (int) (intptr_t) user_data;
+    printf("%c: Phase D handler on channel %c - (0x%X) %s\n", i, i, result, t30_frametype(result));
+    t30_get_transfer_statistics(s, &t);
+    printf("%c: Phase D: bit rate %d\n", i, t.bit_rate);
+    printf("%c: Phase D: ECM %s\n", i, (t.error_correcting_mode)  ?  "on"  :  "off");
+    printf("%c: Phase D: pages transferred %d\n", i, t.pages_transferred);
+    printf("%c: Phase D: image size %d x %d\n", i, t.width, t.length);
+    printf("%c: Phase D: image resolution %d x %d\n", i, t.x_resolution, t.y_resolution);
+    printf("%c: Phase D: bad rows %d\n", i, t.bad_rows);
+    printf("%c: Phase D: longest bad row run %d\n", i, t.longest_bad_row_run);
+    printf("%c: Phase D: coding method %s\n", i, t4_encoding_to_str(t.encoding));
+    printf("%c: Phase D: image size %d bytes\n", i, t.image_size);
+    if ((u = t30_get_tx_ident(s)))
+        printf("%c: Phase D: local ident '%s'\n", i, u);
+    if ((u = t30_get_rx_ident(s)))
+        printf("%c: Phase D: remote ident '%s'\n", i, u);
+    printf("%c: Phase D: bits per row - min %d, max %d\n", i, s->t4.min_row_bits, s->t4.max_row_bits);
+    return T30_ERR_OK;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void phase_e_handler(t30_state_t *s, void *user_data, int result)
+{
+    int i;
+    t30_stats_t t;
+    const char *u;
+    
+    i = (int) (intptr_t) user_data;
+    printf("%c: Phase E handler on channel %c - (%d) %s\n", i, i, result, t30_completion_code_to_str(result));
+    t30_get_transfer_statistics(s, &t);
+    printf("%c: Phase E: bit rate %d\n", i, t.bit_rate);
+    printf("%c: Phase E: ECM %s\n", i, (t.error_correcting_mode)  ?  "on"  :  "off");
+    printf("%c: Phase E: pages transferred %d\n", i, t.pages_transferred);
+    printf("%c: Phase E: image size %d x %d\n", i, t.width, t.length);
+    printf("%c: Phase E: image resolution %d x %d\n", i, t.x_resolution, t.y_resolution);
+    printf("%c: Phase E: bad rows %d\n", i, t.bad_rows);
+    printf("%c: Phase E: longest bad row run %d\n", i, t.longest_bad_row_run);
+    printf("%c: Phase E: coding method %s\n", i, t4_encoding_to_str(t.encoding));
+    printf("%c: Phase E: image size %d bytes\n", i, t.image_size);
+    if ((u = t30_get_tx_ident(s)))
+        printf("%c: Phase E: local ident '%s'\n", i, u);
+    if ((u = t30_get_rx_ident(s)))
+        printf("%c: Phase E: remote ident '%s'\n", i, u);
+    succeeded[i - 'A'] = (result == T30_ERR_OK)  &&  (t.pages_transferred == 12);
+    done[i - 'A'] = TRUE;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int tx_packet_handler_a(t38_core_state_t *s, void *user_data, const uint8_t *buf, int len, int count)
+{
+    t38_terminal_state_t *t;
+    int i;
+    static int subst_seq = 0;
+
+    /* This routine queues messages between two instances of T.38 processing */
+    t = (t38_terminal_state_t *) user_data;
+    if (simulate_incrementing_repeats)
+    {
+        for (i = 0;  i < count;  i++)
+        {
+            span_log(&s->logging, SPAN_LOG_FLOW, "Send seq %d, len %d\n", subst_seq, len);
+
+            g1050_put(path_a_to_b, buf, len, subst_seq, when);
+            subst_seq = (subst_seq + 1) & 0xFFFF;
+        }
+    }
+    else
+    {
+        span_log(&s->logging, SPAN_LOG_FLOW, "Send seq %d, len %d, count %d\n", s->tx_seq_no, len, count);
+
+        for (i = 0;  i < count;  i++)
+            g1050_put(path_a_to_b, buf, len, s->tx_seq_no, when);
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int tx_packet_handler_b(t38_core_state_t *s, void *user_data, const uint8_t *buf, int len, int count)
+{
+    t38_terminal_state_t *t;
+    int i;
+    static int subst_seq = 0;
+
+    /* This routine queues messages between two instances of T.38 processing */
+    t = (t38_terminal_state_t *) user_data;
+    if (simulate_incrementing_repeats)
+    {
+        for (i = 0;  i < count;  i++)
+        {
+            span_log(&s->logging, SPAN_LOG_FLOW, "Send seq %d, len %d\n", subst_seq, len);
+
+            g1050_put(path_b_to_a, buf, len, subst_seq, when);
+            subst_seq = (subst_seq + 1) & 0xFFFF;
+        }
+    }
+    else
+    {
+        span_log(&s->logging, SPAN_LOG_FLOW, "Send seq %d, len %d, count %d\n", s->tx_seq_no, len, count);
+
+        for (i = 0;  i < count;  i++)
+            g1050_put(path_b_to_a, buf, len, s->tx_seq_no, when);
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int main(int argc, char *argv[])
+{
+    int16_t t38_amp_b[SAMPLES_PER_CHUNK];
+    int16_t t30_amp_b[SAMPLES_PER_CHUNK];
+    int16_t out_amp[2*SAMPLES_PER_CHUNK];
+    int t38_len_b;
+    int t30_len_b;
+    int msg_len;
+    uint8_t msg[1024];
+    int log_audio;
+    int outframes;
+    AFfilehandle wave_handle;
+    int t38_version;
+    int use_ecm;
+    int use_tep;
+    int feedback_audio;
+    int use_transmit_on_idle;
+    const char *input_file_name;
+    int i;
+    int seq_no;
+    int model_no;
+    int speed_pattern_no;
+    double tx_when;
+    double rx_when;
+    int use_gui;
+    int supported_modems;
+    int opt;
+    t30_state_t *t30;
+
+    log_audio = FALSE;
+    t38_version = 1;
+    use_ecm = FALSE;
+    input_file_name = INPUT_FILE_NAME;
+    simulate_incrementing_repeats = FALSE;
+    model_no = 0;
+    speed_pattern_no = 1;
+    use_gui = FALSE;
+    use_tep = FALSE;
+    feedback_audio = FALSE;
+    use_transmit_on_idle = TRUE;
+    supported_modems = T30_SUPPORT_V27TER | T30_SUPPORT_V29 | T30_SUPPORT_V17;
+    while ((opt = getopt(argc, argv, "efgi:Ilm:M:s:tv:")) != -1)
+    {
+        switch (opt)
+        {
+        case 'e':
+            use_ecm = TRUE;
+            break;
+        case 'f':
+            feedback_audio = TRUE;
+            break;
+        case 'g':
+#if defined(ENABLE_GUI)
+            use_gui = TRUE;
+#else
+            fprintf(stderr, "Graphical monitoring not available\n");
+            exit(2);
+#endif
+            break;
+        case 'i':
+            input_file_name = optarg;
+            break;
+        case 'I':
+            simulate_incrementing_repeats = TRUE;
+            break;
+        case 'l':
+            log_audio = TRUE;
+            break;
+        case 'm':
+            supported_modems = atoi(optarg);
+            break;
+        case 'M':
+            model_no = optarg[0] - 'A' + 1;
+            break;
+        case 's':
+            speed_pattern_no = atoi(optarg);
+            break;
+        case 't':
+            use_tep = TRUE;
+            break;
+        case 'v':
+            t38_version = atoi(optarg);
+            break;
+        default:
+            //usage();
+            exit(2);
+            break;
+        }
+    }
+
+    printf("Using T.38 version %d\n", t38_version);
+    if (use_ecm)
+        printf("Using ECM\n");
+
+    wave_handle = AF_NULL_FILEHANDLE;
+    if (log_audio)
+    {
+        if ((wave_handle = afOpenFile_telephony_write(OUTPUT_FILE_NAME_WAVE, 2)) == AF_NULL_FILEHANDLE)
+        {
+            fprintf(stderr, "    Cannot create wave file '%s'\n", OUTPUT_FILE_NAME_WAVE);
+            exit(2);
+        }
+    }
+
+    srand48(0x1234567);
+    if ((path_a_to_b = g1050_init(model_no, speed_pattern_no, 100, 33)) == NULL)
+    {
+        fprintf(stderr, "Failed to start IP network path model\n");
+        exit(2);
+    }
+    if ((path_b_to_a = g1050_init(model_no, speed_pattern_no, 100, 33)) == NULL)
+    {
+        fprintf(stderr, "Failed to start IP network path model\n");
+        exit(2);
+    }
+
+    if (t38_terminal_init(&t38_state_a, TRUE, tx_packet_handler_a, &t38_state_b) == NULL)
+    {
+        fprintf(stderr, "Cannot start the T.38 channel\n");
+        exit(2);
+    }
+    t30 = t38_terminal_get_t30_state(&t38_state_a);
+    t38_set_t38_version(&t38_state_a.t38_fe.t38, t38_version);
+    span_log_set_level(&t38_state_a.logging, SPAN_LOG_DEBUG | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME);
+    span_log_set_tag(&t38_state_a.logging, "T.38-A");
+    span_log_set_level(&t38_state_a.t38_fe.t38.logging, SPAN_LOG_DEBUG | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME);
+    span_log_set_tag(&t38_state_a.t38_fe.t38.logging, "T.38-A");
+    span_log_set_level(&t30->logging, SPAN_LOG_DEBUG | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME);
+    span_log_set_tag(&t30->logging, "T.38-A");
+
+    t30_set_supported_modems(t30, supported_modems);
+    t30_set_tx_ident(t30, "11111111");
+    t30_set_tx_file(t30, input_file_name, -1, -1);
+    t30_set_phase_b_handler(t30, phase_b_handler, (void *) (intptr_t) 'A');
+    t30_set_phase_d_handler(t30, phase_d_handler, (void *) (intptr_t) 'A');
+    t30_set_phase_e_handler(t30, phase_e_handler, (void *) (intptr_t) 'A');
+    t30_set_ecm_capability(t30, use_ecm);
+    if (use_ecm)
+        t30_set_supported_compressions(t30, T30_SUPPORT_T4_1D_COMPRESSION | T30_SUPPORT_T4_2D_COMPRESSION | T30_SUPPORT_T6_COMPRESSION);
+
+    if (t38_gateway_init(&t38_state_b, tx_packet_handler_b, &t38_state_a) == NULL)
+    {
+        fprintf(stderr, "Cannot start the T.38 channel\n");
+        exit(2);
+    }
+    t38_gateway_set_transmit_on_idle(&t38_state_b, use_transmit_on_idle);
+    t38_set_t38_version(&t38_state_b.t38x.t38, t38_version);
+    t38_gateway_set_ecm_capability(&t38_state_b, use_ecm);
+    span_log_set_level(&t38_state_b.logging, SPAN_LOG_DEBUG | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME);
+    span_log_set_tag(&t38_state_b.logging, "T.38-B");
+    span_log_set_level(&t38_state_b.t38x.t38.logging, SPAN_LOG_DEBUG | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME);
+    span_log_set_tag(&t38_state_b.t38x.t38.logging, "T.38-B");
+    memset(t38_amp_b, 0, sizeof(t38_amp_b));
+
+    if (fax_init(&fax_state_b, FALSE) == NULL)
+    {
+        fprintf(stderr, "Cannot start FAX\n");
+        exit(2);
+    }
+    t30 = fax_get_t30_state(&fax_state_b);
+    fax_set_transmit_on_idle(&fax_state_b, use_transmit_on_idle);
+    fax_set_tep_mode(&fax_state_b, use_tep);
+    t30_set_supported_modems(t30, supported_modems);
+    t30_set_tx_ident(t30, "22222222");
+    t30_set_tx_nsf(t30, (const uint8_t *) "\x50\x00\x00\x00Spandsp\x00", 12);
+    t30_set_rx_file(t30, OUTPUT_FILE_NAME, -1);
+    t30_set_phase_b_handler(t30, phase_b_handler, (void *) (intptr_t) 'B');
+    t30_set_phase_d_handler(t30, phase_d_handler, (void *) (intptr_t) 'B');
+    t30_set_phase_e_handler(t30, phase_e_handler, (void *) (intptr_t) 'B');
+    t30_set_ecm_capability(t30, use_ecm);
+    if (use_ecm)
+        t30_set_supported_compressions(t30, T30_SUPPORT_T4_1D_COMPRESSION | T30_SUPPORT_T4_2D_COMPRESSION | T30_SUPPORT_T6_COMPRESSION);
+    span_log_set_level(&fax_state_b.logging, SPAN_LOG_DEBUG | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME);
+    span_log_set_tag(&fax_state_b.logging, "FAX-B ");
+    span_log_set_level(&t30->logging, SPAN_LOG_DEBUG | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME);
+    span_log_set_tag(&t30->logging, "FAX-B ");
+    memset(t30_amp_b, 0, sizeof(t30_amp_b));
+
+#if defined(ENABLE_GUI)
+    if (use_gui)
+        start_media_monitor();
+#endif
+    for (;;)
+    {
+        span_log_bump_samples(&t38_state_a.logging, SAMPLES_PER_CHUNK);
+        span_log_bump_samples(&t38_state_a.t38_fe.t38.logging, SAMPLES_PER_CHUNK);
+        span_log_bump_samples(&t38_state_a.t30.logging, SAMPLES_PER_CHUNK);
+        span_log_bump_samples(&t38_state_b.logging, SAMPLES_PER_CHUNK);
+        span_log_bump_samples(&t38_state_b.t38x.t38.logging, SAMPLES_PER_CHUNK);
+        span_log_bump_samples(&fax_state_b.logging, SAMPLES_PER_CHUNK);
+        span_log_bump_samples(&fax_state_b.t30.logging, SAMPLES_PER_CHUNK);
+        memset(out_amp, 0, sizeof(out_amp));
+
+        t38_terminal_send_timeout(&t38_state_a, SAMPLES_PER_CHUNK);
+
+        t30_len_b = fax_tx(&fax_state_b, t30_amp_b, SAMPLES_PER_CHUNK);
+        if (!use_transmit_on_idle)
+        {
+            /* The receive side always expects a full block of samples, but the
+               transmit side may not be sending any when it doesn't need to. We
+               may need to pad with some silence. */
+            if (t30_len_b < SAMPLES_PER_CHUNK)
+            {
+                memset(t30_amp_b + t30_len_b, 0, sizeof(int16_t)*(SAMPLES_PER_CHUNK - t30_len_b));
+                t30_len_b = SAMPLES_PER_CHUNK;
+            }
+        }
+        if (feedback_audio)
+        {
+            for (i = 0;  i < t30_len_b;  i++)
+                t30_amp_b[i] += t38_amp_b[i] >> 1;
+        }
+        if (log_audio)
+        {
+            for (i = 0;  i < t30_len_b;  i++)
+                out_amp[2*i + 1] = t30_amp_b[i];
+        }
+        if (t38_gateway_rx(&t38_state_b, t30_amp_b, t30_len_b))
+            break;
+    
+        t38_len_b = t38_gateway_tx(&t38_state_b, t38_amp_b, SAMPLES_PER_CHUNK);
+        if (!use_transmit_on_idle)
+        {
+            if (t38_len_b < SAMPLES_PER_CHUNK)
+            {
+                memset(t38_amp_b + t38_len_b, 0, sizeof(int16_t)*(SAMPLES_PER_CHUNK - t38_len_b));
+                t38_len_b = SAMPLES_PER_CHUNK;
+            }
+        }
+        if (log_audio)
+        {
+            for (i = 0;  i < t38_len_b;  i++)
+                out_amp[2*i] = t38_amp_b[i];
+        }
+        if (fax_rx(&fax_state_b, t38_amp_b, SAMPLES_PER_CHUNK))
+            break;
+
+        when += 0.02;
+
+        while ((msg_len = g1050_get(path_a_to_b, msg, 1024, when, &seq_no, &tx_when, &rx_when)) >= 0)
+        {
+#if defined(ENABLE_GUI)
+            if (use_gui)
+                media_monitor_rx(seq_no, tx_when, rx_when);
+#endif
+            t38_core_rx_ifp_packet(&t38_state_b.t38x.t38, msg, msg_len, seq_no);
+        }
+        while ((msg_len = g1050_get(path_b_to_a, msg, 1024, when, &seq_no, &tx_when, &rx_when)) >= 0)
+        {
+#if defined(ENABLE_GUI)
+            if (use_gui)
+                media_monitor_rx(seq_no, tx_when, rx_when);
+#endif
+            t38_core_rx_ifp_packet(&t38_state_a.t38_fe.t38, msg, msg_len, seq_no);
+        }
+        if (log_audio)
+        {
+            outframes = afWriteFrames(wave_handle, AF_DEFAULT_TRACK, out_amp, SAMPLES_PER_CHUNK);
+            if (outframes != SAMPLES_PER_CHUNK)
+                break;
+        }
+
+        if (done[0]  &&  done[1])
+            break;
+#if defined(ENABLE_GUI)
+        if (use_gui)
+            media_monitor_update_display();
+#endif
+    }
+    t38_terminal_release(&t38_state_a);
+    fax_release(&fax_state_b);
+    if (log_audio)
+    {
+        if (afCloseFile(wave_handle) != 0)
+        {
+            fprintf(stderr, "    Cannot close wave file '%s'\n", OUTPUT_FILE_NAME_WAVE);
+            exit(2);
+        }
+    }
+    if (!succeeded[0]  ||  !succeeded[1])
+    {
+        printf("Tests failed\n");
+        exit(2);
+    }
+    printf("Tests passed\n");
+    return  0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/tests/t4_tests.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/tests/t4_tests.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,579 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * t4_tests.c - ITU T.4 FAX image to and from TIFF file tests
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2003 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: t4_tests.c,v 1.58 2008/08/17 16:25:52 steveu Exp $
+ */
+
+/*! \file */
+
+/*! \page t4_tests_page T.4 tests
+\section t4_tests_page_sec_1 What does it do
+These tests exercise the image compression and decompression methods defined
+in ITU specifications T.4 and T.6.
+*/
+
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <memory.h>
+
+#include "spandsp.h"
+
+#define IN_FILE_NAME    "../test-data/itu/fax/itutests.tif"
+#define OUT_FILE_NAME   "t4_tests_receive.tif"
+
+#define XSIZE           1728
+
+t4_state_t send_state;
+t4_state_t receive_state;
+
+/* The following are some test cases from T.4 */
+#define FILL_70      "                                                                      "
+#define FILL_80      "                                                                                "
+#define FILL_100     "                                                                                                    "
+#define FILL_670     FILL_100 FILL_100 FILL_100 FILL_100 FILL_100 FILL_100 FILL_70
+#define FILL_980     FILL_100 FILL_100 FILL_100 FILL_100 FILL_100 FILL_100 FILL_100 FILL_100 FILL_100 FILL_80
+
+static const char t4_test_patterns[][1728 + 1] =
+{
+    "XXXXXX              " FILL_980 "  XXX  XXX X                  " FILL_670 "                        XXXX",
+    "XXXXXX              " FILL_980 "     XXX   X                  " FILL_670 "                        XXXX",
+    /* Line start should code to V(0). Line middle codes to VR(3) VL(2) V(0). Line end should code to V(0) V(0). */
+
+    " XXXX               " FILL_980 "    XXXXXXX                   " FILL_670 "                        XX  ",
+    "XXXXX               " FILL_980 "XX       XX                   " FILL_670 "                        XXXX",
+    /* Line start should code to VL(1). Line middle codes to H(7,2). Line end should code to V(0) VR(2) */
+
+    "XXX                 " FILL_980 " XX  XX   XX        XXX       " FILL_670 "                      X     ",
+    "                    " FILL_980 " X       XXX   XXXX           " FILL_670 "                      X   XX",
+    /* Line start should code to P. Line middle codes to P VL(1) V(0) H(3,4) P. Line end codes to V(0) VL(2) V(0). */
+
+    "XXXXX               " FILL_980 "                              " FILL_670 "                        XXXX",
+    "  XXX               " FILL_980 "                              " FILL_670 "                        XX  ",
+    /* Line start should code to VR(2). Line end codes to V(0) VL(2) V(0). */
+
+    "         XX         " FILL_980 "                              " FILL_670 "                      X  XXX",
+    "XXX       X         " FILL_980 "                              " FILL_670 "                      X     ",
+    /* Line start should code to H(0,3) VR(1). Line end codes to V(0) VR(3). */
+
+    "                    " FILL_980 "                              " FILL_670 "                         XX ",
+    "                    " FILL_980 "                              " FILL_670 "                            ",
+    /* Line end codes to P V(0) a'0. */
+
+    "                    " FILL_980 "                              " FILL_670 "                  XXXXXXXXXX",
+    "                    " FILL_980 "                              " FILL_670 "              XXXXXX  XXXXXX",
+    /* Line end codes to H(2,6). */
+
+    "                    " FILL_980 "                              " FILL_670 "                   XX  XXXXX",
+    "                    " FILL_980 "                              " FILL_670 "                   XX       ",
+    /* Line end codes to V(0) H(7,0). */
+};
+
+static void dump_image_as_xxx(t4_state_t *state)
+{
+    uint8_t *s;
+    int i;
+    int j;
+    int k;
+
+    /* Dump the entire image as text 'X's and spaces */
+    printf("Image (%d x %d):\n", receive_state.image_width, receive_state.image_length);
+    s = state->image_buffer;
+    for (i = 0;  i < state->image_length;  i++)
+    {
+        for (j = 0;  j < state->bytes_per_row;  j++)
+        {
+            for (k = 0;  k < 8;  k++)
+            {
+                printf((state->image_buffer[i*state->bytes_per_row + j] & (0x80 >> k))  ?  "X"  :  " ");
+            }
+        }
+        printf("\n");
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static void display_page_stats(t4_state_t *s)
+{
+    t4_stats_t stats;
+
+    t4_get_transfer_statistics(s, &stats);
+    printf("Pages = %d\n", stats.pages_transferred);
+    printf("Image size = %d x %d pixels\n", stats.width, stats.length);
+    printf("Image resolution = %d/m x %d/m\n", stats.x_resolution, stats.y_resolution);
+    printf("Bad rows = %d\n", stats.bad_rows);
+    printf("Longest bad row run = %d\n", stats.longest_bad_row_run);
+    printf("Bits per row - min %d, max %d\n", s->min_row_bits, s->max_row_bits);
+}
+/*- End of function --------------------------------------------------------*/
+
+#if 0
+static int row_read_handler(void *user_data, uint8_t buf[], size_t len)
+{
+    int i;
+    int j;
+    const char *s;
+    static int row = 0;
+
+    s = t4_test_patterns[row++];
+    if (row >= 16)
+        return 0;
+    memset(buf, 0, len);
+    for (i = 0;  i < len;  i++)
+    {
+        for (j = 0;  j < 8;  j++)
+        {
+            if (*s++ != ' ')
+                buf[i] |= (0x80 >> j);
+        }
+    }
+    if (*s)
+        printf("Oops - '%c' at end of row %d\n", *s, row);
+    return len;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int row_write_handler(void *user_data, const uint8_t buf[], size_t len)
+{
+    int i;
+    int j;
+    const char *s;
+    static int row = 0;
+    uint8_t ref[8192];
+
+    if (len == 0)
+        return 0;
+    s = t4_test_patterns[row++];
+    if (row >= 16)
+        row = 0;
+    memset(ref, 0, len);
+    for (i = 0;  i < len;  i++)
+    {
+        for (j = 0;  j < 8;  j++)
+        {
+            if (*s++ != ' ')
+                ref[i] |= (0x80 >> j);
+        }
+    }
+    if (*s)
+        printf("Oops - '%c' at end of row %d\n", *s, row);
+    if (memcmp(buf, ref, len))
+    {
+        printf("Failed at row %d\n", row);
+        exit(2);
+    }
+    return len;
+}
+/*- End of function --------------------------------------------------------*/
+#endif
+
+int main(int argc, char *argv[])
+{
+    int sends;
+    int page_no;
+    int bit;
+    int end_of_page;
+    int end_marks;
+    int decode_test;
+    int compression;
+    int compression_step;
+    int add_page_headers;
+    int min_row_bits;
+    int restart_pages;
+    int block_size;
+    char buf[1024];
+    uint8_t block[1024];
+    const char *in_file_name;
+    int opt;
+    int i;
+    int bit_error_rate;
+    int dump_as_xxx;
+    int tests_failed;
+
+    tests_failed = 0;
+    decode_test = FALSE;
+    compression = -1;
+    add_page_headers = FALSE;
+    restart_pages = FALSE;
+    in_file_name = IN_FILE_NAME;
+    min_row_bits = 0;
+    block_size = 0;
+    bit_error_rate = 0;
+    dump_as_xxx = FALSE;
+    while ((opt = getopt(argc, argv, "126b:dehri:m:x")) != -1)
+    {
+        switch (opt)
+        {
+        case '1':
+            compression = T4_COMPRESSION_ITU_T4_1D;
+            break;
+        case '2':
+            compression = T4_COMPRESSION_ITU_T4_2D;
+            break;
+        case '6':
+            compression = T4_COMPRESSION_ITU_T6;
+            break;
+        case 'b':
+            block_size = atoi(optarg);
+            if (block_size > 1024)
+                block_size = 1024;
+            break;
+        case 'd':
+            decode_test = TRUE;
+            break;
+        case 'e':
+            bit_error_rate = 0x3FF;
+            break;
+        case 'h':
+            add_page_headers = TRUE;
+            break;
+        case 'r':
+            restart_pages = TRUE;
+            break;
+        case 'i':
+            in_file_name = optarg;
+            break;
+        case 'm':
+            min_row_bits = atoi(optarg);
+            break;
+        case 'x':
+            dump_as_xxx = TRUE;
+            break;
+        default:
+            //usage();
+            exit(2);
+            break;
+        }
+    }
+    /* Create a send and a receive end */
+    memset(&send_state, 0, sizeof(send_state));
+    memset(&receive_state, 0, sizeof(receive_state));
+
+    end_of_page = FALSE;
+    if (decode_test)
+    {
+        if (compression < 0)
+            compression = T4_COMPRESSION_ITU_T4_1D;
+        /* Receive end puts TIFF to a new file. We assume the receive width here. */
+        if (t4_rx_init(&receive_state, OUT_FILE_NAME, T4_COMPRESSION_ITU_T4_2D) == NULL)
+        {
+            printf("Failed to init T.4 rx\n");
+            exit(2);
+        }
+        
+        t4_rx_set_rx_encoding(&receive_state, compression);
+        t4_rx_set_x_resolution(&receive_state, T4_X_RESOLUTION_R8);
+        //t4_rx_set_y_resolution(&receive_state, T4_Y_RESOLUTION_FINE);
+        t4_rx_set_y_resolution(&receive_state, T4_Y_RESOLUTION_STANDARD);
+        t4_rx_set_image_width(&receive_state, XSIZE);
+
+        page_no = 1;
+        t4_rx_start_page(&receive_state);
+        while (fgets(buf, 1024, stdin))
+        {
+            if (sscanf(buf, "HDLC:  FCD: 06 %x", &bit) == 1)
+            {
+                /* Useful for breaking up T.38 ECM logs */
+                for (i = 0;  i < 256;  i++)
+                {
+                    if (sscanf(&buf[18 + 3*i], "%x", &bit) != 1)
+                        break;
+                    if ((end_of_page = t4_rx_put_byte(&receive_state, bit)))
+                        break;
+                }
+            }
+            else if (strlen(buf) > 62  &&  sscanf(buf + 62, "Rx %d: IFP %x %x", &bit, &bit, &bit) == 3)
+            {
+                /* Useful for breaking up T.38 non-ECM logs */
+                for (i = 0;  i < 256;  i++)
+                {
+                    if (sscanf(&buf[62 + 29 + 3*i], "%x", &bit) != 1)
+                        break;
+                    bit = bit_reverse8(bit);
+                    if ((end_of_page = t4_rx_put_byte(&receive_state, bit)))
+                        break;
+                }
+            }
+            else if (sscanf(buf, "Rx bit %*d - %d", &bit) == 1)
+            {
+                if ((end_of_page = t4_rx_put_bit(&receive_state, bit)))
+                {
+                    printf("End of page detected\n");
+                    break;
+                }
+            }
+        }
+        if (dump_as_xxx)
+            dump_image_as_xxx(&receive_state);
+        t4_rx_end_page(&receive_state);
+        display_page_stats(&receive_state);
+        t4_rx_end(&receive_state);
+    }
+    else
+    {
+#if 1
+        /* Send end gets TIFF from a file */
+        if (t4_tx_init(&send_state, in_file_name, -1, -1) == NULL)
+        {
+            printf("Failed to init T.4 send\n");
+            exit(2);
+        }
+        t4_tx_set_min_row_bits(&send_state, min_row_bits);
+        t4_tx_set_local_ident(&send_state, "111 2222 3333");
+
+        /* Receive end puts TIFF to a new file. */
+        if (t4_rx_init(&receive_state, OUT_FILE_NAME, T4_COMPRESSION_ITU_T4_2D) == NULL)
+        {
+            printf("Failed to init T.4 rx\n");
+            exit(2);
+        }
+        t4_rx_set_x_resolution(&receive_state, t4_tx_get_x_resolution(&send_state));
+        t4_rx_set_y_resolution(&receive_state, t4_tx_get_y_resolution(&send_state));
+        t4_rx_set_image_width(&receive_state, t4_tx_get_image_width(&send_state));
+
+        /* Now send and receive all the pages in the source TIFF file */
+        page_no = 1;
+        sends = 0;
+        /* Select whether we step round the compression schemes, or use a single specified one. */
+        compression_step = (compression < 0)  ?  0  :  -1;
+        for (;;)
+        {
+            end_marks = 0;
+            /* Add a header line to alternate pages, if required */
+            if (add_page_headers  &&  (sends & 2))
+                t4_tx_set_header_info(&send_state, "Header");
+            else
+                t4_tx_set_header_info(&send_state, NULL);
+            if (restart_pages  &&  (sends & 1))
+            {
+                /* Use restart, to send the page a second time */
+                if (t4_tx_restart_page(&send_state))
+                    break;
+            }
+            else
+            {
+                switch (compression_step)
+                {
+                case 0:
+                    compression = T4_COMPRESSION_ITU_T4_1D;
+                    compression_step++;
+                    break;
+                case 1:
+                    compression = T4_COMPRESSION_ITU_T4_2D;
+                    compression_step++;
+                    break;
+                case 2:
+                    compression = T4_COMPRESSION_ITU_T6;
+                    compression_step = 0;
+                    break;
+                }
+                t4_tx_set_tx_encoding(&send_state, compression);
+                t4_rx_set_rx_encoding(&receive_state, compression);
+
+                if (t4_tx_start_page(&send_state))
+                    break;
+            }
+            t4_rx_start_page(&receive_state);
+            if (block_size == 0)
+            {
+                do
+                {
+                    bit = t4_tx_get_bit(&send_state);
+                    if (bit == PUTBIT_END_OF_DATA)
+                    {
+                        /* T.6 data does not contain an image termination sequence.
+                           T.4 1D and 2D do, and should locate that sequence. */
+                        if (compression == T4_COMPRESSION_ITU_T6)
+                            break;
+                        if (++end_marks > 50)
+                        {
+                            printf("Receiver missed the end of page mark\n");
+                            tests_failed++;
+                            break;
+                        }
+                    }
+                    if (bit_error_rate)
+                    {
+                        if ((rand() % bit_error_rate) == 0)
+                            bit ^= 1;
+                    }
+                    end_of_page = t4_rx_put_bit(&receive_state, bit & 1);
+                }
+                while (!end_of_page);
+            }
+            else if (block_size == 1)
+            {
+                do
+                {
+                    bit = t4_tx_get_byte(&send_state);
+                    if ((bit & 0x100))
+                    {
+                        /* T.6 data does not contain an image termination sequence.
+                           T.4 1D and 2D do, and should locate that sequence. */
+                        if (compression == T4_COMPRESSION_ITU_T6)
+                            break;
+                        if (++end_marks > 50)
+                        {
+                            printf("Receiver missed the end of page mark\n");
+                            tests_failed++;
+                            break;
+                        }
+                    }
+                    end_of_page = t4_rx_put_byte(&receive_state, bit & 0xFF);
+                }
+                while (!end_of_page);
+            }
+            else
+            {
+                do
+                {
+                    bit = t4_tx_get_chunk(&send_state, block, block_size);
+                    if (bit > 0)
+                        end_of_page = t4_rx_put_chunk(&receive_state, block, bit);
+                    if (bit < block_size)
+                    {
+                        /* T.6 data does not contain an image termination sequence.
+                           T.4 1D and 2D do, and should locate that sequence. */
+                        if (compression == T4_COMPRESSION_ITU_T6)
+                            break;
+                        if (++end_marks > 50)
+                        {
+                            printf("Receiver missed the end of page mark\n");
+                            tests_failed++;
+                            break;
+                        }
+                    }
+                }
+                while (!end_of_page);
+            }
+            if (dump_as_xxx)
+                dump_image_as_xxx(&receive_state);
+            display_page_stats(&receive_state);
+            if (!restart_pages  ||  (sends & 1))
+                t4_tx_end_page(&send_state);
+            t4_rx_end_page(&receive_state);
+            sends++;
+        }
+        t4_tx_end(&send_state);
+        t4_rx_end(&receive_state);
+        /* And we should now have a matching received TIFF file. Note this will only match
+           at the image level. TIFF files allow a lot of ways to express the same thing,
+           so bit matching of the files is not the normal case. */
+        fflush(stdout);
+        sprintf(buf, "tiffcmp -t %s " OUT_FILE_NAME, in_file_name);
+        if (tests_failed)//  ||  system(buf))
+        {
+            printf("Tests failed\n");
+            exit(2);
+        }
+#endif
+#if 0
+        /* Send end gets TIFF from a function */
+        if (t4_tx_init(&send_state, in_file_name, -1, -1) == NULL)
+        {
+            printf("Failed to init T.4 tx\n");
+            exit(2);
+        }
+        t4_tx_set_row_read_handler(&send_state, row_read_handler, NULL);
+        t4_tx_set_min_row_bits(&send_state, min_row_bits);
+        t4_tx_set_local_ident(&send_state, "111 2222 3333");
+
+        /* Receive end puts TIFF to a function. */
+        if (t4_rx_init(&receive_state, OUT_FILE_NAME, T4_COMPRESSION_ITU_T4_2D) == NULL)
+        {
+            printf("Failed to init T.4 rx\n");
+            exit(2);
+        }
+        t4_rx_set_row_write_handler(&receive_state, row_write_handler, NULL);
+        t4_rx_set_x_resolution(&receive_state, t4_tx_get_x_resolution(&send_state));
+        t4_rx_set_y_resolution(&receive_state, t4_tx_get_y_resolution(&send_state));
+        t4_rx_set_image_width(&receive_state, t4_tx_get_image_width(&send_state));
+
+        /* Now send and receive all the pages in the source TIFF file */
+        page_no = 1;
+        /* Select whether we step round the compression schemes, or use a single specified one. */
+        compression_step = (compression < 0)  ?  0  :  -1;
+        for (;;)
+        {
+            end_marks = 0;
+            /* Add a header line to alternate pages, if required */
+            switch (compression_step)
+            {
+            case 0:
+                compression = T4_COMPRESSION_ITU_T4_1D;
+                compression_step++;
+                break;
+            case 1:
+                compression = T4_COMPRESSION_ITU_T4_2D;
+                compression_step++;
+                break;
+            case 2:
+                compression = T4_COMPRESSION_ITU_T6;
+                compression_step = 0;
+                break;
+            }
+            t4_tx_set_tx_encoding(&send_state, compression);
+            t4_rx_set_rx_encoding(&receive_state, compression);
+
+            if (t4_tx_start_page(&send_state))
+                break;
+            t4_rx_start_page(&receive_state);
+            do
+            {
+                bit = t4_tx_get_bit(&send_state);
+                if (bit == PUTBIT_END_OF_DATA)
+                {
+                    /* T.6 data does not contain an image termination sequence.
+                       T.4 1D and 2D do, and should locate that sequence. */
+                    if (compression == T4_COMPRESSION_ITU_T6)
+                        break;
+                    if (++end_marks > 50)
+                    {
+                        printf("Receiver missed the end of page mark\n");
+                        tests_failed++;
+                        break;
+                    }
+                }
+                end_of_page = t4_rx_put_bit(&receive_state, bit & 1);
+            }
+            while (!end_of_page);
+            t4_tx_end_page(&send_state);
+            t4_rx_end_page(&receive_state);
+            break;
+        }
+        t4_tx_end(&send_state);
+        t4_rx_end(&receive_state);
+#endif
+
+        printf("Tests passed\n");
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/tests/testadsi.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/tests/testadsi.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,686 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * testadsi.c
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2003 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: testadsi.c,v 1.22 2008/05/13 13:17:26 steveu Exp $
+ */
+
+#if defined(HAVE_CONFIG_H)
+#include <config.h>
+#endif
+
+#include <fcntl.h>
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
+#include <sys/ioctl.h>
+#include <stdlib.h>
+#include <inttypes.h>
+#include <sys/signal.h>
+#include <sys/select.h>
+#include <sys/wait.h>
+#include <sys/resource.h>
+#include <sys/time.h>
+
+#if defined(HAVE_LIBUNICALL)
+
+#include <linux/zaptel.h>
+#include <pthread.h>
+#include <audiofile.h>
+#include <tiffio.h>
+
+#include "unicall.h"
+//#include "../libmfcr2/libmfcr2.h"
+//#include "../libpri/libpri.h"
+
+#include "spandsp.h"
+
+#define FALSE 0
+#define TRUE (!FALSE)
+
+int caller_mode = FALSE;
+static AFfilehandle rxhandle;
+static AFfilehandle txhandle;
+
+#if 0
+int adsi_create_message(adsi_tx_state_t *s, uint8_t *msg)
+{
+    int len;
+
+    len = adsi_add_field(s, msg, -1, CLASS_SDMF_CALLERID, 0);
+    len = adsi_add_field(s, msg, len, 0, "10011750", 8);
+    len = adsi_add_field(s, msg, len, 0, "6095551212", 10);
+    return len;
+}
+#endif
+#if 0
+int adsi_create_message(adsi_tx_state_t *s, uint8_t *msg)
+{
+    int len;
+
+    len = adsi_add_field(s, msg, -1, CLIP_MDMF_CALLERID, 0);
+    len = adsi_add_field(s, msg, len, CLIP_CALLTYPE, "\x81", 1);
+    len = adsi_add_field(s, msg, len, CLIP_DATETIME, "10011750", 8);
+    len = adsi_add_field(s, msg, len, CLIP_CALLER_NUMBER, "12345678", 8);
+    len = adsi_add_field(s, msg, len, CLIP_DIALED_NUMBER, "87654321", 8);
+    len = adsi_add_field(s, msg, len, CLIP_CALLER_NAME, "Steve Underwood", 15);
+    return len;
+}
+#endif
+#if 0
+int adsi_create_message(adsi_tx_state_t *s, uint8_t *msg)
+{
+    int len;
+
+    len = adsi_add_field(s, msg, -1, CLIP_MDMF_CALLERID, 0);
+    len = adsi_add_field(s, msg, len, CLIP_NUM_MSG, "\x03", 1);
+    return len;
+}
+#endif
+#if 0
+int adsi_create_message(adsi_tx_state_t *s, uint8_t *msg)
+{
+    int len;
+
+    len = adsi_add_field(s, msg, -1, CLIP_MDMF_MSG_WAITING, NULL, 0);
+    len = adsi_add_field(s, msg, len, CLIP_VISUAL_INDICATOR, "\x00", 1);
+    return len;
+}
+#endif
+#if 0
+int adsi_create_message(adsi_tx_state_t *s, uint8_t *msg)
+{
+    int len;
+
+    len = adsi_add_field(s, msg, -1, CLIP_MDMF_MSG_WAITING, NULL, 0);
+    len = adsi_add_field(s, msg, len, CLIP_VISUAL_INDICATOR, "\xFF", 1);
+    len = adsi_add_field(s, msg, len, CLIP_NUM_MSG, "\x05", 1);
+    return len;
+}
+#endif
+#if 1
+int adsi_create_message(adsi_tx_state_t *s, uint8_t *msg)
+{
+    int len;
+
+    len = adsi_add_field(s, msg, -1, CLASS_SDMF_MSG_WAITING, NULL, 0);
+    len = adsi_add_field(s, msg, len, 0, "\x6F", 1);
+    len = adsi_add_field(s, msg, len, 0, "\x6F", 1);
+    len = adsi_add_field(s, msg, len, 0, "\x6F", 1);
+    return len;
+}
+#endif
+#if 0
+int adsi_create_message(adsi_tx_state_t *s, uint8_t *msg)
+{
+    int len;
+
+    len = adsi_add_field(s, msg, -1, CLASS_SDMF_MSG_WAITING, NULL, 0);
+    len = adsi_add_field(s, msg, len, 0, "\x42", 1);
+    len = adsi_add_field(s, msg, len, 0, "\x42", 1);
+    len = adsi_add_field(s, msg, len, 0, "\x42", 1);
+    return len;
+}
+#endif
+#if 0
+int adsi_create_message(adsi_tx_state_t *s, uint8_t *msg)
+{
+    int len;
+
+    len = adsi_add_field(s, msg, -1, CLIP_MDMF_SMS, NULL, 0);
+    len = adsi_add_field(s, msg, len, CLIP_DISPLAY_INFO, "\x00ABC", 4);
+    return len;
+}
+#endif
+
+struct
+{
+    pthread_t thread;
+    int chan;
+    int sig_fd;
+    int fd;
+    uc_call_t *call;
+    uc_crn_t crn;
+    int xxx;
+    int cause;
+
+    dtmf_rx_state_t dtmf_state;
+    char dtmf[101];
+    int dtmf_ptr;
+    
+    char *tag;
+    
+    char originating_number[32];
+    char destination_number[32];
+    
+    adsi_rx_state_t adsi_rx;
+    adsi_tx_state_t adsi_tx;
+} chan_stuff[30];
+
+tone_gen_descriptor_t tone_desc;
+tone_gen_state_t gen;
+
+void channel_read_adsi_channel(uc_t *uc, int chan, void *user_data, uint8_t *buf, int len);
+int channel_write_adsi_channel(uc_t *uc, int chan, void *user_data, uint8_t *buf, int max_len);
+int channel_error(uc_t *uc, int chan, void *user_data, int cause);
+int signaling_error(uc_t *uc, void *user_data, int cause);
+
+void channel_read_adsi_channel(uc_t *uc, int chan, void *user_data, uint8_t *buf, int len)
+{
+    int i;
+    int xlen;
+    int16_t pcm_buf[1024];
+    char *s;
+    int outframes;
+    
+    for (i = 0;  i < len;  i++)
+        pcm_buf[i] = alaw_to_linear(buf[i]);
+    /*endfor*/
+    outframes = afWriteFrames(rxhandle,
+                              AF_DEFAULT_TRACK,
+                              pcm_buf,
+                              len);
+    if (outframes != len)
+    {
+        printf("Failed to write %d samples\n", len);
+        exit(2);
+    }
+    
+    dtmf_rx(&chan_stuff[chan].dtmf_state, pcm_buf, len);
+    xlen = dtmf_rx_get(&chan_stuff[chan].dtmf_state,
+                       chan_stuff[chan].dtmf + chan_stuff[chan].dtmf_ptr,
+                       100 - chan_stuff[chan].dtmf_ptr);
+    if (xlen > 0)
+    {
+        s = chan_stuff[chan].dtmf + chan_stuff[chan].dtmf_ptr;
+        while (*s)
+        {
+            if (*s == '#')
+            {
+                uc_set_channel_read_callback(uc, 0, NULL, 0);
+                uc_set_channel_write_callback(uc, 0, NULL, 0);
+                if (uc_call_control(uc, UC_OP_DROPCALL, chan_stuff[chan].crn, (void *) UC_CAUSE_NORMAL_CLEARING))
+                    printf ("A Drop Call failed\n");
+                /*endif*/
+                break;
+            }
+            /*endif*/
+            s++;
+        }
+        /*endwhile*/
+        printf("Got '%s'\n", chan_stuff[chan].dtmf);
+        chan_stuff[chan].dtmf_ptr += xlen;
+    }
+    /*endif*/
+    adsi_rx(&(chan_stuff[chan].adsi_rx), pcm_buf, len);
+}
+/*- End of function --------------------------------------------------------*/
+
+int channel_write_adsi_channel(uc_t *uc, int chan, void *user_data, uint8_t *buf, int max_len)
+{
+    int16_t pcm_buf[1024];
+    int len;
+    int i;
+    static int block = 0;
+    uint8_t adsi_msg[256];
+    int adsi_msg_len;
+
+    if (++block == 100)
+    {
+        adsi_send_alert_tone(&(chan_stuff[chan].adsi_tx));
+        printf("Alert tone sent\n");
+        adsi_msg_len = adsi_create_message(&(chan_stuff[chan].adsi_tx), adsi_msg);
+        if ((len = adsi_put_message(&(chan_stuff[chan].adsi_tx), adsi_msg, adsi_msg_len)) > 0)
+            printf("Message put - %d bytes\n", len);
+    }
+    len = adsi_tx(&(chan_stuff[chan].adsi_tx), pcm_buf, max_len);
+    afWriteFrames(txhandle,
+                  AF_DEFAULT_TRACK,
+                  pcm_buf,
+                  len);
+    for (i = 0;  i < len;  i++)
+        buf[i] = linear_to_alaw(pcm_buf[i]);
+    /*endfor*/
+    return len;
+}
+/*- End of function --------------------------------------------------------*/
+
+int channel_error(uc_t *uc, int chan, void *user_data, int cause)
+{
+    printf("Error %d\n", cause);
+    return  0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int signaling_error(uc_t *uc, void *user_data, int cause)
+{
+    printf("Error %d\n", cause);
+    return  0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void initiate_call(uc_t *uc, int chan, uc_event_t *e)
+{
+    uc_makecall_t makecall;
+    uc_callparms_t *callparms;
+    int ret;
+
+    printf ("Initiating call\n");
+    if ((callparms = uc_new_callparms(NULL)) == NULL)
+        return;
+    /*endif*/
+    uc_callparm_originating_number(callparms, chan_stuff[chan].originating_number);
+    uc_callparm_destination_number(callparms, chan_stuff[chan].destination_number);
+    makecall.callparms = callparms;
+    makecall.crn = 0;
+    if (ret = uc_call_control(uc, UC_OP_MAKECALL, 0, (void *) &makecall) != UC_RET_OK)
+        fprintf(stderr, "Make Call failed - %d\n", ret);
+    /*endif*/
+    chan_stuff[chan].crn = makecall.crn;
+    free(callparms);
+}
+/*- End of function --------------------------------------------------------*/
+
+static void handle_uc_event(uc_t *uc, void *user_data, uc_event_t *e)
+{
+    int chan;
+    
+    chan = (int) user_data;
+    printf ("-- %s (%d)\n", uc_event2str(e->e), chan);
+    switch (e->e)
+    {
+    case UC_EVENT_DEVICEFAIL:
+        break;
+    case UC_EVENT_PROTOCOLFAIL:
+        printf("-- Protocol failure on channel %d, cause %d\n", e->gen.channel, e->gen.data);
+        break;
+    case UC_EVENT_SIGCHANSTATUS:
+        printf("-- Signalling channel status - %s\n", e->sigchanstatus.ok  ?  "Up"  :  "Down");
+        break;
+    case UC_EVENT_ALARM:
+        printf("-- Alarm - 0x%X 0x%X\n", e->alarm.raised, e->alarm.cleared);
+        break;
+    case UC_EVENT_FARBLOCKED:
+        printf("-- Channel far end blocked! :-(\n");
+        chan_stuff[chan].xxx &= ~1;
+        break;
+    case UC_EVENT_FARUNBLOCKED:
+        printf("-- Channel far end unblocked! :-)\n");
+        chan_stuff[chan].xxx |= 1;
+        if (chan_stuff[chan].xxx == 3)
+        {
+            if (caller_mode)
+                initiate_call(uc, chan, e);
+            /*endif*/
+        }
+        /*endif*/
+        break;
+    case UC_EVENT_LOCALBLOCKED:
+        printf("-- Channel local end blocked! :-(\n");
+        chan_stuff[chan].xxx &= ~2;
+        break;
+    case UC_EVENT_LOCALUNBLOCKED:
+        printf("-- Channel local end unblocked! :-)\n");
+        chan_stuff[chan].xxx |= 2;
+        if (chan_stuff[chan].xxx == 3)
+        {
+            if (caller_mode)
+                initiate_call(uc, chan, e);
+            /*endif*/
+        }
+        /*endif*/
+        break;
+    case UC_EVENT_DIALING:
+        printf("-- Dialing on channel %d\n", e->gen.channel);
+        break;
+    case UC_EVENT_ACCEPTED:
+        printf("-- Accepted on channel %d\n", e->gen.channel);
+        if (uc_call_control(uc, UC_OP_ANSWERCALL, e->gen.crn, (void *) -1))
+            fprintf(stderr, "Answer Call failed\n");
+        /*endif*/
+        break;
+    case UC_EVENT_DETECTED:
+        printf("-- Detected on channel %d\n", e->gen.channel);
+        break;
+    case UC_EVENT_ALERTING:
+        printf("-- Alerting on channel %d\n", e->gen.channel);
+        /* This is just a notification of call progress. We need take no action at this point. */
+        break;
+    case UC_EVENT_FARDISCONNECTED:
+        printf("-- Far end disconnected on channel %d\n", e->fardisconnected.channel);
+        /* Kill any outstanding audio processing */
+        uc_set_channel_read_callback(uc, 0, NULL, 0);
+        uc_set_channel_write_callback(uc, 0, NULL, 0);
+        if (uc_call_control(uc, UC_OP_DROPCALL, e->fardisconnected.crn, (void *) UC_CAUSE_NORMAL_CLEARING))
+            fprintf(stderr, "C Drop Call failed\n");
+        /*endif*/
+        break;
+    case UC_EVENT_DROPCALL:
+        printf("-- Drop call on channel %d\n", e->gen.channel);
+        if (uc_call_control(uc, UC_OP_RELEASECALL, e->gen.crn, NULL))
+            fprintf(stderr, "uc_ReleaseCall failed\n");
+        /*endif*/
+        break;
+    case UC_EVENT_RELEASECALL:
+        printf("-- Released on channel %d\n", e->gen.channel);
+        if (caller_mode)
+            initiate_call(uc, chan, e);
+        /*endif*/
+        break;
+    case UC_EVENT_OFFERED:
+        printf("-- Offered on channel %d, CRN %d (ANI: %s, DNIS: %s)\n", e->offered.channel, e->offered.crn, e->offered.parms.originating_number, e->offered.parms.destination_number);
+        if (!caller_mode)
+        {
+            switch (chan_stuff[chan].cause)
+            {
+            case 0:
+                if (uc_call_control(uc, UC_OP_ACCEPTCALL, e->offered.crn, (void *) -1))
+                    fprintf(stderr, "uc_AcceptCall failed\n");
+                /*endif*/
+                break;
+            case 1:
+                if (uc_call_control(uc, UC_OP_ANSWERCALL, e->offered.crn, (void *) -1))
+                    fprintf(stderr, "uc_AnswerCall failed\n");
+                /*endif*/
+                break;
+            case 2:
+                if (uc_call_control(uc, UC_OP_DROPCALL, e->offered.crn, (void *) UC_CAUSE_USER_BUSY))
+                    fprintf(stderr, "E Drop Call failed\n");
+                /*endif*/
+                break;
+            case 3:
+                if (uc_call_control(uc, UC_OP_DROPCALL, e->offered.crn, (void *) UC_CAUSE_UNASSIGNED_NUMBER))
+                    fprintf(stderr, "F Drop Call failed\n");
+                /*endif*/
+                break;
+            case 4:
+                if (uc_call_control(uc, UC_OP_DROPCALL, e->offered.crn, (void *) UC_CAUSE_NETWORK_CONGESTION))
+                    fprintf(stderr, "G Drop Call failed\n");
+                /*endif*/
+                break;
+            case 5:
+                if (uc_call_control(uc, UC_OP_DROPCALL, e->offered.crn, (void *) UC_CAUSE_DEST_OUT_OF_ORDER))
+                    fprintf(stderr, "H Drop Call failed\n");
+                /*endif*/
+                break;
+            }
+            /*endswitch*/
+            if (++chan_stuff[chan].cause > 5)
+                chan_stuff[chan].cause = 0;
+            /*endif*/
+        }
+        /*endif*/
+        break;
+    case UC_EVENT_ANSWERED:
+        printf("-- Answered on channel %d\n", e->gen.channel);
+        uc_set_channel_read_callback(uc, 0, channel_read_adsi_channel, (void *) chan);
+printf("XXX read callback set\n");
+        uc_set_channel_write_callback(uc, 0, channel_write_adsi_channel, (void *) chan);
+printf("XXX write callback set\n");
+        adsi_tx_init(&(chan_stuff[chan].adsi_tx), ADSI_STANDARD_CLASS);
+printf("XXX ADSI inited\n");
+        dtmf_rx_init(&chan_stuff[chan].dtmf_state, NULL, NULL);
+printf("XXX DTMF inited\n");
+        break;
+    case UC_EVENT_CONNECTED:
+        printf("-- Connected on channel %d\n", e->gen.channel);
+        if (uc_call_control(uc, UC_OP_DROPCALL, e->offered.crn, (void *) UC_CAUSE_NORMAL_CLEARING))
+            printf ("I Drop Call failed\n");
+        /*endif*/
+        break;
+    default:
+        fprintf(stderr, "--!! Unknown signaling event %d\n", e->e);
+        break;
+    }
+    /*endswitch*/
+}
+/*- End of function --------------------------------------------------------*/
+
+static void *run_uc(void *arg)
+{
+    uc_t *uc;
+    uc_event_t *e;
+    struct timeval tv = {0,0};
+    struct timeval *next;
+    fd_set rfds;
+    fd_set wfds;
+    fd_set efds;
+    int res;
+    int dfd;
+    int chan;
+    
+    chan = *((int *) arg);
+
+    dfd = chan_stuff[chan].fd;
+    uc = uc_new(dfd, dfd, "mfcr2", "cn", UC_MODE_CPE, 1);
+    //uc = uc_new(dfd, dfd, "pri", "ctr4", UC_MODE_CPE, 1);
+    if (uc == NULL)
+    {
+        fprintf(stderr, "Unable to create instance\n");
+        return NULL;
+    }
+    /*endif*/
+    uc_set_signaling_callback(uc, handle_uc_event, (void *) chan);
+    uc_set_signaling_error_callback(uc, signaling_error, (void *) chan);
+    uc_set_channel_error_callback(uc, 0, channel_error, (void *) chan);
+    uc_set_logging(uc, 0x7FFFFFFF, 0, chan_stuff[chan].tag);
+    uc_call_control(uc, UC_OP_UNBLOCK, 0, (void *) -1);
+    for (;;)
+    {
+        FD_ZERO(&rfds);
+        FD_ZERO(&wfds);
+        FD_ZERO(&efds);
+        FD_SET(dfd, &rfds);
+        FD_SET(dfd, &wfds);
+        FD_SET(dfd, &efds);
+
+        if ((next = uc_schedule_next(uc)))
+        {
+            gettimeofday(&tv, NULL);
+            tv.tv_sec = next->tv_sec - tv.tv_sec;
+            tv.tv_usec = next->tv_usec - tv.tv_usec;
+            if (tv.tv_usec < 0)
+            {
+                tv.tv_usec += 1000000;
+                tv.tv_sec -= 1;
+            }
+            /*endif*/
+            if (tv.tv_sec < 0)
+            {
+                tv.tv_sec = 0;
+                tv.tv_usec = 0;
+            }
+            /*endif*/
+        }
+        /*endif*/
+        res = select(dfd + 1, &rfds, NULL, &efds, next  ?  &tv  :  NULL);
+        e = NULL;
+        if (res == 0)
+        {
+            uc_schedule_run(uc);
+        }
+        else if (res > 0)
+        {
+            e = uc_check_event(uc);
+        }
+        else if (errno != EINTR)
+        {
+            fprintf(stderr, "Error (%d) on select: %s\n", errno, strerror(errno));
+        }
+        /*endif*/
+
+        if (e)
+        {
+            printf("Non-callback signaling event\n");
+            handle_uc_event(uc, (void *) chan, e);
+        }
+        /*endif*/
+    }
+    /*endfor*/
+    return NULL;
+}
+/*- End of function --------------------------------------------------------*/
+
+int main(int argc, char *argv[]) 
+{
+    pthread_attr_t attr;
+    struct zt_bufferinfo b;
+    struct zt_gains g;
+    int chan;
+    int chanx;
+    char dev_name[20];
+    AFfilesetup filesetup;
+    int j;
+
+    filesetup = afNewFileSetup();
+    if (filesetup == AF_NULL_FILESETUP)
+    {
+        fprintf(stderr, "    Failed to create file setup\n");
+        exit(2);
+    }
+    afInitSampleFormat(filesetup, AF_DEFAULT_TRACK, AF_SAMPFMT_TWOSCOMP, 16);
+    afInitRate(filesetup, AF_DEFAULT_TRACK, (float) SAMPLE_RATE);
+    afInitFileFormat(filesetup, AF_FILE_WAVE);
+    afInitChannels(filesetup, AF_DEFAULT_TRACK, 1);
+    rxhandle = afOpenFile("rxadsi.wav", "w", filesetup);
+    if (rxhandle == AF_NULL_FILEHANDLE)
+    {
+        fprintf(stderr, "    Failed to open adsi audio file\n");
+        exit(2);
+    }
+    txhandle = afOpenFile("txadsi.wav", "w", filesetup);
+    if (txhandle == AF_NULL_FILEHANDLE)
+    {
+        fprintf(stderr, "    Failed to open adsi audio file\n");
+        exit(2);
+    }
+
+    uc_start();    
+    pthread_attr_init(&attr);
+    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
+    if (argc < 1)
+    {
+        fprintf(stderr, "Usage: testcall [call]\n");
+        exit(1);
+    }
+    /*endif*/
+    for (chan = 0;  chan < 1/*30*/;  chan++)
+    {
+        chan_stuff[chan].sig_fd = open("/dev/zap/channel", O_RDWR | O_NONBLOCK);
+        if (chan_stuff[chan].sig_fd < 0)
+        {
+            fprintf(stderr, "Failed to open channel: %s\n", strerror(errno));
+            exit(1);
+        }
+        /*endif*/
+        chan_stuff[chan].fd = chan_stuff[chan].sig_fd;
+        
+        /* Allow for the missing channel at TS16 */
+        if (chan < 15)
+            chanx = chan + 1;
+        else
+            chanx = chan + 2;
+        /*endif*/
+        if (ioctl(chan_stuff[chan].fd, ZT_SPECIFY, &chanx))
+        {
+            fprintf(stderr, "Failed to specify channel %d: %s\n", chanx, strerror(errno));
+            exit(1);
+        }
+        /*endif*/
+        if (ioctl(chan_stuff[chan].fd, ZT_GET_BUFINFO, &b))
+        {
+            fprintf(stderr, "Unable to get buffer info on channel %d: %s\n", chanx, strerror(errno));
+            exit(1);
+        }
+        /*endif*/
+        printf ("%d %d %d %d %d %d\n",
+                b.rxbufpolicy,
+                b.txbufpolicy,
+                b.numbufs,
+                b.bufsize,
+                b.readbufs,
+                b.writebufs);
+        b.rxbufpolicy = ZT_POLICY_IMMEDIATE;
+        b.txbufpolicy = ZT_POLICY_IMMEDIATE;
+        b.numbufs = 4;
+        b.bufsize = 160;
+        if (ioctl(chan_stuff[chan].fd, ZT_SET_BUFINFO, &b))
+        {
+            fprintf(stderr, "Unable to set buffer info on channel %d: %s\n", chanx, strerror(errno));
+            exit(1);
+        }
+        /*endif*/
+        if (ioctl(chan_stuff[chan].fd, ZT_GET_BUFINFO, &b))
+        {
+            fprintf(stderr, "Unable to get buffer info on channel %d: %s\n", chanx, strerror(errno));
+            exit(1);
+        }
+        /*endif*/
+        for (j = 0;  j < 256;  j++)
+        {
+            g.rxgain[j] = j;
+            g.txgain[j] = j;
+        }
+        ioctl(chan_stuff[chan].fd, ZT_SETGAINS, &g);
+        printf("%d %d %d %d %d %d\n",
+               b.rxbufpolicy,
+               b.txbufpolicy,
+               b.numbufs,
+               b.bufsize,
+               b.readbufs,
+               b.writebufs);
+
+        if (argc > 1)
+            caller_mode = TRUE;
+        /*endif*/
+        chan_stuff[chan].chan = chan;
+        sprintf(dev_name, "Chan %2d:", chanx);
+        chan_stuff[chan].tag = strdup(dev_name);
+        sprintf(chan_stuff[chan].originating_number, "%d", 987654321 + chan);
+        sprintf(chan_stuff[chan].destination_number, "%d", 1234 + chan);
+        
+        printf("Thread for channel %d\n", chan);
+        if (pthread_create(&chan_stuff[chan].thread, &attr, run_uc, &chan_stuff[chan].chan))
+            exit(2);
+        /*endif*/
+    }
+    /*endfor*/
+    for (;;)
+    {
+        sleep(5);
+        printf("Main thread\n");
+    }
+    /*endfor*/
+    exit(0);
+
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+#else
+int main(int argc, char *argv[]) 
+{
+    printf("This program was not built with Unicall available\n"); 
+}
+/*- End of function --------------------------------------------------------*/
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/tests/testfax.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/tests/testfax.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,652 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * testfax.c
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2003 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: testfax.c,v 1.32 2008/05/13 13:17:26 steveu Exp $
+ */
+
+#if defined(HAVE_CONFIG_H)
+#include <config.h>
+#endif
+
+#include <fcntl.h>
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
+#include <sys/ioctl.h>
+#include <stdlib.h>
+#include <inttypes.h>
+#include <sys/signal.h>
+#include <sys/select.h>
+#include <sys/wait.h>
+#include <sys/resource.h>
+#include <sys/time.h>
+
+#if defined(HAVE_LIBUNICALL)
+
+#include <linux/zaptel.h>
+#include <pthread.h>
+#include <audiofile.h>
+#include <tiffio.h>
+
+#include "unicall.h"
+//#include "../libmfcr2/libmfcr2.h"
+//#include "../libpri/libpri.h"
+//#include "../libpri/libfx.h"
+
+#include "spandsp.h"
+
+int caller_mode = FALSE;
+static AFfilehandle rxhandle;
+static AFfilehandle txhandle;
+
+typedef struct
+{
+    pthread_t thread;
+    int chan;
+    int sig_fd;
+    int fd;
+    uc_call_t *call;
+    uc_crn_t crn;
+    int xxx;
+    int cause;
+    uc_t *uc;
+
+    dtmf_rx_state_t dtmf_state;
+    char dtmf[101];
+    int dtmf_ptr;
+    
+    char *tag;
+    
+    char originating_number[32];
+    char destination_number[32];
+
+    t30_state_t fax;
+} chan_stuff_t;
+
+chan_stuff_t chan_stuff[30];
+
+tone_gen_descriptor_t tone_desc;
+tone_gen_state_t gen;
+
+pthread_mutex_t mutex;
+
+void channel_read_fax_channel(uc_t *uc, int chan, void *user_data, uint8_t *buf, int len);
+int channel_write_fax_channel(uc_t *uc, int chan, void *user_data, uint8_t *buf, int max_len);
+int channel_error(uc_t *uc, int chan, void *user_data, int cause);
+int signaling_error(uc_t *uc, void *user_data, int cause);
+
+void channel_read_fax_channel(uc_t *uc, int chan, void *user_data, uint8_t *buf, int len)
+{
+    int i;
+    int xlen;
+    char *s;
+    int outframes;
+
+#if 0
+    outframes = afWriteFrames(rxhandle,
+                              AF_DEFAULT_TRACK,
+                              buf,
+                              len >> 1);
+    if (outframes != len)
+    {
+        printf("Failed to write %d samples\n", len);
+        exit(2);
+    }
+#endif
+    dtmf_rx(&chan_stuff[chan].dtmf_state, (int16_t *) buf, len);
+    xlen = dtmf_rx_get(&chan_stuff[chan].dtmf_state,
+                       chan_stuff[chan].dtmf + chan_stuff[chan].dtmf_ptr,
+                       100 - chan_stuff[chan].dtmf_ptr);
+    if (xlen > 0)
+    {
+        s = chan_stuff[chan].dtmf + chan_stuff[chan].dtmf_ptr;
+        while (*s)
+        {
+            if (*s == '#')
+            {
+                uc_set_channel_read_callback(uc, 0, NULL, 0);
+                uc_set_channel_write_callback(uc, 0, NULL, 0);
+                if (uc_call_control(uc, UC_OP_DROPCALL, chan_stuff[chan].crn, (void *) UC_CAUSE_NORMAL_CLEARING))
+                    printf ("A Drop Call failed\n");
+                /*endif*/
+                break;
+            }
+            /*endif*/
+            s++;
+        }
+        /*endwhile*/
+        printf("Got '%s'\n", chan_stuff[chan].dtmf);
+        chan_stuff[chan].dtmf_ptr += xlen;
+    }
+    /*endif*/
+    t30_rx(&(chan_stuff[chan].fax), (int16_t *) buf, len);
+}
+/*- End of function --------------------------------------------------------*/
+
+int channel_write_fax_channel(uc_t *uc, int chan, void *user_data, uint8_t *buf, int max_len)
+{
+    int len;
+
+    len = t30_tx(&(chan_stuff[chan].fax), (int16_t *) buf, max_len >> 1);
+    afWriteFrames(txhandle, AF_DEFAULT_TRACK, buf, len);
+    if (len > 0)
+        len <<= 1;
+    return len;
+}
+/*- End of function --------------------------------------------------------*/
+
+int channel_error(uc_t *uc, int chan, void *user_data, int cause)
+{
+    printf("Error %d\n", cause);
+    return  0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int signaling_error(uc_t *uc, void *user_data, int cause)
+{
+    printf("Error %d\n", cause);
+    return  0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void initiate_call(uc_t *uc, int chan, uc_event_t *e)
+{
+    uc_makecall_t makecall;
+    uc_callparms_t *callparms;
+    int ret;
+
+    printf ("Initiating call\n");
+    pthread_mutex_lock(&mutex);
+    if ((callparms = uc_new_callparms(NULL)) == NULL)
+        return;
+    /*endif*/
+    pthread_mutex_unlock(&mutex);
+    uc_callparm_originating_number(callparms, chan_stuff[chan].originating_number);
+    uc_callparm_destination_number(callparms, chan_stuff[chan].destination_number);
+    makecall.callparms = callparms;
+    makecall.crn = 0;
+    if (ret = uc_call_control(uc, UC_OP_MAKECALL, 0, (void *) &makecall) != UC_RET_OK)
+        fprintf(stderr, "Make Call failed - %d\n", ret);
+    /*endif*/
+    chan_stuff[chan].crn = makecall.crn;
+    free(callparms);
+}
+/*- End of function --------------------------------------------------------*/
+
+static void phase_b_handler(t30_state_t *s, void *user_data, int msg)
+{
+    chan_stuff_t *t;
+
+    t = (chan_stuff_t *) user_data;
+    printf("Phase B - %d\n", msg);
+}
+/*- End of function --------------------------------------------------------*/
+
+static void phase_d_handler(t30_state_t *s, void *user_data, int msg)
+{
+    chan_stuff_t *t;
+
+    t = (chan_stuff_t *) user_data;
+    printf("Phase D - %d\n", msg);
+}
+/*- End of function --------------------------------------------------------*/
+
+static void phase_e_handler(t30_state_t *s, void *user_data, int result)
+{
+    chan_stuff_t *t;
+    
+    printf("Phase E - %d\n", result);
+    t = (chan_stuff_t *) user_data;
+    if (uc_call_control(t->uc, UC_OP_DROPCALL, t->crn, (void *) UC_CAUSE_NORMAL_CLEARING))
+        fprintf(stderr, "Phase E Drop Call failed\n");
+    /*endif*/
+}
+/*- End of function --------------------------------------------------------*/
+
+static void flush_handler(t30_state_t *s, void *user_data, int which)
+{
+    chan_stuff_t *t;
+    
+    printf("Flush\n");
+    t = (chan_stuff_t *) user_data;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void handle_uc_event(uc_t *uc, void *user_data, uc_event_t *e)
+{
+    int chan;
+    
+    chan = (int) user_data;
+
+    printf ("-- %s (%d)\n", uc_event2str(e->e), chan);
+    switch (e->e)
+    {
+    case UC_EVENT_DEVICEFAIL:
+        break;
+    case UC_EVENT_PROTOCOLFAIL:
+        printf("-- Protocol failure on channel %d, cause %d\n", e->gen.channel, e->gen.data);
+        break;
+    case UC_EVENT_SIGCHANSTATUS:
+        printf("-- Signalling channel status - %s\n", e->sigchanstatus.ok  ?  "Up"  :  "Down");
+        break;
+    case UC_EVENT_ALARM:
+        printf("-- Alarm - 0x%X 0x%X\n", e->alarm.raised, e->alarm.cleared);
+        break;
+    case UC_EVENT_FARBLOCKED:
+        printf("-- Channel far end blocked! :-(\n");
+        chan_stuff[chan].xxx &= ~1;
+        break;
+    case UC_EVENT_FARUNBLOCKED:
+        printf("-- Channel far end unblocked! :-)\n");
+        chan_stuff[chan].xxx |= 1;
+        if (chan_stuff[chan].xxx == 3)
+        {
+            if (caller_mode)
+                initiate_call(uc, chan, e);
+            /*endif*/
+        }
+        /*endif*/
+        break;
+    case UC_EVENT_LOCALBLOCKED:
+        printf("-- Channel local end blocked! :-(\n");
+        chan_stuff[chan].xxx &= ~2;
+        break;
+    case UC_EVENT_LOCALUNBLOCKED:
+        printf("-- Channel local end unblocked! :-)\n");
+        chan_stuff[chan].xxx |= 2;
+        if (chan_stuff[chan].xxx == 3)
+        {
+            if (caller_mode)
+                initiate_call(uc, chan, e);
+            /*endif*/
+        }
+        /*endif*/
+        break;
+    case UC_EVENT_DIALING:
+        printf("-- Dialing on channel %d\n", e->gen.channel);
+        break;
+    case UC_EVENT_PROCEEDING:
+        printf("-- Proceeding on channel %d\n", e->gen.channel);
+        break;
+    case UC_EVENT_ACCEPTED:
+        printf("-- Accepted on channel %d\n", e->gen.channel);
+        if (uc_call_control(uc, UC_OP_ANSWERCALL, e->gen.crn, (void *) -1))
+            fprintf(stderr, "Answer Call failed\n");
+        /*endif*/
+        break;
+    case UC_EVENT_DETECTED:
+        printf("-- Detected on channel %d\n", e->gen.channel);
+        break;
+    case UC_EVENT_MOREDIGITS:
+        printf("-- More digits on channel %d, CRN %d (ANI: %s, DNIS: %s)\n", e->offered.channel, e->offered.crn, e->offered.parms.originating_number, e->offered.parms.destination_number);
+        break;
+    case UC_EVENT_ALERTING:
+        printf("-- Alerting on channel %d\n", e->gen.channel);
+        /* This is just a notification of call progress. We need take no action at this point. */
+        break;
+    case UC_EVENT_FARDISCONNECTED:
+        printf("-- Far end disconnected on channel %d\n", e->fardisconnected.channel);
+        /* Kill any outstanding audio processing */
+        uc_set_channel_read_callback(uc, 0, NULL, 0);
+        uc_set_channel_write_callback(uc, 0, NULL, 0);
+        if (uc_call_control(uc, UC_OP_DROPCALL, e->fardisconnected.crn, (void *) UC_CAUSE_NORMAL_CLEARING))
+            fprintf(stderr, "C Drop Call failed\n");
+        /*endif*/
+        break;
+    case UC_EVENT_DROPCALL:
+        printf("-- Drop call on channel %d\n", e->gen.channel);
+        if (uc_call_control(uc, UC_OP_RELEASECALL, e->gen.crn, NULL))
+            fprintf(stderr, "uc_ReleaseCall failed\n");
+        /*endif*/
+        break;
+    case UC_EVENT_RELEASECALL:
+        printf("-- Released on channel %d\n", e->gen.channel);
+        if (caller_mode)
+            initiate_call(uc, chan, e);
+        /*endif*/
+        break;
+    case UC_EVENT_OFFERED:
+        printf("-- Offered on channel %d, CRN %d (ANI: %s, DNIS: %s)\n", e->offered.channel, e->offered.crn, e->offered.parms.originating_number, e->offered.parms.destination_number);
+        if (!caller_mode)
+        {
+            switch (chan_stuff[chan].cause)
+            {
+            case 0:
+                if (uc_call_control(uc, UC_OP_ACCEPTCALL, e->offered.crn, (void *) -1))
+                    fprintf(stderr, "uc_AcceptCall failed\n");
+                /*endif*/
+                chan_stuff[chan].crn = e->offered.crn;
+                break;
+            case 1:
+                if (uc_call_control(uc, UC_OP_ANSWERCALL, e->offered.crn, (void *) -1))
+                    fprintf(stderr, "uc_AnswerCall failed\n");
+                /*endif*/
+                chan_stuff[chan].crn = e->offered.crn;
+                break;
+            case 2:
+                if (uc_call_control(uc, UC_OP_DROPCALL, e->offered.crn, (void *) UC_CAUSE_USER_BUSY))
+                    fprintf(stderr, "E Drop Call failed\n");
+                /*endif*/
+                break;
+            case 3:
+                if (uc_call_control(uc, UC_OP_DROPCALL, e->offered.crn, (void *) UC_CAUSE_UNASSIGNED_NUMBER))
+                    fprintf(stderr, "F Drop Call failed\n");
+                /*endif*/
+                break;
+            case 4:
+                if (uc_call_control(uc, UC_OP_DROPCALL, e->offered.crn, (void *) UC_CAUSE_NETWORK_CONGESTION))
+                    fprintf(stderr, "G Drop Call failed\n");
+                /*endif*/
+                break;
+            case 5:
+                if (uc_call_control(uc, UC_OP_DROPCALL, e->offered.crn, (void *) UC_CAUSE_DEST_OUT_OF_ORDER))
+                    fprintf(stderr, "H Drop Call failed\n");
+                /*endif*/
+                break;
+            }
+            /*endswitch*/
+            if (++chan_stuff[chan].cause > 5)
+                chan_stuff[chan].cause = 0;
+            /*endif*/
+        }
+        /*endif*/
+        break;
+    case UC_EVENT_ANSWERED:
+        printf("-- Answered on channel %d\n", e->gen.channel);
+        uc_set_channel_read_callback(uc, 0, channel_read_fax_channel, (void *) chan);
+printf("XXX read callback set\n");
+        uc_set_channel_write_callback(uc, 0, channel_write_fax_channel, (void *) chan);
+printf("XXX write callback set\n");
+        t30_init(&(chan_stuff[chan].fax), FALSE, uc);
+        t30_set_local_ident(&(chan_stuff[chan].fax), "12345678");
+        t30_set_tx_file(&(chan_stuff[chan].fax), "tx.tif");
+        //t30_set_rx_file(&(chan_stuff[chan].fax), "rx.tif");
+        t30_set_phase_b_handler(&(chan_stuff[chan].fax), phase_b_handler, &(chan_stuff[chan]));
+        t30_set_phase_d_handler(&(chan_stuff[chan].fax), phase_d_handler, &(chan_stuff[chan]));
+        t30_set_phase_e_handler(&(chan_stuff[chan].fax), phase_e_handler, &(chan_stuff[chan]));
+        t30_set_flush_handler(&(chan_stuff[chan].fax), flush_handler, &(chan_stuff[chan]));
+printf("XXX FAX inited\n");
+        dtmf_rx_init(&chan_stuff[chan].dtmf_state, NULL, NULL);
+printf("XXX DTMF inited\n");
+        break;
+    case UC_EVENT_CONNECTED:
+        printf("-- Connected on channel %d\n", e->gen.channel);
+        uc_set_channel_read_callback(uc, 0, channel_read_fax_channel, (void *) chan);
+printf("XXX read callback set\n");
+        uc_set_channel_write_callback(uc, 0, channel_write_fax_channel, (void *) chan);
+printf("XXX write callback set\n");
+        t30_init(&(chan_stuff[chan].fax), TRUE, uc);
+        t30_set_local_ident(&(chan_stuff[chan].fax), "87654321");
+        t30_set_tx_file(&(chan_stuff[chan].fax), "tx.tif");
+        //t30_set_rx_file(&(chan_stuff[chan].fax), "rx.tif");
+        t30_set_phase_b_handler(&(chan_stuff[chan].fax), phase_b_handler, &(chan_stuff[chan]));
+        t30_set_phase_d_handler(&(chan_stuff[chan].fax), phase_d_handler, &(chan_stuff[chan]));
+        t30_set_phase_e_handler(&(chan_stuff[chan].fax), phase_e_handler, &(chan_stuff[chan]));
+printf("XXX FAX inited\n");
+#if 0
+        if (uc_call_control(uc, UC_OP_DROPCALL, e->offered.crn, (void *) UC_CAUSE_NORMAL_CLEARING))
+            printf ("I Drop Call failed\n");
+        /*endif*/
+#endif
+        break;
+    default:
+        fprintf(stderr, "--!! Unknown signaling event %d\n", e->e);
+        break;
+    }
+    /*endswitch*/
+}
+/*- End of function --------------------------------------------------------*/
+
+static void *run_uc(void *arg)
+{
+    uc_t *uc;
+    uc_event_t *e;
+    struct timeval tv = {0,0};
+    struct timeval *next;
+    fd_set rfds;
+    fd_set wfds;
+    fd_set efds;
+    int res;
+    int dfd;
+    int chan;
+    
+    chan = *((int *) arg);
+
+    dfd = chan_stuff[chan].fd;
+    if (chan < 4)
+        uc = uc_new(dfd, dfd, "fx", "ls,us", UC_MODE_CO, 1);
+    else
+        uc = uc_new(dfd, dfd, "fx", "ls", UC_MODE_CPE, 1);
+    //uc = uc_new(dfd, dfd, "mfcr2", "cn", UC_MODE_CPE, 1);
+    //uc = uc_new(dfd, dfd, "pri", "ctr4", UC_MODE_CPE, 1);
+    if (uc == NULL)
+    {
+        fprintf(stderr, "Unable to create instance\n");
+        return NULL;
+    }
+    /*endif*/
+    uc_set_api_codec(uc, 0, UC_CODEC_LINEAR16);
+
+    chan_stuff[chan].uc = uc;
+    uc_set_signaling_callback(uc, handle_uc_event, (void *) chan);
+    uc_set_signaling_error_callback(uc, signaling_error, (void *) chan);
+    uc_set_channel_error_callback(uc, 0, channel_error, (void *) chan);
+    uc_set_logging(uc, 0x7FFFFFFF, 0, chan_stuff[chan].tag);
+    uc_call_control(uc, UC_OP_UNBLOCK, 0, (void *) -1);
+    for (;;)
+    {
+        FD_ZERO(&rfds);
+        FD_ZERO(&wfds);
+        FD_ZERO(&efds);
+        FD_SET(dfd, &rfds);
+        FD_SET(dfd, &wfds);
+        FD_SET(dfd, &efds);
+
+        if ((next = uc_schedule_next(uc)))
+        {
+            gettimeofday(&tv, NULL);
+            tv.tv_sec = next->tv_sec - tv.tv_sec;
+            tv.tv_usec = next->tv_usec - tv.tv_usec;
+            if (tv.tv_usec < 0)
+            {
+                tv.tv_usec += 1000000;
+                tv.tv_sec -= 1;
+            }
+            /*endif*/
+            if (tv.tv_sec < 0)
+            {
+                tv.tv_sec = 0;
+                tv.tv_usec = 0;
+            }
+            /*endif*/
+        }
+        /*endif*/
+        res = select(dfd + 1, &rfds, NULL, &efds, next  ?  &tv  :  NULL);
+        e = NULL;
+        if (res == 0)
+        {
+            uc_schedule_run(uc);
+        }
+        else if (res > 0)
+        {
+            e = uc_check_event(uc);
+        }
+        else if (errno != EINTR)
+        {
+            fprintf(stderr, "Error (%d) on select: %s\n", errno, strerror(errno));
+        }
+        /*endif*/
+
+        if (e)
+        {
+            printf("Non-callback signaling event\n");
+            handle_uc_event(uc, (void *) chan, e);
+        }
+        /*endif*/
+    }
+    /*endfor*/
+    return NULL;
+}
+/*- End of function --------------------------------------------------------*/
+
+int main(int argc, char *argv[]) 
+{
+    pthread_attr_t attr;
+    struct zt_bufferinfo b;
+    struct zt_gains g;
+    int chan;
+    int chanx;
+    char dev_name[20];
+    AFfilesetup filesetup;
+    int j;
+
+    filesetup = afNewFileSetup();
+    if (filesetup == AF_NULL_FILESETUP)
+    {
+        fprintf(stderr, "    Failed to create file setup\n");
+        exit(2);
+    }
+    afInitSampleFormat(filesetup, AF_DEFAULT_TRACK, AF_SAMPFMT_TWOSCOMP, 16);
+    afInitRate(filesetup, AF_DEFAULT_TRACK, (float) SAMPLE_RATE);
+    afInitFileFormat(filesetup, AF_FILE_WAVE);
+    afInitChannels(filesetup, AF_DEFAULT_TRACK, 1);
+    rxhandle = afOpenFile("rxfax.wav", "w", filesetup);
+    if (rxhandle == AF_NULL_FILEHANDLE)
+    {
+        fprintf(stderr, "    Failed to open fax audio file\n");
+        exit(2);
+    }
+    txhandle = afOpenFile("txfax.wav", "w", filesetup);
+    if (txhandle == AF_NULL_FILEHANDLE)
+    {
+        fprintf(stderr, "    Failed to open fax audio file\n");
+        exit(2);
+    }
+
+    if (argc < 1)
+    {
+        fprintf(stderr, "Usage: testcall [call]\n");
+        exit(1);
+    }
+    /*endif*/
+    uc_start();    
+    pthread_attr_init(&attr);
+    pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
+    pthread_mutex_init(&mutex, NULL);
+    for (chan = 0;  chan < 5/*30*/;  chan++)
+    {
+        chan_stuff[chan].sig_fd = open("/dev/zap/channel", O_RDWR | O_NONBLOCK);
+        if (chan_stuff[chan].sig_fd < 0)
+        {
+            fprintf(stderr, "Failed to open channel: %s\n", strerror(errno));
+            exit(1);
+        }
+        /*endif*/
+        chan_stuff[chan].fd = chan_stuff[chan].sig_fd;
+        
+        /* Allow for the missing channel at TS16 */
+        chanx = chan + 1 + (chan + 15)%30;
+        chanx = chan + 125;
+        if (ioctl(chan_stuff[chan].fd, ZT_SPECIFY, &chanx))
+        {
+            fprintf(stderr, "Failed to specify channel %d: %s\n", chanx, strerror(errno));
+            exit(1);
+        }
+        /*endif*/
+        if (ioctl(chan_stuff[chan].fd, ZT_GET_BUFINFO, &b))
+        {
+            fprintf(stderr, "Unable to get buffer info on channel %d: %s\n", chanx, strerror(errno));
+            exit(1);
+        }
+        /*endif*/
+        printf ("%d %d %d %d %d %d\n",
+                b.rxbufpolicy,
+                b.txbufpolicy,
+                b.numbufs,
+                b.bufsize,
+                b.readbufs,
+                b.writebufs);
+        b.rxbufpolicy = ZT_POLICY_IMMEDIATE;
+        b.txbufpolicy = ZT_POLICY_IMMEDIATE;
+        b.numbufs = 4;
+        b.bufsize = 160;
+        if (ioctl(chan_stuff[chan].fd, ZT_SET_BUFINFO, &b))
+        {
+            fprintf(stderr, "Unable to set buffer info on channel %d: %s\n", chanx, strerror(errno));
+            exit(1);
+        }
+        /*endif*/
+        if (ioctl(chan_stuff[chan].fd, ZT_GET_BUFINFO, &b))
+        {
+            fprintf(stderr, "Unable to get buffer info on channel %d: %s\n", chanx, strerror(errno));
+            exit(1);
+        }
+        /*endif*/
+        for (j = 0;  j < 256;  j++)
+        {
+            g.rxgain[j] = j;
+            g.txgain[j] = j;
+        }
+        /*endif*/
+        ioctl(chan_stuff[chan].fd, ZT_SETGAINS, &g);
+        printf("%d %d %d %d %d %d\n",
+               b.rxbufpolicy,
+               b.txbufpolicy,
+               b.numbufs,
+               b.bufsize,
+               b.readbufs,
+               b.writebufs);
+
+        if (argc > 1)
+            caller_mode = TRUE;
+        /*endif*/
+        chan_stuff[chan].chan = chan;
+        sprintf(dev_name, "Chan %2d:", chanx);
+        chan_stuff[chan].tag = strdup(dev_name);
+        sprintf(chan_stuff[chan].originating_number, "%d", 987654321 + chan);
+        sprintf(chan_stuff[chan].destination_number, "%d", 1234 + chan);
+        printf("Thread for channel %d\n", chan);
+        if (pthread_create(&chan_stuff[chan].thread, &attr, run_uc, &chan_stuff[chan].chan))
+            exit(2);
+        /*endif*/
+    }
+    /*endfor*/
+    for (;;)
+    {
+        sleep(5);
+        printf("Main thread\n");
+    }
+    /*endfor*/
+
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+#else
+int main(int argc, char *argv[]) 
+{
+    printf("This program was not built with Unicall available\n"); 
+}
+/*- End of function --------------------------------------------------------*/
+#endif
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/tests/time_scale_tests.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/tests/time_scale_tests.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,175 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * time_scale_tests.c
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2004 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: time_scale_tests.c,v 1.22 2008/07/28 15:14:30 steveu Exp $
+ */
+
+/*! \page time_scale_tests_page Time scaling tests
+\section time_scale_tests_page_sec_1 What does it do?
+These tests run a speech file through the time scaling routines.
+
+\section time_scale_tests_page_sec_2 How are the tests run?
+These tests process a speech file called pre_time_scale.wav. This file should contain
+8000 sample/second 16 bits/sample linear audio. The tests read this file, change the
+time scale of its contents, and write the resulting audio to post_time_scale.wav.
+This file also contains 8000 sample/second 16 bits/sample linear audio.
+*/
+
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <string.h>
+#include <audiofile.h>
+
+#include "spandsp.h"
+
+#define BLOCK_LEN       160
+
+#define IN_FILE_NAME    "../test-data/local/short_nb_voice.wav"
+#define OUT_FILE_NAME   "time_scale_result.wav"
+
+int main(int argc, char *argv[])
+{
+    AFfilehandle inhandle;
+    AFfilehandle outhandle;
+    AFfilesetup filesetup;
+    int16_t in[BLOCK_LEN];
+    int16_t out[5*(BLOCK_LEN + TIME_SCALE_MAX_SAMPLE_RATE/TIME_SCALE_MIN_PITCH)];
+    int frames;
+    int new_frames;
+    int out_frames;
+    int count;
+    int max;
+    time_scale_state_t state;
+    float x;
+    float rate;
+    float sample_rate;
+    const char *in_file_name;
+    int sweep_rate;
+    int opt;
+
+    rate = 1.8f;
+    sweep_rate = FALSE;
+    in_file_name = IN_FILE_NAME;
+    while ((opt = getopt(argc, argv, "i:r:s")) != -1)
+    {
+        switch (opt)
+        {
+        case 'i':
+            in_file_name = optarg;
+            break;
+        case 'r':
+            rate = atof(optarg);
+            break;
+        case 's':
+            sweep_rate = TRUE;
+            break;
+        default:
+            //usage();
+            exit(2);
+            break;
+        }
+    }
+    if ((inhandle = afOpenFile(in_file_name, "r", 0)) == AF_NULL_FILEHANDLE)
+    {
+        printf("    Cannot open wave file '%s'\n", in_file_name);
+        exit(2);
+    }
+    if ((x = afGetFrameSize(inhandle, AF_DEFAULT_TRACK, 1)) != 2.0)
+    {
+        printf("    Unexpected frame size in wave file '%s'\n", in_file_name);
+        exit(2);
+    }
+    if ((x = afGetChannels(inhandle, AF_DEFAULT_TRACK)) != 1.0)
+    {
+        printf("    Unexpected number of channels in wave file '%s'\n", in_file_name);
+        exit(2);
+    }
+    sample_rate = afGetRate(inhandle, AF_DEFAULT_TRACK);
+
+    if ((filesetup = afNewFileSetup()) == AF_NULL_FILESETUP)
+    {
+        fprintf(stderr, "    Failed to create file setup\n");
+        exit(2);
+    }
+    afInitSampleFormat(filesetup, AF_DEFAULT_TRACK, AF_SAMPFMT_TWOSCOMP, 16);
+    afInitRate(filesetup, AF_DEFAULT_TRACK, sample_rate);
+    afInitFileFormat(filesetup, AF_FILE_WAVE);
+    afInitChannels(filesetup, AF_DEFAULT_TRACK, 1);
+    if ((outhandle = afOpenFile(OUT_FILE_NAME, "w", filesetup)) == AF_NULL_FILEHANDLE)
+    {
+        fprintf(stderr, "    Cannot create wave file '%s'\n", OUT_FILE_NAME);
+        exit(2);
+    }
+
+    if ((time_scale_init(&state, (int) sample_rate, rate)) == NULL)
+    {
+        fprintf(stderr, "    Cannot start the time scaler\n");
+        exit(2);
+    }
+    max = time_scale_max_output_len(&state, BLOCK_LEN);
+    printf("Rate is %f, longest output block is %d\n", rate, max);
+    count = 0;
+    while ((frames = afReadFrames(inhandle, AF_DEFAULT_TRACK, in, BLOCK_LEN)))
+    {
+        new_frames = time_scale(&state, out, in, frames);
+        out_frames = afWriteFrames(outhandle, AF_DEFAULT_TRACK, out, new_frames);
+        if (out_frames != new_frames)
+        {
+            fprintf(stderr, "    Error writing wave file\n");
+            exit(2);
+        }
+        if (sweep_rate  &&  ++count > 100)
+        {
+            if (rate > 0.5f)
+            {
+                rate -= 0.1f;
+                if (rate >= 0.99f  &&  rate <= 1.01f)
+                    rate -= 0.1f;
+                time_scale_init(&state, SAMPLE_RATE, rate);
+                max = time_scale_max_output_len(&state, BLOCK_LEN);
+                printf("Rate is %f, longest output block is %d\n", rate, max);
+            }
+            count = 0;
+        }
+    }
+    if (afCloseFile(inhandle) != 0)
+    {
+        printf("    Cannot close wave file '%s'\n", in_file_name);
+        exit(2);
+    }
+    if (afCloseFile(outhandle) != 0)
+    {
+        printf("    Cannot close wave file '%s'\n", OUT_FILE_NAME);
+        exit(2);
+    }
+    afFreeFileSetup(filesetup);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/tests/tone_detect_tests.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/tests/tone_detect_tests.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,140 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * tone_detect_tests.c
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2007 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: tone_detect_tests.c,v 1.10 2008/05/13 13:17:26 steveu Exp $
+ */
+
+/*! \page tone_detect_tests_page Tone detection tests
+\section tone_detect_tests_page_sec_1 What does it do?
+*/
+
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <string.h>
+#include <time.h>
+#include <audiofile.h>
+
+#include "spandsp.h"
+
+#define DEC_SAMPLE_RATE     800
+#define DEC_RATIO           10
+#define BLOCK_LEN           56
+#define PG_WINDOW           56
+#define FREQ1               440.0f
+#define FREQ2               480.0f
+
+static int periodogram_tests(void)
+{
+    int i;
+    int j;
+    int k;
+    int len;
+    complexf_t coeffs[PG_WINDOW/2];
+    complexf_t camp[BLOCK_LEN];
+    complexf_t last_result;
+    complexf_t result;
+    complexf_t phase_offset;
+    float freq_error;
+    float pg_scale;
+    float level;
+    float scale1;
+    float scale2;
+    int32_t phase_rate1;
+    int32_t phase_rate2;
+    uint32_t phase_acc1;
+    uint32_t phase_acc2;
+    awgn_state_t noise_source_re;
+    awgn_state_t noise_source_im;
+
+    phase_rate1 = DEC_RATIO*dds_phase_ratef(FREQ1 - 5.0f);
+    phase_rate2 = DEC_RATIO*dds_phase_ratef(FREQ2);
+    phase_acc1 = 0;
+    phase_acc2 = 0;
+    len = periodogram_generate_coeffs(coeffs, FREQ1, DEC_SAMPLE_RATE, PG_WINDOW);
+    if (len != PG_WINDOW/2)
+    {
+        printf("Test failed\n");
+        return -1;
+    }
+    pg_scale = periodogram_generate_phase_offset(&phase_offset, FREQ1, DEC_SAMPLE_RATE, PG_WINDOW);
+    scale1 = dds_scaling_dbm0f(-6.0f);
+    scale2 = dds_scaling_dbm0f(-6.0f);
+
+    for (k = -50;  k < 0;  k++)
+    {
+        printf("Setting noise to %ddBm0\n", k);
+        awgn_init_dbm0(&noise_source_re, 1234567, (float) k);
+        awgn_init_dbm0(&noise_source_im, 7654321, (float) k);
+        last_result = complex_setf(0.0f, 0.0f);
+        for (i = 0;  i < 100;  i++)
+        {
+            for (j = 0;  j < PG_WINDOW;  j++)
+            {
+                result = dds_complexf(&phase_acc1, phase_rate1);
+                camp[j].re = result.re*scale1;
+                camp[j].im = result.im*scale1;
+                result = dds_complexf(&phase_acc2, phase_rate2);
+                camp[j].re += result.re*scale2;
+                camp[j].im += result.im*scale2;
+                camp[j].re += awgn(&noise_source_re);
+                camp[j].im += awgn(&noise_source_im);
+            }
+            result = periodogram(coeffs, camp, PG_WINDOW);
+            level = sqrtf(result.re*result.re + result.im*result.im);
+            freq_error = periodogram_freq_error(&phase_offset, pg_scale, &last_result, &result);
+            last_result = result;
+            if (i == 0)
+                continue;
+
+            printf("Signal level = %.5f, freq error = %.5f\n", level, freq_error);
+            if (level < scale1*0.8f  ||  level > scale1*1.2f)
+            {
+                printf("Test failed - %ddBm0 of noise, signal is %f (%f)\n", k, level, scale1);
+                return -1;
+            }
+            if (freq_error < -10.0f  ||  freq_error > 10.0f)
+            {
+                printf("Test failed - %ddBm0 of noise, %fHz error\n", k, freq_error);
+                return -1;
+            }
+        }
+    }
+    return  0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int main(int argc, char *argv[])
+{
+    if (periodogram_tests())
+        exit(2);
+    printf("Tests passed\n");
+    return  0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/tests/tone_generate_tests.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/tests/tone_generate_tests.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,169 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * tone_generate_tests.c
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2001 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: tone_generate_tests.c,v 1.19 2008/08/16 15:24:16 steveu Exp $
+ */
+
+/*! \page tone_generate_tests_page Tone generation tests
+\section tone_generate_tests_page_sec_1 What does it do?
+*/
+
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <string.h>
+#include <time.h>
+#include <audiofile.h>
+
+#include "spandsp.h"
+#include "spandsp-sim.h"
+
+#define OUTPUT_FILE_NAME    "tone_generate.wav"
+
+int main(int argc, char *argv[])
+{
+    tone_gen_descriptor_t tone_desc;
+    tone_gen_state_t tone_state;
+    int i;
+    int16_t amp[16384];
+    int len;
+    AFfilehandle outhandle;
+    int outframes;
+
+    if ((outhandle = afOpenFile_telephony_write(OUTPUT_FILE_NAME, 1)) == AF_NULL_FILEHANDLE)
+    {
+        fprintf(stderr, "    Cannot open wave file '%s'\n", OUTPUT_FILE_NAME);
+        exit(2);
+    }
+    
+    make_tone_gen_descriptor(&tone_desc,
+                             440,
+                             -10,
+                             620,
+                             -15,
+                             100,
+                             200,
+                             300,
+                             400,
+                             FALSE);
+    tone_gen_init(&tone_state, &tone_desc);
+
+    for (i = 0;  i < 1000;  i++)
+    {
+        len = tone_gen(&tone_state, amp, 160);
+        printf("Generated %d samples\n", len);
+        if (len <= 0)
+            break;
+        outframes = afWriteFrames(outhandle,
+                                  AF_DEFAULT_TRACK,
+                                  amp,
+                                  len);
+    }
+    
+    make_tone_gen_descriptor(&tone_desc,
+                             350,
+                             -10,
+                             440,
+                             -15,
+                             100,
+                             200,
+                             300,
+                             400,
+                             TRUE);
+    tone_gen_init(&tone_state, &tone_desc);
+
+    for (i = 0;  i < 1000;  i++)
+    {
+        len = tone_gen(&tone_state, amp, 160);
+        printf("Generated %d samples\n", len);
+        if (len <= 0)
+            break;
+        outframes = afWriteFrames(outhandle,
+                                  AF_DEFAULT_TRACK,
+                                  amp,
+                                  len);
+    }
+    
+    make_tone_gen_descriptor(&tone_desc,
+                             400,
+                             -10,
+                             450,
+                             -10,
+                             100,
+                             200,
+                             300,
+                             400,
+                             TRUE);
+    tone_gen_init(&tone_state, &tone_desc);
+
+    for (i = 0;  i < 1000;  i++)
+    {
+        len = tone_gen(&tone_state, amp, 160);
+        printf("Generated %d samples\n", len);
+        if (len <= 0)
+            break;
+        outframes = afWriteFrames(outhandle,
+                                  AF_DEFAULT_TRACK,
+                                  amp,
+                                  len);
+    }
+    
+    make_tone_gen_descriptor(&tone_desc,
+                             425,
+                             -10,
+                             -50,
+                             100,
+                             100,
+                             200,
+                             300,
+                             400,
+                             TRUE);
+    tone_gen_init(&tone_state, &tone_desc);
+
+    for (i = 0;  i < 1000;  i++)
+    {
+        len = tone_gen(&tone_state, amp, 160);
+        printf("Generated %d samples\n", len);
+        if (len <= 0)
+            break;
+        outframes = afWriteFrames(outhandle,
+                                  AF_DEFAULT_TRACK,
+                                  amp,
+                                  len);
+    }
+    
+    if (afCloseFile(outhandle) != 0)
+    {
+        fprintf(stderr, "    Cannot close wave file '%s'\n", OUTPUT_FILE_NAME);
+        exit (2);
+    }
+
+    return  0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/tests/tsb85_tests.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/tests/tsb85_tests.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,1101 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * tsb85_tests.c
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2008 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License version 2.1,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: tsb85_tests.c,v 1.19 2008/08/13 00:11:30 steveu Exp $
+ */
+
+/*! \file */
+
+#if defined(HAVE_CONFIG_H)
+#include <config.h>
+#endif
+
+#include <inttypes.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <ctype.h>
+#include "floating_fudge.h"
+#if defined(HAVE_TGMATH_H)
+#include <tgmath.h>
+#endif
+#if defined(HAVE_MATH_H)
+#include <math.h>
+#endif
+#include <assert.h>
+#include <fcntl.h>
+#include <time.h>
+#include <unistd.h>
+#include <audiofile.h>
+
+#if defined(HAVE_LIBXML_XMLMEMORY_H)
+#include <libxml/xmlmemory.h>
+#endif
+#if defined(HAVE_LIBXML_PARSER_H)
+#include <libxml/parser.h>
+#endif
+#if defined(HAVE_LIBXML_XINCLUDE_H)
+#include <libxml/xinclude.h>
+#endif
+
+#include "spandsp.h"
+#include "fax_tester.h"
+
+#define OUTPUT_TIFF_FILE_NAME   "tsb85.tif"
+
+#define OUTPUT_FILE_NAME_WAVE   "tsb85.wav"
+
+#define SAMPLES_PER_CHUNK       160
+
+AFfilehandle out_handle;
+AFfilesetup filesetup;
+
+int use_receiver_not_ready = FALSE;
+int test_local_interrupt = FALSE;
+
+const char *output_tiff_file_name;
+
+fax_state_t fax;
+faxtester_state_t state;
+
+uint8_t image[1000000];
+
+uint8_t awaited[1000];
+int awaited_len = 0;
+
+static int next_step(faxtester_state_t *s);
+
+static int phase_b_handler(t30_state_t *s, void *user_data, int result)
+{
+    int i;
+    const char *u;
+    
+    i = (intptr_t) user_data;
+    if ((u = t30_get_rx_ident(s)))
+        printf("%d: Phase B: remote ident '%s'\n", i, u);
+    if ((u = t30_get_rx_sub_address(s)))
+        printf("%d: Phase B: remote sub-address '%s'\n", i, u);
+    if ((u = t30_get_rx_polled_sub_address(s)))
+        printf("%d: Phase B: remote polled sub-address '%s'\n", i, u);
+    if ((u = t30_get_rx_selective_polling_address(s)))
+        printf("%d: Phase B: remote selective polling address '%s'\n", i, u);
+    if ((u = t30_get_rx_sender_ident(s)))
+        printf("%d: Phase B: remote sender ident '%s'\n", i, u);
+    if ((u = t30_get_rx_password(s)))
+        printf("%d: Phase B: remote password '%s'\n", i, u);
+    printf("%d: Phase B handler on channel %d - (0x%X) %s\n", i, i, result, t30_frametype(result));
+    return T30_ERR_OK;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int phase_d_handler(t30_state_t *s, void *user_data, int result)
+{
+    int i;
+    t30_stats_t t;
+    const char *u;
+
+    i = (intptr_t) user_data;
+    t30_get_transfer_statistics(s, &t);
+
+    printf("%d: Phase D handler on channel %d - (0x%X) %s\n", i, i, result, t30_frametype(result));
+    printf("%d: Phase D: bit rate %d\n", i, t.bit_rate);
+    printf("%d: Phase D: ECM %s\n", i, (t.error_correcting_mode)  ?  "on"  :  "off");
+    printf("%d: Phase D: pages transferred %d\n", i, t.pages_transferred);
+    printf("%d: Phase D: pages in the file %d\n", i, t.pages_in_file);
+    printf("%d: Phase D: image size %d x %d\n", i, t.width, t.length);
+    printf("%d: Phase D: image resolution %d x %d\n", i, t.x_resolution, t.y_resolution);
+    printf("%d: Phase D: bad rows %d\n", i, t.bad_rows);
+    printf("%d: Phase D: longest bad row run %d\n", i, t.longest_bad_row_run);
+    printf("%d: Phase D: compression type %d\n", i, t.encoding);
+    printf("%d: Phase D: image size %d bytes\n", i, t.image_size);
+    if ((u = t30_get_tx_ident(s)))
+        printf("%d: Phase D: local ident '%s'\n", i, u);
+    if ((u = t30_get_rx_ident(s)))
+        printf("%d: Phase D: remote ident '%s'\n", i, u);
+    printf("%d: Phase D: bits per row - min %d, max %d\n", i, s->t4.min_row_bits, s->t4.max_row_bits);
+
+    if (use_receiver_not_ready)
+        t30_set_receiver_not_ready(s, 3);
+
+    if (test_local_interrupt)
+    {
+        if (i == 0)
+        {
+            printf("%d: Initiating interrupt request\n", i);
+            t30_local_interrupt_request(s, TRUE);
+        }
+        else
+        {
+            switch (result)
+            {
+            case T30_PIP:
+            case T30_PRI_MPS:
+            case T30_PRI_EOM:
+            case T30_PRI_EOP:
+                printf("%d: Accepting interrupt request\n", i);
+                t30_local_interrupt_request(s, TRUE);
+                break;
+            case T30_PIN:
+                break;
+            }
+        }
+    }
+    return T30_ERR_OK;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void phase_e_handler(t30_state_t *s, void *user_data, int result)
+{
+    int i;
+    t30_stats_t t;
+    const char *u;
+    
+    i = (intptr_t) user_data;
+    printf("%d: Phase E handler on channel %d - (%d) %s\n", i, i, result, t30_completion_code_to_str(result));    
+    t30_get_transfer_statistics(s, &t);
+    printf("%d: Phase E: bit rate %d\n", i, t.bit_rate);
+    printf("%d: Phase E: ECM %s\n", i, (t.error_correcting_mode)  ?  "on"  :  "off");
+    printf("%d: Phase E: pages transferred %d\n", i, t.pages_transferred);
+    printf("%d: Phase E: pages in the file %d\n", i, t.pages_in_file);
+    printf("%d: Phase E: image size %d x %d\n", i, t.width, t.length);
+    printf("%d: Phase E: image resolution %d x %d\n", i, t.x_resolution, t.y_resolution);
+    printf("%d: Phase E: bad rows %d\n", i, t.bad_rows);
+    printf("%d: Phase E: longest bad row run %d\n", i, t.longest_bad_row_run);
+    printf("%d: Phase E: coding method %s\n", i, t4_encoding_to_str(t.encoding));
+    printf("%d: Phase E: image size %d bytes\n", i, t.image_size);
+    //printf("%d: Phase E: local ident '%s'\n", i, info->ident);
+    if ((u = t30_get_rx_ident(s)))
+        printf("%d: Phase E: remote ident '%s'\n", i, u);
+    if ((u = t30_get_rx_country(s)))
+        printf("%d: Phase E: Remote was made in '%s'\n", i, u);
+    if ((u = t30_get_rx_vendor(s)))
+        printf("%d: Phase E: Remote was made by '%s'\n", i, u);
+    if ((u = t30_get_rx_model(s)))
+        printf("%d: Phase E: Remote is model '%s'\n", i, u);
+}
+/*- End of function --------------------------------------------------------*/
+
+static void t30_real_time_frame_handler(t30_state_t *s,
+                                        void *user_data,
+                                        int direction,
+                                        const uint8_t *msg,
+                                        int len)
+{
+    if (msg == NULL)
+    {
+    }
+    else
+    {
+        printf("T.30: Real time frame handler - %s, %s, length = %d\n",
+               (direction)  ?  "line->T.30"  : "T.30->line",
+               t30_frametype(msg[2]),
+               len);
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static int document_handler(t30_state_t *s, void *user_data, int event)
+{
+    int i;
+    
+    i = (intptr_t) user_data;
+    printf("%d: Document handler on channel %d - event %d\n", i, i, event);
+    return FALSE;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void faxtester_real_time_frame_handler(faxtester_state_t *s,
+                                              void *user_data,
+                                              int direction,
+                                              const uint8_t *msg,
+                                              int len)
+{
+    if (msg == NULL)
+    {
+        next_step(s);
+    }
+    else
+    {
+        printf("Real time frame handler - %s, %s, length = %d\n",
+               (direction)  ?  "line->tester"  : "tester->line",
+               t30_frametype(msg[2]),
+               len);
+        if (direction  &&  msg[1] == 0x13)
+        {
+            if ((awaited_len >= 0  &&  len != abs(awaited_len))
+                ||
+                (awaited_len < 0  &&  len < abs(awaited_len))
+                ||
+                memcmp(msg, awaited, abs(awaited_len)) != 0)
+            {
+                span_log_buf(&s->logging, SPAN_LOG_FLOW, "Expected", awaited, awaited_len);
+                span_log_buf(&s->logging, SPAN_LOG_FLOW, "Received", msg, len);
+                exit(2);
+            }
+        }
+        if (msg[1] == 0x13)
+            next_step(s);
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static void faxtester_front_end_step_complete_handler(faxtester_state_t *s, void *user_data)
+{
+    next_step(s);
+}
+/*- End of function --------------------------------------------------------*/
+
+static void faxtester_front_end_step_timeout_handler(faxtester_state_t *s, void *user_data)
+{
+    span_log(&s->logging, SPAN_LOG_FLOW, "FAX tester step timed out\n");
+    exit(2);
+}
+/*- End of function --------------------------------------------------------*/
+
+static void fax_prepare(void)
+{
+    t30_state_t *t30;
+
+    t30 = fax_get_t30_state(&fax);
+    fax_set_transmit_on_idle(&fax, TRUE);
+    fax_set_tep_mode(&fax, TRUE);
+    t30_set_tx_ident(t30, "1234567890");
+    t30_set_tx_sub_address(t30, "Sub-address");
+    t30_set_tx_sender_ident(t30, "Sender ID");
+    t30_set_tx_password(t30, "Password");
+    t30_set_tx_polled_sub_address(t30, "Polled sub-address");
+    t30_set_tx_selective_polling_address(t30, "Sel polling address");
+    t30_set_tx_nsf(t30, (const uint8_t *) "\x50\x00\x00\x00Spandsp\x00", 12);
+    t30_set_ecm_capability(t30, TRUE);
+    t30_set_supported_t30_features(t30,
+                                   T30_SUPPORT_IDENTIFICATION
+                                 | T30_SUPPORT_SELECTIVE_POLLING
+                                 | T30_SUPPORT_SUB_ADDRESSING);
+    t30_set_supported_image_sizes(t30,
+                                  T30_SUPPORT_US_LETTER_LENGTH
+                                | T30_SUPPORT_US_LEGAL_LENGTH
+                                | T30_SUPPORT_UNLIMITED_LENGTH
+                                | T30_SUPPORT_215MM_WIDTH
+                                | T30_SUPPORT_255MM_WIDTH
+                                | T30_SUPPORT_303MM_WIDTH);
+    t30_set_supported_resolutions(t30,
+                                  T30_SUPPORT_STANDARD_RESOLUTION
+                                | T30_SUPPORT_FINE_RESOLUTION
+                                | T30_SUPPORT_SUPERFINE_RESOLUTION
+                                | T30_SUPPORT_R8_RESOLUTION
+                                | T30_SUPPORT_R16_RESOLUTION
+                                | T30_SUPPORT_300_300_RESOLUTION
+                                | T30_SUPPORT_400_400_RESOLUTION
+                                | T30_SUPPORT_600_600_RESOLUTION
+                                | T30_SUPPORT_1200_1200_RESOLUTION
+                                | T30_SUPPORT_300_600_RESOLUTION
+                                | T30_SUPPORT_400_800_RESOLUTION
+                                | T30_SUPPORT_600_1200_RESOLUTION);
+    t30_set_supported_modems(t30, T30_SUPPORT_V27TER | T30_SUPPORT_V29 | T30_SUPPORT_V17);
+    t30_set_supported_compressions(t30, T30_SUPPORT_T4_1D_COMPRESSION | T30_SUPPORT_T4_2D_COMPRESSION | T30_SUPPORT_T6_COMPRESSION);
+    t30_set_phase_b_handler(t30, phase_b_handler, (void *) (intptr_t) 1);
+    t30_set_phase_d_handler(t30, phase_d_handler, (void *) (intptr_t) 1);
+    t30_set_phase_e_handler(t30, phase_e_handler, (void *) (intptr_t) 1);
+    t30_set_real_time_frame_handler(t30, t30_real_time_frame_handler, (void *) (intptr_t) 1);
+    t30_set_document_handler(t30, document_handler, (void *) (intptr_t) 1);
+
+    span_log_set_level(&t30->logging, SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME | SPAN_LOG_FLOW);
+    span_log_set_tag(&t30->logging, "A");
+    span_log_set_level(&fax.modems.v27ter_rx.logging, SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME | SPAN_LOG_FLOW);
+    span_log_set_tag(&fax.modems.v27ter_rx.logging, "A");
+    span_log_set_level(&fax.modems.v29_rx.logging, SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME | SPAN_LOG_FLOW);
+    span_log_set_tag(&fax.modems.v29_rx.logging, "A");
+    span_log_set_level(&fax.modems.v17_rx.logging, SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME | SPAN_LOG_FLOW);
+    span_log_set_tag(&fax.modems.v17_rx.logging, "A");
+    span_log_set_level(&fax.logging, SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME | SPAN_LOG_FLOW);
+    span_log_set_tag(&fax.logging, "A");
+}
+/*- End of function --------------------------------------------------------*/
+
+static int string_to_msg(uint8_t msg[], uint8_t mask[], const char buf[])
+{
+    int i;
+    int x;
+    const char *t;
+
+    msg[0] = 0;
+    mask[0] = 0xFF;
+    i = 0;
+    t = (char *) buf;
+    while (*t)
+    {
+        /* Skip white space */
+        while (isspace(*t))
+            t++;
+        /* If we find ... we allow arbitrary addition info beyond this point in the message */
+        if (t[0] == '.'  &&  t[1] == '.'  &&  t[2] == '.')
+        {
+            return -i;
+        }
+        else if (isxdigit(*t))
+        {
+            for (  ;  isxdigit(*t);  t++)
+            {
+                x = *t;
+                if (x >= 'a')
+                    x -= 0x20;
+                if (x >= 'A')
+                    x -= ('A' - 10);
+                else
+                    x -= '0';
+                msg[i] = (msg[i] << 4) | x;
+            }
+            mask[i] = 0xFF;
+            if (*t == '/')
+            {
+                /* There is a mask following the byte */
+                mask[i] = 0;
+                for (t++;  isxdigit(*t);  t++)
+                {
+                    x = *t;
+                    if (x >= 'a')
+                        x -= 0x20;
+                    if (x >= 'A')
+                        x -= ('A' - 10);
+                    else
+                        x -= '0';
+                    mask[i] = (mask[i] << 4) | x;
+                }
+            }
+            if (*t  &&  !isspace(*t))
+            {
+                /* Bad string */
+                return 0;
+            }
+            i++;
+        }
+    }
+    return i;
+}
+/*- End of function --------------------------------------------------------*/
+
+#if 0
+static void string_test2(const uint8_t msg[], int len)
+{
+    int i;
+    
+    if (len > 0)
+    {
+        for (i = 0;  i < len - 1;  i++)
+            printf("%02X ", msg[i]);
+        printf("%02X", msg[i]);
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static void string_test3(const char buf[])
+{
+    uint8_t msg[1000];
+    uint8_t mask[1000];
+    int len;
+    int i;
+    
+    len = string_to_msg(msg, mask, buf);
+    printf("Len = %d: ", len);
+    string_test2(msg, abs(len));
+    printf("/");
+    string_test2(mask, abs(len));
+    printf("\n");
+}
+/*- End of function --------------------------------------------------------*/
+
+static int string_test(void)
+{
+    string_test3("FF C8 12 34 56 78");
+    string_test3("FF C8 12/55 34 56/aA 78 ");
+    string_test3("FF C8 12/55 34 56/aA 78 ...");
+    string_test3("FF C8 12/55 34 56/aA 78...");
+    string_test3("FF C8 12/55 34 56/aA 78 ... 99 88 77");
+    exit(0);
+}
+/*- End of function --------------------------------------------------------*/
+#endif
+
+static void corrupt_image(faxtester_state_t *s, uint8_t image[], int len, const char *bad_rows)
+{
+    int i;
+    int j;
+    int k;
+    uint32_t bits;
+    uint32_t bitsx;
+    int list[1000];
+    int x;
+    int row;
+    const char *t;
+
+    /* Form the list of rows to be hit */
+    x = 0;
+    t = bad_rows;
+    while (*t)
+    {
+        while (isspace(*t))
+            t++;
+        if (sscanf(t, "%d", &list[x]) < 1)
+            break;
+        x++;
+        while (isdigit(*t))
+            t++;
+        if (*t == ',')
+            t++;
+    }
+
+    /* Go through the image, and corrupt the first bit of every listed row */
+    bits = 0x7FF;
+    bitsx = 0x7FF;
+    row = 0;
+    for (i = 0;  i < len;  i++)
+    {
+        bits ^= (image[i] << 11);
+        bitsx ^= (image[i] << 11);
+        for (j = 0;  j < 8;  j++)
+        {
+            if ((bits & 0xFFF) == 0x800)
+            {
+                /* We are at an EOL. Is this row in the list of rows to be corrupted? */
+                row++;
+                for (k = 0;  k < x;  k++)
+                {
+                    if (list[k] == row)
+                    {
+                        /* Corrupt this row. TSB85 says to hit the first bit after the EOL */
+                        bitsx ^= 0x1000;
+                    }
+                }
+            }
+            bits >>= 1;
+            bitsx >>= 1;
+        }
+        image[i] = (bitsx >> 3) & 0xFF;
+    }
+    span_log(&s->logging, SPAN_LOG_FLOW, "%d rows found. %d corrupted\n", row, x);
+}
+/*- End of function --------------------------------------------------------*/
+
+static int next_step(faxtester_state_t *s)
+{
+    int delay;
+    int flags;
+    xmlChar *dir;
+    xmlChar *type;
+    xmlChar *modem;
+    xmlChar *value;
+    xmlChar *tag;
+    xmlChar *bad_rows;
+    xmlChar *crc_error;
+    xmlChar *pattern;
+    xmlChar *timeout;
+    xmlChar *min_time;
+    uint8_t buf[1000];
+    uint8_t mask[1000];
+    int i;
+    int j;
+    int hdlc;
+    int short_train;
+    int min_row_bits;
+    int compression;
+    int compression_step;
+    int timer;
+    int len;
+    t4_state_t t4_state;
+    t30_state_t *t30;
+
+    if (s->cur == NULL)
+    {
+        if (!s->final_delayed)
+        {
+            /* Add a bit of waiting at the end, to ensure everything gets flushed through,
+               any timers can expire, etc. */
+            faxtester_set_rx_type(s, T30_MODEM_NONE, 0, FALSE, FALSE);
+            faxtester_set_tx_type(s, T30_MODEM_PAUSE, 0, 120000, FALSE);
+            s->final_delayed = TRUE;
+            return 1;
+        }
+        /* Finished */
+        exit(0);
+    }
+    while (s->cur  &&  xmlStrcmp(s->cur->name, (const xmlChar *) "step") != 0)
+        s->cur = s->cur->next;
+    if (s->cur == NULL)
+    {
+        /* Finished */
+        exit(0);
+    }
+
+    dir = xmlGetProp(s->cur, (const xmlChar *) "dir");
+    type = xmlGetProp(s->cur, (const xmlChar *) "type");
+    modem = xmlGetProp(s->cur, (const xmlChar *) "modem");
+    value = xmlGetProp(s->cur, (const xmlChar *) "value");
+    tag = xmlGetProp(s->cur, (const xmlChar *) "tag");
+    bad_rows = xmlGetProp(s->cur, (const xmlChar *) "bad_rows");
+    crc_error = xmlGetProp(s->cur, (const xmlChar *) "crc_error");
+    pattern = xmlGetProp(s->cur, (const xmlChar *) "pattern");
+    timeout = xmlGetProp(s->cur, (const xmlChar *) "timeout");
+    min_time = xmlGetProp(s->cur, (const xmlChar *) "min_time");
+
+    s->cur = s->cur->next;
+
+    span_log(&s->logging,
+             SPAN_LOG_FLOW, 
+             "Dir - %s, type - %s, modem - %s, value - %s, timeout - %s, tag - %s\n",
+             (dir)  ?  (const char *) dir  :  "",
+             (type)  ?  (const char *) type  :  "",
+             (modem)  ?  (const char *) modem  :  "",
+             (value)  ?  (const char *) value  :  "",
+             (timeout)  ?  (const char *) timeout  :  "",
+             (tag)  ?  (const char *) tag  :  "");
+    if (type == NULL)
+        return 1;
+    if (timeout)
+        timer = atoi((const char *) timeout);
+    else
+        timer = -1;
+
+    if (dir  &&  strcasecmp((const char *) dir, "R") == 0)
+    {
+        /* Receive always has a timeout applied. */
+        if (timer < 0)
+            timer = 7000;
+        faxtester_set_timeout(s, timer);
+        if (modem)
+        {
+            hdlc = (strcasecmp((const char *) type, "PREAMBLE") == 0);
+            short_train = (strcasecmp((const char *) type, "TCF") != 0);
+            faxtester_set_tx_type(s, T30_MODEM_NONE, 0, FALSE, FALSE);
+            if (strcasecmp((const char *) modem, "V.21") == 0)
+            {
+                faxtester_set_rx_type(s, T30_MODEM_V21, 300, FALSE, TRUE);
+            }
+            else if (strcasecmp((const char *) modem, "V.17/14400") == 0)
+            {
+                faxtester_set_rx_type(s, T30_MODEM_V17, 14400, short_train, hdlc);
+            }
+            else if (strcasecmp((const char *) modem, "V.17/12000") == 0)
+            {
+                faxtester_set_rx_type(s, T30_MODEM_V17, 12000, short_train, hdlc);
+            }
+            else if (strcasecmp((const char *) modem, "V.17/9600") == 0)
+            {
+                faxtester_set_rx_type(s, T30_MODEM_V17, 9600, short_train, hdlc);
+            }
+            else if (strcasecmp((const char *) modem, "V.17/7200") == 0)
+            {
+                faxtester_set_rx_type(s, T30_MODEM_V17, 7200, short_train, hdlc);
+            }
+            else if (strcasecmp((const char *) modem, "V.29/9600") == 0)
+            {
+                faxtester_set_rx_type(s, T30_MODEM_V29, 9600, FALSE, hdlc);
+            }
+            else if (strcasecmp((const char *) modem, "V.29/7200") == 0)
+            {
+                faxtester_set_rx_type(s, T30_MODEM_V29, 7200, FALSE, hdlc);
+            }
+            else if (strcasecmp((const char *) modem, "V.27ter/4800") == 0)
+            {
+                faxtester_set_rx_type(s, T30_MODEM_V27TER, 4800, FALSE, hdlc);
+            }
+            else if (strcasecmp((const char *) modem, "V.27ter/2400") == 0)
+            {
+                faxtester_set_rx_type(s, T30_MODEM_V27TER, 2400, FALSE, hdlc);
+            }
+            else
+            {
+                span_log(&s->logging, SPAN_LOG_FLOW, "Unrecognised modem\n");
+            }
+        }
+        if (strcasecmp((const char *) type, "CNG") == 0)
+        {
+            /* Look for CNG */
+            faxtester_set_rx_type(s, T30_MODEM_CNG, 0, FALSE, FALSE);
+            faxtester_set_tx_type(s, T30_MODEM_NONE, 0, FALSE, FALSE);
+        }
+        else if (strcasecmp((const char *) type, "CED") == 0)
+        {
+            /* Look for CED */
+            faxtester_set_rx_type(s, T30_MODEM_CED, 0, FALSE, FALSE);
+            faxtester_set_tx_type(s, T30_MODEM_NONE, 0, FALSE, FALSE);
+        }
+        else if (strcasecmp((const char *) type, "HDLC") == 0)
+        {
+            i = string_to_msg(buf, mask, (const char *) value);
+            bit_reverse(awaited, buf, abs(i));
+            awaited_len = i;
+        }
+        else if (strcasecmp((const char *) type, "TCF") == 0)
+        {
+        }
+        else if (strcasecmp((const char *) type, "MSG") == 0)
+        {
+        }
+        else if (strcasecmp((const char *) type, "PP") == 0)
+        {
+        }
+        else if (strcasecmp((const char *) type, "SILENCE") == 0)
+        {
+            faxtest_set_rx_silence(s);
+        }
+        else
+        {
+            span_log(&s->logging, SPAN_LOG_FLOW, "Unrecognised type '%s'\n", (const char *) type);
+            return 0;
+        }
+    }
+    else
+    {
+        faxtester_set_timeout(s, timer);
+        if (modem)
+        {
+            hdlc = (strcasecmp((const char *) type, "PREAMBLE") == 0);
+            short_train = (strcasecmp((const char *) type, "TCF") != 0);
+            faxtester_set_rx_type(s, T30_MODEM_NONE, 0, FALSE, FALSE);
+            if (strcasecmp((const char *) modem, "V.21") == 0)
+            {
+                faxtester_set_tx_type(s, T30_MODEM_V21, 300, FALSE, TRUE);
+            }
+            else if (strcasecmp((const char *) modem, "V.17/14400") == 0)
+            {
+                faxtester_set_tx_type(s, T30_MODEM_V17, 14400, short_train, hdlc);
+            }
+            else if (strcasecmp((const char *) modem, "V.17/12000") == 0)
+            {
+                faxtester_set_tx_type(s, T30_MODEM_V17, 12000, short_train, hdlc);
+            }
+            else if (strcasecmp((const char *) modem, "V.17/9600") == 0)
+            {
+                faxtester_set_tx_type(s, T30_MODEM_V17, 9600, short_train, hdlc);
+            }
+            else if (strcasecmp((const char *) modem, "V.17/7200") == 0)
+            {
+                faxtester_set_tx_type(s, T30_MODEM_V17, 7200, short_train, hdlc);
+            }
+            else if (strcasecmp((const char *) modem, "V.29/9600") == 0)
+            {
+                faxtester_set_tx_type(s, T30_MODEM_V29, 9600, FALSE, hdlc);
+            }
+            else if (strcasecmp((const char *) modem, "V.29/7200") == 0)
+            {
+                faxtester_set_tx_type(s, T30_MODEM_V29, 7200, FALSE, hdlc);
+            }
+            else if (strcasecmp((const char *) modem, "V.27ter/4800") == 0)
+            {
+                faxtester_set_tx_type(s, T30_MODEM_V27TER, 4800, FALSE, hdlc);
+            }
+            else if (strcasecmp((const char *) modem, "V.27ter/2400") == 0)
+            {
+                faxtester_set_tx_type(s, T30_MODEM_V27TER, 2400, FALSE, hdlc);
+            }
+            else
+            {
+                span_log(&s->logging, SPAN_LOG_FLOW, "Unrecognised modem\n");
+            }
+        }
+
+        if (strcasecmp((const char *) type, "CALL") == 0)
+        {
+            fax_init(&fax, FALSE);
+            fax_prepare();
+            t30 = fax_get_t30_state(&fax);
+            t30_set_rx_file(t30, output_tiff_file_name, -1);
+            if (value)
+                t30_set_tx_file(t30, (const char *) value, -1, -1);
+            return 0;
+        }
+        else if (strcasecmp((const char *) type, "ANSWER") == 0)
+        {
+            fax_init(&fax, TRUE);
+            fax_prepare();
+            t30 = fax_get_t30_state(&fax);
+            if (value)
+                t30_set_tx_file(t30, (const char *) value, -1, -1);
+            return 0;
+        }
+        else if (strcasecmp((const char *) type, "CNG") == 0)
+        {
+            faxtester_set_rx_type(s, T30_MODEM_NONE, 0, FALSE, FALSE);
+            faxtester_set_tx_type(s, T30_MODEM_CNG, 0, FALSE, FALSE);
+        }
+        else if (strcasecmp((const char *) type, "CED") == 0)
+        {
+            faxtester_set_rx_type(s, T30_MODEM_NONE, 0, FALSE, FALSE);
+            faxtester_set_tx_type(s, T30_MODEM_CED, 0, FALSE, FALSE);
+        }
+        else if (strcasecmp((const char *) type, "WAIT") == 0)
+        {
+            delay = (value)  ?  atoi((const char *) value)  :  1;
+            faxtester_set_rx_type(s, T30_MODEM_NONE, 0, FALSE, FALSE);
+            faxtester_set_tx_type(s, T30_MODEM_PAUSE, 0, delay, FALSE);
+        }
+        else if (strcasecmp((const char *) type, "PREAMBLE") == 0)
+        {
+            flags = (value)  ?  atoi((const char *) value)  :  37;
+            faxtester_send_hdlc_flags(s, flags);
+        }
+        else if (strcasecmp((const char *) type, "POSTAMBLE") == 0)
+        {
+            flags = (value)  ?  atoi((const char *) value)  :  5;
+            faxtester_send_hdlc_flags(s, flags);
+        }
+        else if (strcasecmp((const char *) type, "HDLC") == 0)
+        {
+            i = string_to_msg(buf, mask, (const char *) value);
+            bit_reverse(buf, buf, abs(i));
+            if (crc_error  &&  strcasecmp((const char *) crc_error, "0") == 0)
+                faxtester_send_hdlc_msg(s, buf, abs(i), FALSE);
+            else
+                faxtester_send_hdlc_msg(s, buf, abs(i), TRUE);
+        }
+        else if (strcasecmp((const char *) type, "TCF") == 0)
+        {
+            if (value)
+                i = atoi((const char *) value);
+            else
+                i = 450;
+            if (pattern)
+            {
+                /* TODO: implement proper patterns */
+                j = atoi((const char *) pattern);
+                memset(image, 0x55, j);
+                if (i > j)
+                    memset(image + j, 0, i - j);
+            }
+            else
+            {
+                memset(image, 0, i);
+            }
+            faxtester_set_non_ecm_image_buffer(s, image, i);
+        }
+        else if (strcasecmp((const char *) type, "MSG") == 0)
+        {
+            /* A non-ECM page */
+            min_row_bits = 0;
+            compression_step = 0;
+            if (t4_tx_init(&t4_state, (const char *) value, -1, -1) == NULL)
+            {
+                span_log(&s->logging, SPAN_LOG_FLOW, "Failed to init T.4 send\n");
+                exit(2);
+            }
+            t4_tx_set_min_row_bits(&t4_state, min_row_bits);
+            t4_tx_set_header_info(&t4_state, NULL);
+            switch (compression_step)
+            {
+            case 0:
+                compression = T4_COMPRESSION_ITU_T4_1D;
+                break;
+            case 1:
+                compression = T4_COMPRESSION_ITU_T4_2D;
+                break;
+            case 2:
+                compression = T4_COMPRESSION_ITU_T6;
+                break;
+            }
+            t4_tx_set_tx_encoding(&t4_state, compression);
+            if (t4_tx_start_page(&t4_state))
+            {
+                span_log(&s->logging, SPAN_LOG_FLOW, "Failed to start T.4 send\n");
+                exit(2);
+            }
+            len = t4_tx_get_chunk(&t4_state, image, sizeof(image));
+            if (bad_rows)
+            {
+                span_log(&s->logging, SPAN_LOG_FLOW, "We need to corrupt the image\n");
+                corrupt_image(s, image, len, (const char *) bad_rows);
+            }
+            t4_tx_end(&t4_state);
+            faxtester_set_non_ecm_image_buffer(s, image, len);
+        }
+        else if (strcasecmp((const char *) type, "PP") == 0)
+        {
+            compression_step = 0;
+            if (t4_tx_init(&t4_state, (const char *) value, -1, -1) == NULL)
+            {
+                span_log(&s->logging, SPAN_LOG_FLOW, "Failed to init T.4 send\n");
+                exit(2);
+            }
+            t4_tx_set_min_row_bits(&t4_state, 0);
+            t4_tx_set_header_info(&t4_state, NULL);
+            switch (compression_step)
+            {
+            case 0:
+                compression = T4_COMPRESSION_ITU_T4_1D;
+                break;
+            case 1:
+                compression = T4_COMPRESSION_ITU_T4_2D;
+                break;
+            case 2:
+                compression = T4_COMPRESSION_ITU_T6;
+                break;
+            }
+            t4_tx_set_tx_encoding(&t4_state, compression);
+            if (t4_tx_start_page(&t4_state))
+            {
+                span_log(&s->logging, SPAN_LOG_FLOW, "Failed to start T.4 send\n");
+                exit(2);
+            }
+            /*endif*/
+            len = t4_tx_get_chunk(&t4_state, image, sizeof(image));
+            if (bad_rows)
+            {
+                span_log(&s->logging, SPAN_LOG_FLOW, "We need to corrupt the image\n");
+                corrupt_image(s, image, len, (const char *) bad_rows);
+            }
+            /*endif*/
+            t4_tx_end(&t4_state);
+            if (crc_error)
+                i = atoi((const char *) crc_error);
+            else
+                i = -1;
+            /*endif*/
+            faxtester_set_ecm_image_buffer(s, image, len, 64, i);
+        }
+        else
+        {
+            span_log(&s->logging, SPAN_LOG_FLOW, "Unrecognised type '%s'\n", (const char *) type);
+            return 0;
+        }
+        /*endif*/
+    }
+    /*endif*/
+    return 1;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void exchange(faxtester_state_t *s)
+{
+    int16_t amp[SAMPLES_PER_CHUNK];
+    int16_t out_amp[2*SAMPLES_PER_CHUNK];
+    int len;
+    int i;
+    int total_audio_time;
+    int log_audio;
+
+    log_audio = TRUE;
+    output_tiff_file_name = OUTPUT_TIFF_FILE_NAME;
+
+    if (log_audio)
+    {
+        if ((filesetup = afNewFileSetup()) == AF_NULL_FILESETUP)
+        {
+            fprintf(stderr, "    Failed to create file setup\n");
+            exit(2);
+        }
+        /*endif*/
+        afInitSampleFormat(filesetup, AF_DEFAULT_TRACK, AF_SAMPFMT_TWOSCOMP, 16);
+        afInitRate(filesetup, AF_DEFAULT_TRACK, (float) SAMPLE_RATE);
+        afInitFileFormat(filesetup, AF_FILE_WAVE);
+        afInitChannels(filesetup, AF_DEFAULT_TRACK, 2);
+
+        if ((out_handle = afOpenFile(OUTPUT_FILE_NAME_WAVE, "w", filesetup)) == AF_NULL_FILEHANDLE)
+        {
+            fprintf(stderr, "    Cannot create wave file '%s'\n", OUTPUT_FILE_NAME_WAVE);
+            exit(2);
+        }
+        /*endif*/
+    }
+    /*endif*/
+
+    total_audio_time = 0;
+
+    faxtester_set_transmit_on_idle(&state, TRUE);
+    faxtester_set_real_time_frame_handler(&state, faxtester_real_time_frame_handler, NULL);
+    faxtester_set_front_end_step_complete_handler(&state, faxtester_front_end_step_complete_handler, NULL);
+    faxtester_set_front_end_step_timeout_handler(&state, faxtester_front_end_step_timeout_handler, NULL);
+
+    fax_init(&fax, FALSE);
+    fax_prepare();
+
+    while (next_step(s) == 0)
+        ;
+    /*endwhile*/
+    for (;;)
+    {
+        len = fax_tx(&fax, amp, SAMPLES_PER_CHUNK);
+        faxtester_rx(s, amp, len);
+        if (log_audio)
+        {
+            for (i = 0;  i < len;  i++)
+                out_amp[2*i + 0] = amp[i];
+            /*endfor*/
+        }
+        /*endif*/
+
+        total_audio_time += SAMPLES_PER_CHUNK;
+        span_log_bump_samples(&fax.t30.logging, len);
+        span_log_bump_samples(&fax.modems.v27ter_rx.logging, len);
+        span_log_bump_samples(&fax.modems.v29_rx.logging, len);
+        span_log_bump_samples(&fax.modems.v17_rx.logging, len);
+        span_log_bump_samples(&fax.logging, len);
+        span_log_bump_samples(&s->logging, len);
+                
+        len = faxtester_tx(s, amp, 160);
+        if (fax_rx(&fax, amp, len))
+            break;
+        /*endif*/
+        if (log_audio)
+        {
+            for (i = 0;  i < len;  i++)
+                out_amp[2*i + 1] = amp[i];
+            /*endfor*/
+            if (afWriteFrames(out_handle, AF_DEFAULT_TRACK, out_amp, SAMPLES_PER_CHUNK) != SAMPLES_PER_CHUNK)
+                break;
+            /*endif*/
+        }
+        /*endif*/
+    }
+    /*endfor*/
+    if (log_audio)
+    {
+        if (afCloseFile(out_handle))
+        {
+            fprintf(stderr, "    Cannot close wave file '%s'\n", OUTPUT_FILE_NAME_WAVE);
+            exit(2);
+        }
+        /*endif*/
+        afFreeFileSetup(filesetup);
+    }
+    /*endif*/
+}
+/*- End of function --------------------------------------------------------*/
+
+static int parse_test_group(faxtester_state_t *s, xmlDocPtr doc, xmlNsPtr ns, xmlNodePtr cur, const char *test)
+{
+    xmlChar *x;
+
+    while (cur)
+    {
+        if (xmlStrcmp(cur->name, (const xmlChar *) "test") == 0)
+        {
+            if ((x = xmlGetProp(cur, (const xmlChar *) "name")))
+            {
+                if (xmlStrcmp(x, (const xmlChar *) test) == 0)
+                {
+                    span_log(&s->logging, SPAN_LOG_FLOW, "Found '%s'\n", (char *) x);
+                    s->cur = cur->xmlChildrenNode;
+                    return 0;
+                }
+                /*endif*/
+            }
+            /*endif*/
+        }
+        /*endif*/
+        cur = cur->next;
+    }
+    /*endwhile*/
+    return -1;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int get_test_set(faxtester_state_t *s, const char *test_file, const char *test)
+{
+    xmlDocPtr doc;
+    xmlNsPtr ns;
+    xmlNodePtr cur;
+#if 0
+    xmlValidCtxt valid;
+#endif
+
+    ns = NULL;    
+    xmlKeepBlanksDefault(0);
+    xmlCleanupParser();
+    doc = xmlParseFile(test_file);
+    if (doc == NULL)
+    {
+        span_log(&s->logging, SPAN_LOG_FLOW, "No document\n");
+        exit(2);
+    }
+    /*endif*/
+    xmlXIncludeProcess(doc);
+#if 0
+    if (!xmlValidateDocument(&valid, doc))
+    {
+        span_log(&s->logging, SPAN_LOG_FLOW, "Invalid document\n");
+        exit(2);
+    }
+    /*endif*/
+#endif
+    /* Check the document is of the right kind */
+    if ((cur = xmlDocGetRootElement(doc)) == NULL)
+    {
+        span_log(&s->logging, SPAN_LOG_FLOW, "Empty document\n");
+        xmlFreeDoc(doc);
+        exit(2);
+    }
+    /*endif*/
+    if (xmlStrcmp(cur->name, (const xmlChar *) "fax-tests"))
+    {
+        span_log(&s->logging, SPAN_LOG_FLOW, "Document of the wrong type, root node != fax-tests");
+        xmlFreeDoc(doc);
+        exit(2);
+    }
+    /*endif*/
+    cur = cur->xmlChildrenNode;
+    while (cur  &&  xmlIsBlankNode(cur))
+        cur = cur->next;
+    /*endwhile*/
+    if (cur == NULL)
+        exit(2);
+    /*endif*/
+    while (cur)
+    {
+        if (xmlStrcmp(cur->name, (const xmlChar *) "test-group") == 0)
+        {
+            if (parse_test_group(s, doc, ns, cur->xmlChildrenNode, test) == 0)
+            {
+                /* We found the test we want, so run it. */
+                exchange(s);
+                break;
+            }
+            /*endif*/
+        }
+        /*endif*/
+        cur = cur->next;
+    }
+    /*endwhile*/
+    xmlFreeDoc(doc);
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+int main(int argc, char *argv[])
+{
+    const char *test_name;
+
+    //string_test();
+
+    test_name = "MRGN01";
+    if (argc > 1)
+        test_name = argv[1];
+
+    faxtester_init(&state, TRUE);
+    span_log_set_level(&state.logging, SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_SHOW_TAG | SPAN_LOG_SHOW_SAMPLE_TIME | SPAN_LOG_FLOW);
+    span_log_set_tag(&state.logging, "B");
+    get_test_set(&state, "../spandsp/tsb85.xml", test_name);
+    printf("Done\n");
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/tests/tsb85_tests.sh
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/tests/tsb85_tests.sh	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,104 @@
+#!/bin/bash
+#
+# spandsp fax tests
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU Lesser General Public License version 2.1,
+# as published by the Free Software Foundation.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this program; if not, write to the Free Software
+# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+#
+# $Id: tsb85_tests.sh,v 1.3 2008/08/04 14:03:17 steveu Exp $
+#
+
+run_tsb85_test()
+{
+    rm -f fax_tests_1.tif
+    echo ./tsb85_tests ${TEST}
+    ./tsb85_tests ${TEST} 2>xyzzy2
+    RETVAL=$?
+    if [ $RETVAL != 0 ]
+    then
+        echo tsb85_tests ${TEST} failed!
+        exit $RETVAL
+    fi
+}
+
+for TEST in MRGN01 MRGN02 MRGN03 MRGN04 MRGN05 MRGN06a MRGN06b MRGN07 MRGN08 ; do
+    run_tsb85_test
+done
+
+#for TEST in MRGN09 MRGN10 MRGN11 MRGN12 MRGN13 MRGN14 MRGN15 MRGN16 MRGN17 ; do
+for TEST in MRGN09 MRGN10 MRGN11 MRGN12 MRGN13 MRGN15 MRGN17 ; do
+    run_tsb85_test
+done
+
+for TEST in ORGC01 ORGC02 ORGC03 ; do
+    run_tsb85_test
+done
+
+for TEST in OREN01 OREN02 OREN03 OREN04 OREN05 OREN06 OREN07 OREN08 OREN09 OREN10 ; do
+    run_tsb85_test
+done
+
+#for TEST in MRGX01 MRGX02 MRGX03 MRGX04 MRGX05 MRGX06 MRGX07 MRGX08 ; do
+for TEST in MRGX02 MRGX04 MRGX06 MRGX07 MRGX08 ; do
+    run_tsb85_test
+done
+
+#for TEST in MRGX09 MRGX10 MRGX11 MRGX12 MRGX13 MRGX14 MRGX15 ; do
+for TEST in MRGX09 MRGX10 MRGX11 ; do
+    run_tsb85_test
+done
+
+#for TEST in MTGP01 MTGP02 OTGP03 ; do
+#    run_tsb85_test
+#done
+
+#for TEST in MTGN01 MTGN02 MTGN03 MTGN04 MTGN05 MTGN06 MTGN07 MTGN08 MTGN09 MTGN10 ; do
+#    run_tsb85_test
+#done
+
+#for TEST in MTGN11 MTGN12 MTGN13 MTGN14 MTGN15 MTGN16 MTGN17 MTGN18 MTGN19 MTGN20 ; do
+#    run_tsb85_test
+#done
+
+#for TEST in MTGN21 MTGN22 MTGN23 MTGN24 MTGN25 MTGN26 MTGN27 MTGN28 ; do
+#    run_tsb85_test
+#done
+
+#for TEST in OTGC01 OTGC02 OTGC03 OTGC04 OTGC05 OTGC06 OTGC07 OTGC08 OTGC09 OTGC10 OTGC11 ; do
+#    run_tsb85_test
+#done
+
+#for TEST in OTEN01 OTEN02 OTEN03 OTEN04 OTEN05 OTEN06 ; do
+#    run_tsb85_test
+#done
+
+#for TEST in MTGX01 MTGX02 MTGX03 MTGX04 MTGX05 MTGX06 MTGX07 MTGX08 ; do
+#    run_tsb85_test
+#done
+
+#for TEST in MTGX09 MTGX10 MTGX11 MTGX12 MTGX13 MTGX14 MTGX15 MTGX16 ; do
+#    run_tsb85_test
+#done
+
+#for TEST in MTGX17 MTGX18 MTGX19 MTGX20 MTGX21 MTGX22 MTGX23 ; do
+#    run_tsb85_test
+#done
+
+#for TEST in MRGP01 MRGP02 MRGP03 MRGP04 MRGP05 MRGP06 MRGP07 MRGP08 ; do
+for TEST in MRGP03 MRGP04 MRGP05 ; do
+    run_tsb85_test
+done
+
+#for TEST in ORGP09 ORGP10 ; do
+#    run_tsb85_test
+#done

Added: freeswitch/trunk/libs/spandsp/tests/v17_tests.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/tests/v17_tests.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,546 @@
+//#define ADD_MAINS_INTERFERENCE
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * v17_tests.c
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2004 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: v17_tests.c,v 1.87 2008/08/29 09:28:13 steveu Exp $
+ */
+
+/*! \page v17_tests_page V.17 modem tests
+\section v17_tests_page_sec_1 What does it do?
+These tests test one way paths, as V.17 is a half-duplex modem. They allow either:
+
+ - A V.17 transmit modem to feed a V.17 receive modem through a telephone line
+   model. BER testing is then used to evaluate performance under various line
+   conditions. This is effective for testing the basic performance of the
+   receive modem. It is also the only test mode provided for evaluating the
+   transmit modem.
+
+ - A V.17 receive modem is used to decode V.17 audio, stored in a wave file.
+   This is good way to evaluate performance with audio recorded from other
+   models of modem, and with real world problematic telephone lines.
+
+If the appropriate GUI environment exists, the tests are built such that a visual
+display of modem status is maintained.
+
+\section v17_tests_page_sec_2 How is it used?
+*/
+
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
+
+#if defined(HAVE_FL_FL_H)  &&  defined(HAVE_FL_FL_CARTESIAN_H)  &&  defined(HAVE_FL_FL_AUDIO_METER_H)
+#define ENABLE_GUI
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <string.h>
+#include <audiofile.h>
+
+#include "spandsp.h"
+#include "spandsp-sim.h"
+
+#if defined(ENABLE_GUI)
+#include "modem_monitor.h"
+#include "line_model_monitor.h"
+#endif
+
+#define BLOCK_LEN       160
+
+#define OUT_FILE_NAME   "v17.wav"
+
+char *decode_test_file = NULL;
+int use_gui = FALSE;
+
+int symbol_no = 0;
+
+int rx_bits = 0;
+int tx_bits = 0;
+
+int test_bps;
+
+bert_state_t bert;
+one_way_line_model_state_t *line_model;
+
+#if defined(ENABLE_GUI)
+qam_monitor_t *qam_monitor;
+#endif
+
+bert_results_t latest_results;
+
+static void reporter(void *user_data, int reason, bert_results_t *results)
+{
+    switch (reason)
+    {
+    case BERT_REPORT_SYNCED:
+        printf("BERT report synced\n");
+        break;
+    case BERT_REPORT_UNSYNCED:
+        printf("BERT report unsync'ed\n");
+        break;
+    case BERT_REPORT_REGULAR:
+        printf("BERT report regular - %d bits, %d bad bits, %d resyncs\n", results->total_bits, results->bad_bits, results->resyncs);
+        memcpy(&latest_results, results, sizeof(latest_results));
+        break;
+    case BERT_REPORT_GT_10_2:
+        printf("BERT report > 1 in 10^2\n");
+        break;
+    case BERT_REPORT_LT_10_2:
+        printf("BERT report < 1 in 10^2\n");
+        break;
+    case BERT_REPORT_LT_10_3:
+        printf("BERT report < 1 in 10^3\n");
+        break;
+    case BERT_REPORT_LT_10_4:
+        printf("BERT report < 1 in 10^4\n");
+        break;
+    case BERT_REPORT_LT_10_5:
+        printf("BERT report < 1 in 10^5\n");
+        break;
+    case BERT_REPORT_LT_10_6:
+        printf("BERT report < 1 in 10^6\n");
+        break;
+    case BERT_REPORT_LT_10_7:
+        printf("BERT report < 1 in 10^7\n");
+        break;
+    default:
+        printf("BERT report reason %d\n", reason);
+        break;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static int v17_rx_status(void *user_data, int status)
+{
+    v17_rx_state_t *rx;
+    int i;
+    int len;
+    complexf_t *coeffs;
+    
+    printf("V.17 rx status is %d\n", status);
+    rx = (v17_rx_state_t *) user_data;
+    switch (status)
+    {
+    case PUTBIT_TRAINING_FAILED:
+        printf("Training failed\n");
+        break;
+    case PUTBIT_TRAINING_IN_PROGRESS:
+        printf("Training in progress\n");
+        break;
+    case PUTBIT_TRAINING_SUCCEEDED:
+        printf("Training succeeded\n");
+        len = v17_rx_equalizer_state(rx, &coeffs);
+        printf("Equalizer:\n");
+        for (i = 0;  i < len;  i++)
+            printf("%3d (%15.5f, %15.5f) -> %15.5f\n", i, coeffs[i].re, coeffs[i].im, powerf(&coeffs[i]));
+        break;
+    case PUTBIT_CARRIER_UP:
+        printf("Carrier up\n");
+        break;
+    case PUTBIT_CARRIER_DOWN:
+        printf("Carrier down\n");
+        break;
+    default:
+        printf("Eh! - %d\n", status);
+        break;
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void v17putbit(void *user_data, int bit)
+{
+    v17_rx_state_t *rx;
+
+    if (bit < 0)
+    {
+        v17_rx_status(user_data, bit);
+        return;
+    }
+
+    rx = (v17_rx_state_t *) user_data;
+    if (decode_test_file)
+        printf("Rx bit %d - %d\n", rx_bits++, bit);
+    else
+        bert_put_bit(&bert, bit);
+}
+/*- End of function --------------------------------------------------------*/
+
+static int v17_tx_status(void *user_data, int status)
+{
+    switch (status)
+    {
+    case MODEM_TX_STATUS_DATA_EXHAUSTED:
+        printf("V.17 tx data exhausted\n");
+        break;
+    case MODEM_TX_STATUS_SHUTDOWN_COMPLETE:
+        printf("V.17 tx shutdown complete\n");
+        break;
+    default:
+        printf("V.17 tx status is %d\n", status);
+        break;
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int v17getbit(void *user_data)
+{
+    return bert_get_bit(&bert);
+}
+/*- End of function --------------------------------------------------------*/
+
+static void qam_report(void *user_data, const complexf_t *constel, const complexf_t *target, int symbol)
+{
+    int i;
+    int len;
+    complexf_t *coeffs;
+    float fpower;
+    v17_rx_state_t *rx;
+    static float smooth_power = 0.0f;
+    static int update_interval = 100;
+
+    rx = (v17_rx_state_t *) user_data;
+    if (constel)
+    {
+#if defined(ENABLE_GUI)
+        if (use_gui)
+        {
+            qam_monitor_update_constel(qam_monitor, constel);
+            qam_monitor_update_carrier_tracking(qam_monitor, v17_rx_carrier_frequency(rx));
+            qam_monitor_update_symbol_tracking(qam_monitor, v17_rx_symbol_timing_correction(rx));
+        }
+#endif
+        fpower = (constel->re - target->re)*(constel->re - target->re)
+               + (constel->im - target->im)*(constel->im - target->im);
+        smooth_power = 0.95f*smooth_power + 0.05f*fpower;
+        printf("%8d [%8.4f, %8.4f] [%8.4f, %8.4f] %2x %8.4f %8.4f %9.4f %7.3f %7.2f\n",
+               symbol_no,
+               constel->re,
+               constel->im,
+               target->re,
+               target->im,
+               symbol,
+               fpower,
+               smooth_power,
+               v17_rx_carrier_frequency(rx),
+               v17_rx_signal_power(rx),
+               v17_rx_symbol_timing_correction(rx));
+        printf("Carrier %d %f %f\n", symbol_no, v17_rx_carrier_frequency(rx), v17_rx_symbol_timing_correction(rx));
+        symbol_no++;
+        if (--update_interval <= 0)
+        {
+            len = v17_rx_equalizer_state(rx, &coeffs);
+            printf("Equalizer A:\n");
+            for (i = 0;  i < len;  i++)
+                printf("%3d (%15.5f, %15.5f) -> %15.5f\n", i, coeffs[i].re, coeffs[i].im, powerf(&coeffs[i]));
+#if defined(ENABLE_GUI)
+            if (use_gui)
+                qam_monitor_update_equalizer(qam_monitor, coeffs, len);
+#endif
+            update_interval = 100;
+        }
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+int main(int argc, char *argv[])
+{
+    v17_rx_state_t rx;
+    v17_tx_state_t tx;
+    bert_results_t bert_results;
+    int16_t gen_amp[BLOCK_LEN];
+    int16_t amp[BLOCK_LEN];
+    AFfilehandle inhandle;
+    AFfilehandle outhandle;
+    int outframes;
+    int samples;
+    int tep;
+    int block_no;
+    int noise_level;
+    int signal_level;
+    int bits_per_test;
+    int line_model_no;
+    int log_audio;
+    int channel_codec;
+    int rbs_pattern;
+    int opt;
+
+    channel_codec = MUNGE_CODEC_NONE;
+    rbs_pattern = 0;
+    test_bps = 14400;
+    tep = FALSE;
+    line_model_no = 0;
+    decode_test_file = NULL;
+    use_gui = FALSE;
+    noise_level = -70;
+    signal_level = -13;
+    bits_per_test = 50000;
+    log_audio = FALSE;
+    while ((opt = getopt(argc, argv, "b:c:d:glm:n:r:s:t")) != -1)
+    {
+        switch (opt)
+        {
+        case 'b':
+            bits_per_test = atoi(optarg);
+            break;
+        case 'c':
+            channel_codec = atoi(optarg);
+            break;
+        case 'd':
+            decode_test_file = optarg;
+            break;
+        case 'g':
+#if defined(ENABLE_GUI)
+            use_gui = TRUE;
+#else
+            fprintf(stderr, "Graphical monitoring not available\n");
+            exit(2);
+#endif
+            break;
+        case 'l':
+            log_audio = TRUE;
+            break;
+        case 'm':
+            line_model_no = atoi(optarg);
+            break;
+        case 'n':
+            noise_level = atoi(optarg);
+            break;
+        case 'r':
+            rbs_pattern = atoi(optarg);
+            break;
+        case 's':
+            signal_level = atoi(optarg);
+            break;
+        case 't':
+            tep = TRUE;
+            break;
+        default:
+            //usage();
+            exit(2);
+            break;
+        }
+    }
+    argc -= optind;
+    argv += optind;
+    if (argc > 0)
+    {
+        if (strcmp(argv[0], "14400") == 0)
+            test_bps = 14400;
+        else if (strcmp(argv[0], "12000") == 0)
+            test_bps = 12000;
+        else if (strcmp(argv[0], "9600") == 0)
+            test_bps = 9600;
+        else if (strcmp(argv[0], "7200") == 0)
+            test_bps = 7200;
+        else
+        {
+            fprintf(stderr, "Invalid bit rate\n");
+            exit(2);
+        }
+    }
+    inhandle = NULL;
+    outhandle = NULL;
+
+    if (log_audio)
+    {
+        if ((outhandle = afOpenFile_telephony_write(OUT_FILE_NAME, 1)) == AF_NULL_FILEHANDLE)
+        {
+            fprintf(stderr, "    Cannot create wave file '%s'\n", OUT_FILE_NAME);
+            exit(2);
+        }
+    }
+
+    if (decode_test_file)
+    {
+        /* We will decode the audio from a wave file. */
+        if ((inhandle = afOpenFile_telephony_read(decode_test_file, 1)) == AF_NULL_FILEHANDLE)
+        {
+            fprintf(stderr, "    Cannot open wave file '%s'\n", decode_test_file);
+            exit(2);
+        }
+    }
+    else
+    {
+        /* We will generate V.17 audio, and add some noise to it. */
+        v17_tx_init(&tx, test_bps, tep, v17getbit, NULL);
+        v17_tx_power(&tx, signal_level);
+        v17_tx_set_modem_status_handler(&tx, v17_tx_status, (void *) &tx);
+        /* Move the carrier off a bit */
+        tx.carrier_phase_rate = dds_phase_ratef(1792.0f);
+        tx.carrier_phase = 0x40000000;
+
+        bert_init(&bert, bits_per_test, BERT_PATTERN_ITU_O152_11, test_bps, 20);
+        bert_set_report(&bert, 10000, reporter, NULL);
+
+        if ((line_model = one_way_line_model_init(line_model_no, (float) noise_level, channel_codec, rbs_pattern)) == NULL)
+        {
+            fprintf(stderr, "    Failed to create line model\n");
+            exit(2);
+        }
+        one_way_line_model_set_dc(line_model, 0.0f);
+#if defined(ADD_MAINS_INTERFERENCE)
+        one_way_line_model_set_mains_pickup(line_model, 50, -40.0f);
+#endif
+    }
+
+    v17_rx_init(&rx, test_bps, v17putbit, &rx);
+    v17_rx_set_modem_status_handler(&rx, v17_rx_status, (void *) &rx);
+    v17_rx_set_qam_report_handler(&rx, qam_report, (void *) &rx);
+    span_log_set_level(&rx.logging, SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_FLOW);
+    span_log_set_tag(&rx.logging, "V.17 rx");
+
+#if defined(ENABLE_GUI)
+    if (use_gui)
+    {
+        qam_monitor = qam_monitor_init(10.0f, NULL);
+        if (!decode_test_file)
+        {
+            start_line_model_monitor(129);
+            line_model_monitor_line_model_update(line_model->near_filter, line_model->near_filter_len);
+        }
+    }
+#endif
+
+    memset(&latest_results, 0, sizeof(latest_results));
+    for (block_no = 0;  block_no < 100000000;  block_no++)
+    {
+        if (decode_test_file)
+        {
+            samples = afReadFrames(inhandle,
+                                   AF_DEFAULT_TRACK,
+                                   amp,
+                                   BLOCK_LEN);
+#if defined(ENABLE_GUI)
+            if (use_gui)
+                qam_monitor_update_audio_level(qam_monitor, amp, samples);
+#endif
+            if (samples == 0)
+                break;
+        }
+        else
+        {
+            samples = v17_tx(&tx, gen_amp, BLOCK_LEN);
+#if defined(ENABLE_GUI)
+            if (use_gui)
+                qam_monitor_update_audio_level(qam_monitor, gen_amp, samples);
+#endif
+            if (samples == 0)
+            {
+                printf("Restarting on zero output\n");
+
+                /* Push a little silence through, to ensure all the data bits get out of the buffers */
+                memset(amp, 0, BLOCK_LEN*sizeof(int16_t));
+                v17_rx(&rx, amp, BLOCK_LEN);
+
+                /* Note that we might get a few bad bits as the carrier shuts down. */
+                bert_result(&bert, &bert_results);
+                fprintf(stderr, "Final result %ddBm0, %d bits, %d bad bits, %d resyncs\n", signal_level, bert_results.total_bits, bert_results.bad_bits, bert_results.resyncs);
+                fprintf(stderr, "Last report  %ddBm0, %d bits, %d bad bits, %d resyncs\n", signal_level, latest_results.total_bits, latest_results.bad_bits, latest_results.resyncs);
+                /* See if bit errors are appearing yet. Also check we are getting enough bits out of the receiver. The last regular report
+                   should be error free, though the final report will generally contain bits errors as the carrier was dying. The total
+                   number of bits out of the receiver should be at least the number we sent. Also, since BERT sync should have occurred
+                   rapidly at the start of transmission, the last report should have occurred at not much less than the total number of
+                   bits we sent. */
+                if (bert_results.total_bits < bits_per_test
+                    ||
+                    latest_results.total_bits < bits_per_test - 100
+                    ||
+                    latest_results.bad_bits != 0)
+                {
+                    break;
+                }
+                memset(&latest_results, 0, sizeof(latest_results));
+                signal_level--;
+                /* Bump the receiver AGC gain by 1dB, to compensate for the above */
+                rx.agc_scaling_save *= 1.122f;
+                v17_tx_restart(&tx, test_bps, tep, TRUE);
+                v17_tx_power(&tx, signal_level);
+                v17_rx_restart(&rx, test_bps, TRUE);
+                //rx.eq_put_step = rand()%(192*10/3);
+                bert_init(&bert, bits_per_test, BERT_PATTERN_ITU_O152_11, test_bps, 20);
+                bert_set_report(&bert, 10000, reporter, NULL);
+                one_way_line_model_release(line_model);
+                if ((line_model = one_way_line_model_init(line_model_no, (float) noise_level, channel_codec, 0)) == NULL)
+                {
+                    fprintf(stderr, "    Failed to create line model\n");
+                    exit(2);
+                }
+            }
+            if (log_audio)
+            {
+                outframes = afWriteFrames(outhandle,
+                                          AF_DEFAULT_TRACK,
+                                          amp,
+                                          samples);
+                if (outframes != samples)
+                {
+                    fprintf(stderr, "    Error writing wave file\n");
+                    exit(2);
+                }
+            }
+            one_way_line_model(line_model, amp, gen_amp, samples);
+        }
+#if defined(ENABLE_GUI)
+        if (use_gui  &&  !decode_test_file)
+            line_model_monitor_line_spectrum_update(amp, samples);
+#endif
+        v17_rx(&rx, amp, samples);
+    }
+    if (!decode_test_file)
+    {
+        bert_result(&bert, &bert_results);
+        fprintf(stderr, "At completion:\n");
+        fprintf(stderr, "Final result %ddBm0, %d bits, %d bad bits, %d resyncs\n", signal_level, bert_results.total_bits, bert_results.bad_bits, bert_results.resyncs);
+        fprintf(stderr, "Last report  %ddBm0, %d bits, %d bad bits, %d resyncs\n", signal_level, latest_results.total_bits, latest_results.bad_bits, latest_results.resyncs);
+        one_way_line_model_release(line_model);
+
+        if (signal_level > -43)
+        {
+            printf("Tests failed.\n");
+            exit(2);
+        }
+
+        printf("Tests passed.\n");
+    }
+#if defined(ENABLE_GUI)
+    if (use_gui)
+        qam_wait_to_end(qam_monitor);
+#endif
+    if (log_audio)
+    {
+        if (afCloseFile(outhandle))
+        {
+            fprintf(stderr, "    Cannot close wave file '%s'\n", OUT_FILE_NAME);
+            exit(2);
+        }
+    }
+    return  0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/tests/v22bis_tests.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/tests/v22bis_tests.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,415 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * v22bis_tests.c
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2004 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: v22bis_tests.c,v 1.51 2008/08/16 14:59:50 steveu Exp $
+ */
+
+/*! \page v22bis_tests_page V.22bis modem tests
+\section v22bis_tests_page_sec_1 What does it do?
+These tests connect two V.22bis modems back to back, through a telephone line
+model. BER testing is then used to evaluate performance under various line
+conditions.
+
+If the appropriate GUI environment exists, the tests are built such that a visual
+display of modem status is maintained.
+
+\section v22bis_tests_page_sec_2 How is it used?
+*/
+
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
+
+#if defined(HAVE_FL_FL_H)  &&  defined(HAVE_FL_FL_CARTESIAN_H)  &&  defined(HAVE_FL_FL_AUDIO_METER_H)
+#define ENABLE_GUI
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <string.h>
+#include <audiofile.h>
+
+#include "spandsp.h"
+#include "spandsp-sim.h"
+
+#if defined(ENABLE_GUI)
+#include "modem_monitor.h"
+#endif
+
+#define BLOCK_LEN       160
+
+#define IN_FILE_NAME    "v22bis_samp.wav"
+#define OUT_FILE_NAME   "v22bis.wav"
+
+int in_bit = 0;
+int out_bit = 0;
+
+int in_bit_no = 0;
+int out_bit_no = 0;
+
+uint8_t tx_buf[1000];
+int rx_ptr = 0;
+int tx_ptr = 0;
+
+int rx_bits = 0;
+int rx_bad_bits = 0;
+
+int use_gui = FALSE;
+
+both_ways_line_model_state_t *model;
+
+v22bis_state_t caller;
+v22bis_state_t answerer;
+
+struct qam_report_control_s
+{
+    v22bis_state_t *s;
+#if defined(ENABLE_GUI)
+    qam_monitor_t *qam_monitor;
+#endif
+    float smooth_power;
+    int symbol_no;
+};
+
+struct qam_report_control_s qam_caller;
+struct qam_report_control_s qam_answerer;
+
+static void v22bis_putbit(void *user_data, int bit)
+{
+    v22bis_state_t *s;
+    int i;
+    int len;
+    complexf_t *coeffs;
+    
+    s = (v22bis_state_t *) user_data;
+    if (bit < 0)
+    {
+        /* Special conditions */
+        switch (bit)
+        {
+        case PUTBIT_TRAINING_FAILED:
+            printf("Training failed\n");
+            break;
+        case PUTBIT_TRAINING_IN_PROGRESS:
+            printf("Training in progress\n");
+            break;
+        case PUTBIT_TRAINING_SUCCEEDED:
+            printf("Training succeeded\n");
+            len = v22bis_equalizer_state(s, &coeffs);
+            printf("Equalizer:\n");
+            for (i = 0;  i < len;  i++)
+                printf("%3d (%15.5f, %15.5f) -> %15.5f\n", i, coeffs[i].re, coeffs[i].im, powerf(&coeffs[i]));
+            break;
+        case PUTBIT_CARRIER_UP:
+            printf("Carrier up\n");
+            break;
+        case PUTBIT_CARRIER_DOWN:
+            printf("Carrier down\n");
+            break;
+        default:
+            printf("Eh! - %d\n", bit);
+            break;
+        }
+        return;
+    }
+
+    if (bit != tx_buf[rx_ptr])
+    {
+        printf("Rx bit %d - %d\n", rx_bits, bit);
+        rx_bad_bits++;
+    }
+    rx_ptr++;
+    if (rx_ptr > 1000)
+        rx_ptr = 0;
+    rx_bits++;
+    if ((rx_bits % 100000) == 0)
+    {
+        printf("%d bits received, %d bad bits\r", rx_bits, rx_bad_bits);
+        fflush(stdout);
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static int v22bis_getbit(void *user_data)
+{
+    int bit;
+    static int tx_bits = 0;
+
+    bit = rand() & 1;
+    tx_buf[tx_ptr++] = bit;
+    if (tx_ptr > 1000)
+        tx_ptr = 0;
+    //printf("Tx bit %d\n", bit);
+    if (++tx_bits > 100000)
+    {
+        tx_bits = 0;
+        bit = 2;
+    }
+    return bit;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void qam_report(void *user_data, const complexf_t *constel, const complexf_t *target, int symbol)
+{
+    int i;
+    int len;
+    complexf_t *coeffs;
+    float fpower;
+    struct qam_report_control_s *s;
+
+    s = (struct qam_report_control_s *) user_data;
+    if (constel)
+    {
+#if defined(ENABLE_GUI)
+        if (use_gui)
+        {
+            qam_monitor_update_constel(s->qam_monitor, constel);
+            qam_monitor_update_carrier_tracking(s->qam_monitor, v22bis_rx_carrier_frequency(s->s));
+            qam_monitor_update_symbol_tracking(s->qam_monitor, v22bis_symbol_timing_correction(s->s));
+        }
+#endif
+        fpower = (constel->re - target->re)*(constel->re - target->re)
+               + (constel->im - target->im)*(constel->im - target->im);
+        s->smooth_power = 0.95f*s->smooth_power + 0.05f*fpower;
+        printf("%8d [%8.4f, %8.4f] [%8.4f, %8.4f] %2x %8.4f %8.4f\n",
+               s->symbol_no,
+               constel->re,
+               constel->im,
+               target->re,
+               target->im,
+               symbol,
+               fpower,
+               s->smooth_power);
+        s->symbol_no++;
+    }
+    else
+    {
+        printf("Gardner step %d\n", symbol);
+        len = v22bis_equalizer_state(s->s, &coeffs);
+        printf("Equalizer A:\n");
+        for (i = 0;  i < len;  i++)
+            printf("%3d (%15.5f, %15.5f) -> %15.5f\n", i, coeffs[i].re, coeffs[i].im, powerf(&coeffs[i]));
+#if defined(ENABLE_GUI)
+        if (use_gui)
+            qam_monitor_update_equalizer(s->qam_monitor, coeffs, len);
+#endif
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+int main(int argc, char *argv[])
+{
+    int16_t caller_amp[BLOCK_LEN];
+    int16_t answerer_amp[BLOCK_LEN];
+    int16_t caller_model_amp[BLOCK_LEN];
+    int16_t answerer_model_amp[BLOCK_LEN];
+    int16_t out_amp[2*BLOCK_LEN];
+    AFfilehandle outhandle;
+    int outframes;
+    int samples;
+    int i;
+    int test_bps;
+    int line_model_no;
+    int bits_per_test;
+    int noise_level;
+    int signal_level;
+    int log_audio;
+    int channel_codec;
+    int opt;
+    
+    channel_codec = MUNGE_CODEC_NONE;
+    test_bps = 2400;
+    line_model_no = 0;
+    noise_level = -70;
+    signal_level = -13;
+    bits_per_test = 50000;
+    log_audio = FALSE;
+    while ((opt = getopt(argc, argv, "b:c:glm:n:s:")) != -1)
+    {
+        switch (opt)
+        {
+        case 'b':
+            bits_per_test = atoi(optarg);
+            break;
+        case 'c':
+            channel_codec = atoi(optarg);
+            break;
+        case 'g':
+#if defined(ENABLE_GUI)
+            use_gui = TRUE;
+#else
+            fprintf(stderr, "Graphical monitoring not available\n");
+            exit(2);
+#endif
+            break;
+        case 'l':
+            log_audio = TRUE;
+            break;
+        case 'm':
+            line_model_no = atoi(optarg);
+            break;
+        case 'n':
+            noise_level = atoi(optarg);
+            break;
+        case 's':
+            signal_level = atoi(optarg);
+            break;
+        default:
+            //usage();
+            exit(2);
+            break;
+        }
+    }
+    argc -= optind;
+    argv += optind;
+    if (argc > 0)
+    {
+        if (strcmp(argv[0], "2400") == 0)
+            test_bps = 2400;
+        else if (strcmp(argv[0], "1200") == 0)
+            test_bps = 1200;
+        else
+        {
+            fprintf(stderr, "Invalid bit rate\n");
+            exit(2);
+        }
+    }
+    outhandle = AF_NULL_FILEHANDLE;
+    if (log_audio)
+    {
+        if ((outhandle = afOpenFile_telephony_write(OUT_FILE_NAME, 2)) == AF_NULL_FILEHANDLE)
+        {
+            fprintf(stderr, "    Cannot create wave file '%s'\n", OUT_FILE_NAME);
+            exit(2);
+        }
+    }
+    v22bis_init(&caller, test_bps, 2, TRUE, v22bis_getbit, v22bis_putbit, &caller);
+    v22bis_tx_power(&caller, signal_level);
+    /* Move the carrier off a bit */
+    caller.tx.carrier_phase_rate = dds_phase_ratef(1207.0f);
+    v22bis_init(&answerer, test_bps, 2, FALSE, v22bis_getbit, v22bis_putbit, &answerer);
+    v22bis_tx_power(&answerer, signal_level);
+    answerer.tx.carrier_phase_rate = dds_phase_ratef(2407.0f);
+    v22bis_set_qam_report_handler(&caller, qam_report, (void *) &qam_caller);
+    v22bis_set_qam_report_handler(&answerer, qam_report, (void *) &qam_answerer);
+    span_log_set_level(&caller.logging, SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_SHOW_TAG | SPAN_LOG_FLOW);
+    span_log_set_tag(&caller.logging, "caller");
+    span_log_set_level(&answerer.logging, SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_SHOW_TAG | SPAN_LOG_FLOW);
+    span_log_set_tag(&answerer.logging, "answerer");
+
+    qam_caller.s = &caller;
+    qam_caller.smooth_power = 0.0f;
+    qam_caller.symbol_no = 0;
+
+    qam_answerer.s = &answerer;
+    qam_answerer.smooth_power = 0.0f;
+    qam_answerer.symbol_no = 0;
+
+#if defined(ENABLE_GUI)
+    if (use_gui)
+    {
+        qam_caller.qam_monitor = qam_monitor_init(6.0f, "Calling modem");
+        qam_answerer.qam_monitor = qam_monitor_init(6.0f, "Answering modem");
+    }
+#endif
+
+    if ((model = both_ways_line_model_init(line_model_no, (float) noise_level, line_model_no, (float) noise_level, channel_codec, 0)) == NULL)
+    {
+        fprintf(stderr, "    Failed to create line model\n");
+        exit(2);
+    }
+    for (;;)
+    {
+        samples = v22bis_tx(&caller, caller_amp, BLOCK_LEN);
+#if defined(ENABLE_GUI)
+        if (use_gui)
+            qam_monitor_update_audio_level(qam_caller.qam_monitor, caller_amp, samples);
+#endif
+        if (samples == 0)
+        {
+            printf("Restarting on zero output\n");
+            v22bis_restart(&caller, test_bps);
+            rx_ptr = 0;
+            tx_ptr = 0;
+        }
+
+        samples = v22bis_tx(&answerer, answerer_amp, BLOCK_LEN);
+#if defined(ENABLE_GUI)
+        if (use_gui)
+            qam_monitor_update_audio_level(qam_answerer.qam_monitor, answerer_amp, samples);
+#endif
+        if (samples == 0)
+        {
+            printf("Restarting on zero output\n");
+            v22bis_restart(&answerer, test_bps);
+            rx_ptr = 0;
+            tx_ptr = 0;
+        }
+
+        both_ways_line_model(model, 
+                             caller_model_amp,
+                             caller_amp,
+                             answerer_model_amp,
+                             answerer_amp,
+                             samples);
+
+        v22bis_rx(&answerer, caller_model_amp, samples);
+        for (i = 0;  i < samples;  i++)
+            out_amp[2*i] = caller_model_amp[i];
+        for (  ;  i < BLOCK_LEN;  i++)
+            out_amp[2*i] = 0;
+
+        v22bis_rx(&caller, answerer_model_amp, samples);
+        for (i = 0;  i < samples;  i++)
+            out_amp[2*i + 1] = answerer_model_amp[i];
+        for (  ;  i < BLOCK_LEN;  i++)
+            out_amp[2*i + 1] = 0;
+
+        if (log_audio)
+        {
+            outframes = afWriteFrames(outhandle,
+                                      AF_DEFAULT_TRACK,
+                                      out_amp,
+                                      BLOCK_LEN);
+            if (outframes != BLOCK_LEN)
+            {
+                fprintf(stderr, "    Error writing wave file\n");
+                exit(2);
+            }
+        }
+    }
+    if (log_audio)
+    {
+        if (afCloseFile(outhandle) != 0)
+        {
+            fprintf(stderr, "    Cannot close wave file '%s'\n", OUT_FILE_NAME);
+            exit(2);
+        }
+    }
+    return  0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/tests/v27ter_tests.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/tests/v27ter_tests.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,542 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * v27ter_tests.c
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2003 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: v27ter_tests.c,v 1.91 2008/08/29 09:28:13 steveu Exp $
+ */
+
+/*! \page v27ter_tests_page V.27ter modem tests
+\section v27ter_tests_page_sec_1 What does it do?
+These tests test one way paths, as V.27ter is a half-duplex modem. They allow either:
+
+ - A V.27ter transmit modem to feed a V.27ter receive modem through a telephone line
+   model. BER testing is then used to evaluate performance under various line
+   conditions. This is effective for testing the basic performance of the
+   receive modem. It is also the only test mode provided for evaluating the
+   transmit modem.
+
+ - A V.27ter receive modem is used to decode V.27ter audio, stored in a wave file.
+   This is good way to evaluate performance with audio recorded from other
+   models of modem, and with real world problematic telephone lines.
+
+If the appropriate GUI environment exists, the tests are built such that a visual
+display of modem status is maintained.
+
+\section v27ter_tests_page_sec_2 How is it used?
+*/
+
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
+
+#if defined(HAVE_FL_FL_H)  &&  defined(HAVE_FL_FL_CARTESIAN_H)  &&  defined(HAVE_FL_FL_AUDIO_METER_H)
+#define ENABLE_GUI
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <string.h>
+#include <audiofile.h>
+
+#include "spandsp.h"
+#include "spandsp-sim.h"
+
+#if defined(ENABLE_GUI)
+#include "modem_monitor.h"
+#include "line_model_monitor.h"
+#endif
+
+#define BLOCK_LEN       160
+
+#define OUT_FILE_NAME   "v27ter.wav"
+
+char *decode_test_file = NULL;
+int use_gui = FALSE;
+
+int symbol_no = 0;
+
+int rx_bits = 0;
+
+bert_state_t bert;
+one_way_line_model_state_t *line_model;
+
+#if defined(ENABLE_GUI)
+qam_monitor_t *qam_monitor;
+#endif
+
+bert_results_t latest_results;
+
+static void reporter(void *user_data, int reason, bert_results_t *results)
+{
+    switch (reason)
+    {
+    case BERT_REPORT_SYNCED:
+        printf("BERT report synced\n");
+        break;
+    case BERT_REPORT_UNSYNCED:
+        printf("BERT report unsync'ed\n");
+        break;
+    case BERT_REPORT_REGULAR:
+        printf("BERT report regular - %d bits, %d bad bits, %d resyncs\n", results->total_bits, results->bad_bits, results->resyncs);
+        memcpy(&latest_results, results, sizeof(latest_results));
+        break;
+    case BERT_REPORT_GT_10_2:
+        printf("BERT report > 1 in 10^2\n");
+        break;
+    case BERT_REPORT_LT_10_2:
+        printf("BERT report < 1 in 10^2\n");
+        break;
+    case BERT_REPORT_LT_10_3:
+        printf("BERT report < 1 in 10^3\n");
+        break;
+    case BERT_REPORT_LT_10_4:
+        printf("BERT report < 1 in 10^4\n");
+        break;
+    case BERT_REPORT_LT_10_5:
+        printf("BERT report < 1 in 10^5\n");
+        break;
+    case BERT_REPORT_LT_10_6:
+        printf("BERT report < 1 in 10^6\n");
+        break;
+    case BERT_REPORT_LT_10_7:
+        printf("BERT report < 1 in 10^7\n");
+        break;
+    default:
+        printf("BERT report reason %d\n", reason);
+        break;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static int v27ter_rx_status(void *user_data, int status)
+{
+    printf("V.27ter rx status is %d\n", status);
+    switch (status)
+    {
+    case PUTBIT_TRAINING_FAILED:
+        printf("Training failed\n");
+        break;
+    case PUTBIT_TRAINING_IN_PROGRESS:
+        printf("Training in progress\n");
+        break;
+    case PUTBIT_TRAINING_SUCCEEDED:
+        printf("Training succeeded\n");
+        break;
+    case PUTBIT_CARRIER_UP:
+        printf("Carrier up\n");
+        break;
+    case PUTBIT_CARRIER_DOWN:
+        printf("Carrier down\n");
+        break;
+    default:
+        printf("Eh! - %d\n", status);
+        break;
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void v27terputbit(void *user_data, int bit)
+{
+    if (bit < 0)
+    {
+        v27ter_rx_status(user_data, bit);
+        return;
+    }
+    if (decode_test_file)
+        printf("Rx bit %d - %d\n", rx_bits++, bit);
+    else
+        bert_put_bit(&bert, bit);
+}
+/*- End of function --------------------------------------------------------*/
+
+static int v27ter_tx_status(void *user_data, int status)
+{
+    switch (status)
+    {
+    case MODEM_TX_STATUS_DATA_EXHAUSTED:
+        printf("V.27ter tx data exhausted\n");
+        break;
+    case MODEM_TX_STATUS_SHUTDOWN_COMPLETE:
+        printf("V.27ter tx shutdown complete\n");
+        break;
+    default:
+        printf("V.27ter tx status is %d\n", status);
+        break;
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int v27tergetbit(void *user_data)
+{
+    return bert_get_bit(&bert);
+}
+/*- End of function --------------------------------------------------------*/
+
+static void qam_report(void *user_data, const complexf_t *constel, const complexf_t *target, int symbol)
+{
+    int i;
+    int len;
+    complexf_t *coeffs;
+    float fpower;
+    float error;
+    v27ter_rx_state_t *rx;
+    static float smooth_power = 0.0f;
+#if defined(ENABLE_GUI)
+    static int reports = 0;
+#endif
+
+    rx = (v27ter_rx_state_t *) user_data;
+    if (constel)
+    {
+        fpower = (constel->re - target->re)*(constel->re - target->re)
+               + (constel->im - target->im)*(constel->im - target->im);
+        smooth_power = 0.95f*smooth_power + 0.05f*fpower;
+#if defined(ENABLE_GUI)
+        if (use_gui)
+        {
+            qam_monitor_update_constel(qam_monitor, constel);
+            qam_monitor_update_carrier_tracking(qam_monitor, v27ter_rx_carrier_frequency(rx));
+            qam_monitor_update_symbol_tracking(qam_monitor, v27ter_rx_symbol_timing_correction(rx));
+        }
+#endif
+        error = constel->im*target->re - constel->re*target->im;
+        printf("Tracking error %f %f %f %f %f %f\n", error, v27ter_rx_carrier_frequency(rx), constel->re, constel->im, target->re, target->im);
+        printf("%8d [%8.4f, %8.4f] [%8.4f, %8.4f] %2x %8.4f %8.4f %9.4f %7.3f %7.2f\n",
+               symbol_no,
+               constel->re,
+               constel->im,
+               target->re,
+               target->im,
+               symbol,
+               fpower,
+               smooth_power,
+               v27ter_rx_carrier_frequency(rx),
+               v27ter_rx_signal_power(rx),
+               v27ter_rx_symbol_timing_correction(rx));
+        len = v27ter_rx_equalizer_state(rx, &coeffs);
+        printf("Equalizer B:\n");
+        for (i = 0;  i < len;  i++)
+            printf("%3d (%15.5f, %15.5f) -> %15.5f\n", i, coeffs[i].re, coeffs[i].im, powerf(&coeffs[i]));
+        printf("Gardtest %d %f %d\n", symbol_no, v27ter_rx_symbol_timing_correction(rx), rx->gardner_integrate);
+        printf("Carcar %d %f\n", symbol_no, v27ter_rx_carrier_frequency(rx));
+#if defined(ENABLE_GUI)
+        if (use_gui)
+        {
+            if (++reports >= 1000)
+            {
+                qam_monitor_update_equalizer(qam_monitor, coeffs, len);
+                reports = 0;
+            }
+        }
+#endif
+        symbol_no++;
+    }
+    else
+    {
+        printf("Gardner step %d\n", symbol);
+        len = v27ter_rx_equalizer_state(rx, &coeffs);
+        printf("Equalizer A:\n");
+        for (i = 0;  i < len;  i++)
+            printf("%3d (%15.5f, %15.5f) -> %15.5f\n", i, coeffs[i].re, coeffs[i].im, powerf(&coeffs[i]));
+#if defined(ENABLE_GUI)
+        if (use_gui)
+            qam_monitor_update_equalizer(qam_monitor, coeffs, len);
+#endif
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+int main(int argc, char *argv[])
+{
+    v27ter_rx_state_t rx;
+    v27ter_tx_state_t tx;
+    bert_results_t bert_results;
+    int16_t gen_amp[BLOCK_LEN];
+    int16_t amp[BLOCK_LEN];
+    AFfilehandle inhandle;
+    AFfilehandle outhandle;
+    int outframes;
+    int samples;
+    int tep;
+    int test_bps;
+    int noise_level;
+    int signal_level;
+    int bits_per_test;
+    int line_model_no;
+    int block;
+    int log_audio;
+    int channel_codec;
+    int rbs_pattern;
+    int opt;
+
+    channel_codec = MUNGE_CODEC_NONE;
+    rbs_pattern = 0;
+    test_bps = 4800;
+    tep = FALSE;
+    line_model_no = 0;
+    decode_test_file = NULL;
+    use_gui = FALSE;
+    noise_level = -70;
+    signal_level = -13;
+    bits_per_test = 50000;
+    log_audio = FALSE;
+    while ((opt = getopt(argc, argv, "b:c:d:glm:n:r:s:t")) != -1)
+    {
+        switch (opt)
+        {
+        case 'b':
+            bits_per_test = atoi(optarg);
+            break;
+        case 'c':
+            channel_codec = atoi(optarg);
+            break;
+        case 'd':
+            decode_test_file = optarg;
+            break;
+        case 'g':
+#if defined(ENABLE_GUI)
+            use_gui = TRUE;
+#else
+            fprintf(stderr, "Graphical monitoring not available\n");
+            exit(2);
+#endif
+            break;
+        case 'l':
+            log_audio = TRUE;
+            break;
+        case 'm':
+            line_model_no = atoi(optarg);
+            break;
+        case 'n':
+            noise_level = atoi(optarg);
+            break;
+        case 'r':
+            rbs_pattern = atoi(optarg);
+            break;
+        case 's':
+            signal_level = atoi(optarg);
+            break;
+        case 't':
+            tep = TRUE;
+            break;
+        default:
+            //usage();
+            exit(2);
+            break;
+        }
+    }
+    argc -= optind;
+    argv += optind;
+    if (argc > 0)
+    {
+        if (strcmp(argv[0], "4800") == 0)
+            test_bps = 4800;
+        else if (strcmp(argv[0], "2400") == 0)
+            test_bps = 2400;
+        else
+        {
+            fprintf(stderr, "Invalid bit rate\n");
+            exit(2);
+        }
+    }
+
+    inhandle = NULL;
+    outhandle = NULL;
+
+    if (log_audio)
+    {
+        if ((outhandle = afOpenFile_telephony_write(OUT_FILE_NAME, 1)) == AF_NULL_FILEHANDLE)
+        {
+            fprintf(stderr, "    Cannot create wave file '%s'\n", OUT_FILE_NAME);
+            exit(2);
+        }
+    }
+
+    if (decode_test_file)
+    {
+        /* We will decode the audio from a wave file. */
+        if ((inhandle = afOpenFile_telephony_read(decode_test_file, 1)) == AF_NULL_FILEHANDLE)
+        {
+            fprintf(stderr, "    Cannot open wave file '%s'\n", decode_test_file);
+            exit(2);
+        }
+    }
+    else
+    {
+        /* We will generate V.27ter audio, and add some noise to it. */
+        v27ter_tx_init(&tx, test_bps, tep, v27tergetbit, NULL);
+        v27ter_tx_power(&tx, signal_level);
+        v27ter_tx_set_modem_status_handler(&tx, v27ter_tx_status, (void *) &tx);
+        /* Move the carrier off a bit */
+        tx.carrier_phase_rate = dds_phase_ratef(1810.0f);
+
+        bert_init(&bert, bits_per_test, BERT_PATTERN_ITU_O152_11, test_bps, 20);
+        bert_set_report(&bert, 10000, reporter, NULL);
+
+        if ((line_model = one_way_line_model_init(line_model_no, (float) noise_level, channel_codec, rbs_pattern)) == NULL)
+        {
+            fprintf(stderr, "    Failed to create line model\n");
+            exit(2);
+        }
+    }
+
+    v27ter_rx_init(&rx, test_bps, v27terputbit, NULL);
+    v27ter_rx_set_modem_status_handler(&rx, v27ter_rx_status, (void *) &rx);
+    v27ter_rx_set_qam_report_handler(&rx, qam_report, (void *) &rx);
+    span_log_set_level(&rx.logging, SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_FLOW);
+    span_log_set_tag(&rx.logging, "V.27ter-rx");
+
+#if defined(ENABLE_GUI)
+    if (use_gui)
+    {
+        qam_monitor = qam_monitor_init(2.0f, NULL);
+        if (!decode_test_file)
+        {
+            start_line_model_monitor(129);
+            line_model_monitor_line_model_update(line_model->near_filter, line_model->near_filter_len);
+        }
+    }
+#endif
+
+    memset(&latest_results, 0, sizeof(latest_results));
+    for (block = 0;  ;  block++)
+    {
+        if (decode_test_file)
+        {
+            samples = afReadFrames(inhandle,
+                                   AF_DEFAULT_TRACK,
+                                   amp,
+                                   BLOCK_LEN);
+#if defined(ENABLE_GUI)
+            if (use_gui)
+                qam_monitor_update_audio_level(qam_monitor, amp, samples);
+#endif
+            if (samples == 0)
+                break;
+        }
+        else
+        {
+            samples = v27ter_tx(&tx, gen_amp, BLOCK_LEN);
+#if defined(ENABLE_GUI)
+            if (use_gui)
+                qam_monitor_update_audio_level(qam_monitor, gen_amp, samples);
+#endif
+            if (samples == 0)
+            {
+                printf("Restarting on zero output\n");
+
+                /* Push a little silence through, to ensure all the data bits get out of the buffers */
+                memset(amp, 0, BLOCK_LEN*sizeof(int16_t));
+                v27ter_rx(&rx, amp, BLOCK_LEN);
+                v27ter_rx(&rx, amp, BLOCK_LEN);
+                v27ter_rx(&rx, amp, BLOCK_LEN);
+
+                /* Note that we might get a few bad bits as the carrier shuts down. */
+                bert_result(&bert, &bert_results);
+                fprintf(stderr, "Final result %ddBm0, %d bits, %d bad bits, %d resyncs\n", signal_level, bert_results.total_bits, bert_results.bad_bits, bert_results.resyncs);
+                fprintf(stderr, "Last report  %ddBm0, %d bits, %d bad bits, %d resyncs\n", signal_level, latest_results.total_bits, latest_results.bad_bits, latest_results.resyncs);
+                /* See if bit errors are appearing yet. Also check we are getting enough bits out of the receiver. The last regular report
+                   should be error free, though the final report will generally contain bits errors as the carrier was dying. The total
+                   number of bits out of the receiver should be at least the number we sent. Also, since BERT sync should have occurred
+                   rapidly at the start of transmission, the last report should have occurred at not much less than the total number of
+                   bits we sent. */
+                if (bert_results.total_bits < bits_per_test
+                    ||
+                    latest_results.total_bits < bits_per_test - 100
+                    ||
+                    latest_results.bad_bits != 0)
+                {
+                    break;
+                }
+                memset(&latest_results, 0, sizeof(latest_results));
+                signal_level--;
+                v27ter_tx_restart(&tx, test_bps, tep);
+                v27ter_tx_power(&tx, signal_level);
+                v27ter_rx_restart(&rx, test_bps, FALSE);
+                bert_init(&bert, bits_per_test, BERT_PATTERN_ITU_O152_11, test_bps, 20);
+                bert_set_report(&bert, 10000, reporter, NULL);
+                one_way_line_model_release(line_model);
+                if ((line_model = one_way_line_model_init(line_model_no, (float) noise_level, channel_codec, 0)) == NULL)
+                {
+                    fprintf(stderr, "    Failed to create line model\n");
+                    exit(2);
+                }
+            }
+
+            if (log_audio)
+            {
+                outframes = afWriteFrames(outhandle,
+                                          AF_DEFAULT_TRACK,
+                                          gen_amp,
+                                          samples);
+                if (outframes != samples)
+                {
+                    fprintf(stderr, "    Error writing wave file\n");
+                    exit(2);
+                }
+            }
+            one_way_line_model(line_model, amp, gen_amp, samples);
+        }
+#if defined(ENABLE_GUI)
+        if (use_gui  &&  !decode_test_file)
+            line_model_monitor_line_spectrum_update(amp, samples);
+#endif
+        v27ter_rx(&rx, amp, samples);
+        if (decode_test_file == NULL  &&  block%500 == 0)
+            printf("Noise level is %d\n", noise_level);
+    }
+    if (!decode_test_file)
+    {
+        bert_result(&bert, &bert_results);
+        fprintf(stderr, "At completion:\n");
+        fprintf(stderr, "Final result %ddBm0, %d bits, %d bad bits, %d resyncs\n", signal_level, bert_results.total_bits, bert_results.bad_bits, bert_results.resyncs);
+        fprintf(stderr, "Last report  %ddBm0, %d bits, %d bad bits, %d resyncs\n", signal_level, latest_results.total_bits, latest_results.bad_bits, latest_results.resyncs);
+        one_way_line_model_release(line_model);
+        if (signal_level > -43)
+        {
+            printf("Tests failed.\n");
+            exit(2);
+        }
+
+        printf("Tests passed.\n");
+    }
+#if defined(ENABLE_GUI)
+    if (use_gui)
+        qam_wait_to_end(qam_monitor);
+#endif
+    if (log_audio)
+    {
+        if (afCloseFile(outhandle))
+        {
+            fprintf(stderr, "    Cannot close wave file '%s'\n", OUT_FILE_NAME);
+            exit(2);
+        }
+    }
+    return  0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/tests/v29_tests.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/tests/v29_tests.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,537 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * v29_tests.c
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2003 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: v29_tests.c,v 1.102 2008/08/29 09:28:13 steveu Exp $
+ */
+
+/*! \page v29_tests_page V.29 modem tests
+\section v29_tests_page_sec_1 What does it do?
+These tests test one way paths, as V.29 is a half-duplex modem. They allow either:
+
+ - A V.29 transmit modem to feed a V.29 receive modem through a telephone line
+   model. BER testing is then used to evaluate performance under various line
+   conditions. This is effective for testing the basic performance of the
+   receive modem. It is also the only test mode provided for evaluating the
+   transmit modem.
+
+ - A V.29 receive modem is used to decode V.29 audio, stored in a wave file.
+   This is good way to evaluate performance with audio recorded from other
+   models of modem, and with real world problematic telephone lines.
+
+If the appropriate GUI environment exists, the tests are built such that a visual
+display of modem status is maintained.
+
+\section v29_tests_page_sec_2 How is it used?
+*/
+
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
+
+#if defined(HAVE_FL_FL_H)  &&  defined(HAVE_FL_FL_CARTESIAN_H)  &&  defined(HAVE_FL_FL_AUDIO_METER_H)
+#define ENABLE_GUI
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <string.h>
+#include <audiofile.h>
+
+#include "spandsp.h"
+#include "spandsp-sim.h"
+
+#if defined(ENABLE_GUI)
+#include "modem_monitor.h"
+#include "line_model_monitor.h"
+#endif
+
+#define BLOCK_LEN       160
+
+#define OUT_FILE_NAME   "v29.wav"
+
+char *decode_test_file = NULL;
+int use_gui = FALSE;
+
+int symbol_no = 0;
+int rx_bits = 0;
+
+bert_state_t bert;
+one_way_line_model_state_t *line_model;
+
+#if defined(ENABLE_GUI)
+qam_monitor_t *qam_monitor;
+#endif
+
+bert_results_t latest_results;
+
+static void reporter(void *user_data, int reason, bert_results_t *results)
+{
+    switch (reason)
+    {
+    case BERT_REPORT_SYNCED:
+        printf("BERT report synced\n");
+        break;
+    case BERT_REPORT_UNSYNCED:
+        printf("BERT report unsync'ed\n");
+        break;
+    case BERT_REPORT_REGULAR:
+        printf("BERT report regular - %d bits, %d bad bits, %d resyncs\n", results->total_bits, results->bad_bits, results->resyncs);
+        memcpy(&latest_results, results, sizeof(latest_results));
+        break;
+    case BERT_REPORT_GT_10_2:
+        printf("BERT report > 1 in 10^2\n");
+        break;
+    case BERT_REPORT_LT_10_2:
+        printf("BERT report < 1 in 10^2\n");
+        break;
+    case BERT_REPORT_LT_10_3:
+        printf("BERT report < 1 in 10^3\n");
+        break;
+    case BERT_REPORT_LT_10_4:
+        printf("BERT report < 1 in 10^4\n");
+        break;
+    case BERT_REPORT_LT_10_5:
+        printf("BERT report < 1 in 10^5\n");
+        break;
+    case BERT_REPORT_LT_10_6:
+        printf("BERT report < 1 in 10^6\n");
+        break;
+    case BERT_REPORT_LT_10_7:
+        printf("BERT report < 1 in 10^7\n");
+        break;
+    default:
+        printf("BERT report reason %d\n", reason);
+        break;
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+static int v29_rx_status(void *user_data, int status)
+{
+    v29_rx_state_t *rx;
+    int i;
+    int len;
+    complexf_t *coeffs;
+
+    printf("V.29 rx status is %d\n", status);
+    rx = (v29_rx_state_t *) user_data;
+    switch (status)
+    {
+    case PUTBIT_TRAINING_FAILED:
+        printf("Training failed\n");
+        break;
+    case PUTBIT_TRAINING_IN_PROGRESS:
+        printf("Training in progress\n");
+        break;
+    case PUTBIT_TRAINING_SUCCEEDED:
+        printf("Training succeeded\n");
+        len = v29_rx_equalizer_state(rx, &coeffs);
+        printf("Equalizer:\n");
+        for (i = 0;  i < len;  i++)
+            printf("%3d (%15.5f, %15.5f) -> %15.5f\n", i, coeffs[i].re, coeffs[i].im, powerf(&coeffs[i]));
+        break;
+    case PUTBIT_CARRIER_UP:
+        printf("Carrier up\n");
+        break;
+    case PUTBIT_CARRIER_DOWN:
+        printf("Carrier down\n");
+        break;
+    default:
+        printf("Eh! - %d\n", status);
+        break;
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static void v29putbit(void *user_data, int bit)
+{
+    v29_rx_state_t *rx;
+    
+    if (bit < 0)
+    {
+        v29_rx_status(user_data, bit);
+        return;
+    }
+
+    rx = (v29_rx_state_t *) user_data;
+    if (decode_test_file)
+        printf("Rx bit %d - %d\n", rx_bits++, bit);
+    else
+        bert_put_bit(&bert, bit);
+}
+/*- End of function --------------------------------------------------------*/
+
+static int v29_tx_status(void *user_data, int status)
+{
+    switch (status)
+    {
+    case MODEM_TX_STATUS_DATA_EXHAUSTED:
+        printf("V.29 tx data exhausted\n");
+        break;
+    case MODEM_TX_STATUS_SHUTDOWN_COMPLETE:
+        printf("V.29 tx shutdown complete\n");
+        break;
+    default:
+        printf("V.29 tx status is %d\n", status);
+        break;
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int v29getbit(void *user_data)
+{
+    return bert_get_bit(&bert);
+}
+/*- End of function --------------------------------------------------------*/
+
+static void qam_report(void *user_data, const complexf_t *constel, const complexf_t *target, int symbol)
+{
+    int i;
+    int len;
+    complexf_t *coeffs;
+    float fpower;
+    v29_rx_state_t *rx;
+    static float smooth_power = 0.0f;
+    static int update_interval = 100;
+
+    rx = (v29_rx_state_t *) user_data;
+    if (constel)
+    {
+        fpower = (constel->re - target->re)*(constel->re - target->re)
+               + (constel->im - target->im)*(constel->im - target->im);
+        smooth_power = 0.95f*smooth_power + 0.05f*fpower;
+#if defined(ENABLE_GUI)
+        if (use_gui)
+        {
+            qam_monitor_update_constel(qam_monitor, constel);
+            qam_monitor_update_carrier_tracking(qam_monitor, v29_rx_carrier_frequency(rx));
+            //qam_monitor_update_carrier_tracking(qam_monitor, (fpower)  ?  fpower  :  0.001f);
+            qam_monitor_update_symbol_tracking(qam_monitor, v29_rx_symbol_timing_correction(rx));
+        }
+#endif
+        printf("%8d [%8.4f, %8.4f] [%8.4f, %8.4f] %2x %8.4f %8.4f %9.4f %7.3f %7.2f\n",
+               symbol_no,
+               constel->re,
+               constel->im,
+               target->re,
+               target->im,
+               symbol,
+               fpower,
+               smooth_power,
+               v29_rx_carrier_frequency(rx),
+               v29_rx_signal_power(rx),
+               v29_rx_symbol_timing_correction(rx));
+        symbol_no++;
+        if (--update_interval <= 0)
+        {
+            len = v29_rx_equalizer_state(rx, &coeffs);
+            printf("Equalizer A:\n");
+            for (i = 0;  i < len;  i++)
+                printf("%3d (%15.5f, %15.5f) -> %15.5f\n", i, coeffs[i].re, coeffs[i].im, powerf(&coeffs[i]));
+#if defined(ENABLE_GUI)
+            if (use_gui)
+                qam_monitor_update_equalizer(qam_monitor, coeffs, len);
+#endif
+            update_interval = 100;
+        }
+    }
+}
+/*- End of function --------------------------------------------------------*/
+
+int main(int argc, char *argv[])
+{
+    v29_rx_state_t rx;
+    v29_tx_state_t tx;
+    bert_results_t bert_results;
+    int16_t gen_amp[BLOCK_LEN];
+    int16_t amp[BLOCK_LEN];
+    AFfilehandle inhandle;
+    AFfilehandle outhandle;
+    int outframes;
+    int samples;
+    int tep;
+    int test_bps;
+    int noise_level;
+    int signal_level;
+    int bits_per_test;
+    int line_model_no;
+    int block;
+    int log_audio;
+    int channel_codec;
+    int rbs_pattern;
+    int opt;
+    
+    channel_codec = MUNGE_CODEC_NONE;
+    rbs_pattern = 0;
+    test_bps = 9600;
+    tep = FALSE;
+    line_model_no = 0;
+    decode_test_file = NULL;
+    use_gui = FALSE;
+    noise_level = -70;
+    signal_level = -13;
+    bits_per_test = 50000;
+    log_audio = FALSE;
+    while ((opt = getopt(argc, argv, "b:c:d:glm:n:r:s:t")) != -1)
+    {
+        switch (opt)
+        {
+        case 'b':
+            bits_per_test = atoi(optarg);
+            break;
+        case 'c':
+            channel_codec = atoi(optarg);
+            break;
+        case 'd':
+            decode_test_file = optarg;
+            break;
+        case 'g':
+#if defined(ENABLE_GUI)
+            use_gui = TRUE;
+#else
+            fprintf(stderr, "Graphical monitoring not available\n");
+            exit(2);
+#endif
+            break;
+        case 'l':
+            log_audio = TRUE;
+            break;
+        case 'm':
+            line_model_no = atoi(optarg);
+            break;
+        case 'n':
+            noise_level = atoi(optarg);
+            break;
+        case 'r':
+            rbs_pattern = atoi(optarg);
+            break;
+        case 's':
+            signal_level = atoi(optarg);
+            break;
+        case 't':
+            tep = TRUE;
+            break;
+        default:
+            //usage();
+            exit(2);
+            break;
+        }
+    }
+    argc -= optind;
+    argv += optind;
+    if (argc > 0)
+    {
+        if (strcmp(argv[0], "9600") == 0)
+            test_bps = 9600;
+        else if (strcmp(argv[0], "7200") == 0)
+            test_bps = 7200;
+        else if (strcmp(argv[0], "4800") == 0)
+            test_bps = 4800;
+        else
+        {
+            fprintf(stderr, "Invalid bit rate\n");
+            exit(2);
+        }
+    }
+    inhandle = NULL;
+    outhandle = NULL;
+
+    if (log_audio)
+    {
+        if ((outhandle = afOpenFile_telephony_write(OUT_FILE_NAME, 1)) == AF_NULL_FILEHANDLE)
+        {
+            fprintf(stderr, "    Cannot create wave file '%s'\n", OUT_FILE_NAME);
+            exit(2);
+        }
+    }
+
+    if (decode_test_file)
+    {
+        /* We will decode the audio from a wave file. */
+        if ((inhandle = afOpenFile_telephony_read(decode_test_file, 1)) == AF_NULL_FILEHANDLE)
+        {
+            fprintf(stderr, "    Cannot open wave file '%s'\n", decode_test_file);
+            exit(2);
+        }
+    }
+    else
+    {
+        /* We will generate V.29 audio, and add some noise to it. */
+        v29_tx_init(&tx, test_bps, tep, v29getbit, NULL);
+        v29_tx_power(&tx, signal_level);
+        v29_tx_set_modem_status_handler(&tx, v29_tx_status, (void *) &tx);
+        /* Move the carrier off a bit */
+        tx.carrier_phase_rate = dds_phase_ratef(1710.0f);
+        tx.carrier_phase = 0;
+
+        bert_init(&bert, bits_per_test, BERT_PATTERN_ITU_O152_11, test_bps, 20);
+        bert_set_report(&bert, 10000, reporter, NULL);
+
+        if ((line_model = one_way_line_model_init(line_model_no, (float) noise_level, channel_codec, rbs_pattern)) == NULL)
+        {
+            fprintf(stderr, "    Failed to create line model\n");
+            exit(2);
+        }
+    }
+
+    v29_rx_init(&rx, test_bps, v29putbit, &rx);
+    v29_rx_signal_cutoff(&rx, -45.5f);
+    v29_rx_set_modem_status_handler(&rx, v29_rx_status, (void *) &rx);
+    v29_rx_set_qam_report_handler(&rx, qam_report, (void *) &rx);
+    /* Rotate the starting phase */
+    rx.carrier_phase = 0x80000000;
+    span_log_set_level(&rx.logging, SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_FLOW);
+    span_log_set_tag(&rx.logging, "V.29-rx");
+
+#if defined(ENABLE_GUI)
+    if (use_gui)
+    {
+        qam_monitor = qam_monitor_init(6.0f, NULL);
+        if (!decode_test_file)
+        {
+            start_line_model_monitor(129);
+            line_model_monitor_line_model_update(line_model->near_filter, line_model->near_filter_len);
+        }
+    }
+#endif
+
+    memset(&latest_results, 0, sizeof(latest_results));
+    for (block = 0;  ;  block++)
+    {
+        if (decode_test_file)
+        {
+            samples = afReadFrames(inhandle,
+                                   AF_DEFAULT_TRACK,
+                                   amp,
+                                   BLOCK_LEN);
+#if defined(ENABLE_GUI)
+            if (use_gui)
+                qam_monitor_update_audio_level(qam_monitor, amp, samples);
+#endif
+            if (samples == 0)
+                break;
+        }
+        else
+        {
+            samples = v29_tx(&tx, gen_amp, BLOCK_LEN);
+#if defined(ENABLE_GUI)
+            if (use_gui)
+                qam_monitor_update_audio_level(qam_monitor, gen_amp, samples);
+#endif
+            if (samples == 0)
+            {
+                /* Push a little silence through, to ensure all the data bits get out of the buffers */
+                memset(amp, 0, BLOCK_LEN*sizeof(int16_t));
+                v29_rx(&rx, amp, BLOCK_LEN);
+
+                /* Note that we might get a few bad bits as the carrier shuts down. */
+                bert_result(&bert, &bert_results);
+                fprintf(stderr, "Final result %ddBm0, %d bits, %d bad bits, %d resyncs\n", signal_level, bert_results.total_bits, bert_results.bad_bits, bert_results.resyncs);
+                fprintf(stderr, "Last report  %ddBm0, %d bits, %d bad bits, %d resyncs\n", signal_level, latest_results.total_bits, latest_results.bad_bits, latest_results.resyncs);
+                /* See if bit errors are appearing yet. Also check we are getting enough bits out of the receiver. The last regular report
+                   should be error free, though the final report will generally contain bits errors as the carrier was dying. The total
+                   number of bits out of the receiver should be at least the number we sent. Also, since BERT sync should have occurred
+                   rapidly at the start of transmission, the last report should have occurred at not much less than the total number of
+                   bits we sent. */
+                if (bert_results.total_bits < bits_per_test
+                    ||
+                    latest_results.total_bits < bits_per_test - 100
+                    ||
+                    latest_results.bad_bits != 0)
+                {
+                    break;
+                }
+                memset(&latest_results, 0, sizeof(latest_results));
+                signal_level--;
+                v29_tx_restart(&tx, test_bps, tep);
+                v29_tx_power(&tx, signal_level);
+                v29_rx_restart(&rx, test_bps, FALSE);
+                rx.eq_put_step = rand()%(48*10/3);
+                bert_init(&bert, bits_per_test, BERT_PATTERN_ITU_O152_11, test_bps, 20);
+                bert_set_report(&bert, 10000, reporter, NULL);
+                one_way_line_model_release(line_model);
+                if ((line_model = one_way_line_model_init(line_model_no, (float) noise_level, channel_codec, 0)) == NULL)
+                {
+                    fprintf(stderr, "    Failed to create line model\n");
+                    exit(2);
+                }
+            }
+            if (log_audio)
+            {
+                outframes = afWriteFrames(outhandle,
+                                          AF_DEFAULT_TRACK,
+                                          gen_amp,
+                                          samples);
+                if (outframes != samples)
+                {
+                    fprintf(stderr, "    Error writing wave file\n");
+                    exit(2);
+                }
+            }
+            one_way_line_model(line_model, amp, gen_amp, samples);
+        }
+#if defined(ENABLE_GUI)
+        if (use_gui  &&  !decode_test_file)
+            line_model_monitor_line_spectrum_update(amp, samples);
+#endif
+        v29_rx(&rx, amp, samples);
+        if (decode_test_file == NULL  &&  block%500 == 0)
+            printf("Noise level is %d\n", noise_level);
+    }
+    if (!decode_test_file)
+    {
+        bert_result(&bert, &bert_results);
+        fprintf(stderr, "At completion:\n");
+        fprintf(stderr, "Final result %ddBm0, %d bits, %d bad bits, %d resyncs\n", signal_level, bert_results.total_bits, bert_results.bad_bits, bert_results.resyncs);
+        fprintf(stderr, "Last report  %ddBm0, %d bits, %d bad bits, %d resyncs\n", signal_level, latest_results.total_bits, latest_results.bad_bits, latest_results.resyncs);
+        one_way_line_model_release(line_model);
+        
+        if (signal_level > -43)
+        {
+            printf("Tests failed.\n");
+            exit(2);
+        }
+
+        printf("Tests passed.\n");
+    }
+#if defined(ENABLE_GUI)
+    if (use_gui)
+        qam_wait_to_end(qam_monitor);
+#endif
+    if (log_audio)
+    {
+        if (afCloseFile(outhandle))
+        {
+            fprintf(stderr, "    Cannot close wave file '%s'\n", OUT_FILE_NAME);
+            exit(2);
+        }
+    }
+    return  0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/tests/v42_tests.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/tests/v42_tests.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,120 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * v42_tests.c
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2004 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: v42_tests.c,v 1.25 2008/05/13 13:17:26 steveu Exp $
+ */
+
+/* THIS IS A WORK IN PROGRESS. IT IS NOT FINISHED. */
+
+/*! \page v42_tests_page V.42 tests
+\section v42_tests_page_sec_1 What does it do?
+These tests connect two instances of V.42 back to back. V.42 frames are
+then exchanged between them.
+*/
+
+#if defined(HAVE_CONFIG_H)
+#include <config.h>
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <assert.h>
+
+#include "spandsp.h"
+
+v42_state_t caller;
+v42_state_t answerer;
+
+int rx_next[3] = {0};
+int tx_next[3] = {0};
+
+static void v42_status(void *user_data, int status)
+{
+    int x;
+    
+    x = (intptr_t) user_data;
+    printf("%d: Status is '%s' (%d)\n", x, lapm_status_to_str(status), status);
+    //if (status == LAPM_DATA)
+    //    lapm_tx_iframe((x == 1)  ?  &caller.lapm  :  &answerer.lapm, "ABCDEFGHIJ", 10, 1);
+}
+
+static void v42_frames(void *user_data, const uint8_t *msg, int len)
+{
+    int i;
+    int x;
+    
+    x = (intptr_t) user_data;
+    for (i = 0;  i < len;  i++)
+    {
+        if (msg[i] != (rx_next[x] & 0xFF))
+            printf("%d: Mismatch 0x%02X 0x%02X\n", x, msg[i], rx_next[x] & 0xFF);
+        rx_next[x]++;
+    }
+    printf("%d: Got frame len %d\n", x, len);
+}
+
+int main(int argc, char *argv[])
+{
+    int i;
+    int bit;
+    uint8_t buf[1024];
+
+    v42_init(&caller, TRUE, TRUE, v42_frames, (void *) 1);
+    v42_init(&answerer, FALSE, TRUE, v42_frames, (void *) 2);
+    v42_set_status_callback(&caller, v42_status, (void *) 1);
+    v42_set_status_callback(&answerer, v42_status, (void *) 2);
+    span_log_set_level(&caller.logging, SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_DEBUG);
+    span_log_set_tag(&caller.logging, "caller");
+    span_log_set_level(&caller.lapm.logging, SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_DEBUG);
+    span_log_set_tag(&caller.lapm.logging, "caller");
+    span_log_set_level(&caller.lapm.sched.logging, SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_DEBUG);
+    span_log_set_tag(&caller.lapm.sched.logging, "caller");
+    span_log_set_level(&answerer.logging, SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_DEBUG);
+    span_log_set_tag(&answerer.logging, "answerer");
+    span_log_set_level(&answerer.lapm.logging, SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_DEBUG);
+    span_log_set_tag(&answerer.lapm.logging, "answerer");
+    span_log_set_level(&answerer.lapm.sched.logging, SPAN_LOG_SHOW_SEVERITY | SPAN_LOG_SHOW_PROTOCOL | SPAN_LOG_DEBUG);
+    span_log_set_tag(&answerer.lapm.sched.logging, "answerer");
+    for (i = 0;  i < 100000;  i++)
+    {
+        bit = v42_tx_bit(&caller);
+        v42_rx_bit(&answerer, bit);
+        bit = v42_tx_bit(&answerer);
+        //if (i%10000 == 0)
+        //    bit ^= 1;
+        v42_rx_bit(&caller, bit);
+        span_schedule_update(&caller.lapm.sched, 4);
+        span_schedule_update(&answerer.lapm.sched, 4);
+        buf[0] = tx_next[1];
+        if (lapm_tx(&caller.lapm, buf, 1) == 1)
+            tx_next[1]++;
+        buf[0] = tx_next[2];
+        if (lapm_tx(&answerer.lapm, buf, 1) == 1)
+            tx_next[2]++;
+    }
+    return  0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/tests/v42bis_tests.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/tests/v42bis_tests.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,165 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * v42bis_tests.c
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2005 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: v42bis_tests.c,v 1.23 2008/05/13 13:17:27 steveu Exp $
+ */
+
+/* THIS IS A WORK IN PROGRESS. IT IS NOT FINISHED. */
+
+/*! \page v42bis_tests_page V.42bis tests
+\section v42bis_tests_page_sec_1 What does it do?
+These tests compress the contents of a file specified on the command line, writing
+the compressed data to v42bis_tests.v42bis. They then read back the contents of the
+compressed file, decompress, and write the results to v42bis_tests.out. The contents
+of this file should exactly match the original file.
+*/
+
+#if defined(HAVE_CONFIG_H)
+#include <config.h>
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <string.h>
+#include <unistd.h>
+#include <errno.h>
+#include <ctype.h>
+#include <assert.h>
+
+#include "spandsp.h"
+
+#define COMPRESSED_FILE_NAME        "v42bis_tests.v42bis"
+#define OUTPUT_FILE_NAME            "v42bis_tests.out"
+
+int in_octets_to_date = 0;
+int out_octets_to_date = 0;
+
+static void frame_handler(void *user_data, const uint8_t *buf, int len)
+{
+    int ret;
+    
+    if ((ret = write((intptr_t) user_data, buf, len)) != len)
+        fprintf(stderr, "Write error %d/%d\n", ret, errno);
+    out_octets_to_date += len;
+}
+
+static void data_handler(void *user_data, const uint8_t *buf, int len)
+{
+    int ret;
+
+    if ((ret = write((intptr_t) user_data, buf, len)) != len)
+        fprintf(stderr, "Write error %d/%d\n", ret, errno);
+    out_octets_to_date += len;
+}
+
+int main(int argc, char *argv[])
+{
+    int len;
+    v42bis_state_t state_a;
+    v42bis_state_t state_b;
+    uint8_t buf[1024];
+    int in_fd;
+    int v42bis_fd;
+    int out_fd;
+    int do_compression;
+    int do_decompression;
+    time_t now;
+
+    do_compression = TRUE;
+    do_decompression = TRUE;
+    if (argc < 2)
+    {
+        fprintf(stderr, "Usage: %s <file>\n", argv[0]);
+        exit(2);
+    }
+    if (do_compression)
+    {
+        if ((in_fd = open(argv[1], O_RDONLY)) < 0)
+        {
+            fprintf(stderr, "Error opening file '%s'.\n", argv[1]);
+            exit(2);
+        }
+        if ((v42bis_fd = open(COMPRESSED_FILE_NAME, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0)
+        {
+            fprintf(stderr, "Error opening file '%s'.\n", COMPRESSED_FILE_NAME);
+            exit(2);
+        }
+
+        time(&now);
+        v42bis_init(&state_a, 3, 512, 6, frame_handler, (void *) (intptr_t) v42bis_fd, 512, data_handler, NULL, 512);
+        v42bis_compression_control(&state_a, V42BIS_COMPRESSION_MODE_ALWAYS);
+        in_octets_to_date = 0;
+        out_octets_to_date = 0;
+        while ((len = read(in_fd, buf, 1024)) > 0)
+        {
+            if (v42bis_compress(&state_a, buf, len))
+            {
+                fprintf(stderr, "Bad return code from compression\n");
+                exit(2);
+            }
+            in_octets_to_date += len;
+        }
+        v42bis_compress_flush(&state_a);
+        printf("%d bytes compressed to %d bytes in %lds\n", in_octets_to_date, out_octets_to_date, time(NULL) - now);
+        close(in_fd);
+        close(v42bis_fd);
+    }
+
+    if (do_decompression)
+    {
+        /* Now open the files for the decompression. */
+        if ((v42bis_fd = open(COMPRESSED_FILE_NAME, O_RDONLY)) < 0)
+        {
+            fprintf(stderr, "Error opening file '%s'.\n", COMPRESSED_FILE_NAME);
+            exit(2);
+        }
+        if ((out_fd = open(OUTPUT_FILE_NAME, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0)
+        {
+            fprintf(stderr, "Error opening file '%s'.\n", OUTPUT_FILE_NAME);
+            exit(2);
+        }
+    
+        time(&now);
+        v42bis_init(&state_b, 3, 512, 6, frame_handler, (void *) (intptr_t) v42bis_fd, 512, data_handler, (void *) (intptr_t) out_fd, 512);
+        in_octets_to_date = 0;
+        out_octets_to_date = 0;
+        while ((len = read(v42bis_fd, buf, 1024)) > 0)
+        {
+            if (v42bis_decompress(&state_b, buf, len))
+            {
+                fprintf(stderr, "Bad return code from decompression\n");
+                exit(2);
+            }
+            in_octets_to_date += len;
+        }
+        v42bis_decompress_flush(&state_b);
+        printf("%d bytes decompressed to %d bytes in %lds\n", in_octets_to_date, out_octets_to_date, time(NULL) - now);
+        close(v42bis_fd);
+        close(out_fd);
+    }
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/tests/v42bis_tests.sh
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/tests/v42bis_tests.sh	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,154 @@
+#!/bin/sh
+#
+# V.42bis compression/decompression tests, as specified in V.56ter
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU Lesser General Public License version 2.1,
+# as published by the Free Software Foundation.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this program; if not, write to the Free Software
+# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+#
+# $Id: v42bis_tests.sh,v 1.5 2008/05/03 09:34:26 steveu Exp $
+#
+
+BASE=../test-data/itu/v56ter
+
+./v42bis_tests ${BASE}/1.TST
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    exit $RETVAL
+fi
+diff ${BASE}/1.TST v42bis_tests.out
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    exit $RETVAL
+fi
+./v42bis_tests ${BASE}/1X04.TST
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    exit $RETVAL
+fi
+diff ${BASE}/1X04.TST v42bis_tests.out
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    exit $RETVAL
+fi
+./v42bis_tests ${BASE}/1X30.TST
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    exit $RETVAL
+fi
+diff ${BASE}/1X30.TST v42bis_tests.out
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    exit $RETVAL
+fi
+./v42bis_tests ${BASE}/2.TST
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    exit $RETVAL
+fi
+diff ${BASE}/2.TST v42bis_tests.out
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    exit $RETVAL
+fi
+./v42bis_tests ${BASE}/2X10.TST
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    exit $RETVAL
+fi
+diff ${BASE}/2X10.TST v42bis_tests.out
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    exit $RETVAL
+fi
+./v42bis_tests ${BASE}/3.TST
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    exit $RETVAL
+fi
+diff ${BASE}/3.TST v42bis_tests.out
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    exit $RETVAL
+fi
+./v42bis_tests ${BASE}/3X06.TST
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    exit $RETVAL
+fi
+diff ${BASE}/3X06.TST v42bis_tests.out
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    exit $RETVAL
+fi
+./v42bis_tests ${BASE}/4.TST
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    exit $RETVAL
+fi
+diff ${BASE}/4.TST v42bis_tests.out
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    exit $RETVAL
+fi
+./v42bis_tests ${BASE}/4X04.TST
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    exit $RETVAL
+fi
+diff ${BASE}/4X04.TST v42bis_tests.out
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    exit $RETVAL
+fi
+./v42bis_tests ${BASE}/5.TST
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    exit $RETVAL
+fi
+diff ${BASE}/5.TST v42bis_tests.out
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    exit $RETVAL
+fi
+./v42bis_tests ${BASE}/5X16.TST
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    exit $RETVAL
+fi
+diff ${BASE}/5X16.TST v42bis_tests.out
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    exit $RETVAL
+fi

Added: freeswitch/trunk/libs/spandsp/tests/v8_tests.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/tests/v8_tests.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,236 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * v8_tests.c
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2004 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: v8_tests.c,v 1.28 2008/08/29 09:28:13 steveu Exp $
+ */
+
+/*! \page v8_tests_page V.8 tests
+\section v8_tests_page_sec_1 What does it do?
+*/
+
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <string.h>
+#include <audiofile.h>
+
+#include "spandsp.h"
+#include "spandsp-sim.h"
+
+#define FALSE 0
+#define TRUE (!FALSE)
+
+#define SAMPLES_PER_CHUNK   160
+
+#define OUTPUT_FILE_NAME    "v8.wav"
+
+int negotiations_ok = 0;
+
+static void handler(void *user_data, v8_result_t *result)
+{
+    const char *s;
+    
+    s = (const char *) user_data;
+    
+    if (result == NULL)
+    {
+        printf("%s V.8 negotiation failed\n", s);
+        return;
+    }
+    printf("%s V.8 negotiation result:\n", s);
+    printf("  Call function '%s'\n", v8_call_function_to_str(result->call_function));
+    printf("  Negotiated modulation '%s'\n", v8_modulation_to_str(result->negotiated_modulation));
+    printf("  Protocol '%s'\n", v8_protocol_to_str(result->protocol));
+    printf("  PSTN access '%s'\n", v8_pstn_access_to_str(result->pstn_access));
+    printf("  PCM modem availability '%s'\n", v8_pcm_modem_availability_to_str(result->pcm_modem_availability));
+    if (result->call_function == V8_CALL_V_SERIES
+        &&
+        result->negotiated_modulation == V8_MOD_V90
+        &&
+        result->protocol == V8_PROTOCOL_LAPM_V42)
+    {
+        negotiations_ok++;
+    }
+}
+
+int main(int argc, char *argv[])
+{
+    int i;
+    int16_t amp[SAMPLES_PER_CHUNK];
+    int16_t out_amp[2*SAMPLES_PER_CHUNK];
+    v8_state_t v8_caller;
+    v8_state_t v8_answerer;
+    int outframes;
+    int samples;
+    int remnant;
+    int caller_available_modulations;
+    int answerer_available_modulations;
+    AFfilehandle inhandle;
+    AFfilehandle outhandle;
+    int opt;
+    char *decode_test_file;
+    
+    decode_test_file = NULL;
+    while ((opt = getopt(argc, argv, "d:")) != -1)
+    {
+        switch (opt)
+        {
+        case 'd':
+            decode_test_file = optarg;
+            break;
+        default:
+            //usage();
+            exit(2);
+            break;
+        }
+    }
+
+    caller_available_modulations = V8_MOD_V17
+                                 | V8_MOD_V21
+                                 | V8_MOD_V22
+                                 | V8_MOD_V23HALF
+                                 | V8_MOD_V23
+                                 | V8_MOD_V26BIS
+                                 | V8_MOD_V26TER
+                                 | V8_MOD_V27TER
+                                 | V8_MOD_V29
+                                 | V8_MOD_V32
+                                 | V8_MOD_V34HALF
+                                 | V8_MOD_V34
+                                 | V8_MOD_V90
+                                 | V8_MOD_V92;
+    answerer_available_modulations = V8_MOD_V17
+                                   | V8_MOD_V21
+                                   | V8_MOD_V22
+                                   | V8_MOD_V23HALF
+                                   | V8_MOD_V23
+                                   | V8_MOD_V26BIS
+                                   | V8_MOD_V26TER
+                                   | V8_MOD_V27TER
+                                   | V8_MOD_V29
+                                   | V8_MOD_V32
+                                   | V8_MOD_V34HALF
+                                   | V8_MOD_V34
+                                   | V8_MOD_V90
+                                   | V8_MOD_V92;
+    
+    if (decode_test_file == NULL)
+    {
+        if ((outhandle = afOpenFile_telephony_write(OUTPUT_FILE_NAME, 2)) == AF_NULL_FILEHANDLE)
+        {
+            fprintf(stderr, "    Cannot create wave file '%s'\n", OUTPUT_FILE_NAME);
+            exit(2);
+        }
+    
+        v8_init(&v8_caller, TRUE, caller_available_modulations, handler, (void *) "caller");
+        v8_init(&v8_answerer, FALSE, answerer_available_modulations, handler, (void *) "answerer");
+        span_log_set_level(&v8_caller.logging, SPAN_LOG_FLOW | SPAN_LOG_SHOW_TAG);
+        span_log_set_tag(&v8_caller.logging, "caller");
+        span_log_set_level(&v8_answerer.logging, SPAN_LOG_FLOW | SPAN_LOG_SHOW_TAG);
+        span_log_set_tag(&v8_answerer.logging, "answerer");
+        for (i = 0;  i < 1000;  i++)
+        {
+            samples = v8_tx(&v8_caller, amp, SAMPLES_PER_CHUNK);
+            if (samples < SAMPLES_PER_CHUNK)
+            {
+                memset(amp + samples, 0, sizeof(int16_t)*(SAMPLES_PER_CHUNK - samples));
+                samples = SAMPLES_PER_CHUNK;
+            }
+            remnant = v8_rx(&v8_answerer, amp, samples);
+            for (i = 0;  i < samples;  i++)
+                out_amp[2*i] = amp[i];
+            
+            samples = v8_tx(&v8_answerer, amp, SAMPLES_PER_CHUNK);
+            if (samples < SAMPLES_PER_CHUNK)
+            {
+                memset(amp + samples, 0, sizeof(int16_t)*(SAMPLES_PER_CHUNK - samples));
+                samples = SAMPLES_PER_CHUNK;
+            }
+            if (v8_rx(&v8_caller, amp, samples)  &&  remnant)
+                break;
+            for (i = 0;  i < samples;  i++)
+                out_amp[2*i + 1] = amp[i];
+    
+            outframes = afWriteFrames(outhandle,
+                                      AF_DEFAULT_TRACK,
+                                      out_amp,
+                                      samples);
+            if (outframes != samples)
+            {
+                fprintf(stderr, "    Error writing wave file\n");
+                exit(2);
+            }
+        }
+        if (afCloseFile(outhandle))
+        {
+            fprintf(stderr, "    Cannot close wave file '%s'\n", OUTPUT_FILE_NAME);
+            exit(2);
+        }
+        
+        v8_release(&v8_caller);
+        v8_release(&v8_answerer);
+        
+        if (negotiations_ok != 2)
+        {
+            printf("Tests failed.\n");
+            exit(2);
+        }
+        printf("Tests passed.\n");
+    }
+    else
+    {
+        printf("Decode file '%s'\n", decode_test_file);
+        v8_init(&v8_answerer, FALSE, answerer_available_modulations, handler, (void *) "answerer");
+        span_log_set_level(&v8_answerer.logging, SPAN_LOG_FLOW | SPAN_LOG_SHOW_TAG);
+        span_log_set_tag(&v8_answerer.logging, "decoder");
+        if ((inhandle = afOpenFile_telephony_read(decode_test_file, 1)) == AF_NULL_FILEHANDLE)
+        {
+            fprintf(stderr, "    Cannot open speech file '%s'\n", decode_test_file);
+            exit (2);
+        }
+        /*endif*/
+
+        while ((samples = afReadFrames(inhandle, AF_DEFAULT_TRACK, amp, SAMPLES_PER_CHUNK)))
+        {
+            remnant = v8_rx(&v8_answerer, amp, samples);
+        }
+        /*endwhile*/
+
+        v8_release(&v8_answerer);
+        if (afCloseFile(inhandle) != 0)
+        {
+            fprintf(stderr, "    Cannot close speech file '%s'\n", decode_test_file);
+            exit(2);
+        }
+        /*endif*/
+    }
+    return  0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/tests/vector_float_tests.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/tests/vector_float_tests.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,77 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * vector_float_tests.c
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2006 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: vector_float_tests.c,v 1.9 2008/05/13 13:17:27 steveu Exp $
+ */
+
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <string.h>
+#include <audiofile.h>
+
+#include "spandsp.h"
+
+static double vec_dot_prod_dumb(const double x[], const double y[], int n)
+{
+    int i;
+    double z;
+
+    z = 0.0;
+    for (i = 0;  i < n;  i++)
+        z += x[i]*y[i];
+    return z;
+}
+/*- End of function --------------------------------------------------------*/
+
+int main(int argc, char *argv[])
+{
+    int i;
+    double x[100];
+    double y[100];
+    double zsa;
+    double zsb;
+
+    for (i = 0;  i < 99;  i++)
+    {
+        x[i] = rand();
+        y[i] = rand();
+    }
+    zsa = vec_dot_prod(x, y, 99);
+    zsb = vec_dot_prod_dumb(x, y, 99);
+    if (zsa != zsb)
+    {
+        printf("Tests failed\n");
+        exit(2);
+    }
+
+    printf("Tests passed.\n");
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/tests/vector_int_tests.c
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/tests/vector_int_tests.c	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,122 @@
+/*
+ * SpanDSP - a series of DSP components for telephony
+ *
+ * vector_int_tests.c
+ *
+ * Written by Steve Underwood <steveu at coppice.org>
+ *
+ * Copyright (C) 2006 Steve Underwood
+ *
+ * All rights reserved.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2, as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ * $Id: vector_int_tests.c,v 1.8 2008/05/13 13:17:27 steveu Exp $
+ */
+
+#if defined(HAVE_CONFIG_H)
+#include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <string.h>
+#include <audiofile.h>
+
+#include "spandsp.h"
+
+static int32_t vec_dot_prodi16_dumb(const int16_t x[], const int16_t y[], int n)
+{
+    int32_t z;
+    int i;
+
+    z = 0;
+    for (i = 0;  i < n;  i++)
+        z += x[i]*y[i];
+    return  z;
+}
+/*- End of function --------------------------------------------------------*/
+
+static int32_t vec_min_maxi16_dumb(const int16_t x[], int n, int16_t out[])
+{
+    int i;
+    int16_t min;
+    int16_t max;
+    int16_t temp;
+    int32_t z;
+    
+    max = INT16_MIN;
+    min = INT16_MAX;
+    for (i = 0;  i < n;  i++)
+    {
+        temp = x[i];
+        if (temp > max)
+            max = temp;
+        /*endif*/
+        if (temp < min)
+            min = temp;
+        /*endif*/
+    }
+    /*endfor*/
+    out[0] = max;
+    out[1] = min;
+    z = abs(min);
+    if (z > max)
+        return z;
+    return max;
+}
+/*- End of function --------------------------------------------------------*/
+    
+int main(int argc, char *argv[])
+{
+    int i;
+    int32_t za;
+    int32_t zb;
+    int16_t x[99];
+    int16_t y[99];
+    int16_t outa[2];
+    int16_t outb[2];
+
+    for (i = 0;  i < 99;  i++)
+    {
+        x[i] = rand();
+        y[i] = rand();
+    }
+
+    za = vec_dot_prodi16(x, y, 99);
+    zb = vec_dot_prodi16_dumb(x, y, 99);
+    if (za != zb)
+    {
+        printf("Tests failed\n");
+        exit(2);
+    }
+
+    x[42] = -32768;
+    za = vec_min_maxi16_dumb(x, 99, outa);
+    zb = vec_min_maxi16(x, 99, outb);
+    if (za != zb
+        ||
+        outa[0] != outb[0]
+        ||
+        outa[1] != outb[1])
+    {
+        printf("Tests failed\n");
+        exit(2);
+    }
+    printf("Tests passed.\n");
+    return 0;
+}
+/*- End of function --------------------------------------------------------*/
+/*- End of file ------------------------------------------------------------*/

Added: freeswitch/trunk/libs/spandsp/unpack_g722_data.sh
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/unpack_g722_data.sh	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,59 @@
+#!/bin/sh
+#
+# SpanDSP - a series of DSP components for telephony
+#
+# unpack_g722_data.sh
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU Lesser General Public License version 2.1,
+# as published by the Free Software Foundation.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this program; if not, write to the Free Software
+# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+#
+# $Id: unpack_g722_data.sh,v 1.5 2008/05/03 07:55:04 steveu Exp $
+#
+
+ITUDATA="../../../T-REC-G.722-198703-I!AppII!ZPF-E.zip"
+
+cd test-data/itu
+if [ -d g722 ]
+then
+    cd g722
+else
+    mkdir g722
+    RETVAL=$?
+    if [ $RETVAL != 0 ]
+    then
+        echo Cannot create test-data/itu/g722!
+        exit $RETVAL
+    fi
+    cd g722
+fi
+
+rm -rf T*
+rm -rf software
+unzip ${ITUDATA} >/dev/null
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    echo Cannot unpack the ITU test vectors for G.722!
+    exit $RETVAL
+fi
+#rm ${ITUDATA}
+unzip ./software/G722ap2/G722E/Software.zip >/dev/null
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    echo Cannot unpack the ITU test vectors for G.722!
+    exit $RETVAL
+fi
+mv ./software/G722ap2/G722E/T* .
+rm -rf software
+echo The ITU test vectors for G.722 should now be in the g722 directory

Added: freeswitch/trunk/libs/spandsp/unpack_g726_data.sh
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/unpack_g726_data.sh	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,64 @@
+#!/bin/sh
+#
+# SpanDSP - a series of DSP components for telephony
+#
+# unpack_g726_data.sh
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU Lesser General Public License version 2.1,
+# as published by the Free Software Foundation.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this program; if not, write to the Free Software
+# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+#
+# $Id: unpack_g726_data.sh,v 1.6 2008/05/03 07:55:04 steveu Exp $
+#
+
+ITUDATA="../../../T-REC-G.726-199103-I!AppII!SOFT-ZST-E.zip"
+
+cd test-data/itu
+if [ -d g726 ]
+then
+    cd g726
+else
+    mkdir g726
+    RETVAL=$?
+    if [ $RETVAL != 0 ]
+    then
+        echo Cannot create test-data/itu/g726!
+        exit $RETVAL
+    fi
+    cd g726
+fi
+
+rm -rf DISK1
+rm -rf DISK2
+rm -rf G726piiE.WW7.doc
+rm -rf Software.zip
+unzip ${ITUDATA} >/dev/null
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    echo Cannot unpack the ITU test vectors for G.726!
+    exit $RETVAL
+fi
+#rm $(ITUDATA}
+rm G726piiE.WW7.doc
+unzip Software.zip >/dev/null
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    echo Cannot unpack the ITU test vectors for G.726!
+    exit $RETVAL
+fi
+rm Software.zip
+mv ./software/G726ap2/G726ap2e/DISK1 .
+mv ./software/G726ap2/G726ap2e/DISK2 .
+rm -rf ./software
+echo The ITU test vectors for G.726 should now be in the g726 directory

Added: freeswitch/trunk/libs/spandsp/unpack_gsm0610_data.sh
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/unpack_gsm0610_data.sh	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,320 @@
+#!/bin/sh
+#
+# SpanDSP - a series of DSP components for telephony
+#
+# unpack_gsm0610_data.sh
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU Lesser General Public License version 2.1,
+# as published by the Free Software Foundation.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this program; if not, write to the Free Software
+# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+#
+# $Id: unpack_gsm0610_data.sh,v 1.7 2008/05/03 10:59:44 steveu Exp $
+#
+
+# The ETSI distribution file extracts to 5 ZIP files, called DISK1.ZIP to DISK5.ZIP
+# These were originally the contents of 5 floppy disks. Disks 1 to 3 contain data
+# files. However, disks 4 and 5 contain .EXE files, which unpack.... but only in an
+# MS environment. These files need to be executed in a Windows or DOS environment,
+# or a good emulation like FreeDOS or Wine.
+
+ETSIDATA="../../../en_300961v080101p0.zip"
+
+cd test-data
+if [ -d etsi ]
+then
+    cd etsi
+else
+    mkdir etsi
+    RETVAL=$?
+    if [ $RETVAL != 0 ]
+    then
+        echo Cannot create test-data/etsi!
+        exit $RETVAL
+    fi
+    cd etsi
+fi
+if [ -d gsm0610 ]
+then
+    cd gsm0610
+else
+    mkdir gsm0610
+    RETVAL=$?
+    if [ $RETVAL != 0 ]
+    then
+        echo Cannot create test-data/etsi/gsm0610!
+        exit $RETVAL
+    fi
+    cd gsm0610
+fi
+
+if [ $1x ==  --no-exe-runx ]
+then
+    # Run the .exe files, which should be here
+    ./FR_A.EXE
+    ./FR_HOM_A.EXE
+    ./FR_SYN_A.EXE
+    ./FR_U.EXE
+    ./FR_HOM_U.EXE
+    ./FR_SYN_U.EXE
+    exit 0
+fi
+
+# Clear out any leftovers from the past
+rm -rf ASN.1.txt
+rm -rf DISK1.ZIP
+rm -rf DISK2.ZIP
+rm -rf DISK3.ZIP
+rm -rf DISK4.ZIP
+rm -rf DISK5.ZIP
+rm -rf *.EXE
+rm -rf READ_FRA.TXT
+rm -rf ACTION
+rm -rf unpacked
+
+if [ $1x ==  --no-exex ]
+then
+    # We need to prepare the .exe files to be run separately
+    rm -rf *.INP
+    rm -rf *.COD
+    rm -rf *.OUT
+
+    unzip ${ETSIDATA} >/dev/null
+    RETVAL=$?
+    if [ $RETVAL != 0 ]
+    then
+        echo Cannot unpack the ETSI test vectors for GSM 06.10!
+        exit $RETVAL
+    fi
+    unzip ./DISK4.ZIP >/dev/null
+    RETVAL=$?
+    if [ $RETVAL != 0 ]
+    then
+        echo Cannot unpack the ETSI test vectors for GSM 06.10!
+        exit $RETVAL
+    fi
+    unzip ./DISK5.ZIP >/dev/null
+    RETVAL=$?
+    if [ $RETVAL != 0 ]
+    then
+        echo Cannot unpack the ETSI test vectors for GSM 06.10!
+        exit $RETVAL
+    fi
+    rm -rf ASN.1.txt
+    rm -rf DISK1.ZIP
+    rm -rf DISK2.ZIP
+    rm -rf DISK3.ZIP
+    rm -rf DISK4.ZIP
+    rm -rf DISK5.ZIP
+    rm -rf READ_FRA.TXT
+
+    # An environment which is emulating an MS one will probably need
+    # to make the .EXE files actually executable.
+    chmod 755 *.EXE
+
+    echo "Now copy the files from the test-data/etsi/gsm0610 directory to a Windows,"
+    echo "DOS or other machine which can run .exe files. Run each of the .exe"
+    echo "files (there are 6 of them), and copy the whole directory back here."
+    echo "You can then complete the creation of the working data directories"
+    echo "with the command:"
+    echo $0 "--no-exe-continue"
+    exit 0
+fi
+
+unzip ${ETSIDATA} >/dev/null
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    echo Cannot unpack the ETSI test vectors for GSM 06.10!
+    exit $RETVAL
+fi
+#rm ${ETSIDATA}
+
+rm -rf ASN.1.txt
+
+if [ $1x !=  --no-exe-continuex ]
+then
+    # We need to extract and run the .exe files right now. For this to succeed
+    # we must be running in an environment which can run .exe files. This has been
+    # tested with Cygwin on a Windows XP machine.
+    rm -rf *.INP
+    rm -rf *.COD
+    rm -rf *.OUT
+
+    unzip ./DISK4.ZIP >/dev/null
+    RETVAL=$?
+    if [ $RETVAL != 0 ]
+    then
+        echo Cannot unpack the ETSI test vectors for GSM 06.10!
+        exit $RETVAL
+    fi
+    unzip ./DISK5.ZIP >/dev/null
+    RETVAL=$?
+    if [ $RETVAL != 0 ]
+    then
+        echo Cannot unpack the ETSI test vectors for GSM 06.10!
+        exit $RETVAL
+    fi
+    # An environment which is emulating an MS one will probably need
+    # to make the .EXE files actually executable.
+    chmod 755 *.EXE
+    ./FR_HOM_A.EXE >/dev/null
+    ./FR_SYN_A.EXE >/dev/null
+    ./FR_A.EXE >/dev/null
+    ./FR_HOM_U.EXE >/dev/null
+    ./FR_SYN_U.EXE >/dev/null
+    ./FR_U.EXE >/dev/null
+
+    rm -rf READ_FRA.TXT
+fi
+
+rm -rf DISK4.ZIP
+rm -rf DISK5.ZIP
+rm -rf *.EXE
+
+chmod 644 *.INP
+chmod 644 *.OUT
+chmod 644 *.COD
+
+# Create the directories where we want to put the test data files.
+mkdir unpacked
+mkdir unpacked/fr_A
+mkdir unpacked/fr_L
+mkdir unpacked/fr_U
+mkdir unpacked/fr_homing_A
+mkdir unpacked/fr_homing_L
+mkdir unpacked/fr_homing_U
+mkdir unpacked/fr_sync_A
+mkdir unpacked/fr_sync_L
+mkdir unpacked/fr_sync_U
+
+# Disks 1, 2 and 3 simply unzip, and the files have sensible file names. We
+# just need to rearrange the directories in which they are located.
+unzip ./DISK1.ZIP >/dev/null
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    echo Cannot unpack the ETSI test vectors for GSM 06.10!
+    exit $RETVAL
+fi
+unzip ./DISK2.ZIP >/dev/null
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    echo Cannot unpack the ETSI test vectors for GSM 06.10!
+    exit $RETVAL
+fi
+unzip ./DISK3.ZIP >/dev/null
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    echo Cannot unpack the ETSI test vectors for GSM 06.10!
+    exit $RETVAL
+fi
+rm -rf DISK1.ZIP
+rm -rf DISK2.ZIP
+rm -rf DISK3.ZIP
+
+mv "./ACTION/SMG#23/HOLD/0610_5~1/TESTSE~1/DISK1/"* ./unpacked/fr_L
+mv "./ACTION/SMG#23/HOLD/0610_5~1/TESTSE~1/DISK3/"Sync*.cod ./unpacked/fr_sync_L
+mv "./ACTION/SMG#23/HOLD/0610_5~1/TESTSE~1/DISK2/"Seq*h.* ./unpacked/fr_homing_L
+mv "./ACTION/SMG#23/HOLD/0610_5~1/TESTSE~1/DISK3/"Seq*h.* ./unpacked/fr_homing_L
+mv "./ACTION/SMG#23/HOLD/0610_5~1/TESTSE~1/DISK3/Bitsync.inp" ./unpacked/fr_sync_L
+mv "./ACTION/SMG#23/HOLD/0610_5~1/TESTSE~1/DISK3/Seqsync.inp" ./unpacked/fr_sync_L
+
+mv "./ACTION/SMG#23/HOLD/0610_5~1/TESTSE~1/DISK3/Homing01.cod" ./unpacked/fr_homing_L
+mv "./ACTION/SMG#23/HOLD/0610_5~1/TESTSE~1/DISK3/Homing01.out" ./unpacked/fr_homing_L
+
+rm -rf ACTION
+
+# The files extracted by the .EXE files have messy naming, and are not in
+# a sane directory layout. We rename and move them, to make the final result of
+# the files extracted from all five of the original .ZIP files reasonably
+# consistent, and easy to follow.
+rm -rf READ_FRA.TXT
+
+for I in SYN*_A.COD ;
+do
+    mv $I `echo $I | sed -e "s|SYN|./unpacked/fr_sync_A/Sync|" | sed -e "s/COD/cod/"`
+done
+
+for I in SYN*_U.COD ;
+do
+    mv $I `echo $I | sed -e "s|SYN|./unpacked/fr_sync_U/Sync|" | sed -e "s/COD/cod/"`
+done
+
+for I in SEQ*H_A.COD ;
+do
+    mv $I `echo $I | sed -e "s|SEQ|./unpacked/fr_homing_A/Seq|" | sed -e "s/COD/cod/"`
+done
+
+for I in SEQ*H_U.COD ;
+do
+    mv $I `echo $I | sed -e "s|SEQ|./unpacked/fr_homing_U/Seq|" | sed -e "s/COD/cod/"`
+done
+
+for I in SEQ*H_A.INP ;
+do
+    mv $I `echo $I | sed -e "s|SEQ|./unpacked/fr_homing_A/Seq|" | sed -e "s/INP/inp/"`
+done
+
+for I in SEQ*H_U.INP ;
+do
+    mv $I `echo $I | sed -e "s|SEQ|./unpacked/fr_homing_U/Seq|" | sed -e "s/INP/inp/"`
+done
+
+for I in SEQ*H_A.OUT ;
+do
+    mv $I `echo $I | sed -e "s|SEQ|./unpacked/fr_homing_A/Seq|" | sed -e "s/OUT/out/"`
+done
+
+for I in SEQ*H_U.OUT ;
+do
+    mv $I `echo $I | sed -e "s|SEQ|./unpacked/fr_homing_U/Seq|" | sed -e "s/OUT/out/"`
+done
+
+for I in SEQ*-A.COD ;
+do
+    mv $I `echo $I | sed -e "s|SEQ|./unpacked/fr_A/Seq|" | sed -e "s/COD/cod/"`
+done
+
+for I in SEQ*-U.COD ;
+do
+    mv $I `echo $I | sed -e "s|SEQ|./unpacked/fr_U/Seq|" | sed -e "s/COD/cod/"`
+done
+
+for I in SEQ*-A.INP ;
+do
+    mv $I `echo $I | sed -e "s|SEQ|./unpacked/fr_A/Seq|" | sed -e "s/INP/inp/"`
+done
+
+for I in SEQ*-U.INP ;
+do
+    mv $I `echo $I | sed -e "s|SEQ|./unpacked/fr_U/Seq|" | sed -e "s/INP/inp/"`
+done
+
+for I in SEQ*-A.OUT ;
+do
+    mv $I `echo $I | sed -e "s|SEQ|./unpacked/fr_A/Seq|" | sed -e "s/OUT/out/"`
+done
+
+for I in SEQ*-U.OUT ;
+do
+    mv $I `echo $I | sed -e "s|SEQ|./unpacked/fr_U/Seq|" | sed -e "s/OUT/out/"`
+done
+
+mv HOM01_A.OUT ./unpacked/fr_homing_A/Homing01_A.out
+mv HOM01_U.OUT ./unpacked/fr_homing_U/Homing01_U.out
+mv SEQSYN_A.INP ./unpacked/fr_sync_A/Seqsync_A.inp
+mv SEQSYN_U.INP ./unpacked/fr_sync_U/Seqsync_U.inp
+
+echo "The ETSI test vectors for GSM 06.10 should now be correctly laid out in the"
+echo "gsm0610 directory"

Added: freeswitch/trunk/libs/spandsp/unpack_v56ter_data.sh
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/unpack_v56ter_data.sh	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,60 @@
+#!/bin/sh
+#
+# SpanDSP - a series of DSP components for telephony
+#
+# unpack_v56ter_data.sh
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU Lesser General Public License version 2.1,
+# as published by the Free Software Foundation.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this program; if not, write to the Free Software
+# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+#
+# $Id: unpack_v56ter_data.sh,v 1.5 2008/05/03 07:55:04 steveu Exp $
+#
+
+ITUDATA="../../../T-REC-V.56ter-199608-I!!ZPF-E.zip"
+
+cd test-data/itu
+if [ -d v56ter ]
+then
+    cd v56ter
+else
+    mkdir v56ter
+    RETVAL=$?
+    if [ $RETVAL != 0 ]
+    then
+        echo Cannot create test-data/itu/v56ter!
+        exit $RETVAL
+    fi
+    cd v56ter
+fi
+
+rm -rf software
+rm -rf *.TST
+unzip ${ITUDATA} >/dev/null
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    echo Cannot unpack the ITU test vectors for V.56ter!
+    exit $RETVAL
+fi
+#rm ${ITUDATA}
+unzip software/V56ter/V56tere/Software.zip >/dev/null
+RETVAL=$?
+if [ $RETVAL != 0 ]
+then
+    echo Cannot unpack the ITU test vectors for V.56ter!
+    exit $RETVAL
+fi
+mv ./software/V56ter/V56tere/*.TST .
+chmod 644 *.TST
+rm -rf software
+echo The ITU test vectors for V.56ter should now be in the v56ter directory

Added: freeswitch/trunk/libs/spandsp/wrapper.xsl
==============================================================================
--- (empty file)
+++ freeswitch/trunk/libs/spandsp/wrapper.xsl	Wed Sep  3 15:02:00 2008
@@ -0,0 +1,5 @@
+<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
+                version='1.0'>
+  <xsl:import href="http://docbook.sourceforge.net/release/xsl/current/xhtml/chunk.xsl"/>
+  <xsl:param name="html.stylesheet">css.css</xsl:param>
+</xsl:stylesheet>
\ No newline at end of file



More information about the Freeswitch-svn mailing list