Git init
[external/opencore-amr.git] / amrwb / wrapper.cpp
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
19 #include "dec_if.h"
20 #include <stdlib.h>
21 #include <string.h>
22 #include <pvamrwbdecoder_api.h>
23 #include <pvamrwbdecoder.h>
24 #include <pvamrwbdecoder_cnst.h>
25 #include <dtx.h>
26
27 /* This is basically a C rewrite of decode_amr_wb.cpp */
28
29 struct state {
30         void *st; /*   State structure  */
31         unsigned char *pt_st;
32         int16 *ScratchMem;
33
34         uint8* iInputBuf;
35         int16* iInputSampleBuf;
36         int16* iOutputBuf;
37
38         uint8 quality;
39         int16 mode;
40         int16 mode_old;
41         int16 frame_type;
42
43         int16 reset_flag;
44         int16 reset_flag_old;
45         int16 status;
46         RX_State rx_state;
47 };
48
49 void* D_IF_init(void) {
50         struct state* state = (struct state*) malloc(sizeof(struct state));
51         memset(state, 0, sizeof(*state));
52
53         state->iInputSampleBuf = (int16*) malloc(sizeof(int16)*KAMRWB_NB_BITS_MAX);
54         state->reset_flag = 0;
55         state->reset_flag_old = 1;
56         state->mode_old = 0;
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());
60
61         pvDecoder_AmrWb_Init(&state->st, state->pt_st, &state->ScratchMem);
62         return state;
63 }
64
65 void D_IF_exit(void* s) {
66         struct state* state = (struct state*) s;
67         free(state->pt_st);
68         free(state->iInputSampleBuf);
69         free(state);
70 }
71
72 void D_IF_decode(void* s, const unsigned char* in, short* out, int bfi) {
73         struct state* state = (struct state*) s;
74
75         state->mode = (in[0] >> 3) & 0x0f;
76         in++;
77
78         state->quality = 1; /* ? */
79         mime_unsorting((uint8*) in, state->iInputSampleBuf, &state->frame_type, &state->mode, state->quality, &state->rx_state);
80         
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;
84         } else {
85                 state->mode_old = state->mode;
86
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);
91                 }
92         }
93
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 */
97
98                 for (int16 i = 0; i < AMR_WB_PCM_FRAME; i++) {
99                         out[i] = EHF_MASK;
100                 }
101         } else {
102                 int16 frameLength;
103                 state->status = pvDecoder_AmrWb(state->mode,
104                                                    state->iInputSampleBuf,
105                                                    out,
106                                                    &frameLength,
107                                                    state->st,
108                                                    state->frame_type,
109                                                    state->ScratchMem);
110         }
111
112         for (int16 i = 0; i < AMR_WB_PCM_FRAME; i++) {  /* Delete the 2 LSBs (14-bit output) */
113                 out[i] &= 0xfffC;
114         }
115
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);
120         }
121         /* reset decoder if current frame is a homing frame */
122         if (state->reset_flag != 0) {
123                 pvDecoder_AmrWb_Reset(state->st, 1);
124         }
125         state->reset_flag_old = state->reset_flag;
126
127 }
128