// TimeBolt: runs the ffmpeg tool in-process. Compiled with the fftools include paths (see build.sh). // // The tool keeps its options in globals that option parsing only sets when an option is given, so a // "-copyts" from one run would still be on in the next. reset_option_globals() puts every one of them // back to the value it has in ffmpeg_opt.c / cmdutils.c before each run; ffmpeg.c's own statics are reset // by tb_ffmpeg_internal_reset() (patches/0001-ffmpeg-as-library.patch). #include #include #include #include #include "config.h" #include "fftools/ffmpeg.h" #include "fftools/cmdutils.h" #include "fftools/opt_common.h" #include "libavutil/ffversion.h" #include "libavutil/log.h" int ffmpeg_main(int argc, char **argv); void tb_ffmpeg_internal_cancel(void); void tb_ffmpeg_internal_reset(void); extern int hide_banner; static pthread_mutex_t run_lock = PTHREAD_MUTEX_INITIALIZER; static atomic_int running = 0; static void reset_option_globals(void) { dts_delta_threshold = 10; dts_error_threshold = 3600*30; video_sync_method = VSYNC_AUTO; frame_drop_threshold = 0; do_benchmark = 0; do_benchmark_all = 0; do_hex_dump = 0; do_pkt_dump = 0; copy_ts = 0; start_at_zero = 0; copy_tb = -1; debug_ts = 0; exit_on_error = 0; abort_on_flags = 0; print_stats = -1; stdin_interaction = 1; max_error_rate = 2.0/3; filter_complex_nbthreads = 0; filter_buffered_frames = 0; vstats_version = 2; print_graphs = 0; print_graphs_file = NULL; /* freed by ffmpeg_cleanup */ print_graphs_format = NULL; /* freed by ffmpeg_cleanup */ auto_conversion_filters = 1; stats_period = 500000; ignore_unknown_streams = 0; copy_unknown_streams = 0; recast_media = 0; filter_nbthreads = NULL; /* freed by ffmpeg_cleanup */ vstats_filename = NULL; /* freed by ffmpeg_cleanup */ vstats_file = NULL; /* closed by ffmpeg_cleanup, pointer left dangling there */ progress_avio = NULL; /* closed by the last print_report */ filter_hw_device = NULL; /* freed by hw_device_free_all */ atomic_store(&nb_output_dumped, 0); hide_banner = 0; input_files = NULL; nb_input_files = 0; output_files = NULL; nb_output_files = 0; filtergraphs = NULL; nb_filtergraphs = 0; decoders = NULL; nb_decoders = 0; } int tb_ffmpeg_run(int argc, const char *const *argv) { pthread_mutex_lock(&run_lock); /* Reset first, then marked running: a cancel that came between the two was zeroed by the reset and the command ran to its end (an audit, 15 November 2026). Before `running` is set a cancel is a no-op here, and the Swift side says it again once this command is running (FFmpegRunHandle). */ tb_ffmpeg_internal_reset(); reset_option_globals(); atomic_store(&running, 1); /* ffmpeg_main expects argv[0] to be the program name and may keep pointers into argv for the duration of the run, so hand it its own copy. */ char **args = calloc((size_t)argc + 2, sizeof(char *)); int ret = -1; if (args) { args[0] = strdup("ffmpeg"); for (int i = 0; i < argc; i++) args[i + 1] = strdup(argv[i]); ret = ffmpeg_main(argc + 1, args); for (int i = 0; i <= argc; i++) free(args[i]); free(args); } atomic_store(&running, 0); pthread_mutex_unlock(&run_lock); return ret; } void tb_ffmpeg_cancel(void) { if (atomic_load(&running)) tb_ffmpeg_internal_cancel(); } int tb_ffmpeg_is_running(void) { return atomic_load(&running); } const char *tb_ffmpeg_version(void) { return FFMPEG_VERSION; } /* Logging: av_log lines flattened for the host, which cannot take a va_list from Swift comfortably. */ static void (*log_handler)(int level, const char *line) = NULL; static void log_callback(void *ptr, int level, const char *fmt, va_list vl) { if (!log_handler) return; if (level > av_log_get_level()) return; char line[2048]; int print_prefix = 1; av_log_format_line2(ptr, level, fmt, vl, line, sizeof(line), &print_prefix); log_handler(level, line); } void tb_ffmpeg_set_log_handler(void (*handler)(int level, const char *line)) { log_handler = handler; av_log_set_callback(handler ? log_callback : av_log_default_callback); }