tizen 2.3.1 release
[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 #ifndef DISABLE_AMRNB_DECODER
28 void* Decoder_Interface_init(void) {
29         void* ptr = NULL;
30         GSMInitDecode(&ptr, (int8*)"Decoder");
31         return ptr;
32 }
33
34 void Decoder_Interface_exit(void* state) {
35         GSMDecodeFrameExit(&state);
36 }
37
38 void Decoder_Interface_Decode(void* state, const unsigned char* in, short* out, int bfi) {
39         unsigned char type = (in[0] >> 3) & 0x0f;
40         in++;
41         AMRDecode(state, (enum Frame_Type_3GPP) type, (UWord8*) in, out, MIME_IETF);
42 }
43 #endif
44
45 #ifndef DISABLE_AMRNB_ENCODER
46 struct encoder_state {
47         void* encCtx;
48         void* pidSyncCtx;
49 };
50
51 void* Encoder_Interface_init(int dtx) {
52         struct encoder_state* state = (struct encoder_state*) malloc(sizeof(struct encoder_state));
53         AMREncodeInit(&state->encCtx, &state->pidSyncCtx, dtx);
54         return state;
55 }
56
57 void Encoder_Interface_exit(void* s) {
58         struct encoder_state* state = (struct encoder_state*) s;
59         AMREncodeExit(&state->encCtx, &state->pidSyncCtx);
60         free(state);
61 }
62
63 int Encoder_Interface_Encode(void* s, enum Mode mode, const short* speech, unsigned char* out, int forceSpeech) {
64         struct encoder_state* state = (struct encoder_state*) s;
65         enum Frame_Type_3GPP frame_type = (enum Frame_Type_3GPP) mode;
66         int ret = AMREncode(state->encCtx, state->pidSyncCtx, mode, (Word16*) speech, out, &frame_type, AMR_TX_IETF);
67         out[0] |= 0x04;
68         return ret;
69 }
70 #endif
71