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_MEDIACODEC)
40#include "dsp_mediacodec.h"
43#if !defined(WITH_DSP_FFMPEG)
55#define OPUS_MAX_FRAMES 5760ull
58#if defined(WITH_FAAD2)
71#include "dsp_ffmpeg.h"
74#if !defined(WITH_DSP_FFMPEG)
76#define TAG FREERDP_TAG("dsp")
95struct S_FREERDP_DSP_CONTEXT
104#if defined(WITH_LAME)
108#if defined(WITH_OPUS)
109 OpusDecoder* opus_decoder;
110 OpusEncoder* opus_encoder;
112#if defined(WITH_FAAD2)
117#if defined(WITH_FAAC)
119 unsigned long faacInputSamples;
120 unsigned long faacMaxOutputBytes;
123#if defined(WITH_SOXR)
128#if defined(WITH_OPUS)
129static BOOL opus_is_valid_samplerate(
const AUDIO_FORMAT* WINPR_RESTRICT format)
131 WINPR_ASSERT(format);
133 switch (format->nSamplesPerSec)
147static INT16 read_int16(
const BYTE* WINPR_RESTRICT src)
149 return (INT16)(src[0] | (src[1] << 8));
152static BOOL freerdp_dsp_channel_mix(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
153 const BYTE* WINPR_RESTRICT src,
size_t size,
155 const BYTE** WINPR_RESTRICT data,
size_t* WINPR_RESTRICT length)
157 if (!context || !data || !length)
160 if (srcFormat->wFormatTag != WAVE_FORMAT_PCM)
163 const UINT32 bpp = srcFormat->wBitsPerSample > 8 ? 2 : 1;
164 const size_t samples = size / bpp / srcFormat->nChannels;
166 if (context->common.format.nChannels == srcFormat->nChannels)
173 Stream_ResetPosition(context->common.channelmix);
176 if (context->common.format.nChannels > srcFormat->nChannels)
178 switch (srcFormat->nChannels)
181 if (!Stream_EnsureCapacity(context->common.channelmix, size * 2))
184 for (
size_t x = 0; x < samples; x++)
186 for (
size_t y = 0; y < bpp; y++)
187 Stream_Write_UINT8(context->common.channelmix, src[x * bpp + y]);
189 for (
size_t y = 0; y < bpp; y++)
190 Stream_Write_UINT8(context->common.channelmix, src[x * bpp + y]);
193 Stream_SealLength(context->common.channelmix);
194 *data = Stream_Buffer(context->common.channelmix);
195 *length = Stream_Length(context->common.channelmix);
205 switch (srcFormat->nChannels)
208 if (!Stream_EnsureCapacity(context->common.channelmix, size / 2))
213 for (
size_t x = 0; x < samples; x++)
215 for (
size_t y = 0; y < bpp; y++)
216 Stream_Write_UINT8(context->common.channelmix, src[2 * x * bpp + y]);
219 Stream_SealLength(context->common.channelmix);
220 *data = Stream_Buffer(context->common.channelmix);
221 *length = Stream_Length(context->common.channelmix);
237static BOOL freerdp_dsp_resample(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
238 const BYTE* WINPR_RESTRICT src,
size_t size,
240 const BYTE** WINPR_RESTRICT data,
size_t* WINPR_RESTRICT length)
244 if (srcFormat->wFormatTag != WAVE_FORMAT_PCM)
246 WLog_ERR(TAG,
"requires %s for sample input, got %s",
247 audio_format_get_tag_string(WAVE_FORMAT_PCM),
248 audio_format_get_tag_string(srcFormat->wFormatTag));
254 format.wFormatTag = WAVE_FORMAT_UNKNOWN;
255 format.wBitsPerSample = 0;
257 if (audio_format_compatible(&format, &context->common.format))
264#if defined(WITH_SOXR)
265 const size_t srcBytesPerFrame = (srcFormat->wBitsPerSample > 8) ? 2 : 1;
266 const size_t dstBytesPerFrame = (context->common.format.wBitsPerSample > 8) ? 2 : 1;
267 const size_t srcChannels = srcFormat->nChannels;
268 const size_t dstChannels = context->common.format.nChannels;
269 const size_t sbytes = srcChannels * srcBytesPerFrame;
270 const size_t sframes = size / sbytes;
271 const size_t rbytes = dstBytesPerFrame * dstChannels;
273 const size_t rframes =
274 (sframes * context->common.format.nSamplesPerSec + (srcFormat->nSamplesPerSec + 1) / 2) /
275 srcFormat->nSamplesPerSec;
276 const size_t rsize = rframes * rbytes;
278 if (!Stream_EnsureCapacity(context->common.resample, rsize))
284 soxr_process(context->sox, src, sframes, &idone, Stream_Buffer(context->common.resample),
285 Stream_Capacity(context->common.resample) / rbytes, &odone);
286 if (!Stream_SetLength(context->common.resample, odone * rbytes))
289 *data = Stream_Buffer(context->common.resample);
290 *length = Stream_Length(context->common.resample);
291 return (error ==
nullptr);
293 WLog_ERR(TAG,
"Missing resample support, recompile -DWITH_SOXR=ON or -DWITH_DSP_FFMPEG=ON");
305static const INT16 ima_step_index_table[] = {
306 -1, -1, -1, -1, 2, 4, 6, 8, -1, -1, -1, -1, 2, 4, 6, 8
309static const INT16 ima_step_size_table[] = {
310 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 19, 21, 23,
311 25, 28, 31, 34, 37, 41, 45, 50, 55, 60, 66, 73, 80,
312 88, 97, 107, 118, 130, 143, 157, 173, 190, 209, 230, 253, 279,
313 307, 337, 371, 408, 449, 494, 544, 598, 658, 724, 796, 876, 963,
314 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024, 3327,
315 3660, 4026, 4428, 4871, 5358, 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487,
316 12635, 13899, 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
319static inline void dsp_ima_clamp_step(ADPCM* WINPR_RESTRICT adpcm,
size_t channel)
322 if (adpcm->ima.last_step[channel] < 0)
323 adpcm->ima.last_step[channel] = 0;
325 const size_t size = ARRAYSIZE(ima_step_size_table);
326 if ((
size_t)adpcm->ima.last_step[channel] >= size)
327 adpcm->ima.last_step[channel] = (INT16)(size - 1);
330static UINT16 dsp_decode_ima_adpcm_sample(ADPCM* WINPR_RESTRICT adpcm,
unsigned int channel,
333 WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ima.last_step));
334 WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ima.last_sample));
336 const INT16 offset = adpcm->ima.last_step[channel];
337 WINPR_ASSERT(offset >= 0);
338 WINPR_ASSERT(WINPR_CXX_COMPAT_CAST(
size_t, offset) < ARRAYSIZE(ima_step_size_table));
340 const INT32 ss = ima_step_size_table[offset];
355 d += adpcm->ima.last_sample[channel];
362 adpcm->ima.last_sample[channel] = (INT16)d;
364 WINPR_ASSERT(sample < ARRAYSIZE(ima_step_index_table));
365 const int32_t last = adpcm->ima.last_step[channel] + ima_step_index_table[sample];
366 adpcm->ima.last_step[channel] = WINPR_ASSERTING_INT_CAST(int16_t, last);
368 dsp_ima_clamp_step(adpcm, channel);
373static BOOL valid_ima_adpcm_format(
const FREERDP_DSP_CONTEXT* WINPR_RESTRICT context)
375 WINPR_ASSERT(context);
376 if (context->common.format.wFormatTag != WAVE_FORMAT_DVI_ADPCM)
378 if (context->common.format.nBlockAlign <= 4ULL)
380 if (context->common.format.nChannels < 1)
382 if (context->common.format.wBitsPerSample == 0)
387static BOOL freerdp_dsp_decode_ima_adpcm(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
388 const BYTE* WINPR_RESTRICT src,
size_t size,
391 if (!valid_ima_adpcm_format(context))
394 size_t out_size = size * 4ull;
395 const UINT32 block_size = context->common.format.nBlockAlign;
396 const UINT32 channels = context->common.format.nChannels;
398 if (!Stream_EnsureCapacity(out, out_size))
403 if (size % block_size == 0)
408 context->adpcm.ima.last_sample[0] =
409 (INT16)(((UINT16)(*src)) | (((UINT16)(*(src + 1))) << 8));
410 context->adpcm.ima.last_step[0] = (INT16)(*(src + 2));
412 dsp_ima_clamp_step(&context->adpcm, 0u);
422 context->adpcm.ima.last_sample[1] =
423 (INT16)(((UINT16)(*src)) | (((UINT16)(*(src + 1))) << 8));
424 context->adpcm.ima.last_step[1] = (INT16)(*(src + 2));
426 dsp_ima_clamp_step(&context->adpcm, 1u);
437 for (
size_t i = 0; i < 8; i++)
439 BYTE* dst = Stream_Pointer(out);
441 const unsigned channel = (i < 4 ? 0 : 1);
443 const BYTE sample = ((*src) & 0x0f);
444 const UINT16 decoded =
445 dsp_decode_ima_adpcm_sample(&context->adpcm, channel, sample);
446 dst[((i & 3) << 3) + (channel << 1u)] = (decoded & 0xFF);
447 dst[((i & 3) << 3) + (channel << 1u) + 1] = (decoded >> 8);
450 const BYTE sample = ((*src) >> 4);
451 const UINT16 decoded =
452 dsp_decode_ima_adpcm_sample(&context->adpcm, channel, sample);
453 dst[((i & 3) << 3) + (channel << 1u) + 4] = (decoded & 0xFF);
454 dst[((i & 3) << 3) + (channel << 1u) + 5] = (decoded >> 8);
459 if (!Stream_SafeSeek(out, 32))
467 BYTE* dst = Stream_Pointer(out);
468 if (!Stream_SafeSeek(out, 4))
472 const BYTE sample = ((*src) & 0x0f);
473 const UINT16 decoded = dsp_decode_ima_adpcm_sample(&context->adpcm, 0, sample);
474 *dst++ = (decoded & 0xFF);
475 *dst++ = (decoded >> 8);
478 const BYTE sample = ((*src) >> 4);
479 const UINT16 decoded = dsp_decode_ima_adpcm_sample(&context->adpcm, 0, sample);
480 *dst++ = (decoded & 0xFF);
481 *dst++ = (decoded >> 8);
492static BOOL freerdp_dsp_decode_gsm610(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
493 const BYTE* WINPR_RESTRICT src,
size_t size,
498 while (offset < size)
500 gsm_signal gsmBlockBuffer[160] = WINPR_C_ARRAY_INIT;
502 gsm_decode(context->gsm,
503 WINPR_CAST_CONST_PTR_AWAY( &src[offset], gsm_byte*),
509 if ((offset % 65) == 0)
514 if (!Stream_EnsureRemainingCapacity(out,
sizeof(gsmBlockBuffer)))
517 Stream_Write(out, (
void*)gsmBlockBuffer,
sizeof(gsmBlockBuffer));
523static BOOL freerdp_dsp_encode_gsm610(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
524 const BYTE* WINPR_RESTRICT src,
size_t size,
529 while (offset < size)
531 const gsm_signal* signal = WINPR_PACKED_ALIGN_CAST(
const gsm_signal*, &src[offset]);
533 if (!Stream_EnsureRemainingCapacity(out,
sizeof(gsm_frame)))
536 gsm_encode(context->gsm,
537 WINPR_CAST_CONST_PTR_AWAY( signal, gsm_signal*),
538 Stream_Pointer(out));
540 if ((offset % 65) == 0)
541 Stream_Seek(out, 33);
543 Stream_Seek(out, 32);
552#if defined(WITH_LAME)
553static BOOL valid_mp3_format(
const FREERDP_DSP_CONTEXT* WINPR_RESTRICT context)
555 WINPR_ASSERT(context);
556 if (context->common.format.wFormatTag != WAVE_FORMAT_MPEGLAYER3)
558 if (context->common.format.nChannels < 1)
560 if (context->common.format.wBitsPerSample == 0)
562 if (context->common.format.nSamplesPerSec == 0)
567static BOOL freerdp_dsp_decode_mp3(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
568 const BYTE* WINPR_RESTRICT src,
size_t size,
571 if (!context || !src || !out)
573 if (!valid_mp3_format(context))
575 const size_t buffer_size =
576 2ull * context->common.format.nChannels * context->common.format.nSamplesPerSec;
578 if (!Stream_EnsureCapacity(context->common.buffer, 2ull * buffer_size))
581 short* pcm_l = Stream_BufferAs(context->common.buffer,
short);
582 short* pcm_r = Stream_BufferAs(context->common.buffer,
short) + (buffer_size /
sizeof(short));
583 const int rc = hip_decode(
585 WINPR_CAST_CONST_PTR_AWAY( src,
unsigned char*), size,
591 if (!Stream_EnsureRemainingCapacity(out, (
size_t)rc * context->common.format.nChannels * 2ull))
594 for (
int 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, 5ull / 4ull * samples_per_channel + 7200ull))
620 samples_per_channel = size / 2 / context->common.format.nChannels;
621 const size_t outLen = Stream_GetRemainingCapacity(out);
622 if ((outLen > INT_MAX) || (samples_per_channel > INT_MAX))
625 const int rc = lame_encode_buffer_interleaved(
627 WINPR_CAST_CONST_PTR_AWAY(WINPR_PACKED_ALIGN_CAST(
const short*, src),
short*),
628 WINPR_ASSERTING_INT_CAST(
int, samples_per_channel), Stream_Pointer(out),
629 WINPR_ASSERTING_INT_CAST(
int, outLen));
634 Stream_Seek(out, (
size_t)rc);
639#if defined(WITH_FAAC)
640static BOOL freerdp_dsp_encode_faac(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
641 const BYTE* WINPR_RESTRICT src,
size_t size,
644 const int16_t* inSamples = WINPR_PACKED_ALIGN_CAST(
const int16_t*, src);
646 if (!context || !src || !out)
649 const unsigned int bpp = context->common.format.wBitsPerSample / 8;
650 const size_t nrSamples = size / bpp;
652 if (!Stream_EnsureRemainingCapacity(context->common.buffer, nrSamples *
sizeof(int16_t)))
655 for (
size_t x = 0; x < nrSamples; x++)
657 Stream_Write_INT16(context->common.buffer, inSamples[x]);
658 if (Stream_GetPosition(context->common.buffer) / bpp >= context->faacInputSamples)
660 if (!Stream_EnsureRemainingCapacity(out, context->faacMaxOutputBytes))
663 const size_t outLen = Stream_GetRemainingCapacity(out);
664 if ((outLen > UINT_MAX) || (context->faacInputSamples > UINT_MAX))
667 faacEncEncode(context->faac, Stream_BufferAs(context->common.buffer, int32_t),
668 WINPR_ASSERTING_INT_CAST(
unsigned int, context->faacInputSamples),
669 Stream_Pointer(out), WINPR_ASSERTING_INT_CAST(
unsigned int, outLen));
673 Stream_Seek(out, (
size_t)rc);
674 Stream_ResetPosition(context->common.buffer);
682#if defined(WITH_OPUS)
683static BOOL freerdp_dsp_decode_opus(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
684 const BYTE* WINPR_RESTRICT src,
size_t size,
687 if (!context || !src || !out)
691 const size_t max_size = OPUS_MAX_FRAMES * context->common.format.nChannels *
sizeof(int16_t);
692 if (!Stream_EnsureRemainingCapacity(out, max_size))
695 const opus_int32 frames =
696 opus_decode(context->opus_decoder, src, WINPR_ASSERTING_INT_CAST(opus_int32, size),
697 Stream_Pointer(out), OPUS_MAX_FRAMES, 0);
701 Stream_Seek(out, (
size_t)frames * context->common.format.nChannels *
sizeof(int16_t));
706static BOOL freerdp_dsp_encode_opus(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
707 const BYTE* WINPR_RESTRICT src,
size_t size,
710 if (!context || !src || !out)
714 const size_t max_size = OPUS_MAX_FRAMES * context->common.format.nChannels *
sizeof(int16_t);
715 if (!Stream_EnsureRemainingCapacity(out, max_size))
718 const size_t src_frames = size /
sizeof(opus_int16) / context->common.format.nChannels;
719 const opus_int16* src_data = WINPR_PACKED_ALIGN_CAST(
const opus_int16*, src);
720 const opus_int32 bytes = opus_encode(
721 context->opus_encoder, src_data, WINPR_ASSERTING_INT_CAST(opus_int32, src_frames),
722 Stream_Pointer(out), WINPR_ASSERTING_INT_CAST(opus_int32, max_size));
725 return Stream_SafeSeek(out, (
size_t)bytes);
729#if defined(WITH_FAAD2)
730static BOOL freerdp_dsp_decode_faad(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
731 const BYTE* WINPR_RESTRICT src,
size_t size,
734 NeAACDecFrameInfo info;
737 if (!context || !src || !out)
740 if (!context->faadSetup)
747 unsigned long samplerate = 0;
748 unsigned char channels = 0;
751 const long err = NeAACDecInit(context->faad, cnv.pv,
752 size, &samplerate, &channels);
757 if (channels != context->common.format.nChannels)
760 if (samplerate != context->common.format.nSamplesPerSec)
763 context->faadSetup = TRUE;
766 while (offset < size)
774 const size_t outSize = context->common.format.nSamplesPerSec *
775 context->common.format.nChannels *
776 context->common.format.wBitsPerSample / 8;
778 if (!Stream_EnsureRemainingCapacity(out, outSize))
781 void* sample_buffer = Stream_Pointer(out);
783 cnv.cpv = &src[offset];
784 NeAACDecDecode2(context->faad, &info, cnv.pv, size - offset, &sample_buffer,
785 Stream_GetRemainingCapacity(out));
790 offset += info.bytesconsumed;
792 if (info.samples == 0)
795 Stream_Seek(out, info.samples * context->common.format.wBitsPerSample / 8);
814} ima_stereo_encode_map[] = { { 0, 0 }, { 4, 0 }, { 0, 4 }, { 4, 4 }, { 1, 0 }, { 5, 0 },
815 { 1, 4 }, { 5, 4 }, { 2, 0 }, { 6, 0 }, { 2, 4 }, { 6, 4 },
816 { 3, 0 }, { 7, 0 }, { 3, 4 }, { 7, 4 } };
818static BYTE dsp_encode_ima_adpcm_sample(ADPCM* WINPR_RESTRICT adpcm,
size_t channel, INT16 sample)
820 WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ima.last_step));
821 WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ima.last_sample));
823 const INT16 offset = adpcm->ima.last_step[channel];
824 WINPR_ASSERT(offset >= 0);
825 WINPR_ASSERT(WINPR_CXX_COMPAT_CAST(
size_t, offset) < ARRAYSIZE(ima_step_size_table));
827 INT32 ss = ima_step_size_table[offset];
828 INT32 e = sample - adpcm->ima.last_sample[channel];
830 INT32 diff = ss >> 3;
866 diff += adpcm->ima.last_sample[channel];
870 else if (diff > 32767)
873 adpcm->ima.last_sample[channel] = (INT16)diff;
875 WINPR_ASSERT(enc < ARRAYSIZE(ima_step_index_table));
876 const int32_t last = adpcm->ima.last_step[channel] + ima_step_index_table[enc];
877 adpcm->ima.last_step[channel] = WINPR_ASSERTING_INT_CAST(int16_t, last);
879 dsp_ima_clamp_step(adpcm, channel);
884static BOOL freerdp_dsp_encode_ima_adpcm(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
885 const BYTE* WINPR_RESTRICT src,
size_t size,
888 if (!valid_ima_adpcm_format(context))
890 if (!Stream_EnsureRemainingCapacity(out, size))
892 if (!Stream_EnsureRemainingCapacity(context->common.buffer, size + 64ull))
895 const size_t align = (context->common.format.nChannels > 1) ? 32 : 4;
897 while (size >= align)
899 if (Stream_GetPosition(context->common.buffer) % context->common.format.nBlockAlign == 0)
901 Stream_Write_UINT8(context->common.buffer, context->adpcm.ima.last_sample[0] & 0xFF);
902 Stream_Write_UINT8(context->common.buffer,
903 (context->adpcm.ima.last_sample[0] >> 8) & 0xFF);
904 Stream_Write_UINT8(context->common.buffer, (BYTE)context->adpcm.ima.last_step[0]);
905 Stream_Write_UINT8(context->common.buffer, 0);
907 if (context->common.format.nChannels > 1)
909 Stream_Write_UINT8(context->common.buffer,
910 context->adpcm.ima.last_sample[1] & 0xFF);
911 Stream_Write_UINT8(context->common.buffer,
912 (context->adpcm.ima.last_sample[1] >> 8) & 0xFF);
913 Stream_Write_UINT8(context->common.buffer, (BYTE)context->adpcm.ima.last_step[1]);
914 Stream_Write_UINT8(context->common.buffer, 0);
918 if (context->common.format.nChannels > 1)
920 BYTE* dst = Stream_Pointer(context->common.buffer);
923 for (
size_t i = 0; i < 16; i++)
925 const INT16 sample = (INT16)(((UINT16)(*src)) | (((UINT16)(*(src + 1))) << 8));
927 const BYTE encoded = dsp_encode_ima_adpcm_sample(&context->adpcm, i % 2, sample);
928 dst[ima_stereo_encode_map[i].byte_num] |= encoded
929 << ima_stereo_encode_map[i].byte_shift;
932 if (!Stream_SafeSeek(context->common.buffer, 8))
938 INT16 sample = (INT16)(((UINT16)(*src)) | (((UINT16)(*(src + 1))) << 8));
940 BYTE encoded = dsp_encode_ima_adpcm_sample(&context->adpcm, 0, sample);
941 sample = (INT16)(((UINT16)(*src)) | (((UINT16)(*(src + 1))) << 8));
943 encoded |= dsp_encode_ima_adpcm_sample(&context->adpcm, 0, sample) << 4;
944 Stream_Write_UINT8(context->common.buffer, encoded);
948 if (Stream_GetPosition(context->common.buffer) >= context->adpcm.ima.packet_size)
950 BYTE* bsrc = Stream_Buffer(context->common.buffer);
951 Stream_Write(out, bsrc, context->adpcm.ima.packet_size);
952 Stream_ResetPosition(context->common.buffer);
965static const INT32 ms_adpcm_adaptation_table[] = { 230, 230, 230, 230, 307, 409, 512, 614,
966 768, 614, 512, 409, 307, 230, 230, 230 };
968static const INT32 ms_adpcm_coeffs1[7] = { 256, 512, 0, 192, 240, 460, 392 };
970static const INT32 ms_adpcm_coeffs2[7] = { 0, -256, 0, 64, 0, -208, -232 };
972static inline INT16 freerdp_dsp_decode_ms_adpcm_sample(ADPCM* WINPR_RESTRICT adpcm, BYTE sample,
975 WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ms.sample1));
976 WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ms.sample2));
977 WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ms.delta));
978 WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ms.predictor));
980 const INT8 nibble = (INT8)((sample & 0x08) ? (sample - 16) : sample);
981 const BYTE predictor = adpcm->ms.predictor[channel];
983 if (predictor < ARRAYSIZE(ms_adpcm_coeffs1))
984 coeff1 = ms_adpcm_coeffs1[predictor];
987 if (predictor < ARRAYSIZE(ms_adpcm_coeffs2))
988 coeff2 = ms_adpcm_coeffs2[predictor];
990 ((adpcm->ms.sample1[channel] * coeff1) + (adpcm->ms.sample2[channel] * coeff2)) / 256;
991 presample += nibble * adpcm->ms.delta[channel];
993 if (presample > 32767)
995 else if (presample < -32768)
998 adpcm->ms.sample2[channel] = adpcm->ms.sample1[channel];
999 adpcm->ms.sample1[channel] = presample;
1002 if (sample < ARRAYSIZE(ms_adpcm_adaptation_table))
1003 tableval = ms_adpcm_adaptation_table[sample];
1005 adpcm->ms.delta[channel] = adpcm->ms.delta[channel] * tableval / 256;
1007 if (adpcm->ms.delta[channel] < 16)
1008 adpcm->ms.delta[channel] = 16;
1010 return (INT16)presample;
1013static BOOL valid_ms_adpcm_format(
const FREERDP_DSP_CONTEXT* WINPR_RESTRICT context)
1015 WINPR_ASSERT(context);
1016 if (context->common.format.wFormatTag != WAVE_FORMAT_ADPCM)
1018 if (context->common.format.nBlockAlign <= 4ULL)
1020 if (context->common.format.nChannels < 1)
1022 if (context->common.format.wBitsPerSample == 0)
1027static BOOL freerdp_dsp_decode_ms_adpcm(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
1028 const BYTE* WINPR_RESTRICT src,
size_t size,
1031 if (!valid_ms_adpcm_format(context))
1033 const size_t out_size = size * 4;
1034 const UINT32 channels = context->common.format.nChannels;
1035 const UINT32 block_size = context->common.format.nBlockAlign;
1037 if (!Stream_EnsureCapacity(out, out_size))
1042 if (size % block_size == 0)
1049 context->adpcm.ms.predictor[0] = *src++;
1050 context->adpcm.ms.predictor[1] = *src++;
1051 context->adpcm.ms.delta[0] = read_int16(src);
1053 context->adpcm.ms.delta[1] = read_int16(src);
1055 context->adpcm.ms.sample1[0] = read_int16(src);
1057 context->adpcm.ms.sample1[1] = read_int16(src);
1059 context->adpcm.ms.sample2[0] = read_int16(src);
1061 context->adpcm.ms.sample2[1] = read_int16(src);
1064 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample2[0]);
1065 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample2[1]);
1066 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample1[0]);
1067 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample1[1]);
1074 context->adpcm.ms.predictor[0] = *src++;
1075 context->adpcm.ms.delta[0] = read_int16(src);
1077 context->adpcm.ms.sample1[0] = read_int16(src);
1079 context->adpcm.ms.sample2[0] = read_int16(src);
1082 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample2[0]);
1083 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample1[0]);
1092 const BYTE sample = *src++;
1095 out, freerdp_dsp_decode_ms_adpcm_sample(&context->adpcm, sample >> 4, 0));
1097 out, freerdp_dsp_decode_ms_adpcm_sample(&context->adpcm, sample & 0x0F, 1));
1102 const BYTE sample = *src++;
1105 out, freerdp_dsp_decode_ms_adpcm_sample(&context->adpcm, sample >> 4, 0));
1107 out, freerdp_dsp_decode_ms_adpcm_sample(&context->adpcm, sample & 0x0F, 1));
1114 const BYTE sample = *src++;
1116 Stream_Write_INT16(out,
1117 freerdp_dsp_decode_ms_adpcm_sample(&context->adpcm, sample >> 4, 0));
1119 out, freerdp_dsp_decode_ms_adpcm_sample(&context->adpcm, sample & 0x0F, 0));
1126static BYTE freerdp_dsp_encode_ms_adpcm_sample(ADPCM* WINPR_RESTRICT adpcm, INT32 sample,
1129 WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ms.sample1));
1130 WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ms.sample2));
1131 WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ms.delta));
1132 WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ms.predictor));
1135 ((adpcm->ms.sample1[channel] * ms_adpcm_coeffs1[adpcm->ms.predictor[channel]]) +
1136 (adpcm->ms.sample2[channel] * ms_adpcm_coeffs2[adpcm->ms.predictor[channel]])) /
1138 INT32 errordelta = (sample - presample) / adpcm->ms.delta[channel];
1140 if ((sample - presample) % adpcm->ms.delta[channel] > adpcm->ms.delta[channel] / 2)
1145 else if (errordelta < -8)
1148 presample += adpcm->ms.delta[channel] * errordelta;
1150 if (presample > 32767)
1152 else if (presample < -32768)
1155 adpcm->ms.sample2[channel] = adpcm->ms.sample1[channel];
1156 adpcm->ms.sample1[channel] = presample;
1157 const size_t offset = (((BYTE)errordelta) & 0x0F);
1158 WINPR_ASSERT(offset < ARRAYSIZE(ms_adpcm_adaptation_table));
1159 adpcm->ms.delta[channel] = adpcm->ms.delta[channel] * ms_adpcm_adaptation_table[offset] / 256;
1161 if (adpcm->ms.delta[channel] < 16)
1162 adpcm->ms.delta[channel] = 16;
1164 return ((BYTE)errordelta) & 0x0F;
1167static BOOL freerdp_dsp_encode_ms_adpcm(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
1168 const BYTE* WINPR_RESTRICT src,
size_t size,
1171 if (!valid_ms_adpcm_format(context))
1174 const size_t step = 8 + ((context->common.format.nChannels > 1) ? 4 : 0);
1176 if (!Stream_EnsureRemainingCapacity(out, size))
1179 const size_t start = Stream_GetPosition(out);
1181 if (context->adpcm.ms.delta[0] < 16)
1182 context->adpcm.ms.delta[0] = 16;
1184 if (context->adpcm.ms.delta[1] < 16)
1185 context->adpcm.ms.delta[1] = 16;
1187 while (size >= step)
1189 if ((Stream_GetPosition(out) - start) % context->common.format.nBlockAlign == 0)
1191 if (context->common.format.nChannels > 1)
1193 Stream_Write_UINT8(out, context->adpcm.ms.predictor[0]);
1194 Stream_Write_UINT8(out, context->adpcm.ms.predictor[1]);
1195 Stream_Write_UINT8(out, (context->adpcm.ms.delta[0] & 0xFF));
1196 Stream_Write_UINT8(out, ((context->adpcm.ms.delta[0] >> 8) & 0xFF));
1197 Stream_Write_UINT8(out, (context->adpcm.ms.delta[1] & 0xFF));
1198 Stream_Write_UINT8(out, ((context->adpcm.ms.delta[1] >> 8) & 0xFF));
1200 context->adpcm.ms.sample1[0] = read_int16(src + 4);
1201 context->adpcm.ms.sample1[1] = read_int16(src + 6);
1202 context->adpcm.ms.sample2[0] = read_int16(src + 0);
1203 context->adpcm.ms.sample2[1] = read_int16(src + 2);
1205 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample1[0]);
1206 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample1[1]);
1207 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample2[0]);
1208 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample2[1]);
1215 Stream_Write_UINT8(out, context->adpcm.ms.predictor[0]);
1216 Stream_Write_UINT8(out, (BYTE)(context->adpcm.ms.delta[0] & 0xFF));
1217 Stream_Write_UINT8(out, (BYTE)((context->adpcm.ms.delta[0] >> 8) & 0xFF));
1219 context->adpcm.ms.sample1[0] = read_int16(src + 2);
1220 context->adpcm.ms.sample2[0] = read_int16(src + 0);
1222 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample1[0]);
1223 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample2[0]);
1230 const INT16 sample = read_int16(src);
1233 out, (freerdp_dsp_encode_ms_adpcm_sample(&context->adpcm, sample, 0) << 4) & 0xFF);
1236 const INT16 sample = read_int16(src);
1240 Stream_Read_UINT8(out, val);
1241 val += freerdp_dsp_encode_ms_adpcm_sample(&context->adpcm, sample,
1242 context->common.format.nChannels > 1 ? 1 : 0);
1243 Stream_Write_UINT8(out, val);
1253FREERDP_DSP_CONTEXT* freerdp_dsp_context_new(BOOL encoder)
1255#if defined(WITH_DSP_FFMPEG)
1256 return freerdp_dsp_ffmpeg_context_new(encoder);
1258 FREERDP_DSP_CONTEXT* context = calloc(1,
sizeof(FREERDP_DSP_CONTEXT));
1263 if (!freerdp_dsp_common_context_init(&context->common, encoder))
1266#if defined(WITH_GSM)
1267 context->gsm = gsm_create();
1274 int rc = gsm_option(context->gsm, GSM_OPT_WAV49, &val);
1280#if defined(WITH_LAME)
1284 context->lame = lame_init();
1291 context->hip = hip_decode_init();
1298#if defined(WITH_FAAD2)
1302 context->faad = NeAACDecOpen();
1311 freerdp_dsp_context_free(context);
1316void freerdp_dsp_context_free(FREERDP_DSP_CONTEXT* context)
1321#if defined(WITH_FDK_AAC)
1324 fdk_aac_dsp_uninit(ctx);
1327#if defined(WITH_MEDIACODEC)
1331#if defined(WITH_DSP_FFMPEG)
1332 freerdp_dsp_ffmpeg_context_free(context);
1335 freerdp_dsp_common_context_uninit(&context->common);
1337#if defined(WITH_GSM)
1338 gsm_destroy(context->gsm);
1340#if defined(WITH_LAME)
1342 if (context->common.encoder)
1343 lame_close(context->lame);
1345 hip_decode_exit(context->hip);
1348#if defined(WITH_OPUS)
1350 if (context->opus_decoder)
1351 opus_decoder_destroy(context->opus_decoder);
1352 if (context->opus_encoder)
1353 opus_encoder_destroy(context->opus_encoder);
1356#if defined(WITH_FAAD2)
1358 if (!context->common.encoder)
1359 NeAACDecClose(context->faad);
1362#if defined(WITH_FAAC)
1365 faacEncClose(context->faac);
1368#if defined(WITH_SOXR)
1369 soxr_delete(context->sox);
1376BOOL freerdp_dsp_encode(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
1378 const BYTE* WINPR_RESTRICT pdata,
size_t length,
1381#if defined(WITH_MEDIACODEC)
1383 if (mcctx && (mcctx->format.wFormatTag == WAVE_FORMAT_AAC_MS))
1384 return mediacodec_aac_dsp_encode(mcctx, srcFormat, pdata, length, out);
1387#if defined(WITH_FDK_AAC)
1390 switch (ctx->format.wFormatTag)
1392 case WAVE_FORMAT_AAC_MS:
1393 return fdk_aac_dsp_encode(ctx, srcFormat, pdata, length, out);
1399#if defined(WITH_DSP_FFMPEG)
1400 return freerdp_dsp_ffmpeg_encode(context, srcFormat, pdata, length, out);
1402 if (!context || !context->common.encoder || !srcFormat || !pdata || !out)
1406 const BYTE* resampleData =
nullptr;
1407 size_t resampleLength = 0;
1409 if (!freerdp_dsp_channel_mix(context, pdata, length, srcFormat, &resampleData, &resampleLength))
1412 format.nChannels = context->common.format.nChannels;
1414 const BYTE* data =
nullptr;
1415 if (!freerdp_dsp_resample(context, resampleData, resampleLength, &format, &data, &length))
1418 switch (context->common.format.wFormatTag)
1420 case WAVE_FORMAT_PCM:
1421 if (!Stream_EnsureRemainingCapacity(out, length))
1424 Stream_Write(out, data, length);
1427 case WAVE_FORMAT_ADPCM:
1428 return freerdp_dsp_encode_ms_adpcm(context, data, length, out);
1430 case WAVE_FORMAT_DVI_ADPCM:
1431 return freerdp_dsp_encode_ima_adpcm(context, data, length, out);
1432#if defined(WITH_GSM)
1434 case WAVE_FORMAT_GSM610:
1435 return freerdp_dsp_encode_gsm610(context, data, length, out);
1437#if defined(WITH_LAME)
1439 case WAVE_FORMAT_MPEGLAYER3:
1440 return freerdp_dsp_encode_mp3(context, data, length, out);
1442#if defined(WITH_FAAC)
1444 case WAVE_FORMAT_AAC_MS:
1445 return freerdp_dsp_encode_faac(context, data, length, out);
1447#if defined(WITH_OPUS)
1449 case WAVE_FORMAT_OPUS:
1450 return freerdp_dsp_encode_opus(context, data, length, out);
1460BOOL freerdp_dsp_decode(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
1462 const BYTE* WINPR_RESTRICT data,
size_t length,
wStream* WINPR_RESTRICT out)
1464#if defined(WITH_MEDIACODEC)
1466 if (mcctx && (mcctx->format.wFormatTag == WAVE_FORMAT_AAC_MS))
1467 return mediacodec_aac_dsp_decode(mcctx, srcFormat, data, length, out);
1470#if defined(WITH_FDK_AAC)
1473 switch (ctx->format.wFormatTag)
1475 case WAVE_FORMAT_AAC_MS:
1476 return fdk_aac_dsp_decode(ctx, srcFormat, data, length, out);
1482#if defined(WITH_DSP_FFMPEG)
1483 return freerdp_dsp_ffmpeg_decode(context, srcFormat, data, length, out);
1486 if (!context || context->common.encoder || !srcFormat || !data || !out)
1489 switch (context->common.format.wFormatTag)
1491 case WAVE_FORMAT_PCM:
1492 if (!Stream_EnsureRemainingCapacity(out, length))
1495 Stream_Write(out, data, length);
1498 case WAVE_FORMAT_ADPCM:
1499 return freerdp_dsp_decode_ms_adpcm(context, data, length, out);
1501 case WAVE_FORMAT_DVI_ADPCM:
1502 return freerdp_dsp_decode_ima_adpcm(context, data, length, out);
1503#if defined(WITH_GSM)
1505 case WAVE_FORMAT_GSM610:
1506 return freerdp_dsp_decode_gsm610(context, data, length, out);
1508#if defined(WITH_LAME)
1510 case WAVE_FORMAT_MPEGLAYER3:
1511 return freerdp_dsp_decode_mp3(context, data, length, out);
1513#if defined(WITH_FAAD2)
1515 case WAVE_FORMAT_AAC_MS:
1516 return freerdp_dsp_decode_faad(context, data, length, out);
1519#if defined(WITH_OPUS)
1520 case WAVE_FORMAT_OPUS:
1521 return freerdp_dsp_decode_opus(context, data, length, out);
1531BOOL freerdp_dsp_supports_format(
const AUDIO_FORMAT* WINPR_RESTRICT format, BOOL encode)
1533#if defined(WITH_MEDIACODEC)
1534 if (format && (format->wFormatTag == WAVE_FORMAT_AAC_MS))
1535 return mediacodec_aac_dsp_supports_format(format, encode);
1538#if defined(WITH_FDK_AAC)
1539 switch (format->wFormatTag)
1541 case WAVE_FORMAT_AAC_MS:
1549#if defined(WITH_DSP_FFMPEG)
1550 return freerdp_dsp_ffmpeg_supports_format(format, encode);
1553#if !defined(WITH_DSP_EXPERIMENTAL)
1554 WINPR_UNUSED(encode);
1556 switch (format->wFormatTag)
1558 case WAVE_FORMAT_PCM:
1560#if defined(WITH_DSP_EXPERIMENTAL)
1562 case WAVE_FORMAT_ADPCM:
1564 case WAVE_FORMAT_DVI_ADPCM:
1567#if defined(WITH_GSM)
1569 case WAVE_FORMAT_GSM610:
1570#if defined(WITH_DSP_EXPERIMENTAL)
1576#if defined(WITH_LAME)
1578 case WAVE_FORMAT_MPEGLAYER3:
1579#if defined(WITH_DSP_EXPERIMENTAL)
1586 case WAVE_FORMAT_AAC_MS:
1587#if defined(WITH_FAAD2)
1592#if defined(WITH_FAAC)
1598#if defined(WITH_FDK_AAC)
1604#if defined(WITH_OPUS)
1605 case WAVE_FORMAT_OPUS:
1606 return opus_is_valid_samplerate(format);
1616BOOL freerdp_dsp_context_reset(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
1618 WINPR_ATTR_UNUSED UINT32 FramesPerPacket)
1620#if defined(WITH_MEDIACODEC)
1621 if (!context || !targetFormat)
1624 if (targetFormat->wFormatTag == WAVE_FORMAT_AAC_MS)
1627 mediacodec_aac_dsp_uninit(ctx);
1628 ctx->format = *targetFormat;
1629 return mediacodec_aac_dsp_init(ctx, FramesPerPacket);
1633#if defined(WITH_FDK_AAC)
1634 WINPR_ASSERT(targetFormat);
1635 if (targetFormat->wFormatTag == WAVE_FORMAT_AAC_MS)
1638 fdk_aac_dsp_uninit(ctx);
1639 ctx->format = *targetFormat;
1640 return fdk_aac_dsp_init(ctx, FramesPerPacket);
1644#if defined(WITH_DSP_FFMPEG)
1645 return freerdp_dsp_ffmpeg_context_reset(context, targetFormat);
1648 if (!context || !targetFormat)
1651 context->common.format = *targetFormat;
1653 switch (context->common.format.wFormatTag)
1655#if defined(WITH_LAME)
1656 case WAVE_FORMAT_MPEGLAYER3:
1657 if (!valid_mp3_format(context))
1661 case WAVE_FORMAT_ADPCM:
1662 if (!valid_ms_adpcm_format(context))
1665 case WAVE_FORMAT_DVI_ADPCM:
1667 if (!valid_ima_adpcm_format(context))
1669 if (FramesPerPacket == 0)
1672 const size_t min_frame_data = 1ull * context->common.format.wBitsPerSample *
1673 context->common.format.nChannels * FramesPerPacket;
1674 const size_t data_per_block = (1ULL * context->common.format.nBlockAlign -
1675 4ULL * context->common.format.nChannels) *
1677 size_t nb_block_per_packet = min_frame_data / data_per_block;
1679 if (min_frame_data % data_per_block)
1680 nb_block_per_packet++;
1682 context->adpcm.ima.packet_size =
1683 nb_block_per_packet * context->common.format.nBlockAlign;
1684 if (!Stream_EnsureCapacity(context->common.buffer, context->adpcm.ima.packet_size))
1686 Stream_ResetPosition(context->common.buffer);
1693#if defined(WITH_OPUS)
1695 if (opus_is_valid_samplerate(&context->common.format))
1697 if (!context->common.encoder)
1699 int opus_error = OPUS_OK;
1701 context->opus_decoder = opus_decoder_create(
1702 WINPR_ASSERTING_INT_CAST(opus_int32, context->common.format.nSamplesPerSec),
1703 context->common.format.nChannels, &opus_error);
1704 if (opus_error != OPUS_OK)
1709 int opus_error = OPUS_OK;
1711 context->opus_encoder = opus_encoder_create(
1712 WINPR_ASSERTING_INT_CAST(opus_int32, context->common.format.nSamplesPerSec),
1713 context->common.format.nChannels, OPUS_APPLICATION_VOIP, &opus_error);
1714 if (opus_error != OPUS_OK)
1718 opus_encoder_ctl(context->opus_encoder,
1719 OPUS_SET_BITRATE(context->common.format.nAvgBytesPerSec * 8));
1720 if (opus_error != OPUS_OK)
1726#if defined(WITH_FAAD2)
1727 context->faadSetup = FALSE;
1729#if defined(WITH_FAAC)
1731 if (context->common.encoder)
1733 faacEncConfigurationPtr cfg =
nullptr;
1736 faacEncClose(context->faac);
1738 context->faac = faacEncOpen(targetFormat->nSamplesPerSec, targetFormat->nChannels,
1739 &context->faacInputSamples, &context->faacMaxOutputBytes);
1744 cfg = faacEncGetCurrentConfiguration(context->faac);
1745 cfg->inputFormat = FAAC_INPUT_16BIT;
1746 cfg->outputFormat = 0;
1747 cfg->mpegVersion = MPEG4;
1749 cfg->bandWidth = targetFormat->nAvgBytesPerSec;
1750 const int rc = faacEncSetConfiguration(context->faac, cfg);
1756#if defined(WITH_SOXR)
1758 soxr_io_spec_t iospec = soxr_io_spec(SOXR_INT16, SOXR_INT16);
1759 soxr_error_t error =
nullptr;
1761 soxr_delete(context->sox);
1763 soxr_create(context->common.format.nSamplesPerSec, targetFormat->nSamplesPerSec,
1764 targetFormat->nChannels, &error, &iospec,
nullptr,
nullptr);
1766 if (!context->sox || (error !=
nullptr))
1776 WINPR_ASSERT(context);
1777 context->encoder = encode;
1778 context->buffer = Stream_New(
nullptr, 1024);
1779 if (!context->buffer)
1782 context->channelmix = Stream_New(
nullptr, 1024);
1783 if (!context->channelmix)
1786 context->resample = Stream_New(
nullptr, 1024);
1787 if (!context->resample)
1793 freerdp_dsp_common_context_uninit(context);
1799 WINPR_ASSERT(context);
1801 Stream_Free(context->buffer, TRUE);
1802 Stream_Free(context->channelmix, TRUE);
1803 Stream_Free(context->resample, TRUE);
1805 context->buffer =
nullptr;
1806 context->channelmix =
nullptr;
1807 context->resample =
nullptr;