sp_dec.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /*
  2. *****************************************************************************
  3. *
  4. * GSM AMR-NB speech codec R98 Version 7.6.0 December 12, 2001
  5. * R99 Version 3.3.0
  6. * REL-4 Version 4.1.0
  7. *
  8. *****************************************************************************
  9. *
  10. * File : sp_dec.c
  11. * Purpose : Decoding and post filtering of one speech frame.
  12. *
  13. *****************************************************************************
  14. */
  15. /*
  16. *****************************************************************************
  17. * MODULE INCLUDE FILE AND VERSION ID
  18. *****************************************************************************
  19. */
  20. #include "sp_dec.h"
  21. const char sp_dec_id[] = "@(#)$Id $" sp_dec_h;
  22. /*
  23. *****************************************************************************
  24. * INCLUDE FILES
  25. *****************************************************************************
  26. */
  27. #include <stdlib.h>
  28. #include <stdio.h>
  29. #include "typedef.h"
  30. #include "basic_op.h"
  31. #include "count.h"
  32. #include "cnst.h"
  33. #include "set_zero.h"
  34. #include "dec_amr.h"
  35. #include "pstfilt.h"
  36. #include "bits2prm.h"
  37. #include "mode.h"
  38. #include "post_pro.h"
  39. #ifdef MMS_IO
  40. #include "bitno.tab"
  41. #endif
  42. /*
  43. *****************************************************************************
  44. * LOCAL VARIABLES AND TABLES
  45. *****************************************************************************
  46. */
  47. /*---------------------------------------------------------------*
  48. * Constants (defined in "cnst.h") *
  49. *---------------------------------------------------------------*
  50. * L_FRAME :
  51. * M :
  52. * PRM_SIZE :
  53. * AZ_SIZE :
  54. * SERIAL_SIZE :
  55. *---------------------------------------------------------------*/
  56. /*
  57. *****************************************************************************
  58. * PUBLIC PROGRAM CODE
  59. *****************************************************************************
  60. */
  61. /*************************************************************************
  62. *
  63. * Function: Speech_Decode_Frame_init
  64. * Purpose: Allocates memory for filter structure and initializes
  65. * state memory
  66. *
  67. **************************************************************************
  68. */
  69. int Speech_Decode_Frame_init (Speech_Decode_FrameState **state,
  70. char *id)
  71. {
  72. Speech_Decode_FrameState* s;
  73. if (state == (Speech_Decode_FrameState **) NULL){
  74. wfprintf(stderr, "Speech_Decode_Frame_init: invalid parameter\n");
  75. return -1;
  76. }
  77. *state = NULL;
  78. /* allocate memory */
  79. if ((s= (Speech_Decode_FrameState *)
  80. wmalloc(sizeof(Speech_Decode_FrameState))) == NULL) {
  81. wfprintf(stderr, "Speech_Decode_Frame_init: can not malloc state "
  82. "structure\n");
  83. return -1;
  84. }
  85. s->decoder_amrState = NULL;
  86. s->post_state = NULL;
  87. s->postHP_state = NULL;
  88. if (Decoder_amr_init(&s->decoder_amrState) ||
  89. Post_Filter_init(&s->post_state) ||
  90. Post_Process_init(&s->postHP_state) ) {
  91. Speech_Decode_Frame_exit(&s);
  92. return -1;
  93. }
  94. s->complexityCounter = getCounterId(id);
  95. Speech_Decode_Frame_reset(s);
  96. *state = s;
  97. return 0;
  98. }
  99. /*************************************************************************
  100. *
  101. * Function: Speech_Decode_Frame_reset
  102. * Purpose: Resetses state memory
  103. *
  104. **************************************************************************
  105. */
  106. int Speech_Decode_Frame_reset (Speech_Decode_FrameState *state)
  107. {
  108. if (state == (Speech_Decode_FrameState *) NULL){
  109. wfprintf(stderr, "Speech_Decode_Frame_reset: invalid parameter\n");
  110. return -1;
  111. }
  112. Decoder_amr_reset(state->decoder_amrState, (enum Mode)0);
  113. Post_Filter_reset(state->post_state);
  114. Post_Process_reset(state->postHP_state);
  115. state->prev_mode = (enum Mode)0;
  116. setCounter(state->complexityCounter);
  117. Init_WMOPS_counter();
  118. setCounter(0); /* set counter to global counter */
  119. return 0;
  120. }
  121. /*
  122. **************************************************************************
  123. *
  124. * Function: Speech_Decode_Frame_exit
  125. * Purpose: The memory used for state memory is freed
  126. *
  127. **************************************************************************
  128. */
  129. void Speech_Decode_Frame_exit (Speech_Decode_FrameState **state)
  130. {
  131. if (state == NULL || *state == NULL)
  132. return;
  133. Decoder_amr_exit(&(*state)->decoder_amrState);
  134. Post_Filter_exit(&(*state)->post_state);
  135. Post_Process_exit(&(*state)->postHP_state);
  136. setCounter((*state)->complexityCounter);
  137. WMOPS_output(0);
  138. setCounter(0); /* set counter to global counter */
  139. /* deallocate memory */
  140. wfree(*state);
  141. *state = NULL;
  142. return;
  143. }
  144. int Speech_Decode_Frame (
  145. Speech_Decode_FrameState *st, /* io: post filter states */
  146. enum Mode mode, /* i : AMR mode */
  147. Word16 *serial, /* i : serial bit stream */
  148. enum RXFrameType frame_type, /* i : Frame type */
  149. Word16 *synth /* o : synthesis speech (postfiltered */
  150. /* output) */
  151. )
  152. {
  153. Word16 parm[MAX_PRM_SIZE + 1]; /* Synthesis parameters */
  154. Word16 Az_dec[AZ_SIZE]; /* Decoded Az for post-filter */
  155. /* in 4 subframes */
  156. #if !defined(NO13BIT)
  157. Word16 i;
  158. #endif
  159. setCounter(st->complexityCounter);
  160. Reset_WMOPS_counter (); /* reset WMOPS counter for the new frame */
  161. /* Serial to parameters */
  162. test(); test(); sub_ex(0,0); sub_ex(0,0); logic16();
  163. if ((frame_type == RX_SID_BAD) ||
  164. (frame_type == RX_SID_UPDATE)) {
  165. /* Override mode to MRDTX */
  166. Bits2prm (MRDTX, serial, parm);
  167. } else {
  168. Bits2prm (mode, serial, parm);
  169. }
  170. /* Synthesis */
  171. Decoder_amr(st->decoder_amrState, mode, parm, frame_type,
  172. synth, Az_dec);
  173. Post_Filter(st->post_state, mode, synth, Az_dec); /* Post-filter */
  174. /* post HP filter, and 15->16 bits */
  175. Post_Process(st->postHP_state, synth, L_FRAME);
  176. #if !defined(NO13BIT)
  177. /* Truncate to 13 bits */
  178. for (i = 0; i < L_FRAME; i++)
  179. {
  180. synth[i] = synth[i] & 0xfff8; logic16 (); move16 ();
  181. }
  182. #endif
  183. fwc(); /* function worst case */
  184. setCounter(0); /* set counter to global counter */
  185. return 0;
  186. }
  187. #ifdef MMS_IO
  188. /*
  189. **************************************************************************
  190. *
  191. * Function : UnpackBits
  192. * Purpose : Unpack and re-arrange bits from file storage format to the
  193. * format required by speech decoder.
  194. *
  195. **************************************************************************
  196. */
  197. enum RXFrameType UnpackBits (
  198. Word8 q, /* i : Q-bit (i.e. BFI) */
  199. Word16 ft, /* i : frame type (i.e. mode) */
  200. UWord8 packed_bits[], /* i : sorted & packed bits */
  201. enum Mode *mode, /* o : mode information */
  202. Word16 bits[] /* o : serial bits */
  203. )
  204. {
  205. Word16 i, sid_type;
  206. UWord8 *pack_ptr, temp;
  207. pack_ptr = (UWord8*)packed_bits;
  208. /* real NO_DATA frame or unspecified frame type */
  209. if (ft == 15 || (ft > 8 && ft < 15))
  210. {
  211. *mode = (enum Mode)-1;
  212. return RX_NO_DATA;
  213. }
  214. temp = *pack_ptr;
  215. pack_ptr++;
  216. for (i = 1; i < unpacked_size[ft] + 1; i++)
  217. {
  218. if (temp & 0x80) bits[sort_ptr[ft][i-1]] = BIT_1;
  219. else bits[sort_ptr[ft][i-1]] = BIT_0;
  220. if (i % 8)
  221. {
  222. temp <<= 1;
  223. }
  224. else
  225. {
  226. temp = *pack_ptr;
  227. pack_ptr++;
  228. }
  229. }
  230. /* SID frame */
  231. if (ft == MRDTX)
  232. {
  233. if (temp & 0x80) sid_type = 1;
  234. else sid_type = 0;
  235. *mode = (enum Mode)((temp >> 4) & 0x07);
  236. *mode = ((*mode & 0x4) >> 2) | (*mode & 0x2) | ((*mode & 0x1) << 2);
  237. if (q)
  238. {
  239. if (sid_type) return RX_SID_UPDATE;
  240. else return RX_SID_FIRST;
  241. }
  242. else
  243. {
  244. return RX_SID_BAD;
  245. }
  246. }
  247. /* speech frame */
  248. else
  249. {
  250. *mode = (enum Mode)ft;
  251. if (q) return RX_SPEECH_GOOD;
  252. else return RX_SPEECH_BAD;
  253. }
  254. }
  255. #endif
  256. ��