]> git.sesse.net Git - nageru/blob - main.cpp
Add a script for grabbing some example multiangle data off of YouTube.
[nageru] / main.cpp
1 #include <assert.h>
2 #include <stdio.h>
3 #include <stdint.h>
4
5 #include <chrono>
6 #include <memory>
7 #include <mutex>
8 #include <string>
9 #include <thread>
10 #include <vector>
11
12 extern "C" {
13 #include <libavformat/avformat.h>
14 }
15
16 #include <QApplication>
17
18 #include "mainwindow.h"
19 #include "ffmpeg_raii.h"
20 #include "post_to_main_thread.h"
21 #include "ui_mainwindow.h"
22
23 #define MAX_STREAMS 16
24
25 using namespace std;
26 using namespace std::chrono;
27
28 string filename_for_frame(unsigned stream_idx, int64_t pts)
29 {
30         char filename[256];
31         snprintf(filename, sizeof(filename), "frames/cam%d-pts%09ld.jpeg", stream_idx, pts);
32         return filename;
33 }
34
35 mutex frame_mu;
36 vector<int64_t> frames[MAX_STREAMS];
37
38 int thread_func();
39
40 int main(int argc, char **argv)
41 {
42         av_register_all();
43         avformat_network_init();
44
45         QApplication app(argc, argv);
46         MainWindow mainWindow;
47         mainWindow.show();
48
49         thread(thread_func).detach();
50
51         return app.exec();
52 }
53
54 int thread_func()
55 {
56         auto format_ctx = avformat_open_input_unique("multiangle.mp4", nullptr, nullptr);
57         if (format_ctx == nullptr) {
58                 fprintf(stderr, "%s: Error opening file\n", "example.mp4");
59                 return 1;
60         }
61
62         int64_t last_pts = -1;
63
64         for ( ;; ) {
65                 AVPacket pkt;
66                 unique_ptr<AVPacket, decltype(av_packet_unref)*> pkt_cleanup(
67                         &pkt, av_packet_unref);
68                 av_init_packet(&pkt);
69                 pkt.data = nullptr;
70                 pkt.size = 0;
71                 if (av_read_frame(format_ctx.get(), &pkt) != 0) {
72                         break;
73                 }
74                 fprintf(stderr, "Got a frame from camera %d, pts = %ld, size = %d\n",
75                         pkt.stream_index, pkt.pts, pkt.size);
76                 string filename = filename_for_frame(pkt.stream_index, pkt.pts);
77                 FILE *fp = fopen(filename.c_str(), "wb");
78                 if (fp == nullptr) {
79                         perror(filename.c_str());
80                         exit(1);
81                 }
82                 fwrite(pkt.data, pkt.size, 1, fp);
83                 fclose(fp);
84
85                 post_to_main_thread([pkt] {
86                         if (pkt.stream_index == 0) {
87                                 global_mainwindow->ui->input1_display->setFrame(pkt.stream_index, pkt.pts);
88                         } else if (pkt.stream_index == 1) {
89                                 global_mainwindow->ui->input2_display->setFrame(pkt.stream_index, pkt.pts);
90                         } else if (pkt.stream_index == 2) {
91                                 global_mainwindow->ui->input3_display->setFrame(pkt.stream_index, pkt.pts);
92                         }
93                 });
94
95                 assert(pkt.stream_index < MAX_STREAMS);
96                 frames[pkt.stream_index].push_back(pkt.pts);
97
98                 // Hack. Assumes a given timebase.
99                 if (last_pts != -1) {
100                         this_thread::sleep_for(microseconds((pkt.pts - last_pts) * 1000000 / 12800));
101                 }
102                 last_pts = pkt.pts;
103         }
104
105         return 0;
106 }