public virtual void AddSample(Mp4Stream dataStream, ulong offset, uint size, uint duration, uint descriptionIndex, ulong dts, uint ctsDelta, bool sync) { if (this.samples.Count > 0) { Mp4Sample sample = this.samples[this.samples.Count - 1]; if (dts == 0L) { if (sample.Duration == 0) { throw new Exception("INVALID_PARAMETERS"); } dts = sample.Dts + sample.Duration; } else if (sample.Duration == 0) { if (dts <= sample.Dts) { throw new Exception("INVALID_PARAMETERS"); } sample.Duration = (uint)(dts - sample.Dts); } else if (dts != (sample.Dts + sample.Duration)) { throw new Exception("INVALID_PARAMETERS"); } } Mp4Sample item = new Mp4Sample(dataStream, (long)offset, (int)size, duration, descriptionIndex, dts, ctsDelta, sync); this.samples.Add(item); }
public virtual Mp4ContainerBox GenerateStblAtom() { Mp4ContainerBox box = new Mp4ContainerBox(8, Mp4BoxType.STBL); Mp4StsdBox child = new Mp4StsdBox(this); Mp4StszBox box3 = new Mp4StszBox(); Mp4StscBox box4 = new Mp4StscBox(); Mp4SttsBox box5 = new Mp4SttsBox(); Mp4StssBox box6 = new Mp4StssBox(); Mp4CttsBox box7 = null; uint num = 0; uint num2 = 0; ulong item = 0L; uint samplesPerChunk = 0; uint descriptionIndex = 0; uint sampleDelta = 0; uint sampleCount = 0; uint sampleOffset = 0; uint num9 = 0; List <ulong> list = new List <ulong>(); bool flag = false; int num10 = this.SampleCount; for (int i = 0; i < num10; i++) { Mp4Sample sample = this.GetSample(i); uint duration = sample.Duration; if ((duration != sampleDelta) && (sampleCount != 0)) { box5.Entries.Add(new Mp4SttsEntry(sampleCount, sampleDelta)); sampleCount = 0; } sampleCount++; sampleDelta = duration; uint ctsDelta = sample.CtsDelta; if ((ctsDelta != sampleOffset) && (num9 != 0)) { if (box7 == null) { box7 = new Mp4CttsBox(); } box7.Entries.Add(new Mp4CttsEntry(num9, sampleOffset)); num9 = 0; } num9++; sampleOffset = ctsDelta; descriptionIndex = sample.DescriptionIndex; box3.Entries.Add((uint)sample.Size); if (sample.IsSync) { box6.Entries.Add((uint)(i + 1)); if (i == 0) { flag = true; } } else { flag = false; } uint chunkIndex = 0; uint positionInChunk = 0; this.GetSampleChunkPosition((uint)i, out chunkIndex, out positionInChunk); if ((chunkIndex != num) && (samplesPerChunk != 0)) { list.Add(item); item += num2; box4.Entries.Add(new Mp4StscEntry(1, samplesPerChunk, descriptionIndex + 1)); samplesPerChunk = 0; num2 = 0; } num = chunkIndex; num2 += (uint)sample.Size; samplesPerChunk++; } box5.Entries.Add(new Mp4SttsEntry(sampleCount, sampleDelta)); if (box7 != null) { if (num9 == 0) { throw new Exception("ASSERT(current_cts_delta_run != 0)"); } box7.Entries.Add(new Mp4CttsEntry(num9, sampleOffset)); } if (samplesPerChunk != 0) { list.Add(item); box4.Entries.Add(new Mp4StscEntry(1, samplesPerChunk, descriptionIndex + 1)); } box.AddChild(child); box.AddChild(box3); box.AddChild(box4); box.AddChild(box5); if (box7 != null) { box.AddChild(box7); } if (!flag && (box6.Entries.Count != 0)) { box.AddChild(box6); } uint count = (uint)list.Count; if (item <= 0xffffffffL) { uint[] entries = new uint[count]; for (uint j = 0; j < count; j++) { entries[j] = (uint)list[(int)j]; } Mp4StcoBox box8 = new Mp4StcoBox(entries); box.AddChild(box8); return(box); } Mp4Co64Box box9 = new Mp4Co64Box(list.ToArray()); box.AddChild(box9); return(box); }
public override Mp4Sample GetSample(int index) { ulong chunkOffset; Mp4Sample sample = new Mp4Sample(); if ((this.StcoAtom == null) && (this.Co64Atom == null)) { throw new Exception("INVALID_FORMAT"); } index++; this.StscAtom.GetChunkForSample((uint)index, out uint num, out uint num2, out uint num3); if (num2 > index) { throw new Exception("ERROR_INTERNAL"); } try { if (this.StcoAtom != null) { chunkOffset = this.StcoAtom.GetChunkOffset(num); } else { chunkOffset = this.Co64Atom.GetChunkOffset((int)num); } } catch { return(null); } for (uint i = ((uint)index) - num2; i < index; i++) { uint num7 = this.StszAtom.GetSampleSize(i); chunkOffset += num7; } sample.DescriptionIndex = num3 - 1; uint ctsOffset = 0; ulong dts = 0L; uint duration = 0; this.SttsAtom.GetDts(index, out dts, out duration); sample.Duration = duration; sample.Dts = dts; if (this.CttsAtom == null) { sample.Cts = dts; } else { ctsOffset = this.CttsAtom.GetCtsOffset((uint)index); sample.Cts = dts + ctsOffset; } uint sampleSize = this.StszAtom.GetSampleSize((uint)index); sample.Size = (int)sampleSize; if (this.StssAtom == null) { sample.IsSync = true; } else { sample.IsSync = this.StssAtom.IsSampleSync((uint)index); } sample.Offset = (int)chunkOffset; sample.DataStream = this.SampleStream; return(sample); }