File size: 9,652 Bytes
d46f4a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
/* JOrbis
 * Copyright (C) 2000 ymnk, JCraft,Inc.
 *  
 * Written by: 2000 ymnk<ymnk@jcraft.com>
 *   
 * Many thanks to 
 *   Monty <monty@xiph.org> and 
 *   The XIPHOPHORUS Company http://www.xiph.org/ .
 * JOrbis has been based on their awesome works, Vorbis codec.
 *   
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public License
 * as published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
   
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Library General Public License for more details.
 * 
 * You should have received a copy of the GNU Library General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

package com.jcraft.jorbis;

import com.jcraft.jogg.*;

class Mapping0 extends FuncMapping {
	static int seq = 0;

	void free_info(Object imap) {
	};

	void free_look(Object imap) {
	}

	Object look(DspState vd, InfoMode vm, Object m) {
		// System.err.println("Mapping0.look");
		Info vi = vd.vi;
		LookMapping0 look = new LookMapping0();
		InfoMapping0 info = look.map = (InfoMapping0) m;
		look.mode = vm;

		look.time_look = new Object[info.submaps];
		look.floor_look = new Object[info.submaps];
		look.residue_look = new Object[info.submaps];

		look.time_func = new FuncTime[info.submaps];
		look.floor_func = new FuncFloor[info.submaps];
		look.residue_func = new FuncResidue[info.submaps];

		for (int i = 0; i < info.submaps; i++) {
			int timenum = info.timesubmap[i];
			int floornum = info.floorsubmap[i];
			int resnum = info.residuesubmap[i];

			look.time_func[i] = FuncTime.time_P[vi.time_type[timenum]];
			look.time_look[i] = look.time_func[i].look(vd, vm, vi.time_param[timenum]);
			look.floor_func[i] = FuncFloor.floor_P[vi.floor_type[floornum]];
			look.floor_look[i] = look.floor_func[i].look(vd, vm, vi.floor_param[floornum]);
			look.residue_func[i] = FuncResidue.residue_P[vi.residue_type[resnum]];
			look.residue_look[i] = look.residue_func[i].look(vd, vm, vi.residue_param[resnum]);

		}

		if (vi.psys != 0 && vd.analysisp != 0) {
			// ??
		}

		look.ch = vi.channels;

		return (look);
	}

	void pack(Info vi, Object imap, Buffer opb) {
		InfoMapping0 info = (InfoMapping0) imap;

		/*
		 * another 'we meant to do it this way' hack... up to beta 4, we packed 4 binary
		 * zeros here to signify one submapping in use. We now redefine that to mean
		 * four bitflags that indicate use of deeper features; bit0:submappings,
		 * bit1:coupling, bit2,3:reserved. This is backward compatable with all actual
		 * uses of the beta code.
		 */

		if (info.submaps > 1) {
			opb.write(1, 1);
			opb.write(info.submaps - 1, 4);
		} else {
			opb.write(0, 1);
		}

		if (info.coupling_steps > 0) {
			opb.write(1, 1);
			opb.write(info.coupling_steps - 1, 8);
			for (int i = 0; i < info.coupling_steps; i++) {
				opb.write(info.coupling_mag[i], Util.ilog2(vi.channels));
				opb.write(info.coupling_ang[i], Util.ilog2(vi.channels));
			}
		} else {
			opb.write(0, 1);
		}

		opb.write(0, 2); /* 2,3:reserved */

		/* we don't write the channel submappings if we only have one... */
		if (info.submaps > 1) {
			for (int i = 0; i < vi.channels; i++)
				opb.write(info.chmuxlist[i], 4);
		}
		for (int i = 0; i < info.submaps; i++) {
			opb.write(info.timesubmap[i], 8);
			opb.write(info.floorsubmap[i], 8);
			opb.write(info.residuesubmap[i], 8);
		}
	}

	// also responsible for range checking
	Object unpack(Info vi, Buffer opb) {
		InfoMapping0 info = new InfoMapping0();

		if (opb.read(1) != 0) {
			info.submaps = opb.read(4) + 1;
		} else {
			info.submaps = 1;
		}

		if (opb.read(1) != 0) {
			info.coupling_steps = opb.read(8) + 1;

			for (int i = 0; i < info.coupling_steps; i++) {
				int testM = info.coupling_mag[i] = opb.read(Util.ilog2(vi.channels));
				int testA = info.coupling_ang[i] = opb.read(Util.ilog2(vi.channels));

				if (testM < 0 || testA < 0 || testM == testA || testM >= vi.channels || testA >= vi.channels) {
					// goto err_out;
					info.free();
					return (null);
				}
			}
		}

		if (opb.read(2) > 0) { /* 2,3:reserved */
			info.free();
			return (null);
		}

		if (info.submaps > 1) {
			for (int i = 0; i < vi.channels; i++) {
				info.chmuxlist[i] = opb.read(4);
				if (info.chmuxlist[i] >= info.submaps) {
					info.free();
					return (null);
				}
			}
		}

		for (int i = 0; i < info.submaps; i++) {
			info.timesubmap[i] = opb.read(8);
			if (info.timesubmap[i] >= vi.times) {
				info.free();
				return (null);
			}
			info.floorsubmap[i] = opb.read(8);
			if (info.floorsubmap[i] >= vi.floors) {
				info.free();
				return (null);
			}
			info.residuesubmap[i] = opb.read(8);
			if (info.residuesubmap[i] >= vi.residues) {
				info.free();
				return (null);
			}
		}
		return info;
	}

	float[][] pcmbundle = null;
	int[] zerobundle = null;
	int[] nonzero = null;
	Object[] floormemo = null;

	synchronized int inverse(Block vb, Object l) {
		DspState vd = vb.vd;
		Info vi = vd.vi;
		LookMapping0 look = (LookMapping0) l;
		InfoMapping0 info = look.map;
		InfoMode mode = look.mode;
		int n = vb.pcmend = vi.blocksizes[vb.W];

		float[] window = vd.window[vb.W][vb.lW][vb.nW][mode.windowtype];
		if (pcmbundle == null || pcmbundle.length < vi.channels) {
			pcmbundle = new float[vi.channels][];
			nonzero = new int[vi.channels];
			zerobundle = new int[vi.channels];
			floormemo = new Object[vi.channels];
		}

		// time domain information decode (note that applying the
		// information would have to happen later; we'll probably add a
		// function entry to the harness for that later
		// NOT IMPLEMENTED

		// recover the spectral envelope; store it in the PCM vector for now
		for (int i = 0; i < vi.channels; i++) {
			float[] pcm = vb.pcm[i];
			int submap = info.chmuxlist[i];

			floormemo[i] = look.floor_func[submap].inverse1(vb, look.floor_look[submap], floormemo[i]);
			if (floormemo[i] != null) {
				nonzero[i] = 1;
			} else {
				nonzero[i] = 0;
			}
			for (int j = 0; j < n / 2; j++) {
				pcm[j] = 0;
			}

		}

		for (int i = 0; i < info.coupling_steps; i++) {
			if (nonzero[info.coupling_mag[i]] != 0 || nonzero[info.coupling_ang[i]] != 0) {
				nonzero[info.coupling_mag[i]] = 1;
				nonzero[info.coupling_ang[i]] = 1;
			}
		}

		// recover the residue, apply directly to the spectral envelope

		for (int i = 0; i < info.submaps; i++) {
			int ch_in_bundle = 0;
			for (int j = 0; j < vi.channels; j++) {
				if (info.chmuxlist[j] == i) {
					if (nonzero[j] != 0) {
						zerobundle[ch_in_bundle] = 1;
					} else {
						zerobundle[ch_in_bundle] = 0;
					}
					pcmbundle[ch_in_bundle++] = vb.pcm[j];
				}
			}

			look.residue_func[i].inverse(vb, look.residue_look[i], pcmbundle, zerobundle, ch_in_bundle);
		}

		for (int i = info.coupling_steps - 1; i >= 0; i--) {
			float[] pcmM = vb.pcm[info.coupling_mag[i]];
			float[] pcmA = vb.pcm[info.coupling_ang[i]];

			for (int j = 0; j < n / 2; j++) {
				float mag = pcmM[j];
				float ang = pcmA[j];

				if (mag > 0) {
					if (ang > 0) {
						pcmM[j] = mag;
						pcmA[j] = mag - ang;
					} else {
						pcmA[j] = mag;
						pcmM[j] = mag + ang;
					}
				} else {
					if (ang > 0) {
						pcmM[j] = mag;
						pcmA[j] = mag + ang;
					} else {
						pcmA[j] = mag;
						pcmM[j] = mag - ang;
					}
				}
			}
		}

		// /* compute and apply spectral envelope */

		for (int i = 0; i < vi.channels; i++) {
			float[] pcm = vb.pcm[i];
			int submap = info.chmuxlist[i];
			look.floor_func[submap].inverse2(vb, look.floor_look[submap], floormemo[i], pcm);
		}

		// transform the PCM data; takes PCM vector, vb; modifies PCM vector
		// only MDCT right now....

		for (int i = 0; i < vi.channels; i++) {
			float[] pcm = vb.pcm[i];
			// _analysis_output("out",seq+i,pcm,n/2,0,0);
			((Mdct) vd.transform[vb.W][0]).backward(pcm, pcm);
		}

		// now apply the decoded pre-window time information
		// NOT IMPLEMENTED

		// window the data
		for (int i = 0; i < vi.channels; i++) {
			float[] pcm = vb.pcm[i];
			if (nonzero[i] != 0) {
				for (int j = 0; j < n; j++) {
					pcm[j] *= window[j];
				}
			} else {
				for (int j = 0; j < n; j++) {
					pcm[j] = 0.f;
				}
			}
		}

		// now apply the decoded post-window time information
		// NOT IMPLEMENTED
		// all done!
		return (0);
	}

	class InfoMapping0 {
		int submaps; // <= 16
		int[] chmuxlist = new int[256]; // up to 256 channels in a Vorbis stream

		int[] timesubmap = new int[16]; // [mux]
		int[] floorsubmap = new int[16]; // [mux] submap to floors
		int[] residuesubmap = new int[16];// [mux] submap to residue
		int[] psysubmap = new int[16]; // [mux]; encode only

		int coupling_steps;
		int[] coupling_mag = new int[256];
		int[] coupling_ang = new int[256];

		void free() {
			chmuxlist = null;
			timesubmap = null;
			floorsubmap = null;
			residuesubmap = null;
			psysubmap = null;

			coupling_mag = null;
			coupling_ang = null;
		}
	}

	class LookMapping0 {
		InfoMode mode;
		InfoMapping0 map;
		Object[] time_look;
		Object[] floor_look;
		Object[] floor_state;
		Object[] residue_look;
		PsyLook[] psy_look;

		FuncTime[] time_func;
		FuncFloor[] floor_func;
		FuncResidue[] residue_func;

		int ch;
		float[][] decay;
		int lastframe; // if a different mode is called, we need to
		// invalidate decay and floor state
	}

}