1 /* ------------------------------------------------------------------
2 * Copyright (C) 1998-2009 PacketVideo
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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
14 * See the License for the specific language governing permissions
15 * and limitations under the License.
16 * -------------------------------------------------------------------
18 /****************************************************************************************
19 Portions of this file are derived from the following 3GPP standard:
22 ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec
23 Available from http://www.3gpp.org
25 (C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC)
26 Permission to distribute, modify and use this file under the standard license
27 terms listed above has been obtained from the copyright holder.
28 ****************************************************************************************/
30 ------------------------------------------------------------------------------
33 Filename: amrdecode.cpp
35 ------------------------------------------------------------------------------
38 /*----------------------------------------------------------------------------
40 ----------------------------------------------------------------------------*/
41 #include "amrdecode.h"
46 #include "wmf_to_ets.h"
47 #include "if2_to_ets.h"
48 #include "frame_type_3gpp.h"
50 /*----------------------------------------------------------------------------
52 ; Define module specific macros here
53 ----------------------------------------------------------------------------*/
56 /*----------------------------------------------------------------------------
58 ; Include all pre-processor statements here. Include conditional
59 ; compile variables also.
60 ----------------------------------------------------------------------------*/
63 /*----------------------------------------------------------------------------
64 ; LOCAL FUNCTION DEFINITIONS
65 ; Function Prototype declaration
66 ----------------------------------------------------------------------------*/
68 /*----------------------------------------------------------------------------
69 ; LOCAL STORE/BUFFER/POINTER DEFINITIONS
70 ; Variable declaration - defined here and used outside this module
71 ----------------------------------------------------------------------------*/
74 ------------------------------------------------------------------------------
75 FUNCTION NAME: AMRDecode
76 ------------------------------------------------------------------------------
77 INPUT AND OUTPUT DEFINITIONS
80 state_data = pointer to a structure (type void)
82 frame_type = 3GPP frame type (enum Frame_Type_3GPP)
84 speech_bits_ptr = pointer to the beginning of the raw encoded speech bits
85 for the current frame to be decoded (unsigned char)
87 raw_pcm_buffer = pointer to the output pcm outputs array (Word16)
89 input_format = input format used; valid values are AMR_WMF, AMR_IF2,
93 raw_pcm_buffer contains the newly decoded linear PCM speech samples
94 state_data->prev_mode contains the new mode
97 byte_offset = address offset of the next frame to be processed or
98 error condition flag (-1) (int)
100 Global Variables Used:
101 WmfDecBytesPerFrame = table containing the number of core AMR data bytes
102 used by each codec mode for WMF input format (const
105 If2DecBytesPerFrame = table containing the number of core AMR data bytes
106 used by each codec mode for IF2 input format (const
109 Local Variables Needed:
112 ------------------------------------------------------------------------------
115 This function is the top level entry function to the GSM AMR Decoder library.
117 First, it checks the input format type (input_format) to determine the type
118 of de-formattting that needs to be done. If input_format is AMR_WMF, the input
119 data is in WMF (aka, non-IF2) format and the function wmf_to_ets will be
120 called to convert to the ETS format (1 bit/word, where 1 word = 16 bits),
121 and byte_offset will be updated according to the contents of WmfDecBytesPerFrame
124 If input_format is AMR_IF2, the input data is in IF2 format [1] and the
125 function if2_to_ets will be called to convert to the ETS format, and
126 byte_offset will be updated according to the contents of If2DecBytesPerFrame
129 The codec mode and receive frame type is initialized based on the incoming
132 If input_format is AMR_ETS, the input data is in the ETS format. The receive
133 frame type is set to the value in the first location of the buffer pointed to
134 by speech_bits_ptr. Then, the encoded speech parameters in the buffer pointed
135 to by speech_bits is copied to dec_ets_input_bfr and the type will be changed
136 from unsigned char to Word16. Lastly, if the receive frame type is not
137 RX_NO_DATA, the mode is obtained from the buffer pointed to by
138 speech_bits_ptr, offset by MAX_SERIAL_SIZE+1, otherwise, the mode is set to
139 the previous mode (found the in state_data->prev_mode).
141 If input_format is an unsupported format, byte_offset will be set to -1, to
142 indicate an error condition has occurred, and the function will exit.
144 If there are no errors, GSMFrameDecode is called to decode a 20 ms frame. It
145 puts the decoded linear PCM samples in the buffer pointed to by
146 raw_pcm_buffer. Then, the prev_mode field of the structure pointed to by
147 state_data is updated to the current mode.
149 This function returns the new byte_offset value to indicate the address
150 offset of the next speech frame to be decoded.
152 ------------------------------------------------------------------------------
157 ------------------------------------------------------------------------------
160 [1] "AMR Speech Codec Frame Structure", 3GPP TS 26.101 version 4.1.0
163 ------------------------------------------------------------------------------
166 Note: AMRSID_RXTYPE_BIT_OFFSET = 35
167 AMRSID_RXMODE_BIT_OFFSET = 36
168 NUM_AMRSID_RXMODE_BITS = 3
171 // Set up Decoder state structure pointer
172 Speech_Decode_FrameState *decoder_state
173 = (Speech_Decode_FrameState *) state_data
175 // Determine type of de-formatting
177 // Decode WMF or IF2 frames
178 IF ((input_format == AMR_RX_WMF) | (input_format == AMR_RX_IF2))
180 IF (input_format == AMR_RX_WMF)
182 // Convert incoming packetized raw WMF data to ETS format
183 CALL wmf_to_ets(frame_type = frame_type
184 input_ptr = speech_bits_ptr
185 output_ptr = &dec_ets_input_bfr)
189 // Address offset of the start of next frame
190 byte_offset = WmfDecBytesPerFrame[frame_type]
192 ELSEIF (input_format == AMR_RX_IF2)
194 // Convert incoming packetized raw IF2 data to ETS format
195 CALL if2_to_ets(frame_type = frame_type
196 input_ptr = speech_bits_ptr
197 output_ptr = &dec_ets_input_bfr)
201 // Address offset of the start of next frame
202 byte_offset = If2DecBytesPerFrame[frame_type]
206 // Determine AMR codec mode and AMR RX frame type
207 IF (frame_type <= AMR_122)
209 mode = (enum Mode) frame_type;
210 rx_type = RX_SPEECH_GOOD;
212 ELSEIF (frame_type == AMR_SID)
214 // Clear mode store prior to reading mode info from input buffer
217 FOR i = 0 TO NUM_AMRSID_RXMODE_BITS-1
219 mode |= (dec_ets_input_bfr[AMRSID_RXMODE_BIT_OFFSET+i] << i)
223 IF (dec_ets_input_bfr[AMRSID_RXTYPE_BIT_OFFSET] == 0)
225 rx_type = RX_SID_FIRST
228 rx_type = RX_SID_UPDATE
232 ELSEIF ((frame_type > AMR_SID) && (frame_type < NO_DATA))
235 mode = decoder_state->prev_mode
237 // Unsupported SID frames
238 rx_type = RX_SPEECH_BAD;
242 mode = decoder_state->prev_mode
245 rx_type = RX_NO_DATA;
250 ELSEIF (input_format == AMR_RX_ETS)
252 // Change type of pointer to incoming raw ETS data
253 ets_word_ptr = (Word16 *) speech_bits_ptr
256 rx_type = (enum RXFrameType) *ets_word_ptr
257 ets_word_ptr = ets_word_ptr + 1
259 // Copy incoming raw ETS data to dec_ets_input_bfr
260 FOR i = 0 TO MAX_SERIAL_SIZE-1
262 dec_ets_input_bfr[i] = *ets_word_ptr
263 ets_word_ptr = ets_word_ptr + 1
268 IF (rx_type != RX_NO_DATA)
270 mode = (enum Mode) *ets_word_ptr
273 //Use previous mode if no received data
274 mode = decoder_state->prev_mode
278 // Set up byte_offset
279 byte_offset = 2*(MAX_SERIAL_SIZE+2)
282 // Invalid format, return error code
287 // Proceed with decoding frame, if there are no errors
288 IF (byte_offset != -1)
290 // Decode a 20 ms frame
291 CALL GSMFrameDecode( st = decoder_state
293 serial = dec_ets_input_bfr,
294 frame_type = rx_type,
295 synth = (Word16 *)raw_pcm_buffer);
299 // Save mode for next frame
300 decoder_state->prev_mode = mode
306 ------------------------------------------------------------------------------
308 [State any special notes, constraints or cautions for users of this function]
310 ------------------------------------------------------------------------------
315 enum Frame_Type_3GPP frame_type,
316 UWord8 *speech_bits_ptr,
317 Word16 *raw_pcm_buffer,
318 bitstream_format input_format
321 Word16 *ets_word_ptr;
322 enum Mode mode = (enum Mode)MR475;
325 enum RXFrameType rx_type = RX_NO_DATA;
326 Word16 dec_ets_input_bfr[MAX_SERIAL_SIZE];
328 Word16 byte_offset = -1;
330 /* Type cast state_data to Speech_Decode_FrameState rather than passing
331 * that structure type to this function so the structure make up can't
332 * be viewed from higher level functions than this.
334 Speech_Decode_FrameState *decoder_state
335 = (Speech_Decode_FrameState *) state_data;
337 /* Determine type of de-formatting */
338 /* WMF or IF2 frames */
339 if ((input_format == MIME_IETF) | (input_format == IF2))
341 if (input_format == MIME_IETF)
343 /* Convert incoming packetized raw WMF data to ETS format */
344 wmf_to_ets(frame_type, speech_bits_ptr, dec_ets_input_bfr, &(decoder_state->decoder_amrState.common_amr_tbls));
346 /* Address offset of the start of next frame */
347 byte_offset = WmfDecBytesPerFrame[frame_type];
349 else /* else has to be input_format IF2 */
351 /* Convert incoming packetized raw IF2 data to ETS format */
352 if2_to_ets(frame_type, speech_bits_ptr, dec_ets_input_bfr, &(decoder_state->decoder_amrState.common_amr_tbls));
354 /* Address offset of the start of next frame */
355 byte_offset = If2DecBytesPerFrame[frame_type];
358 /* At this point, input data is in ETS format */
359 /* Determine AMR codec mode and AMR RX frame type */
360 if (frame_type <= AMR_122)
362 mode = (enum Mode) frame_type;
363 rx_type = RX_SPEECH_GOOD;
365 else if (frame_type == AMR_SID)
367 /* Clear mode store prior to reading mode info from input buffer */
370 for (i = 0; i < NUM_AMRSID_RXMODE_BITS; i++)
372 tempInt = dec_ets_input_bfr[AMRSID_RXMODE_BIT_OFFSET+i] << i;
373 modeStore |= tempInt;
375 mode = (enum Mode) modeStore;
377 /* Get RX frame type */
378 if (dec_ets_input_bfr[AMRSID_RXTYPE_BIT_OFFSET] == 0)
380 rx_type = RX_SID_FIRST;
384 rx_type = RX_SID_UPDATE;
387 else if (frame_type < AMR_NO_DATA)
389 /* Invalid frame_type, return error code */
390 byte_offset = -1; /* !!! */
394 mode = decoder_state->prev_mode;
397 * RX_NO_DATA, generate exponential decay from latest valid frame for the first 6 frames
398 * after that, create silent frames
400 rx_type = RX_NO_DATA;
407 else if (input_format == ETS)
409 /* Change type of pointer to incoming raw ETS data */
410 ets_word_ptr = (Word16 *) speech_bits_ptr;
412 /* Get RX frame type */
413 rx_type = (enum RXFrameType) * ets_word_ptr;
416 /* Copy incoming raw ETS data to dec_ets_input_bfr */
417 for (i = 0; i < MAX_SERIAL_SIZE; i++)
419 dec_ets_input_bfr[i] = *ets_word_ptr;
424 if (rx_type != RX_NO_DATA)
426 /* Get mode from input bitstream */
427 mode = (enum Mode) * ets_word_ptr;
431 /* Use previous mode if no received data */
432 mode = decoder_state->prev_mode;
435 /* Set up byte_offset */
436 byte_offset = 2 * (MAX_SERIAL_SIZE + 2);
440 /* Invalid input format, return error code */
444 /* Proceed with decoding frame, if there are no errors */
445 if (byte_offset != -1)
447 /* Decode a 20 ms frame */
449 #ifndef CONSOLE_DECODER_REF
450 /* Use PV version of sp_dec.c */
451 GSMFrameDecode(decoder_state, mode, dec_ets_input_bfr, rx_type,
455 /* Use ETS version of sp_dec.c */
456 Speech_Decode_Frame(decoder_state, mode, dec_ets_input_bfr, rx_type,
461 /* Save mode for next frame */
462 decoder_state->prev_mode = mode;
465 return (byte_offset);