TSStreamBuffer.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. //============================================================================
  2. // BDInfo - Blu-ray Video and Audio Analysis Tool
  3. // Copyright © 2010 Cinema Squid
  4. //
  5. // This library is free software; you can redistribute it and/or
  6. // modify it under the terms of the GNU Lesser General Public
  7. // License as published by the Free Software Foundation; either
  8. // version 2.1 of the License, or (at your option) any later version.
  9. //
  10. // This library is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. // Lesser General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Lesser General Public
  16. // License along with this library; if not, write to the Free Software
  17. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. //=============================================================================
  19. using System;
  20. using System.Collections.Specialized;
  21. using System.IO;
  22. namespace BDInfo
  23. {
  24. public class TSStreamBuffer
  25. {
  26. private MemoryStream Stream = new MemoryStream();
  27. private int SkipBits = 0;
  28. private byte[] Buffer;
  29. private int BufferLength = 0;
  30. public int TransferLength = 0;
  31. public TSStreamBuffer()
  32. {
  33. Buffer = new byte[4096];
  34. Stream = new MemoryStream(Buffer);
  35. }
  36. public long Length => (long)BufferLength;
  37. public long Position => Stream.Position;
  38. public void Add(
  39. byte[] buffer,
  40. int offset,
  41. int length)
  42. {
  43. TransferLength += length;
  44. if (BufferLength + length >= Buffer.Length)
  45. {
  46. length = Buffer.Length - BufferLength;
  47. }
  48. if (length > 0)
  49. {
  50. Array.Copy(buffer, offset, Buffer, BufferLength, length);
  51. BufferLength += length;
  52. }
  53. }
  54. public void Seek(
  55. long offset,
  56. SeekOrigin loc)
  57. {
  58. Stream.Seek(offset, loc);
  59. }
  60. public void Reset()
  61. {
  62. BufferLength = 0;
  63. TransferLength = 0;
  64. }
  65. public void BeginRead()
  66. {
  67. SkipBits = 0;
  68. Stream.Seek(0, SeekOrigin.Begin);
  69. }
  70. public void EndRead()
  71. {
  72. }
  73. public byte[] ReadBytes(int bytes)
  74. {
  75. if (Stream.Position + bytes >= BufferLength)
  76. {
  77. return null;
  78. }
  79. byte[] value = new byte[bytes];
  80. Stream.Read(value, 0, bytes);
  81. return value;
  82. }
  83. public byte ReadByte()
  84. {
  85. return (byte)Stream.ReadByte();
  86. }
  87. public int ReadBits(int bits)
  88. {
  89. long pos = Stream.Position;
  90. int shift = 24;
  91. int data = 0;
  92. for (int i = 0; i < 4; i++)
  93. {
  94. if (pos + i >= BufferLength) break;
  95. data += (Stream.ReadByte() << shift);
  96. shift -= 8;
  97. }
  98. var vector = new BitVector32(data);
  99. int value = 0;
  100. for (int i = SkipBits; i < SkipBits + bits; i++)
  101. {
  102. value <<= 1;
  103. value += (vector[1 << (32 - i - 1)] ? 1 : 0);
  104. }
  105. SkipBits += bits;
  106. Stream.Seek(pos + (SkipBits >> 3), SeekOrigin.Begin);
  107. SkipBits = SkipBits % 8;
  108. return value;
  109. }
  110. }
  111. }