bv32_tests.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * broadvoice - a library for the BroadVoice 16 and 32 codecs
  3. *
  4. * bv32_tests.c -
  5. *
  6. * Adapted by Steve Underwood <steveu@coppice.org> from code which is
  7. * Copyright 2000-2009 Broadcom Corporation
  8. *
  9. * All rights reserved.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU Lesser General Public License version 2.1,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  23. *
  24. * $Id: bv32_tests.c,v 1.1.1.1 2009/11/19 12:10:48 steveu Exp $
  25. */
  26. /*! \file */
  27. #if defined(HAVE_CONFIG_H)
  28. #include "config.h"
  29. #endif
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #define BROADVOICE_EXPOSE_INTERNAL_STRUCTURES
  34. #include "broadvoice.h"
  35. #include "g192_bit_stream.h"
  36. #define G192BITSTREAM
  37. int frame;
  38. int16_t bfi = 0;
  39. static void usage(const char *name)
  40. {
  41. fprintf(stderr, "usage: %s enc|dec input output\n", name);
  42. fprintf(stderr, "\nFormat for speech_file:\n Binary file of 16 kHz sampled 16-bit PCM data.\n");
  43. #if defined(G192BITSTREAM)
  44. fprintf(stderr, "\nFormat for bitstream_file per frame: ITU-T G.192 format\n\
  45. One (2-byte) synchronization word [0x6B21],\n\
  46. One (2-byte) size word,\n\
  47. 160 words (2-byte) containing 160 bits.\n\n");
  48. #else
  49. fprintf(stderr, "\nFormat for bitstream_file per frame: Packed Bits\n");
  50. #endif
  51. exit(1);
  52. }
  53. int main(int argc, char **argv)
  54. {
  55. FILE *fi;
  56. FILE *fo;
  57. FILE *fbdi = NULL;
  58. int enc = 1;
  59. int nread;
  60. int i;
  61. int len;
  62. int16_t x[BV32_FRAME_LEN];
  63. bv32_encode_state_t *cs;
  64. bv32_decode_state_t *ds;
  65. uint8_t PackedStream[20];
  66. int next_bad_frame = -1;
  67. int packing;
  68. if ((argc != 4) && (argc != 5))
  69. usage(argv[0]);
  70. if (!strcmp(argv[1], "enc"))
  71. enc = 1;
  72. else if (!strcmp(argv[1], "dec"))
  73. enc = 0;
  74. else
  75. usage(argv[0]);
  76. if (!(fi = fopen(argv[2], "rb")))
  77. {
  78. fprintf(stderr, "error: can't read %s\n", argv[2]);
  79. exit(2);
  80. }
  81. if (!(fo = fopen(argv[3], "wb")))
  82. {
  83. fprintf(stderr, "error: can't write to %s\n", argv[3]);
  84. exit(3);
  85. }
  86. if (argc == 5)
  87. {
  88. if (!(fbdi = fopen(argv[4], "rb")))
  89. {
  90. fprintf(stderr, "error: can't read %s\n", argv[4]);
  91. exit(3);
  92. }
  93. }
  94. if (enc)
  95. {
  96. fprintf(stderr, " BroadVoice32 Encoder V1.0 with ITU-T G.192\n");
  97. fprintf(stderr, " Input speech file : %s\n", argv[2]);
  98. fprintf(stderr, " Output bit-stream file: %s\n", argv[3]);
  99. }
  100. else
  101. {
  102. fprintf(stderr, " BroadVoice32 Decoder V1.0 with ITU-T G.192\n");
  103. fprintf(stderr, " Input bit-stream file : %s\n", argv[2]);
  104. fprintf(stderr, " Output speech file : %s\n", argv[3]);
  105. }
  106. #if defined(G192BITSTREAM)
  107. packing = ITU_CODEC_BITSTREAM_G192;
  108. #else
  109. packing = ITU_CODEC_BITSTREAM_PACKED;
  110. #endif
  111. cs = NULL;
  112. ds = NULL;
  113. if (enc)
  114. cs = bv32_encode_init(NULL);
  115. else
  116. ds = bv32_decode_init(NULL);
  117. frame = 0;
  118. /* Read for the 1st bad frame */
  119. if (fbdi != NULL)
  120. fscanf(fbdi, "%d", &next_bad_frame);
  121. for (;;)
  122. {
  123. frame++;
  124. #if 0
  125. /* Floating only */
  126. if (frame == 1737)
  127. frame++;
  128. #endif
  129. /* Read one speech frame */
  130. if (enc == 1)
  131. {
  132. nread = fread(x, sizeof(int16_t), BV32_FRAME_LEN, fi);
  133. if (nread <= 0)
  134. break;
  135. for (i = nread; i < BV32_FRAME_LEN; i++)
  136. x[i] = 0;
  137. len = bv32_encode(cs, PackedStream, x, BV32_FRAME_LEN);
  138. itu_codec_bitstream_write(PackedStream, 8*len, packing, fo);
  139. }
  140. else
  141. {
  142. nread = itu_codec_bitstream_read(PackedStream, &bfi, 160, packing, fi);
  143. if (nread <= 0)
  144. break;
  145. if (frame == next_bad_frame)
  146. {
  147. fscanf(fbdi, "%d", &next_bad_frame);
  148. bfi = 1;
  149. }
  150. if (bfi)
  151. len = bv32_fillin(ds, x, BV32_FRAME_LEN);
  152. else
  153. len = bv32_decode(ds, x, PackedStream, 20);
  154. fwrite(x, sizeof(int16_t), len, fo);
  155. }
  156. }
  157. if (enc)
  158. bv32_encode_free(cs);
  159. else
  160. bv32_decode_free(ds);
  161. fprintf(stderr, "\r %d %d-sample frames processed.\n", --frame, BV32_FRAME_LEN);
  162. fclose(fi);
  163. fclose(fo);
  164. if (fbdi != NULL)
  165. fclose(fbdi);
  166. fprintf(stderr, "\n\n");
  167. return 0;
  168. }