VP8 raw file: C++/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

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

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++/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 < 7000L
#error "Incompatible Kaitai Struct C++/STL API: version 0.7 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 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();

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

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

    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"



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

void vp8_ivf_t::_read() {
    m_magic1 = m__io->ensure_fixed_contents(std::string("\x44\x4B\x49\x46", 4));
    m_version = m__io->read_u2le();
    m_len_header = m__io->read_u2le();
    m_codec = m__io->ensure_fixed_contents(std::string("\x56\x50\x38\x30", 4));
    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 = new std::vector<blocks_t*>();
    m_image_data->reserve(l_image_data);
    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() {
    for (std::vector<blocks_t*>::iterator it = m_image_data->begin(); it != m_image_data->end(); ++it) {
        delete *it;
    }
    delete m_image_data;
}

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

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

vp8_ivf_t::blocks_t::~blocks_t() {
    delete m_entries;
}

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