4 #include <condition_variable>
12 #include <sys/types.h>
17 #include <libavformat/avformat.h>
20 #include "clip_list.h"
23 #include "disk_space_estimator.h"
24 #include "ffmpeg_raii.h"
27 #include "mainwindow.h"
29 #include "post_to_main_thread.h"
30 #include "ref_counted_gl_sync.h"
32 #include "ui_mainwindow.h"
33 #include "vaapi_jpeg_decoder.h"
35 #include <QApplication>
36 #include <movit/init.h>
37 #include <movit/util.h>
40 using namespace std::chrono;
42 mutex RefCountedGLsync::fence_lock;
43 atomic<bool> should_quit{false};
45 int64_t start_pts = -1;
47 // TODO: Replace by some sort of GUI control, I guess.
48 int64_t current_pts = 0;
50 string filename_for_frame(unsigned stream_idx, int64_t pts)
53 snprintf(filename, sizeof(filename), "frames/cam%d-pts%09ld.jpeg", stream_idx, pts);
58 vector<int64_t> frames[MAX_STREAMS];
61 void load_existing_frames();
62 int record_thread_func();
64 int main(int argc, char **argv)
66 parse_flags(argc, argv);
68 global_flags.stream_source = "multiangle.mp4";
69 global_flags.slow_down_input = true;
70 } else if (optind + 1 == argc) {
71 global_flags.stream_source = argv[optind];
77 avformat_network_init();
78 global_httpd = new HTTPD;
79 global_httpd->start(DEFAULT_HTTPD_PORT);
81 QCoreApplication::setAttribute(Qt::AA_ShareOpenGLContexts, true);
84 fmt.setDepthBufferSize(0);
85 fmt.setStencilBufferSize(0);
86 fmt.setProfile(QSurfaceFormat::CoreProfile);
87 fmt.setMajorVersion(4);
88 fmt.setMinorVersion(5);
90 // Turn off vsync, since Qt generally gives us at most frame rate
91 // (display frequency) / (number of QGLWidgets active).
92 fmt.setSwapInterval(0);
94 QSurfaceFormat::setDefaultFormat(fmt);
96 QGLFormat::setDefaultFormat(QGLFormat::fromSurfaceFormat(fmt));
98 QApplication app(argc, argv);
99 global_share_widget = new QGLWidget();
100 if (!global_share_widget->isValid()) {
101 fprintf(stderr, "Failed to initialize OpenGL. Futatabi needs at least OpenGL 4.5 to function properly.\n");
107 QSurface *surface = create_surface();
108 QOpenGLContext *context = create_context(surface);
109 make_current(context, surface);
110 CHECK(movit::init_movit(MOVIT_SHADER_DIR, movit::MOVIT_DEBUG_OFF));
111 delete_context(context);
112 // TODO: Delete the surface, too.
115 MainWindow mainWindow;
120 load_existing_frames();
121 thread record_thread(record_thread_func);
123 int ret = app.exec();
126 record_thread.join();
127 JPEGFrameView::shutdown();
132 void load_existing_frames()
134 DIR *dir = opendir("frames/");
135 if (dir == nullptr) {
143 dirent *de = readdir(dir);
154 if (sscanf(de->d_name, "cam%d-pts%ld.jpeg", &stream_idx, &pts) == 2 &&
155 stream_idx >= 0 && stream_idx < MAX_STREAMS) {
156 frames[stream_idx].push_back(pts);
157 start_pts = max(start_pts, pts);
163 if (start_pts == -1) {
166 // Add a gap of one second from the old frames to the new ones.
167 start_pts += TIMEBASE;
170 for (int stream_idx = 0; stream_idx < MAX_STREAMS; ++stream_idx) {
171 sort(frames[stream_idx].begin(), frames[stream_idx].end());
175 int record_thread_func()
177 auto format_ctx = avformat_open_input_unique(global_flags.stream_source.c_str(), nullptr, nullptr);
178 if (format_ctx == nullptr) {
179 fprintf(stderr, "%s: Error opening file\n", global_flags.stream_source.c_str());
183 int64_t last_pts = -1;
186 while (!should_quit.load()) {
188 unique_ptr<AVPacket, decltype(av_packet_unref)*> pkt_cleanup(
189 &pkt, av_packet_unref);
190 av_init_packet(&pkt);
194 // TODO: Make it possible to abort av_read_frame() (use an interrupt callback);
195 // right now, should_quit will be ignored if it's hung on I/O.
196 if (av_read_frame(format_ctx.get(), &pkt) != 0) {
200 // Convert pts to our own timebase.
201 AVRational stream_timebase = format_ctx->streams[pkt.stream_index]->time_base;
202 int64_t pts = av_rescale_q(pkt.pts, stream_timebase, AVRational{ 1, TIMEBASE });
204 // Translate offset into our stream.
205 if (last_pts == -1) {
206 pts_offset = start_pts - pts;
208 pts = std::max(pts + pts_offset, start_pts);
210 //fprintf(stderr, "Got a frame from camera %d, pts = %ld, size = %d\n",
211 // pkt.stream_index, pts, pkt.size);
212 string filename = filename_for_frame(pkt.stream_index, pts);
213 FILE *fp = fopen(filename.c_str(), "wb");
215 perror(filename.c_str());
218 fwrite(pkt.data, pkt.size, 1, fp);
221 global_disk_space_estimator->report_write(filename, pts);
223 post_to_main_thread([pkt, pts] {
224 if (pkt.stream_index == 0) {
225 global_mainwindow->ui->input1_display->setFrame(pkt.stream_index, pts, /*interpolated=*/false);
226 } else if (pkt.stream_index == 1) {
227 global_mainwindow->ui->input2_display->setFrame(pkt.stream_index, pts, /*interpolated=*/false);
228 } else if (pkt.stream_index == 2) {
229 global_mainwindow->ui->input3_display->setFrame(pkt.stream_index, pts, /*interpolated=*/false);
230 } else if (pkt.stream_index == 3) {
231 global_mainwindow->ui->input4_display->setFrame(pkt.stream_index, pts, /*interpolated=*/false);
235 assert(pkt.stream_index < MAX_STREAMS);
236 frames[pkt.stream_index].push_back(pts);
238 if (last_pts != -1 && global_flags.slow_down_input) {
239 this_thread::sleep_for(microseconds((pts - last_pts) * 1000000 / TIMEBASE));