2 * FreeRDP: A Remote Desktop Protocol client.
3 * Digital Sound Processing
5 * Copyright 2010-2011 Vic Lee
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
23 #include <freerdp/types.h>
24 #include <freerdp/utils/memory.h>
25 #include <freerdp/utils/dsp.h>
27 uint8* dsp_resample(uint8* src, int bytes_per_sample,
28 uint32 schan, uint32 srate, int sframes,
29 uint32 rchan, uint32 rrate, int * prframes)
39 sbytes = bytes_per_sample * schan;
40 rbytes = bytes_per_sample * rchan;
41 rframes = sframes * rrate / srate;
43 rsize = rbytes * rframes;
44 dst = (uint8*) xzalloc(rsize);
47 for (i = 0; i < rframes; i++)
49 n1 = i * srate / rrate;
52 n2 = (n1 * rrate == i * srate || n1 == sframes - 1 ? n1 : n1 + 1);
53 for (j = 0; j < rbytes; j++)
55 /* Nearest Interpolation, probably the easiest, but works */
56 *p++ = (i * srate - n1 * rrate > n2 * rrate - i * srate ?
57 src[n2 * sbytes + (j % sbytes)] :
58 src[n1 * sbytes + (j % sbytes)]);
66 * Microsoft IMA ADPCM specification:
68 * http://wiki.multimedia.cx/index.php?title=Microsoft_IMA_ADPCM
69 * http://wiki.multimedia.cx/index.php?title=IMA_ADPCM
72 static const sint16 ima_step_index_table[] =
74 -1, -1, -1, -1, 2, 4, 6, 8,
75 -1, -1, -1, -1, 2, 4, 6, 8
78 static const sint16 ima_step_size_table[] =
80 7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
81 19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
82 50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
83 130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
84 337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
85 876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
86 2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
87 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
88 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
91 static uint16 dsp_decode_ima_adpcm_sample(ADPCM* adpcm,
92 int channel, uint8 sample)
97 ss = ima_step_size_table[adpcm->last_step[channel]];
107 d += adpcm->last_sample[channel];
114 adpcm->last_sample[channel] = (sint16) d;
116 adpcm->last_step[channel] += ima_step_index_table[sample];
117 if (adpcm->last_step[channel] < 0)
118 adpcm->last_step[channel] = 0;
119 else if (adpcm->last_step[channel] > 88)
120 adpcm->last_step[channel] = 88;
125 uint8* dsp_decode_ima_adpcm(ADPCM* adpcm,
126 uint8* src, int size, int channels, int block_size, int* out_size)
135 *out_size = size * 4;
136 out = (uint8 *) xzalloc(*out_size);
140 if (size % block_size == 0)
142 adpcm->last_sample[0] = (sint16) (((uint16)(*src)) | (((uint16)(*(src + 1))) << 8));
143 adpcm->last_step[0] = (sint16) (*(src + 2));
149 adpcm->last_sample[1] = (sint16) (((uint16)(*src)) | (((uint16)(*(src + 1))) << 8));
150 adpcm->last_step[1] = (sint16) (*(src + 2));
159 for (i = 0; i < 8; i++)
161 channel = (i < 4 ? 0 : 1);
162 sample = ((*src) & 0x0f);
163 decoded = dsp_decode_ima_adpcm_sample(adpcm, channel, sample);
164 dst[((i & 3) << 3) + (channel << 1)] = (decoded & 0xff);
165 dst[((i & 3) << 3) + (channel << 1) + 1] = (decoded >> 8);
166 sample = ((*src) >> 4);
167 decoded = dsp_decode_ima_adpcm_sample(adpcm, channel, sample);
168 dst[((i & 3) << 3) + (channel << 1) + 4] = (decoded & 0xff);
169 dst[((i & 3) << 3) + (channel << 1) + 5] = (decoded >> 8);
177 sample = ((*src) & 0x0f);
178 decoded = dsp_decode_ima_adpcm_sample(adpcm, 0, sample);
179 *dst++ = (decoded & 0xff);
180 *dst++ = (decoded >> 8);
181 sample = ((*src) >> 4);
182 decoded = dsp_decode_ima_adpcm_sample(adpcm, 0, sample);
183 *dst++ = (decoded & 0xff);
184 *dst++ = (decoded >> 8);
194 * 2 0 6 4 10 8 14 12 <left>
197 * 3 1 7 5 11 9 15 13 <right>
203 } ima_stereo_encode_map[] =
223 static uint8 dsp_encode_ima_adpcm_sample(ADPCM* adpcm,
224 int channel, sint16 sample)
232 ss = ima_step_size_table[adpcm->last_step[channel]];
233 d = e = sample - adpcm->last_sample[channel];
264 diff += adpcm->last_sample[channel];
267 else if (diff > 32767)
269 adpcm->last_sample[channel] = (sint16) diff;
271 adpcm->last_step[channel] += ima_step_index_table[enc];
272 if (adpcm->last_step[channel] < 0)
273 adpcm->last_step[channel] = 0;
274 else if (adpcm->last_step[channel] > 88)
275 adpcm->last_step[channel] = 88;
280 uint8* dsp_encode_ima_adpcm(ADPCM* adpcm,
281 uint8* src, int size, int channels, int block_size, int* out_size)
289 out = (uint8*) xzalloc(size / 2);
293 if ((dst - out) % block_size == 0)
295 *dst++ = adpcm->last_sample[0] & 0xff;
296 *dst++ = (adpcm->last_sample[0] >> 8) & 0xff;
297 *dst++ = (uint8) adpcm->last_step[0];
301 *dst++ = adpcm->last_sample[1] & 0xff;
302 *dst++ = (adpcm->last_sample[1] >> 8) & 0xff;
303 *dst++ = (uint8) adpcm->last_step[1];
311 for (i = 0; i < 16; i++)
313 sample = (sint16) (((uint16)(*src)) | (((uint16)(*(src + 1))) << 8));
315 encoded = dsp_encode_ima_adpcm_sample(adpcm, i % 2, sample);
316 dst[ima_stereo_encode_map[i].byte_num] |= encoded << ima_stereo_encode_map[i].byte_shift;
323 sample = (sint16) (((uint16)(*src)) | (((uint16)(*(src + 1))) << 8));
325 encoded = dsp_encode_ima_adpcm_sample(adpcm, 0, sample);
326 sample = (sint16) (((uint16)(*src)) | (((uint16)(*(src + 1))) << 8));
328 encoded |= dsp_encode_ima_adpcm_sample(adpcm, 0, sample) << 4;
333 *out_size = dst - out;