time handling ok so far
This commit is contained in:
parent
bf32b80521
commit
2976b2ab29
3 changed files with 107 additions and 32 deletions
|
|
@ -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
|
||||
|
|
|
|||
26
src/lib/time.c
Normal file
26
src/lib/time.c
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
/*
|
||||
time.h - Time and frame calculations
|
||||
|
||||
Copyright (C) CinelerraCV
|
||||
2007, Christian Thaeter <ct@pipapo.org>
|
||||
|
||||
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");
|
||||
100
src/lib/time.h
100
src/lib/time.h
|
|
@ -24,8 +24,9 @@
|
|||
|
||||
#include <sys/time.h>
|
||||
#include <time.h>
|
||||
#include <math.h>
|
||||
|
||||
#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);
|
||||
|
|
|
|||
Loading…
Reference in a new issue