2011-08-14 17:46:02 +04:00
|
|
|
/**
|
2012-10-09 07:02:04 +04:00
|
|
|
* FreeRDP: A Remote Desktop Protocol Implementation
|
2011-08-14 17:46:02 +04:00
|
|
|
* Digital Sound Processing
|
|
|
|
*
|
|
|
|
* Copyright 2010-2011 Vic Lee
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2022-02-16 13:20:38 +03:00
|
|
|
#include <freerdp/config.h>
|
2012-08-15 01:09:01 +04:00
|
|
|
|
2021-06-09 15:03:34 +03:00
|
|
|
#include <winpr/assert.h>
|
2011-08-14 17:46:02 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2012-08-15 01:09:01 +04:00
|
|
|
|
2012-11-22 05:21:08 +04:00
|
|
|
#include <winpr/crt.h>
|
|
|
|
|
2011-08-14 17:46:02 +04:00
|
|
|
#include <freerdp/types.h>
|
2018-11-09 12:15:14 +03:00
|
|
|
#include <freerdp/log.h>
|
2013-02-20 02:47:55 +04:00
|
|
|
#include <freerdp/codec/dsp.h>
|
2011-08-14 17:46:02 +04:00
|
|
|
|
2018-11-09 11:08:06 +03:00
|
|
|
#if !defined(WITH_DSP_FFMPEG)
|
2018-02-20 14:15:30 +03:00
|
|
|
#if defined(WITH_GSM)
|
|
|
|
#include <gsm/gsm.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(WITH_LAME)
|
|
|
|
#include <lame/lame.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(WITH_FAAD2)
|
|
|
|
#include <neaacdec.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(WITH_FAAC)
|
|
|
|
#include <faac.h>
|
|
|
|
#endif
|
|
|
|
|
2018-11-13 12:59:30 +03:00
|
|
|
#if defined(WITH_SOXR)
|
|
|
|
#include <soxr.h>
|
|
|
|
#endif
|
|
|
|
|
2018-11-09 11:08:06 +03:00
|
|
|
#else
|
|
|
|
#include "dsp_ffmpeg.h"
|
|
|
|
#endif
|
|
|
|
|
2018-02-20 14:15:30 +03:00
|
|
|
#define TAG FREERDP_TAG("dsp")
|
|
|
|
|
2018-11-09 11:08:06 +03:00
|
|
|
#if !defined(WITH_DSP_FFMPEG)
|
|
|
|
|
2022-02-14 16:59:22 +03:00
|
|
|
typedef union
|
|
|
|
{
|
2018-02-20 14:15:30 +03:00
|
|
|
struct
|
|
|
|
{
|
2021-08-23 15:49:28 +03:00
|
|
|
size_t packet_size;
|
2018-02-20 14:15:30 +03:00
|
|
|
INT16 last_sample[2];
|
|
|
|
INT16 last_step[2];
|
|
|
|
} ima;
|
|
|
|
struct
|
|
|
|
{
|
|
|
|
BYTE predictor[2];
|
|
|
|
INT32 delta[2];
|
|
|
|
INT32 sample1[2];
|
|
|
|
INT32 sample2[2];
|
|
|
|
} ms;
|
2022-02-14 16:59:22 +03:00
|
|
|
} ADPCM;
|
2018-02-20 14:15:30 +03:00
|
|
|
|
2022-02-14 16:59:22 +03:00
|
|
|
struct S_FREERDP_DSP_CONTEXT
|
2018-02-20 14:15:30 +03:00
|
|
|
{
|
|
|
|
BOOL encoder;
|
|
|
|
|
|
|
|
ADPCM adpcm;
|
|
|
|
AUDIO_FORMAT format;
|
|
|
|
|
2021-08-23 15:49:28 +03:00
|
|
|
wStream* channelmix;
|
2018-02-20 14:15:30 +03:00
|
|
|
wStream* resample;
|
2021-08-23 15:49:28 +03:00
|
|
|
wStream* buffer;
|
2018-02-20 14:15:30 +03:00
|
|
|
|
|
|
|
#if defined(WITH_GSM)
|
|
|
|
gsm gsm;
|
|
|
|
#endif
|
|
|
|
#if defined(WITH_LAME)
|
|
|
|
lame_t lame;
|
|
|
|
hip_t hip;
|
|
|
|
#endif
|
|
|
|
#if defined(WITH_FAAD2)
|
|
|
|
NeAACDecHandle faad;
|
|
|
|
BOOL faadSetup;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(WITH_FAAC)
|
|
|
|
faacEncHandle faac;
|
|
|
|
unsigned long faacInputSamples;
|
|
|
|
unsigned long faacMaxOutputBytes;
|
|
|
|
#endif
|
2018-11-13 12:59:30 +03:00
|
|
|
|
|
|
|
#if defined(WITH_SOXR)
|
|
|
|
soxr_t sox;
|
|
|
|
#endif
|
2018-02-20 14:15:30 +03:00
|
|
|
};
|
|
|
|
|
2019-01-24 13:00:48 +03:00
|
|
|
static INT16 read_int16(const BYTE* src)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
return (INT16)(src[0] | (src[1] << 8));
|
2019-01-24 13:00:48 +03:00
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
static BOOL freerdp_dsp_channel_mix(FREERDP_DSP_CONTEXT* context, const BYTE* src, size_t size,
|
|
|
|
const AUDIO_FORMAT* srcFormat, const BYTE** data,
|
|
|
|
size_t* length)
|
2018-12-04 13:38:02 +03:00
|
|
|
{
|
|
|
|
UINT32 bpp;
|
|
|
|
size_t samples;
|
|
|
|
size_t x, y;
|
|
|
|
|
|
|
|
if (!context || !data || !length)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (srcFormat->wFormatTag != WAVE_FORMAT_PCM)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
bpp = srcFormat->wBitsPerSample > 8 ? 2 : 1;
|
2021-08-23 15:49:28 +03:00
|
|
|
samples = size / bpp / srcFormat->nChannels;
|
2018-12-04 13:38:02 +03:00
|
|
|
|
|
|
|
if (context->format.nChannels == srcFormat->nChannels)
|
|
|
|
{
|
|
|
|
*data = src;
|
|
|
|
*length = size;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2021-08-23 15:49:28 +03:00
|
|
|
Stream_SetPosition(context->channelmix, 0);
|
2018-12-04 13:38:02 +03:00
|
|
|
|
|
|
|
/* Destination has more channels than source */
|
|
|
|
if (context->format.nChannels > srcFormat->nChannels)
|
|
|
|
{
|
|
|
|
switch (srcFormat->nChannels)
|
|
|
|
{
|
|
|
|
case 1:
|
2021-08-23 15:49:28 +03:00
|
|
|
if (!Stream_EnsureCapacity(context->channelmix, size * 2))
|
2018-12-04 13:38:02 +03:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
for (x = 0; x < samples; x++)
|
|
|
|
{
|
|
|
|
for (y = 0; y < bpp; y++)
|
2021-08-23 15:49:28 +03:00
|
|
|
Stream_Write_UINT8(context->channelmix, src[x * bpp + y]);
|
2018-12-04 13:38:02 +03:00
|
|
|
|
|
|
|
for (y = 0; y < bpp; y++)
|
2021-08-23 15:49:28 +03:00
|
|
|
Stream_Write_UINT8(context->channelmix, src[x * bpp + y]);
|
2018-12-04 13:38:02 +03:00
|
|
|
}
|
|
|
|
|
2021-08-23 15:49:28 +03:00
|
|
|
Stream_SealLength(context->channelmix);
|
|
|
|
*data = Stream_Buffer(context->channelmix);
|
|
|
|
*length = Stream_Length(context->channelmix);
|
2018-12-04 13:38:02 +03:00
|
|
|
return TRUE;
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
case 2: /* We only support stereo, so we can not handle this case. */
|
2018-12-04 13:38:02 +03:00
|
|
|
default: /* Unsupported number of channels */
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Destination has less channels than source */
|
|
|
|
switch (srcFormat->nChannels)
|
|
|
|
{
|
|
|
|
case 2:
|
2021-08-23 15:49:28 +03:00
|
|
|
if (!Stream_EnsureCapacity(context->channelmix, size / 2))
|
2018-12-04 13:38:02 +03:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
/* Simply drop second channel.
|
|
|
|
* TODO: Calculate average */
|
|
|
|
for (x = 0; x < samples; x++)
|
|
|
|
{
|
|
|
|
for (y = 0; y < bpp; y++)
|
2021-08-23 15:49:28 +03:00
|
|
|
Stream_Write_UINT8(context->channelmix, src[2 * x * bpp + y]);
|
2018-12-04 13:38:02 +03:00
|
|
|
}
|
|
|
|
|
2021-08-23 15:49:28 +03:00
|
|
|
Stream_SealLength(context->channelmix);
|
|
|
|
*data = Stream_Buffer(context->channelmix);
|
|
|
|
*length = Stream_Length(context->channelmix);
|
2018-12-04 13:38:02 +03:00
|
|
|
return TRUE;
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
case 1: /* Invalid, do we want to use a 0 channel sound? */
|
2018-12-04 13:38:02 +03:00
|
|
|
default: /* Unsupported number of channels */
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2013-02-27 02:54:16 +04:00
|
|
|
/**
|
|
|
|
* Microsoft Multimedia Standards Update
|
|
|
|
* http://download.microsoft.com/download/9/8/6/9863C72A-A3AA-4DDB-B1BA-CA8D17EFD2D4/RIFFNEW.pdf
|
|
|
|
*/
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
static BOOL freerdp_dsp_resample(FREERDP_DSP_CONTEXT* context, const BYTE* src, size_t size,
|
|
|
|
const AUDIO_FORMAT* srcFormat, const BYTE** data, size_t* length)
|
2011-08-14 17:46:02 +04:00
|
|
|
{
|
2018-11-13 14:29:19 +03:00
|
|
|
#if defined(WITH_SOXR)
|
2018-11-13 12:59:30 +03:00
|
|
|
soxr_error_t error;
|
|
|
|
size_t idone, odone;
|
2018-11-07 16:14:15 +03:00
|
|
|
size_t sframes, rframes;
|
|
|
|
size_t rsize;
|
|
|
|
size_t sbytes, rbytes;
|
2019-01-24 13:00:48 +03:00
|
|
|
#endif
|
2018-11-09 12:15:14 +03:00
|
|
|
size_t srcBytesPerFrame, dstBytesPerFrame;
|
|
|
|
size_t srcChannels, dstChannels;
|
|
|
|
AUDIO_FORMAT format;
|
|
|
|
|
|
|
|
if (srcFormat->wFormatTag != WAVE_FORMAT_PCM)
|
|
|
|
{
|
|
|
|
WLog_ERR(TAG, "%s requires %s for sample input, got %s", __FUNCTION__,
|
|
|
|
audio_format_get_tag_string(WAVE_FORMAT_PCM),
|
|
|
|
audio_format_get_tag_string(srcFormat->wFormatTag));
|
2018-11-07 16:14:15 +03:00
|
|
|
return FALSE;
|
2018-11-09 12:15:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
srcChannels = srcFormat->nChannels;
|
|
|
|
dstChannels = context->format.nChannels;
|
|
|
|
srcBytesPerFrame = (srcFormat->wBitsPerSample > 8) ? 2 : 1;
|
|
|
|
dstBytesPerFrame = (context->format.wBitsPerSample > 8) ? 2 : 1;
|
|
|
|
/* We want to ignore differences of source and destination format. */
|
|
|
|
format = *srcFormat;
|
|
|
|
format.wFormatTag = WAVE_FORMAT_UNKNOWN;
|
2021-08-23 15:49:28 +03:00
|
|
|
format.wBitsPerSample = 0;
|
2018-11-09 12:15:14 +03:00
|
|
|
|
|
|
|
if (audio_format_compatible(&format, &context->format))
|
2021-08-23 15:49:28 +03:00
|
|
|
{
|
|
|
|
*data = src;
|
|
|
|
*length = size;
|
2018-11-09 12:15:14 +03:00
|
|
|
return TRUE;
|
2021-08-23 15:49:28 +03:00
|
|
|
}
|
2018-11-09 12:15:14 +03:00
|
|
|
|
2018-11-13 13:06:38 +03:00
|
|
|
#if defined(WITH_SOXR)
|
2018-11-09 12:15:14 +03:00
|
|
|
sbytes = srcChannels * srcBytesPerFrame;
|
2018-11-07 16:14:15 +03:00
|
|
|
sframes = size / sbytes;
|
2018-11-09 12:15:14 +03:00
|
|
|
rbytes = dstBytesPerFrame * dstChannels;
|
2018-11-07 16:14:15 +03:00
|
|
|
/* Integer rounding correct division */
|
|
|
|
rframes = (sframes * context->format.nSamplesPerSec + (srcFormat->nSamplesPerSec + 1) / 2) /
|
|
|
|
srcFormat->nSamplesPerSec;
|
2018-11-09 12:15:14 +03:00
|
|
|
rsize = rframes * rbytes;
|
2012-05-22 00:01:24 +04:00
|
|
|
|
2018-11-09 12:15:14 +03:00
|
|
|
if (!Stream_EnsureCapacity(context->resample, rsize))
|
2018-02-20 14:15:30 +03:00
|
|
|
return FALSE;
|
2014-04-29 12:32:16 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
error = soxr_process(context->sox, src, sframes, &idone, Stream_Buffer(context->resample),
|
2018-11-13 13:03:09 +03:00
|
|
|
Stream_Capacity(context->resample) / rbytes, &odone);
|
2018-11-13 12:59:30 +03:00
|
|
|
Stream_SetLength(context->resample, odone * rbytes);
|
2018-11-09 12:15:14 +03:00
|
|
|
*data = Stream_Buffer(context->resample);
|
|
|
|
*length = Stream_Length(context->resample);
|
2018-11-13 12:59:30 +03:00
|
|
|
return (error == 0) ? TRUE : FALSE;
|
2018-11-13 13:06:38 +03:00
|
|
|
#else
|
|
|
|
WLog_ERR(TAG, "Missing resample support, recompile -DWITH_SOXR=ON or -DWITH_DSP_FFMPEG=ON");
|
|
|
|
return FALSE;
|
|
|
|
#endif
|
2011-08-14 17:46:02 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Microsoft IMA ADPCM specification:
|
|
|
|
*
|
|
|
|
* http://wiki.multimedia.cx/index.php?title=Microsoft_IMA_ADPCM
|
|
|
|
* http://wiki.multimedia.cx/index.php?title=IMA_ADPCM
|
|
|
|
*/
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
static const INT16 ima_step_index_table[] = {
|
|
|
|
-1, -1, -1, -1, 2, 4, 6, 8, -1, -1, -1, -1, 2, 4, 6, 8
|
|
|
|
};
|
2011-08-14 17:46:02 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
static const INT16 ima_step_size_table[] = {
|
|
|
|
7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 19, 21, 23,
|
|
|
|
25, 28, 31, 34, 37, 41, 45, 50, 55, 60, 66, 73, 80,
|
|
|
|
88, 97, 107, 118, 130, 143, 157, 173, 190, 209, 230, 253, 279,
|
|
|
|
307, 337, 371, 408, 449, 494, 544, 598, 658, 724, 796, 876, 963,
|
|
|
|
1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066, 2272, 2499, 2749, 3024, 3327,
|
|
|
|
3660, 4026, 4428, 4871, 5358, 5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487,
|
|
|
|
12635, 13899, 15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
|
2011-08-14 17:46:02 +04:00
|
|
|
};
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
static UINT16 dsp_decode_ima_adpcm_sample(ADPCM* adpcm, unsigned int channel, BYTE sample)
|
2011-08-14 17:46:02 +04:00
|
|
|
{
|
2012-10-09 11:26:39 +04:00
|
|
|
INT32 ss;
|
|
|
|
INT32 d;
|
2012-05-24 17:26:17 +04:00
|
|
|
ss = ima_step_size_table[adpcm->ima.last_step[channel]];
|
2011-08-14 17:46:02 +04:00
|
|
|
d = (ss >> 3);
|
2013-02-27 02:54:16 +04:00
|
|
|
|
2011-08-14 17:46:02 +04:00
|
|
|
if (sample & 1)
|
|
|
|
d += (ss >> 2);
|
2018-02-20 14:15:30 +03:00
|
|
|
|
2011-08-14 17:46:02 +04:00
|
|
|
if (sample & 2)
|
|
|
|
d += (ss >> 1);
|
2018-02-20 14:15:30 +03:00
|
|
|
|
2011-08-14 17:46:02 +04:00
|
|
|
if (sample & 4)
|
|
|
|
d += ss;
|
2018-02-20 14:15:30 +03:00
|
|
|
|
2011-08-14 17:46:02 +04:00
|
|
|
if (sample & 8)
|
|
|
|
d = -d;
|
2013-02-27 02:54:16 +04:00
|
|
|
|
2012-05-24 17:26:17 +04:00
|
|
|
d += adpcm->ima.last_sample[channel];
|
2011-08-14 17:46:02 +04:00
|
|
|
|
|
|
|
if (d < -32768)
|
|
|
|
d = -32768;
|
|
|
|
else if (d > 32767)
|
|
|
|
d = 32767;
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
adpcm->ima.last_sample[channel] = (INT16)d;
|
2012-05-24 17:26:17 +04:00
|
|
|
adpcm->ima.last_step[channel] += ima_step_index_table[sample];
|
2013-02-27 02:54:16 +04:00
|
|
|
|
2012-05-24 17:26:17 +04:00
|
|
|
if (adpcm->ima.last_step[channel] < 0)
|
|
|
|
adpcm->ima.last_step[channel] = 0;
|
|
|
|
else if (adpcm->ima.last_step[channel] > 88)
|
|
|
|
adpcm->ima.last_step[channel] = 88;
|
2011-08-14 17:46:02 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
return (UINT16)d;
|
2011-08-14 17:46:02 +04:00
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
static BOOL freerdp_dsp_decode_ima_adpcm(FREERDP_DSP_CONTEXT* context, const BYTE* src, size_t size,
|
|
|
|
wStream* out)
|
2011-08-14 17:46:02 +04:00
|
|
|
{
|
2012-10-09 11:01:37 +04:00
|
|
|
BYTE sample;
|
|
|
|
UINT16 decoded;
|
2019-01-24 13:00:48 +03:00
|
|
|
size_t out_size = size * 4;
|
2018-02-20 14:15:30 +03:00
|
|
|
UINT32 channel;
|
|
|
|
const UINT32 block_size = context->format.nBlockAlign;
|
|
|
|
const UINT32 channels = context->format.nChannels;
|
2019-01-24 13:00:48 +03:00
|
|
|
size_t i;
|
2011-08-14 17:46:02 +04:00
|
|
|
|
2018-02-20 14:15:30 +03:00
|
|
|
if (!Stream_EnsureCapacity(out, out_size))
|
|
|
|
return FALSE;
|
2014-04-29 12:32:16 +04:00
|
|
|
|
2011-08-14 17:46:02 +04:00
|
|
|
while (size > 0)
|
|
|
|
{
|
|
|
|
if (size % block_size == 0)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
context->adpcm.ima.last_sample[0] =
|
|
|
|
(INT16)(((UINT16)(*src)) | (((UINT16)(*(src + 1))) << 8));
|
2018-02-20 14:15:30 +03:00
|
|
|
context->adpcm.ima.last_step[0] = (INT16)(*(src + 2));
|
2011-08-14 17:46:02 +04:00
|
|
|
src += 4;
|
|
|
|
size -= 4;
|
2012-05-09 12:15:23 +04:00
|
|
|
out_size -= 16;
|
2013-02-27 02:54:16 +04:00
|
|
|
|
2011-08-14 17:46:02 +04:00
|
|
|
if (channels > 1)
|
|
|
|
{
|
2019-11-06 17:24:51 +03:00
|
|
|
context->adpcm.ima.last_sample[1] =
|
|
|
|
(INT16)(((UINT16)(*src)) | (((UINT16)(*(src + 1))) << 8));
|
2018-02-20 14:15:30 +03:00
|
|
|
context->adpcm.ima.last_step[1] = (INT16)(*(src + 2));
|
2011-08-14 17:46:02 +04:00
|
|
|
src += 4;
|
|
|
|
size -= 4;
|
2012-05-09 12:15:23 +04:00
|
|
|
out_size -= 16;
|
2011-08-14 17:46:02 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (channels > 1)
|
|
|
|
{
|
|
|
|
for (i = 0; i < 8; i++)
|
|
|
|
{
|
2021-10-04 09:48:38 +03:00
|
|
|
BYTE* dst = Stream_Pointer(out);
|
|
|
|
|
2011-08-14 17:46:02 +04:00
|
|
|
channel = (i < 4 ? 0 : 1);
|
|
|
|
sample = ((*src) & 0x0f);
|
2012-05-09 12:15:23 +04:00
|
|
|
decoded = dsp_decode_ima_adpcm_sample(&context->adpcm, channel, sample);
|
2013-02-27 02:54:16 +04:00
|
|
|
dst[((i & 3) << 3) + (channel << 1)] = (decoded & 0xFF);
|
2011-08-14 17:46:02 +04:00
|
|
|
dst[((i & 3) << 3) + (channel << 1) + 1] = (decoded >> 8);
|
|
|
|
sample = ((*src) >> 4);
|
2012-05-09 12:15:23 +04:00
|
|
|
decoded = dsp_decode_ima_adpcm_sample(&context->adpcm, channel, sample);
|
2013-02-27 02:54:16 +04:00
|
|
|
dst[((i & 3) << 3) + (channel << 1) + 4] = (decoded & 0xFF);
|
2011-08-14 17:46:02 +04:00
|
|
|
dst[((i & 3) << 3) + (channel << 1) + 5] = (decoded >> 8);
|
|
|
|
src++;
|
|
|
|
}
|
2013-02-27 02:54:16 +04:00
|
|
|
|
2021-10-04 09:48:38 +03:00
|
|
|
if (!Stream_SafeSeek(out, 32))
|
|
|
|
return FALSE;
|
2011-08-14 17:46:02 +04:00
|
|
|
size -= 8;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-10-04 09:48:38 +03:00
|
|
|
BYTE* dst = Stream_Pointer(out);
|
|
|
|
if (!Stream_SafeSeek(out, 4))
|
|
|
|
return FALSE;
|
|
|
|
|
2011-08-14 17:46:02 +04:00
|
|
|
sample = ((*src) & 0x0f);
|
2012-05-09 12:15:23 +04:00
|
|
|
decoded = dsp_decode_ima_adpcm_sample(&context->adpcm, 0, sample);
|
2013-02-27 02:54:16 +04:00
|
|
|
*dst++ = (decoded & 0xFF);
|
2011-08-14 17:46:02 +04:00
|
|
|
*dst++ = (decoded >> 8);
|
|
|
|
sample = ((*src) >> 4);
|
2012-05-09 12:15:23 +04:00
|
|
|
decoded = dsp_decode_ima_adpcm_sample(&context->adpcm, 0, sample);
|
2013-02-27 02:54:16 +04:00
|
|
|
*dst++ = (decoded & 0xFF);
|
2011-08-14 17:46:02 +04:00
|
|
|
*dst++ = (decoded >> 8);
|
|
|
|
src++;
|
|
|
|
size--;
|
|
|
|
}
|
|
|
|
}
|
2012-05-09 12:15:23 +04:00
|
|
|
|
2018-02-20 14:15:30 +03:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(WITH_GSM)
|
2019-11-06 17:24:51 +03:00
|
|
|
static BOOL freerdp_dsp_decode_gsm610(FREERDP_DSP_CONTEXT* context, const BYTE* src, size_t size,
|
|
|
|
wStream* out)
|
2018-02-20 14:15:30 +03:00
|
|
|
{
|
|
|
|
size_t offset = 0;
|
|
|
|
|
|
|
|
while (offset < size)
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
gsm_signal gsmBlockBuffer[160] = { 0 };
|
2019-11-06 17:24:51 +03:00
|
|
|
rc = gsm_decode(context->gsm, (gsm_byte*)/* API does not modify */ &src[offset],
|
|
|
|
gsmBlockBuffer);
|
2018-04-03 12:18:22 +03:00
|
|
|
|
2018-02-20 14:15:30 +03:00
|
|
|
if (rc < 0)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if ((offset % 65) == 0)
|
|
|
|
offset += 33;
|
|
|
|
else
|
|
|
|
offset += 32;
|
|
|
|
|
|
|
|
if (!Stream_EnsureRemainingCapacity(out, sizeof(gsmBlockBuffer)))
|
|
|
|
return FALSE;
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
Stream_Write(out, (void*)gsmBlockBuffer, sizeof(gsmBlockBuffer));
|
2018-02-20 14:15:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
static BOOL freerdp_dsp_encode_gsm610(FREERDP_DSP_CONTEXT* context, const BYTE* src, size_t size,
|
|
|
|
wStream* out)
|
2018-02-20 14:15:30 +03:00
|
|
|
{
|
|
|
|
size_t offset = 0;
|
|
|
|
|
|
|
|
while (offset < size)
|
|
|
|
{
|
2019-01-24 13:00:48 +03:00
|
|
|
const gsm_signal* signal = (const gsm_signal*)&src[offset];
|
2018-02-20 14:15:30 +03:00
|
|
|
|
|
|
|
if (!Stream_EnsureRemainingCapacity(out, sizeof(gsm_frame)))
|
|
|
|
return FALSE;
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
gsm_encode(context->gsm, (gsm_signal*)/* API does not modify */ signal,
|
|
|
|
Stream_Pointer(out));
|
2018-04-03 12:18:22 +03:00
|
|
|
|
2018-02-20 14:15:30 +03:00
|
|
|
if ((offset % 65) == 0)
|
|
|
|
Stream_Seek(out, 33);
|
|
|
|
else
|
|
|
|
Stream_Seek(out, 32);
|
2018-04-03 12:18:22 +03:00
|
|
|
|
2018-02-20 14:15:30 +03:00
|
|
|
offset += 160;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(WITH_LAME)
|
2019-11-06 17:24:51 +03:00
|
|
|
static BOOL freerdp_dsp_decode_mp3(FREERDP_DSP_CONTEXT* context, const BYTE* src, size_t size,
|
|
|
|
wStream* out)
|
2018-02-20 14:15:30 +03:00
|
|
|
{
|
|
|
|
int rc, x;
|
|
|
|
short* pcm_l;
|
|
|
|
short* pcm_r;
|
|
|
|
size_t buffer_size;
|
|
|
|
|
|
|
|
if (!context || !src || !out)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
buffer_size = 2 * context->format.nChannels * context->format.nSamplesPerSec;
|
|
|
|
|
|
|
|
if (!Stream_EnsureCapacity(context->buffer, 2 * buffer_size))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
pcm_l = (short*)Stream_Buffer(context->buffer);
|
|
|
|
pcm_r = (short*)Stream_Buffer(context->buffer) + buffer_size;
|
2019-11-06 17:24:51 +03:00
|
|
|
rc = hip_decode(context->hip, (unsigned char*)/* API is not modifying content */ src, size,
|
|
|
|
pcm_l, pcm_r);
|
2018-02-20 14:15:30 +03:00
|
|
|
|
|
|
|
if (rc <= 0)
|
|
|
|
return FALSE;
|
|
|
|
|
2019-01-24 13:00:48 +03:00
|
|
|
if (!Stream_EnsureRemainingCapacity(out, (size_t)rc * context->format.nChannels * 2))
|
2018-02-20 14:15:30 +03:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
for (x = 0; x < rc; x++)
|
|
|
|
{
|
2019-01-24 13:00:48 +03:00
|
|
|
Stream_Write_UINT16(out, (UINT16)pcm_l[x]);
|
|
|
|
Stream_Write_UINT16(out, (UINT16)pcm_r[x]);
|
2018-02-20 14:15:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
static BOOL freerdp_dsp_encode_mp3(FREERDP_DSP_CONTEXT* context, const BYTE* src, size_t size,
|
|
|
|
wStream* out)
|
2018-02-20 14:15:30 +03:00
|
|
|
{
|
|
|
|
size_t samples_per_channel;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
if (!context || !src || !out)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
samples_per_channel = size / context->format.nChannels / context->format.wBitsPerSample / 8;
|
|
|
|
|
|
|
|
/* Ensure worst case buffer size for mp3 stream taken from LAME header */
|
2019-01-24 13:00:48 +03:00
|
|
|
if (!Stream_EnsureRemainingCapacity(out, 5 / 4 * samples_per_channel + 7200))
|
2018-02-20 14:15:30 +03:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
samples_per_channel = size / 2 /* size of a sample */ / context->format.nChannels;
|
|
|
|
rc = lame_encode_buffer_interleaved(context->lame, (short*)src, samples_per_channel,
|
|
|
|
Stream_Pointer(out), Stream_GetRemainingCapacity(out));
|
|
|
|
|
|
|
|
if (rc < 0)
|
|
|
|
return FALSE;
|
|
|
|
|
2019-01-24 13:00:48 +03:00
|
|
|
Stream_Seek(out, (size_t)rc);
|
2018-02-20 14:15:30 +03:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(WITH_FAAC)
|
2019-11-06 17:24:51 +03:00
|
|
|
static BOOL freerdp_dsp_encode_faac(FREERDP_DSP_CONTEXT* context, const BYTE* src, size_t size,
|
|
|
|
wStream* out)
|
2018-02-20 14:15:30 +03:00
|
|
|
{
|
2019-01-24 13:00:48 +03:00
|
|
|
const int16_t* inSamples = (const int16_t*)src;
|
2018-11-13 12:59:30 +03:00
|
|
|
unsigned int bpp;
|
2019-01-24 13:00:48 +03:00
|
|
|
size_t nrSamples, x;
|
2018-02-20 14:15:30 +03:00
|
|
|
int rc;
|
|
|
|
|
|
|
|
if (!context || !src || !out)
|
|
|
|
return FALSE;
|
|
|
|
|
2021-05-11 12:32:50 +03:00
|
|
|
bpp = context->format.wBitsPerSample / 8;
|
2018-11-13 12:59:30 +03:00
|
|
|
nrSamples = size / bpp;
|
2018-02-20 14:15:30 +03:00
|
|
|
|
2021-05-11 12:32:50 +03:00
|
|
|
if (!Stream_EnsureRemainingCapacity(context->buffer, nrSamples * sizeof(int16_t)))
|
2018-02-20 14:15:30 +03:00
|
|
|
return FALSE;
|
|
|
|
|
2021-05-11 12:32:50 +03:00
|
|
|
for (x = 0; x < nrSamples; x++)
|
|
|
|
{
|
|
|
|
Stream_Write_INT16(context->buffer, inSamples[x]);
|
|
|
|
if (Stream_GetPosition(context->buffer) / bpp >= context->faacInputSamples)
|
|
|
|
{
|
|
|
|
if (!Stream_EnsureRemainingCapacity(out, context->faacMaxOutputBytes))
|
|
|
|
return FALSE;
|
|
|
|
rc = faacEncEncode(context->faac, (int32_t*)Stream_Buffer(context->buffer),
|
|
|
|
context->faacInputSamples, Stream_Pointer(out),
|
|
|
|
Stream_GetRemainingCapacity(out));
|
|
|
|
if (rc < 0)
|
|
|
|
return FALSE;
|
|
|
|
if (rc > 0)
|
|
|
|
Stream_Seek(out, (size_t)rc);
|
|
|
|
Stream_SetPosition(context->buffer, 0);
|
|
|
|
}
|
|
|
|
}
|
2018-02-20 14:15:30 +03:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(WITH_FAAD2)
|
2019-11-06 17:24:51 +03:00
|
|
|
static BOOL freerdp_dsp_decode_faad(FREERDP_DSP_CONTEXT* context, const BYTE* src, size_t size,
|
|
|
|
wStream* out)
|
2018-02-20 14:15:30 +03:00
|
|
|
{
|
|
|
|
NeAACDecFrameInfo info;
|
|
|
|
void* output;
|
|
|
|
size_t offset = 0;
|
|
|
|
|
|
|
|
if (!context || !src || !out)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (!context->faadSetup)
|
|
|
|
{
|
2022-04-27 22:02:18 +03:00
|
|
|
union
|
|
|
|
{
|
|
|
|
const void* cpv;
|
|
|
|
void* pv;
|
|
|
|
} cnv;
|
2018-02-20 14:15:30 +03:00
|
|
|
unsigned long samplerate;
|
|
|
|
unsigned char channels;
|
2022-04-27 22:02:18 +03:00
|
|
|
long err;
|
|
|
|
cnv.cpv = src;
|
|
|
|
err = NeAACDecInit(context->faad, /* API is not modifying content */ cnv.pv, size,
|
|
|
|
&samplerate, &channels);
|
2018-02-20 14:15:30 +03:00
|
|
|
|
|
|
|
if (err != 0)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (channels != context->format.nChannels)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (samplerate != context->format.nSamplesPerSec)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
context->faadSetup = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (offset < size)
|
|
|
|
{
|
2022-04-27 22:02:18 +03:00
|
|
|
union
|
|
|
|
{
|
|
|
|
const void* cpv;
|
|
|
|
void* pv;
|
|
|
|
} cnv;
|
2018-02-20 14:15:30 +03:00
|
|
|
size_t outSize;
|
|
|
|
void* sample_buffer;
|
|
|
|
outSize = context->format.nSamplesPerSec * context->format.nChannels *
|
|
|
|
context->format.wBitsPerSample / 8;
|
|
|
|
|
|
|
|
if (!Stream_EnsureRemainingCapacity(out, outSize))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
sample_buffer = Stream_Pointer(out);
|
2022-04-27 22:02:18 +03:00
|
|
|
|
|
|
|
cnv.cpv = &src[offset];
|
|
|
|
output = NeAACDecDecode2(context->faad, &info, cnv.pv, size - offset, &sample_buffer,
|
|
|
|
Stream_GetRemainingCapacity(out));
|
2018-02-20 14:15:30 +03:00
|
|
|
|
|
|
|
if (info.error != 0)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
offset += info.bytesconsumed;
|
|
|
|
|
|
|
|
if (info.samples == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
Stream_Seek(out, info.samples * context->format.wBitsPerSample / 8);
|
|
|
|
}
|
|
|
|
|
2014-04-29 12:32:16 +04:00
|
|
|
return TRUE;
|
2011-08-14 17:46:02 +04:00
|
|
|
}
|
|
|
|
|
2018-02-20 14:15:30 +03:00
|
|
|
#endif
|
|
|
|
|
2011-08-14 17:46:02 +04:00
|
|
|
/**
|
|
|
|
* 0 1 2 3
|
|
|
|
* 2 0 6 4 10 8 14 12 <left>
|
|
|
|
*
|
|
|
|
* 4 5 6 7
|
|
|
|
* 3 1 7 5 11 9 15 13 <right>
|
|
|
|
*/
|
|
|
|
static const struct
|
|
|
|
{
|
2012-10-09 11:01:37 +04:00
|
|
|
BYTE byte_num;
|
|
|
|
BYTE byte_shift;
|
2019-11-06 17:24:51 +03:00
|
|
|
} ima_stereo_encode_map[] = { { 0, 0 }, { 4, 0 }, { 0, 4 }, { 4, 4 }, { 1, 0 }, { 5, 0 },
|
|
|
|
{ 1, 4 }, { 5, 4 }, { 2, 0 }, { 6, 0 }, { 2, 4 }, { 6, 4 },
|
|
|
|
{ 3, 0 }, { 7, 0 }, { 3, 4 }, { 7, 4 } };
|
2011-08-14 17:46:02 +04:00
|
|
|
|
2013-02-27 02:54:16 +04:00
|
|
|
static BYTE dsp_encode_ima_adpcm_sample(ADPCM* adpcm, int channel, INT16 sample)
|
2011-08-14 17:46:02 +04:00
|
|
|
{
|
2012-10-09 11:26:39 +04:00
|
|
|
INT32 e;
|
|
|
|
INT32 d;
|
|
|
|
INT32 ss;
|
2012-10-09 11:01:37 +04:00
|
|
|
BYTE enc;
|
2012-10-09 11:26:39 +04:00
|
|
|
INT32 diff;
|
2012-05-24 17:26:17 +04:00
|
|
|
ss = ima_step_size_table[adpcm->ima.last_step[channel]];
|
|
|
|
d = e = sample - adpcm->ima.last_sample[channel];
|
2011-08-14 17:46:02 +04:00
|
|
|
diff = ss >> 3;
|
|
|
|
enc = 0;
|
2013-02-27 02:54:16 +04:00
|
|
|
|
2011-08-14 17:46:02 +04:00
|
|
|
if (e < 0)
|
|
|
|
{
|
|
|
|
enc = 8;
|
|
|
|
e = -e;
|
|
|
|
}
|
2013-02-27 02:54:16 +04:00
|
|
|
|
2011-08-14 17:46:02 +04:00
|
|
|
if (e >= ss)
|
|
|
|
{
|
|
|
|
enc |= 4;
|
|
|
|
e -= ss;
|
|
|
|
}
|
2013-02-27 02:54:16 +04:00
|
|
|
|
2011-08-14 17:46:02 +04:00
|
|
|
ss >>= 1;
|
2013-02-27 02:54:16 +04:00
|
|
|
|
2011-08-14 17:46:02 +04:00
|
|
|
if (e >= ss)
|
|
|
|
{
|
|
|
|
enc |= 2;
|
|
|
|
e -= ss;
|
|
|
|
}
|
2013-02-27 02:54:16 +04:00
|
|
|
|
2011-08-14 17:46:02 +04:00
|
|
|
ss >>= 1;
|
2013-02-27 02:54:16 +04:00
|
|
|
|
2011-08-14 17:46:02 +04:00
|
|
|
if (e >= ss)
|
|
|
|
{
|
|
|
|
enc |= 1;
|
|
|
|
e -= ss;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (d < 0)
|
|
|
|
diff = d + e - diff;
|
|
|
|
else
|
|
|
|
diff = d - e + diff;
|
|
|
|
|
2012-05-24 17:26:17 +04:00
|
|
|
diff += adpcm->ima.last_sample[channel];
|
2013-02-27 02:54:16 +04:00
|
|
|
|
2011-08-14 17:46:02 +04:00
|
|
|
if (diff < -32768)
|
|
|
|
diff = -32768;
|
|
|
|
else if (diff > 32767)
|
|
|
|
diff = 32767;
|
2013-02-27 02:54:16 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
adpcm->ima.last_sample[channel] = (INT16)diff;
|
2012-05-24 17:26:17 +04:00
|
|
|
adpcm->ima.last_step[channel] += ima_step_index_table[enc];
|
2013-02-27 02:54:16 +04:00
|
|
|
|
2012-05-24 17:26:17 +04:00
|
|
|
if (adpcm->ima.last_step[channel] < 0)
|
|
|
|
adpcm->ima.last_step[channel] = 0;
|
|
|
|
else if (adpcm->ima.last_step[channel] > 88)
|
|
|
|
adpcm->ima.last_step[channel] = 88;
|
2011-08-14 17:46:02 +04:00
|
|
|
|
|
|
|
return enc;
|
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
static BOOL freerdp_dsp_encode_ima_adpcm(FREERDP_DSP_CONTEXT* context, const BYTE* src, size_t size,
|
|
|
|
wStream* out)
|
2011-08-14 17:46:02 +04:00
|
|
|
{
|
2013-02-27 02:54:16 +04:00
|
|
|
int i;
|
2021-10-04 09:48:38 +03:00
|
|
|
size_t start;
|
2012-10-09 11:01:37 +04:00
|
|
|
INT16 sample;
|
|
|
|
BYTE encoded;
|
2018-07-17 13:57:57 +03:00
|
|
|
size_t align;
|
2013-02-27 02:54:16 +04:00
|
|
|
|
2018-07-17 11:30:35 +03:00
|
|
|
if (!Stream_EnsureRemainingCapacity(out, size))
|
2018-02-20 14:15:30 +03:00
|
|
|
return FALSE;
|
2014-04-29 12:32:16 +04:00
|
|
|
|
2021-10-04 09:48:38 +03:00
|
|
|
start = Stream_GetPosition(context->buffer);
|
|
|
|
|
2018-07-17 13:57:57 +03:00
|
|
|
align = (context->format.nChannels > 1) ? 32 : 4;
|
2013-02-27 02:54:16 +04:00
|
|
|
|
2021-08-23 15:49:28 +03:00
|
|
|
while (size >= align)
|
2011-08-14 17:46:02 +04:00
|
|
|
{
|
2021-10-04 09:48:38 +03:00
|
|
|
if ((Stream_GetPosition(context->buffer) - start) % context->format.nBlockAlign == 0)
|
2011-08-14 17:46:02 +04:00
|
|
|
{
|
2021-10-04 09:48:38 +03:00
|
|
|
Stream_Write_UINT8(context->buffer, context->adpcm.ima.last_sample[0] & 0xFF);
|
|
|
|
Stream_Write_UINT8(context->buffer, (context->adpcm.ima.last_sample[0] >> 8) & 0xFF);
|
|
|
|
Stream_Write_UINT8(context->buffer, (BYTE)context->adpcm.ima.last_step[0]);
|
|
|
|
Stream_Write_UINT8(context->buffer, 0);
|
2013-02-27 02:54:16 +04:00
|
|
|
|
2018-02-20 14:15:30 +03:00
|
|
|
if (context->format.nChannels > 1)
|
2011-08-14 17:46:02 +04:00
|
|
|
{
|
2021-10-04 09:48:38 +03:00
|
|
|
Stream_Write_UINT8(context->buffer, context->adpcm.ima.last_sample[1] & 0xFF);
|
|
|
|
Stream_Write_UINT8(context->buffer,
|
|
|
|
(context->adpcm.ima.last_sample[1] >> 8) & 0xFF);
|
|
|
|
Stream_Write_UINT8(context->buffer, (BYTE)context->adpcm.ima.last_step[1]);
|
|
|
|
Stream_Write_UINT8(context->buffer, 0);
|
2011-08-14 17:46:02 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-20 14:15:30 +03:00
|
|
|
if (context->format.nChannels > 1)
|
2011-08-14 17:46:02 +04:00
|
|
|
{
|
2021-10-04 09:48:38 +03:00
|
|
|
BYTE* dst = Stream_Pointer(context->buffer);
|
2013-02-27 02:54:16 +04:00
|
|
|
ZeroMemory(dst, 8);
|
|
|
|
|
2011-08-14 17:46:02 +04:00
|
|
|
for (i = 0; i < 16; i++)
|
|
|
|
{
|
2018-02-20 14:15:30 +03:00
|
|
|
sample = (INT16)(((UINT16)(*src)) | (((UINT16)(*(src + 1))) << 8));
|
2011-08-14 17:46:02 +04:00
|
|
|
src += 2;
|
2012-05-09 12:15:23 +04:00
|
|
|
encoded = dsp_encode_ima_adpcm_sample(&context->adpcm, i % 2, sample);
|
2019-11-06 17:24:51 +03:00
|
|
|
dst[ima_stereo_encode_map[i].byte_num] |= encoded
|
|
|
|
<< ima_stereo_encode_map[i].byte_shift;
|
2011-08-14 17:46:02 +04:00
|
|
|
}
|
2013-02-27 02:54:16 +04:00
|
|
|
|
2021-10-04 09:48:38 +03:00
|
|
|
if (!Stream_SafeSeek(context->buffer, 8))
|
|
|
|
return FALSE;
|
2011-08-14 17:46:02 +04:00
|
|
|
size -= 32;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-02-20 14:15:30 +03:00
|
|
|
sample = (INT16)(((UINT16)(*src)) | (((UINT16)(*(src + 1))) << 8));
|
2011-08-14 17:46:02 +04:00
|
|
|
src += 2;
|
2012-05-09 12:15:23 +04:00
|
|
|
encoded = dsp_encode_ima_adpcm_sample(&context->adpcm, 0, sample);
|
2018-02-20 14:15:30 +03:00
|
|
|
sample = (INT16)(((UINT16)(*src)) | (((UINT16)(*(src + 1))) << 8));
|
2011-08-14 17:46:02 +04:00
|
|
|
src += 2;
|
2012-05-09 12:15:23 +04:00
|
|
|
encoded |= dsp_encode_ima_adpcm_sample(&context->adpcm, 0, sample) << 4;
|
2021-10-04 09:48:38 +03:00
|
|
|
Stream_Write_UINT8(context->buffer, encoded);
|
2011-08-14 17:46:02 +04:00
|
|
|
size -= 4;
|
|
|
|
}
|
2021-08-23 15:49:28 +03:00
|
|
|
|
2021-10-04 09:48:38 +03:00
|
|
|
if (Stream_GetPosition(context->buffer) - start == context->adpcm.ima.packet_size)
|
2021-08-23 15:49:28 +03:00
|
|
|
{
|
2021-10-04 09:48:38 +03:00
|
|
|
BYTE* bsrc = Stream_Buffer(context->buffer);
|
|
|
|
Stream_Write(out, bsrc, context->adpcm.ima.packet_size);
|
|
|
|
Stream_SetPosition(context->buffer, 0);
|
2021-08-23 15:49:28 +03:00
|
|
|
}
|
2011-08-14 17:46:02 +04:00
|
|
|
}
|
2018-02-20 14:15:30 +03:00
|
|
|
|
2014-04-29 12:32:16 +04:00
|
|
|
return TRUE;
|
2011-08-14 17:46:02 +04:00
|
|
|
}
|
|
|
|
|
2012-05-24 17:26:17 +04:00
|
|
|
/**
|
|
|
|
* Microsoft ADPCM Specification:
|
|
|
|
*
|
|
|
|
* http://wiki.multimedia.cx/index.php?title=Microsoft_ADPCM
|
|
|
|
*/
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
static const INT32 ms_adpcm_adaptation_table[] = { 230, 230, 230, 230, 307, 409, 512, 614,
|
|
|
|
768, 614, 512, 409, 307, 230, 230, 230 };
|
2012-05-24 17:26:17 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
static const INT32 ms_adpcm_coeffs1[7] = { 256, 512, 0, 192, 240, 460, 392 };
|
2012-05-24 17:26:17 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
static const INT32 ms_adpcm_coeffs2[7] = { 0, -256, 0, 64, 0, -208, -232 };
|
2012-05-24 17:26:17 +04:00
|
|
|
|
2013-02-27 02:54:16 +04:00
|
|
|
static INLINE INT16 freerdp_dsp_decode_ms_adpcm_sample(ADPCM* adpcm, BYTE sample, int channel)
|
2012-05-24 17:26:17 +04:00
|
|
|
{
|
2012-10-09 11:01:37 +04:00
|
|
|
INT8 nibble;
|
2012-10-09 11:26:39 +04:00
|
|
|
INT32 presample;
|
2019-11-06 17:24:51 +03:00
|
|
|
nibble = (sample & 0x08 ? (INT8)sample - 16 : (INT8)sample);
|
2013-02-27 02:54:16 +04:00
|
|
|
presample = ((adpcm->ms.sample1[channel] * ms_adpcm_coeffs1[adpcm->ms.predictor[channel]]) +
|
2019-11-06 17:24:51 +03:00
|
|
|
(adpcm->ms.sample2[channel] * ms_adpcm_coeffs2[adpcm->ms.predictor[channel]])) /
|
|
|
|
256;
|
2012-05-24 17:26:17 +04:00
|
|
|
presample += nibble * adpcm->ms.delta[channel];
|
2013-02-27 02:54:16 +04:00
|
|
|
|
2012-05-24 17:26:17 +04:00
|
|
|
if (presample > 32767)
|
|
|
|
presample = 32767;
|
|
|
|
else if (presample < -32768)
|
|
|
|
presample = -32768;
|
2013-02-27 02:54:16 +04:00
|
|
|
|
2012-05-24 17:26:17 +04:00
|
|
|
adpcm->ms.sample2[channel] = adpcm->ms.sample1[channel];
|
|
|
|
adpcm->ms.sample1[channel] = presample;
|
|
|
|
adpcm->ms.delta[channel] = adpcm->ms.delta[channel] * ms_adpcm_adaptation_table[sample] / 256;
|
2013-02-27 02:54:16 +04:00
|
|
|
|
2012-05-24 17:26:17 +04:00
|
|
|
if (adpcm->ms.delta[channel] < 16)
|
|
|
|
adpcm->ms.delta[channel] = 16;
|
2013-02-27 02:54:16 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
return (INT16)presample;
|
2012-05-24 17:26:17 +04:00
|
|
|
}
|
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
static BOOL freerdp_dsp_decode_ms_adpcm(FREERDP_DSP_CONTEXT* context, const BYTE* src, size_t size,
|
|
|
|
wStream* out)
|
2012-05-24 17:26:17 +04:00
|
|
|
{
|
2012-10-09 11:01:37 +04:00
|
|
|
BYTE sample;
|
2019-01-24 13:00:48 +03:00
|
|
|
const size_t out_size = size * 4;
|
2018-02-20 14:15:30 +03:00
|
|
|
const UINT32 channels = context->format.nChannels;
|
|
|
|
const UINT32 block_size = context->format.nBlockAlign;
|
2012-05-24 17:26:17 +04:00
|
|
|
|
2018-02-20 14:15:30 +03:00
|
|
|
if (!Stream_EnsureCapacity(out, out_size))
|
|
|
|
return FALSE;
|
2013-02-27 02:54:16 +04:00
|
|
|
|
2012-05-24 17:26:17 +04:00
|
|
|
while (size > 0)
|
|
|
|
{
|
|
|
|
if (size % block_size == 0)
|
|
|
|
{
|
|
|
|
if (channels > 1)
|
|
|
|
{
|
|
|
|
context->adpcm.ms.predictor[0] = *src++;
|
|
|
|
context->adpcm.ms.predictor[1] = *src++;
|
2019-01-24 13:00:48 +03:00
|
|
|
context->adpcm.ms.delta[0] = read_int16(src);
|
2012-05-24 17:26:17 +04:00
|
|
|
src += 2;
|
2019-01-24 13:00:48 +03:00
|
|
|
context->adpcm.ms.delta[1] = read_int16(src);
|
2012-05-24 17:26:17 +04:00
|
|
|
src += 2;
|
2019-01-24 13:00:48 +03:00
|
|
|
context->adpcm.ms.sample1[0] = read_int16(src);
|
2012-05-24 17:26:17 +04:00
|
|
|
src += 2;
|
2019-01-24 13:00:48 +03:00
|
|
|
context->adpcm.ms.sample1[1] = read_int16(src);
|
2012-05-24 17:26:17 +04:00
|
|
|
src += 2;
|
2019-01-24 13:00:48 +03:00
|
|
|
context->adpcm.ms.sample2[0] = read_int16(src);
|
2012-05-24 17:26:17 +04:00
|
|
|
src += 2;
|
2019-01-24 13:00:48 +03:00
|
|
|
context->adpcm.ms.sample2[1] = read_int16(src);
|
2012-05-24 17:26:17 +04:00
|
|
|
src += 2;
|
|
|
|
size -= 14;
|
2021-10-04 09:48:38 +03:00
|
|
|
Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample2[0]);
|
|
|
|
Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample2[1]);
|
|
|
|
Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample1[0]);
|
|
|
|
Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample1[1]);
|
2012-05-24 17:26:17 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
context->adpcm.ms.predictor[0] = *src++;
|
2019-01-24 13:00:48 +03:00
|
|
|
context->adpcm.ms.delta[0] = read_int16(src);
|
2012-05-24 17:26:17 +04:00
|
|
|
src += 2;
|
2019-01-24 13:00:48 +03:00
|
|
|
context->adpcm.ms.sample1[0] = read_int16(src);
|
2012-05-24 17:26:17 +04:00
|
|
|
src += 2;
|
2019-01-24 13:00:48 +03:00
|
|
|
context->adpcm.ms.sample2[0] = read_int16(src);
|
2012-05-24 17:26:17 +04:00
|
|
|
src += 2;
|
|
|
|
size -= 7;
|
2021-10-04 09:48:38 +03:00
|
|
|
Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample2[0]);
|
|
|
|
Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample1[0]);
|
2012-05-24 17:26:17 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (channels > 1)
|
|
|
|
{
|
|
|
|
sample = *src++;
|
|
|
|
size--;
|
2021-10-04 09:48:38 +03:00
|
|
|
Stream_Write_INT16(out,
|
|
|
|
freerdp_dsp_decode_ms_adpcm_sample(&context->adpcm, sample >> 4, 0));
|
|
|
|
Stream_Write_INT16(
|
|
|
|
out, freerdp_dsp_decode_ms_adpcm_sample(&context->adpcm, sample & 0x0F, 1));
|
2012-05-24 17:26:17 +04:00
|
|
|
sample = *src++;
|
|
|
|
size--;
|
2021-10-04 09:48:38 +03:00
|
|
|
Stream_Write_INT16(out,
|
|
|
|
freerdp_dsp_decode_ms_adpcm_sample(&context->adpcm, sample >> 4, 0));
|
|
|
|
Stream_Write_INT16(
|
|
|
|
out, freerdp_dsp_decode_ms_adpcm_sample(&context->adpcm, sample & 0x0F, 1));
|
2012-05-24 17:26:17 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sample = *src++;
|
|
|
|
size--;
|
2021-10-04 09:48:38 +03:00
|
|
|
Stream_Write_INT16(out,
|
|
|
|
freerdp_dsp_decode_ms_adpcm_sample(&context->adpcm, sample >> 4, 0));
|
|
|
|
Stream_Write_INT16(
|
|
|
|
out, freerdp_dsp_decode_ms_adpcm_sample(&context->adpcm, sample & 0x0F, 0));
|
2012-05-24 17:26:17 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-29 12:32:16 +04:00
|
|
|
return TRUE;
|
2012-05-24 17:26:17 +04:00
|
|
|
}
|
|
|
|
|
2012-10-09 11:26:39 +04:00
|
|
|
static BYTE freerdp_dsp_encode_ms_adpcm_sample(ADPCM* adpcm, INT32 sample, int channel)
|
2012-05-24 17:26:17 +04:00
|
|
|
{
|
2012-10-09 11:26:39 +04:00
|
|
|
INT32 presample;
|
|
|
|
INT32 errordelta;
|
2013-02-27 02:54:16 +04:00
|
|
|
presample = ((adpcm->ms.sample1[channel] * ms_adpcm_coeffs1[adpcm->ms.predictor[channel]]) +
|
2019-11-06 17:24:51 +03:00
|
|
|
(adpcm->ms.sample2[channel] * ms_adpcm_coeffs2[adpcm->ms.predictor[channel]])) /
|
|
|
|
256;
|
2012-05-24 17:26:17 +04:00
|
|
|
errordelta = (sample - presample) / adpcm->ms.delta[channel];
|
2013-02-27 02:54:16 +04:00
|
|
|
|
2012-05-24 17:26:17 +04:00
|
|
|
if ((sample - presample) % adpcm->ms.delta[channel] > adpcm->ms.delta[channel] / 2)
|
|
|
|
errordelta++;
|
2013-02-27 02:54:16 +04:00
|
|
|
|
2012-05-24 17:26:17 +04:00
|
|
|
if (errordelta > 7)
|
|
|
|
errordelta = 7;
|
|
|
|
else if (errordelta < -8)
|
|
|
|
errordelta = -8;
|
2013-02-27 02:54:16 +04:00
|
|
|
|
2012-05-24 17:26:17 +04:00
|
|
|
presample += adpcm->ms.delta[channel] * errordelta;
|
2013-02-27 02:54:16 +04:00
|
|
|
|
2012-05-24 17:26:17 +04:00
|
|
|
if (presample > 32767)
|
|
|
|
presample = 32767;
|
|
|
|
else if (presample < -32768)
|
|
|
|
presample = -32768;
|
2013-02-27 02:54:16 +04:00
|
|
|
|
2012-05-24 17:26:17 +04:00
|
|
|
adpcm->ms.sample2[channel] = adpcm->ms.sample1[channel];
|
|
|
|
adpcm->ms.sample1[channel] = presample;
|
2019-11-06 17:24:51 +03:00
|
|
|
adpcm->ms.delta[channel] =
|
|
|
|
adpcm->ms.delta[channel] * ms_adpcm_adaptation_table[(((BYTE)errordelta) & 0x0F)] / 256;
|
2013-02-27 02:54:16 +04:00
|
|
|
|
2012-05-24 17:26:17 +04:00
|
|
|
if (adpcm->ms.delta[channel] < 16)
|
|
|
|
adpcm->ms.delta[channel] = 16;
|
2013-02-27 02:54:16 +04:00
|
|
|
|
2019-11-06 17:24:51 +03:00
|
|
|
return ((BYTE)errordelta) & 0x0F;
|
2012-05-24 17:26:17 +04:00
|
|
|
}
|
|
|
|
|
2018-02-20 14:15:30 +03:00
|
|
|
static BOOL freerdp_dsp_encode_ms_adpcm(FREERDP_DSP_CONTEXT* context, const BYTE* src, size_t size,
|
|
|
|
wStream* out)
|
2012-05-24 17:26:17 +04:00
|
|
|
{
|
2021-10-04 09:48:38 +03:00
|
|
|
size_t start;
|
2012-10-09 11:26:39 +04:00
|
|
|
INT32 sample;
|
2018-08-01 12:57:18 +03:00
|
|
|
const size_t step = 8 + ((context->format.nChannels > 1) ? 4 : 0);
|
2013-02-27 02:54:16 +04:00
|
|
|
|
2018-07-17 11:30:35 +03:00
|
|
|
if (!Stream_EnsureRemainingCapacity(out, size))
|
2018-02-20 14:15:30 +03:00
|
|
|
return FALSE;
|
2014-04-29 12:32:16 +04:00
|
|
|
|
2021-10-04 09:48:38 +03:00
|
|
|
start = Stream_GetPosition(out);
|
2012-05-24 17:26:17 +04:00
|
|
|
|
|
|
|
if (context->adpcm.ms.delta[0] < 16)
|
|
|
|
context->adpcm.ms.delta[0] = 16;
|
2013-02-27 02:54:16 +04:00
|
|
|
|
2012-05-24 17:26:17 +04:00
|
|
|
if (context->adpcm.ms.delta[1] < 16)
|
|
|
|
context->adpcm.ms.delta[1] = 16;
|
|
|
|
|
2018-06-04 11:14:29 +03:00
|
|
|
while (size >= step)
|
2012-05-24 17:26:17 +04:00
|
|
|
{
|
2021-10-04 09:48:38 +03:00
|
|
|
BYTE val;
|
|
|
|
if ((Stream_GetPosition(out) - start) % context->format.nBlockAlign == 0)
|
2012-05-24 17:26:17 +04:00
|
|
|
{
|
2018-02-20 14:15:30 +03:00
|
|
|
if (context->format.nChannels > 1)
|
2012-05-24 17:26:17 +04:00
|
|
|
{
|
2021-10-04 09:48:38 +03:00
|
|
|
Stream_Write_UINT8(out, context->adpcm.ms.predictor[0]);
|
|
|
|
Stream_Write_UINT8(out, context->adpcm.ms.predictor[1]);
|
|
|
|
Stream_Write_UINT8(out, (context->adpcm.ms.delta[0] & 0xFF));
|
|
|
|
Stream_Write_UINT8(out, ((context->adpcm.ms.delta[0] >> 8) & 0xFF));
|
|
|
|
Stream_Write_UINT8(out, (context->adpcm.ms.delta[1] & 0xFF));
|
|
|
|
Stream_Write_UINT8(out, ((context->adpcm.ms.delta[1] >> 8) & 0xFF));
|
|
|
|
|
2019-01-24 13:00:48 +03:00
|
|
|
context->adpcm.ms.sample1[0] = read_int16(src + 4);
|
|
|
|
context->adpcm.ms.sample1[1] = read_int16(src + 6);
|
|
|
|
context->adpcm.ms.sample2[0] = read_int16(src + 0);
|
|
|
|
context->adpcm.ms.sample2[1] = read_int16(src + 2);
|
2021-10-04 09:48:38 +03:00
|
|
|
|
|
|
|
Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample1[0]);
|
|
|
|
Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample1[1]);
|
|
|
|
Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample2[0]);
|
|
|
|
Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample2[1]);
|
|
|
|
|
2012-05-24 17:26:17 +04:00
|
|
|
src += 8;
|
|
|
|
size -= 8;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-10-04 09:48:38 +03:00
|
|
|
Stream_Write_UINT8(out, context->adpcm.ms.predictor[0]);
|
|
|
|
Stream_Write_UINT8(out, (BYTE)(context->adpcm.ms.delta[0] & 0xFF));
|
|
|
|
Stream_Write_UINT8(out, (BYTE)((context->adpcm.ms.delta[0] >> 8) & 0xFF));
|
|
|
|
|
2019-01-24 13:00:48 +03:00
|
|
|
context->adpcm.ms.sample1[0] = read_int16(src + 2);
|
|
|
|
context->adpcm.ms.sample2[0] = read_int16(src + 0);
|
2021-10-04 09:48:38 +03:00
|
|
|
|
|
|
|
Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample1[0]);
|
|
|
|
Stream_Write_INT16(out, (INT16)context->adpcm.ms.sample2[0]);
|
2012-05-24 17:26:17 +04:00
|
|
|
src += 4;
|
|
|
|
size -= 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-24 13:00:48 +03:00
|
|
|
sample = read_int16(src);
|
2012-05-24 17:26:17 +04:00
|
|
|
src += 2;
|
2021-10-04 09:48:38 +03:00
|
|
|
Stream_Write_UINT8(
|
|
|
|
out, (freerdp_dsp_encode_ms_adpcm_sample(&context->adpcm, sample, 0) << 4) & 0xFF);
|
2019-01-24 13:00:48 +03:00
|
|
|
sample = read_int16(src);
|
2012-05-24 17:26:17 +04:00
|
|
|
src += 2;
|
2021-10-04 09:48:38 +03:00
|
|
|
|
|
|
|
Stream_Read_UINT8(out, val);
|
|
|
|
val += freerdp_dsp_encode_ms_adpcm_sample(&context->adpcm, sample,
|
|
|
|
context->format.nChannels > 1 ? 1 : 0);
|
|
|
|
Stream_Write_UINT8(out, val);
|
2012-05-24 17:26:17 +04:00
|
|
|
size -= 4;
|
|
|
|
}
|
2018-02-20 14:15:30 +03:00
|
|
|
|
2014-04-29 12:32:16 +04:00
|
|
|
return TRUE;
|
2012-05-24 17:26:17 +04:00
|
|
|
}
|
|
|
|
|
2018-11-09 11:08:06 +03:00
|
|
|
#endif
|
|
|
|
|
2018-02-20 14:15:30 +03:00
|
|
|
FREERDP_DSP_CONTEXT* freerdp_dsp_context_new(BOOL encoder)
|
2012-05-09 12:15:23 +04:00
|
|
|
{
|
2018-02-20 14:15:30 +03:00
|
|
|
#if defined(WITH_DSP_FFMPEG)
|
|
|
|
return freerdp_dsp_ffmpeg_context_new(encoder);
|
|
|
|
#else
|
|
|
|
FREERDP_DSP_CONTEXT* context = calloc(1, sizeof(FREERDP_DSP_CONTEXT));
|
2012-05-09 12:15:23 +04:00
|
|
|
|
2014-04-29 02:59:41 +04:00
|
|
|
if (!context)
|
|
|
|
return NULL;
|
2012-05-09 12:15:23 +04:00
|
|
|
|
2021-08-23 15:49:28 +03:00
|
|
|
context->channelmix = Stream_New(NULL, 4096);
|
2018-02-20 14:15:30 +03:00
|
|
|
|
2021-08-23 15:49:28 +03:00
|
|
|
if (!context->channelmix)
|
2018-02-20 14:15:30 +03:00
|
|
|
goto fail;
|
|
|
|
|
|
|
|
context->resample = Stream_New(NULL, 4096);
|
|
|
|
|
|
|
|
if (!context->resample)
|
|
|
|
goto fail;
|
|
|
|
|
2021-08-23 15:49:28 +03:00
|
|
|
context->buffer = Stream_New(NULL, 4096);
|
|
|
|
|
|
|
|
if (!context->buffer)
|
|
|
|
goto fail;
|
|
|
|
|
2018-02-20 14:15:30 +03:00
|
|
|
context->encoder = encoder;
|
|
|
|
#if defined(WITH_GSM)
|
|
|
|
context->gsm = gsm_create();
|
|
|
|
|
|
|
|
if (!context->gsm)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
{
|
|
|
|
int rc;
|
|
|
|
int val = 1;
|
|
|
|
rc = gsm_option(context->gsm, GSM_OPT_WAV49, &val);
|
|
|
|
|
|
|
|
if (rc < 0)
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#if defined(WITH_LAME)
|
|
|
|
|
|
|
|
if (encoder)
|
|
|
|
{
|
|
|
|
context->lame = lame_init();
|
|
|
|
|
|
|
|
if (!context->lame)
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
context->hip = hip_decode_init();
|
|
|
|
|
|
|
|
if (!context->hip)
|
|
|
|
goto fail;
|
|
|
|
}
|
2012-05-09 12:15:23 +04:00
|
|
|
|
2018-02-20 14:15:30 +03:00
|
|
|
#endif
|
|
|
|
#if defined(WITH_FAAD2)
|
|
|
|
|
|
|
|
if (!encoder)
|
|
|
|
{
|
|
|
|
context->faad = NeAACDecOpen();
|
|
|
|
|
|
|
|
if (!context->faad)
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2012-05-09 12:15:23 +04:00
|
|
|
return context;
|
2018-02-20 14:15:30 +03:00
|
|
|
fail:
|
|
|
|
freerdp_dsp_context_free(context);
|
|
|
|
return NULL;
|
|
|
|
#endif
|
2012-05-09 12:15:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void freerdp_dsp_context_free(FREERDP_DSP_CONTEXT* context)
|
|
|
|
{
|
2018-02-20 14:15:30 +03:00
|
|
|
#if defined(WITH_DSP_FFMPEG)
|
|
|
|
freerdp_dsp_ffmpeg_context_free(context);
|
|
|
|
#else
|
|
|
|
|
2012-05-09 12:15:23 +04:00
|
|
|
if (context)
|
|
|
|
{
|
2021-08-23 15:49:28 +03:00
|
|
|
Stream_Free(context->channelmix, TRUE);
|
2018-02-20 14:15:30 +03:00
|
|
|
Stream_Free(context->resample, TRUE);
|
2021-08-23 15:49:28 +03:00
|
|
|
Stream_Free(context->buffer, TRUE);
|
2018-02-20 14:15:30 +03:00
|
|
|
#if defined(WITH_GSM)
|
|
|
|
gsm_destroy(context->gsm);
|
|
|
|
#endif
|
|
|
|
#if defined(WITH_LAME)
|
|
|
|
|
|
|
|
if (context->encoder)
|
|
|
|
lame_close(context->lame);
|
|
|
|
else
|
|
|
|
hip_decode_exit(context->hip);
|
|
|
|
|
|
|
|
#endif
|
|
|
|
#if defined(WITH_FAAD2)
|
|
|
|
|
|
|
|
if (!context->encoder)
|
|
|
|
NeAACDecClose(context->faad);
|
|
|
|
|
|
|
|
#endif
|
2019-11-06 17:24:51 +03:00
|
|
|
#if defined(WITH_FAAC)
|
2018-02-20 14:15:30 +03:00
|
|
|
|
|
|
|
if (context->faac)
|
|
|
|
faacEncClose(context->faac);
|
|
|
|
|
2018-11-13 12:59:30 +03:00
|
|
|
#endif
|
|
|
|
#if defined(WITH_SOXR)
|
|
|
|
soxr_delete(context->sox);
|
2018-02-20 14:15:30 +03:00
|
|
|
#endif
|
2012-10-09 07:21:26 +04:00
|
|
|
free(context);
|
2012-05-09 12:15:23 +04:00
|
|
|
}
|
2018-02-20 14:15:30 +03:00
|
|
|
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL freerdp_dsp_encode(FREERDP_DSP_CONTEXT* context, const AUDIO_FORMAT* srcFormat,
|
|
|
|
const BYTE* data, size_t length, wStream* out)
|
|
|
|
{
|
|
|
|
#if defined(WITH_DSP_FFMPEG)
|
|
|
|
return freerdp_dsp_ffmpeg_encode(context, srcFormat, data, length, out);
|
|
|
|
#else
|
2018-12-04 13:38:02 +03:00
|
|
|
const BYTE* resampleData;
|
|
|
|
size_t resampleLength;
|
2019-01-02 18:35:24 +03:00
|
|
|
AUDIO_FORMAT format;
|
2018-02-20 14:15:30 +03:00
|
|
|
|
|
|
|
if (!context || !context->encoder || !srcFormat || !data || !out)
|
|
|
|
return FALSE;
|
|
|
|
|
2019-01-02 18:35:24 +03:00
|
|
|
format = *srcFormat;
|
|
|
|
|
2018-12-04 13:38:02 +03:00
|
|
|
if (!freerdp_dsp_channel_mix(context, data, length, srcFormat, &resampleData, &resampleLength))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
format.nChannels = context->format.nChannels;
|
|
|
|
|
|
|
|
if (!freerdp_dsp_resample(context, resampleData, resampleLength, &format, &data, &length))
|
2018-11-09 12:15:14 +03:00
|
|
|
return FALSE;
|
2018-02-20 14:15:30 +03:00
|
|
|
|
|
|
|
switch (context->format.wFormatTag)
|
|
|
|
{
|
|
|
|
case WAVE_FORMAT_PCM:
|
|
|
|
if (!Stream_EnsureRemainingCapacity(out, length))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
Stream_Write(out, data, length);
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
case WAVE_FORMAT_ADPCM:
|
|
|
|
return freerdp_dsp_encode_ms_adpcm(context, data, length, out);
|
|
|
|
|
|
|
|
case WAVE_FORMAT_DVI_ADPCM:
|
|
|
|
return freerdp_dsp_encode_ima_adpcm(context, data, length, out);
|
|
|
|
#if defined(WITH_GSM)
|
|
|
|
|
|
|
|
case WAVE_FORMAT_GSM610:
|
|
|
|
return freerdp_dsp_encode_gsm610(context, data, length, out);
|
|
|
|
#endif
|
|
|
|
#if defined(WITH_LAME)
|
|
|
|
|
|
|
|
case WAVE_FORMAT_MPEGLAYER3:
|
|
|
|
return freerdp_dsp_encode_mp3(context, data, length, out);
|
|
|
|
#endif
|
|
|
|
#if defined(WITH_FAAC)
|
|
|
|
|
|
|
|
case WAVE_FORMAT_AAC_MS:
|
|
|
|
return freerdp_dsp_encode_faac(context, data, length, out);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
default:
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL freerdp_dsp_decode(FREERDP_DSP_CONTEXT* context, const AUDIO_FORMAT* srcFormat,
|
|
|
|
const BYTE* data, size_t length, wStream* out)
|
|
|
|
{
|
|
|
|
#if defined(WITH_DSP_FFMPEG)
|
|
|
|
return freerdp_dsp_ffmpeg_decode(context, srcFormat, data, length, out);
|
|
|
|
#else
|
|
|
|
|
|
|
|
if (!context || context->encoder || !srcFormat || !data || !out)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
switch (context->format.wFormatTag)
|
|
|
|
{
|
|
|
|
case WAVE_FORMAT_PCM:
|
|
|
|
if (!Stream_EnsureRemainingCapacity(out, length))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
Stream_Write(out, data, length);
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
case WAVE_FORMAT_ADPCM:
|
|
|
|
return freerdp_dsp_decode_ms_adpcm(context, data, length, out);
|
|
|
|
|
|
|
|
case WAVE_FORMAT_DVI_ADPCM:
|
|
|
|
return freerdp_dsp_decode_ima_adpcm(context, data, length, out);
|
|
|
|
#if defined(WITH_GSM)
|
|
|
|
|
|
|
|
case WAVE_FORMAT_GSM610:
|
|
|
|
return freerdp_dsp_decode_gsm610(context, data, length, out);
|
|
|
|
#endif
|
|
|
|
#if defined(WITH_LAME)
|
|
|
|
|
|
|
|
case WAVE_FORMAT_MPEGLAYER3:
|
|
|
|
return freerdp_dsp_decode_mp3(context, data, length, out);
|
|
|
|
#endif
|
|
|
|
#if defined(WITH_FAAD2)
|
|
|
|
|
|
|
|
case WAVE_FORMAT_AAC_MS:
|
|
|
|
return freerdp_dsp_decode_faad(context, data, length, out);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
default:
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL freerdp_dsp_supports_format(const AUDIO_FORMAT* format, BOOL encode)
|
|
|
|
{
|
|
|
|
#if defined(WITH_DSP_FFMPEG)
|
|
|
|
return freerdp_dsp_ffmpeg_supports_format(format, encode);
|
|
|
|
#else
|
|
|
|
|
2019-01-24 13:00:48 +03:00
|
|
|
#if !defined(WITH_DSP_EXPERIMENTAL)
|
|
|
|
WINPR_UNUSED(encode);
|
|
|
|
#endif
|
2018-02-20 14:15:30 +03:00
|
|
|
switch (format->wFormatTag)
|
|
|
|
{
|
|
|
|
case WAVE_FORMAT_PCM:
|
2018-11-13 13:03:09 +03:00
|
|
|
return TRUE;
|
|
|
|
#if defined(WITH_DSP_EXPERIMENTAL)
|
|
|
|
|
2018-02-20 14:15:30 +03:00
|
|
|
case WAVE_FORMAT_ADPCM:
|
2021-08-23 15:49:28 +03:00
|
|
|
return FALSE;
|
2018-02-20 14:15:30 +03:00
|
|
|
case WAVE_FORMAT_DVI_ADPCM:
|
|
|
|
return TRUE;
|
2018-11-13 13:03:09 +03:00
|
|
|
#endif
|
2018-02-20 14:15:30 +03:00
|
|
|
#if defined(WITH_GSM)
|
|
|
|
|
|
|
|
case WAVE_FORMAT_GSM610:
|
2018-03-14 18:10:12 +03:00
|
|
|
#if defined(WITH_DSP_EXPERIMENTAL)
|
2018-02-20 14:15:30 +03:00
|
|
|
return TRUE;
|
2018-03-14 18:10:12 +03:00
|
|
|
#else
|
|
|
|
return !encode;
|
|
|
|
#endif
|
2018-02-20 14:15:30 +03:00
|
|
|
#endif
|
|
|
|
#if defined(WITH_LAME)
|
|
|
|
|
|
|
|
case WAVE_FORMAT_MPEGLAYER3:
|
2018-03-14 18:10:12 +03:00
|
|
|
#if defined(WITH_DSP_EXPERIMENTAL)
|
2018-02-20 14:15:30 +03:00
|
|
|
return TRUE;
|
2018-03-14 18:10:12 +03:00
|
|
|
#else
|
|
|
|
return !encode;
|
|
|
|
#endif
|
2018-02-20 14:15:30 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
case WAVE_FORMAT_AAC_MS:
|
|
|
|
#if defined(WITH_FAAD2)
|
|
|
|
if (!encode)
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
#endif
|
2021-05-11 12:32:50 +03:00
|
|
|
#if defined(WITH_FAAC)
|
2018-02-20 14:15:30 +03:00
|
|
|
|
|
|
|
if (encode)
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
default:
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-08-23 15:49:28 +03:00
|
|
|
BOOL freerdp_dsp_context_reset(FREERDP_DSP_CONTEXT* context, const AUDIO_FORMAT* targetFormat,
|
|
|
|
UINT32 FramesPerPacket)
|
2018-02-20 14:15:30 +03:00
|
|
|
{
|
|
|
|
#if defined(WITH_DSP_FFMPEG)
|
|
|
|
return freerdp_dsp_ffmpeg_context_reset(context, targetFormat);
|
|
|
|
#else
|
|
|
|
|
|
|
|
if (!context || !targetFormat)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
context->format = *targetFormat;
|
2021-08-23 15:49:28 +03:00
|
|
|
|
|
|
|
if (context->format.wFormatTag == WAVE_FORMAT_DVI_ADPCM)
|
|
|
|
{
|
|
|
|
size_t min_frame_data =
|
|
|
|
context->format.wBitsPerSample * context->format.nChannels * FramesPerPacket * 1ULL;
|
|
|
|
size_t data_per_block = (context->format.nBlockAlign - 4 * context->format.nChannels) * 8;
|
|
|
|
size_t nb_block_per_packet = min_frame_data / data_per_block;
|
|
|
|
|
|
|
|
if (min_frame_data % data_per_block)
|
|
|
|
nb_block_per_packet++;
|
|
|
|
|
|
|
|
context->adpcm.ima.packet_size = nb_block_per_packet * context->format.nBlockAlign;
|
|
|
|
Stream_EnsureCapacity(context->buffer, context->adpcm.ima.packet_size);
|
|
|
|
Stream_SetPosition(context->buffer, 0);
|
|
|
|
}
|
|
|
|
|
2018-02-20 14:15:30 +03:00
|
|
|
#if defined(WITH_FAAD2)
|
|
|
|
context->faadSetup = FALSE;
|
|
|
|
#endif
|
|
|
|
#if defined(WITH_FAAC)
|
|
|
|
|
|
|
|
if (context->encoder)
|
|
|
|
{
|
|
|
|
faacEncConfigurationPtr cfg;
|
|
|
|
|
|
|
|
if (context->faac)
|
|
|
|
faacEncClose(context->faac);
|
|
|
|
|
|
|
|
context->faac = faacEncOpen(targetFormat->nSamplesPerSec, targetFormat->nChannels,
|
|
|
|
&context->faacInputSamples, &context->faacMaxOutputBytes);
|
|
|
|
|
|
|
|
if (!context->faac)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
cfg = faacEncGetCurrentConfiguration(context->faac);
|
2021-05-11 12:32:50 +03:00
|
|
|
cfg->inputFormat = FAAC_INPUT_16BIT;
|
|
|
|
cfg->outputFormat = 0;
|
|
|
|
cfg->mpegVersion = MPEG4;
|
|
|
|
cfg->useTns = 1;
|
|
|
|
cfg->bandWidth = targetFormat->nAvgBytesPerSec;
|
2018-02-20 14:15:30 +03:00
|
|
|
faacEncSetConfiguration(context->faac, cfg);
|
|
|
|
}
|
|
|
|
|
2018-11-13 12:59:30 +03:00
|
|
|
#endif
|
|
|
|
#if defined(WITH_SOXR)
|
|
|
|
{
|
|
|
|
soxr_io_spec_t iospec = soxr_io_spec(SOXR_INT16, SOXR_INT16);
|
|
|
|
soxr_error_t error;
|
|
|
|
soxr_delete(context->sox);
|
|
|
|
context->sox = soxr_create(context->format.nSamplesPerSec, targetFormat->nSamplesPerSec,
|
|
|
|
targetFormat->nChannels, &error, &iospec, NULL, NULL);
|
|
|
|
|
|
|
|
if (!context->sox || (error != 0))
|
|
|
|
return FALSE;
|
|
|
|
}
|
2018-02-20 14:15:30 +03:00
|
|
|
#endif
|
|
|
|
return TRUE;
|
|
|
|
#endif
|
2012-05-09 12:15:23 +04:00
|
|
|
}
|