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 ==
nullptr);
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 2ull * context->common.format.nChannels * context->common.format.nSamplesPerSec;
574 if (!Stream_EnsureCapacity(context->common.buffer, 2ull * buffer_size))
577 short* pcm_l = Stream_BufferAs(context->common.buffer,
short);
578 short* pcm_r = Stream_BufferAs(context->common.buffer,
short) + (buffer_size /
sizeof(short));
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 * 2ull))
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, 5ull / 4ull * samples_per_channel + 7200ull))
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 bytes = 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, (
size_t)bytes);
725#if defined(WITH_FAAD2)
726static BOOL freerdp_dsp_decode_faad(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
727 const BYTE* WINPR_RESTRICT src,
size_t size,
730 NeAACDecFrameInfo info;
733 if (!context || !src || !out)
736 if (!context->faadSetup)
743 unsigned long samplerate = 0;
744 unsigned char channels = 0;
747 const long err = NeAACDecInit(context->faad, cnv.pv,
748 size, &samplerate, &channels);
753 if (channels != context->common.format.nChannels)
756 if (samplerate != context->common.format.nSamplesPerSec)
759 context->faadSetup = TRUE;
762 while (offset < size)
770 const size_t outSize = context->common.format.nSamplesPerSec *
771 context->common.format.nChannels *
772 context->common.format.wBitsPerSample / 8;
774 if (!Stream_EnsureRemainingCapacity(out, outSize))
777 void* sample_buffer = Stream_Pointer(out);
779 cnv.cpv = &src[offset];
780 NeAACDecDecode2(context->faad, &info, cnv.pv, size - offset, &sample_buffer,
781 Stream_GetRemainingCapacity(out));
786 offset += info.bytesconsumed;
788 if (info.samples == 0)
791 Stream_Seek(out, info.samples * context->common.format.wBitsPerSample / 8);
810} ima_stereo_encode_map[] = { { 0, 0 }, { 4, 0 }, { 0, 4 }, { 4, 4 }, { 1, 0 }, { 5, 0 },
811 { 1, 4 }, { 5, 4 }, { 2, 0 }, { 6, 0 }, { 2, 4 }, { 6, 4 },
812 { 3, 0 }, { 7, 0 }, { 3, 4 }, { 7, 4 } };
814static BYTE dsp_encode_ima_adpcm_sample(ADPCM* WINPR_RESTRICT adpcm,
size_t channel, INT16 sample)
816 WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ima.last_step));
817 WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ima.last_sample));
819 const INT16 offset = adpcm->ima.last_step[channel];
820 WINPR_ASSERT(offset >= 0);
821 WINPR_ASSERT(WINPR_CXX_COMPAT_CAST(
size_t, offset) < ARRAYSIZE(ima_step_size_table));
823 INT32 ss = ima_step_size_table[offset];
824 INT32 e = sample - adpcm->ima.last_sample[channel];
826 INT32 diff = ss >> 3;
862 diff += adpcm->ima.last_sample[channel];
866 else if (diff > 32767)
869 adpcm->ima.last_sample[channel] = (INT16)diff;
871 WINPR_ASSERT(enc < ARRAYSIZE(ima_step_index_table));
872 const int32_t last = adpcm->ima.last_step[channel] + ima_step_index_table[enc];
873 adpcm->ima.last_step[channel] = WINPR_ASSERTING_INT_CAST(int16_t, last);
875 dsp_ima_clamp_step(adpcm, channel);
880static BOOL freerdp_dsp_encode_ima_adpcm(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
881 const BYTE* WINPR_RESTRICT src,
size_t size,
884 if (!valid_ima_adpcm_format(context))
886 if (!Stream_EnsureRemainingCapacity(out, size))
888 if (!Stream_EnsureRemainingCapacity(context->common.buffer, size + 64ull))
891 const size_t align = (context->common.format.nChannels > 1) ? 32 : 4;
893 while (size >= align)
895 if (Stream_GetPosition(context->common.buffer) % context->common.format.nBlockAlign == 0)
897 Stream_Write_UINT8(context->common.buffer, context->adpcm.ima.last_sample[0] & 0xFF);
898 Stream_Write_UINT8(context->common.buffer,
899 (context->adpcm.ima.last_sample[0] >> 8) & 0xFF);
900 Stream_Write_UINT8(context->common.buffer, (BYTE)context->adpcm.ima.last_step[0]);
901 Stream_Write_UINT8(context->common.buffer, 0);
903 if (context->common.format.nChannels > 1)
905 Stream_Write_UINT8(context->common.buffer,
906 context->adpcm.ima.last_sample[1] & 0xFF);
907 Stream_Write_UINT8(context->common.buffer,
908 (context->adpcm.ima.last_sample[1] >> 8) & 0xFF);
909 Stream_Write_UINT8(context->common.buffer, (BYTE)context->adpcm.ima.last_step[1]);
910 Stream_Write_UINT8(context->common.buffer, 0);
914 if (context->common.format.nChannels > 1)
916 BYTE* dst = Stream_Pointer(context->common.buffer);
919 for (
size_t i = 0; i < 16; i++)
921 const INT16 sample = (INT16)(((UINT16)(*src)) | (((UINT16)(*(src + 1))) << 8));
923 const BYTE encoded = dsp_encode_ima_adpcm_sample(&context->adpcm, i % 2, sample);
924 dst[ima_stereo_encode_map[i].byte_num] |= encoded
925 << ima_stereo_encode_map[i].byte_shift;
928 if (!Stream_SafeSeek(context->common.buffer, 8))
934 INT16 sample = (INT16)(((UINT16)(*src)) | (((UINT16)(*(src + 1))) << 8));
936 BYTE encoded = dsp_encode_ima_adpcm_sample(&context->adpcm, 0, sample);
937 sample = (INT16)(((UINT16)(*src)) | (((UINT16)(*(src + 1))) << 8));
939 encoded |= dsp_encode_ima_adpcm_sample(&context->adpcm, 0, sample) << 4;
940 Stream_Write_UINT8(context->common.buffer, encoded);
944 if (Stream_GetPosition(context->common.buffer) >= context->adpcm.ima.packet_size)
946 BYTE* bsrc = Stream_Buffer(context->common.buffer);
947 Stream_Write(out, bsrc, context->adpcm.ima.packet_size);
948 Stream_ResetPosition(context->common.buffer);
961static const INT32 ms_adpcm_adaptation_table[] = { 230, 230, 230, 230, 307, 409, 512, 614,
962 768, 614, 512, 409, 307, 230, 230, 230 };
964static const INT32 ms_adpcm_coeffs1[7] = { 256, 512, 0, 192, 240, 460, 392 };
966static const INT32 ms_adpcm_coeffs2[7] = { 0, -256, 0, 64, 0, -208, -232 };
968static inline INT16 freerdp_dsp_decode_ms_adpcm_sample(ADPCM* WINPR_RESTRICT adpcm, BYTE sample,
971 WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ms.sample1));
972 WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ms.sample2));
973 WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ms.delta));
974 WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ms.predictor));
976 const INT8 nibble = (INT8)((sample & 0x08) ? (sample - 16) : sample);
977 const BYTE predictor = adpcm->ms.predictor[channel];
979 if (predictor < ARRAYSIZE(ms_adpcm_coeffs1))
980 coeff1 = ms_adpcm_coeffs1[predictor];
983 if (predictor < ARRAYSIZE(ms_adpcm_coeffs2))
984 coeff2 = ms_adpcm_coeffs2[predictor];
986 ((adpcm->ms.sample1[channel] * coeff1) + (adpcm->ms.sample2[channel] * coeff2)) / 256;
987 presample += nibble * adpcm->ms.delta[channel];
989 if (presample > 32767)
991 else if (presample < -32768)
994 adpcm->ms.sample2[channel] = adpcm->ms.sample1[channel];
995 adpcm->ms.sample1[channel] = presample;
998 if (sample < ARRAYSIZE(ms_adpcm_adaptation_table))
999 tableval = ms_adpcm_adaptation_table[sample];
1001 adpcm->ms.delta[channel] = adpcm->ms.delta[channel] * tableval / 256;
1003 if (adpcm->ms.delta[channel] < 16)
1004 adpcm->ms.delta[channel] = 16;
1006 return (INT16)presample;
1009static BOOL valid_ms_adpcm_format(
const FREERDP_DSP_CONTEXT* WINPR_RESTRICT context)
1011 WINPR_ASSERT(context);
1012 if (context->common.format.wFormatTag != WAVE_FORMAT_ADPCM)
1014 if (context->common.format.nBlockAlign <= 4ULL)
1016 if (context->common.format.nChannels < 1)
1018 if (context->common.format.wBitsPerSample == 0)
1023static BOOL freerdp_dsp_decode_ms_adpcm(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
1024 const BYTE* WINPR_RESTRICT src,
size_t size,
1027 if (!valid_ms_adpcm_format(context))
1029 const size_t out_size = size * 4;
1030 const UINT32 channels = context->common.format.nChannels;
1031 const UINT32 block_size = context->common.format.nBlockAlign;
1033 if (!Stream_EnsureCapacity(out, out_size))
1038 if (size % block_size == 0)
1045 context->adpcm.ms.predictor[0] = *src++;
1046 context->adpcm.ms.predictor[1] = *src++;
1047 context->adpcm.ms.delta[0] = read_int16(src);
1049 context->adpcm.ms.delta[1] = read_int16(src);
1051 context->adpcm.ms.sample1[0] = read_int16(src);
1053 context->adpcm.ms.sample1[1] = read_int16(src);
1055 context->adpcm.ms.sample2[0] = read_int16(src);
1057 context->adpcm.ms.sample2[1] = read_int16(src);
1060 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample2[0]);
1061 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample2[1]);
1062 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample1[0]);
1063 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample1[1]);
1070 context->adpcm.ms.predictor[0] = *src++;
1071 context->adpcm.ms.delta[0] = read_int16(src);
1073 context->adpcm.ms.sample1[0] = read_int16(src);
1075 context->adpcm.ms.sample2[0] = read_int16(src);
1078 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample2[0]);
1079 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample1[0]);
1088 const BYTE sample = *src++;
1091 out, freerdp_dsp_decode_ms_adpcm_sample(&context->adpcm, sample >> 4, 0));
1093 out, freerdp_dsp_decode_ms_adpcm_sample(&context->adpcm, sample & 0x0F, 1));
1098 const BYTE sample = *src++;
1101 out, freerdp_dsp_decode_ms_adpcm_sample(&context->adpcm, sample >> 4, 0));
1103 out, freerdp_dsp_decode_ms_adpcm_sample(&context->adpcm, sample & 0x0F, 1));
1110 const BYTE sample = *src++;
1112 Stream_Write_INT16(out,
1113 freerdp_dsp_decode_ms_adpcm_sample(&context->adpcm, sample >> 4, 0));
1115 out, freerdp_dsp_decode_ms_adpcm_sample(&context->adpcm, sample & 0x0F, 0));
1122static BYTE freerdp_dsp_encode_ms_adpcm_sample(ADPCM* WINPR_RESTRICT adpcm, INT32 sample,
1125 WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ms.sample1));
1126 WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ms.sample2));
1127 WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ms.delta));
1128 WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ms.predictor));
1131 ((adpcm->ms.sample1[channel] * ms_adpcm_coeffs1[adpcm->ms.predictor[channel]]) +
1132 (adpcm->ms.sample2[channel] * ms_adpcm_coeffs2[adpcm->ms.predictor[channel]])) /
1134 INT32 errordelta = (sample - presample) / adpcm->ms.delta[channel];
1136 if ((sample - presample) % adpcm->ms.delta[channel] > adpcm->ms.delta[channel] / 2)
1141 else if (errordelta < -8)
1144 presample += adpcm->ms.delta[channel] * errordelta;
1146 if (presample > 32767)
1148 else if (presample < -32768)
1151 adpcm->ms.sample2[channel] = adpcm->ms.sample1[channel];
1152 adpcm->ms.sample1[channel] = presample;
1153 const size_t offset = (((BYTE)errordelta) & 0x0F);
1154 WINPR_ASSERT(offset < ARRAYSIZE(ms_adpcm_adaptation_table));
1155 adpcm->ms.delta[channel] = adpcm->ms.delta[channel] * ms_adpcm_adaptation_table[offset] / 256;
1157 if (adpcm->ms.delta[channel] < 16)
1158 adpcm->ms.delta[channel] = 16;
1160 return ((BYTE)errordelta) & 0x0F;
1163static BOOL freerdp_dsp_encode_ms_adpcm(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
1164 const BYTE* WINPR_RESTRICT src,
size_t size,
1167 if (!valid_ms_adpcm_format(context))
1170 const size_t step = 8 + ((context->common.format.nChannels > 1) ? 4 : 0);
1172 if (!Stream_EnsureRemainingCapacity(out, size))
1175 const size_t start = Stream_GetPosition(out);
1177 if (context->adpcm.ms.delta[0] < 16)
1178 context->adpcm.ms.delta[0] = 16;
1180 if (context->adpcm.ms.delta[1] < 16)
1181 context->adpcm.ms.delta[1] = 16;
1183 while (size >= step)
1185 if ((Stream_GetPosition(out) - start) % context->common.format.nBlockAlign == 0)
1187 if (context->common.format.nChannels > 1)
1189 Stream_Write_UINT8(out, context->adpcm.ms.predictor[0]);
1190 Stream_Write_UINT8(out, context->adpcm.ms.predictor[1]);
1191 Stream_Write_UINT8(out, (context->adpcm.ms.delta[0] & 0xFF));
1192 Stream_Write_UINT8(out, ((context->adpcm.ms.delta[0] >> 8) & 0xFF));
1193 Stream_Write_UINT8(out, (context->adpcm.ms.delta[1] & 0xFF));
1194 Stream_Write_UINT8(out, ((context->adpcm.ms.delta[1] >> 8) & 0xFF));
1196 context->adpcm.ms.sample1[0] = read_int16(src + 4);
1197 context->adpcm.ms.sample1[1] = read_int16(src + 6);
1198 context->adpcm.ms.sample2[0] = read_int16(src + 0);
1199 context->adpcm.ms.sample2[1] = read_int16(src + 2);
1201 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample1[0]);
1202 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample1[1]);
1203 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample2[0]);
1204 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample2[1]);
1211 Stream_Write_UINT8(out, context->adpcm.ms.predictor[0]);
1212 Stream_Write_UINT8(out, (BYTE)(context->adpcm.ms.delta[0] & 0xFF));
1213 Stream_Write_UINT8(out, (BYTE)((context->adpcm.ms.delta[0] >> 8) & 0xFF));
1215 context->adpcm.ms.sample1[0] = read_int16(src + 2);
1216 context->adpcm.ms.sample2[0] = read_int16(src + 0);
1218 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample1[0]);
1219 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample2[0]);
1226 const INT16 sample = read_int16(src);
1229 out, (freerdp_dsp_encode_ms_adpcm_sample(&context->adpcm, sample, 0) << 4) & 0xFF);
1232 const INT16 sample = read_int16(src);
1236 Stream_Read_UINT8(out, val);
1237 val += freerdp_dsp_encode_ms_adpcm_sample(&context->adpcm, sample,
1238 context->common.format.nChannels > 1 ? 1 : 0);
1239 Stream_Write_UINT8(out, val);
1249FREERDP_DSP_CONTEXT* freerdp_dsp_context_new(BOOL encoder)
1251#if defined(WITH_DSP_FFMPEG)
1252 return freerdp_dsp_ffmpeg_context_new(encoder);
1254 FREERDP_DSP_CONTEXT* context = calloc(1,
sizeof(FREERDP_DSP_CONTEXT));
1259 if (!freerdp_dsp_common_context_init(&context->common, encoder))
1262#if defined(WITH_GSM)
1263 context->gsm = gsm_create();
1270 int rc = gsm_option(context->gsm, GSM_OPT_WAV49, &val);
1276#if defined(WITH_LAME)
1280 context->lame = lame_init();
1287 context->hip = hip_decode_init();
1294#if defined(WITH_FAAD2)
1298 context->faad = NeAACDecOpen();
1307 freerdp_dsp_context_free(context);
1312void freerdp_dsp_context_free(FREERDP_DSP_CONTEXT* context)
1317#if defined(WITH_FDK_AAC)
1320 fdk_aac_dsp_uninit(ctx);
1323#if defined(WITH_DSP_FFMPEG)
1324 freerdp_dsp_ffmpeg_context_free(context);
1327 freerdp_dsp_common_context_uninit(&context->common);
1329#if defined(WITH_GSM)
1330 gsm_destroy(context->gsm);
1332#if defined(WITH_LAME)
1334 if (context->common.encoder)
1335 lame_close(context->lame);
1337 hip_decode_exit(context->hip);
1340#if defined(WITH_OPUS)
1342 if (context->opus_decoder)
1343 opus_decoder_destroy(context->opus_decoder);
1344 if (context->opus_encoder)
1345 opus_encoder_destroy(context->opus_encoder);
1348#if defined(WITH_FAAD2)
1350 if (!context->common.encoder)
1351 NeAACDecClose(context->faad);
1354#if defined(WITH_FAAC)
1357 faacEncClose(context->faac);
1360#if defined(WITH_SOXR)
1361 soxr_delete(context->sox);
1368BOOL freerdp_dsp_encode(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
1370 const BYTE* WINPR_RESTRICT pdata,
size_t length,
1373#if defined(WITH_FDK_AAC)
1376 switch (ctx->format.wFormatTag)
1378 case WAVE_FORMAT_AAC_MS:
1379 return fdk_aac_dsp_encode(ctx, srcFormat, pdata, length, out);
1385#if defined(WITH_DSP_FFMPEG)
1386 return freerdp_dsp_ffmpeg_encode(context, srcFormat, pdata, length, out);
1388 if (!context || !context->common.encoder || !srcFormat || !pdata || !out)
1392 const BYTE* resampleData =
nullptr;
1393 size_t resampleLength = 0;
1395 if (!freerdp_dsp_channel_mix(context, pdata, length, srcFormat, &resampleData, &resampleLength))
1398 format.nChannels = context->common.format.nChannels;
1400 const BYTE* data =
nullptr;
1401 if (!freerdp_dsp_resample(context, resampleData, resampleLength, &format, &data, &length))
1404 switch (context->common.format.wFormatTag)
1406 case WAVE_FORMAT_PCM:
1407 if (!Stream_EnsureRemainingCapacity(out, length))
1410 Stream_Write(out, data, length);
1413 case WAVE_FORMAT_ADPCM:
1414 return freerdp_dsp_encode_ms_adpcm(context, data, length, out);
1416 case WAVE_FORMAT_DVI_ADPCM:
1417 return freerdp_dsp_encode_ima_adpcm(context, data, length, out);
1418#if defined(WITH_GSM)
1420 case WAVE_FORMAT_GSM610:
1421 return freerdp_dsp_encode_gsm610(context, data, length, out);
1423#if defined(WITH_LAME)
1425 case WAVE_FORMAT_MPEGLAYER3:
1426 return freerdp_dsp_encode_mp3(context, data, length, out);
1428#if defined(WITH_FAAC)
1430 case WAVE_FORMAT_AAC_MS:
1431 return freerdp_dsp_encode_faac(context, data, length, out);
1433#if defined(WITH_OPUS)
1435 case WAVE_FORMAT_OPUS:
1436 return freerdp_dsp_encode_opus(context, data, length, out);
1446BOOL freerdp_dsp_decode(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
1448 const BYTE* WINPR_RESTRICT data,
size_t length,
wStream* WINPR_RESTRICT out)
1450#if defined(WITH_FDK_AAC)
1453 switch (ctx->format.wFormatTag)
1455 case WAVE_FORMAT_AAC_MS:
1456 return fdk_aac_dsp_decode(ctx, srcFormat, data, length, out);
1462#if defined(WITH_DSP_FFMPEG)
1463 return freerdp_dsp_ffmpeg_decode(context, srcFormat, data, length, out);
1466 if (!context || context->common.encoder || !srcFormat || !data || !out)
1469 switch (context->common.format.wFormatTag)
1471 case WAVE_FORMAT_PCM:
1472 if (!Stream_EnsureRemainingCapacity(out, length))
1475 Stream_Write(out, data, length);
1478 case WAVE_FORMAT_ADPCM:
1479 return freerdp_dsp_decode_ms_adpcm(context, data, length, out);
1481 case WAVE_FORMAT_DVI_ADPCM:
1482 return freerdp_dsp_decode_ima_adpcm(context, data, length, out);
1483#if defined(WITH_GSM)
1485 case WAVE_FORMAT_GSM610:
1486 return freerdp_dsp_decode_gsm610(context, data, length, out);
1488#if defined(WITH_LAME)
1490 case WAVE_FORMAT_MPEGLAYER3:
1491 return freerdp_dsp_decode_mp3(context, data, length, out);
1493#if defined(WITH_FAAD2)
1495 case WAVE_FORMAT_AAC_MS:
1496 return freerdp_dsp_decode_faad(context, data, length, out);
1499#if defined(WITH_OPUS)
1500 case WAVE_FORMAT_OPUS:
1501 return freerdp_dsp_decode_opus(context, data, length, out);
1511BOOL freerdp_dsp_supports_format(
const AUDIO_FORMAT* WINPR_RESTRICT format, BOOL encode)
1513#if defined(WITH_FDK_AAC)
1514 switch (format->wFormatTag)
1516 case WAVE_FORMAT_AAC_MS:
1524#if defined(WITH_DSP_FFMPEG)
1525 return freerdp_dsp_ffmpeg_supports_format(format, encode);
1528#if !defined(WITH_DSP_EXPERIMENTAL)
1529 WINPR_UNUSED(encode);
1531 switch (format->wFormatTag)
1533 case WAVE_FORMAT_PCM:
1535#if defined(WITH_DSP_EXPERIMENTAL)
1537 case WAVE_FORMAT_ADPCM:
1539 case WAVE_FORMAT_DVI_ADPCM:
1542#if defined(WITH_GSM)
1544 case WAVE_FORMAT_GSM610:
1545#if defined(WITH_DSP_EXPERIMENTAL)
1551#if defined(WITH_LAME)
1553 case WAVE_FORMAT_MPEGLAYER3:
1554#if defined(WITH_DSP_EXPERIMENTAL)
1561 case WAVE_FORMAT_AAC_MS:
1562#if defined(WITH_FAAD2)
1567#if defined(WITH_FAAC)
1573#if defined(WITH_FDK_AAC)
1579#if defined(WITH_OPUS)
1580 case WAVE_FORMAT_OPUS:
1581 return opus_is_valid_samplerate(format);
1591BOOL freerdp_dsp_context_reset(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
1593 WINPR_ATTR_UNUSED UINT32 FramesPerPacket)
1595#if defined(WITH_FDK_AAC)
1596 WINPR_ASSERT(targetFormat);
1597 if (targetFormat->wFormatTag == WAVE_FORMAT_AAC_MS)
1600 fdk_aac_dsp_uninit(ctx);
1601 ctx->format = *targetFormat;
1602 return fdk_aac_dsp_init(ctx, FramesPerPacket);
1606#if defined(WITH_DSP_FFMPEG)
1607 return freerdp_dsp_ffmpeg_context_reset(context, targetFormat);
1610 if (!context || !targetFormat)
1613 context->common.format = *targetFormat;
1615 switch (context->common.format.wFormatTag)
1617#if defined(WITH_LAME)
1618 case WAVE_FORMAT_MPEGLAYER3:
1619 if (!valid_mp3_format(context))
1623 case WAVE_FORMAT_ADPCM:
1624 if (!valid_ms_adpcm_format(context))
1627 case WAVE_FORMAT_DVI_ADPCM:
1629 if (!valid_ima_adpcm_format(context))
1631 if (FramesPerPacket == 0)
1634 const size_t min_frame_data = 1ull * context->common.format.wBitsPerSample *
1635 context->common.format.nChannels * FramesPerPacket;
1636 const size_t data_per_block = (1ULL * context->common.format.nBlockAlign -
1637 4ULL * context->common.format.nChannels) *
1639 size_t nb_block_per_packet = min_frame_data / data_per_block;
1641 if (min_frame_data % data_per_block)
1642 nb_block_per_packet++;
1644 context->adpcm.ima.packet_size =
1645 nb_block_per_packet * context->common.format.nBlockAlign;
1646 if (!Stream_EnsureCapacity(context->common.buffer, context->adpcm.ima.packet_size))
1648 Stream_ResetPosition(context->common.buffer);
1655#if defined(WITH_OPUS)
1657 if (opus_is_valid_samplerate(&context->common.format))
1659 if (!context->common.encoder)
1661 int opus_error = OPUS_OK;
1663 context->opus_decoder = opus_decoder_create(
1664 WINPR_ASSERTING_INT_CAST(opus_int32, context->common.format.nSamplesPerSec),
1665 context->common.format.nChannels, &opus_error);
1666 if (opus_error != OPUS_OK)
1671 int opus_error = OPUS_OK;
1673 context->opus_encoder = opus_encoder_create(
1674 WINPR_ASSERTING_INT_CAST(opus_int32, context->common.format.nSamplesPerSec),
1675 context->common.format.nChannels, OPUS_APPLICATION_VOIP, &opus_error);
1676 if (opus_error != OPUS_OK)
1680 opus_encoder_ctl(context->opus_encoder,
1681 OPUS_SET_BITRATE(context->common.format.nAvgBytesPerSec * 8));
1682 if (opus_error != OPUS_OK)
1688#if defined(WITH_FAAD2)
1689 context->faadSetup = FALSE;
1691#if defined(WITH_FAAC)
1693 if (context->common.encoder)
1695 faacEncConfigurationPtr cfg =
nullptr;
1698 faacEncClose(context->faac);
1700 context->faac = faacEncOpen(targetFormat->nSamplesPerSec, targetFormat->nChannels,
1701 &context->faacInputSamples, &context->faacMaxOutputBytes);
1706 cfg = faacEncGetCurrentConfiguration(context->faac);
1707 cfg->inputFormat = FAAC_INPUT_16BIT;
1708 cfg->outputFormat = 0;
1709 cfg->mpegVersion = MPEG4;
1711 cfg->bandWidth = targetFormat->nAvgBytesPerSec;
1712 const int rc = faacEncSetConfiguration(context->faac, cfg);
1718#if defined(WITH_SOXR)
1720 soxr_io_spec_t iospec = soxr_io_spec(SOXR_INT16, SOXR_INT16);
1721 soxr_error_t error =
nullptr;
1723 soxr_delete(context->sox);
1725 soxr_create(context->common.format.nSamplesPerSec, targetFormat->nSamplesPerSec,
1726 targetFormat->nChannels, &error, &iospec,
nullptr,
nullptr);
1728 if (!context->sox || (error !=
nullptr))
1738 WINPR_ASSERT(context);
1739 context->encoder = encode;
1740 context->buffer = Stream_New(
nullptr, 1024);
1741 if (!context->buffer)
1744 context->channelmix = Stream_New(
nullptr, 1024);
1745 if (!context->channelmix)
1748 context->resample = Stream_New(
nullptr, 1024);
1749 if (!context->resample)
1755 freerdp_dsp_common_context_uninit(context);
1761 WINPR_ASSERT(context);
1763 Stream_Free(context->buffer, TRUE);
1764 Stream_Free(context->channelmix, TRUE);
1765 Stream_Free(context->resample, TRUE);
1767 context->buffer =
nullptr;
1768 context->channelmix =
nullptr;
1769 context->resample =
nullptr;