9 #if defined(GRAY_CODES)
11 uint16_t binary_to_gray (uint16_t x
) { return x
^ (x
>> 1); }
14 uint16_t gray_to_binary (uint16_t x
)
15 { int i
; for (i
=1; i
<16; i
+=i
) x
^= x
>> i
; return x
; }
20 void encode_coeff (ENTROPY_CODER significand_bitstream
[],
21 ENTROPY_CODER insignificand_bitstream
[],
24 int sign
= (coeff
>> (8*sizeof(TYPE
)-1)) & 1;
25 #if defined(GRAY_CODES)
26 TYPE significance
= binary_to_gray(coeff
);
28 static TYPE mask
[2] = { 0, ~0 };
29 TYPE significance
= coeff
^ mask
[sign
];
35 OUTPUT_BIT(&significand_bitstream
[i
], (significance
>> i
) & 1);
36 } while (!((significance
>> i
) & 1) && i
> 0);
38 OUTPUT_BIT(&significand_bitstream
[i
], sign
);
41 OUTPUT_BIT(&insignificand_bitstream
[i
], (significance
>> i
) & 1);
47 TYPE
decode_coeff (ENTROPY_CODER significand_bitstream
[],
48 ENTROPY_CODER insignificand_bitstream
[])
50 #if !defined(GRAY_CODES)
51 static TYPE mask
[2] = { 0, ~0 };
53 TYPE significance
= 0;
59 significance
|= INPUT_BIT(&significand_bitstream
[i
]) << i
;
60 // if (ENTROPY_CODER_EOS(&significand_bitstream[i]))
62 } while (!significance
&& i
> 0);
64 sign
= INPUT_BIT(&significand_bitstream
[i
]);
65 // if (ENTROPY_CODER_EOS(&significand_bitstream[i]))
69 significance
|= INPUT_BIT(&insignificand_bitstream
[i
]) << i
;
71 #if defined(GRAY_CODES)
72 significance
|= sign
<< (8*sizeof(TYPE
)-1);
73 return gray_to_binary(significance
);
75 return (significance
^ mask
[sign
]);
81 uint32_t skip_0coeffs (Wavelet3DBuf
* buf
,
82 ENTROPY_CODER s_stream
[],
83 ENTROPY_CODER i_stream
[],
87 uint32_t skip
= limit
;
89 for (i
=0; i
<TYPE_BITS
; i
++) {
90 if (ENTROPY_CODER_SYMBOL(&s_stream
[i
]) != 0) {
93 uint32_t runlength
= ENTROPY_CODER_RUNLENGTH(&s_stream
[i
]);
95 runlength
/= 2; /* sign bits are in this bitplane ... */
103 ENTROPY_CODER_SKIP(&s_stream
[0], 2*skip
); /* kill sign+significance bits */
105 for (i
=1; i
<TYPE_BITS
; i
++)
106 ENTROPY_CODER_SKIP(&s_stream
[i
], skip
);
115 void encode_quadrant (const Wavelet3DBuf
* buf
,
116 int level
, int quadrant
, uint32_t w
, uint32_t h
, uint32_t f
,
117 ENTROPY_CODER significand_bitstream
[],
118 ENTROPY_CODER insignificand_bitstream
[])
122 for (z
=0; z
<f
; z
++) {
123 for (y
=0; y
<h
; y
++) {
124 for (x
=0; x
<w
; x
++) {
125 unsigned int index
= buf
->offset
[level
] [quadrant
]
126 + z
* buf
->width
* buf
->height
127 + y
* buf
->width
+ x
;
129 encode_coeff (significand_bitstream
, insignificand_bitstream
,
138 void encode_coefficients (const Wavelet3DBuf
* buf
,
139 ENTROPY_CODER s_stream
[],
140 ENTROPY_CODER i_stream
[])
144 encode_coeff (s_stream
, i_stream
, buf
->data
[0]);
146 for (level
=0; level
<buf
->scales
-1; level
++) {
147 uint32_t w
, h
, f
, w1
, h1
, f1
;
152 w1
= buf
->w
[level
+1] - w
;
153 h1
= buf
->h
[level
+1] - h
;
154 f1
= buf
->f
[level
+1] - f
;
156 if (w1
> 0) encode_quadrant(buf
,level
,1,w1
,h
,f
,s_stream
,i_stream
);
157 if (h1
> 0) encode_quadrant(buf
,level
,2,w
,h1
,f
,s_stream
,i_stream
);
158 if (f1
> 0) encode_quadrant(buf
,level
,3,w
,h
,f1
,s_stream
,i_stream
);
159 if (w1
> 0 && h1
> 0) encode_quadrant(buf
,level
,4,w1
,h1
,f
,s_stream
,i_stream
);
160 if (w1
> 0 && f1
> 0) encode_quadrant(buf
,level
,5,w1
,h
,f1
,s_stream
,i_stream
);
161 if (h1
> 0 && f1
> 0) encode_quadrant(buf
,level
,6,w
,h1
,f1
,s_stream
,i_stream
);
162 if (h1
> 0 && f1
> 0 && f1
> 0)
163 encode_quadrant (buf
,level
,7,w1
,h1
,f1
,s_stream
,i_stream
);
169 void decode_quadrant (Wavelet3DBuf
* buf
,
170 int level
, int quadrant
, uint32_t w
, uint32_t h
, uint32_t f
,
171 ENTROPY_CODER s_stream
[],
172 ENTROPY_CODER i_stream
[])
183 uint32_t index
= buf
->offset
[level
] [quadrant
]
184 + z
* buf
->width
* buf
->height
185 + y
* buf
->width
+ x
;
187 buf
->data
[index
] = decode_coeff (s_stream
, i_stream
);
189 skip
= skip_0coeffs (buf
, s_stream
, i_stream
,
190 (w
-x
-1)+(h
-y
-1)*w
+(f
-z
-1)*w
*h
);
212 void decode_coefficients (Wavelet3DBuf
* buf
,
213 ENTROPY_CODER s_stream
[],
214 ENTROPY_CODER i_stream
[])
218 buf
->data
[0] = decode_coeff (s_stream
, i_stream
);
220 for (level
=0; level
<buf
->scales
-1; level
++) {
221 uint32_t w
, h
, f
, w1
, h1
, f1
;
226 w1
= buf
->w
[level
+1] - w
;
227 h1
= buf
->h
[level
+1] - h
;
228 f1
= buf
->f
[level
+1] - f
;
230 if (w1
> 0) decode_quadrant(buf
,level
,1,w1
,h
,f
,s_stream
,i_stream
);
231 if (h1
> 0) decode_quadrant(buf
,level
,2,w
,h1
,f
,s_stream
,i_stream
);
232 if (f1
> 0) decode_quadrant(buf
,level
,3,w
,h
,f1
,s_stream
,i_stream
);
233 if (w1
> 0 && h1
> 0) decode_quadrant(buf
,level
,4,w1
,h1
,f
,s_stream
,i_stream
);
234 if (w1
> 0 && f1
> 0) decode_quadrant(buf
,level
,5,w1
,h
,f1
,s_stream
,i_stream
);
235 if (h1
> 0 && f1
> 0) decode_quadrant(buf
,level
,6,w
,h1
,f1
,s_stream
,i_stream
);
236 if (h1
> 0 && f1
> 0 && f1
> 0)
237 decode_quadrant (buf
,level
,7,w1
,h1
,f1
,s_stream
,i_stream
);
243 void encode_coefficients (const Wavelet3DBuf
* buf
,
244 ENTROPY_CODER s_stream
[],
245 ENTROPY_CODER i_stream
[])
249 for (i
=0; i
<buf
->width
*buf
->height
*buf
->frames
; i
++)
250 encode_coeff(s_stream
, i_stream
, buf
->data
[i
]);
257 void decode_coefficients (Wavelet3DBuf
* buf
,
258 ENTROPY_CODER s_stream
[],
259 ENTROPY_CODER i_stream
[])
263 for (i
=0; i
<buf
->width
*buf
->height
*buf
->frames
; i
++) {
266 buf
->data
[i
] = decode_coeff(s_stream
, i_stream
);
268 skip
= skip_0coeffs (buf
, s_stream
, i_stream
,
269 buf
->width
*buf
->height
*buf
->frames
- i
);
278 uint32_t setup_limittabs (ENTROPY_CODER significand_bitstream
[],
279 ENTROPY_CODER insignificand_bitstream
[],
280 uint32_t significand_limittab
[],
281 uint32_t insignificand_limittab
[],
284 uint32_t significand_limit
;
285 uint32_t insignificand_limit
;
289 assert (limit
> 2 * TYPE_BITS
* sizeof(uint32_t)); /* limit too small */
291 printf ("%s: limit == %u\n", __FUNCTION__
, limit
);
292 byte_count
= 2 * TYPE_BITS
* sizeof(uint32_t); /* 2 binary coded limittabs */
294 printf ("%s: rem. limit == %u\n", __FUNCTION__
, limit
);
296 significand_limit
= limit
* 7 / 8;
297 insignificand_limit
= limit
- significand_limit
;
299 printf ("%s: limit == %u\n", __FUNCTION__
, limit
);
300 printf ("significand limit == %u\n", significand_limit
);
301 printf ("insignificand limit == %u\n", insignificand_limit
);
303 for (i
=TYPE_BITS
-1; i
>=0; i
--) {
304 uint32_t s_bytes
, i_bytes
;
307 significand_limittab
[i
] = (significand_limit
+ 1) / 2;
308 insignificand_limittab
[i
] = (insignificand_limit
+ 1) / 2;
310 significand_limittab
[0] = significand_limit
;
311 insignificand_limittab
[0] = insignificand_limit
;
314 s_bytes
= ENTROPY_ENCODER_FLUSH(&significand_bitstream
[i
]);
315 i_bytes
= ENTROPY_ENCODER_FLUSH(&insignificand_bitstream
[i
]);
317 if (s_bytes
< significand_limittab
[i
])
318 significand_limittab
[i
] = s_bytes
;
320 if (i_bytes
< insignificand_limittab
[i
])
321 insignificand_limittab
[i
] = i_bytes
;
323 byte_count
+= significand_limittab
[i
];
324 byte_count
+= insignificand_limittab
[i
];
326 printf ("insignificand_limittab[%i] == %u / %u\n",
327 i
, insignificand_limittab
[i
], i_bytes
);
328 printf (" significand_limittab[%i] == %u / %u\n",
329 i
, significand_limittab
[i
], s_bytes
);
331 significand_limit
-= significand_limittab
[i
];
332 insignificand_limit
-= insignificand_limittab
[i
];
335 printf ("byte_count == %u\n", byte_count
);
342 * write 'em binary for now, should be easy to compress ...
345 uint8_t* write_limittabs (uint8_t *bitstream
,
346 uint32_t significand_limittab
[],
347 uint32_t insignificand_limittab
[])
351 for (i
=0; i
<TYPE_BITS
; i
++) {
352 *(uint32_t*) bitstream
= significand_limittab
[i
];
356 for (i
=0; i
<TYPE_BITS
; i
++) {
357 *(uint32_t*) bitstream
= insignificand_limittab
[i
];
366 uint8_t* read_limittabs (uint8_t *bitstream
,
367 uint32_t significand_limittab
[],
368 uint32_t insignificand_limittab
[])
372 for (i
=0; i
<TYPE_BITS
; i
++) {
373 significand_limittab
[i
] = *(uint32_t*) bitstream
;
374 printf ("significand_limittab[%i] == %u\n", i
, significand_limittab
[i
]);
378 for (i
=0; i
<TYPE_BITS
; i
++) {
379 insignificand_limittab
[i
] = *(uint32_t*) bitstream
;
380 printf ("insignificand_limittab[%i] == %u\n", i
, insignificand_limittab
[i
]);
389 * concatenate entropy coder bitstreams
392 void merge_bitstreams (uint8_t *bitstream
,
393 ENTROPY_CODER significand_bitstream
[],
394 ENTROPY_CODER insignificand_bitstream
[],
395 uint32_t significand_limittab
[],
396 uint32_t insignificand_limittab
[])
400 for (i
=TYPE_BITS
-1; i
>=0; i
--) {
402 ENTROPY_CODER_BITSTREAM(&significand_bitstream
[i
]),
403 significand_limittab
[i
]);
405 bitstream
+= significand_limittab
[i
];
408 for (i
=TYPE_BITS
-1; i
>=0; i
--) {
410 ENTROPY_CODER_BITSTREAM(&insignificand_bitstream
[i
]),
411 insignificand_limittab
[i
]);
413 bitstream
+= insignificand_limittab
[i
];
419 void split_bitstreams (uint8_t *bitstream
,
420 ENTROPY_CODER significand_bitstream
[],
421 ENTROPY_CODER insignificand_bitstream
[],
422 uint32_t significand_limittab
[],
423 uint32_t insignificand_limittab
[])
428 for (i
=TYPE_BITS
-1; i
>=0; i
--) {
429 byte_count
= significand_limittab
[i
];
430 ENTROPY_DECODER_INIT(&significand_bitstream
[i
], bitstream
, byte_count
);
431 bitstream
+= byte_count
;
434 for (i
=TYPE_BITS
-1; i
>=0; i
--) {
435 byte_count
= insignificand_limittab
[i
];
436 ENTROPY_DECODER_INIT(&insignificand_bitstream
[i
], bitstream
, byte_count
);
437 bitstream
+= byte_count
;
442 int wavelet_3d_buf_encode_coeff (const Wavelet3DBuf
* buf
,
446 ENTROPY_CODER significand_bitstream
[TYPE_BITS
];
447 ENTROPY_CODER insignificand_bitstream
[TYPE_BITS
];
448 uint32_t significand_limittab
[TYPE_BITS
];
449 uint32_t insignificand_limittab
[TYPE_BITS
];
453 for (i
=0; i
<TYPE_BITS
; i
++)
454 ENTROPY_ENCODER_INIT(&significand_bitstream
[i
], limit
);
455 for (i
=0; i
<TYPE_BITS
; i
++)
456 ENTROPY_ENCODER_INIT(&insignificand_bitstream
[i
], limit
);
458 encode_coefficients (buf
, significand_bitstream
, insignificand_bitstream
);
460 byte_count
= setup_limittabs (significand_bitstream
, insignificand_bitstream
,
461 significand_limittab
, insignificand_limittab
,
464 bitstream
= write_limittabs (bitstream
,
465 significand_limittab
, insignificand_limittab
);
467 merge_bitstreams (bitstream
, significand_bitstream
, insignificand_bitstream
,
468 significand_limittab
, insignificand_limittab
);
470 for (i
=0; i
<TYPE_BITS
; i
++) {
471 ENTROPY_ENCODER_DONE(&significand_bitstream
[i
]);
472 ENTROPY_ENCODER_DONE(&insignificand_bitstream
[i
]);
479 void wavelet_3d_buf_decode_coeff (Wavelet3DBuf
* buf
,
483 ENTROPY_CODER significand_bitstream
[TYPE_BITS
];
484 ENTROPY_CODER insignificand_bitstream
[TYPE_BITS
];
485 uint32_t significand_limittab
[TYPE_BITS
];
486 uint32_t insignificand_limittab
[TYPE_BITS
];
489 memset (buf
->data
, 0,
490 buf
->width
* buf
->height
* buf
->frames
* sizeof(TYPE
));
492 bitstream
= read_limittabs (bitstream
,
493 significand_limittab
, insignificand_limittab
);
495 split_bitstreams (bitstream
, significand_bitstream
, insignificand_bitstream
,
496 significand_limittab
, insignificand_limittab
);
498 decode_coefficients (buf
, significand_bitstream
, insignificand_bitstream
);
500 for (i
=0; i
<TYPE_BITS
; i
++) {
501 ENTROPY_DECODER_DONE(&significand_bitstream
[i
]);
502 ENTROPY_DECODER_DONE(&insignificand_bitstream
[i
]);