Esempio n. 1
0
        public void Write(FlacStream stream, int sampleCount, ref int currentSubframeSample, ref uint[] samples)
        {
            stream.Writer.WriteBits(encodingParameter_, ENCODING_PARAMETER_BITS_COUNT);

            int i = currentSubframeSample;
            int max = i + sampleCount;
            if (ParamIsEscapeCode())
            {
                stream.Writer.WriteBits(bpsForUnencodedData_, BPS_UNENCODED_DATA_BITS_COUNT);
                while (i < max)
                {
                    stream.Writer.WriteBits(samples[i], bpsForUnencodedData_);
                    i++;
                }
            }
            else
            {
                while (i < max)
                {
                    RiceEncoder.PutResidual(stream, (int)samples[i],encodingParameter_);
                    i++;
                }
            }
            currentSubframeSample = i;
        }
Esempio n. 2
0
        public void Read(FlacStream stream)
        {
            ushort crc = stream.Reader.Crc.GetCRC16();
            crc_ = stream.Reader.ReadUShort();

            Validation.IsValid(crc_ == crc);
        }
Esempio n. 3
0
        public void Read(FlacStream stream, FrameHeader frameHeader, int order, int bitsPerSample)
        {
            bitsPerSample_ = bitsPerSample;
            lpcOrder_ = order;

            uint [] warmupSamples = Warmup.ReadSamples(stream, order, bitsPerSample_);

            quantitizedLpcPrecisionInBitsMinusOne_ = stream.Reader.ReadBitsAsSByte(QLPC_PRECISION_BITS_COUNT);
            quantitizedLpcShiftInBits_ = stream.Reader.ReadBitsAsSByte(QLPC_PRECISION_SHIFT_COUNT);

            predictorCoefficients_ = new uint[order];
            int i  = 0;
            while (i < predictorCoefficients_.Length)
            {
                predictorCoefficients_[i] = stream.Reader.ReadBits(quantitizedLpcPrecisionInBitsMinusOne_ + 1);
                i++;
            }

            samples_ = new uint[frameHeader.BlockSize];

            i = 0;
            foreach (uint w in warmupSamples)
            {
                samples_[i] = w;
                i++;
            }

            residual_ = new Residual();
            residual_.Read(stream, frameHeader.BlockSize, order, ref samples_);
        }
Esempio n. 4
0
        public void Read(FlacStream stream, int bitsPerSampleInFrame)
        {
            bitsPerSampleInFrame_ = bitsPerSampleInFrame;

            // read data
            samples_ = new uint[] { stream.Reader.ReadBitsAsUInt(bitsPerSampleInFrame_) };
        }
Esempio n. 5
0
        //
        public void Read(FlacStream stream, int sampleCount, ref int currentSubframeSample, ref uint[] samples)
        {
            encodingParameter_ = stream.Reader.ReadBitsAsSByte(ENCODING_PARAMETER_BITS_COUNT);

            int i = currentSubframeSample;
            int max = i + sampleCount;
            if (ParamIsEscapeCode())
            {
                bpsForUnencodedData_ = stream.Reader.ReadBitsAsSByte(BPS_UNENCODED_DATA_BITS_COUNT);
                while (i < max)
                {
                 	samples[i] = stream.Reader.ReadBitsAsUInt(bpsForUnencodedData_);
                    i++;
                }
            }
            else
            {
                while (i < max)
                {
                    samples[i] = (uint)RiceDecoder.GetResidual(stream, encodingParameter_);
                    i++;
                }
            }
            currentSubframeSample = i;
        }
Esempio n. 6
0
        public static int GetResidual(FlacStream stream, sbyte parameter)
        {
            Debug.Assert(parameter <= MAX_RICE_PARAMETER);

            int msbValue;
            int lsbValue;

            msbValue = stream.Reader.GetUnaryBitStreamLength();

            if (parameter > 0)
            {
                lsbValue = (int)stream.Reader.ReadBits(parameter);
            }
            else
            {
                lsbValue = 0;
            }

            int residual = (msbValue << parameter) | lsbValue;
            if ((residual & 1) == 1)
            {
                residual = ((residual >> 1) * -1) - 1;
            }
            else
            {
                residual >>= 1;
            }

            return residual;
        }
        public override void Write(FlacStream stream)
        {
            Header.Write(stream);

            foreach (byte b in blockData_)
            {
                stream.Writer.WriteByte(b);
            }
        }
Esempio n. 8
0
        public static MetadataBlock New(FlacStream stream)
        {
            MetadataBlockHeader header = new MetadataBlockHeader();
            header.Read(stream);

            MetadataBlock block = GetBlock(header);
            block.Read(stream);
            return block;
        }
Esempio n. 9
0
        public static void WriteSamples(FlacStream stream, uint[] warmupSamples, int bitsPerSample, int count)
        {
            Debug.Assert(warmupSamples != null);

            int i = 0;
            while (i < count)
            {
                stream.Writer.WriteBits(warmupSamples[i], bitsPerSample);
                i++;
            }
        }
Esempio n. 10
0
        private void Write_(FlacStream stream, int count)
        {
            base.Write(stream); ;

            int i = 0;
            while (i < count)
            {
                stream.Writer.WriteBits(samples_[i], bitsPerSampleInFrame_);
                i++;
            }
        }
        public override void Write(FlacStream stream, ref uint[] samples)
        {
            stream.Writer.WriteBits(partitionOrder_, PARTITION_ORDER_BITS_COUNT);

            int i = 0;
            int currentSubframeSample = predictorOrder_;
            foreach (RicePartition p in partitions_)
            {
                p.Write(stream, GetSampleCount(blockSize_, predictorOrder_, partitionOrder_, i == 0), ref currentSubframeSample, ref samples);
                i++;
            }
        }
Esempio n. 12
0
        /* the count functions "fold" the residual partitions into a single
         * big partition. difference in no. of bytes can arise because of the same
         * */
        public void Write(FlacStream stream, int startSample, int count)
        {
            header_.Write(stream);

            foreach (Subframe subframe in subframes_)
            {
                if (!(subframe  is SubframeConstant))
                {
                    SubframeVerbatim v = subframe as SubframeVerbatim;
                    if (v != null)
                    {
                        ShiftSamples(startSample, count, v);
                    }
                    SubframeFixed f = subframe as SubframeFixed;
                    if (f != null)
                    {
                        f.DecodeSamples();
                        ShiftSamples(startSample, count, f);
                        f.EncodeSamples(count);
                    }
                    SubframeLpc lpc = subframe as SubframeLpc;
                    if (lpc != null)
                    {
                        lpc.DecodeSamples();
            #if DEBUG
                        uint[] debug_DecodedSamples = new uint[lpc.Samples.Length];
                        lpc.Samples.CopyTo(debug_DecodedSamples, 0);
            #endif
                        ShiftSamples(startSample, count, lpc);
                        lpc.EncodeSamples(count);
            #if DEBUG
                        lpc.DecodeSamples();

                        int i0 = startSample;
                        int i1 = 0;
                        int max = i0 + count;
                        while (i0 < max)
                        {
                            Debug.Assert(debug_DecodedSamples[i0] == lpc.Samples[i1]);
                            i0++;
                            i1++;
                        }
                        lpc.EncodeSamples(count);
            #endif
                    }
                }
                subframe.Write(stream, count);
            }

            stream.Writer.WriteBits(0, stream.Writer.BitsToByteBoundary);

            footer_.Write(stream);
        }
Esempio n. 13
0
        public static Subframe New(FlacStream stream, FrameHeader frameHeader, int bitsPerSampleInFrame)
        {
            Subframe sf;
            SubframeHeader header = new SubframeHeader();
            header.Read(stream);

            int subframeType = header.SubframeType;
            if (subframeType == 0)
            {
                // constant
                SubframeConstant subframe = new SubframeConstant();
                subframe.header_ = header;
                subframe.Read(stream, bitsPerSampleInFrame);
                sf = subframe;
            }
            else if (subframeType == 1)
            {
                // verbatim
                SubframeVerbatim subframe = new SubframeVerbatim();
                subframe.header_ = header;
                subframe.Read(stream, bitsPerSampleInFrame, frameHeader.BlockSize);
                sf = subframe;
            }
            else if (subframeType >= 2 && subframeType <= 7)
            {
                throw new FlacFormatReservedException();
            }
            else if (subframeType >= 8 && subframeType <= 15)
            {
                // fixed
                int predictorOrder = subframeType & 7;
                Validation.IsReserved(predictorOrder <= 4);

                SubframeFixed subframe = new SubframeFixed();
                subframe.header_ = header;
                subframe.Read(stream, frameHeader, predictorOrder, bitsPerSampleInFrame);
                sf = subframe;
            }
            else if (subframeType == 16)
            {
                throw new FlacFormatReservedException();
            }
            else
            {
                int lpcOrder = (subframeType & 0x1f) + 1;

                SubframeLpc subframe = new SubframeLpc();
                subframe.header_ = header;
                subframe.Read(stream, frameHeader, lpcOrder, bitsPerSampleInFrame);
                sf = subframe;
            }
            return sf;
        }
        public override void Write(FlacStream stream, int blockSize, ref uint[] samples)
        {
            // only one partition
            partitionOrder_ = 0;
            blockSize_ = blockSize;

            stream.Writer.WriteBits(partitionOrder_, PARTITION_ORDER_BITS_COUNT);

            int currentSubframeSample = predictorOrder_;
            RicePartition p = new RicePartition();
            p.EncodingParameter = 0;
            p.Write(stream, GetSampleCount(blockSize_, predictorOrder_, partitionOrder_, true), ref currentSubframeSample, ref samples);
        }
Esempio n. 15
0
        public void Write(FlacStream stream)
        {
            header_.Write(stream);

            foreach (Subframe subframe in subframes_)
            {
                subframe.Write(stream);
            }

            stream.Writer.WriteBits(0, stream.Writer.BitsToByteBoundary);

            footer_.Write(stream);
        }
Esempio n. 16
0
        public void Read(FlacStream stream, int bitsPerSampleInFrame, int frameBlockSize)
        {
            bitsPerSampleInFrame_ = bitsPerSampleInFrame;
            frameBlockSize_ = frameBlockSize;

            // read data
            samples_ = new uint[frameBlockSize_];

            int i = 0;
            while (i < frameBlockSize_)
            {
                samples_[i] = stream.Reader.ReadBitsAsUInt(bitsPerSampleInFrame_);
                i++;
            }
        }
Esempio n. 17
0
        public static uint[] ReadSamples(FlacStream stream, int order, int bitsPerSample)
        {
            uint[] warmupSamples = null;

            // read unencoded warmup bits
            warmupSamples = new uint[order];

            int i = 0;
            while (i < warmupSamples.Length)
            {
                uint val = stream.Reader.ReadBitsAsUInt(bitsPerSample);
                warmupSamples[i] = val;
                i++;
            }
            return warmupSamples;
        }
Esempio n. 18
0
        public void ReadData(FlacStream stream)
        {
            Debug.Assert(subframes_.Count == 0);

            // one subframe for each channel
            int channels = stream.StreamInfo.Channels;
            Validation.IsValid(channels >= 1);

            while (channels > 0)
            {
                int bps = GetBps();

                Subframe subframe = Subframe.New(stream, header_, bps);
                subframes_.Add(subframe);
                channels--;
            }

            // zero padding
            stream.Reader.SkipToByteBoundary();
        }
Esempio n. 19
0
        public static void PutResidual(FlacStream stream, int residual, sbyte parameter)
        {
            // number : +48, param: 4
            // step 1: 48
            // step 2: 110000
            // step 3: 110000 0
            // step 4: 110 0000
            // step 5: 0000001 0000

            // number : -48, param: 4
            // step 1: 47
            // step 2: 101111
            // step 3: 101111 1
            // step 4: 101 1111
            // step 5: 000001 1111

            // number : 0, param: 4
            // step 1: 0
            // step 2: 0
            // step 3: 0 0
            // step 4:  0000
            // step 5: 1 0000

            if (residual < 0)
            {
                residual = (residual * -1) - 1;
                residual = (residual << 1) | 1;
            }
            else
            {
                residual <<= 1;
            }

            int msbValue = residual >> parameter;
            int lsbValue = residual - (msbValue << parameter);

            Debug.Assert(msbValue < short.MaxValue);
            stream.Writer.WriteUnaryBitStream(msbValue);

            stream.Writer.WriteBits(lsbValue, parameter);
        }
Esempio n. 20
0
        public void Read(FlacStream stream, int blockSize, int predictorOrder, ref uint [] samples)
        {
            codingMethod_ = stream.Reader.ReadBitsAsSByte(CODING_METHOD_BITS_COUNT);

            switch (codingMethod_)
            {
                case 0:
                {
                    method_ = new ResidualCodingMethodPartitionedRice();
                    method_.Read(stream, blockSize, predictorOrder, ref samples);
                    break;
                }
                case 1:
                {
                    throw new FlacNotImplementedException();
                }
                default:
                {
                    throw new FlacFormatReservedException();
                }
            }
        }
        public override void Read(FlacStream stream, int frameBlockSize, int predictorOrder, ref uint[] samples)
        {
            partitionOrder_ = stream.Reader.ReadBitsAsSByte(PARTITION_ORDER_BITS_COUNT);

            predictorOrder_ = predictorOrder;
            blockSize_ = frameBlockSize;

            int partitionCount = 1 << partitionOrder_; // 2 ^ order
            Validation.IsValid(partitionCount >= 1);

            int currentSubframeSample = predictorOrder;

            int sampleCount0 = GetSampleCount(frameBlockSize, predictorOrder, partitionOrder_, true);
            int sampleCount1 = GetSampleCount(frameBlockSize, predictorOrder, partitionOrder_, false);

            while (partitions_.Count < partitionCount)
            {
                int count = partitions_.Count == 0 ? sampleCount0 : sampleCount1;
                RicePartition partition = new RicePartition();
                partition.Read(stream, count, ref currentSubframeSample, ref samples);
                partitions_.Add(partition);
            }
        }
Esempio n. 22
0
 public abstract void Write(FlacStream stream);
Esempio n. 23
0
 public abstract void Read(FlacStream stream);
        public override void Write(FlacStream stream)
        {
            Header.Write(stream);

            stream.Writer.WriteBits(0, Bit.MultiplyBy8I(Header.BlockLengthInBytes));
        }
 public override void Read(FlacStream stream)
 {
     stream.Reader.SkipBits(Bit.MultiplyBy8I(Header.BlockLengthInBytes));
 }
        public override void Read(FlacStream stream)
        {
            minBlockSizeInSamples_ = stream.Reader.ReadUShort();
            maxBlockSizeInSamples_ = stream.Reader.ReadUShort();

            Validation.IsValid(minBlockSizeInSamples_ >= MIN_BLOCK_SIZE && minBlockSizeInSamples_ <= MAX_BLOCK_SIZE);
            Validation.IsValid(maxBlockSizeInSamples_ >= MIN_BLOCK_SIZE && maxBlockSizeInSamples_ <= MAX_BLOCK_SIZE);

            minFrameSizeInBytes_ = stream.Reader.ReadBitsAsInt(FRAME_SIZE_BITS_COUNT);
            maxFrameSizeInBytes_ = stream.Reader.ReadBitsAsInt(FRAME_SIZE_BITS_COUNT);
            sampleRateInHz_ = stream.Reader.ReadBitsAsInt(SAMPLE_RATE_BITS_COUNT);
            channelsMinusOne_ = stream.Reader.ReadBitsAsSByte(CHANNELS_BITS_COUNT);
            bitsPerSampleMinusOne_ = stream.Reader.ReadBitsAsSByte(BPS_BITS_COUNT);
            totalSamplesInStream_ = stream.Reader.ReadBitsAsLong(TOTAL_SAMPLES_BITS_COUNT);

            md5Signature_[0] = stream.Reader.ReadUInt();
            md5Signature_[1] = stream.Reader.ReadUInt();
            md5Signature_[2] = stream.Reader.ReadUInt();
            md5Signature_[3] = stream.Reader.ReadUInt();
        }
        public override void Write(FlacStream stream)
        {
            Header.Write(stream);

            stream.Writer.WriteUShort(minBlockSizeInSamples_);
            stream.Writer.WriteUShort(maxBlockSizeInSamples_);

            stream.Writer.WriteBits(minFrameSizeInBytes_, FRAME_SIZE_BITS_COUNT);
            stream.Writer.WriteBits(maxFrameSizeInBytes_, FRAME_SIZE_BITS_COUNT);
            stream.Writer.WriteBits(sampleRateInHz_, SAMPLE_RATE_BITS_COUNT);
            stream.Writer.WriteBits(channelsMinusOne_, CHANNELS_BITS_COUNT);
            stream.Writer.WriteBits(bitsPerSampleMinusOne_, BPS_BITS_COUNT);
            stream.Writer.WriteBitsAsLong(totalSamplesInStream_,TOTAL_SAMPLES_BITS_COUNT);

            stream.Writer.WriteUInt(md5Signature_[0]);
            stream.Writer.WriteUInt(md5Signature_[1]);
            stream.Writer.WriteUInt(md5Signature_[2]);
            stream.Writer.WriteUInt(md5Signature_[3]);
        }
 public abstract void Write(FlacStream stream, int count, ref uint[] samples);
 public abstract void Read(FlacStream stream, int frameBlockSize, int predictorOrder, ref uint[] samples);
 public override void Read(FlacStream stream)
 {
     throw new FlacNotImplementedException();
 }