summaryrefslogtreecommitdiffstats
path: root/libfreerdp/codec/audio.c
blob: ff2786dd094d73fe6e9000695cf61c5b3c4f0b42 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/**
 * FreeRDP: A Remote Desktop Protocol Implementation
 * Audio Formats
 *
 * Copyright 2013 Marc-Andre Moreau <marcandre.moreau@gmail.com>
 *
 * 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.
 */

#include <freerdp/config.h>

#include <winpr/crt.h>

#include <freerdp/log.h>
#include <freerdp/codec/audio.h>

#define TAG FREERDP_TAG("codec")

UINT32 audio_format_compute_time_length(const AUDIO_FORMAT* format, size_t size)
{
	UINT32 mstime = 0;
	UINT32 wSamples = 0;

	/**
	 * [MSDN-AUDIOFORMAT]:
	 * http://msdn.microsoft.com/en-us/library/ms713497.aspx
	 */

	if (format->wBitsPerSample)
	{
		const size_t samples = (size * 8) / format->wBitsPerSample;
		WINPR_ASSERT(samples <= UINT32_MAX);
		wSamples = (UINT32)samples;
		mstime = (((wSamples * 1000) / format->nSamplesPerSec) / format->nChannels);
	}
	else
	{
		mstime = 0;

		if (format->wFormatTag == WAVE_FORMAT_GSM610)
		{
			UINT16 nSamplesPerBlock = 0;

			if ((format->cbSize == 2) && (format->data))
			{
				nSamplesPerBlock = *((UINT16*)format->data);
				const size_t samples = (size / format->nBlockAlign) * nSamplesPerBlock;
				WINPR_ASSERT(samples <= UINT32_MAX);
				wSamples = (UINT32)samples;
				mstime = (((wSamples * 1000) / format->nSamplesPerSec) / format->nChannels);
			}
			else
			{
				WLog_ERR(TAG,
				         "audio_format_compute_time_length: invalid WAVE_FORMAT_GSM610 format");
			}
		}
		else
		{
			WLog_ERR(TAG, "audio_format_compute_time_length: unknown format %" PRIu16 "",
			         format->wFormatTag);
		}
	}

	return mstime;
}

char* audio_format_get_tag_string(UINT16 wFormatTag)
{
	switch (wFormatTag)
	{
		case WAVE_FORMAT_PCM:
			return "WAVE_FORMAT_PCM";

		case WAVE_FORMAT_ADPCM:
			return "WAVE_FORMAT_ADPCM";

		case WAVE_FORMAT_ALAW:
			return "WAVE_FORMAT_ALAW";

		case WAVE_FORMAT_MULAW:
			return "WAVE_FORMAT_MULAW";

		case WAVE_FORMAT_DVI_ADPCM:
			return "WAVE_FORMAT_DVI_ADPCM";

		case WAVE_FORMAT_GSM610:
			return "WAVE_FORMAT_GSM610";

		case WAVE_FORMAT_MSG723:
			return "WAVE_FORMAT_MSG723";

		case WAVE_FORMAT_DSPGROUP_TRUESPEECH:
			return "WAVE_FORMAT_DSPGROUP_TRUESPEECH	";

		case WAVE_FORMAT_MPEGLAYER3:
			return "WAVE_FORMAT_MPEGLAYER3";

		case WAVE_FORMAT_WMAUDIO2:
			return "WAVE_FORMAT_WMAUDIO2";

		case WAVE_FORMAT_AAC_MS:
			return "WAVE_FORMAT_AAC_MS";
	}

	return "WAVE_FORMAT_UNKNOWN";
}

void audio_format_print(wLog* log, DWORD level, const AUDIO_FORMAT* format)
{
	WLog_Print(log, level,
	           "%s:\t wFormatTag: 0x%04" PRIX16 " nChannels: %" PRIu16 " nSamplesPerSec: %" PRIu32
	           " "
	           "nAvgBytesPerSec: %" PRIu32 " nBlockAlign: %" PRIu16 " wBitsPerSample: %" PRIu16
	           " cbSize: %" PRIu16 "",
	           audio_format_get_tag_string(format->wFormatTag), format->wFormatTag,
	           format->nChannels, format->nSamplesPerSec, format->nAvgBytesPerSec,
	           format->nBlockAlign, format->wBitsPerSample, format->cbSize);
}

void audio_formats_print(wLog* log, DWORD level, const AUDIO_FORMAT* formats, UINT16 count)
{
	if (formats)
	{
		WLog_Print(log, level, "AUDIO_FORMATS (%" PRIu16 ") ={", count);

		for (UINT32 index = 0; index < count; index++)
		{
			const AUDIO_FORMAT* format = &formats[index];
			WLog_Print(log, level, "\t");
			audio_format_print(log, level, format);
		}

		WLog_Print(log, level, "}");
	}
}

BOOL audio_format_read(wStream* s, AUDIO_FORMAT* format)
{
	if (!s || !format)
		return FALSE;

	if (!Stream_CheckAndLogRequiredLength(TAG, s, 18))
		return FALSE;

	Stream_Read_UINT16(s, format->wFormatTag);
	Stream_Read_UINT16(s, format->nChannels);
	Stream_Read_UINT32(s, format->nSamplesPerSec);
	Stream_Read_UINT32(s, format->nAvgBytesPerSec);
	Stream_Read_UINT16(s, format->nBlockAlign);
	Stream_Read_UINT16(s, format->wBitsPerSample);
	Stream_Read_UINT16(s, format->cbSize);

	if (!Stream_CheckAndLogRequiredLength(TAG, s, format->cbSize))
		return FALSE;

	format->data = NULL;

	if (format->cbSize > 0)
	{
		format->data = malloc(format->cbSize);

		if (!format->data)
			return FALSE;

		Stream_Read(s, format->data, format->cbSize);
	}

	return TRUE;
}

BOOL audio_format_write(wStream* s, const AUDIO_FORMAT* format)
{
	if (!s || !format)
		return FALSE;

	if (!Stream_EnsureRemainingCapacity(s, 18 + format->cbSize))
		return FALSE;

	Stream_Write_UINT16(s, format->wFormatTag);      /* wFormatTag (WAVE_FORMAT_PCM) */
	Stream_Write_UINT16(s, format->nChannels);       /* nChannels */
	Stream_Write_UINT32(s, format->nSamplesPerSec);  /* nSamplesPerSec */
	Stream_Write_UINT32(s, format->nAvgBytesPerSec); /* nAvgBytesPerSec */
	Stream_Write_UINT16(s, format->nBlockAlign);     /* nBlockAlign */
	Stream_Write_UINT16(s, format->wBitsPerSample);  /* wBitsPerSample */
	Stream_Write_UINT16(s, format->cbSize);          /* cbSize */

	if (format->cbSize > 0)
		Stream_Write(s, format->data, format->cbSize);

	return TRUE;
}

BOOL audio_format_copy(const AUDIO_FORMAT* srcFormat, AUDIO_FORMAT* dstFormat)
{
	if (!srcFormat || !dstFormat)
		return FALSE;

	*dstFormat = *srcFormat;

	if (srcFormat->cbSize > 0)
	{
		dstFormat->data = malloc(srcFormat->cbSize);

		if (!dstFormat->data)
			return FALSE;

		memcpy(dstFormat->data, srcFormat->data, dstFormat->cbSize);
	}

	return TRUE;
}

BOOL audio_format_compatible(const AUDIO_FORMAT* with, const AUDIO_FORMAT* what)
{
	if (!with || !what)
		return FALSE;

	if (with->wFormatTag != WAVE_FORMAT_UNKNOWN)
	{
		if (with->wFormatTag != what->wFormatTag)
			return FALSE;
	}

	if (with->nChannels != 0)
	{
		if (with->nChannels != what->nChannels)
			return FALSE;
	}

	if (with->nSamplesPerSec != 0)
	{
		if (with->nSamplesPerSec != what->nSamplesPerSec)
			return FALSE;
	}

	if (with->wBitsPerSample != 0)
	{
		if (with->wBitsPerSample != what->wBitsPerSample)
			return FALSE;
	}

	return TRUE;
}

static BOOL audio_format_valid(const AUDIO_FORMAT* format)
{
	if (!format)
		return FALSE;

	if (format->nChannels == 0)
		return FALSE;

	if (format->nSamplesPerSec == 0)
		return FALSE;

	return TRUE;
}

AUDIO_FORMAT* audio_format_new(void)
{
	return audio_formats_new(1);
}

AUDIO_FORMAT* audio_formats_new(size_t count)
{
	return calloc(count, sizeof(AUDIO_FORMAT));
}

void audio_format_free(AUDIO_FORMAT* format)
{
	if (format)
		free(format->data);
}

void audio_formats_free(AUDIO_FORMAT* formats, size_t count)
{
	if (formats)
	{
		for (size_t index = 0; index < count; index++)
		{
			AUDIO_FORMAT* format = &formats[index];
			audio_format_free(format);
		}

		free(formats);
	}
}