1 /* ------------------------------------------------------------------
2 * Copyright (C) 1998-2009 PacketVideo
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
14 * See the License for the specific language governing permissions
15 * and limitations under the License.
16 * -------------------------------------------------------------------
22 #include <pvamrwbdecoder_api.h>
23 #include <pvamrwbdecoder.h>
24 #include <pvamrwbdecoder_cnst.h>
27 /* This is basically a C rewrite of decode_amr_wb.cpp */
30 void *st; /* State structure */
35 int16* iInputSampleBuf;
49 void* D_IF_init(void) {
50 struct state* state = (struct state*) malloc(sizeof(struct state));
51 memset(state, 0, sizeof(*state));
53 state->iInputSampleBuf = (int16*) malloc(sizeof(int16)*KAMRWB_NB_BITS_MAX);
54 state->reset_flag = 0;
55 state->reset_flag_old = 1;
57 state->rx_state.prev_ft = RX_SPEECH_GOOD;
58 state->rx_state.prev_mode = 0;
59 state->pt_st = (unsigned char*) malloc(pvDecoder_AmrWbMemRequirements());
61 pvDecoder_AmrWb_Init(&state->st, state->pt_st, &state->ScratchMem);
65 void D_IF_exit(void* s) {
66 struct state* state = (struct state*) s;
68 free(state->iInputSampleBuf);
72 void D_IF_decode(void* s, const unsigned char* in, short* out, int bfi) {
73 struct state* state = (struct state*) s;
75 state->mode = (in[0] >> 3) & 0x0f;
78 state->quality = 1; /* ? */
79 mime_unsorting((uint8*) in, state->iInputSampleBuf, &state->frame_type, &state->mode, state->quality, &state->rx_state);
81 if ((state->frame_type == RX_NO_DATA) | (state->frame_type == RX_SPEECH_LOST)) {
82 state->mode = state->mode_old;
83 state->reset_flag = 0;
85 state->mode_old = state->mode;
87 /* if homed: check if this frame is another homing frame */
88 if (state->reset_flag_old == 1) {
89 /* only check until end of first subframe */
90 state->reset_flag = pvDecoder_AmrWb_homing_frame_test_first(state->iInputSampleBuf, state->mode);
94 /* produce encoder homing frame if homed & input=decoder homing frame */
95 if ((state->reset_flag != 0) && (state->reset_flag_old != 0)) {
96 /* set homing sequence ( no need to decode anything */
98 for (int16 i = 0; i < AMR_WB_PCM_FRAME; i++) {
103 state->status = pvDecoder_AmrWb(state->mode,
104 state->iInputSampleBuf,
112 for (int16 i = 0; i < AMR_WB_PCM_FRAME; i++) { /* Delete the 2 LSBs (14-bit output) */
116 /* if not homed: check whether current frame is a homing frame */
117 if (state->reset_flag_old == 0) {
118 /* check whole frame */
119 state->reset_flag = pvDecoder_AmrWb_homing_frame_test(state->iInputSampleBuf, state->mode);
121 /* reset decoder if current frame is a homing frame */
122 if (state->reset_flag != 0) {
123 pvDecoder_AmrWb_Reset(state->st, 1);
125 state->reset_flag_old = state->reset_flag;