Skip to content

Commit

Permalink
add 'Box_other' that captures all unknown box data (#1156)
Browse files Browse the repository at this point in the history
  • Loading branch information
farindk committed Jun 11, 2024
1 parent 0b71dd3 commit 62ffc12
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 1 deletion.
78 changes: 77 additions & 1 deletion libheif/box.cc
Original file line number Diff line number Diff line change
Expand Up @@ -610,9 +610,14 @@ Error Box::read(BitstreamRange& range, std::shared_ptr<Box>* result)
box = std::make_shared<Box_mskC>();
break;

default:
case fourcc("mdat"):
// avoid generating a 'Box_other'
box = std::make_shared<Box>();
break;

default:
box = std::make_shared<Box_other>(hdr.get_short_type());
break;
}

box->set_short_header(hdr);
Expand Down Expand Up @@ -849,6 +854,77 @@ void Box::derive_box_version_recursive()
}


Error Box_other::parse(BitstreamRange& range)
{
if (has_fixed_box_size()) {
size_t len = get_box_size() - get_header_size();
m_data.resize(len);
range.read(m_data.data(), len);
}
else {
// TODO: boxes until end of file (we will probably never need this)
}

return range.get_error();
}


Error Box_other::write(StreamWriter& writer) const
{
size_t box_start = reserve_box_header_space(writer);

writer.write(m_data);

prepend_header(writer, box_start);

return Error::Ok;
}


std::string Box_other::dump(Indent& indent) const
{
std::ostringstream sstr;

sstr << BoxHeader::dump(indent);

// --- show raw box content

sstr << std::hex << std::setfill('0');

size_t len = get_box_size() - get_header_size();

for (size_t i = 0; i < len; i++) {
if (i % 16 == 0) {
// start of line

if (i == 0) {
sstr << indent << "data: ";
}
else {
sstr << indent << " ";
}
sstr << std::setw(4) << i << ": "; // address
}
else if (i % 16 == 8) {
// space in middle
sstr << " ";
}
else {
// space between bytes
sstr << " ";
}

sstr << std::setw(2) << ((int) m_data[i]);

if (i % 16 == 15 || i == len - 1) {
sstr << "\n";
}
}

return sstr.str();
}


Error Box_ftyp::parse(BitstreamRange& range)
{
m_major_brand = range.read32();
Expand Down
21 changes: 21 additions & 0 deletions libheif/box.h
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,27 @@ class FullBox : public Box
};


class Box_other : public Box
{
public:
Box_other(uint32_t short_type)
{
set_short_type(short_type);
}

Error write(StreamWriter& writer) const override;

std::string dump(Indent&) const override;

protected:
Error parse(BitstreamRange& range) override;

std::vector<uint8_t> m_data;
};




class Box_ftyp : public Box
{
public:
Expand Down

0 comments on commit 62ffc12

Please sign in to comment.