Improved frame counting capabilities for time lib. Unit tests.

This commit is contained in:
Stefan Kangas 2010-12-13 18:16:15 +01:00 committed by Ichthyostega
parent d2702e8254
commit 94f8379aa2
4 changed files with 113 additions and 45 deletions

View file

@ -22,6 +22,7 @@
#include <nobug.h>
#include "lib/time.h"
#include "lib/tmpbuf.h"
#include <math.h>
/* GAVL_TIME_SCALE is the correct factor or dividend when using gavl_time_t for
* units of whole seconds from gavl_time_t. Since we want to use milliseconds,
@ -58,8 +59,12 @@ lumiera_tmpbuf_print_time (gavl_time_t time)
}
gavl_time_t
lumiera_build_time(long millis, uint secs, uint mins, uint hours)
lumiera_build_time (long millis, uint secs, uint mins, uint hours)
{
REQUIRE (millis >= 0 && millis <= 999);
REQUIRE (mins < 60);
REQUIRE (secs < 60);
gavl_time_t time = millis
+ 1000 * secs
+ 1000 * 60 * mins
@ -68,39 +73,55 @@ lumiera_build_time(long millis, uint secs, uint mins, uint hours)
return time;
}
gavl_time_t
lumiera_build_time_fps (float fps, uint frames, uint secs, uint mins, uint hours)
{
REQUIRE (mins < 60);
REQUIRE (secs < 60);
REQUIRE (frames < fps);
gavl_time_t time = frames * (1000.0 / fps)
+ 1000 * secs
+ 1000 * 60 * mins
+ 1000 * 60 * 60 * hours;
time *= GAVL_TIME_SCALE_MS;
return time;
}
int
lumiera_time_hours(gavl_time_t time)
lumiera_time_hours (gavl_time_t time)
{
return time / GAVL_TIME_SCALE_MS / 1000 / 60 / 60;
}
int
lumiera_time_minutes(gavl_time_t time)
lumiera_time_minutes (gavl_time_t time)
{
return (time / GAVL_TIME_SCALE_MS / 1000 / 60) % 60;
}
int
lumiera_time_seconds(gavl_time_t time)
lumiera_time_seconds (gavl_time_t time)
{
return (time / GAVL_TIME_SCALE_MS / 1000) % 60;
}
int
lumiera_time_millis(gavl_time_t time)
lumiera_time_millis (gavl_time_t time)
{
return (time / GAVL_TIME_SCALE_MS) % 1000;
}
int
lumiera_time_frames(gavl_time_t time, float fps)
lumiera_time_frames (gavl_time_t time, float fps)
{
return (fps * (lumiera_time_millis(time))) / 1000;
}
int
lumiera_time_frame_count(gavl_time_t time, float fps)
lumiera_time_frame_count (gavl_time_t time, float fps)
{
int ms = (time / GAVL_TIME_SCALE_MS);
return fps * ms / 1000;
REQUIRE (fps > 0);
return roundf((time / GAVL_TIME_SCALE_MS / 1000.0f) * fps);
}

View file

@ -36,43 +36,67 @@ lumiera_tmpbuf_print_time (gavl_time_t time);
/**
* Builds a time value by summing up the given components.
* @param millis number of milliseconds
* @param secs number of seconds
* @param mins number of minutes
* @param hours number of hours
*/
gavl_time_t
lumiera_build_time (long millis, uint secs, uint mins, uint hours);
/**
* Get the hour part of given time.
* Builds a time value by summing up the given components.
* @param fps the frame rate to be used
* @param frames number of frames
* @param secs number of seconds
* @param mins number of minutes
* @param hours number of hours
*/
int lumiera_time_hours(gavl_time_t time);
gavl_time_t
lumiera_build_time_fps (float fps, uint frames, uint secs, uint mins, uint hours);
/**
* Get the hour part of given time.
* @param time the time value to use
*/
int
lumiera_time_hours (gavl_time_t time);
/**
* Get the minute part of given time.
* @param time the time value to use
*/
int lumiera_time_minutes(gavl_time_t time);
int
lumiera_time_minutes (gavl_time_t time);
/**
* Get the seconds part of given time.
* @param time the time value to use
*/
int lumiera_time_seconds(gavl_time_t time);
int
lumiera_time_seconds (gavl_time_t time);
/**
* Get the milliseconds part of given time.
* @param time the time value to use
*/
int lumiera_time_millis(gavl_time_t time);
int
lumiera_time_millis (gavl_time_t time);
/**
* Get the frame part of given time, using the given number of frames.
* @param gavl_time_t the time we are interested in converting
* @param fps Frame rate (float for now, but should be a rational)
* Get the frame part of given time, using the given fps.
* @param time the time value to use
* @param fps frame rate (float for now, but should be a rational)
*/
int lumiera_time_frames(gavl_time_t time, float fps);
int
lumiera_time_frames (gavl_time_t time, float fps);
/**
* Get the frame count for the given time.
* @param gavl_time_t the time we are interested in converting
* @param fps Frame rate (float for now, but should be a rational)
* Get the frame count for the given time, using the given fps.
* @param time the time value to use
* @param fps frame rate (float for now, but should be a rational)
*/
int lumiera_time_frame_count(gavl_time_t time, float fps);
int
lumiera_time_frame_count (gavl_time_t time, float fps);
#endif

View file

@ -4,3 +4,6 @@ TEST "basic functionality" basic <<END
err: ECHO: .*: 00:00:00.000
err: ECHO: .*: 03:55:20.700
END
TEST "frame rate dependent calculations" fps <<END
END

View file

@ -28,6 +28,7 @@ typedef unsigned int uint;
TESTS_BEGIN
const int FRAMES = 15;
const int MILLIS = 700;
const int SECONDS = 20;
const int MINUTES = 55;
@ -38,35 +39,54 @@ const float FPS = 24.0;
* 1. Basic functionality
*/
TEST (basic) {
TEST (basic)
{
// Zero
gavl_time_t t = lumiera_build_time(0,0,0,0);
gavl_time_t t = lumiera_build_time (0,0,0,0);
CHECK ((gavl_time_t) t == 0);
CHECK (lumiera_time_millis(t) == 0);
CHECK (lumiera_time_seconds(t) == 0);
CHECK (lumiera_time_minutes(t) == 0);
CHECK (lumiera_time_hours(t) == 0);
CHECK (lumiera_time_frames(t, FPS) == 0);
CHECK (lumiera_time_frames(t, FPS+5) == 0);
CHECK (lumiera_time_frame_count(t, FPS) == 0);
CHECK (lumiera_time_frame_count(t, FPS+5) == 0);
CHECK ((gavl_time_t) t == 0);
CHECK (lumiera_time_millis (t) == 0);
CHECK (lumiera_time_seconds (t) == 0);
CHECK (lumiera_time_minutes (t) == 0);
CHECK (lumiera_time_hours (t) == 0);
CHECK (lumiera_time_frames (t, FPS) == 0);
CHECK (lumiera_time_frames (t, FPS+5) == 0);
CHECK (lumiera_time_frame_count (t, FPS) == 0);
CHECK (lumiera_time_frame_count (t, FPS+5) == 0);
ECHO ("%s", lumiera_tmpbuf_print_time(t));
ECHO ("%s", lumiera_tmpbuf_print_time (t));
// Non-zero
t = lumiera_build_time(MILLIS, SECONDS, MINUTES, HOURS);
t = lumiera_build_time (MILLIS, SECONDS, MINUTES, HOURS);
CHECK (lumiera_time_millis(t) == MILLIS);
CHECK (lumiera_time_seconds(t) == SECONDS);
CHECK (lumiera_time_minutes(t) == MINUTES);
CHECK (lumiera_time_hours(t) == HOURS);
CHECK (lumiera_time_frames(t, FPS) == (int)((FPS * MILLIS) / 1000));
CHECK (lumiera_time_frames(t, FPS+5) == (int)(((FPS+5) * MILLIS) / 1000));
CHECK (lumiera_time_frame_count(t, FPS) == 338896);
CHECK (lumiera_time_frame_count(t, FPS+5) == 409500);
CHECK (lumiera_time_millis (t) == MILLIS);
CHECK (lumiera_time_seconds (t) == SECONDS);
CHECK (lumiera_time_minutes (t) == MINUTES);
CHECK (lumiera_time_hours (t) == HOURS);
CHECK (lumiera_time_frames (t, FPS) == (int)((FPS * MILLIS) / 1000));
CHECK (lumiera_time_frames (t, FPS+5) == (int)(((FPS+5) * MILLIS) / 1000));
CHECK (lumiera_time_frame_count (t, FPS) == 338897);
CHECK (lumiera_time_frame_count (t, FPS+5) == 409500);
ECHO ("%s", lumiera_tmpbuf_print_time(t));
ECHO ("%s", lumiera_tmpbuf_print_time (t));
}
/*
* 2. Frame rate dependent calculations.
*/
TEST (fps)
{
gavl_time_t t = lumiera_build_time_fps (FPS, FRAMES, SECONDS, MINUTES, HOURS);
CHECK (lumiera_time_millis (t) == (int)(FRAMES * (1000.0 / FPS)));
CHECK (lumiera_time_seconds (t) == SECONDS);
CHECK (lumiera_time_minutes (t) == MINUTES);
CHECK (lumiera_time_hours (t) == HOURS);
CHECK (lumiera_time_frames (t, FPS) == FRAMES);
CHECK (lumiera_time_frames (t, FPS+5) == (int)(((FPS+5) * 625) / 1000));
CHECK (lumiera_time_frame_count (t, FPS) == 338895);
CHECK (lumiera_time_frame_count (t, FPS+5) == 409498);
}
TESTS_END