]> git.sesse.net Git - nageru/blob - main.cpp
Convert Y'CbCr using Movit instead of in software with Qt. Saves a fair amount of...
[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 "clip_list.h"
20 #include "defs.h"
21 #include "mainwindow.h"
22 #include "ffmpeg_raii.h"
23 #include "player.h"
24 #include "post_to_main_thread.h"
25 #include "ui_mainwindow.h"
26
27 using namespace std;
28 using namespace std::chrono;
29
30 // TODO: Replace by some sort of GUI control, I guess.
31 int64_t current_pts = 0;
32
33 string filename_for_frame(unsigned stream_idx, int64_t pts)
34 {
35         char filename[256];
36         snprintf(filename, sizeof(filename), "frames/cam%d-pts%09ld.jpeg", stream_idx, pts);
37         return filename;
38 }
39
40 mutex frame_mu;
41 vector<int64_t> frames[MAX_STREAMS];
42 QGLWidget *global_share_widget;
43
44 int record_thread_func();
45
46 int main(int argc, char **argv)
47 {
48         av_register_all();
49         avformat_network_init();
50
51         QCoreApplication::setAttribute(Qt::AA_ShareOpenGLContexts, true);
52
53         QSurfaceFormat fmt;
54         fmt.setDepthBufferSize(0);
55         fmt.setStencilBufferSize(0);
56         fmt.setProfile(QSurfaceFormat::CoreProfile);
57         fmt.setMajorVersion(3);
58         fmt.setMinorVersion(1);
59
60         // Turn off vsync, since Qt generally gives us at most frame rate
61         // (display frequency) / (number of QGLWidgets active).
62         fmt.setSwapInterval(0);
63
64         QSurfaceFormat::setDefaultFormat(fmt);
65
66         QGLFormat::setDefaultFormat(QGLFormat::fromSurfaceFormat(fmt));
67
68         QApplication app(argc, argv);
69         global_share_widget = new QGLWidget();
70         if (!global_share_widget->isValid()) {
71                 fprintf(stderr, "Failed to initialize OpenGL. Futatabi needs at least OpenGL 3.1 to function properly.\n");
72                 exit(1);
73         }
74         MainWindow mainWindow;
75         mainWindow.show();
76
77         thread(record_thread_func).detach();
78
79         return app.exec();
80 }
81
82 int record_thread_func()
83 {
84         auto format_ctx = avformat_open_input_unique("multiangle.mp4", nullptr, nullptr);
85         if (format_ctx == nullptr) {
86                 fprintf(stderr, "%s: Error opening file\n", "example.mp4");
87                 return 1;
88         }
89
90         int64_t last_pts = -1;
91
92         for ( ;; ) {
93                 AVPacket pkt;
94                 unique_ptr<AVPacket, decltype(av_packet_unref)*> pkt_cleanup(
95                         &pkt, av_packet_unref);
96                 av_init_packet(&pkt);
97                 pkt.data = nullptr;
98                 pkt.size = 0;
99                 if (av_read_frame(format_ctx.get(), &pkt) != 0) {
100                         break;
101                 }
102                 //fprintf(stderr, "Got a frame from camera %d, pts = %ld, size = %d\n",
103                 //      pkt.stream_index, pkt.pts, pkt.size);
104                 string filename = filename_for_frame(pkt.stream_index, pkt.pts);
105                 FILE *fp = fopen(filename.c_str(), "wb");
106                 if (fp == nullptr) {
107                         perror(filename.c_str());
108                         exit(1);
109                 }
110                 fwrite(pkt.data, pkt.size, 1, fp);
111                 fclose(fp);
112
113                 post_to_main_thread([pkt] {
114                         if (pkt.stream_index == 0) {
115                                 global_mainwindow->ui->input1_display->setFrame(pkt.stream_index, pkt.pts);
116                         } else if (pkt.stream_index == 1) {
117                                 global_mainwindow->ui->input2_display->setFrame(pkt.stream_index, pkt.pts);
118                         } else if (pkt.stream_index == 2) {
119                                 global_mainwindow->ui->input3_display->setFrame(pkt.stream_index, pkt.pts);
120                         } else if (pkt.stream_index == 3) {
121                                 global_mainwindow->ui->input4_display->setFrame(pkt.stream_index, pkt.pts);
122                         }
123                 });
124
125                 assert(pkt.stream_index < MAX_STREAMS);
126                 frames[pkt.stream_index].push_back(pkt.pts);
127
128                 // Hack. Assumes a given timebase.
129                 if (last_pts != -1) {
130                         this_thread::sleep_for(microseconds((pkt.pts - last_pts) * 1000000 / 12800));
131                 }
132                 last_pts = pkt.pts;
133                 current_pts = pkt.pts;
134         }
135
136         return 0;
137 }