FreeRDP
Loading...
Searching...
No Matches
audio.c
1
19
20#include <freerdp/config.h>
21
22#include <winpr/crt.h>
23
24#include <freerdp/log.h>
25#include <freerdp/codec/audio.h>
26
27#define TAG FREERDP_TAG("codec")
28
29UINT32 audio_format_compute_time_length(const AUDIO_FORMAT* format, size_t size)
30{
31 UINT32 mstime = 0;
32 UINT32 wSamples = 0;
33
38
39 if (format->wBitsPerSample)
40 {
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);
45 }
46 else
47 {
48 mstime = 0;
49
50 if (format->wFormatTag == WAVE_FORMAT_GSM610)
51 {
52 UINT16 nSamplesPerBlock = 0;
53
54 if ((format->cbSize == 2) && (format->data))
55 {
56 nSamplesPerBlock = *(WINPR_PACKED_ALIGN_CAST(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);
61 }
62 else
63 {
64 WLog_ERR(TAG,
65 "audio_format_compute_time_length: invalid WAVE_FORMAT_GSM610 format");
66 }
67 }
68 else
69 {
70 WLog_ERR(TAG, "audio_format_compute_time_length: unknown format %" PRIu16 "",
71 format->wFormatTag);
72 }
73 }
74
75 return mstime;
76}
77
78const char* audio_format_get_tag_string(UINT16 wFormatTag)
79{
80 switch (wFormatTag)
81 {
82 case WAVE_FORMAT_PCM:
83 return "WAVE_FORMAT_PCM";
84
85 case WAVE_FORMAT_ADPCM:
86 return "WAVE_FORMAT_ADPCM";
87
88 case WAVE_FORMAT_ALAW:
89 return "WAVE_FORMAT_ALAW";
90
91 case WAVE_FORMAT_MULAW:
92 return "WAVE_FORMAT_MULAW";
93
94 case WAVE_FORMAT_DVI_ADPCM:
95 return "WAVE_FORMAT_DVI_ADPCM";
96
97 case WAVE_FORMAT_GSM610:
98 return "WAVE_FORMAT_GSM610";
99
100 case WAVE_FORMAT_MSG723:
101 return "WAVE_FORMAT_MSG723";
102
103 case WAVE_FORMAT_DSPGROUP_TRUESPEECH:
104 return "WAVE_FORMAT_DSPGROUP_TRUESPEECH";
105
106 case WAVE_FORMAT_MPEGLAYER3:
107 return "WAVE_FORMAT_MPEGLAYER3";
108
109 case WAVE_FORMAT_WMAUDIO2:
110 return "WAVE_FORMAT_WMAUDIO2";
111
112 case WAVE_FORMAT_AAC_MS:
113 return "WAVE_FORMAT_AAC_MS";
114
115 case WAVE_FORMAT_OPUS:
116 return "WAVE_FORMAT_OPUS";
117
118 default:
119 return "WAVE_FORMAT_UNKNOWN";
120 }
121}
122
123void audio_format_print(wLog* log, DWORD level, const AUDIO_FORMAT* format)
124{
125 WLog_Print(log, level,
126 "%s:\t wFormatTag: 0x%04" PRIX16 " nChannels: %" PRIu16 " nSamplesPerSec: %" PRIu32
127 " "
128 "nAvgBytesPerSec: %" PRIu32 " nBlockAlign: %" PRIu16 " wBitsPerSample: %" PRIu16
129 " cbSize: %" PRIu16 "",
130 audio_format_get_tag_string(format->wFormatTag), format->wFormatTag,
131 format->nChannels, format->nSamplesPerSec, format->nAvgBytesPerSec,
132 format->nBlockAlign, format->wBitsPerSample, format->cbSize);
133}
134
135void audio_formats_print(wLog* log, DWORD level, const AUDIO_FORMAT* formats, UINT16 count)
136{
137 if (formats)
138 {
139 WLog_Print(log, level, "AUDIO_FORMATS (%" PRIu16 ") ={", count);
140
141 for (UINT32 index = 0; index < count; index++)
142 {
143 const AUDIO_FORMAT* format = &formats[index];
144 WLog_Print(log, level, "\t");
145 audio_format_print(log, level, format);
146 }
147
148 WLog_Print(log, level, "}");
149 }
150}
151
152BOOL audio_format_read(wStream* s, AUDIO_FORMAT* format)
153{
154 if (!s || !format)
155 return FALSE;
156
157 if (!Stream_CheckAndLogRequiredLength(TAG, s, 18))
158 return FALSE;
159
160 Stream_Read_UINT16(s, format->wFormatTag);
161 Stream_Read_UINT16(s, format->nChannels);
162 Stream_Read_UINT32(s, format->nSamplesPerSec);
163 Stream_Read_UINT32(s, format->nAvgBytesPerSec);
164 Stream_Read_UINT16(s, format->nBlockAlign);
165 Stream_Read_UINT16(s, format->wBitsPerSample);
166 Stream_Read_UINT16(s, format->cbSize);
167
168 /* if the format does not have an alignment ensure it is 1 */
169 if (format->nBlockAlign == 0)
170 format->nBlockAlign = 1;
171
172 if (!Stream_CheckAndLogRequiredLength(TAG, s, format->cbSize))
173 return FALSE;
174
175 format->data = nullptr;
176
177 if (format->cbSize > 0)
178 {
179 format->data = malloc(format->cbSize);
180
181 if (!format->data)
182 return FALSE;
183
184 Stream_Read(s, format->data, format->cbSize);
185 }
186
187 if (!audio_format_is_valid(format))
188 {
189 free(format->data);
190 format->data = nullptr;
191 return FALSE;
192 }
193 return TRUE;
194}
195
196BOOL audio_format_write(wStream* s, const AUDIO_FORMAT* format)
197{
198 if (!s || !format)
199 return FALSE;
200
201 if (!audio_format_is_valid(format))
202 return FALSE;
203
204 if (!Stream_EnsureRemainingCapacity(s, 18ull + format->cbSize))
205 return FALSE;
206
207 Stream_Write_UINT16(s, format->wFormatTag); /* wFormatTag (WAVE_FORMAT_PCM) */
208 Stream_Write_UINT16(s, format->nChannels); /* nChannels */
209 Stream_Write_UINT32(s, format->nSamplesPerSec); /* nSamplesPerSec */
210 Stream_Write_UINT32(s, format->nAvgBytesPerSec); /* nAvgBytesPerSec */
211 Stream_Write_UINT16(s, format->nBlockAlign); /* nBlockAlign */
212 Stream_Write_UINT16(s, format->wBitsPerSample); /* wBitsPerSample */
213 Stream_Write_UINT16(s, format->cbSize); /* cbSize */
214
215 if (format->cbSize > 0)
216 Stream_Write(s, format->data, format->cbSize);
217
218 return TRUE;
219}
220
221BOOL audio_format_copy(const AUDIO_FORMAT* WINPR_RESTRICT srcFormat,
222 AUDIO_FORMAT* WINPR_RESTRICT dstFormat)
223{
224 if (!srcFormat || !dstFormat)
225 return FALSE;
226
227 if (!audio_format_is_valid(srcFormat))
228 return FALSE;
229
230 *dstFormat = *srcFormat;
231
232 if (srcFormat->cbSize > 0)
233 {
234 dstFormat->data = malloc(srcFormat->cbSize);
235
236 if (!dstFormat->data)
237 return FALSE;
238
239 memcpy(dstFormat->data, srcFormat->data, dstFormat->cbSize);
240 }
241
242 return TRUE;
243}
244
245BOOL audio_format_is_valid(const AUDIO_FORMAT* format)
246{
247 if (!format)
248 return FALSE;
249 if (format->wFormatTag == WAVE_FORMAT_UNKNOWN)
250 return FALSE;
251 if (format->nBlockAlign == 0)
252 return FALSE;
253 if (format->nChannels == 0)
254 return FALSE;
255 if (format->nChannels > 128)
256 return FALSE;
257 if (format->wBitsPerSample == 0)
258 {
259 /* only allow WAVE_FORMAT_GSM610 which is variable-length and has 0 wBitsPerSample */
260 if (format->wFormatTag != WAVE_FORMAT_GSM610)
261 return FALSE;
262 }
263 if (format->wBitsPerSample > 1024)
264 return FALSE;
265 if (format->nSamplesPerSec == 0)
266 return FALSE;
267 if (format->nSamplesPerSec > 1000000)
268 return FALSE;
269 return TRUE;
270}
271
272BOOL audio_format_compatible(const AUDIO_FORMAT* with, const AUDIO_FORMAT* what)
273{
274 if (!with || !what)
275 return FALSE;
276
277 /* with is a filter: a field that is WAVE_FORMAT_UNKNOWN or 0 matches any value (the
278 * /sound:format: and /microphone:format: options, or a resampler that ignores the tag) */
279 if (!audio_format_is_valid(what))
280 return FALSE;
281
282 if (with->wFormatTag != WAVE_FORMAT_UNKNOWN)
283 {
284 if (with->wFormatTag != what->wFormatTag)
285 return FALSE;
286 }
287
288 if (with->nChannels != 0)
289 {
290 if (with->nChannels != what->nChannels)
291 return FALSE;
292 }
293
294 if (with->nSamplesPerSec != 0)
295 {
296 if (with->nSamplesPerSec != what->nSamplesPerSec)
297 return FALSE;
298 }
299
300 if (with->wBitsPerSample != 0)
301 {
302 if (with->wBitsPerSample != what->wBitsPerSample)
303 return FALSE;
304 }
305
306 return TRUE;
307}
308
309AUDIO_FORMAT* audio_format_new(void)
310{
311 return audio_formats_new(1);
312}
313
314AUDIO_FORMAT* audio_formats_new(size_t count)
315{
316 return calloc(count, sizeof(AUDIO_FORMAT));
317}
318
319void audio_format_free(AUDIO_FORMAT* format)
320{
321 if (format)
322 free(format->data);
323}
324
325void audio_formats_free(AUDIO_FORMAT* formats, size_t count)
326{
327 if (formats)
328 {
329 for (size_t index = 0; index < count; index++)
330 {
331 AUDIO_FORMAT* format = &formats[index];
332 audio_format_free(format);
333 }
334
335 free(formats);
336 }
337}