summaryrefslogtreecommitdiff
path: root/songdbj/de/jarnbjo/vorbis/Residue.java
blob: 78c28fa5eded04ab0968de25dc5ad261285e6cac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
 * $ProjectName$
 * $ProjectRevision$
 * -----------------------------------------------------------
 * $Id$
 * -----------------------------------------------------------
 *
 * $Author$
 *
 * Description:
 *
 * Copyright 2002-2003 Tor-Einar Jarnbjo
 * -----------------------------------------------------------
 *
 * Change History
 * -----------------------------------------------------------
 * $Log$
 * Revision 1.1  2005/07/11 15:42:36  hcl
 * Songdb java version, source. only 1.5 compatible
 *
 * Revision 1.1.1.1  2004/04/04 22:09:12  shred
 * First Import
 *
 * Revision 1.3  2003/04/04 08:33:02  jarnbjo
 * no message
 *
 * Revision 1.2  2003/03/16 01:11:12  jarnbjo
 * no message
 *
 *
 */

package de.jarnbjo.vorbis;

import java.io.IOException;
import java.util.HashMap;

import de.jarnbjo.util.io.*;


abstract class Residue {

   protected int begin, end;
   protected int partitionSize; // grouping
   protected int classifications; // partitions
   protected int classBook; // groupbook
   protected int[] cascade; // secondstages
   protected int[][] books;
   protected HashMap looks=new HashMap();

   protected Residue() {
   }

   protected Residue(BitInputStream source, SetupHeader header) throws VorbisFormatException, IOException {
      begin=source.getInt(24);
      end=source.getInt(24);
      partitionSize=source.getInt(24)+1;
      classifications=source.getInt(6)+1;
      classBook=source.getInt(8);

      cascade=new int[classifications];

      int acc=0;

      for(int i=0; i<classifications; i++) {
         int highBits=0, lowBits=0;
         lowBits=source.getInt(3);
         if(source.getBit()) {
            highBits=source.getInt(5);
         }
         cascade[i]=(highBits<<3)|lowBits;
         acc+=Util.icount(cascade[i]);
      }

      books=new int[classifications][8];

      for(int i=0; i<classifications; i++) {
         for(int j=0; j<8; j++) {
            if((cascade[i]&(1<<j))!=0) {
               books[i][j]=source.getInt(8);
               if(books[i][j]>header.getCodeBooks().length) {
                  throw new VorbisFormatException("Reference to invalid codebook entry in residue header.");
               }
            }
         }
      }
   }


   protected static Residue createInstance(BitInputStream source, SetupHeader header) throws VorbisFormatException, IOException {

      int type=source.getInt(16);
      switch(type) {
      case 0:
         //System.out.println("residue type 0");
         return new Residue0(source, header);
      case 1:
         //System.out.println("residue type 1");
         return new Residue2(source, header);
      case 2:
         //System.out.println("residue type 2");
         return new Residue2(source, header);
      default:
         throw new VorbisFormatException("Residue type "+type+" is not supported.");
      }
   }

   protected abstract int getType();
   protected abstract void decodeResidue(VorbisStream vorbis, BitInputStream source, Mode mode, int ch, boolean[] doNotDecodeFlags, float[][] vectors) throws VorbisFormatException, IOException;
   //public abstract double[][] getDecodedVectors();

   protected int getBegin() {
      return begin;
   }

   protected int getEnd() {
      return end;
   }

   protected int getPartitionSize() {
      return partitionSize;
   }

   protected int getClassifications() {
      return classifications;
   }

   protected int getClassBook() {
      return classBook;
   }

   protected int[] getCascade() {
      return cascade;
   }

   protected int[][] getBooks() {
      return books;
   }

   protected final void fill(Residue clone) {
      clone.begin=begin;
      clone.books=books;
      clone.cascade=cascade;
      clone.classBook=classBook;
      clone.classifications=classifications;
      clone.end=end;
      clone.partitionSize=partitionSize;
   }

   protected Look getLook(VorbisStream source, Mode key) {
      //return new Look(source, key);
      Look look=(Look)looks.get(key);
      if(look==null) {
         look=new Look(source, key);
         looks.put(key, look);
      }
      return look;
   }


   class Look  {
      int map;
      int parts;
      int stages;
      CodeBook[] fullbooks;
      CodeBook phrasebook;
      int[][] partbooks;
      int partvals;
      int[][] decodemap;
      int postbits;
      int phrasebits;
      int frames;

      protected Look (VorbisStream source, Mode mode) {
         int dim=0, acc=0, maxstage=0;

         map=mode.getMapping();
         parts=Residue.this.getClassifications();
         fullbooks=source.getSetupHeader().getCodeBooks();
         phrasebook=fullbooks[Residue.this.getClassBook()];
         dim=phrasebook.getDimensions();

         partbooks=new int[parts][];

         for(int j=0;j<parts;j++) {
            int stages=Util.ilog(Residue.this.getCascade()[j]);
            if(stages!=0) {
               if(stages>maxstage) {
                  maxstage=stages;
               }
               partbooks[j]=new int[stages];
               for(int k=0; k<stages; k++){
                  if((Residue.this.getCascade()[j]&(1<<k))!=0){
                     partbooks[j][k]=Residue.this.getBooks()[j][k];
                  }
	            }
            }
         }

         partvals=(int)Math.rint(Math.pow(parts, dim));
         stages=maxstage;

         decodemap=new int[partvals][];

         for(int j=0;j<partvals;j++){
            int val=j;
            int mult=partvals/parts;
            decodemap[j]=new int[dim];

            for(int k=0;k<dim;k++){
               int deco=val/mult;
               val-=deco*mult;
               mult/=parts;
               decodemap[j][k]=deco;
            }
         }
      }

      protected int[][] getDecodeMap() {
         return decodemap;
      }

      protected int getFrames() {
         return frames;
      }

      protected int getMap() {
         return map;
      }

      protected int[][] getPartBooks() {
         return partbooks;
      }

      protected int getParts() {
         return parts;
      }

      protected int getPartVals() {
         return partvals;
      }

      protected int getPhraseBits() {
         return phrasebits;
      }

      protected CodeBook getPhraseBook() {
         return phrasebook;
      }

      protected int getPostBits() {
         return postbits;
      }

      protected int getStages() {
         return stages;
      }
   }

}