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 ------------------------------------------------------------------------------
34 Filename: sid_sync.cpp
35 Functions: sid_sync_init
38 sid_sync_set_handover_debt
41 ------------------------------------------------------------------------------
44 This file contains the functions that initialize, reset, exit, and perform
47 ------------------------------------------------------------------------------
51 /*----------------------------------------------------------------------------
53 ----------------------------------------------------------------------------*/
60 /*----------------------------------------------------------------------------
62 ; [Define module specific macros here]
63 ----------------------------------------------------------------------------*/
66 /*----------------------------------------------------------------------------
68 ; [Include all pre-processor statements here. Include conditional
69 ; compile variables also.]
70 ----------------------------------------------------------------------------*/
72 /*----------------------------------------------------------------------------
73 ; LOCAL FUNCTION DEFINITIONS
74 ; [List function prototypes here]
75 ----------------------------------------------------------------------------*/
77 /*----------------------------------------------------------------------------
78 ; LOCAL VARIABLE DEFINITIONS
79 ; [Variable declaration - defined here and used outside this module]
80 ----------------------------------------------------------------------------*/
84 ------------------------------------------------------------------------------
85 FUNCTION NAME: sid_sync_init
86 ------------------------------------------------------------------------------
87 INPUT AND OUTPUT DEFINITIONS
90 state = pointer containing a pointer to the state structure used for
91 SID synchronization (void)
97 return_value = status of sid_sync_reset function; -1, if state is pointing
98 to a NULL address (int)
100 Global Variables Used:
103 Local Variables Needed:
106 ------------------------------------------------------------------------------
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.
113 ------------------------------------------------------------------------------
118 ------------------------------------------------------------------------------
121 sid_sync.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
123 ------------------------------------------------------------------------------
126 ------------------------------------------------------------------------------
128 [State any special notes, constraints or cautions for users of this function]
130 ------------------------------------------------------------------------------
133 Word16 sid_sync_init(void **state)
139 /* fprintf(stderr, "sid_sync_init:invalid state parameter\n"); */
145 /* allocate memory */
146 if ((s = (sid_syncState *)
147 oscl_malloc(sizeof(sid_syncState))) == NULL)
151 "can not malloc state structure\n"); */
154 s->sid_update_rate = 8;
158 return(sid_sync_reset(s));
161 /****************************************************************************/
164 ------------------------------------------------------------------------------
165 FUNCTION NAME: sid_sync_reset
166 ------------------------------------------------------------------------------
167 INPUT AND OUTPUT DEFINITIONS
170 state = pointer to the state structure used for SID synchronization (void)
176 return_value = 0 (int)
178 Global Variables Used:
181 Local Variables Needed:
184 ------------------------------------------------------------------------------
187 This function performs a reset of the sid_sync module by setting the state
190 ------------------------------------------------------------------------------
195 ------------------------------------------------------------------------------
198 sid_sync.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
200 ------------------------------------------------------------------------------
203 ------------------------------------------------------------------------------
205 [State any special notes, constraints or cautions for users of this function]
207 ------------------------------------------------------------------------------
209 Word16 sid_sync_reset(void *st)
211 sid_syncState *state = (sid_syncState *) st;
213 state->sid_update_counter = 3;
214 state->sid_handover_debt = 0;
215 state->prev_ft = TX_SPEECH_GOOD;
221 /****************************************************************************/
224 ------------------------------------------------------------------------------
225 FUNCTION NAME: sid_sync_exit
226 ------------------------------------------------------------------------------
227 INPUT AND OUTPUT DEFINITIONS
230 state = pointer containing a pointer to the state structure used for
231 SID synchronization (void)
239 Global Variables Used:
242 Local Variables Needed:
245 ------------------------------------------------------------------------------
248 This function frees up the state structure used by sid_sync function. It
249 stores NULL in *state.
251 ------------------------------------------------------------------------------
256 ------------------------------------------------------------------------------
259 sid_sync.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
261 ------------------------------------------------------------------------------
264 ------------------------------------------------------------------------------
266 [State any special notes, constraints or cautions for users of this function]
268 ------------------------------------------------------------------------------
270 void sid_sync_exit(void **state)
272 sid_syncState **st = (sid_syncState **) state;
274 if (st == NULL || *st == NULL)
279 /* deallocate memory */
287 /****************************************************************************/
290 ------------------------------------------------------------------------------
291 FUNCTION NAME: sid_sync_set_handover_debt
292 ------------------------------------------------------------------------------
293 INPUT AND OUTPUT DEFINITIONS
296 st = pointer to the state structure used for SID synchronization
298 debtFrames = number of handover debt frames (Word16)
301 st->sid_handover_debt is set to debtFrames
306 Global Variables Used:
309 Local Variables Needed:
312 ------------------------------------------------------------------------------
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.
319 ------------------------------------------------------------------------------
324 ------------------------------------------------------------------------------
327 sid_sync.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
329 ------------------------------------------------------------------------------
332 ------------------------------------------------------------------------------
334 [State any special notes, constraints or cautions for users of this function]
336 ------------------------------------------------------------------------------
338 void sid_sync_set_handover_debt(sid_syncState *st,
341 /* debtFrames >= 0 */
342 st->sid_handover_debt = debtFrames;
347 /****************************************************************************/
350 ------------------------------------------------------------------------------
351 FUNCTION NAME: sid_sync
352 ------------------------------------------------------------------------------
353 INPUT AND OUTPUT DEFINITIONS
356 state = pointer to the state structure used for SID synchronization
358 mode = codec mode (enum Mode)
359 tx_frame_type = pointer to TX frame type store (enum TXFrameType)
362 tx_frame_type contains the new TX frame type
367 Global Variables Used:
370 Local Variables Needed:
373 ------------------------------------------------------------------------------
376 This function performs SID frame synchronization to ensure that the mode
377 only switches to a neighbouring mode.
379 ------------------------------------------------------------------------------
384 ------------------------------------------------------------------------------
387 sid_sync.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
389 ------------------------------------------------------------------------------
392 ------------------------------------------------------------------------------
394 [State any special notes, constraints or cautions for users of this function]
396 ------------------------------------------------------------------------------
398 void sid_sync(void *state,
400 enum TXFrameType *tx_frame_type)
403 sid_syncState *st = (sid_syncState *) state;
408 st->sid_update_counter--;
410 if (st->prev_ft == TX_SPEECH_GOOD)
412 *tx_frame_type = TX_SID_FIRST;
413 st->sid_update_counter = 3;
417 /* TX_SID_UPDATE or TX_NO_DATA */
418 if ((st->sid_handover_debt > 0) &&
419 (st->sid_update_counter > 2))
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--;
428 if (st->sid_update_counter == 0)
430 *tx_frame_type = TX_SID_UPDATE;
431 st->sid_update_counter = st->sid_update_rate;
435 *tx_frame_type = TX_NO_DATA;
442 st->sid_update_counter = st->sid_update_rate ;
443 *tx_frame_type = TX_SPEECH_GOOD;
445 st->prev_ft = *tx_frame_type;