sid_sync.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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 : sid_sync.c
  11. *
  12. *****************************************************************************
  13. */
  14. /*
  15. *****************************************************************************
  16. * MODULE INCLUDE FILE AND VERSION ID
  17. *****************************************************************************
  18. */
  19. #include "sid_sync.h"
  20. const char sid_sync_id[] = "@(#)$Id $" sid_sync_h;
  21. /*
  22. *****************************************************************************
  23. * INCLUDE FILES
  24. *****************************************************************************
  25. */
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include "typedef.h"
  29. #include "basic_op.h"
  30. #include "count.h"
  31. #include "mode.h"
  32. /*
  33. *****************************************************************************
  34. * LOCAL VARIABLES AND TABLES
  35. *****************************************************************************
  36. */
  37. /*
  38. *****************************************************************************
  39. * PUBLIC PROGRAM CODE
  40. *****************************************************************************
  41. */
  42. int sid_sync_init (sid_syncState **state)
  43. {
  44. sid_syncState* s;
  45. if (state == (sid_syncState **) NULL){
  46. wfprintf(stderr, "sid_sync_init:invalid state parameter\n");
  47. return -1;
  48. }
  49. *state = NULL;
  50. /* allocate memory */
  51. if ((s= (sid_syncState *)
  52. wmalloc(sizeof(sid_syncState))) == NULL){
  53. wfprintf(stderr,
  54. "sid_sync_init: "
  55. "can not malloc state structure\n");
  56. return -1;
  57. }
  58. s->sid_update_rate = 8;
  59. *state = s;
  60. return sid_sync_reset(s);
  61. }
  62. int sid_sync_reset (sid_syncState *st)
  63. {
  64. st->sid_update_counter = 3;
  65. st->sid_handover_debt = 0;
  66. st->prev_ft = TX_SPEECH_GOOD;
  67. return 0;
  68. }
  69. void sid_sync_exit (sid_syncState **state)
  70. {
  71. if (state == NULL || *state == NULL)
  72. return;
  73. /* deallocate memory */
  74. wfree(*state);
  75. *state = NULL;
  76. return;
  77. }
  78. int sid_sync_set_handover_debt (sid_syncState *st,
  79. Word16 debtFrames)
  80. {
  81. /* debtFrames >= 0 */
  82. st->sid_handover_debt = debtFrames;
  83. return 0;
  84. }
  85. void sid_sync (sid_syncState *st, enum Mode mode,
  86. enum TXFrameType *tx_frame_type)
  87. {
  88. if ( mode == MRDTX){
  89. st->sid_update_counter--;
  90. if (st->prev_ft == TX_SPEECH_GOOD)
  91. {
  92. *tx_frame_type = TX_SID_FIRST;
  93. st->sid_update_counter = 3;
  94. }
  95. else
  96. {
  97. /* TX_SID_UPDATE or TX_NO_DATA */
  98. if( (st->sid_handover_debt > 0) &&
  99. (st->sid_update_counter > 2) )
  100. {
  101. /* ensure extra updates are properly delayed after
  102. a possible SID_FIRST */
  103. *tx_frame_type = TX_SID_UPDATE;
  104. st->sid_handover_debt--;
  105. }
  106. else
  107. {
  108. if (st->sid_update_counter == 0)
  109. {
  110. *tx_frame_type = TX_SID_UPDATE;
  111. st->sid_update_counter = st->sid_update_rate;
  112. } else {
  113. *tx_frame_type = TX_NO_DATA;
  114. }
  115. }
  116. }
  117. }
  118. else
  119. {
  120. st->sid_update_counter = st->sid_update_rate ;
  121. *tx_frame_type = TX_SPEECH_GOOD;
  122. }
  123. st->prev_ft = *tx_frame_type;
  124. }