Example #1
0
        public unsafe FlacSubFrameLPC(FlacBitReader reader, FlacFrameHeader header, FlacSubFrameData data, int bps, int order)
            : base(header)
        {

            //warmup
            _warmup = new int[FlacConstant.MaxLpcOrder];
            for (int i = 0; i < order; i++)
            {
                _warmup[i] = data.ResidualBuffer[i] = reader.ReadBitsSigned(bps);
            }

            //header
            int u32 = (int)reader.ReadBits(FlacConstant.SubframeLpcQlpCoeffPrecisionLen);
            if (u32 == (1 << FlacConstant.SubframeLpcQlpCoeffPrecisionLen) - 1)
            {
                Debug.WriteLine("Invalid FlacLPC qlp coeff precision.");
                return; //return false;
            }
            _qlpCoeffPrecision = u32 + 1;

            int level = reader.ReadBitsSigned(FlacConstant.SubframeLpcQlpShiftLen);
            if (level < 0)
                throw new Exception("negative shift");
            _lpcShiftNeeded = level;

            _qlpCoeffs = new int[FlacConstant.MaxLpcOrder];

            //qlp coeffs
            for (int i = 0; i < order; i++)
            {
                _qlpCoeffs[i] = reader.ReadBitsSigned(_qlpCoeffPrecision);
            }

            Residual = new FlacResidual(reader, header, data, order);

            for (int i = 0; i < order; i++)
            {
                data.DestBuffer[i] = data.ResidualBuffer[i];
            }

            int i1 = order;
            int result = 0;
            while ((i1 >>= 1) != 0)
            {
                result++;
            }
            if (bps + _qlpCoeffPrecision + result <= 32)
            {
                if (bps <= 16 && _qlpCoeffPrecision <= 16)
                    RestoreLPCSignal(data.ResidualBuffer + order, data.DestBuffer + order, header.BlockSize - order, order); //Restore(data.residualBuffer + order, data.destBuffer, Header.BlockSize - order, order, order);
                else
                    RestoreLPCSignal(data.ResidualBuffer + order, data.DestBuffer + order, header.BlockSize - order, order);
            }
            else
            {
                RestoreLPCSignalWide(data.ResidualBuffer + order, data.DestBuffer + order, header.BlockSize - order, order);//RestoreWide(data.residualBuffer + order, data.destBuffer, Header.BlockSize - order, order, order);
            }
        }
Example #2
0
        public unsafe FlacSubFrameFixed(FlacBitReader reader, FlacFrameHeader header, FlacSubFrameData data, int bps, int order)
            : base(header)
        {
            for (int i = 0; i < order; i++)
            {
                data.ResidualBuffer[i] = data.DestBuffer[i] = reader.ReadBitsSigned(bps);
            }

            Residual = new FlacResidual(reader, header, data, order); //necessary for decoding
            RestoreSignal(data, header.BlockSize - order, order);
        }
Example #3
0
        public unsafe FlacSubFrameFixed(FlacBitReader reader, FlacFrameHeader header, FlacSubFrameData data, int bps, int order)
            : base(header)
        {
            for (int i = 0; i < order; i++)
            {
                data.ResidualBuffer[i] = data.DestBuffer[i] = reader.ReadBitsSigned(bps);
            }

            Residual = new FlacResidual(reader, header, data, order); //necessary for decoding
            RestoreSignal(data, header.BlockSize - order, order);
        }
Example #4
0
 private void Decode(Stream stream, FlacMetadataStreamInfo streamInfo)
 {
     Header = new FlacFrameHeader(stream, streamInfo);
     _stream = stream;
     _streamInfo = streamInfo;
     HasError = Header.HasError;
     if (!HasError)
     {
         ReadSubFrames();
         FreeBuffers();
     }
 }
Example #5
0
 private void Decode(Stream stream, FlacMetadataStreamInfo streamInfo)
 {
     Header      = new FlacFrameHeader(stream, streamInfo);
     _stream     = stream;
     _streamInfo = streamInfo;
     HasError    = Header.HasError;
     if (!HasError)
     {
         ReadSubFrames();
         FreeBuffers();
     }
 }
Example #6
0
        public unsafe bool ProcessResidual(FlacBitReader reader, FlacFrameHeader header, FlacSubFrameData data, int order)
        {
            data.Content.UpdateSize(PartitionOrder);

            int porder = PartitionOrder;
            FlacEntropyCoding codingMethod = CodingMethod;

            int psize  = header.BlockSize >> porder;
            int resCnt = psize - order;

            int ricelength = 4 + (int)codingMethod; //4bit = RICE I | 5bit = RICE II

            //residual
            int  j = order;
            int *r = data.ResidualBuffer + j;

            int partitioncount = 1 << porder;

            for (int p = 0; p < partitioncount; p++)
            {
                if (p == 1)
                {
                    resCnt = psize;
                }
                int n = Math.Min(resCnt, header.BlockSize - j);

                int k = Content.Parameters[p] = (int)reader.ReadBits(ricelength);
                if (k == (1 << ricelength) - 1)
                {
                    k = (int)reader.ReadBits(5);
                    for (int i = n; i > 0; i--)
                    {
                        *(r) = reader.ReadBitsSigned((int)k);
                    }
                }
                else
                {
                    ReadFlacRiceBlock(reader, n, (int)k, r);
                    r += n;
                }
                j += n;
            }

            return(true);
        }
Example #7
0
        public unsafe bool ProcessResidual(FlacBitReader reader, FlacFrameHeader header, FlacSubFrameData data, int order)
        {
            data.Content.UpdateSize(PartitionOrder);

            int porder = PartitionOrder;
            FlacEntropyCoding codingMethod = CodingMethod;

            int psize = header.BlockSize >> porder;
            int resCnt = psize - order;

            int ricelength = 4 + (int)codingMethod; //4bit = RICE I | 5bit = RICE II

            //residual
            int j = order;
            int* r = data.ResidualBuffer + j;

            int partitioncount = 1 << porder;

            for (int p = 0; p < partitioncount; p++)
            {
                if (p == 1) resCnt = psize;
                int n = Math.Min(resCnt, header.BlockSize - j);

                int k = Content.Parameters[p] = (int)reader.ReadBits(ricelength);
                if (k == (1 << ricelength) - 1)
                {
                    k = (int)reader.ReadBits(5);
                    for (int i = n; i > 0; i--)
                    {
                        *(r) = reader.ReadBitsSigned((int)k);
                    }
                }
                else
                {
                    ReadFlacRiceBlock(reader, n, (int)k, r);
                    r += n;
                }
                j += n;
            }

            return true;
        }
Example #8
0
 public bool CompareTo(FlacFrameHeader header)
 {
     return (BitsPerSample == header.BitsPerSample &&
             Channels == header.Channels &&
             SampleRate == header.SampleRate);
 }
Example #9
0
        public unsafe FlacSubFrameLPC(FlacBitReader reader, FlacFrameHeader header, FlacSubFrameData data, int bps, int order)
            : base(header)
        {
            //warmup
            _warmup = new int[FlacConstant.MaxLpcOrder];
            for (int i = 0; i < order; i++)
            {
                _warmup[i] = data.ResidualBuffer[i] = reader.ReadBitsSigned(bps);
            }

            //header
            int u32 = (int)reader.ReadBits(FlacConstant.SubframeLpcQlpCoeffPrecisionLen);

            if (u32 == (1 << FlacConstant.SubframeLpcQlpCoeffPrecisionLen) - 1)
            {
                Debug.WriteLine("Invalid FlacLPC qlp coeff precision.");
                return; //return false;
            }
            _qlpCoeffPrecision = u32 + 1;

            int level = reader.ReadBitsSigned(FlacConstant.SubframeLpcQlpShiftLen);

            if (level < 0)
            {
                throw new Exception("negative shift");
            }
            _lpcShiftNeeded = level;

            _qlpCoeffs = new int[FlacConstant.MaxLpcOrder];

            //qlp coeffs
            for (int i = 0; i < order; i++)
            {
                _qlpCoeffs[i] = reader.ReadBitsSigned(_qlpCoeffPrecision);
            }

            Residual = new FlacResidual(reader, header, data, order);

            for (int i = 0; i < order; i++)
            {
                data.DestBuffer[i] = data.ResidualBuffer[i];
            }

            int i1     = order;
            int result = 0;

            while ((i1 >>= 1) != 0)
            {
                result++;
            }
            if (bps + _qlpCoeffPrecision + result <= 32)
            {
                if (bps <= 16 && _qlpCoeffPrecision <= 16)
                {
                    RestoreLPCSignal(data.ResidualBuffer + order, data.DestBuffer + order, header.BlockSize - order, order); //Restore(data.residualBuffer + order, data.destBuffer, Header.BlockSize - order, order, order);
                }
                else
                {
                    RestoreLPCSignal(data.ResidualBuffer + order, data.DestBuffer + order, header.BlockSize - order, order);
                }
            }
            else
            {
                RestoreLPCSignalWide(data.ResidualBuffer + order, data.DestBuffer + order, header.BlockSize - order, order);//RestoreWide(data.residualBuffer + order, data.destBuffer, Header.BlockSize - order, order, order);
            }
        }
Example #10
0
        private unsafe List <FlacFrameInformation> ScanThisShit(FlacMetadataStreamInfo streamInfo)
        {
            Stream stream = _stream;

            //if (!(stream is BufferedStream))
            //    stream = new BufferedStream(stream);

            byte[] buffer = new byte[BufferSize];
            int    read   = 0;

            stream.Position = 4; //fLaC

            FlacMetadata.ReadAllMetadataFromStream(stream);

            List <FlacFrameInformation> frames    = new List <FlacFrameInformation>();
            FlacFrameInformation        frameInfo = new FlacFrameInformation();

            frameInfo.IsFirstFrame = true;

            FlacFrameHeader baseHeader = null;

            while (true)
            {
                read = stream.Read(buffer, 0, buffer.Length);
                if (read <= FlacConstant.FrameHeaderSize)
                    break;

                fixed(byte *bufferPtr = buffer)
                {
                    byte *ptr = bufferPtr;

                    //for (int i = 0; i < read - FlacConstant.FrameHeaderSize; i++)
                    while ((bufferPtr + read - FlacConstant.FrameHeaderSize) > ptr)
                    {
                        if ((*ptr++ & 0xFF) == 0xFF && (*ptr & 0xF8) == 0xF8) //check sync
                        {
                            byte *ptrSafe = ptr;
                            ptr--;
                            FlacFrameHeader tmp = null;
                            if (IsFrame(ref ptr, streamInfo, baseHeader, out tmp))
                            {
                                FlacFrameHeader header = tmp;
                                if (frameInfo.IsFirstFrame)
                                {
                                    baseHeader             = header;
                                    frameInfo.IsFirstFrame = false;
                                }

                                if (baseHeader.CompareTo(header))
                                {
                                    frameInfo.StreamOffset = stream.Position - read + ((ptrSafe - 1) - bufferPtr);
                                    frameInfo.Header       = header;
                                    frames.Add(frameInfo);

                                    frameInfo.SampleOffset += header.BlockSize;
                                }
                                else
                                {
                                    ptr = ptrSafe;
                                }
                                //todo:
                            }
                            else
                            {
                                ptr = ptrSafe;
                            }
                        }
                    }
                }

                stream.Position -= FlacConstant.FrameHeaderSize;
            }

            return(frames);
        }
Example #11
0
        public unsafe static FlacSubFrameBase GetSubFrame(FlacBitReader reader, FlacSubFrameData data, FlacFrameHeader header, int bps)
        {
            int wastedBits = 0, order = 0;

            uint x = reader.ReadBits(8);
            bool hasWastedBits = (x & 1) != 0;
            x &= 0xFE; //1111 1110

            if (hasWastedBits)
            {
                int u = (int)reader.ReadUnary();
                wastedBits = u + 1;
                bps -= wastedBits;
            }

            if ((x & 0x80) != 0)
            {
                Debug.WriteLine("Flacdecoder lost sync while reading FlacSubFrameHeader. [x & 0x80].");
                return null;
            }

            FlacSubFrameBase subFrame;

            if ((x > 2 && x < 16) ||
                 (x > 24 && x < 64))
            {
                Debug.WriteLine("Invalid FlacSubFrameHeader. [" + x.ToString("x") + "]");
                return null;
            }

            if (x == 0)
            {
                subFrame = new FlacSubFrameConstant(reader, header, data, bps);
            }
            else if (x == 2)
            {
                //verbatim
                subFrame = new FlacSubFrameVerbatim(reader, header, data, bps);
            }
            else if (x >= 16 && x <= 24)
            {
                //fixed
                order = (int)((x >> 1) & 7);
                subFrame = new FlacSubFrameFixed(reader, header, data, bps, order);
            }
            else if (x >= 64)
            {
                //lpc
                order = (int)(((x >> 1) & 31) + 1);
                subFrame = new FlacSubFrameLPC(reader, header, data, bps, order);
            }
            else
            {
                Debug.WriteLine("Invalid Flac-SubframeType: x = " + x + ".");
                return null;
            }

            if (hasWastedBits)
            {
                int* ptrDest = data.DestBuffer;
                for (int i = 0; i < header.BlockSize; i++)
                {
                    *(ptrDest++) <<= wastedBits;
                }
            }

            //System.Diagnostics.Debug.WriteLine(subFrame.GetType().Name);

            //check null removed
            subFrame.WastedBits = wastedBits;

            return subFrame;
        }
Example #12
0
 protected FlacSubFrameBase(FlacFrameHeader header)
 {
     Header = header;
 }
Example #13
0
        public unsafe static FlacSubFrameBase GetSubFrame(FlacBitReader reader, FlacSubFrameData data, FlacFrameHeader header, int bps)
        {
            int wastedBits = 0, order = 0;

            uint x             = reader.ReadBits(8);
            bool hasWastedBits = (x & 1) != 0;

            x &= 0xFE; //1111 1110

            if (hasWastedBits)
            {
                int u = (int)reader.ReadUnary();
                wastedBits = u + 1;
                bps       -= wastedBits;
            }

            if ((x & 0x80) != 0)
            {
                Debug.WriteLine("Flacdecoder lost sync while reading FlacSubFrameHeader. [x & 0x80].");
                return(null);
            }

            FlacSubFrameBase subFrame;

            if ((x > 2 && x < 16) ||
                (x > 24 && x < 64))
            {
                Debug.WriteLine("Invalid FlacSubFrameHeader. [" + x.ToString("x") + "]");
                return(null);
            }

            if (x == 0)
            {
                subFrame = new FlacSubFrameConstant(reader, header, data, bps);
            }
            else if (x == 2)
            {
                //verbatim
                subFrame = new FlacSubFrameVerbatim(reader, header, data, bps);
            }
            else if (x >= 16 && x <= 24)
            {
                //fixed
                order    = (int)((x >> 1) & 7);
                subFrame = new FlacSubFrameFixed(reader, header, data, bps, order);
            }
            else if (x >= 64)
            {
                //lpc
                order    = (int)(((x >> 1) & 31) + 1);
                subFrame = new FlacSubFrameLPC(reader, header, data, bps, order);
            }
            else
            {
                Debug.WriteLine("Invalid Flac-SubframeType: x = " + x + ".");
                return(null);
            }

            if (hasWastedBits)
            {
                int *ptrDest = data.DestBuffer;
                for (int i = 0; i < header.BlockSize; i++)
                {
                    *(ptrDest++) <<= wastedBits;
                }
            }

            //System.Diagnostics.Debug.WriteLine(subFrame.GetType().Name);

            //check null removed
            subFrame.WastedBits = wastedBits;

            return(subFrame);
        }
Example #14
0
 private unsafe bool IsFrame(ref byte *buffer, FlacMetadataStreamInfo streamInfo, FlacFrameHeader baseHeader, out FlacFrameHeader header)
 {
     header = new FlacFrameHeader(ref buffer, streamInfo, true, false);
     return(!header.HasError);
 }
Example #15
0
 private unsafe bool IsFrame(ref byte* buffer, FlacMetadataStreamInfo streamInfo, FlacFrameHeader baseHeader, out FlacFrameHeader header)
 {
     header = new FlacFrameHeader(ref buffer, streamInfo, true, false);
     return !header.HasError;
 }
Example #16
0
 protected FlacSubFrameBase(FlacFrameHeader header)
 {
     Header = header;
 }
Example #17
0
 public bool CompareTo(FlacFrameHeader header)
 {
     return(BitsPerSample == header.BitsPerSample &&
            Channels == header.Channels &&
            SampleRate == header.SampleRate);
 }