Debian Codegraph @

modules/lsp-runtime-lib/src/test/utest/fmt/lspc/lspc.cpp

raw
1 /*
2 * Copyright (C) 2025 Linux Studio Plugins Project <https://lsp-plug.in/>
3 * (C) 2025 Vladimir Sadovnikov <[email protected]>
4 *
5 * This file is part of lsp-runtime-lib
6 * Created on: 28 авг. 2018 г.
7 *
8 * lsp-runtime-lib is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * any later version.
12 *
13 * lsp-runtime-lib is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with lsp-runtime-lib. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22 #include <lsp-plug.in/common/endian.h>
23 #include <lsp-plug.in/fmt/lspc/File.h>
24 #include <lsp-plug.in/runtime/LSPString.h>
25 #include <lsp-plug.in/stdlib/string.h>
26 #include <lsp-plug.in/test-fw/utest.h>
27 #include <lsp-plug.in/test-fw/ByteBuffer.h>
28
29 #define EXTRA_SIZE 0x10
30 #define BUFFER_SIZE 0x100000
31
32 namespace lsp
33 {
34 typedef struct lspc_chunk_audio_header_v2_t: public lspc::chunk_audio_header_t
35 {
36 uint8_t extra[EXTRA_SIZE];
37 } lspc_chunk_audio_header_v2_t;
38
39 typedef struct lspc_chunk_audio_profile_v2_t: public lspc::chunk_audio_profile_t
40 {
41 uint8_t extra[EXTRA_SIZE];
42 } lspc_chunk_audio_profile_v2_t;
43 }
44
45 UTEST_BEGIN("runtime.fmt.lspc", lspc)
46
47 void create_lspc_file(const ByteBuffer &content, bool v2)
48 {
49 lspc::File fd;
50 io::Path path;
51 UTEST_ASSERT(path.fmt("%s/utest-%s.lspc", tempdir(), full_name()));
52
53 printf("Writing file %s ...\n", path.as_native());
54 status_t res = fd.create(&path);
55
56 UTEST_ASSERT(res == STATUS_OK);
57
58 // Write audio chunk
59 lspc::ChunkWriter *wr = fd.write_chunk(LSPC_CHUNK_AUDIO);
60 UTEST_ASSERT(wr != NULL);
61
62 if (v2)
63 {
64 lspc_chunk_audio_header_v2_t ahdr2;
65 bzero(&ahdr2, sizeof(ahdr2));
66
67 ahdr2.common.version = 2;
68 ahdr2.common.size = sizeof(lspc_chunk_audio_header_v2_t);
69 ahdr2.channels = 1;
70 ahdr2.sample_format = lspc::SAMPLE_FMT_F32LE;
71 ahdr2.sample_rate = 48000;
72 ahdr2.codec = lspc::CODEC_PCM;
73 ahdr2.frames = (content.size() / sizeof(float));
74
75 ahdr2.channels = BE_TO_CPU(ahdr2.channels);
76 ahdr2.sample_format = BE_TO_CPU(ahdr2.sample_format);
77 ahdr2.sample_rate = BE_TO_CPU(ahdr2.sample_rate);
78 ahdr2.codec = BE_TO_CPU(ahdr2.codec);
79 ahdr2.frames = BE_TO_CPU(ahdr2.frames);
80
81 for (size_t i=0; i < EXTRA_SIZE; ++i)
82 ahdr2.extra[i] = uint8_t(i + 1);
83
84 res = wr->write_header(&ahdr2);
85 UTEST_ASSERT(res == STATUS_OK);
86 }
87 else
88 {
89 lspc::chunk_audio_header_t ahdr1;
90 bzero(&ahdr1, sizeof(ahdr1));
91
92 ahdr1.common.version = 1;
93 ahdr1.common.size = sizeof(lspc::chunk_audio_header_t);
94 ahdr1.channels = 1;
95 ahdr1.sample_format = lspc::SAMPLE_FMT_F32LE;
96 ahdr1.sample_rate = 48000;
97 ahdr1.codec = lspc::CODEC_PCM;
98 ahdr1.frames = (content.size() / sizeof(float));
99
100 ahdr1.channels = BE_TO_CPU(ahdr1.channels);
101 ahdr1.sample_format = BE_TO_CPU(ahdr1.sample_format);
102 ahdr1.sample_rate = BE_TO_CPU(ahdr1.sample_rate);
103 ahdr1.codec = BE_TO_CPU(ahdr1.codec);
104 ahdr1.frames = BE_TO_CPU(ahdr1.frames);
105
106 res = wr->write_header(&ahdr1);
107 UTEST_ASSERT(res == STATUS_OK);
108 }
109
110 // Flush data
111 res = wr->flush();
112 UTEST_ASSERT(res == STATUS_OK);
113
114 // Write contents of audio chunk
115 size_t chunk_id = wr->unique_id();
116 res = wr->write(content, content.size());
117 UTEST_ASSERT(res == STATUS_OK);
118
119 // Flush data
120 res = wr->flush();
121 UTEST_ASSERT(res == STATUS_OK);
122
123 // Close chunk
124 res = wr->close();
125 UTEST_ASSERT(res == STATUS_OK);
126 delete wr;
127
128 // Write profile chunk
129 wr = fd.write_chunk(LSPC_CHUNK_PROFILE);
130 UTEST_ASSERT(wr != NULL);
131
132 if (v2)
133 {
134 lspc_chunk_audio_profile_v2_t prof2;
135 bzero(&prof2, sizeof(prof2));
136
137 prof2.common.version = 2;
138 prof2.common.size = sizeof(lspc_chunk_audio_profile_v2_t);
139 prof2.chunk_id = lspc::chunk_id_t(chunk_id);
140 prof2.chirp_order = 1;
141 prof2.alpha = 1.0;
142 prof2.beta = 2.0;
143 prof2.gamma = 3.0;
144 prof2.delta = 4.0;
145 prof2.initial_freq = 10.0;
146 prof2.final_freq = 48000.0;
147
148 prof2.chunk_id = CPU_TO_BE(prof2.chunk_id);
149 prof2.chirp_order = CPU_TO_BE(prof2.chirp_order);
150 prof2.alpha = CPU_TO_BE(prof2.alpha);
151 prof2.beta = CPU_TO_BE(prof2.beta);
152 prof2.gamma = CPU_TO_BE(prof2.gamma);
153 prof2.delta = CPU_TO_BE(prof2.delta);
154 prof2.initial_freq = CPU_TO_BE(prof2.initial_freq);
155 prof2.final_freq = CPU_TO_BE(prof2.final_freq);
156
157 for (size_t i=0; i < EXTRA_SIZE; ++i)
158 prof2.extra[i] = uint8_t(i + 1);
159
160 res = wr->write_header(&prof2);
161 UTEST_ASSERT(res == STATUS_OK);
162 }
163 else
164 {
165 lspc::chunk_audio_profile_t prof1;
166 bzero(&prof1, sizeof(prof1));
167
168 prof1.common.version = 1;
169 prof1.common.size = sizeof(lspc::chunk_audio_profile_t);
170 prof1.chunk_id = lspc::chunk_id_t(chunk_id);
171 prof1.chirp_order = 1;
172 prof1.alpha = 1.0;
173 prof1.beta = 2.0;
174 prof1.gamma = 3.0;
175 prof1.delta = 4.0;
176 prof1.initial_freq = 10.0;
177 prof1.final_freq = 48000.0;
178
179 prof1.chunk_id = CPU_TO_BE(prof1.chunk_id);
180 prof1.chirp_order = CPU_TO_BE(prof1.chirp_order);
181 prof1.alpha = CPU_TO_BE(prof1.alpha);
182 prof1.beta = CPU_TO_BE(prof1.beta);
183 prof1.gamma = CPU_TO_BE(prof1.gamma);
184 prof1.delta = CPU_TO_BE(prof1.delta);
185 prof1.initial_freq = CPU_TO_BE(prof1.initial_freq);
186 prof1.final_freq = CPU_TO_BE(prof1.final_freq);
187
188 res = wr->write_header(&prof1);
189 UTEST_ASSERT(res == STATUS_OK);
190 }
191
192 // Flush data
193 res = wr->flush();
194 UTEST_ASSERT(res == STATUS_OK);
195
196 // Close header
197 res = wr->close();
198 UTEST_ASSERT(res == STATUS_OK);
199 delete wr;
200
201 // Close file
202 res = fd.close();
203 UTEST_ASSERT(res == STATUS_OK);
204 }
205
206 void read_lspc_file(ByteBuffer &content, bool v2)
207 {
208 lspc::File fd;
209 io::Path path;
210
211 UTEST_ASSERT(path.fmt("%s/utest-%s.lspc", tempdir(), full_name()));
212
213 printf("Reading file %s ...\n", path.as_native());
214 status_t res = fd.open(&path);
215
216 UTEST_ASSERT(res == STATUS_OK);
217
218 // Find profile chunk
219 uint32_t chunk_id = 0;
220 lspc::ChunkReader *rd = fd.find_chunk(LSPC_CHUNK_PROFILE, &chunk_id);
221 UTEST_ASSERT_MSG(rd != NULL, "Error while searching for profile chunk");
222
223 if (v2)
224 {
225 // Read as 'upgraded' version
226 lspc_chunk_audio_profile_v2_t prof2;
227 ssize_t n = rd->read_header(&prof2, sizeof(lspc_chunk_audio_profile_v2_t));
228 UTEST_ASSERT( n >= 0 );
229 res = status_t(rd->last_error());
230 UTEST_ASSERT_MSG((res == STATUS_OK) || (res == STATUS_EOF), "Invalid last_error status: %d", int(res));
231
232 prof2.chunk_id = CPU_TO_BE(prof2.chunk_id);
233 prof2.chirp_order = CPU_TO_BE(prof2.chirp_order);
234 prof2.alpha = CPU_TO_BE(prof2.alpha);
235 prof2.beta = CPU_TO_BE(prof2.beta);
236 prof2.gamma = CPU_TO_BE(prof2.gamma);
237 prof2.delta = CPU_TO_BE(prof2.delta);
238 prof2.initial_freq = CPU_TO_BE(prof2.initial_freq);
239 prof2.final_freq = CPU_TO_BE(prof2.final_freq);
240
241 UTEST_ASSERT(prof2.chirp_order == 1);
242 UTEST_ASSERT(prof2.alpha == 1.0);
243 UTEST_ASSERT(prof2.beta == 2.0);
244 UTEST_ASSERT(prof2.gamma == 3.0);
245 UTEST_ASSERT(prof2.delta == 4.0);
246 UTEST_ASSERT(prof2.initial_freq == 10.0);
247 UTEST_ASSERT(prof2.final_freq == 48000.0);
248
249 if (prof2.common.version == 1)
250 {
251 UTEST_ASSERT(prof2.common.size == sizeof(lspc::chunk_audio_profile_t));
252 for (size_t i=0; i < EXTRA_SIZE; ++i)
253 UTEST_ASSERT(prof2.extra[i] == 0);
254 }
255 else if (prof2.common.version == 2)
256 {
257 UTEST_ASSERT(prof2.common.size == sizeof(lspc_chunk_audio_profile_v2_t));
258 for (size_t i=0; i < EXTRA_SIZE; ++i)
259 UTEST_ASSERT(prof2.extra[i] == uint8_t(i + 1));
260 }
261 else
262 {
263 UTEST_FAIL_MSG("Invalid profile header version: %d", int(prof2.common.version));
264 }
265
266 chunk_id = prof2.chunk_id;
267 }
268 else
269 {
270 // Read as 'legacy' version
271 lspc::chunk_audio_profile_t prof1;
272 ssize_t n = rd->read_header(&prof1, sizeof(lspc::chunk_audio_profile_t));
273 UTEST_ASSERT( n >= 0 );
274 res = status_t(rd->last_error());
275 UTEST_ASSERT_MSG((res == STATUS_OK) || (res == STATUS_EOF), "Invalid last_error status: %d", int(res));
276
277 prof1.chunk_id = CPU_TO_BE(prof1.chunk_id);
278 prof1.chirp_order = CPU_TO_BE(prof1.chirp_order);
279 prof1.alpha = CPU_TO_BE(prof1.alpha);
280 prof1.beta = CPU_TO_BE(prof1.beta);
281 prof1.gamma = CPU_TO_BE(prof1.gamma);
282 prof1.delta = CPU_TO_BE(prof1.delta);
283 prof1.initial_freq = CPU_TO_BE(prof1.initial_freq);
284 prof1.final_freq = CPU_TO_BE(prof1.final_freq);
285
286 UTEST_ASSERT(prof1.chirp_order == 1);
287 UTEST_ASSERT(prof1.alpha == 1.0);
288 UTEST_ASSERT(prof1.beta == 2.0);
289 UTEST_ASSERT(prof1.gamma == 3.0);
290 UTEST_ASSERT(prof1.delta == 4.0);
291 UTEST_ASSERT(prof1.initial_freq == 10.0);
292 UTEST_ASSERT(prof1.final_freq == 48000.0);
293
294 UTEST_ASSERT(prof1.common.size == sizeof(lspc::chunk_audio_profile_t));
295 UTEST_ASSERT_MSG((prof1.common.version == 1) || (prof1.common.version == 2),
296 "Invalid profile header version: %d", int(prof1.common.version));
297
298 chunk_id = prof1.chunk_id;
299 }
300
301 // Try to read data after profile header
302 ssize_t count = rd->read(content.data(), content.size());
303 UTEST_ASSERT_MSG(count <= 0, "Extra data found after profile header");
304 res = status_t(rd->last_error());
305 UTEST_ASSERT_MSG((res == STATUS_OK) || (res == STATUS_EOF), "Invalid last_error status: %d", int(res));
306
307 // Close chunk reader
308 res = rd->close();
309 UTEST_ASSERT(res == STATUS_OK);
310 delete rd;
311
312 // Read audio chunk
313 rd = fd.read_chunk(chunk_id);
314 UTEST_ASSERT_MSG(rd != NULL, "Error while reading audio chunk id=%d", int(chunk_id));
315 UTEST_ASSERT(rd->unique_id() == chunk_id);
316 UTEST_ASSERT(rd->magic() == LSPC_CHUNK_AUDIO);
317
318 if (v2)
319 {
320 // Read as 'upgraded' version
321 lspc_chunk_audio_header_v2_t ahdr2;
322 bzero(&ahdr2, sizeof(ahdr2));
323
324 ssize_t n = rd->read_header(&ahdr2, sizeof(lspc_chunk_audio_header_v2_t));
325 UTEST_ASSERT(n >= 0);
326 res = status_t(rd->last_error());
327 UTEST_ASSERT_MSG((res == STATUS_OK), "Invalid last_error status: %d", int(res));
328
329 ahdr2.channels = BE_TO_CPU(ahdr2.channels);
330 ahdr2.sample_format = BE_TO_CPU(ahdr2.sample_format);
331 ahdr2.sample_rate = BE_TO_CPU(ahdr2.sample_rate);
332 ahdr2.codec = BE_TO_CPU(ahdr2.codec);
333 ahdr2.frames = BE_TO_CPU(ahdr2.frames);
334
335 UTEST_ASSERT(ahdr2.channels == 1);
336 UTEST_ASSERT(ahdr2.sample_format == lspc::SAMPLE_FMT_F32LE);
337 UTEST_ASSERT(ahdr2.sample_rate == 48000);
338 UTEST_ASSERT(ahdr2.codec == lspc::CODEC_PCM);
339 UTEST_ASSERT(ahdr2.frames == (content.size() / sizeof(float)));
340
341 if (ahdr2.common.version == 1)
342 {
343 UTEST_ASSERT(ahdr2.common.size == sizeof(lspc::chunk_audio_header_t));
344 for (size_t i=0; i < EXTRA_SIZE; ++i)
345 UTEST_ASSERT(ahdr2.extra[i] == 0);
346 }
347 else if (ahdr2.common.version == 2)
348 {
349 UTEST_ASSERT(ahdr2.common.size == sizeof(lspc_chunk_audio_header_v2_t));
350 for (size_t i=0; i < EXTRA_SIZE; ++i)
351 UTEST_ASSERT(ahdr2.extra[i] == uint8_t(i + 1));
352 }
353 else
354 {
355 UTEST_FAIL_MSG("Invalid profile header version: %d", int(ahdr2.common.version));
356 }
357 }
358 else
359 {
360 // Read as 'legacy' version
361 lspc::chunk_audio_header_t ahdr1;
362 bzero(&ahdr1, sizeof(ahdr1));
363
364 ssize_t n = rd->read_header(&ahdr1, sizeof(lspc::chunk_audio_header_t));
365 UTEST_ASSERT(n >= 0);
366 res = status_t(rd->last_error());
367 UTEST_ASSERT_MSG((res == STATUS_OK), "Invalid last_error status: %d", int(res));
368
369 ahdr1.channels = BE_TO_CPU(ahdr1.channels);
370 ahdr1.sample_format = BE_TO_CPU(ahdr1.sample_format);
371 ahdr1.sample_rate = BE_TO_CPU(ahdr1.sample_rate);
372 ahdr1.codec = BE_TO_CPU(ahdr1.codec);
373 ahdr1.frames = BE_TO_CPU(ahdr1.frames);
374
375 UTEST_ASSERT(ahdr1.channels == 1);
376 UTEST_ASSERT(ahdr1.sample_format == lspc::SAMPLE_FMT_F32LE);
377 UTEST_ASSERT(ahdr1.sample_rate == 48000);
378 UTEST_ASSERT(ahdr1.codec == lspc::CODEC_PCM);
379 UTEST_ASSERT(ahdr1.frames == (content.size() / sizeof(float)));
380
381 UTEST_ASSERT(ahdr1.common.size == sizeof(lspc::chunk_audio_header_t));
382 UTEST_ASSERT_MSG((ahdr1.common.version == 1) || (ahdr1.common.version == 2),
383 "Invalid profile header version: %d", int(ahdr1.common.version));
384 }
385
386 // Read buffer data
387 count = rd->read(content.data(), content.size());
388 UTEST_ASSERT_MSG((count == ssize_t(content.size())),
389 "Invalid read size: %d, expected: %d", int(count), int(content.size()));
390
391 // Try to read data after profile header
392 count = rd->read(content.data(), content.size());
393 UTEST_ASSERT_MSG(count <= 0, "Invalid read size: %d, expected: %d", int(count), int(0));
394 res = status_t(rd->last_error());
395 UTEST_ASSERT_MSG((res == STATUS_OK) || (res == STATUS_EOF), "Invalid last_error status: %d", int(res));
396
397 // Close chunk reader
398 res = rd->close();
399 UTEST_ASSERT(res == STATUS_OK);
400 delete rd;
401
402 // Close file
403 res = fd.close();
404 UTEST_ASSERT(res == STATUS_OK);
405 }
406
407
408 UTEST_MAIN
409 {
410 ByteBuffer src(BUFFER_SIZE);
411 for (size_t i=0; i<=0x03; ++i)
412 {
413 printf("Writing %s data, reading %s data\n",
414 ((i & 0x01) ? "v2" : "v1"),
415 ((i & 0x02) ? "v2" : "v1")
416 );
417
418 ByteBuffer dst(BUFFER_SIZE);
419 create_lspc_file(src, i & 0x01);
420 read_lspc_file(dst, i & 0x02);
421
422 UTEST_ASSERT_MSG(src.valid(), "Source buffer corrupted");
423 UTEST_ASSERT_MSG(dst.valid(), "Destination buffer corrupted");
424
425 if (!src.equals(dst))
426 {
427 src.dump("src");
428 dst.dump("dst");
429 UTEST_FAIL_MSG("Source and destination buffers differ");
430 }
431 }
432 }
433
434 UTEST_END
435
436
437