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 UINT16 dsp_decode_ima_adpcm_sample(ADPCM* WINPR_RESTRICT adpcm,
unsigned int channel,
326 const INT32 ss = ima_step_size_table[adpcm->ima.last_step[channel]];
341 d += adpcm->ima.last_sample[channel];
348 adpcm->ima.last_sample[channel] = (INT16)d;
349 adpcm->ima.last_step[channel] = adpcm->ima.last_step[channel] + ima_step_index_table[sample];
351 if (adpcm->ima.last_step[channel] < 0)
352 adpcm->ima.last_step[channel] = 0;
353 else if (adpcm->ima.last_step[channel] > 88)
354 adpcm->ima.last_step[channel] = 88;
359static BOOL freerdp_dsp_decode_ima_adpcm(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
360 const BYTE* WINPR_RESTRICT src,
size_t size,
363 size_t out_size = size * 4;
364 const UINT32 block_size = context->common.format.nBlockAlign;
365 const UINT32 channels = context->common.format.nChannels;
367 if (!Stream_EnsureCapacity(out, out_size))
372 if (size % block_size == 0)
374 context->adpcm.ima.last_sample[0] =
375 (INT16)(((UINT16)(*src)) | (((UINT16)(*(src + 1))) << 8));
376 context->adpcm.ima.last_step[0] = (INT16)(*(src + 2));
383 context->adpcm.ima.last_sample[1] =
384 (INT16)(((UINT16)(*src)) | (((UINT16)(*(src + 1))) << 8));
385 context->adpcm.ima.last_step[1] = (INT16)(*(src + 2));
394 for (
size_t i = 0; i < 8; i++)
396 BYTE* dst = Stream_Pointer(out);
398 const unsigned channel = (i < 4 ? 0 : 1);
400 const BYTE sample = ((*src) & 0x0f);
401 const UINT16 decoded =
402 dsp_decode_ima_adpcm_sample(&context->adpcm, channel, sample);
403 dst[((i & 3) << 3) + (channel << 1u)] = (decoded & 0xFF);
404 dst[((i & 3) << 3) + (channel << 1u) + 1] = (decoded >> 8);
407 const BYTE sample = ((*src) >> 4);
408 const UINT16 decoded =
409 dsp_decode_ima_adpcm_sample(&context->adpcm, channel, sample);
410 dst[((i & 3) << 3) + (channel << 1u) + 4] = (decoded & 0xFF);
411 dst[((i & 3) << 3) + (channel << 1u) + 5] = (decoded >> 8);
416 if (!Stream_SafeSeek(out, 32))
422 BYTE* dst = Stream_Pointer(out);
423 if (!Stream_SafeSeek(out, 4))
427 const BYTE sample = ((*src) & 0x0f);
428 const UINT16 decoded = dsp_decode_ima_adpcm_sample(&context->adpcm, 0, sample);
429 *dst++ = (decoded & 0xFF);
430 *dst++ = (decoded >> 8);
433 const BYTE sample = ((*src) >> 4);
434 const UINT16 decoded = dsp_decode_ima_adpcm_sample(&context->adpcm, 0, sample);
435 *dst++ = (decoded & 0xFF);
436 *dst++ = (decoded >> 8);
447static BOOL freerdp_dsp_decode_gsm610(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
448 const BYTE* WINPR_RESTRICT src,
size_t size,
453 while (offset < size)
456 gsm_signal gsmBlockBuffer[160] = WINPR_C_ARRAY_INIT;
457 rc = gsm_decode(context->gsm, (gsm_byte*) &src[offset],
463 if ((offset % 65) == 0)
468 if (!Stream_EnsureRemainingCapacity(out,
sizeof(gsmBlockBuffer)))
471 Stream_Write(out, (
void*)gsmBlockBuffer,
sizeof(gsmBlockBuffer));
477static BOOL freerdp_dsp_encode_gsm610(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
478 const BYTE* WINPR_RESTRICT src,
size_t size,
483 while (offset < size)
485 const gsm_signal* signal = (
const gsm_signal*)&src[offset];
487 if (!Stream_EnsureRemainingCapacity(out,
sizeof(gsm_frame)))
490 gsm_encode(context->gsm, (gsm_signal*) signal,
491 Stream_Pointer(out));
493 if ((offset % 65) == 0)
494 Stream_Seek(out, 33);
496 Stream_Seek(out, 32);
505#if defined(WITH_LAME)
506static BOOL freerdp_dsp_decode_mp3(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
507 const BYTE* WINPR_RESTRICT src,
size_t size,
515 if (!context || !src || !out)
518 buffer_size = 2 * context->common.format.nChannels * context->common.format.nSamplesPerSec;
520 if (!Stream_EnsureCapacity(context->common.buffer, 2 * buffer_size))
523 pcm_l = Stream_BufferAs(context->common.buffer,
short);
524 pcm_r = Stream_BufferAs(context->common.buffer,
short) + buffer_size;
525 rc = hip_decode(context->hip, (
unsigned char*) src, size,
531 if (!Stream_EnsureRemainingCapacity(out, (
size_t)rc * context->common.format.nChannels * 2))
534 for (
size_t x = 0; x < rc; x++)
536 Stream_Write_UINT16(out, (UINT16)pcm_l[x]);
537 Stream_Write_UINT16(out, (UINT16)pcm_r[x]);
543static BOOL freerdp_dsp_encode_mp3(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
544 const BYTE* WINPR_RESTRICT src,
size_t size,
547 size_t samples_per_channel;
550 if (!context || !src || !out)
553 samples_per_channel =
554 size / context->common.format.nChannels / context->common.format.wBitsPerSample / 8;
557 if (!Stream_EnsureRemainingCapacity(out, 5 / 4 * samples_per_channel + 7200))
560 samples_per_channel = size / 2 / context->common.format.nChannels;
561 rc = lame_encode_buffer_interleaved(context->lame, (
short*)src, samples_per_channel,
562 Stream_Pointer(out), Stream_GetRemainingCapacity(out));
567 Stream_Seek(out, (
size_t)rc);
572#if defined(WITH_FAAC)
573static BOOL freerdp_dsp_encode_faac(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
574 const BYTE* WINPR_RESTRICT src,
size_t size,
577 const int16_t* inSamples = (
const int16_t*)src;
582 if (!context || !src || !out)
585 bpp = context->common.format.wBitsPerSample / 8;
586 nrSamples = size / bpp;
588 if (!Stream_EnsureRemainingCapacity(context->common.buffer, nrSamples *
sizeof(int16_t)))
591 for (
size_t x = 0; x < nrSamples; x++)
593 Stream_Write_INT16(context->common.buffer, inSamples[x]);
594 if (Stream_GetPosition(context->common.buffer) / bpp >= context->faacInputSamples)
596 if (!Stream_EnsureRemainingCapacity(out, context->faacMaxOutputBytes))
598 rc = faacEncEncode(context->faac, Stream_BufferAs(context->common.buffer, int32_t),
599 context->faacInputSamples, Stream_Pointer(out),
600 Stream_GetRemainingCapacity(out));
604 Stream_Seek(out, (
size_t)rc);
605 Stream_ResetPosition(context->common.buffer);
613#if defined(WITH_OPUS)
614static BOOL freerdp_dsp_decode_opus(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
615 const BYTE* WINPR_RESTRICT src,
size_t size,
618 if (!context || !src || !out)
622 const size_t max_size = OPUS_MAX_FRAMES * context->common.format.nChannels *
sizeof(int16_t);
623 if (!Stream_EnsureRemainingCapacity(context->common.buffer, max_size))
626 const opus_int32 frames =
627 opus_decode(context->opus_decoder, src, WINPR_ASSERTING_INT_CAST(opus_int32, size),
628 Stream_Pointer(out), OPUS_MAX_FRAMES, 0);
632 Stream_Seek(out, (
size_t)frames * context->common.format.nChannels *
sizeof(int16_t));
637static BOOL freerdp_dsp_encode_opus(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
638 const BYTE* WINPR_RESTRICT src,
size_t size,
641 if (!context || !src || !out)
645 const size_t max_size = OPUS_MAX_FRAMES * context->common.format.nChannels *
sizeof(int16_t);
646 if (!Stream_EnsureRemainingCapacity(context->common.buffer, max_size))
649 const size_t src_frames = size /
sizeof(opus_int16) / context->common.format.nChannels;
650 const opus_int16* src_data = (
const opus_int16*)src;
651 const opus_int32 frames = opus_encode(
652 context->opus_encoder, src_data, WINPR_ASSERTING_INT_CAST(opus_int32, src_frames),
653 Stream_Pointer(out), WINPR_ASSERTING_INT_CAST(opus_int32, max_size));
656 return Stream_SafeSeek(out,
657 (
size_t)frames * context->common.format.nChannels *
sizeof(int16_t));
661#if defined(WITH_FAAD2)
662static BOOL freerdp_dsp_decode_faad(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
663 const BYTE* WINPR_RESTRICT src,
size_t size,
666 NeAACDecFrameInfo info;
669 if (!context || !src || !out)
672 if (!context->faadSetup)
679 unsigned long samplerate;
680 unsigned char channels;
683 err = NeAACDecInit(context->faad, cnv.pv, size,
684 &samplerate, &channels);
689 if (channels != context->common.format.nChannels)
692 if (samplerate != context->common.format.nSamplesPerSec)
695 context->faadSetup = TRUE;
698 while (offset < size)
707 outSize = context->common.format.nSamplesPerSec * context->common.format.nChannels *
708 context->common.format.wBitsPerSample / 8;
710 if (!Stream_EnsureRemainingCapacity(out, outSize))
713 sample_buffer = Stream_Pointer(out);
715 cnv.cpv = &src[offset];
716 NeAACDecDecode2(context->faad, &info, cnv.pv, size - offset, &sample_buffer,
717 Stream_GetRemainingCapacity(out));
722 offset += info.bytesconsumed;
724 if (info.samples == 0)
727 Stream_Seek(out, info.samples * context->common.format.wBitsPerSample / 8);
746} ima_stereo_encode_map[] = { { 0, 0 }, { 4, 0 }, { 0, 4 }, { 4, 4 }, { 1, 0 }, { 5, 0 },
747 { 1, 4 }, { 5, 4 }, { 2, 0 }, { 6, 0 }, { 2, 4 }, { 6, 4 },
748 { 3, 0 }, { 7, 0 }, { 3, 4 }, { 7, 4 } };
750static BYTE dsp_encode_ima_adpcm_sample(ADPCM* WINPR_RESTRICT adpcm,
size_t channel, INT16 sample)
752 INT32 ss = ima_step_size_table[adpcm->ima.last_step[channel]];
753 INT32 e = sample - adpcm->ima.last_sample[channel];
755 INT32 diff = ss >> 3;
791 diff += adpcm->ima.last_sample[channel];
795 else if (diff > 32767)
798 adpcm->ima.last_sample[channel] = (INT16)diff;
799 adpcm->ima.last_step[channel] = adpcm->ima.last_step[channel] + ima_step_index_table[enc];
801 if (adpcm->ima.last_step[channel] < 0)
802 adpcm->ima.last_step[channel] = 0;
803 else if (adpcm->ima.last_step[channel] > 88)
804 adpcm->ima.last_step[channel] = 88;
809static BOOL freerdp_dsp_encode_ima_adpcm(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
810 const BYTE* WINPR_RESTRICT src,
size_t size,
813 if (!Stream_EnsureRemainingCapacity(out, size))
815 if (!Stream_EnsureRemainingCapacity(context->common.buffer, size + 64))
818 const size_t align = (context->common.format.nChannels > 1) ? 32 : 4;
820 while (size >= align)
822 if (Stream_GetPosition(context->common.buffer) % context->common.format.nBlockAlign == 0)
824 Stream_Write_UINT8(context->common.buffer, context->adpcm.ima.last_sample[0] & 0xFF);
825 Stream_Write_UINT8(context->common.buffer,
826 (context->adpcm.ima.last_sample[0] >> 8) & 0xFF);
827 Stream_Write_UINT8(context->common.buffer, (BYTE)context->adpcm.ima.last_step[0]);
828 Stream_Write_UINT8(context->common.buffer, 0);
830 if (context->common.format.nChannels > 1)
832 Stream_Write_UINT8(context->common.buffer,
833 context->adpcm.ima.last_sample[1] & 0xFF);
834 Stream_Write_UINT8(context->common.buffer,
835 (context->adpcm.ima.last_sample[1] >> 8) & 0xFF);
836 Stream_Write_UINT8(context->common.buffer, (BYTE)context->adpcm.ima.last_step[1]);
837 Stream_Write_UINT8(context->common.buffer, 0);
841 if (context->common.format.nChannels > 1)
843 BYTE* dst = Stream_Pointer(context->common.buffer);
846 for (
size_t i = 0; i < 16; i++)
848 const INT16 sample = (INT16)(((UINT16)(*src)) | (((UINT16)(*(src + 1))) << 8));
850 const BYTE encoded = dsp_encode_ima_adpcm_sample(&context->adpcm, i % 2, sample);
851 dst[ima_stereo_encode_map[i].byte_num] |= encoded
852 << ima_stereo_encode_map[i].byte_shift;
855 if (!Stream_SafeSeek(context->common.buffer, 8))
861 INT16 sample = (INT16)(((UINT16)(*src)) | (((UINT16)(*(src + 1))) << 8));
863 BYTE encoded = dsp_encode_ima_adpcm_sample(&context->adpcm, 0, sample);
864 sample = (INT16)(((UINT16)(*src)) | (((UINT16)(*(src + 1))) << 8));
866 encoded |= dsp_encode_ima_adpcm_sample(&context->adpcm, 0, sample) << 4;
867 Stream_Write_UINT8(context->common.buffer, encoded);
871 if (Stream_GetPosition(context->common.buffer) >= context->adpcm.ima.packet_size)
873 BYTE* bsrc = Stream_Buffer(context->common.buffer);
874 Stream_Write(out, bsrc, context->adpcm.ima.packet_size);
875 Stream_ResetPosition(context->common.buffer);
888static const INT32 ms_adpcm_adaptation_table[] = { 230, 230, 230, 230, 307, 409, 512, 614,
889 768, 614, 512, 409, 307, 230, 230, 230 };
891static const INT32 ms_adpcm_coeffs1[7] = { 256, 512, 0, 192, 240, 460, 392 };
893static const INT32 ms_adpcm_coeffs2[7] = { 0, -256, 0, 64, 0, -208, -232 };
895static inline INT16 freerdp_dsp_decode_ms_adpcm_sample(ADPCM* WINPR_RESTRICT adpcm, BYTE sample,
898 const INT8 nibble = (INT8)((sample & 0x08) ? (sample - 16) : sample);
900 ((adpcm->ms.sample1[channel] * ms_adpcm_coeffs1[adpcm->ms.predictor[channel]]) +
901 (adpcm->ms.sample2[channel] * ms_adpcm_coeffs2[adpcm->ms.predictor[channel]])) /
903 presample += nibble * adpcm->ms.delta[channel];
905 if (presample > 32767)
907 else if (presample < -32768)
910 adpcm->ms.sample2[channel] = adpcm->ms.sample1[channel];
911 adpcm->ms.sample1[channel] = presample;
912 adpcm->ms.delta[channel] = adpcm->ms.delta[channel] * ms_adpcm_adaptation_table[sample] / 256;
914 if (adpcm->ms.delta[channel] < 16)
915 adpcm->ms.delta[channel] = 16;
917 return (INT16)presample;
920static BOOL freerdp_dsp_decode_ms_adpcm(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
921 const BYTE* WINPR_RESTRICT src,
size_t size,
924 const size_t out_size = size * 4;
925 const UINT32 channels = context->common.format.nChannels;
926 const UINT32 block_size = context->common.format.nBlockAlign;
928 if (!Stream_EnsureCapacity(out, out_size))
933 if (size % block_size == 0)
937 context->adpcm.ms.predictor[0] = *src++;
938 context->adpcm.ms.predictor[1] = *src++;
939 context->adpcm.ms.delta[0] = read_int16(src);
941 context->adpcm.ms.delta[1] = read_int16(src);
943 context->adpcm.ms.sample1[0] = read_int16(src);
945 context->adpcm.ms.sample1[1] = read_int16(src);
947 context->adpcm.ms.sample2[0] = read_int16(src);
949 context->adpcm.ms.sample2[1] = read_int16(src);
952 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample2[0]);
953 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample2[1]);
954 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample1[0]);
955 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample1[1]);
959 context->adpcm.ms.predictor[0] = *src++;
960 context->adpcm.ms.delta[0] = read_int16(src);
962 context->adpcm.ms.sample1[0] = read_int16(src);
964 context->adpcm.ms.sample2[0] = read_int16(src);
967 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample2[0]);
968 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample1[0]);
975 const BYTE sample = *src++;
978 out, freerdp_dsp_decode_ms_adpcm_sample(&context->adpcm, sample >> 4, 0));
980 out, freerdp_dsp_decode_ms_adpcm_sample(&context->adpcm, sample & 0x0F, 1));
983 const BYTE sample = *src++;
986 out, freerdp_dsp_decode_ms_adpcm_sample(&context->adpcm, sample >> 4, 0));
988 out, freerdp_dsp_decode_ms_adpcm_sample(&context->adpcm, sample & 0x0F, 1));
993 const BYTE sample = *src++;
995 Stream_Write_INT16(out,
996 freerdp_dsp_decode_ms_adpcm_sample(&context->adpcm, sample >> 4, 0));
998 out, freerdp_dsp_decode_ms_adpcm_sample(&context->adpcm, sample & 0x0F, 0));
1005static BYTE freerdp_dsp_encode_ms_adpcm_sample(ADPCM* WINPR_RESTRICT adpcm, INT32 sample,
1009 ((adpcm->ms.sample1[channel] * ms_adpcm_coeffs1[adpcm->ms.predictor[channel]]) +
1010 (adpcm->ms.sample2[channel] * ms_adpcm_coeffs2[adpcm->ms.predictor[channel]])) /
1012 INT32 errordelta = (sample - presample) / adpcm->ms.delta[channel];
1014 if ((sample - presample) % adpcm->ms.delta[channel] > adpcm->ms.delta[channel] / 2)
1019 else if (errordelta < -8)
1022 presample += adpcm->ms.delta[channel] * errordelta;
1024 if (presample > 32767)
1026 else if (presample < -32768)
1029 adpcm->ms.sample2[channel] = adpcm->ms.sample1[channel];
1030 adpcm->ms.sample1[channel] = presample;
1031 adpcm->ms.delta[channel] =
1032 adpcm->ms.delta[channel] * ms_adpcm_adaptation_table[(((BYTE)errordelta) & 0x0F)] / 256;
1034 if (adpcm->ms.delta[channel] < 16)
1035 adpcm->ms.delta[channel] = 16;
1037 return ((BYTE)errordelta) & 0x0F;
1040static BOOL freerdp_dsp_encode_ms_adpcm(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
1041 const BYTE* WINPR_RESTRICT src,
size_t size,
1044 const size_t step = 8 + ((context->common.format.nChannels > 1) ? 4 : 0);
1046 if (!Stream_EnsureRemainingCapacity(out, size))
1049 const size_t start = Stream_GetPosition(out);
1051 if (context->adpcm.ms.delta[0] < 16)
1052 context->adpcm.ms.delta[0] = 16;
1054 if (context->adpcm.ms.delta[1] < 16)
1055 context->adpcm.ms.delta[1] = 16;
1057 while (size >= step)
1059 if ((Stream_GetPosition(out) - start) % context->common.format.nBlockAlign == 0)
1061 if (context->common.format.nChannels > 1)
1063 Stream_Write_UINT8(out, context->adpcm.ms.predictor[0]);
1064 Stream_Write_UINT8(out, context->adpcm.ms.predictor[1]);
1065 Stream_Write_UINT8(out, (context->adpcm.ms.delta[0] & 0xFF));
1066 Stream_Write_UINT8(out, ((context->adpcm.ms.delta[0] >> 8) & 0xFF));
1067 Stream_Write_UINT8(out, (context->adpcm.ms.delta[1] & 0xFF));
1068 Stream_Write_UINT8(out, ((context->adpcm.ms.delta[1] >> 8) & 0xFF));
1070 context->adpcm.ms.sample1[0] = read_int16(src + 4);
1071 context->adpcm.ms.sample1[1] = read_int16(src + 6);
1072 context->adpcm.ms.sample2[0] = read_int16(src + 0);
1073 context->adpcm.ms.sample2[1] = read_int16(src + 2);
1075 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample1[0]);
1076 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample1[1]);
1077 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample2[0]);
1078 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample2[1]);
1085 Stream_Write_UINT8(out, context->adpcm.ms.predictor[0]);
1086 Stream_Write_UINT8(out, (BYTE)(context->adpcm.ms.delta[0] & 0xFF));
1087 Stream_Write_UINT8(out, (BYTE)((context->adpcm.ms.delta[0] >> 8) & 0xFF));
1089 context->adpcm.ms.sample1[0] = read_int16(src + 2);
1090 context->adpcm.ms.sample2[0] = read_int16(src + 0);
1092 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample1[0]);
1093 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample2[0]);
1100 const INT16 sample = read_int16(src);
1103 out, (freerdp_dsp_encode_ms_adpcm_sample(&context->adpcm, sample, 0) << 4) & 0xFF);
1106 const INT16 sample = read_int16(src);
1110 Stream_Read_UINT8(out, val);
1111 val += freerdp_dsp_encode_ms_adpcm_sample(&context->adpcm, sample,
1112 context->common.format.nChannels > 1 ? 1 : 0);
1113 Stream_Write_UINT8(out, val);
1123FREERDP_DSP_CONTEXT* freerdp_dsp_context_new(BOOL encoder)
1125#if defined(WITH_DSP_FFMPEG)
1126 return freerdp_dsp_ffmpeg_context_new(encoder);
1128 FREERDP_DSP_CONTEXT* context = calloc(1,
sizeof(FREERDP_DSP_CONTEXT));
1133 if (!freerdp_dsp_common_context_init(&context->common, encoder))
1136#if defined(WITH_GSM)
1137 context->gsm = gsm_create();
1145 rc = gsm_option(context->gsm, GSM_OPT_WAV49, &val);
1151#if defined(WITH_LAME)
1155 context->lame = lame_init();
1162 context->hip = hip_decode_init();
1169#if defined(WITH_FAAD2)
1173 context->faad = NeAACDecOpen();
1182 freerdp_dsp_context_free(context);
1187void freerdp_dsp_context_free(FREERDP_DSP_CONTEXT* context)
1192#if defined(WITH_FDK_AAC)
1195 fdk_aac_dsp_uninit(ctx);
1198#if defined(WITH_DSP_FFMPEG)
1199 freerdp_dsp_ffmpeg_context_free(context);
1202 freerdp_dsp_common_context_uninit(&context->common);
1204#if defined(WITH_GSM)
1205 gsm_destroy(context->gsm);
1207#if defined(WITH_LAME)
1209 if (context->common.encoder)
1210 lame_close(context->lame);
1212 hip_decode_exit(context->hip);
1215#if defined(WITH_OPUS)
1217 if (context->opus_decoder)
1218 opus_decoder_destroy(context->opus_decoder);
1219 if (context->opus_encoder)
1220 opus_encoder_destroy(context->opus_encoder);
1223#if defined(WITH_FAAD2)
1225 if (!context->common.encoder)
1226 NeAACDecClose(context->faad);
1229#if defined(WITH_FAAC)
1232 faacEncClose(context->faac);
1235#if defined(WITH_SOXR)
1236 soxr_delete(context->sox);
1243BOOL freerdp_dsp_encode(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
1245 const BYTE* WINPR_RESTRICT pdata,
size_t length,
1248#if defined(WITH_FDK_AAC)
1251 switch (ctx->format.wFormatTag)
1253 case WAVE_FORMAT_AAC_MS:
1254 return fdk_aac_dsp_encode(ctx, srcFormat, pdata, length, out);
1260#if defined(WITH_DSP_FFMPEG)
1261 return freerdp_dsp_ffmpeg_encode(context, srcFormat, pdata, length, out);
1263 if (!context || !context->common.encoder || !srcFormat || !pdata || !out)
1267 const BYTE* resampleData =
nullptr;
1268 size_t resampleLength = 0;
1270 if (!freerdp_dsp_channel_mix(context, pdata, length, srcFormat, &resampleData, &resampleLength))
1273 format.nChannels = context->common.format.nChannels;
1275 const BYTE* data =
nullptr;
1276 if (!freerdp_dsp_resample(context, resampleData, resampleLength, &format, &data, &length))
1279 switch (context->common.format.wFormatTag)
1281 case WAVE_FORMAT_PCM:
1282 if (!Stream_EnsureRemainingCapacity(out, length))
1285 Stream_Write(out, data, length);
1288 case WAVE_FORMAT_ADPCM:
1289 return freerdp_dsp_encode_ms_adpcm(context, data, length, out);
1291 case WAVE_FORMAT_DVI_ADPCM:
1292 return freerdp_dsp_encode_ima_adpcm(context, data, length, out);
1293#if defined(WITH_GSM)
1295 case WAVE_FORMAT_GSM610:
1296 return freerdp_dsp_encode_gsm610(context, data, length, out);
1298#if defined(WITH_LAME)
1300 case WAVE_FORMAT_MPEGLAYER3:
1301 return freerdp_dsp_encode_mp3(context, data, length, out);
1303#if defined(WITH_FAAC)
1305 case WAVE_FORMAT_AAC_MS:
1306 return freerdp_dsp_encode_faac(context, data, length, out);
1308#if defined(WITH_OPUS)
1310 case WAVE_FORMAT_OPUS:
1311 return freerdp_dsp_encode_opus(context, data, length, out);
1321BOOL freerdp_dsp_decode(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
1323 const BYTE* WINPR_RESTRICT data,
size_t length,
wStream* WINPR_RESTRICT out)
1325#if defined(WITH_FDK_AAC)
1328 switch (ctx->format.wFormatTag)
1330 case WAVE_FORMAT_AAC_MS:
1331 return fdk_aac_dsp_decode(ctx, srcFormat, data, length, out);
1337#if defined(WITH_DSP_FFMPEG)
1338 return freerdp_dsp_ffmpeg_decode(context, srcFormat, data, length, out);
1341 if (!context || context->common.encoder || !srcFormat || !data || !out)
1344 switch (context->common.format.wFormatTag)
1346 case WAVE_FORMAT_PCM:
1347 if (!Stream_EnsureRemainingCapacity(out, length))
1350 Stream_Write(out, data, length);
1353 case WAVE_FORMAT_ADPCM:
1354 return freerdp_dsp_decode_ms_adpcm(context, data, length, out);
1356 case WAVE_FORMAT_DVI_ADPCM:
1357 return freerdp_dsp_decode_ima_adpcm(context, data, length, out);
1358#if defined(WITH_GSM)
1360 case WAVE_FORMAT_GSM610:
1361 return freerdp_dsp_decode_gsm610(context, data, length, out);
1363#if defined(WITH_LAME)
1365 case WAVE_FORMAT_MPEGLAYER3:
1366 return freerdp_dsp_decode_mp3(context, data, length, out);
1368#if defined(WITH_FAAD2)
1370 case WAVE_FORMAT_AAC_MS:
1371 return freerdp_dsp_decode_faad(context, data, length, out);
1374#if defined(WITH_OPUS)
1375 case WAVE_FORMAT_OPUS:
1376 return freerdp_dsp_decode_opus(context, data, length, out);
1386BOOL freerdp_dsp_supports_format(
const AUDIO_FORMAT* WINPR_RESTRICT format, BOOL encode)
1388#if defined(WITH_FDK_AAC)
1389 switch (format->wFormatTag)
1391 case WAVE_FORMAT_AAC_MS:
1399#if defined(WITH_DSP_FFMPEG)
1400 return freerdp_dsp_ffmpeg_supports_format(format, encode);
1403#if !defined(WITH_DSP_EXPERIMENTAL)
1404 WINPR_UNUSED(encode);
1406 switch (format->wFormatTag)
1408 case WAVE_FORMAT_PCM:
1410#if defined(WITH_DSP_EXPERIMENTAL)
1412 case WAVE_FORMAT_ADPCM:
1414 case WAVE_FORMAT_DVI_ADPCM:
1417#if defined(WITH_GSM)
1419 case WAVE_FORMAT_GSM610:
1420#if defined(WITH_DSP_EXPERIMENTAL)
1426#if defined(WITH_LAME)
1428 case WAVE_FORMAT_MPEGLAYER3:
1429#if defined(WITH_DSP_EXPERIMENTAL)
1436 case WAVE_FORMAT_AAC_MS:
1437#if defined(WITH_FAAD2)
1442#if defined(WITH_FAAC)
1448#if defined(WITH_FDK_AAC)
1454#if defined(WITH_OPUS)
1455 case WAVE_FORMAT_OPUS:
1456 return opus_is_valid_samplerate(format);
1466BOOL freerdp_dsp_context_reset(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
1468 WINPR_ATTR_UNUSED UINT32 FramesPerPacket)
1470#if defined(WITH_FDK_AAC)
1471 WINPR_ASSERT(targetFormat);
1472 if (targetFormat->wFormatTag == WAVE_FORMAT_AAC_MS)
1475 fdk_aac_dsp_uninit(ctx);
1476 ctx->format = *targetFormat;
1477 return fdk_aac_dsp_init(ctx, FramesPerPacket);
1481#if defined(WITH_DSP_FFMPEG)
1482 return freerdp_dsp_ffmpeg_context_reset(context, targetFormat);
1485 if (!context || !targetFormat)
1488 context->common.format = *targetFormat;
1490 if (context->common.format.wFormatTag == WAVE_FORMAT_DVI_ADPCM)
1492 size_t min_frame_data = 1ull * context->common.format.wBitsPerSample *
1493 context->common.format.nChannels * FramesPerPacket;
1494 size_t data_per_block =
1495 (1ULL * context->common.format.nBlockAlign - 4ULL * context->common.format.nChannels) *
1497 size_t nb_block_per_packet = min_frame_data / data_per_block;
1499 if (min_frame_data % data_per_block)
1500 nb_block_per_packet++;
1502 context->adpcm.ima.packet_size = nb_block_per_packet * context->common.format.nBlockAlign;
1503 Stream_EnsureCapacity(context->common.buffer, context->adpcm.ima.packet_size);
1504 Stream_ResetPosition(context->common.buffer);
1507#if defined(WITH_OPUS)
1509 if (opus_is_valid_samplerate(&context->common.format))
1511 if (!context->common.encoder)
1513 int opus_error = OPUS_OK;
1515 context->opus_decoder = opus_decoder_create(
1516 WINPR_ASSERTING_INT_CAST(opus_int32, context->common.format.nSamplesPerSec),
1517 context->common.format.nChannels, &opus_error);
1518 if (opus_error != OPUS_OK)
1523 int opus_error = OPUS_OK;
1525 context->opus_encoder = opus_encoder_create(
1526 WINPR_ASSERTING_INT_CAST(opus_int32, context->common.format.nSamplesPerSec),
1527 context->common.format.nChannels, OPUS_APPLICATION_VOIP, &opus_error);
1528 if (opus_error != OPUS_OK)
1532 opus_encoder_ctl(context->opus_encoder,
1533 OPUS_SET_BITRATE(context->common.format.nAvgBytesPerSec * 8));
1534 if (opus_error != OPUS_OK)
1540#if defined(WITH_FAAD2)
1541 context->faadSetup = FALSE;
1543#if defined(WITH_FAAC)
1545 if (context->common.encoder)
1547 faacEncConfigurationPtr cfg;
1550 faacEncClose(context->faac);
1552 context->faac = faacEncOpen(targetFormat->nSamplesPerSec, targetFormat->nChannels,
1553 &context->faacInputSamples, &context->faacMaxOutputBytes);
1558 cfg = faacEncGetCurrentConfiguration(context->faac);
1559 cfg->inputFormat = FAAC_INPUT_16BIT;
1560 cfg->outputFormat = 0;
1561 cfg->mpegVersion = MPEG4;
1563 cfg->bandWidth = targetFormat->nAvgBytesPerSec;
1564 faacEncSetConfiguration(context->faac, cfg);
1568#if defined(WITH_SOXR)
1570 soxr_io_spec_t iospec = soxr_io_spec(SOXR_INT16, SOXR_INT16);
1572 soxr_delete(context->sox);
1574 soxr_create(context->common.format.nSamplesPerSec, targetFormat->nSamplesPerSec,
1575 targetFormat->nChannels, &error, &iospec,
nullptr,
nullptr);
1577 if (!context->sox || (error != 0))
1587 WINPR_ASSERT(context);
1588 context->encoder = encode;
1589 context->buffer = Stream_New(
nullptr, 1024);
1590 if (!context->buffer)
1593 context->channelmix = Stream_New(
nullptr, 1024);
1594 if (!context->channelmix)
1597 context->resample = Stream_New(
nullptr, 1024);
1598 if (!context->resample)
1604 freerdp_dsp_common_context_uninit(context);
1610 WINPR_ASSERT(context);
1612 Stream_Free(context->buffer, TRUE);
1613 Stream_Free(context->channelmix, TRUE);
1614 Stream_Free(context->resample, TRUE);
1616 context->buffer =
nullptr;
1617 context->channelmix =
nullptr;
1618 context->resample =
nullptr;