Coverage Report

Created: 2026-10-15 06:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/FreeRDP/libfreerdp/codec/audio.c
Line
Count
Source
1
/**
2
 * FreeRDP: A Remote Desktop Protocol Implementation
3
 * Audio Formats
4
 *
5
 * Copyright 2013 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6
 *
7
 * Licensed under the Apache License, Version 2.0 (the "License");
8
 * you may not use this file except in compliance with the License.
9
 * You may obtain a copy of the License at
10
 *
11
 *     http://www.apache.org/licenses/LICENSE-2.0
12
 *
13
 * Unless required by applicable law or agreed to in writing, software
14
 * distributed under the License is distributed on an "AS IS" BASIS,
15
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
 * See the License for the specific language governing permissions and
17
 * limitations under the License.
18
 */
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
29
UINT32 audio_format_compute_time_length(const AUDIO_FORMAT* format, size_t size)
30
0
{
31
0
  UINT32 mstime = 0;
32
0
  UINT32 wSamples = 0;
33
34
  /**
35
   * [MSDN-AUDIOFORMAT]:
36
   * http://msdn.microsoft.com/en-us/library/ms713497.aspx
37
   */
38
39
0
  if (format->wBitsPerSample)
40
0
  {
41
0
    const size_t samples = (size * 8) / format->wBitsPerSample;
42
0
    WINPR_ASSERT(samples <= UINT32_MAX);
43
0
    wSamples = (UINT32)samples;
44
0
    mstime = (((wSamples * 1000) / format->nSamplesPerSec) / format->nChannels);
45
0
  }
46
0
  else
47
0
  {
48
0
    mstime = 0;
49
50
0
    if (format->wFormatTag == WAVE_FORMAT_GSM610)
51
0
    {
52
0
      UINT16 nSamplesPerBlock = 0;
53
54
0
      if ((format->cbSize == 2) && (format->data))
55
0
      {
56
0
        nSamplesPerBlock = *(WINPR_PACKED_ALIGN_CAST(UINT16*, format->data));
57
0
        const size_t samples = (size / format->nBlockAlign) * nSamplesPerBlock;
58
0
        WINPR_ASSERT(samples <= UINT32_MAX);
59
0
        wSamples = (UINT32)samples;
60
0
        mstime = (((wSamples * 1000) / format->nSamplesPerSec) / format->nChannels);
61
0
      }
62
0
      else
63
0
      {
64
0
        WLog_ERR(TAG,
65
0
                 "audio_format_compute_time_length: invalid WAVE_FORMAT_GSM610 format");
66
0
      }
67
0
    }
68
0
    else
69
0
    {
70
0
      WLog_ERR(TAG, "audio_format_compute_time_length: unknown format %" PRIu16 "",
71
0
               format->wFormatTag);
72
0
    }
73
0
  }
74
75
0
  return mstime;
76
0
}
77
78
const char* audio_format_get_tag_string(UINT16 wFormatTag)
79
0
{
80
0
  switch (wFormatTag)
81
0
  {
82
0
    case WAVE_FORMAT_PCM:
83
0
      return "WAVE_FORMAT_PCM";
84
85
0
    case WAVE_FORMAT_ADPCM:
86
0
      return "WAVE_FORMAT_ADPCM";
87
88
0
    case WAVE_FORMAT_ALAW:
89
0
      return "WAVE_FORMAT_ALAW";
90
91
0
    case WAVE_FORMAT_MULAW:
92
0
      return "WAVE_FORMAT_MULAW";
93
94
0
    case WAVE_FORMAT_DVI_ADPCM:
95
0
      return "WAVE_FORMAT_DVI_ADPCM";
96
97
0
    case WAVE_FORMAT_GSM610:
98
0
      return "WAVE_FORMAT_GSM610";
99
100
0
    case WAVE_FORMAT_MSG723:
101
0
      return "WAVE_FORMAT_MSG723";
102
103
0
    case WAVE_FORMAT_DSPGROUP_TRUESPEECH:
104
0
      return "WAVE_FORMAT_DSPGROUP_TRUESPEECH ";
105
106
0
    case WAVE_FORMAT_MPEGLAYER3:
107
0
      return "WAVE_FORMAT_MPEGLAYER3";
108
109
0
    case WAVE_FORMAT_WMAUDIO2:
110
0
      return "WAVE_FORMAT_WMAUDIO2";
111
112
0
    case WAVE_FORMAT_AAC_MS:
113
0
      return "WAVE_FORMAT_AAC_MS";
114
115
0
    case WAVE_FORMAT_OPUS:
116
0
      return "WAVE_FORMAT_OPUS";
117
118
0
    default:
119
0
      return "WAVE_FORMAT_UNKNOWN";
120
0
  }
121
0
}
122
123
void audio_format_print(wLog* log, DWORD level, const AUDIO_FORMAT* format)
124
0
{
125
0
  WLog_Print(log, level,
126
0
             "%s:\t wFormatTag: 0x%04" PRIX16 " nChannels: %" PRIu16 " nSamplesPerSec: %" PRIu32
127
0
             " "
128
0
             "nAvgBytesPerSec: %" PRIu32 " nBlockAlign: %" PRIu16 " wBitsPerSample: %" PRIu16
129
0
             " cbSize: %" PRIu16 "",
130
0
             audio_format_get_tag_string(format->wFormatTag), format->wFormatTag,
131
0
             format->nChannels, format->nSamplesPerSec, format->nAvgBytesPerSec,
132
0
             format->nBlockAlign, format->wBitsPerSample, format->cbSize);
133
0
}
134
135
void audio_formats_print(wLog* log, DWORD level, const AUDIO_FORMAT* formats, UINT16 count)
136
0
{
137
0
  if (formats)
138
0
  {
139
0
    WLog_Print(log, level, "AUDIO_FORMATS (%" PRIu16 ") ={", count);
140
141
0
    for (UINT32 index = 0; index < count; index++)
142
0
    {
143
0
      const AUDIO_FORMAT* format = &formats[index];
144
0
      WLog_Print(log, level, "\t");
145
0
      audio_format_print(log, level, format);
146
0
    }
147
148
0
    WLog_Print(log, level, "}");
149
0
  }
150
0
}
151
152
BOOL audio_format_read(wStream* s, AUDIO_FORMAT* format)
153
0
{
154
0
  if (!s || !format)
155
0
    return FALSE;
156
157
0
  if (!Stream_CheckAndLogRequiredLength(TAG, s, 18))
158
0
    return FALSE;
159
160
0
  Stream_Read_UINT16(s, format->wFormatTag);
161
0
  Stream_Read_UINT16(s, format->nChannels);
162
0
  Stream_Read_UINT32(s, format->nSamplesPerSec);
163
0
  Stream_Read_UINT32(s, format->nAvgBytesPerSec);
164
0
  Stream_Read_UINT16(s, format->nBlockAlign);
165
0
  Stream_Read_UINT16(s, format->wBitsPerSample);
166
0
  Stream_Read_UINT16(s, format->cbSize);
167
168
0
  if (!Stream_CheckAndLogRequiredLength(TAG, s, format->cbSize))
169
0
    return FALSE;
170
171
0
  format->data = nullptr;
172
173
0
  if (format->cbSize > 0)
174
0
  {
175
0
    format->data = malloc(format->cbSize);
176
177
0
    if (!format->data)
178
0
      return FALSE;
179
180
0
    Stream_Read(s, format->data, format->cbSize);
181
0
  }
182
183
0
  return TRUE;
184
0
}
185
186
BOOL audio_format_write(wStream* s, const AUDIO_FORMAT* format)
187
0
{
188
0
  if (!s || !format)
189
0
    return FALSE;
190
191
0
  if (!Stream_EnsureRemainingCapacity(s, 18ull + format->cbSize))
192
0
    return FALSE;
193
194
0
  Stream_Write_UINT16(s, format->wFormatTag);      /* wFormatTag (WAVE_FORMAT_PCM) */
195
0
  Stream_Write_UINT16(s, format->nChannels);       /* nChannels */
196
0
  Stream_Write_UINT32(s, format->nSamplesPerSec);  /* nSamplesPerSec */
197
0
  Stream_Write_UINT32(s, format->nAvgBytesPerSec); /* nAvgBytesPerSec */
198
0
  Stream_Write_UINT16(s, format->nBlockAlign);     /* nBlockAlign */
199
0
  Stream_Write_UINT16(s, format->wBitsPerSample);  /* wBitsPerSample */
200
0
  Stream_Write_UINT16(s, format->cbSize);          /* cbSize */
201
202
0
  if (format->cbSize > 0)
203
0
    Stream_Write(s, format->data, format->cbSize);
204
205
0
  return TRUE;
206
0
}
207
208
BOOL audio_format_copy(const AUDIO_FORMAT* WINPR_RESTRICT srcFormat,
209
                       AUDIO_FORMAT* WINPR_RESTRICT dstFormat)
210
0
{
211
0
  if (!srcFormat || !dstFormat)
212
0
    return FALSE;
213
214
0
  *dstFormat = *srcFormat;
215
216
0
  if (srcFormat->cbSize > 0)
217
0
  {
218
0
    dstFormat->data = malloc(srcFormat->cbSize);
219
220
0
    if (!dstFormat->data)
221
0
      return FALSE;
222
223
0
    memcpy(dstFormat->data, srcFormat->data, dstFormat->cbSize);
224
0
  }
225
226
0
  return TRUE;
227
0
}
228
229
BOOL audio_format_compatible(const AUDIO_FORMAT* with, const AUDIO_FORMAT* what)
230
0
{
231
0
  if (!with || !what)
232
0
    return FALSE;
233
234
0
  if (with->wFormatTag != WAVE_FORMAT_UNKNOWN)
235
0
  {
236
0
    if (with->wFormatTag != what->wFormatTag)
237
0
      return FALSE;
238
0
  }
239
240
0
  if (with->nChannels != 0)
241
0
  {
242
0
    if (with->nChannels != what->nChannels)
243
0
      return FALSE;
244
0
  }
245
246
0
  if (with->nSamplesPerSec != 0)
247
0
  {
248
0
    if (with->nSamplesPerSec != what->nSamplesPerSec)
249
0
      return FALSE;
250
0
  }
251
252
0
  if (with->wBitsPerSample != 0)
253
0
  {
254
0
    if (with->wBitsPerSample != what->wBitsPerSample)
255
0
      return FALSE;
256
0
  }
257
258
0
  return TRUE;
259
0
}
260
261
AUDIO_FORMAT* audio_format_new(void)
262
0
{
263
0
  return audio_formats_new(1);
264
0
}
265
266
AUDIO_FORMAT* audio_formats_new(size_t count)
267
0
{
268
0
  return calloc(count, sizeof(AUDIO_FORMAT));
269
0
}
270
271
void audio_format_free(AUDIO_FORMAT* format)
272
0
{
273
0
  if (format)
274
0
    free(format->data);
275
0
}
276
277
void audio_formats_free(AUDIO_FORMAT* formats, size_t count)
278
0
{
279
0
  if (formats)
280
0
  {
281
0
    for (size_t index = 0; index < count; index++)
282
0
    {
283
0
      AUDIO_FORMAT* format = &formats[index];
284
0
      audio_format_free(format);
285
0
    }
286
287
0
    free(formats);
288
0
  }
289
0
}