streamtools: refactor output/scaler drivers to their own directories
[jpcrr.git] / streamtools / output-drv / ivfenc.cpp
blob3932caf2a21ab77b8055c8c1650c394ee42d7061
1 #include <cstring>
2 #include "output-drv.hpp"
3 #include <cstdio>
4 #include <stdexcept>
5 #include <vector>
6 #include <string>
7 #include <sstream>
9 namespace
11 std::string expand_options(const std::string& opts, uint32_t rn, uint32_t rd)
13 std::ostringstream ret;
14 if(rd)
15 ret << "--timebase " << rd << "/" << rn << " ";
16 ret << expand_arguments_common(opts, "--", "=");
17 return ret.str();
20 class output_driver_ivfenc : public output_driver
22 public:
23 output_driver_ivfenc(const std::string& _filename, const std::string& _options)
25 filename = _filename;
26 options = _options;
27 set_video_callback<output_driver_ivfenc>(*this, &output_driver_ivfenc::video_callback);
30 ~output_driver_ivfenc()
32 pclose(out);
35 void ready()
37 const video_settings& v = get_video_settings();
38 framesize = 4 * v.get_width() * v.get_height();
39 width = v.get_width();
40 height = v.get_height();
42 std::stringstream commandline;
43 commandline << "ivfenc --width=" << v.get_width() << " --height=" << v.get_height() << " ";
44 commandline << expand_options(options, v.get_rate_num(), v.get_rate_denum());
45 commandline << " - " << filename;
46 std::string s = commandline.str();
47 out = popen(s.c_str(), "w");
48 if(!out) {
49 std::stringstream str;
50 str << "Can't run ivfenc (" << s << ")";
51 throw std::runtime_error(str.str());
55 void video_callback(uint64_t timestamp, const uint8_t* raw_rgbx_data)
57 I420_convert_common(raw_rgbx_data, width, height, out, true);
59 private:
60 FILE* out;
61 std::string filename;
62 std::string options;
63 size_t framesize;
64 uint32_t width;
65 uint32_t height;
68 class output_driver_ivfenc_factory : output_driver_factory
70 public:
71 output_driver_ivfenc_factory()
72 : output_driver_factory("ivfenc")
76 output_driver& make(const std::string& type, const std::string& name, const std::string& parameters)
78 return *new output_driver_ivfenc(name, parameters);
80 } factory;