Skip to content

Commit cbd8e27

Browse files
[FEAT] Add timing module in lib_ccxr (#1640)
* feat: Add new module for timings functionality * feat: Add timing functionality in `timing.rs` module * feat: List all module & function conversion * chore: Clippy fixes * feat: Equivalent `ccx_common_timing.h` functions in rust module * feat: Add static constants & include struct in `build.rs` * feat: Add extern C functions * feat: Include & use rust extern functions in C * fix: Windows build * fix: Windows build --------- Co-authored-by: Prateek Sunal <prtksunal@gmail.com>
1 parent 349020e commit cbd8e27

File tree

12 files changed

+1150
-12
lines changed

12 files changed

+1150
-12
lines changed

src/lib_ccx/ccx_common_timing.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,18 @@ int gop_rollover = 0;
3030

3131
struct ccx_common_timing_settings_t ccx_common_timing_settings;
3232

33+
#ifndef DISABLE_RUST
34+
void ccxr_add_current_pts(struct ccx_common_timing_ctx *ctx, LLONG pts);
35+
void ccxr_set_current_pts(struct ccx_common_timing_ctx *ctx, LLONG pts);
36+
int ccxr_set_fts(struct ccx_common_timing_ctx *ctx);
37+
LLONG ccxr_get_fts(struct ccx_common_timing_ctx *ctx, int current_field);
38+
LLONG ccxr_get_fts_max(struct ccx_common_timing_ctx *ctx);
39+
char *ccxr_print_mstime_static(LLONG mstime, char *buf);
40+
void ccxr_print_debug_timing(struct ccx_common_timing_ctx *ctx);
41+
void ccxr_calculate_ms_gop_time(struct gop_time_code *g);
42+
int ccxr_gop_accepted(struct gop_time_code *g);
43+
#endif
44+
3345
void ccx_common_timing_init(LLONG *file_position, int no_sync)
3446
{
3547
ccx_common_timing_settings.disable_sync_check = 0;
@@ -73,11 +85,18 @@ struct ccx_common_timing_ctx *init_timing_ctx(struct ccx_common_timing_settings_
7385

7486
void add_current_pts(struct ccx_common_timing_ctx *ctx, LLONG pts)
7587
{
88+
#ifndef DISABLE_RUST
89+
return ccxr_add_current_pts(ctx, pts);
90+
#endif
91+
7692
set_current_pts(ctx, ctx->current_pts + pts);
7793
}
7894

7995
void set_current_pts(struct ccx_common_timing_ctx *ctx, LLONG pts)
8096
{
97+
#ifndef DISABLE_RUST
98+
return ccxr_set_current_pts(ctx, pts);
99+
#endif
81100
LLONG prev_pts = ctx->current_pts;
82101
ctx->current_pts = pts;
83102
if (ctx->pts_set == 0)
@@ -95,6 +114,9 @@ void set_current_pts(struct ccx_common_timing_ctx *ctx, LLONG pts)
95114

96115
int set_fts(struct ccx_common_timing_ctx *ctx)
97116
{
117+
#ifndef DISABLE_RUST
118+
return ccxr_set_fts(ctx);
119+
#endif
98120
int pts_jump = 0;
99121

100122
// ES don't have PTS unless GOP timing is used
@@ -266,6 +288,10 @@ int set_fts(struct ccx_common_timing_ctx *ctx)
266288

267289
LLONG get_fts(struct ccx_common_timing_ctx *ctx, int current_field)
268290
{
291+
#ifndef DISABLE_RUST
292+
return ccxr_get_fts(ctx, current_field);
293+
#endif
294+
269295
LLONG fts;
270296

271297
switch (current_field)
@@ -290,6 +316,10 @@ LLONG get_fts(struct ccx_common_timing_ctx *ctx, int current_field)
290316

291317
LLONG get_fts_max(struct ccx_common_timing_ctx *ctx)
292318
{
319+
#ifndef DISABLE_RUST
320+
return ccxr_get_fts_max(ctx);
321+
#endif
322+
293323
// This returns the maximum FTS that belonged to a frame. Caption block
294324
// counters are not applicable.
295325
return ctx->fts_max + ctx->fts_global;
@@ -350,13 +380,22 @@ size_t print_mstime_buff(LLONG mstime, char *fmt, char *buf)
350380
char *print_mstime_static(LLONG mstime)
351381
{
352382
static char buf[15]; // 14 should be long enough
383+
384+
#ifndef DISABLE_RUST
385+
return ccxr_print_mstime_static(mstime, buf);
386+
#endif
387+
353388
print_mstime_buff(mstime, "%02u:%02u:%02u:%03u", buf);
354389
return buf;
355390
}
356391

357392
/* Helper function for to display debug timing info. */
358393
void print_debug_timing(struct ccx_common_timing_ctx *ctx)
359394
{
395+
#ifndef DISABLE_RUST
396+
return ccxr_print_debug_timing(ctx);
397+
#endif
398+
360399
// Avoid wrong "Calc. difference" and "Asynchronous by" numbers
361400
// for uninitialized min_pts
362401
LLONG tempmin_pts = (ctx->min_pts == 0x01FFFFFFFFLL ? ctx->sync_pts : ctx->min_pts);
@@ -383,6 +422,10 @@ void print_debug_timing(struct ccx_common_timing_ctx *ctx)
383422

384423
void calculate_ms_gop_time(struct gop_time_code *g)
385424
{
425+
#ifndef DISABLE_RUST
426+
return ccxr_calculate_ms_gop_time(g);
427+
#endif
428+
386429
int seconds = (g->time_code_hours * 3600) + (g->time_code_minutes * 60) + g->time_code_seconds;
387430
g->ms = (LLONG)(1000 * (seconds + g->time_code_pictures / current_fps));
388431
if (gop_rollover)
@@ -391,6 +434,10 @@ void calculate_ms_gop_time(struct gop_time_code *g)
391434

392435
int gop_accepted(struct gop_time_code *g)
393436
{
437+
#ifndef DISABLE_RUST
438+
return ccxr_gop_accepted(g);
439+
#endif
440+
394441
if (!((g->time_code_hours <= 23) && (g->time_code_minutes <= 59) && (g->time_code_seconds <= 59) && (g->time_code_pictures <= 59)))
395442
return 0;
396443

src/rust/build.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ fn main() {
3131
"ccx_output_format",
3232
"ccx_boundary_time",
3333
"gop_time_code",
34+
"ccx_common_timing_settings_t",
3435
"ccx_s_options",
3536
"ccx_s_teletext_config",
3637
"ccx_output_date_format",

src/rust/lib_ccxr/src/common/options.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ impl Default for EncodersTranscriptFormat {
3838
}
3939
}
4040

41-
#[derive(Debug, Default, Copy, Clone)]
41+
#[derive(Debug, Default, Copy, Clone, PartialEq)]
4242
pub enum FrameType {
4343
#[default]
4444
ResetOrUnknown = 0,
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//! Provides Rust equivalent for functions in C. Uses Rust-native types as input and output.
2+
3+
use super::*;
4+
5+
/// Rust equivalent for `add_current_pts` function in C. Uses Rust-native types as input and output.
6+
pub fn add_current_pts(ctx: &mut TimingContext, pts: MpegClockTick) {
7+
ctx.add_current_pts(pts)
8+
}
9+
10+
/// Rust equivalent for `set_current_pts` function in C. Uses Rust-native types as input and output.
11+
pub fn set_current_pts(ctx: &mut TimingContext, pts: MpegClockTick) {
12+
ctx.set_current_pts(pts)
13+
}
14+
15+
/// Rust equivalent for `set_fts` function in C. Uses Rust-native types as input and output.
16+
pub fn set_fts(ctx: &mut TimingContext) -> bool {
17+
ctx.set_fts()
18+
}
19+
20+
/// Rust equivalent for `get_fts` function in C. Uses Rust-native types as input and output.
21+
pub fn get_fts(ctx: &mut TimingContext, current_field: CaptionField) -> Timestamp {
22+
ctx.get_fts(current_field)
23+
}
24+
25+
/// Rust equivalent for `get_fts_max` function in C. Uses Rust-native types as input and output.
26+
pub fn get_fts_max(ctx: &mut TimingContext) -> Timestamp {
27+
ctx.get_fts_max()
28+
}
29+
30+
/// Rust equivalent for `print_mstime_static` function in C. Uses Rust-native types as input and output.
31+
pub fn print_mstime_static(mstime: Timestamp, sep: char) -> String {
32+
mstime.to_hms_millis_time(sep).unwrap()
33+
}
34+
35+
/// Rust equivalent for `print_debug_timing` function in C. Uses Rust-native types as input and output.
36+
pub fn print_debug_timing(ctx: &mut TimingContext) {
37+
ctx.print_debug_timing()
38+
}
39+
40+
/// Rust equivalent for `calculate_ms_gop_time` function in C. Uses Rust-native types as input and output.
41+
pub fn calculate_ms_gop_time(g: GopTimeCode) -> Timestamp {
42+
g.timestamp()
43+
}
44+
45+
/// Rust equivalent for `gop_accepted` function in C. Uses Rust-native types as input and output.
46+
pub fn gop_accepted(g: GopTimeCode) -> bool {
47+
let mut timing_info = GLOBAL_TIMING_INFO.write().unwrap();
48+
49+
let gop_time = if let Some(gt) = timing_info.gop_time {
50+
gt
51+
} else {
52+
return true;
53+
};
54+
55+
if g.did_rollover(&gop_time) {
56+
timing_info.gop_rollover = true;
57+
true
58+
} else {
59+
gop_time.timestamp() <= g.timestamp()
60+
}
61+
}

src/rust/lib_ccxr/src/time/mod.rs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1-
//! Provide types for storing time in different formats
1+
//! Provide types for storing time in different formats and manage timing information while decoding.
22
//!
33
//! Time can be represented in one of following formats:
44
//! - [`Timestamp`] as number of milliseconds
55
//! - [`MpegClockTick`] as number of clock ticks (as defined in the MPEG standard)
66
//! - [`FrameCount`] as number of frames
77
//! - [`GopTimeCode`] as a GOP time code (as defined in the MPEG standard)
88
//!
9+
//! [`GLOBAL_TIMING_INFO`] and [`TimingContext`] are used for managing time-related information
10+
//! during the deocoding process.
11+
//!
912
//! # Conversion Guide
1013
//!
1114
//! | From | To |
@@ -16,8 +19,27 @@
1619
//! | any pts | [`MpegClockTick`] |
1720
//! | any frame count | [`FrameCount`] |
1821
//! | `gop_time_code` | [`GopTimeCode`] |
22+
//! | `current_field` | [`CaptionField`] |
23+
//! | `ccx_common_timing_ctx.pts_set` | [`PtsSet`] |
24+
//! | `ccx_common_timing_settings_t` | [`TimingSettings`] |
25+
//! | `ccx_common_timing_ctx` | [`TimingContext`] |
26+
//! | `init_timing_ctx` | [`TimingContext::new`] |
27+
//! | `add_current_pts` | [`TimingContext::add_current_pts`] |
28+
//! | `set_current_pts` | [`TimingContext::set_current_pts`] |
29+
//! | `set_fts` | [`TimingContext::set_fts`] |
30+
//! | `get_fts` | [`TimingContext::get_fts`] |
31+
//! | `get_fts_max` | [`TimingContext::get_fts_max`] |
32+
//! | `print_mstime_buff` | [`Timestamp::write_hms_millis_time`] |
1933
//! | `print_mstime_static` | [`Timestamp::to_hms_millis_time`] |
34+
//! | `print_scc_time` | [`Timestamp::to_scc_time`] |
35+
//! | `print_debug_timing` | [`TimingContext::print_debug_timing`] |
2036
//! | `gop_accepted` | [`GopTimeCode::did_rollover`] + some additional logic |
2137
//! | `calculate_ms_gop_time` | [`GopTimeCode::new`], [`GopTimeCode::timestamp`] |
38+
//! | `cb_708`, `cb_field1`, `cb_field2`, `pts_big_change`, `current_fps`, `frames_since_ref_time`, `total_frames_count`, `gop_time`, `first_gop_time`, `fts_at_gop_start`, `gop_rollover`, `ccx_common_timing_settings` | [`GlobalTimingInfo`], [`GLOBAL_TIMING_INFO`] |
2239
40+
pub mod c_functions;
41+
pub mod timing;
2342
pub mod units;
43+
44+
pub use timing::*;
45+
pub use units::*;

0 commit comments

Comments
 (0)