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 
34  Pathname: ./audio/gsm-amr/c/src/sp_dec.c
35  Functions: GSMInitDecode
36             Speech_Decode_Frame_reset
37             GSMDecodeFrameExit
38             GSMFrameDecode
39 
40      Date: 08/03/2001
41 
42 ------------------------------------------------------------------------------
43  REVISION HISTORY
44 
45  Description: Add PV coding template. Filled out template sections and
46               reformatted code to follow C coding standard. Removed code that
47               handles SID in GSMFrameDecode.
48 
49  Description: Made the following changes per comments from Phase 2/3 review:
50               1. Updated to more recent PV C coding template.
51               2. Took out all the tabs in the file and replaced with spaces.
52               3. Deleted bit_offset from input list of GSMFrameDecode.
53 
54  Description: Changing several variables passed into these functions of type
55               Speech_Decode_FrameState to type void.
56 
57  Description: Cleaning up brackets and line spacing for statements with
58               brackets as per a review comments.
59 
60  Description: Synchronized file with UMTS version 3.2.0. Removed unnecessary
61               include files.
62 
63  Description: Removed all references to malloc/free, except for the top-level
64  malloc in GSMInitDecode, and corresponding free in GSMDecodeFrameExit.
65 
66  Also, modified function calls throughout to reflect the fact that the members
67  of the structure Decoder_amrState are no longer pointers to be set via
68  malloc, but full-blown structures.  (Changes of the type D_plsfState *lsfState
69  to D_plsfState lsfState)
70 
71  Description: Created overflow and pass the variable into the decoder.
72 
73  Description: Changed inititlaization of the pointer to overflow flag. Removed
74               code related to MOPS counter.
75 
76  Description:  Replaced OSCL mem type functions and eliminated include
77                files that now are chosen by OSCL definitions
78 
79  Description:  Replaced "int" and/or "char" with defined types.
80                Added proper casting (Word32) to some left shifting operations
81 
82  Description:
83 
84 ------------------------------------------------------------------------------
85  MODULE DESCRIPTION
86 
87  This file contains the functions that initialize, invoke, reset, and exit
88  the GSM AMR decoder.
89 
90 ------------------------------------------------------------------------------
91 */
92 
93 /*----------------------------------------------------------------------------
94 ; INCLUDES
95 ----------------------------------------------------------------------------*/
96 #include <stdlib.h>
97 
98 #include "sp_dec.h"
99 #include "typedef.h"
100 #include "cnst.h"
101 #include "dec_amr.h"
102 #include "pstfilt.h"
103 #include "bits2prm.h"
104 #include "mode.h"
105 #include "post_pro.h"
106 
107 
108 /*----------------------------------------------------------------------------
109 ; MACROS
110 ; Define module specific macros here
111 ----------------------------------------------------------------------------*/
112 
113 /*----------------------------------------------------------------------------
114 ; DEFINES
115 ; Include all pre-processor statements here. Include conditional
116 ; compile variables also.
117 ----------------------------------------------------------------------------*/
118 
119 /*----------------------------------------------------------------------------
120 ; LOCAL FUNCTION DEFINITIONS
121 ; Function Prototype declaration
122 ----------------------------------------------------------------------------*/
123 
124 /*----------------------------------------------------------------------------
125 ; LOCAL VARIABLE DEFINITIONS
126 ; Variable declaration - defined here and used outside this module
127 ----------------------------------------------------------------------------*/
128 
129 
130 /*
131 ------------------------------------------------------------------------------
132  FUNCTION NAME: GSMInitDecode
133 ------------------------------------------------------------------------------
134  INPUT AND OUTPUT DEFINITIONS
135 
136  Inputs:
137     state = pointer to an array of pointers to structures of type
138             Speech_Decode_FrameState
139     no_hp_post_MR122 = flag to turn off high-pass post filter for 12.2 kbps
140                        mode (Flag)
141     id = pointer to an array whose contents are of type char
142 
143  Outputs:
144     decoder_amrState field of the structure pointed to by the pointer pointed
145        to by state is set to NULL
146     post_state field of the structure pointed to by the pointer pointed to
147       by state is set to NULL
148     postHP_state field of the structure pointed to by the pointer pointed to
149       by state is set to NULL
150     no_hp_post_MR122 field of the structure pointed to by the pointer pointed
151       to by state is set to the input no_hp_post_MR122
152 
153  Returns:
154     return_value = set to zero, if initialization was successful; -1,
155                    otherwise (int)
156 
157  Global Variables Used:
158     None
159 
160  Local Variables Needed:
161     None
162 
163 ------------------------------------------------------------------------------
164  FUNCTION DESCRIPTION
165 
166  This function allocates memory for filter structure and initializes state
167  memory used by the GSM AMR decoder.
168 
169 ------------------------------------------------------------------------------
170  REQUIREMENTS
171 
172  None
173 
174 ------------------------------------------------------------------------------
175  REFERENCES
176 
177  sp_dec.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
178 
179 ------------------------------------------------------------------------------
180  PSEUDO-CODE
181 
182  Note: Original function name of Speech_Decode_Frame_init was changed to
183        GSMInitDecode in the Code section.
184 
185 int Speech_Decode_Frame_init (Speech_Decode_FrameState **state,
186                               char *id)
187 {
188   Speech_Decode_FrameState* s;
189 
190   if (state == (Speech_Decode_FrameState **) NULL){
191       fprintf(stderr, "Speech_Decode_Frame_init: invalid parameter\n");
192       return -1;
193   }
194   *state = NULL;
195 
196   // allocate memory
197   if ((s= (Speech_Decode_FrameState *)
198           malloc(sizeof(Speech_Decode_FrameState))) == NULL) {
199       fprintf(stderr, "Speech_Decode_Frame_init: can not malloc state "
200               "structure\n");
201       return -1;
202   }
203   s->decoder_amrState = NULL;
204   s->post_state = NULL;
205   s->postHP_state = NULL;
206 
207   if (Decoder_amr_init(&s->decoder_amrState) ||
208       Post_Filter_init(&s->post_state) ||
209       Post_Process_init(&s->postHP_state) ) {
210       Speech_Decode_Frame_exit(&s);
211       return -1;
212   }
213 
214   s->complexityCounter = getCounterId(id);
215 
216   Speech_Decode_Frame_reset(s);
217   *state = s;
218 
219   return 0;
220 }
221 
222 ------------------------------------------------------------------------------
223  RESOURCES USED [optional]
224 
225  When the code is written for a specific target processor the
226  the resources used should be documented below.
227 
228  HEAP MEMORY USED: x bytes
229 
230  STACK MEMORY USED: x bytes
231 
232  CLOCK CYCLES: (cycle count equation for this function) + (variable
233                 used to represent cycle count for each subroutine
234                 called)
235      where: (cycle count variable) = cycle count for [subroutine
236                                      name]
237 
238 ------------------------------------------------------------------------------
239  CAUTION [optional]
240  [State any special notes, constraints or cautions for users of this function]
241 
242 ------------------------------------------------------------------------------
243 */
244 
GSMInitDecode(void ** state_data,Word8 * id)245 Word16 GSMInitDecode(void **state_data,
246                      Word8 * id)
247 {
248     Speech_Decode_FrameState* s;
249     OSCL_UNUSED_ARG(id);
250 
251     if (state_data == NULL)
252     {
253         /*  fprintf(stderr, "Speech_Decode_Frame_init:
254                              invalid parameter\n");  */
255         return (-1);
256     }
257     *state_data = NULL;
258 
259     /* allocate memory */
260     if ((s = (Speech_Decode_FrameState *)
261              malloc(sizeof(Speech_Decode_FrameState))) == NULL)
262     {
263         /*  fprintf(stderr, "Speech_Decode_Frame_init: can not malloc state "
264             "structure\n");  */
265         return (-1);
266     }
267 
268     if (Decoder_amr_init(&s->decoder_amrState)
269             || Post_Process_reset(&s->postHP_state))
270     {
271         free(s);
272         return (-1);
273     }
274 
275 
276     Speech_Decode_Frame_reset(s);
277     *state_data = (void *)s;
278 
279     return (0);
280 }
281 
282 
283 /****************************************************************************/
284 
285 
286 /*
287 ------------------------------------------------------------------------------
288  FUNCTION NAME: Speech_Decode_Frame_reset
289 ------------------------------------------------------------------------------
290  INPUT AND OUTPUT DEFINITIONS
291 
292  Inputs:
293     state = pointer to structures of type Speech_Decode_FrameState
294 
295  Outputs:
296     None
297 
298  Returns:
299     return_value = set to zero if reset was successful; -1, otherwise (int)
300 
301  Global Variables Used:
302     None
303 
304  Local Variables Needed:
305     None
306 
307 ------------------------------------------------------------------------------
308  FUNCTION DESCRIPTION
309 
310  This function resets the state memory used by the GSM AMR decoder.
311 
312 ------------------------------------------------------------------------------
313  REQUIREMENTS
314 
315  None
316 
317 ------------------------------------------------------------------------------
318  REFERENCES
319 
320  sp_dec.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
321 
322 ------------------------------------------------------------------------------
323  PSEUDO-CODE
324 
325 int Speech_Decode_Frame_reset (Speech_Decode_FrameState *state)
326 {
327   if (state == (Speech_Decode_FrameState *) NULL){
328       fprintf(stderr, "Speech_Decode_Frame_reset: invalid parameter\n");
329       return -1;
330   }
331 
332   Decoder_amr_reset(state->decoder_amrState, (enum Mode)0);
333   Post_Filter_reset(state->post_state);
334   Post_Process_reset(state->postHP_state);
335 
336   state->prev_mode = (enum Mode)0;
337 
338   setCounter(state->complexityCounter);
339   Init_WMOPS_counter();
340   setCounter(0); // set counter to global counter
341 
342   return 0;
343 }
344 
345 ------------------------------------------------------------------------------
346  RESOURCES USED [optional]
347 
348  When the code is written for a specific target processor the
349  the resources used should be documented below.
350 
351  HEAP MEMORY USED: x bytes
352 
353  STACK MEMORY USED: x bytes
354 
355  CLOCK CYCLES: (cycle count equation for this function) + (variable
356                 used to represent cycle count for each subroutine
357                 called)
358      where: (cycle count variable) = cycle count for [subroutine
359                                      name]
360 
361 ------------------------------------------------------------------------------
362  CAUTION [optional]
363  [State any special notes, constraints or cautions for users of this function]
364 
365 ------------------------------------------------------------------------------
366 */
Speech_Decode_Frame_reset(void * state_data)367 Word16 Speech_Decode_Frame_reset(void *state_data)
368 {
369 
370     Speech_Decode_FrameState *state =
371         (Speech_Decode_FrameState *) state_data;
372 
373     if (state_data ==  NULL)
374     {
375         /*  fprintf(stderr, "Speech_Decode_Frame_reset:
376                              invalid parameter\n");  */
377         return (-1);
378     }
379 
380     Decoder_amr_reset(&(state->decoder_amrState), MR475);
381     Post_Filter_reset(&(state->post_state));
382     Post_Process_reset(&(state->postHP_state));
383 
384     state->prev_mode = MR475;
385 
386     return (0);
387 }
388 
389 /****************************************************************************/
390 
391 /*
392 ------------------------------------------------------------------------------
393  FUNCTION NAME: GSMDecodeFrameExit
394 ------------------------------------------------------------------------------
395  INPUT AND OUTPUT DEFINITIONS
396 
397  Inputs:
398     state = pointer to an array of pointers to structures of type
399             Speech_Decode_FrameState
400 
401  Outputs:
402     state contents is set to NULL
403 
404  Returns:
405     None
406 
407  Global Variables Used:
408     None
409 
410  Local Variables Needed:
411     None
412 
413 ------------------------------------------------------------------------------
414  FUNCTION DESCRIPTION
415 
416  This function frees up the memory used for the state memory of the GSM AMR
417  decoder.
418 
419 ------------------------------------------------------------------------------
420  REQUIREMENTS
421 
422  None
423 
424 ------------------------------------------------------------------------------
425  REFERENCES
426 
427  sp_dec.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
428 
429 ------------------------------------------------------------------------------
430  PSEUDO-CODE
431 
432  Note: The original function name of Speech_Decode_Frame_exit was changed to
433        GSMDecodeFrameExit in the Code section.
434 
435 void Speech_Decode_Frame_exit (Speech_Decode_FrameState **state)
436 {
437   if (state == NULL || *state == NULL)
438       return;
439 
440   Decoder_amr_exit(&(*state)->decoder_amrState);
441   Post_Filter_exit(&(*state)->post_state);
442   Post_Process_exit(&(*state)->postHP_state);
443 
444   setCounter((*state)->complexityCounter);
445   WMOPS_output(0);
446   setCounter(0); // set counter to global counter
447 
448   // deallocate memory
449   free(*state);
450   *state = NULL;
451 
452   return;
453 }
454 
455 ------------------------------------------------------------------------------
456  RESOURCES USED [optional]
457 
458  When the code is written for a specific target processor the
459  the resources used should be documented below.
460 
461  HEAP MEMORY USED: x bytes
462 
463  STACK MEMORY USED: x bytes
464 
465  CLOCK CYCLES: (cycle count equation for this function) + (variable
466                 used to represent cycle count for each subroutine
467                 called)
468      where: (cycle count variable) = cycle count for [subroutine
469                                      name]
470 
471 ------------------------------------------------------------------------------
472  CAUTION [optional]
473  [State any special notes, constraints or cautions for users of this function]
474 
475 ------------------------------------------------------------------------------
476 */
477 
GSMDecodeFrameExit(void ** state_data)478 void GSMDecodeFrameExit(void **state_data)
479 {
480 
481     Speech_Decode_FrameState **state =
482         (Speech_Decode_FrameState **) state_data;
483 
484     if (state == NULL || *state == NULL)
485     {
486         return;
487     }
488 
489     /* deallocate memory */
490     free(*state);
491     *state = NULL;
492 
493     return;
494 }
495 
496 /****************************************************************************/
497 
498 /*
499 ------------------------------------------------------------------------------
500  FUNCTION NAME: GSMFrameDecode
501 ------------------------------------------------------------------------------
502  INPUT AND OUTPUT DEFINITIONS
503 
504  Inputs:
505     st = pointer to structures of type Speech_Decode_FrameState
506     mode = GSM AMR codec mode (enum Mode)
507     serial = pointer to the serial bit stream buffer (unsigned char)
508     frame_type = GSM AMR receive frame type (enum RXFrameType)
509     synth = pointer to the output synthesis speech buffer (Word16)
510 
511  Outputs:
512     synth contents are truncated to 13 bits if NO13BIT is not defined,
513       otherwise, its contents are left at 16 bits
514 
515  Returns:
516     return_value = set to zero (int)
517 
518  Global Variables Used:
519     None
520 
521  Local Variables Needed:
522     None
523 
524 ------------------------------------------------------------------------------
525  FUNCTION DESCRIPTION
526 
527  This function is the entry point to the GSM AMR decoder. The following
528  operations are performed on one received frame: First, the codec
529  parameters are parsed from the buffer pointed to by serial according to
530  frame_type. Then the AMR decoder is invoked via a call to Decoder_amr. Post
531  filtering of the decoded data is done via a call to Post_Filter function.
532  Lastly, the decoded data is post-processed via a call to Post_Process
533  function. If NO13BIT is not defined, the contents of the buffer pointed to
534  by synth is truncated to 13 bits. It remains unchanged otherwise.
535 
536 ------------------------------------------------------------------------------
537  REQUIREMENTS
538 
539  None
540 
541 ------------------------------------------------------------------------------
542  REFERENCES
543 
544  sp_dec.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
545 
546 ------------------------------------------------------------------------------
547  PSEUDO-CODE
548 
549  Note: The original function name of Speech_Decode_Frame_exit was changed to
550        GSMFrameDecode in the Code section.
551 
552 int Speech_Decode_Frame (
553     Speech_Decode_FrameState *st, // io: post filter states
554     enum Mode mode,               // i : AMR mode
555     Word16 *serial,               // i : serial bit stream
556     enum RXFrameType frame_type,  // i : Frame type
557     Word16 *synth                 // o : synthesis speech (postfiltered
558                                   //     output)
559 )
560 {
561   Word16 parm[MAX_PRM_SIZE + 1];  // Synthesis parameters
562   Word16 Az_dec[AZ_SIZE];         // Decoded Az for post-filter
563                                   // in 4 subframes
564 
565 #if !defined(NO13BIT)
566   Word16 i;
567 #endif
568 
569   setCounter(st->complexityCounter);
570   Reset_WMOPS_counter ();          // reset WMOPS counter for the new frame
571 
572   // Serial to parameters
573   if ((frame_type == RX_SID_BAD) ||
574       (frame_type == RX_SID_UPDATE)) {
575     // Override mode to MRDTX
576     Bits2prm (MRDTX, serial, parm);
577   } else {
578     Bits2prm (mode, serial, parm);
579   }
580 
581   // Synthesis
582   Decoder_amr(st->decoder_amrState, mode, parm, frame_type,
583               synth, Az_dec);
584 
585   Post_Filter(st->post_state, mode, synth, Az_dec);   // Post-filter
586 
587   // post HP filter, and 15->16 bits
588   Post_Process(st->postHP_state, synth, L_FRAME);
589 
590 #if !defined(NO13BIT)
591   // Truncate to 13 bits
592   for (i = 0; i < L_FRAME; i++)
593   {
594      synth[i] = synth[i] & 0xfff8;
595   }
596 #endif
597 
598   setCounter(0); // set counter to global counter
599 
600   return 0;
601 }
602 
603 
604 ------------------------------------------------------------------------------
605  RESOURCES USED [optional]
606 
607  When the code is written for a specific target processor the
608  the resources used should be documented below.
609 
610  HEAP MEMORY USED: x bytes
611 
612  STACK MEMORY USED: x bytes
613 
614  CLOCK CYCLES: (cycle count equation for this function) + (variable
615                 used to represent cycle count for each subroutine
616                 called)
617      where: (cycle count variable) = cycle count for [subroutine
618                                      name]
619 
620 ------------------------------------------------------------------------------
621  CAUTION [optional]
622  [State any special notes, constraints or cautions for users of this function]
623 
624 ------------------------------------------------------------------------------
625 */
626 
GSMFrameDecode(Speech_Decode_FrameState * st,enum Mode mode,Word16 * serial,enum RXFrameType frame_type,Word16 * synth)627 void GSMFrameDecode(
628     Speech_Decode_FrameState *st, /* io: post filter states                */
629     enum Mode mode,               /* i : AMR mode                          */
630     Word16 *serial,               /* i : serial bit stream                 */
631     enum RXFrameType frame_type,  /* i : Frame type                        */
632     Word16 *synth)                /* o : synthesis speech (postfiltered    */
633 /*     output)                           */
634 
635 {
636     Word16 parm[MAX_PRM_SIZE + 1];  /* Synthesis parameters                */
637     Word16 Az_dec[AZ_SIZE];         /* Decoded Az for post-filter          */
638     /* in 4 subframes                      */
639     Flag *pOverflow = &(st->decoder_amrState.overflow);  /* Overflow flag  */
640 
641 #if !defined(NO13BIT)
642     Word16 i;
643 #endif
644 
645     /* Serial to parameters   */
646     if ((frame_type == RX_SID_BAD) ||
647             (frame_type == RX_SID_UPDATE))
648     {
649         /* Override mode to MRDTX */
650         Bits2prm(MRDTX, serial, parm);
651     }
652     else
653     {
654         Bits2prm(mode, serial, parm);
655     }
656 
657     /* Synthesis */
658     Decoder_amr(
659         &(st->decoder_amrState),
660         mode,
661         parm,
662         frame_type,
663         synth,
664         Az_dec);
665 
666     /* Post-filter */
667     Post_Filter(
668         &(st->post_state),
669         mode,
670         synth,
671         Az_dec,
672         pOverflow);
673 
674     /* post HP filter, and 15->16 bits */
675     Post_Process(
676         &(st->postHP_state),
677         synth,
678         L_FRAME,
679         pOverflow);
680 
681 #if !defined(NO13BIT)
682     /* Truncate to 13 bits */
683     for (i = 0; i < L_FRAME; i++)
684     {
685         synth[i] = synth[i] & 0xfff8;
686     }
687 #endif
688 
689     return;
690 }
691 
692 
693 
694