public bool NextNalUnit()
        {
            // FIXME: 00 00 01 xx -OF- 00 00 00 01 xx voor volgende NAL unit (extra (0) hoort dus bij volgende NAL unit)

            // 00000000 00000000 00000001 0xxxxxxx
            long startPosition = _dataReader.Position;
            uint startCode     = _dataReader.NextStartCode(25, 0x0000002, 7);

            if (_dataReader.Position > (startPosition + _maxNalUnitDistance))
            {
                Result.Invalidate();
                return(false);
            }
            if (startCode != 0)
            {
                return(true);
            }
            if (_dataReader.Position == _dataReader.Length)
            {
                return(true);
            }

            return(false);            // This is unlikely to occur, but should return 'false'
        }
        private IStreamParser FindNalUnit(bool firstHeader)
        {
            bool detectByteStreamFormat    = firstHeader || (_streamParser == _byteStreamParser);
            bool detectNalUnitStreamFormat = firstHeader || (_streamParser == _nalUnitStreamParser);

            while (_dataReader.State == DataReaderState.Ready)
            {
                // Note: This assumes that a valid NAL unit is less than 1MB in size.
                uint startCode      = _dataReader.NextStartCode(9, 0, 23);
                long bytesRemaining = (_dataReader.Length - _dataReader.Position);
                if (bytesRemaining < 4)
                {
                    _dataReader.Position = _dataReader.Length;
                    break;
                }

                // Check for 'byte stream' format with '00 00 01' start code prefix
                if (detectByteStreamFormat)
                {
                    if (((startCode & ~0xff) == 0x00000100) && NalUnitStartByteByteStream[startCode & 0xff])
                    {
                        return(_byteStreamParser);
                    }
                }

                // Check for 'NAL unit stream' format: "00 LL xx", where 'LL' is the length prefix and 'xx' is the NAL unit byte
                if (detectNalUnitStreamFormat)
                {
                    if (_nalUnitStreamParser.IsShortLengthPrefixedNalUnit(startCode >> 8, bytesRemaining))
                    {
                        // Note: The data should not contain illegal sequences of '00 00 0x xx',
                        //       other than start code prefix emulation prevention sequences.
                        return(_nalUnitStreamParser);
                    }
                }

                // Advance to next position, i.e. the NAL unit byte
                _dataReader.Position++;

                // Check for 'byte stream' format with '00 00 00 01' start code prefix
                var nalUnitByte = (byte)(_dataReader.ShowBits(32) & 0xff);
                if (detectByteStreamFormat)
                {
                    if ((startCode == 0x00000001) && NalUnitStartByteSequenceParameterSet[nalUnitByte])
                    {
                        _dataReader.Position--;
                        return(_byteStreamParser);
                    }
                }

                // Check for 'NAL unit stream' format: "00 LL LL LL xx", where 'LLLLLL' is the length prefix and 'xx' is the NAL unit byte
                if (detectNalUnitStreamFormat)
                {
                    ulong nextFiveBytes = ((ulong)startCode << 8) | nalUnitByte;
                    if (_nalUnitStreamParser.IsLongLengthPrefixedNalUnit(nextFiveBytes, bytesRemaining))
                    {
                        // TODO: The data should not contain illegal sequences of '00 00 0x xx',
                        //       other than start code prefix emulation prevention sequences.

                        _dataReader.Position--;
                        return(_nalUnitStreamParser);
                    }
                }
            }

            // No more NAL units!
            return(null);
        }
 public uint NextStartCode()
 {
     // Start code format is 23 bits (0), 1 bit (1), 8 bits (id)
     return(_dataReader.NextStartCode(24, 0x000001, 8));
 }