Esempio n. 1
0
        internal bool DescendToAtom([NotNull, ItemNotNull] params string[] hierarchy)
        {
            Reset();

            using (var reader = new Mp4Reader(_stream))
            {
                foreach (var fourCc in hierarchy)
                {
                    do
                    {
                        var subAtom = new AtomInfo((uint)_stream.Position, reader.ReadUInt32BigEndian(),
                                                   reader.ReadFourCc());

                        if (subAtom.End > _stream.Length)
                        {
                            throw new EndOfStreamException($"{fourCc} atom is missing.");
                        }

                        if (fourCc.Equals(subAtom.FourCc, StringComparison.Ordinal))
                        {
                            _atomInfoStack.Push(subAtom);
                            _stream.Seek(GetDataLength(fourCc), SeekOrigin.Current);
                            break;
                        }

                        _stream.Position = subAtom.End;
                    } while (_stream.Position < (_atomInfoStack.Count == 0 ? _stream.Length : _atomInfoStack.Peek().End));
                }
            }

            return(hierarchy.Last().Equals(_atomInfoStack.Peek().FourCc, StringComparison.Ordinal));
        }
Esempio n. 2
0
        internal AtomInfo[] GetChildAtomInfo()
        {
            var result = new List <AtomInfo>();

            using (var reader = new Mp4Reader(_stream))
            {
                _stream.Position = _atomInfoStack.Count == 0
                    ? 0
                    : _atomInfoStack.Peek().Start + 8 + GetDataLength(_atomInfoStack.Peek().FourCc);

                while (_stream.Position < (_atomInfoStack.Count == 0 ? _stream.Length : _atomInfoStack.Peek().End))
                {
                    var childAtom = new AtomInfo(
                        (uint)_stream.Position,
                        reader.ReadUInt32BigEndian(),
                        reader.ReadFourCc());

                    // The stream might be padded with empty space
                    if (childAtom.FourCc.Equals("\0\0\0\0", StringComparison.Ordinal))
                    {
                        _stream.Position = result.Count > 0 ? result.Last().End : 0;
                        break;
                    }

                    result.Add(childAtom);
                    _stream.Position = childAtom.End;
                }
            }

            return(result.ToArray());
        }
Esempio n. 3
0
        internal byte[] ReadAtom([NotNull] AtomInfo atom)
        {
            _stream.Position = atom.Start;

            using (var reader = new Mp4Reader(_stream))
                return(reader.ReadBytes((int)atom.Size));
        }
Esempio n. 4
0
        internal void UpdateStco(uint offset)
        {
            DescendToAtom("moov", "trak", "mdia", "minf", "stbl", "stco");
            _stream.Seek(4, SeekOrigin.Current);

            using (var reader = new Mp4Reader(_stream))
                using (var writer = new Mp4Writer(_stream))
                {
                    var count     = reader.ReadUInt32BigEndian();
                    var dataStart = _stream.Position;

                    for (var i = 0; i < count; i++)
                    {
                        _stream.Position = dataStart + i * 4;
                        var value = reader.ReadUInt32BigEndian();
                        _stream.Seek(-4, SeekOrigin.Current);
                        writer.WriteBigEndian(value + offset);
                    }
                }
        }