-
Notifications
You must be signed in to change notification settings - Fork 1
/
amrdecode.c
469 lines (368 loc) · 14.9 KB
/
amrdecode.c
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
/* ------------------------------------------------------------------
* Copyright (C) 1998-2009 PacketVideo
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied.
* See the License for the specific language governing permissions
* and limitations under the License.
* -------------------------------------------------------------------
*/
/****************************************************************************************
Portions of this file are derived from the following 3GPP standard:
3GPP TS 26.073
ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec
Available from http://www.3gpp.org
(C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC)
Permission to distribute, modify and use this file under the standard license
terms listed above has been obtained from the copyright holder.
****************************************************************************************/
/*
------------------------------------------------------------------------------
Filename: amrdecode.cpp
------------------------------------------------------------------------------
*/
/*----------------------------------------------------------------------------
; INCLUDES
----------------------------------------------------------------------------*/
#include "amrdecode.h"
#include "cnst.h"
#include "typedef.h"
#include "frame.h"
#include "sp_dec.h"
#include "wmf_to_ets.h"
#include "if2_to_ets.h"
#include "frame_type_3gpp.h"
/*----------------------------------------------------------------------------
; MACROS
; Define module specific macros here
----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------
; DEFINES
; Include all pre-processor statements here. Include conditional
; compile variables also.
----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------
; LOCAL FUNCTION DEFINITIONS
; Function Prototype declaration
----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------
; LOCAL STORE/BUFFER/POINTER DEFINITIONS
; Variable declaration - defined here and used outside this module
----------------------------------------------------------------------------*/
/*
------------------------------------------------------------------------------
FUNCTION NAME: AMRDecode
------------------------------------------------------------------------------
INPUT AND OUTPUT DEFINITIONS
Inputs:
state_data = pointer to a structure (type void)
frame_type = 3GPP frame type (enum Frame_Type_3GPP)
speech_bits_ptr = pointer to the beginning of the raw encoded speech bits
for the current frame to be decoded (unsigned char)
raw_pcm_buffer = pointer to the output pcm outputs array (Word16)
input_format = input format used; valid values are AMR_WMF, AMR_IF2,
and AMR_ETS (Word16)
Outputs:
raw_pcm_buffer contains the newly decoded linear PCM speech samples
state_data->prev_mode contains the new mode
Returns:
byte_offset = address offset of the next frame to be processed or
error condition flag (-1) (int)
Global Variables Used:
WmfDecBytesPerFrame = table containing the number of core AMR data bytes
used by each codec mode for WMF input format (const
int)
If2DecBytesPerFrame = table containing the number of core AMR data bytes
used by each codec mode for IF2 input format (const
int)
Local Variables Needed:
None
------------------------------------------------------------------------------
FUNCTION DESCRIPTION
This function is the top level entry function to the GSM AMR Decoder library.
First, it checks the input format type (input_format) to determine the type
of de-formattting that needs to be done. If input_format is AMR_WMF, the input
data is in WMF (aka, non-IF2) format and the function wmf_to_ets will be
called to convert to the ETS format (1 bit/word, where 1 word = 16 bits),
and byte_offset will be updated according to the contents of WmfDecBytesPerFrame
table.
If input_format is AMR_IF2, the input data is in IF2 format [1] and the
function if2_to_ets will be called to convert to the ETS format, and
byte_offset will be updated according to the contents of If2DecBytesPerFrame
table.
The codec mode and receive frame type is initialized based on the incoming
frame_type.
If input_format is AMR_ETS, the input data is in the ETS format. The receive
frame type is set to the value in the first location of the buffer pointed to
by speech_bits_ptr. Then, the encoded speech parameters in the buffer pointed
to by speech_bits is copied to dec_ets_input_bfr and the type will be changed
from unsigned char to Word16. Lastly, if the receive frame type is not
RX_NO_DATA, the mode is obtained from the buffer pointed to by
speech_bits_ptr, offset by MAX_SERIAL_SIZE+1, otherwise, the mode is set to
the previous mode (found the in state_data->prev_mode).
If input_format is an unsupported format, byte_offset will be set to -1, to
indicate an error condition has occurred, and the function will exit.
If there are no errors, GSMFrameDecode is called to decode a 20 ms frame. It
puts the decoded linear PCM samples in the buffer pointed to by
raw_pcm_buffer. Then, the prev_mode field of the structure pointed to by
state_data is updated to the current mode.
This function returns the new byte_offset value to indicate the address
offset of the next speech frame to be decoded.
------------------------------------------------------------------------------
REQUIREMENTS
None
------------------------------------------------------------------------------
REFERENCES
[1] "AMR Speech Codec Frame Structure", 3GPP TS 26.101 version 4.1.0
Release 4, June 2001
------------------------------------------------------------------------------
PSEUDO-CODE
Note: AMRSID_RXTYPE_BIT_OFFSET = 35
AMRSID_RXMODE_BIT_OFFSET = 36
NUM_AMRSID_RXMODE_BITS = 3
// Set up Decoder state structure pointer
Speech_Decode_FrameState *decoder_state
= (Speech_Decode_FrameState *) state_data
// Determine type of de-formatting
// Decode WMF or IF2 frames
IF ((input_format == AMR_RX_WMF) | (input_format == AMR_RX_IF2))
THEN
IF (input_format == AMR_RX_WMF)
THEN
// Convert incoming packetized raw WMF data to ETS format
CALL wmf_to_ets(frame_type = frame_type
input_ptr = speech_bits_ptr
output_ptr = &dec_ets_input_bfr)
MODIFYING(nothing)
RETURNING(nothing)
// Address offset of the start of next frame
byte_offset = WmfDecBytesPerFrame[frame_type]
ELSEIF (input_format == AMR_RX_IF2)
THEN
// Convert incoming packetized raw IF2 data to ETS format
CALL if2_to_ets(frame_type = frame_type
input_ptr = speech_bits_ptr
output_ptr = &dec_ets_input_bfr)
MODIFYING(nothing)
RETURNING(nothing)
// Address offset of the start of next frame
byte_offset = If2DecBytesPerFrame[frame_type]
ENDIF
// Determine AMR codec mode and AMR RX frame type
IF (frame_type <= AMR_122)
THEN
mode = (enum Mode) frame_type;
rx_type = RX_SPEECH_GOOD;
ELSEIF (frame_type == AMR_SID)
THEN
// Clear mode store prior to reading mode info from input buffer
mode = 0
FOR i = 0 TO NUM_AMRSID_RXMODE_BITS-1
mode |= (dec_ets_input_bfr[AMRSID_RXMODE_BIT_OFFSET+i] << i)
ENDFOR
IF (dec_ets_input_bfr[AMRSID_RXTYPE_BIT_OFFSET] == 0)
THEN
rx_type = RX_SID_FIRST
ELSE
rx_type = RX_SID_UPDATE
ENDIF
ELSEIF ((frame_type > AMR_SID) && (frame_type < NO_DATA))
THEN
// Use previous mode
mode = decoder_state->prev_mode
// Unsupported SID frames
rx_type = RX_SPEECH_BAD;
ELSE
// Use previous mode
mode = decoder_state->prev_mode
// No data received
rx_type = RX_NO_DATA;
ENDIF
// Decode ETS frames
ELSEIF (input_format == AMR_RX_ETS)
THEN
// Change type of pointer to incoming raw ETS data
ets_word_ptr = (Word16 *) speech_bits_ptr
// Get RX frame type
rx_type = (enum RXFrameType) *ets_word_ptr
ets_word_ptr = ets_word_ptr + 1
// Copy incoming raw ETS data to dec_ets_input_bfr
FOR i = 0 TO MAX_SERIAL_SIZE-1
dec_ets_input_bfr[i] = *ets_word_ptr
ets_word_ptr = ets_word_ptr + 1
ENDFOR
// Get codec mode
IF (rx_type != RX_NO_DATA)
THEN
mode = (enum Mode) *ets_word_ptr
ELSE
//Use previous mode if no received data
mode = decoder_state->prev_mode
ENDIF
// Set up byte_offset
byte_offset = 2*(MAX_SERIAL_SIZE+2)
ELSE
// Invalid format, return error code
byte_offset = -1
ENDIF
// Proceed with decoding frame, if there are no errors
IF (byte_offset != -1)
THEN
// Decode a 20 ms frame
CALL GSMFrameDecode( st = decoder_state
mode = mode
serial = dec_ets_input_bfr,
frame_type = rx_type,
synth = (Word16 *)raw_pcm_buffer);
MODIFYING (nothing)
RETURNING (Nothing)
// Save mode for next frame
decoder_state->prev_mode = mode
ENDIF
RETURN (byte_offset)
------------------------------------------------------------------------------
CAUTION [optional]
[State any special notes, constraints or cautions for users of this function]
------------------------------------------------------------------------------
*/
Word16 AMRDecode(
void *state_data,
enum Frame_Type_3GPP frame_type,
UWord8 *speech_bits_ptr,
Word16 *raw_pcm_buffer,
bitstream_format input_format
)
{
Word16 *ets_word_ptr;
enum Mode mode = (enum Mode)MR475;
int modeStore;
int tempInt;
enum RXFrameType rx_type = RX_NO_DATA;
Word16 dec_ets_input_bfr[MAX_SERIAL_SIZE];
Word16 i;
Word16 byte_offset = -1;
/* Type cast state_data to Speech_Decode_FrameState rather than passing
* that structure type to this function so the structure make up can't
* be viewed from higher level functions than this.
*/
Speech_Decode_FrameState *decoder_state
= (Speech_Decode_FrameState *) state_data;
/* Determine type of de-formatting */
/* WMF or IF2 frames */
if ((input_format == MIME_IETF) | (input_format == IF2))
{
if (input_format == MIME_IETF)
{
/* Convert incoming packetized raw WMF data to ETS format */
wmf_to_ets(frame_type, speech_bits_ptr, dec_ets_input_bfr, &(decoder_state->decoder_amrState.common_amr_tbls));
/* Address offset of the start of next frame */
byte_offset = WmfDecBytesPerFrame[frame_type];
}
else /* else has to be input_format IF2 */
{
/* Convert incoming packetized raw IF2 data to ETS format */
if2_to_ets(frame_type, speech_bits_ptr, dec_ets_input_bfr, &(decoder_state->decoder_amrState.common_amr_tbls));
/* Address offset of the start of next frame */
byte_offset = If2DecBytesPerFrame[frame_type];
}
/* At this point, input data is in ETS format */
/* Determine AMR codec mode and AMR RX frame type */
if (frame_type <= AMR_122)
{
mode = (enum Mode) frame_type;
rx_type = RX_SPEECH_GOOD;
}
else if (frame_type == AMR_SID)
{
/* Clear mode store prior to reading mode info from input buffer */
modeStore = 0;
for (i = 0; i < NUM_AMRSID_RXMODE_BITS; i++)
{
tempInt = dec_ets_input_bfr[AMRSID_RXMODE_BIT_OFFSET+i] << i;
modeStore |= tempInt;
}
mode = (enum Mode) modeStore;
/* Get RX frame type */
if (dec_ets_input_bfr[AMRSID_RXTYPE_BIT_OFFSET] == 0)
{
rx_type = RX_SID_FIRST;
}
else
{
rx_type = RX_SID_UPDATE;
}
}
else if (frame_type < AMR_NO_DATA)
{
/* Invalid frame_type, return error code */
byte_offset = -1; /* !!! */
}
else
{
mode = decoder_state->prev_mode;
/*
* RX_NO_DATA, generate exponential decay from latest valid frame for the first 6 frames
* after that, create silent frames
*/
rx_type = RX_NO_DATA;
}
}
/* ETS frames */
else if (input_format == ETS)
{
/* Change type of pointer to incoming raw ETS data */
ets_word_ptr = (Word16 *) speech_bits_ptr;
/* Get RX frame type */
rx_type = (enum RXFrameType) * ets_word_ptr;
ets_word_ptr++;
/* Copy incoming raw ETS data to dec_ets_input_bfr */
for (i = 0; i < MAX_SERIAL_SIZE; i++)
{
dec_ets_input_bfr[i] = *ets_word_ptr;
ets_word_ptr++;
}
/* Get codec mode */
if (rx_type != RX_NO_DATA)
{
/* Get mode from input bitstream */
mode = (enum Mode) * ets_word_ptr;
}
else
{
/* Use previous mode if no received data */
mode = decoder_state->prev_mode;
}
/* Set up byte_offset */
byte_offset = 2 * (MAX_SERIAL_SIZE + 2);
}
else
{
/* Invalid input format, return error code */
byte_offset = -1;
}
/* Proceed with decoding frame, if there are no errors */
if (byte_offset != -1)
{
/* Decode a 20 ms frame */
#ifndef CONSOLE_DECODER_REF
/* Use PV version of sp_dec.c */
GSMFrameDecode(decoder_state, mode, dec_ets_input_bfr, rx_type,
raw_pcm_buffer);
#else
/* Use ETS version of sp_dec.c */
Speech_Decode_Frame(decoder_state, mode, dec_ets_input_bfr, rx_type,
raw_pcm_buffer);
#endif
/* Save mode for next frame */
decoder_state->prev_mode = mode;
}
return (byte_offset);
}