deemph.c revision 5d5c3a132bb446ac78a37dfaac24a46cacf0dd73
1/*
2 ** Copyright 2003-2010, VisualOn, Inc.
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 express or implied.
13 ** See the License for the specific language governing permissions and
14 ** limitations under the License.
15 */
16
17/***********************************************************************
18*       File: deemph.c                                                 *
19*                                                                      *
20*      Description:filtering through 1/(1-mu z^ -1)                    *
21*                  Deemph2 --> signal is divided by 2                  *
22*                  Deemph_32 --> for 32 bits signal.                   *
23*                                                                      *
24************************************************************************/
25
26#include "typedef.h"
27#include "basic_op.h"
28#include "math_op.h"
29
30void Deemph(
31        Word16 x[],                           /* (i/o)   : input signal overwritten by the output */
32        Word16 mu,                            /* (i) Q15 : deemphasis factor                      */
33        Word16 L,                             /* (i)     : vector size                            */
34        Word16 * mem                          /* (i/o)   : memory (y[-1])                         */
35       )
36{
37    Word32 i;
38    Word32 L_tmp;
39
40    L_tmp = L_deposit_h(x[0]);
41    L_tmp = L_mac(L_tmp, *mem, mu);
42    x[0] = vo_round(L_tmp);
43
44    for (i = 1; i < L; i++)
45    {
46        L_tmp = L_deposit_h(x[i]);
47        L_tmp = L_mac(L_tmp, x[i - 1], mu);
48        x[i] = voround(L_tmp);
49    }
50
51    *mem = x[L - 1];
52
53    return;
54}
55
56
57void Deemph2(
58        Word16 x[],                           /* (i/o)   : input signal overwritten by the output */
59        Word16 mu,                            /* (i) Q15 : deemphasis factor                      */
60        Word16 L,                             /* (i)     : vector size                            */
61        Word16 * mem                          /* (i/o)   : memory (y[-1])                         */
62        )
63{
64    Word32 i;
65    Word32 L_tmp;
66    L_tmp = x[0] << 15;
67    L_tmp += ((*mem) * mu)<<1;
68    x[0] = (L_tmp + 0x8000)>>16;
69    for (i = 1; i < L; i++)
70    {
71        Word32 tmp;
72        L_tmp = x[i] << 15;
73        tmp = (x[i - 1] * mu)<<1;
74        if (tmp > 0 && L_tmp > INT_MAX - tmp) {
75            L_tmp = INT_MAX;
76        } else if (tmp < 0 && L_tmp < INT_MIN - tmp) {
77            L_tmp = INT_MIN;
78        } else {
79            L_tmp += tmp;
80        }
81        if (L_tmp > INT32_MAX - 0x8000) {
82            L_tmp = INT_MAX - 0x8000;
83        }
84        x[i] = (L_tmp + 0x8000)>>16;
85    }
86    *mem = x[L - 1];
87    return;
88}
89
90
91void Deemph_32(
92        Word16 x_hi[],                        /* (i)     : input signal (bit31..16) */
93        Word16 x_lo[],                        /* (i)     : input signal (bit15..4)  */
94        Word16 y[],                           /* (o)     : output signal (x16)      */
95        Word16 mu,                            /* (i) Q15 : deemphasis factor        */
96        Word16 L,                             /* (i)     : vector size              */
97        Word16 * mem                          /* (i/o)   : memory (y[-1])           */
98          )
99{
100    Word16 fac;
101    Word32 i, L_tmp;
102
103    fac = mu >> 1;                                /* Q15 --> Q14 */
104
105    L_tmp = L_deposit_h(x_hi[0]);
106    L_tmp += (x_lo[0] * 8)<<1;
107    L_tmp = (L_tmp << 3);
108    L_tmp += ((*mem) * fac)<<1;
109    L_tmp = (L_tmp << 1);
110    y[0] = (L_tmp + 0x8000)>>16;
111
112    for (i = 1; i < L; i++)
113    {
114        L_tmp = L_deposit_h(x_hi[i]);
115        L_tmp += (x_lo[i] * 8)<<1;
116        L_tmp = (L_tmp << 3);
117        L_tmp += (y[i - 1] * fac)<<1;
118        L_tmp = (L_tmp << 1);
119        y[i] = (L_tmp + 0x8000)>>16;
120    }
121
122    *mem = y[L - 1];
123
124    return;
125}
126
127
128
129