Skip to content

Commit

Permalink
simplify code by replacing dynamic_casts with get_property<> function…
Browse files Browse the repository at this point in the history
… calls
  • Loading branch information
farindk committed Sep 22, 2024
1 parent 1f48f78 commit acdc8d9
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 129 deletions.
30 changes: 4 additions & 26 deletions libheif/codecs/avif.cc
Original file line number Diff line number Diff line change
Expand Up @@ -610,39 +610,17 @@ Result<ImageItem::CodedImageData> ImageItem_AVIF::encode(const std::shared_ptr<H

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;
}
}
}

std::shared_ptr<Box_av1C> av1C_box = get_file()->get_property<Box_av1C>(get_id());
if (!av1C_box)
{
return Error(heif_error_Invalid_input,
heif_suberror_No_av1C_box);
}
else if (!av1C_box->get_headers(&data))

std::vector<uint8_t> data;
if (!av1C_box->get_headers(&data))
{
return Error(heif_error_Invalid_input,
heif_suberror_No_item_data);
Expand Down
6 changes: 1 addition & 5 deletions libheif/codecs/grid.cc
Original file line number Diff line number Diff line change
Expand Up @@ -241,11 +241,7 @@ Result<std::shared_ptr<HeifPixelImage>> ImageItem_Grid::decode_full_grid_image(c
}
}


auto ipma = get_file()->get_ipma_box();
auto ipco = get_file()->get_ipco_box();
auto pixi_box = ipco->get_property_for_item_ID(get_id(), ipma, fourcc("pixi"));
auto pixi = std::dynamic_pointer_cast<Box_pixi>(pixi_box);
//auto pixi = get_file()->get_property<Box_pixi>(get_id());

const uint32_t w = grid.get_width();
const uint32_t h = grid.get_height();
Expand Down
29 changes: 4 additions & 25 deletions libheif/codecs/hevc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -725,37 +725,16 @@ Result<ImageItem::CodedImageData> ImageItem_HEVC::encode(const std::shared_ptr<H

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;
}
}
}

std::shared_ptr<Box_hvcC> hvcC_box = get_file()->get_property<Box_hvcC>(get_id());
if (!hvcC_box) {
return Error{heif_error_Invalid_input,
heif_suberror_No_hvcC_box};
}
else if (!hvcC_box->get_headers(&data)) {

std::vector<uint8_t> data;
if (!hvcC_box->get_headers(&data)) {
return Error{heif_error_Invalid_input,
heif_suberror_No_item_data};
}
Expand Down
24 changes: 4 additions & 20 deletions libheif/codecs/jpeg.cc
Original file line number Diff line number Diff line change
Expand Up @@ -165,30 +165,14 @@ Result<ImageItem::CodedImageData> ImageItem_JPEG::encode(const std::shared_ptr<H

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

// --- check if 'jpgC' is present
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_jpgC> jpgC_box;
for (auto& prop : properties) {
if (prop->get_short_type() == fourcc("jpgC")) {
jpgC_box = std::dynamic_pointer_cast<Box_jpgC>(prop);
if (jpgC_box) {
data = jpgC_box->get_data();
break;
}
}
std::shared_ptr<Box_jpgC> jpgC_box = get_file()->get_property<Box_jpgC>(get_id());
if (jpgC_box) {
return jpgC_box->get_data();
}

return data;
return std::vector<uint8_t>{};
}


Expand Down
27 changes: 2 additions & 25 deletions libheif/codecs/jpeg2000.cc
Original file line number Diff line number Diff line change
Expand Up @@ -525,32 +525,9 @@ Result<ImageItem::CodedImageData> ImageItem_JPEG2000::encode(const std::shared_p

Result<std::vector<uint8_t>> ImageItem_JPEG2000::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_j2kH> j2kH_box;
for (auto &prop : properties)
{
if (prop->get_short_type() == fourcc("j2kH"))
{
j2kH_box = std::dynamic_pointer_cast<Box_j2kH>(prop);
if (j2kH_box)
{
break;
}
}
}

std::shared_ptr<Box_j2kH> j2kH_box = get_file()->get_property<Box_j2kH>(get_id());
if (!j2kH_box)
{
// TODO - Correctly Find the j2kH box
Expand All @@ -562,7 +539,7 @@ Result<std::vector<uint8_t>> ImageItem_JPEG2000::read_bitstream_configuration_da
// heif_suberror_No_item_data);
// }

return data;
return std::vector<uint8_t>{};
}


Expand Down
18 changes: 6 additions & 12 deletions libheif/codecs/uncompressed_image.cc
Original file line number Diff line number Diff line change
Expand Up @@ -310,16 +310,13 @@ Error UncompressedImageCodec::get_heif_chroma_uncompressed(const std::shared_ptr

int UncompressedImageCodec::get_luma_bits_per_pixel_from_configuration_unci(const HeifFile& heif_file, heif_item_id imageID)
{
auto ipco = heif_file.get_ipco_box();
auto ipma = heif_file.get_ipma_box();
std::shared_ptr<Box_uncC> uncC_box = heif_file.get_property<Box_uncC>(imageID);
std::shared_ptr<Box_cmpd> cmpd_box = heif_file.get_property<Box_cmpd>(imageID);

auto box1 = ipco->get_property_for_item_ID(imageID, ipma, fourcc("uncC"));
std::shared_ptr<Box_uncC> uncC_box = std::dynamic_pointer_cast<Box_uncC>(box1);
auto box2 = ipco->get_property_for_item_ID(imageID, ipma, fourcc("cmpd"));
std::shared_ptr<Box_cmpd> cmpd_box = std::dynamic_pointer_cast<Box_cmpd>(box2);
if (!uncC_box) {
return -1;
}

if (!cmpd_box) {
if (isKnownUncompressedFrameConfigurationBoxProfile(uncC_box)) {
return 8;
Expand Down Expand Up @@ -362,17 +359,14 @@ int UncompressedImageCodec::get_luma_bits_per_pixel_from_configuration_unci(cons

int UncompressedImageCodec::get_chroma_bits_per_pixel_from_configuration_unci(const HeifFile& heif_file, heif_item_id imageID)
{
auto ipco = heif_file.get_ipco_box();
auto ipma = heif_file.get_ipma_box();
std::shared_ptr<Box_uncC> uncC_box = heif_file.get_property<Box_uncC>(imageID);
std::shared_ptr<Box_cmpd> cmpd_box = heif_file.get_property<Box_cmpd>(imageID);

auto box1 = ipco->get_property_for_item_ID(imageID, ipma, fourcc("uncC"));
std::shared_ptr<Box_uncC> uncC_box = std::dynamic_pointer_cast<Box_uncC>(box1);
auto box2 = ipco->get_property_for_item_ID(imageID, ipma, fourcc("cmpd"));
std::shared_ptr<Box_cmpd> cmpd_box = std::dynamic_pointer_cast<Box_cmpd>(box2);
if (uncC_box && uncC_box->get_version() == 1) {
// All of the version 1 cases are 8 bit
return 8;
}

if (!uncC_box || !cmpd_box) {
return -1;
}
Expand Down
20 changes: 4 additions & 16 deletions libheif/codecs/vvc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -620,8 +620,6 @@ Result<ImageItem::CodedImageData> ImageItem_VVC::encode(const std::shared_ptr<He

Result<std::vector<uint8_t>> ImageItem_VVC::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;
Expand All @@ -634,26 +632,16 @@ Result<std::vector<uint8_t>> ImageItem_VVC::read_bitstream_configuration_data(he

// --- get codec configuration

std::shared_ptr<Box_vvcC> vvcC_box;
for (auto &prop : properties)
{
if (prop->get_short_type() == fourcc("vvcC"))
{
vvcC_box = std::dynamic_pointer_cast<Box_vvcC>(prop);
if (vvcC_box)
{
break;
}
}
}

std::shared_ptr<Box_vvcC> vvcC_box = get_file()->get_property<Box_vvcC>(get_id());
if (!vvcC_box)
{
assert(false);
return Error(heif_error_Invalid_input,
heif_suberror_No_vvcC_box);
}
else if (!vvcC_box->get_headers(&data))

std::vector<uint8_t> data;
if (!vvcC_box->get_headers(&data))
{
return Error(heif_error_Invalid_input,
heif_suberror_No_item_data);
Expand Down

0 comments on commit acdc8d9

Please sign in to comment.