6 #include <condition_variable>
14 #include <libavformat/avformat.h>
17 #include <QApplication>
19 #include <movit/init.h>
20 #include <movit/util.h>
22 #include "clip_list.h"
25 #include "mainwindow.h"
26 #include "ffmpeg_raii.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"
36 using namespace std::chrono;
38 std::mutex RefCountedGLsync::fence_lock;
40 // TODO: Replace by some sort of GUI control, I guess.
41 int64_t current_pts = 0;
43 string filename_for_frame(unsigned stream_idx, int64_t pts)
46 snprintf(filename, sizeof(filename), "frames/cam%d-pts%09ld.jpeg", stream_idx, pts);
51 vector<int64_t> frames[MAX_STREAMS];
54 int record_thread_func();
56 int main(int argc, char **argv)
58 avformat_network_init();
59 global_httpd = new HTTPD;
60 global_httpd->start(DEFAULT_HTTPD_PORT);
62 QCoreApplication::setAttribute(Qt::AA_ShareOpenGLContexts, true);
65 fmt.setDepthBufferSize(0);
66 fmt.setStencilBufferSize(0);
67 fmt.setProfile(QSurfaceFormat::CoreProfile);
68 fmt.setMajorVersion(4);
69 fmt.setMinorVersion(5);
71 // Turn off vsync, since Qt generally gives us at most frame rate
72 // (display frequency) / (number of QGLWidgets active).
73 fmt.setSwapInterval(0);
75 QSurfaceFormat::setDefaultFormat(fmt);
77 QGLFormat::setDefaultFormat(QGLFormat::fromSurfaceFormat(fmt));
79 QApplication app(argc, argv);
80 global_share_widget = new QGLWidget();
81 if (!global_share_widget->isValid()) {
82 fprintf(stderr, "Failed to initialize OpenGL. Futatabi needs at least OpenGL 4.5 to function properly.\n");
88 QSurface *surface = create_surface();
89 QOpenGLContext *context = create_context(surface);
90 make_current(context, surface);
91 CHECK(movit::init_movit(MOVIT_SHADER_DIR, movit::MOVIT_DEBUG_OFF));
92 delete_context(context);
93 // TODO: Delete the surface, too.
96 MainWindow mainWindow;
99 thread(record_thread_func).detach();
106 int record_thread_func()
108 auto format_ctx = avformat_open_input_unique("multiangle.mp4", nullptr, nullptr);
109 if (format_ctx == nullptr) {
110 fprintf(stderr, "%s: Error opening file\n", "example.mp4");
114 int64_t last_pts = -1;
118 unique_ptr<AVPacket, decltype(av_packet_unref)*> pkt_cleanup(
119 &pkt, av_packet_unref);
120 av_init_packet(&pkt);
123 if (av_read_frame(format_ctx.get(), &pkt) != 0) {
127 // Convert pts to our own timebase.
128 // TODO: Figure out offsets, too.
129 AVRational stream_timebase = format_ctx->streams[pkt.stream_index]->time_base;
130 pkt.pts = av_rescale_q(pkt.pts, stream_timebase, AVRational{ 1, TIMEBASE });
132 //fprintf(stderr, "Got a frame from camera %d, pts = %ld, size = %d\n",
133 // pkt.stream_index, pkt.pts, pkt.size);
134 string filename = filename_for_frame(pkt.stream_index, pkt.pts);
135 FILE *fp = fopen(filename.c_str(), "wb");
137 perror(filename.c_str());
140 fwrite(pkt.data, pkt.size, 1, fp);
143 post_to_main_thread([pkt] {
144 if (pkt.stream_index == 0) {
145 global_mainwindow->ui->input1_display->setFrame(pkt.stream_index, pkt.pts, /*interpolated=*/false);
146 } else if (pkt.stream_index == 1) {
147 global_mainwindow->ui->input2_display->setFrame(pkt.stream_index, pkt.pts, /*interpolated=*/false);
148 } else if (pkt.stream_index == 2) {
149 global_mainwindow->ui->input3_display->setFrame(pkt.stream_index, pkt.pts, /*interpolated=*/false);
150 } else if (pkt.stream_index == 3) {
151 global_mainwindow->ui->input4_display->setFrame(pkt.stream_index, pkt.pts, /*interpolated=*/false);
155 assert(pkt.stream_index < MAX_STREAMS);
156 frames[pkt.stream_index].push_back(pkt.pts);
158 // Hack. Remove when we're dealing with live streams.
159 if (last_pts != -1) {
160 this_thread::sleep_for(microseconds((pkt.pts - last_pts) * 1000000 / TIMEBASE));
163 current_pts = pkt.pts;