FreeRDP
Loading...
Searching...
No Matches
dsp.c
1
20#include <freerdp/config.h>
21
22#include <winpr/assert.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
26
27#include <winpr/crt.h>
28
29#include <freerdp/types.h>
30#include <freerdp/log.h>
31#include <freerdp/codec/dsp.h>
32
33#include "dsp.h"
34
35#if defined(WITH_FDK_AAC)
36#include "dsp_fdk_aac.h"
37#endif
38
39#if defined(WITH_MEDIACODEC)
40#include "dsp_mediacodec.h"
41#endif
42
43#if !defined(WITH_DSP_FFMPEG)
44#if defined(WITH_GSM)
45#include <gsm/gsm.h>
46#endif
47
48#if defined(WITH_LAME)
49#include <lame/lame.h>
50#endif
51
52#if defined(WITH_OPUS)
53#include <opus/opus.h>
54
55#define OPUS_MAX_FRAMES 5760ull
56#endif
57
58#if defined(WITH_FAAD2)
59#include <neaacdec.h>
60#endif
61
62#if defined(WITH_FAAC)
63#include <faac.h>
64#endif
65
66#if defined(WITH_SOXR)
67#include <soxr.h>
68#endif
69
70#else
71#include "dsp_ffmpeg.h"
72#endif
73
74#if !defined(WITH_DSP_FFMPEG)
75
76#define TAG FREERDP_TAG("dsp")
77
78typedef union
79{
80 struct
81 {
82 size_t packet_size;
83 INT16 last_sample[2];
84 INT16 last_step[2];
85 } ima;
86 struct
87 {
88 BYTE predictor[2];
89 INT32 delta[2];
90 INT32 sample1[2];
91 INT32 sample2[2];
92 } ms;
93} ADPCM;
94
95struct S_FREERDP_DSP_CONTEXT
96{
98
99 ADPCM adpcm;
100
101#if defined(WITH_GSM)
102 gsm gsm;
103#endif
104#if defined(WITH_LAME)
105 lame_t lame;
106 hip_t hip;
107#endif
108#if defined(WITH_OPUS)
109 OpusDecoder* opus_decoder;
110 OpusEncoder* opus_encoder;
111 wStream* opus_input; /* samples not encoded yet */
112 size_t opus_packet_frames; /* the shortest packet to encode */
113#endif
114#if defined(WITH_FAAD2)
115 NeAACDecHandle faad;
116 BOOL faadSetup;
117#endif
118
119#if defined(WITH_FAAC)
120 faacEncHandle faac;
121 unsigned long faacInputSamples;
122 unsigned long faacMaxOutputBytes;
123#endif
124
125#if defined(WITH_SOXR)
126 soxr_t sox;
127#endif
128};
129
130#if defined(WITH_OPUS)
131static BOOL opus_is_valid_samplerate(const AUDIO_FORMAT* WINPR_RESTRICT format)
132{
133 WINPR_ASSERT(format);
134
135 switch (format->nSamplesPerSec)
136 {
137 case 8000:
138 case 12000:
139 case 16000:
140 case 24000:
141 case 48000:
142 return TRUE;
143 default:
144 return FALSE;
145 }
146}
147#endif
148
149static INT16 read_int16(const BYTE* WINPR_RESTRICT src)
150{
151 return (INT16)(src[0] | (src[1] << 8));
152}
153
154static BOOL freerdp_dsp_channel_mix(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
155 const BYTE* WINPR_RESTRICT src, size_t size,
156 const AUDIO_FORMAT* WINPR_RESTRICT srcFormat,
157 const BYTE** WINPR_RESTRICT data, size_t* WINPR_RESTRICT length)
158{
159 if (!context || !data || !length)
160 return FALSE;
161
162 if (srcFormat->wFormatTag != WAVE_FORMAT_PCM)
163 return FALSE;
164
165 const UINT32 bpp = srcFormat->wBitsPerSample > 8 ? 2 : 1;
166 const size_t samples = size / bpp / srcFormat->nChannels;
167
168 if (context->common.format.nChannels == srcFormat->nChannels)
169 {
170 *data = src;
171 *length = size;
172 return TRUE;
173 }
174
175 Stream_ResetPosition(context->common.channelmix);
176
177 /* Destination has more channels than source */
178 if (context->common.format.nChannels > srcFormat->nChannels)
179 {
180 switch (srcFormat->nChannels)
181 {
182 case 1:
183 if (!Stream_EnsureCapacity(context->common.channelmix, size * 2))
184 return FALSE;
185
186 for (size_t x = 0; x < samples; x++)
187 {
188 for (size_t y = 0; y < bpp; y++)
189 Stream_Write_UINT8(context->common.channelmix, src[x * bpp + y]);
190
191 for (size_t y = 0; y < bpp; y++)
192 Stream_Write_UINT8(context->common.channelmix, src[x * bpp + y]);
193 }
194
195 Stream_SealLength(context->common.channelmix);
196 *data = Stream_Buffer(context->common.channelmix);
197 *length = Stream_Length(context->common.channelmix);
198 return TRUE;
199
200 case 2: /* We only support stereo, so we can not handle this case. */
201 default: /* Unsupported number of channels */
202 return FALSE;
203 }
204 }
205
206 /* Destination has less channels than source */
207 switch (srcFormat->nChannels)
208 {
209 case 2:
210 if (!Stream_EnsureCapacity(context->common.channelmix, size / 2))
211 return FALSE;
212
213 /* Simply drop second channel.
214 * TODO: Calculate average */
215 for (size_t x = 0; x < samples; x++)
216 {
217 for (size_t y = 0; y < bpp; y++)
218 Stream_Write_UINT8(context->common.channelmix, src[2 * x * bpp + y]);
219 }
220
221 Stream_SealLength(context->common.channelmix);
222 *data = Stream_Buffer(context->common.channelmix);
223 *length = Stream_Length(context->common.channelmix);
224 return TRUE;
225
226 case 1: /* Invalid, do we want to use a 0 channel sound? */
227 default: /* Unsupported number of channels */
228 return FALSE;
229 }
230
231 return FALSE;
232}
233
239static BOOL freerdp_dsp_resample(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
240 const BYTE* WINPR_RESTRICT src, size_t size,
241 const AUDIO_FORMAT* WINPR_RESTRICT srcFormat,
242 const BYTE** WINPR_RESTRICT data, size_t* WINPR_RESTRICT length)
243{
244 AUDIO_FORMAT format;
245
246 if (srcFormat->wFormatTag != WAVE_FORMAT_PCM)
247 {
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));
251 return FALSE;
252 }
253
254 /* We want to ignore differences of source and destination format. */
255 format = *srcFormat;
256 format.wFormatTag = WAVE_FORMAT_UNKNOWN;
257 format.wBitsPerSample = 0;
258
259 if (audio_format_compatible(&format, &context->common.format))
260 {
261 *data = src;
262 *length = size;
263 return TRUE;
264 }
265
266#if defined(WITH_SOXR)
267 const size_t srcBytesPerFrame = (srcFormat->wBitsPerSample > 8) ? 2 : 1;
268 const size_t dstBytesPerFrame = (context->common.format.wBitsPerSample > 8) ? 2 : 1;
269 const size_t srcChannels = srcFormat->nChannels;
270 const size_t dstChannels = context->common.format.nChannels;
271 const size_t sbytes = srcChannels * srcBytesPerFrame;
272 const size_t sframes = size / sbytes;
273 const size_t rbytes = dstBytesPerFrame * dstChannels;
274 /* Integer rounding correct division */
275 const size_t rframes =
276 (sframes * context->common.format.nSamplesPerSec + (srcFormat->nSamplesPerSec + 1) / 2) /
277 srcFormat->nSamplesPerSec;
278 const size_t rsize = rframes * rbytes;
279
280 if (!Stream_EnsureCapacity(context->common.resample, rsize))
281 return FALSE;
282
283 size_t idone = 0;
284 size_t odone = 0;
285 soxr_error_t error =
286 soxr_process(context->sox, src, sframes, &idone, Stream_Buffer(context->common.resample),
287 Stream_Capacity(context->common.resample) / rbytes, &odone);
288 if (!Stream_SetLength(context->common.resample, odone * rbytes))
289 return FALSE;
290
291 *data = Stream_Buffer(context->common.resample);
292 *length = Stream_Length(context->common.resample);
293 return (error == nullptr);
294#else
295 WLog_ERR(TAG, "Missing resample support, recompile -DWITH_SOXR=ON or -DWITH_DSP_FFMPEG=ON");
296 return FALSE;
297#endif
298}
299
307static const INT16 ima_step_index_table[] = {
308 -1, -1, -1, -1, 2, 4, 6, 8, -1, -1, -1, -1, 2, 4, 6, 8
309};
310
311static const INT16 ima_step_size_table[] = {
312 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 19, 21, 23,
313 25, 28, 31, 34, 37, 41, 45, 50, 55, 60, 66, 73, 80,
314 88, 97, 107, 118, 130, 143, 157, 173, 190, 209, 230, 253, 279,
315 307, 337, 371, 408, 449, 494, 544, 598, 658, 724, 796, 876, 963,
316 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024, 3327,
317 3660, 4026, 4428, 4871, 5358, 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487,
318 12635, 13899, 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
319};
320
321static inline void dsp_ima_clamp_step(ADPCM* WINPR_RESTRICT adpcm, size_t channel)
322{
323 WINPR_ASSERT(adpcm);
324 if (adpcm->ima.last_step[channel] < 0)
325 adpcm->ima.last_step[channel] = 0;
326
327 const size_t size = ARRAYSIZE(ima_step_size_table);
328 if ((size_t)adpcm->ima.last_step[channel] >= size)
329 adpcm->ima.last_step[channel] = (INT16)(size - 1);
330}
331
332static UINT16 dsp_decode_ima_adpcm_sample(ADPCM* WINPR_RESTRICT adpcm, unsigned int channel,
333 BYTE sample)
334{
335 WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ima.last_step));
336 WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ima.last_sample));
337
338 const INT16 offset = adpcm->ima.last_step[channel];
339 WINPR_ASSERT(offset >= 0);
340 WINPR_ASSERT(WINPR_CXX_COMPAT_CAST(size_t, offset) < ARRAYSIZE(ima_step_size_table));
341
342 const INT32 ss = ima_step_size_table[offset];
343 INT32 d = (ss >> 3);
344
345 if (sample & 1)
346 d += (ss >> 2);
347
348 if (sample & 2)
349 d += (ss >> 1);
350
351 if (sample & 4)
352 d += ss;
353
354 if (sample & 8)
355 d = -d;
356
357 d += adpcm->ima.last_sample[channel];
358
359 if (d < -32768)
360 d = -32768;
361 else if (d > 32767)
362 d = 32767;
363
364 adpcm->ima.last_sample[channel] = (INT16)d;
365
366 WINPR_ASSERT(sample < ARRAYSIZE(ima_step_index_table));
367 const int32_t last = adpcm->ima.last_step[channel] + ima_step_index_table[sample];
368 adpcm->ima.last_step[channel] = WINPR_ASSERTING_INT_CAST(int16_t, last);
369
370 dsp_ima_clamp_step(adpcm, channel);
371
372 return (UINT16)d;
373}
374
375static BOOL valid_ima_adpcm_format(const FREERDP_DSP_CONTEXT* WINPR_RESTRICT context)
376{
377 WINPR_ASSERT(context);
378 if (context->common.format.wFormatTag != WAVE_FORMAT_DVI_ADPCM)
379 return FALSE;
380 if (context->common.format.nBlockAlign <= 4ULL)
381 return FALSE;
382 if (context->common.format.nChannels < 1)
383 return FALSE;
384 if (context->common.format.wBitsPerSample == 0)
385 return FALSE;
386 return TRUE;
387}
388
389static BOOL freerdp_dsp_decode_ima_adpcm(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
390 const BYTE* WINPR_RESTRICT src, size_t size,
391 wStream* WINPR_RESTRICT out)
392{
393 if (!valid_ima_adpcm_format(context))
394 return FALSE;
395
396 size_t out_size = size * 4ull;
397 const UINT32 block_size = context->common.format.nBlockAlign;
398 const UINT32 channels = context->common.format.nChannels;
399
400 if (!Stream_EnsureCapacity(out, out_size))
401 return FALSE;
402
403 while (size > 0)
404 {
405 if (size % block_size == 0)
406 {
407 if (size < 4)
408 return FALSE;
409
410 context->adpcm.ima.last_sample[0] =
411 (INT16)(((UINT16)(*src)) | (((UINT16)(*(src + 1))) << 8));
412 context->adpcm.ima.last_step[0] = (INT16)(*(src + 2));
413
414 dsp_ima_clamp_step(&context->adpcm, 0u);
415
416 src += 4;
417 size -= 4;
418 out_size -= 16;
419
420 if (channels > 1)
421 {
422 if (size < 4)
423 return FALSE;
424 context->adpcm.ima.last_sample[1] =
425 (INT16)(((UINT16)(*src)) | (((UINT16)(*(src + 1))) << 8));
426 context->adpcm.ima.last_step[1] = (INT16)(*(src + 2));
427
428 dsp_ima_clamp_step(&context->adpcm, 1u);
429 src += 4;
430 size -= 4;
431 out_size -= 16;
432 }
433 }
434
435 if (channels > 1)
436 {
437 if (size < 8)
438 return FALSE;
439 for (size_t i = 0; i < 8; i++)
440 {
441 BYTE* dst = Stream_Pointer(out);
442
443 const unsigned channel = (i < 4 ? 0 : 1);
444 {
445 const BYTE sample = ((*src) & 0x0f);
446 const UINT16 decoded =
447 dsp_decode_ima_adpcm_sample(&context->adpcm, channel, sample);
448 dst[((i & 3) << 3) + (channel << 1u)] = (decoded & 0xFF);
449 dst[((i & 3) << 3) + (channel << 1u) + 1] = (decoded >> 8);
450 }
451 {
452 const BYTE sample = ((*src) >> 4);
453 const UINT16 decoded =
454 dsp_decode_ima_adpcm_sample(&context->adpcm, channel, sample);
455 dst[((i & 3) << 3) + (channel << 1u) + 4] = (decoded & 0xFF);
456 dst[((i & 3) << 3) + (channel << 1u) + 5] = (decoded >> 8);
457 }
458 src++;
459 }
460
461 if (!Stream_SafeSeek(out, 32))
462 return FALSE;
463 size -= 8;
464 }
465 else
466 {
467 if (size < 1)
468 return FALSE;
469 BYTE* dst = Stream_Pointer(out);
470 if (!Stream_SafeSeek(out, 4))
471 return FALSE;
472
473 {
474 const BYTE sample = ((*src) & 0x0f);
475 const UINT16 decoded = dsp_decode_ima_adpcm_sample(&context->adpcm, 0, sample);
476 *dst++ = (decoded & 0xFF);
477 *dst++ = (decoded >> 8);
478 }
479 {
480 const BYTE sample = ((*src) >> 4);
481 const UINT16 decoded = dsp_decode_ima_adpcm_sample(&context->adpcm, 0, sample);
482 *dst++ = (decoded & 0xFF);
483 *dst++ = (decoded >> 8);
484 }
485 src++;
486 size--;
487 }
488 }
489
490 return TRUE;
491}
492
493#if defined(WITH_GSM)
494static BOOL freerdp_dsp_decode_gsm610(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
495 const BYTE* WINPR_RESTRICT src, size_t size,
496 wStream* WINPR_RESTRICT out)
497{
498 size_t offset = 0;
499
500 while (offset < size)
501 {
502 /* gsm_decode(GSM_OPT_WAV49) requires alternating fixed 33/32 bytes as input.
503 * abort if the data supplied was inconsistent.
504 */
505 const BOOL large = (offset % 65) == 0;
506 const size_t toadd = large ? 33 : 32;
507 if (size - offset < toadd)
508 return FALSE;
509
510 gsm_signal gsmBlockBuffer[160] = WINPR_C_ARRAY_INIT;
511 const int rc =
512 gsm_decode(context->gsm,
513 WINPR_CAST_CONST_PTR_AWAY(/* API does not modify */ &src[offset], gsm_byte*),
514 gsmBlockBuffer);
515
516 if (rc < 0)
517 return FALSE;
518
519 offset += toadd;
520
521 if (!Stream_EnsureRemainingCapacity(out, sizeof(gsmBlockBuffer)))
522 return FALSE;
523
524 Stream_Write(out, (void*)gsmBlockBuffer, sizeof(gsmBlockBuffer));
525 }
526
527 return TRUE;
528}
529
530static BOOL freerdp_dsp_encode_gsm610(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
531 const BYTE* WINPR_RESTRICT src, size_t size,
532 wStream* WINPR_RESTRICT out)
533{
534 size_t offset = 0;
535
536 while (offset < size)
537 {
538 const gsm_signal* signal = WINPR_PACKED_ALIGN_CAST(const gsm_signal*, &src[offset]);
539
540 if (!Stream_EnsureRemainingCapacity(out, sizeof(gsm_frame)))
541 return FALSE;
542
543 gsm_encode(context->gsm,
544 WINPR_CAST_CONST_PTR_AWAY(/* API does not modify */ signal, gsm_signal*),
545 Stream_Pointer(out));
546
547 if ((offset % 65) == 0)
548 Stream_Seek(out, 33);
549 else
550 Stream_Seek(out, 32);
551
552 offset += 160;
553 }
554
555 return TRUE;
556}
557#endif
558
559#if defined(WITH_LAME)
560static BOOL valid_mp3_format(const FREERDP_DSP_CONTEXT* WINPR_RESTRICT context)
561{
562 WINPR_ASSERT(context);
563 if (context->common.format.wFormatTag != WAVE_FORMAT_MPEGLAYER3)
564 return FALSE;
565 if (context->common.format.nChannels < 1)
566 return FALSE;
567 if (context->common.format.wBitsPerSample == 0)
568 return FALSE;
569 if (context->common.format.nSamplesPerSec == 0)
570 return FALSE;
571 return TRUE;
572}
573
574static BOOL freerdp_dsp_decode_mp3(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
575 const BYTE* WINPR_RESTRICT src, size_t size,
576 wStream* WINPR_RESTRICT out)
577{
578 if (!context || !src || !out)
579 return FALSE;
580 if (!valid_mp3_format(context))
581 return FALSE;
582 const size_t buffer_size =
583 2ull * context->common.format.nChannels * context->common.format.nSamplesPerSec;
584
585 if (!Stream_EnsureCapacity(context->common.buffer, 2ull * buffer_size))
586 return FALSE;
587
588 short* pcm_l = Stream_BufferAs(context->common.buffer, short);
589 short* pcm_r = Stream_BufferAs(context->common.buffer, short) + (buffer_size / sizeof(short));
590 const int rc = hip_decode(
591 context->hip,
592 WINPR_CAST_CONST_PTR_AWAY(/* API is not modifying content */ src, unsigned char*), size,
593 pcm_l, pcm_r);
594
595 if (rc <= 0)
596 return FALSE;
597
598 if (!Stream_EnsureRemainingCapacity(out, (size_t)rc * context->common.format.nChannels * 2ull))
599 return FALSE;
600
601 for (int x = 0; x < rc; x++)
602 {
603 Stream_Write_UINT16(out, (UINT16)pcm_l[x]);
604 Stream_Write_UINT16(out, (UINT16)pcm_r[x]);
605 }
606
607 return TRUE;
608}
609
610static BOOL freerdp_dsp_encode_mp3(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
611 const BYTE* WINPR_RESTRICT src, size_t size,
612 wStream* WINPR_RESTRICT out)
613{
614 if (!context || !src || !out)
615 return FALSE;
616
617 if (!valid_mp3_format(context))
618 return FALSE;
619
620 size_t samples_per_channel =
621 size / context->common.format.nChannels / context->common.format.wBitsPerSample / 8;
622
623 /* Ensure worst case buffer size for mp3 stream taken from LAME header */
624 if (!Stream_EnsureRemainingCapacity(out, 5ull / 4ull * samples_per_channel + 7200ull))
625 return FALSE;
626
627 samples_per_channel = size / 2 /* size of a sample */ / context->common.format.nChannels;
628 const size_t outLen = Stream_GetRemainingCapacity(out);
629 if ((outLen > INT_MAX) || (samples_per_channel > INT_MAX))
630 return FALSE;
631
632 const int rc = lame_encode_buffer_interleaved(
633 context->lame,
634 WINPR_CAST_CONST_PTR_AWAY(WINPR_PACKED_ALIGN_CAST(const short*, src), short*),
635 WINPR_ASSERTING_INT_CAST(int, samples_per_channel), Stream_Pointer(out),
636 WINPR_ASSERTING_INT_CAST(int, outLen));
637
638 if (rc < 0)
639 return FALSE;
640
641 Stream_Seek(out, (size_t)rc);
642 return TRUE;
643}
644#endif
645
646#if defined(WITH_FAAC)
647static BOOL freerdp_dsp_encode_faac(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
648 const BYTE* WINPR_RESTRICT src, size_t size,
649 wStream* WINPR_RESTRICT out)
650{
651 const int16_t* inSamples = WINPR_PACKED_ALIGN_CAST(const int16_t*, src);
652
653 if (!context || !src || !out)
654 return FALSE;
655
656 const unsigned int bpp = context->common.format.wBitsPerSample / 8;
657 const size_t nrSamples = size / bpp;
658
659 if (!Stream_EnsureRemainingCapacity(context->common.buffer, nrSamples * sizeof(int16_t)))
660 return FALSE;
661
662 for (size_t x = 0; x < nrSamples; x++)
663 {
664 Stream_Write_INT16(context->common.buffer, inSamples[x]);
665 if (Stream_GetPosition(context->common.buffer) / bpp >= context->faacInputSamples)
666 {
667 if (!Stream_EnsureRemainingCapacity(out, context->faacMaxOutputBytes))
668 return FALSE;
669
670 const size_t outLen = Stream_GetRemainingCapacity(out);
671 if ((outLen > UINT_MAX) || (context->faacInputSamples > UINT_MAX))
672 return FALSE;
673 const int rc =
674 faacEncEncode(context->faac, Stream_BufferAs(context->common.buffer, int32_t),
675 WINPR_ASSERTING_INT_CAST(unsigned int, context->faacInputSamples),
676 Stream_Pointer(out), WINPR_ASSERTING_INT_CAST(unsigned int, outLen));
677 if (rc < 0)
678 return FALSE;
679 if (rc > 0)
680 Stream_Seek(out, (size_t)rc);
681 Stream_ResetPosition(context->common.buffer);
682 }
683 }
684
685 return TRUE;
686}
687#endif
688
689#if defined(WITH_OPUS)
690static BOOL freerdp_dsp_decode_opus(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
691 const BYTE* WINPR_RESTRICT src, size_t size,
692 wStream* WINPR_RESTRICT out)
693{
694 if (!context || !context->opus_decoder || !src || !out)
695 return FALSE;
696
697 /* Max packet duration is 120ms (5760 at 48KHz) */
698 const size_t max_size = OPUS_MAX_FRAMES * context->common.format.nChannels * sizeof(int16_t);
699 if (!Stream_EnsureRemainingCapacity(out, max_size))
700 return FALSE;
701
702 const opus_int32 frames =
703 opus_decode(context->opus_decoder, src, WINPR_ASSERTING_INT_CAST(opus_int32, size),
704 Stream_Pointer(out), OPUS_MAX_FRAMES, 0);
705 if (frames < 0)
706 return FALSE;
707
708 Stream_Seek(out, (size_t)frames * context->common.format.nChannels * sizeof(int16_t));
709
710 return TRUE;
711}
712
713/* Packet durations opus_encode() takes, in units of 2.5 ms, longest first. */
714static const UINT32 opus_packet_units[] = { 24, 16, 8, 4, 2, 1 };
715
716static BOOL opus_is_packet_frames(UINT32 rate, size_t frames)
717{
718 for (size_t x = 0; x < ARRAYSIZE(opus_packet_units); x++)
719 {
720 if (frames == 1ull * rate * opus_packet_units[x] / 400)
721 return TRUE;
722 }
723 return FALSE;
724}
725
726/*
727 * opus_encode() only takes 2.5, 5, 10, 20, 40 or 60 ms of audio, but callers hand over what their
728 * capture device delivered. Collect the samples and encode the longest valid packet, at least
729 * opus_packet_frames long; keep the rest for the next call. Nothing is written until enough
730 * samples arrived.
731 */
732static BOOL freerdp_dsp_encode_opus(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
733 const BYTE* WINPR_RESTRICT src, size_t size,
734 wStream* WINPR_RESTRICT out)
735{
736 if (!context || !context->opus_encoder || !context->opus_input || !src || !out)
737 return FALSE;
738
739 wStream* input = context->opus_input;
740 const UINT32 rate = context->common.format.nSamplesPerSec;
741 const size_t frame_bytes = sizeof(opus_int16) * context->common.format.nChannels;
742 if (!Stream_EnsureRemainingCapacity(input, size))
743 return FALSE;
744 Stream_Write(input, src, size);
745
746 const size_t available = Stream_GetPosition(input) / frame_bytes;
747 if (available < context->opus_packet_frames)
748 return TRUE;
749
750 size_t frames = 0;
751 for (size_t x = 0; x < ARRAYSIZE(opus_packet_units); x++)
752 {
753 frames = 1ull * rate * opus_packet_units[x] / 400;
754 if (frames <= available)
755 break;
756 }
757
758 /* Max packet duration is 120ms (5760 at 48KHz) */
759 const size_t max_size = OPUS_MAX_FRAMES * context->common.format.nChannels * sizeof(int16_t);
760 if (!Stream_EnsureRemainingCapacity(out, max_size))
761 return FALSE;
762
763 const opus_int16* src_data = WINPR_PACKED_ALIGN_CAST(const opus_int16*, Stream_Buffer(input));
764 const opus_int32 bytes =
765 opus_encode(context->opus_encoder, src_data, WINPR_ASSERTING_INT_CAST(opus_int32, frames),
766 Stream_Pointer(out), WINPR_ASSERTING_INT_CAST(opus_int32, max_size));
767 if (bytes < 0)
768 return FALSE;
769 Stream_Seek(out, (size_t)bytes);
770
771 const size_t used = frames * frame_bytes;
772 const size_t left = Stream_GetPosition(input) - used;
773 memmove(Stream_Buffer(input), Stream_Buffer(input) + used, left);
774 return Stream_SetPosition(input, left);
775}
776#endif
777
778#if defined(WITH_FAAD2)
779static BOOL freerdp_dsp_decode_faad(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
780 const BYTE* WINPR_RESTRICT src, size_t size,
781 wStream* WINPR_RESTRICT out)
782{
783 NeAACDecFrameInfo info;
784 size_t offset = 0;
785
786 if (!context || !src || !out)
787 return FALSE;
788
789 if (!context->faadSetup)
790 {
791 union
792 {
793 const void* cpv;
794 void* pv;
795 } cnv;
796 unsigned long samplerate = 0;
797 unsigned char channels = 0;
798
799 cnv.cpv = src;
800 const long err = NeAACDecInit(context->faad, /* API is not modifying content */ cnv.pv,
801 size, &samplerate, &channels);
802
803 if (err != 0)
804 return FALSE;
805
806 if (channels != context->common.format.nChannels)
807 return FALSE;
808
809 if (samplerate != context->common.format.nSamplesPerSec)
810 return FALSE;
811
812 context->faadSetup = TRUE;
813 }
814
815 while (offset < size)
816 {
817 union
818 {
819 const void* cpv;
820 void* pv;
821 } cnv;
822
823 const size_t outSize = context->common.format.nSamplesPerSec *
824 context->common.format.nChannels *
825 context->common.format.wBitsPerSample / 8;
826
827 if (!Stream_EnsureRemainingCapacity(out, outSize))
828 return FALSE;
829
830 void* sample_buffer = Stream_Pointer(out);
831
832 cnv.cpv = &src[offset];
833 NeAACDecDecode2(context->faad, &info, cnv.pv, size - offset, &sample_buffer,
834 Stream_GetRemainingCapacity(out));
835
836 if (info.error != 0)
837 return FALSE;
838
839 offset += info.bytesconsumed;
840
841 if (info.samples == 0)
842 continue;
843
844 Stream_Seek(out, info.samples * context->common.format.wBitsPerSample / 8);
845 }
846
847 return TRUE;
848}
849
850#endif
851
859static const struct
860{
861 BYTE byte_num;
862 BYTE byte_shift;
863} ima_stereo_encode_map[] = { { 0, 0 }, { 4, 0 }, { 0, 4 }, { 4, 4 }, { 1, 0 }, { 5, 0 },
864 { 1, 4 }, { 5, 4 }, { 2, 0 }, { 6, 0 }, { 2, 4 }, { 6, 4 },
865 { 3, 0 }, { 7, 0 }, { 3, 4 }, { 7, 4 } };
866
867static BYTE dsp_encode_ima_adpcm_sample(ADPCM* WINPR_RESTRICT adpcm, size_t channel, INT16 sample)
868{
869 WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ima.last_step));
870 WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ima.last_sample));
871
872 const INT16 offset = adpcm->ima.last_step[channel];
873 WINPR_ASSERT(offset >= 0);
874 WINPR_ASSERT(WINPR_CXX_COMPAT_CAST(size_t, offset) < ARRAYSIZE(ima_step_size_table));
875
876 INT32 ss = ima_step_size_table[offset];
877 INT32 e = sample - adpcm->ima.last_sample[channel];
878 INT32 d = e;
879 INT32 diff = ss >> 3;
880 BYTE enc = 0;
881
882 if (e < 0)
883 {
884 enc = 8;
885 e = -e;
886 }
887
888 if (e >= ss)
889 {
890 enc |= 4;
891 e -= ss;
892 }
893
894 ss >>= 1;
895
896 if (e >= ss)
897 {
898 enc |= 2;
899 e -= ss;
900 }
901
902 ss >>= 1;
903
904 if (e >= ss)
905 {
906 enc |= 1;
907 e -= ss;
908 }
909
910 if (d < 0)
911 diff = d + e - diff;
912 else
913 diff = d - e + diff;
914
915 diff += adpcm->ima.last_sample[channel];
916
917 if (diff < -32768)
918 diff = -32768;
919 else if (diff > 32767)
920 diff = 32767;
921
922 adpcm->ima.last_sample[channel] = (INT16)diff;
923
924 WINPR_ASSERT(enc < ARRAYSIZE(ima_step_index_table));
925 const int32_t last = adpcm->ima.last_step[channel] + ima_step_index_table[enc];
926 adpcm->ima.last_step[channel] = WINPR_ASSERTING_INT_CAST(int16_t, last);
927
928 dsp_ima_clamp_step(adpcm, channel);
929
930 return enc;
931}
932
933static BOOL freerdp_dsp_encode_ima_adpcm(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
934 const BYTE* WINPR_RESTRICT src, size_t size,
935 wStream* WINPR_RESTRICT out)
936{
937 if (!valid_ima_adpcm_format(context))
938 return FALSE;
939 if (!Stream_EnsureRemainingCapacity(out, size))
940 return FALSE;
941 if (!Stream_EnsureRemainingCapacity(context->common.buffer, size + 64ull))
942 return FALSE;
943
944 const size_t align = (context->common.format.nChannels > 1) ? 32 : 4;
945
946 while (size >= align)
947 {
948 if (Stream_GetPosition(context->common.buffer) % context->common.format.nBlockAlign == 0)
949 {
950 Stream_Write_UINT8(context->common.buffer, context->adpcm.ima.last_sample[0] & 0xFF);
951 Stream_Write_UINT8(context->common.buffer,
952 (context->adpcm.ima.last_sample[0] >> 8) & 0xFF);
953 Stream_Write_UINT8(context->common.buffer, (BYTE)context->adpcm.ima.last_step[0]);
954 Stream_Write_UINT8(context->common.buffer, 0);
955
956 if (context->common.format.nChannels > 1)
957 {
958 Stream_Write_UINT8(context->common.buffer,
959 context->adpcm.ima.last_sample[1] & 0xFF);
960 Stream_Write_UINT8(context->common.buffer,
961 (context->adpcm.ima.last_sample[1] >> 8) & 0xFF);
962 Stream_Write_UINT8(context->common.buffer, (BYTE)context->adpcm.ima.last_step[1]);
963 Stream_Write_UINT8(context->common.buffer, 0);
964 }
965 }
966
967 if (context->common.format.nChannels > 1)
968 {
969 BYTE* dst = Stream_Pointer(context->common.buffer);
970 ZeroMemory(dst, 8);
971
972 for (size_t i = 0; i < 16; i++)
973 {
974 const INT16 sample = (INT16)(((UINT16)(*src)) | (((UINT16)(*(src + 1))) << 8));
975 src += 2;
976 const BYTE encoded = dsp_encode_ima_adpcm_sample(&context->adpcm, i % 2, sample);
977 dst[ima_stereo_encode_map[i].byte_num] |= encoded
978 << ima_stereo_encode_map[i].byte_shift;
979 }
980
981 if (!Stream_SafeSeek(context->common.buffer, 8))
982 return FALSE;
983 size -= 32;
984 }
985 else
986 {
987 INT16 sample = (INT16)(((UINT16)(*src)) | (((UINT16)(*(src + 1))) << 8));
988 src += 2;
989 BYTE encoded = dsp_encode_ima_adpcm_sample(&context->adpcm, 0, sample);
990 sample = (INT16)(((UINT16)(*src)) | (((UINT16)(*(src + 1))) << 8));
991 src += 2;
992 encoded |= dsp_encode_ima_adpcm_sample(&context->adpcm, 0, sample) << 4;
993 Stream_Write_UINT8(context->common.buffer, encoded);
994 size -= 4;
995 }
996
997 if (Stream_GetPosition(context->common.buffer) >= context->adpcm.ima.packet_size)
998 {
999 BYTE* bsrc = Stream_Buffer(context->common.buffer);
1000 Stream_Write(out, bsrc, context->adpcm.ima.packet_size);
1001 Stream_ResetPosition(context->common.buffer);
1002 }
1003 }
1004
1005 return TRUE;
1006}
1007
1014static const INT32 ms_adpcm_adaptation_table[] = { 230, 230, 230, 230, 307, 409, 512, 614,
1015 768, 614, 512, 409, 307, 230, 230, 230 };
1016
1017static const INT32 ms_adpcm_coeffs1[7] = { 256, 512, 0, 192, 240, 460, 392 };
1018
1019static const INT32 ms_adpcm_coeffs2[7] = { 0, -256, 0, 64, 0, -208, -232 };
1020
1021static inline INT16 freerdp_dsp_decode_ms_adpcm_sample(ADPCM* WINPR_RESTRICT adpcm, BYTE sample,
1022 size_t channel)
1023{
1024 WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ms.sample1));
1025 WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ms.sample2));
1026 WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ms.delta));
1027 WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ms.predictor));
1028
1029 const INT8 nibble = (INT8)((sample & 0x08) ? (sample - 16) : sample);
1030 const BYTE predictor = adpcm->ms.predictor[channel];
1031 INT32 coeff1 = 0;
1032 if (predictor < ARRAYSIZE(ms_adpcm_coeffs1))
1033 coeff1 = ms_adpcm_coeffs1[predictor];
1034
1035 INT32 coeff2 = 0;
1036 if (predictor < ARRAYSIZE(ms_adpcm_coeffs2))
1037 coeff2 = ms_adpcm_coeffs2[predictor];
1038 INT32 presample =
1039 ((adpcm->ms.sample1[channel] * coeff1) + (adpcm->ms.sample2[channel] * coeff2)) / 256;
1040 presample += nibble * adpcm->ms.delta[channel];
1041
1042 if (presample > 32767)
1043 presample = 32767;
1044 else if (presample < -32768)
1045 presample = -32768;
1046
1047 adpcm->ms.sample2[channel] = adpcm->ms.sample1[channel];
1048 adpcm->ms.sample1[channel] = presample;
1049
1050 INT32 tableval = 0;
1051 if (sample < ARRAYSIZE(ms_adpcm_adaptation_table))
1052 tableval = ms_adpcm_adaptation_table[sample];
1053
1054 adpcm->ms.delta[channel] = adpcm->ms.delta[channel] * tableval / 256;
1055
1056 if (adpcm->ms.delta[channel] < 16)
1057 adpcm->ms.delta[channel] = 16;
1058
1059 return (INT16)presample;
1060}
1061
1062static BOOL valid_ms_adpcm_format(const FREERDP_DSP_CONTEXT* WINPR_RESTRICT context)
1063{
1064 WINPR_ASSERT(context);
1065 if (context->common.format.wFormatTag != WAVE_FORMAT_ADPCM)
1066 return FALSE;
1067 if (context->common.format.nBlockAlign <= 4ULL)
1068 return FALSE;
1069 if (context->common.format.nChannels < 1)
1070 return FALSE;
1071 if (context->common.format.wBitsPerSample == 0)
1072 return FALSE;
1073 return TRUE;
1074}
1075
1076static BOOL freerdp_dsp_decode_ms_adpcm(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
1077 const BYTE* WINPR_RESTRICT src, size_t size,
1078 wStream* WINPR_RESTRICT out)
1079{
1080 if (!valid_ms_adpcm_format(context))
1081 return FALSE;
1082 const size_t out_size = size * 4;
1083 const UINT32 channels = context->common.format.nChannels;
1084 const UINT32 block_size = context->common.format.nBlockAlign;
1085
1086 if (!Stream_EnsureCapacity(out, out_size))
1087 return FALSE;
1088
1089 while (size > 0)
1090 {
1091 if (size % block_size == 0)
1092 {
1093 if (channels > 1)
1094 {
1095 if (size < 14)
1096 return FALSE;
1097
1098 context->adpcm.ms.predictor[0] = *src++;
1099 context->adpcm.ms.predictor[1] = *src++;
1100 context->adpcm.ms.delta[0] = read_int16(src);
1101 src += 2;
1102 context->adpcm.ms.delta[1] = read_int16(src);
1103 src += 2;
1104 context->adpcm.ms.sample1[0] = read_int16(src);
1105 src += 2;
1106 context->adpcm.ms.sample1[1] = read_int16(src);
1107 src += 2;
1108 context->adpcm.ms.sample2[0] = read_int16(src);
1109 src += 2;
1110 context->adpcm.ms.sample2[1] = read_int16(src);
1111 src += 2;
1112 size -= 14;
1113 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample2[0]);
1114 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample2[1]);
1115 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample1[0]);
1116 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample1[1]);
1117 }
1118 else
1119 {
1120 if (size < 7)
1121 return FALSE;
1122
1123 context->adpcm.ms.predictor[0] = *src++;
1124 context->adpcm.ms.delta[0] = read_int16(src);
1125 src += 2;
1126 context->adpcm.ms.sample1[0] = read_int16(src);
1127 src += 2;
1128 context->adpcm.ms.sample2[0] = read_int16(src);
1129 src += 2;
1130 size -= 7;
1131 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample2[0]);
1132 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample1[0]);
1133 }
1134 }
1135
1136 if (channels > 1)
1137 {
1138 {
1139 if (size < 1)
1140 return FALSE;
1141 const BYTE sample = *src++;
1142 size--;
1143 Stream_Write_INT16(
1144 out, freerdp_dsp_decode_ms_adpcm_sample(&context->adpcm, sample >> 4, 0));
1145 Stream_Write_INT16(
1146 out, freerdp_dsp_decode_ms_adpcm_sample(&context->adpcm, sample & 0x0F, 1));
1147 }
1148 {
1149 if (size < 1)
1150 return FALSE;
1151 const BYTE sample = *src++;
1152 size--;
1153 Stream_Write_INT16(
1154 out, freerdp_dsp_decode_ms_adpcm_sample(&context->adpcm, sample >> 4, 0));
1155 Stream_Write_INT16(
1156 out, freerdp_dsp_decode_ms_adpcm_sample(&context->adpcm, sample & 0x0F, 1));
1157 }
1158 }
1159 else
1160 {
1161 if (size < 1)
1162 return FALSE;
1163 const BYTE sample = *src++;
1164 size--;
1165 Stream_Write_INT16(out,
1166 freerdp_dsp_decode_ms_adpcm_sample(&context->adpcm, sample >> 4, 0));
1167 Stream_Write_INT16(
1168 out, freerdp_dsp_decode_ms_adpcm_sample(&context->adpcm, sample & 0x0F, 0));
1169 }
1170 }
1171
1172 return TRUE;
1173}
1174
1175static BYTE freerdp_dsp_encode_ms_adpcm_sample(ADPCM* WINPR_RESTRICT adpcm, INT32 sample,
1176 size_t channel)
1177{
1178 WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ms.sample1));
1179 WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ms.sample2));
1180 WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ms.delta));
1181 WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ms.predictor));
1182
1183 INT32 presample =
1184 ((adpcm->ms.sample1[channel] * ms_adpcm_coeffs1[adpcm->ms.predictor[channel]]) +
1185 (adpcm->ms.sample2[channel] * ms_adpcm_coeffs2[adpcm->ms.predictor[channel]])) /
1186 256;
1187 INT32 errordelta = (sample - presample) / adpcm->ms.delta[channel];
1188
1189 if ((sample - presample) % adpcm->ms.delta[channel] > adpcm->ms.delta[channel] / 2)
1190 errordelta++;
1191
1192 if (errordelta > 7)
1193 errordelta = 7;
1194 else if (errordelta < -8)
1195 errordelta = -8;
1196
1197 presample += adpcm->ms.delta[channel] * errordelta;
1198
1199 if (presample > 32767)
1200 presample = 32767;
1201 else if (presample < -32768)
1202 presample = -32768;
1203
1204 adpcm->ms.sample2[channel] = adpcm->ms.sample1[channel];
1205 adpcm->ms.sample1[channel] = presample;
1206 const size_t offset = (((BYTE)errordelta) & 0x0F);
1207 WINPR_ASSERT(offset < ARRAYSIZE(ms_adpcm_adaptation_table));
1208 adpcm->ms.delta[channel] = adpcm->ms.delta[channel] * ms_adpcm_adaptation_table[offset] / 256;
1209
1210 if (adpcm->ms.delta[channel] < 16)
1211 adpcm->ms.delta[channel] = 16;
1212
1213 return ((BYTE)errordelta) & 0x0F;
1214}
1215
1216static BOOL freerdp_dsp_encode_ms_adpcm(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
1217 const BYTE* WINPR_RESTRICT src, size_t size,
1218 wStream* WINPR_RESTRICT out)
1219{
1220 if (!valid_ms_adpcm_format(context))
1221 return FALSE;
1222
1223 const size_t step = 8 + ((context->common.format.nChannels > 1) ? 4 : 0);
1224
1225 if (!Stream_EnsureRemainingCapacity(out, size))
1226 return FALSE;
1227
1228 const size_t start = Stream_GetPosition(out);
1229
1230 if (context->adpcm.ms.delta[0] < 16)
1231 context->adpcm.ms.delta[0] = 16;
1232
1233 if (context->adpcm.ms.delta[1] < 16)
1234 context->adpcm.ms.delta[1] = 16;
1235
1236 while (size >= step)
1237 {
1238 if ((Stream_GetPosition(out) - start) % context->common.format.nBlockAlign == 0)
1239 {
1240 if (context->common.format.nChannels > 1)
1241 {
1242 Stream_Write_UINT8(out, context->adpcm.ms.predictor[0]);
1243 Stream_Write_UINT8(out, context->adpcm.ms.predictor[1]);
1244 Stream_Write_UINT8(out, (context->adpcm.ms.delta[0] & 0xFF));
1245 Stream_Write_UINT8(out, ((context->adpcm.ms.delta[0] >> 8) & 0xFF));
1246 Stream_Write_UINT8(out, (context->adpcm.ms.delta[1] & 0xFF));
1247 Stream_Write_UINT8(out, ((context->adpcm.ms.delta[1] >> 8) & 0xFF));
1248
1249 context->adpcm.ms.sample1[0] = read_int16(src + 4);
1250 context->adpcm.ms.sample1[1] = read_int16(src + 6);
1251 context->adpcm.ms.sample2[0] = read_int16(src + 0);
1252 context->adpcm.ms.sample2[1] = read_int16(src + 2);
1253
1254 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample1[0]);
1255 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample1[1]);
1256 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample2[0]);
1257 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample2[1]);
1258
1259 src += 8;
1260 size -= 8;
1261 }
1262 else
1263 {
1264 Stream_Write_UINT8(out, context->adpcm.ms.predictor[0]);
1265 Stream_Write_UINT8(out, (BYTE)(context->adpcm.ms.delta[0] & 0xFF));
1266 Stream_Write_UINT8(out, (BYTE)((context->adpcm.ms.delta[0] >> 8) & 0xFF));
1267
1268 context->adpcm.ms.sample1[0] = read_int16(src + 2);
1269 context->adpcm.ms.sample2[0] = read_int16(src + 0);
1270
1271 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample1[0]);
1272 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample2[0]);
1273 src += 4;
1274 size -= 4;
1275 }
1276 }
1277
1278 {
1279 const INT16 sample = read_int16(src);
1280 src += 2;
1281 Stream_Write_UINT8(
1282 out, (freerdp_dsp_encode_ms_adpcm_sample(&context->adpcm, sample, 0) << 4) & 0xFF);
1283 }
1284 {
1285 const INT16 sample = read_int16(src);
1286 src += 2;
1287
1288 BYTE val = 0;
1289 Stream_Read_UINT8(out, val);
1290 val += freerdp_dsp_encode_ms_adpcm_sample(&context->adpcm, sample,
1291 context->common.format.nChannels > 1 ? 1 : 0);
1292 Stream_Write_UINT8(out, val);
1293 }
1294 size -= 4;
1295 }
1296
1297 return TRUE;
1298}
1299
1300#endif
1301
1302FREERDP_DSP_CONTEXT* freerdp_dsp_context_new(BOOL encoder)
1303{
1304#if defined(WITH_DSP_FFMPEG)
1305 return freerdp_dsp_ffmpeg_context_new(encoder);
1306#else
1307 FREERDP_DSP_CONTEXT* context = calloc(1, sizeof(FREERDP_DSP_CONTEXT));
1308
1309 if (!context)
1310 return nullptr;
1311
1312 if (!freerdp_dsp_common_context_init(&context->common, encoder))
1313 goto fail;
1314
1315#if defined(WITH_GSM)
1316 context->gsm = gsm_create();
1317
1318 if (!context->gsm)
1319 goto fail;
1320
1321 {
1322 int val = 1;
1323 int rc = gsm_option(context->gsm, GSM_OPT_WAV49, &val);
1324
1325 if (rc < 0)
1326 goto fail;
1327 }
1328#endif
1329#if defined(WITH_LAME)
1330
1331 if (encoder)
1332 {
1333 context->lame = lame_init();
1334
1335 if (!context->lame)
1336 goto fail;
1337 }
1338 else
1339 {
1340 context->hip = hip_decode_init();
1341
1342 if (!context->hip)
1343 goto fail;
1344 }
1345
1346#endif
1347#if defined(WITH_FAAD2)
1348
1349 if (!encoder)
1350 {
1351 context->faad = NeAACDecOpen();
1352
1353 if (!context->faad)
1354 goto fail;
1355 }
1356
1357#endif
1358 return context;
1359fail:
1360 freerdp_dsp_context_free(context);
1361 return nullptr;
1362#endif
1363}
1364
1365void freerdp_dsp_context_free(FREERDP_DSP_CONTEXT* context)
1366{
1367 if (!context)
1368 return;
1369
1370#if defined(WITH_FDK_AAC)
1372 WINPR_ASSERT(ctx);
1373 fdk_aac_dsp_uninit(ctx);
1374#endif
1375
1376#if defined(WITH_MEDIACODEC)
1377 mediacodec_aac_dsp_uninit((FREERDP_DSP_COMMON_CONTEXT*)context);
1378#endif
1379
1380#if defined(WITH_DSP_FFMPEG)
1381 freerdp_dsp_ffmpeg_context_free(context);
1382#else
1383
1384 freerdp_dsp_common_context_uninit(&context->common);
1385
1386#if defined(WITH_GSM)
1387 gsm_destroy(context->gsm);
1388#endif
1389#if defined(WITH_LAME)
1390
1391 if (context->common.encoder)
1392 lame_close(context->lame);
1393 else
1394 hip_decode_exit(context->hip);
1395
1396#endif
1397#if defined(WITH_OPUS)
1398
1399 if (context->opus_decoder)
1400 opus_decoder_destroy(context->opus_decoder);
1401 if (context->opus_encoder)
1402 opus_encoder_destroy(context->opus_encoder);
1403 Stream_Free(context->opus_input, TRUE);
1404
1405#endif
1406#if defined(WITH_FAAD2)
1407
1408 if (!context->common.encoder)
1409 NeAACDecClose(context->faad);
1410
1411#endif
1412#if defined(WITH_FAAC)
1413
1414 if (context->faac)
1415 faacEncClose(context->faac);
1416
1417#endif
1418#if defined(WITH_SOXR)
1419 soxr_delete(context->sox);
1420#endif
1421 free(context);
1422
1423#endif
1424}
1425
1426BOOL freerdp_dsp_encode(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
1427 const AUDIO_FORMAT* WINPR_RESTRICT srcFormat,
1428 const BYTE* WINPR_RESTRICT pdata, size_t length,
1429 wStream* WINPR_RESTRICT out)
1430{
1431#if defined(WITH_MEDIACODEC)
1433 if (mcctx && (mcctx->format.wFormatTag == WAVE_FORMAT_AAC_MS))
1434 return mediacodec_aac_dsp_encode(mcctx, srcFormat, pdata, length, out);
1435#endif
1436
1437#if defined(WITH_FDK_AAC)
1439 WINPR_ASSERT(ctx);
1440 switch (ctx->format.wFormatTag)
1441 {
1442 case WAVE_FORMAT_AAC_MS:
1443 return fdk_aac_dsp_encode(ctx, srcFormat, pdata, length, out);
1444 default:
1445 break;
1446 }
1447#endif
1448
1449#if defined(WITH_DSP_FFMPEG)
1450 return freerdp_dsp_ffmpeg_encode(context, srcFormat, pdata, length, out);
1451#else
1452 if (!context || !context->common.encoder || !srcFormat || !pdata || !out)
1453 return FALSE;
1454
1455 AUDIO_FORMAT format = *srcFormat;
1456 const BYTE* resampleData = nullptr;
1457 size_t resampleLength = 0;
1458
1459 if (!freerdp_dsp_channel_mix(context, pdata, length, srcFormat, &resampleData, &resampleLength))
1460 return FALSE;
1461
1462 format.nChannels = context->common.format.nChannels;
1463
1464 const BYTE* data = nullptr;
1465 if (!freerdp_dsp_resample(context, resampleData, resampleLength, &format, &data, &length))
1466 return FALSE;
1467
1468 switch (context->common.format.wFormatTag)
1469 {
1470 case WAVE_FORMAT_PCM:
1471 if (!Stream_EnsureRemainingCapacity(out, length))
1472 return FALSE;
1473
1474 Stream_Write(out, data, length);
1475 return TRUE;
1476
1477 case WAVE_FORMAT_ADPCM:
1478 return freerdp_dsp_encode_ms_adpcm(context, data, length, out);
1479
1480 case WAVE_FORMAT_DVI_ADPCM:
1481 return freerdp_dsp_encode_ima_adpcm(context, data, length, out);
1482#if defined(WITH_GSM)
1483
1484 case WAVE_FORMAT_GSM610:
1485 return freerdp_dsp_encode_gsm610(context, data, length, out);
1486#endif
1487#if defined(WITH_LAME)
1488
1489 case WAVE_FORMAT_MPEGLAYER3:
1490 return freerdp_dsp_encode_mp3(context, data, length, out);
1491#endif
1492#if defined(WITH_FAAC)
1493
1494 case WAVE_FORMAT_AAC_MS:
1495 return freerdp_dsp_encode_faac(context, data, length, out);
1496#endif
1497#if defined(WITH_OPUS)
1498
1499 case WAVE_FORMAT_OPUS:
1500 return freerdp_dsp_encode_opus(context, data, length, out);
1501#endif
1502 default:
1503 return FALSE;
1504 }
1505
1506 return FALSE;
1507#endif
1508}
1509
1510BOOL freerdp_dsp_decode(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
1511 const AUDIO_FORMAT* WINPR_RESTRICT srcFormat,
1512 const BYTE* WINPR_RESTRICT data, size_t length, wStream* WINPR_RESTRICT out)
1513{
1514#if defined(WITH_MEDIACODEC)
1516 if (mcctx && (mcctx->format.wFormatTag == WAVE_FORMAT_AAC_MS))
1517 return mediacodec_aac_dsp_decode(mcctx, srcFormat, data, length, out);
1518#endif
1519
1520#if defined(WITH_FDK_AAC)
1522 WINPR_ASSERT(ctx);
1523 switch (ctx->format.wFormatTag)
1524 {
1525 case WAVE_FORMAT_AAC_MS:
1526 return fdk_aac_dsp_decode(ctx, srcFormat, data, length, out);
1527 default:
1528 break;
1529 }
1530#endif
1531
1532#if defined(WITH_DSP_FFMPEG)
1533 return freerdp_dsp_ffmpeg_decode(context, srcFormat, data, length, out);
1534#else
1535
1536 if (!context || context->common.encoder || !srcFormat || !data || !out)
1537 return FALSE;
1538
1539 switch (context->common.format.wFormatTag)
1540 {
1541 case WAVE_FORMAT_PCM:
1542 if (!Stream_EnsureRemainingCapacity(out, length))
1543 return FALSE;
1544
1545 Stream_Write(out, data, length);
1546 return TRUE;
1547
1548 case WAVE_FORMAT_ADPCM:
1549 return freerdp_dsp_decode_ms_adpcm(context, data, length, out);
1550
1551 case WAVE_FORMAT_DVI_ADPCM:
1552 return freerdp_dsp_decode_ima_adpcm(context, data, length, out);
1553#if defined(WITH_GSM)
1554
1555 case WAVE_FORMAT_GSM610:
1556 return freerdp_dsp_decode_gsm610(context, data, length, out);
1557#endif
1558#if defined(WITH_LAME)
1559
1560 case WAVE_FORMAT_MPEGLAYER3:
1561 return freerdp_dsp_decode_mp3(context, data, length, out);
1562#endif
1563#if defined(WITH_FAAD2)
1564
1565 case WAVE_FORMAT_AAC_MS:
1566 return freerdp_dsp_decode_faad(context, data, length, out);
1567#endif
1568
1569#if defined(WITH_OPUS)
1570 case WAVE_FORMAT_OPUS:
1571 return freerdp_dsp_decode_opus(context, data, length, out);
1572#endif
1573 default:
1574 return FALSE;
1575 }
1576
1577 return FALSE;
1578#endif
1579}
1580
1581BOOL freerdp_dsp_supports_format(const AUDIO_FORMAT* WINPR_RESTRICT format, BOOL encode)
1582{
1583#if defined(WITH_MEDIACODEC)
1584 if (format && (format->wFormatTag == WAVE_FORMAT_AAC_MS))
1585 return mediacodec_aac_dsp_supports_format(format, encode);
1586#endif
1587
1588#if defined(WITH_FDK_AAC)
1589 switch (format->wFormatTag)
1590 {
1591 case WAVE_FORMAT_AAC_MS:
1592 return TRUE;
1593 default:
1594 break;
1595 }
1596
1597#endif
1598
1599#if defined(WITH_DSP_FFMPEG)
1600 return freerdp_dsp_ffmpeg_supports_format(format, encode);
1601#else
1602
1603#if !defined(WITH_DSP_EXPERIMENTAL)
1604 WINPR_UNUSED(encode);
1605#endif
1606 switch (format->wFormatTag)
1607 {
1608 case WAVE_FORMAT_PCM:
1609 return TRUE;
1610#if defined(WITH_DSP_EXPERIMENTAL)
1611
1612 case WAVE_FORMAT_ADPCM:
1613 return FALSE;
1614 case WAVE_FORMAT_DVI_ADPCM:
1615 return TRUE;
1616#endif
1617#if defined(WITH_GSM)
1618
1619 case WAVE_FORMAT_GSM610:
1620#if defined(WITH_DSP_EXPERIMENTAL)
1621 return TRUE;
1622#else
1623 return !encode;
1624#endif
1625#endif
1626#if defined(WITH_LAME)
1627
1628 case WAVE_FORMAT_MPEGLAYER3:
1629#if defined(WITH_DSP_EXPERIMENTAL)
1630 return TRUE;
1631#else
1632 return !encode;
1633#endif
1634#endif
1635
1636 case WAVE_FORMAT_AAC_MS:
1637#if defined(WITH_FAAD2)
1638 if (!encode)
1639 return TRUE;
1640
1641#endif
1642#if defined(WITH_FAAC)
1643
1644 if (encode)
1645 return TRUE;
1646
1647#endif
1648#if defined(WITH_FDK_AAC)
1649 return TRUE;
1650#else
1651 return FALSE;
1652#endif
1653
1654#if defined(WITH_OPUS)
1655 case WAVE_FORMAT_OPUS:
1656 return opus_is_valid_samplerate(format);
1657#endif
1658 default:
1659 return FALSE;
1660 }
1661
1662 return FALSE;
1663#endif
1664}
1665
1666BOOL freerdp_dsp_context_reset(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
1667 const AUDIO_FORMAT* WINPR_RESTRICT targetFormat,
1668 UINT32 FramesPerPacket)
1669{
1670#if defined(WITH_MEDIACODEC)
1671 if (!context || !targetFormat)
1672 return FALSE;
1673
1674 if (targetFormat->wFormatTag == WAVE_FORMAT_AAC_MS)
1675 {
1677 mediacodec_aac_dsp_uninit(ctx);
1678 ctx->format = *targetFormat;
1679 return mediacodec_aac_dsp_init(ctx, FramesPerPacket);
1680 }
1681#endif
1682
1683#if defined(WITH_FDK_AAC)
1684 WINPR_ASSERT(targetFormat);
1685 if (targetFormat->wFormatTag == WAVE_FORMAT_AAC_MS)
1686 {
1688 fdk_aac_dsp_uninit(ctx);
1689 ctx->format = *targetFormat;
1690 return fdk_aac_dsp_init(ctx, FramesPerPacket);
1691 }
1692#endif
1693
1694#if defined(WITH_DSP_FFMPEG)
1695 return freerdp_dsp_ffmpeg_context_reset(context, targetFormat);
1696#else
1697
1698 if (!context || !targetFormat)
1699 return FALSE;
1700
1701 context->common.format = *targetFormat;
1702
1703 switch (context->common.format.wFormatTag)
1704 {
1705#if defined(WITH_LAME)
1706 case WAVE_FORMAT_MPEGLAYER3:
1707 if (!valid_mp3_format(context))
1708 return FALSE;
1709 break;
1710#endif
1711 case WAVE_FORMAT_ADPCM:
1712 if (!valid_ms_adpcm_format(context))
1713 return FALSE;
1714 break;
1715 case WAVE_FORMAT_DVI_ADPCM:
1716 {
1717 if (!valid_ima_adpcm_format(context))
1718 return FALSE;
1719 if (FramesPerPacket == 0)
1720 return FALSE;
1721
1722 const size_t min_frame_data = 1ull * context->common.format.wBitsPerSample *
1723 context->common.format.nChannels * FramesPerPacket;
1724 const size_t data_per_block = (1ULL * context->common.format.nBlockAlign -
1725 4ULL * context->common.format.nChannels) *
1726 8ULL;
1727 size_t nb_block_per_packet = min_frame_data / data_per_block;
1728
1729 if (min_frame_data % data_per_block)
1730 nb_block_per_packet++;
1731
1732 context->adpcm.ima.packet_size =
1733 nb_block_per_packet * context->common.format.nBlockAlign;
1734 if (!Stream_EnsureCapacity(context->common.buffer, context->adpcm.ima.packet_size))
1735 return FALSE;
1736 Stream_ResetPosition(context->common.buffer);
1737 }
1738 break;
1739 default:
1740 break;
1741 }
1742
1743#if defined(WITH_OPUS)
1744
1745 if (context->opus_decoder)
1746 opus_decoder_destroy(context->opus_decoder);
1747 context->opus_decoder = nullptr;
1748 if (context->opus_encoder)
1749 opus_encoder_destroy(context->opus_encoder);
1750 context->opus_encoder = nullptr;
1751
1752 if (context->common.format.wFormatTag == WAVE_FORMAT_OPUS)
1753 {
1754 if (!opus_is_valid_samplerate(&context->common.format))
1755 return FALSE;
1756
1757 if (!context->common.encoder)
1758 {
1759 int opus_error = OPUS_OK;
1760
1761 context->opus_decoder = opus_decoder_create(
1762 WINPR_ASSERTING_INT_CAST(opus_int32, context->common.format.nSamplesPerSec),
1763 context->common.format.nChannels, &opus_error);
1764 if (opus_error != OPUS_OK)
1765 return FALSE;
1766 }
1767 else
1768 {
1769 int opus_error = OPUS_OK;
1770
1771 context->opus_encoder = opus_encoder_create(
1772 WINPR_ASSERTING_INT_CAST(opus_int32, context->common.format.nSamplesPerSec),
1773 context->common.format.nChannels, OPUS_APPLICATION_VOIP, &opus_error);
1774 if (opus_error != OPUS_OK)
1775 return FALSE;
1776
1777 opus_error =
1778 opus_encoder_ctl(context->opus_encoder,
1779 OPUS_SET_BITRATE(context->common.format.nAvgBytesPerSec * 8));
1780 if (opus_error != OPUS_OK)
1781 return FALSE;
1782
1783 /* the packets the peer asked for if Opus allows them, else 20 ms */
1784 const UINT32 rate = context->common.format.nSamplesPerSec;
1785 context->opus_packet_frames =
1786 opus_is_packet_frames(rate, FramesPerPacket) ? FramesPerPacket : rate / 50;
1787 if (!context->opus_input)
1788 context->opus_input = Stream_New(nullptr, OPUS_MAX_FRAMES * sizeof(opus_int16) *
1789 context->common.format.nChannels);
1790 if (!context->opus_input)
1791 return FALSE;
1792 Stream_ResetPosition(context->opus_input);
1793 }
1794 }
1795
1796#endif
1797#if defined(WITH_FAAD2)
1798 context->faadSetup = FALSE;
1799#endif
1800#if defined(WITH_FAAC)
1801
1802 if (context->common.encoder)
1803 {
1804 faacEncConfigurationPtr cfg = nullptr;
1805
1806 if (context->faac)
1807 faacEncClose(context->faac);
1808
1809 context->faac = faacEncOpen(targetFormat->nSamplesPerSec, targetFormat->nChannels,
1810 &context->faacInputSamples, &context->faacMaxOutputBytes);
1811
1812 if (!context->faac)
1813 return FALSE;
1814
1815 cfg = faacEncGetCurrentConfiguration(context->faac);
1816 cfg->inputFormat = FAAC_INPUT_16BIT;
1817 cfg->outputFormat = 0;
1818 cfg->mpegVersion = MPEG4;
1819 cfg->useTns = 1;
1820 cfg->bandWidth = targetFormat->nAvgBytesPerSec;
1821 const int rc = faacEncSetConfiguration(context->faac, cfg);
1822 if (rc <= 0)
1823 return FALSE;
1824 }
1825
1826#endif
1827#if defined(WITH_SOXR)
1828 {
1829 soxr_io_spec_t iospec = soxr_io_spec(SOXR_INT16, SOXR_INT16);
1830 soxr_error_t error = nullptr;
1831
1832 soxr_delete(context->sox);
1833 context->sox =
1834 soxr_create(context->common.format.nSamplesPerSec, targetFormat->nSamplesPerSec,
1835 targetFormat->nChannels, &error, &iospec, nullptr, nullptr);
1836
1837 if (!context->sox || (error != nullptr))
1838 return FALSE;
1839 }
1840#endif
1841 return TRUE;
1842#endif
1843}
1844
1845BOOL freerdp_dsp_common_context_init(FREERDP_DSP_COMMON_CONTEXT* context, BOOL encode)
1846{
1847 WINPR_ASSERT(context);
1848 context->encoder = encode;
1849 context->buffer = Stream_New(nullptr, 1024);
1850 if (!context->buffer)
1851 goto fail;
1852
1853 context->channelmix = Stream_New(nullptr, 1024);
1854 if (!context->channelmix)
1855 goto fail;
1856
1857 context->resample = Stream_New(nullptr, 1024);
1858 if (!context->resample)
1859 goto fail;
1860
1861 return TRUE;
1862
1863fail:
1864 freerdp_dsp_common_context_uninit(context);
1865 return FALSE;
1866}
1867
1868void freerdp_dsp_common_context_uninit(FREERDP_DSP_COMMON_CONTEXT* context)
1869{
1870 WINPR_ASSERT(context);
1871
1872 Stream_Free(context->buffer, TRUE);
1873 Stream_Free(context->channelmix, TRUE);
1874 Stream_Free(context->resample, TRUE);
1875
1876 context->buffer = nullptr;
1877 context->channelmix = nullptr;
1878 context->resample = nullptr;
1879}