1e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard/*
2e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard ** Copyright 2003-2010, VisualOn, Inc.
3e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard **
4e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard ** Licensed under the Apache License, Version 2.0 (the "License");
5e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard ** you may not use this file except in compliance with the License.
6e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard ** You may obtain a copy of the License at
7e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard **
8e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard **     http://www.apache.org/licenses/LICENSE-2.0
9e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard **
10e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard ** Unless required by applicable law or agreed to in writing, software
11e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard ** distributed under the License is distributed on an "AS IS" BASIS,
12e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard ** See the License for the specific language governing permissions and
14e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard ** limitations under the License.
15e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard */
16e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard/*******************************************************************************
17e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard	File:		pre_echo_control.c
18e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard
19e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard	Content:	Pre echo control functions
20e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard
21956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong*******************************************************************************/
22956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong
23e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard#include "basic_op.h"
24e2e838afcf03e603a41a0455846eaf9614537c16Mans Rullgard#include "oper_32b.h"
25956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong
26956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong#include "oper_32b.h"
27956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong#include "pre_echo_control.h"
28956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong
29956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong
30956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong/*****************************************************************************
31956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong*
32b676a05348e4c516fa8b57e33b10548e6142c3f8Mans Rullgard* function name:InitPreEchoControl
33956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong* description: init pre echo control parameter
34956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong*
35956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong*****************************************************************************/
36956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dongvoid InitPreEchoControl(Word32 *pbThresholdNm1,
37956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong                        Word16  numPb,
38956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong                        Word32 *pbThresholdQuiet)
39956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong{
40956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong  Word16 pb;
41956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong
42956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong  for(pb=0; pb<numPb; pb++) {
43b676a05348e4c516fa8b57e33b10548e6142c3f8Mans Rullgard    pbThresholdNm1[pb] = pbThresholdQuiet[pb];
44956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong  }
45956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong}
46956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong
47956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong/*****************************************************************************
48956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong*
49b676a05348e4c516fa8b57e33b10548e6142c3f8Mans Rullgard* function name:PreEchoControl
50956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong* description: update shreshold to avoid pre echo
51956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong*			   thr(n) = max(rpmin*thrq(n), min(thrq(n), rpelev*thrq1(n)))
52956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong*
53956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong*
54956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong*****************************************************************************/
55956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dongvoid PreEchoControl(Word32 *pbThresholdNm1,
56956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong                    Word16  numPb,
57956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong                    Word32  maxAllowedIncreaseFactor,
58956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong                    Word16  minRemainingThresholdFactor,
59956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong                    Word32 *pbThreshold,
60956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong                    Word16  mdctScale,
61956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong                    Word16  mdctScalenm1)
62956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong{
63956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong  Word32 i;
64956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong  Word32 tmpThreshold1, tmpThreshold2;
65956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong  Word32 scaling;
66956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong
67956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong  /* maxAllowedIncreaseFactor is hard coded to 2 */
68956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong  (void)maxAllowedIncreaseFactor;
69956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong
70956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong  scaling = ((mdctScale - mdctScalenm1) << 1);
71b676a05348e4c516fa8b57e33b10548e6142c3f8Mans Rullgard
72956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong  if ( scaling > 0 ) {
73956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong    for(i = 0; i < numPb; i++) {
74956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong      tmpThreshold1 = pbThresholdNm1[i] >> (scaling-1);
75956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong      tmpThreshold2 = L_mpy_ls(pbThreshold[i], minRemainingThresholdFactor);
76956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong
77956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong      /* copy thresholds to internal memory */
78b676a05348e4c516fa8b57e33b10548e6142c3f8Mans Rullgard      pbThresholdNm1[i] = pbThreshold[i];
79b676a05348e4c516fa8b57e33b10548e6142c3f8Mans Rullgard
80956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong
81956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong      if(pbThreshold[i] > tmpThreshold1) {
82b676a05348e4c516fa8b57e33b10548e6142c3f8Mans Rullgard        pbThreshold[i] = tmpThreshold1;
83956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong      }
84b676a05348e4c516fa8b57e33b10548e6142c3f8Mans Rullgard
85956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong      if(tmpThreshold2 > pbThreshold[i]) {
86b676a05348e4c516fa8b57e33b10548e6142c3f8Mans Rullgard        pbThreshold[i] = tmpThreshold2;
87956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong      }
88956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong
89956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong    }
90956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong  }
91956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong  else {
92956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong    scaling = -scaling;
93956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong    for(i = 0; i < numPb; i++) {
94956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong
95956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong      tmpThreshold1 = pbThresholdNm1[i] << 1;
96956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong      tmpThreshold2 = L_mpy_ls(pbThreshold[i], minRemainingThresholdFactor);
97956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong
98956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong      /* copy thresholds to internal memory */
99b676a05348e4c516fa8b57e33b10548e6142c3f8Mans Rullgard      pbThresholdNm1[i] = pbThreshold[i];
100b676a05348e4c516fa8b57e33b10548e6142c3f8Mans Rullgard
101956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong
102956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong      if(((pbThreshold[i] >> scaling) > tmpThreshold1)) {
103956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong        pbThreshold[i] = tmpThreshold1 << scaling;
104956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong      }
105b676a05348e4c516fa8b57e33b10548e6142c3f8Mans Rullgard
106956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong      if(tmpThreshold2 > pbThreshold[i]) {
107b676a05348e4c516fa8b57e33b10548e6142c3f8Mans Rullgard        pbThreshold[i] = tmpThreshold2;
108956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong      }
109956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong
110956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong    }
111956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong  }
112956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong}
113956c553ab0ce72f8074ad0fda2ffd66a0305700cJames Dong
114