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 ------------------------------------------------------------------------------
35 Functions: GSMInitDecode
36 Speech_Decode_Frame_reset
40 ------------------------------------------------------------------------------
43 This file contains the functions that initialize, invoke, reset, and exit
46 ------------------------------------------------------------------------------
49 /*----------------------------------------------------------------------------
51 ----------------------------------------------------------------------------*/
60 #include "bitno_tab.h"
63 /*----------------------------------------------------------------------------
65 ; Define module specific macros here
66 ----------------------------------------------------------------------------*/
68 /*----------------------------------------------------------------------------
70 ; Include all pre-processor statements here. Include conditional
71 ; compile variables also.
72 ----------------------------------------------------------------------------*/
74 /*----------------------------------------------------------------------------
75 ; LOCAL FUNCTION DEFINITIONS
76 ; Function Prototype declaration
77 ----------------------------------------------------------------------------*/
79 /*----------------------------------------------------------------------------
80 ; LOCAL VARIABLE DEFINITIONS
81 ; Variable declaration - defined here and used outside this module
82 ----------------------------------------------------------------------------*/
85 ------------------------------------------------------------------------------
86 FUNCTION NAME: Bin2int
87 ------------------------------------------------------------------------------
88 INPUT AND OUTPUT DEFINITIONS
91 no_of_bits = number of bits associated with value
92 bitstream = pointer to buffer where bits are read
100 Global Variables Used:
103 Local Variables Needed:
106 ------------------------------------------------------------------------------
110 Purpose : Read "no_of_bits" bits from the array bitstream[]
111 and convert to integer.
113 ------------------------------------------------------------------------------
118 ------------------------------------------------------------------------------
121 bits2prm.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
123 ------------------------------------------------------------------------------
126 static Word16 Bin2int ( // Reconstructed parameter
127 Word16 no_of_bits, // input : number of bits associated with value
128 Word16 *bitstream // output: address where bits are written
131 Word16 value, i, bit;
134 for (i = 0; i < no_of_bits; i++)
136 value = shl (value, 1);
138 if (sub (bit, BIT_1) == 0)
139 value = add (value, 1);
144 ------------------------------------------------------------------------------
146 [State any special notes, constraints or cautions for users of this function]
148 ------------------------------------------------------------------------------
151 /*----------------------------------------------------------------------------
153 ----------------------------------------------------------------------------*/
154 static Word16 Bin2int( /* Reconstructed parameter */
155 Word16 no_of_bits, /* input : number of bits associated with value */
156 Word16 *bitstream /* input: address where bits are read from */
164 for (i = 0; i < no_of_bits; i++)
167 single_bit = *(bitstream++);
175 ------------------------------------------------------------------------------
176 FUNCTION NAME: bits2prm
177 ------------------------------------------------------------------------------
178 INPUT AND OUTPUT DEFINITIONS
181 mode = AMR mode of type enum Mode
182 bits[] = pointer to serial bits of type Word16
183 prm[] = pointer to analysis parameters of type Word16
191 Global Variables Used:
194 Local Variables Needed:
197 ------------------------------------------------------------------------------
201 Purpose : Retrieves the vector of encoder parameters from
202 the received serial bits in a frame.
204 ------------------------------------------------------------------------------
209 ------------------------------------------------------------------------------
212 bits2prm.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
214 ------------------------------------------------------------------------------
218 enum Mode mode, // i : AMR mode
219 Word16 bits[], // i : serial bits (size <= MAX_SERIAL_SIZE)
220 Word16 prm[] // o : analysis parameters (size <= MAX_PRM_SIZE)
225 for (i = 0; i < prmno[mode]; i++)
227 prm[i] = Bin2int (bitno[mode][i], bits);
228 bits += bitno[mode][i];
229 add(0,0); // account for above pointer update
235 ------------------------------------------------------------------------------
237 [State any special notes, constraints or cautions for users of this function]
239 ------------------------------------------------------------------------------
242 /*----------------------------------------------------------------------------
244 ----------------------------------------------------------------------------*/
246 enum Mode mode, /* i : AMR mode */
247 Word16 bits[], /* i : serial bits (size <= MAX_SERIAL_SIZE) */
248 Word16 prm[], /* o : analysis parameters (size <= MAX_PRM_SIZE) */
249 CommonAmrTbls* common_amr_tbls /* i : ptr to strcut of table ptrs */
253 const Word16* prmno_ptr = common_amr_tbls->prmno_ptr;
254 const Word16* const* bitno_ptr = common_amr_tbls->bitno_ptr;
257 for (i = 0; i < prmno_ptr[mode]; i++)
259 prm[i] = Bin2int(bitno_ptr[mode][i], bits);
260 bits += bitno_ptr[mode][i];
270 ------------------------------------------------------------------------------
271 FUNCTION NAME: GSMInitDecode
272 ------------------------------------------------------------------------------
273 INPUT AND OUTPUT DEFINITIONS
276 state = pointer to an array of pointers to structures of type
277 Speech_Decode_FrameState
278 no_hp_post_MR122 = flag to turn off high-pass post filter for 12.2 kbps
280 id = pointer to an array whose contents are of type char
283 decoder_amrState field of the structure pointed to by the pointer pointed
284 to by state is set to NULL
285 post_state field of the structure pointed to by the pointer pointed to
286 by state is set to NULL
287 postHP_state field of the structure pointed to by the pointer pointed to
288 by state is set to NULL
289 no_hp_post_MR122 field of the structure pointed to by the pointer pointed
290 to by state is set to the input no_hp_post_MR122
293 return_value = set to zero, if initialization was successful; -1,
296 Global Variables Used:
299 Local Variables Needed:
302 ------------------------------------------------------------------------------
305 This function allocates memory for filter structure and initializes state
306 memory used by the GSM AMR decoder.
308 ------------------------------------------------------------------------------
313 ------------------------------------------------------------------------------
316 sp_dec.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
318 ------------------------------------------------------------------------------
321 Note: Original function name of Speech_Decode_Frame_init was changed to
322 GSMInitDecode in the Code section.
324 int Speech_Decode_Frame_init (Speech_Decode_FrameState **state,
327 Speech_Decode_FrameState* s;
329 if (state == (Speech_Decode_FrameState **) NULL){
330 fprintf(stderr, "Speech_Decode_Frame_init: invalid parameter\n");
336 if ((s= (Speech_Decode_FrameState *)
337 malloc(sizeof(Speech_Decode_FrameState))) == NULL) {
338 fprintf(stderr, "Speech_Decode_Frame_init: can not malloc state "
342 s->decoder_amrState = NULL;
343 s->post_state = NULL;
344 s->postHP_state = NULL;
346 if (Decoder_amr_init(&s->decoder_amrState) ||
347 Post_Filter_init(&s->post_state) ||
348 Post_Process_init(&s->postHP_state) ) {
349 Speech_Decode_Frame_exit(&s);
353 s->complexityCounter = getCounterId(id);
355 Speech_Decode_Frame_reset(s);
361 ------------------------------------------------------------------------------
363 [State any special notes, constraints or cautions for users of this function]
365 ------------------------------------------------------------------------------
368 Word16 GSMInitDecode(void **state_data,
371 Speech_Decode_FrameState* s;
374 if (state_data == NULL)
376 /* fprintf(stderr, "Speech_Decode_Frame_init:
377 invalid parameter\n"); */
382 /* allocate memory */
383 if ((s = (Speech_Decode_FrameState *)
384 oscl_malloc(sizeof(Speech_Decode_FrameState))) == NULL)
386 /* fprintf(stderr, "Speech_Decode_Frame_init: can not malloc state "
391 if (Decoder_amr_init(&s->decoder_amrState)
392 || Post_Process_reset(&s->postHP_state))
394 Speech_Decode_FrameState *tmp = s;
396 * dereferencing type-punned pointer avoid
397 * breaking strict-aliasing rules
399 void** tempVoid = (void**) tmp;
400 GSMDecodeFrameExit(tempVoid);
405 Speech_Decode_Frame_reset(s);
406 *state_data = (void *)s;
412 /****************************************************************************/
416 ------------------------------------------------------------------------------
417 FUNCTION NAME: Speech_Decode_Frame_reset
418 ------------------------------------------------------------------------------
419 INPUT AND OUTPUT DEFINITIONS
422 state = pointer to structures of type Speech_Decode_FrameState
428 return_value = set to zero if reset was successful; -1, otherwise (int)
430 Global Variables Used:
433 Local Variables Needed:
436 ------------------------------------------------------------------------------
439 This function resets the state memory used by the GSM AMR decoder.
441 ------------------------------------------------------------------------------
446 ------------------------------------------------------------------------------
449 sp_dec.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
451 ------------------------------------------------------------------------------
454 int Speech_Decode_Frame_reset (Speech_Decode_FrameState *state)
456 if (state == (Speech_Decode_FrameState *) NULL){
457 fprintf(stderr, "Speech_Decode_Frame_reset: invalid parameter\n");
461 Decoder_amr_reset(state->decoder_amrState, (enum Mode)0);
462 Post_Filter_reset(state->post_state);
463 Post_Process_reset(state->postHP_state);
465 state->prev_mode = (enum Mode)0;
467 setCounter(state->complexityCounter);
468 Init_WMOPS_counter();
469 setCounter(0); // set counter to global counter
474 ------------------------------------------------------------------------------
476 [State any special notes, constraints or cautions for users of this function]
478 ------------------------------------------------------------------------------
480 Word16 Speech_Decode_Frame_reset(void *state_data)
483 Speech_Decode_FrameState *state =
484 (Speech_Decode_FrameState *) state_data;
486 if (state_data == NULL)
488 /* fprintf(stderr, "Speech_Decode_Frame_reset:
489 invalid parameter\n"); */
493 Decoder_amr_reset(&(state->decoder_amrState), MR475);
494 Post_Filter_reset(&(state->post_state));
495 Post_Process_reset(&(state->postHP_state));
497 state->prev_mode = MR475;
502 /****************************************************************************/
505 ------------------------------------------------------------------------------
506 FUNCTION NAME: GSMDecodeFrameExit
507 ------------------------------------------------------------------------------
508 INPUT AND OUTPUT DEFINITIONS
511 state = pointer to an array of pointers to structures of type
512 Speech_Decode_FrameState
515 state contents is set to NULL
520 Global Variables Used:
523 Local Variables Needed:
526 ------------------------------------------------------------------------------
529 This function frees up the memory used for the state memory of the GSM AMR
532 ------------------------------------------------------------------------------
537 ------------------------------------------------------------------------------
540 sp_dec.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
542 ------------------------------------------------------------------------------
545 Note: The original function name of Speech_Decode_Frame_exit was changed to
546 GSMDecodeFrameExit in the Code section.
548 void Speech_Decode_Frame_exit (Speech_Decode_FrameState **state)
550 if (state == NULL || *state == NULL)
553 Decoder_amr_exit(&(*state)->decoder_amrState);
554 Post_Filter_exit(&(*state)->post_state);
555 Post_Process_exit(&(*state)->postHP_state);
557 setCounter((*state)->complexityCounter);
559 setCounter(0); // set counter to global counter
568 ------------------------------------------------------------------------------
570 [State any special notes, constraints or cautions for users of this function]
572 ------------------------------------------------------------------------------
575 void GSMDecodeFrameExit(void **state_data)
578 Speech_Decode_FrameState **state =
579 (Speech_Decode_FrameState **) state_data;
581 if (state == NULL || *state == NULL)
586 /* deallocate memory */
593 /****************************************************************************/
596 ------------------------------------------------------------------------------
597 FUNCTION NAME: GSMFrameDecode
598 ------------------------------------------------------------------------------
599 INPUT AND OUTPUT DEFINITIONS
602 st = pointer to structures of type Speech_Decode_FrameState
603 mode = GSM AMR codec mode (enum Mode)
604 serial = pointer to the serial bit stream buffer (unsigned char)
605 frame_type = GSM AMR receive frame type (enum RXFrameType)
606 synth = pointer to the output synthesis speech buffer (Word16)
609 synth contents are truncated to 13 bits if NO13BIT is not defined,
610 otherwise, its contents are left at 16 bits
613 return_value = set to zero (int)
615 Global Variables Used:
618 Local Variables Needed:
621 ------------------------------------------------------------------------------
624 This function is the entry point to the GSM AMR decoder. The following
625 operations are performed on one received frame: First, the codec
626 parameters are parsed from the buffer pointed to by serial according to
627 frame_type. Then the AMR decoder is invoked via a call to Decoder_amr. Post
628 filtering of the decoded data is done via a call to Post_Filter function.
629 Lastly, the decoded data is post-processed via a call to Post_Process
630 function. If NO13BIT is not defined, the contents of the buffer pointed to
631 by synth is truncated to 13 bits. It remains unchanged otherwise.
633 ------------------------------------------------------------------------------
638 ------------------------------------------------------------------------------
641 sp_dec.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
643 ------------------------------------------------------------------------------
646 Note: The original function name of Speech_Decode_Frame_exit was changed to
647 GSMFrameDecode in the Code section.
649 int Speech_Decode_Frame (
650 Speech_Decode_FrameState *st, // io: post filter states
651 enum Mode mode, // i : AMR mode
652 Word16 *serial, // i : serial bit stream
653 enum RXFrameType frame_type, // i : Frame type
654 Word16 *synth // o : synthesis speech (postfiltered
658 Word16 parm[MAX_PRM_SIZE + 1]; // Synthesis parameters
659 Word16 Az_dec[AZ_SIZE]; // Decoded Az for post-filter
662 #if !defined(NO13BIT)
666 setCounter(st->complexityCounter);
667 Reset_WMOPS_counter (); // reset WMOPS counter for the new frame
669 // Serial to parameters
670 if ((frame_type == RX_SID_BAD) ||
671 (frame_type == RX_SID_UPDATE)) {
672 // Override mode to MRDTX
673 Bits2prm (MRDTX, serial, parm);
675 Bits2prm (mode, serial, parm);
679 Decoder_amr(st->decoder_amrState, mode, parm, frame_type,
682 Post_Filter(st->post_state, mode, synth, Az_dec); // Post-filter
684 // post HP filter, and 15->16 bits
685 Post_Process(st->postHP_state, synth, L_FRAME);
687 #if !defined(NO13BIT)
688 // Truncate to 13 bits
689 for (i = 0; i < L_FRAME; i++)
691 synth[i] = synth[i] & 0xfff8;
695 setCounter(0); // set counter to global counter
701 ------------------------------------------------------------------------------
703 [State any special notes, constraints or cautions for users of this function]
705 ------------------------------------------------------------------------------
709 Speech_Decode_FrameState *st, /* io: post filter states */
710 enum Mode mode, /* i : AMR mode */
711 Word16 *serial, /* i : serial bit stream */
712 enum RXFrameType frame_type, /* i : Frame type */
713 Word16 *synth) /* o : synthesis speech (postfiltered */
717 Word16 parm[MAX_PRM_SIZE + 1]; /* Synthesis parameters */
718 Word16 Az_dec[AZ_SIZE]; /* Decoded Az for post-filter */
720 Flag *pOverflow = &(st->decoder_amrState.overflow); /* Overflow flag */
722 #if !defined(NO13BIT)
726 /* Serial to parameters */
727 if ((frame_type == RX_SID_BAD) ||
728 (frame_type == RX_SID_UPDATE))
730 /* Override mode to MRDTX */
731 Bits2prm(MRDTX, serial, parm, &st->decoder_amrState.common_amr_tbls);
735 Bits2prm(mode, serial, parm, &st->decoder_amrState.common_amr_tbls);
740 &(st->decoder_amrState),
755 /* post HP filter, and 15->16 bits */
762 #if !defined(NO13BIT)
763 /* Truncate to 13 bits */
764 for (i = 0; i < L_FRAME; i++)
766 synth[i] = synth[i] & 0xfff8;