1 /* ------------------------------------------------------------------
2 * Copyright (C) 1998-2009 PacketVideo
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
14 * See the License for the specific language governing permissions
15 * and limitations under the License.
16 * -------------------------------------------------------------------
18 /****************************************************************************************
19 Portions of this file are derived from the following 3GPP standard:
22 ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec
23 Available from http://www.3gpp.org
25 (C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC)
26 Permission to distribute, modify and use this file under the standard license
27 terms listed above has been obtained from the copyright holder.
28 ****************************************************************************************/
30 ------------------------------------------------------------------------------
35 Funtions: GSMInitEncode
36 Speech_Encode_Frame_reset
38 Speech_Encode_Frame_First
41 ------------------------------------------------------------------------------
44 These functions comprise the pre filtering and encoding of one speech frame.
46 ------------------------------------------------------------------------------
50 /*----------------------------------------------------------------------------
52 ----------------------------------------------------------------------------*/
63 /*----------------------------------------------------------------------------
65 ; Define module specific macros here
66 ----------------------------------------------------------------------------*/
69 /*----------------------------------------------------------------------------
71 ; Include all pre-processor statements here. Include conditional
72 ; compile variables also.
73 ----------------------------------------------------------------------------*/
75 /*----------------------------------------------------------------------------
76 ; LOCAL FUNCTION DEFINITIONS
77 ; Function Prototype declaration
78 ----------------------------------------------------------------------------*/
80 /*----------------------------------------------------------------------------
81 ; LOCAL VARIABLE DEFINITIONS
82 ; Variable declaration - defined here and used outside this module
83 ----------------------------------------------------------------------------*/
86 ------------------------------------------------------------------------------
87 FUNCTION NAME: GSMInitEncode
88 ------------------------------------------------------------------------------
89 INPUT AND OUTPUT DEFINITIONS
91 state = pointer to an array of pointers to structures of type
92 Speech_Decode_FrameState
93 dtx = flag to turn off or turn on DTX (Flag)
94 id = pointer to an array whose contents are of type char
97 pre_state field of the structure pointed to by the pointer pointed to
98 by state is set to NULL
99 cod_amr_state field of the structure pointed to by the pointer pointed to
100 by state is set to NULL
101 dtx field of the structure pointed to by the pointer pointed to by state
102 is set to the input dtx
105 return_value = set to zero, if initialization was successful; -1,
108 Global Variables Used:
111 Local Variables Needed:
114 ------------------------------------------------------------------------------
117 This function allocates memory for filter structure and initializes state
120 ------------------------------------------------------------------------------
125 ------------------------------------------------------------------------------
128 sp_enc.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
130 ------------------------------------------------------------------------------
132 Note: Original function name of Speech_Encode_Frame_init was changed to
133 GSMInitEncode in the Code section.
135 int Speech_Encode_Frame_init (void **state_data,
139 Speech_Encode_FrameState* s;
141 if (state_data == NULL){
142 fprintf(stderr, "Speech_Encode_Frame_init: invalid parameter\n");
148 if ((s= (Speech_Encode_FrameState *) malloc(sizeof(Speech_Encode_FrameState))) == NULL){
149 fprintf(stderr, "Speech_Encode_Frame_init: can not malloc state "
154 s->complexityCounter = getCounterId(id);
157 s->cod_amr_state = NULL;
160 if (Pre_Process_init(&s->pre_state) ||
161 cod_amr_init(&s->cod_amr_state, s->dtx)) {
162 GSMEncodeFrameExit(&s);
166 Speech_Encode_Frame_reset(s);
167 *state_data = (void *)s;
173 ------------------------------------------------------------------------------
175 [State any special notes, constraints or cautions for users of this function]
177 ------------------------------------------------------------------------------
180 Word16 GSMInitEncode(void **state_data,
184 Speech_Encode_FrameState* s;
188 if (state_data == NULL)
190 /* fprintf(stderr, "Speech_Encode_Frame_init: invalid parameter\n"); */
195 /* allocate memory */
196 if ((s = (Speech_Encode_FrameState *) oscl_malloc(sizeof(Speech_Encode_FrameState))) == NULL)
198 /* fprintf(stderr, "Speech_Encode_Frame_init: can not malloc state "
204 s->cod_amr_state = NULL;
207 if (Pre_Process_init(&s->pre_state) ||
208 cod_amr_init(&s->cod_amr_state, s->dtx))
210 Speech_Encode_FrameState** temp = &s;
211 GSMEncodeFrameExit((void**)temp);
215 Speech_Encode_Frame_reset(s);
216 *state_data = (void *)s;
223 ------------------------------------------------------------------------------
224 FUNCTION NAME: Speech_Encode_Frame_reset
225 ------------------------------------------------------------------------------
226 INPUT AND OUTPUT DEFINITIONS
229 state = pointer to structures of type Speech_Decode_FrameState
235 return_value = set to zero if reset was successful; -1, otherwise (int)
237 Global Variables Used:
240 Local Variables Needed:
243 ------------------------------------------------------------------------------
246 This function resets state memory
248 ------------------------------------------------------------------------------
253 ------------------------------------------------------------------------------
256 sp_enc.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
258 ------------------------------------------------------------------------------
261 int Speech_Encode_Frame_reset (void *state_data)
264 Speech_Encode_FrameState *state =
265 (Speech_Encode_FrameState *) state_data;
267 if (state_data == NULL){
268 fprintf(stderr, "Speech_Encode_Frame_reset
269 : invalid parameter\n");
273 Pre_Process_reset(state->pre_state);
274 cod_amr_reset(state->cod_amr_state);
276 setCounter(state->complexityCounter);
277 Init_WMOPS_counter();
278 setCounter(0); // set counter to global counter
283 ------------------------------------------------------------------------------
285 [State any special notes, constraints or cautions for users of this function]
287 ------------------------------------------------------------------------------
290 Word16 Speech_Encode_Frame_reset(void *state_data)
293 Speech_Encode_FrameState *state =
294 (Speech_Encode_FrameState *) state_data;
296 if (state_data == NULL)
298 /* fprintf(stderr, "Speech_Encode_Frame_reset
299 : invalid parameter\n"); */
303 Pre_Process_reset(state->pre_state);
304 cod_amr_reset(state->cod_amr_state);
309 /****************************************************************************/
312 ------------------------------------------------------------------------------
313 FUNCTION NAME: GSMEncodeFrameExit
314 ------------------------------------------------------------------------------
315 INPUT AND OUTPUT DEFINITIONS
318 state = pointer to a pointer to a structure of type cod_amrState
321 state points to a NULL address
326 Global Variables Used:
329 Local Variables Needed:
332 ------------------------------------------------------------------------------
335 This function frees the memory used for state memory.
337 ------------------------------------------------------------------------------
342 ------------------------------------------------------------------------------
345 sp_enc.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
347 ------------------------------------------------------------------------------
350 Note: Original function name of Speech_Encode_Frame_exit was changed to
351 GSMEncodeFrameExit in the Code section.
353 void Speech_Encode_Frame_exit (void **state_data)
356 Speech_Encode_FrameState **state =
357 (Speech_Encode_FrameState **) state_data;
359 if (state == NULL || *state == NULL)
362 Pre_Process_exit(&(*state)->pre_state);
363 cod_amr_exit(&(*state)->cod_amr_state);
365 setCounter((*state)->complexityCounter);
367 setCounter(0); // set counter to global counter
376 ------------------------------------------------------------------------------
378 [State any special notes, constraints or cautions for users of this function]
380 ------------------------------------------------------------------------------
383 void GSMEncodeFrameExit(void **state_data)
386 Speech_Encode_FrameState **state =
387 (Speech_Encode_FrameState **) state_data;
389 if (state == NULL || *state == NULL)
392 Pre_Process_exit(&(*state)->pre_state);
393 cod_amr_exit(&(*state)->cod_amr_state);
395 /* deallocate memory */
402 /****************************************************************************/
405 ------------------------------------------------------------------------------
406 FUNCTION NAME: Speech_Encode_Frame_First
407 ------------------------------------------------------------------------------
409 INPUT AND OUTPUT DEFINITIONS
412 st = pointer to a structure of type Speech_Encode_FrameState that contains
413 the post filter states
414 new_speech = pointer to buffer of length L_FRAME that contains
415 the speech input (Word16)
418 The structure of type Speech_Encode_FrameState pointed to by st is updated.
421 return_value = 0 (int)
423 Global Variables Used:
426 Local Variables Needed:
429 ------------------------------------------------------------------------------
432 This function encodes the first frame of speech. It calls the pre-processing
433 filter and the first frame encoder.
435 ------------------------------------------------------------------------------
440 ------------------------------------------------------------------------------
443 sp_enc.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
445 ------------------------------------------------------------------------------
448 int Speech_Encode_Frame_First (
449 Speech_Encode_FrameState *st, // i/o : post filter states
450 Word16 *new_speech) // i : speech input
452 #if !defined(NO13BIT)
456 setCounter(st->complexityCounter);
458 #if !defined(NO13BIT)
459 // Delete the 3 LSBs (13-bit input)
460 for (i = 0; i < L_NEXT; i++)
462 new_speech[i] = new_speech[i] & 0xfff8;
466 // filter + downscaling
467 Pre_Process (st->pre_state, new_speech, L_NEXT);
469 cod_amr_first(st->cod_amr_state, new_speech);
471 Init_WMOPS_counter (); // reset WMOPS counter for the new frame
477 ------------------------------------------------------------------------------
479 [State any special notes, constraints or cautions for users of this function]
481 ------------------------------------------------------------------------------
484 void Speech_Encode_Frame_First(
485 Speech_Encode_FrameState *st, /* i/o : post filter states */
486 Word16 *new_speech) /* i : speech input */
488 #if !defined(NO13BIT)
492 #if !defined(NO13BIT)
493 /* Delete the 3 LSBs (13-bit input) */
494 for (i = 0; i < L_NEXT; i++)
496 new_speech[i] = new_speech[i] & 0xfff8;
500 /* filter + downscaling */
501 Pre_Process(st->pre_state, new_speech, L_NEXT);
503 cod_amr_first(st->cod_amr_state, new_speech);
509 ------------------------------------------------------------------------------
510 FUNCTION NAME: cod_amr
511 ------------------------------------------------------------------------------
512 INPUT AND OUTPUT DEFINITIONS
515 state_data = a void pointer to the post filter states
516 mode = AMR mode of type enum Mode
517 new_speech = pointer to buffer of length L_FRAME that contains
518 the speech input of type Word16
519 serial = pointer to the serial bit stream of type Word16
520 usedMode = pointer to the used mode of type enum Mode
523 serial -> encoded serial bit stream
524 The value pointed to by usedMode is updated.
527 return_value = 0 (int)
529 Global Variables Used:
532 Local Variables Needed:
535 ------------------------------------------------------------------------------
538 This function is the entry point to the GSM AMR encoder. The following
539 operations are performed to generate one encoded frame: First, the incoming
540 audio samples are passed through the pre-processing filter where they are
541 filtered and downscaled. A call is then made to the main encoder cod_amr().
542 This generates the set of encoded parameters which include the LSP, adaptive
543 codebook, and fixed codebook quantization indices (addresses and gains). The
544 generated parameters are then converted to serial bits.
546 ------------------------------------------------------------------------------
551 ------------------------------------------------------------------------------
554 sp_enc.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
556 ------------------------------------------------------------------------------
558 Note: Original function name of Speech_Encode_Frame was changed to
559 GSMEncodeFrame in the Code section.
561 int Speech_Encode_Frame (
562 void *state_data, // i/o : post filter states
563 enum Mode mode, // i : speech coder mode
564 Word16 *new_speech, // i : speech input
565 Word16 *serial, // o : serial bit stream
566 enum Mode *usedMode // o : used speech coder mode
570 Speech_Encode_FrameState *st =
571 (Speech_Encode_FrameState *) state_data;
573 Word16 prm[MAX_PRM_SIZE]; // Analysis parameters
574 Word16 syn[L_FRAME]; // Buffer for synthesis speech
577 setCounter(st->complexityCounter);
578 Reset_WMOPS_counter (); // reset WMOPS counter for the new frame
579 // initialize the serial output frame to zero
580 for (i = 0; i < MAX_SERIAL_SIZE; i++)
584 #if !defined(NO13BIT)
585 // Delete the 3 LSBs (13-bit input)
586 for (i = 0; i < L_FRAME; i++)
588 new_speech[i] = new_speech[i] & 0xfff8;
594 // filter + downscaling
595 Pre_Process (st->pre_state, new_speech, L_FRAME);
597 // Call the speech encoder
598 cod_amr(st->cod_amr_state, mode, new_speech, prm, usedMode, syn);
600 // Parameters to serial bits
601 Prm2bits (*usedMode, prm, &serial[0]);
604 setCounter(0); // set counter to global counter
609 ------------------------------------------------------------------------------
611 [State any special notes, constraints or cautions for users of this function]
613 ------------------------------------------------------------------------------
617 void *state_data, /* i/o : post filter states */
618 enum Mode mode, /* i : speech coder mode */
619 Word16 *new_speech, /* i : speech input */
620 Word16 *serial, /* o : serial bit stream */
621 enum Mode *usedMode /* o : used speech coder mode */
625 Speech_Encode_FrameState *st =
626 (Speech_Encode_FrameState *) state_data;
628 Word16 prm[MAX_PRM_SIZE]; /* Analysis parameters. */
629 Word16 syn[L_FRAME]; /* Buffer for synthesis speech */
632 /* initialize the serial output frame to zero */
633 for (i = 0; i < MAX_SERIAL_SIZE; i++)
637 #if !defined(NO13BIT)
638 /* Delete the 3 LSBs (13-bit input) */
639 for (i = 0; i < L_FRAME; i++)
641 new_speech[i] = new_speech[i] & 0xfff8;
645 /* filter + downscaling */
646 Pre_Process(st->pre_state, new_speech, L_FRAME);
648 /* Call the speech encoder */
649 cod_amr(st->cod_amr_state, mode, new_speech, prm, usedMode, syn);
651 /* Parameters to serial bits */
652 Prm2bits(*usedMode, prm, &serial[0], &(st->cod_amr_state->common_amr_tbls));