TrackHeaderBox.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using System.Collections;
  7. namespace MatrixIO.IO.Bmff.Boxes
  8. {
  9. /// <summary>
  10. /// Track Header Box ('tkhd')
  11. /// </summary>
  12. [Box("tkhd", "Track Header Box")]
  13. public class TrackHeaderBox : FullBox
  14. {
  15. public TrackHeaderBox() : base() { }
  16. public TrackHeaderBox(Stream stream) : base(stream) { }
  17. internal override ulong CalculateSize()
  18. {
  19. return base.CalculateSize() +
  20. (ulong)(Version == 1 ? 8 + 8 + 4 + 4 + 8 : 4 + 4 + 4 + 4 + 4) + (2 * 4) + 2 + 2 + 2 + 2 + (9 * 4) + 4 + 4;
  21. }
  22. protected override void LoadFromStream(Stream stream)
  23. {
  24. base.LoadFromStream(stream);
  25. if (Version == 1)
  26. {
  27. CreationTime = MovieHeaderBox.Convert1904Time(stream.ReadBEUInt64());
  28. ModificationTime = MovieHeaderBox.Convert1904Time(stream.ReadBEUInt64());
  29. TrackID = stream.ReadBEUInt32();
  30. _Reserved1 = stream.ReadBEUInt32();
  31. Duration = stream.ReadBEUInt64();
  32. }
  33. else // if (Version == 0)
  34. {
  35. CreationTime = MovieHeaderBox.Convert1904Time(stream.ReadBEUInt32());
  36. ModificationTime = MovieHeaderBox.Convert1904Time(stream.ReadBEUInt32());
  37. TrackID = stream.ReadBEUInt32();
  38. _Reserved1 = stream.ReadBEUInt32();
  39. Duration = stream.ReadBEUInt32();
  40. }
  41. for (int i = 0; i < 2; i++) _Reserved2[0] = stream.ReadBEUInt32();
  42. Layer = stream.ReadBEInt16();
  43. AlternateGroup = stream.ReadBEInt16();
  44. Volume = stream.ReadBEInt16();
  45. _Reserved3 = stream.ReadBEUInt16();
  46. for (int i = 0; i < 9; i++) _Matrix[i] = stream.ReadBEInt32();
  47. Width = stream.ReadBEUInt32();
  48. Height = stream.ReadBEUInt32();
  49. }
  50. protected override void SaveToStream(Stream stream)
  51. {
  52. base.SaveToStream(stream);
  53. if (Version == 1)
  54. {
  55. stream.WriteBEUInt64(MovieHeaderBox.Convert1904Time(CreationTime));
  56. stream.WriteBEUInt64(MovieHeaderBox.Convert1904Time(ModificationTime));
  57. stream.WriteBEUInt32(TrackID);
  58. stream.WriteBEUInt32(_Reserved1);
  59. stream.WriteBEUInt64(Duration);
  60. }
  61. else // if (Version == 0)
  62. {
  63. stream.WriteBEUInt32((uint)MovieHeaderBox.Convert1904Time(CreationTime));
  64. stream.WriteBEUInt32((uint)MovieHeaderBox.Convert1904Time(ModificationTime));
  65. stream.WriteBEUInt32(TrackID);
  66. stream.WriteBEUInt32(_Reserved1);
  67. stream.WriteBEUInt32((uint)Duration);
  68. }
  69. for(int i=0; i<2; i++) stream.WriteBEUInt32(_Reserved2[i]);
  70. stream.WriteBEInt16(Layer);
  71. stream.WriteBEInt16(AlternateGroup);
  72. stream.WriteBEInt16(Volume);
  73. stream.WriteBEUInt16(_Reserved3);
  74. for (int i = 0; i < 9; i++) stream.WriteBEInt32(_Matrix[i]);
  75. stream.WriteBEUInt32(Width);
  76. stream.WriteBEUInt32(Height);
  77. }
  78. public DateTime CreationTime { get; set; }
  79. public DateTime ModificationTime { get; set; }
  80. public uint TrackID { get; set; }
  81. private uint _Reserved1;
  82. public ulong Duration { get; set; }
  83. private uint[] _Reserved2 = new uint[2];
  84. public short Layer { get; set; }
  85. public short AlternateGroup { get; set; }
  86. public short Volume { get; set; }
  87. private ushort _Reserved3;
  88. private int[] _Matrix = new int[9] { 0x00010000, 0, 0, 0, 0x00010000, 0, 0, 0, 0x40000000 }; // Unity Matrix
  89. public int[] Matrix { get { return _Matrix; } }
  90. public uint Width { get; set; }
  91. public uint Height { get; set; }
  92. }
  93. }