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