sp_enc.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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_enc.c
  11. * Purpose : Pre filtering and encoding of one speech frame.
  12. *
  13. *****************************************************************************
  14. */
  15. /*
  16. *****************************************************************************
  17. * MODULE INCLUDE FILE AND VERSION ID
  18. *****************************************************************************
  19. */
  20. /*
  21. *****************************************************************************
  22. * INCLUDE FILES
  23. *****************************************************************************
  24. */
  25. #include <stdlib.h>
  26. #include <stdio.h>
  27. #include "typedef.h"
  28. #include "basic_op.h"
  29. #include "cnst.h"
  30. #include "count.h"
  31. #include "set_zero.h"
  32. #include "pre_proc.h"
  33. #include "prm2bits.h"
  34. #include "mode.h"
  35. #include "cod_amr.h"
  36. #ifdef MMS_IO
  37. #include "frame.h"
  38. #include "bitno.tab"
  39. #endif
  40. #include "sp_enc.h"
  41. const char sp_enc_id[] = "@(#)$Id $" sp_enc_h;
  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_Encode_Frame_init
  64. * Purpose: Allocates memory for filter structure and initializes
  65. * state memory
  66. *
  67. **************************************************************************
  68. */
  69. int Speech_Encode_Frame_init (Speech_Encode_FrameState **state,
  70. Flag dtx,
  71. char *id)
  72. {
  73. Speech_Encode_FrameState* s;
  74. if (state == (Speech_Encode_FrameState **) NULL){
  75. wfprintf(stderr, "Speech_Encode_Frame_init: invalid parameter\n");
  76. return -1;
  77. }
  78. *state = NULL;
  79. /* allocate memory */
  80. if ((s= (Speech_Encode_FrameState *) wmalloc(sizeof(Speech_Encode_FrameState))) == NULL){
  81. wfprintf(stderr, "Speech_Encode_Frame_init: can not malloc state "
  82. "structure\n");
  83. return -1;
  84. }
  85. s->complexityCounter = getCounterId(id);
  86. s->pre_state = NULL;
  87. s->cod_amr_state = NULL;
  88. s->dtx = dtx;
  89. if (Pre_Process_init(&s->pre_state) ||
  90. cod_amr_init(&s->cod_amr_state, s->dtx)) {
  91. Speech_Encode_Frame_exit(&s);
  92. return -1;
  93. }
  94. Speech_Encode_Frame_reset(s);
  95. *state = s;
  96. return 0;
  97. }
  98. /*************************************************************************
  99. *
  100. * Function: Speech_Encode_Frame_reset
  101. * Purpose: Resetses state memory
  102. *
  103. **************************************************************************
  104. */
  105. int Speech_Encode_Frame_reset (Speech_Encode_FrameState *state)
  106. {
  107. if (state == (Speech_Encode_FrameState *) NULL){
  108. wfprintf(stderr, "Speech_Encode_Frame_reset: invalid parameter\n");
  109. return -1;
  110. }
  111. Pre_Process_reset(state->pre_state);
  112. cod_amr_reset(state->cod_amr_state);
  113. setCounter(state->complexityCounter);
  114. Init_WMOPS_counter();
  115. setCounter(0); /* set counter to global counter */
  116. return 0;
  117. }
  118. /*************************************************************************
  119. *
  120. * Function: Speech_Encode_Frame_exit
  121. * Purpose: The memory used for state memory is freed
  122. *
  123. **************************************************************************
  124. */
  125. void Speech_Encode_Frame_exit (Speech_Encode_FrameState **state)
  126. {
  127. if (state == NULL || *state == NULL)
  128. return;
  129. Pre_Process_exit(&(*state)->pre_state);
  130. cod_amr_exit(&(*state)->cod_amr_state);
  131. setCounter((*state)->complexityCounter);
  132. WMOPS_output(0);
  133. setCounter(0); /* set counter to global counter */
  134. /* deallocate memory */
  135. wfree(*state);
  136. *state = NULL;
  137. return;
  138. }
  139. int Speech_Encode_Frame_First (
  140. Speech_Encode_FrameState *st, /* i/o : post filter states */
  141. Word16 *new_speech) /* i : speech input */
  142. {
  143. #if !defined(NO13BIT)
  144. Word16 i;
  145. #endif
  146. setCounter(st->complexityCounter);
  147. #if !defined(NO13BIT)
  148. /* Delete the 3 LSBs (13-bit input) */
  149. for (i = 0; i < L_NEXT; i++)
  150. {
  151. new_speech[i] = new_speech[i] & 0xfff8; move16 (); logic16 ();
  152. }
  153. #endif
  154. /* filter + downscaling */
  155. Pre_Process (st->pre_state, new_speech, L_NEXT);
  156. cod_amr_first(st->cod_amr_state, new_speech);
  157. Init_WMOPS_counter (); /* reset WMOPS counter for the new frame */
  158. return 0;
  159. }
  160. #include "log.h"
  161. int Speech_Encode_Frame (
  162. Speech_Encode_FrameState *st, /* i/o : post filter states */
  163. enum Mode mode, /* i : speech coder mode */
  164. Word16 *new_speech, /* i : speech input */
  165. Word16 *serial, /* o : serial bit stream */
  166. enum Mode *usedMode /* o : used speech coder mode */
  167. )
  168. {
  169. Word16 prm[MAX_PRM_SIZE]; /* Analysis parameters. */
  170. Word16 syn[L_FRAME]; /* Buffer for synthesis speech */
  171. Word16 i;
  172. setCounter(st->complexityCounter);
  173. Reset_WMOPS_counter (); /* reset WMOPS counter for the new frame */
  174. /* initialize the serial output frame to zero */
  175. for (i = 0; i < MAX_SERIAL_SIZE; i++)
  176. {
  177. serial[i] = 0; move16 ();
  178. }
  179. #if !defined(NO13BIT)
  180. /* Delete the 3 LSBs (13-bit input) */
  181. for (i = 0; i < L_FRAME; i++)
  182. {
  183. new_speech[i] = new_speech[i] & 0xfff8; move16 (); logic16 ();
  184. }
  185. #endif
  186. /* filter + downscaling */
  187. Pre_Process (st->pre_state, new_speech, L_FRAME);
  188. /* Call the speech encoder */
  189. cod_amr(st->cod_amr_state, mode, new_speech, prm, usedMode, syn);
  190. /* Parameters to serial bits */
  191. Prm2bits (*usedMode, prm, &serial[0]);
  192. fwc();
  193. setCounter(0); /* set counter to global counter */
  194. return 0;
  195. }
  196. #ifdef MMS_IO
  197. /*************************************************************************
  198. *
  199. * FUNCTION: PackBits
  200. *
  201. * PURPOSE: Sorts speech bits according decreasing subjective importance
  202. * and packs into octets according to AMR file storage format
  203. * as specified in RFC 3267 (Sections 5.1 and 5.3).
  204. *
  205. * DESCRIPTION: Depending on the mode, different numbers of bits are
  206. * processed. Details can be found in specification mentioned
  207. * above and in file "bitno.tab".
  208. *
  209. *************************************************************************/
  210. Word16 PackBits(
  211. enum Mode used_mode, /* i : actual AMR mode */
  212. enum Mode mode, /* i : requested AMR (speech) mode */
  213. enum TXFrameType fr_type, /* i : frame type */
  214. Word16 bits[], /* i : serial bits */
  215. UWord8 packed_bits[] /* o : sorted&packed bits */
  216. )
  217. {
  218. Word16 i;
  219. UWord8 temp;
  220. UWord8 *pack_ptr;
  221. temp = 0;
  222. pack_ptr = (UWord8*)packed_bits;
  223. /* file storage format can handle only speech frames, AMR SID frames and NO_DATA frames */
  224. /* -> force NO_DATA frame */
  225. if (used_mode < 0 || used_mode > 15 || (used_mode > 8 && used_mode < 15))
  226. {
  227. used_mode = 15;
  228. }
  229. /* mark empty frames between SID updates as NO_DATA frames */
  230. if (used_mode == MRDTX && fr_type == TX_NO_DATA)
  231. {
  232. used_mode = 15;
  233. }
  234. /* insert table of contents (ToC) byte at the beginning of the frame */
  235. *pack_ptr = toc_byte[used_mode];
  236. pack_ptr++;
  237. /* note that NO_DATA frames (used_mode==15) do not need further processing */
  238. if (used_mode == 15)
  239. {
  240. return 1;
  241. }
  242. temp = 0;
  243. /* sort and pack speech bits */
  244. for (i = 1; i < unpacked_size[used_mode] + 1; i++)
  245. {
  246. if (bits[sort_ptr[used_mode][i-1]] == BIT_1)
  247. {
  248. temp++;
  249. }
  250. if (i % 8)
  251. {
  252. temp <<= 1;
  253. }
  254. else
  255. {
  256. *pack_ptr = temp;
  257. pack_ptr++;
  258. temp = 0;
  259. }
  260. }
  261. /* insert SID type indication and speech mode in case of SID frame */
  262. if (used_mode == MRDTX)
  263. {
  264. if (fr_type == TX_SID_UPDATE)
  265. {
  266. temp++;
  267. }
  268. temp <<= 3;
  269. temp += ((mode & 0x4) >> 2) | (mode & 0x2) | ((mode & 0x1) << 2);
  270. temp <<= 1;
  271. }
  272. /* insert unused bits (zeros) at the tail of the last byte */
  273. temp <<= (unused_size[used_mode] - 1);
  274. *pack_ptr = temp;
  275. return packed_size[used_mode];
  276. }
  277. #endif
  278. ��