summaryrefslogtreecommitdiffstats
path: root/third_party/libwebrtc/modules/audio_coding/codecs/ilbc/ilbc.c
blob: ba6c3e46c373926fbc0a9e2278bb12873d94a110 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/*
 *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

/******************************************************************

 iLBC Speech Coder ANSI-C Source Code

 iLBCInterface.c

******************************************************************/

#include "modules/audio_coding/codecs/ilbc/ilbc.h"

#include <stdlib.h>

#include "modules/audio_coding/codecs/ilbc/decode.h"
#include "modules/audio_coding/codecs/ilbc/defines.h"
#include "modules/audio_coding/codecs/ilbc/encode.h"
#include "modules/audio_coding/codecs/ilbc/init_decode.h"
#include "modules/audio_coding/codecs/ilbc/init_encode.h"
#include "rtc_base/checks.h"

int16_t WebRtcIlbcfix_EncoderAssign(IlbcEncoderInstance** iLBC_encinst,
                                    int16_t* ILBCENC_inst_Addr,
                                    int16_t* size) {
  *iLBC_encinst=(IlbcEncoderInstance*)ILBCENC_inst_Addr;
  *size=sizeof(IlbcEncoder)/sizeof(int16_t);
  if (*iLBC_encinst!=NULL) {
    return(0);
  } else {
    return(-1);
  }
}

int16_t WebRtcIlbcfix_DecoderAssign(IlbcDecoderInstance** iLBC_decinst,
                                    int16_t* ILBCDEC_inst_Addr,
                                    int16_t* size) {
  *iLBC_decinst=(IlbcDecoderInstance*)ILBCDEC_inst_Addr;
  *size=sizeof(IlbcDecoder)/sizeof(int16_t);
  if (*iLBC_decinst!=NULL) {
    return(0);
  } else {
    return(-1);
  }
}

int16_t WebRtcIlbcfix_EncoderCreate(IlbcEncoderInstance **iLBC_encinst) {
  *iLBC_encinst=(IlbcEncoderInstance*)malloc(sizeof(IlbcEncoder));
  if (*iLBC_encinst!=NULL) {
    return(0);
  } else {
    return(-1);
  }
}

int16_t WebRtcIlbcfix_DecoderCreate(IlbcDecoderInstance **iLBC_decinst) {
  *iLBC_decinst=(IlbcDecoderInstance*)malloc(sizeof(IlbcDecoder));
  if (*iLBC_decinst!=NULL) {
    return(0);
  } else {
    return(-1);
  }
}

int16_t WebRtcIlbcfix_EncoderFree(IlbcEncoderInstance *iLBC_encinst) {
  free(iLBC_encinst);
  return(0);
}

int16_t WebRtcIlbcfix_DecoderFree(IlbcDecoderInstance *iLBC_decinst) {
  free(iLBC_decinst);
  return(0);
}

int16_t WebRtcIlbcfix_EncoderInit(IlbcEncoderInstance* iLBCenc_inst,
                                  int16_t mode) {
  if ((mode==20)||(mode==30)) {
    WebRtcIlbcfix_InitEncode((IlbcEncoder*) iLBCenc_inst, mode);
    return(0);
  } else {
    return(-1);
  }
}

int WebRtcIlbcfix_Encode(IlbcEncoderInstance* iLBCenc_inst,
                         const int16_t* speechIn,
                         size_t len,
                         uint8_t* encoded) {
  size_t pos = 0;
  size_t encpos = 0;

  if ((len != ((IlbcEncoder*)iLBCenc_inst)->blockl) &&
#ifdef SPLIT_10MS
      (len != 80) &&
#endif
      (len != 2*((IlbcEncoder*)iLBCenc_inst)->blockl) &&
      (len != 3*((IlbcEncoder*)iLBCenc_inst)->blockl))
  {
    /* A maximum of 3 frames/packet is allowed */
    return(-1);
  } else {

    /* call encoder */
    while (pos<len) {
      WebRtcIlbcfix_EncodeImpl((uint16_t*)&encoded[2 * encpos], &speechIn[pos],
                               (IlbcEncoder*)iLBCenc_inst);
#ifdef SPLIT_10MS
      pos += 80;
      if(((IlbcEncoder*)iLBCenc_inst)->section == 0)
#else
        pos += ((IlbcEncoder*)iLBCenc_inst)->blockl;
#endif
      encpos += ((IlbcEncoder*)iLBCenc_inst)->no_of_words;
    }
    return (int)(encpos*2);
  }
}

int16_t WebRtcIlbcfix_DecoderInit(IlbcDecoderInstance* iLBCdec_inst,
                                  int16_t mode) {
  if ((mode==20)||(mode==30)) {
    WebRtcIlbcfix_InitDecode((IlbcDecoder*) iLBCdec_inst, mode, 1);
    return(0);
  } else {
    return(-1);
  }
}
void WebRtcIlbcfix_DecoderInit20Ms(IlbcDecoderInstance* iLBCdec_inst) {
  WebRtcIlbcfix_InitDecode((IlbcDecoder*) iLBCdec_inst, 20, 1);
}
void WebRtcIlbcfix_Decoderinit30Ms(IlbcDecoderInstance* iLBCdec_inst) {
  WebRtcIlbcfix_InitDecode((IlbcDecoder*) iLBCdec_inst, 30, 1);
}


int WebRtcIlbcfix_Decode(IlbcDecoderInstance* iLBCdec_inst,
                         const uint8_t* encoded,
                         size_t len,
                         int16_t* decoded,
                         int16_t* speechType)
{
  size_t i=0;
  /* Allow for automatic switching between the frame sizes
     (although you do get some discontinuity) */
  if ((len==((IlbcDecoder*)iLBCdec_inst)->no_of_bytes)||
      (len==2*((IlbcDecoder*)iLBCdec_inst)->no_of_bytes)||
      (len==3*((IlbcDecoder*)iLBCdec_inst)->no_of_bytes)) {
    /* ok, do nothing */
  } else {
    /* Test if the mode has changed */
    if (((IlbcDecoder*)iLBCdec_inst)->mode==20) {
      if ((len==NO_OF_BYTES_30MS)||
          (len==2*NO_OF_BYTES_30MS)||
          (len==3*NO_OF_BYTES_30MS)) {
        WebRtcIlbcfix_InitDecode(
            ((IlbcDecoder*)iLBCdec_inst), 30,
            ((IlbcDecoder*)iLBCdec_inst)->use_enhancer);
      } else {
        /* Unsupported frame length */
        return(-1);
      }
    } else {
      if ((len==NO_OF_BYTES_20MS)||
          (len==2*NO_OF_BYTES_20MS)||
          (len==3*NO_OF_BYTES_20MS)) {
        WebRtcIlbcfix_InitDecode(
            ((IlbcDecoder*)iLBCdec_inst), 20,
            ((IlbcDecoder*)iLBCdec_inst)->use_enhancer);
      } else {
        /* Unsupported frame length */
        return(-1);
      }
    }
  }

  while ((i*((IlbcDecoder*)iLBCdec_inst)->no_of_bytes)<len) {
    if (WebRtcIlbcfix_DecodeImpl(
            &decoded[i * ((IlbcDecoder*)iLBCdec_inst)->blockl],
            (const uint16_t*)&encoded
                [2 * i * ((IlbcDecoder*)iLBCdec_inst)->no_of_words],
            (IlbcDecoder*)iLBCdec_inst, 1) == -1)
      return -1;
    i++;
  }
  /* iLBC does not support VAD/CNG yet */
  *speechType=1;
  return (int)(i*((IlbcDecoder*)iLBCdec_inst)->blockl);
}

int WebRtcIlbcfix_Decode20Ms(IlbcDecoderInstance* iLBCdec_inst,
                             const uint8_t* encoded,
                             size_t len,
                             int16_t* decoded,
                             int16_t* speechType)
{
  size_t i=0;
  if ((len==((IlbcDecoder*)iLBCdec_inst)->no_of_bytes)||
      (len==2*((IlbcDecoder*)iLBCdec_inst)->no_of_bytes)||
      (len==3*((IlbcDecoder*)iLBCdec_inst)->no_of_bytes)) {
    /* ok, do nothing */
  } else {
    return(-1);
  }

  while ((i*((IlbcDecoder*)iLBCdec_inst)->no_of_bytes)<len) {
    if (!WebRtcIlbcfix_DecodeImpl(
        &decoded[i * ((IlbcDecoder*)iLBCdec_inst)->blockl],
        (const uint16_t*)&encoded
            [2 * i * ((IlbcDecoder*)iLBCdec_inst)->no_of_words],
        (IlbcDecoder*)iLBCdec_inst, 1))
      return -1;
    i++;
  }
  /* iLBC does not support VAD/CNG yet */
  *speechType=1;
  return (int)(i*((IlbcDecoder*)iLBCdec_inst)->blockl);
}

int WebRtcIlbcfix_Decode30Ms(IlbcDecoderInstance* iLBCdec_inst,
                             const uint8_t* encoded,
                             size_t len,
                             int16_t* decoded,
                             int16_t* speechType)
{
  size_t i=0;
  if ((len==((IlbcDecoder*)iLBCdec_inst)->no_of_bytes)||
      (len==2*((IlbcDecoder*)iLBCdec_inst)->no_of_bytes)||
      (len==3*((IlbcDecoder*)iLBCdec_inst)->no_of_bytes)) {
    /* ok, do nothing */
  } else {
    return(-1);
  }

  while ((i*((IlbcDecoder*)iLBCdec_inst)->no_of_bytes)<len) {
    if (!WebRtcIlbcfix_DecodeImpl(
        &decoded[i * ((IlbcDecoder*)iLBCdec_inst)->blockl],
        (const uint16_t*)&encoded
            [2 * i * ((IlbcDecoder*)iLBCdec_inst)->no_of_words],
        (IlbcDecoder*)iLBCdec_inst, 1))
      return -1;
    i++;
  }
  /* iLBC does not support VAD/CNG yet */
  *speechType=1;
  return (int)(i*((IlbcDecoder*)iLBCdec_inst)->blockl);
}

size_t WebRtcIlbcfix_DecodePlc(IlbcDecoderInstance* iLBCdec_inst,
                               int16_t* decoded,
                               size_t noOfLostFrames) {
  size_t i;
  uint16_t dummy;

  for (i=0;i<noOfLostFrames;i++) {
    // PLC decoding shouldn't fail, because there is no external input data
    // that can be bad.
    int result = WebRtcIlbcfix_DecodeImpl(
        &decoded[i * ((IlbcDecoder*)iLBCdec_inst)->blockl], &dummy,
        (IlbcDecoder*)iLBCdec_inst, 0);
    RTC_CHECK_EQ(result, 0);
  }
  return (noOfLostFrames*((IlbcDecoder*)iLBCdec_inst)->blockl);
}

size_t WebRtcIlbcfix_NetEqPlc(IlbcDecoderInstance* iLBCdec_inst,
                              int16_t* decoded,
                              size_t noOfLostFrames) {
  /* Two input parameters not used, but needed for function pointers in NetEQ */
  (void)(decoded = NULL);
  (void)(noOfLostFrames = 0);

  WebRtcSpl_MemSetW16(((IlbcDecoder*)iLBCdec_inst)->enh_buf, 0, ENH_BUFL);
  ((IlbcDecoder*)iLBCdec_inst)->prev_enh_pl = 2;

  return (0);
}

void WebRtcIlbcfix_version(char *version)
{
  strcpy((char*)version, "1.1.1");
}