Skip to content

Commit

Permalink
More error checking
Browse files Browse the repository at this point in the history
  • Loading branch information
dukesook committed Nov 7, 2023
1 parent 70a85c4 commit 8270e93
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 37 deletions.
76 changes: 48 additions & 28 deletions libheif/heif_properties.cc
Original file line number Diff line number Diff line change
Expand Up @@ -301,17 +301,23 @@ void heif_property_user_description_release(struct heif_property_user_descriptio
}


struct heif_error heif_property_set_clock_info(const struct heif_context* context,
struct heif_error heif_property_set_clock_info(const struct heif_context* ctx,
heif_item_id itemId,
heif_tai_clock_info clock,
heif_property_id* out_propertyId)
{
if (!context) {
if (!ctx) {
return {heif_error_Usage_error, heif_suberror_Null_pointer_argument, "NULL passed"};
}

// Check if itemId exists
auto file = ctx->context->get_heif_file();
if (!file->image_exists(itemId)) {
return {heif_error_Input_does_not_exist, heif_suberror_Invalid_parameter_value, "itemId does not exist"};
}

// Create new taic if one doesn't exist for the itemId.
auto taic = context->context->get_heif_file()->get_property<Box_taic>(itemId);
auto taic = ctx->context->get_heif_file()->get_property<Box_taic>(itemId);
if (!taic) {
taic = std::make_shared<Box_taic>();
}
Expand All @@ -322,7 +328,7 @@ struct heif_error heif_property_set_clock_info(const struct heif_context* contex
taic->set_clock_source(clock.clock_source);

bool essential = false;
heif_property_id id = context->context->add_property(itemId, taic, essential);
heif_property_id id = ctx->context->add_property(itemId, taic, essential);

if (out_propertyId) {
*out_propertyId = id;
Expand All @@ -331,23 +337,28 @@ struct heif_error heif_property_set_clock_info(const struct heif_context* contex
return heif_error_success;
}

struct heif_error heif_property_get_clock_info(const struct heif_context* context,
struct heif_error heif_property_get_clock_info(const struct heif_context* ctx,
heif_item_id itemId,
heif_tai_clock_info* out_clock)
{
if (!context) {
if (!ctx) {
return {heif_error_Usage_error, heif_suberror_Invalid_parameter_value, "NULL heif_context passed in"};
} else if (!out_clock) {
return {heif_error_Usage_error, heif_suberror_Invalid_parameter_value, "NULL heif_tai_clock_info passed in"};
return {heif_error_Input_does_not_exist, heif_suberror_Invalid_parameter_value, "NULL heif_tai_clock_info passed in"};
}
auto file = context->context->get_heif_file();

// Only create a new taic if one doesn't exist for the itemId.
auto taic = context->context->get_heif_file()->get_property<Box_taic>(itemId);
// Check if itemId exists
auto file = ctx->context->get_heif_file();
if (!file->image_exists(itemId)) {
return {heif_error_Input_does_not_exist, heif_suberror_Invalid_parameter_value, "itemId does not exist"};
}

// Check if taic exists for itemId
auto taic = file->get_property<Box_taic>(itemId);
if (!taic) {
taic = std::make_shared<Box_taic>();
bool essential = false;
context->context->add_property(itemId, taic, essential); // Should we output taic property id?
out_clock = nullptr;
return {heif_error_Usage_error, heif_suberror_Invalid_property, "TAI Clock property not found for itemId"};

}


Expand All @@ -360,32 +371,38 @@ struct heif_error heif_property_get_clock_info(const struct heif_context* contex

}

struct heif_error heif_property_set_tai_timestamp(const struct heif_context* context,
struct heif_error heif_property_set_tai_timestamp(const struct heif_context* ctx,
heif_item_id itemId,
uint64_t tai_timestamp,
uint8_t status_bits,
heif_property_id* out_propertyId)
{
if (!context) {
if (!ctx) {
return {heif_error_Usage_error, heif_suberror_Null_pointer_argument, "NULL passed"};
}

// Check if itemId exists
auto file = ctx->context->get_heif_file();
if (!file->image_exists(itemId)) {
return {heif_error_Input_does_not_exist, heif_suberror_Invalid_parameter_value, "itemId does not exist"};
}

// Create new itai if one doesn't exist for the itemId.
auto itai = context->context->get_heif_file()->get_property<Box_itai>(itemId);
auto itai = file->get_property<Box_itai>(itemId);
if (!itai) {
itai = std::make_shared<Box_itai>();
}

itai->set_TAI_timestamp(tai_timestamp);
itai->set_status_bits(status_bits);

heif_property_id id = context->context->add_property(itemId, itai, false);
heif_property_id id = ctx->context->add_property(itemId, itai, false);

// Create new taic if one doesn't exist for the itemId.
auto taic = context->context->get_heif_file()->get_property<Box_taic>(itemId);
auto taic = file->get_property<Box_taic>(itemId);
if (!taic) {
taic = std::make_shared<Box_taic>();
context->context->add_property(itemId, taic, false);
ctx->context->add_property(itemId, taic, false);
// Should we output taic_id?
}

Expand All @@ -397,24 +414,27 @@ struct heif_error heif_property_set_tai_timestamp(const struct heif_context* con
return heif_error_success;
}

struct heif_error heif_property_get_tai_timestamp(const struct heif_context* context,
struct heif_error heif_property_get_tai_timestamp(const struct heif_context* ctx,
heif_item_id itemId,
uint64_t* out_tai_timestamp,
uint8_t* out_status_bits)
{
if (!context) {
if (!ctx) {
return {heif_error_Usage_error, heif_suberror_Invalid_parameter_value, "NULL passed"};
}

auto file = context->context->get_heif_file();
// Check if itemId exists
auto file = ctx->context->get_heif_file();
if (!file->image_exists(itemId)) {
return {heif_error_Input_does_not_exist, heif_suberror_Invalid_parameter_value, "itemId does not exist"};
}

// TODO - use a function to get the taic instead of duplicating code.
auto ipco = file->get_ipco_box();
auto impa = file->get_ipma_box();
auto prop = ipco->get_property_for_item_ID(itemId, impa, fourcc("itai"));
auto itai = std::dynamic_pointer_cast<Box_itai>(prop);
//Check if itai exists for itemId
auto itai = file->get_property<Box_itai>(itemId);
if (!itai) {
return {heif_error_Usage_error, heif_suberror_Invalid_property, "Timestamp property not found"};
out_tai_timestamp = nullptr;
out_status_bits = nullptr;
return {heif_error_Usage_error, heif_suberror_Invalid_property, "Timestamp property not found for itemId"};
}

if (out_tai_timestamp) {
Expand Down
13 changes: 4 additions & 9 deletions libheif/heif_properties.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,25 +147,20 @@ struct heif_tai_clock_info
uint8_t clock_source;
};

//Creates a clock info property if it doesn't already exist.
// Creates a new clock info property if it doesn't already exist.
LIBHEIF_API
struct heif_error heif_property_set_clock_info(const struct heif_context* ctx,
heif_item_id itemId,
heif_tai_clock_info clock,
heif_property_id out_propertyId);
heif_property_id* out_propertyId);

LIBHEIF_API
struct heif_error heif_property_get_clock_info(const struct heif_context* ctx,
heif_item_id itemId,
heif_tai_clock_info* out_clock);

/**
* Creates a TAI timestamp property. If one already exists, then update it
* Creates a clock info property if it doesn't already exist.
*
* @param tai_timestamp if unknown: 0xFFFFFFFFFFFFFFFF
* @param status_bits if unknown: 0
*/
// Creates a new TAI timestamp property if one doesn't already exist for itemId.
// Creates a new clock info property if one doesn't already exist for itemId.
struct heif_error heif_property_set_tai_timestamp(const struct heif_context* ctx,
heif_item_id itemId,
uint64_t tai_timestamp,
Expand Down

0 comments on commit 8270e93

Please sign in to comment.