VirtualBox

source: vbox/trunk/src/VBox/RDP/client/rdpsnd_dsp.c@ 53007

最後變更 在這個檔案從53007是 37224,由 vboxsync 提交於 14 年 前

RDP/client: fix OSE

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 10.8 KB
 
1/*
2 rdesktop: A Remote Desktop Protocol client.
3 Sound DSP routines
4 Copyright (C) Michael Gernoth <[email protected]> 2006-2008
5
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
18*/
19
20/*
21 * Oracle GPL Disclaimer: For the avoidance of doubt, except that if any license choice
22 * other than GPL or LGPL is available it will apply instead, Oracle elects to use only
23 * the General Public License version 2 (GPLv2) at this time for any software where
24 * a choice of GPL license versions is made available with the language indicating
25 * that GPLv2 or any later version may be used, or where a choice of which version
26 * of the GPL is applied is otherwise unspecified.
27 */
28
29#include <strings.h>
30
31#include "rdesktop.h"
32#include "rdpsnd.h"
33#include "rdpsnd_dsp.h"
34
35#ifdef HAVE_LIBSAMPLERATE
36#include <samplerate.h>
37
38#define SRC_CONVERTER SRC_SINC_MEDIUM_QUALITY
39#endif
40
41#define MAX_VOLUME 65535
42
43static uint16 softvol_left = MAX_VOLUME;
44static uint16 softvol_right = MAX_VOLUME;
45static uint32 resample_to_srate = 44100;
46static uint16 resample_to_bitspersample = 16;
47static uint16 resample_to_channels = 2;
48#ifdef HAVE_LIBSAMPLERATE
49static SRC_STATE *src_converter = NULL;
50#endif
51
52void
53rdpsnd_dsp_softvol_set(uint16 left, uint16 right)
54{
55 softvol_left = left;
56 softvol_right = right;
57 DEBUG(("rdpsnd_dsp_softvol_set: left: %u, right: %u\n", left, right));
58}
59
60void
61rdpsnd_dsp_softvol(unsigned char *buffer, unsigned int size, RD_WAVEFORMATEX * format)
62{
63 unsigned int factor_left, factor_right;
64 unsigned char *posin = buffer;
65 unsigned char *posout = buffer;
66
67 if ((softvol_left == MAX_VOLUME) && (softvol_right == MAX_VOLUME))
68 return;
69
70 factor_left = (softvol_left * 256) / MAX_VOLUME;
71 factor_right = (softvol_right * 256) / MAX_VOLUME;
72
73 if (format->nChannels == 1)
74 {
75 factor_left = factor_right = (factor_left + factor_right) / 2;
76 }
77
78 if (format->wBitsPerSample == 8)
79 {
80 sint8 val;
81
82 while (posout < buffer + size)
83 {
84 /* Left */
85 val = *posin++;
86 val = (val * factor_left) >> 8;
87 *posout++ = val;
88
89 /* Right */
90 val = *posin++;
91 val = (val * factor_right) >> 8;
92 *posout++ = val;
93 }
94 }
95 else
96 {
97 sint16 val;
98
99 while (posout < buffer + size)
100 {
101 /* Left */
102 val = *posin++;
103 val |= *posin++ << 8;
104 val = (val * factor_left) >> 8;
105 *posout++ = val & 0xff;
106 *posout++ = val >> 8;
107
108 /* Right */
109 val = *posin++;
110 val |= *posin++ << 8;
111 val = (val * factor_right) >> 8;
112 *posout++ = val & 0xff;
113 *posout++ = val >> 8;
114 }
115 }
116
117 DEBUG(("using softvol with factors left: %d, right: %d (%d/%d)\n", factor_left,
118 factor_right, format->wBitsPerSample, format->nChannels));
119}
120
121void
122rdpsnd_dsp_swapbytes(unsigned char *buffer, unsigned int size, RD_WAVEFORMATEX * format)
123{
124 int i;
125 uint8 swap;
126
127 if (format->wBitsPerSample == 8)
128 return;
129
130 if (size & 0x1)
131 warning("badly aligned sound data");
132
133 for (i = 0; i < (int) size; i += 2)
134 {
135 swap = *(buffer + i);
136 *(buffer + i) = *(buffer + i + 1);
137 *(buffer + i + 1) = swap;
138 }
139}
140
141RD_BOOL
142rdpsnd_dsp_resample_set(uint32 device_srate, uint16 device_bitspersample, uint16 device_channels)
143{
144#ifdef HAVE_LIBSAMPLERATE
145 int err;
146#endif
147
148 if (device_bitspersample != 16 && device_bitspersample != 8)
149 return False;
150
151 if (device_channels != 1 && device_channels != 2)
152 return False;
153
154 resample_to_srate = device_srate;
155 resample_to_bitspersample = device_bitspersample;
156 resample_to_channels = device_channels;
157
158#ifdef HAVE_LIBSAMPLERATE
159 if (src_converter != NULL)
160 src_converter = src_delete(src_converter);
161
162 if ((src_converter = src_new(SRC_CONVERTER, device_channels, &err)) == NULL)
163 {
164 warning("src_new failed: %d!\n", err);
165 return False;
166 }
167#endif
168
169 return True;
170}
171
172RD_BOOL
173rdpsnd_dsp_resample_supported(RD_WAVEFORMATEX * format)
174{
175 if (format->wFormatTag != WAVE_FORMAT_PCM)
176 return False;
177 if ((format->nChannels != 1) && (format->nChannels != 2))
178 return False;
179 if ((format->wBitsPerSample != 8) && (format->wBitsPerSample != 16))
180 return False;
181
182 return True;
183}
184
185uint32
186rdpsnd_dsp_resample(unsigned char **out, unsigned char *in, unsigned int size,
187 RD_WAVEFORMATEX * format, RD_BOOL stream_be)
188{
189#ifdef HAVE_LIBSAMPLERATE
190 SRC_DATA resample_data;
191 float *infloat, *outfloat;
192 int err;
193#else
194 int ratio1k = (resample_to_srate * 1000) / format->nSamplesPerSec;
195#endif
196 int innum, outnum;
197 unsigned char *tmpdata = NULL, *tmp = NULL;
198 int samplewidth = format->wBitsPerSample / 8;
199 int outsize = 0;
200 int i;
201
202 if ((resample_to_bitspersample == format->wBitsPerSample) &&
203 (resample_to_channels == format->nChannels) &&
204 (resample_to_srate == format->nSamplesPerSec))
205 return 0;
206
207#ifdef B_ENDIAN
208 if (!stream_be)
209 rdpsnd_dsp_swapbytes(in, size, format);
210#endif
211
212 if (resample_to_channels != format->nChannels)
213 {
214 int newsize = (size / format->nChannels) * resample_to_channels;
215 tmpdata = (unsigned char *) xmalloc(newsize);
216
217 for (i = 0; i < newsize / samplewidth; i++)
218 {
219 if (format->nChannels > resample_to_channels)
220 memcpy(tmpdata + (i * samplewidth),
221 in +
222 (((i * format->nChannels) / resample_to_channels) *
223 samplewidth), samplewidth);
224 else
225 memcpy(tmpdata + (i * samplewidth),
226 in +
227 (((i / resample_to_channels) * format->nChannels +
228 (i % format->nChannels)) * samplewidth), samplewidth);
229
230 }
231
232 in = tmpdata;
233 size = newsize;
234 }
235
236
237 /* Expand 8bit input-samples to 16bit */
238#ifndef HAVE_LIBSAMPLERATE /* libsamplerate needs 16bit samples */
239 if (format->wBitsPerSample != resample_to_bitspersample)
240#endif
241 {
242 /* source: 8 bit, dest: 16bit */
243 if (format->wBitsPerSample == 8)
244 {
245 tmp = tmpdata;
246 tmpdata = (unsigned char *) xmalloc(size * 2);
247 for (i = 0; i < (int) size; i++)
248 {
249 tmpdata[i * 2] = in[i];
250 tmpdata[(i * 2) + 1] = 0x00;
251 }
252 in = tmpdata;
253 samplewidth = 16 / 2;
254 size *= 2;
255
256 if (tmp != NULL)
257 xfree(tmp);
258 }
259 }
260
261 innum = size / samplewidth;
262
263 /* Do the resampling */
264#ifdef HAVE_LIBSAMPLERATE
265 if (src_converter == NULL)
266 {
267 warning("no samplerate converter available!!\n");
268 return 0;
269 }
270
271 outnum = ((float) innum * ((float) resample_to_srate / (float) format->nSamplesPerSec)) + 1;
272
273 infloat = (float *) xmalloc(sizeof(float) * innum);
274 outfloat = (float *) xmalloc(sizeof(float) * outnum);
275
276 src_short_to_float_array((short *) in, infloat, innum);
277
278 bzero(&resample_data, sizeof(resample_data));
279 resample_data.data_in = infloat;
280 resample_data.data_out = outfloat;
281 resample_data.input_frames = innum / resample_to_channels;
282 resample_data.output_frames = outnum / resample_to_channels;
283 resample_data.src_ratio = (double) resample_to_srate / (double) format->nSamplesPerSec;
284 resample_data.end_of_input = 0;
285
286 if ((err = src_process(src_converter, &resample_data)) != 0)
287 error("src_process: %s", src_strerror(err));
288
289 xfree(infloat);
290
291 outsize = resample_data.output_frames_gen * resample_to_channels * samplewidth;
292 *out = (unsigned char *) xmalloc(outsize);
293 src_float_to_short_array(outfloat, (short *) *out,
294 resample_data.output_frames_gen * resample_to_channels);
295 xfree(outfloat);
296
297#else
298 /* Michaels simple linear resampler */
299 if (resample_to_srate < format->nSamplesPerSec)
300 {
301 warning("downsampling currently not supported!\n");
302 return 0;
303 }
304
305 outnum = (innum * ratio1k) / 1000;
306
307 outsize = outnum * samplewidth;
308 *out = (unsigned char *) xmalloc(outsize);
309 bzero(*out, outsize);
310
311 for (i = 0; i < outsize / (resample_to_channels * samplewidth); i++)
312 {
313 int source = (i * 1000) / ratio1k;
314#if 0 /* Partial for linear resampler */
315 int part = (i * 100000) / ratio1k - source * 100;
316#endif
317 int j;
318
319 if (source * resample_to_channels + samplewidth > (int) size)
320 break;
321
322#if 0 /* Linear resampling, TODO: soundquality fixes (LP filter) */
323 if (samplewidth == 1)
324 {
325 sint8 cval1, cval2;
326 for (j = 0; j < resample_to_channels; j++)
327 {
328 memcpy(&cval1,
329 in + (source * resample_to_channels * samplewidth) +
330 (samplewidth * j), samplewidth);
331 memcpy(&cval2,
332 in + ((source + 1) * resample_to_channels * samplewidth) +
333 (samplewidth * j), samplewidth);
334
335 cval1 += (sint8) (cval2 * part) / 100;
336
337 memcpy(*out + (i * resample_to_channels * samplewidth) +
338 (samplewidth * j), &cval1, samplewidth);
339 }
340 }
341 else
342 {
343 sint16 sval1, sval2;
344 for (j = 0; j < resample_to_channels; j++)
345 {
346 memcpy(&sval1,
347 in + (source * resample_to_channels * samplewidth) +
348 (samplewidth * j), samplewidth);
349 memcpy(&sval2,
350 in + ((source + 1) * resample_to_channels * samplewidth) +
351 (samplewidth * j), samplewidth);
352
353 sval1 += (sint16) (sval2 * part) / 100;
354
355 memcpy(*out + (i * resample_to_channels * samplewidth) +
356 (samplewidth * j), &sval1, samplewidth);
357 }
358 }
359#else /* Nearest neighbor search */
360 for (j = 0; j < resample_to_channels; j++)
361 {
362 memcpy(*out + (i * resample_to_channels * samplewidth) + (samplewidth * j),
363 in + (source * resample_to_channels * samplewidth) +
364 (samplewidth * j), samplewidth);
365 }
366#endif
367 }
368 outsize = i * resample_to_channels * samplewidth;
369#endif
370
371 if (tmpdata != NULL)
372 xfree(tmpdata);
373
374 /* Shrink 16bit output-samples to 8bit */
375#ifndef HAVE_LIBSAMPLERATE /* libsamplerate produces 16bit samples */
376 if (format->wBitsPerSample != resample_to_bitspersample)
377#endif
378 {
379 /* source: 16 bit, dest: 8 bit */
380 if (resample_to_bitspersample == 8)
381 {
382 for (i = 0; i < outsize; i++)
383 {
384 *out[i] = *out[i * 2];
385 }
386 outsize /= 2;
387 }
388 }
389
390#ifdef B_ENDIAN
391 if (!stream_be)
392 rdpsnd_dsp_swapbytes(*out, outsize, format);
393#endif
394 return outsize;
395}
396
397STREAM
398rdpsnd_dsp_process(unsigned char *data, unsigned int size, struct audio_driver * current_driver,
399 RD_WAVEFORMATEX * format)
400{
401 static struct stream out;
402 RD_BOOL stream_be = False;
403
404 /* softvol and byteswap do not change the amount of data they
405 return, so they can operate on the input-stream */
406 if (current_driver->wave_out_volume == rdpsnd_dsp_softvol_set)
407 rdpsnd_dsp_softvol(data, size, format);
408
409#ifdef B_ENDIAN
410 if (current_driver->need_byteswap_on_be)
411 {
412 rdpsnd_dsp_swapbytes(data, size, format);
413 stream_be = True;
414 }
415#endif
416
417 out.data = NULL;
418
419 if (current_driver->need_resampling)
420 out.size = rdpsnd_dsp_resample(&out.data, data, size, format, stream_be);
421
422 if (out.data == NULL)
423 {
424 out.data = (unsigned char *) xmalloc(size);
425 memcpy(out.data, data, size);
426 out.size = size;
427 }
428
429 out.p = out.data;
430 out.end = out.p + out.size;
431
432 return &out;
433}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette