Debian Codegraph @

modules/lsp-runtime-lib/src/main/fmt/obj/Decompressor.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: 26 июл. 2025 г.
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/alloc.h>
23 #include <lsp-plug.in/fmt/obj/const.h>
24 #include <lsp-plug.in/fmt/obj/Decompressor.h>
25 #include <lsp-plug.in/io/InMemoryStream.h>
26
27 namespace lsp
28 {
29 namespace obj
30 {
31 static const uint8_t event_codes[] =
32 {
33 CEV_VERTEX3, // 0x00
34 CEV_NORMAL3, // 0x01
35 CEV_FACE_N, // 0x02
36 CEV_FACE, // 0x03
37
38 CEV_TEXCOORD2, // 0x08
39 CEV_FACE_NF, // 0x09
40 CEV_FACE_TNF, // 0x0a
41 CEV_OBJECT, // 0x0b
42
43 CEV_FACE_T, // 0x18
44 CEV_FACE_TN, // 0x19
45 CEV_PVERTEX3, // 0x1a
46 CEV_LINE, // 0x1b
47
48 CEV_LINE_T, // 0x38
49 CEV_POINT, // 0x39
50 CEV_VERTEX2, // 0x3a
51 CEV_NORMAL2, // 0x3b
52
53 CEV_VERTEX4, // 0x78
54 CEV_PVERTEX2, // 0x79
55 CEV_PVERTEX4, // 0x7a
56 CEV_NORMAL4, // 0x7b
57
58 CEV_TEXCOORD1, // 0xf8
59 CEV_TEXCOORD3, // 0xf9
60 CEV_EOF, // 0xfa
61 CEV_EOF, // 0xfb
62 };
63
64 static inline float bin_to_float(int32_t v)
65 {
66 union {
67 float f32;
68 int32_t i32;
69 } cvt;
70
71 cvt.i32 = v;
72 return cvt.f32;
73 }
74
75 static inline int32_t zigzag_decode(uint32_t value)
76 {
77 return (int32_t(value) >> 1) ^ -(int32_t(value) & 1);
78 }
79
80 Decompressor::Decompressor()
81 {
82 vFloatBuf = NULL;
83
84 nFloatHead = 0;
85 nFloatSize = 0;
86 nFloatCap = 0;
87 nFloatBits = 0;
88
89 nLastEvent = compressed_event_type_t(-1);
90 }
91
92 Decompressor::~Decompressor()
93 {
94 clear_state();
95 }
96
97 void Decompressor::clear_state()
98 {
99 if (vFloatBuf != NULL)
100 {
101 free(vFloatBuf);
102 vFloatBuf = NULL;
103 }
104 }
105
106 status_t Decompressor::parse_file(IObjHandler *handler, const char *path)
107 {
108 clear_state();
109 status_t res = sStream.open(path);
110 if (res == STATUS_OK)
111 res = parse_data(handler);
112 res = update_status(res, sStream.close());
113 clear_state();
114 return res;
115 }
116
117 status_t Decompressor::parse_file(IObjHandler *handler, const LSPString *path)
118 {
119 clear_state();
120 status_t res = sStream.open(path);
121 if (res == STATUS_OK)
122 res = parse_data(handler);
123 res = update_status(res, sStream.close());
124 clear_state();
125 return res;
126 }
127
128 status_t Decompressor::parse_file(IObjHandler *handler, const io::Path *path)
129 {
130 clear_state();
131 status_t res = sStream.open(path);
132 if (res == STATUS_OK)
133 res = parse_data(handler);
134 res = update_status(res, sStream.close());
135 clear_state();
136 return res;
137 }
138
139 status_t Decompressor::parse_data(IObjHandler *handler, io::IInStream *is, size_t flags)
140 {
141 clear_state();
142 status_t res = sStream.wrap(is, flags);
143 if (res == STATUS_OK)
144 res = parse_data(handler);
145 res = update_status(res, sStream.close());
146 clear_state();
147 return res;
148 }
149
150 status_t Decompressor::parse_data(IObjHandler *handler, const void *data, size_t size)
151 {
152 clear_state();
153 io::InMemoryStream is;
154 is.wrap(data, size);
155 status_t res = sStream.wrap(&is, WRAP_CLOSE);
156 if (res == STATUS_OK)
157 res = parse_data(handler);
158 res = update_status(res, sStream.close());
159 clear_state();
160 return res;
161 }
162
163 status_t Decompressor::parse_header()
164 {
165 compressed_header_t hdr;
166
167 // Read header
168 ssize_t nread = sStream.read_fully(&hdr, sizeof(compressed_header_t));
169 if (nread < 0)
170 return status_t(-nread);
171 else if (nread != sizeof(compressed_header_t))
172 return STATUS_BAD_FORMAT;
173
174 // Validate header
175 if (hdr.signature != COMPRESSED_SIGNATURE)
176 return STATUS_BAD_FORMAT;
177 if (hdr.version != 0)
178 return STATUS_UNSUPPORTED_FORMAT;
179
180 if ((hdr.float_bits > MAX_FLOAT_BUF_BITS) || (hdr.float_bits < MIN_FLOAT_BUF_BITS))
181 return STATUS_CORRUPTED;
182 if ((hdr.pad[0] != 'L') || (hdr.pad[1] != 'S'))
183 return STATUS_BAD_FORMAT;
184
185 // Check that there is nothing to change
186 const size_t float_cap = 1 << hdr.float_bits;
187 float *ptr = static_cast<float *>(malloc(float_cap * sizeof(float)));
188 if (ptr == NULL)
189 return STATUS_NO_MEM;
190
191 vFloatBuf = ptr;
192 nFloatHead = 0;
193 nFloatSize = 0;
194 nFloatCap = uint32_t(float_cap);
195 nFloatBits = uint32_t(hdr.float_bits);
196
197 return STATUS_OK;
198 }
199
200 status_t Decompressor::read_event(compressed_event_type_t *event)
201 {
202 bool b;
203 size_t group = 0;
204 uint32_t cmd = 0;
205
206 // Check if command has been repeated
207 ssize_t nread = sStream.readb(&b);
208 if (nread != 1)
209 return (nread < 0) ? status_t(-nread) : STATUS_CORRUPTED;
210
211 if (b)
212 {
213 if (nLastEvent == compressed_event_type_t(-1))
214 return STATUS_CORRUPTED;
215
216 *event = nLastEvent;
217 return STATUS_OK;
218 }
219
220 // Determine group of commands
221 while (true)
222 {
223 nread = sStream.readb(&b);
224 if (nread != 1)
225 return (nread < 0) ? status_t(-nread) : STATUS_CORRUPTED;
226 if (!b)
227 break;
228 ++group;
229 }
230
231 // Read subcommand identifier
232 nread = sStream.readv(&cmd, 2);
233 if (nread != 2)
234 return (nread < 0) ? status_t(-nread) : STATUS_CORRUPTED;
235
236 // Convert subcommand + group to event code
237 cmd = uint32_t((group << 2) | cmd);
238 if (cmd >= sizeof(event_codes))
239 return STATUS_CORRUPTED;
240
241 nLastEvent = compressed_event_type_t(event_codes[cmd]);
242 *event = nLastEvent;
243
244 return STATUS_OK;
245 }
246
247 status_t Decompressor::read_float(float *dst)
248 {
249 // Read index
250 uint32_t index = 0;
251 ssize_t nread = sStream.readv(&index, nFloatBits);
252 if (nread != ssize_t(nFloatBits))
253 return (nread < 0) ? status_t(-nread) : STATUS_CORRUPTED;
254
255 // Analyze index
256 const uint32_t base = nFloatHead + nFloatCap - 1;
257
258 if (index < nFloatSize)
259 {
260 *dst = vFloatBuf[(base - index) % nFloatCap];
261 if (index > 0)
262 {
263 // Advance position of item one step forward if it is not the first one
264 const uint32_t idx = (base - index) % nFloatCap;
265 lsp::swap(vFloatBuf[idx], vFloatBuf[(idx + 1) % nFloatCap]);
266 }
267
268 return STATUS_OK;
269 }
270
271 float value;
272 if (index == nFloatSize)
273 {
274 // Read index of base float
275 nread = sStream.readv(&index, nFloatBits);
276 if (nread != ssize_t(nFloatBits))
277 return (nread < 0) ? status_t(-nread) : STATUS_CORRUPTED;
278
279 // Read delta
280 size_t delta = 0;
281 status_t res = read_varint(&delta);
282 if (res != STATUS_OK)
283 return res;
284
285 // Compute floating-point value
286 int32_t dvalue = zigzag_decode(int32_t(delta));
287 const int32_t image = vIntBuf[(base - index) % nFloatCap] + dvalue;
288 value = bin_to_float(image);
289 }
290 else
291 {
292 // Read just the floating-point value
293 int32_t image;
294 nread = sStream.readv(&image);
295 if (nread != ssize_t(sizeof(image) * 8))
296 return (nread < 0) ? status_t(-nread) : STATUS_CORRUPTED;
297
298 value = bin_to_float(LE_TO_CPU(image));
299 }
300
301 // Push item to buffer and return result
302 vFloatBuf[nFloatHead] = value;
303 nFloatHead = (nFloatHead + 1) % nFloatCap;
304 if (nFloatSize < (nFloatCap - 2))
305 ++nFloatSize;
306
307 // Return value
308 *dst = value;
309
310 return STATUS_OK;
311 }
312
313 status_t Decompressor::read_varint(size_t *dst)
314 {
315 size_t value = 0;
316 size_t shift = 0;
317 uint8_t b;
318
319 do
320 {
321 ssize_t nread = sStream.readv(&b, 7);
322 if (nread != 7)
323 return (nread < 0) ? status_t(-nread) : STATUS_CORRUPTED;
324
325 value |= size_t(b & 0x3f) << shift;
326 shift += 6;
327 } while (b & 0x40);
328
329 *dst = value;
330
331 return STATUS_OK;
332 }
333
334 status_t Decompressor::read_varint_icount(size_t *dst)
335 {
336 size_t bits = 3;
337 size_t max = 1 << bits;
338 size_t value = 0;
339 size_t b = 0;
340 size_t shift = 0;
341
342 while (true)
343 {
344 ssize_t nread = sStream.readv(fixed_int(&b), bits + 1);
345 if (nread != ssize_t(bits + 1))
346 return (nread < 0) ? status_t(-nread) : STATUS_CORRUPTED;
347
348 // Decode value
349 value |= (b & (max - 1)) << shift;
350 if (!(b & max))
351 break;
352
353 // Update bit count
354 shift += bits;
355 bits += 2;
356 max <<= 2;
357 }
358
359 *dst = value;
360
361 return STATUS_OK;
362 }
363
364 status_t Decompressor::read_indices(index_t *dst, size_t count, bool read)
365 {
366 if (!read)
367 {
368 for (size_t i=0; i<count; ++i)
369 dst[i] = -1;
370 return STATUS_OK;
371 }
372
373 size_t value = 0;
374 status_t res = read_varint_icount(&value);
375 if (res != STATUS_OK)
376 return res;
377 dst[0] = zigzag_decode(int32_t(value));
378
379 for (size_t i=1; i<count; ++i)
380 {
381 status_t res = read_varint_icount(&value);
382 if (res != STATUS_OK)
383 return res;
384 dst[i] = dst[0] + zigzag_decode(int32_t(value));
385 }
386
387 return STATUS_OK;
388 }
389
390 status_t Decompressor::read_utf8(LSPString *dst)
391 {
392 // Read length of the string
393 size_t length = 0;
394 status_t res = read_varint(&length);
395 if (res != STATUS_OK)
396 return res;
397
398 // Allocate contents for the string data
399 char *buf = static_cast<char *>(malloc(length * sizeof(char)));
400 if (buf == NULL)
401 return STATUS_NO_MEM;
402 lsp_finally { free(buf); };
403
404 // Read UTF-8 sequence
405 ssize_t nread = sStream.read_fully(buf, length);
406 if (nread != ssize_t(length))
407 return (nread < 0) ? status_t(-nread) : STATUS_CORRUPTED;
408
409 // Fill string with UTF-8 sequence
410 if (!dst->set_utf8(buf, length))
411 return STATUS_NO_MEM;
412
413 return STATUS_OK;
414 }
415
416 status_t Decompressor::parse_vertex(IObjHandler *handler, size_t coords)
417 {
418 float x = 0.0f, y = 0.0f, z = 0.0f, w = 1.0f;
419
420 status_t res = read_float(&x);
421 if (res == STATUS_OK)
422 res = read_float(&y);
423 if ((res == STATUS_OK) && (coords > 2))
424 res = read_float(&z);
425 if ((res == STATUS_OK) && (coords > 3))
426 res = read_float(&w);
427 if (res == STATUS_OK)
428 res = status_t(handler->add_vertex(x, y, z, w));
429
430 return res;
431 }
432
433 status_t Decompressor::parse_pvertex(IObjHandler *handler, size_t coords)
434 {
435 float x = 0.0f, y = 0.0f, z = 0.0f, w = 1.0f;
436
437 status_t res = read_float(&x);
438 if (res == STATUS_OK)
439 res = read_float(&y);
440 if ((res == STATUS_OK) && (coords > 2))
441 res = read_float(&z);
442 if ((res == STATUS_OK) && (coords > 3))
443 res = read_float(&w);
444 if (res == STATUS_OK)
445 res = status_t(handler->add_param_vertex(x, y, z, w));
446
447 return res;
448 }
449
450 status_t Decompressor::parse_normal(IObjHandler *handler, size_t coords)
451 {
452 float nx = 0.0f, ny = 0.0f, nz = 0.0f, nw = 0.0f;
453
454 status_t res = read_float(&nx);
455 if (res == STATUS_OK)
456 res = read_float(&ny);
457 if ((res == STATUS_OK) && (coords > 2))
458 res = read_float(&nz);
459 if ((res == STATUS_OK) && (coords > 3))
460 res = read_float(&nw);
461 if (res == STATUS_OK)
462 res = status_t(handler->add_normal(nx, ny, nz, nw));
463
464 return res;
465 }
466
467 status_t Decompressor::parse_texcoord(IObjHandler *handler, size_t coords)
468 {
469 float u = 0.0f, v = 0.0f, w = 0.0f;
470
471 status_t res = read_float(&u);
472 if ((res == STATUS_OK) && (coords > 1))
473 res = read_float(&v);
474 if ((res == STATUS_OK) && (coords > 2))
475 res = read_float(&w);
476 if (res == STATUS_OK)
477 res = status_t(handler->add_texture_vertex(u, v, w));
478 return res;
479 }
480
481 status_t Decompressor::parse_face(IObjHandler *handler, bool texcoords, bool normals, bool fill)
482 {
483 size_t count = 0;
484 status_t res = read_varint_icount(&count);
485 if (res != STATUS_OK)
486 return res;
487
488 lltl::darray<index_t> data;
489 if (!data.reserve(count * 3))
490 return STATUS_NO_MEM;
491
492 index_t *vv = data.array();
493 index_t *vt = &vv[count];
494 index_t *vn = &vt[count];
495
496 if ((res = read_indices(vv, count, true)) != STATUS_OK)
497 return res;
498 if ((res = read_indices(vt, count, texcoords)) != STATUS_OK)
499 return res;
500
501 if (fill)
502 {
503 size_t index = 0;
504 if ((res = read_varint_icount(&index)) != STATUS_OK)
505 return res;
506 for (size_t i=0; i<count; ++i)
507 vn[i] = index;
508 }
509 else
510 {
511 if ((res = read_indices(vn, count, normals)) != STATUS_OK)
512 return res;
513 }
514
515
516 return status_t(handler->add_face(vv, vn, vt, count));
517 }
518
519 status_t Decompressor::parse_line(IObjHandler *handler, bool texcoords)
520 {
521 size_t count = 0;
522 status_t res = read_varint_icount(&count);
523 if (res != STATUS_OK)
524 return res;
525
526 lltl::darray<index_t> data;
527 if (!data.reserve(count * 2))
528 return STATUS_NO_MEM;
529
530 index_t *vv = data.array();
531 index_t *vt = &vv[count];
532 if ((res = read_indices(vv, count, true)) != STATUS_OK)
533 return res;
534 if ((res = read_indices(vt, count, texcoords)) != STATUS_OK)
535 return res;
536
537 return status_t(handler->add_line(vv, vt, count));
538 }
539
540 status_t Decompressor::parse_points(IObjHandler *handler)
541 {
542 size_t count = 0;
543 status_t res = read_varint_icount(&count);
544 if (res != STATUS_OK)
545 return res;
546
547 lltl::darray<index_t> data;
548 if (!data.reserve(count))
549 return STATUS_NO_MEM;
550
551 index_t *vv = data.array();
552 if ((res = read_indices(vv, count, true)) != STATUS_OK)
553 return res;
554
555 return status_t(handler->add_points(vv, count));
556 }
557
558 status_t Decompressor::parse_object(IObjHandler *handler)
559 {
560 LSPString name;
561 status_t res = read_utf8(&name);
562 if (res != STATUS_OK)
563 return res;
564
565 return handler->begin_object(&name);
566 }
567
568 status_t Decompressor::parse_data(IObjHandler *handler)
569 {
570 status_t res = parse_header();
571 if (res != STATUS_OK)
572 return res;
573
574 size_t nobj = 0;
575 compressed_event_type_t event;
576 while ((res = read_event(&event)) == STATUS_OK)
577 {
578 if (event == CEV_EOF)
579 break;
580
581 switch (event)
582 {
583 case CEV_VERTEX2:
584 res = parse_vertex(handler, 2);
585 break;
586 case CEV_VERTEX3:
587 res = parse_vertex(handler, 3);
588 break;
589 case CEV_VERTEX4:
590 res = parse_vertex(handler, 4);
591 break;
592
593 case CEV_PVERTEX2:
594 res = parse_pvertex(handler, 2);
595 break;
596 case CEV_PVERTEX3:
597 res = parse_pvertex(handler, 3);
598 break;
599 case CEV_PVERTEX4:
600 res = parse_pvertex(handler, 4);
601 break;
602
603 case CEV_TEXCOORD1:
604 res = parse_texcoord(handler, 1);
605 break;
606 case CEV_TEXCOORD2:
607 res = parse_texcoord(handler, 2);
608 break;
609 case CEV_TEXCOORD3:
610 res = parse_texcoord(handler, 3);
611 break;
612
613 case CEV_NORMAL2:
614 res = parse_normal(handler, 2);
615 break;
616 case CEV_NORMAL3:
617 res = parse_normal(handler, 3);
618 break;
619 case CEV_NORMAL4:
620 res = parse_normal(handler, 4);
621 break;
622
623 case CEV_FACE:
624 res = parse_face(handler, false, false, false);
625 break;
626 case CEV_FACE_T:
627 res = parse_face(handler, true, false, false);
628 break;
629 case CEV_FACE_N:
630 res = parse_face(handler, false, true, false);
631 break;
632 case CEV_FACE_NF:
633 res = parse_face(handler, false, true, true);
634 break;
635 case CEV_FACE_TN:
636 res = parse_face(handler, true, true, false);
637 break;
638 case CEV_FACE_TNF:
639 res = parse_face(handler, true, true, true);
640 break;
641
642 case CEV_LINE:
643 res = parse_line(handler, false);
644 break;
645 case CEV_LINE_T:
646 res = parse_line(handler, true);
647 break;
648
649 case CEV_POINT:
650 res = parse_points(handler);
651 break;
652
653 case CEV_OBJECT:
654 if (nobj++)
655 {
656 if ((res = handler->end_object()) != STATUS_OK)
657 return res;
658 }
659 res = parse_object(handler);
660 break;
661
662 default:
663 return STATUS_CORRUPTED;
664 }
665 }
666
667 // Post-process data
668 if (res == STATUS_OK)
669 {
670 if (nobj > 0)
671 {
672 if ((res = handler->end_object()) != STATUS_OK)
673 return res;
674 }
675
676 res = handler->end_of_data();
677 }
678
679 return res;
680 }
681
682 } /* namespace obj */
683 } /* namespace lsp */
684
685