From 70a85c462768e7d3f608c86323844f8e31f46c1d Mon Sep 17 00:00:00 2001 From: dukesook Date: Mon, 6 Nov 2023 20:12:50 -0700 Subject: [PATCH] Use a struct for clock input parameters --- libheif/box.h | 5 ++- libheif/heif_properties.cc | 72 ++++++++++++++++---------------------- libheif/heif_properties.h | 31 ++++++++-------- 3 files changed, 48 insertions(+), 60 deletions(-) diff --git a/libheif/box.h b/libheif/box.h index 6fdee58794..06105bdf81 100644 --- a/libheif/box.h +++ b/libheif/box.h @@ -1117,9 +1117,8 @@ class Box_taic : public FullBox Error parse(BitstreamRange& range) override; private: - // Initialized to "unknown" - uint64_t m_time_uncertainty = 0xFFFFFFFFFFFFFFFF; - int64_t m_correction_offset = 0x7FFFFFFFFFFFFFFF; + uint64_t m_time_uncertainty = HEIF_TAI_CLOCK_UNKNOWN_TIME_UNCERTAINTY; + int64_t m_correction_offset = HEIF_TAI_CLOCK_UNKNOWN_CORRECTION_OFFSET; float m_clock_drift_rate = std::numeric_limits::quiet_NaN(); uint8_t m_clock_source = 0; }; diff --git a/libheif/heif_properties.cc b/libheif/heif_properties.cc index d487c5f776..936ed2854c 100644 --- a/libheif/heif_properties.cc +++ b/libheif/heif_properties.cc @@ -303,23 +303,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, heif_item_id itemId, - uint64_t time_uncertainty, - int64_t correction_offset, - float clock_drift_rate, - uint8_t clock_source, + heif_tai_clock_info clock, heif_property_id* out_propertyId) { if (!context) { return {heif_error_Usage_error, heif_suberror_Null_pointer_argument, "NULL passed"}; } - // TODO - if the property already exists, we should update it instead of creating a new one. - auto taic = std::make_shared(); + // Create new taic if one doesn't exist for the itemId. + auto taic = context->context->get_heif_file()->get_property(itemId); + if (!taic) { + taic = std::make_shared(); + } - taic->set_time_uncertainty(time_uncertainty); - taic->set_correction_offset(correction_offset); - taic->set_clock_drift_rate(clock_drift_rate); - taic->set_clock_source(clock_source); + taic->set_time_uncertainty(clock.time_uncertainty); + taic->set_correction_offset(clock.correction_offset); + taic->set_clock_drift_rate(clock.clock_drift_rate); + taic->set_clock_source(clock.clock_source); bool essential = false; heif_property_id id = context->context->add_property(itemId, taic, essential); @@ -333,40 +333,28 @@ struct heif_error heif_property_set_clock_info(const struct heif_context* contex struct heif_error heif_property_get_clock_info(const struct heif_context* context, heif_item_id itemId, - uint64_t* out_time_uncertainty, - int64_t* out_correction_offset, - float* out_clock_drift_rate, - uint8_t* out_clock_source) + heif_tai_clock_info* out_clock) { if (!context) { - return {heif_error_Usage_error, heif_suberror_Invalid_parameter_value, "NULL passed"}; + 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"}; } - auto file = context->context->get_heif_file(); - // 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("taic")); - auto taic = std::dynamic_pointer_cast(prop); + // Only create a new taic if one doesn't exist for the itemId. + auto taic = context->context->get_heif_file()->get_property(itemId); if (!taic) { - return {heif_error_Usage_error, heif_suberror_Invalid_property, "Clock info property not found"}; + taic = std::make_shared(); + bool essential = false; + context->context->add_property(itemId, taic, essential); // Should we output taic property id? } - // TODO - if the value is unknown, return a nullptr. - if (out_time_uncertainty) { - *out_time_uncertainty = taic->get_time_uncertainty(); - } - if (out_correction_offset) { - *out_correction_offset = taic->get_correction_offset(); - } - if (out_clock_drift_rate) { - *out_clock_drift_rate = taic->get_clock_drift_rate(); - } - if (out_clock_source) { - *out_clock_source = taic->get_clock_source(); - } + out_clock->time_uncertainty = taic->get_time_uncertainty(); + out_clock->correction_offset = taic->get_correction_offset(); + out_clock->clock_drift_rate = taic->get_clock_drift_rate(); + out_clock->clock_source = taic->get_clock_source(); return heif_error_success; @@ -382,20 +370,22 @@ struct heif_error heif_property_set_tai_timestamp(const struct heif_context* con return {heif_error_Usage_error, heif_suberror_Null_pointer_argument, "NULL passed"}; } - // TODO - if the property already exists, we should update it instead of creating a new one. - auto itai = std::make_shared(); + // Create new itai if one doesn't exist for the itemId. + auto itai = context->context->get_heif_file()->get_property(itemId); + if (!itai) { + itai = std::make_shared(); + } itai->set_TAI_timestamp(tai_timestamp); itai->set_status_bits(status_bits); - bool essential = false; - heif_property_id id = context->context->add_property(itemId, itai, essential); + heif_property_id id = context->context->add_property(itemId, itai, false); - // Add taic property if it doesn't exist. + // Create new taic if one doesn't exist for the itemId. auto taic = context->context->get_heif_file()->get_property(itemId); if (!taic) { taic = std::make_shared(); - context->context->add_property(itemId, taic, essential); + context->context->add_property(itemId, taic, false); // Should we output taic_id? } diff --git a/libheif/heif_properties.h b/libheif/heif_properties.h index b95e666856..bc7cb37ab4 100644 --- a/libheif/heif_properties.h +++ b/libheif/heif_properties.h @@ -135,30 +135,29 @@ void heif_item_get_property_transform_crop_borders(const struct heif_context* co // ========================= Timestamps ========================= -/** - * Creates a clock info property if it doesn't already exist. - * - * @param time_uncertainty if unknown: 0xFFFFFFFFFFFFFFFF - * @param correction_offset if unknown: 0x7FFFFFFFFFFFFFFF - * @param clock_drift_rate if unknown: NaN - * @param clock_source if unknown: 0 - */ +const uint64_t HEIF_TAI_CLOCK_UNKNOWN_TIME_UNCERTAINTY = 0xFFFFFFFFFFFFFFFF; +const int64_t HEIF_TAI_CLOCK_UNKNOWN_CORRECTION_OFFSET = 0x7FFFFFFFFFFFFFFF; +struct heif_tai_clock_info +{ + uint8_t version; + + uint64_t time_uncertainty; + int64_t correction_offset; + float clock_drift_rate; + uint8_t clock_source; +}; + +//Creates a 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, - uint64_t time_uncertainty, - int64_t correction_offset, - float clock_drift_rate, - uint8_t clock_source, + heif_tai_clock_info clock, heif_property_id out_propertyId); LIBHEIF_API struct heif_error heif_property_get_clock_info(const struct heif_context* ctx, heif_item_id itemId, - uint64_t* out_time_uncertainty, - int64_t* out_correction_offset, - float* out_clock_drift_rate, - uint8_t* out_clock_source); + heif_tai_clock_info* out_clock); /** * Creates a TAI timestamp property. If one already exists, then update it