Debian Codegraph @

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