VP8 raw file: C# parsing library

IVF is a simple container format for raw VP8 data, which is an open and royalty-free video compression format, currently developed by Google.

Test .ivf files are available at https://chromium.googlesource.com/webm/vp8-test-vectors

File extension

ivf

KS implementation details

License: CC0-1.0
Minimal Kaitai Struct required: 0.7

References

This page hosts a formal specification of VP8 raw file using Kaitai Struct. This specification can be automatically translated into a variety of programming languages to get a parsing library.

Usage

Runtime library

All parsing code for C# generated by Kaitai Struct depends on the C# runtime library. You have to install it before you can parse data.

The C# runtime library is available in the NuGet Gallery. Installation instructions can also be found there.

Code

Parse a local file and get structure in memory:

var data = Vp8Ivf.FromFile("path/to/local/file.ivf");

Or parse structure from a byte array:

byte[] someArray = new byte[] { ... };
var data = new Vp8Ivf(new KaitaiStream(someArray));

After that, one can get various attributes from the structure by accessing properties like:

data.Magic1 // => Magic Number of IVF Files

C# source code to parse VP8 raw file

Vp8Ivf.cs

// This is a generated file! Please edit source .ksy file and use kaitai-struct-compiler to rebuild

using System.Collections.Generic;

namespace Kaitai
{

    /// <summary>
    /// IVF is a simple container format for raw VP8 data, which is an open and
    /// royalty-free video compression format, currently developed by Google.
    /// 
    /// Test .ivf files are available at
    /// &lt;https://chromium.googlesource.com/webm/vp8-test-vectors&gt;
    /// </summary>
    /// <remarks>
    /// Reference: <a href="https://wiki.multimedia.cx/index.php/IVF">Source</a>
    /// </remarks>
    public partial class Vp8Ivf : KaitaiStruct
    {
        public static Vp8Ivf FromFile(string fileName)
        {
            return new Vp8Ivf(new KaitaiStream(fileName));
        }

        public Vp8Ivf(KaitaiStream p__io, KaitaiStruct p__parent = null, Vp8Ivf p__root = null) : base(p__io)
        {
            m_parent = p__parent;
            m_root = p__root ?? this;
            _read();
        }
        private void _read()
        {
            _magic1 = m_io.ReadBytes(4);
            if (!((KaitaiStream.ByteArrayCompare(Magic1, new byte[] { 68, 75, 73, 70 }) == 0)))
            {
                throw new ValidationNotEqualError(new byte[] { 68, 75, 73, 70 }, Magic1, M_Io, "/seq/0");
            }
            _version = m_io.ReadU2le();
            _lenHeader = m_io.ReadU2le();
            _codec = m_io.ReadBytes(4);
            if (!((KaitaiStream.ByteArrayCompare(Codec, new byte[] { 86, 80, 56, 48 }) == 0)))
            {
                throw new ValidationNotEqualError(new byte[] { 86, 80, 56, 48 }, Codec, M_Io, "/seq/3");
            }
            _width = m_io.ReadU2le();
            _height = m_io.ReadU2le();
            _framerate = m_io.ReadU4le();
            _timescale = m_io.ReadU4le();
            _numFrames = m_io.ReadU4le();
            _unused = m_io.ReadU4le();
            _imageData = new List<Blocks>();
            for (var i = 0; i < NumFrames; i++)
            {
                _imageData.Add(new Blocks(m_io, this, m_root));
            }
        }
        public partial class Blocks : KaitaiStruct
        {
            public static Blocks FromFile(string fileName)
            {
                return new Blocks(new KaitaiStream(fileName));
            }

            public Blocks(KaitaiStream p__io, Vp8Ivf p__parent = null, Vp8Ivf p__root = null) : base(p__io)
            {
                m_parent = p__parent;
                m_root = p__root;
                _read();
            }
            private void _read()
            {
                _entries = new Block(m_io, this, m_root);
            }
            private Block _entries;
            private Vp8Ivf m_root;
            private Vp8Ivf m_parent;
            public Block Entries { get { return _entries; } }
            public Vp8Ivf M_Root { get { return m_root; } }
            public Vp8Ivf M_Parent { get { return m_parent; } }
        }
        public partial class Block : KaitaiStruct
        {
            public static Block FromFile(string fileName)
            {
                return new Block(new KaitaiStream(fileName));
            }

            public Block(KaitaiStream p__io, Vp8Ivf.Blocks p__parent = null, Vp8Ivf p__root = null) : base(p__io)
            {
                m_parent = p__parent;
                m_root = p__root;
                _read();
            }
            private void _read()
            {
                _lenFrame = m_io.ReadU4le();
                _timestamp = m_io.ReadU8le();
                _framedata = m_io.ReadBytes(LenFrame);
            }
            private uint _lenFrame;
            private ulong _timestamp;
            private byte[] _framedata;
            private Vp8Ivf m_root;
            private Vp8Ivf.Blocks m_parent;

            /// <summary>
            /// size of the frame data
            /// </summary>
            public uint LenFrame { get { return _lenFrame; } }
            public ulong Timestamp { get { return _timestamp; } }
            public byte[] Framedata { get { return _framedata; } }
            public Vp8Ivf M_Root { get { return m_root; } }
            public Vp8Ivf.Blocks M_Parent { get { return m_parent; } }
        }
        private byte[] _magic1;
        private ushort _version;
        private ushort _lenHeader;
        private byte[] _codec;
        private ushort _width;
        private ushort _height;
        private uint _framerate;
        private uint _timescale;
        private uint _numFrames;
        private uint _unused;
        private List<Blocks> _imageData;
        private Vp8Ivf m_root;
        private KaitaiStruct m_parent;

        /// <summary>
        /// Magic Number of IVF Files
        /// </summary>
        public byte[] Magic1 { get { return _magic1; } }

        /// <summary>
        /// This should be 0
        /// </summary>
        public ushort Version { get { return _version; } }

        /// <summary>
        /// Normally the header length is 32 byte
        /// </summary>
        public ushort LenHeader { get { return _lenHeader; } }

        /// <summary>
        /// Name of the codec e.g. 'VP80' for VP8
        /// </summary>
        public byte[] Codec { get { return _codec; } }

        /// <summary>
        /// The (initial) width of the video, every keyframe may change the resolution
        /// </summary>
        public ushort Width { get { return _width; } }

        /// <summary>
        /// The (initial) height of the video, every keyframe may change the resolution
        /// </summary>
        public ushort Height { get { return _height; } }

        /// <summary>
        /// the (framerate * timescale) e.g. for 30 fps -&gt; 30000
        /// </summary>
        public uint Framerate { get { return _framerate; } }

        /// <summary>
        /// the timescale is a divider of the seconds (VPX is integer math only) mostly 1000
        /// </summary>
        public uint Timescale { get { return _timescale; } }

        /// <summary>
        /// the number of frames (if not a camera stream)
        /// </summary>
        public uint NumFrames { get { return _numFrames; } }
        public uint Unused { get { return _unused; } }
        public List<Blocks> ImageData { get { return _imageData; } }
        public Vp8Ivf M_Root { get { return m_root; } }
        public KaitaiStruct M_Parent { get { return m_parent; } }
    }
}