1 #include "pbo_frame_allocator.h"
3 #include <bmusb/bmusb.h>
4 #include <movit/util.h>
11 #include "mjpeg_encoder.h"
12 #include "v210_converter.h"
13 #include "va_display_with_cleanup.h"
19 void set_clamp_to_edge()
21 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
23 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
25 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
31 PBOFrameAllocator::PBOFrameAllocator(bmusb::PixelFormat pixel_format, size_t frame_size, GLuint width, GLuint height, unsigned card_index, MJPEGEncoder *mjpeg_encoder, size_t num_queued_frames, GLenum buffer, GLenum permissions, GLenum map_bits)
32 : card_index(card_index), mjpeg_encoder(mjpeg_encoder), pixel_format(pixel_format), buffer(buffer)
34 userdata.reset(new Userdata[num_queued_frames]);
35 for (size_t i = 0; i < num_queued_frames; ++i) {
36 init_frame(i, frame_size, width, height, permissions, map_bits);
38 glBindBuffer(buffer, 0);
40 glBindTexture(GL_TEXTURE_2D, 0);
44 void PBOFrameAllocator::init_frame(size_t frame_idx, size_t frame_size, GLuint width, GLuint height, GLenum permissions, GLenum map_bits)
47 glGenBuffers(1, &pbo);
49 glBindBuffer(buffer, pbo);
51 glBufferStorage(buffer, frame_size, nullptr, permissions | GL_MAP_PERSISTENT_BIT);
55 frame.data = (uint8_t *)glMapBufferRange(buffer, 0, frame_size, permissions | map_bits | GL_MAP_PERSISTENT_BIT);
56 frame.data2 = frame.data + frame_size / 2;
58 frame.size = frame_size;
59 Userdata *ud = &userdata[frame_idx];
62 ud->pixel_format = pixel_format;
63 ud->data_copy_malloc = new uint8_t[frame_size];
66 // For 8-bit non-planar Y'CbCr, we ask the driver to split Y' and Cb/Cr
67 // into separate textures. For 10-bit, the input format (v210)
68 // is complicated enough that we need to interpolate up to 4:4:4,
69 // which we do in a compute shader ourselves. For BGRA, the data
70 // is already 4:4:4:4.
71 frame.interleaved = (pixel_format == bmusb::PixelFormat_8BitYCbCr);
73 // Create textures. We don't allocate any data for the second field at this point
74 // (just create the texture state with the samplers), since our default assumed
75 // resolution is progressive.
76 switch (pixel_format) {
77 case bmusb::PixelFormat_8BitYCbCr:
78 glGenTextures(2, ud->tex_y);
80 glGenTextures(2, ud->tex_cbcr);
83 case bmusb::PixelFormat_10BitYCbCr:
84 glGenTextures(2, ud->tex_v210);
86 glGenTextures(2, ud->tex_444);
89 case bmusb::PixelFormat_8BitBGRA:
90 glGenTextures(2, ud->tex_rgba);
93 case bmusb::PixelFormat_8BitYCbCrPlanar:
94 glGenTextures(2, ud->tex_y);
96 glGenTextures(2, ud->tex_cb);
98 glGenTextures(2, ud->tex_cr);
105 ud->last_width[0] = width;
106 ud->last_height[0] = height;
107 ud->last_cbcr_width[0] = width / 2;
108 ud->last_cbcr_height[0] = height;
109 ud->last_v210_width[0] = 0;
111 ud->last_width[1] = 0;
112 ud->last_height[1] = 0;
113 ud->last_cbcr_width[1] = 0;
114 ud->last_cbcr_height[1] = 0;
115 ud->last_v210_width[1] = 0;
117 ud->last_interlaced = false;
118 ud->last_has_signal = false;
119 ud->last_is_connected = false;
120 for (unsigned field = 0; field < 2; ++field) {
121 switch (pixel_format) {
122 case bmusb::PixelFormat_10BitYCbCr: {
123 const size_t v210_width = v210Converter::get_minimum_v210_texture_width(width);
125 // Seemingly we need to set the minification filter even though
126 // shader image loads don't use them, or NVIDIA will just give us
128 glBindTexture(GL_TEXTURE_2D, ud->tex_v210[field]);
130 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
133 ud->last_v210_width[0] = v210_width;
134 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, v210_width, height, 0, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV, nullptr);
138 glBindTexture(GL_TEXTURE_2D, ud->tex_444[field]);
142 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, width, height, 0, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV, nullptr);
147 case bmusb::PixelFormat_8BitYCbCr:
148 glBindTexture(GL_TEXTURE_2D, ud->tex_y[field]);
152 glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, width, height, 0, GL_RED, GL_UNSIGNED_BYTE, nullptr);
156 glBindTexture(GL_TEXTURE_2D, ud->tex_cbcr[field]);
160 glTexImage2D(GL_TEXTURE_2D, 0, GL_RG8, width / 2, height, 0, GL_RG, GL_UNSIGNED_BYTE, nullptr);
164 case bmusb::PixelFormat_8BitBGRA:
165 glBindTexture(GL_TEXTURE_2D, ud->tex_rgba[field]);
169 glTexImage2D(GL_TEXTURE_2D, 0, GL_SRGB8_ALPHA8, width, height, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, nullptr);
173 case bmusb::PixelFormat_8BitYCbCrPlanar:
174 glBindTexture(GL_TEXTURE_2D, ud->tex_y[field]);
178 glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, width, height, 0, GL_RED, GL_UNSIGNED_BYTE, nullptr);
182 glBindTexture(GL_TEXTURE_2D, ud->tex_cb[field]);
186 glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, width / 2, height, 0, GL_RED, GL_UNSIGNED_BYTE, nullptr);
190 glBindTexture(GL_TEXTURE_2D, ud->tex_cr[field]);
194 glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, width / 2, height, 0, GL_RED, GL_UNSIGNED_BYTE, nullptr);
203 freelist.push(frame);
206 PBOFrameAllocator::~PBOFrameAllocator()
208 while (!freelist.empty()) {
209 Frame frame = freelist.front();
211 destroy_frame(&frame);
215 void PBOFrameAllocator::destroy_frame(Frame *frame)
217 Userdata *ud = (Userdata *)frame->userdata;
218 delete[] ud->data_copy_malloc;
220 GLuint pbo = ud->pbo;
221 glBindBuffer(buffer, pbo);
223 glUnmapBuffer(buffer);
225 glBindBuffer(buffer, 0);
227 glDeleteBuffers(1, &pbo);
229 switch (pixel_format) {
230 case bmusb::PixelFormat_10BitYCbCr:
231 glDeleteTextures(2, ud->tex_v210);
233 glDeleteTextures(2, ud->tex_444);
236 case bmusb::PixelFormat_8BitYCbCr:
237 glDeleteTextures(2, ud->tex_y);
239 glDeleteTextures(2, ud->tex_cbcr);
242 case bmusb::PixelFormat_8BitBGRA:
243 glDeleteTextures(2, ud->tex_rgba);
246 case bmusb::PixelFormat_8BitYCbCrPlanar:
247 glDeleteTextures(2, ud->tex_y);
249 glDeleteTextures(2, ud->tex_cb);
251 glDeleteTextures(2, ud->tex_cr);
258 //static int sumsum = 0;
260 bmusb::FrameAllocator::Frame PBOFrameAllocator::alloc_frame()
264 lock_guard<mutex> lock(freelist_mutex); // Meh.
265 if (freelist.empty()) {
266 printf("Frame overrun (no more spare PBO frames), dropping frame!\n");
268 //fprintf(stderr, "freelist has %d allocated\n", ++sumsum);
269 vf = freelist.front();
270 freelist.pop(); // Meh.
275 if (mjpeg_encoder != nullptr &&
276 mjpeg_encoder->should_encode_mjpeg_for_card(card_index) &&
277 vf.userdata != nullptr) {
278 Userdata *ud = (Userdata *)vf.userdata;
279 vf.data_copy = ud->data_copy_malloc;
280 ud->data_copy_current_src = Userdata::FROM_MALLOC;
282 vf.data_copy = nullptr;
288 bmusb::FrameAllocator::Frame PBOFrameAllocator::create_frame(size_t width, size_t height, size_t stride)
293 lock_guard<mutex> lock(freelist_mutex);
294 if (freelist.empty()) {
295 printf("Frame overrun (no more spare PBO frames), dropping frame!\n");
300 vf = freelist.front();
307 Userdata *userdata = (Userdata *)vf.userdata;
309 if (mjpeg_encoder != nullptr &&
310 mjpeg_encoder->should_encode_mjpeg_for_card(card_index)) {
311 if (mjpeg_encoder->using_vaapi()) {
312 VADisplay va_dpy = mjpeg_encoder->va_dpy->va_dpy;
313 MJPEGEncoder::VAResources resources = mjpeg_encoder->get_va_resources(width, height);
314 MJPEGEncoder::ReleaseVAResources release(mjpeg_encoder, resources);
316 if (resources.image.pitches[0] == stride) {
317 userdata->va_resources = move(resources);
318 userdata->va_resources_release = move(release);
320 VAStatus va_status = vaMapBuffer(va_dpy, resources.image.buf, (void **)&vf.data_copy);
321 CHECK_VASTATUS(va_status, "vaMapBuffer");
322 vf.data_copy += resources.image.offsets[0];
323 userdata->data_copy_current_src = Userdata::FROM_VA_API;
325 printf("WARNING: Could not copy directly into VA-API MJPEG buffer for %zu x %zu, since producer and consumer disagreed on stride (%zu != %d).\n", width, height, stride, resources.image.pitches[0]);
326 vf.data_copy = userdata->data_copy_malloc;
327 userdata->data_copy_current_src = Userdata::FROM_MALLOC;
330 vf.data_copy = userdata->data_copy_malloc;
331 userdata->data_copy_current_src = Userdata::FROM_MALLOC;
334 vf.data_copy = nullptr;
340 void PBOFrameAllocator::release_frame(Frame frame)
342 if (frame.overflow > 0) {
343 printf("%d bytes overflow after last (PBO) frame\n", int(frame.overflow));
347 // Poison the page. (Note that this might be bogus if you don't have an OpenGL context.)
348 memset(frame.data, 0, frame.size);
349 Userdata *userdata = (Userdata *)frame.userdata;
350 for (unsigned field = 0; field < 2; ++field) {
351 glBindTexture(GL_TEXTURE_2D, userdata->tex_y[field]);
353 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
355 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
357 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
359 glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, userdata->last_width[field], userdata->last_height[field], 0, GL_RED, GL_UNSIGNED_BYTE, nullptr);
362 glBindTexture(GL_TEXTURE_2D, userdata->tex_cbcr[field]);
364 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
366 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
368 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
370 glTexImage2D(GL_TEXTURE_2D, 0, GL_RG8, userdata->last_width[field] / 2, userdata->last_height[field], 0, GL_RED, GL_UNSIGNED_BYTE, nullptr);
376 // In case we never got to upload the frame to MJPEGEncoder.
377 Userdata *userdata = (Userdata *)frame.userdata;
378 MJPEGEncoder::VAResources resources __attribute__((unused)) = move(userdata->va_resources);
379 MJPEGEncoder::ReleaseVAResources release = move(userdata->va_resources_release);
381 if (frame.data_copy != nullptr && userdata->data_copy_current_src == Userdata::FROM_VA_API) {
382 VADisplay va_dpy = mjpeg_encoder->va_dpy->va_dpy;
383 VAStatus va_status = vaUnmapBuffer(va_dpy, resources.image.buf);
384 CHECK_VASTATUS(va_status, "vaUnmapBuffer");
386 frame.data_copy = nullptr;
390 lock_guard<mutex> lock(freelist_mutex);
391 freelist.push(frame);