Microsoft AVI file format: C++/STL parsing library

KS implementation details

License: CC0-1.0
Minimal Kaitai Struct required: 0.7

This page hosts a formal specification of Microsoft AVI file format 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 a stream for reading from a local file:
      #include <fstream>
      
      std::ifstream is("path/to/local/file.avi", std::ifstream::binary);
    • Or one can prepare a stream for reading from existing std::string str:
      #include <sstream>
      
      std::istringstream is(str);
    • Or one can parse arbitrary char* buffer in memory, given that we know its size:
      #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:
    avi_t data(&ks);

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

data.magic1() // => get magic1

C++/STL source code to parse Microsoft AVI file format

avi.h

#ifndef AVI_H_
#define AVI_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

/**
 * \sa Source
 */

class avi_t : public kaitai::kstruct {

public:
    class list_body_t;
    class rect_t;
    class blocks_t;
    class avih_body_t;
    class block_t;
    class strh_body_t;
    class strf_body_t;

    enum chunk_type_t {
        CHUNK_TYPE_IDX1 = 829973609,
        CHUNK_TYPE_JUNK = 1263424842,
        CHUNK_TYPE_INFO = 1330007625,
        CHUNK_TYPE_ISFT = 1413894985,
        CHUNK_TYPE_LIST = 1414744396,
        CHUNK_TYPE_STRF = 1718776947,
        CHUNK_TYPE_AVIH = 1751742049,
        CHUNK_TYPE_STRH = 1752331379,
        CHUNK_TYPE_MOVI = 1769369453,
        CHUNK_TYPE_HDRL = 1819436136,
        CHUNK_TYPE_STRL = 1819440243
    };

    enum stream_type_t {
        STREAM_TYPE_MIDS = 1935960429,
        STREAM_TYPE_VIDS = 1935960438,
        STREAM_TYPE_AUDS = 1935963489,
        STREAM_TYPE_TXTS = 1937012852
    };

    enum handler_type_t {
        HANDLER_TYPE_MP3 = 85,
        HANDLER_TYPE_AC3 = 8192,
        HANDLER_TYPE_DTS = 8193,
        HANDLER_TYPE_CVID = 1684633187,
        HANDLER_TYPE_XVID = 1684633208
    };

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

private:
    void _read();

public:
    ~avi_t();

    class list_body_t : public kaitai::kstruct {

    public:

        list_body_t(kaitai::kstream* p__io, avi_t::block_t* p__parent = 0, avi_t* p__root = 0);

    private:
        void _read();

    public:
        ~list_body_t();

    private:
        chunk_type_t m_list_type;
        blocks_t* m_data;
        avi_t* m__root;
        avi_t::block_t* m__parent;

    public:
        chunk_type_t list_type() const { return m_list_type; }
        blocks_t* data() const { return m_data; }
        avi_t* _root() const { return m__root; }
        avi_t::block_t* _parent() const { return m__parent; }
    };

    class rect_t : public kaitai::kstruct {

    public:

        rect_t(kaitai::kstream* p__io, avi_t::strh_body_t* p__parent = 0, avi_t* p__root = 0);

    private:
        void _read();

    public:
        ~rect_t();

    private:
        int16_t m_left;
        int16_t m_top;
        int16_t m_right;
        int16_t m_bottom;
        avi_t* m__root;
        avi_t::strh_body_t* m__parent;

    public:
        int16_t left() const { return m_left; }
        int16_t top() const { return m_top; }
        int16_t right() const { return m_right; }
        int16_t bottom() const { return m_bottom; }
        avi_t* _root() const { return m__root; }
        avi_t::strh_body_t* _parent() const { return m__parent; }
    };

    class blocks_t : public kaitai::kstruct {

    public:

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

    private:
        void _read();

    public:
        ~blocks_t();

    private:
        std::vector<block_t*>* m_entries;
        avi_t* m__root;
        kaitai::kstruct* m__parent;

    public:
        std::vector<block_t*>* entries() const { return m_entries; }
        avi_t* _root() const { return m__root; }
        kaitai::kstruct* _parent() const { return m__parent; }
    };

    /**
     * Main header of an AVI file, defined as AVIMAINHEADER structure
     * \sa Source
     */

    class avih_body_t : public kaitai::kstruct {

    public:

        avih_body_t(kaitai::kstream* p__io, avi_t::block_t* p__parent = 0, avi_t* p__root = 0);

    private:
        void _read();

    public:
        ~avih_body_t();

    private:
        uint32_t m_micro_sec_per_frame;
        uint32_t m_max_bytes_per_sec;
        uint32_t m_padding_granularity;
        uint32_t m_flags;
        uint32_t m_total_frames;
        uint32_t m_initial_frames;
        uint32_t m_streams;
        uint32_t m_suggested_buffer_size;
        uint32_t m_width;
        uint32_t m_height;
        std::string m_reserved;
        avi_t* m__root;
        avi_t::block_t* m__parent;

    public:
        uint32_t micro_sec_per_frame() const { return m_micro_sec_per_frame; }
        uint32_t max_bytes_per_sec() const { return m_max_bytes_per_sec; }
        uint32_t padding_granularity() const { return m_padding_granularity; }
        uint32_t flags() const { return m_flags; }
        uint32_t total_frames() const { return m_total_frames; }
        uint32_t initial_frames() const { return m_initial_frames; }
        uint32_t streams() const { return m_streams; }
        uint32_t suggested_buffer_size() const { return m_suggested_buffer_size; }
        uint32_t width() const { return m_width; }
        uint32_t height() const { return m_height; }
        std::string reserved() const { return m_reserved; }
        avi_t* _root() const { return m__root; }
        avi_t::block_t* _parent() const { return m__parent; }
    };

    class block_t : public kaitai::kstruct {

    public:

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

    private:
        void _read();

    public:
        ~block_t();

    private:
        chunk_type_t m_four_cc;
        uint32_t m_block_size;
        kaitai::kstruct* m_data;
        bool n_data;

    public:
        bool _is_null_data() { data(); return n_data; };

    private:
        avi_t* m__root;
        avi_t::blocks_t* m__parent;
        std::string m__raw_data;
        kaitai::kstream* m__io__raw_data;

    public:
        chunk_type_t four_cc() const { return m_four_cc; }
        uint32_t block_size() const { return m_block_size; }
        kaitai::kstruct* data() const { return m_data; }
        avi_t* _root() const { return m__root; }
        avi_t::blocks_t* _parent() const { return m__parent; }
        std::string _raw_data() const { return m__raw_data; }
        kaitai::kstream* _io__raw_data() const { return m__io__raw_data; }
    };

    /**
     * Stream header (one header per stream), defined as AVISTREAMHEADER structure
     * \sa Source
     */

    class strh_body_t : public kaitai::kstruct {

    public:

        strh_body_t(kaitai::kstream* p__io, avi_t::block_t* p__parent = 0, avi_t* p__root = 0);

    private:
        void _read();

    public:
        ~strh_body_t();

    private:
        stream_type_t m_fcc_type;
        handler_type_t m_fcc_handler;
        uint32_t m_flags;
        uint16_t m_priority;
        uint16_t m_language;
        uint32_t m_initial_frames;
        uint32_t m_scale;
        uint32_t m_rate;
        uint32_t m_start;
        uint32_t m_length;
        uint32_t m_suggested_buffer_size;
        uint32_t m_quality;
        uint32_t m_sample_size;
        rect_t* m_frame;
        avi_t* m__root;
        avi_t::block_t* m__parent;

    public:

        /**
         * Type of the data contained in the stream
         */
        stream_type_t fcc_type() const { return m_fcc_type; }

        /**
         * Type of preferred data handler for the stream (specifies codec for audio / video streams)
         */
        handler_type_t fcc_handler() const { return m_fcc_handler; }
        uint32_t flags() const { return m_flags; }
        uint16_t priority() const { return m_priority; }
        uint16_t language() const { return m_language; }
        uint32_t initial_frames() const { return m_initial_frames; }
        uint32_t scale() const { return m_scale; }
        uint32_t rate() const { return m_rate; }
        uint32_t start() const { return m_start; }
        uint32_t length() const { return m_length; }
        uint32_t suggested_buffer_size() const { return m_suggested_buffer_size; }
        uint32_t quality() const { return m_quality; }
        uint32_t sample_size() const { return m_sample_size; }
        rect_t* frame() const { return m_frame; }
        avi_t* _root() const { return m__root; }
        avi_t::block_t* _parent() const { return m__parent; }
    };

    /**
     * Stream format description
     */

    class strf_body_t : public kaitai::kstruct {

    public:

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

    private:
        void _read();

    public:
        ~strf_body_t();

    private:
        avi_t* m__root;
        kaitai::kstruct* m__parent;

    public:
        avi_t* _root() const { return m__root; }
        kaitai::kstruct* _parent() const { return m__parent; }
    };

private:
    std::string m_magic1;
    uint32_t m_file_size;
    std::string m_magic2;
    blocks_t* m_data;
    avi_t* m__root;
    kaitai::kstruct* m__parent;
    std::string m__raw_data;
    kaitai::kstream* m__io__raw_data;

public:
    std::string magic1() const { return m_magic1; }
    uint32_t file_size() const { return m_file_size; }
    std::string magic2() const { return m_magic2; }
    blocks_t* data() const { return m_data; }
    avi_t* _root() const { return m__root; }
    kaitai::kstruct* _parent() const { return m__parent; }
    std::string _raw_data() const { return m__raw_data; }
    kaitai::kstream* _io__raw_data() const { return m__io__raw_data; }
};

#endif  // AVI_H_

avi.cpp

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

#include "avi.h"



avi_t::avi_t(kaitai::kstream* p__io, kaitai::kstruct* p__parent, avi_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = this;
    _read();
}

void avi_t::_read() {
    m_magic1 = m__io->ensure_fixed_contents(std::string("\x52\x49\x46\x46", 4));
    m_file_size = m__io->read_u4le();
    m_magic2 = m__io->ensure_fixed_contents(std::string("\x41\x56\x49\x20", 4));
    m__raw_data = m__io->read_bytes((file_size() - 4));
    m__io__raw_data = new kaitai::kstream(m__raw_data);
    m_data = new blocks_t(m__io__raw_data, this, m__root);
}

avi_t::~avi_t() {
    delete m__io__raw_data;
    delete m_data;
}

avi_t::list_body_t::list_body_t(kaitai::kstream* p__io, avi_t::block_t* p__parent, avi_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    _read();
}

void avi_t::list_body_t::_read() {
    m_list_type = static_cast<avi_t::chunk_type_t>(m__io->read_u4le());
    m_data = new blocks_t(m__io, this, m__root);
}

avi_t::list_body_t::~list_body_t() {
    delete m_data;
}

avi_t::rect_t::rect_t(kaitai::kstream* p__io, avi_t::strh_body_t* p__parent, avi_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    _read();
}

void avi_t::rect_t::_read() {
    m_left = m__io->read_s2le();
    m_top = m__io->read_s2le();
    m_right = m__io->read_s2le();
    m_bottom = m__io->read_s2le();
}

avi_t::rect_t::~rect_t() {
}

avi_t::blocks_t::blocks_t(kaitai::kstream* p__io, kaitai::kstruct* p__parent, avi_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    _read();
}

void avi_t::blocks_t::_read() {
    m_entries = new std::vector<block_t*>();
    {
        int i = 0;
        while (!m__io->is_eof()) {
            m_entries->push_back(new block_t(m__io, this, m__root));
            i++;
        }
    }
}

avi_t::blocks_t::~blocks_t() {
    for (std::vector<block_t*>::iterator it = m_entries->begin(); it != m_entries->end(); ++it) {
        delete *it;
    }
    delete m_entries;
}

avi_t::avih_body_t::avih_body_t(kaitai::kstream* p__io, avi_t::block_t* p__parent, avi_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    _read();
}

void avi_t::avih_body_t::_read() {
    m_micro_sec_per_frame = m__io->read_u4le();
    m_max_bytes_per_sec = m__io->read_u4le();
    m_padding_granularity = m__io->read_u4le();
    m_flags = m__io->read_u4le();
    m_total_frames = m__io->read_u4le();
    m_initial_frames = m__io->read_u4le();
    m_streams = m__io->read_u4le();
    m_suggested_buffer_size = m__io->read_u4le();
    m_width = m__io->read_u4le();
    m_height = m__io->read_u4le();
    m_reserved = m__io->read_bytes(16);
}

avi_t::avih_body_t::~avih_body_t() {
}

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

void avi_t::block_t::_read() {
    m_four_cc = static_cast<avi_t::chunk_type_t>(m__io->read_u4le());
    m_block_size = m__io->read_u4le();
    n_data = true;
    switch (four_cc()) {
    case CHUNK_TYPE_LIST: {
        n_data = false;
        m__raw_data = m__io->read_bytes(block_size());
        m__io__raw_data = new kaitai::kstream(m__raw_data);
        m_data = new list_body_t(m__io__raw_data, this, m__root);
        break;
    }
    case CHUNK_TYPE_AVIH: {
        n_data = false;
        m__raw_data = m__io->read_bytes(block_size());
        m__io__raw_data = new kaitai::kstream(m__raw_data);
        m_data = new avih_body_t(m__io__raw_data, this, m__root);
        break;
    }
    case CHUNK_TYPE_STRH: {
        n_data = false;
        m__raw_data = m__io->read_bytes(block_size());
        m__io__raw_data = new kaitai::kstream(m__raw_data);
        m_data = new strh_body_t(m__io__raw_data, this, m__root);
        break;
    }
    default: {
        m__raw_data = m__io->read_bytes(block_size());
        break;
    }
    }
}

avi_t::block_t::~block_t() {
    if (!n_data) {
        delete m__io__raw_data;
        delete m_data;
    }
}

avi_t::strh_body_t::strh_body_t(kaitai::kstream* p__io, avi_t::block_t* p__parent, avi_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    _read();
}

void avi_t::strh_body_t::_read() {
    m_fcc_type = static_cast<avi_t::stream_type_t>(m__io->read_u4le());
    m_fcc_handler = static_cast<avi_t::handler_type_t>(m__io->read_u4le());
    m_flags = m__io->read_u4le();
    m_priority = m__io->read_u2le();
    m_language = m__io->read_u2le();
    m_initial_frames = m__io->read_u4le();
    m_scale = m__io->read_u4le();
    m_rate = m__io->read_u4le();
    m_start = m__io->read_u4le();
    m_length = m__io->read_u4le();
    m_suggested_buffer_size = m__io->read_u4le();
    m_quality = m__io->read_u4le();
    m_sample_size = m__io->read_u4le();
    m_frame = new rect_t(m__io, this, m__root);
}

avi_t::strh_body_t::~strh_body_t() {
    delete m_frame;
}

avi_t::strf_body_t::strf_body_t(kaitai::kstream* p__io, kaitai::kstruct* p__parent, avi_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    _read();
}

void avi_t::strf_body_t::_read() {
}

avi_t::strf_body_t::~strf_body_t() {
}