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_enc.c
35 Funtions: GSMInitEncode
36 Speech_Encode_Frame_reset
37 GSMEncodeFrameExit
38 Speech_Encode_Frame_First
39 GSMEncodeFrame
40
41 Date: 02/07/2002
42
43 ------------------------------------------------------------------------------
44 REVISION HISTORY
45
46 Description: Cleaned up INCLUDES. removed inclusion of basic_op.h and count.h.
47
48
49 Description: Revert back to Speech_Encode_Frame_reset() and
50 Speech_Encode_Frame_First
51
52 Description: Replaced OSCL mem type functions and eliminated include
53 files that now are chosen by OSCL definitions
54
55 Description: Replaced "int" and/or "char" with OSCL defined types.
56
57 Description:
58
59 ------------------------------------------------------------------------------
60 MODULE DESCRIPTION
61
62 These functions comprise the pre filtering and encoding of one speech frame.
63
64 ------------------------------------------------------------------------------
65 */
66
67
68 /*----------------------------------------------------------------------------
69 ; INCLUDES
70 ----------------------------------------------------------------------------*/
71 #include <stdlib.h>
72
73 #include "sp_enc.h"
74 #include "typedef.h"
75 #include "cnst.h"
76 #include "set_zero.h"
77 #include "pre_proc.h"
78 #include "prm2bits.h"
79 #include "mode.h"
80 #include "cod_amr.h"
81
82 /*----------------------------------------------------------------------------
83 ; MACROS
84 ; Define module specific macros here
85 ----------------------------------------------------------------------------*/
86
87
88 /*----------------------------------------------------------------------------
89 ; DEFINES
90 ; Include all pre-processor statements here. Include conditional
91 ; compile variables also.
92 ----------------------------------------------------------------------------*/
93
94 /*----------------------------------------------------------------------------
95 ; LOCAL FUNCTION DEFINITIONS
96 ; Function Prototype declaration
97 ----------------------------------------------------------------------------*/
98
99 /*----------------------------------------------------------------------------
100 ; LOCAL VARIABLE DEFINITIONS
101 ; Variable declaration - defined here and used outside this module
102 ----------------------------------------------------------------------------*/
103
104 /*
105 ------------------------------------------------------------------------------
106 FUNCTION NAME: GSMInitEncode
107 ------------------------------------------------------------------------------
108 INPUT AND OUTPUT DEFINITIONS
109 Inputs:
110 state = pointer to an array of pointers to structures of type
111 Speech_Decode_FrameState
112 dtx = flag to turn off or turn on DTX (Flag)
113 id = pointer to an array whose contents are of type char
114
115 Outputs:
116 pre_state field of the structure pointed to by the pointer pointed to
117 by state is set to NULL
118 cod_amr_state field of the structure pointed to by the pointer pointed to
119 by state is set to NULL
120 dtx field of the structure pointed to by the pointer pointed to by state
121 is set to the input dtx
122
123 Returns:
124 return_value = set to zero, if initialization was successful; -1,
125 otherwise (int)
126
127 Global Variables Used:
128 None
129
130 Local Variables Needed:
131 None
132
133 ------------------------------------------------------------------------------
134 FUNCTION DESCRIPTION
135
136 This function allocates memory for filter structure and initializes state
137 memory
138
139 ------------------------------------------------------------------------------
140 REQUIREMENTS
141
142 None.
143
144 ------------------------------------------------------------------------------
145 REFERENCES
146
147 sp_enc.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
148
149 ------------------------------------------------------------------------------
150 PSEUDO-CODE
151 Note: Original function name of Speech_Encode_Frame_init was changed to
152 GSMInitEncode in the Code section.
153
154 int Speech_Encode_Frame_init (void **state_data,
155 Flag dtx,
156 char *id)
157 {
158 Speech_Encode_FrameState* s;
159
160 if (state_data == NULL){
161 fprintf(stderr, "Speech_Encode_Frame_init: invalid parameter\n");
162 return -1;
163 }
164 *state_data = NULL;
165
166 // allocate memory
167 if ((s= (Speech_Encode_FrameState *) malloc(sizeof(Speech_Encode_FrameState))) == NULL){
168 fprintf(stderr, "Speech_Encode_Frame_init: can not malloc state "
169 "structure\n");
170 return -1;
171 }
172
173 s->complexityCounter = getCounterId(id);
174
175 s->pre_state = NULL;
176 s->cod_amr_state = NULL;
177 s->dtx = dtx;
178
179 if (Pre_Process_init(&s->pre_state) ||
180 cod_amr_init(&s->cod_amr_state, s->dtx)) {
181 GSMEncodeFrameExit(&s);
182 return -1;
183 }
184
185 Speech_Encode_Frame_reset(s);
186 *state_data = (void *)s;
187
188 return 0;
189 }
190
191
192 ------------------------------------------------------------------------------
193 RESOURCES USED [optional]
194
195 When the code is written for a specific target processor the
196 the resources used should be documented below.
197
198 HEAP MEMORY USED: x bytes
199
200 STACK MEMORY USED: x bytes
201
202 CLOCK CYCLES: (cycle count equation for this function) + (variable
203 used to represent cycle count for each subroutine
204 called)
205 where: (cycle count variable) = cycle count for [subroutine
206 name]
207
208 ------------------------------------------------------------------------------
209 CAUTION [optional]
210 [State any special notes, constraints or cautions for users of this function]
211
212 ------------------------------------------------------------------------------
213 */
214
GSMInitEncode(void ** state_data,Flag dtx,Word8 * id)215 Word16 GSMInitEncode(void **state_data,
216 Flag dtx,
217 Word8 *id)
218 {
219 Speech_Encode_FrameState* s;
220
221 OSCL_UNUSED_ARG(id);
222
223 if (state_data == NULL)
224 {
225 /* fprintf(stderr, "Speech_Encode_Frame_init: invalid parameter\n"); */
226 return -1;
227 }
228 *state_data = NULL;
229
230 /* allocate memory */
231 if ((s = (Speech_Encode_FrameState *) malloc(sizeof(Speech_Encode_FrameState))) == NULL)
232 {
233 /* fprintf(stderr, "Speech_Encode_Frame_init: can not malloc state "
234 "structure\n"); */
235 return -1;
236 }
237
238 s->pre_state = NULL;
239 s->cod_amr_state = NULL;
240 s->dtx = dtx;
241
242 if (Pre_Process_init(&s->pre_state) ||
243 cod_amr_init(&s->cod_amr_state, s->dtx))
244 {
245 Speech_Encode_FrameState** temp = &s;
246 GSMEncodeFrameExit((void**)temp);
247 return -1;
248 }
249
250 Speech_Encode_Frame_reset(s);
251 *state_data = (void *)s;
252
253 return 0;
254 }
255
256
257 /*
258 ------------------------------------------------------------------------------
259 FUNCTION NAME: Speech_Encode_Frame_reset
260 ------------------------------------------------------------------------------
261 INPUT AND OUTPUT DEFINITIONS
262
263 Inputs:
264 state = pointer to structures of type Speech_Decode_FrameState
265
266 Outputs:
267 None
268
269 Returns:
270 return_value = set to zero if reset was successful; -1, otherwise (int)
271
272 Global Variables Used:
273 None
274
275 Local Variables Needed:
276 None
277
278 ------------------------------------------------------------------------------
279 FUNCTION DESCRIPTION
280
281 This function resets state memory
282
283 ------------------------------------------------------------------------------
284 REQUIREMENTS
285
286 None.
287
288 ------------------------------------------------------------------------------
289 REFERENCES
290
291 sp_enc.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
292
293 ------------------------------------------------------------------------------
294 PSEUDO-CODE
295
296 int Speech_Encode_Frame_reset (void *state_data)
297 {
298
299 Speech_Encode_FrameState *state =
300 (Speech_Encode_FrameState *) state_data;
301
302 if (state_data == NULL){
303 fprintf(stderr, "Speech_Encode_Frame_reset
304 : invalid parameter\n");
305 return -1;
306 }
307
308 Pre_Process_reset(state->pre_state);
309 cod_amr_reset(state->cod_amr_state);
310
311 setCounter(state->complexityCounter);
312 Init_WMOPS_counter();
313 setCounter(0); // set counter to global counter
314
315 return 0;
316 }
317
318 ------------------------------------------------------------------------------
319 RESOURCES USED [optional]
320
321 When the code is written for a specific target processor the
322 the resources used should be documented below.
323
324 HEAP MEMORY USED: x bytes
325
326 STACK MEMORY USED: x bytes
327
328 CLOCK CYCLES: (cycle count equation for this function) + (variable
329 used to represent cycle count for each subroutine
330 called)
331 where: (cycle count variable) = cycle count for [subroutine
332 name]
333
334 ------------------------------------------------------------------------------
335 CAUTION [optional]
336 [State any special notes, constraints or cautions for users of this function]
337
338 ------------------------------------------------------------------------------
339 */
340
Speech_Encode_Frame_reset(void * state_data)341 Word16 Speech_Encode_Frame_reset(void *state_data)
342 {
343
344 Speech_Encode_FrameState *state =
345 (Speech_Encode_FrameState *) state_data;
346
347 if (state_data == NULL)
348 {
349 /* fprintf(stderr, "Speech_Encode_Frame_reset
350 : invalid parameter\n"); */
351 return -1;
352 }
353
354 Pre_Process_reset(state->pre_state);
355 cod_amr_reset(state->cod_amr_state);
356
357 return 0;
358 }
359
360 /****************************************************************************/
361
362 /*
363 ------------------------------------------------------------------------------
364 FUNCTION NAME: GSMEncodeFrameExit
365 ------------------------------------------------------------------------------
366 INPUT AND OUTPUT DEFINITIONS
367
368 Inputs:
369 state = pointer to a pointer to a structure of type cod_amrState
370
371 Outputs:
372 state points to a NULL address
373
374 Returns:
375 None.
376
377 Global Variables Used:
378 None.
379
380 Local Variables Needed:
381 None.
382
383 ------------------------------------------------------------------------------
384 FUNCTION DESCRIPTION
385
386 This function frees the memory used for state memory.
387
388 ------------------------------------------------------------------------------
389 REQUIREMENTS
390
391 None.
392
393 ------------------------------------------------------------------------------
394 REFERENCES
395
396 sp_enc.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
397
398 ------------------------------------------------------------------------------
399 PSEUDO-CODE
400
401 Note: Original function name of Speech_Encode_Frame_exit was changed to
402 GSMEncodeFrameExit in the Code section.
403
404 void Speech_Encode_Frame_exit (void **state_data)
405 {
406
407 Speech_Encode_FrameState **state =
408 (Speech_Encode_FrameState **) state_data;
409
410 if (state == NULL || *state == NULL)
411 return;
412
413 Pre_Process_exit(&(*state)->pre_state);
414 cod_amr_exit(&(*state)->cod_amr_state);
415
416 setCounter((*state)->complexityCounter);
417 WMOPS_output(0);
418 setCounter(0); // set counter to global counter
419
420 // deallocate memory
421 free(*state);
422 *state = NULL;
423
424 return;
425 }
426
427 ------------------------------------------------------------------------------
428 RESOURCES USED [optional]
429
430 When the code is written for a specific target processor the
431 the resources used should be documented below.
432
433 HEAP MEMORY USED: x bytes
434
435 STACK MEMORY USED: x bytes
436
437 CLOCK CYCLES: (cycle count equation for this function) + (variable
438 used to represent cycle count for each subroutine
439 called)
440 where: (cycle count variable) = cycle count for [subroutine
441 name]
442
443 ------------------------------------------------------------------------------
444 CAUTION [optional]
445 [State any special notes, constraints or cautions for users of this function]
446
447 ------------------------------------------------------------------------------
448 */
449
GSMEncodeFrameExit(void ** state_data)450 void GSMEncodeFrameExit(void **state_data)
451 {
452
453 Speech_Encode_FrameState **state =
454 (Speech_Encode_FrameState **) state_data;
455
456 if (state == NULL || *state == NULL)
457 return;
458
459 Pre_Process_exit(&(*state)->pre_state);
460 cod_amr_exit(&(*state)->cod_amr_state);
461
462 /* deallocate memory */
463 free(*state);
464 *state = NULL;
465
466 return;
467 }
468
469 /****************************************************************************/
470
471 /*
472 ------------------------------------------------------------------------------
473 FUNCTION NAME: Speech_Encode_Frame_First
474 ------------------------------------------------------------------------------
475
476 INPUT AND OUTPUT DEFINITIONS
477
478 Inputs:
479 st = pointer to a structure of type Speech_Encode_FrameState that contains
480 the post filter states
481 new_speech = pointer to buffer of length L_FRAME that contains
482 the speech input (Word16)
483
484 Outputs:
485 The structure of type Speech_Encode_FrameState pointed to by st is updated.
486
487 Returns:
488 return_value = 0 (int)
489
490 Global Variables Used:
491 None.
492
493 Local Variables Needed:
494 None.
495
496 ------------------------------------------------------------------------------
497 FUNCTION DESCRIPTION
498
499 This function encodes the first frame of speech. It calls the pre-processing
500 filter and the first frame encoder.
501
502 ------------------------------------------------------------------------------
503 REQUIREMENTS
504
505 None.
506
507 ------------------------------------------------------------------------------
508 REFERENCES
509
510 sp_enc.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
511
512 ------------------------------------------------------------------------------
513 PSEUDO-CODE
514
515 int Speech_Encode_Frame_First (
516 Speech_Encode_FrameState *st, // i/o : post filter states
517 Word16 *new_speech) // i : speech input
518 {
519 #if !defined(NO13BIT)
520 Word16 i;
521 #endif
522
523 setCounter(st->complexityCounter);
524
525 #if !defined(NO13BIT)
526 // Delete the 3 LSBs (13-bit input)
527 for (i = 0; i < L_NEXT; i++)
528 {
529 new_speech[i] = new_speech[i] & 0xfff8;
530 }
531 #endif
532
533 // filter + downscaling
534 Pre_Process (st->pre_state, new_speech, L_NEXT);
535
536 cod_amr_first(st->cod_amr_state, new_speech);
537
538 Init_WMOPS_counter (); // reset WMOPS counter for the new frame
539
540 return 0;
541 }
542
543
544 ------------------------------------------------------------------------------
545 RESOURCES USED [optional]
546
547 When the code is written for a specific target processor the
548 the resources used should be documented below.
549
550 HEAP MEMORY USED: x bytes
551
552 STACK MEMORY USED: x bytes
553
554 CLOCK CYCLES: (cycle count equation for this function) + (variable
555 used to represent cycle count for each subroutine
556 called)
557 where: (cycle count variable) = cycle count for [subroutine
558 name]
559
560 ------------------------------------------------------------------------------
561 CAUTION [optional]
562 [State any special notes, constraints or cautions for users of this function]
563
564 ------------------------------------------------------------------------------
565 */
566
Speech_Encode_Frame_First(Speech_Encode_FrameState * st,Word16 * new_speech)567 void Speech_Encode_Frame_First(
568 Speech_Encode_FrameState *st, /* i/o : post filter states */
569 Word16 *new_speech) /* i : speech input */
570 {
571 #if !defined(NO13BIT)
572 Word16 i;
573 #endif
574
575 #if !defined(NO13BIT)
576 /* Delete the 3 LSBs (13-bit input) */
577 for (i = 0; i < L_NEXT; i++)
578 {
579 new_speech[i] = new_speech[i] & 0xfff8;
580 }
581 #endif
582
583 /* filter + downscaling */
584 Pre_Process(st->pre_state, new_speech, L_NEXT);
585
586 cod_amr_first(st->cod_amr_state, new_speech);
587
588 return;
589 }
590
591 /*
592 ------------------------------------------------------------------------------
593 FUNCTION NAME: cod_amr
594 ------------------------------------------------------------------------------
595 INPUT AND OUTPUT DEFINITIONS
596
597 Inputs:
598 state_data = a void pointer to the post filter states
599 mode = AMR mode of type enum Mode
600 new_speech = pointer to buffer of length L_FRAME that contains
601 the speech input of type Word16
602 serial = pointer to the serial bit stream of type Word16
603 usedMode = pointer to the used mode of type enum Mode
604
605 Outputs:
606 serial -> encoded serial bit stream
607 The value pointed to by usedMode is updated.
608
609 Returns:
610 return_value = 0 (int)
611
612 Global Variables Used:
613 None.
614
615 Local Variables Needed:
616 None.
617
618 ------------------------------------------------------------------------------
619 FUNCTION DESCRIPTION
620
621 This function is the entry point to the GSM AMR encoder. The following
622 operations are performed to generate one encoded frame: First, the incoming
623 audio samples are passed through the pre-processing filter where they are
624 filtered and downscaled. A call is then made to the main encoder cod_amr().
625 This generates the set of encoded parameters which include the LSP, adaptive
626 codebook, and fixed codebook quantization indices (addresses and gains). The
627 generated parameters are then converted to serial bits.
628
629 ------------------------------------------------------------------------------
630 REQUIREMENTS
631
632 None.
633
634 ------------------------------------------------------------------------------
635 REFERENCES
636
637 sp_enc.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
638
639 ------------------------------------------------------------------------------
640 PSEUDO-CODE
641 Note: Original function name of Speech_Encode_Frame was changed to
642 GSMEncodeFrame in the Code section.
643
644 int Speech_Encode_Frame (
645 void *state_data, // i/o : post filter states
646 enum Mode mode, // i : speech coder mode
647 Word16 *new_speech, // i : speech input
648 Word16 *serial, // o : serial bit stream
649 enum Mode *usedMode // o : used speech coder mode
650 )
651 {
652
653 Speech_Encode_FrameState *st =
654 (Speech_Encode_FrameState *) state_data;
655
656 Word16 prm[MAX_PRM_SIZE]; // Analysis parameters
657 Word16 syn[L_FRAME]; // Buffer for synthesis speech
658 Word16 i;
659
660 setCounter(st->complexityCounter);
661 Reset_WMOPS_counter (); // reset WMOPS counter for the new frame
662 // initialize the serial output frame to zero
663 for (i = 0; i < MAX_SERIAL_SIZE; i++)
664 {
665 serial[i] = 0;
666 }
667 #if !defined(NO13BIT)
668 // Delete the 3 LSBs (13-bit input)
669 for (i = 0; i < L_FRAME; i++)
670 {
671 new_speech[i] = new_speech[i] & 0xfff8;
672
673
674 }
675 #endif
676
677 // filter + downscaling
678 Pre_Process (st->pre_state, new_speech, L_FRAME);
679
680 // Call the speech encoder
681 cod_amr(st->cod_amr_state, mode, new_speech, prm, usedMode, syn);
682
683 // Parameters to serial bits
684 Prm2bits (*usedMode, prm, &serial[0]);
685
686 fwc();
687 setCounter(0); // set counter to global counter
688
689 return 0;
690 }
691
692 ------------------------------------------------------------------------------
693 RESOURCES USED [optional]
694
695 When the code is written for a specific target processor the
696 the resources used should be documented below.
697
698 HEAP MEMORY USED: x bytes
699
700 STACK MEMORY USED: x bytes
701
702 CLOCK CYCLES: (cycle count equation for this function) + (variable
703 used to represent cycle count for each subroutine
704 called)
705 where: (cycle count variable) = cycle count for [subroutine
706 name]
707
708 ------------------------------------------------------------------------------
709 CAUTION [optional]
710 [State any special notes, constraints or cautions for users of this function]
711
712 ------------------------------------------------------------------------------
713 */
714
GSMEncodeFrame(void * state_data,enum Mode mode,Word16 * new_speech,Word16 * serial,enum Mode * usedMode)715 void GSMEncodeFrame(
716 void *state_data, /* i/o : post filter states */
717 enum Mode mode, /* i : speech coder mode */
718 Word16 *new_speech, /* i : speech input */
719 Word16 *serial, /* o : serial bit stream */
720 enum Mode *usedMode /* o : used speech coder mode */
721 )
722 {
723
724 Speech_Encode_FrameState *st =
725 (Speech_Encode_FrameState *) state_data;
726
727 Word16 prm[MAX_PRM_SIZE]; /* Analysis parameters. */
728 Word16 syn[L_FRAME]; /* Buffer for synthesis speech */
729 Word16 i;
730
731 /* initialize the serial output frame to zero */
732 for (i = 0; i < MAX_SERIAL_SIZE; i++)
733 {
734 serial[i] = 0;
735 }
736 #if !defined(NO13BIT)
737 /* Delete the 3 LSBs (13-bit input) */
738 for (i = 0; i < L_FRAME; i++)
739 {
740 new_speech[i] = new_speech[i] & 0xfff8;
741 }
742 #endif
743
744 /* filter + downscaling */
745 Pre_Process(st->pre_state, new_speech, L_FRAME);
746
747 /* Call the speech encoder */
748 cod_amr(st->cod_amr_state, mode, new_speech, prm, usedMode, syn);
749
750 /* Parameters to serial bits */
751 Prm2bits(*usedMode, prm, &serial[0]);
752
753 return;
754 }
755