decoder.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * ===================================================================
  3. * TS 26.104
  4. * REL-5 V5.4.0 2004-03
  5. * REL-6 V6.1.0 2004-03
  6. * REL-15 V15.1.0 2018-07
  7. * 3GPP AMR Floating-point Speech Codec
  8. * ===================================================================
  9. *
  10. */
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include "interf_dec.h"
  14. #include "sp_dec.h"
  15. #include "typedef.h"
  16. #include "board.h"
  17. #ifndef ETSI
  18. #ifndef IF2
  19. #include <string.h>
  20. #define AMR_MAGIC_NUMBER "#!AMR\n"
  21. #endif
  22. #endif
  23. #define PATH_STRING_MAX_LEN 256
  24. char file_speech_path[PATH_STRING_MAX_LEN];
  25. char file_analysis_path[PATH_STRING_MAX_LEN];
  26. static void DecoderUsage(void)
  27. {
  28. rt_kprintf("Usage: decoder [analysis_file] [speech_file]\n");
  29. rt_kprintf(" example : decoder out.amr out.wav\n");
  30. }
  31. void DeocoderCopyright(void)
  32. {
  33. rt_kprintf("Copyright:\n");
  34. rt_kprintf("========================================\n");
  35. rt_kprintf(" TS 26.104 \n");
  36. rt_kprintf(" REL-16 V16.0.0 2018-09 \n");
  37. rt_kprintf(" 3GPP AMR Floating-point Speech Encoder \n");
  38. rt_kprintf("========================================\n");
  39. }
  40. /*
  41. * main
  42. *
  43. *
  44. * Function:
  45. * Speech decoder main program
  46. *
  47. * Usage: decoder bitstream_file synthesis_file
  48. *
  49. * Format for ETSI bitstream file:
  50. * 1 word (2-byte) for the TX frame type
  51. * 244 words (2-byte) containing 244 bits.
  52. * Bit 0 = 0x0000 and Bit 1 = 0x0001
  53. * 1 word (2-byte) for the mode indication
  54. * 4 words for future use, currently written as zero
  55. *
  56. * Format for 3GPP bitstream file:
  57. * Holds mode information and bits packed to octets.
  58. * Size is from 1 byte to 31 bytes.
  59. *
  60. * Format for synthesis_file:
  61. * Speech is written to a 16 bit 8kHz file.
  62. *
  63. * ETSI bitstream file format is defined using ETSI as preprocessor
  64. * definition
  65. * Returns:
  66. * 0
  67. */
  68. static void decoder_thread(void *parameter)
  69. {
  70. FILE *file_speech, *file_analysis;
  71. short synth[160];
  72. int frames = 0;
  73. int *destate;
  74. int read_size;
  75. #ifndef ETSI
  76. unsigned char analysis[32];
  77. enum Mode dec_mode;
  78. #ifdef IF2
  79. short block_size[16] =
  80. {12, 13, 15, 17, 18, 20, 25, 30, 5, 0, 0, 0, 0, 0, 0, 0};
  81. #else
  82. char magic[8];
  83. short block_size[16] = {12, 13, 15, 17, 19, 20, 26, 31, 5, 0, 0, 0, 0, 0, 0, 0};
  84. #endif
  85. #else
  86. short analysis[250];
  87. #endif
  88. /* Process command line options */
  89. rt_tick_t tick = rt_tick_get_millisecond();
  90. // open speech file
  91. file_speech = fopen(file_speech_path, "wb");
  92. if (file_speech == NULL)
  93. {
  94. rt_kprintf("open speech file '%s' error !\n", file_speech_path);
  95. return;
  96. }
  97. // open analysis file
  98. file_analysis = fopen(file_analysis_path, "rb");
  99. if (file_analysis == NULL)
  100. {
  101. rt_kprintf("open analysis file '%s' error !\n", file_analysis_path);
  102. fclose(file_speech);
  103. return;
  104. }
  105. /* init decoder */
  106. destate = Decoder_Interface_init();
  107. DeocoderCopyright();
  108. #ifndef ETSI
  109. #ifndef IF2
  110. /* read and verify magic number */
  111. fread(magic, sizeof(char), strlen(AMR_MAGIC_NUMBER), file_analysis);
  112. if (strncmp(magic, AMR_MAGIC_NUMBER, strlen(AMR_MAGIC_NUMBER)))
  113. {
  114. rt_kprintf("%s%s\n", "Invalid magic number: ", magic);
  115. fclose(file_speech);
  116. fclose(file_analysis);
  117. return;
  118. }
  119. #endif
  120. #endif
  121. #ifndef ETSI
  122. /* find mode, read file */
  123. while (fread(analysis, sizeof(unsigned char), 1, file_analysis) > 0)
  124. {
  125. #ifdef IF2
  126. dec_mode = analysis[0] & 0x000F;
  127. #else
  128. dec_mode = (analysis[0] >> 3) & 0x000F;
  129. #endif
  130. read_size = block_size[dec_mode];
  131. fread(&analysis[1], sizeof(char), read_size, file_analysis);
  132. #else
  133. read_size = 250;
  134. /* read file */
  135. while (fread(analysis, sizeof(short), read_size, file_analysis) > 0)
  136. {
  137. #endif
  138. frames++;
  139. /* call decoder */
  140. Decoder_Interface_Decode(destate, analysis, synth, 0);
  141. fwrite(synth, sizeof(short), 160, file_speech);
  142. }
  143. Decoder_Interface_exit(destate);
  144. fclose(file_speech);
  145. fclose(file_analysis);
  146. rt_kprintf("%s%i%s\n", "Decoded ", frames, " frames.");
  147. rt_kprintf("decoder used %d.%03d s\n", (rt_tick_get_millisecond() - tick) / 1000, (rt_tick_get_millisecond() - tick) % 1000);
  148. return;
  149. }
  150. static void decoder(int argc, char *argv[])
  151. {
  152. if (argc != 3)
  153. {
  154. DecoderUsage();
  155. return;
  156. }
  157. // set file_analysis_path
  158. memset(file_analysis_path, 0, PATH_STRING_MAX_LEN);
  159. memcpy(file_analysis_path, argv[1], strlen(argv[1]));
  160. // set file_speech_path
  161. memset(file_speech_path, 0, PATH_STRING_MAX_LEN);
  162. memcpy(file_speech_path, argv[2], strlen(argv[2]));
  163. // create thread
  164. rt_thread_t thread = rt_thread_create("decoder", decoder_thread, RT_NULL, 4 * 1024, 21, 10);
  165. if (thread != RT_NULL)
  166. rt_thread_startup(thread);
  167. return;
  168. }
  169. #ifdef AMRNB_USING_CMD
  170. MSH_CMD_EXPORT_ALIAS(decoder, amr_decoder, AMR - NB decoder cmd);
  171. #endif