|
| 1 | +/* |
| 2 | + * HEIF EVC codec. |
| 3 | + * Copyright (c) 2024 Brad Hards <[email protected]> |
| 4 | + * |
| 5 | + * This file is part of libheif. |
| 6 | + * |
| 7 | + * libheif is free software: you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU Lesser General Public License as |
| 9 | + * published by the Free Software Foundation, either version 3 of |
| 10 | + * the License, or (at your option) any later version. |
| 11 | + * |
| 12 | + * libheif is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU Lesser General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU Lesser General Public License |
| 18 | + * along with libheif. If not, see <http://www.gnu.org/licenses/>. |
| 19 | + */ |
| 20 | + |
| 21 | +#include "evc_boxes.h" |
| 22 | +#include "bitstream.h" |
| 23 | +#include "error.h" |
| 24 | +#include "file.h" |
| 25 | + |
| 26 | +#include <iomanip> |
| 27 | +#include <libheif/api_structs.h> |
| 28 | + |
| 29 | + |
| 30 | + |
| 31 | +Error Box_evcC::parse(BitstreamRange& range, const heif_security_limits* limits) |
| 32 | +{ |
| 33 | + m_configuration.configurationVersion = range.read8(); |
| 34 | + m_configuration.profile_idc = range.read8(); |
| 35 | + m_configuration.level_idc = range.read8(); |
| 36 | + m_configuration.toolset_idc_h = range.read32(); |
| 37 | + m_configuration.toolset_idc_l = range.read32(); |
| 38 | + uint8_t b = range.read8(); |
| 39 | + m_configuration.chroma_format_idc = (b >> 6) & 0b11; |
| 40 | + uint8_t bit_depth_luma_minus8 = (b >> 3) & 0b111; |
| 41 | + m_configuration.bit_depth_luma = bit_depth_luma_minus8 + 8; |
| 42 | + uint8_t bit_depth_chroma_minus8 = b & 0b111; |
| 43 | + m_configuration.bit_depth_chroma = bit_depth_chroma_minus8 + 8; |
| 44 | + m_configuration.pic_width_in_luma_samples = range.read16(); |
| 45 | + m_configuration.pic_height_in_luma_samples = range.read16(); |
| 46 | + b = range.read8(); |
| 47 | + uint8_t lengthSizeMinus1 = b & 0b11; |
| 48 | + m_configuration.lengthSize = lengthSizeMinus1 + 1; |
| 49 | + uint8_t num_of_arrays = range.read8(); |
| 50 | + for (uint8_t j = 0; j < num_of_arrays && !range.error(); j++) { |
| 51 | + NalArray array; |
| 52 | + b = range.read8(); |
| 53 | + array.array_completeness = ((b & 0x80) == 0x80); |
| 54 | + array.NAL_unit_type = (b & 0b00111111); |
| 55 | + uint16_t num_nalus = range.read16(); |
| 56 | + for (int i = 0; i < num_nalus && !range.error(); i++) { |
| 57 | + uint16_t nal_unit_length = range.read16(); |
| 58 | + if (nal_unit_length == 0) { |
| 59 | + // Ignore empty NAL units. |
| 60 | + continue; |
| 61 | + } |
| 62 | + std::vector<uint8_t> nal_unit; |
| 63 | + if (range.prepare_read(nal_unit_length)) { |
| 64 | + nal_unit.resize(nal_unit_length); |
| 65 | + bool success = range.get_istream()->read((char*) nal_unit.data(), nal_unit_length); |
| 66 | + if (!success) { |
| 67 | + return Error{heif_error_Invalid_input, heif_suberror_End_of_data, "error while reading evcC box"}; |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + array.nal_units.push_back(std::move(nal_unit)); |
| 72 | + } |
| 73 | + m_nal_array.push_back(array); |
| 74 | + } |
| 75 | + |
| 76 | + return range.get_error(); |
| 77 | +} |
| 78 | + |
| 79 | + |
| 80 | +std::string Box_evcC::dump(Indent& indent) const |
| 81 | +{ |
| 82 | + std::ostringstream sstr; |
| 83 | + sstr << Box::dump(indent); |
| 84 | + // TODO: decode more of this |
| 85 | + sstr << indent << "configurationVersion: " << ((int)m_configuration.configurationVersion) << "\n"; |
| 86 | + sstr << indent << "profile_idc: " << ((int)m_configuration.profile_idc) << "\n"; |
| 87 | + sstr << indent << "level_idc: " << ((int)m_configuration.level_idc) << "\n"; |
| 88 | + sstr << indent << "toolset_idc_h: " << m_configuration.toolset_idc_h << "\n"; |
| 89 | + sstr << indent << "toolset_idc_l: " << m_configuration.toolset_idc_l << "\n"; |
| 90 | + sstr << indent << "chroma_format_idc: " << ((int)m_configuration.chroma_format_idc) |
| 91 | + << " (" << get_chroma_format_as_text() << ")\n"; |
| 92 | + sstr << indent << "bit_depth_luma: " << ((int)m_configuration.bit_depth_luma) << "\n"; |
| 93 | + sstr << indent << "bit_depth_chroma: " << ((int)m_configuration.bit_depth_chroma) << "\n"; |
| 94 | + sstr << indent << "pic_width_in_luma_samples: " << m_configuration.pic_width_in_luma_samples << "\n"; |
| 95 | + sstr << indent << "pic_height_in_luma_samples: " << m_configuration.pic_height_in_luma_samples << "\n"; |
| 96 | + sstr << indent << "length_size: " << ((int)m_configuration.lengthSize) << "\n"; |
| 97 | + for (const auto &array : m_nal_array) |
| 98 | + { |
| 99 | + sstr << indent << "<array>\n"; |
| 100 | + |
| 101 | + indent++; |
| 102 | + sstr << indent << "array_completeness: " << (array.array_completeness ? "true" : "false") << "\n" |
| 103 | + << indent << "NAL_unit_type: " << ((int) array.NAL_unit_type) << "\n"; |
| 104 | + |
| 105 | + for (const auto& unit : array.nal_units) { |
| 106 | + sstr << indent; |
| 107 | + for (uint8_t b : unit) { |
| 108 | + sstr << std::setfill('0') << std::setw(2) << std::hex << ((int) b) << " "; |
| 109 | + } |
| 110 | + sstr << "\n"; |
| 111 | + sstr << std::dec; |
| 112 | + } |
| 113 | + |
| 114 | + indent--; |
| 115 | + } |
| 116 | + return sstr.str(); |
| 117 | +} |
| 118 | + |
| 119 | +std::string Box_evcC::get_chroma_format_as_text() const |
| 120 | +{ |
| 121 | + switch (m_configuration.chroma_format_idc) |
| 122 | + { |
| 123 | + case 0: |
| 124 | + return std::string("Monochrome"); |
| 125 | + case 1: |
| 126 | + return std::string("4:2:0"); |
| 127 | + case 2: |
| 128 | + return std::string("4:2:2"); |
| 129 | + case 3: |
| 130 | + return std::string("4:4:4"); |
| 131 | + default: |
| 132 | + return std::string("Invalid"); |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +Error Box_evcC::write(StreamWriter& writer) const |
| 137 | +{ |
| 138 | + size_t box_start = reserve_box_header_space(writer); |
| 139 | + |
| 140 | + writer.write8(m_configuration.configurationVersion); |
| 141 | + writer.write8(m_configuration.profile_idc); |
| 142 | + writer.write8(m_configuration.level_idc); |
| 143 | + writer.write32(m_configuration.toolset_idc_h); |
| 144 | + writer.write32(m_configuration.toolset_idc_l); |
| 145 | + uint8_t bit_depth_luma_minus8 = ((m_configuration.bit_depth_luma - 8) & 0b111); |
| 146 | + uint8_t bit_depth_chroma_minus8 = ((m_configuration.bit_depth_chroma - 8) & 0b111); |
| 147 | + uint8_t b = (m_configuration.chroma_format_idc << 6) | (bit_depth_luma_minus8 << 3) | bit_depth_chroma_minus8; |
| 148 | + writer.write8(b); |
| 149 | + writer.write16(m_configuration.pic_width_in_luma_samples); |
| 150 | + writer.write16(m_configuration.pic_height_in_luma_samples); |
| 151 | + writer.write8(m_configuration.lengthSize - 1); |
| 152 | + writer.write8((uint8_t)m_nal_array.size()); |
| 153 | + for (const NalArray& array : m_nal_array) { |
| 154 | + |
| 155 | + writer.write8((uint8_t) ((array.array_completeness? 0x80: 0x00) | |
| 156 | + (array.NAL_unit_type & 0b00111111))); |
| 157 | + |
| 158 | + writer.write16((uint16_t)array.nal_units.size()); |
| 159 | + |
| 160 | + for (const std::vector<uint8_t>& nal_unit : array.nal_units) { |
| 161 | + writer.write16((uint16_t) nal_unit.size()); |
| 162 | + writer.write(nal_unit); |
| 163 | + } |
| 164 | + } |
| 165 | + prepend_header(writer, box_start); |
| 166 | + |
| 167 | + return Error::Ok; |
| 168 | +} |
0 commit comments