Git init
[external/opencore-amr.git] / amrnb / wrapper.cpp
1 /* ------------------------------------------------------------------
2  * Copyright (C) 2009 Martin Storsjo
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 #define AMRNB_WRAPPER_INTERNAL
20 #include <sp_dec.h>
21 #include <amrdecode.h>
22 #include <amrencode.h>
23 #include "interf_dec.h"
24 #include "interf_enc.h"
25 #include <stdlib.h>
26
27 void* Decoder_Interface_init(void) {
28         void* ptr = NULL;
29         GSMInitDecode(&ptr, (int8*)"Decoder");
30         return ptr;
31 }
32
33 void Decoder_Interface_exit(void* state) {
34         GSMDecodeFrameExit(&state);
35 }
36
37 void Decoder_Interface_Decode(void* state, const unsigned char* in, short* out, int bfi) {
38         unsigned char type = (in[0] >> 3) & 0x0f;
39         in++;
40         AMRDecode(state, (enum Frame_Type_3GPP) type, (UWord8*) in, out, MIME_IETF);
41 }
42
43 struct encoder_state {
44         void* encCtx;
45         void* pidSyncCtx;
46 };
47
48 void* Encoder_Interface_init(int dtx) {
49         struct encoder_state* state = (struct encoder_state*) malloc(sizeof(struct encoder_state));
50         AMREncodeInit(&state->encCtx, &state->pidSyncCtx, dtx);
51         return state;
52 }
53
54 void Encoder_Interface_exit(void* s) {
55         struct encoder_state* state = (struct encoder_state*) s;
56         AMREncodeExit(&state->encCtx, &state->pidSyncCtx);
57         free(state);
58 }
59
60 int Encoder_Interface_Encode(void* s, enum Mode mode, const short* speech, unsigned char* out, int forceSpeech) {
61         struct encoder_state* state = (struct encoder_state*) s;
62         enum Frame_Type_3GPP frame_type = (enum Frame_Type_3GPP) mode;
63         int ret = AMREncode(state->encCtx, state->pidSyncCtx, mode, (Word16*) speech, out, &frame_type, AMR_TX_IETF);
64         out[0] |= 0x04;
65         return ret;
66 }
67