This page hosts a formal specification of Microsoft AVI file using Kaitai Struct. This specification can be automatically translated into a variety of programming languages to get a parsing 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.
Using Kaitai Struct in C++/STL usually consists of 3 steps.
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.avi", std::ifstream::binary);
#include <sstream>
std::istringstream is(str);
#include <sstream>
const char buf[] = { ... };
std::string str(buf, sizeof buf);
std::istringstream is(str);
#include "kaitai/kaitaistream.h"
kaitai::kstream ks(&is);
avi_t data(&ks);
After that, one can get various attributes from the structure by invoking getter methods like:
data.magic1() // => get magic1
#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
/**
* \sa https://learn.microsoft.com/en-us/previous-versions/ms779636(v=vs.85) 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 = nullptr, avi_t* p__root = nullptr);
private:
void _read();
void _clean_up();
public:
~avi_t();
class list_body_t : public kaitai::kstruct {
public:
list_body_t(kaitai::kstream* p__io, avi_t::block_t* p__parent = nullptr, avi_t* p__root = nullptr);
private:
void _read();
void _clean_up();
public:
~list_body_t();
private:
chunk_type_t m_list_type;
std::unique_ptr<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.get(); }
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 = nullptr, avi_t* p__root = nullptr);
private:
void _read();
void _clean_up();
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 = nullptr, avi_t* p__root = nullptr);
private:
void _read();
void _clean_up();
public:
~blocks_t();
private:
std::unique_ptr<std::vector<std::unique_ptr<block_t>>> m_entries;
avi_t* m__root;
kaitai::kstruct* m__parent;
public:
std::vector<std::unique_ptr<block_t>>* entries() const { return m_entries.get(); }
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 https://learn.microsoft.com/en-us/previous-versions/ms779632(v=vs.85) Source
*/
class avih_body_t : public kaitai::kstruct {
public:
avih_body_t(kaitai::kstream* p__io, avi_t::block_t* p__parent = nullptr, avi_t* p__root = nullptr);
private:
void _read();
void _clean_up();
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 = nullptr, avi_t* p__root = nullptr);
private:
void _read();
void _clean_up();
public:
~block_t();
private:
chunk_type_t m_four_cc;
uint32_t m_block_size;
std::unique_ptr<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;
std::unique_ptr<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.get(); }
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.get(); }
};
/**
* Stream header (one header per stream), defined as AVISTREAMHEADER structure
* \sa https://learn.microsoft.com/en-us/previous-versions/ms779638(v=vs.85) Source
*/
class strh_body_t : public kaitai::kstruct {
public:
strh_body_t(kaitai::kstream* p__io, avi_t::block_t* p__parent = nullptr, avi_t* p__root = nullptr);
private:
void _read();
void _clean_up();
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;
std::unique_ptr<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.get(); }
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 = nullptr, avi_t* p__root = nullptr);
private:
void _read();
void _clean_up();
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;
std::unique_ptr<blocks_t> m_data;
avi_t* m__root;
kaitai::kstruct* m__parent;
std::string m__raw_data;
std::unique_ptr<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.get(); }
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.get(); }
};
// This is a generated file! Please edit source .ksy file and use kaitai-struct-compiler to rebuild
#include "avi.h"
#include "kaitai/exceptions.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;
m_data = nullptr;
m__io__raw_data = nullptr;
_read();
}
void avi_t::_read() {
m_magic1 = m__io->read_bytes(4);
if (!(magic1() == std::string("\x52\x49\x46\x46", 4))) {
throw kaitai::validation_not_equal_error<std::string>(std::string("\x52\x49\x46\x46", 4), magic1(), _io(), std::string("/seq/0"));
}
m_file_size = m__io->read_u4le();
m_magic2 = m__io->read_bytes(4);
if (!(magic2() == std::string("\x41\x56\x49\x20", 4))) {
throw kaitai::validation_not_equal_error<std::string>(std::string("\x41\x56\x49\x20", 4), magic2(), _io(), std::string("/seq/2"));
}
m__raw_data = m__io->read_bytes((file_size() - 4));
m__io__raw_data = std::unique_ptr<kaitai::kstream>(new kaitai::kstream(m__raw_data));
m_data = std::unique_ptr<blocks_t>(new blocks_t(m__io__raw_data.get(), this, m__root));
}
avi_t::~avi_t() {
_clean_up();
}
void avi_t::_clean_up() {
}
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;
m_data = nullptr;
_read();
}
void avi_t::list_body_t::_read() {
m_list_type = static_cast<avi_t::chunk_type_t>(m__io->read_u4le());
m_data = std::unique_ptr<blocks_t>(new blocks_t(m__io, this, m__root));
}
avi_t::list_body_t::~list_body_t() {
_clean_up();
}
void avi_t::list_body_t::_clean_up() {
}
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() {
_clean_up();
}
void avi_t::rect_t::_clean_up() {
}
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;
m_entries = nullptr;
_read();
}
void avi_t::blocks_t::_read() {
m_entries = std::unique_ptr<std::vector<std::unique_ptr<block_t>>>(new std::vector<std::unique_ptr<block_t>>());
{
int i = 0;
while (!m__io->is_eof()) {
m_entries->push_back(std::move(std::unique_ptr<block_t>(new block_t(m__io, this, m__root))));
i++;
}
}
}
avi_t::blocks_t::~blocks_t() {
_clean_up();
}
void avi_t::blocks_t::_clean_up() {
}
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() {
_clean_up();
}
void avi_t::avih_body_t::_clean_up() {
}
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;
m__io__raw_data = nullptr;
_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 avi_t::CHUNK_TYPE_LIST: {
n_data = false;
m__raw_data = m__io->read_bytes(block_size());
m__io__raw_data = std::unique_ptr<kaitai::kstream>(new kaitai::kstream(m__raw_data));
m_data = std::unique_ptr<list_body_t>(new list_body_t(m__io__raw_data.get(), this, m__root));
break;
}
case avi_t::CHUNK_TYPE_AVIH: {
n_data = false;
m__raw_data = m__io->read_bytes(block_size());
m__io__raw_data = std::unique_ptr<kaitai::kstream>(new kaitai::kstream(m__raw_data));
m_data = std::unique_ptr<avih_body_t>(new avih_body_t(m__io__raw_data.get(), this, m__root));
break;
}
case avi_t::CHUNK_TYPE_STRH: {
n_data = false;
m__raw_data = m__io->read_bytes(block_size());
m__io__raw_data = std::unique_ptr<kaitai::kstream>(new kaitai::kstream(m__raw_data));
m_data = std::unique_ptr<strh_body_t>(new strh_body_t(m__io__raw_data.get(), this, m__root));
break;
}
default: {
m__raw_data = m__io->read_bytes(block_size());
break;
}
}
}
avi_t::block_t::~block_t() {
_clean_up();
}
void avi_t::block_t::_clean_up() {
if (!n_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;
m_frame = nullptr;
_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 = std::unique_ptr<rect_t>(new rect_t(m__io, this, m__root));
}
avi_t::strh_body_t::~strh_body_t() {
_clean_up();
}
void avi_t::strh_body_t::_clean_up() {
}
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() {
_clean_up();
}
void avi_t::strf_body_t::_clean_up() {
}