1 #ifndef _PBO_FRAME_ALLOCATOR
2 #define _PBO_FRAME_ALLOCATOR 1
14 #include <movit/effect.h>
15 #include <movit/ycbcr.h>
17 #include "bmusb/bmusb.h"
18 #include "mjpeg_encoder.h"
19 #include "shared/va_resource_pool.h"
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 {
28 // Note: You need to have an OpenGL context when calling
30 PBOFrameAllocator(bmusb::PixelFormat pixel_format,
32 GLuint width, GLuint height,
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;
44 // NOTE: Does not check the buffer types; they are just assumed to be compatible.
45 void reconfigure(bmusb::PixelFormat pixel_format,
47 GLuint width, GLuint height,
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);
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
62 bmusb::PixelFormat pixel_format;
64 // Used only for PixelFormat_8BitYCbCrPlanar.
65 movit::YCbCrFormat ycbcr_format;
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};
81 // These are the source of the “data_copy” member in Frame,
82 // used for MJPEG encoding. There are three possibilities:
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;
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);
108 MJPEGEncoder *mjpeg_encoder;
109 bmusb::PixelFormat pixel_format;
110 std::mutex freelist_mutex;
111 std::queue<Frame> freelist;
113 std::unique_ptr<Userdata[]> userdata;
115 // Used only for reconfigure(), to check whether we can do without.
117 size_t num_queued_frames;
118 GLuint width, height;
121 int generation = 0; // Under freelist_mutex.
123 struct LingeringGeneration {
124 std::unique_ptr<Userdata[]> userdata;
125 size_t num_frames_left;
127 std::map<int, LingeringGeneration> lingering_generations;
130 #endif // !defined(_PBO_FRAME_ALLOCATOR)