decoder.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*************************************************************************
  2. *
  3. * GSM AMR-NB speech codec R98 Version 7.6.0 December 12, 2001
  4. * R99 Version 3.3.0
  5. * REL-4 Version 4.1.0
  6. *
  7. ********************************************************************************
  8. *
  9. * File : decoder.c
  10. * Purpose : Speech decoder main program.
  11. *
  12. ********************************************************************************
  13. *
  14. * Usage : decoder bitstream_file synth_file
  15. *
  16. *
  17. * Format for bitstream_file:
  18. * 1 word (2-byte) for the frame type
  19. * (see frame.h for possible values)
  20. * Normally, the TX frame type is expected.
  21. * RX frame type can be forced with "-rxframetype"
  22. * 244 words (2-byte) containing 244 bits.
  23. * Bit 0 = 0x0000 and Bit 1 = 0x0001
  24. * 1 word (2-byte) for the mode indication
  25. * (see mode.h for possible values)
  26. * 4 words for future use, currently unused
  27. *
  28. * Format for synth_file:
  29. * Synthesis is written to a binary file of 16 bits data.
  30. *
  31. ********************************************************************************
  32. */
  33. /*
  34. ********************************************************************************
  35. * INCLUDE FILES
  36. ********************************************************************************
  37. */
  38. #include <stdio.h>
  39. #include <stdlib.h>
  40. #include <string.h>
  41. #include <errno.h>
  42. #include "typedef.h"
  43. #include "n_proc.h"
  44. #include "cnst.h"
  45. #include "mode.h"
  46. #include "frame.h"
  47. #include "strfunc.h"
  48. #include "sp_dec.h"
  49. #include "d_homing.h"
  50. #ifdef MMS_IO
  51. #define AMR_MAGIC_NUMBER "#!AMR\n"
  52. #define MAX_PACKED_SIZE (MAX_SERIAL_SIZE / 8 + 2)
  53. #endif
  54. const char decoder_id[] = "@(#)$Id $";
  55. /* frame size in serial bitstream file (frame type + serial stream + flags) */
  56. #define SERIAL_FRAMESIZE (1+MAX_SERIAL_SIZE+5)
  57. /*
  58. ********************************************************************************
  59. * LOCAL PROGRAM CODE
  60. ********************************************************************************
  61. */
  62. static enum RXFrameType tx_to_rx (enum TXFrameType tx_type)
  63. {
  64. switch (tx_type) {
  65. case TX_SPEECH_GOOD: return RX_SPEECH_GOOD;
  66. case TX_SPEECH_DEGRADED: return RX_SPEECH_DEGRADED;
  67. case TX_SPEECH_BAD: return RX_SPEECH_BAD;
  68. case TX_SID_FIRST: return RX_SID_FIRST;
  69. case TX_SID_UPDATE: return RX_SID_UPDATE;
  70. case TX_SID_BAD: return RX_SID_BAD;
  71. case TX_ONSET: return RX_ONSET;
  72. case TX_NO_DATA: return RX_NO_DATA;
  73. default:
  74. wfprintf(stderr, "tx_to_rx: unknown TX frame type %d\n", tx_type);
  75. exit(1);
  76. }
  77. }
  78. /*
  79. ********************************************************************************
  80. * MAIN PROGRAM
  81. ********************************************************************************
  82. */
  83. int main (int argc, char *argv[])
  84. {
  85. Speech_Decode_FrameState *speech_decoder_state = NULL;
  86. Word16 serial[SERIAL_FRAMESIZE]; /* coded bits */
  87. Word16 synth[L_FRAME]; /* Synthesis */
  88. Word32 frame;
  89. char *progname = argv[0];
  90. char *fileName = NULL;
  91. char *serialFileName = NULL;
  92. FILE *file_syn, *file_serial;
  93. int rxframetypeMode = 0; /* use RX frame type codes */
  94. enum Mode mode = (enum Mode)0;
  95. enum RXFrameType rx_type = (enum RXFrameType)0;
  96. enum TXFrameType tx_type = (enum TXFrameType)0;
  97. Word16 reset_flag = 0;
  98. Word16 reset_flag_old = 1;
  99. Word16 i;
  100. #ifdef MMS_IO
  101. UWord8 toc, q, ft;
  102. Word8 magic[8];
  103. UWord8 packed_bits[MAX_PACKED_SIZE];
  104. Word16 packed_size[16] = {12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0};
  105. #endif
  106. proc_head ("Decoder");
  107. #ifndef MMS_IO
  108. /*----------------------------------------------------------------------*
  109. * process command line options *
  110. *----------------------------------------------------------------------*/
  111. while (argc > 1) {
  112. if (strcmp(argv[1], "-rxframetype") == 0)
  113. rxframetypeMode = 1;
  114. else break;
  115. argc--;
  116. argv++;
  117. }
  118. #endif
  119. /*----------------------------------------------------------------------*
  120. * check number of arguments *
  121. *----------------------------------------------------------------------*/
  122. if (argc != 3)
  123. {
  124. wfprintf (stderr,
  125. " Usage:\n\n"
  126. #ifndef MMS_IO
  127. " %s [-rxframetype] bitstream_file synth_file\n\n"
  128. " -rxframetype expects the RX frame type in bitstream_file (instead of TX)\n\n",
  129. #else
  130. " %s bitstream_file synth_file\n\n",
  131. #endif
  132. progname);
  133. exit (1);
  134. }
  135. serialFileName = argv[1];
  136. fileName = argv[2];
  137. /*----------------------------------------------------------------------*
  138. * Open serial bit stream and output speech file *
  139. *----------------------------------------------------------------------*/
  140. if (strcmp(serialFileName, "-") == 0) {
  141. file_serial = stdin;
  142. }
  143. else if ((file_serial = fopen (serialFileName, "rb")) == NULL)
  144. {
  145. wfprintf (stderr, "Input file '%s' does not exist !!\n", serialFileName);
  146. exit (0);
  147. }
  148. wfprintf (stderr, "Input bitstream file: %s\n", serialFileName);
  149. if (strcmp(fileName, "-") == 0) {
  150. file_syn = stdout;
  151. }
  152. else if ((file_syn = fopen (fileName, "wb")) == NULL)
  153. {
  154. wfprintf (stderr, "Cannot create output file '%s' !!\n", fileName);
  155. exit (0);
  156. }
  157. wfprintf (stderr, "Synthesis speech file: %s\n", fileName);
  158. #ifdef MMS_IO
  159. /* read and verify magic number */
  160. fread(magic, sizeof(Word8), strlen(AMR_MAGIC_NUMBER), file_serial);
  161. if (strncmp((const char *)magic, AMR_MAGIC_NUMBER, strlen(AMR_MAGIC_NUMBER)))
  162. {
  163. wfprintf(stderr, "%s%s\n", "Invalid magic number: ", magic);
  164. fclose(file_serial);
  165. fclose(file_syn);
  166. return 1;
  167. }
  168. #endif
  169. /*-----------------------------------------------------------------------*
  170. * Initialization of decoder *
  171. *-----------------------------------------------------------------------*/
  172. if (Speech_Decode_Frame_init(&speech_decoder_state, "Decoder"))
  173. exit(-1);
  174. /*-----------------------------------------------------------------------*
  175. * process serial bitstream frame by frame *
  176. *-----------------------------------------------------------------------*/
  177. frame = 0;
  178. #ifndef MMS_IO
  179. while (fread (serial, sizeof (Word16), SERIAL_FRAMESIZE, file_serial)
  180. == SERIAL_FRAMESIZE)
  181. {
  182. /* get frame type and mode information from frame */
  183. if (rxframetypeMode) {
  184. rx_type = (enum RXFrameType)serial[0];
  185. } else {
  186. tx_type = (enum TXFrameType)serial[0];
  187. rx_type = tx_to_rx (tx_type);
  188. }
  189. mode = (enum Mode) serial[1+MAX_SERIAL_SIZE];
  190. #else
  191. while (fread (&toc, sizeof(UWord8), 1, file_serial) == 1)
  192. {
  193. /* read rest of the frame based on ToC byte */
  194. q = (toc >> 2) & 0x01;
  195. ft = (toc >> 3) & 0x0F;
  196. fread (packed_bits, sizeof(UWord8), packed_size[ft], file_serial);
  197. rx_type = UnpackBits(q, ft, packed_bits, &mode, &serial[1]);
  198. #endif
  199. ++frame;
  200. if ( (frame%50) == 0) {
  201. wfprintf (stderr, "\rframe=%d ", frame);
  202. }
  203. if (rx_type == RX_NO_DATA) {
  204. mode = speech_decoder_state->prev_mode;
  205. }
  206. else {
  207. speech_decoder_state->prev_mode = mode;
  208. }
  209. /* if homed: check if this frame is another homing frame */
  210. if (reset_flag_old == 1)
  211. {
  212. /* only check until end of first subframe */
  213. reset_flag = decoder_homing_frame_test_first(&serial[1], mode);
  214. }
  215. /* produce encoder homing frame if homed & input=decoder homing frame */
  216. if ((reset_flag != 0) && (reset_flag_old != 0))
  217. {
  218. for (i = 0; i < L_FRAME; i++)
  219. {
  220. synth[i] = EHF_MASK;
  221. }
  222. }
  223. else
  224. {
  225. /* decode frame */
  226. Speech_Decode_Frame(speech_decoder_state, mode, &serial[1],
  227. rx_type, synth);
  228. }
  229. /* write synthesized speech to file */
  230. if (fwrite (synth, sizeof (Word16), L_FRAME, file_syn) != L_FRAME) {
  231. wfprintf(stderr, "\nerror writing output file: %s\n",
  232. strerror(errno));
  233. };
  234. fflush(file_syn);
  235. /* if not homed: check whether current frame is a homing frame */
  236. if (reset_flag_old == 0)
  237. {
  238. /* check whole frame */
  239. reset_flag = decoder_homing_frame_test(&serial[1], mode);
  240. }
  241. /* reset decoder if current frame is a homing frame */
  242. if (reset_flag != 0)
  243. {
  244. Speech_Decode_Frame_reset(speech_decoder_state);
  245. }
  246. reset_flag_old = reset_flag;
  247. }
  248. wfprintf (stderr, "\n%d frame(s) processed\n", frame);
  249. /*-----------------------------------------------------------------------*
  250. * Close down speech decoder *
  251. *-----------------------------------------------------------------------*/
  252. Speech_Decode_Frame_exit(&speech_decoder_state);
  253. return 0;
  254. }