main.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  1. /**********************************************************************
  2. Each of the companies; Lucent, Motorola, Nokia, and Qualcomm (hereinafter
  3. referred to individually as "Source" or collectively as "Sources") do
  4. hereby state:
  5. To the extent to which the Source(s) may legally and freely do so, the
  6. Source(s), upon submission of a Contribution, grant(s) a free,
  7. irrevocable, non-exclusive, license to the Third Generation Partnership
  8. Project 2 (3GPP2) and its Organizational Partners: ARIB, CCSA, TIA, TTA,
  9. and TTC, under the Source's copyright or copyright license rights in the
  10. Contribution, to, in whole or in part, copy, make derivative works,
  11. perform, display and distribute the Contribution and derivative works
  12. thereof consistent with 3GPP2's and each Organizational Partner's
  13. policies and procedures, with the right to (i) sublicense the foregoing
  14. rights consistent with 3GPP2's and each Organizational Partner's policies
  15. and procedures and (ii) copyright and sell, if applicable) in 3GPP2's name
  16. or each Organizational Partner's name any 3GPP2 or transposed Publication
  17. even though this Publication may contain the Contribution or a derivative
  18. work thereof. The Contribution shall disclose any known limitations on
  19. the Source's rights to license as herein provided.
  20. When a Contribution is submitted by the Source(s) to assist the
  21. formulating groups of 3GPP2 or any of its Organizational Partners, it
  22. is proposed to the Committee as a basis for discussion and is not to
  23. be construed as a binding proposal on the Source(s). The Source(s)
  24. specifically reserve(s) the right to amend or modify the material
  25. contained in the Contribution. Nothing contained in the Contribution
  26. shall, except as herein expressly provided, be construed as conferring
  27. by implication, estoppel or otherwise, any license or right under (i)
  28. any existing or later issuing patent, whether or not the use of
  29. information in the document necessarily employs an invention of any
  30. existing or later issued patent, (ii) any copyright, (iii) any
  31. trademark, or (iv) any other intellectual property right.
  32. With respect to the Software necessary for the practice of any or
  33. all Normative portions of the Enhanced Variable Rate Codec (EVRC) as
  34. it exists on the date of submittal of this form, should the EVRC be
  35. approved as a Specification or Report by 3GPP2, or as a transposed
  36. Standard by any of the 3GPP2's Organizational Partners, the Source(s)
  37. state(s) that a worldwide license to reproduce, use and distribute the
  38. Software, the license rights to which are held by the Source(s), will
  39. be made available to applicants under terms and conditions that are
  40. reasonable and non-discriminatory, which may include monetary compensation,
  41. and only to the extent necessary for the practice of any or all of the
  42. Normative portions of the EVRC or the field of use of practice of the
  43. EVRC Specification, Report, or Standard. The statement contained above
  44. is irrevocable and shall be binding upon the Source(s). In the event
  45. the rights of the Source(s) in and to copyright or copyright license
  46. rights subject to such commitment are assigned or transferred, the
  47. Source(s) shall notify the assignee or transferee of the existence of
  48. such commitments.
  49. *******************************************************************/
  50. /*======================================================================*/
  51. /* Enhanced Variable Rate Codec - Bit-Exact C Specification */
  52. /* Copyright (C) 1997-1998 Telecommunications Industry Association. */
  53. /* All rights reserved. */
  54. /*----------------------------------------------------------------------*/
  55. /* Note: Reproduction and use of this software for the design and */
  56. /* development of North American Wideband CDMA Digital */
  57. /* Cellular Telephony Standards is authorized by the TIA. */
  58. /* The TIA does not authorize the use of this software for any */
  59. /* other purpose. */
  60. /* */
  61. /* The availability of this software does not provide any license */
  62. /* by implication, estoppel, or otherwise under any patent rights */
  63. /* of TIA member companies or others covering any use of the */
  64. /* contents herein. */
  65. /* */
  66. /* Any copies of this software or derivative works must include */
  67. /* this and all other proprietary notices. */
  68. /*======================================================================*/
  69. /* Memory Usage: */
  70. /* ROM: */
  71. /* Static/Global RAM: */
  72. /* Stack/Local RAM: */
  73. /*----------------------------------------------------------------------*/
  74. /*======================================================================*/
  75. /* ..Includes. */
  76. /*----------------------------------------------------------------------*/
  77. #include <stdio.h>
  78. #include <stdlib.h>
  79. #include <string.h>
  80. /*#include <unistd.h>*/
  81. #include "getopt.h"
  82. #include "typedefs.h"
  83. #include "mathevrc.h"
  84. #include "macro.h"
  85. #include "bqiir.h"
  86. #include "decode.h"
  87. #include "encode.h"
  88. #include "ns127.h"
  89. #include "rda.h"
  90. #include "tty.h"
  91. #include "tty_dbg.h"
  92. /*======================================================================*/
  93. /* ..Defines. */
  94. /*----------------------------------------------------------------------*/
  95. /* Version 2.0 21 Oct 2002
  96. * ANSI-127 fixes:
  97. * -Bottom guard for offset in cshift.c
  98. * -Erasing all-zeros Packets
  99. * -Muting after multiple all-ones packets
  100. * -Fixed seed for frame erasures
  101. * -Add delta delay sanity check
  102. * -LSP bandwidth expansion for frame erasures
  103. * TTY for 45.45 baud and 50 baud
  104. * Minimum input energy threshold in TTY detector
  105. *
  106. * Version 2.1 15 Apr 2003
  107. * - Changed decode.c so that acb_gain = 0 before ORing the gains
  108. * from each of the subframes for half rate frames to determine
  109. * if the frame is a TTY frame.
  110. *
  111. * Version 2.2 8 Dec 2003
  112. * - Changed the order of linking the libraries and eliminated
  113. * routines with the same name because of a conflict with
  114. * routines with the same name in libraries dmath.a and dsp40.a.
  115. *
  116. * Version 2.3 25 Jan 2004
  117. * -Added -s <filename> option for signaling file to support dim &
  118. * burst simulation.
  119. */
  120. #define PROGRAM_VERSION "2.3"
  121. #define PROGRAM_DATE "25 Jan 2004"
  122. #define SPEECH_BUFFER_LEN 160
  123. #define LOOKAHEAD_LEN 80
  124. #define BITSTREAM_BUFFER_LEN 12 /* data + 1 word for rate */
  125. /*======================================================================*/
  126. /* ..Structs. */
  127. /*----------------------------------------------------------------------*/
  128. typedef struct
  129. {
  130. char *input_filename;
  131. char *output_filename;
  132. Shortword max_frames;
  133. Shortword encode_only;
  134. Shortword decode_only;
  135. Shortword max_rate;
  136. Shortword max_rate_default;
  137. Shortword min_rate;
  138. Shortword min_rate_default;
  139. Shortword post_filter;
  140. Shortword post_filter_default;
  141. Shortword noise_suppression;
  142. Shortword noise_suppression_default;
  143. Shortword ibuf_len;
  144. Shortword obuf_len;
  145. char *signaling_filename;
  146. FILE *fp_signaling;
  147. }
  148. EvrcArgs;
  149. typedef struct
  150. {
  151. Longword max_ops;
  152. Longword max_fnum;
  153. Longword min_ops;
  154. Longword min_fnum;
  155. float avg_ops;
  156. char name[80];
  157. } op_struct;
  158. /*======================================================================*/
  159. /* ..Print banner. */
  160. /*----------------------------------------------------------------------*/
  161. void banner(FILE * fp)
  162. {
  163. /*....execute.... */
  164. wfprintf(fp, "\n\
  165. ---------------------------------------------------------------------\n\
  166. >>>>>>>>>>>>>>>>> CDMA Enhanced Variable-Rate Codec <<<<<<<<<<<<<<<<<\n\
  167. <<<<<<<<<<<<<<<<<<<<<< Fixed-point C simulation >>>>>>>>>>>>>>>>>>>>>\n\
  168. >>>>>>>>>>>>>>>>>>> - Version %s, %s - <<<<<<<<<<<<<<<<<<<\n\
  169. ---------------------------------------------------------------------\n",
  170. PROGRAM_VERSION,
  171. PROGRAM_DATE);
  172. }
  173. /*======================================================================*/
  174. /* ..Usage. */
  175. /*----------------------------------------------------------------------*/
  176. void usage( FILE * fp,
  177. char *prog_name)
  178. {
  179. /*....execute.... */
  180. banner(fp);
  181. wfprintf(fp, "\
  182. usage:\n\
  183. %s <required_args> [optional_args]\n\
  184. \n\
  185. required_args:\n\
  186. -i <filename> Input filename.\n\
  187. -o <filename> Output filename.\n\
  188. \n\
  189. optional_args:\n\
  190. -e Encode only (infile=speech, outfile = bitstream).\n\
  191. -d Decode only (infile=bitstream, outfile = speech).\n\
  192. -h <rate> Maximum rate, 1(eighth),3(half), or 4(full); (default=4).\n\
  193. -l <rate> Minimum rate, 1(eighth),3(half), or 4(full); (default=1).\n\
  194. -p <1 or 0> Enable (1)/disable (0) post filter, (default=1).\n\
  195. -n <1 or 0> Enable (1)/disable (0) noise suppression, (default=1).\n\
  196. -f <maxframe> Set maximum number of frames to process.\n\
  197. -s <filename> Signaling filename\n\
  198. -t <1 or 0> Turn ON(1)/OFF(0) TTY Mode.(default=1)\n\
  199. \n",
  200. prog_name
  201. );
  202. }
  203. /*======================================================================*/
  204. /* ..Print command line arguments. */
  205. /*----------------------------------------------------------------------*/
  206. void print_eargs( FILE *fp,
  207. EvrcArgs *e)
  208. {
  209. /*....execute.... */
  210. banner(fp);
  211. if (e->encode_only && !e->decode_only)
  212. {
  213. e->ibuf_len = SPEECH_BUFFER_LEN;
  214. e->obuf_len = BITSTREAM_BUFFER_LEN - 1;
  215. wfprintf(fp, " input_speechfile == \"%s\"\n", e->input_filename);
  216. wfprintf(fp, " output_bitstream => \"%s\"\n", e->output_filename);
  217. }
  218. else if (e->decode_only && !e->encode_only)
  219. {
  220. e->ibuf_len = BITSTREAM_BUFFER_LEN - 1;
  221. e->obuf_len = SPEECH_BUFFER_LEN;
  222. wfprintf(fp, " input_bitstream => \"%s\"\n", e->input_filename);
  223. wfprintf(fp, " output_speechfile == \"%s\"\n", e->output_filename);
  224. }
  225. else if (!e->encode_only && !e->decode_only)
  226. {
  227. e->ibuf_len = SPEECH_BUFFER_LEN;
  228. e->obuf_len = SPEECH_BUFFER_LEN;
  229. wfprintf(fp, " input_speechfile == \"%s\"\n", e->input_filename);
  230. wfprintf(fp, " output_speechfile == \"%s\"\n", e->output_filename);
  231. }
  232. if (e->signaling_filename != NULL)
  233. {
  234. wfprintf(fp," signaling file == \"%s\"\n",e->signaling_filename);
  235. }
  236. if (e->max_rate_default)
  237. {
  238. wfprintf(fp, " max_rate == %d\n", e->max_rate);
  239. }
  240. else
  241. {
  242. wfprintf(fp, " max_rate => %d\n", e->max_rate);
  243. }
  244. if (e->min_rate_default)
  245. {
  246. wfprintf(fp, " min_rate == %d\n", e->min_rate);
  247. }
  248. else
  249. {
  250. wfprintf(fp, " min_rate => %d\n", e->min_rate);
  251. }
  252. if (e->post_filter)
  253. {
  254. if (e->post_filter_default)
  255. {
  256. wfprintf(fp, " post_filter == ON\n");
  257. }
  258. else
  259. {
  260. wfprintf(fp, " post_filter => ON\n");
  261. }
  262. }
  263. else
  264. {
  265. if (e->post_filter_default)
  266. {
  267. wfprintf(fp, " post_filter == OFF\n");
  268. }
  269. else
  270. {
  271. wfprintf(fp, " post_filter => OFF\n");
  272. }
  273. }
  274. if (e->noise_suppression)
  275. {
  276. if (e->noise_suppression_default)
  277. {
  278. wfprintf(fp, " noise_suppression == ON\n");
  279. }
  280. else
  281. {
  282. wfprintf(fp, " noise_suppression => ON\n");
  283. }
  284. }
  285. else
  286. {
  287. if (e->noise_suppression_default)
  288. {
  289. wfprintf(fp, " noise_suppression == OFF\n");
  290. }
  291. else
  292. {
  293. wfprintf(fp, " noise_suppression => OFF\n");
  294. }
  295. }
  296. if( tty_option )
  297. {
  298. wfprintf(fp, " TTY => ON\n");
  299. }
  300. else
  301. {
  302. wfprintf(fp, " TTY => OFF\n");
  303. }
  304. wfprintf(fp, "---------------------------------------------------------------------\n");
  305. }
  306. /*======================================================================*/
  307. /* ..Get command line arguments. */
  308. /*----------------------------------------------------------------------*/
  309. EvrcArgs *get_eargs(
  310. int argc,
  311. char *argv[]
  312. )
  313. {
  314. /*....(local) variables.... */
  315. EvrcArgs *eargs;
  316. Shortword option;
  317. /*....execute.... */
  318. if (argc < 5)
  319. {
  320. eargs = NULL;
  321. }
  322. else
  323. {
  324. if ((eargs = (EvrcArgs *) malloc(sizeof(EvrcArgs))) == NULL)
  325. {
  326. wfprintf(stderr, "%s: ERROR - Unable to malloc arg memory.\n", argv[0]);
  327. exit(-1);
  328. }
  329. eargs->input_filename = NULL;
  330. eargs->output_filename = NULL;
  331. eargs->encode_only = 0;
  332. eargs->decode_only = 0;
  333. eargs->max_rate = 4;
  334. eargs->max_rate_default = 4;
  335. eargs->min_rate = 1;
  336. eargs->min_rate_default = 1;
  337. eargs->post_filter = 1;
  338. eargs->post_filter_default = 1;
  339. eargs->noise_suppression = 1;
  340. eargs->noise_suppression_default = 1;
  341. eargs->ibuf_len = SPEECH_BUFFER_LEN;
  342. eargs->obuf_len = SPEECH_BUFFER_LEN;
  343. eargs->max_frames = 0x7fff;
  344. tty_option = 1;
  345. eargs->signaling_filename = NULL;
  346. eargs->fp_signaling = NULL;
  347. while ((option = getopt(argc, argv, "i:o:redh:l:p:n:c:f:t:D:s:")) != EOF)
  348. {
  349. switch (option)
  350. {
  351. case 'i':
  352. eargs->input_filename = argv[optind - 1];
  353. break;
  354. case 'o':
  355. eargs->output_filename = argv[optind - 1];
  356. break;
  357. case 'e':
  358. eargs->encode_only = 1;
  359. if (eargs->decode_only)
  360. {
  361. wfprintf(stderr, "%s: ERROR - Encode with decode.\n", argv[0]);
  362. exit(-1);
  363. }
  364. break;
  365. case 'd':
  366. eargs->decode_only = 1;
  367. if (eargs->encode_only)
  368. {
  369. wfprintf(stderr, "%s: ERROR - Decode with encode.\n", argv[0]);
  370. exit(-1);
  371. }
  372. break;
  373. case 'h':
  374. eargs->max_rate = (Shortword) atoi(argv[optind - 1]);
  375. if (eargs->max_rate > 4)
  376. eargs->max_rate = 4;
  377. if (eargs->max_rate < 1)
  378. eargs->max_rate = 1;
  379. eargs->max_rate_default = 0;
  380. break;
  381. case 'l':
  382. eargs->min_rate = (Shortword) atoi(argv[optind - 1]);
  383. if (eargs->min_rate > 4)
  384. eargs->min_rate = 4;
  385. if (eargs->min_rate < 1)
  386. eargs->min_rate = 1;
  387. eargs->min_rate_default = 0;
  388. break;
  389. case 'p':
  390. eargs->post_filter = (Shortword) atoi(argv[optind - 1]);
  391. if (eargs->post_filter != 0)
  392. eargs->post_filter = 1;
  393. eargs->post_filter_default = 0;
  394. break;
  395. case 'n':
  396. eargs->noise_suppression = (Shortword) atoi(argv[optind - 1]);
  397. if (eargs->noise_suppression != 0)
  398. eargs->noise_suppression = 1;
  399. eargs->noise_suppression_default = 0;
  400. break;
  401. case 'f':
  402. eargs->max_frames = (Shortword) atoi(argv[optind - 1]);
  403. break;
  404. case 't':
  405. tty_option = (short)atoi(argv[optind-1]);
  406. break;
  407. case 'D':
  408. tty_debug_print_flag = (short) atoi(argv[optind-1]) & TTY_DEBUG_PRINT;
  409. tty_debug_flag = (short) atoi(argv[optind-1]) & TTY_DEBUG_DUMP;
  410. break;
  411. case 's':
  412. eargs->signaling_filename = argv[optind-1];
  413. eargs->fp_signaling = fopen(argv[optind-1],"rb");
  414. if( eargs->fp_signaling == NULL )
  415. {
  416. wfprintf(stderr,"Error opening signaling file %s\n",argv[optind-1]);
  417. exit(-1);
  418. }
  419. break;
  420. }
  421. }
  422. if (eargs->min_rate > eargs->max_rate)
  423. {
  424. eargs->min_rate = eargs->max_rate;
  425. }
  426. }
  427. return (eargs);
  428. }
  429. /*======================================================================*/
  430. /* ..Returns number of frames in a binary file. */
  431. /*----------------------------------------------------------------------*/
  432. Shortword GetNumFrames( FILE * fp,
  433. Shortword blocksize)
  434. {
  435. /*....(local) variables.... */
  436. Shortword position;
  437. Shortword numFrames;
  438. /*....execute.... */
  439. position = ftell(fp);
  440. fseek(fp, 0L, 2);
  441. numFrames = ftell(fp) / blocksize;
  442. fseek(fp, position, 0);
  443. return (numFrames);
  444. }
  445. void reset_op_stats(op_struct *op_stat, char *name)
  446. {
  447. op_stat->avg_ops=0;
  448. op_stat->max_ops=0;
  449. op_stat->min_ops=0x7fffffffL;
  450. strcpy(op_stat->name,name);
  451. }
  452. void update_op_stats(op_struct *op_stat, Longword ops, Longword fnum)
  453. {
  454. op_stat->avg_ops+=ops;
  455. if (ops > op_stat->max_ops)
  456. {
  457. op_stat->max_ops=ops;
  458. op_stat->max_fnum=fnum;
  459. }
  460. if (ops < op_stat->min_ops)
  461. {
  462. op_stat->min_ops=ops;
  463. op_stat->min_fnum=fnum;
  464. }
  465. }
  466. void print_op_stats(FILE *fp, op_struct *op_stat, Longword fnum)
  467. {
  468. float avg;
  469. float avg_mops;
  470. avg = op_stat->avg_ops/(float)fnum;
  471. avg_mops = avg*50.0*1.0e-6;
  472. wfprintf(fp,"%s Average OPS/Frame : %6.0f\n",op_stat->name,avg);
  473. wfprintf(fp,"%s Average MOPS : %2.03f\n",op_stat->name,avg_mops);
  474. wfprintf(fp,"%s Max OPS/Frame : %ld\n",op_stat->name,op_stat->max_ops);
  475. wfprintf(fp,"%s Max OPS at Frame #: %ld\n",op_stat->name,op_stat->max_fnum);
  476. wfprintf(fp,"%s Min OPS/Frame : %ld\n",op_stat->name,op_stat->min_ops);
  477. wfprintf(fp,"%s Min OPS at Frame #: %ld\n\n",op_stat->name,op_stat->min_fnum);
  478. }
  479. /*======================================================================*/
  480. /* ..Speech encoder. */
  481. /*----------------------------------------------------------------------*/
  482. void Encode(
  483. Shortword rate,
  484. Shortword * obuffer
  485. )
  486. {
  487. /*....execute.... */
  488. encode(rate, obuffer);
  489. post_encode();
  490. }
  491. /*======================================================================*/
  492. /* ..Speech decoder. */
  493. /*----------------------------------------------------------------------*/
  494. void Decode(
  495. Shortword * ibuffer,
  496. Shortword rate,
  497. Shortword post_filter,
  498. Shortword * obuffer
  499. )
  500. {
  501. /*....execute.... */
  502. decode(ibuffer, rate, post_filter, obuffer);
  503. }
  504. /*======================================================================*/
  505. /* ..Main. */
  506. /*----------------------------------------------------------------------*/
  507. int main(int argc, char *argv[])
  508. {
  509. /*....(local) variables.... */
  510. EvrcArgs *eargs;
  511. op_struct encode_ops;
  512. op_struct decode_ops;
  513. op_struct ns_ops;
  514. op_struct rda_ops;
  515. op_struct hpf_ops;
  516. op_struct pe_ops;
  517. FILE *ifileP;
  518. FILE *ofileP;
  519. Shortword buf[SPEECH_BUFFER_LEN];
  520. Shortword buf16[BITSTREAM_BUFFER_LEN];
  521. Shortword buf_count;
  522. Shortword ibuf_len;
  523. Shortword obuf_len;
  524. Shortword j;
  525. Shortword rate;
  526. Shortword beta;
  527. float rate_sum;
  528. float avg_rate;
  529. Longword R[17];
  530. Shortword min_rate;
  531. Shortword max_rate;
  532. char signaling;
  533. Shortword k;
  534. /*....execute.... */
  535. /*...get arguments and check usage... */
  536. if ((eargs = get_eargs(argc, argv)) == NULL)
  537. {
  538. usage(stdout, argv[0]);
  539. exit(-1);
  540. }
  541. print_eargs(stdout, eargs);
  542. /*...open files... */
  543. if ((ifileP = fopen(eargs->input_filename, "rb")) == NULL)
  544. {
  545. wfprintf(stderr, "%s: ERROR - Unable to open input file \"%s\".\n",
  546. argv[0],
  547. eargs->input_filename
  548. );
  549. exit(-1);
  550. }
  551. if ((ofileP = fopen(eargs->output_filename, "wb")) == NULL)
  552. {
  553. wfprintf(stderr, "%s: ERROR - Unable to open output file \"%s\".\n",
  554. argv[0],
  555. eargs->output_filename
  556. );
  557. exit(-1);
  558. }
  559. /*...loop counter max... */
  560. if (eargs->decode_only)
  561. {
  562. buf_count = GetNumFrames(ifileP, sizeof(Shortword) * BITSTREAM_BUFFER_LEN);
  563. }
  564. else
  565. {
  566. buf_count = GetNumFrames(ifileP, sizeof(Shortword) * SPEECH_BUFFER_LEN);
  567. }
  568. /*...processing loop... */
  569. reset_op_stats(&encode_ops,"Encode\0");
  570. reset_op_stats(&decode_ops,"Decode\0");
  571. reset_op_stats(&pe_ops,"Pre-Encode\0");
  572. reset_op_stats(&ns_ops,"Noise Supression\0");
  573. reset_op_stats(&hpf_ops,"BQIIR\0");
  574. reset_op_stats(&rda_ops,"RDA\0");
  575. InitEncoder();
  576. InitDecoder();
  577. if( tty_option == TTY_NO_GAIN )
  578. {
  579. init_tty_enc( &tty_enc_char, &tty_enc_header, &tty_enc_baud_rate);
  580. init_tty_dec();
  581. tty_debug();
  582. }
  583. rate_sum = 0.0;
  584. avg_rate = 0.0;
  585. ibuf_len = eargs->ibuf_len;
  586. obuf_len = eargs->obuf_len;
  587. j = 1;
  588. if (!eargs->decode_only)
  589. {
  590. while (((fread(buf, sizeof(Shortword), ibuf_len, ifileP)) == ibuf_len)
  591. && (j<=eargs->max_frames))
  592. {
  593. OP_RESET;
  594. bqiir(buf);
  595. update_op_stats(&hpf_ops,op_counter,j);
  596. if( tty_option == TTY_NO_GAIN )
  597. {
  598. tty_enc_flag = tty_enc( &tty_enc_char,
  599. &tty_enc_header,
  600. &tty_enc_baud_rate,
  601. buf,
  602. FrameSize );
  603. }
  604. if( (tty_option == TTY_DISABLED || tty_enc_flag == 0)
  605. && eargs->noise_suppression)
  606. {
  607. OP_RESET;
  608. noise_suprs(buf);
  609. noise_suprs(buf + ibuf_len / 2);
  610. }
  611. else
  612. {
  613. for (k=0; k<ibuf_len; k++)
  614. {
  615. buf[k] = shift_r(buf[k],-1);
  616. }
  617. }
  618. update_op_stats(&ns_ops,op_counter,j);
  619. OP_RESET;
  620. beta = pre_encode(buf, R);
  621. update_op_stats(&pe_ops,op_counter,j);
  622. OP_RESET;
  623. if( tty_option == TTY_NO_GAIN && tty_enc_flag != 0 )
  624. {
  625. /* Force to full rate for TTY packets */
  626. min_rate = MIN(4,eargs->max_rate);
  627. }
  628. else
  629. {
  630. min_rate = eargs->min_rate;
  631. }
  632. max_rate = eargs->max_rate;
  633. if( eargs->fp_signaling != NULL )
  634. {
  635. k = fread(&signaling, sizeof(char), 1, eargs->fp_signaling);
  636. if( k == 0 )
  637. {
  638. fseek(eargs->fp_signaling,0,0);
  639. k = fread(&signaling, sizeof(char), 1, eargs->fp_signaling);
  640. if( k == 0 )
  641. {
  642. wfprintf(stderr,"Error reading signaling file %s: File Empty!\n",eargs->signaling_filename);
  643. exit(-1);
  644. }
  645. }
  646. if( signaling )
  647. {
  648. max_rate = MIN(3,eargs->max_rate);
  649. }
  650. }
  651. rate = select_rate(R, max_rate, min_rate, beta);
  652. update_op_stats(&rda_ops,op_counter,j);
  653. OP_RESET;
  654. switch (rate)
  655. {
  656. case 1:
  657. rate_sum += 1;
  658. break;
  659. case 3:
  660. rate_sum += 4;
  661. break;
  662. default:
  663. rate_sum += 8;
  664. break;
  665. }
  666. avg_rate = (rate_sum / (float) j) * 1.2;
  667. Encode(rate, buf16);
  668. update_op_stats(&encode_ops, op_counter, j);
  669. OP_RESET;
  670. if (eargs->encode_only)
  671. {
  672. fwrite(&rate, sizeof(Shortword), 1, ofileP);
  673. fwrite(buf16, sizeof(Shortword), obuf_len, ofileP);
  674. wfprintf(stderr, "Encoding %d of %d avg_rate= %6.2f\r", j, buf_count, avg_rate);
  675. }
  676. else
  677. {
  678. Decode(buf16, rate, eargs->post_filter, buf);
  679. update_op_stats(&decode_ops, op_counter, j);
  680. fwrite(buf, sizeof(Shortword), obuf_len, ofileP);
  681. wfprintf(stderr, "Encoding/Decoding %d of %d avg_rate= %6.2f\r", j, buf_count, avg_rate);
  682. }
  683. j++;
  684. }
  685. }
  686. else
  687. {
  688. while ((fread(&rate, sizeof(Shortword), 1, ifileP)) == 1)
  689. {
  690. if ((fread(buf16, sizeof(Shortword), ibuf_len, ifileP)) == ibuf_len)
  691. {
  692. OP_RESET;
  693. Decode(buf16, rate, eargs->post_filter, buf);
  694. update_op_stats(&decode_ops, op_counter, j);
  695. wfprintf(stderr, "Decoding %d of %d\r", j, buf_count);
  696. fwrite(buf, sizeof(Shortword), obuf_len, ofileP);
  697. j++;
  698. }
  699. }
  700. }
  701. if (eargs->fp_signaling != NULL)
  702. fclose(eargs->fp_signaling);
  703. wfprintf(stderr, "\n");
  704. exit(0);
  705. }