/* * int init_encode(StaticCodeBook s){ * //memset(c,0,sizeof(codebook)); * c=s; * entries=s.entries; * dim=s.dim; * codelist=make_words(s.lengthlist, s.entries); * valuelist=s.unquantize(); * return(0); * } */ internal int init_decode(StaticCodeBook s) { //memset(c,0,sizeof(codebook)); c = s; entries = s.entries; dim = s.dim; valuelist = s.unquantize(); decode_tree = make_decode_tree(); if (decode_tree == null) { //goto err_out; clear(); return(-1); } return(0); // err_out: // vorbis_book_clear(c); // return(-1); }
// Decode side is specced and easier, because we don't need to find // matches using different criteria; we simply read and map. There are // two things we need to do 'depending': // // We may need to support interleave. We don't really, but it's // convenient to do it here rather than rebuild the vector later. // // Cascades may be additive or multiplicitive; this is not inherent in // the codebook, but set in the code using the codebook. Like // interleaving, it's easiest to do it here. // stage==0 -> declarative (set the value) // stage==1 -> additive // stage==2 -> multiplicitive // returns the entry number or -1 on eof internal int decode(csBuffer b) { int ptr = 0; DecodeAux t = decode_tree; int lok = b.look(t.tabn); //System.err.println(this+" "+t+" lok="+lok+", tabn="+t.tabn); if (lok >= 0) { ptr = t.tab[lok]; b.adv(t.tabl[lok]); if (ptr <= 0) { return(-ptr); } } do { switch (b.read1()) { case 0: ptr = t.ptr0[ptr]; break; case 1: ptr = t.ptr1[ptr]; break; case -1: default: return(-1); } }while(ptr > 0); return(-ptr); }
// build the decode helper tree from the codewords internal DecodeAux make_decode_tree() { int top = 0; DecodeAux t = new DecodeAux(); int[] ptr0 = t.ptr0 = new int[entries * 2]; int[] ptr1 = t.ptr1 = new int[entries * 2]; int[] codelist = make_words(c.lengthlist, c.entries); if (codelist == null) { return(null); } t.aux = entries * 2; for (int i = 0; i < entries; i++) { if (c.lengthlist[i] > 0) { int ptr = 0; int j; for (j = 0; j < c.lengthlist[i] - 1; j++) { int bit = (int)(((uint)codelist[i] >> j) & 1); if (bit == 0) { if (ptr0[ptr] == 0) { ptr0[ptr] = ++top; } ptr = ptr0[ptr]; } else { if (ptr1[ptr] == 0) { ptr1[ptr] = ++top; } ptr = ptr1[ptr]; } } if ((((uint)codelist[i] >> j) & 1) == 0) { ptr0[ptr] = -i; } else { ptr1[ptr] = -i; } } } //free(codelist); t.tabn = ilog(entries) - 4; if (t.tabn < 5) { t.tabn = 5; } int n = 1 << t.tabn; t.tab = new int[n]; t.tabl = new int[n]; for (int i = 0; i < n; i++) { int p = 0; int j = 0; for (j = 0; j < t.tabn && (p > 0 || j == 0); j++) { if ((i & (1 << j)) != 0) { p = ptr1[p]; } else { p = ptr0[p]; } } t.tab[i] = p; // -code t.tabl[i] = j; // length } return(t); }