sublay1.cpp
上传用户:tuheem
上传日期:2007-05-01
资源大小:21889k
文件大小:7k
- /* sublay1.cpp
- Implementation of layer I subband objects */
- /*
- * @(#) subband_layer_1.cc 1.7, last edit: 6/15/94 16:51:49
- * @(#) Copyright (C) 1993, 1994 Tobias Bading (bading@cs.tu-berlin.de)
- * @(#) Berlin University of Technology
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU 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 General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- */
- /*
- * Changes from version 1.1 to 1.2:
- * - scalefactors itself instead of scalefactor indices are stored in
- * SubbandLayer1... objects
- * - check for small values in [-1.0E-7, 1.0E-7] removed, because the
- * test itself was slower than some SynthesisFilter::input_sample() calls
- * - check for illegal scalefactor index 63 removed
- */
- #include "sublay1.h"
- #include "scalfact.h"
- #ifdef __WIN32__
- #pragma warning (disable: 4305)
- #endif
- // factors and offsets for sample requantization:
- static const real table_factor[15] = {
- 0.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),
- (1.0f/16.0f) * (32.0f/31.0f), (1.0f/32.0f) * (64.0f/63.0f), (1.0f/64.0f) * (128.0f/127.0f),
- (1.0f/128.0f) * (256.0f/255.0f), (1.0f/256.0f) * (512.0f/511.0f),
- (1.0f/512.0f) * (1024.0f/1023.0f), (1.0f/1024.0f) * (2048.0f/2047.0f),
- (1.0f/2048.0f) * (4096.0f/4095.0f), (1.0f/4096.0f) * (8192.0f/8191.0f),
- (1.0f/8192.0f) * (16384.0f/16383.0f), (1.0f/16384.0f) * (32768.0f/32767.0f)
- };
- static const real table_offset[15] = {
- 0.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),
- ((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),
- ((1.0f/128.0f)-1.0f) * (256.0f/255.0f), ((1.0f/256.0f)-1.0f) * (512.0f/511.0f),
- ((1.0f/512.0f)-1.0f) * (1024.0f/1023.0f), ((1.0f/1024.0f)-1.0f) * (2048.0f/2047.0f),
- ((1.0f/2048.0f)-1.0f) * (4096.0f/4095.0f), ((1.0f/4096.0f)-1.0f) * (8192.0f/8191.0f),
- ((1.0f/8192.0f)-1.0f) * (16384.0f/16383.0f), ((1.0f/16384.0f)-1.0f) * (32768.0f/32767.0f)
- };
- /**********************/ // used for single channel mode
- /*** Standard Class ***/ // and in derived class for intensity
- /**********************/ // stereo mode
- SubbandLayer1::SubbandLayer1 (uint32 subbandnumber)
- {
- this->subbandnumber = subbandnumber;
- samplenumber = 0;
- }
- void SubbandLayer1::read_allocation (Ibitstream *stream, Header *, Crc16 *crc)
- {
- if ((allocation = stream->get_bits (4)) == 15) ;
- // cerr << "WARNING: stream contains an illegal allocation!n"; // MPEG-stream is corrupted!
- if (crc)
- crc->add_bits (allocation, 4);
- if (allocation)
- {
- samplelength = allocation + 1;
- factor = table_factor[allocation];
- offset = table_offset[allocation];
- }
- }
- void SubbandLayer1::read_scalefactor (Ibitstream *stream, Header *)
- {
- if (allocation)
- scalefactor = scalefactors[stream->get_bits (6)];
- }
- bool SubbandLayer1::read_sampledata (Ibitstream *stream)
- {
- if (allocation)
- {
- sample = real (stream->get_bits (samplelength));
- }
- if (++samplenumber == 12)
- {
- samplenumber = 0;
- return true;
- }
- return false;
- }
- bool SubbandLayer1::put_next_sample (e_channels channels,
- SynthesisFilter *filter1, SynthesisFilter *)
- {
- if (allocation && channels != right)
- {
- register real scaled_sample = (sample * factor + offset) * scalefactor;
- filter1->input_sample (scaled_sample, subbandnumber);
- }
- return true;
- }
- /******************************/
- /*** Intensity Stereo Class ***/
- /******************************/
- SubbandLayer1IntensityStereo::SubbandLayer1IntensityStereo (uint32 subbandnumber)
- : SubbandLayer1 (subbandnumber)
- {
- }
- void SubbandLayer1IntensityStereo::read_scalefactor (Ibitstream *stream, Header *)
- {
- if (allocation)
- {
- scalefactor = scalefactors[stream->get_bits (6)];
- channel2_scalefactor = scalefactors[stream->get_bits (6)];
- }
- }
- bool SubbandLayer1IntensityStereo::put_next_sample (e_channels channels,
- SynthesisFilter *filter1, SynthesisFilter *filter2)
- {
- if (allocation)
- {
- sample = sample * factor + offset; // requantization
- if (channels == both)
- {
- register real sample1 = sample * scalefactor,
- sample2 = sample * channel2_scalefactor;
- filter1->input_sample (sample1, subbandnumber);
- filter2->input_sample (sample2, subbandnumber);
- }
- else if (channels == left)
- {
- register real sample1 = sample * scalefactor;
- filter1->input_sample (sample1, subbandnumber);
- }
- else
- {
- register real sample2 = sample * channel2_scalefactor;
- filter1->input_sample (sample2, subbandnumber);
- }
- }
- return true;
- }
- /********************/
- /*** Stereo Class ***/
- /********************/
- SubbandLayer1Stereo::SubbandLayer1Stereo (uint32 subbandnumber)
- : SubbandLayer1 (subbandnumber)
- {
- }
- void SubbandLayer1Stereo::read_allocation (Ibitstream *stream, Header *, Crc16 *crc)
- {
- allocation = stream->get_bits (4);
- channel2_allocation = stream->get_bits (4);
- if (crc)
- {
- crc->add_bits (allocation, 4);
- crc->add_bits (channel2_allocation, 4);
- }
- if (allocation)
- {
- samplelength = allocation + 1;
- factor = table_factor[allocation];
- offset = table_offset[allocation];
- }
- if (channel2_allocation)
- {
- channel2_samplelength = channel2_allocation + 1;
- channel2_factor = table_factor[channel2_allocation];
- channel2_offset = table_offset[channel2_allocation];
- }
- }
- void SubbandLayer1Stereo::read_scalefactor (Ibitstream *stream, Header *)
- {
- if (allocation)
- scalefactor = scalefactors[stream->get_bits (6)];
- if (channel2_allocation)
- channel2_scalefactor = scalefactors[stream->get_bits (6)];
- }
- bool SubbandLayer1Stereo::read_sampledata (Ibitstream *stream)
- {
- bool returnvalue = SubbandLayer1::read_sampledata (stream);
- if (channel2_allocation)
- {
- channel2_sample = real (stream->get_bits (channel2_samplelength));
- }
- return(returnvalue);
- }
- bool SubbandLayer1Stereo::put_next_sample (e_channels channels,
- SynthesisFilter *filter1, SynthesisFilter *filter2)
- {
- SubbandLayer1::put_next_sample (channels, filter1, filter2);
- if (channel2_allocation && channels != left)
- {
- register float sample2 = (channel2_sample * channel2_factor + channel2_offset) *
- channel2_scalefactor;
- if (channels == both)
- filter2->input_sample (sample2, subbandnumber);
- else
- filter1->input_sample (sample2, subbandnumber);
- }
- return true;
- }