1 #ifndef NETWORK_COMPRESSION_H
2 #define NETWORK_COMPRESSION_H
4 #include "vg/vg_stdint.h"
7 typedef struct bitpack_ctx bitpack_ctx
;
16 u32 bytes
, buffer_len
;
19 static void bitpack_bytes( bitpack_ctx
*ctx
, u32 bytes
, void *data
){
21 for( u32 i
=0; i
<bytes
; i
++ ){
22 u32 index
= ctx
->bytes
+i
;
23 if( ctx
->mode
== k_bitpack_compress
){
24 if( index
< ctx
->buffer_len
)
25 ctx
->buffer
[index
] = ext
[i
];
28 if( index
< ctx
->buffer_len
)
29 ext
[i
] = ctx
->buffer
[index
];
37 static void bitpack_qf32( bitpack_ctx
*ctx
, u32 bits
,
38 f32 min
, f32 max
, f32
*v
){
39 u32 mask
= (0x1 << bits
) - 1;
41 if( ctx
->mode
== k_bitpack_compress
){
42 u32 a
= vg_quantf( *v
, bits
, min
, max
);
43 bitpack_bytes( ctx
, bits
/8, &a
);
47 bitpack_bytes( ctx
, bits
/8, &a
);
48 *v
= vg_dequantf( a
, bits
, min
, max
);
52 static void bitpack_qv2f( bitpack_ctx
*ctx
, u32 bits
,
53 f32 min
, f32 max
, v2f v
){
54 for( u32 i
=0; i
<2; i
++ )
55 bitpack_qf32( ctx
, bits
, min
, max
, v
+i
);
58 static void bitpack_qv3f( bitpack_ctx
*ctx
, u32 bits
,
59 f32 min
, f32 max
, v3f v
){
60 for( u32 i
=0; i
<3; i
++ )
61 bitpack_qf32( ctx
, bits
, min
, max
, v
+i
);
64 static void bitpack_qv4f( bitpack_ctx
*ctx
, u32 bits
,
65 f32 min
, f32 max
, v4f v
){
66 for( u32 i
=0; i
<4; i
++ )
67 bitpack_qf32( ctx
, bits
, min
, max
, v
+i
);
70 static void bitpack_qquat( bitpack_ctx
*ctx
, v4f quat
){
71 const f32 k_domain
= 0.70710678118f
;
73 if( ctx
->mode
== k_bitpack_compress
){
75 for( u32 i
=0; i
<4; i
++ )
76 qabs
[i
] = fabsf(quat
[i
]);
78 u32 lxy
= qabs
[1]>qabs
[0],
79 lzw
= (qabs
[3]>qabs
[2])+2,
80 l
= qabs
[lzw
]>qabs
[lxy
]? lzw
: lxy
;
82 f32 sign
= vg_signf(quat
[l
]);
85 for( u32 i
=0, j
=0; i
<4; i
++ )
87 smallest
[j
++] = vg_quantf( quat
[i
]*sign
, 10, -k_domain
, k_domain
);
89 u32 comp
= (smallest
[0]<<2) | (smallest
[1]<<12) | (smallest
[2]<<22) | l
;
90 bitpack_bytes( ctx
, 4, &comp
);
94 bitpack_bytes( ctx
, 4, &comp
);
96 u32 smallest
[3] = {(comp
>>2 )&0x3ff,
103 for( u32 i
=0, j
=0; i
<4; i
++ ){
105 quat
[i
] = vg_dequantf( smallest
[j
++], 10, -k_domain
, k_domain
);
106 m
-= quat
[i
]*quat
[i
];
115 #endif /* NETWORK_COMPRESSION_H */