Exemplo n.º 1
0
        public override void BuildTree(BitStream m)
        {
            //Read tag
            int tag = m.ReadBits(1);

            //If tag is zero, finish
            if (tag == 0)
            {
                return;
            }
            lowByteTree = new Huffmantree();
            lowByteTree.BuildTree(m);


            highByteTree = new Huffmantree();
            highByteTree.BuildTree(m);


            iMarker1 = m.ReadBits(16);
            //System.Console.WriteLine("M1:" + iMarker1);
            iMarker2 = m.ReadBits(16);
            //System.Console.WriteLine("M2:" + iMarker2);
            iMarker3 = m.ReadBits(16);
            //System.Console.WriteLine("M3:" + iMarker3);
            RootNode = new Node();
            BuildTree(m, RootNode);

            //For some reason we have to skip a bit
            m.ReadBits(1);

            if (marker1 == null)
            {
                // System.Console.WriteLine("Not using marker 1");
                marker1 = new Node();
            }
            if (marker2 == null)
            {
                //  System.Console.WriteLine("Not using marker 2");
                marker2 = new Node();
            }
            if (marker3 == null)
            {
                //   System.Console.WriteLine("Not using marker 3");
                marker3 = new Node();
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Reads the next frame.
        /// </summary>            
        public void ReadNextFrame()
        {
            uint mask = 1;

            if (CurrentFrame >= File.Header.NbFrames)
                throw new EndOfStreamException("No more frames");

            long currentPos = File.Stream.Position;

            //If this frame has a palette record
            if ((File.FrameTypes[CurrentFrame] & mask) > 0)
            {
                //Update the palette
                UpdatePalette();
            }

            //Sound data
            mask <<= 1;
            for (int i = 0; i < 7; i++, mask <<= 1)
            {
                if ((file.FrameTypes[CurrentFrame] & mask) > 0)
                {
                    long pos = File.Stream.Position;
                    uint length = Util.ReadDWord(File.Stream);

                    //We assume compression, if not, well too bad
                    uint unpackedLength = Util.ReadDWord(File.Stream);
                    BitStream m = new BitStream(File.Stream);
                    if (m.ReadBits(1) != 0) //Audio present
                    {
                        bool stereo = m.ReadBits(1) > 0;
                        bool is16Bit = m.ReadBits(1) > 0;

                        //Next are some trees
                        uint nbTrees = 1;
                        if (stereo)
                            nbTrees <<= 1;
                        if (is16Bit)
                            nbTrees <<= 1;
                        Huffmantree[] tree = new Huffmantree[nbTrees];
                        byte[] audioData = new byte[unpackedLength + 4];
                        uint audioDataIndex = 0;
                        for (int k = 0; k < nbTrees; k++)
                        {
                            tree[k] = new Huffmantree();
                            tree[k].BuildTree(m);
                        }

                        int res;
                        if (is16Bit)
                        {
                            Int16 rightBaseMSB = 0, rightBaseLSB = 0, leftBaseMSB = 0, leftBaseLSB = 0;
                            rightBaseMSB = (Int16)(m.ReadBits(8));
                            rightBaseLSB = (Int16)(m.ReadBits(8));
                            //Add sample (little endian)
                            audioData[audioDataIndex++] = (byte)rightBaseLSB; //Lower byte
                            audioData[audioDataIndex++] = (byte)rightBaseMSB; //Higher byte
                            if (stereo)
                            {
                                leftBaseMSB = (Int16)(m.ReadBits(8));
                                leftBaseLSB = (Int16)(m.ReadBits(8));
                                //Add sample (little endian)
                                audioData[audioDataIndex++] = (byte)leftBaseLSB; //Lower byte
                                audioData[audioDataIndex++] = (byte)leftBaseMSB; //Higher byte
                            }

                            for (int l = 0; l < unpackedLength / 2; l++)
                            {
                                if ((l & ((stereo) ? 1 : 0)) > 0)
                                {
                                    res = tree[2].Decode(m);
                                    leftBaseLSB += (Int16)res;
                                    res = tree[3].Decode(m);
                                    leftBaseMSB += (Int16)res;
                                    leftBaseMSB += (Int16)(leftBaseLSB >> 8);
                                    leftBaseLSB &= 0xFF;

                                    //Add sample (little endian)
                                    audioData[audioDataIndex++] = (byte)leftBaseLSB; //Lower byte
                                    audioData[audioDataIndex++] = (byte)leftBaseMSB; //Higher byte
                                }
                                else
                                {
                                    res = tree[0].Decode(m);
                                    rightBaseLSB += (Int16)res;
                                    res = tree[1].Decode(m);
                                    rightBaseMSB += (Int16)res;
                                    rightBaseMSB += (Int16)(rightBaseLSB >> 8);
                                    rightBaseLSB &= 0xFF;

                                    //Add sample (little endian)
                                    audioData[audioDataIndex++] = (byte)rightBaseLSB; //Lower byte
                                    audioData[audioDataIndex++] = (byte)rightBaseMSB; //Higher byte
                                }
                            }
                        }
                        else
                        {
                            byte rightBase = (byte)m.ReadBits(8), leftBase = 0;

                            //Add sample
                            audioData[audioDataIndex++] = rightBase;

                            if (stereo)
                            {
                                leftBase = (byte)m.ReadBits(8);
                                //Add sample
                                audioData[audioDataIndex++] = leftBase;
                            }

                            for (int l = 0; l < unpackedLength; l++)
                            {
                                if ((l & ((stereo) ? 1 : 0)) > 0)
                                {
                                    leftBase += (byte)tree[1].Decode(m);
                                    //Add sample
                                    audioData[audioDataIndex++] = leftBase;
                                }
                                else
                                {
                                    rightBase += (byte)tree[0].Decode(m);
                                    //Add sample
                                    audioData[audioDataIndex++] = rightBase;
                                }
                            }
                        }
                        lastAudioData[i] = audioData;
                    }

                    File.Stream.Seek(pos + (long)length, SeekOrigin.Begin);
                }
            }

            //Video data
            try
            {
                DecodeVideo();
            }
            catch (IOException exc)
            {
                Console.WriteLine("Exception caught while decoding frame:" + exc.ToString());
            }

            //Seek to the next frame
            File.Stream.Seek(currentPos + File.FrameSizes[CurrentFrame], SeekOrigin.Begin);
            CurrentFrame++;
        }
Exemplo n.º 3
0
        public override void BuildTree(BitStream m)
        {
            //Read tag
            int tag = m.ReadBits(1);
            //If tag is zero, finish
            if (tag == 0) return;
            lowByteTree = new Huffmantree();
            lowByteTree.BuildTree(m);
 

            highByteTree = new Huffmantree();
            highByteTree.BuildTree(m);

           
            iMarker1 = m.ReadBits(16);
            //System.Console.WriteLine("M1:" + iMarker1);
            iMarker2 = m.ReadBits(16);
            //System.Console.WriteLine("M2:" + iMarker2);
            iMarker3 = m.ReadBits(16);
            //System.Console.WriteLine("M3:" + iMarker3);
            RootNode = new Node();
            BuildTree(m, RootNode);

            //For some reason we have to skip a bit
            m.ReadBits(1);

            if (marker1 == null)
	    {
               // System.Console.WriteLine("Not using marker 1");
                marker1 = new Node();
	    }
            if (marker2 == null)
            {
              //  System.Console.WriteLine("Not using marker 2");
                marker2 = new Node();
            }
            if (marker3 == null)
            {
             //   System.Console.WriteLine("Not using marker 3");
                marker3 = new Node();
	    }

        }
Exemplo n.º 4
0
        /// <summary>
        /// Reads the next frame.
        /// </summary>
        public void ReadNextFrame()
        {
            uint mask = 1;

            if (CurrentFrame >= File.Header.NbFrames)
            {
                throw new EndOfStreamException("No more frames");
            }

            long currentPos = File.Stream.Position;

            //If this frame has a palette record
            if ((File.FrameTypes[CurrentFrame] & mask) > 0)
            {
                //Update the palette
                UpdatePalette();
            }

            //Sound data
            mask <<= 1;
            for (int i = 0; i < 7; i++, mask <<= 1)
            {
                if ((file.FrameTypes[CurrentFrame] & mask) > 0)
                {
                    long pos    = File.Stream.Position;
                    uint length = Util.ReadDWord(File.Stream);

                    //We assume compression, if not, well too bad
                    uint      unpackedLength = Util.ReadDWord(File.Stream);
                    BitStream m = new BitStream(File.Stream);
                    if (m.ReadBits(1) != 0) //Audio present
                    {
                        bool stereo  = m.ReadBits(1) > 0;
                        bool is16Bit = m.ReadBits(1) > 0;

                        //Next are some trees
                        uint nbTrees = 1;
                        if (stereo)
                        {
                            nbTrees <<= 1;
                        }
                        if (is16Bit)
                        {
                            nbTrees <<= 1;
                        }
                        Huffmantree[] tree           = new Huffmantree[nbTrees];
                        byte[]        audioData      = new byte[unpackedLength + 4];
                        uint          audioDataIndex = 0;
                        for (int k = 0; k < nbTrees; k++)
                        {
                            tree[k] = new Huffmantree();
                            tree[k].BuildTree(m);
                        }

                        int res;
                        if (is16Bit)
                        {
                            Int16 rightBaseMSB = 0, rightBaseLSB = 0, leftBaseMSB = 0, leftBaseLSB = 0;
                            rightBaseMSB = (Int16)(m.ReadBits(8));
                            rightBaseLSB = (Int16)(m.ReadBits(8));
                            //Add sample (little endian)
                            audioData[audioDataIndex++] = (byte)rightBaseLSB; //Lower byte
                            audioData[audioDataIndex++] = (byte)rightBaseMSB; //Higher byte
                            if (stereo)
                            {
                                leftBaseMSB = (Int16)(m.ReadBits(8));
                                leftBaseLSB = (Int16)(m.ReadBits(8));
                                //Add sample (little endian)
                                audioData[audioDataIndex++] = (byte)leftBaseLSB; //Lower byte
                                audioData[audioDataIndex++] = (byte)leftBaseMSB; //Higher byte
                            }

                            for (int l = 0; l < unpackedLength / 2; l++)
                            {
                                if ((l & ((stereo) ? 1 : 0)) > 0)
                                {
                                    res          = tree[2].Decode(m);
                                    leftBaseLSB += (Int16)res;
                                    res          = tree[3].Decode(m);
                                    leftBaseMSB += (Int16)res;
                                    leftBaseMSB += (Int16)(leftBaseLSB >> 8);
                                    leftBaseLSB &= 0xFF;

                                    //Add sample (little endian)
                                    audioData[audioDataIndex++] = (byte)leftBaseLSB; //Lower byte
                                    audioData[audioDataIndex++] = (byte)leftBaseMSB; //Higher byte
                                }
                                else
                                {
                                    res           = tree[0].Decode(m);
                                    rightBaseLSB += (Int16)res;
                                    res           = tree[1].Decode(m);
                                    rightBaseMSB += (Int16)res;
                                    rightBaseMSB += (Int16)(rightBaseLSB >> 8);
                                    rightBaseLSB &= 0xFF;

                                    //Add sample (little endian)
                                    audioData[audioDataIndex++] = (byte)rightBaseLSB; //Lower byte
                                    audioData[audioDataIndex++] = (byte)rightBaseMSB; //Higher byte
                                }
                            }
                        }
                        else
                        {
                            byte rightBase = (byte)m.ReadBits(8), leftBase = 0;

                            //Add sample
                            audioData[audioDataIndex++] = rightBase;

                            if (stereo)
                            {
                                leftBase = (byte)m.ReadBits(8);
                                //Add sample
                                audioData[audioDataIndex++] = leftBase;
                            }

                            for (int l = 0; l < unpackedLength; l++)
                            {
                                if ((l & ((stereo) ? 1 : 0)) > 0)
                                {
                                    leftBase += (byte)tree[1].Decode(m);
                                    //Add sample
                                    audioData[audioDataIndex++] = leftBase;
                                }
                                else
                                {
                                    rightBase += (byte)tree[0].Decode(m);
                                    //Add sample
                                    audioData[audioDataIndex++] = rightBase;
                                }
                            }
                        }
                        lastAudioData[i] = audioData;
                    }

                    File.Stream.Seek(pos + (long)length, SeekOrigin.Begin);
                }
            }

            //Video data
            try
            {
                DecodeVideo();
            }
            catch (IOException exc)
            {
                Console.WriteLine("Exception caught while decoding frame:" + exc.ToString());
            }

            //Seek to the next frame
            File.Stream.Seek(currentPos + File.FrameSizes[CurrentFrame], SeekOrigin.Begin);
            CurrentFrame++;
        }