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 Speech_Decode_FrameState *tmp = s;
272 /*
273 * dereferencing type-punned pointer avoid
274 * breaking strict-aliasing rules
275 */
276 void** tempVoid = (void**) tmp;
277 GSMDecodeFrameExit(tempVoid);
278 return (-1);
279 }
280
281
282 Speech_Decode_Frame_reset(s);
283 *state_data = (void *)s;
284
285 return (0);
286 }
287
288
289 /****************************************************************************/
290
291
292 /*
293 ------------------------------------------------------------------------------
294 FUNCTION NAME: Speech_Decode_Frame_reset
295 ------------------------------------------------------------------------------
296 INPUT AND OUTPUT DEFINITIONS
297
298 Inputs:
299 state = pointer to structures of type Speech_Decode_FrameState
300
301 Outputs:
302 None
303
304 Returns:
305 return_value = set to zero if reset was successful; -1, otherwise (int)
306
307 Global Variables Used:
308 None
309
310 Local Variables Needed:
311 None
312
313 ------------------------------------------------------------------------------
314 FUNCTION DESCRIPTION
315
316 This function resets the state memory used by the GSM AMR decoder.
317
318 ------------------------------------------------------------------------------
319 REQUIREMENTS
320
321 None
322
323 ------------------------------------------------------------------------------
324 REFERENCES
325
326 sp_dec.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
327
328 ------------------------------------------------------------------------------
329 PSEUDO-CODE
330
331 int Speech_Decode_Frame_reset (Speech_Decode_FrameState *state)
332 {
333 if (state == (Speech_Decode_FrameState *) NULL){
334 fprintf(stderr, "Speech_Decode_Frame_reset: invalid parameter\n");
335 return -1;
336 }
337
338 Decoder_amr_reset(state->decoder_amrState, (enum Mode)0);
339 Post_Filter_reset(state->post_state);
340 Post_Process_reset(state->postHP_state);
341
342 state->prev_mode = (enum Mode)0;
343
344 setCounter(state->complexityCounter);
345 Init_WMOPS_counter();
346 setCounter(0); // set counter to global counter
347
348 return 0;
349 }
350
351 ------------------------------------------------------------------------------
352 RESOURCES USED [optional]
353
354 When the code is written for a specific target processor the
355 the resources used should be documented below.
356
357 HEAP MEMORY USED: x bytes
358
359 STACK MEMORY USED: x bytes
360
361 CLOCK CYCLES: (cycle count equation for this function) + (variable
362 used to represent cycle count for each subroutine
363 called)
364 where: (cycle count variable) = cycle count for [subroutine
365 name]
366
367 ------------------------------------------------------------------------------
368 CAUTION [optional]
369 [State any special notes, constraints or cautions for users of this function]
370
371 ------------------------------------------------------------------------------
372 */
Speech_Decode_Frame_reset(void * state_data)373 Word16 Speech_Decode_Frame_reset(void *state_data)
374 {
375
376 Speech_Decode_FrameState *state =
377 (Speech_Decode_FrameState *) state_data;
378
379 if (state_data == NULL)
380 {
381 /* fprintf(stderr, "Speech_Decode_Frame_reset:
382 invalid parameter\n"); */
383 return (-1);
384 }
385
386 Decoder_amr_reset(&(state->decoder_amrState), MR475);
387 Post_Filter_reset(&(state->post_state));
388 Post_Process_reset(&(state->postHP_state));
389
390 state->prev_mode = MR475;
391
392 return (0);
393 }
394
395 /****************************************************************************/
396
397 /*
398 ------------------------------------------------------------------------------
399 FUNCTION NAME: GSMDecodeFrameExit
400 ------------------------------------------------------------------------------
401 INPUT AND OUTPUT DEFINITIONS
402
403 Inputs:
404 state = pointer to an array of pointers to structures of type
405 Speech_Decode_FrameState
406
407 Outputs:
408 state contents is set to NULL
409
410 Returns:
411 None
412
413 Global Variables Used:
414 None
415
416 Local Variables Needed:
417 None
418
419 ------------------------------------------------------------------------------
420 FUNCTION DESCRIPTION
421
422 This function frees up the memory used for the state memory of the GSM AMR
423 decoder.
424
425 ------------------------------------------------------------------------------
426 REQUIREMENTS
427
428 None
429
430 ------------------------------------------------------------------------------
431 REFERENCES
432
433 sp_dec.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
434
435 ------------------------------------------------------------------------------
436 PSEUDO-CODE
437
438 Note: The original function name of Speech_Decode_Frame_exit was changed to
439 GSMDecodeFrameExit in the Code section.
440
441 void Speech_Decode_Frame_exit (Speech_Decode_FrameState **state)
442 {
443 if (state == NULL || *state == NULL)
444 return;
445
446 Decoder_amr_exit(&(*state)->decoder_amrState);
447 Post_Filter_exit(&(*state)->post_state);
448 Post_Process_exit(&(*state)->postHP_state);
449
450 setCounter((*state)->complexityCounter);
451 WMOPS_output(0);
452 setCounter(0); // set counter to global counter
453
454 // deallocate memory
455 free(*state);
456 *state = NULL;
457
458 return;
459 }
460
461 ------------------------------------------------------------------------------
462 RESOURCES USED [optional]
463
464 When the code is written for a specific target processor the
465 the resources used should be documented below.
466
467 HEAP MEMORY USED: x bytes
468
469 STACK MEMORY USED: x bytes
470
471 CLOCK CYCLES: (cycle count equation for this function) + (variable
472 used to represent cycle count for each subroutine
473 called)
474 where: (cycle count variable) = cycle count for [subroutine
475 name]
476
477 ------------------------------------------------------------------------------
478 CAUTION [optional]
479 [State any special notes, constraints or cautions for users of this function]
480
481 ------------------------------------------------------------------------------
482 */
483
GSMDecodeFrameExit(void ** state_data)484 void GSMDecodeFrameExit(void **state_data)
485 {
486
487 Speech_Decode_FrameState **state =
488 (Speech_Decode_FrameState **) state_data;
489
490 if (state == NULL || *state == NULL)
491 {
492 return;
493 }
494
495 /* deallocate memory */
496 free(*state);
497 *state = NULL;
498
499 return;
500 }
501
502 /****************************************************************************/
503
504 /*
505 ------------------------------------------------------------------------------
506 FUNCTION NAME: GSMFrameDecode
507 ------------------------------------------------------------------------------
508 INPUT AND OUTPUT DEFINITIONS
509
510 Inputs:
511 st = pointer to structures of type Speech_Decode_FrameState
512 mode = GSM AMR codec mode (enum Mode)
513 serial = pointer to the serial bit stream buffer (unsigned char)
514 frame_type = GSM AMR receive frame type (enum RXFrameType)
515 synth = pointer to the output synthesis speech buffer (Word16)
516
517 Outputs:
518 synth contents are truncated to 13 bits if NO13BIT is not defined,
519 otherwise, its contents are left at 16 bits
520
521 Returns:
522 return_value = set to zero (int)
523
524 Global Variables Used:
525 None
526
527 Local Variables Needed:
528 None
529
530 ------------------------------------------------------------------------------
531 FUNCTION DESCRIPTION
532
533 This function is the entry point to the GSM AMR decoder. The following
534 operations are performed on one received frame: First, the codec
535 parameters are parsed from the buffer pointed to by serial according to
536 frame_type. Then the AMR decoder is invoked via a call to Decoder_amr. Post
537 filtering of the decoded data is done via a call to Post_Filter function.
538 Lastly, the decoded data is post-processed via a call to Post_Process
539 function. If NO13BIT is not defined, the contents of the buffer pointed to
540 by synth is truncated to 13 bits. It remains unchanged otherwise.
541
542 ------------------------------------------------------------------------------
543 REQUIREMENTS
544
545 None
546
547 ------------------------------------------------------------------------------
548 REFERENCES
549
550 sp_dec.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
551
552 ------------------------------------------------------------------------------
553 PSEUDO-CODE
554
555 Note: The original function name of Speech_Decode_Frame_exit was changed to
556 GSMFrameDecode in the Code section.
557
558 int Speech_Decode_Frame (
559 Speech_Decode_FrameState *st, // io: post filter states
560 enum Mode mode, // i : AMR mode
561 Word16 *serial, // i : serial bit stream
562 enum RXFrameType frame_type, // i : Frame type
563 Word16 *synth // o : synthesis speech (postfiltered
564 // output)
565 )
566 {
567 Word16 parm[MAX_PRM_SIZE + 1]; // Synthesis parameters
568 Word16 Az_dec[AZ_SIZE]; // Decoded Az for post-filter
569 // in 4 subframes
570
571 #if !defined(NO13BIT)
572 Word16 i;
573 #endif
574
575 setCounter(st->complexityCounter);
576 Reset_WMOPS_counter (); // reset WMOPS counter for the new frame
577
578 // Serial to parameters
579 if ((frame_type == RX_SID_BAD) ||
580 (frame_type == RX_SID_UPDATE)) {
581 // Override mode to MRDTX
582 Bits2prm (MRDTX, serial, parm);
583 } else {
584 Bits2prm (mode, serial, parm);
585 }
586
587 // Synthesis
588 Decoder_amr(st->decoder_amrState, mode, parm, frame_type,
589 synth, Az_dec);
590
591 Post_Filter(st->post_state, mode, synth, Az_dec); // Post-filter
592
593 // post HP filter, and 15->16 bits
594 Post_Process(st->postHP_state, synth, L_FRAME);
595
596 #if !defined(NO13BIT)
597 // Truncate to 13 bits
598 for (i = 0; i < L_FRAME; i++)
599 {
600 synth[i] = synth[i] & 0xfff8;
601 }
602 #endif
603
604 setCounter(0); // set counter to global counter
605
606 return 0;
607 }
608
609
610 ------------------------------------------------------------------------------
611 RESOURCES USED [optional]
612
613 When the code is written for a specific target processor the
614 the resources used should be documented below.
615
616 HEAP MEMORY USED: x bytes
617
618 STACK MEMORY USED: x bytes
619
620 CLOCK CYCLES: (cycle count equation for this function) + (variable
621 used to represent cycle count for each subroutine
622 called)
623 where: (cycle count variable) = cycle count for [subroutine
624 name]
625
626 ------------------------------------------------------------------------------
627 CAUTION [optional]
628 [State any special notes, constraints or cautions for users of this function]
629
630 ------------------------------------------------------------------------------
631 */
632
GSMFrameDecode(Speech_Decode_FrameState * st,enum Mode mode,Word16 * serial,enum RXFrameType frame_type,Word16 * synth)633 void GSMFrameDecode(
634 Speech_Decode_FrameState *st, /* io: post filter states */
635 enum Mode mode, /* i : AMR mode */
636 Word16 *serial, /* i : serial bit stream */
637 enum RXFrameType frame_type, /* i : Frame type */
638 Word16 *synth) /* o : synthesis speech (postfiltered */
639 /* output) */
640
641 {
642 Word16 parm[MAX_PRM_SIZE + 1]; /* Synthesis parameters */
643 Word16 Az_dec[AZ_SIZE]; /* Decoded Az for post-filter */
644 /* in 4 subframes */
645 Flag *pOverflow = &(st->decoder_amrState.overflow); /* Overflow flag */
646
647 #if !defined(NO13BIT)
648 Word16 i;
649 #endif
650
651 /* Serial to parameters */
652 if ((frame_type == RX_SID_BAD) ||
653 (frame_type == RX_SID_UPDATE))
654 {
655 /* Override mode to MRDTX */
656 Bits2prm(MRDTX, serial, parm);
657 }
658 else
659 {
660 Bits2prm(mode, serial, parm);
661 }
662
663 /* Synthesis */
664 Decoder_amr(
665 &(st->decoder_amrState),
666 mode,
667 parm,
668 frame_type,
669 synth,
670 Az_dec);
671
672 /* Post-filter */
673 Post_Filter(
674 &(st->post_state),
675 mode,
676 synth,
677 Az_dec,
678 pOverflow);
679
680 /* post HP filter, and 15->16 bits */
681 Post_Process(
682 &(st->postHP_state),
683 synth,
684 L_FRAME,
685 pOverflow);
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 return;
696 }
697
698
699
700