From 6f5d1817fdb305b9f63a5e8abfbcf46da41d3245 Mon Sep 17 00:00:00 2001 From: Tomi Valkeinen Date: Sun, 12 Jun 2016 09:50:16 +0300 Subject: utils: add wbm2m --- utils/CMakeLists.txt | 3 + utils/wbm2m.cpp | 168 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 171 insertions(+) create mode 100644 utils/wbm2m.cpp (limited to 'utils') diff --git a/utils/CMakeLists.txt b/utils/CMakeLists.txt index ece8c67..dd95f70 100644 --- a/utils/CMakeLists.txt +++ b/utils/CMakeLists.txt @@ -21,3 +21,6 @@ target_link_libraries(kmsblank kms++ kms++util ${LIBDRM_LIBRARIES}) add_executable (wbcap wbcap.cpp) target_link_libraries(wbcap kms++ kms++util ${LIBDRM_LIBRARIES}) + +add_executable (wbm2m wbm2m.cpp) +target_link_libraries(wbm2m kms++ kms++util ${LIBDRM_LIBRARIES}) diff --git a/utils/wbm2m.cpp b/utils/wbm2m.cpp new file mode 100644 index 0000000..1f8dffa --- /dev/null +++ b/utils/wbm2m.cpp @@ -0,0 +1,168 @@ +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +const uint32_t NUM_SRC_BUFS=2; +const uint32_t NUM_DST_BUFS=2; + +using namespace std; +using namespace kms; + +static const char* usage_str = + "Usage: wbm2m [OPTIONS]\n\n" + "Options:\n" + " -f, --format=4CC Output format" + " -h, --help Print this help\n" + ; + +const int bar_speed = 4; +const int bar_width = 10; + +static unsigned get_bar_pos(DumbFramebuffer* fb, unsigned frame_num) +{ + return (frame_num * bar_speed) % (fb->width() - bar_width + 1); +} + +static void read_frame(DumbFramebuffer* fb, unsigned frame_num) +{ + static map s_bar_pos_map; + + int old_pos = -1; + if (s_bar_pos_map.find(fb) != s_bar_pos_map.end()) + old_pos = s_bar_pos_map[fb]; + + int pos = get_bar_pos(fb, frame_num); + draw_color_bar(*fb, old_pos, pos, bar_width); + draw_text(*fb, fb->width() / 2, 0, to_string(frame_num), RGB(255, 255, 255)); + s_bar_pos_map[fb] = pos; +} + +int main(int argc, char** argv) +{ + // XXX get from args + const uint32_t src_width = 800; + const uint32_t src_height = 480; + const auto src_fmt = PixelFormat::XRGB8888; + const uint32_t num_src_frames = 10; + + const uint32_t dst_width = 800; + const uint32_t dst_height = 480; + auto dst_fmt = PixelFormat::XRGB8888; + + const string filename = "wb-out.raw"; + + OptionSet optionset = { + Option("f|format=", [&](string s) + { + dst_fmt = FourCCToPixelFormat(s); + }), + Option("h|help", [&]() + { + puts(usage_str); + exit(-1); + }), + }; + + optionset.parse(argc, argv); + + if (optionset.params().size() > 0) { + puts(usage_str); + exit(-1); + } + + VideoDevice vid("/dev/video10"); + + Card card; + + uint32_t src_frame_num = 0; + uint32_t dst_frame_num = 0; + + VideoStreamer* out = vid.get_output_streamer(); + VideoStreamer* in = vid.get_capture_streamer(); + + out->set_format(src_fmt, src_width, src_height); + in->set_format(dst_fmt, dst_width, dst_height); + + out->set_queue_size(NUM_SRC_BUFS); + in->set_queue_size(NUM_DST_BUFS); + + + for (unsigned i = 0; i < min(NUM_SRC_BUFS, num_src_frames); ++i) { + auto fb = new DumbFramebuffer(card, src_width, src_height, src_fmt); + + read_frame(fb, src_frame_num++); + + out->queue(fb); + } + + for (unsigned i = 0; i < min(NUM_DST_BUFS, num_src_frames); ++i) { + auto fb = new DumbFramebuffer(card, dst_width, dst_height, dst_fmt); + in->queue(fb); + } + + vector fds(3); + + fds[0].fd = 0; + fds[0].events = POLLIN; + fds[1].fd = vid.fd(); + fds[1].events = POLLIN; + fds[2].fd = card.fd(); + fds[2].events = POLLIN; + + ofstream os(filename, ofstream::binary); + + out->stream_on(); + in->stream_on(); + + while (true) { + int r = poll(fds.data(), fds.size(), -1); + ASSERT(r > 0); + + if (fds[0].revents != 0) + break; + + if (fds[1].revents) { + fds[1].revents = 0; + + + try { + DumbFramebuffer *dst_fb = in->dequeue(); + printf("Writing frame %u\n", dst_frame_num); + for (unsigned i = 0; i < dst_fb->num_planes(); ++i) + os.write((char*)dst_fb->map(i), dst_fb->size(i)); + in->queue(dst_fb); + + dst_frame_num++; + + if (dst_frame_num >= num_src_frames) + break; + + } catch (system_error& se) { + if (se.code() != errc::resource_unavailable_try_again) + FAIL("dequeue failed: %s", se.what()); + + break; + } + + DumbFramebuffer *src_fb = out->dequeue(); + + if (src_frame_num < num_src_frames) { + read_frame(src_fb, src_frame_num++); + out->queue(src_fb); + } + } + + if (fds[2].revents) { + fds[2].revents = 0; + } + } + + printf("exiting...\n"); +} -- cgit v1.2.3