Skip to content

Commit

Permalink
read bitstream config for HEVC and AV1 in ImageItem* classes
Browse files Browse the repository at this point in the history
  • Loading branch information
farindk committed Aug 18, 2024
1 parent 3cca72a commit fb06b56
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 5 deletions.
45 changes: 45 additions & 0 deletions libheif/codecs/avif.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "bitstream.h"
#include "common_utils.h"
#include "libheif/api_structs.h"
#include "file.h"
#include <iomanip>
#include <limits>
#include <string>
Expand Down Expand Up @@ -605,3 +606,47 @@ Result<ImageItem::CodedImageData> ImageItem_AVIF::encode(const std::shared_ptr<H

return codedImage;
}


Result<std::vector<uint8_t>> ImageItem_AVIF::read_bitstream_configuration_data(heif_item_id itemId) const
{
std::vector<uint8_t> data;

// --- get properties for this image

std::vector<std::shared_ptr<Box>> properties;
auto ipma_box = get_file()->get_ipma_box();
Error err = get_file()->get_ipco_box()->get_properties_for_item_ID(itemId, ipma_box, properties);
if (err)
{
return err;
}

// --- get codec configuration

std::shared_ptr<Box_av1C> av1C_box;
for (auto &prop : properties)
{
if (prop->get_short_type() == fourcc("av1C"))
{
av1C_box = std::dynamic_pointer_cast<Box_av1C>(prop);
if (av1C_box)
{
break;
}
}
}

if (!av1C_box)
{
return Error(heif_error_Invalid_input,
heif_suberror_No_av1C_box);
}
else if (!av1C_box->get_headers(&data))
{
return Error(heif_error_Invalid_input,
heif_suberror_No_item_data);
}

return data;
}
3 changes: 3 additions & 0 deletions libheif/codecs/avif.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,9 @@ class ImageItem_AVIF : public ImageItem
struct heif_encoder* encoder,
const struct heif_encoding_options& options,
enum heif_image_input_class input_class) override;

protected:
Result<std::vector<uint8_t>> read_bitstream_configuration_data(heif_item_id itemId) const override;
};

#endif
41 changes: 41 additions & 0 deletions libheif/codecs/hevc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "hevc.h"
#include "bitstream.h"
#include "error.h"
#include "file.h"

#include <cassert>
#include <cmath>
Expand Down Expand Up @@ -721,3 +722,43 @@ Result<ImageItem::CodedImageData> ImageItem_HEVC::encode(const std::shared_ptr<H

return codedImage;
}

Result<std::vector<uint8_t>> ImageItem_HEVC::read_bitstream_configuration_data(heif_item_id itemId) const
{
std::vector<uint8_t> data;

// --- get properties for this image
std::vector<std::shared_ptr<Box>> properties;
auto ipma_box = get_file()->get_ipma_box();
Error err = get_file()->get_ipco_box()->get_properties_for_item_ID(itemId, ipma_box, properties);
if (err)
{
return err;
}

// --- get codec configuration

std::shared_ptr<Box_hvcC> hvcC_box;
for (auto &prop : properties)
{
if (prop->get_short_type() == fourcc("hvcC"))
{
hvcC_box = std::dynamic_pointer_cast<Box_hvcC>(prop);
if (hvcC_box)
{
break;
}
}
}

if (!hvcC_box) {
return Error{heif_error_Invalid_input,
heif_suberror_No_hvcC_box};
}
else if (!hvcC_box->get_headers(&data)) {
return Error{heif_error_Invalid_input,
heif_suberror_No_item_data};
}

return data;
}
4 changes: 4 additions & 0 deletions libheif/codecs/hevc.h
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ class ImageItem_HEVC : public ImageItem

heif_compression_format get_compression_format() const override { return heif_compression_HEVC; }

protected:
Result<std::vector<uint8_t>> read_bitstream_configuration_data(heif_item_id itemId) const override;

public:

Result<CodedImageData> encode(const std::shared_ptr<HeifPixelImage>& image,
struct heif_encoder* encoder,
Expand Down
4 changes: 2 additions & 2 deletions libheif/codecs/image_item.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1026,12 +1026,12 @@ Result<std::shared_ptr<HeifPixelImage>> ImageItem::decode_image(heif_colorspace
}


std::vector<uint8_t> ImageItem::read_bitstream_configuration_data_override(heif_item_id itemId, heif_compression_format format) const
Result<std::vector<uint8_t>> ImageItem::read_bitstream_configuration_data_override(heif_item_id itemId, heif_compression_format format) const
{
auto item_codec = ImageItem::alloc_for_compression_format(const_cast<HeifContext*>(get_context()), format);
assert(item_codec);

return read_bitstream_configuration_data(itemId);
return item_codec->read_bitstream_configuration_data(itemId);
}


Expand Down
4 changes: 2 additions & 2 deletions libheif/codecs/image_item.h
Original file line number Diff line number Diff line change
Expand Up @@ -384,9 +384,9 @@ class ImageItem : public ErrorBuffer
const struct heif_decoding_options& options,
const std::vector<uint8_t>& data);

std::vector<uint8_t> read_bitstream_configuration_data_override(heif_item_id itemId, heif_compression_format format) const;
Result<std::vector<uint8_t>> read_bitstream_configuration_data_override(heif_item_id itemId, heif_compression_format format) const;

virtual std::vector<uint8_t> read_bitstream_configuration_data(heif_item_id itemId) const { return {}; }
virtual Result<std::vector<uint8_t>> read_bitstream_configuration_data(heif_item_id itemId) const { return std::vector<uint8_t>{}; }

virtual Result<CodedImageData> encode(const std::shared_ptr<HeifPixelImage>& image,
struct heif_encoder* encoder,
Expand Down
7 changes: 6 additions & 1 deletion libheif/codecs/tild.cc
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,12 @@ Result<std::shared_ptr<HeifPixelImage>> ImageItem_Tild::decode_grid_tile(const h

// --- get compressed data

std::vector<uint8_t> data = read_bitstream_configuration_data_override(get_id(), format);
Result<std::vector<uint8_t>> dataResult = read_bitstream_configuration_data_override(get_id(), format);
if (dataResult.error) {
return dataResult.error;
}

std::vector<uint8_t>& data = dataResult.value;

// --- decode

Expand Down
3 changes: 3 additions & 0 deletions libheif/file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1023,6 +1023,8 @@ const Error HeifFile::do_decompress_data(std::shared_ptr<Box_cmpC> &cmpC_box, st
}
#endif


// TODO: replace with function in ImageItem
const Error HeifFile::get_compressed_image_data_hvc1(heif_item_id ID, std::vector<uint8_t> *data, const Box_iloc::Item *item) const
{
// --- get properties for this image
Expand Down Expand Up @@ -1108,6 +1110,7 @@ const Error HeifFile::get_compressed_image_data_vvc(heif_item_id ID, std::vector
return m_iloc_box->read_data(*item, m_input_stream, m_idat_box, data);
}

// TODO: replace with function in ImageItem
const Error HeifFile::get_compressed_image_data_av1(heif_item_id ID, std::vector<uint8_t> *data, const Box_iloc::Item *item) const
{
// --- --- --- AV1
Expand Down

0 comments on commit fb06b56

Please sign in to comment.