Initialize Tizen 2.3
[external/opencore-amr.git] / opencore / codecs_v2 / audio / gsm_amr / amr_nb / enc / src / sp_enc.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_enc.cpp
35  Funtions: GSMInitEncode
36            Speech_Encode_Frame_reset
37            GSMEncodeFrameExit
38            Speech_Encode_Frame_First
39            GSMEncodeFrame
40
41 ------------------------------------------------------------------------------
42  MODULE DESCRIPTION
43
44  These functions comprise the pre filtering and encoding of one speech frame.
45
46 ------------------------------------------------------------------------------
47 */
48
49
50 /*----------------------------------------------------------------------------
51 ; INCLUDES
52 ----------------------------------------------------------------------------*/
53 #include "sp_enc.h"
54 #include "typedef.h"
55 #include "cnst.h"
56 #include "set_zero.h"
57 #include "pre_proc.h"
58 #include "prm2bits.h"
59 #include "mode.h"
60 #include "cod_amr.h"
61 #include "oscl_mem.h"
62
63 /*----------------------------------------------------------------------------
64 ; MACROS
65 ; Define module specific macros here
66 ----------------------------------------------------------------------------*/
67
68
69 /*----------------------------------------------------------------------------
70 ; DEFINES
71 ; Include all pre-processor statements here. Include conditional
72 ; compile variables also.
73 ----------------------------------------------------------------------------*/
74
75 /*----------------------------------------------------------------------------
76 ; LOCAL FUNCTION DEFINITIONS
77 ; Function Prototype declaration
78 ----------------------------------------------------------------------------*/
79
80 /*----------------------------------------------------------------------------
81 ; LOCAL VARIABLE DEFINITIONS
82 ; Variable declaration - defined here and used outside this module
83 ----------------------------------------------------------------------------*/
84
85 /*
86 ------------------------------------------------------------------------------
87  FUNCTION NAME: GSMInitEncode
88 ------------------------------------------------------------------------------
89  INPUT AND OUTPUT DEFINITIONS
90  Inputs:
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
95
96  Outputs:
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
103
104  Returns:
105     return_value = set to zero, if initialization was successful; -1,
106                    otherwise (int)
107
108  Global Variables Used:
109     None
110
111  Local Variables Needed:
112     None
113
114 ------------------------------------------------------------------------------
115  FUNCTION DESCRIPTION
116
117  This function allocates memory for filter structure and initializes state
118  memory
119
120 ------------------------------------------------------------------------------
121  REQUIREMENTS
122
123  None.
124
125 ------------------------------------------------------------------------------
126  REFERENCES
127
128  sp_enc.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
129
130 ------------------------------------------------------------------------------
131  PSEUDO-CODE
132  Note: Original function name of Speech_Encode_Frame_init was changed to
133        GSMInitEncode in the Code section.
134
135 int Speech_Encode_Frame_init (void **state_data,
136                    Flag   dtx,
137                    char  *id)
138 {
139   Speech_Encode_FrameState* s;
140
141   if (state_data == NULL){
142       fprintf(stderr, "Speech_Encode_Frame_init: invalid parameter\n");
143       return -1;
144   }
145   *state_data = NULL;
146
147   // allocate memory
148   if ((s= (Speech_Encode_FrameState *) malloc(sizeof(Speech_Encode_FrameState))) == NULL){
149       fprintf(stderr, "Speech_Encode_Frame_init: can not malloc state "
150                       "structure\n");
151       return -1;
152   }
153
154   s->complexityCounter = getCounterId(id);
155
156   s->pre_state = NULL;
157   s->cod_amr_state = NULL;
158   s->dtx = dtx;
159
160   if (Pre_Process_init(&s->pre_state) ||
161       cod_amr_init(&s->cod_amr_state, s->dtx)) {
162       GSMEncodeFrameExit(&s);
163       return -1;
164   }
165
166   Speech_Encode_Frame_reset(s);
167   *state_data = (void *)s;
168
169   return 0;
170 }
171
172
173 ------------------------------------------------------------------------------
174  CAUTION [optional]
175  [State any special notes, constraints or cautions for users of this function]
176
177 ------------------------------------------------------------------------------
178 */
179
180 Word16 GSMInitEncode(void **state_data,
181                      Flag   dtx,
182                      Word8  *id)
183 {
184     Speech_Encode_FrameState* s;
185
186     OSCL_UNUSED_ARG(id);
187
188     if (state_data == NULL)
189     {
190         /*  fprintf(stderr, "Speech_Encode_Frame_init: invalid parameter\n");  */
191         return -1;
192     }
193     *state_data = NULL;
194
195     /* allocate memory */
196     if ((s = (Speech_Encode_FrameState *) oscl_malloc(sizeof(Speech_Encode_FrameState))) == NULL)
197     {
198         /*  fprintf(stderr, "Speech_Encode_Frame_init: can not malloc state "
199                         "structure\n");  */
200         return -1;
201     }
202
203     s->pre_state = NULL;
204     s->cod_amr_state = NULL;
205     s->dtx = dtx;
206
207     if (Pre_Process_init(&s->pre_state) ||
208             cod_amr_init(&s->cod_amr_state, s->dtx))
209     {
210         Speech_Encode_FrameState** temp = &s;
211         GSMEncodeFrameExit((void**)temp);
212         return -1;
213     }
214
215     Speech_Encode_Frame_reset(s);
216     *state_data = (void *)s;
217
218     return 0;
219 }
220
221
222 /*
223 ------------------------------------------------------------------------------
224  FUNCTION NAME: Speech_Encode_Frame_reset
225 ------------------------------------------------------------------------------
226  INPUT AND OUTPUT DEFINITIONS
227
228  Inputs:
229     state = pointer to structures of type Speech_Decode_FrameState
230
231  Outputs:
232     None
233
234  Returns:
235     return_value = set to zero if reset was successful; -1, otherwise (int)
236
237  Global Variables Used:
238     None
239
240  Local Variables Needed:
241     None
242
243 ------------------------------------------------------------------------------
244  FUNCTION DESCRIPTION
245
246  This function resets state memory
247
248 ------------------------------------------------------------------------------
249  REQUIREMENTS
250
251  None.
252
253 ------------------------------------------------------------------------------
254  REFERENCES
255
256  sp_enc.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
257
258 ------------------------------------------------------------------------------
259  PSEUDO-CODE
260
261 int Speech_Encode_Frame_reset (void *state_data)
262 {
263
264   Speech_Encode_FrameState *state =
265      (Speech_Encode_FrameState *) state_data;
266
267   if (state_data == NULL){
268         fprintf(stderr, "Speech_Encode_Frame_reset
269                            : invalid parameter\n");
270       return -1;
271   }
272
273   Pre_Process_reset(state->pre_state);
274   cod_amr_reset(state->cod_amr_state);
275
276   setCounter(state->complexityCounter);
277   Init_WMOPS_counter();
278   setCounter(0); // set counter to global counter
279
280   return 0;
281 }
282
283 ------------------------------------------------------------------------------
284  CAUTION [optional]
285  [State any special notes, constraints or cautions for users of this function]
286
287 ------------------------------------------------------------------------------
288 */
289
290 Word16 Speech_Encode_Frame_reset(void *state_data)
291 {
292
293     Speech_Encode_FrameState *state =
294         (Speech_Encode_FrameState *) state_data;
295
296     if (state_data == NULL)
297     {
298         /*  fprintf(stderr, "Speech_Encode_Frame_reset
299                              : invalid parameter\n");  */
300         return -1;
301     }
302
303     Pre_Process_reset(state->pre_state);
304     cod_amr_reset(state->cod_amr_state);
305
306     return 0;
307 }
308
309 /****************************************************************************/
310
311 /*
312 ------------------------------------------------------------------------------
313  FUNCTION NAME: GSMEncodeFrameExit
314 ------------------------------------------------------------------------------
315  INPUT AND OUTPUT DEFINITIONS
316
317  Inputs:
318     state = pointer to a pointer to a structure of type cod_amrState
319
320  Outputs:
321     state points to a NULL address
322
323  Returns:
324     None.
325
326  Global Variables Used:
327     None.
328
329  Local Variables Needed:
330     None.
331
332 ------------------------------------------------------------------------------
333  FUNCTION DESCRIPTION
334
335  This function frees the memory used for state memory.
336
337 ------------------------------------------------------------------------------
338  REQUIREMENTS
339
340  None.
341
342 ------------------------------------------------------------------------------
343  REFERENCES
344
345  sp_enc.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
346
347 ------------------------------------------------------------------------------
348  PSEUDO-CODE
349
350  Note: Original function name of Speech_Encode_Frame_exit was changed to
351        GSMEncodeFrameExit in the Code section.
352
353 void Speech_Encode_Frame_exit (void **state_data)
354 {
355
356     Speech_Encode_FrameState **state =
357         (Speech_Encode_FrameState **) state_data;
358
359   if (state == NULL || *state == NULL)
360       return;
361
362   Pre_Process_exit(&(*state)->pre_state);
363   cod_amr_exit(&(*state)->cod_amr_state);
364
365   setCounter((*state)->complexityCounter);
366   WMOPS_output(0);
367   setCounter(0); // set counter to global counter
368
369   // deallocate memory
370   free(*state);
371   *state = NULL;
372
373   return;
374 }
375
376 ------------------------------------------------------------------------------
377  CAUTION [optional]
378  [State any special notes, constraints or cautions for users of this function]
379
380 ------------------------------------------------------------------------------
381 */
382
383 void GSMEncodeFrameExit(void **state_data)
384 {
385
386     Speech_Encode_FrameState **state =
387         (Speech_Encode_FrameState **) state_data;
388
389     if (state == NULL || *state == NULL)
390         return;
391
392     Pre_Process_exit(&(*state)->pre_state);
393     cod_amr_exit(&(*state)->cod_amr_state);
394
395     /* deallocate memory */
396     oscl_free(*state);
397     *state = NULL;
398
399     return;
400 }
401
402 /****************************************************************************/
403
404 /*
405 ------------------------------------------------------------------------------
406  FUNCTION NAME: Speech_Encode_Frame_First
407 ------------------------------------------------------------------------------
408
409  INPUT AND OUTPUT DEFINITIONS
410
411  Inputs:
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)
416
417  Outputs:
418     The structure of type Speech_Encode_FrameState pointed to by st is updated.
419
420  Returns:
421     return_value = 0 (int)
422
423  Global Variables Used:
424     None.
425
426  Local Variables Needed:
427     None.
428
429 ------------------------------------------------------------------------------
430  FUNCTION DESCRIPTION
431
432  This function encodes the first frame of speech. It calls the pre-processing
433  filter and the first frame encoder.
434
435 ------------------------------------------------------------------------------
436  REQUIREMENTS
437
438  None.
439
440 ------------------------------------------------------------------------------
441  REFERENCES
442
443  sp_enc.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
444
445 ------------------------------------------------------------------------------
446  PSEUDO-CODE
447
448 int Speech_Encode_Frame_First (
449     Speech_Encode_FrameState *st,  // i/o : post filter states
450     Word16 *new_speech)            // i   : speech input
451 {
452 #if !defined(NO13BIT)
453    Word16 i;
454 #endif
455
456    setCounter(st->complexityCounter);
457
458 #if !defined(NO13BIT)
459   // Delete the 3 LSBs (13-bit input)
460   for (i = 0; i < L_NEXT; i++)
461   {
462      new_speech[i] = new_speech[i] & 0xfff8;
463   }
464 #endif
465
466   // filter + downscaling
467   Pre_Process (st->pre_state, new_speech, L_NEXT);
468
469   cod_amr_first(st->cod_amr_state, new_speech);
470
471   Init_WMOPS_counter (); // reset WMOPS counter for the new frame
472
473   return 0;
474 }
475
476
477 ------------------------------------------------------------------------------
478  CAUTION [optional]
479  [State any special notes, constraints or cautions for users of this function]
480
481 ------------------------------------------------------------------------------
482 */
483
484 void Speech_Encode_Frame_First(
485     Speech_Encode_FrameState *st,  /* i/o : post filter states       */
486     Word16 *new_speech)            /* i   : speech input             */
487 {
488 #if !defined(NO13BIT)
489     Word16 i;
490 #endif
491
492 #if !defined(NO13BIT)
493     /* Delete the 3 LSBs (13-bit input) */
494     for (i = 0; i < L_NEXT; i++)
495     {
496         new_speech[i] = new_speech[i] & 0xfff8;
497     }
498 #endif
499
500     /* filter + downscaling */
501     Pre_Process(st->pre_state, new_speech, L_NEXT);
502
503     cod_amr_first(st->cod_amr_state, new_speech);
504
505     return;
506 }
507
508 /*
509 ------------------------------------------------------------------------------
510  FUNCTION NAME: cod_amr
511 ------------------------------------------------------------------------------
512  INPUT AND OUTPUT DEFINITIONS
513
514  Inputs:
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
521
522  Outputs:
523     serial -> encoded serial bit stream
524     The value pointed to by usedMode is updated.
525
526  Returns:
527     return_value = 0 (int)
528
529  Global Variables Used:
530     None.
531
532  Local Variables Needed:
533     None.
534
535 ------------------------------------------------------------------------------
536  FUNCTION DESCRIPTION
537
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.
545
546 ------------------------------------------------------------------------------
547  REQUIREMENTS
548
549  None.
550
551 ------------------------------------------------------------------------------
552  REFERENCES
553
554  sp_enc.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
555
556 ------------------------------------------------------------------------------
557  PSEUDO-CODE
558  Note: Original function name of Speech_Encode_Frame was changed to
559        GSMEncodeFrame in the Code section.
560
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
567     )
568 {
569
570   Speech_Encode_FrameState *st =
571      (Speech_Encode_FrameState *) state_data;
572
573   Word16 prm[MAX_PRM_SIZE];   // Analysis parameters
574   Word16 syn[L_FRAME];        // Buffer for synthesis speech
575   Word16 i;
576
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++)
581   {
582     serial[i] = 0;
583   }
584 #if !defined(NO13BIT)
585   // Delete the 3 LSBs (13-bit input)
586   for (i = 0; i < L_FRAME; i++)
587   {
588      new_speech[i] = new_speech[i] & 0xfff8;
589
590
591   }
592 #endif
593
594   // filter + downscaling
595   Pre_Process (st->pre_state, new_speech, L_FRAME);
596
597   // Call the speech encoder
598   cod_amr(st->cod_amr_state, mode, new_speech, prm, usedMode, syn);
599
600   // Parameters to serial bits
601   Prm2bits (*usedMode, prm, &serial[0]);
602
603   fwc();
604   setCounter(0); // set counter to global counter
605
606   return 0;
607 }
608
609 ------------------------------------------------------------------------------
610  CAUTION [optional]
611  [State any special notes, constraints or cautions for users of this function]
612
613 ------------------------------------------------------------------------------
614 */
615
616 void GSMEncodeFrame(
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      */
622 )
623 {
624
625     Speech_Encode_FrameState *st =
626         (Speech_Encode_FrameState *) state_data;
627
628     Word16 prm[MAX_PRM_SIZE];   /* Analysis parameters.                 */
629     Word16 syn[L_FRAME];        /* Buffer for synthesis speech          */
630     Word16 i;
631
632     /* initialize the serial output frame to zero */
633     for (i = 0; i < MAX_SERIAL_SIZE; i++)
634     {
635         serial[i] = 0;
636     }
637 #if !defined(NO13BIT)
638     /* Delete the 3 LSBs (13-bit input) */
639     for (i = 0; i < L_FRAME; i++)
640     {
641         new_speech[i] = new_speech[i] & 0xfff8;
642     }
643 #endif
644
645     /* filter + downscaling */
646     Pre_Process(st->pre_state, new_speech, L_FRAME);
647
648     /* Call the speech encoder */
649     cod_amr(st->cod_amr_state, mode, new_speech, prm, usedMode, syn);
650
651     /* Parameters to serial bits */
652     Prm2bits(*usedMode, prm, &serial[0], &(st->cod_amr_state->common_amr_tbls));
653
654     return;
655 }