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