TSStreamBuffer.cs 3.8 KB

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