示例#1
0
        private void SeekHeader()
        {
            byte[] buffer = new byte[4];
            do
            {
                // read 4 bytes (possible header)
                int read = source.Read(buffer, 0, 4);

                // less than 4 bytes read -> EOF
                if(read < 4)
                {
                    break;
                }

                // check for typical header strucure
                if(SyncCheck(buffer))
                {
                    // load the header bytes into the header class
                    Header head = new Header(buffer, (source.Position-4));

                    // extract header information and do another check for invalid values
                    if(head.Init())
                    {
                        // if frame contains CRC bytes, copy them to the header
                        if(head.crc)
                        {
                            source.Read(head.crcbytes, 0, 2);
                            source.Seek(-2, SeekOrigin.Current);
                        }

                        // check if the frame length matches the header data
                        byte[] syncbytes = new byte[4];
                        long currentpos = source.Position;
                        source.Seek(head.NextFrame(), SeekOrigin.Begin);
                        source.Read(syncbytes, 0, 4);
                        source.Seek(-1, SeekOrigin.Current);
                        // do we have another header?
                        if(SyncCheck(syncbytes))
                        {
                            // first header?
                            if(headerstream.Count <= 0)
                            {
                                if(SyncCompare(buffer, syncbytes)) // check if MPEG Version / Layer matches those of the next header (to reduce chance to catch noise)
                                {
                                    comparebuffer = (byte[])buffer.Clone();

                                    // we have found a frame!
                                    headerstream.Add(head);
                                    Console.WriteLine(head.ToString());
                                    //break;
                                }
                                else
                                {
                                    // resume search where we left
                                    source.Seek(currentpos, SeekOrigin.Begin);
                                }

                            }
                            else  // not the first header, so we don't do more checks to save some time
                            {
                                // we have found a frame!
                                headerstream.Add(head);
                                //Console.WriteLine(head.ToString());
                            }
                        }

                    }
                }

                // move 1 byte per cycle (instead of 4)
                source.Seek(-3, SeekOrigin.Current);

            } while (true);

            Console.WriteLine("Frames: " + headerstream.Count.ToString());

            if(headerstream.Count <= 0)
            {
                throw new Exception("Could not find MPEG headers");
            }
        }