1 // $Id: bitpack.c,v 1.1 2001/02/13 01:06:24 giles Exp $
4 // Revision 1.1 2001/02/13 01:06:24 giles
10 void pack3d(tarkindata
*td
, oggpack_buffer
*o
)
12 int x
, y
, z
, wx
, wy
, fromx
, fromy
, tox
, toy
, cidx
, cidold
;
13 uint xbit
, ybit
, vx
, vy
;
14 ulong zbase
, linaddr
, vectorcount
;
19 for(z
=0;z
<td
->z_workspace
;z
++) {
20 zbase
= z
*td
->x_workspace
*td
->y_workspace
;
21 for(ybit
=0;ybit
<td
->y_bits
;ybit
++) {
22 for(xbit
=0;xbit
<td
->x_bits
;xbit
++) {
23 fromx
= (1 << xbit
) - (xbit
==0); fromy
= (1 << ybit
) - (ybit
==0);
24 tox
= 1 << (xbit
+1); toy
= 1 << (ybit
+1);
25 wx
= xbit
+ (xbit
==0); wy
= ybit
+ (ybit
==0);
27 // Scan for vectors in array between (fromx..tox, fromy..toy)
28 for(vectorcount
=0,y
=fromy
;y
<toy
;y
++) for(x
=fromx
;x
<tox
;x
++) {
29 linaddr
= zbase
+y
*td
->x_workspace
+x
;
30 if(td
->vectors
[linaddr
]!=0) {
31 // save global vector address in array
32 td
->dwtv
[cidx
].addr
= linaddr
;
33 td
->dwtv
[cidx
++].mag
= td
->vectors
[linaddr
];
38 // Pack count of vectors found into (wx+wy+1) bits
39 // printf("%ld(%d)\n", vectorcount, wx+wy+1);
40 _oggpack_write(o
, vectorcount
, wx
+wy
+1);
42 for(;cidold
<cidx
;cidold
++) {
43 // Convert vector address into vx, vy
44 linaddr
= td
->dwtv
[cidold
].addr
- zbase
;
45 vx
= linaddr
% td
->x_workspace
; vy
= linaddr
/ td
->x_workspace
;
47 // Make vx/vy refer from domain origin
48 vx
-= fromx
; vy
-= fromy
;
50 // pack vx and vy into 1<<wx and 1<<wy bits respectively
51 _oggpack_write(o
, vx
, wx
);
52 _oggpack_write(o
, vy
, wy
);
53 // printf("%lu %d(%d) %d(%d)\n", cidold, vx, wx, vy, wy);
54 // printf("%lu\n", td->dwtv[cidold].addr);
61 void unpack3d(tarkindata
*td
, oggpack_buffer
*o
)
63 int x
, y
, z
, wx
, wy
, fromx
, fromy
;
64 uint xbit
, ybit
, vx
, vy
;
65 ulong zbase
, linaddr
, cidx
, vectorcount
;
69 for(z
=0;z
<td
->z_workspace
;z
++) {
70 // printf("Frame %d\n", z);
71 zbase
= z
*td
->x_workspace
*td
->y_workspace
;
72 for(ybit
=0;ybit
<td
->y_bits
;ybit
++) {
73 for(xbit
=0;xbit
<td
->x_bits
;xbit
++) {
74 fromx
= (1 << xbit
) - (xbit
==0); fromy
= (1 << ybit
) - (ybit
==0);
75 wx
= xbit
+ (xbit
==0); wy
= ybit
+ (ybit
==0);
77 // Grab the vector count for this domain
78 vectorcount
= _oggpack_read(o
, wx
+wy
+1);
79 // printf("%ld(%d)\n", vectorcount, wx+wy+1);
81 // Grab the vectors (if any) and populate the dwtvec array with coords
82 for(;vectorcount
>0;vectorcount
--) {
83 vx
= _oggpack_read(o
, wx
);
84 vy
= _oggpack_read(o
, wy
);
85 // printf("%lu %d(%d) %d(%d)\n", cidx, vx, wx, vy, wy);
87 vx
+= fromx
; vy
+= fromy
;
88 linaddr
= vx
+vy
*td
->x_workspace
+zbase
;
89 td
->dwtv
[cidx
++].addr
= linaddr
;
90 // printf("%lu\n", linaddr);
98 // packblock() and unpackblock() assume a packing schema of successive layers of 2d frames
99 // An attempt should be made at full multidimensional packing at some point, to see if
100 // some additional crunching of vector coordinates can take place.
102 // packblock assumes an empty oggpack_buffer, which gets populated with vector bits
104 void packblock(tarkindata
*td
, oggpack_buffer
*oggb
)
111 if(!td
->dwtv
) td
->dwtv
= (dwt_vector
*)malloc(td
->vectorcount
*sizeof(dwt_vector
));
113 fprintf(stderr
, "Out of memory in packblock()\nFatal Error, program halted.\n");
116 bzero(td
->dwtv
, td
->vectorcount
*sizeof(dwt_vector
));
118 _oggpack_writeinit(oggb
);
119 _oggpack_write(oggb
, td
->vectorcount
, sizeof(ulong
)*8);
123 // Replace this bit with the codebook stuff
124 // printf("short int: %d\n", sizeof(short int));
125 for(a
=0;a
<td
->vectorcount
;a
++) {
126 val
= (short int)((td
->dwtv
[a
].mag
)/4);
129 // printf("%lu %d\n", td->dwtv[a].addr, val);
130 _oggpack_write(oggb
, l
, 32);
134 // unpackblock assumes the oggpack_buffer has already been init'd with data waiting
135 void unpackblock(tarkindata
*td
, oggpack_buffer
*oggb
)
142 td
->vectorcount
= _oggpack_read(oggb
, sizeof(ulong
)*8);
143 // printf("Vectorcount: %ld\n", td->vectorcount);
145 if(!td
->dwtv
) td
->dwtv
= (dwt_vector
*)malloc(td
->vectorcount
*sizeof(dwt_vector
));
147 fprintf(stderr
, "Out of memory in unpackblock()\nFatal Error, program halted.\n");
150 bzero(td
->dwtv
, td
->vectorcount
*sizeof(dwt_vector
));
153 td
->vectors
= (float *)calloc(td
->sz_workspace
, sizeof(float));
155 // Replace this bit with the codebook stuff
156 for(a
=0;a
<td
->vectorcount
;a
++) {
157 l
= _oggpack_read(oggb
, 32);
159 td
->vectors
[td
->dwtv
[a
].addr
] = f
;
160 // printf("%lu %d\n", td->dwtv[a].addr, val);