hc
2023-02-13 e440ec23c5a540cdd3f7464e8779219be6fd3d95
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
From 200cb3393dbdbb14847cb9b6a96a6f823ae93f50 Mon Sep 17 00:00:00 2001
From: Yu YongZhen <yuyz@rock-chips.com>
Date: Fri, 11 May 2018 14:45:40 +0800
Subject: [PATCH] add dcbfilter 16bit process
 
---
 src/pcm/pcm.c | 168 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 167 insertions(+), 1 deletion(-)
 
diff --git a/src/pcm/pcm.c b/src/pcm/pcm.c
index fc7bd52..9c90afb 100644
--- a/src/pcm/pcm.c
+++ b/src/pcm/pcm.c
@@ -664,6 +664,167 @@ playback devices.
               P_STATE(PAUSED) | \
               P_STATE(DRAINING))
 
+//#define DCB_FILTER_16BIT
+#ifdef DCB_FILTER_16BIT
+
+#define A1 32511 // (1-2^(-7))     Q32:1.31 // 32752=>0.99951171875
+
+#define TO_16BIT_SHIFT 15
+#define MAX_Uint_PCMBIT_SIZE 4294967296
+#define MAX_UNSIGN_PCMBIT_SIZE 65536
+#define MAX_SIGN_POS_PCMBIT_SIZE 32768
+#define MAX_SIGN_NEG_PCMBIT_SIZE -32768
+
+/* static variables for previous values */
+/* left 1 */
+static int16_t x_prev_l1=0;
+static int16_t   y_prev_l1=0;
+/* right 1 */
+static int16_t x_prev_r1=0;
+static int16_t   y_prev_r1=0;
+/* left 2 */
+static int16_t x_prev_l2=0;
+static int16_t   y_prev_l2=0;
+/* right 1 */
+static int16_t x_prev_r2=0;
+static int16_t   y_prev_r2=0;
+/* left 1 */
+static int16_t x_prev_l3=0;
+static int16_t   y_prev_l3=0;
+/* right 1 */
+static int16_t x_prev_r3=0;
+static int16_t   y_prev_r3=0;
+
+void dc_filter_left1(int16_t *pcmIn)
+{
+    int16_t sampleIn, delta_x, sampleOut;
+    int16_t   a1_y_prev;
+
+    sampleIn = *pcmIn;
+    delta_x = sampleIn-x_prev_l1;
+    a1_y_prev = A1*y_prev_l1/MAX_SIGN_POS_PCMBIT_SIZE;
+    sampleOut = delta_x+a1_y_prev;
+
+    x_prev_l1 = sampleIn;
+    y_prev_l1 = sampleOut;
+
+    *pcmIn = sampleOut;
+}
+void dc_filter_right1(int16_t *pcmIn)
+{
+    int16_t sampleIn, delta_x, sampleOut;
+    int16_t a1_y_prev;
+
+    sampleIn = *pcmIn;
+    delta_x = sampleIn-x_prev_r1;
+    a1_y_prev = A1*y_prev_r1/MAX_SIGN_POS_PCMBIT_SIZE;
+    sampleOut = delta_x+a1_y_prev;
+
+    x_prev_r1 = sampleIn;
+    y_prev_r1 = sampleOut;
+
+    *pcmIn = sampleOut;
+}
+void dc_filter_left2(int16_t *pcmIn)
+{
+    int16_t sampleIn, delta_x, sampleOut;
+    int16_t a1_y_prev;
+
+    sampleIn = *pcmIn;
+    delta_x = sampleIn-x_prev_l2;
+    a1_y_prev = A1*y_prev_l2/MAX_SIGN_POS_PCMBIT_SIZE;
+    sampleOut = delta_x+a1_y_prev;
+
+    x_prev_l2 = sampleIn;
+    y_prev_l2 = sampleOut;
+
+    *pcmIn = sampleOut;
+}
+
+void dc_filter_right2(int16_t *pcmIn)
+{
+    int16_t sampleIn, delta_x, sampleOut;
+    int16_t a1_y_prev;
+
+    sampleIn = *pcmIn;
+    delta_x = sampleIn-x_prev_r2;
+    a1_y_prev = A1*y_prev_r2/MAX_SIGN_POS_PCMBIT_SIZE;
+    sampleOut = delta_x+a1_y_prev;
+
+    x_prev_r2 = sampleIn;
+    y_prev_r2 = sampleOut;
+
+    *pcmIn = sampleOut;
+}
+void dc_filter_left3(int16_t *pcmIn)
+{
+    int16_t sampleIn, delta_x, sampleOut;
+    int16_t a1_y_prev;
+
+    sampleIn = *pcmIn;
+    delta_x = sampleIn-x_prev_l3;
+    a1_y_prev = A1*y_prev_l3/MAX_SIGN_POS_PCMBIT_SIZE;
+    sampleOut = delta_x+a1_y_prev;
+
+    x_prev_l3 = sampleIn;
+    y_prev_l3 = (int16_t)sampleOut;
+
+    *pcmIn = sampleOut;
+}
+
+void dc_filter_right3(int16_t *pcmIn)
+{
+    int16_t sampleIn, delta_x, sampleOut;
+    int16_t a1_y_prev;
+
+    sampleIn = *pcmIn;
+    delta_x = sampleIn-x_prev_r3;
+    a1_y_prev = A1*y_prev_r3/MAX_SIGN_POS_PCMBIT_SIZE;
+    sampleOut = delta_x+a1_y_prev;
+
+    x_prev_r3 = sampleIn;
+    y_prev_r3 = (int16_t)sampleOut;
+
+    *pcmIn = sampleOut;
+}
+
+void DCBDoing(void* pIn, int length, int channels)
+{
+    int i = 0;
+    int16_t * pInBuf  =  (int16_t *)pIn;
+
+    //printf("vicent-DCB_Doing---------------length = %d\n",length);
+
+    for(i = 0; i < length; i ++ ) {
+
+        int curChannel = i % channels;
+
+        switch(curChannel){
+            case 0:
+                dc_filter_left1(pInBuf);
+                break;
+            case 1:
+                dc_filter_right1(pInBuf);
+                break;
+            case 2:
+                dc_filter_left2(pInBuf);
+                break;
+            case 3:
+                dc_filter_right2(pInBuf);
+                break;
+            case 4:
+                dc_filter_left3(pInBuf);
+                break;
+            case 5:
+                dc_filter_right3(pInBuf);
+                break;
+            default:
+                break;
+        }
+        pInBuf++;
+    }
+}
+#endif
 /* check whether the PCM is in the unexpected state */
 static int bad_pcm_state(snd_pcm_t *pcm, unsigned int supported_states)
 {
@@ -1509,7 +1670,12 @@ snd_pcm_sframes_t snd_pcm_readi(snd_pcm_t *pcm, void *buffer, snd_pcm_uframes_t
     }
     if (bad_pcm_state(pcm, P_STATE_RUNNABLE))
         return -EBADFD;
-    return _snd_pcm_readi(pcm, buffer, size);
+
+    snd_pcm_sframes_t tmp = _snd_pcm_readi(pcm, buffer, size);  
+#ifdef DCB_FILTER_16BIT
+    DCBDoing((void*)buffer, tmp * pcm->frame_bits / 8 / sizeof(int16_t), pcm->channels);
+#endif
+    return tmp;
 }
 
 /**
-- 
2.7.4