]> git.sesse.net Git - nageru/blob - jpeg_frame_view.h
Assorted clang-format fixes (not complete).
[nageru] / jpeg_frame_view.h
1 #ifndef _JPEG_FRAME_VIEW_H
2 #define _JPEG_FRAME_VIEW_H 1
3
4 #include "jpeg_frame.h"
5 #include "ycbcr_converter.h"
6
7 #include <QGLWidget>
8 #include <epoxy/gl.h>
9 #include <memory>
10 #include <movit/effect_chain.h>
11 #include <movit/flat_input.h>
12 #include <movit/mix_effect.h>
13 #include <movit/ycbcr_input.h>
14 #include <stdint.h>
15 #include <thread>
16
17 struct JPEGID {
18         unsigned stream_idx;
19         int64_t pts;
20         bool interpolated;
21 };
22 enum CacheMissBehavior {
23         DECODE_IF_NOT_IN_CACHE,
24         RETURN_NULLPTR_IF_NOT_IN_CACHE
25 };
26
27 // This is, well, a hack. We hope for no collisions.
28 inline JPEGID create_jpegid_for_interpolated_fade(unsigned stream_idx, int64_t pts, unsigned secondary_stream_idx, int64_t secondary_pts)
29 {
30         JPEGID id;
31         id.stream_idx = (stream_idx << 8) | secondary_stream_idx;
32
33         uint64_t rot = secondary_stream_idx;
34         rot = (rot << 32) | (rot >> 32);
35         id.pts = pts ^ int64_t(rot);
36         id.interpolated = true;
37         return id;
38 }
39
40 std::string filename_for_frame(unsigned stream_idx, int64_t pts);
41 std::shared_ptr<Frame> decode_jpeg(const std::string &filename);
42 std::shared_ptr<Frame> decode_jpeg_with_cache(JPEGID id, CacheMissBehavior cache_miss_behavior, bool *did_decode);
43
44 class JPEGFrameView : public QGLWidget {
45         Q_OBJECT
46
47 public:
48         JPEGFrameView(QWidget *parent);
49
50         void setFrame(unsigned stream_idx, int64_t pts, bool interpolated, int secondary_stream_idx = -1, int64_t secondary_pts = -1, float fade_alpha = 0.0f);
51         static void insert_interpolated_frame(JPEGID id, std::shared_ptr<Frame> frame);
52
53         void mousePressEvent(QMouseEvent *event) override;
54
55         unsigned get_stream_idx() const { return current_stream_idx; }
56
57         void setDecodedFrame(std::shared_ptr<Frame> frame, std::shared_ptr<Frame> secondary_frame, float fade_alpha);
58         void set_overlay(const std::string &text);  // Blank for none.
59
60         static void shutdown();
61
62 signals:
63         void clicked();
64
65 protected:
66         void initializeGL() override;
67         void resizeGL(int width, int height) override;
68         void paintGL() override;
69
70 private:
71         // The stream index of the latest frame we displayed.
72         unsigned current_stream_idx = 0;
73
74         std::unique_ptr<YCbCrConverter> ycbcr_converter;
75         movit::EffectChain *current_chain = nullptr;  // Owned by ycbcr_converter.
76
77         std::shared_ptr<Frame> current_frame;  // So that we hold on to the pixels.
78         std::shared_ptr<Frame> current_secondary_frame;  // Same.
79
80         static constexpr int overlay_base_width = 16, overlay_base_height = 16;
81         int overlay_width = overlay_base_width, overlay_height = overlay_base_height;
82         std::unique_ptr<QImage> overlay_image;  // If nullptr, no overlay.
83         std::unique_ptr<movit::EffectChain> overlay_chain;  // Just to get the overlay on screen in the easiest way possible.
84         movit::FlatInput *overlay_input;
85         bool overlay_input_needs_refresh = false;
86
87         int gl_width, gl_height;
88
89         static std::thread jpeg_decoder_thread;
90 };
91
92 #endif  // !defined(_JPEG_FRAME_VIEW_H)