]> git.sesse.net Git - nageru/blob - nageru/pbo_frame_allocator.h
Heed the Exif white point when playing back (MJPEG) video.
[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         struct Userdata {
40                 GLuint pbo;
41
42                 // NOTE: These frames typically go into LiveInputWrapper, which is
43                 // configured to accept one type of frame only. In other words,
44                 // the existence of a format field doesn't mean you can set it
45                 // freely at runtime.
46                 bmusb::PixelFormat pixel_format;
47
48                 // Used only for PixelFormat_8BitYCbCrPlanar.
49                 movit::YCbCrFormat ycbcr_format;
50
51                 // The second set is only used for the second field of interlaced inputs.
52                 GLuint tex_y[2], tex_cbcr[2];  // For PixelFormat_8BitYCbCr.
53                 GLuint tex_cb[2], tex_cr[2];  // For PixelFormat_8BitYCbCrPlanar (which also uses tex_y).
54                 GLuint tex_v210[2], tex_444[2];  // For PixelFormat_10BitYCbCr.
55                 GLuint tex_rgba[2];  // For PixelFormat_8BitBGRA.
56                 GLuint last_width[2], last_height[2];
57                 GLuint last_cbcr_width[2], last_cbcr_height[2];
58                 GLuint last_v210_width[2];  // PixelFormat_10BitYCbCr.
59                 bool last_interlaced, last_has_signal, last_is_connected;
60                 unsigned last_frame_rate_nom, last_frame_rate_den;
61                 bool has_last_subtitle = false;
62                 std::string last_subtitle;
63                 movit::RGBTriplet white_balance{1.0f, 1.0f, 1.0f};
64
65                 // These are the source of the “data_copy” member in Frame,
66                 // used for MJPEG encoding. There are three possibilities:
67                 //
68                 //  - MJPEG encoding is not active (at all, or for this specific
69                 //    card). Then data_copy is nullptr, and what's in here
70                 //    does not matter at all.
71                 //  - We can encode directly into VA-API buffers (ie., VA-API
72                 //    is active, and nothing strange happened wrt. strides);
73                 //    then va_resources, va_resources_release and va_image
74                 //    are fetched from MJPEGEncoder at create_frame() and released
75                 //    back when the frame is uploaded (or would have been).
76                 //    In this case, data_copy points into the mapped VAImage.
77                 //  - If not, data_copy points to data_copy_malloc, and is copied
78                 //    from there into VA-API buffers (by MJPEGEncoder) if needed.
79                 enum { FROM_MALLOC, FROM_VA_API } data_copy_current_src;
80                 uint8_t *data_copy_malloc;
81                 MJPEGEncoder::VAResources va_resources;
82                 MJPEGEncoder::ReleaseVAResources va_resources_release;
83         };
84
85 private:
86         void init_frame(size_t frame_idx, size_t frame_size, GLuint width, GLuint height, GLenum permissions, GLenum map_bits);
87         void destroy_frame(Frame *frame);
88
89         unsigned card_index;
90         MJPEGEncoder *mjpeg_encoder;
91         bmusb::PixelFormat pixel_format;
92         std::mutex freelist_mutex;
93         std::queue<Frame> freelist;
94         GLenum buffer;
95         std::unique_ptr<Userdata[]> userdata;
96 };
97
98 #endif  // !defined(_PBO_FRAME_ALLOCATOR)