]> git.sesse.net Git - pkanalytics/blob - video_widget.h
4361d3098c8f3fd2d6414d69ebd868f32a3f2ed6
[pkanalytics] / video_widget.h
1 #ifndef _VIDEO_WIDGET_H
2 #define _VIDEO_WIDGET_H 1
3
4 #include <QWidget>
5 #include <QOpenGLWindow>
6 #include <string>
7 #include <atomic>
8 #include <thread>
9 #include <chrono>
10 #include <deque>
11
12 extern "C" {
13 #include <libavutil/pixfmt.h>
14 #include <libavutil/rational.h>
15 }
16
17 #include "ffmpeg_raii.h"
18 #include "quittable_sleeper.h"
19
20 // Because QVideoWidget sucks, sadly. (Don't use GStreamer, kids.)
21
22 class VideoWindow;
23
24 class VideoWidget : public QWidget {
25         Q_OBJECT
26
27 public:
28         VideoWidget(QWidget *parent);
29         ~VideoWidget() { stop(); }
30
31         bool open(const std::string &filename);  // False on error.
32         void play();
33         uint64_t get_position() const { return last_position; }  // In milliseconds.
34         void pause();
35         void stop();
36         void seek(int64_t relative_seek_ms);  // Relative seek.
37         void seek_frames(int64_t relative_frames);  // Relative seek.
38         void seek_absolute(int64_t position_ms);  // Absolute seek.
39
40         void wheelEvent(QWheelEvent *event) override;
41
42         // For dragging, and for back/forward button presses.
43         void mousePressEvent(QMouseEvent *e);
44         void mouseReleaseEvent(QMouseEvent *e);
45         void mouseMoveEvent(QMouseEvent *e);
46
47         // Public due to shared_ptr.
48         struct Frame {
49                 unsigned width, height;
50                 unsigned chroma_width, chroma_height;
51                 VideoWidget *owner;  // For the freelist.
52                 GLuint pbo;
53                 uint8_t *data;  // Persistently mapped into the PBO. Y, followed by Cb, followed by Cr.
54                 size_t need_flush_len;  // 0 = no flush needed.
55         };
56
57 signals:
58         void position_changed(uint64_t pos);
59         void mouse_back_clicked();
60         void mouse_forward_clicked();
61
62 private:
63         std::shared_ptr<Frame> alloc_frame(unsigned width, unsigned height, unsigned chroma_width, unsigned chroma_height);
64         static void free_frame(Frame *frame);
65
66         std::mutex current_frame_mu;
67         std::shared_ptr<Frame> current_frame;  // Protected by current_frame_mu.
68         std::mutex freelist_mu;
69         std::deque<Frame *> frame_freelist;  // Protected by freelist_mu.
70         std::deque<AVFrameWithDeleter> queued_frames;  // Frames decoded but not displayed. Only used when frame-stepping backwards.
71
72         double zoom_matrix[9] = {  // Column-major, to match with OpenGL.
73                 1.0, 0.0, 0.0,
74                 0.0, 1.0, 0.0,
75                 0.0, 0.0, 1.0,
76         };
77
78         int64_t pts_origin;
79         int64_t last_pts;
80         std::atomic<uint64_t> last_position{0};  // TODO: sort of redundant wrt. last_pts (but this one can be read from the other thread)
81         enum RunState { NOT_RUNNING, STARTING, RUNNING, VIDEO_FILE_ERROR };  // NOT_RUNNING and VIDEO_FILE_ERROR both imply that the thread isn't running and can freely be restarted.
82         std::atomic<RunState> running{NOT_RUNNING};
83         bool paused = false;
84         std::chrono::steady_clock::time_point start, next_frame_start, last_frame;
85
86         SwsContextWithDeleter sws_ctx;
87         int sws_last_width = -1, sws_last_height = -1, sws_last_src_format = -1;
88         AVPixelFormat sws_dst_format = AVPixelFormat(-1);  // In practice, always initialized.
89         AVRational video_timebase, audio_timebase;
90
91         QuittableSleeper producer_thread_should_quit;
92         std::thread producer_thread;
93
94         std::mutex queue_mu;
95         struct QueuedCommand {
96                 enum Command { PAUSE, RESUME, SEEK, SEEK_ABSOLUTE } command;
97                 int64_t relative_seek_ms;  // For SEEK.
98                 int64_t relative_seek_frames;  // For SEEK.
99                 int64_t seek_ms;  // For SEEK_ABSOLUTE.
100         };
101         std::vector<QueuedCommand> command_queue;  // Protected by <queue_mu>.
102
103         std::string pathname;
104
105         bool dragging = false;
106         float last_drag_x, last_drag_y;
107
108         VideoWindow *video_window;
109
110         void producer_thread_func();
111         bool play_video(const std::string &pathname);
112         void internal_rewind();
113         AVFrameWithDeleter decode_frame(AVFormatContext *format_ctx, AVCodecContext *video_codec_ctx,
114                 const std::string &pathname, int video_stream_index,
115                 bool *error);
116         std::shared_ptr<Frame> make_video_frame(const AVFrame *frame);
117         bool process_queued_commands(AVFormatContext *format_ctx, AVCodecContext *video_codec_ctx, int video_stream_index, bool *seeked);
118         void store_pts(int64_t pts);
119
120         void fixup_zoom_matrix();
121 };
122
123 class VideoWindow : public QOpenGLWindow {  // Private implementation of VideoWidget, but moc does not support nested classes.
124         Q_OBJECT;
125
126 public:
127         VideoWindow(VideoWidget *video) : video(video) {}
128         void initializeGL() override;
129         void resizeGL(int w, int h) override;
130         void paintGL() override;
131         void set_current_frame(std::shared_ptr<VideoWidget::Frame> new_frame);  // Takes ownership. Thread-safe.
132         void set_cbcr_offset(float x, float y)  // FIXME: Thread-safe?
133         {
134                 cbcr_offset[0] = x;
135                 cbcr_offset[1] = y;
136         }
137         void set_zoom_matrix(double new_zoom_matrix[9]) {
138                 memcpy(zoom_matrix, new_zoom_matrix, sizeof(zoom_matrix));
139                 update();
140         }
141
142         void wheelEvent(QWheelEvent *e) override { emit mouse_wheel(e); }
143         void mousePressEvent(QMouseEvent *e) override { emit mouse_pressed(e); }
144         void mouseReleaseEvent(QMouseEvent *e) override { emit mouse_released(e); }
145         void mouseMoveEvent(QMouseEvent *e) override { emit mouse_moved(e); }
146
147 signals:
148         void mouse_wheel(QWheelEvent *e);
149         void mouse_pressed(QMouseEvent *e);
150         void mouse_released(QMouseEvent *e);
151         void mouse_moved(QMouseEvent *e);
152
153 private:
154         VideoWidget *video;
155         GLuint ycbcr_vertex_shader, ycbcr_fragment_shader, ycbcr_program;
156         GLuint bilinear_sampler;
157         GLuint tex[3];
158         GLuint last_width = 0, last_height = 0;
159         GLuint last_chroma_width = 0, last_chroma_height = 0;
160         double display_aspect = 1.0;
161         GLfloat cbcr_offset[2];
162
163         double zoom_matrix[9] = {  // Column-major, to match with OpenGL.
164                 1.0, 0.0, 0.0,
165                 0.0, 1.0, 0.0,
166                 0.0, 0.0, 1.0,
167         };
168
169         std::mutex current_frame_mu;
170         std::shared_ptr<VideoWidget::Frame> current_frame;  // Protected by current_frame_mu.
171 };
172
173 #endif  // !defined(_VIDEO_WIDGET_H)