Updated project files to VS2008 SP1. Removed deprecated features (and warning message...
[xiph/unicode.git] / tarkin / bitpack.c
blob1bcd16b893d7dc93880b6dbb3a8b8e0f057999c0
1 // $Id: bitpack.c,v 1.1 2001/02/13 01:06:24 giles Exp $
2 //
3 // $Log: bitpack.c,v $
4 // Revision 1.1 2001/02/13 01:06:24 giles
5 // Initial revision
6 //
8 #include "tarkin.h"
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;
16 cidx = 0;
17 cidold = 0;
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];
34 vectorcount++;
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;
67 cidx = 0;
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);
86 // fflush(stdout);
87 vx += fromx; vy += fromy;
88 linaddr = vx+vy*td->x_workspace+zbase;
89 td->dwtv[cidx++].addr = linaddr;
90 // printf("%lu\n", linaddr);
91 // fflush(stdout);
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)
106 int a, d, n;
107 short int val;
108 ulong l;
109 float f;
111 if(!td->dwtv) td->dwtv = (dwt_vector *)malloc(td->vectorcount*sizeof(dwt_vector));
112 if(!td->dwtv) {
113 fprintf(stderr, "Out of memory in packblock()\nFatal Error, program halted.\n");
114 exit(-1);
116 bzero(td->dwtv, td->vectorcount*sizeof(dwt_vector));
118 _oggpack_writeinit(oggb);
119 _oggpack_write(oggb, td->vectorcount, sizeof(ulong)*8);
121 pack3d(td, oggb);
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);
127 f = td->dwtv[a].mag;
128 memcpy(&l, &f, 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)
137 int a;
138 short int val;
139 ulong l;
140 float f;
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));
146 if(!td->dwtv) {
147 fprintf(stderr, "Out of memory in unpackblock()\nFatal Error, program halted.\n");
148 exit(-1);
150 bzero(td->dwtv, td->vectorcount*sizeof(dwt_vector));
152 unpack3d(td, oggb);
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);
158 memcpy(&f, &l, 4);
159 td->vectors[td->dwtv[a].addr] = f;
160 // printf("%lu %d\n", td->dwtv[a].addr, val);