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