runMACS
 All Data Structures Files Functions Variables Enumerations Enumerator Macros
FFMpegExtractor.cpp
Go to the documentation of this file.
1 #include <FFMpegExtractor.h>
2 #include <utils.h>
3 #include <stringtricks.h>
4 #include <ConfigManager.h>
5 
6 #include <config.h>
7 
8 #include <cstring>
9 #include <cstdio>
10 #ifdef HAVE_WINDOWS
11 #define POPEN _popen
12 #define PCLOSE _pclose
13 #define POPEN_MODE_WRITE "wb"
14 #endif
15 #ifdef HAVE_LINUX
16 #define POPEN popen
17 #define PCLOSE pclose
18 #define POPEN_MODE_WRITE "w"
19 #endif
20 
21 #include <iostream>
22 #include <sstream>
23 
24 using namespace std;
25 
26 FFMpegExtractor::FFMpegExtractor(zmq::context_t & _ctx,
27  const PubSubEndpoint & _source,
28  const std::string & _identifier,
29  const std::string & _destination) :
30  Extractor(_ctx, _source),
31  s_identifier(_identifier),
32  destination(_destination),
33  width(0),
34  height(0),
35  bytesPerPixel(0) {
36  pix_fmt = string("bayer_bggr8");
37 }
38 
40 
41 }
42 
43 std::string FFMpegExtractor::type() {
44  return "FFMpeg";
45 }
46 
48  return s_identifier;
49 }
50 
51 std::list<std::string> FFMpegExtractor::parameters() {
52  return {destination, keepGoing?"active":"done"};
53 }
54 
55 /****** PRIVATE *****/
56 
57 void FFMpegExtractor::openStream() {
58  auto cmd = SSTR(
59  "/usr/bin/ffmpeg ",
60  "-re ",
61  "-f rawvideo ",
62  "-sn -an -vcodec rawvideo -pix_fmt ", pix_fmt, " ",
63  "-video_size ", width/reduction, "x", height/reduction, " ",
64  "-threads 4 ",
65  "-i - ",
66  "-f flv ",
67  destination);
68  cout << cmd << endl;
69  ffmpeg = POPEN(cmd.c_str(), POPEN_MODE_WRITE);
70 }
71 
72 void FFMpegExtractor::closeStream() {
73  if(ffmpeg != nullptr) {
74  PCLOSE(ffmpeg);
75  ffmpeg = nullptr;
76  }
77 }
78 
79 void FFMpegExtractor::handleSize(unsigned int _width, unsigned int _height, unsigned int _bytesPerPixel) {
80  if((width != _width) | (height != _height) | (bytesPerPixel != _bytesPerPixel)) {
81  closeStream();
82  if(buffer != nullptr) {
83  delete buffer;
84  }
85  width = _width;
86  height = _height;
87  bytesPerPixel = _bytesPerPixel;
88  openStream();
89  buffer = new unsigned char[width * height / (reduction * reduction)];
90  }
91 }
92 
93 template<typename D, unsigned int shift, unsigned int pattern_size>
94 void collectShift(unsigned char* target, void* source, size_t width, size_t height, unsigned int reduction=1) {
95  D* d_source = reinterpret_cast<D*>(source);
96  unsigned int k = 0;
97  for(size_t i = 0; i < height; i+=reduction*pattern_size) {
98  for(size_t ii = 0; ii < pattern_size; ++ii) {
99  for(size_t j = 0; j < width; j+=reduction*pattern_size) {
100  for(size_t jj = 0; jj < pattern_size; ++jj) {
101  target[k] = d_source[(i+ii)*width+(j+jj)] >> shift;
102  ++k;
103  }
104  }
105  }
106  }
107 }
108 
109 void FFMpegExtractor::extractImpl(unsigned int _width,
110  unsigned int _height,
111  unsigned int _bytesPerPixel,
112  uint64_t highResTime,
113  uint64_t wallTime,
114  zmq::message_t * sourceImage) {
115  handleSize(_width, _height, _bytesPerPixel);
116  if(bytesPerPixel == 1) {
117  collectShift<uint8_t, 0, 2>(buffer, sourceImage->data(), width, height, reduction);
118  } else if(bytesPerPixel == 2) {
119  collectShift<uint16_t, 4, 2>(buffer, sourceImage->data(), width, height, reduction);
120  } else {
121  throw 5;
122  }
123 
124  fwrite(buffer, width/reduction, height/reduction, ffmpeg);
125  /* minimal implementation MUST delete sourceImage! */
126  delete sourceImage;
127 }
128 
129 void FFMpegExtractor::extractTeardown() {
130  closeStream();
131 }
Base class for an image extractor.
Definition: Extractor.h:17
STL namespace.
std::string SSTR(Args &&...components)
Creates a temporary string stream for string concatenation.
Definition: stringtricks.h:21
std::string identifier()
std::list< std::string > parameters()
volatile bool keepGoing
Definition: Extractor.h:29
FFMpegExtractor(zmq::context_t &_ctx, const PubSubEndpoint &_source, const std::string &_identifier, const std::string &_destination)
void collectShift(unsigned char *target, void *source, size_t width, size_t height, unsigned int reduction=1)
std::string type()