Skip to content

Commit

Permalink
Use a struct for clock input parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
dukesook committed Nov 7, 2023
1 parent 985b6ca commit 70a85c4
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 60 deletions.
5 changes: 2 additions & 3 deletions libheif/box.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<float>::quiet_NaN();
uint8_t m_clock_source = 0;
};
Expand Down
72 changes: 31 additions & 41 deletions libheif/heif_properties.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<Box_taic>();
// Create new taic if one doesn't exist for the itemId.
auto taic = context->context->get_heif_file()->get_property<Box_taic>(itemId);
if (!taic) {
taic = std::make_shared<Box_taic>();
}

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);
Expand All @@ -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<Box_taic>(prop);
// 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);
if (!taic) {
return {heif_error_Usage_error, heif_suberror_Invalid_property, "Clock info property not found"};
taic = std::make_shared<Box_taic>();
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;

Expand All @@ -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<Box_itai>();
// Create new itai if one doesn't exist for the itemId.
auto itai = context->context->get_heif_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);

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<Box_taic>(itemId);
if (!taic) {
taic = std::make_shared<Box_taic>();
context->context->add_property(itemId, taic, essential);
context->context->add_property(itemId, taic, false);
// Should we output taic_id?
}

Expand Down
31 changes: 15 additions & 16 deletions libheif/heif_properties.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 70a85c4

Please sign in to comment.