20 #include <freerdp/config.h>
22 #include <winpr/crt.h>
24 #include <freerdp/log.h>
25 #include <freerdp/codec/audio.h>
27 #define TAG FREERDP_TAG("codec")
29 UINT32 audio_format_compute_time_length(
const AUDIO_FORMAT* format,
size_t size)
39 if (format->wBitsPerSample)
41 const size_t samples = (size * 8) / format->wBitsPerSample;
42 WINPR_ASSERT(samples <= UINT32_MAX);
43 wSamples = (UINT32)samples;
44 mstime = (((wSamples * 1000) / format->nSamplesPerSec) / format->nChannels);
50 if (format->wFormatTag == WAVE_FORMAT_GSM610)
52 UINT16 nSamplesPerBlock = 0;
54 if ((format->cbSize == 2) && (format->data))
56 nSamplesPerBlock = *((UINT16*)format->data);
57 const size_t samples = (size / format->nBlockAlign) * nSamplesPerBlock;
58 WINPR_ASSERT(samples <= UINT32_MAX);
59 wSamples = (UINT32)samples;
60 mstime = (((wSamples * 1000) / format->nSamplesPerSec) / format->nChannels);
65 "audio_format_compute_time_length: invalid WAVE_FORMAT_GSM610 format");
70 WLog_ERR(TAG,
"audio_format_compute_time_length: unknown format %" PRIu16
"",
78 const char* audio_format_get_tag_string(UINT16 wFormatTag)
83 return "WAVE_FORMAT_PCM";
85 case WAVE_FORMAT_ADPCM:
86 return "WAVE_FORMAT_ADPCM";
88 case WAVE_FORMAT_ALAW:
89 return "WAVE_FORMAT_ALAW";
91 case WAVE_FORMAT_MULAW:
92 return "WAVE_FORMAT_MULAW";
94 case WAVE_FORMAT_DVI_ADPCM:
95 return "WAVE_FORMAT_DVI_ADPCM";
97 case WAVE_FORMAT_GSM610:
98 return "WAVE_FORMAT_GSM610";
100 case WAVE_FORMAT_MSG723:
101 return "WAVE_FORMAT_MSG723";
103 case WAVE_FORMAT_DSPGROUP_TRUESPEECH:
104 return "WAVE_FORMAT_DSPGROUP_TRUESPEECH ";
106 case WAVE_FORMAT_MPEGLAYER3:
107 return "WAVE_FORMAT_MPEGLAYER3";
109 case WAVE_FORMAT_WMAUDIO2:
110 return "WAVE_FORMAT_WMAUDIO2";
112 case WAVE_FORMAT_AAC_MS:
113 return "WAVE_FORMAT_AAC_MS";
115 return "WAVE_FORMAT_UNKNOWN";
119 void audio_format_print(wLog* log, DWORD level,
const AUDIO_FORMAT* format)
121 WLog_Print(log, level,
122 "%s:\t wFormatTag: 0x%04" PRIX16
" nChannels: %" PRIu16
" nSamplesPerSec: %" PRIu32
124 "nAvgBytesPerSec: %" PRIu32
" nBlockAlign: %" PRIu16
" wBitsPerSample: %" PRIu16
125 " cbSize: %" PRIu16
"",
126 audio_format_get_tag_string(format->wFormatTag), format->wFormatTag,
127 format->nChannels, format->nSamplesPerSec, format->nAvgBytesPerSec,
128 format->nBlockAlign, format->wBitsPerSample, format->cbSize);
131 void audio_formats_print(wLog* log, DWORD level,
const AUDIO_FORMAT* formats, UINT16 count)
135 WLog_Print(log, level,
"AUDIO_FORMATS (%" PRIu16
") ={", count);
137 for (UINT32 index = 0; index < count; index++)
140 WLog_Print(log, level,
"\t");
141 audio_format_print(log, level, format);
144 WLog_Print(log, level,
"}");
153 if (!Stream_CheckAndLogRequiredLength(TAG, s, 18))
156 Stream_Read_UINT16(s, format->wFormatTag);
157 Stream_Read_UINT16(s, format->nChannels);
158 Stream_Read_UINT32(s, format->nSamplesPerSec);
159 Stream_Read_UINT32(s, format->nAvgBytesPerSec);
160 Stream_Read_UINT16(s, format->nBlockAlign);
161 Stream_Read_UINT16(s, format->wBitsPerSample);
162 Stream_Read_UINT16(s, format->cbSize);
164 if (!Stream_CheckAndLogRequiredLength(TAG, s, format->cbSize))
169 if (format->cbSize > 0)
171 format->data = malloc(format->cbSize);
176 Stream_Read(s, format->data, format->cbSize);
187 if (!Stream_EnsureRemainingCapacity(s, 18 + format->cbSize))
190 Stream_Write_UINT16(s, format->wFormatTag);
191 Stream_Write_UINT16(s, format->nChannels);
192 Stream_Write_UINT32(s, format->nSamplesPerSec);
193 Stream_Write_UINT32(s, format->nAvgBytesPerSec);
194 Stream_Write_UINT16(s, format->nBlockAlign);
195 Stream_Write_UINT16(s, format->wBitsPerSample);
196 Stream_Write_UINT16(s, format->cbSize);
198 if (format->cbSize > 0)
199 Stream_Write(s, format->data, format->cbSize);
204 BOOL audio_format_copy(
const AUDIO_FORMAT* WINPR_RESTRICT srcFormat,
207 if (!srcFormat || !dstFormat)
210 *dstFormat = *srcFormat;
212 if (srcFormat->cbSize > 0)
214 dstFormat->data = malloc(srcFormat->cbSize);
216 if (!dstFormat->data)
219 memcpy(dstFormat->data, srcFormat->data, dstFormat->cbSize);
230 if (with->wFormatTag != WAVE_FORMAT_UNKNOWN)
232 if (with->wFormatTag != what->wFormatTag)
236 if (with->nChannels != 0)
238 if (with->nChannels != what->nChannels)
242 if (with->nSamplesPerSec != 0)
244 if (with->nSamplesPerSec != what->nSamplesPerSec)
248 if (with->wBitsPerSample != 0)
250 if (with->wBitsPerSample != what->wBitsPerSample)
257 static BOOL audio_format_valid(
const AUDIO_FORMAT* format)
262 if (format->nChannels == 0)
265 if (format->nSamplesPerSec == 0)
273 return audio_formats_new(1);
287 void audio_formats_free(
AUDIO_FORMAT* formats,
size_t count)
291 for (
size_t index = 0; index < count; index++)
294 audio_format_free(format);