BaseMediaFile.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using MatrixIO.IO.Bmff;
  6. using System.IO;
  7. using MatrixIO.IO.Bmff.Boxes;
  8. using System.Diagnostics;
  9. using System.IO.Compression;
  10. using System.Collections.ObjectModel;
  11. namespace BmffViewer
  12. {
  13. public class BaseMediaFile : BaseMedia, IMediaFile, IDisposable
  14. {
  15. protected FileInfo _FileInfo;
  16. public string Name { get { return _FileInfo.Name; } }
  17. public string NameWithoutExtension { get { return System.IO.Path.GetFileNameWithoutExtension(FullName); } }
  18. public string FullName { get { return _FileInfo.FullName; } }
  19. public string Extension { get { return _FileInfo.Extension; } }
  20. public string DirectoryName { get { return _FileInfo.DirectoryName; } }
  21. public Stream SourceStream { get { return base._SourceStream; } }
  22. private readonly bool DisposeSourceStream = false;
  23. protected BaseMediaFile() { }
  24. public BaseMediaFile(string name) : this(new FileInfo(name)) { }
  25. public BaseMediaFile(FileInfo fileInfo) : this(fileInfo.OpenRead())
  26. {
  27. _FileInfo = fileInfo;
  28. DisposeSourceStream = true;
  29. }
  30. public BaseMediaFile(FileStream stream)
  31. : base(new BufferedStream(stream))
  32. {
  33. if(_FileInfo == null)
  34. _FileInfo = new FileInfo(stream.Name);
  35. }
  36. public void SaveAs(string path)
  37. {
  38. using (FileStream stream = File.Open(path, FileMode.Create))
  39. {
  40. SaveTo(stream);
  41. stream.Close();
  42. }
  43. }
  44. public bool DecompressMovieHeader()
  45. {
  46. MovieBox moov = this.Children.OfType<MovieBox>().SingleOrDefault();
  47. if (moov == null) return false;
  48. CompressedMovieBox cmov = moov.Children.OfType<CompressedMovieBox>().SingleOrDefault();
  49. if (cmov == null) return false;
  50. DataCompressionBox dcom = cmov.Children.OfType<DataCompressionBox>().SingleOrDefault();
  51. if (dcom == null) return false;
  52. CompressedMovieDataBox cmvd = cmov.Children.OfType<CompressedMovieDataBox>().SingleOrDefault();
  53. if (cmvd == null) return false;
  54. if (dcom.Format != "zlib") throw new FileFormatException("Unsupported compression type: " + dcom.Format);
  55. Stream contentStream = cmvd.GetContentStream();
  56. contentStream.ReadByte();
  57. contentStream.ReadByte();
  58. Stream compressedStream = new DeflateStream(contentStream, CompressionMode.Decompress, true);
  59. MemoryStream uncompressedStream = new MemoryStream();
  60. compressedStream.CopyTo(uncompressedStream);
  61. uncompressedStream.Flush();
  62. compressedStream.Dispose();
  63. uncompressedStream.Seek(0, SeekOrigin.Begin);
  64. BaseMedia uncompressedMoov = new BaseMedia(uncompressedStream);
  65. MovieBox newMoov = uncompressedMoov.Children.OfType<MovieBox>().SingleOrDefault();
  66. if (newMoov != null)
  67. {
  68. int insertionPoint = Children.IndexOf(moov);
  69. Children.Remove(moov);
  70. FreeSpaceBox free = new FreeSpaceBox(moov.EffectiveSize);
  71. ((ObservableCollection<Box>)Children).Insert(insertionPoint, free);
  72. ((ObservableCollection<Box>)Children).Add(newMoov);
  73. }
  74. return true;
  75. }
  76. public bool FastStart(string path, bool removeFreeSpace = true)
  77. {
  78. using (BaseMediaFile clone = new BaseMediaFile(this._FileInfo))
  79. {
  80. clone.DecompressMovieHeader();
  81. MovieBox moov = clone.Children.OfType<MovieBox>().SingleOrDefault();
  82. if (moov == null) return false;
  83. MovieDataBox mdat = clone.Children.OfType<MovieDataBox>().SingleOrDefault();
  84. if (mdat == null) return false;
  85. clone.Children.Remove(moov);
  86. clone.Children.Insert(clone.Children.IndexOf(mdat), moov);
  87. if (removeFreeSpace)
  88. {
  89. Box[] freeSpaceBoxes = clone.Children.Where((box) => { return box.Type == "free" || box.Type == "skip" || box.Type == "junk" || box.Type == "wide"; }).ToArray();
  90. foreach (Box free in freeSpaceBoxes)
  91. {
  92. clone.Children.Remove(free);
  93. Debug.WriteLine("Removing '" + free + "'");
  94. }
  95. }
  96. Debug.WriteLine("Calculating new 'mdat' offset.");
  97. ulong mdatOffset = 0;
  98. foreach (Box box in clone.Children)
  99. {
  100. if (box is MovieDataBox) break;
  101. else mdatOffset += GetBoxWriteSize(box);
  102. }
  103. bool isPositiveAdjustment = mdatOffset > mdat.Offset.Value;
  104. ulong chunkAdjustment = mdatOffset > mdat.Offset.Value ? mdatOffset - mdat.Offset.Value : mdat.Offset.Value - mdatOffset;
  105. Debug.WriteLine("Looking for 'stco' and 'co64' boxes that need to be updated.");
  106. var traks = moov.Children.OfType<TrackBox>();
  107. foreach (TrackBox track in traks)
  108. {
  109. MediaBox mdia = track.Children.OfType<MediaBox>().SingleOrDefault();
  110. if (mdia != null)
  111. {
  112. MediaInformationBox minf = mdia.Children.OfType<MediaInformationBox>().SingleOrDefault();
  113. if (minf != null)
  114. {
  115. SampleTableBox stbl = minf.Children.OfType<SampleTableBox>().Single();
  116. if (stbl != null)
  117. {
  118. Debug.WriteLine("Updating 'stco' offsets.");
  119. bool upgradeToChunkLongOffsetBox = false;
  120. ChunkOffsetBox stco = stbl.Children.OfType<ChunkOffsetBox>().SingleOrDefault();
  121. if (stco != null)
  122. {
  123. for (int i = 0; i < stco.Entries.Count; i++)
  124. {
  125. try
  126. {
  127. if (isPositiveAdjustment)
  128. stco.Entries[i] = checked((uint)((ulong)stco.Entries[i] + chunkAdjustment));
  129. else
  130. stco.Entries[i] = checked((uint)((ulong)stco.Entries[i] - chunkAdjustment));
  131. }
  132. catch (OverflowException)
  133. {
  134. upgradeToChunkLongOffsetBox = true;
  135. break;
  136. }
  137. }
  138. }
  139. if (upgradeToChunkLongOffsetBox)
  140. {
  141. Debug.WriteLine("Overflow encountered. Converting 'stco' to 'co64' to compensate.");
  142. // TODO: Copy and adjust stco values
  143. }
  144. ChunkLargeOffsetBox co64 = stbl.Children.OfType<ChunkLargeOffsetBox>().SingleOrDefault();
  145. if (co64 != null)
  146. {
  147. Debug.WriteLine("Updating 'co64' offsets.");
  148. for (int i = 0; i < co64.Entries.Count; i++)
  149. {
  150. if (isPositiveAdjustment)
  151. co64.Entries[i] = checked(co64.Entries[i] + chunkAdjustment);
  152. else
  153. co64.Entries[i] = checked(co64.Entries[i] - chunkAdjustment);
  154. }
  155. }
  156. }
  157. }
  158. }
  159. }
  160. clone.SaveAs(path);
  161. return true;
  162. }
  163. }
  164. public void Close()
  165. {
  166. SourceStream.Close();
  167. }
  168. public override string ToString()
  169. {
  170. return Name;
  171. }
  172. public void Dispose()
  173. {
  174. if(DisposeSourceStream) SourceStream.Dispose();
  175. }
  176. }
  177. }