20#include <freerdp/config.h>
22#include <winpr/assert.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)
51#define OPUS_MAX_FRAMES 5760ull
54#if defined(WITH_FAAD2)
67#include "dsp_ffmpeg.h"
70#if !defined(WITH_DSP_FFMPEG)
72#define TAG FREERDP_TAG("dsp")
91struct 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)
125static BOOL opus_is_valid_samplerate(
const AUDIO_FORMAT* WINPR_RESTRICT format)
127 WINPR_ASSERT(format);
129 switch (format->nSamplesPerSec)
143static INT16 read_int16(
const BYTE* WINPR_RESTRICT src)
145 return (INT16)(src[0] | (src[1] << 8));
148static 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_ResetPosition(context->common.channelmix);
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);
233static 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 if (!Stream_SetLength(context->common.resample, odone * rbytes))
293 *data = Stream_Buffer(context->common.resample);
294 *length = Stream_Length(context->common.resample);
295 return (error == 0) != 0;
297 WLog_ERR(TAG,
"Missing resample support, recompile -DWITH_SOXR=ON or -DWITH_DSP_FFMPEG=ON");
309static const INT16 ima_step_index_table[] = {
310 -1, -1, -1, -1, 2, 4, 6, 8, -1, -1, -1, -1, 2, 4, 6, 8
313static const INT16 ima_step_size_table[] = {
314 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 19, 21, 23,
315 25, 28, 31, 34, 37, 41, 45, 50, 55, 60, 66, 73, 80,
316 88, 97, 107, 118, 130, 143, 157, 173, 190, 209, 230, 253, 279,
317 307, 337, 371, 408, 449, 494, 544, 598, 658, 724, 796, 876, 963,
318 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024, 3327,
319 3660, 4026, 4428, 4871, 5358, 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487,
320 12635, 13899, 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
323static inline void dsp_ima_clamp_step(ADPCM* WINPR_RESTRICT adpcm,
unsigned int channel)
326 if (adpcm->ima.last_step[channel] < 0)
327 adpcm->ima.last_step[channel] = 0;
329 const size_t size = ARRAYSIZE(ima_step_size_table);
330 if (adpcm->ima.last_step[channel] > size)
331 adpcm->ima.last_step[channel] = size;
334static UINT16 dsp_decode_ima_adpcm_sample(ADPCM* WINPR_RESTRICT adpcm,
unsigned int channel,
337 WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ima.last_step));
338 WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ima.last_sample));
340 const INT16 offset = adpcm->ima.last_step[channel];
341 WINPR_ASSERT(offset >= 0);
342 WINPR_ASSERT(WINPR_CXX_COMPAT_CAST(
size_t, offset) < ARRAYSIZE(ima_step_size_table));
344 const INT32 ss = ima_step_size_table[offset];
359 d += adpcm->ima.last_sample[channel];
366 adpcm->ima.last_sample[channel] = (INT16)d;
368 WINPR_ASSERT(sample < ARRAYSIZE(ima_step_index_table));
369 adpcm->ima.last_step[channel] = adpcm->ima.last_step[channel] + ima_step_index_table[sample];
371 dsp_ima_clamp_step(adpcm, channel);
376static BOOL valid_ima_adpcm_format(
const FREERDP_DSP_CONTEXT* WINPR_RESTRICT context)
378 WINPR_ASSERT(context);
379 if (context->common.format.wFormatTag != WAVE_FORMAT_DVI_ADPCM)
381 if (context->common.format.nBlockAlign <= 4ULL)
383 if (context->common.format.nChannels < 1)
385 if (context->common.format.wBitsPerSample == 0)
390static BOOL freerdp_dsp_decode_ima_adpcm(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
391 const BYTE* WINPR_RESTRICT src,
size_t size,
394 if (!valid_ima_adpcm_format(context))
397 size_t out_size = size * 4ull;
398 const UINT32 block_size = context->common.format.nBlockAlign;
399 const UINT32 channels = context->common.format.nChannels;
401 if (!Stream_EnsureCapacity(out, out_size))
406 if (size % block_size == 0)
411 context->adpcm.ima.last_sample[0] =
412 (INT16)(((UINT16)(*src)) | (((UINT16)(*(src + 1))) << 8));
413 context->adpcm.ima.last_step[0] = (INT16)(*(src + 2));
415 dsp_ima_clamp_step(&context->adpcm, 0);
425 context->adpcm.ima.last_sample[1] =
426 (INT16)(((UINT16)(*src)) | (((UINT16)(*(src + 1))) << 8));
427 context->adpcm.ima.last_step[1] = (INT16)(*(src + 2));
429 dsp_ima_clamp_step(&context->adpcm, 1);
440 for (
size_t i = 0; i < 8; i++)
442 BYTE* dst = Stream_Pointer(out);
444 const unsigned channel = (i < 4 ? 0 : 1);
446 const BYTE sample = ((*src) & 0x0f);
447 const UINT16 decoded =
448 dsp_decode_ima_adpcm_sample(&context->adpcm, channel, sample);
449 dst[((i & 3) << 3) + (channel << 1u)] = (decoded & 0xFF);
450 dst[((i & 3) << 3) + (channel << 1u) + 1] = (decoded >> 8);
453 const BYTE sample = ((*src) >> 4);
454 const UINT16 decoded =
455 dsp_decode_ima_adpcm_sample(&context->adpcm, channel, sample);
456 dst[((i & 3) << 3) + (channel << 1u) + 4] = (decoded & 0xFF);
457 dst[((i & 3) << 3) + (channel << 1u) + 5] = (decoded >> 8);
462 if (!Stream_SafeSeek(out, 32))
470 BYTE* dst = Stream_Pointer(out);
471 if (!Stream_SafeSeek(out, 4))
475 const BYTE sample = ((*src) & 0x0f);
476 const UINT16 decoded = dsp_decode_ima_adpcm_sample(&context->adpcm, 0, sample);
477 *dst++ = (decoded & 0xFF);
478 *dst++ = (decoded >> 8);
481 const BYTE sample = ((*src) >> 4);
482 const UINT16 decoded = dsp_decode_ima_adpcm_sample(&context->adpcm, 0, sample);
483 *dst++ = (decoded & 0xFF);
484 *dst++ = (decoded >> 8);
495static BOOL freerdp_dsp_decode_gsm610(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
496 const BYTE* WINPR_RESTRICT src,
size_t size,
501 while (offset < size)
504 gsm_signal gsmBlockBuffer[160] = WINPR_C_ARRAY_INIT;
505 rc = gsm_decode(context->gsm, (gsm_byte*) &src[offset],
511 if ((offset % 65) == 0)
516 if (!Stream_EnsureRemainingCapacity(out,
sizeof(gsmBlockBuffer)))
519 Stream_Write(out, (
void*)gsmBlockBuffer,
sizeof(gsmBlockBuffer));
525static BOOL freerdp_dsp_encode_gsm610(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
526 const BYTE* WINPR_RESTRICT src,
size_t size,
531 while (offset < size)
533 const gsm_signal* signal = (
const gsm_signal*)&src[offset];
535 if (!Stream_EnsureRemainingCapacity(out,
sizeof(gsm_frame)))
538 gsm_encode(context->gsm, (gsm_signal*) signal,
539 Stream_Pointer(out));
541 if ((offset % 65) == 0)
542 Stream_Seek(out, 33);
544 Stream_Seek(out, 32);
553#if defined(WITH_LAME)
554static BOOL valid_mp3_format(
const FREERDP_DSP_CONTEXT* WINPR_RESTRICT context)
556 WINPR_ASSERT(context);
557 if (context->common.format.wFormatTag != WAVE_FORMAT_MPEGLAYER3)
559 if (context->common.format.nChannels < 1)
561 if (context->common.format.wBitsPerSample == 0)
563 if (context->common.format.nSamplesPerSec == 0)
568static BOOL freerdp_dsp_decode_mp3(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
569 const BYTE* WINPR_RESTRICT src,
size_t size,
572 if (!context || !src || !out)
574 if (!valid_mp3_format(context))
576 const size_t buffer_size =
577 2 * context->common.format.nChannels * context->common.format.nSamplesPerSec;
579 if (!Stream_EnsureCapacity(context->common.buffer, 2 * buffer_size))
582 short* pcm_l = Stream_BufferAs(context->common.buffer,
short);
583 short* pcm_r = Stream_BufferAs(context->common.buffer,
short) + buffer_size;
584 const int rc = hip_decode(context->hip, (
unsigned char*) src,
590 if (!Stream_EnsureRemainingCapacity(out, (
size_t)rc * context->common.format.nChannels * 2))
593 for (
size_t x = 0; x < rc; x++)
595 Stream_Write_UINT16(out, (UINT16)pcm_l[x]);
596 Stream_Write_UINT16(out, (UINT16)pcm_r[x]);
602static BOOL freerdp_dsp_encode_mp3(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
603 const BYTE* WINPR_RESTRICT src,
size_t size,
606 if (!context || !src || !out)
609 if (!valid_mp3_format(context))
612 size_t samples_per_channel =
613 size / context->common.format.nChannels / context->common.format.wBitsPerSample / 8;
616 if (!Stream_EnsureRemainingCapacity(out, 5 / 4 * samples_per_channel + 7200))
619 samples_per_channel = size / 2 / context->common.format.nChannels;
621 lame_encode_buffer_interleaved(context->lame, (
short*)src, samples_per_channel,
622 Stream_Pointer(out), Stream_GetRemainingCapacity(out));
627 Stream_Seek(out, (
size_t)rc);
632#if defined(WITH_FAAC)
633static BOOL freerdp_dsp_encode_faac(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
634 const BYTE* WINPR_RESTRICT src,
size_t size,
637 const int16_t* inSamples = (
const int16_t*)src;
642 if (!context || !src || !out)
645 bpp = context->common.format.wBitsPerSample / 8;
646 nrSamples = size / bpp;
648 if (!Stream_EnsureRemainingCapacity(context->common.buffer, nrSamples *
sizeof(int16_t)))
651 for (
size_t x = 0; x < nrSamples; x++)
653 Stream_Write_INT16(context->common.buffer, inSamples[x]);
654 if (Stream_GetPosition(context->common.buffer) / bpp >= context->faacInputSamples)
656 if (!Stream_EnsureRemainingCapacity(out, context->faacMaxOutputBytes))
658 rc = faacEncEncode(context->faac, Stream_BufferAs(context->common.buffer, int32_t),
659 context->faacInputSamples, Stream_Pointer(out),
660 Stream_GetRemainingCapacity(out));
664 Stream_Seek(out, (
size_t)rc);
665 Stream_ResetPosition(context->common.buffer);
673#if defined(WITH_OPUS)
674static BOOL freerdp_dsp_decode_opus(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
675 const BYTE* WINPR_RESTRICT src,
size_t size,
678 if (!context || !src || !out)
682 const size_t max_size = OPUS_MAX_FRAMES * context->common.format.nChannels *
sizeof(int16_t);
683 if (!Stream_EnsureRemainingCapacity(context->common.buffer, max_size))
686 const opus_int32 frames =
687 opus_decode(context->opus_decoder, src, WINPR_ASSERTING_INT_CAST(opus_int32, size),
688 Stream_Pointer(out), OPUS_MAX_FRAMES, 0);
692 Stream_Seek(out, (
size_t)frames * context->common.format.nChannels *
sizeof(int16_t));
697static BOOL freerdp_dsp_encode_opus(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
698 const BYTE* WINPR_RESTRICT src,
size_t size,
701 if (!context || !src || !out)
705 const size_t max_size = OPUS_MAX_FRAMES * context->common.format.nChannels *
sizeof(int16_t);
706 if (!Stream_EnsureRemainingCapacity(context->common.buffer, max_size))
709 const size_t src_frames = size /
sizeof(opus_int16) / context->common.format.nChannels;
710 const opus_int16* src_data = (
const opus_int16*)src;
711 const opus_int32 frames = opus_encode(
712 context->opus_encoder, src_data, WINPR_ASSERTING_INT_CAST(opus_int32, src_frames),
713 Stream_Pointer(out), WINPR_ASSERTING_INT_CAST(opus_int32, max_size));
716 return Stream_SafeSeek(out,
717 (
size_t)frames * context->common.format.nChannels *
sizeof(int16_t));
721#if defined(WITH_FAAD2)
722static BOOL freerdp_dsp_decode_faad(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
723 const BYTE* WINPR_RESTRICT src,
size_t size,
726 NeAACDecFrameInfo info;
729 if (!context || !src || !out)
732 if (!context->faadSetup)
739 unsigned long samplerate;
740 unsigned char channels;
743 err = NeAACDecInit(context->faad, cnv.pv, size,
744 &samplerate, &channels);
749 if (channels != context->common.format.nChannels)
752 if (samplerate != context->common.format.nSamplesPerSec)
755 context->faadSetup = TRUE;
758 while (offset < size)
767 outSize = context->common.format.nSamplesPerSec * context->common.format.nChannels *
768 context->common.format.wBitsPerSample / 8;
770 if (!Stream_EnsureRemainingCapacity(out, outSize))
773 sample_buffer = Stream_Pointer(out);
775 cnv.cpv = &src[offset];
776 NeAACDecDecode2(context->faad, &info, cnv.pv, size - offset, &sample_buffer,
777 Stream_GetRemainingCapacity(out));
782 offset += info.bytesconsumed;
784 if (info.samples == 0)
787 Stream_Seek(out, info.samples * context->common.format.wBitsPerSample / 8);
806} ima_stereo_encode_map[] = { { 0, 0 }, { 4, 0 }, { 0, 4 }, { 4, 4 }, { 1, 0 }, { 5, 0 },
807 { 1, 4 }, { 5, 4 }, { 2, 0 }, { 6, 0 }, { 2, 4 }, { 6, 4 },
808 { 3, 0 }, { 7, 0 }, { 3, 4 }, { 7, 4 } };
810static BYTE dsp_encode_ima_adpcm_sample(ADPCM* WINPR_RESTRICT adpcm,
size_t channel, INT16 sample)
812 WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ima.last_step));
813 WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ima.last_sample));
815 const INT16 offset = adpcm->ima.last_step[channel];
816 WINPR_ASSERT(offset >= 0);
817 WINPR_ASSERT(WINPR_CXX_COMPAT_CAST(
size_t, offset) < ARRAYSIZE(ima_step_size_table));
819 INT32 ss = ima_step_size_table[offset];
820 INT32 e = sample - adpcm->ima.last_sample[channel];
822 INT32 diff = ss >> 3;
858 diff += adpcm->ima.last_sample[channel];
862 else if (diff > 32767)
865 adpcm->ima.last_sample[channel] = (INT16)diff;
867 WINPR_ASSERT(enc < ARRAYSIZE(ima_step_index_table));
868 adpcm->ima.last_step[channel] = adpcm->ima.last_step[channel] + ima_step_index_table[enc];
870 dsp_ima_clamp_step(adpcm, channel);
875static BOOL freerdp_dsp_encode_ima_adpcm(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
876 const BYTE* WINPR_RESTRICT src,
size_t size,
879 if (!valid_ima_adpcm_format(context))
881 if (!Stream_EnsureRemainingCapacity(out, size))
883 if (!Stream_EnsureRemainingCapacity(context->common.buffer, size + 64))
886 const size_t align = (context->common.format.nChannels > 1) ? 32 : 4;
888 while (size >= align)
890 if (Stream_GetPosition(context->common.buffer) % context->common.format.nBlockAlign == 0)
892 Stream_Write_UINT8(context->common.buffer, context->adpcm.ima.last_sample[0] & 0xFF);
893 Stream_Write_UINT8(context->common.buffer,
894 (context->adpcm.ima.last_sample[0] >> 8) & 0xFF);
895 Stream_Write_UINT8(context->common.buffer, (BYTE)context->adpcm.ima.last_step[0]);
896 Stream_Write_UINT8(context->common.buffer, 0);
898 if (context->common.format.nChannels > 1)
900 Stream_Write_UINT8(context->common.buffer,
901 context->adpcm.ima.last_sample[1] & 0xFF);
902 Stream_Write_UINT8(context->common.buffer,
903 (context->adpcm.ima.last_sample[1] >> 8) & 0xFF);
904 Stream_Write_UINT8(context->common.buffer, (BYTE)context->adpcm.ima.last_step[1]);
905 Stream_Write_UINT8(context->common.buffer, 0);
909 if (context->common.format.nChannels > 1)
911 BYTE* dst = Stream_Pointer(context->common.buffer);
914 for (
size_t i = 0; i < 16; i++)
916 const INT16 sample = (INT16)(((UINT16)(*src)) | (((UINT16)(*(src + 1))) << 8));
918 const BYTE encoded = dsp_encode_ima_adpcm_sample(&context->adpcm, i % 2, sample);
919 dst[ima_stereo_encode_map[i].byte_num] |= encoded
920 << ima_stereo_encode_map[i].byte_shift;
923 if (!Stream_SafeSeek(context->common.buffer, 8))
929 INT16 sample = (INT16)(((UINT16)(*src)) | (((UINT16)(*(src + 1))) << 8));
931 BYTE encoded = dsp_encode_ima_adpcm_sample(&context->adpcm, 0, sample);
932 sample = (INT16)(((UINT16)(*src)) | (((UINT16)(*(src + 1))) << 8));
934 encoded |= dsp_encode_ima_adpcm_sample(&context->adpcm, 0, sample) << 4;
935 Stream_Write_UINT8(context->common.buffer, encoded);
939 if (Stream_GetPosition(context->common.buffer) >= context->adpcm.ima.packet_size)
941 BYTE* bsrc = Stream_Buffer(context->common.buffer);
942 Stream_Write(out, bsrc, context->adpcm.ima.packet_size);
943 Stream_ResetPosition(context->common.buffer);
956static const INT32 ms_adpcm_adaptation_table[] = { 230, 230, 230, 230, 307, 409, 512, 614,
957 768, 614, 512, 409, 307, 230, 230, 230 };
959static const INT32 ms_adpcm_coeffs1[7] = { 256, 512, 0, 192, 240, 460, 392 };
961static const INT32 ms_adpcm_coeffs2[7] = { 0, -256, 0, 64, 0, -208, -232 };
963static inline INT16 freerdp_dsp_decode_ms_adpcm_sample(ADPCM* WINPR_RESTRICT adpcm, BYTE sample,
966 WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ms.sample1));
967 WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ms.sample2));
968 WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ms.delta));
969 WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ms.predictor));
971 const INT8 nibble = (INT8)((sample & 0x08) ? (sample - 16) : sample);
972 const BYTE predictor = adpcm->ms.predictor[channel];
974 if (predictor < ARRAYSIZE(ms_adpcm_coeffs1))
975 coeff1 = ms_adpcm_coeffs1[predictor];
978 if (predictor < ARRAYSIZE(ms_adpcm_coeffs2))
979 coeff2 = ms_adpcm_coeffs2[predictor];
981 ((adpcm->ms.sample1[channel] * coeff1) + (adpcm->ms.sample2[channel] * coeff2)) / 256;
982 presample += nibble * adpcm->ms.delta[channel];
984 if (presample > 32767)
986 else if (presample < -32768)
989 adpcm->ms.sample2[channel] = adpcm->ms.sample1[channel];
990 adpcm->ms.sample1[channel] = presample;
993 if (sample < ARRAYSIZE(ms_adpcm_adaptation_table))
994 tableval = ms_adpcm_adaptation_table[sample];
996 adpcm->ms.delta[channel] = adpcm->ms.delta[channel] * tableval / 256;
998 if (adpcm->ms.delta[channel] < 16)
999 adpcm->ms.delta[channel] = 16;
1001 return (INT16)presample;
1004static BOOL valid_ms_adpcm_format(
const FREERDP_DSP_CONTEXT* WINPR_RESTRICT context)
1006 WINPR_ASSERT(context);
1007 if (context->common.format.wFormatTag != WAVE_FORMAT_ADPCM)
1009 if (context->common.format.nBlockAlign <= 4ULL)
1011 if (context->common.format.nChannels < 1)
1013 if (context->common.format.wBitsPerSample == 0)
1018static BOOL freerdp_dsp_decode_ms_adpcm(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
1019 const BYTE* WINPR_RESTRICT src,
size_t size,
1022 if (!valid_ms_adpcm_format(context))
1024 const size_t out_size = size * 4;
1025 const UINT32 channels = context->common.format.nChannels;
1026 const UINT32 block_size = context->common.format.nBlockAlign;
1028 if (!Stream_EnsureCapacity(out, out_size))
1033 if (size % block_size == 0)
1040 context->adpcm.ms.predictor[0] = *src++;
1041 context->adpcm.ms.predictor[1] = *src++;
1042 context->adpcm.ms.delta[0] = read_int16(src);
1044 context->adpcm.ms.delta[1] = read_int16(src);
1046 context->adpcm.ms.sample1[0] = read_int16(src);
1048 context->adpcm.ms.sample1[1] = read_int16(src);
1050 context->adpcm.ms.sample2[0] = read_int16(src);
1052 context->adpcm.ms.sample2[1] = read_int16(src);
1055 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample2[0]);
1056 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample2[1]);
1057 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample1[0]);
1058 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample1[1]);
1065 context->adpcm.ms.predictor[0] = *src++;
1066 context->adpcm.ms.delta[0] = read_int16(src);
1068 context->adpcm.ms.sample1[0] = read_int16(src);
1070 context->adpcm.ms.sample2[0] = read_int16(src);
1073 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample2[0]);
1074 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample1[0]);
1083 const BYTE sample = *src++;
1086 out, freerdp_dsp_decode_ms_adpcm_sample(&context->adpcm, sample >> 4, 0));
1088 out, freerdp_dsp_decode_ms_adpcm_sample(&context->adpcm, sample & 0x0F, 1));
1093 const BYTE sample = *src++;
1096 out, freerdp_dsp_decode_ms_adpcm_sample(&context->adpcm, sample >> 4, 0));
1098 out, freerdp_dsp_decode_ms_adpcm_sample(&context->adpcm, sample & 0x0F, 1));
1105 const BYTE sample = *src++;
1107 Stream_Write_INT16(out,
1108 freerdp_dsp_decode_ms_adpcm_sample(&context->adpcm, sample >> 4, 0));
1110 out, freerdp_dsp_decode_ms_adpcm_sample(&context->adpcm, sample & 0x0F, 0));
1117static BYTE freerdp_dsp_encode_ms_adpcm_sample(ADPCM* WINPR_RESTRICT adpcm, INT32 sample,
1120 WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ms.sample1));
1121 WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ms.sample2));
1122 WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ms.delta));
1123 WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ms.predictor));
1126 ((adpcm->ms.sample1[channel] * ms_adpcm_coeffs1[adpcm->ms.predictor[channel]]) +
1127 (adpcm->ms.sample2[channel] * ms_adpcm_coeffs2[adpcm->ms.predictor[channel]])) /
1129 INT32 errordelta = (sample - presample) / adpcm->ms.delta[channel];
1131 if ((sample - presample) % adpcm->ms.delta[channel] > adpcm->ms.delta[channel] / 2)
1136 else if (errordelta < -8)
1139 presample += adpcm->ms.delta[channel] * errordelta;
1141 if (presample > 32767)
1143 else if (presample < -32768)
1146 adpcm->ms.sample2[channel] = adpcm->ms.sample1[channel];
1147 adpcm->ms.sample1[channel] = presample;
1148 const size_t offset = (((BYTE)errordelta) & 0x0F);
1149 WINPR_ASSERT(offset < ARRAYSIZE(ms_adpcm_adaptation_table));
1150 adpcm->ms.delta[channel] = adpcm->ms.delta[channel] * ms_adpcm_adaptation_table[offset] / 256;
1152 if (adpcm->ms.delta[channel] < 16)
1153 adpcm->ms.delta[channel] = 16;
1155 return ((BYTE)errordelta) & 0x0F;
1158static BOOL freerdp_dsp_encode_ms_adpcm(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
1159 const BYTE* WINPR_RESTRICT src,
size_t size,
1162 if (!valid_ms_adpcm_format(context))
1165 const size_t step = 8 + ((context->common.format.nChannels > 1) ? 4 : 0);
1167 if (!Stream_EnsureRemainingCapacity(out, size))
1170 const size_t start = Stream_GetPosition(out);
1172 if (context->adpcm.ms.delta[0] < 16)
1173 context->adpcm.ms.delta[0] = 16;
1175 if (context->adpcm.ms.delta[1] < 16)
1176 context->adpcm.ms.delta[1] = 16;
1178 while (size >= step)
1180 if ((Stream_GetPosition(out) - start) % context->common.format.nBlockAlign == 0)
1182 if (context->common.format.nChannels > 1)
1184 Stream_Write_UINT8(out, context->adpcm.ms.predictor[0]);
1185 Stream_Write_UINT8(out, context->adpcm.ms.predictor[1]);
1186 Stream_Write_UINT8(out, (context->adpcm.ms.delta[0] & 0xFF));
1187 Stream_Write_UINT8(out, ((context->adpcm.ms.delta[0] >> 8) & 0xFF));
1188 Stream_Write_UINT8(out, (context->adpcm.ms.delta[1] & 0xFF));
1189 Stream_Write_UINT8(out, ((context->adpcm.ms.delta[1] >> 8) & 0xFF));
1191 context->adpcm.ms.sample1[0] = read_int16(src + 4);
1192 context->adpcm.ms.sample1[1] = read_int16(src + 6);
1193 context->adpcm.ms.sample2[0] = read_int16(src + 0);
1194 context->adpcm.ms.sample2[1] = read_int16(src + 2);
1196 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample1[0]);
1197 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample1[1]);
1198 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample2[0]);
1199 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample2[1]);
1206 Stream_Write_UINT8(out, context->adpcm.ms.predictor[0]);
1207 Stream_Write_UINT8(out, (BYTE)(context->adpcm.ms.delta[0] & 0xFF));
1208 Stream_Write_UINT8(out, (BYTE)((context->adpcm.ms.delta[0] >> 8) & 0xFF));
1210 context->adpcm.ms.sample1[0] = read_int16(src + 2);
1211 context->adpcm.ms.sample2[0] = read_int16(src + 0);
1213 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample1[0]);
1214 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample2[0]);
1221 const INT16 sample = read_int16(src);
1224 out, (freerdp_dsp_encode_ms_adpcm_sample(&context->adpcm, sample, 0) << 4) & 0xFF);
1227 const INT16 sample = read_int16(src);
1231 Stream_Read_UINT8(out, val);
1232 val += freerdp_dsp_encode_ms_adpcm_sample(&context->adpcm, sample,
1233 context->common.format.nChannels > 1 ? 1 : 0);
1234 Stream_Write_UINT8(out, val);
1244FREERDP_DSP_CONTEXT* freerdp_dsp_context_new(BOOL encoder)
1246#if defined(WITH_DSP_FFMPEG)
1247 return freerdp_dsp_ffmpeg_context_new(encoder);
1249 FREERDP_DSP_CONTEXT* context = calloc(1,
sizeof(FREERDP_DSP_CONTEXT));
1254 if (!freerdp_dsp_common_context_init(&context->common, encoder))
1257#if defined(WITH_GSM)
1258 context->gsm = gsm_create();
1266 rc = gsm_option(context->gsm, GSM_OPT_WAV49, &val);
1272#if defined(WITH_LAME)
1276 context->lame = lame_init();
1283 context->hip = hip_decode_init();
1290#if defined(WITH_FAAD2)
1294 context->faad = NeAACDecOpen();
1303 freerdp_dsp_context_free(context);
1308void freerdp_dsp_context_free(FREERDP_DSP_CONTEXT* context)
1313#if defined(WITH_FDK_AAC)
1316 fdk_aac_dsp_uninit(ctx);
1319#if defined(WITH_DSP_FFMPEG)
1320 freerdp_dsp_ffmpeg_context_free(context);
1323 freerdp_dsp_common_context_uninit(&context->common);
1325#if defined(WITH_GSM)
1326 gsm_destroy(context->gsm);
1328#if defined(WITH_LAME)
1330 if (context->common.encoder)
1331 lame_close(context->lame);
1333 hip_decode_exit(context->hip);
1336#if defined(WITH_OPUS)
1338 if (context->opus_decoder)
1339 opus_decoder_destroy(context->opus_decoder);
1340 if (context->opus_encoder)
1341 opus_encoder_destroy(context->opus_encoder);
1344#if defined(WITH_FAAD2)
1346 if (!context->common.encoder)
1347 NeAACDecClose(context->faad);
1350#if defined(WITH_FAAC)
1353 faacEncClose(context->faac);
1356#if defined(WITH_SOXR)
1357 soxr_delete(context->sox);
1364BOOL freerdp_dsp_encode(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
1366 const BYTE* WINPR_RESTRICT pdata,
size_t length,
1369#if defined(WITH_FDK_AAC)
1372 switch (ctx->format.wFormatTag)
1374 case WAVE_FORMAT_AAC_MS:
1375 return fdk_aac_dsp_encode(ctx, srcFormat, pdata, length, out);
1381#if defined(WITH_DSP_FFMPEG)
1382 return freerdp_dsp_ffmpeg_encode(context, srcFormat, pdata, length, out);
1384 if (!context || !context->common.encoder || !srcFormat || !pdata || !out)
1388 const BYTE* resampleData =
nullptr;
1389 size_t resampleLength = 0;
1391 if (!freerdp_dsp_channel_mix(context, pdata, length, srcFormat, &resampleData, &resampleLength))
1394 format.nChannels = context->common.format.nChannels;
1396 const BYTE* data =
nullptr;
1397 if (!freerdp_dsp_resample(context, resampleData, resampleLength, &format, &data, &length))
1400 switch (context->common.format.wFormatTag)
1402 case WAVE_FORMAT_PCM:
1403 if (!Stream_EnsureRemainingCapacity(out, length))
1406 Stream_Write(out, data, length);
1409 case WAVE_FORMAT_ADPCM:
1410 return freerdp_dsp_encode_ms_adpcm(context, data, length, out);
1412 case WAVE_FORMAT_DVI_ADPCM:
1413 return freerdp_dsp_encode_ima_adpcm(context, data, length, out);
1414#if defined(WITH_GSM)
1416 case WAVE_FORMAT_GSM610:
1417 return freerdp_dsp_encode_gsm610(context, data, length, out);
1419#if defined(WITH_LAME)
1421 case WAVE_FORMAT_MPEGLAYER3:
1422 return freerdp_dsp_encode_mp3(context, data, length, out);
1424#if defined(WITH_FAAC)
1426 case WAVE_FORMAT_AAC_MS:
1427 return freerdp_dsp_encode_faac(context, data, length, out);
1429#if defined(WITH_OPUS)
1431 case WAVE_FORMAT_OPUS:
1432 return freerdp_dsp_encode_opus(context, data, length, out);
1442BOOL freerdp_dsp_decode(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
1444 const BYTE* WINPR_RESTRICT data,
size_t length,
wStream* WINPR_RESTRICT out)
1446#if defined(WITH_FDK_AAC)
1449 switch (ctx->format.wFormatTag)
1451 case WAVE_FORMAT_AAC_MS:
1452 return fdk_aac_dsp_decode(ctx, srcFormat, data, length, out);
1458#if defined(WITH_DSP_FFMPEG)
1459 return freerdp_dsp_ffmpeg_decode(context, srcFormat, data, length, out);
1462 if (!context || context->common.encoder || !srcFormat || !data || !out)
1465 switch (context->common.format.wFormatTag)
1467 case WAVE_FORMAT_PCM:
1468 if (!Stream_EnsureRemainingCapacity(out, length))
1471 Stream_Write(out, data, length);
1474 case WAVE_FORMAT_ADPCM:
1475 return freerdp_dsp_decode_ms_adpcm(context, data, length, out);
1477 case WAVE_FORMAT_DVI_ADPCM:
1478 return freerdp_dsp_decode_ima_adpcm(context, data, length, out);
1479#if defined(WITH_GSM)
1481 case WAVE_FORMAT_GSM610:
1482 return freerdp_dsp_decode_gsm610(context, data, length, out);
1484#if defined(WITH_LAME)
1486 case WAVE_FORMAT_MPEGLAYER3:
1487 return freerdp_dsp_decode_mp3(context, data, length, out);
1489#if defined(WITH_FAAD2)
1491 case WAVE_FORMAT_AAC_MS:
1492 return freerdp_dsp_decode_faad(context, data, length, out);
1495#if defined(WITH_OPUS)
1496 case WAVE_FORMAT_OPUS:
1497 return freerdp_dsp_decode_opus(context, data, length, out);
1507BOOL freerdp_dsp_supports_format(
const AUDIO_FORMAT* WINPR_RESTRICT format, BOOL encode)
1509#if defined(WITH_FDK_AAC)
1510 switch (format->wFormatTag)
1512 case WAVE_FORMAT_AAC_MS:
1520#if defined(WITH_DSP_FFMPEG)
1521 return freerdp_dsp_ffmpeg_supports_format(format, encode);
1524#if !defined(WITH_DSP_EXPERIMENTAL)
1525 WINPR_UNUSED(encode);
1527 switch (format->wFormatTag)
1529 case WAVE_FORMAT_PCM:
1531#if defined(WITH_DSP_EXPERIMENTAL)
1533 case WAVE_FORMAT_ADPCM:
1535 case WAVE_FORMAT_DVI_ADPCM:
1538#if defined(WITH_GSM)
1540 case WAVE_FORMAT_GSM610:
1541#if defined(WITH_DSP_EXPERIMENTAL)
1547#if defined(WITH_LAME)
1549 case WAVE_FORMAT_MPEGLAYER3:
1550#if defined(WITH_DSP_EXPERIMENTAL)
1557 case WAVE_FORMAT_AAC_MS:
1558#if defined(WITH_FAAD2)
1563#if defined(WITH_FAAC)
1569#if defined(WITH_FDK_AAC)
1575#if defined(WITH_OPUS)
1576 case WAVE_FORMAT_OPUS:
1577 return opus_is_valid_samplerate(format);
1587BOOL freerdp_dsp_context_reset(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
1589 WINPR_ATTR_UNUSED UINT32 FramesPerPacket)
1591#if defined(WITH_FDK_AAC)
1592 WINPR_ASSERT(targetFormat);
1593 if (targetFormat->wFormatTag == WAVE_FORMAT_AAC_MS)
1596 fdk_aac_dsp_uninit(ctx);
1597 ctx->format = *targetFormat;
1598 return fdk_aac_dsp_init(ctx, FramesPerPacket);
1602#if defined(WITH_DSP_FFMPEG)
1603 return freerdp_dsp_ffmpeg_context_reset(context, targetFormat);
1606 if (!context || !targetFormat)
1609 context->common.format = *targetFormat;
1611 switch (context->common.format.wFormatTag)
1613#if defined(WITH_LAME)
1614 case WAVE_FORMAT_MPEGLAYER3:
1615 if (!valid_mp3_format(context))
1619 case WAVE_FORMAT_ADPCM:
1620 if (!valid_ms_adpcm_format(context))
1623 case WAVE_FORMAT_DVI_ADPCM:
1625 if (!valid_ima_adpcm_format(context))
1627 if (FramesPerPacket == 0)
1630 const size_t min_frame_data = 1ull * context->common.format.wBitsPerSample *
1631 context->common.format.nChannels * FramesPerPacket;
1632 const size_t data_per_block = (1ULL * context->common.format.nBlockAlign -
1633 4ULL * context->common.format.nChannels) *
1635 size_t nb_block_per_packet = min_frame_data / data_per_block;
1637 if (min_frame_data % data_per_block)
1638 nb_block_per_packet++;
1640 context->adpcm.ima.packet_size =
1641 nb_block_per_packet * context->common.format.nBlockAlign;
1642 if (!Stream_EnsureCapacity(context->common.buffer, context->adpcm.ima.packet_size))
1644 Stream_ResetPosition(context->common.buffer);
1651#if defined(WITH_OPUS)
1653 if (opus_is_valid_samplerate(&context->common.format))
1655 if (!context->common.encoder)
1657 int opus_error = OPUS_OK;
1659 context->opus_decoder = opus_decoder_create(
1660 WINPR_ASSERTING_INT_CAST(opus_int32, context->common.format.nSamplesPerSec),
1661 context->common.format.nChannels, &opus_error);
1662 if (opus_error != OPUS_OK)
1667 int opus_error = OPUS_OK;
1669 context->opus_encoder = opus_encoder_create(
1670 WINPR_ASSERTING_INT_CAST(opus_int32, context->common.format.nSamplesPerSec),
1671 context->common.format.nChannels, OPUS_APPLICATION_VOIP, &opus_error);
1672 if (opus_error != OPUS_OK)
1676 opus_encoder_ctl(context->opus_encoder,
1677 OPUS_SET_BITRATE(context->common.format.nAvgBytesPerSec * 8));
1678 if (opus_error != OPUS_OK)
1684#if defined(WITH_FAAD2)
1685 context->faadSetup = FALSE;
1687#if defined(WITH_FAAC)
1689 if (context->common.encoder)
1691 faacEncConfigurationPtr cfg =
nullptr;
1694 faacEncClose(context->faac);
1696 context->faac = faacEncOpen(targetFormat->nSamplesPerSec, targetFormat->nChannels,
1697 &context->faacInputSamples, &context->faacMaxOutputBytes);
1702 cfg = faacEncGetCurrentConfiguration(context->faac);
1703 cfg->inputFormat = FAAC_INPUT_16BIT;
1704 cfg->outputFormat = 0;
1705 cfg->mpegVersion = MPEG4;
1707 cfg->bandWidth = targetFormat->nAvgBytesPerSec;
1708 const int rc = faacEncSetConfiguration(context->faac, cfg);
1714#if defined(WITH_SOXR)
1716 soxr_io_spec_t iospec = soxr_io_spec(SOXR_INT16, SOXR_INT16);
1717 soxr_error_t error =
nullptr;
1719 soxr_delete(context->sox);
1721 soxr_create(context->common.format.nSamplesPerSec, targetFormat->nSamplesPerSec,
1722 targetFormat->nChannels, &error, &iospec,
nullptr,
nullptr);
1724 if (!context->sox || (error !=
nullptr))
1734 WINPR_ASSERT(context);
1735 context->encoder = encode;
1736 context->buffer = Stream_New(
nullptr, 1024);
1737 if (!context->buffer)
1740 context->channelmix = Stream_New(
nullptr, 1024);
1741 if (!context->channelmix)
1744 context->resample = Stream_New(
nullptr, 1024);
1745 if (!context->resample)
1751 freerdp_dsp_common_context_uninit(context);
1757 WINPR_ASSERT(context);
1759 Stream_Free(context->buffer, TRUE);
1760 Stream_Free(context->channelmix, TRUE);
1761 Stream_Free(context->resample, TRUE);
1763 context->buffer =
nullptr;
1764 context->channelmix =
nullptr;
1765 context->resample =
nullptr;