Skip to content

Commit

Permalink
store VVC config NALs in vvcC header (#519)
Browse files Browse the repository at this point in the history
  • Loading branch information
farindk committed Jul 22, 2024
1 parent f350ce9 commit 586335e
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 16 deletions.
63 changes: 57 additions & 6 deletions libheif/codecs/vvc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ Error Box_vvcC::parse(BitstreamRange& range)

NalArray array;

array.m_array_completeness = (byte >> 6) & 1;
array.m_array_completeness = (byte >> 7) & 1;
array.m_NAL_unit_type = (byte & 0x3F);

int nUnits = range.read16();
Expand Down Expand Up @@ -156,11 +156,45 @@ Error Box_vvcC::parse(BitstreamRange& range)
}


bool Box_vvcC::get_headers(std::vector<uint8_t>* dest) const
{
for (const auto& nal_array : m_nal_array) {
for (const auto& nal : nal_array.m_nal_units) {
assert(nal.size() <= 0xFFFF);
auto size = static_cast<uint16_t>(nal.size());

dest->push_back(0);
dest->push_back(0);
dest->push_back(static_cast<uint8_t>(size >> 8));
dest->push_back(static_cast<uint8_t>(size & 0xFF));

dest->insert(dest->end(), nal.begin(), nal.end());
}
}

return true;
}


void Box_vvcC::append_nal_data(const std::vector<uint8_t>& nal)
{
assert(nal.size()>=2);
uint8_t nal_type = (nal[1] >> 3) & 0x1F;

// insert into existing array if it exists

for (auto& nalarray : m_nal_array) {
if (nalarray.m_NAL_unit_type == nal_type) {
nalarray.m_nal_units.push_back(nal);
return;
}
}

// generate new NAL array

NalArray array;
array.m_array_completeness = true;
array.m_NAL_unit_type = uint8_t(nal[0] >> 1);
array.m_NAL_unit_type = uint8_t((nal[1] >> 3) & 0x1F);
array.m_nal_units.push_back(nal);

m_nal_array.push_back(array);
Expand Down Expand Up @@ -290,6 +324,22 @@ Error Box_vvcC::write(StreamWriter& writer) const

static const char* vvc_chroma_names[4] = {"mono", "4:2:0", "4:2:2", "4:4:4"};

const char* NAL_name(uint8_t nal_type)
{
switch (nal_type) {
case 12: return "OPI";
case 13: return "DCI";
case 14: return "VPS";
case 15: return "SPS";
case 16: return "PPS";
case 17: return "PREFIX_APS";
case 18: return "SUFFIX_APS";
case 19: return "PH";
default: return "?";
}
}


std::string Box_vvcC::dump(Indent& indent) const
{
std::ostringstream sstr;
Expand Down Expand Up @@ -318,23 +368,24 @@ std::string Box_vvcC::dump(Indent& indent) const

sstr << indent << "num of arrays: " << m_nal_array.size() << "\n";

sstr << indent << "config NALs:";
sstr << indent << "config NALs:\n";
for (const auto& nal_array : m_nal_array) {
indent++;
sstr << indent << "NAL type: " << ((int)nal_array.m_NAL_unit_type) << " (" << NAL_name(nal_array.m_NAL_unit_type) << ")\n";
sstr << indent << "array completeness: " << ((int)nal_array.m_array_completeness) << "\n";
sstr << std::hex << std::setw(2) << std::setfill('0') << nal_array.m_NAL_unit_type << "\n";

for (const auto& nal : nal_array.m_nal_units) {
indent++;
std::string ind = indent.get_string();
sstr << write_raw_data_as_hex(nal.data(), nal.size(), ind, ind);
indent--;
}
indent--;
}
sstr << std::dec << std::setw(0) << "\n";

return sstr.str();
}


static std::vector<uint8_t> remove_start_code_emulation(const uint8_t* sps, size_t size)
{
std::vector<uint8_t> out_data;
Expand Down
10 changes: 1 addition & 9 deletions libheif/codecs/vvc.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,7 @@ class Box_vvcC : public FullBox

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

bool get_headers(std::vector<uint8_t>* dest) const
{
// TODO

#if 0
*dest = m_config_NALs;
#endif
return true;
}
bool get_headers(std::vector<uint8_t>* dest) const;

void set_configuration(const configuration& config) { m_configuration = config; }

Expand Down
2 changes: 1 addition & 1 deletion libheif/context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2948,7 +2948,7 @@ Error HeifContext::encode_image_as_vvc(const std::shared_ptr<HeifPixelImage>& im
m_heif_file->set_vvcC_configuration(image_id, config);
}

switch (data[0] >> 1) {
switch (nal_type) {
case 14: // VPS
case 15: // SPS
case 16: // PPS
Expand Down

0 comments on commit 586335e

Please sign in to comment.