streamtools: Remove few unneeded includes.
[jpcrr.git] / streamtools / output-drv-x264.cpp
blob49caffdc176dbff79d2b6ffe1c0dd3f77e564258
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 << "--fps " << rn << "/" << rd << " ";
16 ret << expand_arguments_common(opts, "--", " ");
17 return ret.str();
20 class output_driver_x264 : public output_driver
22 public:
23 output_driver_x264(const std::string& _filename, const std::string& _options)
25 filename = _filename;
26 options = _options;
27 set_video_callback<output_driver_x264>(*this, &output_driver_x264::video_callback);
30 ~output_driver_x264()
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 << "x264 ";
44 commandline << expand_options(options, v.get_rate_num(), v.get_rate_denum());
45 commandline << " - -o " << filename << " " << v.get_width() << "x" << v.get_height();
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 x264 (" << 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_x264_factory : output_driver_factory
70 public:
71 output_driver_x264_factory()
72 : output_driver_factory("x264")
76 output_driver& make(const std::string& type, const std::string& name, const std::string& parameters)
78 return *new output_driver_x264(name, parameters);
80 } factory;