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: /audio/gsm-amr/c/src/sid_sync.c
35  Functions: sid_sync_init
36             sid_sync_reset
37             sid_sync_exit
38             sid_sync_set_handover_debt
39             sid_sync
40 
41      Date: 03/13/2002
42 
43 ------------------------------------------------------------------------------
44  REVISION HISTORY
45 
46  Description: Changed type definition of state pointer to 'void' for
47               sid_sync_init, sid_sync_reset, sid_sync_exit, and sid_sync.
48               Updated to PV coding template.
49 
50  Description:  Replaced OSCL mem type functions and eliminated include
51                files that now are chosen by OSCL definitions
52 
53  Description:  Replaced "int" and/or "char" with OSCL defined types.
54 
55  Description:
56 
57 ------------------------------------------------------------------------------
58  MODULE DESCRIPTION
59 
60  This file contains the functions that initialize, reset, exit, and perform
61  SID synchronization.
62 
63 ------------------------------------------------------------------------------
64 */
65 
66 
67 /*----------------------------------------------------------------------------
68 ; INCLUDES
69 ----------------------------------------------------------------------------*/
70 #include <stdlib.h>
71 
72 #include "typedef.h"
73 #include "basic_op.h"
74 #include "mode.h"
75 #include "sid_sync.h"
76 
77 /*----------------------------------------------------------------------------
78 ; MACROS
79 ; [Define module specific macros here]
80 ----------------------------------------------------------------------------*/
81 
82 
83 /*----------------------------------------------------------------------------
84 ; DEFINES
85 ; [Include all pre-processor statements here. Include conditional
86 ; compile variables also.]
87 ----------------------------------------------------------------------------*/
88 
89 /*----------------------------------------------------------------------------
90 ; LOCAL FUNCTION DEFINITIONS
91 ; [List function prototypes here]
92 ----------------------------------------------------------------------------*/
93 
94 /*----------------------------------------------------------------------------
95 ; LOCAL VARIABLE DEFINITIONS
96 ; [Variable declaration - defined here and used outside this module]
97 ----------------------------------------------------------------------------*/
98 
99 
100 /*
101 ------------------------------------------------------------------------------
102  FUNCTION NAME: sid_sync_init
103 ------------------------------------------------------------------------------
104  INPUT AND OUTPUT DEFINITIONS
105 
106  Inputs:
107     state = pointer containing a pointer to the state structure used for
108             SID synchronization (void)
109 
110  Outputs:
111     None
112 
113  Returns:
114     return_value = status of sid_sync_reset function; -1, if state is pointing
115                    to a NULL address (int)
116 
117  Global Variables Used:
118     None
119 
120  Local Variables Needed:
121     None
122 
123 ------------------------------------------------------------------------------
124  FUNCTION DESCRIPTION
125 
126  This function initialize one instance of the sid_sync module. It stores
127  the pointer to state struct in *st. This pointer has to be passed to sid_sync
128  in each call. This function returns 0 on success, otherwise, -1.
129 
130 ------------------------------------------------------------------------------
131  REQUIREMENTS
132 
133  None
134 
135 ------------------------------------------------------------------------------
136  REFERENCES
137 
138  sid_sync.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
139 
140 ------------------------------------------------------------------------------
141  PSEUDO-CODE
142 
143 ------------------------------------------------------------------------------
144  RESOURCES USED [optional]
145 
146  When the code is written for a specific target processor the
147  the resources used should be documented below.
148 
149  HEAP MEMORY USED: x bytes
150 
151  STACK MEMORY USED: x bytes
152 
153  CLOCK CYCLES: (cycle count equation for this function) + (variable
154                 used to represent cycle count for each subroutine
155                 called)
156      where: (cycle count variable) = cycle count for [subroutine
157                                      name]
158 
159 ------------------------------------------------------------------------------
160  CAUTION [optional]
161  [State any special notes, constraints or cautions for users of this function]
162 
163 ------------------------------------------------------------------------------
164 */
165 
sid_sync_init(void ** state)166 Word16 sid_sync_init(void **state)
167 {
168     sid_syncState* s;
169 
170     if (state == NULL)
171     {
172         /* fprintf(stderr, "sid_sync_init:invalid state parameter\n"); */
173         return -1;
174     }
175 
176     *state = NULL;
177 
178     /* allocate memory */
179     if ((s = (sid_syncState *)
180              malloc(sizeof(sid_syncState))) == NULL)
181     {
182         /* fprintf(stderr,
183                 "sid_sync_init: "
184                 "can not malloc state structure\n"); */
185         return -1;
186     }
187     s->sid_update_rate = 8;
188 
189     *state = (void *)s;
190 
191     return(sid_sync_reset(s));
192 }
193 
194 /****************************************************************************/
195 
196 /*
197 ------------------------------------------------------------------------------
198  FUNCTION NAME: sid_sync_reset
199 ------------------------------------------------------------------------------
200  INPUT AND OUTPUT DEFINITIONS
201 
202  Inputs:
203     state = pointer to the state structure used for SID synchronization (void)
204 
205  Outputs:
206     None
207 
208  Returns:
209     return_value = 0 (int)
210 
211  Global Variables Used:
212     None
213 
214  Local Variables Needed:
215     None
216 
217 ------------------------------------------------------------------------------
218  FUNCTION DESCRIPTION
219 
220  This function performs a reset of the sid_sync module by setting the state
221  memory to zero.
222 
223 ------------------------------------------------------------------------------
224  REQUIREMENTS
225 
226  None
227 
228 ------------------------------------------------------------------------------
229  REFERENCES
230 
231  sid_sync.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
232 
233 ------------------------------------------------------------------------------
234  PSEUDO-CODE
235 
236 ------------------------------------------------------------------------------
237  RESOURCES USED [optional]
238 
239  When the code is written for a specific target processor the
240  the resources used should be documented below.
241 
242  HEAP MEMORY USED: x bytes
243 
244  STACK MEMORY USED: x bytes
245 
246  CLOCK CYCLES: (cycle count equation for this function) + (variable
247                 used to represent cycle count for each subroutine
248                 called)
249      where: (cycle count variable) = cycle count for [subroutine
250                                      name]
251 
252 ------------------------------------------------------------------------------
253  CAUTION [optional]
254  [State any special notes, constraints or cautions for users of this function]
255 
256 ------------------------------------------------------------------------------
257 */
sid_sync_reset(void * st)258 Word16 sid_sync_reset(void *st)
259 {
260     sid_syncState *state = (sid_syncState *) st;
261 
262     state->sid_update_counter = 3;
263     state->sid_handover_debt = 0;
264     state->prev_ft = TX_SPEECH_GOOD;
265 
266     return 0;
267 }
268 
269 
270 /****************************************************************************/
271 
272 /*
273 ------------------------------------------------------------------------------
274  FUNCTION NAME: sid_sync_exit
275 ------------------------------------------------------------------------------
276  INPUT AND OUTPUT DEFINITIONS
277 
278  Inputs:
279     state = pointer containing a pointer to the state structure used for
280             SID synchronization (void)
281 
282  Outputs:
283     None
284 
285  Returns:
286     None
287 
288  Global Variables Used:
289     None
290 
291  Local Variables Needed:
292     None
293 
294 ------------------------------------------------------------------------------
295  FUNCTION DESCRIPTION
296 
297  This function frees up the state structure used by sid_sync function. It
298  stores NULL in *state.
299 
300 ------------------------------------------------------------------------------
301  REQUIREMENTS
302 
303  None
304 
305 ------------------------------------------------------------------------------
306  REFERENCES
307 
308  sid_sync.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
309 
310 ------------------------------------------------------------------------------
311  PSEUDO-CODE
312 
313 ------------------------------------------------------------------------------
314  RESOURCES USED [optional]
315 
316  When the code is written for a specific target processor the
317  the resources used should be documented below.
318 
319  HEAP MEMORY USED: x bytes
320 
321  STACK MEMORY USED: x bytes
322 
323  CLOCK CYCLES: (cycle count equation for this function) + (variable
324                 used to represent cycle count for each subroutine
325                 called)
326      where: (cycle count variable) = cycle count for [subroutine
327                                      name]
328 
329 ------------------------------------------------------------------------------
330  CAUTION [optional]
331  [State any special notes, constraints or cautions for users of this function]
332 
333 ------------------------------------------------------------------------------
334 */
sid_sync_exit(void ** state)335 void sid_sync_exit(void **state)
336 {
337     sid_syncState **st = (sid_syncState **) state;
338 
339     if (st == NULL || *st == NULL)
340     {
341         return;
342     }
343 
344     /* deallocate memory */
345     free(*st);
346     *st = NULL;
347 
348     return;
349 
350 }
351 
352 /****************************************************************************/
353 
354 /*
355 ------------------------------------------------------------------------------
356  FUNCTION NAME: sid_sync_set_handover_debt
357 ------------------------------------------------------------------------------
358  INPUT AND OUTPUT DEFINITIONS
359 
360  Inputs:
361     st = pointer to the state structure used for SID synchronization
362          (sid_syncState)
363     debtFrames = number of handover debt frames (Word16)
364 
365  Outputs:
366     st->sid_handover_debt is set to debtFrames
367 
368  Returns:
369     return_value = 0
370 
371  Global Variables Used:
372     None
373 
374  Local Variables Needed:
375     None
376 
377 ------------------------------------------------------------------------------
378  FUNCTION DESCRIPTION
379 
380  This function updates the handover debt to debtFrames. Extra SID_UPD are
381  scheduled to update remote decoder CNI states, right after an handover.
382  This is primarily for use on MS UL side.
383 
384 ------------------------------------------------------------------------------
385  REQUIREMENTS
386 
387  None
388 
389 ------------------------------------------------------------------------------
390  REFERENCES
391 
392  sid_sync.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
393 
394 ------------------------------------------------------------------------------
395  PSEUDO-CODE
396 
397 ------------------------------------------------------------------------------
398  RESOURCES USED [optional]
399 
400  When the code is written for a specific target processor the
401  the resources used should be documented below.
402 
403  HEAP MEMORY USED: x bytes
404 
405  STACK MEMORY USED: x bytes
406 
407  CLOCK CYCLES: (cycle count equation for this function) + (variable
408                 used to represent cycle count for each subroutine
409                 called)
410      where: (cycle count variable) = cycle count for [subroutine
411                                      name]
412 
413 ------------------------------------------------------------------------------
414  CAUTION [optional]
415  [State any special notes, constraints or cautions for users of this function]
416 
417 ------------------------------------------------------------------------------
418 */
sid_sync_set_handover_debt(sid_syncState * st,Word16 debtFrames)419 void sid_sync_set_handover_debt(sid_syncState *st,
420                                 Word16 debtFrames)
421 {
422     /* debtFrames >= 0 */
423     st->sid_handover_debt = debtFrames;
424     return;
425 }
426 
427 
428 /****************************************************************************/
429 
430 /*
431 ------------------------------------------------------------------------------
432  FUNCTION NAME: sid_sync
433 ------------------------------------------------------------------------------
434  INPUT AND OUTPUT DEFINITIONS
435 
436  Inputs:
437     state = pointer to the state structure used for SID synchronization
438             (sid_syncState)
439     mode = codec mode (enum Mode)
440     tx_frame_type = pointer to TX frame type store (enum TXFrameType)
441 
442  Outputs:
443     tx_frame_type contains the new TX frame type
444 
445  Returns:
446     None
447 
448  Global Variables Used:
449     None
450 
451  Local Variables Needed:
452     None
453 
454 ------------------------------------------------------------------------------
455  FUNCTION DESCRIPTION
456 
457  This function performs SID frame synchronization to ensure that the mode
458  only switches to a neighbouring mode.
459 
460 ------------------------------------------------------------------------------
461  REQUIREMENTS
462 
463  None
464 
465 ------------------------------------------------------------------------------
466  REFERENCES
467 
468  sid_sync.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
469 
470 ------------------------------------------------------------------------------
471  PSEUDO-CODE
472 
473 ------------------------------------------------------------------------------
474  RESOURCES USED [optional]
475 
476  When the code is written for a specific target processor the
477  the resources used should be documented below.
478 
479  HEAP MEMORY USED: x bytes
480 
481  STACK MEMORY USED: x bytes
482 
483  CLOCK CYCLES: (cycle count equation for this function) + (variable
484                 used to represent cycle count for each subroutine
485                 called)
486      where: (cycle count variable) = cycle count for [subroutine
487                                      name]
488 
489 ------------------------------------------------------------------------------
490  CAUTION [optional]
491  [State any special notes, constraints or cautions for users of this function]
492 
493 ------------------------------------------------------------------------------
494 */
sid_sync(void * state,enum Mode mode,enum TXFrameType * tx_frame_type)495 void sid_sync(void *state,
496               enum Mode mode,
497               enum TXFrameType *tx_frame_type)
498 {
499 
500     sid_syncState *st = (sid_syncState *) state;
501 
502     if (mode == MRDTX)
503     {
504 
505         st->sid_update_counter--;
506 
507         if (st->prev_ft == TX_SPEECH_GOOD)
508         {
509             *tx_frame_type = TX_SID_FIRST;
510             st->sid_update_counter = 3;
511         }
512         else
513         {
514             /* TX_SID_UPDATE or TX_NO_DATA */
515             if ((st->sid_handover_debt > 0) &&
516                     (st->sid_update_counter > 2))
517             {
518                 /* ensure extra updates are  properly delayed after
519                    a possible SID_FIRST */
520                 *tx_frame_type = TX_SID_UPDATE;
521                 st->sid_handover_debt--;
522             }
523             else
524             {
525                 if (st->sid_update_counter == 0)
526                 {
527                     *tx_frame_type = TX_SID_UPDATE;
528                     st->sid_update_counter = st->sid_update_rate;
529                 }
530                 else
531                 {
532                     *tx_frame_type = TX_NO_DATA;
533                 }
534             }
535         }
536     }
537     else
538     {
539         st->sid_update_counter = st->sid_update_rate ;
540         *tx_frame_type = TX_SPEECH_GOOD;
541     }
542     st->prev_ft = *tx_frame_type;
543 }
544 
545