Skip to content

Commit

Permalink
replace all fourcc-type strings with uint32_t constants
Browse files Browse the repository at this point in the history
  • Loading branch information
farindk committed Oct 2, 2024
1 parent 1e64957 commit 1bb435c
Show file tree
Hide file tree
Showing 26 changed files with 80 additions and 104 deletions.
20 changes: 13 additions & 7 deletions libheif/api/libheif/heif.cc
Original file line number Diff line number Diff line change
Expand Up @@ -292,13 +292,13 @@ heif_brand2 heif_read_main_brand(const uint8_t* data, int len)
}


heif_brand2 heif_fourcc_to_brand(const char* fourcc)
heif_brand2 heif_fourcc_to_brand(const char* fourcc_string)
{
if (fourcc == nullptr || !fourcc[0] || !fourcc[1] || !fourcc[2] || !fourcc[3]) {
if (fourcc_string == nullptr || !fourcc_string[0] || !fourcc_string[1] || !fourcc_string[2] || !fourcc_string[3]) {
return 0;
}

return fourcc_to_uint32(fourcc);
return fourcc(fourcc_string);
}


Expand Down Expand Up @@ -337,7 +337,7 @@ int heif_has_compatible_brand(const uint8_t* data, int len, const char* brand_fo
return -2;
}

return ftyp->has_compatible_brand(fourcc_to_uint32(brand_fourcc)) ? 1 : 0;
return ftyp->has_compatible_brand(fourcc(brand_fourcc)) ? 1 : 0;
}


Expand Down Expand Up @@ -3478,7 +3478,7 @@ struct heif_error heif_context_add_image_tile(struct heif_context* ctx,
const struct heif_image* image,
struct heif_encoder* encoder)
{
if (tiled_image->image->get_infe_type() == std::string{"tili"}) {
if (tiled_image->image->get_infe_type() == fourcc("tili")) {
Error err = ctx->context->add_tild_image_tile(tiled_image->image->get_id(), tile_x, tile_y, image->image, encoder);
return err.error_struct(ctx->context.get());
}
Expand Down Expand Up @@ -3642,8 +3642,14 @@ struct heif_error heif_context_add_generic_metadata(struct heif_context* ctx,
const void* data, int size,
const char* item_type, const char* content_type)
{
if (item_type == nullptr || strlen(item_type) != 4) {
return {heif_error_Usage_error,
heif_suberror_Invalid_parameter_value,
"called heif_context_add_generic_metadata() with invalid 'item_type'."};
}

Error error = ctx->context->add_generic_metadata(image_handle->image, data, size,
item_type, content_type, nullptr, heif_metadata_compression_off, nullptr);
fourcc(item_type), content_type, nullptr, heif_metadata_compression_off, nullptr);
if (error != Error::Ok) {
return error.error_struct(ctx->context.get());
}
Expand All @@ -3660,7 +3666,7 @@ struct heif_error heif_context_add_generic_uri_metadata(struct heif_context* ctx
heif_item_id* out_item_id)
{
Error error = ctx->context->add_generic_metadata(image_handle->image, data, size,
"uri ", nullptr, item_uri_type, heif_metadata_compression_off, out_item_id);
fourcc("uri "), nullptr, item_uri_type, heif_metadata_compression_off, out_item_id);
if (error != Error::Ok) {
return error.error_struct(ctx->context.get());
}
Expand Down
8 changes: 7 additions & 1 deletion libheif/api/libheif/heif_items.cc
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,13 @@ struct heif_error heif_context_add_item(struct heif_context* ctx,
const void* data, int size,
heif_item_id* out_item_id)
{
Result<heif_item_id> result = ctx->context->get_heif_file()->add_infe(item_type, (const uint8_t*) data, size);
if (item_type == nullptr || strlen(item_type) != 4) {
return {heif_error_Usage_error,
heif_suberror_Invalid_parameter_value,
"called heif_context_add_item() with invalid 'item_type'."};
}

Result<heif_item_id> result = ctx->context->get_heif_file()->add_infe(fourcc(item_type), (const uint8_t*) data, size);

if (result && out_item_id) {
*out_item_id = result.value;
Expand Down
41 changes: 7 additions & 34 deletions libheif/box.cc
Original file line number Diff line number Diff line change
Expand Up @@ -161,25 +161,6 @@ bool Fraction::is_valid() const
return denominator != 0;
}

static uint32_t from_fourcc(const char* string)
{
return ((string[0] << 24) |
(string[1] << 16) |
(string[2] << 8) |
(string[3]));
}

std::string to_fourcc(uint32_t code)
{
std::string str(" ");
str[0] = static_cast<char>((code >> 24) & 0xFF);
str[1] = static_cast<char>((code >> 16) & 0xFF);
str[2] = static_cast<char>((code >> 8) & 0xFF);
str[3] = static_cast<char>((code >> 0) & 0xFF);

return str;
}


BoxHeader::BoxHeader() = default;

Expand Down Expand Up @@ -2089,9 +2070,6 @@ Error Box_infe::parse(BitstreamRange& range)

m_item_protection_index = range.read16();
m_item_type_4cc = range.read32();
if (m_item_type_4cc != 0) {
m_item_type = to_fourcc(m_item_type_4cc);
}

m_item_name = range.read_string();
if (m_item_type_4cc == fourcc("mime")) {
Expand Down Expand Up @@ -2120,7 +2098,7 @@ void Box_infe::derive_box_version()
}


if (m_item_type != "") {
if (m_item_type_4cc != 0) {
min_version = std::max(min_version, 2);
}

Expand Down Expand Up @@ -2163,19 +2141,14 @@ Error Box_infe::write(StreamWriter& writer) const

writer.write16(m_item_protection_index);

if (m_item_type.empty()) {
writer.write32(0);
}
else {
writer.write32(from_fourcc(m_item_type.c_str()));
}
writer.write32(m_item_type_4cc);

writer.write(m_item_name);
if (m_item_type == "mime") {
if (m_item_type_4cc == fourcc("mime")) {
writer.write(m_content_type);
writer.write(m_content_encoding);
}
else if (m_item_type == "uri ") {
else if (m_item_type_4cc == fourcc("uri ")) {
writer.write(m_item_uri_type);
}
}
Expand All @@ -2193,15 +2166,15 @@ std::string Box_infe::dump(Indent& indent) const

sstr << indent << "item_ID: " << m_item_ID << "\n"
<< indent << "item_protection_index: " << m_item_protection_index << "\n"
<< indent << "item_type: " << m_item_type << "\n"
<< indent << "item_type: " << to_fourcc(m_item_type_4cc) << "\n"
<< indent << "item_name: " << m_item_name << "\n";

if (m_item_type == "mime") {
if (m_item_type_4cc == fourcc("mime")) {
sstr << indent << "content_type: " << m_content_type << "\n"
<< indent << "content_encoding: " << m_content_encoding << "\n";
}

if (m_item_type == "uri ") {
if (m_item_type_4cc == fourcc("uri ")) {
sstr << indent << "item uri type: " << m_item_uri_type << "\n";
}

Expand Down
7 changes: 1 addition & 6 deletions libheif/box.h
Original file line number Diff line number Diff line change
Expand Up @@ -589,13 +589,9 @@ class Box_infe : public FullBox

void set_item_ID(heif_item_id id) { m_item_ID = id; }

const std::string& get_item_type() const { return m_item_type; }

uint32_t get_item_type_4cc() const { return m_item_type_4cc; }

void set_item_type(const std::string& type) { m_item_type = type; m_item_type_4cc = fourcc_to_uint32(type.c_str()); }

void set_item_type_4cc(uint32_t type) { m_item_type_4cc = type; m_item_type = to_fourcc(type); }
void set_item_type_4cc(uint32_t type) { m_item_type_4cc = type; }

void set_item_name(const std::string& name) { m_item_name = name; }

Expand Down Expand Up @@ -625,7 +621,6 @@ class Box_infe : public FullBox
uint16_t m_item_protection_index = 0;

uint32_t m_item_type_4cc = 0;
std::string m_item_type; // deprecated, prefer to use m_item_type_4cc
std::string m_item_name;
std::string m_content_type;
std::string m_content_encoding;
Expand Down
2 changes: 1 addition & 1 deletion libheif/codecs/avc.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class ImageItem_AVC : public ImageItem

ImageItem_AVC(HeifContext* ctx) : ImageItem(ctx) {}

const char* get_infe_type() const override { return "avc1"; }
uint32_t get_infe_type() const override { return fourcc("avc1"); }

const char* get_auxC_alpha_channel_type() const override { return "urn:mpeg:mpegB:cicp:systems:auxiliary:alpha"; }

Expand Down
2 changes: 1 addition & 1 deletion libheif/codecs/avif.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ class ImageItem_AVIF : public ImageItem

ImageItem_AVIF(HeifContext* ctx) : ImageItem(ctx) {}

const char* get_infe_type() const override { return "av01"; }
uint32_t get_infe_type() const override { return fourcc("av01"); }

const char* get_auxC_alpha_channel_type() const override { return "urn:mpeg:mpegB:cicp:systems:auxiliary:alpha"; }

Expand Down
2 changes: 1 addition & 1 deletion libheif/codecs/grid.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class ImageItem_Grid : public ImageItem

ImageItem_Grid(HeifContext* ctx);

const char* get_infe_type() const override { return "grid"; }
uint32_t get_infe_type() const override { return fourcc("grid"); }

// const heif_color_profile_nclx* get_forced_output_nclx() const override { return nullptr; }

Expand Down
2 changes: 1 addition & 1 deletion libheif/codecs/hevc.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ class ImageItem_HEVC : public ImageItem

ImageItem_HEVC(HeifContext* ctx) : ImageItem(ctx) {}

const char* get_infe_type() const override { return "hvc1"; }
uint32_t get_infe_type() const override { return fourcc("hvc1"); }

// TODO: MIAF says that the *:hevc:* urn is deprecated and we should use "urn:mpeg:mpegB:cicp:systems:auxiliary:alpha"
const char* get_auxC_alpha_channel_type() const override { return "urn:mpeg:hevc:2015:auxid:1"; }
Expand Down
4 changes: 2 additions & 2 deletions libheif/codecs/iden.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class ImageItem_iden : public ImageItem

ImageItem_iden(HeifContext* ctx);

const char* get_infe_type() const override { return "iden"; }
uint32_t get_infe_type() const override { return fourcc("iden"); }

// const heif_color_profile_nclx* get_forced_output_nclx() const override { return nullptr; }

Expand All @@ -52,7 +52,7 @@ class ImageItem_iden : public ImageItem
enum heif_image_input_class input_class) override
{
return Error{heif_error_Unsupported_feature,
heif_suberror_Unspecified, "Cannot encode image to 'iovl'"};
heif_suberror_Unspecified, "Cannot encode image to 'iden'"};
}

Result<std::shared_ptr<HeifPixelImage>> decode_compressed_image(const struct heif_decoding_options& options,
Expand Down
2 changes: 1 addition & 1 deletion libheif/codecs/image_item.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class ImageItem : public ErrorBuffer
struct heif_encoder* encoder,
const struct heif_encoding_options& options);

virtual const char* get_infe_type() const { return "????"; } // TODO = 0;
virtual uint32_t get_infe_type() const { return 0; }

virtual const char* get_auxC_alpha_channel_type() const { return "urn:mpeg:mpegB:cicp:systems:auxiliary:alpha"; }

Expand Down
2 changes: 1 addition & 1 deletion libheif/codecs/jpeg.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class ImageItem_JPEG : public ImageItem

ImageItem_JPEG(HeifContext* ctx) : ImageItem(ctx) { }

const char* get_infe_type() const override { return "jpeg"; }
uint32_t get_infe_type() const override { return fourcc("jpeg"); }

const heif_color_profile_nclx* get_forced_output_nclx() const override;

Expand Down
2 changes: 1 addition & 1 deletion libheif/codecs/jpeg2000.h
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ class ImageItem_JPEG2000 : public ImageItem

ImageItem_JPEG2000(HeifContext* ctx) : ImageItem(ctx) {}

const char* get_infe_type() const override { return "j2k1"; }
uint32_t get_infe_type() const override { return fourcc("j2k1"); }

heif_compression_format get_compression_format() const override { return heif_compression_JPEG2000; }

Expand Down
2 changes: 1 addition & 1 deletion libheif/codecs/mask_image.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class ImageItem_mask : public ImageItem

ImageItem_mask(HeifContext* ctx) : ImageItem(ctx) {}

const char* get_infe_type() const override { return "mski"; }
uint32_t get_infe_type() const override { return fourcc("mski"); }

const heif_color_profile_nclx* get_forced_output_nclx() const override { return nullptr; }

Expand Down
2 changes: 1 addition & 1 deletion libheif/codecs/overlay.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class ImageItem_Overlay : public ImageItem

ImageItem_Overlay(HeifContext* ctx);

const char* get_infe_type() const override { return "iovl"; }
uint32_t get_infe_type() const override { return fourcc("iovl"); }

// const heif_color_profile_nclx* get_forced_output_nclx() const override { return nullptr; }

Expand Down
2 changes: 1 addition & 1 deletion libheif/codecs/tild.cc
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ ImageItem_Tild::add_new_tild_item(HeifContext* ctx, const heif_tild_image_parame

auto file = ctx->get_heif_file();

heif_item_id tild_id = ctx->get_heif_file()->add_new_image("tild");
heif_item_id tild_id = ctx->get_heif_file()->add_new_image(fourcc("tild"));
auto tild_image = std::make_shared<ImageItem_Tild>(ctx, tild_id);
ctx->insert_new_image(tild_id, tild_image);

Expand Down
2 changes: 1 addition & 1 deletion libheif/codecs/tild.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ class ImageItem_Tild : public ImageItem

ImageItem_Tild(HeifContext* ctx);

const char* get_infe_type() const override { return "tili"; }
uint32_t get_infe_type() const override { return fourcc("tili"); }

// const heif_color_profile_nclx* get_forced_output_nclx() const override { return nullptr; }

Expand Down
4 changes: 2 additions & 2 deletions libheif/codecs/uncompressed/unc_boxes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -201,14 +201,14 @@ Error Box_uncC::parse(BitstreamRange& range)
parse_full_box_header(range);
m_profile = range.read32();
if (get_version() == 1) {
if (m_profile == fourcc_to_uint32("rgb3")) {
if (m_profile == fourcc("rgb3")) {
Box_uncC::Component component0 = {0, 8, component_format_unsigned, 0};
add_component(component0);
Box_uncC::Component component1 = {1, 8, component_format_unsigned, 0};
add_component(component1);
Box_uncC::Component component2 = {2, 8, component_format_unsigned, 0};
add_component(component2);
} else if ((m_profile == fourcc_to_uint32("rgba")) || (m_profile == fourcc_to_uint32("abgr"))) {
} else if ((m_profile == fourcc("rgba")) || (m_profile == fourcc("abgr"))) {
Box_uncC::Component component0 = {0, 8, component_format_unsigned, 0};
add_component(component0);
Box_uncC::Component component1 = {1, 8, component_format_unsigned, 0};
Expand Down
6 changes: 3 additions & 3 deletions libheif/codecs/uncompressed/unc_image.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1777,9 +1777,9 @@ static void maybe_make_minimised_uncC(std::shared_ptr<Box_uncC>& uncC, const std
return;
}
if (image->get_chroma_format() == heif_chroma_interleaved_RGBA) {
uncC->set_profile(fourcc_to_uint32("rgba"));
uncC->set_profile(fourcc("rgba"));
} else {
uncC->set_profile(fourcc_to_uint32("rgb3"));
uncC->set_profile(fourcc("rgb3"));
}
uncC->set_version(1);
}
Expand Down Expand Up @@ -2039,7 +2039,7 @@ Result<std::shared_ptr<ImageItem_uncompressed>> ImageItem_uncompressed::add_unci

auto file = ctx->get_heif_file();

heif_item_id unci_id = ctx->get_heif_file()->add_new_image("unci");
heif_item_id unci_id = ctx->get_heif_file()->add_new_image(fourcc("unci"));
auto unci_image = std::make_shared<ImageItem_uncompressed>(ctx, unci_id);
ctx->insert_new_image(unci_id, unci_image);

Expand Down
2 changes: 1 addition & 1 deletion libheif/codecs/uncompressed/unc_image.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class ImageItem_uncompressed : public ImageItem

ImageItem_uncompressed(HeifContext* ctx) : ImageItem(ctx) {}

const char* get_infe_type() const override { return "unci"; }
uint32_t get_infe_type() const override { return fourcc("unci"); }

heif_compression_format get_compression_format() const override { return heif_compression_uncompressed; }

Expand Down
2 changes: 1 addition & 1 deletion libheif/codecs/vvc.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class ImageItem_VVC : public ImageItem

ImageItem_VVC(HeifContext* ctx) : ImageItem(ctx) {}

const char* get_infe_type() const override { return "vvc1"; }
uint32_t get_infe_type() const override { return fourcc("vvc1"); }

const char* get_auxC_alpha_channel_type() const override { return "urn:mpeg:mpegB:cicp:systems:auxiliary:alpha"; }

Expand Down
12 changes: 12 additions & 0 deletions libheif/common_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,15 @@ uint8_t compute_avif_profile(int bits_per_pixel, heif_chroma chroma)
return 2;
}
}


std::string to_fourcc(uint32_t code)
{
std::string str(" ");
str[0] = static_cast<char>((code >> 24) & 0xFF);
str[1] = static_cast<char>((code >> 16) & 0xFF);
str[2] = static_cast<char>((code >> 8) & 0xFF);
str[3] = static_cast<char>((code >> 0) & 0xFF);

return str;
}
5 changes: 1 addition & 4 deletions libheif/common_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,14 @@
#endif


constexpr inline uint32_t fourcc_to_uint32(const char* id)
constexpr inline uint32_t fourcc(const char* id)
{
return (((((uint32_t) id[0])&0xFF) << 24) |
((((uint32_t) id[1])&0xFF) << 16) |
((((uint32_t) id[2])&0xFF) << 8) |
((((uint32_t) id[3])&0xFF) << 0));
}

// abbreviation
constexpr inline uint32_t fourcc(const char* id) { return fourcc_to_uint32(id); }

std::string to_fourcc(uint32_t code);


Expand Down
Loading

0 comments on commit 1bb435c

Please sign in to comment.