FreeRDP
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_DSP_FFMPEG)
40 #if defined(WITH_GSM)
41 #include <gsm/gsm.h>
42 #endif
43 
44 #if defined(WITH_LAME)
45 #include <lame/lame.h>
46 #endif
47 
48 #if defined(WITH_OPUS)
49 #include <opus/opus.h>
50 
51 #define OPUS_MAX_FRAMES 5760
52 #endif
53 
54 #if defined(WITH_FAAD2)
55 #include <neaacdec.h>
56 #endif
57 
58 #if defined(WITH_FAAC)
59 #include <faac.h>
60 #endif
61 
62 #if defined(WITH_SOXR)
63 #include <soxr.h>
64 #endif
65 
66 #else
67 #include "dsp_ffmpeg.h"
68 #endif
69 
70 #if !defined(WITH_DSP_FFMPEG)
71 
72 #define TAG FREERDP_TAG("dsp")
73 
74 typedef union
75 {
76  struct
77  {
78  size_t packet_size;
79  INT16 last_sample[2];
80  INT16 last_step[2];
81  } ima;
82  struct
83  {
84  BYTE predictor[2];
85  INT32 delta[2];
86  INT32 sample1[2];
87  INT32 sample2[2];
88  } ms;
89 } ADPCM;
90 
91 struct S_FREERDP_DSP_CONTEXT
92 {
94 
95  ADPCM adpcm;
96 
97 #if defined(WITH_GSM)
98  gsm gsm;
99 #endif
100 #if defined(WITH_LAME)
101  lame_t lame;
102  hip_t hip;
103 #endif
104 #if defined(WITH_OPUS)
105  OpusDecoder* opus_decoder;
106  OpusEncoder* opus_encoder;
107 #endif
108 #if defined(WITH_FAAD2)
109  NeAACDecHandle faad;
110  BOOL faadSetup;
111 #endif
112 
113 #if defined(WITH_FAAC)
114  faacEncHandle faac;
115  unsigned long faacInputSamples;
116  unsigned long faacMaxOutputBytes;
117 #endif
118 
119 #if defined(WITH_SOXR)
120  soxr_t sox;
121 #endif
122 };
123 
124 #if defined(WITH_OPUS)
125 static BOOL opus_is_valid_samplerate(const AUDIO_FORMAT* WINPR_RESTRICT format)
126 {
127  WINPR_ASSERT(format);
128 
129  switch (format->nSamplesPerSec)
130  {
131  case 8000:
132  case 12000:
133  case 16000:
134  case 24000:
135  case 48000:
136  return TRUE;
137  default:
138  return FALSE;
139  }
140 }
141 #endif
142 
143 static INT16 read_int16(const BYTE* WINPR_RESTRICT src)
144 {
145  return (INT16)(src[0] | (src[1] << 8));
146 }
147 
148 static BOOL freerdp_dsp_channel_mix(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
149  const BYTE* WINPR_RESTRICT src, size_t size,
150  const AUDIO_FORMAT* WINPR_RESTRICT srcFormat,
151  const BYTE** WINPR_RESTRICT data, size_t* WINPR_RESTRICT length)
152 {
153  if (!context || !data || !length)
154  return FALSE;
155 
156  if (srcFormat->wFormatTag != WAVE_FORMAT_PCM)
157  return FALSE;
158 
159  const UINT32 bpp = srcFormat->wBitsPerSample > 8 ? 2 : 1;
160  const size_t samples = size / bpp / srcFormat->nChannels;
161 
162  if (context->common.format.nChannels == srcFormat->nChannels)
163  {
164  *data = src;
165  *length = size;
166  return TRUE;
167  }
168 
169  Stream_SetPosition(context->common.channelmix, 0);
170 
171  /* Destination has more channels than source */
172  if (context->common.format.nChannels > srcFormat->nChannels)
173  {
174  switch (srcFormat->nChannels)
175  {
176  case 1:
177  if (!Stream_EnsureCapacity(context->common.channelmix, size * 2))
178  return FALSE;
179 
180  for (size_t x = 0; x < samples; x++)
181  {
182  for (size_t y = 0; y < bpp; y++)
183  Stream_Write_UINT8(context->common.channelmix, src[x * bpp + y]);
184 
185  for (size_t y = 0; y < bpp; y++)
186  Stream_Write_UINT8(context->common.channelmix, src[x * bpp + y]);
187  }
188 
189  Stream_SealLength(context->common.channelmix);
190  *data = Stream_Buffer(context->common.channelmix);
191  *length = Stream_Length(context->common.channelmix);
192  return TRUE;
193 
194  case 2: /* We only support stereo, so we can not handle this case. */
195  default: /* Unsupported number of channels */
196  return FALSE;
197  }
198  }
199 
200  /* Destination has less channels than source */
201  switch (srcFormat->nChannels)
202  {
203  case 2:
204  if (!Stream_EnsureCapacity(context->common.channelmix, size / 2))
205  return FALSE;
206 
207  /* Simply drop second channel.
208  * TODO: Calculate average */
209  for (size_t x = 0; x < samples; x++)
210  {
211  for (size_t y = 0; y < bpp; y++)
212  Stream_Write_UINT8(context->common.channelmix, src[2 * x * bpp + y]);
213  }
214 
215  Stream_SealLength(context->common.channelmix);
216  *data = Stream_Buffer(context->common.channelmix);
217  *length = Stream_Length(context->common.channelmix);
218  return TRUE;
219 
220  case 1: /* Invalid, do we want to use a 0 channel sound? */
221  default: /* Unsupported number of channels */
222  return FALSE;
223  }
224 
225  return FALSE;
226 }
227 
233 static BOOL freerdp_dsp_resample(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
234  const BYTE* WINPR_RESTRICT src, size_t size,
235  const AUDIO_FORMAT* WINPR_RESTRICT srcFormat,
236  const BYTE** WINPR_RESTRICT data, size_t* WINPR_RESTRICT length)
237 {
238 #if defined(WITH_SOXR)
239  soxr_error_t error;
240  size_t idone, odone;
241  size_t sframes, rframes;
242  size_t rsize;
243  size_t sbytes, rbytes;
244  size_t dstChannels;
245  size_t srcChannels;
246  size_t srcBytesPerFrame, dstBytesPerFrame;
247 #endif
248  AUDIO_FORMAT format;
249 
250  if (srcFormat->wFormatTag != WAVE_FORMAT_PCM)
251  {
252  WLog_ERR(TAG, "requires %s for sample input, got %s",
253  audio_format_get_tag_string(WAVE_FORMAT_PCM),
254  audio_format_get_tag_string(srcFormat->wFormatTag));
255  return FALSE;
256  }
257 
258  /* We want to ignore differences of source and destination format. */
259  format = *srcFormat;
260  format.wFormatTag = WAVE_FORMAT_UNKNOWN;
261  format.wBitsPerSample = 0;
262 
263  if (audio_format_compatible(&format, &context->common.format))
264  {
265  *data = src;
266  *length = size;
267  return TRUE;
268  }
269 
270 #if defined(WITH_SOXR)
271  srcBytesPerFrame = (srcFormat->wBitsPerSample > 8) ? 2 : 1;
272  dstBytesPerFrame = (context->common.format.wBitsPerSample > 8) ? 2 : 1;
273  srcChannels = srcFormat->nChannels;
274  dstChannels = context->common.format.nChannels;
275  sbytes = srcChannels * srcBytesPerFrame;
276  sframes = size / sbytes;
277  rbytes = dstBytesPerFrame * dstChannels;
278  /* Integer rounding correct division */
279  rframes =
280  (sframes * context->common.format.nSamplesPerSec + (srcFormat->nSamplesPerSec + 1) / 2) /
281  srcFormat->nSamplesPerSec;
282  rsize = rframes * rbytes;
283 
284  if (!Stream_EnsureCapacity(context->common.resample, rsize))
285  return FALSE;
286 
287  error =
288  soxr_process(context->sox, src, sframes, &idone, Stream_Buffer(context->common.resample),
289  Stream_Capacity(context->common.resample) / rbytes, &odone);
290  Stream_SetLength(context->common.resample, odone * rbytes);
291  *data = Stream_Buffer(context->common.resample);
292  *length = Stream_Length(context->common.resample);
293  return (error == 0) ? TRUE : FALSE;
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 
307 static 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 
311 static 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 
321 static UINT16 dsp_decode_ima_adpcm_sample(ADPCM* WINPR_RESTRICT adpcm, unsigned int channel,
322  BYTE sample)
323 {
324  const INT32 ss = ima_step_size_table[adpcm->ima.last_step[channel]];
325  INT32 d = (ss >> 3);
326 
327  if (sample & 1)
328  d += (ss >> 2);
329 
330  if (sample & 2)
331  d += (ss >> 1);
332 
333  if (sample & 4)
334  d += ss;
335 
336  if (sample & 8)
337  d = -d;
338 
339  d += adpcm->ima.last_sample[channel];
340 
341  if (d < -32768)
342  d = -32768;
343  else if (d > 32767)
344  d = 32767;
345 
346  adpcm->ima.last_sample[channel] = (INT16)d;
347  adpcm->ima.last_step[channel] += ima_step_index_table[sample];
348 
349  if (adpcm->ima.last_step[channel] < 0)
350  adpcm->ima.last_step[channel] = 0;
351  else if (adpcm->ima.last_step[channel] > 88)
352  adpcm->ima.last_step[channel] = 88;
353 
354  return (UINT16)d;
355 }
356 
357 static BOOL freerdp_dsp_decode_ima_adpcm(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
358  const BYTE* WINPR_RESTRICT src, size_t size,
359  wStream* WINPR_RESTRICT out)
360 {
361  size_t out_size = size * 4;
362  const UINT32 block_size = context->common.format.nBlockAlign;
363  const UINT32 channels = context->common.format.nChannels;
364 
365  if (!Stream_EnsureCapacity(out, out_size))
366  return FALSE;
367 
368  while (size > 0)
369  {
370  if (size % block_size == 0)
371  {
372  context->adpcm.ima.last_sample[0] =
373  (INT16)(((UINT16)(*src)) | (((UINT16)(*(src + 1))) << 8));
374  context->adpcm.ima.last_step[0] = (INT16)(*(src + 2));
375  src += 4;
376  size -= 4;
377  out_size -= 16;
378 
379  if (channels > 1)
380  {
381  context->adpcm.ima.last_sample[1] =
382  (INT16)(((UINT16)(*src)) | (((UINT16)(*(src + 1))) << 8));
383  context->adpcm.ima.last_step[1] = (INT16)(*(src + 2));
384  src += 4;
385  size -= 4;
386  out_size -= 16;
387  }
388  }
389 
390  if (channels > 1)
391  {
392  for (size_t i = 0; i < 8; i++)
393  {
394  BYTE* dst = Stream_Pointer(out);
395 
396  const int channel = (i < 4 ? 0 : 1);
397  {
398  const BYTE sample = ((*src) & 0x0f);
399  const UINT16 decoded =
400  dsp_decode_ima_adpcm_sample(&context->adpcm, channel, sample);
401  dst[((i & 3) << 3) + (channel << 1)] = (decoded & 0xFF);
402  dst[((i & 3) << 3) + (channel << 1) + 1] = (decoded >> 8);
403  }
404  {
405  const BYTE sample = ((*src) >> 4);
406  const UINT16 decoded =
407  dsp_decode_ima_adpcm_sample(&context->adpcm, channel, sample);
408  dst[((i & 3) << 3) + (channel << 1) + 4] = (decoded & 0xFF);
409  dst[((i & 3) << 3) + (channel << 1) + 5] = (decoded >> 8);
410  }
411  src++;
412  }
413 
414  if (!Stream_SafeSeek(out, 32))
415  return FALSE;
416  size -= 8;
417  }
418  else
419  {
420  BYTE* dst = Stream_Pointer(out);
421  if (!Stream_SafeSeek(out, 4))
422  return FALSE;
423 
424  {
425  const BYTE sample = ((*src) & 0x0f);
426  const UINT16 decoded = dsp_decode_ima_adpcm_sample(&context->adpcm, 0, sample);
427  *dst++ = (decoded & 0xFF);
428  *dst++ = (decoded >> 8);
429  }
430  {
431  const BYTE sample = ((*src) >> 4);
432  const UINT16 decoded = dsp_decode_ima_adpcm_sample(&context->adpcm, 0, sample);
433  *dst++ = (decoded & 0xFF);
434  *dst++ = (decoded >> 8);
435  }
436  src++;
437  size--;
438  }
439  }
440 
441  return TRUE;
442 }
443 
444 #if defined(WITH_GSM)
445 static BOOL freerdp_dsp_decode_gsm610(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
446  const BYTE* WINPR_RESTRICT src, size_t size,
447  wStream* WINPR_RESTRICT out)
448 {
449  size_t offset = 0;
450 
451  while (offset < size)
452  {
453  int rc;
454  gsm_signal gsmBlockBuffer[160] = { 0 };
455  rc = gsm_decode(context->gsm, (gsm_byte*)/* API does not modify */ &src[offset],
456  gsmBlockBuffer);
457 
458  if (rc < 0)
459  return FALSE;
460 
461  if ((offset % 65) == 0)
462  offset += 33;
463  else
464  offset += 32;
465 
466  if (!Stream_EnsureRemainingCapacity(out, sizeof(gsmBlockBuffer)))
467  return FALSE;
468 
469  Stream_Write(out, (void*)gsmBlockBuffer, sizeof(gsmBlockBuffer));
470  }
471 
472  return TRUE;
473 }
474 
475 static BOOL freerdp_dsp_encode_gsm610(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
476  const BYTE* WINPR_RESTRICT src, size_t size,
477  wStream* WINPR_RESTRICT out)
478 {
479  size_t offset = 0;
480 
481  while (offset < size)
482  {
483  const gsm_signal* signal = (const gsm_signal*)&src[offset];
484 
485  if (!Stream_EnsureRemainingCapacity(out, sizeof(gsm_frame)))
486  return FALSE;
487 
488  gsm_encode(context->gsm, (gsm_signal*)/* API does not modify */ signal,
489  Stream_Pointer(out));
490 
491  if ((offset % 65) == 0)
492  Stream_Seek(out, 33);
493  else
494  Stream_Seek(out, 32);
495 
496  offset += 160;
497  }
498 
499  return TRUE;
500 }
501 #endif
502 
503 #if defined(WITH_LAME)
504 static BOOL freerdp_dsp_decode_mp3(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
505  const BYTE* WINPR_RESTRICT src, size_t size,
506  wStream* WINPR_RESTRICT out)
507 {
508  int rc;
509  short* pcm_l;
510  short* pcm_r;
511  size_t buffer_size;
512 
513  if (!context || !src || !out)
514  return FALSE;
515 
516  buffer_size = 2 * context->common.format.nChannels * context->common.format.nSamplesPerSec;
517 
518  if (!Stream_EnsureCapacity(context->common.buffer, 2 * buffer_size))
519  return FALSE;
520 
521  pcm_l = Stream_BufferAs(context->common.buffer, short);
522  pcm_r = Stream_BufferAs(context->common.buffer, short) + buffer_size;
523  rc = hip_decode(context->hip, (unsigned char*)/* API is not modifying content */ src, size,
524  pcm_l, pcm_r);
525 
526  if (rc <= 0)
527  return FALSE;
528 
529  if (!Stream_EnsureRemainingCapacity(out, (size_t)rc * context->common.format.nChannels * 2))
530  return FALSE;
531 
532  for (size_t x = 0; x < rc; x++)
533  {
534  Stream_Write_UINT16(out, (UINT16)pcm_l[x]);
535  Stream_Write_UINT16(out, (UINT16)pcm_r[x]);
536  }
537 
538  return TRUE;
539 }
540 
541 static BOOL freerdp_dsp_encode_mp3(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
542  const BYTE* WINPR_RESTRICT src, size_t size,
543  wStream* WINPR_RESTRICT out)
544 {
545  size_t samples_per_channel;
546  int rc;
547 
548  if (!context || !src || !out)
549  return FALSE;
550 
551  samples_per_channel =
552  size / context->common.format.nChannels / context->common.format.wBitsPerSample / 8;
553 
554  /* Ensure worst case buffer size for mp3 stream taken from LAME header */
555  if (!Stream_EnsureRemainingCapacity(out, 5 / 4 * samples_per_channel + 7200))
556  return FALSE;
557 
558  samples_per_channel = size / 2 /* size of a sample */ / context->common.format.nChannels;
559  rc = lame_encode_buffer_interleaved(context->lame, (short*)src, samples_per_channel,
560  Stream_Pointer(out), Stream_GetRemainingCapacity(out));
561 
562  if (rc < 0)
563  return FALSE;
564 
565  Stream_Seek(out, (size_t)rc);
566  return TRUE;
567 }
568 #endif
569 
570 #if defined(WITH_FAAC)
571 static BOOL freerdp_dsp_encode_faac(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
572  const BYTE* WINPR_RESTRICT src, size_t size,
573  wStream* WINPR_RESTRICT out)
574 {
575  const int16_t* inSamples = (const int16_t*)src;
576  unsigned int bpp;
577  size_t nrSamples;
578  int rc;
579 
580  if (!context || !src || !out)
581  return FALSE;
582 
583  bpp = context->common.format.wBitsPerSample / 8;
584  nrSamples = size / bpp;
585 
586  if (!Stream_EnsureRemainingCapacity(context->common.buffer, nrSamples * sizeof(int16_t)))
587  return FALSE;
588 
589  for (size_t x = 0; x < nrSamples; x++)
590  {
591  Stream_Write_INT16(context->common.buffer, inSamples[x]);
592  if (Stream_GetPosition(context->common.buffer) / bpp >= context->faacInputSamples)
593  {
594  if (!Stream_EnsureRemainingCapacity(out, context->faacMaxOutputBytes))
595  return FALSE;
596  rc = faacEncEncode(context->faac, Stream_BufferAs(context->common.buffer, int32_t),
597  context->faacInputSamples, Stream_Pointer(out),
598  Stream_GetRemainingCapacity(out));
599  if (rc < 0)
600  return FALSE;
601  if (rc > 0)
602  Stream_Seek(out, (size_t)rc);
603  Stream_SetPosition(context->common.buffer, 0);
604  }
605  }
606 
607  return TRUE;
608 }
609 #endif
610 
611 #if defined(WITH_OPUS)
612 static BOOL freerdp_dsp_decode_opus(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
613  const BYTE* WINPR_RESTRICT src, size_t size,
614  wStream* WINPR_RESTRICT out)
615 {
616  size_t max_size = 5760;
617  int frames;
618 
619  if (!context || !src || !out)
620  return FALSE;
621 
622  /* Max packet duration is 120ms (5760 at 48KHz) */
623  max_size = OPUS_MAX_FRAMES * context->common.format.nChannels * sizeof(int16_t);
624  if (!Stream_EnsureRemainingCapacity(context->common.buffer, max_size))
625  return FALSE;
626 
627  frames = opus_decode(context->opus_decoder, src, size, Stream_Pointer(out), OPUS_MAX_FRAMES, 0);
628  if (frames < 0)
629  return FALSE;
630 
631  Stream_Seek(out, frames * context->common.format.nChannels * sizeof(int16_t));
632 
633  return TRUE;
634 }
635 
636 static BOOL freerdp_dsp_encode_opus(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
637  const BYTE* WINPR_RESTRICT src, size_t size,
638  wStream* WINPR_RESTRICT out)
639 {
640  if (!context || !src || !out)
641  return FALSE;
642 
643  /* Max packet duration is 120ms (5760 at 48KHz) */
644  const size_t max_size = OPUS_MAX_FRAMES * context->common.format.nChannels * sizeof(int16_t);
645  if (!Stream_EnsureRemainingCapacity(context->common.buffer, max_size))
646  return FALSE;
647 
648  const int src_frames = size / sizeof(opus_int16) / context->common.format.nChannels;
649  const opus_int16* src_data = (const opus_int16*)src;
650  const int frames =
651  opus_encode(context->opus_encoder, src_data, src_frames, Stream_Pointer(out), max_size);
652  if (frames < 0)
653  return FALSE;
654  return Stream_SafeSeek(out, frames * context->common.format.nChannels * sizeof(int16_t));
655 }
656 #endif
657 
658 #if defined(WITH_FAAD2)
659 static BOOL freerdp_dsp_decode_faad(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
660  const BYTE* WINPR_RESTRICT src, size_t size,
661  wStream* WINPR_RESTRICT out)
662 {
663  NeAACDecFrameInfo info;
664  size_t offset = 0;
665 
666  if (!context || !src || !out)
667  return FALSE;
668 
669  if (!context->faadSetup)
670  {
671  union
672  {
673  const void* cpv;
674  void* pv;
675  } cnv;
676  unsigned long samplerate;
677  unsigned char channels;
678  long err;
679  cnv.cpv = src;
680  err = NeAACDecInit(context->faad, /* API is not modifying content */ cnv.pv, size,
681  &samplerate, &channels);
682 
683  if (err != 0)
684  return FALSE;
685 
686  if (channels != context->common.format.nChannels)
687  return FALSE;
688 
689  if (samplerate != context->common.format.nSamplesPerSec)
690  return FALSE;
691 
692  context->faadSetup = TRUE;
693  }
694 
695  while (offset < size)
696  {
697  union
698  {
699  const void* cpv;
700  void* pv;
701  } cnv;
702  size_t outSize;
703  void* sample_buffer;
704  outSize = context->common.format.nSamplesPerSec * context->common.format.nChannels *
705  context->common.format.wBitsPerSample / 8;
706 
707  if (!Stream_EnsureRemainingCapacity(out, outSize))
708  return FALSE;
709 
710  sample_buffer = Stream_Pointer(out);
711 
712  cnv.cpv = &src[offset];
713  NeAACDecDecode2(context->faad, &info, cnv.pv, size - offset, &sample_buffer,
714  Stream_GetRemainingCapacity(out));
715 
716  if (info.error != 0)
717  return FALSE;
718 
719  offset += info.bytesconsumed;
720 
721  if (info.samples == 0)
722  continue;
723 
724  Stream_Seek(out, info.samples * context->common.format.wBitsPerSample / 8);
725  }
726 
727  return TRUE;
728 }
729 
730 #endif
731 
739 static const struct
740 {
741  BYTE byte_num;
742  BYTE byte_shift;
743 } ima_stereo_encode_map[] = { { 0, 0 }, { 4, 0 }, { 0, 4 }, { 4, 4 }, { 1, 0 }, { 5, 0 },
744  { 1, 4 }, { 5, 4 }, { 2, 0 }, { 6, 0 }, { 2, 4 }, { 6, 4 },
745  { 3, 0 }, { 7, 0 }, { 3, 4 }, { 7, 4 } };
746 
747 static BYTE dsp_encode_ima_adpcm_sample(ADPCM* WINPR_RESTRICT adpcm, int channel, INT16 sample)
748 {
749  INT32 ss = ima_step_size_table[adpcm->ima.last_step[channel]];
750  INT32 e = sample - adpcm->ima.last_sample[channel];
751  INT32 d = e;
752  INT32 diff = ss >> 3;
753  BYTE enc = 0;
754 
755  if (e < 0)
756  {
757  enc = 8;
758  e = -e;
759  }
760 
761  if (e >= ss)
762  {
763  enc |= 4;
764  e -= ss;
765  }
766 
767  ss >>= 1;
768 
769  if (e >= ss)
770  {
771  enc |= 2;
772  e -= ss;
773  }
774 
775  ss >>= 1;
776 
777  if (e >= ss)
778  {
779  enc |= 1;
780  e -= ss;
781  }
782 
783  if (d < 0)
784  diff = d + e - diff;
785  else
786  diff = d - e + diff;
787 
788  diff += adpcm->ima.last_sample[channel];
789 
790  if (diff < -32768)
791  diff = -32768;
792  else if (diff > 32767)
793  diff = 32767;
794 
795  adpcm->ima.last_sample[channel] = (INT16)diff;
796  adpcm->ima.last_step[channel] += ima_step_index_table[enc];
797 
798  if (adpcm->ima.last_step[channel] < 0)
799  adpcm->ima.last_step[channel] = 0;
800  else if (adpcm->ima.last_step[channel] > 88)
801  adpcm->ima.last_step[channel] = 88;
802 
803  return enc;
804 }
805 
806 static BOOL freerdp_dsp_encode_ima_adpcm(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
807  const BYTE* WINPR_RESTRICT src, size_t size,
808  wStream* WINPR_RESTRICT out)
809 {
810  if (!Stream_EnsureRemainingCapacity(out, size))
811  return FALSE;
812  if (!Stream_EnsureRemainingCapacity(context->common.buffer, size + 64))
813  return FALSE;
814 
815  const size_t align = (context->common.format.nChannels > 1) ? 32 : 4;
816 
817  while (size >= align)
818  {
819  if (Stream_GetPosition(context->common.buffer) % context->common.format.nBlockAlign == 0)
820  {
821  Stream_Write_UINT8(context->common.buffer, context->adpcm.ima.last_sample[0] & 0xFF);
822  Stream_Write_UINT8(context->common.buffer,
823  (context->adpcm.ima.last_sample[0] >> 8) & 0xFF);
824  Stream_Write_UINT8(context->common.buffer, (BYTE)context->adpcm.ima.last_step[0]);
825  Stream_Write_UINT8(context->common.buffer, 0);
826 
827  if (context->common.format.nChannels > 1)
828  {
829  Stream_Write_UINT8(context->common.buffer,
830  context->adpcm.ima.last_sample[1] & 0xFF);
831  Stream_Write_UINT8(context->common.buffer,
832  (context->adpcm.ima.last_sample[1] >> 8) & 0xFF);
833  Stream_Write_UINT8(context->common.buffer, (BYTE)context->adpcm.ima.last_step[1]);
834  Stream_Write_UINT8(context->common.buffer, 0);
835  }
836  }
837 
838  if (context->common.format.nChannels > 1)
839  {
840  BYTE* dst = Stream_Pointer(context->common.buffer);
841  ZeroMemory(dst, 8);
842 
843  for (size_t i = 0; i < 16; i++)
844  {
845  const INT16 sample = (INT16)(((UINT16)(*src)) | (((UINT16)(*(src + 1))) << 8));
846  src += 2;
847  const BYTE encoded = dsp_encode_ima_adpcm_sample(&context->adpcm, i % 2, sample);
848  dst[ima_stereo_encode_map[i].byte_num] |= encoded
849  << ima_stereo_encode_map[i].byte_shift;
850  }
851 
852  if (!Stream_SafeSeek(context->common.buffer, 8))
853  return FALSE;
854  size -= 32;
855  }
856  else
857  {
858  INT16 sample = (INT16)(((UINT16)(*src)) | (((UINT16)(*(src + 1))) << 8));
859  src += 2;
860  BYTE encoded = dsp_encode_ima_adpcm_sample(&context->adpcm, 0, sample);
861  sample = (INT16)(((UINT16)(*src)) | (((UINT16)(*(src + 1))) << 8));
862  src += 2;
863  encoded |= dsp_encode_ima_adpcm_sample(&context->adpcm, 0, sample) << 4;
864  Stream_Write_UINT8(context->common.buffer, encoded);
865  size -= 4;
866  }
867 
868  if (Stream_GetPosition(context->common.buffer) >= context->adpcm.ima.packet_size)
869  {
870  BYTE* bsrc = Stream_Buffer(context->common.buffer);
871  Stream_Write(out, bsrc, context->adpcm.ima.packet_size);
872  Stream_SetPosition(context->common.buffer, 0);
873  }
874  }
875 
876  return TRUE;
877 }
878 
885 static const INT32 ms_adpcm_adaptation_table[] = { 230, 230, 230, 230, 307, 409, 512, 614,
886  768, 614, 512, 409, 307, 230, 230, 230 };
887 
888 static const INT32 ms_adpcm_coeffs1[7] = { 256, 512, 0, 192, 240, 460, 392 };
889 
890 static const INT32 ms_adpcm_coeffs2[7] = { 0, -256, 0, 64, 0, -208, -232 };
891 
892 static INLINE INT16 freerdp_dsp_decode_ms_adpcm_sample(ADPCM* WINPR_RESTRICT adpcm, BYTE sample,
893  int channel)
894 {
895  const INT8 nibble = (sample & 0x08 ? (INT8)sample - 16 : (INT8)sample);
896  INT32 presample =
897  ((adpcm->ms.sample1[channel] * ms_adpcm_coeffs1[adpcm->ms.predictor[channel]]) +
898  (adpcm->ms.sample2[channel] * ms_adpcm_coeffs2[adpcm->ms.predictor[channel]])) /
899  256;
900  presample += nibble * adpcm->ms.delta[channel];
901 
902  if (presample > 32767)
903  presample = 32767;
904  else if (presample < -32768)
905  presample = -32768;
906 
907  adpcm->ms.sample2[channel] = adpcm->ms.sample1[channel];
908  adpcm->ms.sample1[channel] = presample;
909  adpcm->ms.delta[channel] = adpcm->ms.delta[channel] * ms_adpcm_adaptation_table[sample] / 256;
910 
911  if (adpcm->ms.delta[channel] < 16)
912  adpcm->ms.delta[channel] = 16;
913 
914  return (INT16)presample;
915 }
916 
917 static BOOL freerdp_dsp_decode_ms_adpcm(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
918  const BYTE* WINPR_RESTRICT src, size_t size,
919  wStream* WINPR_RESTRICT out)
920 {
921  const size_t out_size = size * 4;
922  const UINT32 channels = context->common.format.nChannels;
923  const UINT32 block_size = context->common.format.nBlockAlign;
924 
925  if (!Stream_EnsureCapacity(out, out_size))
926  return FALSE;
927 
928  while (size > 0)
929  {
930  if (size % block_size == 0)
931  {
932  if (channels > 1)
933  {
934  context->adpcm.ms.predictor[0] = *src++;
935  context->adpcm.ms.predictor[1] = *src++;
936  context->adpcm.ms.delta[0] = read_int16(src);
937  src += 2;
938  context->adpcm.ms.delta[1] = read_int16(src);
939  src += 2;
940  context->adpcm.ms.sample1[0] = read_int16(src);
941  src += 2;
942  context->adpcm.ms.sample1[1] = read_int16(src);
943  src += 2;
944  context->adpcm.ms.sample2[0] = read_int16(src);
945  src += 2;
946  context->adpcm.ms.sample2[1] = read_int16(src);
947  src += 2;
948  size -= 14;
949  Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample2[0]);
950  Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample2[1]);
951  Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample1[0]);
952  Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample1[1]);
953  }
954  else
955  {
956  context->adpcm.ms.predictor[0] = *src++;
957  context->adpcm.ms.delta[0] = read_int16(src);
958  src += 2;
959  context->adpcm.ms.sample1[0] = read_int16(src);
960  src += 2;
961  context->adpcm.ms.sample2[0] = read_int16(src);
962  src += 2;
963  size -= 7;
964  Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample2[0]);
965  Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample1[0]);
966  }
967  }
968 
969  if (channels > 1)
970  {
971  {
972  const BYTE sample = *src++;
973  size--;
974  Stream_Write_INT16(
975  out, freerdp_dsp_decode_ms_adpcm_sample(&context->adpcm, sample >> 4, 0));
976  Stream_Write_INT16(
977  out, freerdp_dsp_decode_ms_adpcm_sample(&context->adpcm, sample & 0x0F, 1));
978  }
979  {
980  const BYTE sample = *src++;
981  size--;
982  Stream_Write_INT16(
983  out, freerdp_dsp_decode_ms_adpcm_sample(&context->adpcm, sample >> 4, 0));
984  Stream_Write_INT16(
985  out, freerdp_dsp_decode_ms_adpcm_sample(&context->adpcm, sample & 0x0F, 1));
986  }
987  }
988  else
989  {
990  const BYTE sample = *src++;
991  size--;
992  Stream_Write_INT16(out,
993  freerdp_dsp_decode_ms_adpcm_sample(&context->adpcm, sample >> 4, 0));
994  Stream_Write_INT16(
995  out, freerdp_dsp_decode_ms_adpcm_sample(&context->adpcm, sample & 0x0F, 0));
996  }
997  }
998 
999  return TRUE;
1000 }
1001 
1002 static BYTE freerdp_dsp_encode_ms_adpcm_sample(ADPCM* WINPR_RESTRICT adpcm, INT32 sample,
1003  int channel)
1004 {
1005  INT32 presample =
1006  ((adpcm->ms.sample1[channel] * ms_adpcm_coeffs1[adpcm->ms.predictor[channel]]) +
1007  (adpcm->ms.sample2[channel] * ms_adpcm_coeffs2[adpcm->ms.predictor[channel]])) /
1008  256;
1009  INT32 errordelta = (sample - presample) / adpcm->ms.delta[channel];
1010 
1011  if ((sample - presample) % adpcm->ms.delta[channel] > adpcm->ms.delta[channel] / 2)
1012  errordelta++;
1013 
1014  if (errordelta > 7)
1015  errordelta = 7;
1016  else if (errordelta < -8)
1017  errordelta = -8;
1018 
1019  presample += adpcm->ms.delta[channel] * errordelta;
1020 
1021  if (presample > 32767)
1022  presample = 32767;
1023  else if (presample < -32768)
1024  presample = -32768;
1025 
1026  adpcm->ms.sample2[channel] = adpcm->ms.sample1[channel];
1027  adpcm->ms.sample1[channel] = presample;
1028  adpcm->ms.delta[channel] =
1029  adpcm->ms.delta[channel] * ms_adpcm_adaptation_table[(((BYTE)errordelta) & 0x0F)] / 256;
1030 
1031  if (adpcm->ms.delta[channel] < 16)
1032  adpcm->ms.delta[channel] = 16;
1033 
1034  return ((BYTE)errordelta) & 0x0F;
1035 }
1036 
1037 static BOOL freerdp_dsp_encode_ms_adpcm(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
1038  const BYTE* WINPR_RESTRICT src, size_t size,
1039  wStream* WINPR_RESTRICT out)
1040 {
1041  const size_t step = 8 + ((context->common.format.nChannels > 1) ? 4 : 0);
1042 
1043  if (!Stream_EnsureRemainingCapacity(out, size))
1044  return FALSE;
1045 
1046  const size_t start = Stream_GetPosition(out);
1047 
1048  if (context->adpcm.ms.delta[0] < 16)
1049  context->adpcm.ms.delta[0] = 16;
1050 
1051  if (context->adpcm.ms.delta[1] < 16)
1052  context->adpcm.ms.delta[1] = 16;
1053 
1054  while (size >= step)
1055  {
1056  if ((Stream_GetPosition(out) - start) % context->common.format.nBlockAlign == 0)
1057  {
1058  if (context->common.format.nChannels > 1)
1059  {
1060  Stream_Write_UINT8(out, context->adpcm.ms.predictor[0]);
1061  Stream_Write_UINT8(out, context->adpcm.ms.predictor[1]);
1062  Stream_Write_UINT8(out, (context->adpcm.ms.delta[0] & 0xFF));
1063  Stream_Write_UINT8(out, ((context->adpcm.ms.delta[0] >> 8) & 0xFF));
1064  Stream_Write_UINT8(out, (context->adpcm.ms.delta[1] & 0xFF));
1065  Stream_Write_UINT8(out, ((context->adpcm.ms.delta[1] >> 8) & 0xFF));
1066 
1067  context->adpcm.ms.sample1[0] = read_int16(src + 4);
1068  context->adpcm.ms.sample1[1] = read_int16(src + 6);
1069  context->adpcm.ms.sample2[0] = read_int16(src + 0);
1070  context->adpcm.ms.sample2[1] = read_int16(src + 2);
1071 
1072  Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample1[0]);
1073  Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample1[1]);
1074  Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample2[0]);
1075  Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample2[1]);
1076 
1077  src += 8;
1078  size -= 8;
1079  }
1080  else
1081  {
1082  Stream_Write_UINT8(out, context->adpcm.ms.predictor[0]);
1083  Stream_Write_UINT8(out, (BYTE)(context->adpcm.ms.delta[0] & 0xFF));
1084  Stream_Write_UINT8(out, (BYTE)((context->adpcm.ms.delta[0] >> 8) & 0xFF));
1085 
1086  context->adpcm.ms.sample1[0] = read_int16(src + 2);
1087  context->adpcm.ms.sample2[0] = read_int16(src + 0);
1088 
1089  Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample1[0]);
1090  Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample2[0]);
1091  src += 4;
1092  size -= 4;
1093  }
1094  }
1095 
1096  {
1097  const INT16 sample = read_int16(src);
1098  src += 2;
1099  Stream_Write_UINT8(
1100  out, (freerdp_dsp_encode_ms_adpcm_sample(&context->adpcm, sample, 0) << 4) & 0xFF);
1101  }
1102  {
1103  const INT16 sample = read_int16(src);
1104  src += 2;
1105 
1106  BYTE val = 0;
1107  Stream_Read_UINT8(out, val);
1108  val += freerdp_dsp_encode_ms_adpcm_sample(&context->adpcm, sample,
1109  context->common.format.nChannels > 1 ? 1 : 0);
1110  Stream_Write_UINT8(out, val);
1111  }
1112  size -= 4;
1113  }
1114 
1115  return TRUE;
1116 }
1117 
1118 #endif
1119 
1120 FREERDP_DSP_CONTEXT* freerdp_dsp_context_new(BOOL encoder)
1121 {
1122 #if defined(WITH_DSP_FFMPEG)
1123  return freerdp_dsp_ffmpeg_context_new(encoder);
1124 #else
1125  FREERDP_DSP_CONTEXT* context = calloc(1, sizeof(FREERDP_DSP_CONTEXT));
1126 
1127  if (!context)
1128  return NULL;
1129 
1130  if (!freerdp_dsp_common_context_init(&context->common, encoder))
1131  goto fail;
1132 
1133 #if defined(WITH_GSM)
1134  context->gsm = gsm_create();
1135 
1136  if (!context->gsm)
1137  goto fail;
1138 
1139  {
1140  int rc;
1141  int val = 1;
1142  rc = gsm_option(context->gsm, GSM_OPT_WAV49, &val);
1143 
1144  if (rc < 0)
1145  goto fail;
1146  }
1147 #endif
1148 #if defined(WITH_LAME)
1149 
1150  if (encoder)
1151  {
1152  context->lame = lame_init();
1153 
1154  if (!context->lame)
1155  goto fail;
1156  }
1157  else
1158  {
1159  context->hip = hip_decode_init();
1160 
1161  if (!context->hip)
1162  goto fail;
1163  }
1164 
1165 #endif
1166 #if defined(WITH_FAAD2)
1167 
1168  if (!encoder)
1169  {
1170  context->faad = NeAACDecOpen();
1171 
1172  if (!context->faad)
1173  goto fail;
1174  }
1175 
1176 #endif
1177  return context;
1178 fail:
1179  freerdp_dsp_context_free(context);
1180  return NULL;
1181 #endif
1182 }
1183 
1184 void freerdp_dsp_context_free(FREERDP_DSP_CONTEXT* context)
1185 {
1186  if (!context)
1187  return;
1188 
1189 #if defined(WITH_FDK_AAC)
1191  WINPR_ASSERT(ctx);
1192  fdk_aac_dsp_uninit(ctx);
1193 #endif
1194 
1195 #if defined(WITH_DSP_FFMPEG)
1196  freerdp_dsp_ffmpeg_context_free(context);
1197 #else
1198 
1199  freerdp_dsp_common_context_uninit(&context->common);
1200 
1201 #if defined(WITH_GSM)
1202  gsm_destroy(context->gsm);
1203 #endif
1204 #if defined(WITH_LAME)
1205 
1206  if (context->common.encoder)
1207  lame_close(context->lame);
1208  else
1209  hip_decode_exit(context->hip);
1210 
1211 #endif
1212 #if defined(WITH_OPUS)
1213 
1214  if (context->opus_decoder)
1215  opus_decoder_destroy(context->opus_decoder);
1216  if (context->opus_encoder)
1217  opus_encoder_destroy(context->opus_encoder);
1218 
1219 #endif
1220 #if defined(WITH_FAAD2)
1221 
1222  if (!context->common.encoder)
1223  NeAACDecClose(context->faad);
1224 
1225 #endif
1226 #if defined(WITH_FAAC)
1227 
1228  if (context->faac)
1229  faacEncClose(context->faac);
1230 
1231 #endif
1232 #if defined(WITH_SOXR)
1233  soxr_delete(context->sox);
1234 #endif
1235  free(context);
1236 
1237 #endif
1238 }
1239 
1240 BOOL freerdp_dsp_encode(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
1241  const AUDIO_FORMAT* WINPR_RESTRICT srcFormat,
1242  const BYTE* WINPR_RESTRICT pdata, size_t length,
1243  wStream* WINPR_RESTRICT out)
1244 {
1245 #if defined(WITH_FDK_AAC)
1247  WINPR_ASSERT(ctx);
1248  switch (ctx->format.wFormatTag)
1249  {
1250  case WAVE_FORMAT_AAC_MS:
1251  return fdk_aac_dsp_encode(ctx, srcFormat, pdata, length, out);
1252  default:
1253  break;
1254  }
1255 #endif
1256 
1257 #if defined(WITH_DSP_FFMPEG)
1258  return freerdp_dsp_ffmpeg_encode(context, srcFormat, pdata, length, out);
1259 #else
1260  if (!context || !context->common.encoder || !srcFormat || !pdata || !out)
1261  return FALSE;
1262 
1263  AUDIO_FORMAT format = *srcFormat;
1264  const BYTE* resampleData = NULL;
1265  size_t resampleLength = 0;
1266 
1267  if (!freerdp_dsp_channel_mix(context, pdata, length, srcFormat, &resampleData, &resampleLength))
1268  return FALSE;
1269 
1270  format.nChannels = context->common.format.nChannels;
1271 
1272  const BYTE* data = NULL;
1273  if (!freerdp_dsp_resample(context, resampleData, resampleLength, &format, &data, &length))
1274  return FALSE;
1275 
1276  switch (context->common.format.wFormatTag)
1277  {
1278  case WAVE_FORMAT_PCM:
1279  if (!Stream_EnsureRemainingCapacity(out, length))
1280  return FALSE;
1281 
1282  Stream_Write(out, data, length);
1283  return TRUE;
1284 
1285  case WAVE_FORMAT_ADPCM:
1286  return freerdp_dsp_encode_ms_adpcm(context, data, length, out);
1287 
1288  case WAVE_FORMAT_DVI_ADPCM:
1289  return freerdp_dsp_encode_ima_adpcm(context, data, length, out);
1290 #if defined(WITH_GSM)
1291 
1292  case WAVE_FORMAT_GSM610:
1293  return freerdp_dsp_encode_gsm610(context, data, length, out);
1294 #endif
1295 #if defined(WITH_LAME)
1296 
1297  case WAVE_FORMAT_MPEGLAYER3:
1298  return freerdp_dsp_encode_mp3(context, data, length, out);
1299 #endif
1300 #if defined(WITH_FAAC)
1301 
1302  case WAVE_FORMAT_AAC_MS:
1303  return freerdp_dsp_encode_faac(context, data, length, out);
1304 #endif
1305 #if defined(WITH_OPUS)
1306 
1307  case WAVE_FORMAT_OPUS:
1308  return freerdp_dsp_encode_opus(context, data, length, out);
1309 #endif
1310  default:
1311  return FALSE;
1312  }
1313 
1314  return FALSE;
1315 #endif
1316 }
1317 
1318 BOOL freerdp_dsp_decode(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
1319  const AUDIO_FORMAT* WINPR_RESTRICT srcFormat,
1320  const BYTE* WINPR_RESTRICT data, size_t length, wStream* WINPR_RESTRICT out)
1321 {
1322 #if defined(WITH_FDK_AAC)
1324  WINPR_ASSERT(ctx);
1325  switch (ctx->format.wFormatTag)
1326  {
1327  case WAVE_FORMAT_AAC_MS:
1328  return fdk_aac_dsp_decode(ctx, srcFormat, data, length, out);
1329  default:
1330  break;
1331  }
1332 #endif
1333 
1334 #if defined(WITH_DSP_FFMPEG)
1335  return freerdp_dsp_ffmpeg_decode(context, srcFormat, data, length, out);
1336 #else
1337 
1338  if (!context || context->common.encoder || !srcFormat || !data || !out)
1339  return FALSE;
1340 
1341  switch (context->common.format.wFormatTag)
1342  {
1343  case WAVE_FORMAT_PCM:
1344  if (!Stream_EnsureRemainingCapacity(out, length))
1345  return FALSE;
1346 
1347  Stream_Write(out, data, length);
1348  return TRUE;
1349 
1350  case WAVE_FORMAT_ADPCM:
1351  return freerdp_dsp_decode_ms_adpcm(context, data, length, out);
1352 
1353  case WAVE_FORMAT_DVI_ADPCM:
1354  return freerdp_dsp_decode_ima_adpcm(context, data, length, out);
1355 #if defined(WITH_GSM)
1356 
1357  case WAVE_FORMAT_GSM610:
1358  return freerdp_dsp_decode_gsm610(context, data, length, out);
1359 #endif
1360 #if defined(WITH_LAME)
1361 
1362  case WAVE_FORMAT_MPEGLAYER3:
1363  return freerdp_dsp_decode_mp3(context, data, length, out);
1364 #endif
1365 #if defined(WITH_FAAD2)
1366 
1367  case WAVE_FORMAT_AAC_MS:
1368  return freerdp_dsp_decode_faad(context, data, length, out);
1369 #endif
1370 
1371 #if defined(WITH_OPUS)
1372  case WAVE_FORMAT_OPUS:
1373  return freerdp_dsp_decode_opus(context, data, length, out);
1374 #endif
1375  default:
1376  return FALSE;
1377  }
1378 
1379  return FALSE;
1380 #endif
1381 }
1382 
1383 BOOL freerdp_dsp_supports_format(const AUDIO_FORMAT* WINPR_RESTRICT format, BOOL encode)
1384 {
1385 #if defined(WITH_FDK_AAC)
1386  switch (format->wFormatTag)
1387  {
1388  case WAVE_FORMAT_AAC_MS:
1389  return TRUE;
1390  default:
1391  break;
1392  }
1393 
1394 #endif
1395 
1396 #if defined(WITH_DSP_FFMPEG)
1397  return freerdp_dsp_ffmpeg_supports_format(format, encode);
1398 #else
1399 
1400 #if !defined(WITH_DSP_EXPERIMENTAL)
1401  WINPR_UNUSED(encode);
1402 #endif
1403  switch (format->wFormatTag)
1404  {
1405  case WAVE_FORMAT_PCM:
1406  return TRUE;
1407 #if defined(WITH_DSP_EXPERIMENTAL)
1408 
1409  case WAVE_FORMAT_ADPCM:
1410  return FALSE;
1411  case WAVE_FORMAT_DVI_ADPCM:
1412  return TRUE;
1413 #endif
1414 #if defined(WITH_GSM)
1415 
1416  case WAVE_FORMAT_GSM610:
1417 #if defined(WITH_DSP_EXPERIMENTAL)
1418  return TRUE;
1419 #else
1420  return !encode;
1421 #endif
1422 #endif
1423 #if defined(WITH_LAME)
1424 
1425  case WAVE_FORMAT_MPEGLAYER3:
1426 #if defined(WITH_DSP_EXPERIMENTAL)
1427  return TRUE;
1428 #else
1429  return !encode;
1430 #endif
1431 #endif
1432 
1433  case WAVE_FORMAT_AAC_MS:
1434 #if defined(WITH_FAAD2)
1435  if (!encode)
1436  return TRUE;
1437 
1438 #endif
1439 #if defined(WITH_FAAC)
1440 
1441  if (encode)
1442  return TRUE;
1443 
1444 #endif
1445 #if defined(WITH_OPUS)
1446  /* fallthrough */
1447  WINPR_FALLTHROUGH
1448  case WAVE_FORMAT_OPUS:
1449  return opus_is_valid_samplerate(format);
1450 #endif
1451  /* fallthrough */
1452  WINPR_FALLTHROUGH
1453  default:
1454  return FALSE;
1455  }
1456 
1457  return FALSE;
1458 #endif
1459 }
1460 
1461 BOOL freerdp_dsp_context_reset(FREERDP_DSP_CONTEXT* WINPR_RESTRICT context,
1462  const AUDIO_FORMAT* WINPR_RESTRICT targetFormat,
1463  UINT32 FramesPerPacket)
1464 {
1465 #if defined(WITH_FDK_AAC)
1466  WINPR_ASSERT(targetFormat);
1467  if (targetFormat->wFormatTag == WAVE_FORMAT_AAC_MS)
1468  {
1470  fdk_aac_dsp_uninit(ctx);
1471  ctx->format = *targetFormat;
1472  return fdk_aac_dsp_init(ctx, FramesPerPacket);
1473  }
1474 #endif
1475 
1476 #if defined(WITH_DSP_FFMPEG)
1477  return freerdp_dsp_ffmpeg_context_reset(context, targetFormat);
1478 #else
1479 
1480  if (!context || !targetFormat)
1481  return FALSE;
1482 
1483  context->common.format = *targetFormat;
1484 
1485  if (context->common.format.wFormatTag == WAVE_FORMAT_DVI_ADPCM)
1486  {
1487  size_t min_frame_data = 1ull * context->common.format.wBitsPerSample *
1488  context->common.format.nChannels * FramesPerPacket;
1489  size_t data_per_block =
1490  (1ULL * context->common.format.nBlockAlign - 4ULL * context->common.format.nChannels) *
1491  8ULL;
1492  size_t nb_block_per_packet = min_frame_data / data_per_block;
1493 
1494  if (min_frame_data % data_per_block)
1495  nb_block_per_packet++;
1496 
1497  context->adpcm.ima.packet_size = nb_block_per_packet * context->common.format.nBlockAlign;
1498  Stream_EnsureCapacity(context->common.buffer, context->adpcm.ima.packet_size);
1499  Stream_SetPosition(context->common.buffer, 0);
1500  }
1501 
1502 #if defined(WITH_OPUS)
1503 
1504  if (opus_is_valid_samplerate(&context->common.format))
1505  {
1506  if (!context->common.encoder)
1507  {
1508  int opus_error = OPUS_OK;
1509 
1510  context->opus_decoder =
1511  opus_decoder_create(context->common.format.nSamplesPerSec,
1512  context->common.format.nChannels, &opus_error);
1513  if (opus_error != OPUS_OK)
1514  return FALSE;
1515  }
1516  else
1517  {
1518  int opus_error = OPUS_OK;
1519 
1520  context->opus_encoder = opus_encoder_create(context->common.format.nSamplesPerSec,
1521  context->common.format.nChannels,
1522  OPUS_APPLICATION_VOIP, &opus_error);
1523  if (opus_error != OPUS_OK)
1524  return FALSE;
1525 
1526  opus_error =
1527  opus_encoder_ctl(context->opus_encoder,
1528  OPUS_SET_BITRATE(context->common.format.nAvgBytesPerSec * 8));
1529  if (opus_error != OPUS_OK)
1530  return FALSE;
1531  }
1532  }
1533 
1534 #endif
1535 #if defined(WITH_FAAD2)
1536  context->faadSetup = FALSE;
1537 #endif
1538 #if defined(WITH_FAAC)
1539 
1540  if (context->common.encoder)
1541  {
1542  faacEncConfigurationPtr cfg;
1543 
1544  if (context->faac)
1545  faacEncClose(context->faac);
1546 
1547  context->faac = faacEncOpen(targetFormat->nSamplesPerSec, targetFormat->nChannels,
1548  &context->faacInputSamples, &context->faacMaxOutputBytes);
1549 
1550  if (!context->faac)
1551  return FALSE;
1552 
1553  cfg = faacEncGetCurrentConfiguration(context->faac);
1554  cfg->inputFormat = FAAC_INPUT_16BIT;
1555  cfg->outputFormat = 0;
1556  cfg->mpegVersion = MPEG4;
1557  cfg->useTns = 1;
1558  cfg->bandWidth = targetFormat->nAvgBytesPerSec;
1559  faacEncSetConfiguration(context->faac, cfg);
1560  }
1561 
1562 #endif
1563 #if defined(WITH_SOXR)
1564  {
1565  soxr_io_spec_t iospec = soxr_io_spec(SOXR_INT16, SOXR_INT16);
1566  soxr_error_t error;
1567  soxr_delete(context->sox);
1568  context->sox =
1569  soxr_create(context->common.format.nSamplesPerSec, targetFormat->nSamplesPerSec,
1570  targetFormat->nChannels, &error, &iospec, NULL, NULL);
1571 
1572  if (!context->sox || (error != 0))
1573  return FALSE;
1574  }
1575 #endif
1576  return TRUE;
1577 #endif
1578 }
1579 
1580 BOOL freerdp_dsp_common_context_init(FREERDP_DSP_COMMON_CONTEXT* context, BOOL encode)
1581 {
1582  WINPR_ASSERT(context);
1583  context->encoder = encode;
1584  context->buffer = Stream_New(NULL, 1024);
1585  if (!context->buffer)
1586  goto fail;
1587 
1588  context->channelmix = Stream_New(NULL, 1024);
1589  if (!context->channelmix)
1590  goto fail;
1591 
1592  context->resample = Stream_New(NULL, 1024);
1593  if (!context->resample)
1594  goto fail;
1595 
1596  return TRUE;
1597 
1598 fail:
1599  freerdp_dsp_common_context_uninit(context);
1600  return FALSE;
1601 }
1602 
1603 void freerdp_dsp_common_context_uninit(FREERDP_DSP_COMMON_CONTEXT* context)
1604 {
1605  WINPR_ASSERT(context);
1606 
1607  Stream_Free(context->buffer, TRUE);
1608  Stream_Free(context->channelmix, TRUE);
1609  Stream_Free(context->resample, TRUE);
1610 
1611  context->buffer = NULL;
1612  context->channelmix = NULL;
1613  context->resample = NULL;
1614 }