]> git.sesse.net Git - nageru/blob - nageru/pbo_frame_allocator.h
Fix a dangling reference (found by GCC 14).
[nageru] / nageru / pbo_frame_allocator.h
1 #ifndef _PBO_FRAME_ALLOCATOR 
2 #define _PBO_FRAME_ALLOCATOR 1
3
4 #include <epoxy/gl.h>
5 #include <stdbool.h>
6 #include <stddef.h>
7 #include <stdint.h>
8 #include <map>
9 #include <memory>
10 #include <mutex>
11 #include <string>
12 #include <queue>
13
14 #include <movit/effect.h>
15 #include <movit/ycbcr.h>
16
17 #include "bmusb/bmusb.h"
18 #include "mjpeg_encoder.h"
19 #include "shared/va_resource_pool.h"
20
21 class MJPEGEncoder;
22
23 // An allocator that allocates straight into OpenGL pinned memory.
24 // Meant for video frames only. We use a queue rather than a stack,
25 // since we want to maximize pipelineability.
26 class PBOFrameAllocator : public bmusb::FrameAllocator {
27 public:
28         // Note: You need to have an OpenGL context when calling
29         // the constructor.
30         PBOFrameAllocator(bmusb::PixelFormat pixel_format,
31                           size_t frame_size,
32                           GLuint width, GLuint height,
33                           unsigned card_index,
34                           MJPEGEncoder *mjpeg_encoder = nullptr,
35                           size_t num_queued_frames = 16,
36                           GLenum buffer = GL_PIXEL_UNPACK_BUFFER_ARB,
37                           GLenum permissions = GL_MAP_WRITE_BIT,
38                           GLenum map_bits = GL_MAP_FLUSH_EXPLICIT_BIT);
39         ~PBOFrameAllocator() override;
40         Frame alloc_frame() override;
41         Frame create_frame(size_t width, size_t height, size_t stride) override;
42         void release_frame(Frame frame) override;
43
44         // NOTE: Does not check the buffer types; they are just assumed to be compatible.
45         void reconfigure(bmusb::PixelFormat pixel_format,
46                          size_t frame_size,
47                          GLuint width, GLuint height,
48                          unsigned card_index,
49                          MJPEGEncoder *mjpeg_encoder = nullptr,
50                          size_t num_queued_frames = 16,
51                          GLenum buffer = GL_PIXEL_UNPACK_BUFFER_ARB,
52                          GLenum permissions = GL_MAP_WRITE_BIT,
53                          GLenum map_bits = GL_MAP_FLUSH_EXPLICIT_BIT);
54
55         struct Userdata {
56                 GLuint pbo;
57
58                 // NOTE: These frames typically go into LiveInputWrapper, which is
59                 // configured to accept one type of frame only. In other words,
60                 // the existence of a format field doesn't mean you can set it
61                 // freely at runtime.
62                 bmusb::PixelFormat pixel_format;
63
64                 // Used only for PixelFormat_8BitYCbCrPlanar.
65                 movit::YCbCrFormat ycbcr_format;
66
67                 // The second set is only used for the second field of interlaced inputs.
68                 GLuint tex_y[2], tex_cbcr[2];  // For PixelFormat_8BitYCbCr.
69                 GLuint tex_cb[2], tex_cr[2];  // For PixelFormat_8BitYCbCrPlanar (which also uses tex_y).
70                 GLuint tex_v210[2], tex_444[2];  // For PixelFormat_10BitYCbCr.
71                 GLuint tex_rgba[2];  // For PixelFormat_8BitBGRA.
72                 GLuint last_width[2], last_height[2];
73                 GLuint last_cbcr_width[2], last_cbcr_height[2];
74                 GLuint last_v210_width[2];  // PixelFormat_10BitYCbCr.
75                 bool last_interlaced, last_has_signal, last_is_connected;
76                 unsigned last_frame_rate_nom, last_frame_rate_den;
77                 bool has_last_subtitle = false;
78                 std::string last_subtitle;
79                 movit::RGBTriplet white_balance{1.0f, 1.0f, 1.0f};
80
81                 // These are the source of the “data_copy” member in Frame,
82                 // used for MJPEG encoding. There are three possibilities:
83                 //
84                 //  - MJPEG encoding is not active (at all, or for this specific
85                 //    card). Then data_copy is nullptr, and what's in here
86                 //    does not matter at all.
87                 //  - We can encode directly into VA-API buffers (ie., VA-API
88                 //    is active, and nothing strange happened wrt. strides);
89                 //    then va_resources, va_resources_release and va_image
90                 //    are fetched from MJPEGEncoder at create_frame() and released
91                 //    back when the frame is uploaded (or would have been).
92                 //    In this case, data_copy points into the mapped VAImage.
93                 //  - If not, data_copy points to data_copy_malloc, and is copied
94                 //    from there into VA-API buffers (by MJPEGEncoder) if needed.
95                 enum { FROM_MALLOC, FROM_VA_API } data_copy_current_src;
96                 uint8_t *data_copy_malloc;
97                 VAResourcePool::VAResources va_resources;
98                 ReleaseVAResources va_resources_release;
99
100                 int generation;
101         };
102
103 private:
104         void init_frame(size_t frame_idx, size_t frame_size, GLuint width, GLuint height, GLenum permissions, GLenum map_bits, int generation);
105         void destroy_frame(Frame *frame);
106
107         unsigned card_index;
108         MJPEGEncoder *mjpeg_encoder;
109         bmusb::PixelFormat pixel_format;
110         std::mutex freelist_mutex;
111         std::queue<Frame> freelist;
112         GLenum buffer;
113         std::unique_ptr<Userdata[]> userdata;
114
115         // Used only for reconfigure(), to check whether we can do without.
116         size_t frame_size;
117         size_t num_queued_frames;
118         GLuint width, height;
119         GLenum permissions;
120         GLenum map_bits;
121         int generation = 0;  // Under freelist_mutex.
122
123         struct LingeringGeneration {
124                 std::unique_ptr<Userdata[]> userdata;
125                 size_t num_frames_left;
126         };
127         std::map<int, LingeringGeneration> lingering_generations;
128 };
129
130 #endif  // !defined(_PBO_FRAME_ALLOCATOR)