diff options
author | Michael Giacomelli <giac2000@hotmail.com> | 2012-05-06 19:59:46 -0400 |
---|---|---|
committer | Michael Giacomelli <giac2000@hotmail.com> | 2012-05-20 01:05:47 -0400 |
commit | f358228ea1bc66804e9ea12b65c2593c6c1fe8ee (patch) | |
tree | 70b1c2dd09b231aeca6f2d9cc19f8e7b135bc542 | |
parent | 8b3d389f022c19e6be7cb8f6ae646c3c8aa3097f (diff) | |
download | rockbox-f358228ea1bc66804e9ea12b65c2593c6c1fe8ee.tar.gz rockbox-f358228ea1bc66804e9ea12b65c2593c6c1fe8ee.zip |
Work in progress hermite resampler.
Based on http://src.gnu-darwin.org/ports/multimedia/helixplayer/work/hxplay-1.0.7/audio/resampler/hermite.c
Change-Id: Id87565a060aa2383701e7c2f3ea023c7555ad9ef
-rwxr-xr-x[-rw-r--r--] | lib/rbcodec/dsp/lin_resample.c | 130 |
1 files changed, 124 insertions, 6 deletions
diff --git a/lib/rbcodec/dsp/lin_resample.c b/lib/rbcodec/dsp/lin_resample.c index 34dc35b2dd..a7f3f3842b 100644..100755 --- a/lib/rbcodec/dsp/lin_resample.c +++ b/lib/rbcodec/dsp/lin_resample.c | |||
@@ -30,8 +30,10 @@ | |||
30 | * Linear interpolation resampling that introduces a one sample delay because | 30 | * Linear interpolation resampling that introduces a one sample delay because |
31 | * of our inability to look into the future at the end of a frame. | 31 | * of our inability to look into the future at the end of a frame. |
32 | */ | 32 | */ |
33 | |||
34 | #define HERMITE 1 | ||
33 | 35 | ||
34 | #if 0 /* Set to '1' to enable debug messages */ | 36 | #if 1 /* Set to '1' to enable debug messages */ |
35 | #include <debug.h> | 37 | #include <debug.h> |
36 | #else | 38 | #else |
37 | #undef DEBUGF | 39 | #undef DEBUGF |
@@ -46,7 +48,7 @@ static int32_t resample_out_bufs[3][RESAMPLE_BUF_COUNT] IBSS_ATTR; | |||
46 | /* Data for each resampler on each DSP */ | 48 | /* Data for each resampler on each DSP */ |
47 | static struct resample_data | 49 | static struct resample_data |
48 | { | 50 | { |
49 | uint32_t delta; /* 00h: Phase delta for each step */ | 51 | uint32_t delta; /* 00h: Phase delta for each step in s15.16*/ |
50 | uint32_t phase; /* 04h: Current phase [pos16|frac16] */ | 52 | uint32_t phase; /* 04h: Current phase [pos16|frac16] */ |
51 | int32_t last_sample[2]; /* 08h: Last samples for interpolation (L+R) */ | 53 | int32_t last_sample[2]; /* 08h: Last samples for interpolation (L+R) */ |
52 | /* 10h */ | 54 | /* 10h */ |
@@ -54,12 +56,20 @@ static struct resample_data | |||
54 | struct dsp_config *dsp; /* The DSP for this resampler */ | 56 | struct dsp_config *dsp; /* The DSP for this resampler */ |
55 | struct dsp_buffer resample_buf; /* Buffer descriptor for resampled data */ | 57 | struct dsp_buffer resample_buf; /* Buffer descriptor for resampled data */ |
56 | int32_t *resample_buf_arr[2]; /* Actual output data pointers */ | 58 | int32_t *resample_buf_arr[2]; /* Actual output data pointers */ |
59 | |||
60 | /*Hermite Resampler*/ | ||
61 | |||
62 | int32_t last_samples[6]; | ||
63 | |||
57 | } resample_data[DSP_COUNT] IBSS_ATTR; | 64 | } resample_data[DSP_COUNT] IBSS_ATTR; |
58 | 65 | ||
59 | /* Actual worker function. Implemented here or in target assembly code. */ | 66 | /* Actual worker function. Implemented here or in target assembly code. */ |
60 | int lin_resample_resample(struct resample_data *data, struct dsp_buffer *src, | 67 | int lin_resample_resample(struct resample_data *data, struct dsp_buffer *src, |
61 | struct dsp_buffer *dst); | 68 | struct dsp_buffer *dst); |
62 | 69 | ||
70 | int hermite_resample_resample(struct resample_data *data, struct dsp_buffer *src, | ||
71 | struct dsp_buffer *dst); | ||
72 | |||
63 | static void lin_resample_flush_data(struct resample_data *data) | 73 | static void lin_resample_flush_data(struct resample_data *data) |
64 | { | 74 | { |
65 | data->phase = 0; | 75 | data->phase = 0; |
@@ -94,6 +104,101 @@ static bool lin_resample_new_delta(struct resample_data *data, | |||
94 | return true; | 104 | return true; |
95 | } | 105 | } |
96 | 106 | ||
107 | |||
108 | |||
109 | int hermite_resample_resample(struct resample_data *data, struct dsp_buffer *src, | ||
110 | struct dsp_buffer *dst) | ||
111 | { | ||
112 | int ch = src->format.num_channels - 1; | ||
113 | uint32_t count = MIN(src->remcount, 0x8000); | ||
114 | uint32_t delta = data->delta; | ||
115 | uint32_t phase, pos; | ||
116 | int32_t *d; | ||
117 | int x0, x1, x2, x3, frac, acc0; | ||
118 | //DEBUGF("hermite_resample_resample top\n"); | ||
119 | /* restore state */ | ||
120 | |||
121 | |||
122 | //DEBUGF("count: %d delta: %d, phase: %d (%d)\n",count, delta,phase >> 16, phase); | ||
123 | do | ||
124 | { | ||
125 | const int32_t *s = src->p32[ch]; | ||
126 | |||
127 | d = dst->p32[ch]; | ||
128 | int32_t *dmax = d + dst->bufcount; | ||
129 | |||
130 | phase = data->phase; | ||
131 | pos = phase >> 16; | ||
132 | pos = MIN(pos, count); | ||
133 | |||
134 | int32_t last = pos > 0 ? s[pos - 1] : data->last_sample[ch]; | ||
135 | |||
136 | if (pos < count) | ||
137 | { | ||
138 | while (1) | ||
139 | { | ||
140 | |||
141 | int i = pos; | ||
142 | if (i < 3) { | ||
143 | x3 = (i < 3 ? data->last_samples[i+0] : s[i-3]) ; | ||
144 | x2 = (i < 2 ? data->last_samples[i+1] : s[i-2]) ; | ||
145 | x1 = (i < 1 ? data->last_samples[i+2] : s[i-1]) ; | ||
146 | } else { | ||
147 | x3 = s[i-3] ; | ||
148 | x2 = s[i-2] ; | ||
149 | x1 = s[i-1] ; | ||
150 | } | ||
151 | x0 = s[i] ; | ||
152 | //frac = f >> 1; | ||
153 | frac=(0x0000FFFF&phase) << 15; | ||
154 | //DEBUGF("pos: %d phase: %d frac: %d\n",pos, phase, frac); | ||
155 | |||
156 | /* 4-tap Hermite, using Farrow structure */ | ||
157 | acc0 = (3 * (x2 - x1) + x0 - x3) >> 1; | ||
158 | acc0 = FRACMUL(acc0, frac); | ||
159 | acc0 += 2 * x1 + x3 - ((5 * x2 + x0) >> 1); | ||
160 | acc0 = FRACMUL(acc0, frac); | ||
161 | acc0 += (x1 - x3) >> 1; | ||
162 | acc0 = FRACMUL(acc0, frac); | ||
163 | acc0 += x2; | ||
164 | |||
165 | |||
166 | *d++ = acc0; | ||
167 | |||
168 | phase += delta; | ||
169 | pos = phase >> 16; | ||
170 | |||
171 | if (pos >= count || d >= dmax) | ||
172 | break; | ||
173 | |||
174 | // if (pos > 0){ | ||
175 | // /* save delay samples for next time (last_samples[0] = oldest, last_samples[2] = newest) */ | ||
176 | // data->last_samples[ch*3+0] = (pos < 3 ? data->last_samples[pos+0] : s[pos-3]); | ||
177 | // data->last_samples[ch*3+1] = (pos < 2 ? data->last_samples[pos+1] : s[pos-2]); | ||
178 | // data->last_samples[ch*3+2] = (pos < 1 ? data->last_samples[pos+2] : s[pos-1]); | ||
179 | // } | ||
180 | } | ||
181 | |||
182 | //if (pos > 0) | ||
183 | //{ | ||
184 | pos = MIN(pos, count); | ||
185 | data->last_samples[ch*3+0] = (pos < 3 ? data->last_samples[pos+0] : s[pos-3]); | ||
186 | data->last_samples[ch*3+1] = (pos < 2 ? data->last_samples[pos+1] : s[pos-2]); | ||
187 | data->last_samples[ch*3+2] = (pos < 1 ? data->last_samples[pos+2] : s[pos-1]); | ||
188 | //} | ||
189 | } | ||
190 | |||
191 | } | ||
192 | while (--ch >= 0); | ||
193 | |||
194 | |||
195 | /* Wrap phase accumulator back to start of next frame. */ | ||
196 | data->phase = phase - (pos << 16); | ||
197 | |||
198 | dst->remcount = d - dst->p32[0]; | ||
199 | return pos; | ||
200 | } | ||
201 | |||
97 | #if !defined(CPU_COLDFIRE) && !defined(CPU_ARM) | 202 | #if !defined(CPU_COLDFIRE) && !defined(CPU_ARM) |
98 | /* Where the real work is done */ | 203 | /* Where the real work is done */ |
99 | int lin_resample_resample(struct resample_data *data, struct dsp_buffer *src, | 204 | int lin_resample_resample(struct resample_data *data, struct dsp_buffer *src, |
@@ -104,7 +209,7 @@ int lin_resample_resample(struct resample_data *data, struct dsp_buffer *src, | |||
104 | uint32_t delta = data->delta; | 209 | uint32_t delta = data->delta; |
105 | uint32_t phase, pos; | 210 | uint32_t phase, pos; |
106 | int32_t *d; | 211 | int32_t *d; |
107 | 212 | DEBUGF("count: %d delta: %d, phase: %d (%d)\n",count, delta,phase >> 16, phase); | |
108 | do | 213 | do |
109 | { | 214 | { |
110 | const int32_t *s = src->p32[ch]; | 215 | const int32_t *s = src->p32[ch]; |
@@ -122,6 +227,7 @@ int lin_resample_resample(struct resample_data *data, struct dsp_buffer *src, | |||
122 | { | 227 | { |
123 | while (1) | 228 | while (1) |
124 | { | 229 | { |
230 | DEBUGF("phase: %d frac: %d\n", phase, (phase & 0xffff) << 15); | ||
125 | *d++ = last + FRACMUL((phase & 0xffff) << 15, s[pos] - last); | 231 | *d++ = last + FRACMUL((phase & 0xffff) << 15, s[pos] - last); |
126 | phase += delta; | 232 | phase += delta; |
127 | pos = phase >> 16; | 233 | pos = phase >> 16; |
@@ -139,16 +245,17 @@ int lin_resample_resample(struct resample_data *data, struct dsp_buffer *src, | |||
139 | last = s[pos - 1]; | 245 | last = s[pos - 1]; |
140 | } | 246 | } |
141 | } | 247 | } |
142 | 248 | ||
249 | DEBUGF("pos: %d count: %d\n", pos, count); | ||
143 | data->last_sample[ch] = last; | 250 | data->last_sample[ch] = last; |
144 | } | 251 | } |
145 | while (--ch >= 0); | 252 | while (--ch >= 0); |
146 | 253 | ||
147 | /* Wrap phase accumulator back to start of next frame. */ | 254 | /* Wrap phase accumulator back to start of next frame. */ |
148 | data->phase = phase - (pos << 16); | 255 | data->phase = phase - (pos << 16); |
149 | 256 | ||
150 | dst->remcount = d - dst->p32[0]; | 257 | dst->remcount = d - dst->p32[0]; |
151 | 258 | DEBUGF("remcount: %d, pos %d\n", dst->remcount, pos); | |
152 | return pos; | 259 | return pos; |
153 | } | 260 | } |
154 | #endif /* CPU */ | 261 | #endif /* CPU */ |
@@ -176,7 +283,12 @@ static void lin_resample_process(struct dsp_proc_entry *this, | |||
176 | { | 283 | { |
177 | dst->bufcount = RESAMPLE_BUF_COUNT; | 284 | dst->bufcount = RESAMPLE_BUF_COUNT; |
178 | 285 | ||
286 | #if HERMITE | ||
287 | int consumed = hermite_resample_resample(data, src, dst); | ||
288 | #else | ||
179 | int consumed = lin_resample_resample(data, src, dst); | 289 | int consumed = lin_resample_resample(data, src, dst); |
290 | #endif | ||
291 | |||
180 | 292 | ||
181 | /* Advance src by consumed amount */ | 293 | /* Advance src by consumed amount */ |
182 | if (consumed > 0) | 294 | if (consumed > 0) |
@@ -211,7 +323,13 @@ static void lin_resample_new_format(struct dsp_proc_entry *this, | |||
211 | if (src->format.frequency != frequency) | 323 | if (src->format.frequency != frequency) |
212 | { | 324 | { |
213 | DEBUGF(" DSP_PROC_RESAMPLE- new delta\n"); | 325 | DEBUGF(" DSP_PROC_RESAMPLE- new delta\n"); |
326 | DEBUGF("hermite_resample_new_delta in\n"); | ||
327 | #if 0 | ||
328 | active = hermite_resample_new_delta(data, src); | ||
329 | #else | ||
214 | active = lin_resample_new_delta(data, src); | 330 | active = lin_resample_new_delta(data, src); |
331 | #endif | ||
332 | DEBUGF("hermite_resample_new_delta out\n"); | ||
215 | dsp_proc_activate(dsp, DSP_PROC_RESAMPLE, active); | 333 | dsp_proc_activate(dsp, DSP_PROC_RESAMPLE, active); |
216 | } | 334 | } |
217 | 335 | ||