diff --git a/libheif/box.cc b/libheif/box.cc index e752ac48076..f79df7aecfb 100644 --- a/libheif/box.cc +++ b/libheif/box.cc @@ -3087,3 +3087,27 @@ Error Box_udes::write(StreamWriter& writer) const prepend_header(writer, box_start); return Error::Ok; } + + +std::string Box_uuid::dump(Indent& indent) const +{ + std::ostringstream sstr; + sstr << Box::dump(indent); + sstr << indent << "uuid_type: "; + for (uint8_t byte: m_uuid_type) { + sstr << byte << " "; + } + sstr << "\n"; + return sstr.str(); +} + +Error Box_uuid::write(StreamWriter& writer) const +{ + size_t box_start = reserve_box_header_space(writer); + writer.write(m_uuid_data); + prepend_header(writer, box_start); + return Error::Ok; +} + + + diff --git a/libheif/box.h b/libheif/box.h index 3a651f6ea95..ff59615f44c 100644 --- a/libheif/box.h +++ b/libheif/box.h @@ -127,6 +127,7 @@ class BoxHeader void set_short_type(uint32_t type) { m_type = type; } + void set_uuid_type(const std::vector& uuid_type) { m_uuid_type = uuid_type; } Error parse_header(BitstreamRange& range); @@ -140,9 +141,9 @@ class BoxHeader uint64_t m_size = 0; uint32_t m_type = 0; - std::vector m_uuid_type; protected: + std::vector m_uuid_type; uint32_t m_header_size = 0; }; @@ -1058,4 +1059,32 @@ class Box_udes : public FullBox std::string m_tags; }; +/** + * Universally Unique Identifier. + * + * A box of type "uuid" is used to store user-defined data. + * The uuid_type (aka extended_type) in the BoxHeader is used to identify the uuid_data. + * + * See ISO/IEC 14496-12 Section 4.2.2 and Section 11.1. + */ +class Box_uuid : public Box +{ +public: + Box_uuid() + { + set_short_type(fourcc("uuid")); + } + + std::string dump(Indent&) const override; + + Error write(StreamWriter& writer) const override; + + std::vector get_uuid_data() const { return m_uuid_data; } + + void set_uuid_data(const std::vector& uuid_data) { m_uuid_data = uuid_data; } + +private: + std::vector m_uuid_data; +}; + #endif diff --git a/libheif/heif_properties.cc b/libheif/heif_properties.cc index d73cea41ca9..d962da52224 100644 --- a/libheif/heif_properties.cc +++ b/libheif/heif_properties.cc @@ -300,3 +300,28 @@ void heif_property_user_description_release(struct heif_property_user_descriptio delete udes; } + +struct heif_error heif_item_add_property_uuid(const struct heif_context* context, + heif_item_id itemId, + const struct heif_property_uuid* uuid, + heif_property_id* out_propertyId) +{ + if (!context || !uuid || !uuid->data) { + return {heif_error_Usage_error, heif_suberror_Null_pointer_argument, "NULL argument passed in"}; + } + + std::vector data(static_cast(uuid->data), static_cast(uuid->data) + uuid->size); + std::vector uuid_type(uuid->uuid_type, uuid->uuid_type + 16); + + auto uuid_box = std::make_shared(); + uuid_box->set_uuid_type(uuid_type); + uuid_box->set_uuid_data(data); + + heif_property_id id = context->context->add_property(itemId, uuid_box, false); + + if (out_propertyId) { + *out_propertyId = id; + } + + return heif_error_success; +} \ No newline at end of file diff --git a/libheif/heif_properties.h b/libheif/heif_properties.h index 4ed15c8a904..e2bffe4f7a0 100644 --- a/libheif/heif_properties.h +++ b/libheif/heif_properties.h @@ -37,7 +37,8 @@ enum heif_item_property_type heif_item_property_type_transform_mirror = heif_fourcc('i', 'm', 'i', 'r'), heif_item_property_type_transform_rotation = heif_fourcc('i', 'r', 'o', 't'), heif_item_property_type_transform_crop = heif_fourcc('c', 'l', 'a', 'p'), - heif_item_property_type_image_size = heif_fourcc('i', 's', 'p', 'e') + heif_item_property_type_image_size = heif_fourcc('i', 's', 'p', 'e'), + heif_item_property_type_uuid = heif_fourcc('u', 'u', 'i', 'd'), }; // Get the heif_property_id for a heif_item_id. @@ -131,6 +132,18 @@ void heif_item_get_property_transform_crop_borders(const struct heif_context* co int image_width, int image_height, int* left, int* top, int* right, int* bottom); +struct heif_property_uuid +{ + void* data; + size_t size; + uint8_t uuid_type[16]; // The Extended Type +}; +LIBHEIF_API +struct heif_error heif_item_add_property_uuid(const struct heif_context* context, + heif_item_id itemId, + const struct heif_property_uuid* uuid, + heif_property_id* out_propertyId); + #ifdef __cplusplus } #endif