VP8 raw file: C++98/STL 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++98/STL generated by Kaitai Struct depends on the C++/STL runtime library. You have to install it before you can parse data.

For C++, the easiest way is to clone the runtime library sources and build them along with your project.

Code

Using Kaitai Struct in C++/STL usually consists of 3 steps.

  1. We need to create an STL input stream (std::istream). One can open local file for that, or use existing std::string or char* buffer.
    #include <fstream>
    
    std::ifstream is("path/to/local/file.ivf", std::ifstream::binary);
    
    #include <sstream>
    
    std::istringstream is(str);
    
    #include <sstream>
    
    const char buf[] = { ... };
    std::string str(buf, sizeof buf);
    std::istringstream is(str);
    
  2. We need to wrap our input stream into Kaitai stream:
    #include "kaitai/kaitaistream.h"
    
    kaitai::kstream ks(&is);
    
  3. And finally, we can invoke the parsing:
    vp8_ivf_t data(&ks);
    

After that, one can get various attributes from the structure by invoking getter methods like:

data.magic1() // => Magic Number of IVF Files

C++98/STL source code to parse VP8 raw file

vp8_ivf.h

#ifndef VP8_IVF_H_
#define VP8_IVF_H_

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

#include "kaitai/kaitaistruct.h"
#include <stdint.h>
#include <vector>

#if KAITAI_STRUCT_VERSION < 9000L
#error "Incompatible Kaitai Struct C++/STL API: version 0.9 or later is required"
#endif

/**
 * 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>
 * \sa https://wiki.multimedia.cx/index.php/IVF Source
 */

class vp8_ivf_t : public kaitai::kstruct {

public:
    class blocks_t;
    class block_t;

    vp8_ivf_t(kaitai::kstream* p__io, kaitai::kstruct* p__parent = 0, vp8_ivf_t* p__root = 0);

private:
    void _read();
    void _clean_up();

public:
    ~vp8_ivf_t();

    class blocks_t : public kaitai::kstruct {

    public:

        blocks_t(kaitai::kstream* p__io, vp8_ivf_t* p__parent = 0, vp8_ivf_t* p__root = 0);

    private:
        void _read();
        void _clean_up();

    public:
        ~blocks_t();

    private:
        block_t* m_entries;
        vp8_ivf_t* m__root;
        vp8_ivf_t* m__parent;

    public:
        block_t* entries() const { return m_entries; }
        vp8_ivf_t* _root() const { return m__root; }
        vp8_ivf_t* _parent() const { return m__parent; }
    };

    class block_t : public kaitai::kstruct {

    public:

        block_t(kaitai::kstream* p__io, vp8_ivf_t::blocks_t* p__parent = 0, vp8_ivf_t* p__root = 0);

    private:
        void _read();
        void _clean_up();

    public:
        ~block_t();

    private:
        uint32_t m_len_frame;
        uint64_t m_timestamp;
        std::string m_framedata;
        vp8_ivf_t* m__root;
        vp8_ivf_t::blocks_t* m__parent;

    public:

        /**
         * size of the frame data
         */
        uint32_t len_frame() const { return m_len_frame; }
        uint64_t timestamp() const { return m_timestamp; }
        std::string framedata() const { return m_framedata; }
        vp8_ivf_t* _root() const { return m__root; }
        vp8_ivf_t::blocks_t* _parent() const { return m__parent; }
    };

private:
    std::string m_magic1;
    uint16_t m_version;
    uint16_t m_len_header;
    std::string m_codec;
    uint16_t m_width;
    uint16_t m_height;
    uint32_t m_framerate;
    uint32_t m_timescale;
    uint32_t m_num_frames;
    uint32_t m_unused;
    std::vector<blocks_t*>* m_image_data;
    vp8_ivf_t* m__root;
    kaitai::kstruct* m__parent;

public:

    /**
     * Magic Number of IVF Files
     */
    std::string magic1() const { return m_magic1; }

    /**
     * This should be 0
     */
    uint16_t version() const { return m_version; }

    /**
     * Normally the header length is 32 byte
     */
    uint16_t len_header() const { return m_len_header; }

    /**
     * Name of the codec e.g. 'VP80' for VP8
     */
    std::string codec() const { return m_codec; }

    /**
     * The (initial) width of the video, every keyframe may change the resolution
     */
    uint16_t width() const { return m_width; }

    /**
     * The (initial) height of the video, every keyframe may change the resolution
     */
    uint16_t height() const { return m_height; }

    /**
     * the (framerate * timescale) e.g. for 30 fps -> 30000
     */
    uint32_t framerate() const { return m_framerate; }

    /**
     * the timescale is a divider of the seconds (VPX is integer math only) mostly 1000
     */
    uint32_t timescale() const { return m_timescale; }

    /**
     * the number of frames (if not a camera stream)
     */
    uint32_t num_frames() const { return m_num_frames; }
    uint32_t unused() const { return m_unused; }
    std::vector<blocks_t*>* image_data() const { return m_image_data; }
    vp8_ivf_t* _root() const { return m__root; }
    kaitai::kstruct* _parent() const { return m__parent; }
};

#endif  // VP8_IVF_H_

vp8_ivf.cpp

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

#include "vp8_ivf.h"
#include "kaitai/exceptions.h"

vp8_ivf_t::vp8_ivf_t(kaitai::kstream* p__io, kaitai::kstruct* p__parent, vp8_ivf_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = this;
    m_image_data = 0;

    try {
        _read();
    } catch(...) {
        _clean_up();
        throw;
    }
}

void vp8_ivf_t::_read() {
    m_magic1 = m__io->read_bytes(4);
    if (!(magic1() == std::string("\x44\x4B\x49\x46", 4))) {
        throw kaitai::validation_not_equal_error<std::string>(std::string("\x44\x4B\x49\x46", 4), magic1(), _io(), std::string("/seq/0"));
    }
    m_version = m__io->read_u2le();
    m_len_header = m__io->read_u2le();
    m_codec = m__io->read_bytes(4);
    if (!(codec() == std::string("\x56\x50\x38\x30", 4))) {
        throw kaitai::validation_not_equal_error<std::string>(std::string("\x56\x50\x38\x30", 4), codec(), _io(), std::string("/seq/3"));
    }
    m_width = m__io->read_u2le();
    m_height = m__io->read_u2le();
    m_framerate = m__io->read_u4le();
    m_timescale = m__io->read_u4le();
    m_num_frames = m__io->read_u4le();
    m_unused = m__io->read_u4le();
    m_image_data = new std::vector<blocks_t*>();
    const int l_image_data = num_frames();
    for (int i = 0; i < l_image_data; i++) {
        m_image_data->push_back(new blocks_t(m__io, this, m__root));
    }
}

vp8_ivf_t::~vp8_ivf_t() {
    _clean_up();
}

void vp8_ivf_t::_clean_up() {
    if (m_image_data) {
        for (std::vector<blocks_t*>::iterator it = m_image_data->begin(); it != m_image_data->end(); ++it) {
            delete *it;
        }
        delete m_image_data; m_image_data = 0;
    }
}

vp8_ivf_t::blocks_t::blocks_t(kaitai::kstream* p__io, vp8_ivf_t* p__parent, vp8_ivf_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    m_entries = 0;

    try {
        _read();
    } catch(...) {
        _clean_up();
        throw;
    }
}

void vp8_ivf_t::blocks_t::_read() {
    m_entries = new block_t(m__io, this, m__root);
}

vp8_ivf_t::blocks_t::~blocks_t() {
    _clean_up();
}

void vp8_ivf_t::blocks_t::_clean_up() {
    if (m_entries) {
        delete m_entries; m_entries = 0;
    }
}

vp8_ivf_t::block_t::block_t(kaitai::kstream* p__io, vp8_ivf_t::blocks_t* p__parent, vp8_ivf_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;

    try {
        _read();
    } catch(...) {
        _clean_up();
        throw;
    }
}

void vp8_ivf_t::block_t::_read() {
    m_len_frame = m__io->read_u4le();
    m_timestamp = m__io->read_u8le();
    m_framedata = m__io->read_bytes(len_frame());
}

vp8_ivf_t::block_t::~block_t() {
    _clean_up();
}

void vp8_ivf_t::block_t::_clean_up() {
}