Now uses the tbb::pipeline, we just have to add filters now!
This commit is contained in:
parent
4cfab2eaa0
commit
335832789a
@ -23,7 +23,7 @@ prpa_LDADD = \
|
|||||||
lib/libfilterSample.a \
|
lib/libfilterSample.a \
|
||||||
lib/libencoder.a
|
lib/libencoder.a
|
||||||
|
|
||||||
prpa_SOURCES = src/main.cc src/configuration.cc
|
prpa_SOURCES = src/main.cc src/configuration.cc src/pipeline.cc
|
||||||
|
|
||||||
prpa_LDFLAGS = \
|
prpa_LDFLAGS = \
|
||||||
$(BOOST_LDFLAGS) \
|
$(BOOST_LDFLAGS) \
|
||||||
|
@ -1,19 +1,43 @@
|
|||||||
#include "video_reader.hh"
|
#include "video_reader.hh"
|
||||||
|
|
||||||
VideoReader::VideoReader(std::string filename)
|
VideoReader::VideoReader(const VideoReader& vr)
|
||||||
: capture_(filename)
|
: filter(tbb::filter::serial_in_order)
|
||||||
|
{
|
||||||
|
this->capture_ = vr.capture_;
|
||||||
|
this->maxframe_ = vr.maxframe_;
|
||||||
|
this->current_ = vr.current_;
|
||||||
|
}
|
||||||
|
|
||||||
|
void* VideoReader::operator()(void* ign)
|
||||||
|
{
|
||||||
|
(void)ign;
|
||||||
|
|
||||||
|
if (this->capture_.grab() && this->current_ < this->maxframe_)
|
||||||
|
{
|
||||||
|
this->current_++;
|
||||||
|
cv::Mat retrieved;
|
||||||
|
cv::Mat* res = new cv::Mat;
|
||||||
|
this->capture_.retrieve(retrieved);
|
||||||
|
*res = retrieved.clone();
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void VideoReader::maxframe_set(int limit)
|
||||||
|
{
|
||||||
|
this->maxframe_ = limit;
|
||||||
|
}
|
||||||
|
|
||||||
|
int VideoReader::framecount_get()
|
||||||
{
|
{
|
||||||
if (this->capture_.isOpened())
|
if (this->capture_.isOpened())
|
||||||
{
|
return (int)this->capture_.get(CV_CAP_PROP_FRAME_COUNT);
|
||||||
while (this->capture_.grab())
|
else
|
||||||
{
|
return -1;
|
||||||
cv::Mat retrieved;
|
|
||||||
cv::Mat* mat = new cv::Mat();
|
|
||||||
this->capture_.retrieve(retrieved);
|
|
||||||
*mat = retrieved.clone();
|
|
||||||
this->images_.push_back(mat);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int VideoReader::fps_get()
|
int VideoReader::fps_get()
|
||||||
|
@ -2,17 +2,25 @@
|
|||||||
# define VIDEO_READER_HH_
|
# define VIDEO_READER_HH_
|
||||||
|
|
||||||
# include <string>
|
# include <string>
|
||||||
|
# include "tbb/pipeline.h"
|
||||||
# include "video.hh"
|
# include "video.hh"
|
||||||
|
|
||||||
class VideoReader: public Video
|
class VideoReader: public tbb::filter
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
VideoReader(std::string filename);
|
VideoReader(const VideoReader& vr);
|
||||||
|
VideoReader(std::string filename) : filter(tbb::filter::serial_in_order),
|
||||||
|
capture_(filename), current_(0) {}
|
||||||
|
int framecount_get();
|
||||||
int fps_get();
|
int fps_get();
|
||||||
int width_get();
|
int width_get();
|
||||||
int height_get();
|
int height_get();
|
||||||
|
void maxframe_set(int limit);
|
||||||
|
void* operator()(void*);
|
||||||
private:
|
private:
|
||||||
cv::VideoCapture capture_;
|
cv::VideoCapture capture_;
|
||||||
|
int maxframe_;
|
||||||
|
int current_;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif /* !VIDEO_READER_HH_ */
|
#endif /* !VIDEO_READER_HH_ */
|
||||||
|
@ -1,5 +1,11 @@
|
|||||||
#include "video_writer.hh"
|
#include "video_writer.hh"
|
||||||
|
|
||||||
|
VideoWriter::VideoWriter(const VideoWriter& vw)
|
||||||
|
: filter(tbb::filter::serial_in_order)
|
||||||
|
{
|
||||||
|
this->writer_ = vw.writer_;
|
||||||
|
}
|
||||||
|
|
||||||
bool VideoWriter::open(std::string filename, std::string enc,
|
bool VideoWriter::open(std::string filename, std::string enc,
|
||||||
int fps, int width, int height)
|
int fps, int width, int height)
|
||||||
{
|
{
|
||||||
@ -10,11 +16,12 @@ bool VideoWriter::open(std::string filename, std::string enc,
|
|||||||
CV_FOURCC(enc[0], enc[1], enc[2], enc[3]), fps, { width, height } );
|
CV_FOURCC(enc[0], enc[1], enc[2], enc[3]), fps, { width, height } );
|
||||||
}
|
}
|
||||||
|
|
||||||
void VideoWriter::write()
|
void* VideoWriter::operator()(void* para)
|
||||||
{
|
{
|
||||||
|
cv::Mat* mat = (cv::Mat*)para;
|
||||||
if (this->writer_.isOpened())
|
if (this->writer_.isOpened())
|
||||||
{
|
this->writer_.write(*mat);
|
||||||
for (auto it = this->images_.begin(); it != this->images_.end(); ++it)
|
|
||||||
this->writer_.write(**it);
|
delete mat;
|
||||||
}
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
@ -2,12 +2,15 @@
|
|||||||
# define VIDEO_WRITER_HH_
|
# define VIDEO_WRITER_HH_
|
||||||
|
|
||||||
# include <string>
|
# include <string>
|
||||||
|
# include "tbb/pipeline.h"
|
||||||
# include "video.hh"
|
# include "video.hh"
|
||||||
|
|
||||||
class VideoWriter: public Video
|
class VideoWriter: public tbb::filter
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
void write();
|
VideoWriter() : filter(tbb::filter::serial_in_order) {}
|
||||||
|
VideoWriter(const VideoWriter&);
|
||||||
|
void* operator()(void* mat);
|
||||||
bool open(std::string filename, std::string enc,
|
bool open(std::string filename, std::string enc,
|
||||||
int fps, int width, int height);
|
int fps, int width, int height);
|
||||||
private:
|
private:
|
||||||
|
@ -2,8 +2,10 @@
|
|||||||
#include <boost/program_options.hpp>
|
#include <boost/program_options.hpp>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include "tbb/task_scheduler_init.h"
|
||||||
|
|
||||||
#include "configuration.hh"
|
#include "configuration.hh"
|
||||||
|
#include "pipeline.hh"
|
||||||
|
|
||||||
#include "encoder/video.hh"
|
#include "encoder/video.hh"
|
||||||
#include "encoder/video_reader.hh"
|
#include "encoder/video_reader.hh"
|
||||||
@ -87,6 +89,7 @@ bool parse_cli(Configuration& conf, int argc, char** argv)
|
|||||||
|
|
||||||
int main (int argc, char** argv)
|
int main (int argc, char** argv)
|
||||||
{
|
{
|
||||||
|
tbb::task_scheduler_init init;
|
||||||
Configuration conf = Configuration();
|
Configuration conf = Configuration();
|
||||||
|
|
||||||
if (!parse_cli(conf, argc, argv))
|
if (!parse_cli(conf, argc, argv))
|
||||||
@ -95,8 +98,10 @@ int main (int argc, char** argv)
|
|||||||
VideoReader vr(conf.input.front());
|
VideoReader vr(conf.input.front());
|
||||||
VideoWriter vw = VideoWriter();
|
VideoWriter vw = VideoWriter();
|
||||||
vw.open(conf.output, conf.codec, vr.fps_get(), vr.width_get(), vr.height_get());
|
vw.open(conf.output, conf.codec, vr.fps_get(), vr.width_get(), vr.height_get());
|
||||||
vw.copy(vr);
|
|
||||||
vw.write();
|
Pipeline pipe(std::list<VideoReader> { vr }, vw);
|
||||||
|
// add filters
|
||||||
|
pipe.run();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
23
src/pipeline.cc
Normal file
23
src/pipeline.cc
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
#include "tbb/pipeline.h"
|
||||||
|
|
||||||
|
#include "pipeline.hh"
|
||||||
|
|
||||||
|
bool Pipeline::add_filter(std::string filter)
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Pipeline::run()
|
||||||
|
{
|
||||||
|
int minframe = this->input_.front().framecount_get();
|
||||||
|
for (auto it = this->input_.begin(); it != this->input_.end(); ++it)
|
||||||
|
if (minframe > it->framecount_get())
|
||||||
|
minframe = it->framecount_get();
|
||||||
|
for (auto it = this->input_.begin(); it != this->input_.end(); ++it)
|
||||||
|
it->maxframe_set(minframe);
|
||||||
|
|
||||||
|
this->pipeline_.add_filter(this->input_.front());
|
||||||
|
this->pipeline_.add_filter(this->output_);
|
||||||
|
|
||||||
|
this->pipeline_.run(CHUNK_SIZE);
|
||||||
|
}
|
28
src/pipeline.hh
Normal file
28
src/pipeline.hh
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
#ifndef PIPELINE_HH_
|
||||||
|
# define PIPELINE_HH_
|
||||||
|
|
||||||
|
# include <list>
|
||||||
|
# include "opencv2/opencv.hpp"
|
||||||
|
# include "tbb/pipeline.h"
|
||||||
|
|
||||||
|
# include "encoder/video.hh"
|
||||||
|
# include "encoder/video_reader.hh"
|
||||||
|
# include "encoder/video_writer.hh"
|
||||||
|
|
||||||
|
# define CHUNK_SIZE 16
|
||||||
|
|
||||||
|
class Pipeline
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Pipeline(std::list<VideoReader> input, VideoWriter output)
|
||||||
|
: input_(input), output_(output) {}
|
||||||
|
bool add_filter(std::string filter);
|
||||||
|
void run();
|
||||||
|
private:
|
||||||
|
std::list<tbb::filter_t<cv::Mat*, cv::Mat*>> filters_;
|
||||||
|
std::list<VideoReader> input_;
|
||||||
|
VideoWriter output_;
|
||||||
|
tbb::pipeline pipeline_;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif /* !PIPELINE_HH_ */
|
Reference in New Issue
Block a user