amrwb_wrapper.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /* ------------------------------------------------------------------
  2. * Copyright (C) 1998-2009 PacketVideo
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  13. * express or implied.
  14. * See the License for the specific language governing permissions
  15. * and limitations under the License.
  16. * -------------------------------------------------------------------
  17. */
  18. #include "dec_if.h"
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <pvamrwbdecoder_api.h>
  22. #include <pvamrwbdecoder.h>
  23. #include <pvamrwbdecoder_cnst.h>
  24. #include <dtx.h>
  25. #ifdef __LUATOS__
  26. #include "luat_base.h"
  27. #include "luat_mem.h"
  28. #define malloc luat_heap_malloc
  29. #define free luat_heap_free
  30. #endif
  31. /* This is basically a C rewrite of decode_amr_wb.cpp */
  32. struct state {
  33. void *st; /* State structure */
  34. unsigned char *pt_st;
  35. int16 *ScratchMem;
  36. uint8* iInputBuf;
  37. int16* iInputSampleBuf;
  38. int16* iOutputBuf;
  39. uint8 quality;
  40. int16 mode;
  41. int16 mode_old;
  42. int16 frame_type;
  43. int16 reset_flag;
  44. int16 reset_flag_old;
  45. int16 status;
  46. RX_State rx_state;
  47. };
  48. void* D_IF_init(void) {
  49. struct state* state = (struct state*) malloc(sizeof(struct state));
  50. memset(state, 0, sizeof(*state));
  51. state->iInputSampleBuf = (int16*) malloc(sizeof(int16)*KAMRWB_NB_BITS_MAX);
  52. state->reset_flag = 0;
  53. state->reset_flag_old = 1;
  54. state->mode_old = 0;
  55. state->rx_state.prev_ft = RX_SPEECH_GOOD;
  56. state->rx_state.prev_mode = 0;
  57. state->pt_st = (unsigned char*) malloc(pvDecoder_AmrWbMemRequirements());
  58. pvDecoder_AmrWb_Init(&state->st, state->pt_st, &state->ScratchMem);
  59. return state;
  60. }
  61. void D_IF_exit(void* s) {
  62. struct state* state = (struct state*) s;
  63. free(state->pt_st);
  64. free(state->iInputSampleBuf);
  65. free(state);
  66. }
  67. void D_IF_decode(void* s, const unsigned char* in, short* out, int bfi) {
  68. struct state* state = (struct state*) s;
  69. state->mode = (in[0] >> 3) & 0x0f;
  70. in++;
  71. state->quality = 1; /* ? */
  72. mime_unsorting((uint8*) in, state->iInputSampleBuf, &state->frame_type, &state->mode, state->quality, &state->rx_state);
  73. if ((state->frame_type == RX_NO_DATA) | (state->frame_type == RX_SPEECH_LOST)) {
  74. state->mode = state->mode_old;
  75. state->reset_flag = 0;
  76. } else {
  77. state->mode_old = state->mode;
  78. /* if homed: check if this frame is another homing frame */
  79. if (state->reset_flag_old == 1) {
  80. /* only check until end of first subframe */
  81. state->reset_flag = pvDecoder_AmrWb_homing_frame_test_first(state->iInputSampleBuf, state->mode);
  82. }
  83. }
  84. /* produce encoder homing frame if homed & input=decoder homing frame */
  85. if ((state->reset_flag != 0) && (state->reset_flag_old != 0)) {
  86. /* set homing sequence ( no need to decode anything */
  87. for (int16 i = 0; i < AMR_WB_PCM_FRAME; i++) {
  88. out[i] = EHF_MASK;
  89. }
  90. } else {
  91. int16 frameLength;
  92. state->status = pvDecoder_AmrWb(state->mode,
  93. state->iInputSampleBuf,
  94. out,
  95. &frameLength,
  96. state->st,
  97. state->frame_type,
  98. state->ScratchMem);
  99. }
  100. for (int16 i = 0; i < AMR_WB_PCM_FRAME; i++) { /* Delete the 2 LSBs (14-bit output) */
  101. out[i] &= 0xfffC;
  102. }
  103. /* if not homed: check whether current frame is a homing frame */
  104. if (state->reset_flag_old == 0) {
  105. /* check whole frame */
  106. state->reset_flag = pvDecoder_AmrWb_homing_frame_test(state->iInputSampleBuf, state->mode);
  107. }
  108. /* reset decoder if current frame is a homing frame */
  109. if (state->reset_flag != 0) {
  110. pvDecoder_AmrWb_Reset(state->st, 1);
  111. }
  112. state->reset_flag_old = state->reset_flag;
  113. }