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 Functions: dtx_enc_init
42 ------------------------------------------------------------------------------
45 This file contains the various functions that perform the computation of the
46 Silence Indicator (SID) parameters when in Discontinuous Transmission (DTX)
49 ------------------------------------------------------------------------------
53 /*----------------------------------------------------------------------------
55 ----------------------------------------------------------------------------*/
66 /*----------------------------------------------------------------------------
68 ; Define module specific macros here
69 ----------------------------------------------------------------------------*/
70 extern Word32 L_add(register Word32 L_var1, register Word32 L_var2, Flag *pOverflow);
72 /*----------------------------------------------------------------------------
74 ; Include all pre-processor statements here. Include conditional
75 ; compile variables also.
76 ----------------------------------------------------------------------------*/
78 /*----------------------------------------------------------------------------
79 ; LOCAL FUNCTION DEFINITIONS
80 ; Function Prototype declaration
81 ----------------------------------------------------------------------------*/
83 /*----------------------------------------------------------------------------
84 ; LOCAL VARIABLE DEFINITIONS
85 ; Variable declaration - defined here and used outside this module
86 ----------------------------------------------------------------------------*/
90 ------------------------------------------------------------------------------
91 FUNCTION NAME: dtx_enc_init
92 ------------------------------------------------------------------------------
93 INPUT AND OUTPUT DEFINITIONS
96 st = pointer to an array of pointers to structures of type
100 pointer pointed to by st is set to the address of the allocated
104 return_value = 0, if initialization was successful; -1, otherwise (int)
106 Global Variables Used:
109 Local Variables Needed:
112 ------------------------------------------------------------------------------
115 This function allocates the state memory used by the dtx_enc function.
117 ------------------------------------------------------------------------------
122 ------------------------------------------------------------------------------
125 dtx_enc.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
127 ------------------------------------------------------------------------------
130 int dtx_enc_init (dtx_encState **st)
134 if (st == (dtx_encState **) NULL){
135 fprintf(stderr, "dtx_enc_init: invalid parameter\n");
142 if ((s= (dtx_encState *) malloc(sizeof(dtx_encState))) == NULL){
143 fprintf(stderr, "dtx_enc_init: can not malloc state structure\n");
153 ------------------------------------------------------------------------------
155 [State any special notes, constraints or cautions for users of this function]
157 ------------------------------------------------------------------------------
160 Word16 dtx_enc_init(dtx_encState **st, const Word16* lsp_init_data_ptr)
164 if (st == (dtx_encState **) NULL)
171 /* allocate memory */
172 if ((s = (dtx_encState *) oscl_malloc(sizeof(dtx_encState))) == NULL)
177 dtx_enc_reset(s, lsp_init_data_ptr);
183 /****************************************************************************/
186 ------------------------------------------------------------------------------
187 FUNCTION NAME: dtx_enc_reset
188 ------------------------------------------------------------------------------
189 INPUT AND OUTPUT DEFINITIONS
192 st = pointer to structures of type dtx_encState
195 structure pointed to by st is initialized to its reset value
198 return_value = 1, if reset was successful; -1, otherwise (int)
200 Global Variables Used:
203 Local Variables Needed:
204 lsp_init_data = table containing LSP initialization values;
205 table elements are constants of type Word16;
208 ------------------------------------------------------------------------------
211 This function initializes the fields of the state memory used by dtx_enc
212 to their reset values.
214 ------------------------------------------------------------------------------
219 ------------------------------------------------------------------------------
222 dtx_enc.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
224 ------------------------------------------------------------------------------
227 int dtx_enc_reset (dtx_encState *st)
231 if (st == (dtx_encState *) NULL){
232 fprintf(stderr, "dtx_enc_reset: invalid parameter\n");
237 st->log_en_index = 0;
238 st->init_lsf_vq_index = 0;
239 st->lsp_index[0] = 0;
240 st->lsp_index[1] = 0;
241 st->lsp_index[2] = 0;
244 for(i = 0; i < DTX_HIST_SIZE; i++)
246 Copy(lsp_init_data, &st->lsp_hist[i * M], M);
249 // Reset energy history
250 Set_zero(st->log_en_hist, M);
252 st->dtxHangoverCount = DTX_HANG_CONST;
253 st->decAnaElapsedCount = 32767;
258 ------------------------------------------------------------------------------
260 [State any special notes, constraints or cautions for users of this function]
262 ------------------------------------------------------------------------------
265 Word16 dtx_enc_reset(dtx_encState *st, const Word16* lsp_init_data_ptr)
269 if (st == (dtx_encState *) NULL)
275 st->log_en_index = 0;
276 st->init_lsf_vq_index = 0;
277 st->lsp_index[0] = 0;
278 st->lsp_index[1] = 0;
279 st->lsp_index[2] = 0;
281 /* Init lsp_hist[] */
282 for (i = 0; i < DTX_HIST_SIZE; i++)
284 oscl_memcpy(&st->lsp_hist[i * M], lsp_init_data_ptr, M*sizeof(Word16));
287 /* Reset energy history */
288 oscl_memset(st->log_en_hist, 0, sizeof(Word16)*M);
289 st->dtxHangoverCount = DTX_HANG_CONST;
290 st->decAnaElapsedCount = 32767;
295 /****************************************************************************/
298 ------------------------------------------------------------------------------
299 FUNCTION NAME: dtx_enc_exit
300 ------------------------------------------------------------------------------
301 INPUT AND OUTPUT DEFINITIONS
304 st = pointer to an array of pointers to structures of type
308 st points to the NULL address
313 Global Variables Used:
316 Local Variables Needed:
319 ------------------------------------------------------------------------------
322 This function deallocates the state memory used by dtx_enc function.
324 ------------------------------------------------------------------------------
329 ------------------------------------------------------------------------------
332 dtx_enc.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
334 ------------------------------------------------------------------------------
337 void dtx_enc_exit (dtx_encState **st)
339 if (st == NULL || *st == NULL)
349 ------------------------------------------------------------------------------
351 [State any special notes, constraints or cautions for users of this function]
353 ------------------------------------------------------------------------------
356 void dtx_enc_exit(dtx_encState **st)
358 if (st == NULL || *st == NULL)
363 /* deallocate memory */
370 /****************************************************************************/
373 ------------------------------------------------------------------------------
374 FUNCTION NAME: dtx_enc
375 ------------------------------------------------------------------------------
376 INPUT AND OUTPUT DEFINITIONS
379 st = pointer to structures of type dtx_encState
380 computeSidFlag = compute SID flag of type Word16
381 qSt = pointer to structures of type Q_plsfState
382 predState = pointer to structures of type gc_predState
383 anap = pointer to an array of pointers to analysis parameters of
387 structure pointed to by st contains the newly calculated SID
389 structure pointed to by predState contains the new logarithmic frame
391 pointer pointed to by anap points to the location of the new
392 logarithmic frame energy and new LSPs
395 return_value = 0 (int)
397 Global Variables Used:
400 Local Variables Needed:
403 ------------------------------------------------------------------------------
406 This function calculates the SID parameters when in the DTX mode.
408 ------------------------------------------------------------------------------
413 ------------------------------------------------------------------------------
416 dtx_enc.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
418 ------------------------------------------------------------------------------
421 int dtx_enc(dtx_encState *st, // i/o : State struct
422 Word16 computeSidFlag, // i : compute SID
423 Q_plsfState *qSt, // i/o : Qunatizer state struct
424 gc_predState* predState, // i/o : State struct
425 Word16 **anap // o : analysis parameters
435 // VOX mode computation of SID parameters
436 if ((computeSidFlag != 0) ||
437 (st->log_en_index == 0))
439 // compute new SID frame if safe i.e don't
440 // compute immediately after a talk spurt
442 for (i = 0; i < M; i++)
447 // average energy and lsp
448 for (i = 0; i < DTX_HIST_SIZE; i++)
451 shr(st->log_en_hist[i],2));
453 for (j = 0; j < M; j++)
455 L_lsp[j] = L_add(L_lsp[j],
456 L_deposit_l(st->lsp_hist[i * M + j]));
460 log_en = shr(log_en, 1);
461 for (j = 0; j < M; j++)
463 lsp[j] = extract_l(L_shr(L_lsp[j], 3)); // divide by 8
466 // quantize logarithmic energy to 6 bits
467 st->log_en_index = add(log_en, 2560); // +2.5 in Q10
468 st->log_en_index = add(st->log_en_index, 128); // add 0.5/4 in Q10
469 st->log_en_index = shr(st->log_en_index, 8);
471 if (sub(st->log_en_index, 63) > 0)
473 st->log_en_index = 63;
475 if (st->log_en_index < 0)
477 st->log_en_index = 0;
480 // update gain predictor memory
481 log_en = shl(st->log_en_index, -2+10); // Q11 and divide by 4
482 log_en = sub(log_en, 2560); // add 2.5 in Q11
484 log_en = sub(log_en, 9000);
489 if (sub(log_en, -14436) < 0)
494 // past_qua_en for other modes than MR122
495 predState->past_qua_en[0] = log_en;
496 predState->past_qua_en[1] = log_en;
497 predState->past_qua_en[2] = log_en;
498 predState->past_qua_en[3] = log_en;
500 // scale down by factor 20*log10(2) in Q15
501 log_en = mult(5443, log_en);
503 // past_qua_en for mode MR122
504 predState->past_qua_en_MR122[0] = log_en;
505 predState->past_qua_en_MR122[1] = log_en;
506 predState->past_qua_en_MR122[2] = log_en;
507 predState->past_qua_en_MR122[3] = log_en;
509 // make sure that LSP's are ordered
510 Lsp_lsf(lsp, lsf, M);
511 Reorder_lsf(lsf, LSF_GAP, M);
512 Lsf_lsp(lsf, lsp, M);
514 // Quantize lsp and put on parameter list
515 Q_plsf_3(qSt, MRDTX, lsp, lsp_q, st->lsp_index,
516 &st->init_lsf_vq_index);
519 *(*anap)++ = st->init_lsf_vq_index; // 3 bits
521 *(*anap)++ = st->lsp_index[0]; // 8 bits
522 *(*anap)++ = st->lsp_index[1]; // 9 bits
523 *(*anap)++ = st->lsp_index[2]; // 9 bits
526 *(*anap)++ = st->log_en_index; // 6 bits
532 ------------------------------------------------------------------------------
534 [State any special notes, constraints or cautions for users of this function]
536 ------------------------------------------------------------------------------
539 void dtx_enc(dtx_encState *st, /* i/o : State struct */
540 Word16 computeSidFlag, /* i : compute SID */
541 Q_plsfState *qSt, /* i/o : Qunatizer state struct */
542 gc_predState* predState, /* i/o : State struct */
543 Word16 **anap, /* o : analysis parameters */
544 Flag *pOverflow /* i/o : overflow indicator */
547 register Word16 i, j;
555 /* VOX mode computation of SID parameters */
557 if ((computeSidFlag != 0) ||
558 (st->log_en_index == 0))
560 /* compute new SID frame if safe i.e don't
561 * compute immediately after a talk spurt */
563 for (i = M - 1; i >= 0; i--)
568 /* average energy and lsp */
569 for (i = DTX_HIST_SIZE - 1; i >= 0; i--)
571 if (st->log_en_hist[i] < 0)
573 temp = ~((~(st->log_en_hist[i])) >> 2);
577 temp = st->log_en_hist[i] >> 2;
579 log_en = add_16(log_en, temp, pOverflow);
581 for (j = M - 1; j >= 0; j--)
583 L_lsp[j] = L_add(L_lsp[j],
584 (Word32)(st->lsp_hist[i * M + j]),
591 log_en = ~((~log_en) >> 1);
595 log_en = log_en >> 1;
598 for (j = M - 1; j >= 0; j--)
603 lsp[j] = (Word16)(~((~L_lsp[j]) >> 3));
607 lsp[j] = (Word16)(L_lsp[j] >> 3);
611 /* quantize logarithmic energy to 6 bits */
613 st->log_en_index = log_en + 2560;
614 /* add 0.5/4 in Q10 */
615 st->log_en_index += 128;
616 if (st->log_en_index < 0)
618 st->log_en_index = ~((~st->log_en_index) >> 8);
622 st->log_en_index = st->log_en_index >> 8;
625 /*---------------------------------------------*/
626 /* Limit to max and min allowable 6-bit values */
627 /* Note: For assembly implementation, use the */
629 /* if(st->long_en_index >> 6 != 0) */
631 /* if(st->long_en_index < 0) */
633 /* st->long_en_index = 0 */
637 /* st->long_en_index = 63 */
640 /*---------------------------------------------*/
641 if (st->log_en_index > 63)
643 st->log_en_index = 63;
645 else if (st->log_en_index < 0)
647 st->log_en_index = 0;
650 /* update gain predictor memory */
651 /* Q11 and divide by 4 */
652 log_en = (Word16)(((Word32) st->log_en_index) << (-2 + 10));
654 log_en = sub(log_en, 11560, pOverflow);
660 else if (log_en < -14436)
665 /* past_qua_en for other modes than MR122 */
666 predState->past_qua_en[0] = log_en;
667 predState->past_qua_en[1] = log_en;
668 predState->past_qua_en[2] = log_en;
669 predState->past_qua_en[3] = log_en;
671 /* scale down by factor 20*log10(2) in Q15 */
672 log_en = (Word16)(((Word32)(5443 * log_en)) >> 15);
674 /* past_qua_en for mode MR122 */
675 predState->past_qua_en_MR122[0] = log_en;
676 predState->past_qua_en_MR122[1] = log_en;
677 predState->past_qua_en_MR122[2] = log_en;
678 predState->past_qua_en_MR122[3] = log_en;
680 /* make sure that LSP's are ordered */
681 Lsp_lsf(lsp, lsf, M, pOverflow);
682 Reorder_lsf(lsf, LSF_GAP, M, pOverflow);
683 Lsf_lsp(lsf, lsp, M, pOverflow);
685 /* Quantize lsp and put on parameter list */
686 Q_plsf_3(qSt, MRDTX, lsp, lsp_q, st->lsp_index,
687 &st->init_lsf_vq_index, pOverflow);
690 *(*anap)++ = st->init_lsf_vq_index; /* 3 bits */
691 *(*anap)++ = st->lsp_index[0]; /* 8 bits */
692 *(*anap)++ = st->lsp_index[1]; /* 9 bits */
693 *(*anap)++ = st->lsp_index[2]; /* 9 bits */
694 *(*anap)++ = st->log_en_index; /* 6 bits */
699 /****************************************************************************/
703 ------------------------------------------------------------------------------
704 FUNCTION NAME: dtx_buffer
705 ------------------------------------------------------------------------------
706 INPUT AND OUTPUT DEFINITIONS
709 st = pointer to structures of type dtx_encState
710 lsp_new = LSP vector whose elements are of type Word16; vector
712 speech = vector of speech samples of type Word16; vector length is
716 structure pointed to by st contains the new LSPs and logarithmic
720 return_value = 0 (int)
722 Global Variables Used:
725 Local Variables Needed:
728 ------------------------------------------------------------------------------
731 This function handles the DTX buffer.
733 ------------------------------------------------------------------------------
738 ------------------------------------------------------------------------------
741 dtx_enc.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
743 ------------------------------------------------------------------------------
746 int dtx_buffer(dtx_encState *st, // i/o : State struct
747 Word16 lsp_new[], // i : LSP vector
748 Word16 speech[] // i : speech samples
757 // update pointer to circular buffer
758 st->hist_ptr = add(st->hist_ptr, 1);
759 if (sub(st->hist_ptr, DTX_HIST_SIZE) == 0)
764 // copy lsp vector into buffer
765 Copy(lsp_new, &st->lsp_hist[st->hist_ptr * M], M);
767 // compute log energy based on frame energy
768 L_frame_en = 0; // Q0
769 for (i=0; i < L_FRAME; i++)
771 L_frame_en = L_mac(L_frame_en, speech[i], speech[i]);
773 Log2(L_frame_en, &log_en_e, &log_en_m);
775 // convert exponent and mantissa to Word16 Q10
776 log_en = shl(log_en_e, 10); // Q10
777 log_en = add(log_en, shr(log_en_m, 15-10));
779 // divide with L_FRAME i.e subtract with log2(L_FRAME) = 7.32193
780 log_en = sub(log_en, 8521);
782 // insert into log energy buffer with division by 2
783 log_en = shr(log_en, 1);
784 st->log_en_hist[st->hist_ptr] = log_en; // Q10
789 ------------------------------------------------------------------------------
791 [State any special notes, constraints or cautions for users of this function]
793 ------------------------------------------------------------------------------
796 void dtx_buffer(dtx_encState *st, /* i/o : State struct */
797 Word16 lsp_new[], /* i : LSP vector */
798 Word16 speech[], /* i : speech samples */
799 Flag *pOverflow /* i/o : overflow indicator */
809 Word16 *p_speech = &speech[0];
811 /* update pointer to circular buffer */
814 if (st->hist_ptr == DTX_HIST_SIZE)
819 /* copy lsp vector into buffer */
820 oscl_memcpy(&st->lsp_hist[st->hist_ptr * M], lsp_new, M*sizeof(Word16));
822 /* compute log energy based on frame energy */
823 L_frame_en = 0; /* Q0 */
825 for (i = L_FRAME; i != 0; i--)
827 L_frame_en += (((Word32) * p_speech) * *(p_speech)) << 1;
836 Log2(L_frame_en, &log_en_e, &log_en_m, pOverflow);
838 /* convert exponent and mantissa to Word16 Q10 */
840 L_temp = ((Word32) log_en_e) << 10;
841 if (L_temp != (Word32)((Word16) L_temp))
844 log_en = (log_en_e > 0) ? MAX_16 : MIN_16;
848 log_en = (Word16) L_temp;
851 log_en += log_en_m >> (15 - 10);
853 /* divide with L_FRAME i.e subtract with log2(L_FRAME) = 7.32193 */
856 /* insert into log energy buffer with division by 2 */
858 st->log_en_hist[st->hist_ptr] = log_en >> 1; /* Q10 */
862 /****************************************************************************/
865 ------------------------------------------------------------------------------
866 FUNCTION NAME: tx_dtx_handler
867 ------------------------------------------------------------------------------
868 INPUT AND OUTPUT DEFINITIONS
871 st = pointer to structures of type dtx_encState
872 vad_flag = VAD decision flag of type Word16
873 usedMode = pointer to the currently used mode of type enum Mode
876 structure pointed to by st contains the newly calculated speech
880 compute_new_sid_possible = flag to indicate a change in the
881 used mode; store type is Word16
883 Global Variables Used:
886 Local Variables Needed:
889 ------------------------------------------------------------------------------
892 This function adds extra speech hangover to analyze speech on the decoding
895 ------------------------------------------------------------------------------
900 ------------------------------------------------------------------------------
903 dtx_enc.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
905 ------------------------------------------------------------------------------
908 Word16 tx_dtx_handler(dtx_encState *st, // i/o : State struct
909 Word16 vad_flag, // i : vad decision
910 enum Mode *usedMode // i/o : mode changed or not
913 Word16 compute_new_sid_possible;
915 // this state machine is in synch with the GSMEFR txDtx machine
916 st->decAnaElapsedCount = add(st->decAnaElapsedCount, 1);
918 compute_new_sid_possible = 0;
922 st->dtxHangoverCount = DTX_HANG_CONST;
926 if (st->dtxHangoverCount == 0)
927 { // out of decoder analysis hangover
928 st->decAnaElapsedCount = 0;
930 compute_new_sid_possible = 1;
933 { // in possible analysis hangover
934 st->dtxHangoverCount = sub(st->dtxHangoverCount, 1);
936 // decAnaElapsedCount + dtxHangoverCount < DTX_ELAPSED_FRAMES_THRESH
937 if (sub(add(st->decAnaElapsedCount, st->dtxHangoverCount),
938 DTX_ELAPSED_FRAMES_THRESH) < 0)
941 // if short time since decoder update, do not add extra HO
944 // override VAD and stay in
945 // speech mode *usedMode
946 // and add extra hangover
950 return compute_new_sid_possible;
953 ------------------------------------------------------------------------------
955 [State any special notes, constraints or cautions for users of this function]
957 ------------------------------------------------------------------------------
960 Word16 tx_dtx_handler(dtx_encState *st, /* i/o : State struct */
961 Word16 vad_flag, /* i : vad decision */
962 enum Mode *usedMode, /* i/o : mode changed or not */
963 Flag *pOverflow /* i/o : overflow indicator */
966 Word16 compute_new_sid_possible;
969 /* this state machine is in synch with the GSMEFR txDtx machine */
970 st->decAnaElapsedCount = add_16(st->decAnaElapsedCount, 1, pOverflow);
972 compute_new_sid_possible = 0;
976 st->dtxHangoverCount = DTX_HANG_CONST;
980 if (st->dtxHangoverCount == 0)
981 { /* out of decoder analysis hangover */
982 st->decAnaElapsedCount = 0;
984 compute_new_sid_possible = 1;
987 { /* in possible analysis hangover */
988 st->dtxHangoverCount -= 1;
990 /* decAnaElapsedCount + dtxHangoverCount < */
991 /* DTX_ELAPSED_FRAMES_THRESH */
992 count = add_16(st->decAnaElapsedCount, st->dtxHangoverCount,
994 if (count < DTX_ELAPSED_FRAMES_THRESH)
997 /* if short time since decoder update, */
998 /* do not add extra HO */
1003 return(compute_new_sid_possible);