2 #include "output-drv.hpp"
12 std::string
expand_options(const std::string
& opts
, uint32_t rn
, uint32_t rd
)
15 std::ostringstream ret
;
17 ret
<< "--fps " << rn
<< "/" << rd
<< " ";
18 for(size_t i
= 0; i
< opts
.length(); i
++) {
37 class output_driver_x264
: public output_driver
40 output_driver_x264(const std::string
& _filename
, const std::string
& _options
)
44 set_video_callback
<output_driver_x264
>(*this, &output_driver_x264::video_callback
);
54 const video_settings
& v
= get_video_settings();
55 framesize
= 4 * v
.get_width() * v
.get_height();
56 width
= v
.get_width();
58 std::stringstream commandline
;
59 commandline
<< "x264 ";
60 commandline
<< expand_options(options
, v
.get_rate_num(), v
.get_rate_denum());
61 commandline
<< "- -o " << filename
<< " " << v
.get_width() << "x" << v
.get_height();
62 std::string s
= commandline
.str();
63 out
= popen(s
.c_str(), "w");
65 std::stringstream str
;
66 str
<< "Can't run x264 (" << s
<< ")";
67 throw std::runtime_error(str
.str());
71 void video_callback(uint64_t timestamp
, const uint8_t* raw_rgbx_data
)
73 std::vector
<unsigned char> tmp(framesize
* 3 / 8);
74 size_t primarysize
= framesize
/ 4;
75 size_t offs1
= primarysize
/ 4;
77 Convert32To_I420Frame(raw_rgbx_data
, &tmp
[0], framesize
/ 4, width
);
79 if((r
= fwrite(&tmp
[0], 1, primarysize
, out
)) < primarysize
) {
80 std::stringstream str
;
81 str
<< "Error writing frame to x264 (requested " << primarysize
<< ", got " << r
<< ")";
82 throw std::runtime_error(str
.str());
85 if((r
= fwrite(&tmp
[primarysize
+ offs1
], 1, primarysize
/ 4, out
)) < primarysize
/ 4) {
86 std::stringstream str
;
87 str
<< "Error writing frame to x264 (requested " << primarysize
/ 4 << ", got " << r
<< ")";
88 throw std::runtime_error(str
.str());
90 if((r
= fwrite(&tmp
[primarysize
+ offs2
], 1, primarysize
/ 4, out
)) < primarysize
/ 4) {
91 std::stringstream str
;
92 str
<< "Error writing frame to x264 (requested " << primarysize
/ 4 << ", got " << r
<< ")";
93 throw std::runtime_error(str
.str());
104 class output_driver_x264_factory
: output_driver_factory
107 output_driver_x264_factory()
108 : output_driver_factory("x264")
112 output_driver
& make(const std::string
& type
, const std::string
& name
, const std::string
& parameters
)
114 return *new output_driver_x264(name
, parameters
);