6 #include <condition_variable>
14 #include <libavformat/avformat.h>
17 #include <QApplication>
19 #include "clip_list.h"
21 #include "mainwindow.h"
22 #include "ffmpeg_raii.h"
24 #include "post_to_main_thread.h"
25 #include "ui_mainwindow.h"
28 using namespace std::chrono;
30 // TODO: Replace by some sort of GUI control, I guess.
31 int64_t current_pts = 0;
33 string filename_for_frame(unsigned stream_idx, int64_t pts)
36 snprintf(filename, sizeof(filename), "frames/cam%d-pts%09ld.jpeg", stream_idx, pts);
41 vector<int64_t> frames[MAX_STREAMS];
42 QGLWidget *global_share_widget;
44 int record_thread_func();
46 int main(int argc, char **argv)
48 avformat_network_init();
50 QCoreApplication::setAttribute(Qt::AA_ShareOpenGLContexts, true);
53 fmt.setDepthBufferSize(0);
54 fmt.setStencilBufferSize(0);
55 fmt.setProfile(QSurfaceFormat::CoreProfile);
56 fmt.setMajorVersion(3);
57 fmt.setMinorVersion(1);
59 // Turn off vsync, since Qt generally gives us at most frame rate
60 // (display frequency) / (number of QGLWidgets active).
61 fmt.setSwapInterval(0);
63 QSurfaceFormat::setDefaultFormat(fmt);
65 QGLFormat::setDefaultFormat(QGLFormat::fromSurfaceFormat(fmt));
67 QApplication app(argc, argv);
68 global_share_widget = new QGLWidget();
69 if (!global_share_widget->isValid()) {
70 fprintf(stderr, "Failed to initialize OpenGL. Futatabi needs at least OpenGL 3.1 to function properly.\n");
73 MainWindow mainWindow;
76 thread(record_thread_func).detach();
81 int record_thread_func()
83 auto format_ctx = avformat_open_input_unique("multiangle.mp4", nullptr, nullptr);
84 if (format_ctx == nullptr) {
85 fprintf(stderr, "%s: Error opening file\n", "example.mp4");
89 int64_t last_pts = -1;
93 unique_ptr<AVPacket, decltype(av_packet_unref)*> pkt_cleanup(
94 &pkt, av_packet_unref);
98 if (av_read_frame(format_ctx.get(), &pkt) != 0) {
101 //fprintf(stderr, "Got a frame from camera %d, pts = %ld, size = %d\n",
102 // pkt.stream_index, pkt.pts, pkt.size);
103 string filename = filename_for_frame(pkt.stream_index, pkt.pts);
104 FILE *fp = fopen(filename.c_str(), "wb");
106 perror(filename.c_str());
109 fwrite(pkt.data, pkt.size, 1, fp);
112 post_to_main_thread([pkt] {
113 if (pkt.stream_index == 0) {
114 global_mainwindow->ui->input1_display->setFrame(pkt.stream_index, pkt.pts);
115 } else if (pkt.stream_index == 1) {
116 global_mainwindow->ui->input2_display->setFrame(pkt.stream_index, pkt.pts);
117 } else if (pkt.stream_index == 2) {
118 global_mainwindow->ui->input3_display->setFrame(pkt.stream_index, pkt.pts);
119 } else if (pkt.stream_index == 3) {
120 global_mainwindow->ui->input4_display->setFrame(pkt.stream_index, pkt.pts);
124 assert(pkt.stream_index < MAX_STREAMS);
125 frames[pkt.stream_index].push_back(pkt.pts);
127 // Hack. Assumes a given timebase.
128 if (last_pts != -1) {
129 this_thread::sleep_for(microseconds((pkt.pts - last_pts) * 1000000 / 12800));
132 current_pts = pkt.pts;