]> git.sesse.net Git - nageru/blob - jpeg_frame_view.h
Make for somewhat cleaner shutdown.
[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/ycbcr_input.h>
12
13 #include <memory>
14 #include <thread>
15
16 #include "jpeg_frame.h"
17
18 struct JPEGID {
19         unsigned stream_idx;
20         int64_t pts;
21         bool interpolated;
22 };
23 enum CacheMissBehavior {
24         DECODE_IF_NOT_IN_CACHE,
25         RETURN_NULLPTR_IF_NOT_IN_CACHE
26 };
27
28 std::string filename_for_frame(unsigned stream_idx, int64_t pts);
29 std::shared_ptr<Frame> decode_jpeg(const std::string &filename);
30 std::shared_ptr<Frame> decode_jpeg_with_cache(JPEGID id, CacheMissBehavior cache_miss_behavior, bool *did_decode);
31
32 class JPEGFrameView : public QGLWidget {
33         Q_OBJECT
34
35 public:
36         JPEGFrameView(QWidget *parent);
37
38         void setFrame(unsigned stream_idx, int64_t pts, bool interpolated);
39         static void insert_interpolated_frame(unsigned stream_idx, int64_t pts, std::shared_ptr<Frame> frame);
40
41         void mousePressEvent(QMouseEvent *event) override;
42
43         unsigned get_stream_idx() const { return current_stream_idx; }
44
45         void setDecodedFrame(std::shared_ptr<Frame> frame);
46         void set_overlay(const std::string &text);  // Blank for none.
47
48         static void shutdown();
49
50 signals:
51         void clicked();
52
53 protected:
54         void initializeGL() override;
55         void resizeGL(int width, int height) override;
56         void paintGL() override;
57
58 private:
59         // The stream index of the latest frame we displayed.
60         unsigned current_stream_idx = 0;
61
62         std::unique_ptr<movit::EffectChain> planar_chain;
63         std::shared_ptr<Frame> current_frame;  // So that we hold on to the pixels.
64         movit::YCbCrInput *ycbcr_planar_input;
65         movit::YCbCrFormat ycbcr_format;
66
67         std::unique_ptr<movit::EffectChain> semiplanar_chain;
68         movit::YCbCrInput *ycbcr_semiplanar_input;
69
70         static constexpr int overlay_base_width = 16, overlay_base_height = 16;
71         int overlay_width = overlay_base_width, overlay_height = overlay_base_height;
72         std::unique_ptr<QImage> overlay_image;  // If nullptr, no overlay.
73         std::unique_ptr<movit::EffectChain> overlay_chain;  // Just to get the overlay on screen in the easiest way possible.
74         movit::FlatInput *overlay_input;
75         bool overlay_input_needs_refresh = false;
76
77         int gl_width, gl_height;
78
79         static std::thread jpeg_decoder_thread;
80 };
81
82 #endif  // !defined(_JPEG_FRAME_VIEW_H)