2 * Copyright (C) 2000 ymnk, JCraft,Inc.
4 * Written by: 2000 ymnk<ymnk@jcraft.com>
7 * Monty <monty@xiph.org> and
8 * The XIPHOPHORUS Company http://www.xiph.org/ .
9 * JOrbis has been based on their awesome works, Vorbis codec.
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Library General Public License
13 * as published by the Free Software Foundation; either version 2 of
14 * the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Library General Public License for more details.
21 * You should have received a copy of the GNU Library General Public
22 * License along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 package com.jcraft.jorbis;
28 import com.jcraft.jogg.*;
30 class Mapping0 extends FuncMapping{
32 void free_info(Object imap){};
33 void free_look(Object imap){
35 LookMapping0 l=(LookMapping0)imap;
36 InfoMapping0 info=l.map;
38 for(int i=0;i<l.map.submaps;i++){
39 l.time_func[i].free_look(l.time_look[i]);
40 l.floor_func[i].free_look(l.floor_look[i]);
41 l.residue_func[i].free_look(l.residue_look[i]);
42 if(l.psy_look!=null)l.psy_look[i].clear();
46 if(l.floor_state!=null){
47 for(int i=0;i<l.ch;i++)
48 l.floor_func[info.chmuxlist[i]].free_state(l.floor_state[i]);
49 //free(l.floor_state);
53 for(int i=0;i<l.ch;i++){
54 //if(l.decay[i])free(l->decay[i]);
61 //free(l->floor_func);
62 //free(l->residue_func);
64 //free(l->floor_look);
65 //free(l->residue_look);
66 //f(l->psy_look)free(l->psy_look);
73 //memset(l,0,sizeof(vorbis_look_mapping0));
78 Object look(DspState vd, InfoMode vm, Object m){
79 //System.err.println("Mapping0.look");
81 LookMapping0 look=new LookMapping0();
82 InfoMapping0 info=look.map=(InfoMapping0)m;
85 look.time_look=new Object[info.submaps];
86 look.floor_look=new Object[info.submaps];
87 look.residue_look=new Object[info.submaps];
91 look.floor_state=new Object[vi.channels];
94 look.psy_look=new PsyLook[info.submaps];
95 for(int i=0; i<info.submaps; i++){ look.psy_look[i]=new PsyLook(); }
99 look.time_func=new FuncTime[info.submaps];
100 look.floor_func=new FuncFloor[info.submaps];
101 look.residue_func=new FuncResidue[info.submaps];
103 for(int i=0;i<info.submaps;i++){
104 int timenum=info.timesubmap[i];
105 int floornum=info.floorsubmap[i];
106 int resnum=info.residuesubmap[i];
108 look.time_func[i]=FuncTime.time_P[vi.time_type[timenum]];
109 look.time_look[i]=look.time_func[i].look(vd,vm,vi.time_param[timenum]);
110 look.floor_func[i]=FuncFloor.floor_P[vi.floor_type[floornum]];
111 look.floor_look[i]=look.floor_func[i].
112 look(vd,vm,vi.floor_param[floornum]);
113 look.residue_func[i]=FuncResidue.residue_P[vi.residue_type[resnum]];
114 look.residue_look[i]=look.residue_func[i].
115 look(vd,vm,vi.residue_param[resnum]);
118 if(vi.psys!=0 && vd.analysisp!=0){
119 int psynum=info.psysubmap[i];
120 look.psy_look[i].init(vi.psy_param[psynum],
121 vi.blocksizes[vm.blockflag]/2,vi.rate);
126 if(vi.psys!=0 && vd.analysisp!=0){
128 if(info->psy[0] != info->psy[1]){
130 int psynum=info->psy[0];
131 look->psy_look[0]=_ogg_calloc(1,sizeof(vorbis_look_psy));
132 _vp_psy_init(look->psy_look[0],ci->psy_param[psynum],
134 ci->blocksizes[vm->blockflag]/2,vi->rate);
137 look->psy_look[1]=_ogg_calloc(1,sizeof(vorbis_look_psy));
138 _vp_psy_init(look->psy_look[1],ci->psy_param[psynum],
140 ci->blocksizes[vm->blockflag]/2,vi->rate);
143 int psynum=info->psy[0];
144 look->psy_look[0]=_ogg_calloc(1,sizeof(vorbis_look_psy));
145 look->psy_look[1]=look->psy_look[0];
146 _vp_psy_init(look->psy_look[0],ci->psy_param[psynum],
148 ci->blocksizes[vm->blockflag]/2,vi->rate);
155 // if(vd->analysisp)drft_init(&look->fft_look,ci->blocksizes[vm->blockflag]);
161 void pack(Info vi, Object imap, Buffer opb){
162 InfoMapping0 info=(InfoMapping0)imap;
164 /* another 'we meant to do it this way' hack... up to beta 4, we
165 packed 4 binary zeros here to signify one submapping in use. We
166 now redefine that to mean four bitflags that indicate use of
167 deeper features; bit0:submappings, bit1:coupling,
168 bit2,3:reserved. This is backward compatable with all actual uses
173 opb.write(info.submaps-1,4);
179 if(info.coupling_steps>0){
181 opb.write(info.coupling_steps-1,8);
182 for(int i=0;i<info.coupling_steps;i++){
183 opb.write(info.coupling_mag[i],ilog2(vi.channels));
184 opb.write(info.coupling_ang[i],ilog2(vi.channels));
191 opb.write(0,2); /* 2,3:reserved */
193 /* we don't write the channel submappings if we only have one... */
195 for(int i=0;i<vi.channels;i++)
196 opb.write(info.chmuxlist[i],4);
198 for(int i=0;i<info.submaps;i++){
199 opb.write(info.timesubmap[i],8);
200 opb.write(info.floorsubmap[i],8);
201 opb.write(info.residuesubmap[i],8);
205 // also responsible for range checking
206 Object unpack(Info vi, Buffer opb){
207 InfoMapping0 info=new InfoMapping0();
211 info.submaps=opb.read(4)+1;
218 info.coupling_steps=opb.read(8)+1;
220 for(int i=0;i<info.coupling_steps;i++){
221 int testM=info.coupling_mag[i]=opb.read(ilog2(vi.channels));
222 int testA=info.coupling_ang[i]=opb.read(ilog2(vi.channels));
227 testM>=vi.channels ||
236 if(opb.read(2)>0){ /* 2,3:reserved */
243 for(int i=0;i<vi.channels;i++){
244 info.chmuxlist[i]=opb.read(4);
245 if(info.chmuxlist[i]>=info.submaps){
253 for(int i=0;i<info.submaps;i++){
254 info.timesubmap[i]=opb.read(8);
255 if(info.timesubmap[i]>=vi.times){
260 info.floorsubmap[i]=opb.read(8);
261 if(info.floorsubmap[i]>=vi.floors){
266 info.residuesubmap[i]=opb.read(8);
267 if(info.residuesubmap[i]>=vi.residues){
280 // no time mapping implementation for now
282 int forward(Block vb, Object l){
285 LookMapping0 look=(LookMapping0)l;
286 InfoMapping0 info=look.map;
287 InfoMode mode=look.mode;
289 float[] window=vd.window[vb.W][vb.lW][vb.nW][mode.windowtype];
291 float[][] pcmbundle=new float[vi.channles][];
292 int[] nonzero=new int[vi.channels];
294 // time domain pre-window: NONE IMPLEMENTED
296 // window the PCM data: takes PCM vector, vb; modifies PCM vector
298 for(int i=0;i<vi.channels;i++){
299 float[] pcm=vb.pcm[i];
304 // time-domain post-window: NONE IMPLEMENTED
306 // transform the PCM data; takes PCM vector, vb; modifies PCM vector
307 // only MDCT right now....
308 for(int i=0;i<vi.channels;i++){
309 float[] pcm=vb.pcm[i];
310 mdct_forward(vd.transform[vb.W][0],pcm,pcm);
314 float[] floor=_vorbis_block_alloc(vb,n*sizeof(float)/2);
316 for(int i=0;i<vi.channels;i++){
317 float[] pcm=vb.pcm[i];
318 float[] decay=look.decay[i];
319 int submap=info.chmuxlist[i];
321 // if some other mode/mapping was called last frame, our decay
322 // accumulator is out of date. Clear it.
323 //if(look.lastframe+1 != vb->sequence)
324 // memset(decay,0,n*sizeof(float)/2);
326 // perform psychoacoustics; do masking
327 _vp_compute_mask(look.psy_look[submap],pcm,floor,decay);
329 _analysis_output("mdct",seq,pcm,n/2,0,1);
330 _analysis_output("lmdct",seq,pcm,n/2,0,0);
331 _analysis_output("prefloor",seq,floor,n/2,0,1);
333 // perform floor encoding
334 nonzero[i]=look.floor_func[submap].
335 forward(vb,look.floor_look[submap],floor,floor,look.floor_state[i]);
337 _analysis_output("floor",seq,floor,n/2,0,1);
339 // apply the floor, do optional noise levelling
340 _vp_apply_floor(look->psy_look+submap,pcm,floor);
342 _analysis_output("res",seq++,pcm,n/2,0,0);
345 // perform residue encoding with residue mapping; this is
346 // multiplexed. All the channels belonging to one submap are
347 // encoded (values interleaved), then the next submap, etc
349 for(int i=0;i<info.submaps;i++){
351 for(int j=0;j<vi.channels;j++){
352 if(info.chmuxlist[j]==i && nonzero[j]==1){
353 pcmbundle[ch_in_bundle++]=vb.pcm[j];
356 look.residue_func[i].forward(vb,look.residue_look[i], pcmbundle,ch_in_bundle);
359 look.lastframe=vb.sequence;
364 float[][] pcmbundle=null;
365 int[] zerobundle=null;
367 Object[] floormemo=null;
369 synchronized int inverse(Block vb, Object l){
370 //System.err.println("Mapping0.inverse");
373 LookMapping0 look=(LookMapping0)l;
374 InfoMapping0 info=look.map;
375 InfoMode mode=look.mode;
376 int n=vb.pcmend=vi.blocksizes[vb.W];
378 float[] window=vd.window[vb.W][vb.lW][vb.nW][mode.windowtype];
379 // float[][] pcmbundle=new float[vi.channels][];
380 // int[] nonzero=new int[vi.channels];
381 if(pcmbundle==null || pcmbundle.length<vi.channels){
382 pcmbundle=new float[vi.channels][];
383 nonzero=new int[vi.channels];
384 zerobundle=new int[vi.channels];
385 floormemo=new Object[vi.channels];
388 // time domain information decode (note that applying the
389 // information would have to happen later; we'll probably add a
390 // function entry to the harness for that later
393 // recover the spectral envelope; store it in the PCM vector for now
394 for(int i=0;i<vi.channels;i++){
395 float[] pcm=vb.pcm[i];
396 int submap=info.chmuxlist[i];
398 floormemo[i]=look.floor_func[submap].inverse1(vb,look.
402 if(floormemo[i]!=null){ nonzero[i]=1; }
403 else{ nonzero[i]=0; }
404 for(int j=0; j<n/2; j++){
408 //_analysis_output("ifloor",seq+i,pcm,n/2,0,1);
411 for(int i=0; i<info.coupling_steps; i++){
412 if(nonzero[info.coupling_mag[i]]!=0 ||
413 nonzero[info.coupling_ang[i]]!=0){
414 nonzero[info.coupling_mag[i]]=1;
415 nonzero[info.coupling_ang[i]]=1;
419 // recover the residue, apply directly to the spectral envelope
421 for(int i=0;i<info.submaps;i++){
423 for(int j=0;j<vi.channels;j++){
424 if(info.chmuxlist[j]==i){
426 zerobundle[ch_in_bundle]=1;
429 zerobundle[ch_in_bundle]=0;
431 pcmbundle[ch_in_bundle++]=vb.pcm[j];
435 look.residue_func[i].inverse(vb,look.residue_look[i],
436 pcmbundle,zerobundle,ch_in_bundle);
440 for(int i=info.coupling_steps-1;i>=0;i--){
441 float[] pcmM=vb.pcm[info.coupling_mag[i]];
442 float[] pcmA=vb.pcm[info.coupling_ang[i]];
444 for(int j=0;j<n/2;j++){
471 // /* compute and apply spectral envelope */
473 for(int i=0;i<vi.channels;i++){
474 float[] pcm=vb.pcm[i];
475 int submap=info.chmuxlist[i];
476 look.floor_func[submap].inverse2(vb,look.floor_look[submap],floormemo[i],pcm);
479 // transform the PCM data; takes PCM vector, vb; modifies PCM vector
480 // only MDCT right now....
482 for(int i=0;i<vi.channels;i++){
483 float[] pcm=vb.pcm[i];
484 //_analysis_output("out",seq+i,pcm,n/2,0,0);
485 ((Mdct)vd.transform[vb.W][0]).backward(pcm,pcm);
488 // now apply the decoded pre-window time information
492 for(int i=0;i<vi.channels;i++){
493 float[] pcm=vb.pcm[i];
495 for(int j=0;j<n;j++){
500 for(int j=0;j<n;j++){
504 //_analysis_output("final",seq++,pcm,n,0,0);
507 // now apply the decoded post-window time information
514 private static int ilog2(int v){
525 int submaps; // <= 16
526 int[] chmuxlist=new int[256]; // up to 256 channels in a Vorbis stream
528 int[] timesubmap=new int[16]; // [mux]
529 int[] floorsubmap=new int[16]; // [mux] submap to floors
530 int[] residuesubmap=new int[16];// [mux] submap to residue
531 int[] psysubmap=new int[16]; // [mux]; encode only
534 int[] coupling_mag=new int[256];
535 int[] coupling_ang=new int[256];
554 Object[] floor_state;
555 Object[] residue_look;
558 FuncTime[] time_func;
559 FuncFloor[] floor_func;
560 FuncResidue[] residue_func;
564 int lastframe; // if a different mode is called, we need to
565 // invalidate decay and floor state