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
97 FREERDP_DSP_COMMON_CONTEXT common;
104#if defined(WITH_LAME)
108#if defined(WITH_OPUS)
109 OpusDecoder* opus_decoder;
110 OpusEncoder* opus_encoder;
112 size_t opus_packet_frames;
114#if defined(WITH_FAAD2)
119#if defined(WITH_FAAC)
121 unsigned long faacInputSamples;
122 unsigned long faacMaxOutputBytes;
125#if defined(WITH_SOXR)
130#if defined(WITH_OPUS)
131static BOOL opus_is_valid_samplerate(
const AUDIO_FORMAT* WINPR_RESTRICT format)
133 WINPR_ASSERT(format);
135 switch (format->nSamplesPerSec)
149static INT16 read_int16(
const BYTE* WINPR_RESTRICT src)
151 return (INT16)(src[0] | (src[1] << 8));
154static BOOL freerdp_dsp_channel_mix(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
155 const BYTE* WINPR_RESTRICT src,
size_t size,
157 const BYTE** WINPR_RESTRICT data,
size_t* WINPR_RESTRICT length)
159 if (!context || !data || !length)
162 if (srcFormat->wFormatTag != WAVE_FORMAT_PCM)
165 const UINT32 bpp = srcFormat->wBitsPerSample > 8 ? 2 : 1;
166 const size_t samples = size / bpp / srcFormat->nChannels;
168 if (context->common.format.nChannels == srcFormat->nChannels)
175 Stream_ResetPosition(context->common.channelmix);
178 if (context->common.format.nChannels > srcFormat->nChannels)
180 switch (srcFormat->nChannels)
183 if (!Stream_EnsureCapacity(context->common.channelmix, size * 2))
186 for (
size_t x = 0; x < samples; x++)
188 for (
size_t y = 0; y < bpp; y++)
189 Stream_Write_UINT8(context->common.channelmix, src[x * bpp + y]);
191 for (
size_t y = 0; y < bpp; y++)
192 Stream_Write_UINT8(context->common.channelmix, src[x * bpp + y]);
195 Stream_SealLength(context->common.channelmix);
196 *data = Stream_Buffer(context->common.channelmix);
197 *length = Stream_Length(context->common.channelmix);
207 switch (srcFormat->nChannels)
210 if (!Stream_EnsureCapacity(context->common.channelmix, size / 2))
215 for (
size_t x = 0; x < samples; x++)
217 for (
size_t y = 0; y < bpp; y++)
218 Stream_Write_UINT8(context->common.channelmix, src[2 * x * bpp + y]);
221 Stream_SealLength(context->common.channelmix);
222 *data = Stream_Buffer(context->common.channelmix);
223 *length = Stream_Length(context->common.channelmix);
239static BOOL freerdp_dsp_resample(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
240 const BYTE* WINPR_RESTRICT src,
size_t size,
242 const BYTE** WINPR_RESTRICT data,
size_t* WINPR_RESTRICT length)
246 if (srcFormat->wFormatTag != WAVE_FORMAT_PCM)
248 WLog_ERR(TAG,
"requires %s for sample input, got %s",
249 audio_format_get_tag_string(WAVE_FORMAT_PCM),
250 audio_format_get_tag_string(srcFormat->wFormatTag));
256 format.wFormatTag = WAVE_FORMAT_UNKNOWN;
257 format.wBitsPerSample = 0;
259 if (audio_format_compatible(&format, &context->common.format))
266#if defined(WITH_SOXR)
267 if (srcFormat->wBitsPerSample != 16)
269 WLog_ERR(TAG,
"requires 16 bit samples for input, got %" PRIu16, srcFormat->wBitsPerSample);
273 const size_t srcBytesPerFrame = (srcFormat->wBitsPerSample > 8) ? 2 : 1;
274 const size_t dstBytesPerFrame = (context->common.format.wBitsPerSample > 8) ? 2 : 1;
275 const size_t srcChannels = srcFormat->nChannels;
276 const size_t dstChannels = context->common.format.nChannels;
277 const size_t sbytes = srcChannels * srcBytesPerFrame;
278 const size_t sframes = size / sbytes;
279 const size_t rbytes = dstBytesPerFrame * dstChannels;
281 const size_t rframes = (sframes * context->common.format.nSamplesPerSec +
282 (context->common.format.nSamplesPerSec + 1) / 2) /
283 context->common.format.nSamplesPerSec;
284 const size_t rsize = rframes * rbytes;
286 if (!Stream_EnsureCapacity(context->common.resample, rsize))
292 soxr_process(context->sox, src, sframes, &idone, Stream_Buffer(context->common.resample),
293 Stream_Capacity(context->common.resample) / rbytes, &odone);
294 if (!Stream_SetLength(context->common.resample, odone * rbytes))
297 *data = Stream_Buffer(context->common.resample);
298 *length = Stream_Length(context->common.resample);
299 return (error ==
nullptr);
301 WLog_ERR(TAG,
"Missing resample support, recompile -DWITH_SOXR=ON or -DWITH_DSP_FFMPEG=ON");
313static const INT16 ima_step_index_table[] = {
314 -1, -1, -1, -1, 2, 4, 6, 8, -1, -1, -1, -1, 2, 4, 6, 8
317static const INT16 ima_step_size_table[] = {
318 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 19, 21, 23,
319 25, 28, 31, 34, 37, 41, 45, 50, 55, 60, 66, 73, 80,
320 88, 97, 107, 118, 130, 143, 157, 173, 190, 209, 230, 253, 279,
321 307, 337, 371, 408, 449, 494, 544, 598, 658, 724, 796, 876, 963,
322 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024, 3327,
323 3660, 4026, 4428, 4871, 5358, 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487,
324 12635, 13899, 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
327static inline void dsp_ima_clamp_step(ADPCM* WINPR_RESTRICT adpcm,
size_t channel)
330 if (adpcm->ima.last_step[channel] < 0)
331 adpcm->ima.last_step[channel] = 0;
333 const size_t size = ARRAYSIZE(ima_step_size_table);
334 if ((
size_t)adpcm->ima.last_step[channel] >= size)
335 adpcm->ima.last_step[channel] = (INT16)(size - 1);
338static UINT16 dsp_decode_ima_adpcm_sample(ADPCM* WINPR_RESTRICT adpcm,
unsigned int channel,
341 WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ima.last_step));
342 WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ima.last_sample));
344 const INT16 offset = adpcm->ima.last_step[channel];
345 WINPR_ASSERT(offset >= 0);
346 WINPR_ASSERT(WINPR_CXX_COMPAT_CAST(
size_t, offset) < ARRAYSIZE(ima_step_size_table));
348 const INT32 ss = ima_step_size_table[offset];
363 d += adpcm->ima.last_sample[channel];
370 adpcm->ima.last_sample[channel] = (INT16)d;
372 WINPR_ASSERT(sample < ARRAYSIZE(ima_step_index_table));
373 const int32_t last = adpcm->ima.last_step[channel] + ima_step_index_table[sample];
374 adpcm->ima.last_step[channel] = WINPR_ASSERTING_INT_CAST(int16_t, last);
376 dsp_ima_clamp_step(adpcm, channel);
381static BOOL valid_ima_adpcm_format(
const FREERDP_DSP_CONTEXT* WINPR_RESTRICT context)
383 WINPR_ASSERT(context);
384 if (context->common.format.wFormatTag != WAVE_FORMAT_DVI_ADPCM)
386 if (context->common.format.nBlockAlign <= 4ULL)
388 if (context->common.format.nChannels < 1)
390 if (context->common.format.wBitsPerSample == 0)
395static BOOL freerdp_dsp_decode_ima_adpcm(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
396 const BYTE* WINPR_RESTRICT src,
size_t size,
399 if (!valid_ima_adpcm_format(context))
402 size_t out_size = size * 4ull;
403 const UINT32 block_size = context->common.format.nBlockAlign;
404 const UINT32 channels = context->common.format.nChannels;
406 if (!Stream_EnsureCapacity(out, out_size))
411 if (size % block_size == 0)
416 context->adpcm.ima.last_sample[0] =
417 (INT16)(((UINT16)(*src)) | (((UINT16)(*(src + 1))) << 8));
418 context->adpcm.ima.last_step[0] = (INT16)(*(src + 2));
420 dsp_ima_clamp_step(&context->adpcm, 0u);
430 context->adpcm.ima.last_sample[1] =
431 (INT16)(((UINT16)(*src)) | (((UINT16)(*(src + 1))) << 8));
432 context->adpcm.ima.last_step[1] = (INT16)(*(src + 2));
434 dsp_ima_clamp_step(&context->adpcm, 1u);
445 for (
size_t i = 0; i < 8; i++)
447 BYTE* dst = Stream_Pointer(out);
449 const unsigned channel = (i < 4 ? 0 : 1);
451 const BYTE sample = ((*src) & 0x0f);
452 const UINT16 decoded =
453 dsp_decode_ima_adpcm_sample(&context->adpcm, channel, sample);
454 dst[((i & 3) << 3) + (channel << 1u)] = (decoded & 0xFF);
455 dst[((i & 3) << 3) + (channel << 1u) + 1] = (decoded >> 8);
458 const BYTE sample = ((*src) >> 4);
459 const UINT16 decoded =
460 dsp_decode_ima_adpcm_sample(&context->adpcm, channel, sample);
461 dst[((i & 3) << 3) + (channel << 1u) + 4] = (decoded & 0xFF);
462 dst[((i & 3) << 3) + (channel << 1u) + 5] = (decoded >> 8);
467 if (!Stream_SafeSeek(out, 32))
475 BYTE* dst = Stream_Pointer(out);
476 if (!Stream_SafeSeek(out, 4))
480 const BYTE sample = ((*src) & 0x0f);
481 const UINT16 decoded = dsp_decode_ima_adpcm_sample(&context->adpcm, 0, sample);
482 *dst++ = (decoded & 0xFF);
483 *dst++ = (decoded >> 8);
486 const BYTE sample = ((*src) >> 4);
487 const UINT16 decoded = dsp_decode_ima_adpcm_sample(&context->adpcm, 0, sample);
488 *dst++ = (decoded & 0xFF);
489 *dst++ = (decoded >> 8);
500#define GSM_FRAME_SAMPLES 160
502static BOOL freerdp_dsp_decode_gsm610(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
503 const BYTE* WINPR_RESTRICT src,
size_t size,
508 while (offset < size)
514 const BOOL large = gsm_option(context->gsm, GSM_OPT_FRAME_INDEX,
nullptr) == 0;
515 const size_t toadd = large ? 33 : 32;
516 if (size - offset < toadd)
519 gsm_signal gsmBlockBuffer[GSM_FRAME_SAMPLES] = WINPR_C_ARRAY_INIT;
521 gsm_decode(context->gsm,
522 WINPR_CAST_CONST_PTR_AWAY( &src[offset], gsm_byte*),
530 if (!Stream_EnsureRemainingCapacity(out,
sizeof(gsmBlockBuffer)))
533 Stream_Write(out, (
void*)gsmBlockBuffer,
sizeof(gsmBlockBuffer));
545static BOOL freerdp_dsp_encode_gsm610(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
546 const BYTE* WINPR_RESTRICT src,
size_t size,
549 wStream* input = context->common.buffer;
550 const size_t blockSize = 2 * GSM_FRAME_SAMPLES *
sizeof(gsm_signal);
551 const size_t blockBytes = 32 + 33;
553 if (!Stream_EnsureRemainingCapacity(input, size))
555 Stream_Write(input, src, size);
557 const size_t blocks = Stream_GetPosition(input) / blockSize;
558 if (!Stream_EnsureRemainingCapacity(out, blocks * blockBytes))
561 gsm_signal* signal = Stream_BufferAs(input, gsm_signal);
562 for (
size_t x = 0; x < blocks; x++)
564 gsm_encode(context->gsm, signal, Stream_Pointer(out));
565 Stream_Seek(out, 32);
566 signal += GSM_FRAME_SAMPLES;
568 gsm_encode(context->gsm, signal, Stream_Pointer(out));
569 Stream_Seek(out, 33);
570 signal += GSM_FRAME_SAMPLES;
573 const size_t used = blocks * blockSize;
574 const size_t left = Stream_GetPosition(input) - used;
575 memmove(Stream_Buffer(input), Stream_Buffer(input) + used, left);
576 return Stream_SetPosition(input, left);
580#if defined(WITH_LAME)
581static BOOL valid_mp3_format(
const FREERDP_DSP_CONTEXT* WINPR_RESTRICT context)
583 WINPR_ASSERT(context);
584 if (context->common.format.wFormatTag != WAVE_FORMAT_MPEGLAYER3)
586 if (context->common.format.nChannels < 1)
588 if (context->common.format.nChannels > 128)
590 if (context->common.format.wBitsPerSample == 0)
592 if (context->common.format.nSamplesPerSec == 0)
594 if (context->common.format.nSamplesPerSec > SIZE_MAX / 4ull / context->common.format.nChannels)
599static BOOL freerdp_dsp_decode_mp3(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
600 const BYTE* WINPR_RESTRICT src,
size_t size,
606 const size_t OUTSIZE_CLIPPED = (4096 *
sizeof(short));
608 if (!context || !src || !out)
610 if (!valid_mp3_format(context))
613 const size_t buffer_size = MAX(OUTSIZE_CLIPPED, 2ull * context->common.format.nChannels *
614 context->common.format.nSamplesPerSec);
616 if (!Stream_EnsureCapacity(context->common.buffer, 2ull * buffer_size))
619 mp3data_struct header = WINPR_C_ARRAY_INIT;
620 short* pcm_l = Stream_BufferAs(context->common.buffer,
short);
621 short* pcm_r = Stream_BufferAs(context->common.buffer,
short) + (buffer_size /
sizeof(short));
622 int rc = hip_decode1_headers(
624 WINPR_CAST_CONST_PTR_AWAY( src,
unsigned char*), size,
625 pcm_l, pcm_r, &header);
627 if ((rc <= 0) || (header.header_parsed != 1) || (header.samplerate <= 0) ||
628 (header.framesize <= 0))
631 if ((
size_t)header.samplerate != context->common.format.nSamplesPerSec)
634 if (context->common.format.nChannels != 2)
637 if (header.stereo != context->common.format.nChannels)
640 if ((
size_t)header.framesize > buffer_size /
sizeof(
short))
643 if (!Stream_EnsureRemainingCapacity(out, (
size_t)rc * context->common.format.nChannels * 2ull))
646 for (
int x = 0; x < rc; x++)
648 Stream_Write_UINT16(out, (UINT16)pcm_l[x]);
649 Stream_Write_UINT16(out, (UINT16)pcm_r[x]);
654 rc = hip_decode1(context->hip,
nullptr, 0, pcm_l, pcm_r);
655 if (!Stream_EnsureRemainingCapacity(out,
656 (
size_t)rc * context->common.format.nChannels * 2ull))
659 for (
int x = 0; x < rc; x++)
661 Stream_Write_UINT16(out, (UINT16)pcm_l[x]);
662 Stream_Write_UINT16(out, (UINT16)pcm_r[x]);
669static BOOL freerdp_dsp_encode_mp3(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
670 const BYTE* WINPR_RESTRICT src,
size_t size,
673 if (!context || !src || !out)
676 if (!valid_mp3_format(context))
679 size_t samples_per_channel =
680 size / context->common.format.nChannels / context->common.format.wBitsPerSample / 8;
683 if (!Stream_EnsureRemainingCapacity(out, 5ull / 4ull * samples_per_channel + 7200ull))
686 samples_per_channel = size / 2 / context->common.format.nChannels;
687 const size_t outLen = Stream_GetRemainingCapacity(out);
688 if ((outLen > INT_MAX) || (samples_per_channel > INT_MAX))
691 const int rc = lame_encode_buffer_interleaved(
693 WINPR_CAST_CONST_PTR_AWAY(WINPR_PACKED_ALIGN_CAST(
const short*, src),
short*),
694 WINPR_ASSERTING_INT_CAST(
int, samples_per_channel), Stream_Pointer(out),
695 WINPR_ASSERTING_INT_CAST(
int, outLen));
700 Stream_Seek(out, (
size_t)rc);
705#if defined(WITH_FAAC)
706static BOOL freerdp_dsp_encode_faac(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
707 const BYTE* WINPR_RESTRICT src,
size_t size,
710 const int16_t* inSamples = WINPR_PACKED_ALIGN_CAST(
const int16_t*, src);
712 if (!context || !src || !out)
715 if (context->common.format.wBitsPerSample != 16)
717 WLog_ERR(TAG,
"Encoder requires wBitsPerSample == 16");
722 const unsigned int bpp = context->common.format.wBitsPerSample / 8;
723 const size_t nrSamples = size / bpp;
725 if (!Stream_EnsureRemainingCapacity(context->common.buffer, nrSamples *
sizeof(int16_t)))
728 for (
size_t x = 0; x < nrSamples; x++)
730 Stream_Write_INT16(context->common.buffer, inSamples[x]);
731 if (Stream_GetPosition(context->common.buffer) / bpp >= context->faacInputSamples)
733 if (!Stream_EnsureRemainingCapacity(out, context->faacMaxOutputBytes))
736 const size_t outLen = Stream_GetRemainingCapacity(out);
737 if ((outLen > UINT_MAX) || (context->faacInputSamples > UINT_MAX))
740 faacEncEncode(context->faac, Stream_BufferAs(context->common.buffer, int32_t),
741 WINPR_ASSERTING_INT_CAST(
unsigned int, context->faacInputSamples),
742 Stream_Pointer(out), WINPR_ASSERTING_INT_CAST(
unsigned int, outLen));
746 Stream_Seek(out, (
size_t)rc);
747 Stream_ResetPosition(context->common.buffer);
755#if defined(WITH_OPUS)
756static BOOL freerdp_dsp_decode_opus(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
757 const BYTE* WINPR_RESTRICT src,
size_t size,
760 if (!context || !context->opus_decoder || !src || !out)
764 const size_t max_size = OPUS_MAX_FRAMES * context->common.format.nChannels *
sizeof(int16_t);
765 if (!Stream_EnsureRemainingCapacity(out, max_size))
768 const opus_int32 frames =
769 opus_decode(context->opus_decoder, src, WINPR_ASSERTING_INT_CAST(opus_int32, size),
770 Stream_Pointer(out), OPUS_MAX_FRAMES, 0);
774 Stream_Seek(out, (
size_t)frames * context->common.format.nChannels *
sizeof(int16_t));
780static const UINT32 opus_packet_units[] = { 24, 16, 8, 4, 2, 1 };
782static BOOL opus_is_packet_frames(UINT32 rate,
size_t frames)
784 for (
size_t x = 0; x < ARRAYSIZE(opus_packet_units); x++)
786 if (frames == 1ull * rate * opus_packet_units[x] / 400)
798static BOOL freerdp_dsp_encode_opus(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
799 const BYTE* WINPR_RESTRICT src,
size_t size,
802 if (!context || !context->opus_encoder || !context->opus_input || !src || !out)
805 wStream* input = context->opus_input;
806 const UINT32 rate = context->common.format.nSamplesPerSec;
807 const size_t frame_bytes =
sizeof(opus_int16) * context->common.format.nChannels;
808 if (!Stream_EnsureRemainingCapacity(input, size))
810 Stream_Write(input, src, size);
812 const size_t available = Stream_GetPosition(input) / frame_bytes;
813 if (available < context->opus_packet_frames)
817 for (
size_t x = 0; x < ARRAYSIZE(opus_packet_units); x++)
819 frames = 1ull * rate * opus_packet_units[x] / 400;
820 if (frames <= available)
825 const size_t max_size = OPUS_MAX_FRAMES * context->common.format.nChannels *
sizeof(int16_t);
826 if (!Stream_EnsureRemainingCapacity(out, max_size))
829 const opus_int16* src_data = WINPR_PACKED_ALIGN_CAST(
const opus_int16*, Stream_Buffer(input));
830 const opus_int32 bytes =
831 opus_encode(context->opus_encoder, src_data, WINPR_ASSERTING_INT_CAST(opus_int32, frames),
832 Stream_Pointer(out), WINPR_ASSERTING_INT_CAST(opus_int32, max_size));
835 Stream_Seek(out, (
size_t)bytes);
837 const size_t used = frames * frame_bytes;
838 const size_t left = Stream_GetPosition(input) - used;
839 memmove(Stream_Buffer(input), Stream_Buffer(input) + used, left);
840 return Stream_SetPosition(input, left);
844#if defined(WITH_FAAD2)
845static BOOL freerdp_dsp_decode_faad(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
846 const BYTE* WINPR_RESTRICT src,
size_t size,
849 NeAACDecFrameInfo info;
852 if (!context || !src || !out)
855 if (!context->faadSetup)
862 unsigned long samplerate = 0;
863 unsigned char channels = 0;
866 const long err = NeAACDecInit(context->faad, cnv.pv,
867 size, &samplerate, &channels);
872 if (channels != context->common.format.nChannels)
875 if (samplerate != context->common.format.nSamplesPerSec)
878 if (context->common.format.wBitsPerSample != 16)
880 WLog_ERR(TAG,
"Encoder requires wBitsPerSample == 16");
884 context->faadSetup = TRUE;
887 while (offset < size)
895 const size_t outSize = context->common.format.nSamplesPerSec *
896 context->common.format.nChannels *
897 context->common.format.wBitsPerSample / 8;
899 if (!Stream_EnsureRemainingCapacity(out, outSize))
902 void* sample_buffer = Stream_Pointer(out);
904 cnv.cpv = &src[offset];
905 NeAACDecDecode2(context->faad, &info, cnv.pv, size - offset, &sample_buffer,
906 Stream_GetRemainingCapacity(out));
911 offset += info.bytesconsumed;
913 if (info.samples == 0)
916 Stream_Seek(out, info.samples * context->common.format.wBitsPerSample / 8);
935} ima_stereo_encode_map[] = { { 0, 0 }, { 4, 0 }, { 0, 4 }, { 4, 4 }, { 1, 0 }, { 5, 0 },
936 { 1, 4 }, { 5, 4 }, { 2, 0 }, { 6, 0 }, { 2, 4 }, { 6, 4 },
937 { 3, 0 }, { 7, 0 }, { 3, 4 }, { 7, 4 } };
939static BYTE dsp_encode_ima_adpcm_sample(ADPCM* WINPR_RESTRICT adpcm,
size_t channel, INT16 sample)
941 WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ima.last_step));
942 WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ima.last_sample));
944 const INT16 offset = adpcm->ima.last_step[channel];
945 WINPR_ASSERT(offset >= 0);
946 WINPR_ASSERT(WINPR_CXX_COMPAT_CAST(
size_t, offset) < ARRAYSIZE(ima_step_size_table));
948 INT32 ss = ima_step_size_table[offset];
949 INT32 e = sample - adpcm->ima.last_sample[channel];
951 INT32 diff = ss >> 3;
987 diff += adpcm->ima.last_sample[channel];
991 else if (diff > 32767)
994 adpcm->ima.last_sample[channel] = (INT16)diff;
996 WINPR_ASSERT(enc < ARRAYSIZE(ima_step_index_table));
997 const int32_t last = adpcm->ima.last_step[channel] + ima_step_index_table[enc];
998 adpcm->ima.last_step[channel] = WINPR_ASSERTING_INT_CAST(int16_t, last);
1000 dsp_ima_clamp_step(adpcm, channel);
1005static BOOL freerdp_dsp_encode_ima_adpcm(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
1006 const BYTE* WINPR_RESTRICT src,
size_t size,
1009 if (!valid_ima_adpcm_format(context))
1011 if (!Stream_EnsureRemainingCapacity(out, size))
1013 if (!Stream_EnsureRemainingCapacity(context->common.buffer, size + 64ull))
1016 const size_t align = (context->common.format.nChannels > 1) ? 32 : 4;
1018 while (size >= align)
1020 if (Stream_GetPosition(context->common.buffer) % context->common.format.nBlockAlign == 0)
1022 Stream_Write_UINT8(context->common.buffer, context->adpcm.ima.last_sample[0] & 0xFF);
1023 Stream_Write_UINT8(context->common.buffer,
1024 (context->adpcm.ima.last_sample[0] >> 8) & 0xFF);
1025 Stream_Write_UINT8(context->common.buffer, (BYTE)context->adpcm.ima.last_step[0]);
1026 Stream_Write_UINT8(context->common.buffer, 0);
1028 if (context->common.format.nChannels > 1)
1030 Stream_Write_UINT8(context->common.buffer,
1031 context->adpcm.ima.last_sample[1] & 0xFF);
1032 Stream_Write_UINT8(context->common.buffer,
1033 (context->adpcm.ima.last_sample[1] >> 8) & 0xFF);
1034 Stream_Write_UINT8(context->common.buffer, (BYTE)context->adpcm.ima.last_step[1]);
1035 Stream_Write_UINT8(context->common.buffer, 0);
1039 if (context->common.format.nChannels > 1)
1041 BYTE* dst = Stream_Pointer(context->common.buffer);
1044 for (
size_t i = 0; i < 16; i++)
1046 const INT16 sample = (INT16)(((UINT16)(*src)) | (((UINT16)(*(src + 1))) << 8));
1048 const BYTE encoded = dsp_encode_ima_adpcm_sample(&context->adpcm, i % 2, sample);
1049 dst[ima_stereo_encode_map[i].byte_num] |= encoded
1050 << ima_stereo_encode_map[i].byte_shift;
1053 if (!Stream_SafeSeek(context->common.buffer, 8))
1059 INT16 sample = (INT16)(((UINT16)(*src)) | (((UINT16)(*(src + 1))) << 8));
1061 BYTE encoded = dsp_encode_ima_adpcm_sample(&context->adpcm, 0, sample);
1062 sample = (INT16)(((UINT16)(*src)) | (((UINT16)(*(src + 1))) << 8));
1064 encoded |= dsp_encode_ima_adpcm_sample(&context->adpcm, 0, sample) << 4;
1065 Stream_Write_UINT8(context->common.buffer, encoded);
1069 if (Stream_GetPosition(context->common.buffer) >= context->adpcm.ima.packet_size)
1071 const BYTE* bsrc = Stream_Buffer(context->common.buffer);
1072 if (!Stream_EnsureRemainingCapacity(context->common.buffer,
1073 context->adpcm.ima.packet_size))
1075 Stream_Write(out, bsrc, context->adpcm.ima.packet_size);
1076 Stream_ResetPosition(context->common.buffer);
1089static const INT32 ms_adpcm_adaptation_table[] = { 230, 230, 230, 230, 307, 409, 512, 614,
1090 768, 614, 512, 409, 307, 230, 230, 230 };
1092static const INT32 ms_adpcm_coeffs1[7] = { 256, 512, 0, 192, 240, 460, 392 };
1094static const INT32 ms_adpcm_coeffs2[7] = { 0, -256, 0, 64, 0, -208, -232 };
1096static inline INT32 saturated_multiply(INT32 a, INT32 b)
1098 WINPR_ASSERT(b > 0);
1099 if (a > INT32_MAX / b)
1101 if (a < INT32_MIN / b)
1106static inline INT16 freerdp_dsp_decode_ms_adpcm_sample(ADPCM* WINPR_RESTRICT adpcm, BYTE sample,
1109 WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ms.sample1));
1110 WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ms.sample2));
1111 WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ms.delta));
1112 WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ms.predictor));
1114 const INT8 nibble = (INT8)((sample & 0x08) ? (sample - 16) : sample);
1115 const BYTE predictor = adpcm->ms.predictor[channel];
1117 if (predictor < ARRAYSIZE(ms_adpcm_coeffs1))
1118 coeff1 = ms_adpcm_coeffs1[predictor];
1121 if (predictor < ARRAYSIZE(ms_adpcm_coeffs2))
1122 coeff2 = ms_adpcm_coeffs2[predictor];
1124 ((adpcm->ms.sample1[channel] * coeff1) + (adpcm->ms.sample2[channel] * coeff2)) / 256;
1125 presample += nibble * adpcm->ms.delta[channel];
1127 if (presample > 32767)
1129 else if (presample < -32768)
1132 adpcm->ms.sample2[channel] = adpcm->ms.sample1[channel];
1133 adpcm->ms.sample1[channel] = presample;
1136 if (sample < ARRAYSIZE(ms_adpcm_adaptation_table))
1137 tableval = ms_adpcm_adaptation_table[sample];
1139 adpcm->ms.delta[channel] = saturated_multiply(adpcm->ms.delta[channel], tableval) / 256;
1141 if (adpcm->ms.delta[channel] < 16)
1142 adpcm->ms.delta[channel] = 16;
1144 return (INT16)presample;
1147static BOOL valid_ms_adpcm_format(
const FREERDP_DSP_CONTEXT* WINPR_RESTRICT context)
1149 WINPR_ASSERT(context);
1150 if (context->common.format.wFormatTag != WAVE_FORMAT_ADPCM)
1152 if (context->common.format.nBlockAlign <= 4ULL)
1154 if (context->common.format.nChannels < 1)
1156 if (context->common.format.wBitsPerSample == 0)
1161static BOOL freerdp_dsp_decode_ms_adpcm(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
1162 const BYTE* WINPR_RESTRICT src,
size_t size,
1165 if (!valid_ms_adpcm_format(context))
1167 const size_t out_size = size * 4;
1168 const UINT32 channels = context->common.format.nChannels;
1169 const UINT32 block_size = context->common.format.nBlockAlign;
1171 if (!Stream_EnsureCapacity(out, out_size))
1176 if (size % block_size == 0)
1183 context->adpcm.ms.predictor[0] = *src++;
1184 context->adpcm.ms.predictor[1] = *src++;
1185 context->adpcm.ms.delta[0] = read_int16(src);
1187 context->adpcm.ms.delta[1] = read_int16(src);
1189 context->adpcm.ms.sample1[0] = read_int16(src);
1191 context->adpcm.ms.sample1[1] = read_int16(src);
1193 context->adpcm.ms.sample2[0] = read_int16(src);
1195 context->adpcm.ms.sample2[1] = read_int16(src);
1198 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample2[0]);
1199 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample2[1]);
1200 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample1[0]);
1201 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample1[1]);
1208 context->adpcm.ms.predictor[0] = *src++;
1209 context->adpcm.ms.delta[0] = read_int16(src);
1211 context->adpcm.ms.sample1[0] = read_int16(src);
1213 context->adpcm.ms.sample2[0] = read_int16(src);
1216 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample2[0]);
1217 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample1[0]);
1226 const BYTE sample = *src++;
1229 out, freerdp_dsp_decode_ms_adpcm_sample(&context->adpcm, sample >> 4, 0));
1231 out, freerdp_dsp_decode_ms_adpcm_sample(&context->adpcm, sample & 0x0F, 1));
1236 const BYTE sample = *src++;
1239 out, freerdp_dsp_decode_ms_adpcm_sample(&context->adpcm, sample >> 4, 0));
1241 out, freerdp_dsp_decode_ms_adpcm_sample(&context->adpcm, sample & 0x0F, 1));
1248 const BYTE sample = *src++;
1250 Stream_Write_INT16(out,
1251 freerdp_dsp_decode_ms_adpcm_sample(&context->adpcm, sample >> 4, 0));
1253 out, freerdp_dsp_decode_ms_adpcm_sample(&context->adpcm, sample & 0x0F, 0));
1260static BYTE freerdp_dsp_encode_ms_adpcm_sample(ADPCM* WINPR_RESTRICT adpcm, INT32 sample,
1263 WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ms.sample1));
1264 WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ms.sample2));
1265 WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ms.delta));
1266 WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ms.predictor));
1269 ((adpcm->ms.sample1[channel] * ms_adpcm_coeffs1[adpcm->ms.predictor[channel]]) +
1270 (adpcm->ms.sample2[channel] * ms_adpcm_coeffs2[adpcm->ms.predictor[channel]])) /
1272 INT32 errordelta = (sample - presample) / adpcm->ms.delta[channel];
1274 if ((sample - presample) % adpcm->ms.delta[channel] > adpcm->ms.delta[channel] / 2)
1279 else if (errordelta < -8)
1282 presample += adpcm->ms.delta[channel] * errordelta;
1284 if (presample > 32767)
1286 else if (presample < -32768)
1289 adpcm->ms.sample2[channel] = adpcm->ms.sample1[channel];
1290 adpcm->ms.sample1[channel] = presample;
1291 const size_t offset = (((BYTE)errordelta) & 0x0F);
1292 WINPR_ASSERT(offset < ARRAYSIZE(ms_adpcm_adaptation_table));
1293 adpcm->ms.delta[channel] = adpcm->ms.delta[channel] * ms_adpcm_adaptation_table[offset] / 256;
1295 if (adpcm->ms.delta[channel] < 16)
1296 adpcm->ms.delta[channel] = 16;
1298 return ((BYTE)errordelta) & 0x0F;
1301static BOOL freerdp_dsp_encode_ms_adpcm(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
1302 const BYTE* WINPR_RESTRICT src,
size_t size,
1305 if (!valid_ms_adpcm_format(context))
1308 const size_t step = 8 + ((context->common.format.nChannels > 1) ? 4 : 0);
1310 if (!Stream_EnsureRemainingCapacity(out, size))
1313 const size_t start = Stream_GetPosition(out);
1315 if (context->adpcm.ms.delta[0] < 16)
1316 context->adpcm.ms.delta[0] = 16;
1318 if (context->adpcm.ms.delta[1] < 16)
1319 context->adpcm.ms.delta[1] = 16;
1321 while (size >= step)
1323 if ((Stream_GetPosition(out) - start) % context->common.format.nBlockAlign == 0)
1325 if (context->common.format.nChannels > 1)
1327 Stream_Write_UINT8(out, context->adpcm.ms.predictor[0]);
1328 Stream_Write_UINT8(out, context->adpcm.ms.predictor[1]);
1329 Stream_Write_UINT8(out, (context->adpcm.ms.delta[0] & 0xFF));
1330 Stream_Write_UINT8(out, ((context->adpcm.ms.delta[0] >> 8) & 0xFF));
1331 Stream_Write_UINT8(out, (context->adpcm.ms.delta[1] & 0xFF));
1332 Stream_Write_UINT8(out, ((context->adpcm.ms.delta[1] >> 8) & 0xFF));
1334 context->adpcm.ms.sample1[0] = read_int16(src + 4);
1335 context->adpcm.ms.sample1[1] = read_int16(src + 6);
1336 context->adpcm.ms.sample2[0] = read_int16(src + 0);
1337 context->adpcm.ms.sample2[1] = read_int16(src + 2);
1339 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample1[0]);
1340 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample1[1]);
1341 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample2[0]);
1342 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample2[1]);
1349 Stream_Write_UINT8(out, context->adpcm.ms.predictor[0]);
1350 Stream_Write_UINT8(out, (BYTE)(context->adpcm.ms.delta[0] & 0xFF));
1351 Stream_Write_UINT8(out, (BYTE)((context->adpcm.ms.delta[0] >> 8) & 0xFF));
1353 context->adpcm.ms.sample1[0] = read_int16(src + 2);
1354 context->adpcm.ms.sample2[0] = read_int16(src + 0);
1356 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample1[0]);
1357 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample2[0]);
1364 const INT16 sample = read_int16(src);
1367 out, (freerdp_dsp_encode_ms_adpcm_sample(&context->adpcm, sample, 0) << 4) & 0xFF);
1370 const INT16 sample = read_int16(src);
1374 Stream_Read_UINT8(out, val);
1375 val += freerdp_dsp_encode_ms_adpcm_sample(&context->adpcm, sample,
1376 context->common.format.nChannels > 1 ? 1 : 0);
1377 Stream_Write_UINT8(out, val);
1387FREERDP_DSP_CONTEXT* freerdp_dsp_context_new(BOOL encoder)
1389#if defined(WITH_DSP_FFMPEG)
1390 return freerdp_dsp_ffmpeg_context_new(encoder);
1392 FREERDP_DSP_CONTEXT* context = calloc(1,
sizeof(FREERDP_DSP_CONTEXT));
1397 if (!freerdp_dsp_common_context_init(&context->common, encoder))
1400#if defined(WITH_GSM)
1401 context->gsm = gsm_create();
1408 int rc = gsm_option(context->gsm, GSM_OPT_WAV49, &val);
1414#if defined(WITH_LAME)
1418 context->lame = lame_init();
1425 context->hip = hip_decode_init();
1432#if defined(WITH_FAAD2)
1436 context->faad = NeAACDecOpen();
1445 freerdp_dsp_context_free(context);
1450void freerdp_dsp_context_free(FREERDP_DSP_CONTEXT* context)
1455#if defined(WITH_FDK_AAC)
1458 fdk_aac_dsp_uninit(ctx);
1461#if defined(WITH_MEDIACODEC)
1465#if defined(WITH_DSP_FFMPEG)
1466 freerdp_dsp_ffmpeg_context_free(context);
1469 freerdp_dsp_common_context_uninit(&context->common);
1471#if defined(WITH_GSM)
1472 gsm_destroy(context->gsm);
1474#if defined(WITH_LAME)
1476 if (context->common.encoder)
1477 lame_close(context->lame);
1479 hip_decode_exit(context->hip);
1482#if defined(WITH_OPUS)
1484 if (context->opus_decoder)
1485 opus_decoder_destroy(context->opus_decoder);
1486 if (context->opus_encoder)
1487 opus_encoder_destroy(context->opus_encoder);
1488 Stream_Free(context->opus_input, TRUE);
1491#if defined(WITH_FAAD2)
1493 if (!context->common.encoder)
1494 NeAACDecClose(context->faad);
1497#if defined(WITH_FAAC)
1500 faacEncClose(context->faac);
1503#if defined(WITH_SOXR)
1504 soxr_delete(context->sox);
1511BOOL freerdp_dsp_encode(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
1513 const BYTE* WINPR_RESTRICT pdata,
size_t length,
1516#if defined(WITH_MEDIACODEC)
1518 if (mcctx && (mcctx->format.wFormatTag == WAVE_FORMAT_AAC_MS))
1519 return mediacodec_aac_dsp_encode(mcctx, srcFormat, pdata, length, out);
1522#if defined(WITH_FDK_AAC)
1525 switch (ctx->format.wFormatTag)
1527 case WAVE_FORMAT_AAC_MS:
1528 return fdk_aac_dsp_encode(ctx, srcFormat, pdata, length, out);
1534#if defined(WITH_DSP_FFMPEG)
1535 return freerdp_dsp_ffmpeg_encode(context, srcFormat, pdata, length, out);
1537 if (!context || !context->common.encoder || !srcFormat || !pdata || !out)
1541 const BYTE* resampleData =
nullptr;
1542 size_t resampleLength = 0;
1544 if (!freerdp_dsp_channel_mix(context, pdata, length, srcFormat, &resampleData, &resampleLength))
1547 format.nChannels = context->common.format.nChannels;
1549 const BYTE* data =
nullptr;
1550 if (!freerdp_dsp_resample(context, resampleData, resampleLength, &format, &data, &length))
1553 switch (context->common.format.wFormatTag)
1555 case WAVE_FORMAT_PCM:
1556 if (!Stream_EnsureRemainingCapacity(out, length))
1559 Stream_Write(out, data, length);
1562 case WAVE_FORMAT_ADPCM:
1563 return freerdp_dsp_encode_ms_adpcm(context, data, length, out);
1565 case WAVE_FORMAT_DVI_ADPCM:
1566 return freerdp_dsp_encode_ima_adpcm(context, data, length, out);
1567#if defined(WITH_GSM)
1569 case WAVE_FORMAT_GSM610:
1570 return freerdp_dsp_encode_gsm610(context, data, length, out);
1572#if defined(WITH_LAME)
1574 case WAVE_FORMAT_MPEGLAYER3:
1575 return freerdp_dsp_encode_mp3(context, data, length, out);
1577#if defined(WITH_FAAC)
1579 case WAVE_FORMAT_AAC_MS:
1580 return freerdp_dsp_encode_faac(context, data, length, out);
1582#if defined(WITH_OPUS)
1584 case WAVE_FORMAT_OPUS:
1585 return freerdp_dsp_encode_opus(context, data, length, out);
1595BOOL freerdp_dsp_decode(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
1597 const BYTE* WINPR_RESTRICT data,
size_t length,
wStream* WINPR_RESTRICT out)
1599#if defined(WITH_MEDIACODEC)
1601 if (mcctx && (mcctx->format.wFormatTag == WAVE_FORMAT_AAC_MS))
1602 return mediacodec_aac_dsp_decode(mcctx, srcFormat, data, length, out);
1605#if defined(WITH_FDK_AAC)
1608 switch (ctx->format.wFormatTag)
1610 case WAVE_FORMAT_AAC_MS:
1611 return fdk_aac_dsp_decode(ctx, srcFormat, data, length, out);
1617#if defined(WITH_DSP_FFMPEG)
1618 return freerdp_dsp_ffmpeg_decode(context, srcFormat, data, length, out);
1621 if (!context || context->common.encoder || !srcFormat || !data || !out)
1624 switch (context->common.format.wFormatTag)
1626 case WAVE_FORMAT_PCM:
1627 if (!Stream_EnsureRemainingCapacity(out, length))
1630 Stream_Write(out, data, length);
1633 case WAVE_FORMAT_ADPCM:
1634 return freerdp_dsp_decode_ms_adpcm(context, data, length, out);
1636 case WAVE_FORMAT_DVI_ADPCM:
1637 return freerdp_dsp_decode_ima_adpcm(context, data, length, out);
1638#if defined(WITH_GSM)
1640 case WAVE_FORMAT_GSM610:
1641 return freerdp_dsp_decode_gsm610(context, data, length, out);
1643#if defined(WITH_LAME)
1645 case WAVE_FORMAT_MPEGLAYER3:
1646 return freerdp_dsp_decode_mp3(context, data, length, out);
1648#if defined(WITH_FAAD2)
1650 case WAVE_FORMAT_AAC_MS:
1651 return freerdp_dsp_decode_faad(context, data, length, out);
1654#if defined(WITH_OPUS)
1655 case WAVE_FORMAT_OPUS:
1656 return freerdp_dsp_decode_opus(context, data, length, out);
1666BOOL freerdp_dsp_supports_format(
const AUDIO_FORMAT* WINPR_RESTRICT format, BOOL encode)
1668 WINPR_ASSERT(format);
1669 if (!audio_format_is_valid(format))
1672#if defined(WITH_MEDIACODEC)
1673 if (format && (format->wFormatTag == WAVE_FORMAT_AAC_MS))
1674 return mediacodec_aac_dsp_supports_format(format, encode);
1677#if defined(WITH_FDK_AAC)
1678 switch (format->wFormatTag)
1680 case WAVE_FORMAT_AAC_MS:
1688#if defined(WITH_DSP_FFMPEG)
1689 return freerdp_dsp_ffmpeg_supports_format(format, encode);
1692#if !defined(WITH_DSP_EXPERIMENTAL)
1693 WINPR_UNUSED(encode);
1695 switch (format->wFormatTag)
1697 case WAVE_FORMAT_PCM:
1699#if defined(WITH_DSP_EXPERIMENTAL)
1701 case WAVE_FORMAT_ADPCM:
1703 case WAVE_FORMAT_DVI_ADPCM:
1706#if defined(WITH_GSM)
1708 case WAVE_FORMAT_GSM610:
1709#if defined(WITH_DSP_EXPERIMENTAL)
1715#if defined(WITH_LAME)
1717 case WAVE_FORMAT_MPEGLAYER3:
1718#if defined(WITH_DSP_EXPERIMENTAL)
1725 case WAVE_FORMAT_AAC_MS:
1726#if defined(WITH_FAAD2)
1731#if defined(WITH_FAAC)
1737#if defined(WITH_FDK_AAC)
1743#if defined(WITH_OPUS)
1744 case WAVE_FORMAT_OPUS:
1745 return opus_is_valid_samplerate(format);
1755BOOL freerdp_dsp_context_reset(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
1757 WINPR_ATTR_UNUSED UINT32 FramesPerPacket)
1759 if (!context || !targetFormat)
1761 if (!audio_format_is_valid(targetFormat))
1764 if (targetFormat->wFormatTag == WAVE_FORMAT_AAC_MS)
1766#if defined(WITH_MEDIACODEC)
1768 mediacodec_aac_dsp_uninit(ctx);
1769 ctx->format = *targetFormat;
1770 return mediacodec_aac_dsp_init(ctx, FramesPerPacket);
1773#if defined(WITH_FDK_AAC)
1775 fdk_aac_dsp_uninit(ctx);
1776 ctx->format = *targetFormat;
1777 return fdk_aac_dsp_init(ctx, FramesPerPacket);
1781#if defined(WITH_DSP_FFMPEG)
1782 return freerdp_dsp_ffmpeg_context_reset(context, targetFormat);
1784 ZeroMemory(&context->adpcm,
sizeof(context->adpcm));
1785 context->common.format = *targetFormat;
1787 switch (context->common.format.wFormatTag)
1789#if defined(WITH_LAME)
1790 case WAVE_FORMAT_MPEGLAYER3:
1791 if (!valid_mp3_format(context))
1795 case WAVE_FORMAT_ADPCM:
1796 if (!valid_ms_adpcm_format(context))
1799 case WAVE_FORMAT_DVI_ADPCM:
1801 if (!valid_ima_adpcm_format(context))
1803 if (FramesPerPacket == 0)
1806 const size_t min_frame_data = 1ull * context->common.format.wBitsPerSample *
1807 context->common.format.nChannels * FramesPerPacket;
1808 const size_t data_per_block = (1ULL * context->common.format.nBlockAlign -
1809 4ULL * context->common.format.nChannels) *
1811 size_t nb_block_per_packet = min_frame_data / data_per_block;
1813 if (min_frame_data % data_per_block)
1814 nb_block_per_packet++;
1816 context->adpcm.ima.packet_size =
1817 nb_block_per_packet * context->common.format.nBlockAlign;
1818 if (!Stream_EnsureCapacity(context->common.buffer, context->adpcm.ima.packet_size))
1820 Stream_ResetPosition(context->common.buffer);
1823#if defined(WITH_GSM)
1824 case WAVE_FORMAT_GSM610:
1828 if (gsm_option(context->gsm, GSM_OPT_FRAME_INDEX, &index) < 0)
1830 Stream_ResetPosition(context->common.buffer);
1838#if defined(WITH_OPUS)
1840 if (context->opus_decoder)
1841 opus_decoder_destroy(context->opus_decoder);
1842 context->opus_decoder =
nullptr;
1843 if (context->opus_encoder)
1844 opus_encoder_destroy(context->opus_encoder);
1845 context->opus_encoder =
nullptr;
1847 if (context->common.format.wFormatTag == WAVE_FORMAT_OPUS)
1849 if (!opus_is_valid_samplerate(&context->common.format))
1852 if (!context->common.encoder)
1854 int opus_error = OPUS_OK;
1856 context->opus_decoder = opus_decoder_create(
1857 WINPR_ASSERTING_INT_CAST(opus_int32, context->common.format.nSamplesPerSec),
1858 context->common.format.nChannels, &opus_error);
1859 if (opus_error != OPUS_OK)
1864 int opus_error = OPUS_OK;
1866 context->opus_encoder = opus_encoder_create(
1867 WINPR_ASSERTING_INT_CAST(opus_int32, context->common.format.nSamplesPerSec),
1868 context->common.format.nChannels, OPUS_APPLICATION_VOIP, &opus_error);
1869 if (opus_error != OPUS_OK)
1873 opus_encoder_ctl(context->opus_encoder,
1874 OPUS_SET_BITRATE(context->common.format.nAvgBytesPerSec * 8));
1875 if (opus_error != OPUS_OK)
1879 const UINT32 rate = context->common.format.nSamplesPerSec;
1880 context->opus_packet_frames =
1881 opus_is_packet_frames(rate, FramesPerPacket) ? FramesPerPacket : rate / 50;
1882 if (!context->opus_input)
1883 context->opus_input = Stream_New(
nullptr, OPUS_MAX_FRAMES *
sizeof(opus_int16) *
1884 context->common.format.nChannels);
1885 if (!context->opus_input)
1887 Stream_ResetPosition(context->opus_input);
1892#if defined(WITH_FAAD2)
1893 context->faadSetup = FALSE;
1895#if defined(WITH_FAAC)
1897 if (context->common.encoder)
1899 faacEncConfigurationPtr cfg =
nullptr;
1901 if (targetFormat->wBitsPerSample != 16)
1905 faacEncClose(context->faac);
1907 context->faac = faacEncOpen(targetFormat->nSamplesPerSec, targetFormat->nChannels,
1908 &context->faacInputSamples, &context->faacMaxOutputBytes);
1913 cfg = faacEncGetCurrentConfiguration(context->faac);
1914 cfg->inputFormat = FAAC_INPUT_16BIT;
1915 cfg->outputFormat = 0;
1916 cfg->mpegVersion = MPEG4;
1918 cfg->bandWidth = targetFormat->nAvgBytesPerSec;
1919 const int rc = faacEncSetConfiguration(context->faac, cfg);
1925#if defined(WITH_SOXR)
1927 soxr_io_spec_t iospec = soxr_io_spec(SOXR_INT16, SOXR_INT16);
1928 soxr_error_t error =
nullptr;
1930 if (context->common.format.wBitsPerSample != 16)
1933 soxr_delete(context->sox);
1935 soxr_create(context->common.format.nSamplesPerSec, targetFormat->nSamplesPerSec,
1936 targetFormat->nChannels, &error, &iospec,
nullptr,
nullptr);
1938 if (!context->sox || (error !=
nullptr))
1948 WINPR_ASSERT(context);
1949 context->encoder = encode;
1950 context->buffer = Stream_New(
nullptr, 1024);
1951 if (!context->buffer)
1954 context->channelmix = Stream_New(
nullptr, 1024);
1955 if (!context->channelmix)
1958 context->resample = Stream_New(
nullptr, 1024);
1959 if (!context->resample)
1965 freerdp_dsp_common_context_uninit(context);
1971 WINPR_ASSERT(context);
1973 Stream_Free(context->buffer, TRUE);
1974 Stream_Free(context->channelmix, TRUE);
1975 Stream_Free(context->resample, TRUE);
1977 context->buffer =
nullptr;
1978 context->channelmix =
nullptr;
1979 context->resample =
nullptr;