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 ((
size_t)adpcm->ima.last_step[channel] >= size)
331 adpcm->ima.last_step[channel] = (INT16)(size - 1);
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 const int32_t last = adpcm->ima.last_step[channel] + ima_step_index_table[sample];
370 adpcm->ima.last_step[channel] = WINPR_ASSERTING_INT_CAST(int16_t, last);
372 dsp_ima_clamp_step(adpcm, channel);
377static BOOL valid_ima_adpcm_format(
const FREERDP_DSP_CONTEXT* WINPR_RESTRICT context)
379 WINPR_ASSERT(context);
380 if (context->common.format.wFormatTag != WAVE_FORMAT_DVI_ADPCM)
382 if (context->common.format.nBlockAlign <= 4ULL)
384 if (context->common.format.nChannels < 1)
386 if (context->common.format.wBitsPerSample == 0)
391static BOOL freerdp_dsp_decode_ima_adpcm(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
392 const BYTE* WINPR_RESTRICT src,
size_t size,
395 if (!valid_ima_adpcm_format(context))
398 size_t out_size = size * 4ull;
399 const UINT32 block_size = context->common.format.nBlockAlign;
400 const UINT32 channels = context->common.format.nChannels;
402 if (!Stream_EnsureCapacity(out, out_size))
407 if (size % block_size == 0)
412 context->adpcm.ima.last_sample[0] =
413 (INT16)(((UINT16)(*src)) | (((UINT16)(*(src + 1))) << 8));
414 context->adpcm.ima.last_step[0] = (INT16)(*(src + 2));
416 dsp_ima_clamp_step(&context->adpcm, 0);
426 context->adpcm.ima.last_sample[1] =
427 (INT16)(((UINT16)(*src)) | (((UINT16)(*(src + 1))) << 8));
428 context->adpcm.ima.last_step[1] = (INT16)(*(src + 2));
430 dsp_ima_clamp_step(&context->adpcm, 1);
441 for (
size_t i = 0; i < 8; i++)
443 BYTE* dst = Stream_Pointer(out);
445 const unsigned channel = (i < 4 ? 0 : 1);
447 const BYTE sample = ((*src) & 0x0f);
448 const UINT16 decoded =
449 dsp_decode_ima_adpcm_sample(&context->adpcm, channel, sample);
450 dst[((i & 3) << 3) + (channel << 1u)] = (decoded & 0xFF);
451 dst[((i & 3) << 3) + (channel << 1u) + 1] = (decoded >> 8);
454 const BYTE sample = ((*src) >> 4);
455 const UINT16 decoded =
456 dsp_decode_ima_adpcm_sample(&context->adpcm, channel, sample);
457 dst[((i & 3) << 3) + (channel << 1u) + 4] = (decoded & 0xFF);
458 dst[((i & 3) << 3) + (channel << 1u) + 5] = (decoded >> 8);
463 if (!Stream_SafeSeek(out, 32))
471 BYTE* dst = Stream_Pointer(out);
472 if (!Stream_SafeSeek(out, 4))
476 const BYTE sample = ((*src) & 0x0f);
477 const UINT16 decoded = dsp_decode_ima_adpcm_sample(&context->adpcm, 0, sample);
478 *dst++ = (decoded & 0xFF);
479 *dst++ = (decoded >> 8);
482 const BYTE sample = ((*src) >> 4);
483 const UINT16 decoded = dsp_decode_ima_adpcm_sample(&context->adpcm, 0, sample);
484 *dst++ = (decoded & 0xFF);
485 *dst++ = (decoded >> 8);
496static BOOL freerdp_dsp_decode_gsm610(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
497 const BYTE* WINPR_RESTRICT src,
size_t size,
502 while (offset < size)
505 gsm_signal gsmBlockBuffer[160] = WINPR_C_ARRAY_INIT;
506 rc = gsm_decode(context->gsm, (gsm_byte*) &src[offset],
512 if ((offset % 65) == 0)
517 if (!Stream_EnsureRemainingCapacity(out,
sizeof(gsmBlockBuffer)))
520 Stream_Write(out, (
void*)gsmBlockBuffer,
sizeof(gsmBlockBuffer));
526static BOOL freerdp_dsp_encode_gsm610(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
527 const BYTE* WINPR_RESTRICT src,
size_t size,
532 while (offset < size)
534 const gsm_signal* signal = (
const gsm_signal*)&src[offset];
536 if (!Stream_EnsureRemainingCapacity(out,
sizeof(gsm_frame)))
539 gsm_encode(context->gsm, (gsm_signal*) signal,
540 Stream_Pointer(out));
542 if ((offset % 65) == 0)
543 Stream_Seek(out, 33);
545 Stream_Seek(out, 32);
554#if defined(WITH_LAME)
555static BOOL valid_mp3_format(
const FREERDP_DSP_CONTEXT* WINPR_RESTRICT context)
557 WINPR_ASSERT(context);
558 if (context->common.format.wFormatTag != WAVE_FORMAT_MPEGLAYER3)
560 if (context->common.format.nChannels < 1)
562 if (context->common.format.wBitsPerSample == 0)
564 if (context->common.format.nSamplesPerSec == 0)
569static BOOL freerdp_dsp_decode_mp3(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
570 const BYTE* WINPR_RESTRICT src,
size_t size,
573 if (!context || !src || !out)
575 if (!valid_mp3_format(context))
577 const size_t buffer_size =
578 2 * context->common.format.nChannels * context->common.format.nSamplesPerSec;
580 if (!Stream_EnsureCapacity(context->common.buffer, 2 * buffer_size))
583 short* pcm_l = Stream_BufferAs(context->common.buffer,
short);
584 short* pcm_r = Stream_BufferAs(context->common.buffer,
short) + buffer_size;
585 const int rc = hip_decode(context->hip, (
unsigned char*) src,
591 if (!Stream_EnsureRemainingCapacity(out, (
size_t)rc * context->common.format.nChannels * 2))
594 for (
size_t x = 0; x < rc; x++)
596 Stream_Write_UINT16(out, (UINT16)pcm_l[x]);
597 Stream_Write_UINT16(out, (UINT16)pcm_r[x]);
603static BOOL freerdp_dsp_encode_mp3(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
604 const BYTE* WINPR_RESTRICT src,
size_t size,
607 if (!context || !src || !out)
610 if (!valid_mp3_format(context))
613 size_t samples_per_channel =
614 size / context->common.format.nChannels / context->common.format.wBitsPerSample / 8;
617 if (!Stream_EnsureRemainingCapacity(out, 5 / 4 * samples_per_channel + 7200))
620 samples_per_channel = size / 2 / context->common.format.nChannels;
622 lame_encode_buffer_interleaved(context->lame, (
short*)src, samples_per_channel,
623 Stream_Pointer(out), Stream_GetRemainingCapacity(out));
628 Stream_Seek(out, (
size_t)rc);
633#if defined(WITH_FAAC)
634static BOOL freerdp_dsp_encode_faac(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
635 const BYTE* WINPR_RESTRICT src,
size_t size,
638 const int16_t* inSamples = (
const int16_t*)src;
643 if (!context || !src || !out)
646 bpp = context->common.format.wBitsPerSample / 8;
647 nrSamples = size / bpp;
649 if (!Stream_EnsureRemainingCapacity(context->common.buffer, nrSamples *
sizeof(int16_t)))
652 for (
size_t x = 0; x < nrSamples; x++)
654 Stream_Write_INT16(context->common.buffer, inSamples[x]);
655 if (Stream_GetPosition(context->common.buffer) / bpp >= context->faacInputSamples)
657 if (!Stream_EnsureRemainingCapacity(out, context->faacMaxOutputBytes))
659 rc = faacEncEncode(context->faac, Stream_BufferAs(context->common.buffer, int32_t),
660 context->faacInputSamples, Stream_Pointer(out),
661 Stream_GetRemainingCapacity(out));
665 Stream_Seek(out, (
size_t)rc);
666 Stream_ResetPosition(context->common.buffer);
674#if defined(WITH_OPUS)
675static BOOL freerdp_dsp_decode_opus(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
676 const BYTE* WINPR_RESTRICT src,
size_t size,
679 if (!context || !src || !out)
683 const size_t max_size = OPUS_MAX_FRAMES * context->common.format.nChannels *
sizeof(int16_t);
684 if (!Stream_EnsureRemainingCapacity(context->common.buffer, max_size))
687 const opus_int32 frames =
688 opus_decode(context->opus_decoder, src, WINPR_ASSERTING_INT_CAST(opus_int32, size),
689 Stream_Pointer(out), OPUS_MAX_FRAMES, 0);
693 Stream_Seek(out, (
size_t)frames * context->common.format.nChannels *
sizeof(int16_t));
698static BOOL freerdp_dsp_encode_opus(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
699 const BYTE* WINPR_RESTRICT src,
size_t size,
702 if (!context || !src || !out)
706 const size_t max_size = OPUS_MAX_FRAMES * context->common.format.nChannels *
sizeof(int16_t);
707 if (!Stream_EnsureRemainingCapacity(context->common.buffer, max_size))
710 const size_t src_frames = size /
sizeof(opus_int16) / context->common.format.nChannels;
711 const opus_int16* src_data = (
const opus_int16*)src;
712 const opus_int32 frames = opus_encode(
713 context->opus_encoder, src_data, WINPR_ASSERTING_INT_CAST(opus_int32, src_frames),
714 Stream_Pointer(out), WINPR_ASSERTING_INT_CAST(opus_int32, max_size));
717 return Stream_SafeSeek(out,
718 (
size_t)frames * context->common.format.nChannels *
sizeof(int16_t));
722#if defined(WITH_FAAD2)
723static BOOL freerdp_dsp_decode_faad(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
724 const BYTE* WINPR_RESTRICT src,
size_t size,
727 NeAACDecFrameInfo info;
730 if (!context || !src || !out)
733 if (!context->faadSetup)
740 unsigned long samplerate;
741 unsigned char channels;
744 err = NeAACDecInit(context->faad, cnv.pv, size,
745 &samplerate, &channels);
750 if (channels != context->common.format.nChannels)
753 if (samplerate != context->common.format.nSamplesPerSec)
756 context->faadSetup = TRUE;
759 while (offset < size)
768 outSize = context->common.format.nSamplesPerSec * context->common.format.nChannels *
769 context->common.format.wBitsPerSample / 8;
771 if (!Stream_EnsureRemainingCapacity(out, outSize))
774 sample_buffer = Stream_Pointer(out);
776 cnv.cpv = &src[offset];
777 NeAACDecDecode2(context->faad, &info, cnv.pv, size - offset, &sample_buffer,
778 Stream_GetRemainingCapacity(out));
783 offset += info.bytesconsumed;
785 if (info.samples == 0)
788 Stream_Seek(out, info.samples * context->common.format.wBitsPerSample / 8);
807} ima_stereo_encode_map[] = { { 0, 0 }, { 4, 0 }, { 0, 4 }, { 4, 4 }, { 1, 0 }, { 5, 0 },
808 { 1, 4 }, { 5, 4 }, { 2, 0 }, { 6, 0 }, { 2, 4 }, { 6, 4 },
809 { 3, 0 }, { 7, 0 }, { 3, 4 }, { 7, 4 } };
811static BYTE dsp_encode_ima_adpcm_sample(ADPCM* WINPR_RESTRICT adpcm,
size_t channel, INT16 sample)
813 WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ima.last_step));
814 WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ima.last_sample));
816 const INT16 offset = adpcm->ima.last_step[channel];
817 WINPR_ASSERT(offset >= 0);
818 WINPR_ASSERT(WINPR_CXX_COMPAT_CAST(
size_t, offset) < ARRAYSIZE(ima_step_size_table));
820 INT32 ss = ima_step_size_table[offset];
821 INT32 e = sample - adpcm->ima.last_sample[channel];
823 INT32 diff = ss >> 3;
859 diff += adpcm->ima.last_sample[channel];
863 else if (diff > 32767)
866 adpcm->ima.last_sample[channel] = (INT16)diff;
868 WINPR_ASSERT(enc < ARRAYSIZE(ima_step_index_table));
869 const int32_t last = adpcm->ima.last_step[channel] + ima_step_index_table[enc];
870 adpcm->ima.last_step[channel] = WINPR_ASSERTING_INT_CAST(int16_t, last);
872 dsp_ima_clamp_step(adpcm, channel);
877static BOOL freerdp_dsp_encode_ima_adpcm(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
878 const BYTE* WINPR_RESTRICT src,
size_t size,
881 if (!valid_ima_adpcm_format(context))
883 if (!Stream_EnsureRemainingCapacity(out, size))
885 if (!Stream_EnsureRemainingCapacity(context->common.buffer, size + 64))
888 const size_t align = (context->common.format.nChannels > 1) ? 32 : 4;
890 while (size >= align)
892 if (Stream_GetPosition(context->common.buffer) % context->common.format.nBlockAlign == 0)
894 Stream_Write_UINT8(context->common.buffer, context->adpcm.ima.last_sample[0] & 0xFF);
895 Stream_Write_UINT8(context->common.buffer,
896 (context->adpcm.ima.last_sample[0] >> 8) & 0xFF);
897 Stream_Write_UINT8(context->common.buffer, (BYTE)context->adpcm.ima.last_step[0]);
898 Stream_Write_UINT8(context->common.buffer, 0);
900 if (context->common.format.nChannels > 1)
902 Stream_Write_UINT8(context->common.buffer,
903 context->adpcm.ima.last_sample[1] & 0xFF);
904 Stream_Write_UINT8(context->common.buffer,
905 (context->adpcm.ima.last_sample[1] >> 8) & 0xFF);
906 Stream_Write_UINT8(context->common.buffer, (BYTE)context->adpcm.ima.last_step[1]);
907 Stream_Write_UINT8(context->common.buffer, 0);
911 if (context->common.format.nChannels > 1)
913 BYTE* dst = Stream_Pointer(context->common.buffer);
916 for (
size_t i = 0; i < 16; i++)
918 const INT16 sample = (INT16)(((UINT16)(*src)) | (((UINT16)(*(src + 1))) << 8));
920 const BYTE encoded = dsp_encode_ima_adpcm_sample(&context->adpcm, i % 2, sample);
921 dst[ima_stereo_encode_map[i].byte_num] |= encoded
922 << ima_stereo_encode_map[i].byte_shift;
925 if (!Stream_SafeSeek(context->common.buffer, 8))
931 INT16 sample = (INT16)(((UINT16)(*src)) | (((UINT16)(*(src + 1))) << 8));
933 BYTE encoded = dsp_encode_ima_adpcm_sample(&context->adpcm, 0, sample);
934 sample = (INT16)(((UINT16)(*src)) | (((UINT16)(*(src + 1))) << 8));
936 encoded |= dsp_encode_ima_adpcm_sample(&context->adpcm, 0, sample) << 4;
937 Stream_Write_UINT8(context->common.buffer, encoded);
941 if (Stream_GetPosition(context->common.buffer) >= context->adpcm.ima.packet_size)
943 BYTE* bsrc = Stream_Buffer(context->common.buffer);
944 Stream_Write(out, bsrc, context->adpcm.ima.packet_size);
945 Stream_ResetPosition(context->common.buffer);
958static const INT32 ms_adpcm_adaptation_table[] = { 230, 230, 230, 230, 307, 409, 512, 614,
959 768, 614, 512, 409, 307, 230, 230, 230 };
961static const INT32 ms_adpcm_coeffs1[7] = { 256, 512, 0, 192, 240, 460, 392 };
963static const INT32 ms_adpcm_coeffs2[7] = { 0, -256, 0, 64, 0, -208, -232 };
965static inline INT16 freerdp_dsp_decode_ms_adpcm_sample(ADPCM* WINPR_RESTRICT adpcm, BYTE sample,
968 WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ms.sample1));
969 WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ms.sample2));
970 WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ms.delta));
971 WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ms.predictor));
973 const INT8 nibble = (INT8)((sample & 0x08) ? (sample - 16) : sample);
974 const BYTE predictor = adpcm->ms.predictor[channel];
976 if (predictor < ARRAYSIZE(ms_adpcm_coeffs1))
977 coeff1 = ms_adpcm_coeffs1[predictor];
980 if (predictor < ARRAYSIZE(ms_adpcm_coeffs2))
981 coeff2 = ms_adpcm_coeffs2[predictor];
983 ((adpcm->ms.sample1[channel] * coeff1) + (adpcm->ms.sample2[channel] * coeff2)) / 256;
984 presample += nibble * adpcm->ms.delta[channel];
986 if (presample > 32767)
988 else if (presample < -32768)
991 adpcm->ms.sample2[channel] = adpcm->ms.sample1[channel];
992 adpcm->ms.sample1[channel] = presample;
995 if (sample < ARRAYSIZE(ms_adpcm_adaptation_table))
996 tableval = ms_adpcm_adaptation_table[sample];
998 adpcm->ms.delta[channel] = adpcm->ms.delta[channel] * tableval / 256;
1000 if (adpcm->ms.delta[channel] < 16)
1001 adpcm->ms.delta[channel] = 16;
1003 return (INT16)presample;
1006static BOOL valid_ms_adpcm_format(
const FREERDP_DSP_CONTEXT* WINPR_RESTRICT context)
1008 WINPR_ASSERT(context);
1009 if (context->common.format.wFormatTag != WAVE_FORMAT_ADPCM)
1011 if (context->common.format.nBlockAlign <= 4ULL)
1013 if (context->common.format.nChannels < 1)
1015 if (context->common.format.wBitsPerSample == 0)
1020static BOOL freerdp_dsp_decode_ms_adpcm(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
1021 const BYTE* WINPR_RESTRICT src,
size_t size,
1024 if (!valid_ms_adpcm_format(context))
1026 const size_t out_size = size * 4;
1027 const UINT32 channels = context->common.format.nChannels;
1028 const UINT32 block_size = context->common.format.nBlockAlign;
1030 if (!Stream_EnsureCapacity(out, out_size))
1035 if (size % block_size == 0)
1042 context->adpcm.ms.predictor[0] = *src++;
1043 context->adpcm.ms.predictor[1] = *src++;
1044 context->adpcm.ms.delta[0] = read_int16(src);
1046 context->adpcm.ms.delta[1] = read_int16(src);
1048 context->adpcm.ms.sample1[0] = read_int16(src);
1050 context->adpcm.ms.sample1[1] = read_int16(src);
1052 context->adpcm.ms.sample2[0] = read_int16(src);
1054 context->adpcm.ms.sample2[1] = read_int16(src);
1057 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample2[0]);
1058 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample2[1]);
1059 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample1[0]);
1060 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample1[1]);
1067 context->adpcm.ms.predictor[0] = *src++;
1068 context->adpcm.ms.delta[0] = read_int16(src);
1070 context->adpcm.ms.sample1[0] = read_int16(src);
1072 context->adpcm.ms.sample2[0] = read_int16(src);
1075 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample2[0]);
1076 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample1[0]);
1085 const BYTE sample = *src++;
1088 out, freerdp_dsp_decode_ms_adpcm_sample(&context->adpcm, sample >> 4, 0));
1090 out, freerdp_dsp_decode_ms_adpcm_sample(&context->adpcm, sample & 0x0F, 1));
1095 const BYTE sample = *src++;
1098 out, freerdp_dsp_decode_ms_adpcm_sample(&context->adpcm, sample >> 4, 0));
1100 out, freerdp_dsp_decode_ms_adpcm_sample(&context->adpcm, sample & 0x0F, 1));
1107 const BYTE sample = *src++;
1109 Stream_Write_INT16(out,
1110 freerdp_dsp_decode_ms_adpcm_sample(&context->adpcm, sample >> 4, 0));
1112 out, freerdp_dsp_decode_ms_adpcm_sample(&context->adpcm, sample & 0x0F, 0));
1119static BYTE freerdp_dsp_encode_ms_adpcm_sample(ADPCM* WINPR_RESTRICT adpcm, INT32 sample,
1122 WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ms.sample1));
1123 WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ms.sample2));
1124 WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ms.delta));
1125 WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ms.predictor));
1128 ((adpcm->ms.sample1[channel] * ms_adpcm_coeffs1[adpcm->ms.predictor[channel]]) +
1129 (adpcm->ms.sample2[channel] * ms_adpcm_coeffs2[adpcm->ms.predictor[channel]])) /
1131 INT32 errordelta = (sample - presample) / adpcm->ms.delta[channel];
1133 if ((sample - presample) % adpcm->ms.delta[channel] > adpcm->ms.delta[channel] / 2)
1138 else if (errordelta < -8)
1141 presample += adpcm->ms.delta[channel] * errordelta;
1143 if (presample > 32767)
1145 else if (presample < -32768)
1148 adpcm->ms.sample2[channel] = adpcm->ms.sample1[channel];
1149 adpcm->ms.sample1[channel] = presample;
1150 const size_t offset = (((BYTE)errordelta) & 0x0F);
1151 WINPR_ASSERT(offset < ARRAYSIZE(ms_adpcm_adaptation_table));
1152 adpcm->ms.delta[channel] = adpcm->ms.delta[channel] * ms_adpcm_adaptation_table[offset] / 256;
1154 if (adpcm->ms.delta[channel] < 16)
1155 adpcm->ms.delta[channel] = 16;
1157 return ((BYTE)errordelta) & 0x0F;
1160static BOOL freerdp_dsp_encode_ms_adpcm(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
1161 const BYTE* WINPR_RESTRICT src,
size_t size,
1164 if (!valid_ms_adpcm_format(context))
1167 const size_t step = 8 + ((context->common.format.nChannels > 1) ? 4 : 0);
1169 if (!Stream_EnsureRemainingCapacity(out, size))
1172 const size_t start = Stream_GetPosition(out);
1174 if (context->adpcm.ms.delta[0] < 16)
1175 context->adpcm.ms.delta[0] = 16;
1177 if (context->adpcm.ms.delta[1] < 16)
1178 context->adpcm.ms.delta[1] = 16;
1180 while (size >= step)
1182 if ((Stream_GetPosition(out) - start) % context->common.format.nBlockAlign == 0)
1184 if (context->common.format.nChannels > 1)
1186 Stream_Write_UINT8(out, context->adpcm.ms.predictor[0]);
1187 Stream_Write_UINT8(out, context->adpcm.ms.predictor[1]);
1188 Stream_Write_UINT8(out, (context->adpcm.ms.delta[0] & 0xFF));
1189 Stream_Write_UINT8(out, ((context->adpcm.ms.delta[0] >> 8) & 0xFF));
1190 Stream_Write_UINT8(out, (context->adpcm.ms.delta[1] & 0xFF));
1191 Stream_Write_UINT8(out, ((context->adpcm.ms.delta[1] >> 8) & 0xFF));
1193 context->adpcm.ms.sample1[0] = read_int16(src + 4);
1194 context->adpcm.ms.sample1[1] = read_int16(src + 6);
1195 context->adpcm.ms.sample2[0] = read_int16(src + 0);
1196 context->adpcm.ms.sample2[1] = read_int16(src + 2);
1198 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample1[0]);
1199 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample1[1]);
1200 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample2[0]);
1201 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample2[1]);
1208 Stream_Write_UINT8(out, context->adpcm.ms.predictor[0]);
1209 Stream_Write_UINT8(out, (BYTE)(context->adpcm.ms.delta[0] & 0xFF));
1210 Stream_Write_UINT8(out, (BYTE)((context->adpcm.ms.delta[0] >> 8) & 0xFF));
1212 context->adpcm.ms.sample1[0] = read_int16(src + 2);
1213 context->adpcm.ms.sample2[0] = read_int16(src + 0);
1215 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample1[0]);
1216 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample2[0]);
1223 const INT16 sample = read_int16(src);
1226 out, (freerdp_dsp_encode_ms_adpcm_sample(&context->adpcm, sample, 0) << 4) & 0xFF);
1229 const INT16 sample = read_int16(src);
1233 Stream_Read_UINT8(out, val);
1234 val += freerdp_dsp_encode_ms_adpcm_sample(&context->adpcm, sample,
1235 context->common.format.nChannels > 1 ? 1 : 0);
1236 Stream_Write_UINT8(out, val);
1246FREERDP_DSP_CONTEXT* freerdp_dsp_context_new(BOOL encoder)
1248#if defined(WITH_DSP_FFMPEG)
1249 return freerdp_dsp_ffmpeg_context_new(encoder);
1251 FREERDP_DSP_CONTEXT* context = calloc(1,
sizeof(FREERDP_DSP_CONTEXT));
1256 if (!freerdp_dsp_common_context_init(&context->common, encoder))
1259#if defined(WITH_GSM)
1260 context->gsm = gsm_create();
1268 rc = gsm_option(context->gsm, GSM_OPT_WAV49, &val);
1274#if defined(WITH_LAME)
1278 context->lame = lame_init();
1285 context->hip = hip_decode_init();
1292#if defined(WITH_FAAD2)
1296 context->faad = NeAACDecOpen();
1305 freerdp_dsp_context_free(context);
1310void freerdp_dsp_context_free(FREERDP_DSP_CONTEXT* context)
1315#if defined(WITH_FDK_AAC)
1318 fdk_aac_dsp_uninit(ctx);
1321#if defined(WITH_DSP_FFMPEG)
1322 freerdp_dsp_ffmpeg_context_free(context);
1325 freerdp_dsp_common_context_uninit(&context->common);
1327#if defined(WITH_GSM)
1328 gsm_destroy(context->gsm);
1330#if defined(WITH_LAME)
1332 if (context->common.encoder)
1333 lame_close(context->lame);
1335 hip_decode_exit(context->hip);
1338#if defined(WITH_OPUS)
1340 if (context->opus_decoder)
1341 opus_decoder_destroy(context->opus_decoder);
1342 if (context->opus_encoder)
1343 opus_encoder_destroy(context->opus_encoder);
1346#if defined(WITH_FAAD2)
1348 if (!context->common.encoder)
1349 NeAACDecClose(context->faad);
1352#if defined(WITH_FAAC)
1355 faacEncClose(context->faac);
1358#if defined(WITH_SOXR)
1359 soxr_delete(context->sox);
1366BOOL freerdp_dsp_encode(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
1368 const BYTE* WINPR_RESTRICT pdata,
size_t length,
1371#if defined(WITH_FDK_AAC)
1374 switch (ctx->format.wFormatTag)
1376 case WAVE_FORMAT_AAC_MS:
1377 return fdk_aac_dsp_encode(ctx, srcFormat, pdata, length, out);
1383#if defined(WITH_DSP_FFMPEG)
1384 return freerdp_dsp_ffmpeg_encode(context, srcFormat, pdata, length, out);
1386 if (!context || !context->common.encoder || !srcFormat || !pdata || !out)
1390 const BYTE* resampleData =
nullptr;
1391 size_t resampleLength = 0;
1393 if (!freerdp_dsp_channel_mix(context, pdata, length, srcFormat, &resampleData, &resampleLength))
1396 format.nChannels = context->common.format.nChannels;
1398 const BYTE* data =
nullptr;
1399 if (!freerdp_dsp_resample(context, resampleData, resampleLength, &format, &data, &length))
1402 switch (context->common.format.wFormatTag)
1404 case WAVE_FORMAT_PCM:
1405 if (!Stream_EnsureRemainingCapacity(out, length))
1408 Stream_Write(out, data, length);
1411 case WAVE_FORMAT_ADPCM:
1412 return freerdp_dsp_encode_ms_adpcm(context, data, length, out);
1414 case WAVE_FORMAT_DVI_ADPCM:
1415 return freerdp_dsp_encode_ima_adpcm(context, data, length, out);
1416#if defined(WITH_GSM)
1418 case WAVE_FORMAT_GSM610:
1419 return freerdp_dsp_encode_gsm610(context, data, length, out);
1421#if defined(WITH_LAME)
1423 case WAVE_FORMAT_MPEGLAYER3:
1424 return freerdp_dsp_encode_mp3(context, data, length, out);
1426#if defined(WITH_FAAC)
1428 case WAVE_FORMAT_AAC_MS:
1429 return freerdp_dsp_encode_faac(context, data, length, out);
1431#if defined(WITH_OPUS)
1433 case WAVE_FORMAT_OPUS:
1434 return freerdp_dsp_encode_opus(context, data, length, out);
1444BOOL freerdp_dsp_decode(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
1446 const BYTE* WINPR_RESTRICT data,
size_t length,
wStream* WINPR_RESTRICT out)
1448#if defined(WITH_FDK_AAC)
1451 switch (ctx->format.wFormatTag)
1453 case WAVE_FORMAT_AAC_MS:
1454 return fdk_aac_dsp_decode(ctx, srcFormat, data, length, out);
1460#if defined(WITH_DSP_FFMPEG)
1461 return freerdp_dsp_ffmpeg_decode(context, srcFormat, data, length, out);
1464 if (!context || context->common.encoder || !srcFormat || !data || !out)
1467 switch (context->common.format.wFormatTag)
1469 case WAVE_FORMAT_PCM:
1470 if (!Stream_EnsureRemainingCapacity(out, length))
1473 Stream_Write(out, data, length);
1476 case WAVE_FORMAT_ADPCM:
1477 return freerdp_dsp_decode_ms_adpcm(context, data, length, out);
1479 case WAVE_FORMAT_DVI_ADPCM:
1480 return freerdp_dsp_decode_ima_adpcm(context, data, length, out);
1481#if defined(WITH_GSM)
1483 case WAVE_FORMAT_GSM610:
1484 return freerdp_dsp_decode_gsm610(context, data, length, out);
1486#if defined(WITH_LAME)
1488 case WAVE_FORMAT_MPEGLAYER3:
1489 return freerdp_dsp_decode_mp3(context, data, length, out);
1491#if defined(WITH_FAAD2)
1493 case WAVE_FORMAT_AAC_MS:
1494 return freerdp_dsp_decode_faad(context, data, length, out);
1497#if defined(WITH_OPUS)
1498 case WAVE_FORMAT_OPUS:
1499 return freerdp_dsp_decode_opus(context, data, length, out);
1509BOOL freerdp_dsp_supports_format(
const AUDIO_FORMAT* WINPR_RESTRICT format, BOOL encode)
1511#if defined(WITH_FDK_AAC)
1512 switch (format->wFormatTag)
1514 case WAVE_FORMAT_AAC_MS:
1522#if defined(WITH_DSP_FFMPEG)
1523 return freerdp_dsp_ffmpeg_supports_format(format, encode);
1526#if !defined(WITH_DSP_EXPERIMENTAL)
1527 WINPR_UNUSED(encode);
1529 switch (format->wFormatTag)
1531 case WAVE_FORMAT_PCM:
1533#if defined(WITH_DSP_EXPERIMENTAL)
1535 case WAVE_FORMAT_ADPCM:
1537 case WAVE_FORMAT_DVI_ADPCM:
1540#if defined(WITH_GSM)
1542 case WAVE_FORMAT_GSM610:
1543#if defined(WITH_DSP_EXPERIMENTAL)
1549#if defined(WITH_LAME)
1551 case WAVE_FORMAT_MPEGLAYER3:
1552#if defined(WITH_DSP_EXPERIMENTAL)
1559 case WAVE_FORMAT_AAC_MS:
1560#if defined(WITH_FAAD2)
1565#if defined(WITH_FAAC)
1571#if defined(WITH_FDK_AAC)
1577#if defined(WITH_OPUS)
1578 case WAVE_FORMAT_OPUS:
1579 return opus_is_valid_samplerate(format);
1589BOOL freerdp_dsp_context_reset(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
1591 WINPR_ATTR_UNUSED UINT32 FramesPerPacket)
1593#if defined(WITH_FDK_AAC)
1594 WINPR_ASSERT(targetFormat);
1595 if (targetFormat->wFormatTag == WAVE_FORMAT_AAC_MS)
1598 fdk_aac_dsp_uninit(ctx);
1599 ctx->format = *targetFormat;
1600 return fdk_aac_dsp_init(ctx, FramesPerPacket);
1604#if defined(WITH_DSP_FFMPEG)
1605 return freerdp_dsp_ffmpeg_context_reset(context, targetFormat);
1608 if (!context || !targetFormat)
1611 context->common.format = *targetFormat;
1613 switch (context->common.format.wFormatTag)
1615#if defined(WITH_LAME)
1616 case WAVE_FORMAT_MPEGLAYER3:
1617 if (!valid_mp3_format(context))
1621 case WAVE_FORMAT_ADPCM:
1622 if (!valid_ms_adpcm_format(context))
1625 case WAVE_FORMAT_DVI_ADPCM:
1627 if (!valid_ima_adpcm_format(context))
1629 if (FramesPerPacket == 0)
1632 const size_t min_frame_data = 1ull * context->common.format.wBitsPerSample *
1633 context->common.format.nChannels * FramesPerPacket;
1634 const size_t data_per_block = (1ULL * context->common.format.nBlockAlign -
1635 4ULL * context->common.format.nChannels) *
1637 size_t nb_block_per_packet = min_frame_data / data_per_block;
1639 if (min_frame_data % data_per_block)
1640 nb_block_per_packet++;
1642 context->adpcm.ima.packet_size =
1643 nb_block_per_packet * context->common.format.nBlockAlign;
1644 if (!Stream_EnsureCapacity(context->common.buffer, context->adpcm.ima.packet_size))
1646 Stream_ResetPosition(context->common.buffer);
1653#if defined(WITH_OPUS)
1655 if (opus_is_valid_samplerate(&context->common.format))
1657 if (!context->common.encoder)
1659 int opus_error = OPUS_OK;
1661 context->opus_decoder = opus_decoder_create(
1662 WINPR_ASSERTING_INT_CAST(opus_int32, context->common.format.nSamplesPerSec),
1663 context->common.format.nChannels, &opus_error);
1664 if (opus_error != OPUS_OK)
1669 int opus_error = OPUS_OK;
1671 context->opus_encoder = opus_encoder_create(
1672 WINPR_ASSERTING_INT_CAST(opus_int32, context->common.format.nSamplesPerSec),
1673 context->common.format.nChannels, OPUS_APPLICATION_VOIP, &opus_error);
1674 if (opus_error != OPUS_OK)
1678 opus_encoder_ctl(context->opus_encoder,
1679 OPUS_SET_BITRATE(context->common.format.nAvgBytesPerSec * 8));
1680 if (opus_error != OPUS_OK)
1686#if defined(WITH_FAAD2)
1687 context->faadSetup = FALSE;
1689#if defined(WITH_FAAC)
1691 if (context->common.encoder)
1693 faacEncConfigurationPtr cfg =
nullptr;
1696 faacEncClose(context->faac);
1698 context->faac = faacEncOpen(targetFormat->nSamplesPerSec, targetFormat->nChannels,
1699 &context->faacInputSamples, &context->faacMaxOutputBytes);
1704 cfg = faacEncGetCurrentConfiguration(context->faac);
1705 cfg->inputFormat = FAAC_INPUT_16BIT;
1706 cfg->outputFormat = 0;
1707 cfg->mpegVersion = MPEG4;
1709 cfg->bandWidth = targetFormat->nAvgBytesPerSec;
1710 const int rc = faacEncSetConfiguration(context->faac, cfg);
1716#if defined(WITH_SOXR)
1718 soxr_io_spec_t iospec = soxr_io_spec(SOXR_INT16, SOXR_INT16);
1719 soxr_error_t error =
nullptr;
1721 soxr_delete(context->sox);
1723 soxr_create(context->common.format.nSamplesPerSec, targetFormat->nSamplesPerSec,
1724 targetFormat->nChannels, &error, &iospec,
nullptr,
nullptr);
1726 if (!context->sox || (error !=
nullptr))
1736 WINPR_ASSERT(context);
1737 context->encoder = encode;
1738 context->buffer = Stream_New(
nullptr, 1024);
1739 if (!context->buffer)
1742 context->channelmix = Stream_New(
nullptr, 1024);
1743 if (!context->channelmix)
1746 context->resample = Stream_New(
nullptr, 1024);
1747 if (!context->resample)
1753 freerdp_dsp_common_context_uninit(context);
1759 WINPR_ASSERT(context);
1761 Stream_Free(context->buffer, TRUE);
1762 Stream_Free(context->channelmix, TRUE);
1763 Stream_Free(context->resample, TRUE);
1765 context->buffer =
nullptr;
1766 context->channelmix =
nullptr;
1767 context->resample =
nullptr;