Initialize Tizen 2.3
[external/opencore-amr.git] / opencore / codecs_v2 / audio / gsm_amr / amr_nb / dec / src / amrdecode.cpp
1 /* ------------------------------------------------------------------
2  * Copyright (C) 1998-2009 PacketVideo
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 Portions of this file are derived from the following 3GPP standard:
20
21     3GPP TS 26.073
22     ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec
23     Available from http://www.3gpp.org
24
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 ****************************************************************************************/
29 /*
30 ------------------------------------------------------------------------------
31
32
33  Filename: amrdecode.cpp
34
35 ------------------------------------------------------------------------------
36 */
37
38 /*----------------------------------------------------------------------------
39 ; INCLUDES
40 ----------------------------------------------------------------------------*/
41 #include "amrdecode.h"
42 #include "cnst.h"
43 #include "typedef.h"
44 #include "frame.h"
45 #include "sp_dec.h"
46 #include "wmf_to_ets.h"
47 #include "if2_to_ets.h"
48 #include "frame_type_3gpp.h"
49
50 /*----------------------------------------------------------------------------
51 ; MACROS
52 ; Define module specific macros here
53 ----------------------------------------------------------------------------*/
54
55
56 /*----------------------------------------------------------------------------
57 ; DEFINES
58 ; Include all pre-processor statements here. Include conditional
59 ; compile variables also.
60 ----------------------------------------------------------------------------*/
61
62
63 /*----------------------------------------------------------------------------
64 ; LOCAL FUNCTION DEFINITIONS
65 ; Function Prototype declaration
66 ----------------------------------------------------------------------------*/
67
68 /*----------------------------------------------------------------------------
69 ; LOCAL STORE/BUFFER/POINTER DEFINITIONS
70 ; Variable declaration - defined here and used outside this module
71 ----------------------------------------------------------------------------*/
72
73 /*
74 ------------------------------------------------------------------------------
75  FUNCTION NAME: AMRDecode
76 ------------------------------------------------------------------------------
77  INPUT AND OUTPUT DEFINITIONS
78
79  Inputs:
80     state_data      = pointer to a structure (type void)
81
82     frame_type      = 3GPP frame type (enum Frame_Type_3GPP)
83
84     speech_bits_ptr = pointer to the beginning of the raw encoded speech bits
85                       for the current frame to be decoded (unsigned char)
86
87     raw_pcm_buffer  = pointer to the output pcm outputs array (Word16)
88
89     input_format    = input format used; valid values are AMR_WMF, AMR_IF2,
90                       and AMR_ETS (Word16)
91
92  Outputs:
93     raw_pcm_buffer contains the newly decoded linear PCM speech samples
94     state_data->prev_mode contains the new mode
95
96  Returns:
97     byte_offset     = address offset of the next frame to be processed or
98                       error condition flag (-1) (int)
99
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
103                           int)
104
105     If2DecBytesPerFrame = table containing the number of core AMR data bytes
106                           used by each codec mode for IF2 input format (const
107                           int)
108
109  Local Variables Needed:
110     None
111
112 ------------------------------------------------------------------------------
113  FUNCTION DESCRIPTION
114
115  This function is the top level entry function to the GSM AMR Decoder library.
116
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
122  table.
123
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
127  table.
128
129  The codec mode and receive frame type is initialized based on the incoming
130  frame_type.
131
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).
140
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.
143
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.
148
149  This function returns the new byte_offset value to indicate the address
150  offset of the next speech frame to be decoded.
151
152 ------------------------------------------------------------------------------
153  REQUIREMENTS
154
155  None
156
157 ------------------------------------------------------------------------------
158  REFERENCES
159
160  [1] "AMR Speech Codec Frame Structure", 3GPP TS 26.101 version 4.1.0
161      Release 4, June 2001
162
163 ------------------------------------------------------------------------------
164  PSEUDO-CODE
165
166  Note: AMRSID_RXTYPE_BIT_OFFSET = 35
167        AMRSID_RXMODE_BIT_OFFSET = 36
168        NUM_AMRSID_RXMODE_BITS = 3
169
170
171  // Set up Decoder state structure pointer
172  Speech_Decode_FrameState *decoder_state
173             = (Speech_Decode_FrameState *) state_data
174
175  // Determine type of de-formatting
176
177  // Decode WMF or IF2 frames
178  IF ((input_format == AMR_RX_WMF) | (input_format == AMR_RX_IF2))
179  THEN
180      IF (input_format == AMR_RX_WMF)
181      THEN
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)
186            MODIFYING(nothing)
187            RETURNING(nothing)
188
189          // Address offset of the start of next frame
190          byte_offset = WmfDecBytesPerFrame[frame_type]
191
192      ELSEIF (input_format == AMR_RX_IF2)
193      THEN
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)
198            MODIFYING(nothing)
199            RETURNING(nothing)
200
201          // Address offset of the start of next frame
202          byte_offset = If2DecBytesPerFrame[frame_type]
203
204      ENDIF
205
206        // Determine AMR codec mode and AMR RX frame type
207      IF (frame_type <= AMR_122)
208      THEN
209          mode = (enum Mode) frame_type;
210          rx_type = RX_SPEECH_GOOD;
211
212      ELSEIF (frame_type == AMR_SID)
213      THEN
214          // Clear mode store prior to reading mode info from input buffer
215          mode = 0
216
217          FOR i = 0 TO NUM_AMRSID_RXMODE_BITS-1
218
219              mode |= (dec_ets_input_bfr[AMRSID_RXMODE_BIT_OFFSET+i] << i)
220
221          ENDFOR
222
223          IF (dec_ets_input_bfr[AMRSID_RXTYPE_BIT_OFFSET] == 0)
224          THEN
225              rx_type = RX_SID_FIRST
226
227          ELSE
228              rx_type = RX_SID_UPDATE
229
230          ENDIF
231
232      ELSEIF ((frame_type > AMR_SID) && (frame_type < NO_DATA))
233      THEN
234          // Use previous mode
235          mode = decoder_state->prev_mode
236
237          // Unsupported SID frames
238          rx_type = RX_SPEECH_BAD;
239
240      ELSE
241          // Use previous mode
242          mode = decoder_state->prev_mode
243
244          // No data received
245          rx_type = RX_NO_DATA;
246
247      ENDIF
248
249  // Decode ETS frames
250  ELSEIF (input_format == AMR_RX_ETS)
251  THEN
252      // Change type of pointer to incoming raw ETS data
253      ets_word_ptr = (Word16 *) speech_bits_ptr
254
255      // Get RX frame type
256      rx_type = (enum RXFrameType) *ets_word_ptr
257      ets_word_ptr = ets_word_ptr + 1
258
259      // Copy incoming raw ETS data to dec_ets_input_bfr
260      FOR i = 0 TO MAX_SERIAL_SIZE-1
261
262          dec_ets_input_bfr[i] = *ets_word_ptr
263          ets_word_ptr = ets_word_ptr + 1
264
265      ENDFOR
266
267      // Get codec mode
268      IF (rx_type != RX_NO_DATA)
269      THEN
270          mode = (enum Mode) *ets_word_ptr
271
272      ELSE
273          //Use previous mode if no received data
274          mode = decoder_state->prev_mode
275
276      ENDIF
277
278      // Set up byte_offset
279      byte_offset = 2*(MAX_SERIAL_SIZE+2)
280
281  ELSE
282      // Invalid format, return error code
283      byte_offset = -1
284
285  ENDIF
286
287  // Proceed with decoding frame, if there are no errors
288  IF (byte_offset != -1)
289  THEN
290      // Decode a 20 ms frame
291      CALL GSMFrameDecode( st = decoder_state
292                           mode = mode
293                           serial = dec_ets_input_bfr,
294                           frame_type = rx_type,
295                           synth = (Word16 *)raw_pcm_buffer);
296        MODIFYING (nothing)
297        RETURNING (Nothing)
298
299      // Save mode for next frame
300      decoder_state->prev_mode = mode
301
302  ENDIF
303
304  RETURN (byte_offset)
305
306 ------------------------------------------------------------------------------
307  CAUTION [optional]
308  [State any special notes, constraints or cautions for users of this function]
309
310 ------------------------------------------------------------------------------
311 */
312
313 Word16 AMRDecode(
314     void                      *state_data,
315     enum Frame_Type_3GPP      frame_type,
316     UWord8                    *speech_bits_ptr,
317     Word16                    *raw_pcm_buffer,
318     bitstream_format          input_format
319 )
320 {
321     Word16 *ets_word_ptr;
322     enum Mode mode = (enum Mode)MR475;
323     int modeStore;
324     int tempInt;
325     enum RXFrameType rx_type = RX_NO_DATA;
326     Word16 dec_ets_input_bfr[MAX_SERIAL_SIZE];
327     Word16 i;
328     Word16 byte_offset = -1;
329
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.
333      */
334     Speech_Decode_FrameState *decoder_state
335     = (Speech_Decode_FrameState *) state_data;
336
337     /* Determine type of de-formatting */
338     /* WMF or IF2 frames */
339     if ((input_format == MIME_IETF) | (input_format == IF2))
340     {
341         if (input_format == MIME_IETF)
342         {
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));
345
346             /* Address offset of the start of next frame */
347             byte_offset = WmfDecBytesPerFrame[frame_type];
348         }
349         else   /* else has to be input_format  IF2 */
350         {
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));
353
354             /* Address offset of the start of next frame */
355             byte_offset = If2DecBytesPerFrame[frame_type];
356         }
357
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)
361         {
362             mode = (enum Mode) frame_type;
363             rx_type = RX_SPEECH_GOOD;
364         }
365         else if (frame_type == AMR_SID)
366         {
367             /* Clear mode store prior to reading mode info from input buffer */
368             modeStore = 0;
369
370             for (i = 0; i < NUM_AMRSID_RXMODE_BITS; i++)
371             {
372                 tempInt = dec_ets_input_bfr[AMRSID_RXMODE_BIT_OFFSET+i] << i;
373                 modeStore |= tempInt;
374             }
375             mode = (enum Mode) modeStore;
376
377             /* Get RX frame type */
378             if (dec_ets_input_bfr[AMRSID_RXTYPE_BIT_OFFSET] == 0)
379             {
380                 rx_type = RX_SID_FIRST;
381             }
382             else
383             {
384                 rx_type = RX_SID_UPDATE;
385             }
386         }
387         else if (frame_type < AMR_NO_DATA)
388         {
389             /* Invalid frame_type, return error code */
390             byte_offset = -1;   /*  !!! */
391         }
392         else
393         {
394             mode = decoder_state->prev_mode;
395
396             /*
397              * RX_NO_DATA, generate exponential decay from latest valid frame for the first 6 frames
398              * after that, create silent frames
399              */
400             rx_type = RX_NO_DATA;
401
402         }
403
404     }
405
406     /* ETS frames */
407     else if (input_format == ETS)
408     {
409         /* Change type of pointer to incoming raw ETS data */
410         ets_word_ptr = (Word16 *) speech_bits_ptr;
411
412         /* Get RX frame type */
413         rx_type = (enum RXFrameType) * ets_word_ptr;
414         ets_word_ptr++;
415
416         /* Copy incoming raw ETS data to dec_ets_input_bfr */
417         for (i = 0; i < MAX_SERIAL_SIZE; i++)
418         {
419             dec_ets_input_bfr[i] = *ets_word_ptr;
420             ets_word_ptr++;
421         }
422
423         /* Get codec mode */
424         if (rx_type != RX_NO_DATA)
425         {
426             /* Get mode from input bitstream */
427             mode = (enum Mode) * ets_word_ptr;
428         }
429         else
430         {
431             /* Use previous mode if no received data */
432             mode = decoder_state->prev_mode;
433         }
434
435         /* Set up byte_offset */
436         byte_offset = 2 * (MAX_SERIAL_SIZE + 2);
437     }
438     else
439     {
440         /* Invalid input format, return error code */
441         byte_offset = -1;
442     }
443
444     /* Proceed with decoding frame, if there are no errors */
445     if (byte_offset != -1)
446     {
447         /* Decode a 20 ms frame */
448
449 #ifndef CONSOLE_DECODER_REF
450         /* Use PV version of sp_dec.c */
451         GSMFrameDecode(decoder_state, mode, dec_ets_input_bfr, rx_type,
452                        raw_pcm_buffer);
453
454 #else
455         /* Use ETS version of sp_dec.c */
456         Speech_Decode_Frame(decoder_state, mode, dec_ets_input_bfr, rx_type,
457                             raw_pcm_buffer);
458
459 #endif
460
461         /* Save mode for next frame */
462         decoder_state->prev_mode = mode;
463     }
464
465     return (byte_offset);
466 }
467
468
469