TSStreamBuffer.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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
  37. {
  38. get
  39. {
  40. return (long)BufferLength;
  41. }
  42. }
  43. public long Position
  44. {
  45. get
  46. {
  47. return Stream.Position;
  48. }
  49. }
  50. public void Add(
  51. byte[] buffer,
  52. int offset,
  53. int length)
  54. {
  55. TransferLength += length;
  56. if (BufferLength + length >= Buffer.Length)
  57. {
  58. length = Buffer.Length - BufferLength;
  59. }
  60. if (length > 0)
  61. {
  62. Array.Copy(buffer, offset, Buffer, BufferLength, length);
  63. BufferLength += length;
  64. }
  65. }
  66. public void Seek(
  67. long offset,
  68. SeekOrigin loc)
  69. {
  70. Stream.Seek(offset, loc);
  71. }
  72. public void Reset()
  73. {
  74. BufferLength = 0;
  75. TransferLength = 0;
  76. }
  77. public void BeginRead()
  78. {
  79. SkipBits = 0;
  80. Stream.Seek(0, SeekOrigin.Begin);
  81. }
  82. public void EndRead()
  83. {
  84. }
  85. public byte[] ReadBytes(int bytes)
  86. {
  87. if (Stream.Position + bytes >= BufferLength)
  88. {
  89. return null;
  90. }
  91. byte[] value = new byte[bytes];
  92. Stream.Read(value, 0, bytes);
  93. return value;
  94. }
  95. public byte ReadByte()
  96. {
  97. return (byte)Stream.ReadByte();
  98. }
  99. public int ReadBits(int bits)
  100. {
  101. long pos = Stream.Position;
  102. int shift = 24;
  103. int data = 0;
  104. for (int i = 0; i < 4; i++)
  105. {
  106. if (pos + i >= BufferLength) break;
  107. data += (Stream.ReadByte() << shift);
  108. shift -= 8;
  109. }
  110. BitVector32 vector = new BitVector32(data);
  111. int value = 0;
  112. for (int i = SkipBits; i < SkipBits + bits; i++)
  113. {
  114. value <<= 1;
  115. value += (vector[1 << (32 - i - 1)] ? 1 : 0);
  116. }
  117. SkipBits += bits;
  118. Stream.Seek(pos + (SkipBits >> 3), SeekOrigin.Begin);
  119. SkipBits = SkipBits % 8;
  120. return value;
  121. }
  122. }
  123. }