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
D_IF_init(void)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
D_IF_exit(void * s)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
D_IF_decode(void * s,const unsigned char * in,short * out,int bfi)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 if (bfi) {
78 state->mode = 15; // NO_DATA
79 }
80
81 state->quality = 1; /* ? */
82 mime_unsorting((uint8*) in, state->iInputSampleBuf, &state->frame_type, &state->mode, state->quality, &state->rx_state);
83
84 if ((state->frame_type == RX_NO_DATA) | (state->frame_type == RX_SPEECH_LOST)) {
85 state->mode = state->mode_old;
86 state->reset_flag = 0;
87 } else {
88 state->mode_old = state->mode;
89
90 /* if homed: check if this frame is another homing frame */
91 if (state->reset_flag_old == 1) {
92 /* only check until end of first subframe */
93 state->reset_flag = pvDecoder_AmrWb_homing_frame_test_first(state->iInputSampleBuf, state->mode);
94 }
95 }
96
97 /* produce encoder homing frame if homed & input=decoder homing frame */
98 if ((state->reset_flag != 0) && (state->reset_flag_old != 0)) {
99 /* set homing sequence ( no need to decode anything */
100
101 for (int16 i = 0; i < AMR_WB_PCM_FRAME; i++) {
102 out[i] = EHF_MASK;
103 }
104 } else {
105 int16 frameLength;
106 state->status = pvDecoder_AmrWb(state->mode,
107 state->iInputSampleBuf,
108 out,
109 &frameLength,
110 state->st,
111 state->frame_type,
112 state->ScratchMem);
113 }
114
115 for (int16 i = 0; i < AMR_WB_PCM_FRAME; i++) { /* Delete the 2 LSBs (14-bit output) */
116 out[i] &= 0xfffC;
117 }
118
119 /* if not homed: check whether current frame is a homing frame */
120 if (state->reset_flag_old == 0) {
121 /* check whole frame */
122 state->reset_flag = pvDecoder_AmrWb_homing_frame_test(state->iInputSampleBuf, state->mode);
123 }
124 /* reset decoder if current frame is a homing frame */
125 if (state->reset_flag != 0) {
126 pvDecoder_AmrWb_Reset(state->st, 1);
127 }
128 state->reset_flag_old = state->reset_flag;
129
130 }
131
132