TrackFragmentHeaderBox.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. namespace MatrixIO.IO.Bmff.Boxes
  7. {
  8. /// <summary>
  9. /// Track Fragment Header Box ("tfhd")
  10. /// </summary>
  11. [Box("tfhd", "Track Fragment Header Box")]
  12. public class TrackFragmentHeaderBox : FullBox
  13. {
  14. public TrackFragmentHeaderBox() : base() { }
  15. public TrackFragmentHeaderBox(Stream stream) : base(stream) { }
  16. internal override ulong CalculateSize()
  17. {
  18. ulong calculatedSize = base.CalculateSize() + 4;
  19. if (BaseDataOffset.HasValue) calculatedSize += 8;
  20. if (SampleDescriptionIndex.HasValue) calculatedSize += 4;
  21. if (DefaultSampleDuration.HasValue) calculatedSize += 4;
  22. if (DefaultSampleSize.HasValue) calculatedSize += 4;
  23. if (DefaultSampleFlags != null) calculatedSize += 4;
  24. return calculatedSize;
  25. }
  26. protected override void LoadFromStream(Stream stream)
  27. {
  28. base.LoadFromStream(stream);
  29. TrackID = stream.ReadBEUInt32();
  30. if ((Flags & TrackFragmentFlags.BaseDataOffsetPresent) == TrackFragmentFlags.BaseDataOffsetPresent)
  31. BaseDataOffset = stream.ReadBEUInt64();
  32. if ((Flags & TrackFragmentFlags.SampleDrescriptionIndexPresent) == TrackFragmentFlags.SampleDrescriptionIndexPresent)
  33. SampleDescriptionIndex = stream.ReadBEUInt32();
  34. if ((Flags & TrackFragmentFlags.DefaultSampleDurationPresent) == TrackFragmentFlags.DefaultSampleDurationPresent)
  35. DefaultSampleDuration = stream.ReadBEUInt32();
  36. if ((Flags & TrackFragmentFlags.DefaultSampleSizePresent) == TrackFragmentFlags.DefaultSampleSizePresent)
  37. DefaultSampleSize = stream.ReadBEUInt32();
  38. if ((Flags & TrackFragmentFlags.DefaultSampleFlagsPresent) == TrackFragmentFlags.DefaultSampleFlagsPresent)
  39. DefaultSampleFlags = new SampleFlags(stream.ReadBEUInt32());
  40. if ((Flags & TrackFragmentFlags.DurationIsEmpty) == TrackFragmentFlags.DurationIsEmpty)
  41. DurationIsEmpty = true;
  42. }
  43. protected override void SaveToStream(Stream stream)
  44. {
  45. TrackFragmentFlags newFlags = 0;
  46. if (BaseDataOffset.HasValue) newFlags |= TrackFragmentFlags.BaseDataOffsetPresent;
  47. if (SampleDescriptionIndex.HasValue) newFlags |= TrackFragmentFlags.SampleDrescriptionIndexPresent;
  48. if (DefaultSampleDuration.HasValue) newFlags |= TrackFragmentFlags.DefaultSampleDurationPresent;
  49. if (DefaultSampleSize.HasValue) newFlags |= TrackFragmentFlags.DefaultSampleSizePresent;
  50. if (DefaultSampleFlags != null) newFlags |= TrackFragmentFlags.DefaultSampleFlagsPresent;
  51. if (DurationIsEmpty) newFlags |= TrackFragmentFlags.DurationIsEmpty;
  52. Flags = newFlags;
  53. base.SaveToStream(stream);
  54. stream.WriteBEUInt32(TrackID);
  55. if (BaseDataOffset.HasValue) stream.WriteBEUInt64(BaseDataOffset.Value);
  56. if (SampleDescriptionIndex.HasValue) stream.WriteBEUInt32(SampleDescriptionIndex.Value);
  57. if (DefaultSampleDuration.HasValue) stream.WriteBEUInt32(DefaultSampleDuration.Value);
  58. if (DefaultSampleSize.HasValue) stream.WriteBEUInt32(DefaultSampleSize.Value);
  59. if (DefaultSampleFlags!=null) stream.WriteBEUInt32(DefaultSampleFlags._flags);
  60. }
  61. private new TrackFragmentFlags Flags
  62. {
  63. get
  64. {
  65. return (TrackFragmentFlags)_Flags;
  66. }
  67. set
  68. {
  69. _Flags = (uint)value;
  70. }
  71. }
  72. public uint TrackID { get; set; }
  73. public ulong? BaseDataOffset { get; set; }
  74. public uint? SampleDescriptionIndex { get; set; }
  75. public uint? DefaultSampleDuration { get; set; }
  76. public uint? DefaultSampleSize { get; set; }
  77. public SampleFlags DefaultSampleFlags { get; set; }
  78. bool DurationIsEmpty { get; set; }
  79. [FlagsAttribute]
  80. public enum TrackFragmentFlags : int
  81. {
  82. BaseDataOffsetPresent = 0x000001,
  83. SampleDrescriptionIndexPresent = 0x000002,
  84. DefaultSampleDurationPresent = 0x000008,
  85. DefaultSampleSizePresent = 0x000010,
  86. DefaultSampleFlagsPresent = 0x000020,
  87. DurationIsEmpty = 0x010000,
  88. }
  89. }
  90. }