Ejemplo n.º 1
0
        public static FlacSubFrameBase GetSubFrame(FlacBitReader reader, FlacSubFrameData data, FlacFrameHeader header,
                                                   int bitsPerSample)
        {
            unchecked
            {
                int wastedBits = 0, order;

                var firstByte = reader.ReadBits(8);

                if ((firstByte & 0x80) != 0) //Zero bit padding, to prevent sync-fooling string of 1s
                {
                    Debug.WriteLine("Flacdecoder subframe-header got no zero-bit padding.");
                    return(null);
                }

                var hasWastedBits = (firstByte & 1) != 0; //Wasted bits-per-sample' flag
                if (hasWastedBits)
                {
                    var k = (int)reader.ReadUnary();
                    wastedBits     = k + 1; //"k-1" follows -> add 1
                    bitsPerSample -= wastedBits;
                }

                FlacSubFrameBase subFrame;
                var subframeType = (firstByte & 0x7E) >> 1; //0111 1110

                if (subframeType == 0)                      //000000
                {
                    subFrame = new FlacSubFrameConstant(reader, header, data, bitsPerSample);
                }
                else if (subframeType == 1) //000001
                {
                    subFrame = new FlacSubFrameVerbatim(reader, header, data, bitsPerSample);
                }
                else if ((subframeType & 0x20) != 0) //100000 = 0x20
                {
                    order    = (int)(subframeType & 0x1F) + 1;
                    subFrame = new FlacSubFrameLPC(reader, header, data, bitsPerSample, order);
                }
                else if ((subframeType & 0x08) != 0) //001000 = 0x08
                {
                    order = (int)(subframeType & 0x07);
                    if (order > 4)
                    {
                        return(null);
                    }
                    subFrame = new FlacSubFrameFixed(reader, header, data, bitsPerSample, order);
                }
                else
                {
                    Debug.WriteLine($"Invalid Flac-SubframeType. SubframeType: 0x{subframeType:x}.");
                    return(null);
                }

                if (hasWastedBits)
                {
                    var destination = data.DestinationBuffer.Span;
                    for (var i = 0; i < header.BlockSize; i++)
                    {
                        destination[i] <<= wastedBits;
                    }
                }

                return(subFrame);
            }
        }
Ejemplo n.º 2
0
        public static unsafe FlacSubFrameBase GetSubFrame(FlacBitReader reader, FlacSubFrameData data, FlacFrameHeader header, int bitsPerSample)
        {
            int wastedBits = 0, order;

            uint firstByte = reader.ReadBits(8);

            if ((firstByte & 0x80) != 0) //Zero bit padding, to prevent sync-fooling string of 1s
            {
                Debug.WriteLine("Flacdecoder subframe-header got no zero-bit padding.");
                return null;
            }

            bool hasWastedBits = (firstByte & 1) != 0; //Wasted bits-per-sample' flag
            if (hasWastedBits)
            {
                int k = (int)reader.ReadUnary();
                wastedBits = k + 1; //"k-1" follows -> add 1
                bitsPerSample -= wastedBits;
            }

            FlacSubFrameBase subFrame;
            var subframeType = (firstByte & 0x7E) >> 1; //0111 1110

            if (subframeType == 0) //000000
            {
                subFrame = new FlacSubFrameConstant(reader, header, data, bitsPerSample);
            }
            else if (subframeType == 1) //000001
            {
                subFrame = new FlacSubFrameVerbatim(reader, header, data, bitsPerSample);
            }
            else if ((subframeType & 0x08) != 0) //001000 = 0x08
            {
                order = (int) (subframeType & 0x07);
                subFrame = new FlacSubFrameFixed(reader, header, data, bitsPerSample, order);
            }
            else if ((subframeType & 0x20) != 0) //100000 = 0x20
            {
                order = (int) (subframeType & 0x1F) + 1;
                subFrame = new FlacSubFrameLPC(reader, header, data, bitsPerSample, order);
            }
            else
            {
                Debug.WriteLine(String.Format("Invalid Flac-SubframeType. SubframeType: 0x{0:x}.", subframeType));
                return null;
            }

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

            #if FLAC_DEBUG
            subFrame.WastedBits = wastedBits;
            #endif
            return subFrame;
        }
Ejemplo n.º 3
0
        public static unsafe FlacSubFrameBase GetSubFrame(FlacBitReader reader, FlacSubFrameData data, FlacFrameHeader header, int bps)
        {
            uint x;
            int wastedBits = 0, totalBits = 0, order = 0;

            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;
                totalBits = bps;
            }

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

            FlacSubFrameBase subFrame = null;

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

            if (subFrame != null)
                subFrame.WastedBits = wastedBits;
            else
                Debug.WriteLine("Unknown error while reading FlacSubFrameHeader");

            return subFrame;
        }
Ejemplo n.º 4
0
        public unsafe static FlacSubFrameBase GetSubFrame(FlacBitReader reader, FlacSubFrameData data, FlacFrameHeader header, int bitsPerSample)
        {
            int wastedBits = 0, order;

            uint firstByte = reader.ReadBits(8);

            if ((firstByte & 0x80) != 0) //Zero bit padding, to prevent sync-fooling string of 1s
            {
                Debug.WriteLine("Flacdecoder subframe-header got no zero-bit padding.");
                return(null);
            }

            bool hasWastedBits = (firstByte & 1) != 0; //Wasted bits-per-sample' flag

            if (hasWastedBits)
            {
                int k = (int)reader.ReadUnary();
                wastedBits     = k + 1; //"k-1" follows -> add 1
                bitsPerSample -= wastedBits;
            }

            FlacSubFrameBase subFrame;
            var subframeType = (firstByte & 0x7E) >> 1; //0111 1110

            if (subframeType == 0)                      //000000
            {
                subFrame = new FlacSubFrameConstant(reader, header, data, bitsPerSample);
            }
            else if (subframeType == 1) //000001
            {
                subFrame = new FlacSubFrameVerbatim(reader, header, data, bitsPerSample);
            }
            else if ((subframeType & 0x08) != 0) //001000 = 0x08
            {
                order    = (int)(subframeType & 0x07);
                subFrame = new FlacSubFrameFixed(reader, header, data, bitsPerSample, order);
            }
            else if ((subframeType & 0x20) != 0) //100000 = 0x20
            {
                order    = (int)(subframeType & 0x1F) + 1;
                subFrame = new FlacSubFrameLPC(reader, header, data, bitsPerSample, order);
            }
            else
            {
                Debug.WriteLine(String.Format("Invalid Flac-SubframeType. SubframeType: 0x{0:x}.", subframeType));
                return(null);
            }

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

#if FLAC_DEBUG
            subFrame.WastedBits = wastedBits;
#endif
            return(subFrame);
        }
Ejemplo n.º 5
0
        public unsafe static FlacSubFrameBase GetSubFrame(FlacBitReader reader, FlacSubFrameData data, FlacFrameHeader header, int bps)
        {
            uint x;
            int  wastedBits = 0, totalBits = 0, order = 0;

            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;
                totalBits  = bps;
            }

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

            FlacSubFrameBase subFrame = null;

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

            if (subFrame != null)
            {
                subFrame.WastedBits = wastedBits;
            }
            else
            {
                Debug.WriteLine("Unknown error while reading FlacSubFrameHeader");
            }

            return(subFrame);
        }