1 #ifndef _PBO_FRAME_ALLOCATOR
2 #define _PBO_FRAME_ALLOCATOR 1
11 #include <movit/ycbcr.h>
13 #include "bmusb/bmusb.h"
14 #include "mjpeg_encoder.h"
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 {
23 // Note: You need to have an OpenGL context when calling
25 PBOFrameAllocator(bmusb::PixelFormat pixel_format,
27 GLuint width, GLuint height,
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;
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
46 bmusb::PixelFormat pixel_format;
48 // Used only for PixelFormat_8BitYCbCrPlanar.
49 movit::YCbCrFormat ycbcr_format;
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;
64 // These are the source of the “data_copy” member in Frame,
65 // used for MJPEG encoding. There are three possibilities:
67 // - MJPEG encoding is not active (at all, or for this specific
68 // card). Then data_copy is nullptr, and what's in here
69 // does not matter at all.
70 // - We can encode directly into VA-API buffers (ie., VA-API
71 // is active, and nothing strange happened wrt. strides);
72 // then va_resources, va_resources_release and va_image
73 // are fetched from MJPEGEncoder at create_frame() and released
74 // back when the frame is uploaded (or would have been).
75 // In this case, data_copy points into the mapped VAImage.
76 // - If not, data_copy points to data_copy_malloc, and is copied
77 // from there into VA-API buffers (by MJPEGEncoder) if needed.
78 enum { FROM_MALLOC, FROM_VA_API } data_copy_current_src;
79 uint8_t *data_copy_malloc;
80 MJPEGEncoder::VAResources va_resources;
81 MJPEGEncoder::ReleaseVAResources va_resources_release;
85 void init_frame(size_t frame_idx, size_t frame_size, GLuint width, GLuint height, GLenum permissions, GLenum map_bits);
86 void destroy_frame(Frame *frame);
89 MJPEGEncoder *mjpeg_encoder;
90 bmusb::PixelFormat pixel_format;
91 std::mutex freelist_mutex;
92 std::queue<Frame> freelist;
94 std::unique_ptr<Userdata[]> userdata;
97 #endif // !defined(_PBO_FRAME_ALLOCATOR)