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)
240 if (srcFormat->wFormatTag != WAVE_FORMAT_PCM)
242 WLog_ERR(TAG,
"requires %s for sample input, got %s",
243 audio_format_get_tag_string(WAVE_FORMAT_PCM),
244 audio_format_get_tag_string(srcFormat->wFormatTag));
250 format.wFormatTag = WAVE_FORMAT_UNKNOWN;
251 format.wBitsPerSample = 0;
253 if (audio_format_compatible(&format, &context->common.format))
260#if defined(WITH_SOXR)
261 const size_t srcBytesPerFrame = (srcFormat->wBitsPerSample > 8) ? 2 : 1;
262 const size_t dstBytesPerFrame = (context->common.format.wBitsPerSample > 8) ? 2 : 1;
263 const size_t srcChannels = srcFormat->nChannels;
264 const size_t dstChannels = context->common.format.nChannels;
265 const size_t sbytes = srcChannels * srcBytesPerFrame;
266 const size_t sframes = size / sbytes;
267 const size_t rbytes = dstBytesPerFrame * dstChannels;
269 const size_t rframes =
270 (sframes * context->common.format.nSamplesPerSec + (srcFormat->nSamplesPerSec + 1) / 2) /
271 srcFormat->nSamplesPerSec;
272 const size_t rsize = rframes * rbytes;
274 if (!Stream_EnsureCapacity(context->common.resample, rsize))
280 soxr_process(context->sox, src, sframes, &idone, Stream_Buffer(context->common.resample),
281 Stream_Capacity(context->common.resample) / rbytes, &odone);
282 if (!Stream_SetLength(context->common.resample, odone * rbytes))
285 *data = Stream_Buffer(context->common.resample);
286 *length = Stream_Length(context->common.resample);
287 return (error == 0) != 0;
289 WLog_ERR(TAG,
"Missing resample support, recompile -DWITH_SOXR=ON or -DWITH_DSP_FFMPEG=ON");
301static const INT16 ima_step_index_table[] = {
302 -1, -1, -1, -1, 2, 4, 6, 8, -1, -1, -1, -1, 2, 4, 6, 8
305static const INT16 ima_step_size_table[] = {
306 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 19, 21, 23,
307 25, 28, 31, 34, 37, 41, 45, 50, 55, 60, 66, 73, 80,
308 88, 97, 107, 118, 130, 143, 157, 173, 190, 209, 230, 253, 279,
309 307, 337, 371, 408, 449, 494, 544, 598, 658, 724, 796, 876, 963,
310 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024, 3327,
311 3660, 4026, 4428, 4871, 5358, 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487,
312 12635, 13899, 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
315static inline void dsp_ima_clamp_step(ADPCM* WINPR_RESTRICT adpcm,
size_t channel)
318 if (adpcm->ima.last_step[channel] < 0)
319 adpcm->ima.last_step[channel] = 0;
321 const size_t size = ARRAYSIZE(ima_step_size_table);
322 if ((
size_t)adpcm->ima.last_step[channel] >= size)
323 adpcm->ima.last_step[channel] = (INT16)(size - 1);
326static UINT16 dsp_decode_ima_adpcm_sample(ADPCM* WINPR_RESTRICT adpcm,
unsigned int channel,
329 WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ima.last_step));
330 WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ima.last_sample));
332 const INT16 offset = adpcm->ima.last_step[channel];
333 WINPR_ASSERT(offset >= 0);
334 WINPR_ASSERT(WINPR_CXX_COMPAT_CAST(
size_t, offset) < ARRAYSIZE(ima_step_size_table));
336 const INT32 ss = ima_step_size_table[offset];
351 d += adpcm->ima.last_sample[channel];
358 adpcm->ima.last_sample[channel] = (INT16)d;
360 WINPR_ASSERT(sample < ARRAYSIZE(ima_step_index_table));
361 const int32_t last = adpcm->ima.last_step[channel] + ima_step_index_table[sample];
362 adpcm->ima.last_step[channel] = WINPR_ASSERTING_INT_CAST(int16_t, last);
364 dsp_ima_clamp_step(adpcm, channel);
369static BOOL valid_ima_adpcm_format(
const FREERDP_DSP_CONTEXT* WINPR_RESTRICT context)
371 WINPR_ASSERT(context);
372 if (context->common.format.wFormatTag != WAVE_FORMAT_DVI_ADPCM)
374 if (context->common.format.nBlockAlign <= 4ULL)
376 if (context->common.format.nChannels < 1)
378 if (context->common.format.wBitsPerSample == 0)
383static BOOL freerdp_dsp_decode_ima_adpcm(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
384 const BYTE* WINPR_RESTRICT src,
size_t size,
387 if (!valid_ima_adpcm_format(context))
390 size_t out_size = size * 4ull;
391 const UINT32 block_size = context->common.format.nBlockAlign;
392 const UINT32 channels = context->common.format.nChannels;
394 if (!Stream_EnsureCapacity(out, out_size))
399 if (size % block_size == 0)
404 context->adpcm.ima.last_sample[0] =
405 (INT16)(((UINT16)(*src)) | (((UINT16)(*(src + 1))) << 8));
406 context->adpcm.ima.last_step[0] = (INT16)(*(src + 2));
408 dsp_ima_clamp_step(&context->adpcm, 0u);
418 context->adpcm.ima.last_sample[1] =
419 (INT16)(((UINT16)(*src)) | (((UINT16)(*(src + 1))) << 8));
420 context->adpcm.ima.last_step[1] = (INT16)(*(src + 2));
422 dsp_ima_clamp_step(&context->adpcm, 1u);
433 for (
size_t i = 0; i < 8; i++)
435 BYTE* dst = Stream_Pointer(out);
437 const unsigned channel = (i < 4 ? 0 : 1);
439 const BYTE sample = ((*src) & 0x0f);
440 const UINT16 decoded =
441 dsp_decode_ima_adpcm_sample(&context->adpcm, channel, sample);
442 dst[((i & 3) << 3) + (channel << 1u)] = (decoded & 0xFF);
443 dst[((i & 3) << 3) + (channel << 1u) + 1] = (decoded >> 8);
446 const BYTE sample = ((*src) >> 4);
447 const UINT16 decoded =
448 dsp_decode_ima_adpcm_sample(&context->adpcm, channel, sample);
449 dst[((i & 3) << 3) + (channel << 1u) + 4] = (decoded & 0xFF);
450 dst[((i & 3) << 3) + (channel << 1u) + 5] = (decoded >> 8);
455 if (!Stream_SafeSeek(out, 32))
463 BYTE* dst = Stream_Pointer(out);
464 if (!Stream_SafeSeek(out, 4))
468 const BYTE sample = ((*src) & 0x0f);
469 const UINT16 decoded = dsp_decode_ima_adpcm_sample(&context->adpcm, 0, sample);
470 *dst++ = (decoded & 0xFF);
471 *dst++ = (decoded >> 8);
474 const BYTE sample = ((*src) >> 4);
475 const UINT16 decoded = dsp_decode_ima_adpcm_sample(&context->adpcm, 0, sample);
476 *dst++ = (decoded & 0xFF);
477 *dst++ = (decoded >> 8);
488static BOOL freerdp_dsp_decode_gsm610(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
489 const BYTE* WINPR_RESTRICT src,
size_t size,
494 while (offset < size)
496 gsm_signal gsmBlockBuffer[160] = WINPR_C_ARRAY_INIT;
498 gsm_decode(context->gsm,
499 WINPR_CAST_CONST_PTR_AWAY( &src[offset], gsm_byte*),
505 if ((offset % 65) == 0)
510 if (!Stream_EnsureRemainingCapacity(out,
sizeof(gsmBlockBuffer)))
513 Stream_Write(out, (
void*)gsmBlockBuffer,
sizeof(gsmBlockBuffer));
519static BOOL freerdp_dsp_encode_gsm610(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
520 const BYTE* WINPR_RESTRICT src,
size_t size,
525 while (offset < size)
527 const gsm_signal* signal = WINPR_PACKED_ALIGN_CAST(
const gsm_signal*, &src[offset]);
529 if (!Stream_EnsureRemainingCapacity(out,
sizeof(gsm_frame)))
532 gsm_encode(context->gsm,
533 WINPR_CAST_CONST_PTR_AWAY( signal, gsm_signal*),
534 Stream_Pointer(out));
536 if ((offset % 65) == 0)
537 Stream_Seek(out, 33);
539 Stream_Seek(out, 32);
548#if defined(WITH_LAME)
549static BOOL valid_mp3_format(
const FREERDP_DSP_CONTEXT* WINPR_RESTRICT context)
551 WINPR_ASSERT(context);
552 if (context->common.format.wFormatTag != WAVE_FORMAT_MPEGLAYER3)
554 if (context->common.format.nChannels < 1)
556 if (context->common.format.wBitsPerSample == 0)
558 if (context->common.format.nSamplesPerSec == 0)
563static BOOL freerdp_dsp_decode_mp3(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
564 const BYTE* WINPR_RESTRICT src,
size_t size,
567 if (!context || !src || !out)
569 if (!valid_mp3_format(context))
571 const size_t buffer_size =
572 2 * context->common.format.nChannels * context->common.format.nSamplesPerSec;
574 if (!Stream_EnsureCapacity(context->common.buffer, 2 * buffer_size))
577 short* pcm_l = Stream_BufferAs(context->common.buffer,
short);
578 short* pcm_r = Stream_BufferAs(context->common.buffer,
short) + buffer_size;
579 const int rc = hip_decode(
581 WINPR_CAST_CONST_PTR_AWAY( src,
unsigned char*), size,
587 if (!Stream_EnsureRemainingCapacity(out, (
size_t)rc * context->common.format.nChannels * 2))
590 for (
int x = 0; x < rc; x++)
592 Stream_Write_UINT16(out, (UINT16)pcm_l[x]);
593 Stream_Write_UINT16(out, (UINT16)pcm_r[x]);
599static BOOL freerdp_dsp_encode_mp3(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
600 const BYTE* WINPR_RESTRICT src,
size_t size,
603 if (!context || !src || !out)
606 if (!valid_mp3_format(context))
609 size_t samples_per_channel =
610 size / context->common.format.nChannels / context->common.format.wBitsPerSample / 8;
613 if (!Stream_EnsureRemainingCapacity(out, 5 / 4 * samples_per_channel + 7200))
616 samples_per_channel = size / 2 / context->common.format.nChannels;
617 const size_t outLen = Stream_GetRemainingCapacity(out);
618 if ((outLen > INT_MAX) || (samples_per_channel > INT_MAX))
621 const int rc = lame_encode_buffer_interleaved(
623 WINPR_CAST_CONST_PTR_AWAY(WINPR_PACKED_ALIGN_CAST(
const short*, src),
short*),
624 WINPR_ASSERTING_INT_CAST(
int, samples_per_channel), Stream_Pointer(out),
625 WINPR_ASSERTING_INT_CAST(
int, outLen));
630 Stream_Seek(out, (
size_t)rc);
635#if defined(WITH_FAAC)
636static BOOL freerdp_dsp_encode_faac(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
637 const BYTE* WINPR_RESTRICT src,
size_t size,
640 const int16_t* inSamples = WINPR_PACKED_ALIGN_CAST(
const int16_t*, src);
642 if (!context || !src || !out)
645 const unsigned int bpp = context->common.format.wBitsPerSample / 8;
646 const size_t 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))
659 const size_t outLen = Stream_GetRemainingCapacity(out);
660 if ((outLen > UINT_MAX) || (context->faacInputSamples > UINT_MAX))
663 faacEncEncode(context->faac, Stream_BufferAs(context->common.buffer, int32_t),
664 WINPR_ASSERTING_INT_CAST(
unsigned int, context->faacInputSamples),
665 Stream_Pointer(out), WINPR_ASSERTING_INT_CAST(
unsigned int, outLen));
669 Stream_Seek(out, (
size_t)rc);
670 Stream_ResetPosition(context->common.buffer);
678#if defined(WITH_OPUS)
679static BOOL freerdp_dsp_decode_opus(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
680 const BYTE* WINPR_RESTRICT src,
size_t size,
683 if (!context || !src || !out)
687 const size_t max_size = OPUS_MAX_FRAMES * context->common.format.nChannels *
sizeof(int16_t);
688 if (!Stream_EnsureRemainingCapacity(out, max_size))
691 const opus_int32 frames =
692 opus_decode(context->opus_decoder, src, WINPR_ASSERTING_INT_CAST(opus_int32, size),
693 Stream_Pointer(out), OPUS_MAX_FRAMES, 0);
697 Stream_Seek(out, (
size_t)frames * context->common.format.nChannels *
sizeof(int16_t));
702static BOOL freerdp_dsp_encode_opus(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
703 const BYTE* WINPR_RESTRICT src,
size_t size,
706 if (!context || !src || !out)
710 const size_t max_size = OPUS_MAX_FRAMES * context->common.format.nChannels *
sizeof(int16_t);
711 if (!Stream_EnsureRemainingCapacity(out, max_size))
714 const size_t src_frames = size /
sizeof(opus_int16) / context->common.format.nChannels;
715 const opus_int16* src_data = WINPR_PACKED_ALIGN_CAST(
const opus_int16*, src);
716 const opus_int32 frames = opus_encode(
717 context->opus_encoder, src_data, WINPR_ASSERTING_INT_CAST(opus_int32, src_frames),
718 Stream_Pointer(out), WINPR_ASSERTING_INT_CAST(opus_int32, max_size));
721 return Stream_SafeSeek(out,
722 (
size_t)frames * context->common.format.nChannels *
sizeof(int16_t));
726#if defined(WITH_FAAD2)
727static BOOL freerdp_dsp_decode_faad(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
728 const BYTE* WINPR_RESTRICT src,
size_t size,
731 NeAACDecFrameInfo info;
734 if (!context || !src || !out)
737 if (!context->faadSetup)
744 unsigned long samplerate = 0;
745 unsigned char channels = 0;
748 const long err = NeAACDecInit(context->faad, cnv.pv,
749 size, &samplerate, &channels);
754 if (channels != context->common.format.nChannels)
757 if (samplerate != context->common.format.nSamplesPerSec)
760 context->faadSetup = TRUE;
763 while (offset < size)
771 const size_t outSize = context->common.format.nSamplesPerSec *
772 context->common.format.nChannels *
773 context->common.format.wBitsPerSample / 8;
775 if (!Stream_EnsureRemainingCapacity(out, outSize))
778 void* sample_buffer = Stream_Pointer(out);
780 cnv.cpv = &src[offset];
781 NeAACDecDecode2(context->faad, &info, cnv.pv, size - offset, &sample_buffer,
782 Stream_GetRemainingCapacity(out));
787 offset += info.bytesconsumed;
789 if (info.samples == 0)
792 Stream_Seek(out, info.samples * context->common.format.wBitsPerSample / 8);
811} ima_stereo_encode_map[] = { { 0, 0 }, { 4, 0 }, { 0, 4 }, { 4, 4 }, { 1, 0 }, { 5, 0 },
812 { 1, 4 }, { 5, 4 }, { 2, 0 }, { 6, 0 }, { 2, 4 }, { 6, 4 },
813 { 3, 0 }, { 7, 0 }, { 3, 4 }, { 7, 4 } };
815static BYTE dsp_encode_ima_adpcm_sample(ADPCM* WINPR_RESTRICT adpcm,
size_t channel, INT16 sample)
817 WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ima.last_step));
818 WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ima.last_sample));
820 const INT16 offset = adpcm->ima.last_step[channel];
821 WINPR_ASSERT(offset >= 0);
822 WINPR_ASSERT(WINPR_CXX_COMPAT_CAST(
size_t, offset) < ARRAYSIZE(ima_step_size_table));
824 INT32 ss = ima_step_size_table[offset];
825 INT32 e = sample - adpcm->ima.last_sample[channel];
827 INT32 diff = ss >> 3;
863 diff += adpcm->ima.last_sample[channel];
867 else if (diff > 32767)
870 adpcm->ima.last_sample[channel] = (INT16)diff;
872 WINPR_ASSERT(enc < ARRAYSIZE(ima_step_index_table));
873 const int32_t last = adpcm->ima.last_step[channel] + ima_step_index_table[enc];
874 adpcm->ima.last_step[channel] = WINPR_ASSERTING_INT_CAST(int16_t, last);
876 dsp_ima_clamp_step(adpcm, channel);
881static BOOL freerdp_dsp_encode_ima_adpcm(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
882 const BYTE* WINPR_RESTRICT src,
size_t size,
885 if (!valid_ima_adpcm_format(context))
887 if (!Stream_EnsureRemainingCapacity(out, size))
889 if (!Stream_EnsureRemainingCapacity(context->common.buffer, size + 64))
892 const size_t align = (context->common.format.nChannels > 1) ? 32 : 4;
894 while (size >= align)
896 if (Stream_GetPosition(context->common.buffer) % context->common.format.nBlockAlign == 0)
898 Stream_Write_UINT8(context->common.buffer, context->adpcm.ima.last_sample[0] & 0xFF);
899 Stream_Write_UINT8(context->common.buffer,
900 (context->adpcm.ima.last_sample[0] >> 8) & 0xFF);
901 Stream_Write_UINT8(context->common.buffer, (BYTE)context->adpcm.ima.last_step[0]);
902 Stream_Write_UINT8(context->common.buffer, 0);
904 if (context->common.format.nChannels > 1)
906 Stream_Write_UINT8(context->common.buffer,
907 context->adpcm.ima.last_sample[1] & 0xFF);
908 Stream_Write_UINT8(context->common.buffer,
909 (context->adpcm.ima.last_sample[1] >> 8) & 0xFF);
910 Stream_Write_UINT8(context->common.buffer, (BYTE)context->adpcm.ima.last_step[1]);
911 Stream_Write_UINT8(context->common.buffer, 0);
915 if (context->common.format.nChannels > 1)
917 BYTE* dst = Stream_Pointer(context->common.buffer);
920 for (
size_t i = 0; i < 16; i++)
922 const INT16 sample = (INT16)(((UINT16)(*src)) | (((UINT16)(*(src + 1))) << 8));
924 const BYTE encoded = dsp_encode_ima_adpcm_sample(&context->adpcm, i % 2, sample);
925 dst[ima_stereo_encode_map[i].byte_num] |= encoded
926 << ima_stereo_encode_map[i].byte_shift;
929 if (!Stream_SafeSeek(context->common.buffer, 8))
935 INT16 sample = (INT16)(((UINT16)(*src)) | (((UINT16)(*(src + 1))) << 8));
937 BYTE encoded = dsp_encode_ima_adpcm_sample(&context->adpcm, 0, sample);
938 sample = (INT16)(((UINT16)(*src)) | (((UINT16)(*(src + 1))) << 8));
940 encoded |= dsp_encode_ima_adpcm_sample(&context->adpcm, 0, sample) << 4;
941 Stream_Write_UINT8(context->common.buffer, encoded);
945 if (Stream_GetPosition(context->common.buffer) >= context->adpcm.ima.packet_size)
947 BYTE* bsrc = Stream_Buffer(context->common.buffer);
948 Stream_Write(out, bsrc, context->adpcm.ima.packet_size);
949 Stream_ResetPosition(context->common.buffer);
962static const INT32 ms_adpcm_adaptation_table[] = { 230, 230, 230, 230, 307, 409, 512, 614,
963 768, 614, 512, 409, 307, 230, 230, 230 };
965static const INT32 ms_adpcm_coeffs1[7] = { 256, 512, 0, 192, 240, 460, 392 };
967static const INT32 ms_adpcm_coeffs2[7] = { 0, -256, 0, 64, 0, -208, -232 };
969static inline INT16 freerdp_dsp_decode_ms_adpcm_sample(ADPCM* WINPR_RESTRICT adpcm, BYTE sample,
972 WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ms.sample1));
973 WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ms.sample2));
974 WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ms.delta));
975 WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ms.predictor));
977 const INT8 nibble = (INT8)((sample & 0x08) ? (sample - 16) : sample);
978 const BYTE predictor = adpcm->ms.predictor[channel];
980 if (predictor < ARRAYSIZE(ms_adpcm_coeffs1))
981 coeff1 = ms_adpcm_coeffs1[predictor];
984 if (predictor < ARRAYSIZE(ms_adpcm_coeffs2))
985 coeff2 = ms_adpcm_coeffs2[predictor];
987 ((adpcm->ms.sample1[channel] * coeff1) + (adpcm->ms.sample2[channel] * coeff2)) / 256;
988 presample += nibble * adpcm->ms.delta[channel];
990 if (presample > 32767)
992 else if (presample < -32768)
995 adpcm->ms.sample2[channel] = adpcm->ms.sample1[channel];
996 adpcm->ms.sample1[channel] = presample;
999 if (sample < ARRAYSIZE(ms_adpcm_adaptation_table))
1000 tableval = ms_adpcm_adaptation_table[sample];
1002 adpcm->ms.delta[channel] = adpcm->ms.delta[channel] * tableval / 256;
1004 if (adpcm->ms.delta[channel] < 16)
1005 adpcm->ms.delta[channel] = 16;
1007 return (INT16)presample;
1010static BOOL valid_ms_adpcm_format(
const FREERDP_DSP_CONTEXT* WINPR_RESTRICT context)
1012 WINPR_ASSERT(context);
1013 if (context->common.format.wFormatTag != WAVE_FORMAT_ADPCM)
1015 if (context->common.format.nBlockAlign <= 4ULL)
1017 if (context->common.format.nChannels < 1)
1019 if (context->common.format.wBitsPerSample == 0)
1024static BOOL freerdp_dsp_decode_ms_adpcm(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
1025 const BYTE* WINPR_RESTRICT src,
size_t size,
1028 if (!valid_ms_adpcm_format(context))
1030 const size_t out_size = size * 4;
1031 const UINT32 channels = context->common.format.nChannels;
1032 const UINT32 block_size = context->common.format.nBlockAlign;
1034 if (!Stream_EnsureCapacity(out, out_size))
1039 if (size % block_size == 0)
1046 context->adpcm.ms.predictor[0] = *src++;
1047 context->adpcm.ms.predictor[1] = *src++;
1048 context->adpcm.ms.delta[0] = read_int16(src);
1050 context->adpcm.ms.delta[1] = read_int16(src);
1052 context->adpcm.ms.sample1[0] = read_int16(src);
1054 context->adpcm.ms.sample1[1] = read_int16(src);
1056 context->adpcm.ms.sample2[0] = read_int16(src);
1058 context->adpcm.ms.sample2[1] = read_int16(src);
1061 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample2[0]);
1062 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample2[1]);
1063 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample1[0]);
1064 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample1[1]);
1071 context->adpcm.ms.predictor[0] = *src++;
1072 context->adpcm.ms.delta[0] = read_int16(src);
1074 context->adpcm.ms.sample1[0] = read_int16(src);
1076 context->adpcm.ms.sample2[0] = read_int16(src);
1079 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample2[0]);
1080 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample1[0]);
1089 const BYTE sample = *src++;
1092 out, freerdp_dsp_decode_ms_adpcm_sample(&context->adpcm, sample >> 4, 0));
1094 out, freerdp_dsp_decode_ms_adpcm_sample(&context->adpcm, sample & 0x0F, 1));
1099 const BYTE sample = *src++;
1102 out, freerdp_dsp_decode_ms_adpcm_sample(&context->adpcm, sample >> 4, 0));
1104 out, freerdp_dsp_decode_ms_adpcm_sample(&context->adpcm, sample & 0x0F, 1));
1111 const BYTE sample = *src++;
1113 Stream_Write_INT16(out,
1114 freerdp_dsp_decode_ms_adpcm_sample(&context->adpcm, sample >> 4, 0));
1116 out, freerdp_dsp_decode_ms_adpcm_sample(&context->adpcm, sample & 0x0F, 0));
1123static BYTE freerdp_dsp_encode_ms_adpcm_sample(ADPCM* WINPR_RESTRICT adpcm, INT32 sample,
1126 WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ms.sample1));
1127 WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ms.sample2));
1128 WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ms.delta));
1129 WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ms.predictor));
1132 ((adpcm->ms.sample1[channel] * ms_adpcm_coeffs1[adpcm->ms.predictor[channel]]) +
1133 (adpcm->ms.sample2[channel] * ms_adpcm_coeffs2[adpcm->ms.predictor[channel]])) /
1135 INT32 errordelta = (sample - presample) / adpcm->ms.delta[channel];
1137 if ((sample - presample) % adpcm->ms.delta[channel] > adpcm->ms.delta[channel] / 2)
1142 else if (errordelta < -8)
1145 presample += adpcm->ms.delta[channel] * errordelta;
1147 if (presample > 32767)
1149 else if (presample < -32768)
1152 adpcm->ms.sample2[channel] = adpcm->ms.sample1[channel];
1153 adpcm->ms.sample1[channel] = presample;
1154 const size_t offset = (((BYTE)errordelta) & 0x0F);
1155 WINPR_ASSERT(offset < ARRAYSIZE(ms_adpcm_adaptation_table));
1156 adpcm->ms.delta[channel] = adpcm->ms.delta[channel] * ms_adpcm_adaptation_table[offset] / 256;
1158 if (adpcm->ms.delta[channel] < 16)
1159 adpcm->ms.delta[channel] = 16;
1161 return ((BYTE)errordelta) & 0x0F;
1164static BOOL freerdp_dsp_encode_ms_adpcm(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
1165 const BYTE* WINPR_RESTRICT src,
size_t size,
1168 if (!valid_ms_adpcm_format(context))
1171 const size_t step = 8 + ((context->common.format.nChannels > 1) ? 4 : 0);
1173 if (!Stream_EnsureRemainingCapacity(out, size))
1176 const size_t start = Stream_GetPosition(out);
1178 if (context->adpcm.ms.delta[0] < 16)
1179 context->adpcm.ms.delta[0] = 16;
1181 if (context->adpcm.ms.delta[1] < 16)
1182 context->adpcm.ms.delta[1] = 16;
1184 while (size >= step)
1186 if ((Stream_GetPosition(out) - start) % context->common.format.nBlockAlign == 0)
1188 if (context->common.format.nChannels > 1)
1190 Stream_Write_UINT8(out, context->adpcm.ms.predictor[0]);
1191 Stream_Write_UINT8(out, context->adpcm.ms.predictor[1]);
1192 Stream_Write_UINT8(out, (context->adpcm.ms.delta[0] & 0xFF));
1193 Stream_Write_UINT8(out, ((context->adpcm.ms.delta[0] >> 8) & 0xFF));
1194 Stream_Write_UINT8(out, (context->adpcm.ms.delta[1] & 0xFF));
1195 Stream_Write_UINT8(out, ((context->adpcm.ms.delta[1] >> 8) & 0xFF));
1197 context->adpcm.ms.sample1[0] = read_int16(src + 4);
1198 context->adpcm.ms.sample1[1] = read_int16(src + 6);
1199 context->adpcm.ms.sample2[0] = read_int16(src + 0);
1200 context->adpcm.ms.sample2[1] = read_int16(src + 2);
1202 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample1[0]);
1203 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample1[1]);
1204 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample2[0]);
1205 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample2[1]);
1212 Stream_Write_UINT8(out, context->adpcm.ms.predictor[0]);
1213 Stream_Write_UINT8(out, (BYTE)(context->adpcm.ms.delta[0] & 0xFF));
1214 Stream_Write_UINT8(out, (BYTE)((context->adpcm.ms.delta[0] >> 8) & 0xFF));
1216 context->adpcm.ms.sample1[0] = read_int16(src + 2);
1217 context->adpcm.ms.sample2[0] = read_int16(src + 0);
1219 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample1[0]);
1220 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample2[0]);
1227 const INT16 sample = read_int16(src);
1230 out, (freerdp_dsp_encode_ms_adpcm_sample(&context->adpcm, sample, 0) << 4) & 0xFF);
1233 const INT16 sample = read_int16(src);
1237 Stream_Read_UINT8(out, val);
1238 val += freerdp_dsp_encode_ms_adpcm_sample(&context->adpcm, sample,
1239 context->common.format.nChannels > 1 ? 1 : 0);
1240 Stream_Write_UINT8(out, val);
1250FREERDP_DSP_CONTEXT* freerdp_dsp_context_new(BOOL encoder)
1252#if defined(WITH_DSP_FFMPEG)
1253 return freerdp_dsp_ffmpeg_context_new(encoder);
1255 FREERDP_DSP_CONTEXT* context = calloc(1,
sizeof(FREERDP_DSP_CONTEXT));
1260 if (!freerdp_dsp_common_context_init(&context->common, encoder))
1263#if defined(WITH_GSM)
1264 context->gsm = gsm_create();
1272 rc = gsm_option(context->gsm, GSM_OPT_WAV49, &val);
1278#if defined(WITH_LAME)
1282 context->lame = lame_init();
1289 context->hip = hip_decode_init();
1296#if defined(WITH_FAAD2)
1300 context->faad = NeAACDecOpen();
1309 freerdp_dsp_context_free(context);
1314void freerdp_dsp_context_free(FREERDP_DSP_CONTEXT* context)
1319#if defined(WITH_FDK_AAC)
1322 fdk_aac_dsp_uninit(ctx);
1325#if defined(WITH_DSP_FFMPEG)
1326 freerdp_dsp_ffmpeg_context_free(context);
1329 freerdp_dsp_common_context_uninit(&context->common);
1331#if defined(WITH_GSM)
1332 gsm_destroy(context->gsm);
1334#if defined(WITH_LAME)
1336 if (context->common.encoder)
1337 lame_close(context->lame);
1339 hip_decode_exit(context->hip);
1342#if defined(WITH_OPUS)
1344 if (context->opus_decoder)
1345 opus_decoder_destroy(context->opus_decoder);
1346 if (context->opus_encoder)
1347 opus_encoder_destroy(context->opus_encoder);
1350#if defined(WITH_FAAD2)
1352 if (!context->common.encoder)
1353 NeAACDecClose(context->faad);
1356#if defined(WITH_FAAC)
1359 faacEncClose(context->faac);
1362#if defined(WITH_SOXR)
1363 soxr_delete(context->sox);
1370BOOL freerdp_dsp_encode(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
1372 const BYTE* WINPR_RESTRICT pdata,
size_t length,
1375#if defined(WITH_FDK_AAC)
1378 switch (ctx->format.wFormatTag)
1380 case WAVE_FORMAT_AAC_MS:
1381 return fdk_aac_dsp_encode(ctx, srcFormat, pdata, length, out);
1387#if defined(WITH_DSP_FFMPEG)
1388 return freerdp_dsp_ffmpeg_encode(context, srcFormat, pdata, length, out);
1390 if (!context || !context->common.encoder || !srcFormat || !pdata || !out)
1394 const BYTE* resampleData =
nullptr;
1395 size_t resampleLength = 0;
1397 if (!freerdp_dsp_channel_mix(context, pdata, length, srcFormat, &resampleData, &resampleLength))
1400 format.nChannels = context->common.format.nChannels;
1402 const BYTE* data =
nullptr;
1403 if (!freerdp_dsp_resample(context, resampleData, resampleLength, &format, &data, &length))
1406 switch (context->common.format.wFormatTag)
1408 case WAVE_FORMAT_PCM:
1409 if (!Stream_EnsureRemainingCapacity(out, length))
1412 Stream_Write(out, data, length);
1415 case WAVE_FORMAT_ADPCM:
1416 return freerdp_dsp_encode_ms_adpcm(context, data, length, out);
1418 case WAVE_FORMAT_DVI_ADPCM:
1419 return freerdp_dsp_encode_ima_adpcm(context, data, length, out);
1420#if defined(WITH_GSM)
1422 case WAVE_FORMAT_GSM610:
1423 return freerdp_dsp_encode_gsm610(context, data, length, out);
1425#if defined(WITH_LAME)
1427 case WAVE_FORMAT_MPEGLAYER3:
1428 return freerdp_dsp_encode_mp3(context, data, length, out);
1430#if defined(WITH_FAAC)
1432 case WAVE_FORMAT_AAC_MS:
1433 return freerdp_dsp_encode_faac(context, data, length, out);
1435#if defined(WITH_OPUS)
1437 case WAVE_FORMAT_OPUS:
1438 return freerdp_dsp_encode_opus(context, data, length, out);
1448BOOL freerdp_dsp_decode(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
1450 const BYTE* WINPR_RESTRICT data,
size_t length,
wStream* WINPR_RESTRICT out)
1452#if defined(WITH_FDK_AAC)
1455 switch (ctx->format.wFormatTag)
1457 case WAVE_FORMAT_AAC_MS:
1458 return fdk_aac_dsp_decode(ctx, srcFormat, data, length, out);
1464#if defined(WITH_DSP_FFMPEG)
1465 return freerdp_dsp_ffmpeg_decode(context, srcFormat, data, length, out);
1468 if (!context || context->common.encoder || !srcFormat || !data || !out)
1471 switch (context->common.format.wFormatTag)
1473 case WAVE_FORMAT_PCM:
1474 if (!Stream_EnsureRemainingCapacity(out, length))
1477 Stream_Write(out, data, length);
1480 case WAVE_FORMAT_ADPCM:
1481 return freerdp_dsp_decode_ms_adpcm(context, data, length, out);
1483 case WAVE_FORMAT_DVI_ADPCM:
1484 return freerdp_dsp_decode_ima_adpcm(context, data, length, out);
1485#if defined(WITH_GSM)
1487 case WAVE_FORMAT_GSM610:
1488 return freerdp_dsp_decode_gsm610(context, data, length, out);
1490#if defined(WITH_LAME)
1492 case WAVE_FORMAT_MPEGLAYER3:
1493 return freerdp_dsp_decode_mp3(context, data, length, out);
1495#if defined(WITH_FAAD2)
1497 case WAVE_FORMAT_AAC_MS:
1498 return freerdp_dsp_decode_faad(context, data, length, out);
1501#if defined(WITH_OPUS)
1502 case WAVE_FORMAT_OPUS:
1503 return freerdp_dsp_decode_opus(context, data, length, out);
1513BOOL freerdp_dsp_supports_format(
const AUDIO_FORMAT* WINPR_RESTRICT format, BOOL encode)
1515#if defined(WITH_FDK_AAC)
1516 switch (format->wFormatTag)
1518 case WAVE_FORMAT_AAC_MS:
1526#if defined(WITH_DSP_FFMPEG)
1527 return freerdp_dsp_ffmpeg_supports_format(format, encode);
1530#if !defined(WITH_DSP_EXPERIMENTAL)
1531 WINPR_UNUSED(encode);
1533 switch (format->wFormatTag)
1535 case WAVE_FORMAT_PCM:
1537#if defined(WITH_DSP_EXPERIMENTAL)
1539 case WAVE_FORMAT_ADPCM:
1541 case WAVE_FORMAT_DVI_ADPCM:
1544#if defined(WITH_GSM)
1546 case WAVE_FORMAT_GSM610:
1547#if defined(WITH_DSP_EXPERIMENTAL)
1553#if defined(WITH_LAME)
1555 case WAVE_FORMAT_MPEGLAYER3:
1556#if defined(WITH_DSP_EXPERIMENTAL)
1563 case WAVE_FORMAT_AAC_MS:
1564#if defined(WITH_FAAD2)
1569#if defined(WITH_FAAC)
1575#if defined(WITH_FDK_AAC)
1581#if defined(WITH_OPUS)
1582 case WAVE_FORMAT_OPUS:
1583 return opus_is_valid_samplerate(format);
1593BOOL freerdp_dsp_context_reset(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
1595 WINPR_ATTR_UNUSED UINT32 FramesPerPacket)
1597#if defined(WITH_FDK_AAC)
1598 WINPR_ASSERT(targetFormat);
1599 if (targetFormat->wFormatTag == WAVE_FORMAT_AAC_MS)
1602 fdk_aac_dsp_uninit(ctx);
1603 ctx->format = *targetFormat;
1604 return fdk_aac_dsp_init(ctx, FramesPerPacket);
1608#if defined(WITH_DSP_FFMPEG)
1609 return freerdp_dsp_ffmpeg_context_reset(context, targetFormat);
1612 if (!context || !targetFormat)
1615 context->common.format = *targetFormat;
1617 switch (context->common.format.wFormatTag)
1619#if defined(WITH_LAME)
1620 case WAVE_FORMAT_MPEGLAYER3:
1621 if (!valid_mp3_format(context))
1625 case WAVE_FORMAT_ADPCM:
1626 if (!valid_ms_adpcm_format(context))
1629 case WAVE_FORMAT_DVI_ADPCM:
1631 if (!valid_ima_adpcm_format(context))
1633 if (FramesPerPacket == 0)
1636 const size_t min_frame_data = 1ull * context->common.format.wBitsPerSample *
1637 context->common.format.nChannels * FramesPerPacket;
1638 const size_t data_per_block = (1ULL * context->common.format.nBlockAlign -
1639 4ULL * context->common.format.nChannels) *
1641 size_t nb_block_per_packet = min_frame_data / data_per_block;
1643 if (min_frame_data % data_per_block)
1644 nb_block_per_packet++;
1646 context->adpcm.ima.packet_size =
1647 nb_block_per_packet * context->common.format.nBlockAlign;
1648 if (!Stream_EnsureCapacity(context->common.buffer, context->adpcm.ima.packet_size))
1650 Stream_ResetPosition(context->common.buffer);
1657#if defined(WITH_OPUS)
1659 if (opus_is_valid_samplerate(&context->common.format))
1661 if (!context->common.encoder)
1663 int opus_error = OPUS_OK;
1665 context->opus_decoder = opus_decoder_create(
1666 WINPR_ASSERTING_INT_CAST(opus_int32, context->common.format.nSamplesPerSec),
1667 context->common.format.nChannels, &opus_error);
1668 if (opus_error != OPUS_OK)
1673 int opus_error = OPUS_OK;
1675 context->opus_encoder = opus_encoder_create(
1676 WINPR_ASSERTING_INT_CAST(opus_int32, context->common.format.nSamplesPerSec),
1677 context->common.format.nChannels, OPUS_APPLICATION_VOIP, &opus_error);
1678 if (opus_error != OPUS_OK)
1682 opus_encoder_ctl(context->opus_encoder,
1683 OPUS_SET_BITRATE(context->common.format.nAvgBytesPerSec * 8));
1684 if (opus_error != OPUS_OK)
1690#if defined(WITH_FAAD2)
1691 context->faadSetup = FALSE;
1693#if defined(WITH_FAAC)
1695 if (context->common.encoder)
1697 faacEncConfigurationPtr cfg =
nullptr;
1700 faacEncClose(context->faac);
1702 context->faac = faacEncOpen(targetFormat->nSamplesPerSec, targetFormat->nChannels,
1703 &context->faacInputSamples, &context->faacMaxOutputBytes);
1708 cfg = faacEncGetCurrentConfiguration(context->faac);
1709 cfg->inputFormat = FAAC_INPUT_16BIT;
1710 cfg->outputFormat = 0;
1711 cfg->mpegVersion = MPEG4;
1713 cfg->bandWidth = targetFormat->nAvgBytesPerSec;
1714 const int rc = faacEncSetConfiguration(context->faac, cfg);
1720#if defined(WITH_SOXR)
1722 soxr_io_spec_t iospec = soxr_io_spec(SOXR_INT16, SOXR_INT16);
1723 soxr_error_t error =
nullptr;
1725 soxr_delete(context->sox);
1727 soxr_create(context->common.format.nSamplesPerSec, targetFormat->nSamplesPerSec,
1728 targetFormat->nChannels, &error, &iospec,
nullptr,
nullptr);
1730 if (!context->sox || (error !=
nullptr))
1740 WINPR_ASSERT(context);
1741 context->encoder = encode;
1742 context->buffer = Stream_New(
nullptr, 1024);
1743 if (!context->buffer)
1746 context->channelmix = Stream_New(
nullptr, 1024);
1747 if (!context->channelmix)
1750 context->resample = Stream_New(
nullptr, 1024);
1751 if (!context->resample)
1757 freerdp_dsp_common_context_uninit(context);
1763 WINPR_ASSERT(context);
1765 Stream_Free(context->buffer, TRUE);
1766 Stream_Free(context->channelmix, TRUE);
1767 Stream_Free(context->resample, TRUE);
1769 context->buffer =
nullptr;
1770 context->channelmix =
nullptr;
1771 context->resample =
nullptr;