Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In memory frame buffer #191

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions liboptv/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ set (CMAKE_INSTALL_RPATH "$ORIGIN/../lib:$ORIGIN/")

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/CMakeModules/")
project(OpenPTV)
# enable_testing()
enable_testing()
add_subdirectory(src)
# add_subdirectory(tests)
add_subdirectory(tests)

INSTALL(DIRECTORY include/ DESTINATION include/optv/)

Expand Down
112 changes: 106 additions & 6 deletions liboptv/include/tracking_frame_buf.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,22 +82,122 @@ int write_frame(frame *self, char *corres_file_base, char *linkage_file_base,
char *prio_file_base, char **target_file_base, int frame_num);


/*
* Following is the frame buffer class, that holds a given number of frame structs,
* and treats them as a deque (doube-ended queue, a queue that can advance forward
* or backward).
*
* The memory locations are advanced with fb_next()/fb_prev(). Filling out the new
* frame that joined the queue and flushing out the frame that exited the queue
* are respectively done using fb_read_frame_at_end() and fb_write_frame_from_start().
*
* To make it possible to read frames from different sources, we use a
* base-class/child-class structure. framebuf_base is the base class, and implements
* only the memory-advance methods. The filling-out of frames is done by
* "virtual" functions. That is to say, fb_read_frame_at_end, for example,
* only forwards the call to a function that really reads the data.
*
* The virtual functions are placed in the "vtable" by the constructor - the
* function that allocates and populates the child class.
*
* In tracking_framebuf.c and here there's an example child-class that reads
* frame information from *_target files. This is the original mode of
* liboptv. The struct framebuf *inherits* from framebuf_base (by incorporating
* the base as 1st member - the order is important). fb_init(), the constructor,
* then populates the vtable with e.g. fb_disk_* functions. These are the derived
* implementations of the base-class virtual functions.
*
* There is also a virtual destructor, fb_free(), which delegates to the free()
* virtual function, and implemented in the example by e.g. fb_disk_free().
*
* Note: fb_disk_free does not release the strings it holds, as I don't remember if
* it owns them.
*
* Yes, in C++ it's easier :)
*/

// Virtual function table definitions for frame buffer objects:
typedef struct framebuf_base* fbp;
typedef struct {
void (*free)(fbp self);
int (*read_frame_at_end)(fbp self, int frame_num, int read_links);
int (*write_frame_from_start)(fbp self, int frame_num);
} fb_vtable;

typedef struct framebuf_base {
fb_vtable *_vptr;

/* _ring_vec is the underlying double-size vector, buf is the pointer to
the start of the ring. */
frame **buf, **_ring_vec;
int buf_len, num_cams;
} framebuf_base;

// These just call the corresponding virtual function.
// Actual implementations are below each child class.
void fb_free(framebuf_base *self);
int fb_read_frame_at_end(framebuf_base *self, int frame_num, int read_links);
int fb_write_frame_from_start(framebuf_base *self, int frame_num);

// Non-virtual methods of the base class.
void fb_base_init(framebuf_base *new_buf, int buf_len, int num_cams, int max_targets);
void fb_next(framebuf_base *self);
void fb_prev(framebuf_base *self);

// child class that reads from _target files.
typedef struct {
framebuf_base base; // must be 1st member.

char *corres_file_base, *linkage_file_base, *prio_file_base;
char **target_file_base;
} framebuf;

void fb_init(framebuf *new_buf, int buf_len, int num_cams, int max_targets,\
void fb_init(framebuf *new_buf, int buf_len, int num_cams, int max_targets,
char *corres_file_base, char* linkage_file_base, char *prio_file_base,
char **target_file_base);
void fb_free(framebuf *self);
void fb_next(framebuf *self);
void fb_prev(framebuf *self);
int fb_read_frame_at_end(framebuf *self, int frame_num, int read_links);
int fb_write_frame_from_start(framebuf *self, int frame_num);

void fb_disk_free(framebuf_base *self);
int fb_disk_read_frame_at_end(framebuf_base *self, int frame_num, int read_links);
int fb_disk_write_frame_from_start(framebuf_base *self, int frame_num);

// Child class that reads from memory. The incoming/outgoing frames are read
// or written to a memory in address changed from outside on each frame. The
// tracking code is responsible for managing those pointers. The information
// on when to change them is passed by using callbacks: whenever the framebuf
// finishes a read or write it calls the respective callback, to let the
// tracker do the necessary updates.
typedef int (*mem_io_fun)(int frame_num, void* tracker_info);
typedef struct {
framebuf_base base; // must be 1st member.

frame **incoming, **outgoing;
mem_io_fun read_callback;
mem_io_fun write_callback;
void* tracker_info; // constant pointer, passed to the callbacks.

// pointer to pointer: the outside pointer is constant, and describes
// where the driver code keeps the address of the (changing) incoming
// frame, or the possibly-changing place to write the outgoing frame.
// Note that the incoming/outgoing `frame` struct is copied from/to
// incoming/outgoing mmemory, but the subordinate allocations (corres,
// target, etc.) are not - it's up to you to recycle or delete them,
// just like it's up to you to allocate them.
} framebuf_inmem;

void fb_inmem_init(
framebuf_inmem *new_buf,
int buf_len, int num_cams, int max_targets,
frame **incoming, frame **outgoing, void* tracker_info,
mem_io_fun read_callback, mem_io_fun write_callback
);

void fb_inmem_free(framebuf_base *self);
// Note: frame number is not currently used. When we fold the threaded
// code into here, we might have an objects that can translate frame-num to
// pointer, instead of using **incoming etc. Anyway, we must have this
// argument because that's how the base-class defines this virtual function.
// read_links is ignored because either the incoming frame has them or it doesn't.
int fb_inmem_read_frame_at_end(framebuf_base *self, int frame_num, int read_links);
int fb_inmem_write_frame_from_start(framebuf_base *self, int frame_num);

#endif
11 changes: 6 additions & 5 deletions liboptv/include/tracking_run.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include "tracking_frame_buf.h"

typedef struct {
framebuf *fb;
framebuf_base *fb;
sequence_par *seq_par;
track_par *tpar;
volume_par *vpar;
Expand All @@ -27,10 +27,11 @@ typedef struct {

tracking_run* tr_new_legacy(char *seq_par_fname, char *tpar_fname,
char *vpar_fname, char *cpar_fnamei, Calibration **cal);
tracking_run* tr_new(sequence_par *seq_par, track_par *tpar,
volume_par *vpar, control_par *cpar, int buf_len, int max_targets,
char *corres_file_base, char *linkage_file_base, char *prio_file_base,
Calibration **cal, double flatten_tol);

tracking_run *tr_new(
sequence_par *seq_par, track_par *tpar, volume_par *vpar, control_par *cpar,
Calibration **cal, framebuf_base *fb, double flatten_tol);

void tr_free(tracking_run *tr);

#endif
4 changes: 2 additions & 2 deletions liboptv/src/track.c
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,7 @@ void trackcorr_c_loop (tracking_run *run_info, int step) {

/* Shortcuts into the tracking_run struct */
Calibration **cal;
framebuf *fb;
framebuf_base *fb;
track_par *tpar;
volume_par *vpar;
control_par *cpar;
Expand Down Expand Up @@ -1002,7 +1002,7 @@ double trackback_c (tracking_run *run_info)
track_par *tpar;
volume_par *vpar;
control_par *cpar;
framebuf *fb;
framebuf_base *fb;
Calibration **cal;

/* Shortcuts to inside current frame */
Expand Down
Loading