Git init
[external/opencore-amr.git] / opencore / codecs_v2 / audio / gsm_amr / amr_nb / dec / src / sp_dec.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
34  Filename: sp_dec.cpp
35  Functions: GSMInitDecode
36             Speech_Decode_Frame_reset
37             GSMDecodeFrameExit
38             GSMFrameDecode
39
40 ------------------------------------------------------------------------------
41  MODULE DESCRIPTION
42
43  This file contains the functions that initialize, invoke, reset, and exit
44  the GSM AMR decoder.
45
46 ------------------------------------------------------------------------------
47 */
48
49 /*----------------------------------------------------------------------------
50 ; INCLUDES
51 ----------------------------------------------------------------------------*/
52 #include "sp_dec.h"
53 #include "typedef.h"
54 #include "cnst.h"
55 #include "dec_amr.h"
56 #include "pstfilt.h"
57 #include "mode.h"
58 #include "post_pro.h"
59 #include "oscl_mem.h"
60 #include "bitno_tab.h"
61
62
63 /*----------------------------------------------------------------------------
64 ; MACROS
65 ; Define module specific macros here
66 ----------------------------------------------------------------------------*/
67
68 /*----------------------------------------------------------------------------
69 ; DEFINES
70 ; Include all pre-processor statements here. Include conditional
71 ; compile variables also.
72 ----------------------------------------------------------------------------*/
73
74 /*----------------------------------------------------------------------------
75 ; LOCAL FUNCTION DEFINITIONS
76 ; Function Prototype declaration
77 ----------------------------------------------------------------------------*/
78
79 /*----------------------------------------------------------------------------
80 ; LOCAL VARIABLE DEFINITIONS
81 ; Variable declaration - defined here and used outside this module
82 ----------------------------------------------------------------------------*/
83
84 /*
85 ------------------------------------------------------------------------------
86  FUNCTION NAME: Bin2int
87 ------------------------------------------------------------------------------
88  INPUT AND OUTPUT DEFINITIONS
89
90  Inputs:
91     no_of_bits = number of bits associated with value
92     bitstream = pointer to buffer where bits are read
93
94  Outputs:
95     None
96
97  Returns:
98     None
99
100  Global Variables Used:
101     None
102
103  Local Variables Needed:
104     None
105
106 ------------------------------------------------------------------------------
107  FUNCTION DESCRIPTION
108
109   Function    : Bin2int
110   Purpose     : Read "no_of_bits" bits from the array bitstream[]
111                 and convert to integer.
112
113 ------------------------------------------------------------------------------
114  REQUIREMENTS
115
116  None
117
118 ------------------------------------------------------------------------------
119  REFERENCES
120
121  bits2prm.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
122
123 ------------------------------------------------------------------------------
124  PSEUDO-CODE
125
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
129 )
130 {
131     Word16 value, i, bit;
132
133     value = 0;
134     for (i = 0; i < no_of_bits; i++)
135     {
136         value = shl (value, 1);
137         bit = *bitstream++;
138         if (sub (bit, BIT_1) == 0)
139             value = add (value, 1);
140     }
141     return (value);
142 }
143
144 ------------------------------------------------------------------------------
145  CAUTION [optional]
146  [State any special notes, constraints or cautions for users of this function]
147
148 ------------------------------------------------------------------------------
149 */
150
151 /*----------------------------------------------------------------------------
152 ; FUNCTION CODE
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      */
157 )
158 {
159     Word16 value;
160     Word16 i;
161     Word16 single_bit;
162
163     value = 0;
164     for (i = 0; i < no_of_bits; i++)
165     {
166         value <<= 1;
167         single_bit = *(bitstream++);
168         value |= single_bit;
169     }
170     return (value);
171 }
172
173
174 /*
175 ------------------------------------------------------------------------------
176  FUNCTION NAME: bits2prm
177 ------------------------------------------------------------------------------
178  INPUT AND OUTPUT DEFINITIONS
179
180  Inputs:
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
184
185  Outputs:
186     None
187
188  Returns:
189     None
190
191  Global Variables Used:
192     None
193
194  Local Variables Needed:
195     None
196
197 ------------------------------------------------------------------------------
198  FUNCTION DESCRIPTION
199
200   Function    : Bits2prm
201   Purpose     : Retrieves the vector of encoder parameters from
202                 the received serial bits in a frame.
203
204 ------------------------------------------------------------------------------
205  REQUIREMENTS
206
207  None
208
209 ------------------------------------------------------------------------------
210  REFERENCES
211
212  bits2prm.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
213
214 ------------------------------------------------------------------------------
215  PSEUDO-CODE
216
217 void Bits2prm (
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)
221 )
222 {
223     Word16 i;
224
225     for (i = 0; i < prmno[mode]; i++)
226     {
227         prm[i] = Bin2int (bitno[mode][i], bits);
228         bits += bitno[mode][i];
229         add(0,0);       // account for above pointer update
230     }
231
232    return;
233 }
234
235 ------------------------------------------------------------------------------
236  CAUTION [optional]
237  [State any special notes, constraints or cautions for users of this function]
238
239 ------------------------------------------------------------------------------
240 */
241
242 /*----------------------------------------------------------------------------
243 ; FUNCTION CODE
244 ----------------------------------------------------------------------------*/
245 void Bits2prm(
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        */
250 )
251 {
252     Word16 i;
253     const Word16* prmno_ptr = common_amr_tbls->prmno_ptr;
254     const Word16* const* bitno_ptr = common_amr_tbls->bitno_ptr;
255
256
257     for (i = 0; i < prmno_ptr[mode]; i++)
258     {
259         prm[i] = Bin2int(bitno_ptr[mode][i], bits);
260         bits += bitno_ptr[mode][i];
261     }
262
263     return;
264 }
265
266
267
268
269 /*
270 ------------------------------------------------------------------------------
271  FUNCTION NAME: GSMInitDecode
272 ------------------------------------------------------------------------------
273  INPUT AND OUTPUT DEFINITIONS
274
275  Inputs:
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
279                        mode (Flag)
280     id = pointer to an array whose contents are of type char
281
282  Outputs:
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
291
292  Returns:
293     return_value = set to zero, if initialization was successful; -1,
294                    otherwise (int)
295
296  Global Variables Used:
297     None
298
299  Local Variables Needed:
300     None
301
302 ------------------------------------------------------------------------------
303  FUNCTION DESCRIPTION
304
305  This function allocates memory for filter structure and initializes state
306  memory used by the GSM AMR decoder.
307
308 ------------------------------------------------------------------------------
309  REQUIREMENTS
310
311  None
312
313 ------------------------------------------------------------------------------
314  REFERENCES
315
316  sp_dec.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
317
318 ------------------------------------------------------------------------------
319  PSEUDO-CODE
320
321  Note: Original function name of Speech_Decode_Frame_init was changed to
322        GSMInitDecode in the Code section.
323
324 int Speech_Decode_Frame_init (Speech_Decode_FrameState **state,
325                               char *id)
326 {
327   Speech_Decode_FrameState* s;
328
329   if (state == (Speech_Decode_FrameState **) NULL){
330       fprintf(stderr, "Speech_Decode_Frame_init: invalid parameter\n");
331       return -1;
332   }
333   *state = NULL;
334
335   // allocate memory
336   if ((s= (Speech_Decode_FrameState *)
337           malloc(sizeof(Speech_Decode_FrameState))) == NULL) {
338       fprintf(stderr, "Speech_Decode_Frame_init: can not malloc state "
339               "structure\n");
340       return -1;
341   }
342   s->decoder_amrState = NULL;
343   s->post_state = NULL;
344   s->postHP_state = NULL;
345
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);
350       return -1;
351   }
352
353   s->complexityCounter = getCounterId(id);
354
355   Speech_Decode_Frame_reset(s);
356   *state = s;
357
358   return 0;
359 }
360
361 ------------------------------------------------------------------------------
362  CAUTION [optional]
363  [State any special notes, constraints or cautions for users of this function]
364
365 ------------------------------------------------------------------------------
366 */
367
368 Word16 GSMInitDecode(void **state_data,
369                      Word8 * id)
370 {
371     Speech_Decode_FrameState* s;
372     OSCL_UNUSED_ARG(id);
373
374     if (state_data == NULL)
375     {
376         /*  fprintf(stderr, "Speech_Decode_Frame_init:
377                              invalid parameter\n");  */
378         return (-1);
379     }
380     *state_data = NULL;
381
382     /* allocate memory */
383     if ((s = (Speech_Decode_FrameState *)
384              oscl_malloc(sizeof(Speech_Decode_FrameState))) == NULL)
385     {
386         /*  fprintf(stderr, "Speech_Decode_Frame_init: can not malloc state "
387             "structure\n");  */
388         return (-1);
389     }
390
391     if (Decoder_amr_init(&s->decoder_amrState)
392             || Post_Process_reset(&s->postHP_state))
393     {
394         Speech_Decode_FrameState *tmp = s;
395         /*
396          *  dereferencing type-punned pointer avoid
397          *  breaking strict-aliasing rules
398          */
399         void** tempVoid = (void**) tmp;
400         GSMDecodeFrameExit(tempVoid);
401         return (-1);
402     }
403
404
405     Speech_Decode_Frame_reset(s);
406     *state_data = (void *)s;
407
408     return (0);
409 }
410
411
412 /****************************************************************************/
413
414
415 /*
416 ------------------------------------------------------------------------------
417  FUNCTION NAME: Speech_Decode_Frame_reset
418 ------------------------------------------------------------------------------
419  INPUT AND OUTPUT DEFINITIONS
420
421  Inputs:
422     state = pointer to structures of type Speech_Decode_FrameState
423
424  Outputs:
425     None
426
427  Returns:
428     return_value = set to zero if reset was successful; -1, otherwise (int)
429
430  Global Variables Used:
431     None
432
433  Local Variables Needed:
434     None
435
436 ------------------------------------------------------------------------------
437  FUNCTION DESCRIPTION
438
439  This function resets the state memory used by the GSM AMR decoder.
440
441 ------------------------------------------------------------------------------
442  REQUIREMENTS
443
444  None
445
446 ------------------------------------------------------------------------------
447  REFERENCES
448
449  sp_dec.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
450
451 ------------------------------------------------------------------------------
452  PSEUDO-CODE
453
454 int Speech_Decode_Frame_reset (Speech_Decode_FrameState *state)
455 {
456   if (state == (Speech_Decode_FrameState *) NULL){
457       fprintf(stderr, "Speech_Decode_Frame_reset: invalid parameter\n");
458       return -1;
459   }
460
461   Decoder_amr_reset(state->decoder_amrState, (enum Mode)0);
462   Post_Filter_reset(state->post_state);
463   Post_Process_reset(state->postHP_state);
464
465   state->prev_mode = (enum Mode)0;
466
467   setCounter(state->complexityCounter);
468   Init_WMOPS_counter();
469   setCounter(0); // set counter to global counter
470
471   return 0;
472 }
473
474 ------------------------------------------------------------------------------
475  CAUTION [optional]
476  [State any special notes, constraints or cautions for users of this function]
477
478 ------------------------------------------------------------------------------
479 */
480 Word16 Speech_Decode_Frame_reset(void *state_data)
481 {
482
483     Speech_Decode_FrameState *state =
484         (Speech_Decode_FrameState *) state_data;
485
486     if (state_data ==  NULL)
487     {
488         /*  fprintf(stderr, "Speech_Decode_Frame_reset:
489                              invalid parameter\n");  */
490         return (-1);
491     }
492
493     Decoder_amr_reset(&(state->decoder_amrState), MR475);
494     Post_Filter_reset(&(state->post_state));
495     Post_Process_reset(&(state->postHP_state));
496
497     state->prev_mode = MR475;
498
499     return (0);
500 }
501
502 /****************************************************************************/
503
504 /*
505 ------------------------------------------------------------------------------
506  FUNCTION NAME: GSMDecodeFrameExit
507 ------------------------------------------------------------------------------
508  INPUT AND OUTPUT DEFINITIONS
509
510  Inputs:
511     state = pointer to an array of pointers to structures of type
512             Speech_Decode_FrameState
513
514  Outputs:
515     state contents is set to NULL
516
517  Returns:
518     None
519
520  Global Variables Used:
521     None
522
523  Local Variables Needed:
524     None
525
526 ------------------------------------------------------------------------------
527  FUNCTION DESCRIPTION
528
529  This function frees up the memory used for the state memory of the GSM AMR
530  decoder.
531
532 ------------------------------------------------------------------------------
533  REQUIREMENTS
534
535  None
536
537 ------------------------------------------------------------------------------
538  REFERENCES
539
540  sp_dec.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
541
542 ------------------------------------------------------------------------------
543  PSEUDO-CODE
544
545  Note: The original function name of Speech_Decode_Frame_exit was changed to
546        GSMDecodeFrameExit in the Code section.
547
548 void Speech_Decode_Frame_exit (Speech_Decode_FrameState **state)
549 {
550   if (state == NULL || *state == NULL)
551       return;
552
553   Decoder_amr_exit(&(*state)->decoder_amrState);
554   Post_Filter_exit(&(*state)->post_state);
555   Post_Process_exit(&(*state)->postHP_state);
556
557   setCounter((*state)->complexityCounter);
558   WMOPS_output(0);
559   setCounter(0); // set counter to global counter
560
561   // deallocate memory
562   free(*state);
563   *state = NULL;
564
565   return;
566 }
567
568 ------------------------------------------------------------------------------
569  CAUTION [optional]
570  [State any special notes, constraints or cautions for users of this function]
571
572 ------------------------------------------------------------------------------
573 */
574
575 void GSMDecodeFrameExit(void **state_data)
576 {
577
578     Speech_Decode_FrameState **state =
579         (Speech_Decode_FrameState **) state_data;
580
581     if (state == NULL || *state == NULL)
582     {
583         return;
584     }
585
586     /* deallocate memory */
587     oscl_free(*state);
588     *state = NULL;
589
590     return;
591 }
592
593 /****************************************************************************/
594
595 /*
596 ------------------------------------------------------------------------------
597  FUNCTION NAME: GSMFrameDecode
598 ------------------------------------------------------------------------------
599  INPUT AND OUTPUT DEFINITIONS
600
601  Inputs:
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)
607
608  Outputs:
609     synth contents are truncated to 13 bits if NO13BIT is not defined,
610       otherwise, its contents are left at 16 bits
611
612  Returns:
613     return_value = set to zero (int)
614
615  Global Variables Used:
616     None
617
618  Local Variables Needed:
619     None
620
621 ------------------------------------------------------------------------------
622  FUNCTION DESCRIPTION
623
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.
632
633 ------------------------------------------------------------------------------
634  REQUIREMENTS
635
636  None
637
638 ------------------------------------------------------------------------------
639  REFERENCES
640
641  sp_dec.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
642
643 ------------------------------------------------------------------------------
644  PSEUDO-CODE
645
646  Note: The original function name of Speech_Decode_Frame_exit was changed to
647        GSMFrameDecode in the Code section.
648
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
655                                   //     output)
656 )
657 {
658   Word16 parm[MAX_PRM_SIZE + 1];  // Synthesis parameters
659   Word16 Az_dec[AZ_SIZE];         // Decoded Az for post-filter
660                                   // in 4 subframes
661
662 #if !defined(NO13BIT)
663   Word16 i;
664 #endif
665
666   setCounter(st->complexityCounter);
667   Reset_WMOPS_counter ();          // reset WMOPS counter for the new frame
668
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);
674   } else {
675     Bits2prm (mode, serial, parm);
676   }
677
678   // Synthesis
679   Decoder_amr(st->decoder_amrState, mode, parm, frame_type,
680               synth, Az_dec);
681
682   Post_Filter(st->post_state, mode, synth, Az_dec);   // Post-filter
683
684   // post HP filter, and 15->16 bits
685   Post_Process(st->postHP_state, synth, L_FRAME);
686
687 #if !defined(NO13BIT)
688   // Truncate to 13 bits
689   for (i = 0; i < L_FRAME; i++)
690   {
691      synth[i] = synth[i] & 0xfff8;
692   }
693 #endif
694
695   setCounter(0); // set counter to global counter
696
697   return 0;
698 }
699
700
701 ------------------------------------------------------------------------------
702  CAUTION [optional]
703  [State any special notes, constraints or cautions for users of this function]
704
705 ------------------------------------------------------------------------------
706 */
707
708 void GSMFrameDecode(
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    */
714 /*     output)                           */
715
716 {
717     Word16 parm[MAX_PRM_SIZE + 1];  /* Synthesis parameters                */
718     Word16 Az_dec[AZ_SIZE];         /* Decoded Az for post-filter          */
719     /* in 4 subframes                      */
720     Flag *pOverflow = &(st->decoder_amrState.overflow);  /* Overflow flag  */
721
722 #if !defined(NO13BIT)
723     Word16 i;
724 #endif
725
726     /* Serial to parameters   */
727     if ((frame_type == RX_SID_BAD) ||
728             (frame_type == RX_SID_UPDATE))
729     {
730         /* Override mode to MRDTX */
731         Bits2prm(MRDTX, serial, parm, &st->decoder_amrState.common_amr_tbls);
732     }
733     else
734     {
735         Bits2prm(mode, serial, parm, &st->decoder_amrState.common_amr_tbls);
736     }
737
738     /* Synthesis */
739     Decoder_amr(
740         &(st->decoder_amrState),
741         mode,
742         parm,
743         frame_type,
744         synth,
745         Az_dec);
746
747     /* Post-filter */
748     Post_Filter(
749         &(st->post_state),
750         mode,
751         synth,
752         Az_dec,
753         pOverflow);
754
755     /* post HP filter, and 15->16 bits */
756     Post_Process(
757         &(st->postHP_state),
758         synth,
759         L_FRAME,
760         pOverflow);
761
762 #if !defined(NO13BIT)
763     /* Truncate to 13 bits */
764     for (i = 0; i < L_FRAME; i++)
765     {
766         synth[i] = synth[i] & 0xfff8;
767     }
768 #endif
769
770     return;
771 }
772
773
774