]> git.sesse.net Git - nageru/blob - main.cpp
Start hacking in support for interpolated frames in the main application.
[nageru] / main.cpp
1 #include <assert.h>
2 #include <stdio.h>
3 #include <stdint.h>
4
5 #include <chrono>
6 #include <condition_variable>
7 #include <memory>
8 #include <mutex>
9 #include <string>
10 #include <thread>
11 #include <vector>
12
13 extern "C" {
14 #include <libavformat/avformat.h>
15 }
16
17 #include <QApplication>
18
19 #include <movit/init.h>
20 #include <movit/util.h>
21
22 #include "clip_list.h"
23 #include "context.h"
24 #include "defs.h"
25 #include "mainwindow.h"
26 #include "ffmpeg_raii.h"
27 #include "httpd.h"
28 #include "player.h"
29 #include "post_to_main_thread.h"
30 #include "ref_counted_gl_sync.h"
31 #include "ui_mainwindow.h"
32
33 using namespace std;
34 using namespace std::chrono;
35
36 std::mutex RefCountedGLsync::fence_lock;
37
38 // TODO: Replace by some sort of GUI control, I guess.
39 int64_t current_pts = 0;
40
41 string filename_for_frame(unsigned stream_idx, int64_t pts)
42 {
43         char filename[256];
44         snprintf(filename, sizeof(filename), "frames/cam%d-pts%09ld.jpeg", stream_idx, pts);
45         return filename;
46 }
47
48 mutex frame_mu;
49 vector<int64_t> frames[MAX_STREAMS];
50 HTTPD *global_httpd;
51
52 int record_thread_func();
53
54 int main(int argc, char **argv)
55 {
56         avformat_network_init();
57         global_httpd = new HTTPD;
58         global_httpd->start(DEFAULT_HTTPD_PORT);
59
60         QCoreApplication::setAttribute(Qt::AA_ShareOpenGLContexts, true);
61
62         QSurfaceFormat fmt;
63         fmt.setDepthBufferSize(0);
64         fmt.setStencilBufferSize(0);
65         fmt.setProfile(QSurfaceFormat::CoreProfile);
66         fmt.setMajorVersion(4);
67         fmt.setMinorVersion(5);
68
69         // Turn off vsync, since Qt generally gives us at most frame rate
70         // (display frequency) / (number of QGLWidgets active).
71         fmt.setSwapInterval(0);
72
73         QSurfaceFormat::setDefaultFormat(fmt);
74
75         QGLFormat::setDefaultFormat(QGLFormat::fromSurfaceFormat(fmt));
76
77         QApplication app(argc, argv);
78         global_share_widget = new QGLWidget();
79         if (!global_share_widget->isValid()) {
80                 fprintf(stderr, "Failed to initialize OpenGL. Futatabi needs at least OpenGL 4.5 to function properly.\n");
81                 exit(1);
82         }
83
84         // Initialize Movit.
85         {
86                 QSurface *surface = create_surface();
87                 QOpenGLContext *context = create_context(surface);
88                 make_current(context, surface);
89                 CHECK(movit::init_movit(MOVIT_SHADER_DIR, movit::MOVIT_DEBUG_OFF));
90                 delete_context(context);
91                 // TODO: Delete the surface, too.
92         }
93
94         MainWindow mainWindow;
95         mainWindow.show();
96
97         thread(record_thread_func).detach();
98
99         return app.exec();
100 }
101
102 int record_thread_func()
103 {
104         auto format_ctx = avformat_open_input_unique("multiangle.mp4", nullptr, nullptr);
105         if (format_ctx == nullptr) {
106                 fprintf(stderr, "%s: Error opening file\n", "example.mp4");
107                 return 1;
108         }
109
110         int64_t last_pts = -1;
111
112         for ( ;; ) {
113                 AVPacket pkt;
114                 unique_ptr<AVPacket, decltype(av_packet_unref)*> pkt_cleanup(
115                         &pkt, av_packet_unref);
116                 av_init_packet(&pkt);
117                 pkt.data = nullptr;
118                 pkt.size = 0;
119                 if (av_read_frame(format_ctx.get(), &pkt) != 0) {
120                         break;
121                 }
122                 //fprintf(stderr, "Got a frame from camera %d, pts = %ld, size = %d\n",
123                 //      pkt.stream_index, pkt.pts, pkt.size);
124                 string filename = filename_for_frame(pkt.stream_index, pkt.pts);
125                 FILE *fp = fopen(filename.c_str(), "wb");
126                 if (fp == nullptr) {
127                         perror(filename.c_str());
128                         exit(1);
129                 }
130                 fwrite(pkt.data, pkt.size, 1, fp);
131                 fclose(fp);
132
133                 post_to_main_thread([pkt] {
134                         if (pkt.stream_index == 0) {
135                                 global_mainwindow->ui->input1_display->setFrame(pkt.stream_index, pkt.pts);
136                         } else if (pkt.stream_index == 1) {
137                                 global_mainwindow->ui->input2_display->setFrame(pkt.stream_index, pkt.pts);
138                         } else if (pkt.stream_index == 2) {
139                                 global_mainwindow->ui->input3_display->setFrame(pkt.stream_index, pkt.pts);
140                         } else if (pkt.stream_index == 3) {
141                                 global_mainwindow->ui->input4_display->setFrame(pkt.stream_index, pkt.pts);
142                         }
143                 });
144
145                 assert(pkt.stream_index < MAX_STREAMS);
146                 frames[pkt.stream_index].push_back(pkt.pts);
147
148                 // Hack. Assumes a given timebase.
149                 if (last_pts != -1) {
150                         this_thread::sleep_for(microseconds((pkt.pts - last_pts) * 1000000 / 12800));
151                 }
152                 last_pts = pkt.pts;
153                 current_pts = pkt.pts;
154         }
155
156         return 0;
157 }