FreeRDP
Loading...
Searching...
No Matches
dsp.c
1
19
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{
97 FREERDP_DSP_COMMON_CONTEXT common;
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
238
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 if (srcFormat->wBitsPerSample != 16)
268 {
269 WLog_ERR(TAG, "requires 16 bit samples for input, got %" PRIu16, srcFormat->wBitsPerSample);
270 return FALSE;
271 }
272
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;
280 /* Integer rounding correct division */
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;
285
286 if (!Stream_EnsureCapacity(context->common.resample, rsize))
287 return FALSE;
288
289 size_t idone = 0;
290 size_t odone = 0;
291 soxr_error_t error =
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))
295 return FALSE;
296
297 *data = Stream_Buffer(context->common.resample);
298 *length = Stream_Length(context->common.resample);
299 return (error == nullptr);
300#else
301 WLog_ERR(TAG, "Missing resample support, recompile -DWITH_SOXR=ON or -DWITH_DSP_FFMPEG=ON");
302 return FALSE;
303#endif
304}
305
312
313static const INT16 ima_step_index_table[] = {
314 -1, -1, -1, -1, 2, 4, 6, 8, -1, -1, -1, -1, 2, 4, 6, 8
315};
316
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
325};
326
327static inline void dsp_ima_clamp_step(ADPCM* WINPR_RESTRICT adpcm, size_t channel)
328{
329 WINPR_ASSERT(adpcm);
330 if (adpcm->ima.last_step[channel] < 0)
331 adpcm->ima.last_step[channel] = 0;
332
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);
336}
337
338static UINT16 dsp_decode_ima_adpcm_sample(ADPCM* WINPR_RESTRICT adpcm, unsigned int channel,
339 BYTE sample)
340{
341 WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ima.last_step));
342 WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ima.last_sample));
343
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));
347
348 const INT32 ss = ima_step_size_table[offset];
349 INT32 d = (ss >> 3);
350
351 if (sample & 1)
352 d += (ss >> 2);
353
354 if (sample & 2)
355 d += (ss >> 1);
356
357 if (sample & 4)
358 d += ss;
359
360 if (sample & 8)
361 d = -d;
362
363 d += adpcm->ima.last_sample[channel];
364
365 if (d < -32768)
366 d = -32768;
367 else if (d > 32767)
368 d = 32767;
369
370 adpcm->ima.last_sample[channel] = (INT16)d;
371
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);
375
376 dsp_ima_clamp_step(adpcm, channel);
377
378 return (UINT16)d;
379}
380
381static BOOL valid_ima_adpcm_format(const FREERDP_DSP_CONTEXT* WINPR_RESTRICT context)
382{
383 WINPR_ASSERT(context);
384 if (context->common.format.wFormatTag != WAVE_FORMAT_DVI_ADPCM)
385 return FALSE;
386 if (context->common.format.nBlockAlign <= 4ULL)
387 return FALSE;
388 if (context->common.format.nChannels < 1)
389 return FALSE;
390 if (context->common.format.wBitsPerSample == 0)
391 return FALSE;
392 return TRUE;
393}
394
395static BOOL freerdp_dsp_decode_ima_adpcm(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
396 const BYTE* WINPR_RESTRICT src, size_t size,
397 wStream* WINPR_RESTRICT out)
398{
399 if (!valid_ima_adpcm_format(context))
400 return FALSE;
401
402 size_t out_size = size * 4ull;
403 const UINT32 block_size = context->common.format.nBlockAlign;
404 const UINT32 channels = context->common.format.nChannels;
405
406 if (!Stream_EnsureCapacity(out, out_size))
407 return FALSE;
408
409 while (size > 0)
410 {
411 if (size % block_size == 0)
412 {
413 if (size < 4)
414 return FALSE;
415
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));
419
420 dsp_ima_clamp_step(&context->adpcm, 0u);
421
422 src += 4;
423 size -= 4;
424 out_size -= 16;
425
426 if (channels > 1)
427 {
428 if (size < 4)
429 return FALSE;
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));
433
434 dsp_ima_clamp_step(&context->adpcm, 1u);
435 src += 4;
436 size -= 4;
437 out_size -= 16;
438 }
439 }
440
441 if (channels > 1)
442 {
443 if (size < 8)
444 return FALSE;
445 for (size_t i = 0; i < 8; i++)
446 {
447 BYTE* dst = Stream_Pointer(out);
448
449 const unsigned channel = (i < 4 ? 0 : 1);
450 {
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);
456 }
457 {
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);
463 }
464 src++;
465 }
466
467 if (!Stream_SafeSeek(out, 32))
468 return FALSE;
469 size -= 8;
470 }
471 else
472 {
473 if (size < 1)
474 return FALSE;
475 BYTE* dst = Stream_Pointer(out);
476 if (!Stream_SafeSeek(out, 4))
477 return FALSE;
478
479 {
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);
484 }
485 {
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);
490 }
491 src++;
492 size--;
493 }
494 }
495
496 return TRUE;
497}
498
499#if defined(WITH_GSM)
500#define GSM_FRAME_SAMPLES 160
501
502static BOOL freerdp_dsp_decode_gsm610(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
503 const BYTE* WINPR_RESTRICT src, size_t size,
504 wStream* WINPR_RESTRICT out)
505{
506 size_t offset = 0;
507
508 while (offset < size)
509 {
510 /* gsm_decode(GSM_OPT_WAV49) requires alternating fixed 33/32 bytes as input.
511 * It keeps track of that across calls, a previous call might have ended with 33 bytes.
512 * abort if the data supplied was inconsistent.
513 */
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)
517 return FALSE;
518
519 gsm_signal gsmBlockBuffer[GSM_FRAME_SAMPLES] = WINPR_C_ARRAY_INIT;
520 const int rc =
521 gsm_decode(context->gsm,
522 WINPR_CAST_CONST_PTR_AWAY(/* API does not modify */ &src[offset], gsm_byte*),
523 gsmBlockBuffer);
524
525 if (rc < 0)
526 return FALSE;
527
528 offset += toadd;
529
530 if (!Stream_EnsureRemainingCapacity(out, sizeof(gsmBlockBuffer)))
531 return FALSE;
532
533 Stream_Write(out, (void*)gsmBlockBuffer, sizeof(gsmBlockBuffer));
534 }
535
536 return TRUE;
537}
538
539/*
540 * gsm_encode() always reads GSM_FRAME_SAMPLES samples, but callers hand over what their capture
541 * device delivered. With GSM_OPT_WAV49 two frames of 32 and 33 bytes form one block (nBlockAlign)
542 * and every packet starts with a new block. Collect the samples, only encode whole blocks and keep
543 * the rest for the next call. Nothing is written until enough samples arrived.
544 */
545static BOOL freerdp_dsp_encode_gsm610(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
546 const BYTE* WINPR_RESTRICT src, size_t size,
547 wStream* WINPR_RESTRICT out)
548{
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;
552
553 if (!Stream_EnsureRemainingCapacity(input, size))
554 return FALSE;
555 Stream_Write(input, src, size);
556
557 const size_t blocks = Stream_GetPosition(input) / blockSize;
558 if (!Stream_EnsureRemainingCapacity(out, blocks * blockBytes))
559 return FALSE;
560
561 gsm_signal* signal = Stream_BufferAs(input, gsm_signal);
562 for (size_t x = 0; x < blocks; x++)
563 {
564 gsm_encode(context->gsm, signal, Stream_Pointer(out));
565 Stream_Seek(out, 32);
566 signal += GSM_FRAME_SAMPLES;
567
568 gsm_encode(context->gsm, signal, Stream_Pointer(out));
569 Stream_Seek(out, 33);
570 signal += GSM_FRAME_SAMPLES;
571 }
572
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);
577}
578#endif
579
580#if defined(WITH_LAME)
581static BOOL valid_mp3_format(const FREERDP_DSP_CONTEXT* WINPR_RESTRICT context)
582{
583 WINPR_ASSERT(context);
584 if (context->common.format.wFormatTag != WAVE_FORMAT_MPEGLAYER3)
585 return FALSE;
586 if (context->common.format.nChannels < 1)
587 return FALSE;
588 if (context->common.format.nChannels > 128) // Arbitrary upper bound, way out of anything in use
589 return FALSE;
590 if (context->common.format.wBitsPerSample == 0)
591 return FALSE;
592 if (context->common.format.nSamplesPerSec == 0)
593 return FALSE;
594 if (context->common.format.nSamplesPerSec > SIZE_MAX / 4ull / context->common.format.nChannels)
595 return FALSE;
596 return TRUE;
597}
598
599static BOOL freerdp_dsp_decode_mp3(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
600 const BYTE* WINPR_RESTRICT src, size_t size,
601 wStream* WINPR_RESTRICT out)
602{
603 /* constant from lame source. This is the definition used in hip_decode1_headersB as output
604 * buffer length.
605 */
606 const size_t OUTSIZE_CLIPPED = (4096 * sizeof(short));
607
608 if (!context || !src || !out)
609 return FALSE;
610 if (!valid_mp3_format(context))
611 return FALSE;
612
613 const size_t buffer_size = MAX(OUTSIZE_CLIPPED, 2ull * context->common.format.nChannels *
614 context->common.format.nSamplesPerSec);
615
616 if (!Stream_EnsureCapacity(context->common.buffer, 2ull * buffer_size))
617 return FALSE;
618
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(
623 context->hip,
624 WINPR_CAST_CONST_PTR_AWAY(/* API is not modifying content */ src, unsigned char*), size,
625 pcm_l, pcm_r, &header);
626
627 if ((rc <= 0) || (header.header_parsed != 1) || (header.samplerate <= 0) ||
628 (header.framesize <= 0))
629 return FALSE;
630
631 if ((size_t)header.samplerate != context->common.format.nSamplesPerSec)
632 return FALSE;
633
634 if (context->common.format.nChannels != 2)
635 return FALSE;
636
637 if (header.stereo != context->common.format.nChannels)
638 return FALSE;
639
640 if ((size_t)header.framesize > buffer_size / sizeof(short))
641 return FALSE;
642
643 if (!Stream_EnsureRemainingCapacity(out, (size_t)rc * context->common.format.nChannels * 2ull))
644 return FALSE;
645
646 for (int x = 0; x < rc; x++)
647 {
648 Stream_Write_UINT16(out, (UINT16)pcm_l[x]);
649 Stream_Write_UINT16(out, (UINT16)pcm_r[x]);
650 }
651
652 do
653 {
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))
657 return FALSE;
658
659 for (int x = 0; x < rc; x++)
660 {
661 Stream_Write_UINT16(out, (UINT16)pcm_l[x]);
662 Stream_Write_UINT16(out, (UINT16)pcm_r[x]);
663 }
664 } while (rc > 0);
665
666 return TRUE;
667}
668
669static BOOL freerdp_dsp_encode_mp3(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
670 const BYTE* WINPR_RESTRICT src, size_t size,
671 wStream* WINPR_RESTRICT out)
672{
673 if (!context || !src || !out)
674 return FALSE;
675
676 if (!valid_mp3_format(context))
677 return FALSE;
678
679 size_t samples_per_channel =
680 size / context->common.format.nChannels / context->common.format.wBitsPerSample / 8;
681
682 /* Ensure worst case buffer size for mp3 stream taken from LAME header */
683 if (!Stream_EnsureRemainingCapacity(out, 5ull / 4ull * samples_per_channel + 7200ull))
684 return FALSE;
685
686 samples_per_channel = size / 2 /* size of a sample */ / context->common.format.nChannels;
687 const size_t outLen = Stream_GetRemainingCapacity(out);
688 if ((outLen > INT_MAX) || (samples_per_channel > INT_MAX))
689 return FALSE;
690
691 const int rc = lame_encode_buffer_interleaved(
692 context->lame,
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));
696
697 if (rc < 0)
698 return FALSE;
699
700 Stream_Seek(out, (size_t)rc);
701 return TRUE;
702}
703#endif
704
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,
708 wStream* WINPR_RESTRICT out)
709{
710 const int16_t* inSamples = WINPR_PACKED_ALIGN_CAST(const int16_t*, src);
711
712 if (!context || !src || !out)
713 return FALSE;
714
715 if (context->common.format.wBitsPerSample != 16)
716 {
717 WLog_ERR(TAG, "Encoder requires wBitsPerSample == 16");
718 return FALSE;
719 }
720
721 /* The encoder is configured with FAAC_INPUT_16BIT */
722 const unsigned int bpp = context->common.format.wBitsPerSample / 8;
723 const size_t nrSamples = size / bpp;
724
725 if (!Stream_EnsureRemainingCapacity(context->common.buffer, nrSamples * sizeof(int16_t)))
726 return FALSE;
727
728 for (size_t x = 0; x < nrSamples; x++)
729 {
730 Stream_Write_INT16(context->common.buffer, inSamples[x]);
731 if (Stream_GetPosition(context->common.buffer) / bpp >= context->faacInputSamples)
732 {
733 if (!Stream_EnsureRemainingCapacity(out, context->faacMaxOutputBytes))
734 return FALSE;
735
736 const size_t outLen = Stream_GetRemainingCapacity(out);
737 if ((outLen > UINT_MAX) || (context->faacInputSamples > UINT_MAX))
738 return FALSE;
739 const int rc =
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));
743 if (rc < 0)
744 return FALSE;
745 if (rc > 0)
746 Stream_Seek(out, (size_t)rc);
747 Stream_ResetPosition(context->common.buffer);
748 }
749 }
750
751 return TRUE;
752}
753#endif
754
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,
758 wStream* WINPR_RESTRICT out)
759{
760 if (!context || !context->opus_decoder || !src || !out)
761 return FALSE;
762
763 /* Max packet duration is 120ms (5760 at 48KHz) */
764 const size_t max_size = OPUS_MAX_FRAMES * context->common.format.nChannels * sizeof(int16_t);
765 if (!Stream_EnsureRemainingCapacity(out, max_size))
766 return FALSE;
767
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);
771 if (frames < 0)
772 return FALSE;
773
774 Stream_Seek(out, (size_t)frames * context->common.format.nChannels * sizeof(int16_t));
775
776 return TRUE;
777}
778
779/* Packet durations opus_encode() takes, in units of 2.5 ms, longest first. */
780static const UINT32 opus_packet_units[] = { 24, 16, 8, 4, 2, 1 };
781
782static BOOL opus_is_packet_frames(UINT32 rate, size_t frames)
783{
784 for (size_t x = 0; x < ARRAYSIZE(opus_packet_units); x++)
785 {
786 if (frames == 1ull * rate * opus_packet_units[x] / 400)
787 return TRUE;
788 }
789 return FALSE;
790}
791
792/*
793 * opus_encode() only takes 2.5, 5, 10, 20, 40 or 60 ms of audio, but callers hand over what their
794 * capture device delivered. Collect the samples and encode the longest valid packet, at least
795 * opus_packet_frames long; keep the rest for the next call. Nothing is written until enough
796 * samples arrived.
797 */
798static BOOL freerdp_dsp_encode_opus(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
799 const BYTE* WINPR_RESTRICT src, size_t size,
800 wStream* WINPR_RESTRICT out)
801{
802 if (!context || !context->opus_encoder || !context->opus_input || !src || !out)
803 return FALSE;
804
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))
809 return FALSE;
810 Stream_Write(input, src, size);
811
812 const size_t available = Stream_GetPosition(input) / frame_bytes;
813 if (available < context->opus_packet_frames)
814 return TRUE;
815
816 size_t frames = 0;
817 for (size_t x = 0; x < ARRAYSIZE(opus_packet_units); x++)
818 {
819 frames = 1ull * rate * opus_packet_units[x] / 400;
820 if (frames <= available)
821 break;
822 }
823
824 /* Max packet duration is 120ms (5760 at 48KHz) */
825 const size_t max_size = OPUS_MAX_FRAMES * context->common.format.nChannels * sizeof(int16_t);
826 if (!Stream_EnsureRemainingCapacity(out, max_size))
827 return FALSE;
828
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));
833 if (bytes < 0)
834 return FALSE;
835 Stream_Seek(out, (size_t)bytes);
836
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);
841}
842#endif
843
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,
847 wStream* WINPR_RESTRICT out)
848{
849 NeAACDecFrameInfo info;
850 size_t offset = 0;
851
852 if (!context || !src || !out)
853 return FALSE;
854
855 if (!context->faadSetup)
856 {
857 union
858 {
859 const void* cpv;
860 void* pv;
861 } cnv;
862 unsigned long samplerate = 0;
863 unsigned char channels = 0;
864
865 cnv.cpv = src;
866 const long err = NeAACDecInit(context->faad, /* API is not modifying content */ cnv.pv,
867 size, &samplerate, &channels);
868
869 if (err != 0)
870 return FALSE;
871
872 if (channels != context->common.format.nChannels)
873 return FALSE;
874
875 if (samplerate != context->common.format.nSamplesPerSec)
876 return FALSE;
877
878 if (context->common.format.wBitsPerSample != 16)
879 {
880 WLog_ERR(TAG, "Encoder requires wBitsPerSample == 16");
881 return FALSE;
882 }
883
884 context->faadSetup = TRUE;
885 }
886
887 while (offset < size)
888 {
889 union
890 {
891 const void* cpv;
892 void* pv;
893 } cnv;
894
895 const size_t outSize = context->common.format.nSamplesPerSec *
896 context->common.format.nChannels *
897 context->common.format.wBitsPerSample / 8;
898
899 if (!Stream_EnsureRemainingCapacity(out, outSize))
900 return FALSE;
901
902 void* sample_buffer = Stream_Pointer(out);
903
904 cnv.cpv = &src[offset];
905 NeAACDecDecode2(context->faad, &info, cnv.pv, size - offset, &sample_buffer,
906 Stream_GetRemainingCapacity(out));
907
908 if (info.error != 0)
909 return FALSE;
910
911 offset += info.bytesconsumed;
912
913 if (info.samples == 0)
914 continue;
915
916 Stream_Seek(out, info.samples * context->common.format.wBitsPerSample / 8);
917 }
918
919 return TRUE;
920}
921
922#endif
923
931static const struct
932{
933 BYTE byte_num;
934 BYTE byte_shift;
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 } };
938
939static BYTE dsp_encode_ima_adpcm_sample(ADPCM* WINPR_RESTRICT adpcm, size_t channel, INT16 sample)
940{
941 WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ima.last_step));
942 WINPR_ASSERT(channel < ARRAYSIZE(adpcm->ima.last_sample));
943
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));
947
948 INT32 ss = ima_step_size_table[offset];
949 INT32 e = sample - adpcm->ima.last_sample[channel];
950 INT32 d = e;
951 INT32 diff = ss >> 3;
952 BYTE enc = 0;
953
954 if (e < 0)
955 {
956 enc = 8;
957 e = -e;
958 }
959
960 if (e >= ss)
961 {
962 enc |= 4;
963 e -= ss;
964 }
965
966 ss >>= 1;
967
968 if (e >= ss)
969 {
970 enc |= 2;
971 e -= ss;
972 }
973
974 ss >>= 1;
975
976 if (e >= ss)
977 {
978 enc |= 1;
979 e -= ss;
980 }
981
982 if (d < 0)
983 diff = d + e - diff;
984 else
985 diff = d - e + diff;
986
987 diff += adpcm->ima.last_sample[channel];
988
989 if (diff < -32768)
990 diff = -32768;
991 else if (diff > 32767)
992 diff = 32767;
993
994 adpcm->ima.last_sample[channel] = (INT16)diff;
995
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);
999
1000 dsp_ima_clamp_step(adpcm, channel);
1001
1002 return enc;
1003}
1004
1005static BOOL freerdp_dsp_encode_ima_adpcm(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
1006 const BYTE* WINPR_RESTRICT src, size_t size,
1007 wStream* WINPR_RESTRICT out)
1008{
1009 if (!valid_ima_adpcm_format(context))
1010 return FALSE;
1011 if (!Stream_EnsureRemainingCapacity(out, size))
1012 return FALSE;
1013 if (!Stream_EnsureRemainingCapacity(context->common.buffer, size + 64ull))
1014 return FALSE;
1015
1016 const size_t align = (context->common.format.nChannels > 1) ? 32 : 4;
1017
1018 while (size >= align)
1019 {
1020 if (Stream_GetPosition(context->common.buffer) % context->common.format.nBlockAlign == 0)
1021 {
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);
1027
1028 if (context->common.format.nChannels > 1)
1029 {
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);
1036 }
1037 }
1038
1039 if (context->common.format.nChannels > 1)
1040 {
1041 BYTE* dst = Stream_Pointer(context->common.buffer);
1042 ZeroMemory(dst, 8);
1043
1044 for (size_t i = 0; i < 16; i++)
1045 {
1046 const INT16 sample = (INT16)(((UINT16)(*src)) | (((UINT16)(*(src + 1))) << 8));
1047 src += 2;
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;
1051 }
1052
1053 if (!Stream_SafeSeek(context->common.buffer, 8))
1054 return FALSE;
1055 size -= 32;
1056 }
1057 else
1058 {
1059 INT16 sample = (INT16)(((UINT16)(*src)) | (((UINT16)(*(src + 1))) << 8));
1060 src += 2;
1061 BYTE encoded = dsp_encode_ima_adpcm_sample(&context->adpcm, 0, sample);
1062 sample = (INT16)(((UINT16)(*src)) | (((UINT16)(*(src + 1))) << 8));
1063 src += 2;
1064 encoded |= dsp_encode_ima_adpcm_sample(&context->adpcm, 0, sample) << 4;
1065 Stream_Write_UINT8(context->common.buffer, encoded);
1066 size -= 4;
1067 }
1068
1069 if (Stream_GetPosition(context->common.buffer) >= context->adpcm.ima.packet_size)
1070 {
1071 const BYTE* bsrc = Stream_Buffer(context->common.buffer);
1072 if (!Stream_EnsureRemainingCapacity(context->common.buffer,
1073 context->adpcm.ima.packet_size))
1074 return FALSE;
1075 Stream_Write(out, bsrc, context->adpcm.ima.packet_size);
1076 Stream_ResetPosition(context->common.buffer);
1077 }
1078 }
1079
1080 return TRUE;
1081}
1082
1088
1089static const INT32 ms_adpcm_adaptation_table[] = { 230, 230, 230, 230, 307, 409, 512, 614,
1090 768, 614, 512, 409, 307, 230, 230, 230 };
1091
1092static const INT32 ms_adpcm_coeffs1[7] = { 256, 512, 0, 192, 240, 460, 392 };
1093
1094static const INT32 ms_adpcm_coeffs2[7] = { 0, -256, 0, 64, 0, -208, -232 };
1095
1096static inline INT32 saturated_multiply(INT32 a, INT32 b)
1097{
1098 WINPR_ASSERT(b > 0);
1099 if (a > INT32_MAX / b)
1100 return INT32_MAX;
1101 if (a < INT32_MIN / b)
1102 return INT32_MIN;
1103 return a * b;
1104}
1105
1106static inline INT16 freerdp_dsp_decode_ms_adpcm_sample(ADPCM* WINPR_RESTRICT adpcm, BYTE sample,
1107 size_t channel)
1108{
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));
1113
1114 const INT8 nibble = (INT8)((sample & 0x08) ? (sample - 16) : sample);
1115 const BYTE predictor = adpcm->ms.predictor[channel];
1116 INT32 coeff1 = 0;
1117 if (predictor < ARRAYSIZE(ms_adpcm_coeffs1))
1118 coeff1 = ms_adpcm_coeffs1[predictor];
1119
1120 INT32 coeff2 = 0;
1121 if (predictor < ARRAYSIZE(ms_adpcm_coeffs2))
1122 coeff2 = ms_adpcm_coeffs2[predictor];
1123 INT32 presample =
1124 ((adpcm->ms.sample1[channel] * coeff1) + (adpcm->ms.sample2[channel] * coeff2)) / 256;
1125 presample += nibble * adpcm->ms.delta[channel];
1126
1127 if (presample > 32767)
1128 presample = 32767;
1129 else if (presample < -32768)
1130 presample = -32768;
1131
1132 adpcm->ms.sample2[channel] = adpcm->ms.sample1[channel];
1133 adpcm->ms.sample1[channel] = presample;
1134
1135 INT32 tableval = 0;
1136 if (sample < ARRAYSIZE(ms_adpcm_adaptation_table))
1137 tableval = ms_adpcm_adaptation_table[sample];
1138
1139 adpcm->ms.delta[channel] = saturated_multiply(adpcm->ms.delta[channel], tableval) / 256;
1140
1141 if (adpcm->ms.delta[channel] < 16)
1142 adpcm->ms.delta[channel] = 16;
1143
1144 return (INT16)presample;
1145}
1146
1147static BOOL valid_ms_adpcm_format(const FREERDP_DSP_CONTEXT* WINPR_RESTRICT context)
1148{
1149 WINPR_ASSERT(context);
1150 if (context->common.format.wFormatTag != WAVE_FORMAT_ADPCM)
1151 return FALSE;
1152 if (context->common.format.nBlockAlign <= 4ULL)
1153 return FALSE;
1154 if (context->common.format.nChannels < 1)
1155 return FALSE;
1156 if (context->common.format.wBitsPerSample == 0)
1157 return FALSE;
1158 return TRUE;
1159}
1160
1161static BOOL freerdp_dsp_decode_ms_adpcm(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
1162 const BYTE* WINPR_RESTRICT src, size_t size,
1163 wStream* WINPR_RESTRICT out)
1164{
1165 if (!valid_ms_adpcm_format(context))
1166 return FALSE;
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;
1170
1171 if (!Stream_EnsureCapacity(out, out_size))
1172 return FALSE;
1173
1174 while (size > 0)
1175 {
1176 if (size % block_size == 0)
1177 {
1178 if (channels > 1)
1179 {
1180 if (size < 14)
1181 return FALSE;
1182
1183 context->adpcm.ms.predictor[0] = *src++;
1184 context->adpcm.ms.predictor[1] = *src++;
1185 context->adpcm.ms.delta[0] = read_int16(src);
1186 src += 2;
1187 context->adpcm.ms.delta[1] = read_int16(src);
1188 src += 2;
1189 context->adpcm.ms.sample1[0] = read_int16(src);
1190 src += 2;
1191 context->adpcm.ms.sample1[1] = read_int16(src);
1192 src += 2;
1193 context->adpcm.ms.sample2[0] = read_int16(src);
1194 src += 2;
1195 context->adpcm.ms.sample2[1] = read_int16(src);
1196 src += 2;
1197 size -= 14;
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]);
1202 }
1203 else
1204 {
1205 if (size < 7)
1206 return FALSE;
1207
1208 context->adpcm.ms.predictor[0] = *src++;
1209 context->adpcm.ms.delta[0] = read_int16(src);
1210 src += 2;
1211 context->adpcm.ms.sample1[0] = read_int16(src);
1212 src += 2;
1213 context->adpcm.ms.sample2[0] = read_int16(src);
1214 src += 2;
1215 size -= 7;
1216 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample2[0]);
1217 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample1[0]);
1218 }
1219 }
1220
1221 if (channels > 1)
1222 {
1223 {
1224 if (size < 1)
1225 return FALSE;
1226 const BYTE sample = *src++;
1227 size--;
1228 Stream_Write_INT16(
1229 out, freerdp_dsp_decode_ms_adpcm_sample(&context->adpcm, sample >> 4, 0));
1230 Stream_Write_INT16(
1231 out, freerdp_dsp_decode_ms_adpcm_sample(&context->adpcm, sample & 0x0F, 1));
1232 }
1233 {
1234 if (size < 1)
1235 return FALSE;
1236 const BYTE sample = *src++;
1237 size--;
1238 Stream_Write_INT16(
1239 out, freerdp_dsp_decode_ms_adpcm_sample(&context->adpcm, sample >> 4, 0));
1240 Stream_Write_INT16(
1241 out, freerdp_dsp_decode_ms_adpcm_sample(&context->adpcm, sample & 0x0F, 1));
1242 }
1243 }
1244 else
1245 {
1246 if (size < 1)
1247 return FALSE;
1248 const BYTE sample = *src++;
1249 size--;
1250 Stream_Write_INT16(out,
1251 freerdp_dsp_decode_ms_adpcm_sample(&context->adpcm, sample >> 4, 0));
1252 Stream_Write_INT16(
1253 out, freerdp_dsp_decode_ms_adpcm_sample(&context->adpcm, sample & 0x0F, 0));
1254 }
1255 }
1256
1257 return TRUE;
1258}
1259
1260static BYTE freerdp_dsp_encode_ms_adpcm_sample(ADPCM* WINPR_RESTRICT adpcm, INT32 sample,
1261 size_t channel)
1262{
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));
1267
1268 INT32 presample =
1269 ((adpcm->ms.sample1[channel] * ms_adpcm_coeffs1[adpcm->ms.predictor[channel]]) +
1270 (adpcm->ms.sample2[channel] * ms_adpcm_coeffs2[adpcm->ms.predictor[channel]])) /
1271 256;
1272 INT32 errordelta = (sample - presample) / adpcm->ms.delta[channel];
1273
1274 if ((sample - presample) % adpcm->ms.delta[channel] > adpcm->ms.delta[channel] / 2)
1275 errordelta++;
1276
1277 if (errordelta > 7)
1278 errordelta = 7;
1279 else if (errordelta < -8)
1280 errordelta = -8;
1281
1282 presample += adpcm->ms.delta[channel] * errordelta;
1283
1284 if (presample > 32767)
1285 presample = 32767;
1286 else if (presample < -32768)
1287 presample = -32768;
1288
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;
1294
1295 if (adpcm->ms.delta[channel] < 16)
1296 adpcm->ms.delta[channel] = 16;
1297
1298 return ((BYTE)errordelta) & 0x0F;
1299}
1300
1301static BOOL freerdp_dsp_encode_ms_adpcm(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
1302 const BYTE* WINPR_RESTRICT src, size_t size,
1303 wStream* WINPR_RESTRICT out)
1304{
1305 if (!valid_ms_adpcm_format(context))
1306 return FALSE;
1307
1308 const size_t step = 8 + ((context->common.format.nChannels > 1) ? 4 : 0);
1309
1310 if (!Stream_EnsureRemainingCapacity(out, size))
1311 return FALSE;
1312
1313 const size_t start = Stream_GetPosition(out);
1314
1315 if (context->adpcm.ms.delta[0] < 16)
1316 context->adpcm.ms.delta[0] = 16;
1317
1318 if (context->adpcm.ms.delta[1] < 16)
1319 context->adpcm.ms.delta[1] = 16;
1320
1321 while (size >= step)
1322 {
1323 if ((Stream_GetPosition(out) - start) % context->common.format.nBlockAlign == 0)
1324 {
1325 if (context->common.format.nChannels > 1)
1326 {
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));
1333
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);
1338
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]);
1343
1344 src += 8;
1345 size -= 8;
1346 }
1347 else
1348 {
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));
1352
1353 context->adpcm.ms.sample1[0] = read_int16(src + 2);
1354 context->adpcm.ms.sample2[0] = read_int16(src + 0);
1355
1356 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample1[0]);
1357 Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample2[0]);
1358 src += 4;
1359 size -= 4;
1360 }
1361 }
1362
1363 {
1364 const INT16 sample = read_int16(src);
1365 src += 2;
1366 Stream_Write_UINT8(
1367 out, (freerdp_dsp_encode_ms_adpcm_sample(&context->adpcm, sample, 0) << 4) & 0xFF);
1368 }
1369 {
1370 const INT16 sample = read_int16(src);
1371 src += 2;
1372
1373 BYTE val = 0;
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);
1378 }
1379 size -= 4;
1380 }
1381
1382 return TRUE;
1383}
1384
1385#endif
1386
1387FREERDP_DSP_CONTEXT* freerdp_dsp_context_new(BOOL encoder)
1388{
1389#if defined(WITH_DSP_FFMPEG)
1390 return freerdp_dsp_ffmpeg_context_new(encoder);
1391#else
1392 FREERDP_DSP_CONTEXT* context = calloc(1, sizeof(FREERDP_DSP_CONTEXT));
1393
1394 if (!context)
1395 return nullptr;
1396
1397 if (!freerdp_dsp_common_context_init(&context->common, encoder))
1398 goto fail;
1399
1400#if defined(WITH_GSM)
1401 context->gsm = gsm_create();
1402
1403 if (!context->gsm)
1404 goto fail;
1405
1406 {
1407 int val = 1;
1408 int rc = gsm_option(context->gsm, GSM_OPT_WAV49, &val);
1409
1410 if (rc < 0)
1411 goto fail;
1412 }
1413#endif
1414#if defined(WITH_LAME)
1415
1416 if (encoder)
1417 {
1418 context->lame = lame_init();
1419
1420 if (!context->lame)
1421 goto fail;
1422 }
1423 else
1424 {
1425 context->hip = hip_decode_init();
1426
1427 if (!context->hip)
1428 goto fail;
1429 }
1430
1431#endif
1432#if defined(WITH_FAAD2)
1433
1434 if (!encoder)
1435 {
1436 context->faad = NeAACDecOpen();
1437
1438 if (!context->faad)
1439 goto fail;
1440 }
1441
1442#endif
1443 return context;
1444fail:
1445 freerdp_dsp_context_free(context);
1446 return nullptr;
1447#endif
1448}
1449
1450void freerdp_dsp_context_free(FREERDP_DSP_CONTEXT* context)
1451{
1452 if (!context)
1453 return;
1454
1455#if defined(WITH_FDK_AAC)
1457 WINPR_ASSERT(ctx);
1458 fdk_aac_dsp_uninit(ctx);
1459#endif
1460
1461#if defined(WITH_MEDIACODEC)
1462 mediacodec_aac_dsp_uninit((FREERDP_DSP_COMMON_CONTEXT*)context);
1463#endif
1464
1465#if defined(WITH_DSP_FFMPEG)
1466 freerdp_dsp_ffmpeg_context_free(context);
1467#else
1468
1469 freerdp_dsp_common_context_uninit(&context->common);
1470
1471#if defined(WITH_GSM)
1472 gsm_destroy(context->gsm);
1473#endif
1474#if defined(WITH_LAME)
1475
1476 if (context->common.encoder)
1477 lame_close(context->lame);
1478 else
1479 hip_decode_exit(context->hip);
1480
1481#endif
1482#if defined(WITH_OPUS)
1483
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);
1489
1490#endif
1491#if defined(WITH_FAAD2)
1492
1493 if (!context->common.encoder)
1494 NeAACDecClose(context->faad);
1495
1496#endif
1497#if defined(WITH_FAAC)
1498
1499 if (context->faac)
1500 faacEncClose(context->faac);
1501
1502#endif
1503#if defined(WITH_SOXR)
1504 soxr_delete(context->sox);
1505#endif
1506 free(context);
1507
1508#endif
1509}
1510
1511BOOL freerdp_dsp_encode(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
1512 const AUDIO_FORMAT* WINPR_RESTRICT srcFormat,
1513 const BYTE* WINPR_RESTRICT pdata, size_t length,
1514 wStream* WINPR_RESTRICT out)
1515{
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);
1520#endif
1521
1522#if defined(WITH_FDK_AAC)
1524 WINPR_ASSERT(ctx);
1525 switch (ctx->format.wFormatTag)
1526 {
1527 case WAVE_FORMAT_AAC_MS:
1528 return fdk_aac_dsp_encode(ctx, srcFormat, pdata, length, out);
1529 default:
1530 break;
1531 }
1532#endif
1533
1534#if defined(WITH_DSP_FFMPEG)
1535 return freerdp_dsp_ffmpeg_encode(context, srcFormat, pdata, length, out);
1536#else
1537 if (!context || !context->common.encoder || !srcFormat || !pdata || !out)
1538 return FALSE;
1539
1540 AUDIO_FORMAT format = *srcFormat;
1541 const BYTE* resampleData = nullptr;
1542 size_t resampleLength = 0;
1543
1544 if (!freerdp_dsp_channel_mix(context, pdata, length, srcFormat, &resampleData, &resampleLength))
1545 return FALSE;
1546
1547 format.nChannels = context->common.format.nChannels;
1548
1549 const BYTE* data = nullptr;
1550 if (!freerdp_dsp_resample(context, resampleData, resampleLength, &format, &data, &length))
1551 return FALSE;
1552
1553 switch (context->common.format.wFormatTag)
1554 {
1555 case WAVE_FORMAT_PCM:
1556 if (!Stream_EnsureRemainingCapacity(out, length))
1557 return FALSE;
1558
1559 Stream_Write(out, data, length);
1560 return TRUE;
1561
1562 case WAVE_FORMAT_ADPCM:
1563 return freerdp_dsp_encode_ms_adpcm(context, data, length, out);
1564
1565 case WAVE_FORMAT_DVI_ADPCM:
1566 return freerdp_dsp_encode_ima_adpcm(context, data, length, out);
1567#if defined(WITH_GSM)
1568
1569 case WAVE_FORMAT_GSM610:
1570 return freerdp_dsp_encode_gsm610(context, data, length, out);
1571#endif
1572#if defined(WITH_LAME)
1573
1574 case WAVE_FORMAT_MPEGLAYER3:
1575 return freerdp_dsp_encode_mp3(context, data, length, out);
1576#endif
1577#if defined(WITH_FAAC)
1578
1579 case WAVE_FORMAT_AAC_MS:
1580 return freerdp_dsp_encode_faac(context, data, length, out);
1581#endif
1582#if defined(WITH_OPUS)
1583
1584 case WAVE_FORMAT_OPUS:
1585 return freerdp_dsp_encode_opus(context, data, length, out);
1586#endif
1587 default:
1588 return FALSE;
1589 }
1590
1591 return FALSE;
1592#endif
1593}
1594
1595BOOL freerdp_dsp_decode(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
1596 const AUDIO_FORMAT* WINPR_RESTRICT srcFormat,
1597 const BYTE* WINPR_RESTRICT data, size_t length, wStream* WINPR_RESTRICT out)
1598{
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);
1603#endif
1604
1605#if defined(WITH_FDK_AAC)
1607 WINPR_ASSERT(ctx);
1608 switch (ctx->format.wFormatTag)
1609 {
1610 case WAVE_FORMAT_AAC_MS:
1611 return fdk_aac_dsp_decode(ctx, srcFormat, data, length, out);
1612 default:
1613 break;
1614 }
1615#endif
1616
1617#if defined(WITH_DSP_FFMPEG)
1618 return freerdp_dsp_ffmpeg_decode(context, srcFormat, data, length, out);
1619#else
1620
1621 if (!context || context->common.encoder || !srcFormat || !data || !out)
1622 return FALSE;
1623
1624 switch (context->common.format.wFormatTag)
1625 {
1626 case WAVE_FORMAT_PCM:
1627 if (!Stream_EnsureRemainingCapacity(out, length))
1628 return FALSE;
1629
1630 Stream_Write(out, data, length);
1631 return TRUE;
1632
1633 case WAVE_FORMAT_ADPCM:
1634 return freerdp_dsp_decode_ms_adpcm(context, data, length, out);
1635
1636 case WAVE_FORMAT_DVI_ADPCM:
1637 return freerdp_dsp_decode_ima_adpcm(context, data, length, out);
1638#if defined(WITH_GSM)
1639
1640 case WAVE_FORMAT_GSM610:
1641 return freerdp_dsp_decode_gsm610(context, data, length, out);
1642#endif
1643#if defined(WITH_LAME)
1644
1645 case WAVE_FORMAT_MPEGLAYER3:
1646 return freerdp_dsp_decode_mp3(context, data, length, out);
1647#endif
1648#if defined(WITH_FAAD2)
1649
1650 case WAVE_FORMAT_AAC_MS:
1651 return freerdp_dsp_decode_faad(context, data, length, out);
1652#endif
1653
1654#if defined(WITH_OPUS)
1655 case WAVE_FORMAT_OPUS:
1656 return freerdp_dsp_decode_opus(context, data, length, out);
1657#endif
1658 default:
1659 return FALSE;
1660 }
1661
1662 return FALSE;
1663#endif
1664}
1665
1666BOOL freerdp_dsp_supports_format(const AUDIO_FORMAT* WINPR_RESTRICT format, BOOL encode)
1667{
1668 WINPR_ASSERT(format);
1669 if (!audio_format_is_valid(format))
1670 return FALSE;
1671
1672#if defined(WITH_MEDIACODEC)
1673 if (format && (format->wFormatTag == WAVE_FORMAT_AAC_MS))
1674 return mediacodec_aac_dsp_supports_format(format, encode);
1675#endif
1676
1677#if defined(WITH_FDK_AAC)
1678 switch (format->wFormatTag)
1679 {
1680 case WAVE_FORMAT_AAC_MS:
1681 return TRUE;
1682 default:
1683 break;
1684 }
1685
1686#endif
1687
1688#if defined(WITH_DSP_FFMPEG)
1689 return freerdp_dsp_ffmpeg_supports_format(format, encode);
1690#else
1691
1692#if !defined(WITH_DSP_EXPERIMENTAL)
1693 WINPR_UNUSED(encode);
1694#endif
1695 switch (format->wFormatTag)
1696 {
1697 case WAVE_FORMAT_PCM:
1698 return TRUE;
1699#if defined(WITH_DSP_EXPERIMENTAL)
1700
1701 case WAVE_FORMAT_ADPCM:
1702 return FALSE;
1703 case WAVE_FORMAT_DVI_ADPCM:
1704 return TRUE;
1705#endif
1706#if defined(WITH_GSM)
1707
1708 case WAVE_FORMAT_GSM610:
1709#if defined(WITH_DSP_EXPERIMENTAL)
1710 return TRUE;
1711#else
1712 return !encode;
1713#endif
1714#endif
1715#if defined(WITH_LAME)
1716
1717 case WAVE_FORMAT_MPEGLAYER3:
1718#if defined(WITH_DSP_EXPERIMENTAL)
1719 return TRUE;
1720#else
1721 return !encode;
1722#endif
1723#endif
1724
1725 case WAVE_FORMAT_AAC_MS:
1726#if defined(WITH_FAAD2)
1727 if (!encode)
1728 return TRUE;
1729
1730#endif
1731#if defined(WITH_FAAC)
1732
1733 if (encode)
1734 return TRUE;
1735
1736#endif
1737#if defined(WITH_FDK_AAC)
1738 return TRUE;
1739#else
1740 return FALSE;
1741#endif
1742
1743#if defined(WITH_OPUS)
1744 case WAVE_FORMAT_OPUS:
1745 return opus_is_valid_samplerate(format);
1746#endif
1747 default:
1748 return FALSE;
1749 }
1750
1751 return FALSE;
1752#endif
1753}
1754
1755BOOL freerdp_dsp_context_reset(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
1756 const AUDIO_FORMAT* WINPR_RESTRICT targetFormat,
1757 WINPR_ATTR_UNUSED UINT32 FramesPerPacket)
1758{
1759 if (!context || !targetFormat)
1760 return FALSE;
1761 if (!audio_format_is_valid(targetFormat))
1762 return FALSE;
1763
1764 if (targetFormat->wFormatTag == WAVE_FORMAT_AAC_MS)
1765 {
1766#if defined(WITH_MEDIACODEC)
1768 mediacodec_aac_dsp_uninit(ctx);
1769 ctx->format = *targetFormat;
1770 return mediacodec_aac_dsp_init(ctx, FramesPerPacket);
1771#endif
1772
1773#if defined(WITH_FDK_AAC)
1775 fdk_aac_dsp_uninit(ctx);
1776 ctx->format = *targetFormat;
1777 return fdk_aac_dsp_init(ctx, FramesPerPacket);
1778#endif
1779 }
1780
1781#if defined(WITH_DSP_FFMPEG)
1782 return freerdp_dsp_ffmpeg_context_reset(context, targetFormat);
1783#else
1784 ZeroMemory(&context->adpcm, sizeof(context->adpcm));
1785 context->common.format = *targetFormat;
1786
1787 switch (context->common.format.wFormatTag)
1788 {
1789#if defined(WITH_LAME)
1790 case WAVE_FORMAT_MPEGLAYER3:
1791 if (!valid_mp3_format(context))
1792 return FALSE;
1793 break;
1794#endif
1795 case WAVE_FORMAT_ADPCM:
1796 if (!valid_ms_adpcm_format(context))
1797 return FALSE;
1798 break;
1799 case WAVE_FORMAT_DVI_ADPCM:
1800 {
1801 if (!valid_ima_adpcm_format(context))
1802 return FALSE;
1803 if (FramesPerPacket == 0)
1804 return FALSE;
1805
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) *
1810 8ULL;
1811 size_t nb_block_per_packet = min_frame_data / data_per_block;
1812
1813 if (min_frame_data % data_per_block)
1814 nb_block_per_packet++;
1815
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))
1819 return FALSE;
1820 Stream_ResetPosition(context->common.buffer);
1821 }
1822 break;
1823#if defined(WITH_GSM)
1824 case WAVE_FORMAT_GSM610:
1825 {
1826 /* start with a new block */
1827 int index = 0;
1828 if (gsm_option(context->gsm, GSM_OPT_FRAME_INDEX, &index) < 0)
1829 return FALSE;
1830 Stream_ResetPosition(context->common.buffer);
1831 }
1832 break;
1833#endif
1834 default:
1835 break;
1836 }
1837
1838#if defined(WITH_OPUS)
1839
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;
1846
1847 if (context->common.format.wFormatTag == WAVE_FORMAT_OPUS)
1848 {
1849 if (!opus_is_valid_samplerate(&context->common.format))
1850 return FALSE;
1851
1852 if (!context->common.encoder)
1853 {
1854 int opus_error = OPUS_OK;
1855
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)
1860 return FALSE;
1861 }
1862 else
1863 {
1864 int opus_error = OPUS_OK;
1865
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)
1870 return FALSE;
1871
1872 opus_error =
1873 opus_encoder_ctl(context->opus_encoder,
1874 OPUS_SET_BITRATE(context->common.format.nAvgBytesPerSec * 8));
1875 if (opus_error != OPUS_OK)
1876 return FALSE;
1877
1878 /* the packets the peer asked for if Opus allows them, else 20 ms */
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)
1886 return FALSE;
1887 Stream_ResetPosition(context->opus_input);
1888 }
1889 }
1890
1891#endif
1892#if defined(WITH_FAAD2)
1893 context->faadSetup = FALSE;
1894#endif
1895#if defined(WITH_FAAC)
1896
1897 if (context->common.encoder)
1898 {
1899 faacEncConfigurationPtr cfg = nullptr;
1900
1901 if (targetFormat->wBitsPerSample != 16)
1902 return FALSE;
1903
1904 if (context->faac)
1905 faacEncClose(context->faac);
1906
1907 context->faac = faacEncOpen(targetFormat->nSamplesPerSec, targetFormat->nChannels,
1908 &context->faacInputSamples, &context->faacMaxOutputBytes);
1909
1910 if (!context->faac)
1911 return FALSE;
1912
1913 cfg = faacEncGetCurrentConfiguration(context->faac);
1914 cfg->inputFormat = FAAC_INPUT_16BIT;
1915 cfg->outputFormat = 0;
1916 cfg->mpegVersion = MPEG4;
1917 cfg->useTns = 1;
1918 cfg->bandWidth = targetFormat->nAvgBytesPerSec;
1919 const int rc = faacEncSetConfiguration(context->faac, cfg);
1920 if (rc <= 0)
1921 return FALSE;
1922 }
1923
1924#endif
1925#if defined(WITH_SOXR)
1926 {
1927 soxr_io_spec_t iospec = soxr_io_spec(SOXR_INT16, SOXR_INT16);
1928 soxr_error_t error = nullptr;
1929
1930 if (context->common.format.wBitsPerSample != 16)
1931 return FALSE;
1932
1933 soxr_delete(context->sox);
1934 context->sox =
1935 soxr_create(context->common.format.nSamplesPerSec, targetFormat->nSamplesPerSec,
1936 targetFormat->nChannels, &error, &iospec, nullptr, nullptr);
1937
1938 if (!context->sox || (error != nullptr))
1939 return FALSE;
1940 }
1941#endif
1942 return TRUE;
1943#endif
1944}
1945
1946BOOL freerdp_dsp_common_context_init(FREERDP_DSP_COMMON_CONTEXT* context, BOOL encode)
1947{
1948 WINPR_ASSERT(context);
1949 context->encoder = encode;
1950 context->buffer = Stream_New(nullptr, 1024);
1951 if (!context->buffer)
1952 goto fail;
1953
1954 context->channelmix = Stream_New(nullptr, 1024);
1955 if (!context->channelmix)
1956 goto fail;
1957
1958 context->resample = Stream_New(nullptr, 1024);
1959 if (!context->resample)
1960 goto fail;
1961
1962 return TRUE;
1963
1964fail:
1965 freerdp_dsp_common_context_uninit(context);
1966 return FALSE;
1967}
1968
1969void freerdp_dsp_common_context_uninit(FREERDP_DSP_COMMON_CONTEXT* context)
1970{
1971 WINPR_ASSERT(context);
1972
1973 Stream_Free(context->buffer, TRUE);
1974 Stream_Free(context->channelmix, TRUE);
1975 Stream_Free(context->resample, TRUE);
1976
1977 context->buffer = nullptr;
1978 context->channelmix = nullptr;
1979 context->resample = nullptr;
1980}