20 #include <freerdp/config.h>
22 #include <winpr/assert.h>
27 #include <winpr/crt.h>
29 #include <freerdp/types.h>
30 #include <freerdp/log.h>
31 #include <freerdp/codec/dsp.h>
35 #if defined(WITH_FDK_AAC)
36 #include "dsp_fdk_aac.h"
39 #if !defined(WITH_DSP_FFMPEG)
44 #if defined(WITH_LAME)
45 #include <lame/lame.h>
48 #if defined(WITH_OPUS)
49 #include <opus/opus.h>
51 #define OPUS_MAX_FRAMES 5760
54 #if defined(WITH_FAAD2)
58 #if defined(WITH_FAAC)
62 #if defined(WITH_SOXR)
67 #include "dsp_ffmpeg.h"
70 #if !defined(WITH_DSP_FFMPEG)
72 #define TAG FREERDP_TAG("dsp")
91 struct S_FREERDP_DSP_CONTEXT
100 #if defined(WITH_LAME)
104 #if defined(WITH_OPUS)
105 OpusDecoder* opus_decoder;
106 OpusEncoder* opus_encoder;
108 #if defined(WITH_FAAD2)
113 #if defined(WITH_FAAC)
115 unsigned long faacInputSamples;
116 unsigned long faacMaxOutputBytes;
119 #if defined(WITH_SOXR)
124 #if defined(WITH_OPUS)
125 static BOOL opus_is_valid_samplerate(
const AUDIO_FORMAT* WINPR_RESTRICT format)
127 WINPR_ASSERT(format);
129 switch (format->nSamplesPerSec)
143 static INT16 read_int16(
const BYTE* WINPR_RESTRICT src)
145 return (INT16)(src[0] | (src[1] << 8));
148 static BOOL freerdp_dsp_channel_mix(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
149 const BYTE* WINPR_RESTRICT src,
size_t size,
151 const BYTE** WINPR_RESTRICT data,
size_t* WINPR_RESTRICT length)
153 if (!context || !data || !length)
156 if (srcFormat->wFormatTag != WAVE_FORMAT_PCM)
159 const UINT32 bpp = srcFormat->wBitsPerSample > 8 ? 2 : 1;
160 const size_t samples = size / bpp / srcFormat->nChannels;
162 if (context->common.format.nChannels == srcFormat->nChannels)
169 Stream_SetPosition(context->common.channelmix, 0);
172 if (context->common.format.nChannels > srcFormat->nChannels)
174 switch (srcFormat->nChannels)
177 if (!Stream_EnsureCapacity(context->common.channelmix, size * 2))
180 for (
size_t x = 0; x < samples; x++)
182 for (
size_t y = 0; y < bpp; y++)
183 Stream_Write_UINT8(context->common.channelmix, src[x * bpp + y]);
185 for (
size_t y = 0; y < bpp; y++)
186 Stream_Write_UINT8(context->common.channelmix, src[x * bpp + y]);
189 Stream_SealLength(context->common.channelmix);
190 *data = Stream_Buffer(context->common.channelmix);
191 *length = Stream_Length(context->common.channelmix);
201 switch (srcFormat->nChannels)
204 if (!Stream_EnsureCapacity(context->common.channelmix, size / 2))
209 for (
size_t x = 0; x < samples; x++)
211 for (
size_t y = 0; y < bpp; y++)
212 Stream_Write_UINT8(context->common.channelmix, src[2 * x * bpp + y]);
215 Stream_SealLength(context->common.channelmix);
216 *data = Stream_Buffer(context->common.channelmix);
217 *length = Stream_Length(context->common.channelmix);
233 static BOOL freerdp_dsp_resample(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
234 const BYTE* WINPR_RESTRICT src,
size_t size,
236 const BYTE** WINPR_RESTRICT data,
size_t* WINPR_RESTRICT length)
238 #if defined(WITH_SOXR)
241 size_t sframes, rframes;
243 size_t sbytes, rbytes;
246 size_t srcBytesPerFrame, dstBytesPerFrame;
250 if (srcFormat->wFormatTag != WAVE_FORMAT_PCM)
252 WLog_ERR(TAG,
"requires %s for sample input, got %s",
253 audio_format_get_tag_string(WAVE_FORMAT_PCM),
254 audio_format_get_tag_string(srcFormat->wFormatTag));
260 format.wFormatTag = WAVE_FORMAT_UNKNOWN;
261 format.wBitsPerSample = 0;
263 if (audio_format_compatible(&format, &context->common.format))
270 #if defined(WITH_SOXR)
271 srcBytesPerFrame = (srcFormat->wBitsPerSample > 8) ? 2 : 1;
272 dstBytesPerFrame = (context->common.format.wBitsPerSample > 8) ? 2 : 1;
273 srcChannels = srcFormat->nChannels;
274 dstChannels = context->common.format.nChannels;
275 sbytes = srcChannels * srcBytesPerFrame;
276 sframes = size / sbytes;
277 rbytes = dstBytesPerFrame * dstChannels;
280 (sframes * context->common.format.nSamplesPerSec + (srcFormat->nSamplesPerSec + 1) / 2) /
281 srcFormat->nSamplesPerSec;
282 rsize = rframes * rbytes;
284 if (!Stream_EnsureCapacity(context->common.resample, rsize))
288 soxr_process(context->sox, src, sframes, &idone, Stream_Buffer(context->common.resample),
289 Stream_Capacity(context->common.resample) / rbytes, &odone);
290 Stream_SetLength(context->common.resample, odone * rbytes);
291 *data = Stream_Buffer(context->common.resample);
292 *length = Stream_Length(context->common.resample);
293 return (error == 0) ? TRUE : FALSE;
295 WLog_ERR(TAG,
"Missing resample support, recompile -DWITH_SOXR=ON or -DWITH_DSP_FFMPEG=ON");
307 static const INT16 ima_step_index_table[] = {
308 -1, -1, -1, -1, 2, 4, 6, 8, -1, -1, -1, -1, 2, 4, 6, 8
311 static const INT16 ima_step_size_table[] = {
312 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 19, 21, 23,
313 25, 28, 31, 34, 37, 41, 45, 50, 55, 60, 66, 73, 80,
314 88, 97, 107, 118, 130, 143, 157, 173, 190, 209, 230, 253, 279,
315 307, 337, 371, 408, 449, 494, 544, 598, 658, 724, 796, 876, 963,
316 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024, 3327,
317 3660, 4026, 4428, 4871, 5358, 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487,
318 12635, 13899, 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
321 static UINT16 dsp_decode_ima_adpcm_sample(ADPCM* WINPR_RESTRICT adpcm,
unsigned int channel,
324 const INT32 ss = ima_step_size_table[adpcm->ima.last_step[channel]];
339 d += adpcm->ima.last_sample[channel];
346 adpcm->ima.last_sample[channel] = (INT16)d;
347 adpcm->ima.last_step[channel] += ima_step_index_table[sample];
349 if (adpcm->ima.last_step[channel] < 0)
350 adpcm->ima.last_step[channel] = 0;
351 else if (adpcm->ima.last_step[channel] > 88)
352 adpcm->ima.last_step[channel] = 88;
357 static BOOL freerdp_dsp_decode_ima_adpcm(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
358 const BYTE* WINPR_RESTRICT src,
size_t size,
361 size_t out_size = size * 4;
362 const UINT32 block_size = context->common.format.nBlockAlign;
363 const UINT32 channels = context->common.format.nChannels;
365 if (!Stream_EnsureCapacity(out, out_size))
370 if (size % block_size == 0)
372 context->adpcm.ima.last_sample[0] =
373 (INT16)(((UINT16)(*src)) | (((UINT16)(*(src + 1))) << 8));
374 context->adpcm.ima.last_step[0] = (INT16)(*(src + 2));
381 context->adpcm.ima.last_sample[1] =
382 (INT16)(((UINT16)(*src)) | (((UINT16)(*(src + 1))) << 8));
383 context->adpcm.ima.last_step[1] = (INT16)(*(src + 2));
392 for (
size_t i = 0; i < 8; i++)
394 BYTE* dst = Stream_Pointer(out);
396 const int channel = (i < 4 ? 0 : 1);
398 const BYTE sample = ((*src) & 0x0f);
399 const UINT16 decoded =
400 dsp_decode_ima_adpcm_sample(&context->adpcm, channel, sample);
401 dst[((i & 3) << 3) + (channel << 1)] = (decoded & 0xFF);
402 dst[((i & 3) << 3) + (channel << 1) + 1] = (decoded >> 8);
405 const BYTE sample = ((*src) >> 4);
406 const UINT16 decoded =
407 dsp_decode_ima_adpcm_sample(&context->adpcm, channel, sample);
408 dst[((i & 3) << 3) + (channel << 1) + 4] = (decoded & 0xFF);
409 dst[((i & 3) << 3) + (channel << 1) + 5] = (decoded >> 8);
414 if (!Stream_SafeSeek(out, 32))
420 BYTE* dst = Stream_Pointer(out);
421 if (!Stream_SafeSeek(out, 4))
425 const BYTE sample = ((*src) & 0x0f);
426 const UINT16 decoded = dsp_decode_ima_adpcm_sample(&context->adpcm, 0, sample);
427 *dst++ = (decoded & 0xFF);
428 *dst++ = (decoded >> 8);
431 const BYTE sample = ((*src) >> 4);
432 const UINT16 decoded = dsp_decode_ima_adpcm_sample(&context->adpcm, 0, sample);
433 *dst++ = (decoded & 0xFF);
434 *dst++ = (decoded >> 8);
444 #if defined(WITH_GSM)
445 static BOOL freerdp_dsp_decode_gsm610(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
446 const BYTE* WINPR_RESTRICT src,
size_t size,
451 while (offset < size)
454 gsm_signal gsmBlockBuffer[160] = { 0 };
455 rc = gsm_decode(context->gsm, (gsm_byte*) &src[offset],
461 if ((offset % 65) == 0)
466 if (!Stream_EnsureRemainingCapacity(out,
sizeof(gsmBlockBuffer)))
469 Stream_Write(out, (
void*)gsmBlockBuffer,
sizeof(gsmBlockBuffer));
475 static BOOL freerdp_dsp_encode_gsm610(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
476 const BYTE* WINPR_RESTRICT src,
size_t size,
481 while (offset < size)
483 const gsm_signal* signal = (
const gsm_signal*)&src[offset];
485 if (!Stream_EnsureRemainingCapacity(out,
sizeof(gsm_frame)))
488 gsm_encode(context->gsm, (gsm_signal*) signal,
489 Stream_Pointer(out));
491 if ((offset % 65) == 0)
492 Stream_Seek(out, 33);
494 Stream_Seek(out, 32);
503 #if defined(WITH_LAME)
504 static BOOL freerdp_dsp_decode_mp3(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
505 const BYTE* WINPR_RESTRICT src,
size_t size,
513 if (!context || !src || !out)
516 buffer_size = 2 * context->common.format.nChannels * context->common.format.nSamplesPerSec;
518 if (!Stream_EnsureCapacity(context->common.buffer, 2 * buffer_size))
521 pcm_l = Stream_BufferAs(context->common.buffer,
short);
522 pcm_r = Stream_BufferAs(context->common.buffer,
short) + buffer_size;
523 rc = hip_decode(context->hip, (
unsigned char*) src, size,
529 if (!Stream_EnsureRemainingCapacity(out, (
size_t)rc * context->common.format.nChannels * 2))
532 for (
size_t x = 0; x < rc; x++)
534 Stream_Write_UINT16(out, (UINT16)pcm_l[x]);
535 Stream_Write_UINT16(out, (UINT16)pcm_r[x]);
541 static BOOL freerdp_dsp_encode_mp3(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
542 const BYTE* WINPR_RESTRICT src,
size_t size,
545 size_t samples_per_channel;
548 if (!context || !src || !out)
551 samples_per_channel =
552 size / context->common.format.nChannels / context->common.format.wBitsPerSample / 8;
555 if (!Stream_EnsureRemainingCapacity(out, 5 / 4 * samples_per_channel + 7200))
558 samples_per_channel = size / 2 / context->common.format.nChannels;
559 rc = lame_encode_buffer_interleaved(context->lame, (
short*)src, samples_per_channel,
560 Stream_Pointer(out), Stream_GetRemainingCapacity(out));
565 Stream_Seek(out, (
size_t)rc);
570 #if defined(WITH_FAAC)
571 static BOOL freerdp_dsp_encode_faac(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
572 const BYTE* WINPR_RESTRICT src,
size_t size,
575 const int16_t* inSamples = (
const int16_t*)src;
580 if (!context || !src || !out)
583 bpp = context->common.format.wBitsPerSample / 8;
584 nrSamples = size / bpp;
586 if (!Stream_EnsureRemainingCapacity(context->common.buffer, nrSamples *
sizeof(int16_t)))
589 for (
size_t x = 0; x < nrSamples; x++)
591 Stream_Write_INT16(context->common.buffer, inSamples[x]);
592 if (Stream_GetPosition(context->common.buffer) / bpp >= context->faacInputSamples)
594 if (!Stream_EnsureRemainingCapacity(out, context->faacMaxOutputBytes))
596 rc = faacEncEncode(context->faac, Stream_BufferAs(context->common.buffer, int32_t),
597 context->faacInputSamples, Stream_Pointer(out),
598 Stream_GetRemainingCapacity(out));
602 Stream_Seek(out, (
size_t)rc);
603 Stream_SetPosition(context->common.buffer, 0);
611 #if defined(WITH_OPUS)
612 static BOOL freerdp_dsp_decode_opus(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
613 const BYTE* WINPR_RESTRICT src,
size_t size,
616 size_t max_size = 5760;
619 if (!context || !src || !out)
623 max_size = OPUS_MAX_FRAMES * context->common.format.nChannels *
sizeof(int16_t);
624 if (!Stream_EnsureRemainingCapacity(context->common.buffer, max_size))
627 frames = opus_decode(context->opus_decoder, src, size, Stream_Pointer(out), OPUS_MAX_FRAMES, 0);
631 Stream_Seek(out, frames * context->common.format.nChannels *
sizeof(int16_t));
636 static BOOL freerdp_dsp_encode_opus(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
637 const BYTE* WINPR_RESTRICT src,
size_t size,
640 if (!context || !src || !out)
644 const size_t max_size = OPUS_MAX_FRAMES * context->common.format.nChannels *
sizeof(int16_t);
645 if (!Stream_EnsureRemainingCapacity(context->common.buffer, max_size))
648 const int src_frames = size /
sizeof(opus_int16) / context->common.format.nChannels;
649 const opus_int16* src_data = (
const opus_int16*)src;
651 opus_encode(context->opus_encoder, src_data, src_frames, Stream_Pointer(out), max_size);
654 return Stream_SafeSeek(out, frames * context->common.format.nChannels *
sizeof(int16_t));
658 #if defined(WITH_FAAD2)
659 static BOOL freerdp_dsp_decode_faad(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
660 const BYTE* WINPR_RESTRICT src,
size_t size,
663 NeAACDecFrameInfo info;
666 if (!context || !src || !out)
669 if (!context->faadSetup)
676 unsigned long samplerate;
677 unsigned char channels;
680 err = NeAACDecInit(context->faad, cnv.pv, size,
681 &samplerate, &channels);
686 if (channels != context->common.format.nChannels)
689 if (samplerate != context->common.format.nSamplesPerSec)
692 context->faadSetup = TRUE;
695 while (offset < size)
704 outSize = context->common.format.nSamplesPerSec * context->common.format.nChannels *
705 context->common.format.wBitsPerSample / 8;
707 if (!Stream_EnsureRemainingCapacity(out, outSize))
710 sample_buffer = Stream_Pointer(out);
712 cnv.cpv = &src[offset];
713 NeAACDecDecode2(context->faad, &info, cnv.pv, size - offset, &sample_buffer,
714 Stream_GetRemainingCapacity(out));
719 offset += info.bytesconsumed;
721 if (info.samples == 0)
724 Stream_Seek(out, info.samples * context->common.format.wBitsPerSample / 8);
743 } ima_stereo_encode_map[] = { { 0, 0 }, { 4, 0 }, { 0, 4 }, { 4, 4 }, { 1, 0 }, { 5, 0 },
744 { 1, 4 }, { 5, 4 }, { 2, 0 }, { 6, 0 }, { 2, 4 }, { 6, 4 },
745 { 3, 0 }, { 7, 0 }, { 3, 4 }, { 7, 4 } };
747 static BYTE dsp_encode_ima_adpcm_sample(ADPCM* WINPR_RESTRICT adpcm,
int channel, INT16 sample)
749 INT32 ss = ima_step_size_table[adpcm->ima.last_step[channel]];
750 INT32 e = sample - adpcm->ima.last_sample[channel];
752 INT32 diff = ss >> 3;
788 diff += adpcm->ima.last_sample[channel];
792 else if (diff > 32767)
795 adpcm->ima.last_sample[channel] = (INT16)diff;
796 adpcm->ima.last_step[channel] += ima_step_index_table[enc];
798 if (adpcm->ima.last_step[channel] < 0)
799 adpcm->ima.last_step[channel] = 0;
800 else if (adpcm->ima.last_step[channel] > 88)
801 adpcm->ima.last_step[channel] = 88;
806 static BOOL freerdp_dsp_encode_ima_adpcm(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
807 const BYTE* WINPR_RESTRICT src,
size_t size,
810 if (!Stream_EnsureRemainingCapacity(out, size))
812 if (!Stream_EnsureRemainingCapacity(context->common.buffer, size + 64))
815 const size_t align = (context->common.format.nChannels > 1) ? 32 : 4;
817 while (size >= align)
819 if (Stream_GetPosition(context->common.buffer) % context->common.format.nBlockAlign == 0)
821 Stream_Write_UINT8(context->common.buffer, context->adpcm.ima.last_sample[0] & 0xFF);
822 Stream_Write_UINT8(context->common.buffer,
823 (context->adpcm.ima.last_sample[0] >> 8) & 0xFF);
824 Stream_Write_UINT8(context->common.buffer, (BYTE)context->adpcm.ima.last_step[0]);
825 Stream_Write_UINT8(context->common.buffer, 0);
827 if (context->common.format.nChannels > 1)
829 Stream_Write_UINT8(context->common.buffer,
830 context->adpcm.ima.last_sample[1] & 0xFF);
831 Stream_Write_UINT8(context->common.buffer,
832 (context->adpcm.ima.last_sample[1] >> 8) & 0xFF);
833 Stream_Write_UINT8(context->common.buffer, (BYTE)context->adpcm.ima.last_step[1]);
834 Stream_Write_UINT8(context->common.buffer, 0);
838 if (context->common.format.nChannels > 1)
840 BYTE* dst = Stream_Pointer(context->common.buffer);
843 for (
size_t i = 0; i < 16; i++)
845 const INT16 sample = (INT16)(((UINT16)(*src)) | (((UINT16)(*(src + 1))) << 8));
847 const BYTE encoded = dsp_encode_ima_adpcm_sample(&context->adpcm, i % 2, sample);
848 dst[ima_stereo_encode_map[i].byte_num] |= encoded
849 << ima_stereo_encode_map[i].byte_shift;
852 if (!Stream_SafeSeek(context->common.buffer, 8))
858 INT16 sample = (INT16)(((UINT16)(*src)) | (((UINT16)(*(src + 1))) << 8));
860 BYTE encoded = dsp_encode_ima_adpcm_sample(&context->adpcm, 0, sample);
861 sample = (INT16)(((UINT16)(*src)) | (((UINT16)(*(src + 1))) << 8));
863 encoded |= dsp_encode_ima_adpcm_sample(&context->adpcm, 0, sample) << 4;
864 Stream_Write_UINT8(context->common.buffer, encoded);
868 if (Stream_GetPosition(context->common.buffer) >= context->adpcm.ima.packet_size)
870 BYTE* bsrc = Stream_Buffer(context->common.buffer);
871 Stream_Write(out, bsrc, context->adpcm.ima.packet_size);
872 Stream_SetPosition(context->common.buffer, 0);
885 static const INT32 ms_adpcm_adaptation_table[] = { 230, 230, 230, 230, 307, 409, 512, 614,
886 768, 614, 512, 409, 307, 230, 230, 230 };
888 static const INT32 ms_adpcm_coeffs1[7] = { 256, 512, 0, 192, 240, 460, 392 };
890 static const INT32 ms_adpcm_coeffs2[7] = { 0, -256, 0, 64, 0, -208, -232 };
892 static INLINE INT16 freerdp_dsp_decode_ms_adpcm_sample(ADPCM* WINPR_RESTRICT adpcm, BYTE sample,
895 const INT8 nibble = (sample & 0x08 ? (INT8)sample - 16 : (INT8)sample);
897 ((adpcm->ms.sample1[channel] * ms_adpcm_coeffs1[adpcm->ms.predictor[channel]]) +
898 (adpcm->ms.sample2[channel] * ms_adpcm_coeffs2[adpcm->ms.predictor[channel]])) /
900 presample += nibble * adpcm->ms.delta[channel];
902 if (presample > 32767)
904 else if (presample < -32768)
907 adpcm->ms.sample2[channel] = adpcm->ms.sample1[channel];
908 adpcm->ms.sample1[channel] = presample;
909 adpcm->ms.delta[channel] = adpcm->ms.delta[channel] * ms_adpcm_adaptation_table[sample] / 256;
911 if (adpcm->ms.delta[channel] < 16)
912 adpcm->ms.delta[channel] = 16;
914 return (INT16)presample;
917 static BOOL freerdp_dsp_decode_ms_adpcm(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
918 const BYTE* WINPR_RESTRICT src,
size_t size,
921 const size_t out_size = size * 4;
922 const UINT32 channels = context->common.format.nChannels;
923 const UINT32 block_size = context->common.format.nBlockAlign;
925 if (!Stream_EnsureCapacity(out, out_size))
930 if (size % block_size == 0)
934 context->adpcm.ms.predictor[0] = *src++;
935 context->adpcm.ms.predictor[1] = *src++;
936 context->adpcm.ms.delta[0] = read_int16(src);
938 context->adpcm.ms.delta[1] = read_int16(src);
940 context->adpcm.ms.sample1[0] = read_int16(src);
942 context->adpcm.ms.sample1[1] = read_int16(src);
944 context->adpcm.ms.sample2[0] = read_int16(src);
946 context->adpcm.ms.sample2[1] = read_int16(src);
949 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample2[0]);
950 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample2[1]);
951 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample1[0]);
952 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample1[1]);
956 context->adpcm.ms.predictor[0] = *src++;
957 context->adpcm.ms.delta[0] = read_int16(src);
959 context->adpcm.ms.sample1[0] = read_int16(src);
961 context->adpcm.ms.sample2[0] = read_int16(src);
964 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample2[0]);
965 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample1[0]);
972 const BYTE sample = *src++;
975 out, freerdp_dsp_decode_ms_adpcm_sample(&context->adpcm, sample >> 4, 0));
977 out, freerdp_dsp_decode_ms_adpcm_sample(&context->adpcm, sample & 0x0F, 1));
980 const BYTE sample = *src++;
983 out, freerdp_dsp_decode_ms_adpcm_sample(&context->adpcm, sample >> 4, 0));
985 out, freerdp_dsp_decode_ms_adpcm_sample(&context->adpcm, sample & 0x0F, 1));
990 const BYTE sample = *src++;
992 Stream_Write_INT16(out,
993 freerdp_dsp_decode_ms_adpcm_sample(&context->adpcm, sample >> 4, 0));
995 out, freerdp_dsp_decode_ms_adpcm_sample(&context->adpcm, sample & 0x0F, 0));
1002 static BYTE freerdp_dsp_encode_ms_adpcm_sample(ADPCM* WINPR_RESTRICT adpcm, INT32 sample,
1006 ((adpcm->ms.sample1[channel] * ms_adpcm_coeffs1[adpcm->ms.predictor[channel]]) +
1007 (adpcm->ms.sample2[channel] * ms_adpcm_coeffs2[adpcm->ms.predictor[channel]])) /
1009 INT32 errordelta = (sample - presample) / adpcm->ms.delta[channel];
1011 if ((sample - presample) % adpcm->ms.delta[channel] > adpcm->ms.delta[channel] / 2)
1016 else if (errordelta < -8)
1019 presample += adpcm->ms.delta[channel] * errordelta;
1021 if (presample > 32767)
1023 else if (presample < -32768)
1026 adpcm->ms.sample2[channel] = adpcm->ms.sample1[channel];
1027 adpcm->ms.sample1[channel] = presample;
1028 adpcm->ms.delta[channel] =
1029 adpcm->ms.delta[channel] * ms_adpcm_adaptation_table[(((BYTE)errordelta) & 0x0F)] / 256;
1031 if (adpcm->ms.delta[channel] < 16)
1032 adpcm->ms.delta[channel] = 16;
1034 return ((BYTE)errordelta) & 0x0F;
1037 static BOOL freerdp_dsp_encode_ms_adpcm(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
1038 const BYTE* WINPR_RESTRICT src,
size_t size,
1041 const size_t step = 8 + ((context->common.format.nChannels > 1) ? 4 : 0);
1043 if (!Stream_EnsureRemainingCapacity(out, size))
1046 const size_t start = Stream_GetPosition(out);
1048 if (context->adpcm.ms.delta[0] < 16)
1049 context->adpcm.ms.delta[0] = 16;
1051 if (context->adpcm.ms.delta[1] < 16)
1052 context->adpcm.ms.delta[1] = 16;
1054 while (size >= step)
1056 if ((Stream_GetPosition(out) - start) % context->common.format.nBlockAlign == 0)
1058 if (context->common.format.nChannels > 1)
1060 Stream_Write_UINT8(out, context->adpcm.ms.predictor[0]);
1061 Stream_Write_UINT8(out, context->adpcm.ms.predictor[1]);
1062 Stream_Write_UINT8(out, (context->adpcm.ms.delta[0] & 0xFF));
1063 Stream_Write_UINT8(out, ((context->adpcm.ms.delta[0] >> 8) & 0xFF));
1064 Stream_Write_UINT8(out, (context->adpcm.ms.delta[1] & 0xFF));
1065 Stream_Write_UINT8(out, ((context->adpcm.ms.delta[1] >> 8) & 0xFF));
1067 context->adpcm.ms.sample1[0] = read_int16(src + 4);
1068 context->adpcm.ms.sample1[1] = read_int16(src + 6);
1069 context->adpcm.ms.sample2[0] = read_int16(src + 0);
1070 context->adpcm.ms.sample2[1] = read_int16(src + 2);
1072 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample1[0]);
1073 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample1[1]);
1074 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample2[0]);
1075 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample2[1]);
1082 Stream_Write_UINT8(out, context->adpcm.ms.predictor[0]);
1083 Stream_Write_UINT8(out, (BYTE)(context->adpcm.ms.delta[0] & 0xFF));
1084 Stream_Write_UINT8(out, (BYTE)((context->adpcm.ms.delta[0] >> 8) & 0xFF));
1086 context->adpcm.ms.sample1[0] = read_int16(src + 2);
1087 context->adpcm.ms.sample2[0] = read_int16(src + 0);
1089 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample1[0]);
1090 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample2[0]);
1097 const INT16 sample = read_int16(src);
1100 out, (freerdp_dsp_encode_ms_adpcm_sample(&context->adpcm, sample, 0) << 4) & 0xFF);
1103 const INT16 sample = read_int16(src);
1107 Stream_Read_UINT8(out, val);
1108 val += freerdp_dsp_encode_ms_adpcm_sample(&context->adpcm, sample,
1109 context->common.format.nChannels > 1 ? 1 : 0);
1110 Stream_Write_UINT8(out, val);
1120 FREERDP_DSP_CONTEXT* freerdp_dsp_context_new(BOOL encoder)
1122 #if defined(WITH_DSP_FFMPEG)
1123 return freerdp_dsp_ffmpeg_context_new(encoder);
1125 FREERDP_DSP_CONTEXT* context = calloc(1,
sizeof(FREERDP_DSP_CONTEXT));
1130 if (!freerdp_dsp_common_context_init(&context->common, encoder))
1133 #if defined(WITH_GSM)
1134 context->gsm = gsm_create();
1142 rc = gsm_option(context->gsm, GSM_OPT_WAV49, &val);
1148 #if defined(WITH_LAME)
1152 context->lame = lame_init();
1159 context->hip = hip_decode_init();
1166 #if defined(WITH_FAAD2)
1170 context->faad = NeAACDecOpen();
1179 freerdp_dsp_context_free(context);
1184 void freerdp_dsp_context_free(FREERDP_DSP_CONTEXT* context)
1189 #if defined(WITH_FDK_AAC)
1192 fdk_aac_dsp_uninit(ctx);
1195 #if defined(WITH_DSP_FFMPEG)
1196 freerdp_dsp_ffmpeg_context_free(context);
1199 freerdp_dsp_common_context_uninit(&context->common);
1201 #if defined(WITH_GSM)
1202 gsm_destroy(context->gsm);
1204 #if defined(WITH_LAME)
1206 if (context->common.encoder)
1207 lame_close(context->lame);
1209 hip_decode_exit(context->hip);
1212 #if defined(WITH_OPUS)
1214 if (context->opus_decoder)
1215 opus_decoder_destroy(context->opus_decoder);
1216 if (context->opus_encoder)
1217 opus_encoder_destroy(context->opus_encoder);
1220 #if defined(WITH_FAAD2)
1222 if (!context->common.encoder)
1223 NeAACDecClose(context->faad);
1226 #if defined(WITH_FAAC)
1229 faacEncClose(context->faac);
1232 #if defined(WITH_SOXR)
1233 soxr_delete(context->sox);
1240 BOOL freerdp_dsp_encode(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
1242 const BYTE* WINPR_RESTRICT pdata,
size_t length,
1245 #if defined(WITH_FDK_AAC)
1248 switch (ctx->format.wFormatTag)
1250 case WAVE_FORMAT_AAC_MS:
1251 return fdk_aac_dsp_encode(ctx, srcFormat, pdata, length, out);
1257 #if defined(WITH_DSP_FFMPEG)
1258 return freerdp_dsp_ffmpeg_encode(context, srcFormat, pdata, length, out);
1260 if (!context || !context->common.encoder || !srcFormat || !pdata || !out)
1264 const BYTE* resampleData = NULL;
1265 size_t resampleLength = 0;
1267 if (!freerdp_dsp_channel_mix(context, pdata, length, srcFormat, &resampleData, &resampleLength))
1270 format.nChannels = context->common.format.nChannels;
1272 const BYTE* data = NULL;
1273 if (!freerdp_dsp_resample(context, resampleData, resampleLength, &format, &data, &length))
1276 switch (context->common.format.wFormatTag)
1278 case WAVE_FORMAT_PCM:
1279 if (!Stream_EnsureRemainingCapacity(out, length))
1282 Stream_Write(out, data, length);
1285 case WAVE_FORMAT_ADPCM:
1286 return freerdp_dsp_encode_ms_adpcm(context, data, length, out);
1288 case WAVE_FORMAT_DVI_ADPCM:
1289 return freerdp_dsp_encode_ima_adpcm(context, data, length, out);
1290 #if defined(WITH_GSM)
1292 case WAVE_FORMAT_GSM610:
1293 return freerdp_dsp_encode_gsm610(context, data, length, out);
1295 #if defined(WITH_LAME)
1297 case WAVE_FORMAT_MPEGLAYER3:
1298 return freerdp_dsp_encode_mp3(context, data, length, out);
1300 #if defined(WITH_FAAC)
1302 case WAVE_FORMAT_AAC_MS:
1303 return freerdp_dsp_encode_faac(context, data, length, out);
1305 #if defined(WITH_OPUS)
1307 case WAVE_FORMAT_OPUS:
1308 return freerdp_dsp_encode_opus(context, data, length, out);
1318 BOOL freerdp_dsp_decode(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
1320 const BYTE* WINPR_RESTRICT data,
size_t length,
wStream* WINPR_RESTRICT out)
1322 #if defined(WITH_FDK_AAC)
1325 switch (ctx->format.wFormatTag)
1327 case WAVE_FORMAT_AAC_MS:
1328 return fdk_aac_dsp_decode(ctx, srcFormat, data, length, out);
1334 #if defined(WITH_DSP_FFMPEG)
1335 return freerdp_dsp_ffmpeg_decode(context, srcFormat, data, length, out);
1338 if (!context || context->common.encoder || !srcFormat || !data || !out)
1341 switch (context->common.format.wFormatTag)
1343 case WAVE_FORMAT_PCM:
1344 if (!Stream_EnsureRemainingCapacity(out, length))
1347 Stream_Write(out, data, length);
1350 case WAVE_FORMAT_ADPCM:
1351 return freerdp_dsp_decode_ms_adpcm(context, data, length, out);
1353 case WAVE_FORMAT_DVI_ADPCM:
1354 return freerdp_dsp_decode_ima_adpcm(context, data, length, out);
1355 #if defined(WITH_GSM)
1357 case WAVE_FORMAT_GSM610:
1358 return freerdp_dsp_decode_gsm610(context, data, length, out);
1360 #if defined(WITH_LAME)
1362 case WAVE_FORMAT_MPEGLAYER3:
1363 return freerdp_dsp_decode_mp3(context, data, length, out);
1365 #if defined(WITH_FAAD2)
1367 case WAVE_FORMAT_AAC_MS:
1368 return freerdp_dsp_decode_faad(context, data, length, out);
1371 #if defined(WITH_OPUS)
1372 case WAVE_FORMAT_OPUS:
1373 return freerdp_dsp_decode_opus(context, data, length, out);
1383 BOOL freerdp_dsp_supports_format(
const AUDIO_FORMAT* WINPR_RESTRICT format, BOOL encode)
1385 #if defined(WITH_FDK_AAC)
1386 switch (format->wFormatTag)
1388 case WAVE_FORMAT_AAC_MS:
1396 #if defined(WITH_DSP_FFMPEG)
1397 return freerdp_dsp_ffmpeg_supports_format(format, encode);
1400 #if !defined(WITH_DSP_EXPERIMENTAL)
1401 WINPR_UNUSED(encode);
1403 switch (format->wFormatTag)
1405 case WAVE_FORMAT_PCM:
1407 #if defined(WITH_DSP_EXPERIMENTAL)
1409 case WAVE_FORMAT_ADPCM:
1411 case WAVE_FORMAT_DVI_ADPCM:
1414 #if defined(WITH_GSM)
1416 case WAVE_FORMAT_GSM610:
1417 #if defined(WITH_DSP_EXPERIMENTAL)
1423 #if defined(WITH_LAME)
1425 case WAVE_FORMAT_MPEGLAYER3:
1426 #if defined(WITH_DSP_EXPERIMENTAL)
1433 case WAVE_FORMAT_AAC_MS:
1434 #if defined(WITH_FAAD2)
1439 #if defined(WITH_FAAC)
1445 #if defined(WITH_OPUS)
1448 case WAVE_FORMAT_OPUS:
1449 return opus_is_valid_samplerate(format);
1461 BOOL freerdp_dsp_context_reset(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
1463 UINT32 FramesPerPacket)
1465 #if defined(WITH_FDK_AAC)
1466 WINPR_ASSERT(targetFormat);
1467 if (targetFormat->wFormatTag == WAVE_FORMAT_AAC_MS)
1470 fdk_aac_dsp_uninit(ctx);
1471 ctx->format = *targetFormat;
1472 return fdk_aac_dsp_init(ctx, FramesPerPacket);
1476 #if defined(WITH_DSP_FFMPEG)
1477 return freerdp_dsp_ffmpeg_context_reset(context, targetFormat);
1480 if (!context || !targetFormat)
1483 context->common.format = *targetFormat;
1485 if (context->common.format.wFormatTag == WAVE_FORMAT_DVI_ADPCM)
1487 size_t min_frame_data = 1ull * context->common.format.wBitsPerSample *
1488 context->common.format.nChannels * FramesPerPacket;
1489 size_t data_per_block =
1490 (1ULL * context->common.format.nBlockAlign - 4ULL * context->common.format.nChannels) *
1492 size_t nb_block_per_packet = min_frame_data / data_per_block;
1494 if (min_frame_data % data_per_block)
1495 nb_block_per_packet++;
1497 context->adpcm.ima.packet_size = nb_block_per_packet * context->common.format.nBlockAlign;
1498 Stream_EnsureCapacity(context->common.buffer, context->adpcm.ima.packet_size);
1499 Stream_SetPosition(context->common.buffer, 0);
1502 #if defined(WITH_OPUS)
1504 if (opus_is_valid_samplerate(&context->common.format))
1506 if (!context->common.encoder)
1508 int opus_error = OPUS_OK;
1510 context->opus_decoder =
1511 opus_decoder_create(context->common.format.nSamplesPerSec,
1512 context->common.format.nChannels, &opus_error);
1513 if (opus_error != OPUS_OK)
1518 int opus_error = OPUS_OK;
1520 context->opus_encoder = opus_encoder_create(context->common.format.nSamplesPerSec,
1521 context->common.format.nChannels,
1522 OPUS_APPLICATION_VOIP, &opus_error);
1523 if (opus_error != OPUS_OK)
1527 opus_encoder_ctl(context->opus_encoder,
1528 OPUS_SET_BITRATE(context->common.format.nAvgBytesPerSec * 8));
1529 if (opus_error != OPUS_OK)
1535 #if defined(WITH_FAAD2)
1536 context->faadSetup = FALSE;
1538 #if defined(WITH_FAAC)
1540 if (context->common.encoder)
1542 faacEncConfigurationPtr cfg;
1545 faacEncClose(context->faac);
1547 context->faac = faacEncOpen(targetFormat->nSamplesPerSec, targetFormat->nChannels,
1548 &context->faacInputSamples, &context->faacMaxOutputBytes);
1553 cfg = faacEncGetCurrentConfiguration(context->faac);
1554 cfg->inputFormat = FAAC_INPUT_16BIT;
1555 cfg->outputFormat = 0;
1556 cfg->mpegVersion = MPEG4;
1558 cfg->bandWidth = targetFormat->nAvgBytesPerSec;
1559 faacEncSetConfiguration(context->faac, cfg);
1563 #if defined(WITH_SOXR)
1565 soxr_io_spec_t iospec = soxr_io_spec(SOXR_INT16, SOXR_INT16);
1567 soxr_delete(context->sox);
1569 soxr_create(context->common.format.nSamplesPerSec, targetFormat->nSamplesPerSec,
1570 targetFormat->nChannels, &error, &iospec, NULL, NULL);
1572 if (!context->sox || (error != 0))
1582 WINPR_ASSERT(context);
1583 context->encoder = encode;
1584 context->buffer = Stream_New(NULL, 1024);
1585 if (!context->buffer)
1588 context->channelmix = Stream_New(NULL, 1024);
1589 if (!context->channelmix)
1592 context->resample = Stream_New(NULL, 1024);
1593 if (!context->resample)
1599 freerdp_dsp_common_context_uninit(context);
1605 WINPR_ASSERT(context);
1607 Stream_Free(context->buffer, TRUE);
1608 Stream_Free(context->channelmix, TRUE);
1609 Stream_Free(context->resample, TRUE);
1611 context->buffer = NULL;
1612 context->channelmix = NULL;
1613 context->resample = NULL;