-
-
Notifications
You must be signed in to change notification settings - Fork 236
Expand file tree
/
Copy pathsample_spec_parse.rl
More file actions
372 lines (305 loc) · 10.9 KB
/
sample_spec_parse.rl
File metadata and controls
372 lines (305 loc) · 10.9 KB
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/*
* Copyright (c) 2023 Roc Streaming authors
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include "roc_audio/channel_tables.h"
#include "roc_audio/sample_spec.h"
#include "roc_core/log.h"
#include "roc_core/macro_helpers.h"
#include "roc_core/panic.h"
namespace roc {
namespace audio {
%%{
machine parse_sample_spec;
write data;
}%%
namespace {
bool parse_size_t(const char* str, size_t str_len, size_t& result) {
char* str_end = NULL;
long num = strtol(str, &str_end, 10);
if (num == LONG_MAX || num == LONG_MIN || str_end != str + str_len) {
return false;
}
if (num < 0 || (uint64_t)num > (uint64_t)ROC_MAX_OF(size_t)) {
return false;
}
result = (size_t)num;
return true;
}
bool parse_surround_channel(const char* str, size_t str_len, ChannelPosition& result) {
for (size_t i = 0; i < ROC_ARRAY_SIZE(ChanPositionNames); i++) {
if (strlen(ChanPositionNames[i].name) == str_len &&
memcmp(ChanPositionNames[i].name, str, str_len) == 0) {
result = ChanPositionNames[i].pos;
return true;
}
}
return false;
}
bool parse_surround_mask(const char* str, size_t str_len, ChannelMask& result) {
for (size_t i = 0; i < ROC_ARRAY_SIZE(ChanMaskNames); i++) {
if (strlen(ChanMaskNames[i].name) == str_len &&
memcmp(ChanMaskNames[i].name, str, str_len) == 0) {
result = ChanMaskNames[i].mask;
return true;
}
}
return false;
}
bool parse_multitrack_channel(const char* str, size_t str_len, size_t& result) {
if (!parse_size_t(str, str_len, result)) {
return false;
}
if (result >= ChannelSet::max_channels()) {
return false;
}
return true;
}
bool parse_multitrack_mask(const char* str, size_t str_len, ChannelSet& result) {
size_t str_pos = str_len - 1;
size_t ch_pos = 0;
do {
char digit_str[2] = { str[str_pos], '\0' };
char* digit_end = NULL;
const long num = (unsigned)strtol(digit_str, &digit_end, 16);
if (num == LONG_MAX || num == LONG_MIN || digit_end != digit_str + 1) {
return false;
}
for (size_t bit = 0; bit < 4; bit++) {
if (num & (1 << bit)) {
result.toggle_channel(ch_pos, true);
}
ch_pos++;
}
} while (str_pos-- != 0);
return true;
}
bool parse_sample_rate(const char* str, size_t str_len, size_t& result) {
if (!parse_size_t(str, str_len, result)) {
return false;
}
if (result == 0) {
return false;
}
return true;
}
bool parse_format(const char* str, SampleSpec& sample_spec) {
const Format fmt = format_from_str(str);
if (fmt != Format_Invalid) {
sample_spec.set_format(fmt);
return true;
}
if (sample_spec.set_custom_format(str)) {
return true;
}
return false;
}
bool parse_subformat(const char* str, SampleSpec& sample_spec) {
switch (sample_spec.format()) {
case Format_Pcm:
case Format_Wav:
case Format_Custom: {
const PcmSubformat pcm_fmt = pcm_subformat_from_str(str);
if (pcm_fmt != PcmSubformat_Invalid) {
// This is PCM sub-format.
sample_spec.set_pcm_subformat(pcm_fmt);
return true;
}
if (sample_spec.format() != Format_Pcm) {
// This is custom sub-format. Allow only if format is not PCM.
if (sample_spec.set_custom_subformat(str)) {
return true;
}
}
} break;
case Format_Invalid:
case Format_Max:
break;
}
return false;
}
bool parse_sample_spec_imp(const char* str, SampleSpec& sample_spec) {
if (!str) {
roc_log(LogError, "parse sample spec: input string is null");
return false;
}
sample_spec.clear();
// for ragel
const char* p = str;
const char *pe = str + strlen(str);
const char *eof = pe;
int cs = 0;
// for start_token
const char* start_p = NULL;
// for mtr range
size_t mtr_range_begin = 0,
mtr_range_end = 0;
// parse result
bool success = false;
%%{
action start_token {
start_p = p;
}
action set_surround_mask {
ChannelMask ch_mask = 0;
if (!parse_surround_mask(start_p, p - start_p, ch_mask)) {
roc_log(LogError, "parse sample spec: invalid channel mask name '%.*s' in '%s'",
int(p - start_p), start_p, str);
return false;
}
sample_spec.channel_set().set_mask(ch_mask);
}
action set_surround_channel {
ChannelPosition ch_pos = ChanPos_Max;
if (!parse_surround_channel(start_p, p - start_p, ch_pos)) {
roc_log(LogError, "parse sample spec: invalid channel name '%.*s' in '%s'",
int(p - start_p), start_p, str);
return false;
}
sample_spec.channel_set().toggle_channel(ch_pos, true);
}
action set_surround {
sample_spec.channel_set().set_layout(ChanLayout_Surround);
sample_spec.channel_set().set_order(ChanOrder_Smpte);
}
action set_mtr_number {
size_t ch_pos = 0;
if (!parse_multitrack_channel(start_p, p - start_p, ch_pos)) {
roc_log(LogError, "parse sample spec: invalid channel number '%.*s' in '%s',"
" should be integer in range [0; %d]",
int(p - start_p), start_p, str,
(int)ChannelSet::max_channels() - 1);
return false;
}
sample_spec.channel_set().toggle_channel(ch_pos, true);
}
action set_mtr_range_begin {
if (!parse_multitrack_channel(start_p, p - start_p, mtr_range_begin)) {
roc_log(LogError, "parse sample spec: invalid channel number '%.*s',"
" should be integer in range [0; %d]",
int(p - start_p), start_p,
(int)ChannelSet::max_channels() - 1);
return false;
}
}
action set_mtr_range_end {
if (!parse_multitrack_channel(start_p, p - start_p, mtr_range_end)) {
roc_log(LogError, "parse sample spec: invalid channel number '%.*s' in '%s',"
" should be integer in range [0; %d]",
int(p - start_p), start_p, str,
(int)ChannelSet::max_channels() - 1);
return false;
}
}
action set_mtr_range {
sample_spec.channel_set().toggle_channel_range(
mtr_range_begin, mtr_range_end, true);
}
action set_mtr_mask {
if (!parse_multitrack_mask(start_p, p - start_p, sample_spec.channel_set())) {
roc_log(LogError, "parse sample spec: invalid channel mask '%.*s' in '%s'",
int(p - start_p), start_p, str);
return false;
}
}
action set_mtr {
sample_spec.channel_set().set_layout(ChanLayout_Multitrack);
sample_spec.channel_set().set_order(ChanOrder_None);
}
action set_format {
char buf[16] = {};
if (p - start_p > sizeof(buf) - 1) {
roc_log(LogError, "parse sample spec: invalid format '%.*s' in '%s'",
int(p - start_p), start_p, str);
return false;
}
strncat(buf, start_p, p - start_p);
if (!parse_format(buf, sample_spec)) {
roc_log(LogError, "parse sample spec: invalid format '%.*s' in '%s'",
int(p - start_p), start_p, str);
return false;
}
}
action set_subformat {
char buf[16] = {};
if (p - start_p > sizeof(buf) - 1) {
roc_log(LogError, "parse sample spec: invalid subformat '%.*s' in '%s'",
int(p - start_p), start_p, str);
return false;
}
strncat(buf, start_p, p - start_p);
if (!parse_subformat(buf, sample_spec)) {
roc_log(LogError, "parse sample spec: invalid subformat '%.*s' in '%s'",
int(p - start_p), start_p, str);
return false;
}
}
action set_rate {
size_t rate = 0;
if (!parse_sample_rate(start_p, p - start_p, rate)) {
roc_log(LogError, "parse sample spec: invalid sample rate '%.*s' in '%s'",
int(p - start_p), start_p, str);
return false;
}
sample_spec.set_sample_rate(rate);
}
delimiter = [,_\.] | space;
surround_mask = ([a-z] [a-z0-9.]+) >start_token %set_surround_mask;
surround_channel = [A-Z]+ >start_token %set_surround_channel;
surround_list = surround_channel (delimiter surround_channel)*;
surround = (surround_mask | surround_list) %set_surround;
mtr_mask_hex = [0-9a-fA-F]+ >start_token %set_mtr_mask;
mtr_mask = '0x' mtr_mask_hex;
mtr_number = [0-9]+ >start_token %set_mtr_number;
mtr_range_begin = [0-9]+ >start_token %set_mtr_range_begin;
mtr_range_end = [0-9]+ >start_token %set_mtr_range_end;
mtr_range = (mtr_range_begin '-' mtr_range_end) >start_token %set_mtr_range;
mtr_channel = mtr_number | mtr_range;
mtr_list = mtr_channel (delimiter mtr_channel)*;
mtr = (mtr_mask | mtr_list) %set_mtr;
format = [a-z0-9_]+ >start_token %set_format;
subformat = [a-z0-9_]+ >start_token %set_subformat;
rate = [0-9]+ >start_token %set_rate;
channels = surround | mtr;
main := ( ('-' | format ('@' subformat)?) '/' ('-' | rate) '/' ('-' | channels) )
%{ success = true; }
;
write init;
write exec;
}%%
if (!success) {
roc_log(LogError,
"parse sample spec: expected '<format>[@<subformat>]/<rate>/<channels>',"
" got '%s'", str);
return false;
}
return true;
}
bool validate_sample_spec(const SampleSpec& sample_spec) {
switch (sample_spec.format()) {
case Format_Pcm:
if (!sample_spec.has_subformat()) {
roc_log(LogError, "parse sample spec: subformat required when format is pcm");
return false;
}
break;
case Format_Invalid:
case Format_Custom:
case Format_Max:
break;
}
return true;
}
} // namespace
bool parse_sample_spec(const char* str, SampleSpec& result) {
if (!parse_sample_spec_imp(str, result) || !validate_sample_spec(result)) {
result.clear();
return false;
}
return true;
}
} // namespace audio
} // namespace roc