From 2976b2ab29d2010df37502e43d72c42b5d2eb0c8 Mon Sep 17 00:00:00 2001 From: Christian Thaeter Date: Fri, 24 Aug 2007 06:02:29 +0200 Subject: [PATCH] time handling ok so far --- src/lib/Makefile.am | 13 +++++- src/lib/time.c | 26 ++++++++++++ src/lib/time.h | 100 +++++++++++++++++++++++++++++++------------- 3 files changed, 107 insertions(+), 32 deletions(-) create mode 100644 src/lib/time.c diff --git a/src/lib/Makefile.am b/src/lib/Makefile.am index 7419bedf9..ed81d9a5b 100644 --- a/src/lib/Makefile.am +++ b/src/lib/Makefile.am @@ -19,6 +19,15 @@ libcin3_a_srcdir = $(top_srcdir)/src/lib noinst_LIBRARIES += libcin3.a libcin3_a_CFLAGS = $(CFLAGS) -std=gnu99 -Wall -Werror +libcin3_a_CPPFLAGS = -I$(top_srcdir)/src/ + +libcin3_a_SOURCES = \ + $(libcin3_a_srcdir)/plugin.c \ + $(libcin3_a_srcdir)/error.c \ + $(libcin3_a_srcdir)/time.c + +noinst_HEADERS += \ + $(libcin3_a_srcdir)/plugin.h \ + $(libcin3_a_srcdir)/error.h \ + $(libcin3_a_srcdir)/time.h -libcin3_a_SOURCES = $(libcin3_a_srcdir)/plugin.c $(libcin3_a_srcdir)/error.c -noinst_HEADERS += $(libcin3_a_srcdir)/plugin.h $(libcin3_a_srcdir)/error.h diff --git a/src/lib/time.c b/src/lib/time.c new file mode 100644 index 000000000..6cbdde6ee --- /dev/null +++ b/src/lib/time.c @@ -0,0 +1,26 @@ +/* + time.h - Time and frame calculations + + Copyright (C) CinelerraCV + 2007, Christian Thaeter + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. +*/ + +#include "lib/error.h" + +CINELERRA_ERROR_DEFINE(TIME_OVERFLOW, "Time overflow"); +CINELERRA_ERROR_DEFINE(TIME_UNDERFLOW, "Time underflow"); +CINELERRA_ERROR_DEFINE(TIME_NEGATIVE, "Time negative"); diff --git a/src/lib/time.h b/src/lib/time.h index ef0979bd0..78f0854e1 100644 --- a/src/lib/time.h +++ b/src/lib/time.h @@ -24,8 +24,9 @@ #include #include +#include -#include "error.h" +#include "lib/error.h" /* this time functions are small macro like wrapers, they are all inlined for performance reasons @@ -64,12 +65,26 @@ typedef cinelerra_framerate* CinelerraFramerate; typedef unsigned long cinelerra_frame; +/** + * normalize time after operations. + * used internally + */ +static inline void +cinelerra_time_normalize (CinelerraTime time) +{ + REQUIRE (time); + if (time->tv_usec >= 1000000) + { + time->tv_sec += (time->tv_usec / 1000000); + time->tv_usec = (time->tv_usec % 1000000); + } +} /** * set a time value to zero. */ -inline CinelerraTime +static inline CinelerraTime cinelerra_time_clear (CinelerraTime time) { if(time) @@ -83,7 +98,7 @@ cinelerra_time_clear (CinelerraTime time) /** * get current time. */ -inline CinelerraTime +static inline CinelerraTime cinelerra_time_current (CinelerraTime time) { if (time) @@ -98,7 +113,7 @@ cinelerra_time_current (CinelerraTime time) /** * init from floating point representation. */ -inline CinelerraTime +static inline CinelerraTime cinelerra_time_set_double (CinelerraTime time, double fp) { if (time) @@ -106,21 +121,62 @@ cinelerra_time_set_double (CinelerraTime time, double fp) if (fp >= 0.0) { time->tv_sec = fp; - time->tv_usec = (fp - time->tv_sec) * 1000000.0; + time->tv_usec = round((fp - time->tv_sec) * 1000000.0); return time; } else { + time->tv_sec = (time_t)-1; + time->tv_usec = (suseconds_t)-1; cinelerra_error_set(CINELERRA_ERROR_TIME_NEGATIVE); } } return NULL; } +/** + * initialize with seconds and microseconds. + */ +static inline CinelerraTime +cinelerra_time_init (CinelerraTime time, time_t sec, suseconds_t usec) +{ + if (time) + { + time->tv_sec = sec; + time->tv_usec = usec; + cinelerra_time_normalize (time); + } + return time; +} + +/** + * get the seconds part from a time. + */ +static inline time_t +cinelerra_time_sec (CinelerraTime time) +{ + if (time) + return time->tv_sec; + else + return (time_t)-1; +} + +/** + * get the microseconds part of a time. + */ +static inline suseconds_t +cinelerra_time_usec (CinelerraTime time) +{ + if (time) + return time->tv_usec; + else + return (suseconds_t)-1; +} + /** * convert to floating point repesentation. */ -inline double +static inline double cinelerra_time_double_get (CinelerraTime time) { if (time) @@ -135,26 +191,10 @@ cinelerra_time_double_get (CinelerraTime time) } - -/** - * normalize time after operations. - * used internally - */ -inline void -cinelerra_time_normalize (CinelerraTime time) -{ - REQUIRE (time); - if (time->tv_usec >= 1000000) - { - time->tv_sec += (time->tv_usec / 1000000); - time->tv_usec = (time->tv_usec % 1000000); - } -} - /** * copy time */ -inline CinelerraTime +static inline CinelerraTime cinelerra_time_copy (CinelerraTime dest, const CinelerraTime src) { if (dest && src) @@ -162,13 +202,13 @@ cinelerra_time_copy (CinelerraTime dest, const CinelerraTime src) dest->tv_sec = src->tv_sec; dest->tv_usec = src->tv_usec; } - return test; + return dest; } /** * add time. */ -inline CinelerraTime +static inline CinelerraTime cinelerra_time_add (CinelerraTime dest, const CinelerraTime src) { if (dest && src) @@ -176,7 +216,7 @@ cinelerra_time_add (CinelerraTime dest, const CinelerraTime src) time_t t = dest->tv_sec; dest->tv_sec += src->tv_sec; - time->tv_usec += src->tv_usec; + dest->tv_usec += src->tv_usec; cinelerra_time_normalize (dest); @@ -192,7 +232,7 @@ cinelerra_time_add (CinelerraTime dest, const CinelerraTime src) /** * substact time. */ -inline CinelerraTime +static inline CinelerraTime cinelerra_time_sub (CinelerraTime dest, const CinelerraTime src) { if (dest && src) @@ -200,12 +240,12 @@ cinelerra_time_sub (CinelerraTime dest, const CinelerraTime src) time_t t = dest->tv_sec; dest->tv_sec -= src->tv_sec; - if (time->tv_usec >= src->tv_usec) - time->tv_usec -= src->tv_usec; + if (dest->tv_usec >= src->tv_usec) + dest->tv_usec -= src->tv_usec; else { --dest->tv_sec; - time->tv_usec += src->tv_usec; + dest->tv_usec += 1000000 - src->tv_usec; } cinelerra_time_normalize (dest);