Initialize Tizen 2.3
[external/opencore-amr.git] / opencore / codecs_v2 / audio / gsm_amr / amr_nb / enc / src / sid_sync.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: sid_sync.cpp
35  Functions: sid_sync_init
36             sid_sync_reset
37             sid_sync_exit
38             sid_sync_set_handover_debt
39             sid_sync
40
41 ------------------------------------------------------------------------------
42  MODULE DESCRIPTION
43
44  This file contains the functions that initialize, reset, exit, and perform
45  SID synchronization.
46
47 ------------------------------------------------------------------------------
48 */
49
50
51 /*----------------------------------------------------------------------------
52 ; INCLUDES
53 ----------------------------------------------------------------------------*/
54 #include "typedef.h"
55 #include "basic_op.h"
56 #include "mode.h"
57 #include "sid_sync.h"
58 #include "oscl_mem.h"
59
60 /*----------------------------------------------------------------------------
61 ; MACROS
62 ; [Define module specific macros here]
63 ----------------------------------------------------------------------------*/
64
65
66 /*----------------------------------------------------------------------------
67 ; DEFINES
68 ; [Include all pre-processor statements here. Include conditional
69 ; compile variables also.]
70 ----------------------------------------------------------------------------*/
71
72 /*----------------------------------------------------------------------------
73 ; LOCAL FUNCTION DEFINITIONS
74 ; [List function prototypes here]
75 ----------------------------------------------------------------------------*/
76
77 /*----------------------------------------------------------------------------
78 ; LOCAL VARIABLE DEFINITIONS
79 ; [Variable declaration - defined here and used outside this module]
80 ----------------------------------------------------------------------------*/
81
82
83 /*
84 ------------------------------------------------------------------------------
85  FUNCTION NAME: sid_sync_init
86 ------------------------------------------------------------------------------
87  INPUT AND OUTPUT DEFINITIONS
88
89  Inputs:
90     state = pointer containing a pointer to the state structure used for
91             SID synchronization (void)
92
93  Outputs:
94     None
95
96  Returns:
97     return_value = status of sid_sync_reset function; -1, if state is pointing
98                    to a NULL address (int)
99
100  Global Variables Used:
101     None
102
103  Local Variables Needed:
104     None
105
106 ------------------------------------------------------------------------------
107  FUNCTION DESCRIPTION
108
109  This function initialize one instance of the sid_sync module. It stores
110  the pointer to state struct in *st. This pointer has to be passed to sid_sync
111  in each call. This function returns 0 on success, otherwise, -1.
112
113 ------------------------------------------------------------------------------
114  REQUIREMENTS
115
116  None
117
118 ------------------------------------------------------------------------------
119  REFERENCES
120
121  sid_sync.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
122
123 ------------------------------------------------------------------------------
124  PSEUDO-CODE
125
126 ------------------------------------------------------------------------------
127  CAUTION [optional]
128  [State any special notes, constraints or cautions for users of this function]
129
130 ------------------------------------------------------------------------------
131 */
132
133 Word16 sid_sync_init(void **state)
134 {
135     sid_syncState* s;
136
137     if (state == NULL)
138     {
139         /* fprintf(stderr, "sid_sync_init:invalid state parameter\n"); */
140         return -1;
141     }
142
143     *state = NULL;
144
145     /* allocate memory */
146     if ((s = (sid_syncState *)
147              oscl_malloc(sizeof(sid_syncState))) == NULL)
148     {
149         /* fprintf(stderr,
150                 "sid_sync_init: "
151                 "can not malloc state structure\n"); */
152         return -1;
153     }
154     s->sid_update_rate = 8;
155
156     *state = (void *)s;
157
158     return(sid_sync_reset(s));
159 }
160
161 /****************************************************************************/
162
163 /*
164 ------------------------------------------------------------------------------
165  FUNCTION NAME: sid_sync_reset
166 ------------------------------------------------------------------------------
167  INPUT AND OUTPUT DEFINITIONS
168
169  Inputs:
170     state = pointer to the state structure used for SID synchronization (void)
171
172  Outputs:
173     None
174
175  Returns:
176     return_value = 0 (int)
177
178  Global Variables Used:
179     None
180
181  Local Variables Needed:
182     None
183
184 ------------------------------------------------------------------------------
185  FUNCTION DESCRIPTION
186
187  This function performs a reset of the sid_sync module by setting the state
188  memory to zero.
189
190 ------------------------------------------------------------------------------
191  REQUIREMENTS
192
193  None
194
195 ------------------------------------------------------------------------------
196  REFERENCES
197
198  sid_sync.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
199
200 ------------------------------------------------------------------------------
201  PSEUDO-CODE
202
203 ------------------------------------------------------------------------------
204  CAUTION [optional]
205  [State any special notes, constraints or cautions for users of this function]
206
207 ------------------------------------------------------------------------------
208 */
209 Word16 sid_sync_reset(void *st)
210 {
211     sid_syncState *state = (sid_syncState *) st;
212
213     state->sid_update_counter = 3;
214     state->sid_handover_debt = 0;
215     state->prev_ft = TX_SPEECH_GOOD;
216
217     return 0;
218 }
219
220
221 /****************************************************************************/
222
223 /*
224 ------------------------------------------------------------------------------
225  FUNCTION NAME: sid_sync_exit
226 ------------------------------------------------------------------------------
227  INPUT AND OUTPUT DEFINITIONS
228
229  Inputs:
230     state = pointer containing a pointer to the state structure used for
231             SID synchronization (void)
232
233  Outputs:
234     None
235
236  Returns:
237     None
238
239  Global Variables Used:
240     None
241
242  Local Variables Needed:
243     None
244
245 ------------------------------------------------------------------------------
246  FUNCTION DESCRIPTION
247
248  This function frees up the state structure used by sid_sync function. It
249  stores NULL in *state.
250
251 ------------------------------------------------------------------------------
252  REQUIREMENTS
253
254  None
255
256 ------------------------------------------------------------------------------
257  REFERENCES
258
259  sid_sync.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
260
261 ------------------------------------------------------------------------------
262  PSEUDO-CODE
263
264 ------------------------------------------------------------------------------
265  CAUTION [optional]
266  [State any special notes, constraints or cautions for users of this function]
267
268 ------------------------------------------------------------------------------
269 */
270 void sid_sync_exit(void **state)
271 {
272     sid_syncState **st = (sid_syncState **) state;
273
274     if (st == NULL || *st == NULL)
275     {
276         return;
277     }
278
279     /* deallocate memory */
280     oscl_free(*st);
281     *st = NULL;
282
283     return;
284
285 }
286
287 /****************************************************************************/
288
289 /*
290 ------------------------------------------------------------------------------
291  FUNCTION NAME: sid_sync_set_handover_debt
292 ------------------------------------------------------------------------------
293  INPUT AND OUTPUT DEFINITIONS
294
295  Inputs:
296     st = pointer to the state structure used for SID synchronization
297          (sid_syncState)
298     debtFrames = number of handover debt frames (Word16)
299
300  Outputs:
301     st->sid_handover_debt is set to debtFrames
302
303  Returns:
304     return_value = 0
305
306  Global Variables Used:
307     None
308
309  Local Variables Needed:
310     None
311
312 ------------------------------------------------------------------------------
313  FUNCTION DESCRIPTION
314
315  This function updates the handover debt to debtFrames. Extra SID_UPD are
316  scheduled to update remote decoder CNI states, right after an handover.
317  This is primarily for use on MS UL side.
318
319 ------------------------------------------------------------------------------
320  REQUIREMENTS
321
322  None
323
324 ------------------------------------------------------------------------------
325  REFERENCES
326
327  sid_sync.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
328
329 ------------------------------------------------------------------------------
330  PSEUDO-CODE
331
332 ------------------------------------------------------------------------------
333  CAUTION [optional]
334  [State any special notes, constraints or cautions for users of this function]
335
336 ------------------------------------------------------------------------------
337 */
338 void sid_sync_set_handover_debt(sid_syncState *st,
339                                 Word16 debtFrames)
340 {
341     /* debtFrames >= 0 */
342     st->sid_handover_debt = debtFrames;
343     return;
344 }
345
346
347 /****************************************************************************/
348
349 /*
350 ------------------------------------------------------------------------------
351  FUNCTION NAME: sid_sync
352 ------------------------------------------------------------------------------
353  INPUT AND OUTPUT DEFINITIONS
354
355  Inputs:
356     state = pointer to the state structure used for SID synchronization
357             (sid_syncState)
358     mode = codec mode (enum Mode)
359     tx_frame_type = pointer to TX frame type store (enum TXFrameType)
360
361  Outputs:
362     tx_frame_type contains the new TX frame type
363
364  Returns:
365     None
366
367  Global Variables Used:
368     None
369
370  Local Variables Needed:
371     None
372
373 ------------------------------------------------------------------------------
374  FUNCTION DESCRIPTION
375
376  This function performs SID frame synchronization to ensure that the mode
377  only switches to a neighbouring mode.
378
379 ------------------------------------------------------------------------------
380  REQUIREMENTS
381
382  None
383
384 ------------------------------------------------------------------------------
385  REFERENCES
386
387  sid_sync.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
388
389 ------------------------------------------------------------------------------
390  PSEUDO-CODE
391
392 ------------------------------------------------------------------------------
393  CAUTION [optional]
394  [State any special notes, constraints or cautions for users of this function]
395
396 ------------------------------------------------------------------------------
397 */
398 void sid_sync(void *state,
399               enum Mode mode,
400               enum TXFrameType *tx_frame_type)
401 {
402
403     sid_syncState *st = (sid_syncState *) state;
404
405     if (mode == MRDTX)
406     {
407
408         st->sid_update_counter--;
409
410         if (st->prev_ft == TX_SPEECH_GOOD)
411         {
412             *tx_frame_type = TX_SID_FIRST;
413             st->sid_update_counter = 3;
414         }
415         else
416         {
417             /* TX_SID_UPDATE or TX_NO_DATA */
418             if ((st->sid_handover_debt > 0) &&
419                     (st->sid_update_counter > 2))
420             {
421                 /* ensure extra updates are  properly delayed after
422                    a possible SID_FIRST */
423                 *tx_frame_type = TX_SID_UPDATE;
424                 st->sid_handover_debt--;
425             }
426             else
427             {
428                 if (st->sid_update_counter == 0)
429                 {
430                     *tx_frame_type = TX_SID_UPDATE;
431                     st->sid_update_counter = st->sid_update_rate;
432                 }
433                 else
434                 {
435                     *tx_frame_type = TX_NO_DATA;
436                 }
437             }
438         }
439     }
440     else
441     {
442         st->sid_update_counter = st->sid_update_rate ;
443         *tx_frame_type = TX_SPEECH_GOOD;
444     }
445     st->prev_ft = *tx_frame_type;
446 }
447