Skip to content

Commit

Permalink
Add frame rate management (new -r option)
Browse files Browse the repository at this point in the history
delete new line
  • Loading branch information
Erwan Raffin committed Apr 30, 2015
1 parent 9db61c2 commit 8eb9bd5
Show file tree
Hide file tree
Showing 8 changed files with 366 additions and 6 deletions.
8 changes: 4 additions & 4 deletions CMakeLists.txt
Expand Up @@ -369,22 +369,22 @@ if(ENABLE_EXECUTABLE)

if(SDL_FOUND)
# Build executable
add_executable(hevc ${HEVC_SOURCES_FILES} main_hm/sdl.c)
add_executable(hevc ${HEVC_SOURCES_FILES} main_hm/sdl.c main_hm/SDL_framerate.c)
# Link executable
target_link_libraries(hevc ${LINK_LIBRARIES_LIST} ${SDL_LIBRARY})
# Set include directory specific for this file. Avoid conflicts when including SDL.h
# if both SDL and SDL2 are installed
set_source_files_properties(main_hm/sdl.c PROPERTIES COMPILE_FLAGS -I"${SDL_INCLUDE_DIR}")
set_source_files_properties(main_hm/sdl.c main_hm/SDL_framerate.c PROPERTIES COMPILE_FLAGS -I"${SDL_INCLUDE_DIR}")
endif()

if(SDL2_FOUND)
# Build executable
add_executable(hevc_sdl2 ${HEVC_SOURCES_FILES} main_hm/sdl2.c)
add_executable(hevc_sdl2 ${HEVC_SOURCES_FILES} main_hm/sdl2.c main_hm/SDL_framerate.c)
# Link executable
target_link_libraries(hevc_sdl2 ${LINK_LIBRARIES_LIST} ${SDL2_LIBRARY})
# Set include directory specific for this file. Avoid conflicts when including SDL.h
# if both SDL and SDL2 are installed
set_source_files_properties(main_hm/sdl2.c PROPERTIES COMPILE_FLAGS -I"${SDL2_INCLUDE_DIR}")
set_source_files_properties(main_hm/sdl2.c main_hm/SDL_framerate.c PROPERTIES COMPILE_FLAGS -I"${SDL2_INCLUDE_DIR}")
endif()

endif()
Expand Down
197 changes: 197 additions & 0 deletions main_hm/SDL_framerate.c
@@ -0,0 +1,197 @@
/*
SDL_framerate.c: framerate manager
Copyright (C) 2001-2012 Andreas Schiffler
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
Andreas Schiffler -- aschiffler at ferzkopp dot net
*/

/*
Modification of the original code: disable reset of delay interpolation.
*/

#include "SDL_framerate.h"

/*!
\brief Internal wrapper to SDL_GetTicks that ensures a non-zero return value.
\return The tick count.
*/
Uint32 _getTicks()
{
Uint32 ticks = SDL_GetTicks();

/*
* Since baseticks!=0 is used to track initialization
* we need to ensure that the tick count is always >0
* since SDL_GetTicks may not have incremented yet and
* return 0 depending on the timing of the calls.
*/
if (ticks == 0) {
return 1;
} else {
return ticks;
}
}

/*!
\brief Initialize the framerate manager.
Initialize the framerate manager, set default framerate of 30Hz and
reset delay interpolation.
\param manager Pointer to the framerate manager.
*/
void SDL_initFramerate(FPSmanager * manager)
{
/*
* Store some sane values
*/
manager->framecount = 0;
manager->rate = FPS_DEFAULT;
manager->rateticks = (1000.0f / (float) FPS_DEFAULT);
manager->baseticks = _getTicks();
manager->lastticks = manager->baseticks;

}

/*!
\brief Set the framerate in Hz
Sets a new framerate for the manager and reset delay interpolation.
Rate values must be between FPS_LOWER_LIMIT and FPS_UPPER_LIMIT inclusive to be accepted.
\param manager Pointer to the framerate manager.
\param rate The new framerate in Hz (frames per second).
\return 0 for sucess and -1 for error.
*/
int SDL_setFramerate(FPSmanager * manager, Uint32 rate)
{
if ((rate >= FPS_LOWER_LIMIT) && (rate <= FPS_UPPER_LIMIT)) {
manager->framecount = 0;
manager->rate = rate;
manager->rateticks = (1000.0f / (float) rate);
return (0);
} else {
return (-1);
}
}

/*!
\brief Return the current target framerate in Hz
Get the currently set framerate of the manager.
\param manager Pointer to the framerate manager.
\return Current framerate in Hz or -1 for error.
*/
int SDL_getFramerate(FPSmanager * manager)
{
if (manager == NULL) {
return (-1);
} else {
return ((int)manager->rate);
}
}

/*!
\brief Return the current framecount.
Get the current framecount from the framerate manager.
A frame is counted each time SDL_framerateDelay is called.
\param manager Pointer to the framerate manager.
\return Current frame count or -1 for error.
*/
int SDL_getFramecount(FPSmanager * manager)
{
if (manager == NULL) {
return (-1);
} else {
return ((int)manager->framecount);
}
}

/*!
\brief Delay execution to maintain a constant framerate and calculate fps.
Generate a delay to accomodate currently set framerate. Call once in the
graphics/rendering loop. If the computer cannot keep up with the rate (i.e.
drawing too slow), the delay is zero and the delay interpolation is reset.
\param manager Pointer to the framerate manager.
\return The time that passed since the last call to the function in ms. May return 0.
*/
Uint32 SDL_framerateDelay(FPSmanager * manager)
{
Uint32 current_ticks;
Uint32 target_ticks;
Uint32 the_delay;
Uint32 time_passed = 0;

/*
* No manager, no delay
*/
if (manager == NULL) {
return 0;
}

/*
* Initialize uninitialized manager
*/
if (manager->baseticks == 0) {
SDL_initFramerate(manager);
}

/*
* Next frame
*/
manager->framecount++;

/*
* Get/calc ticks
*/
current_ticks = _getTicks();
time_passed = current_ticks - manager->lastticks;
manager->lastticks = current_ticks;
target_ticks = manager->baseticks + (Uint32) ((float) manager->framecount * manager->rateticks);

if (current_ticks <= target_ticks) {
the_delay = target_ticks - current_ticks;
SDL_Delay(the_delay);
}
// NOTE: Modification of the original code
/*
else {
manager->framecount = 0;
manager->baseticks = _getTicks();
}
*/

return time_passed;
}
100 changes: 100 additions & 0 deletions main_hm/SDL_framerate.h
@@ -0,0 +1,100 @@
/*
SDL_framerate.h: framerate manager
Copyright (C) 2001-2012 Andreas Schiffler
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
Andreas Schiffler -- aschiffler at ferzkopp dot net
*/

#ifndef _SDL_framerate_h
#define _SDL_framerate_h

/* Set up for C function definitions, even when using C++ */
#ifdef __cplusplus
extern "C" {
#endif
/* --- */

#include "SDL.h"

/* --------- Definitions */

/*!
\brief Highest possible rate supported by framerate controller in Hz (1/s).
*/
#define FPS_UPPER_LIMIT 200

/*!
\brief Lowest possible rate supported by framerate controller in Hz (1/s).
*/
#define FPS_LOWER_LIMIT 1

/*!
\brief Default rate of framerate controller in Hz (1/s).
*/
#define FPS_DEFAULT 30

/*!
\brief Structure holding the state and timing information of the framerate controller.
*/
typedef struct {
Uint32 framecount;
float rateticks;
Uint32 baseticks;
Uint32 lastticks;
Uint32 rate;
} FPSmanager;

/* ---- Function Prototypes */

#ifdef _MSC_VER
# if defined(DLL_EXPORT) && !defined(LIBSDL_GFX_DLL_IMPORT)
# define SDL_FRAMERATE_SCOPE __declspec(dllexport)
# else
# ifdef LIBSDL_GFX_DLL_IMPORT
# define SDL_FRAMERATE_SCOPE __declspec(dllimport)
# endif
# endif
#endif
#ifndef SDL_FRAMERATE_SCOPE
# define SDL_FRAMERATE_SCOPE extern
#endif

/* Functions return 0 or value for sucess and -1 for error */

SDL_FRAMERATE_SCOPE void SDL_initFramerate(FPSmanager * manager);
SDL_FRAMERATE_SCOPE int SDL_setFramerate(FPSmanager * manager, Uint32 rate);
SDL_FRAMERATE_SCOPE int SDL_getFramerate(FPSmanager * manager);
SDL_FRAMERATE_SCOPE int SDL_getFramecount(FPSmanager * manager);
SDL_FRAMERATE_SCOPE Uint32 SDL_framerateDelay(FPSmanager * manager);

/* --- */

/* Ends C function definitions when using C++ */
#ifdef __cplusplus
}
#endif

#endif /* _SDL_framerate_h */
7 changes: 6 additions & 1 deletion main_hm/getopt.c
Expand Up @@ -60,6 +60,7 @@ void print_usage() {
printf(" -w : Do not apply cropping windows\n");
printf(" -l <Quality layer id> \n");
printf(" -s <num> Stop after num frames \n");
printf(" -r <num> Frame rate (FPS) \n");
}

/*
Expand Down Expand Up @@ -131,7 +132,7 @@ int getopt(int nargc, char * const *nargv, const char *ostr) {
void init_main(int argc, char *argv[]) {
// every command line option must be followed by ':' if it takes an
// argument, and '::' if this argument is optional
const char *ostr = "achi:no:p:f:s:t:wl:";
const char *ostr = "achi:no:p:f:s:t:wl:r:";

int c;
check_md5_flags = ENABLE;
Expand All @@ -144,6 +145,7 @@ void init_main(int argc, char *argv[]) {
no_cropping = DISABLE;
quality_layer_id = 0; // Base layer
num_frames = 0;
frame_rate = 0;

program = argv[0];

Expand Down Expand Up @@ -187,6 +189,9 @@ void init_main(int argc, char *argv[]) {
case 's':
num_frames = atoi(optarg);
break;
case 'r':
frame_rate = atoi(optarg);
break;
default:
print_usage();
exit(1);
Expand Down
1 change: 1 addition & 0 deletions main_hm/getopt.h
Expand Up @@ -52,6 +52,7 @@ int temporal_layer_id;
int quality_layer_id;
int no_cropping;
int num_frames;
int frame_rate;

// initialize APR and parse command-line options
void init_main(int argc, char *argv[]);
Expand Down
7 changes: 7 additions & 0 deletions main_hm/main.c
Expand Up @@ -181,6 +181,10 @@ static void video_decode_example(const char *filename)
openHevcFrameCpy.pvV = NULL;
#if USE_SDL
Init_Time();
if (frame_rate > 0) {
initFramerate_SDL();
setFramerate_SDL(frame_rate);
}
#endif
#ifdef TIME2
time_us = GetTimeMs64();
Expand Down Expand Up @@ -249,6 +253,9 @@ static void video_decode_example(const char *filename)
}
}
#if USE_SDL
if (frame_rate > 0) {
framerateDelay_SDL();
}
if (display_flags == ENABLE) {
libOpenHevcGetOutput(openHevcHandle, 1, &openHevcFrame);
libOpenHevcGetPictureInfo(openHevcHandle, &openHevcFrame.frameInfo);
Expand Down

0 comments on commit 8eb9bd5

Please sign in to comment.