VP8 raw file: C++11/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++11/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++11/STL source code to parse VP8 raw file

vp8_ivf.h

#pragma once

// 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 <memory>
#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 = nullptr, vp8_ivf_t* p__root = nullptr);

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 = nullptr, vp8_ivf_t* p__root = nullptr);

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

    public:
        ~blocks_t();

    private:
        std::unique_ptr<block_t> m_entries;
        vp8_ivf_t* m__root;
        vp8_ivf_t* m__parent;

    public:
        block_t* entries() const { return m_entries.get(); }
        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 = nullptr, vp8_ivf_t* p__root = nullptr);

    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::unique_ptr<std::vector<std::unique_ptr<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<std::unique_ptr<blocks_t>>* image_data() const { return m_image_data.get(); }
    vp8_ivf_t* _root() const { return m__root; }
    kaitai::kstruct* _parent() const { return m__parent; }
};

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 = nullptr;
    _read();
}

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();
    int l_image_data = num_frames();
    m_image_data = std::unique_ptr<std::vector<std::unique_ptr<blocks_t>>>(new std::vector<std::unique_ptr<blocks_t>>());
    m_image_data->reserve(l_image_data);
    for (int i = 0; i < l_image_data; i++) {
        m_image_data->push_back(std::move(std::unique_ptr<blocks_t>(new blocks_t(m__io, this, m__root))));
    }
}

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

void vp8_ivf_t::_clean_up() {
}

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 = nullptr;
    _read();
}

void vp8_ivf_t::blocks_t::_read() {
    m_entries = std::unique_ptr<block_t>(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() {
}

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;
    _read();
}

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() {
}