Path: blob/main/src/lwjgl/java/javazoom/jl/decoder/LayerIDecoder.java
8650 views
/*1* 09/26/08 throw exception on subbband alloc error: Christopher G. Jennings ([email protected])2*3* 11/19/04 1.0 moved to LGPL.4*5* 12/12/99 Initial version. Adapted from javalayer.java6* and Subband*.java. [email protected]7*8* 02/28/99 Initial version : javalayer.java by E.B9*-----------------------------------------------------------------------10* This program is free software; you can redistribute it and/or modify11* it under the terms of the GNU Library General Public License as published12* by the Free Software Foundation; either version 2 of the License, or13* (at your option) any later version.14*15* This program is distributed in the hope that it will be useful,16* but WITHOUT ANY WARRANTY; without even the implied warranty of17* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the18* GNU Library General Public License for more details.19*20* You should have received a copy of the GNU Library General Public21* License along with this program; if not, write to the Free Software22* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.23*----------------------------------------------------------------------24*/2526package javazoom.jl.decoder;2728/**29* Implements decoding of MPEG Audio Layer I frames.30*/31class LayerIDecoder implements FrameDecoder32{33protected Bitstream stream;34protected Header header;35protected SynthesisFilter filter1, filter2;36protected Obuffer buffer;37protected int which_channels;38protected int mode;3940protected int num_subbands;41protected Subband[] subbands;42protected Crc16 crc = null; // new Crc16[1] to enable CRC checking.4344public LayerIDecoder()45{46crc = new Crc16();47}4849public void create(Bitstream stream0, Header header0,50SynthesisFilter filtera, SynthesisFilter filterb,51Obuffer buffer0, int which_ch0)52{53stream = stream0;54header = header0;55filter1 = filtera;56filter2 = filterb;57buffer = buffer0;58which_channels = which_ch0;5960}6162public void decodeFrame() throws DecoderException63{6465num_subbands = header.number_of_subbands();66subbands = new Subband[32];67mode = header.mode();6869createSubbands();7071readAllocation();72readScaleFactorSelection();7374if ((crc != null) || header.checksum_ok())75{76readScaleFactors();7778readSampleData();79}8081}8283protected void createSubbands()84{85int i;86if (mode == Header.SINGLE_CHANNEL)87for (i = 0; i < num_subbands; ++i)88subbands[i] = new SubbandLayer1(i);89else if (mode == Header.JOINT_STEREO)90{91for (i = 0; i < header.intensity_stereo_bound(); ++i)92subbands[i] = new SubbandLayer1Stereo(i);93for (; i < num_subbands; ++i)94subbands[i] = new SubbandLayer1IntensityStereo(i);95}96else97{98for (i = 0; i < num_subbands; ++i)99subbands[i] = new SubbandLayer1Stereo(i);100}101}102103protected void readAllocation() throws DecoderException104{105// start to read audio data:106for (int i = 0; i < num_subbands; ++i)107subbands[i].read_allocation(stream, header, crc);108109}110111protected void readScaleFactorSelection()112{113// scale factor selection not present for layer I.114}115116protected void readScaleFactors()117{118for (int i = 0; i < num_subbands; ++i)119subbands[i].read_scalefactor(stream, header);120}121122protected void readSampleData()123{124boolean read_ready = false;125boolean write_ready = false;126int mode = header.mode();127int i;128do129{130for (i = 0; i < num_subbands; ++i)131read_ready = subbands[i].read_sampledata(stream);132do133{134for (i = 0; i < num_subbands; ++i)135write_ready = subbands[i].put_next_sample(which_channels,filter1, filter2);136137filter1.calculate_pcm_samples(buffer);138if ((which_channels == OutputChannels.BOTH_CHANNELS) && (mode != Header.SINGLE_CHANNEL))139filter2.calculate_pcm_samples(buffer);140} while (!write_ready);141} while (!read_ready);142143}144145/**146* Abstract base class for subband classes of layer I and II147*/148abstract static class Subband149{150/*151* Changes from version 1.1 to 1.2:152* - array size increased by one, although a scalefactor with index 63153* is illegal (to prevent segmentation faults)154*/155// Scalefactors for layer I and II, Annex 3-B.1 in ISO/IEC DIS 11172:156public static final float scalefactors[] =157{1582.00000000000000f, 1.58740105196820f, 1.25992104989487f, 1.00000000000000f,1590.79370052598410f, 0.62996052494744f, 0.50000000000000f, 0.39685026299205f,1600.31498026247372f, 0.25000000000000f, 0.19842513149602f, 0.15749013123686f,1610.12500000000000f, 0.09921256574801f, 0.07874506561843f, 0.06250000000000f,1620.04960628287401f, 0.03937253280921f, 0.03125000000000f, 0.02480314143700f,1630.01968626640461f, 0.01562500000000f, 0.01240157071850f, 0.00984313320230f,1640.00781250000000f, 0.00620078535925f, 0.00492156660115f, 0.00390625000000f,1650.00310039267963f, 0.00246078330058f, 0.00195312500000f, 0.00155019633981f,1660.00123039165029f, 0.00097656250000f, 0.00077509816991f, 0.00061519582514f,1670.00048828125000f, 0.00038754908495f, 0.00030759791257f, 0.00024414062500f,1680.00019377454248f, 0.00015379895629f, 0.00012207031250f, 0.00009688727124f,1690.00007689947814f, 0.00006103515625f, 0.00004844363562f, 0.00003844973907f,1700.00003051757813f, 0.00002422181781f, 0.00001922486954f, 0.00001525878906f,1710.00001211090890f, 0.00000961243477f, 0.00000762939453f, 0.00000605545445f,1720.00000480621738f, 0.00000381469727f, 0.00000302772723f, 0.00000240310869f,1730.00000190734863f, 0.00000151386361f, 0.00000120155435f, 0.00000000000000f /* illegal scalefactor */174};175176public abstract void read_allocation (Bitstream stream, Header header, Crc16 crc) throws DecoderException;177public abstract void read_scalefactor (Bitstream stream, Header header);178public abstract boolean read_sampledata (Bitstream stream);179public abstract boolean put_next_sample (int channels, SynthesisFilter filter1, SynthesisFilter filter2);180};181182/**183* Class for layer I subbands in single channel mode.184* Used for single channel mode185* and in derived class for intensity stereo mode186*/187static class SubbandLayer1 extends Subband188{189190// Factors and offsets for sample re-quantization191public static final float table_factor[] = {1920.0f, (1.0f/2.0f) * (4.0f/3.0f), (1.0f/4.0f) * (8.0f/7.0f), (1.0f/8.0f) * (16.0f/15.0f),193(1.0f/16.0f) * (32.0f/31.0f), (1.0f/32.0f) * (64.0f/63.0f), (1.0f/64.0f) * (128.0f/127.0f),194(1.0f/128.0f) * (256.0f/255.0f), (1.0f/256.0f) * (512.0f/511.0f),195(1.0f/512.0f) * (1024.0f/1023.0f), (1.0f/1024.0f) * (2048.0f/2047.0f),196(1.0f/2048.0f) * (4096.0f/4095.0f), (1.0f/4096.0f) * (8192.0f/8191.0f),197(1.0f/8192.0f) * (16384.0f/16383.0f), (1.0f/16384.0f) * (32768.0f/32767.0f)198};199200public static final float table_offset[] = {2010.0f, ((1.0f/2.0f)-1.0f) * (4.0f/3.0f), ((1.0f/4.0f)-1.0f) * (8.0f/7.0f), ((1.0f/8.0f)-1.0f) * (16.0f/15.0f),202((1.0f/16.0f)-1.0f) * (32.0f/31.0f), ((1.0f/32.0f)-1.0f) * (64.0f/63.0f), ((1.0f/64.0f)-1.0f) * (128.0f/127.0f),203((1.0f/128.0f)-1.0f) * (256.0f/255.0f), ((1.0f/256.0f)-1.0f) * (512.0f/511.0f),204((1.0f/512.0f)-1.0f) * (1024.0f/1023.0f), ((1.0f/1024.0f)-1.0f) * (2048.0f/2047.0f),205((1.0f/2048.0f)-1.0f) * (4096.0f/4095.0f), ((1.0f/4096.0f)-1.0f) * (8192.0f/8191.0f),206((1.0f/8192.0f)-1.0f) * (16384.0f/16383.0f), ((1.0f/16384.0f)-1.0f) * (32768.0f/32767.0f)207};208209protected int subbandnumber;210protected int samplenumber;211protected int allocation;212protected float scalefactor;213protected int samplelength;214protected float sample;215protected float factor, offset;216217/**218* Constructor.219*/220public SubbandLayer1(int subbandnumber)221{222this.subbandnumber = subbandnumber;223samplenumber = 0;224}225226/**227*228*/229public void read_allocation(Bitstream stream, Header header, Crc16 crc) throws DecoderException230{231if ((allocation = stream.get_bits (4)) == 15)232{233// CGJ: catch this condition and throw appropriate exception234throw new DecoderException(DecoderErrors.ILLEGAL_SUBBAND_ALLOCATION, null);235// cerr << "WARNING: stream contains an illegal allocation!\n";236// MPEG-stream is corrupted!237}238239if (crc != null) crc.add_bits (allocation, 4);240if (allocation != 0)241{242samplelength = allocation + 1;243factor = table_factor[allocation];244offset = table_offset[allocation];245}246}247248/**249*250*/251public void read_scalefactor(Bitstream stream, Header header)252{253if (allocation != 0) scalefactor = scalefactors[stream.get_bits(6)];254}255256/**257*258*/259public boolean read_sampledata(Bitstream stream)260{261if (allocation != 0)262{263sample = (float) (stream.get_bits(samplelength));264}265if (++samplenumber == 12)266{267samplenumber = 0;268return true;269}270return false;271}272273/**274*275*/276public boolean put_next_sample(int channels, SynthesisFilter filter1, SynthesisFilter filter2)277{278if ((allocation !=0) && (channels != OutputChannels.RIGHT_CHANNEL))279{280float scaled_sample = (sample * factor + offset) * scalefactor;281filter1.input_sample (scaled_sample, subbandnumber);282}283return true;284}285};286287/**288* Class for layer I subbands in joint stereo mode.289*/290static class SubbandLayer1IntensityStereo extends SubbandLayer1291{292protected float channel2_scalefactor;293294/**295* Constructor296*/297public SubbandLayer1IntensityStereo(int subbandnumber)298{299super(subbandnumber);300}301302/**303*304*/305public void read_allocation(Bitstream stream, Header header, Crc16 crc) throws DecoderException306{307super.read_allocation (stream, header, crc);308}309310/**311*312*/313public void read_scalefactor (Bitstream stream, Header header)314{315if (allocation != 0)316{317scalefactor = scalefactors[stream.get_bits(6)];318channel2_scalefactor = scalefactors[stream.get_bits(6)];319}320}321322/**323*324*/325public boolean read_sampledata(Bitstream stream)326{327return super.read_sampledata (stream);328}329330/**331*332*/333public boolean put_next_sample (int channels, SynthesisFilter filter1, SynthesisFilter filter2)334{335if (allocation !=0 )336{337sample = sample * factor + offset; // re-quantization338if (channels == OutputChannels.BOTH_CHANNELS)339{340float sample1 = sample * scalefactor,341sample2 = sample * channel2_scalefactor;342filter1.input_sample(sample1, subbandnumber);343filter2.input_sample(sample2, subbandnumber);344}345else if (channels == OutputChannels.LEFT_CHANNEL)346{347float sample1 = sample * scalefactor;348filter1.input_sample(sample1, subbandnumber);349}350else351{352float sample2 = sample * channel2_scalefactor;353filter1.input_sample(sample2, subbandnumber);354}355}356return true;357}358};359360/**361* Class for layer I subbands in stereo mode.362*/363static class SubbandLayer1Stereo extends SubbandLayer1364{365protected int channel2_allocation;366protected float channel2_scalefactor;367protected int channel2_samplelength;368protected float channel2_sample;369protected float channel2_factor, channel2_offset;370371372/**373* Constructor374*/375public SubbandLayer1Stereo(int subbandnumber)376{377super(subbandnumber);378}379380/**381*382*/383public void read_allocation (Bitstream stream, Header header, Crc16 crc) throws DecoderException384{385allocation = stream.get_bits(4);386channel2_allocation = stream.get_bits(4);387if (crc != null)388{389crc.add_bits (allocation, 4);390crc.add_bits (channel2_allocation, 4);391}392if (allocation != 0)393{394samplelength = allocation + 1;395factor = table_factor[allocation];396offset = table_offset[allocation];397}398if (channel2_allocation != 0)399{400channel2_samplelength = channel2_allocation + 1;401channel2_factor = table_factor[channel2_allocation];402channel2_offset = table_offset[channel2_allocation];403}404}405406/**407*408*/409public void read_scalefactor(Bitstream stream, Header header)410{411if (allocation != 0) scalefactor = scalefactors[stream.get_bits(6)];412if (channel2_allocation != 0) channel2_scalefactor = scalefactors[stream.get_bits(6)];413}414415/**416*417*/418public boolean read_sampledata (Bitstream stream)419{420boolean returnvalue = super.read_sampledata(stream);421if (channel2_allocation != 0)422{423channel2_sample = (float) (stream.get_bits(channel2_samplelength));424}425return(returnvalue);426}427428/**429*430*/431public boolean put_next_sample(int channels, SynthesisFilter filter1, SynthesisFilter filter2)432{433super.put_next_sample (channels, filter1, filter2);434if ((channel2_allocation != 0) && (channels != OutputChannels.LEFT_CHANNEL))435{436float sample2 = (channel2_sample * channel2_factor + channel2_offset) *437channel2_scalefactor;438if (channels == OutputChannels.BOTH_CHANNELS)439filter2.input_sample (sample2, subbandnumber);440else441filter1.input_sample (sample2, subbandnumber);442}443return true;444}445};446447}448449450