1/*
2 * Copyright (C) 2004-2010 NXP Software
3 * Copyright (C) 2010 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 *      http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18/**********************************************************************************
19   INCLUDE FILES
20***********************************************************************************/
21
22#include "LVC_Mixer_Private.h"
23#include "LVM_Macros.h"
24
25/**********************************************************************************
26   FUNCTION LVCore_MIXSOFT_1ST_D16C31_WRA
27***********************************************************************************/
28
29void LVC_Core_MixInSoft_D16C31_SAT( LVMixer3_st *ptrInstance,
30                                    const LVM_INT16     *src,
31                                          LVM_INT16     *dst,
32                                          LVM_INT16     n)
33{
34
35    LVM_INT16   OutLoop;
36    LVM_INT16   InLoop;
37    LVM_INT16   CurrentShort;
38    LVM_INT32   ii,jj;
39    Mix_Private_st  *pInstance=(Mix_Private_st *)(ptrInstance->PrivateParams);
40    LVM_INT32   Delta=pInstance->Delta;
41    LVM_INT32   Current=pInstance->Current;
42    LVM_INT32   Target=pInstance->Target;
43    LVM_INT32   Temp;
44
45    InLoop = (LVM_INT16)(n >> 2); /* Process per 4 samples */
46    OutLoop = (LVM_INT16)(n - (InLoop << 2));
47
48    if(Current<Target){
49        if (OutLoop){
50            ADD2_SAT_32x32(Current,Delta,Temp);                                      /* Q31 + Q31 into Q31*/
51            Current=Temp;
52            if (Current > Target)
53                Current = Target;
54
55            CurrentShort = (LVM_INT16)(Current>>16);                                 /* From Q31 to Q15*/
56
57            for (ii = OutLoop; ii != 0; ii--){
58                Temp = ((LVM_INT32)*dst) + (((LVM_INT32)*(src++) * CurrentShort)>>15);      /* Q15 + Q15*Q15>>15 into Q15 */
59                if (Temp > 0x00007FFF)
60                    *dst++ = 0x7FFF;
61                else if (Temp < -0x00008000)
62                    *dst++ = - 0x8000;
63                else
64                    *dst++ = (LVM_INT16)Temp;
65            }
66        }
67
68        for (ii = InLoop; ii != 0; ii--){
69            ADD2_SAT_32x32(Current,Delta,Temp);                                      /* Q31 + Q31 into Q31*/
70            Current=Temp;
71            if (Current > Target)
72                Current = Target;
73
74            CurrentShort = (LVM_INT16)(Current>>16);                                 /* From Q31 to Q15*/
75
76            for (jj = 4; jj!=0 ; jj--){
77                Temp = ((LVM_INT32)*dst) + (((LVM_INT32)*(src++) * CurrentShort)>>15);      /* Q15 + Q15*Q15>>15 into Q15 */
78                if (Temp > 0x00007FFF)
79                    *dst++ = 0x7FFF;
80                else if (Temp < -0x00008000)
81                    *dst++ = - 0x8000;
82                else
83                    *dst++ = (LVM_INT16)Temp;
84            }
85        }
86    }
87    else{
88        if (OutLoop){
89            Current -= Delta;                                                        /* Q31 + Q31 into Q31*/
90            if (Current < Target)
91                Current = Target;
92
93            CurrentShort = (LVM_INT16)(Current>>16);                                 /* From Q31 to Q15*/
94
95            for (ii = OutLoop; ii != 0; ii--){
96                Temp = ((LVM_INT32)*dst) + (((LVM_INT32)*(src++) * CurrentShort)>>15);      /* Q15 + Q15*Q15>>15 into Q15 */
97                if (Temp > 0x00007FFF)
98                    *dst++ = 0x7FFF;
99                else if (Temp < -0x00008000)
100                    *dst++ = - 0x8000;
101                else
102                    *dst++ = (LVM_INT16)Temp;
103            }
104        }
105
106        for (ii = InLoop; ii != 0; ii--){
107            Current -= Delta;                                                        /* Q31 + Q31 into Q31*/
108            if (Current < Target)
109                Current = Target;
110
111            CurrentShort = (LVM_INT16)(Current>>16);                                 /* From Q31 to Q15*/
112
113            for (jj = 4; jj!=0 ; jj--){
114                Temp = ((LVM_INT32)*dst) + (((LVM_INT32)*(src++) * CurrentShort)>>15);      /* Q15 + Q15*Q15>>15 into Q15 */
115                if (Temp > 0x00007FFF)
116                    *dst++ = 0x7FFF;
117                else if (Temp < -0x00008000)
118                    *dst++ = - 0x8000;
119                else
120                    *dst++ = (LVM_INT16)Temp;
121            }
122        }
123    }
124    pInstance->Current=Current;
125}
126
127
128/**********************************************************************************/
129