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;
39 // NOTE: Does not check the buffer types; they are just assumed to be compatible.
40 void reconfigure(bmusb::PixelFormat pixel_format,
42 GLuint width, GLuint height,
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);
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
57 bmusb::PixelFormat pixel_format;
59 // Used only for PixelFormat_8BitYCbCrPlanar.
60 movit::YCbCrFormat ycbcr_format;
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};
76 // These are the source of the “data_copy” member in Frame,
77 // used for MJPEG encoding. There are three possibilities:
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 VAResourcePool::VAResources va_resources;
93 ReleaseVAResources va_resources_release;
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);
103 MJPEGEncoder *mjpeg_encoder;
104 bmusb::PixelFormat pixel_format;
105 std::mutex freelist_mutex;
106 std::queue<Frame> freelist;
108 std::unique_ptr<Userdata[]> userdata;
110 // Used only for reconfigure(), to check whether we can do without.
112 size_t num_queued_frames;
113 GLuint width, height;
116 int generation = 0; // Under freelist_mutex.
118 struct LingeringGeneration {
119 std::unique_ptr<Userdata[]> userdata;
120 size_t num_frames_left;
122 std::map<int, LingeringGeneration> lingering_generations;
125 #endif // !defined(_PBO_FRAME_ALLOCATOR)