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),
33 mjpeg_encoder(mjpeg_encoder),
34 pixel_format(pixel_format),
36 frame_size(frame_size),
37 num_queued_frames(num_queued_frames),
40 permissions(permissions),
43 userdata.reset(new Userdata[num_queued_frames]);
44 for (size_t i = 0; i < num_queued_frames; ++i) {
45 init_frame(i, frame_size, width, height, permissions, map_bits, generation);
47 glBindBuffer(buffer, 0);
49 glBindTexture(GL_TEXTURE_2D, 0);
53 void PBOFrameAllocator::init_frame(size_t frame_idx, size_t frame_size, GLuint width, GLuint height, GLenum permissions, GLenum map_bits, int generation)
56 glGenBuffers(1, &pbo);
58 glBindBuffer(buffer, pbo);
60 glBufferStorage(buffer, frame_size, nullptr, permissions | GL_MAP_PERSISTENT_BIT);
64 frame.data = (uint8_t *)glMapBufferRange(buffer, 0, frame_size, permissions | map_bits | GL_MAP_PERSISTENT_BIT);
65 frame.data2 = frame.data + frame_size / 2;
67 frame.size = frame_size;
68 Userdata *ud = &userdata[frame_idx];
70 ud->generation = generation;
72 ud->pixel_format = pixel_format;
73 ud->data_copy_malloc = new uint8_t[frame_size];
76 // For 8-bit non-planar Y'CbCr, we ask the driver to split Y' and Cb/Cr
77 // into separate textures. For 10-bit, the input format (v210)
78 // is complicated enough that we need to interpolate up to 4:4:4,
79 // which we do in a compute shader ourselves. For BGRA, the data
80 // is already 4:4:4:4.
81 frame.interleaved = (pixel_format == bmusb::PixelFormat_8BitYCbCr);
83 // Create textures. We don't allocate any data for the second field at this point
84 // (just create the texture state with the samplers), since our default assumed
85 // resolution is progressive.
86 switch (pixel_format) {
87 case bmusb::PixelFormat_8BitYCbCr:
88 glGenTextures(2, ud->tex_y);
90 glGenTextures(2, ud->tex_cbcr);
93 case bmusb::PixelFormat_10BitYCbCr:
94 glGenTextures(2, ud->tex_v210);
96 glGenTextures(2, ud->tex_444);
99 case bmusb::PixelFormat_8BitBGRA:
100 glGenTextures(2, ud->tex_rgba);
103 case bmusb::PixelFormat_8BitYCbCrPlanar:
104 glGenTextures(2, ud->tex_y);
106 glGenTextures(2, ud->tex_cb);
108 glGenTextures(2, ud->tex_cr);
115 ud->last_width[0] = width;
116 ud->last_height[0] = height;
117 ud->last_cbcr_width[0] = width / 2;
118 ud->last_cbcr_height[0] = height;
119 ud->last_v210_width[0] = 0;
121 ud->last_width[1] = 0;
122 ud->last_height[1] = 0;
123 ud->last_cbcr_width[1] = 0;
124 ud->last_cbcr_height[1] = 0;
125 ud->last_v210_width[1] = 0;
127 ud->last_interlaced = false;
128 ud->last_has_signal = false;
129 ud->last_is_connected = false;
130 for (unsigned field = 0; field < 2; ++field) {
131 switch (pixel_format) {
132 case bmusb::PixelFormat_10BitYCbCr: {
133 const size_t v210_width = v210Converter::get_minimum_v210_texture_width(width);
135 // Seemingly we need to set the minification filter even though
136 // shader image loads don't use them, or NVIDIA will just give us
138 glBindTexture(GL_TEXTURE_2D, ud->tex_v210[field]);
140 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
143 ud->last_v210_width[0] = v210_width;
144 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, v210_width, height, 0, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV, nullptr);
148 glBindTexture(GL_TEXTURE_2D, ud->tex_444[field]);
152 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, width, height, 0, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV, nullptr);
157 case bmusb::PixelFormat_8BitYCbCr:
158 glBindTexture(GL_TEXTURE_2D, ud->tex_y[field]);
162 glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, width, height, 0, GL_RED, GL_UNSIGNED_BYTE, nullptr);
166 glBindTexture(GL_TEXTURE_2D, ud->tex_cbcr[field]);
170 glTexImage2D(GL_TEXTURE_2D, 0, GL_RG8, width / 2, height, 0, GL_RG, GL_UNSIGNED_BYTE, nullptr);
174 case bmusb::PixelFormat_8BitBGRA:
175 glBindTexture(GL_TEXTURE_2D, ud->tex_rgba[field]);
179 glTexImage2D(GL_TEXTURE_2D, 0, GL_SRGB8_ALPHA8, width, height, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, nullptr);
183 case bmusb::PixelFormat_8BitYCbCrPlanar:
184 glBindTexture(GL_TEXTURE_2D, ud->tex_y[field]);
188 glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, width, height, 0, GL_RED, GL_UNSIGNED_BYTE, nullptr);
192 glBindTexture(GL_TEXTURE_2D, ud->tex_cb[field]);
196 glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, width / 2, height, 0, GL_RED, GL_UNSIGNED_BYTE, nullptr);
200 glBindTexture(GL_TEXTURE_2D, ud->tex_cr[field]);
204 glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, width / 2, height, 0, GL_RED, GL_UNSIGNED_BYTE, nullptr);
213 freelist.push(frame);
216 PBOFrameAllocator::~PBOFrameAllocator()
218 while (!freelist.empty()) {
219 Frame frame = freelist.front();
221 destroy_frame(&frame);
225 void PBOFrameAllocator::destroy_frame(Frame *frame)
227 Userdata *ud = (Userdata *)frame->userdata;
228 delete[] ud->data_copy_malloc;
230 GLuint pbo = ud->pbo;
231 glBindBuffer(buffer, pbo);
233 glUnmapBuffer(buffer);
235 glBindBuffer(buffer, 0);
237 glDeleteBuffers(1, &pbo);
239 switch (pixel_format) {
240 case bmusb::PixelFormat_10BitYCbCr:
241 glDeleteTextures(2, ud->tex_v210);
243 glDeleteTextures(2, ud->tex_444);
246 case bmusb::PixelFormat_8BitYCbCr:
247 glDeleteTextures(2, ud->tex_y);
249 glDeleteTextures(2, ud->tex_cbcr);
252 case bmusb::PixelFormat_8BitBGRA:
253 glDeleteTextures(2, ud->tex_rgba);
256 case bmusb::PixelFormat_8BitYCbCrPlanar:
257 glDeleteTextures(2, ud->tex_y);
259 glDeleteTextures(2, ud->tex_cb);
261 glDeleteTextures(2, ud->tex_cr);
268 //static int sumsum = 0;
270 bmusb::FrameAllocator::Frame PBOFrameAllocator::alloc_frame()
274 lock_guard<mutex> lock(freelist_mutex); // Meh.
275 if (freelist.empty()) {
276 printf("Frame overrun (no more spare PBO frames), dropping frame!\n");
278 //fprintf(stderr, "freelist has %d allocated\n", ++sumsum);
279 vf = freelist.front();
280 freelist.pop(); // Meh.
285 if (mjpeg_encoder != nullptr &&
286 mjpeg_encoder->should_encode_mjpeg_for_card(card_index) &&
287 vf.userdata != nullptr) {
288 Userdata *ud = (Userdata *)vf.userdata;
289 vf.data_copy = ud->data_copy_malloc;
290 ud->data_copy_current_src = Userdata::FROM_MALLOC;
292 vf.data_copy = nullptr;
298 bmusb::FrameAllocator::Frame PBOFrameAllocator::create_frame(size_t width, size_t height, size_t stride)
303 lock_guard<mutex> lock(freelist_mutex);
304 if (freelist.empty()) {
305 printf("Frame overrun (no more spare PBO frames), dropping frame!\n");
310 vf = freelist.front();
317 Userdata *userdata = (Userdata *)vf.userdata;
319 if (mjpeg_encoder != nullptr &&
320 mjpeg_encoder->should_encode_mjpeg_for_card(card_index)) {
321 if (mjpeg_encoder->using_vaapi()) {
322 VADisplay va_dpy = mjpeg_encoder->va_dpy->va_dpy;
323 MJPEGEncoder::VAResources resources = mjpeg_encoder->get_va_resources(width, height);
324 MJPEGEncoder::ReleaseVAResources release(mjpeg_encoder, resources);
326 if (resources.image.pitches[0] == stride) {
327 userdata->va_resources = move(resources);
328 userdata->va_resources_release = move(release);
330 VAStatus va_status = vaMapBuffer(va_dpy, resources.image.buf, (void **)&vf.data_copy);
331 CHECK_VASTATUS(va_status, "vaMapBuffer");
332 vf.data_copy += resources.image.offsets[0];
333 userdata->data_copy_current_src = Userdata::FROM_VA_API;
335 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]);
336 vf.data_copy = userdata->data_copy_malloc;
337 userdata->data_copy_current_src = Userdata::FROM_MALLOC;
340 vf.data_copy = userdata->data_copy_malloc;
341 userdata->data_copy_current_src = Userdata::FROM_MALLOC;
344 vf.data_copy = nullptr;
350 void PBOFrameAllocator::release_frame(Frame frame)
352 if (frame.overflow > 0) {
353 printf("%d bytes overflow after last (PBO) frame\n", int(frame.overflow));
357 // Poison the page. (Note that this might be bogus if you don't have an OpenGL context.)
358 memset(frame.data, 0, frame.size);
359 Userdata *userdata = (Userdata *)frame.userdata;
360 for (unsigned field = 0; field < 2; ++field) {
361 glBindTexture(GL_TEXTURE_2D, userdata->tex_y[field]);
363 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
365 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
367 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
369 glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, userdata->last_width[field], userdata->last_height[field], 0, GL_RED, GL_UNSIGNED_BYTE, nullptr);
372 glBindTexture(GL_TEXTURE_2D, userdata->tex_cbcr[field]);
374 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
376 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
378 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
380 glTexImage2D(GL_TEXTURE_2D, 0, GL_RG8, userdata->last_width[field] / 2, userdata->last_height[field], 0, GL_RED, GL_UNSIGNED_BYTE, nullptr);
386 // In case we never got to upload the frame to MJPEGEncoder.
387 Userdata *userdata = (Userdata *)frame.userdata;
388 MJPEGEncoder::VAResources resources __attribute__((unused)) = move(userdata->va_resources);
389 MJPEGEncoder::ReleaseVAResources release = move(userdata->va_resources_release);
391 if (frame.data_copy != nullptr && userdata->data_copy_current_src == Userdata::FROM_VA_API) {
392 VADisplay va_dpy = mjpeg_encoder->va_dpy->va_dpy;
393 VAStatus va_status = vaUnmapBuffer(va_dpy, resources.image.buf);
394 CHECK_VASTATUS(va_status, "vaUnmapBuffer");
396 frame.data_copy = nullptr;
400 lock_guard<mutex> lock(freelist_mutex);
401 Userdata *userdata = (Userdata *)frame.userdata;
402 if (userdata->generation == generation) {
403 freelist.push(frame);
405 destroy_frame(&frame);
410 void PBOFrameAllocator::reconfigure(bmusb::PixelFormat pixel_format,
412 GLuint width, GLuint height,
414 MJPEGEncoder *mjpeg_encoder,
415 size_t num_queued_frames,
420 if (pixel_format == this->pixel_format &&
421 frame_size == this->frame_size &&
422 width == this->width && height == this->height &&
423 card_index == this->card_index &&
424 mjpeg_encoder == this->mjpeg_encoder &&
425 num_queued_frames == this->num_queued_frames &&
426 buffer == this->buffer &&
427 permissions == this->permissions &&
428 map_bits == this->map_bits) {
432 this->pixel_format = pixel_format;
433 this->frame_size = frame_size;
435 this->height = height;
436 this->card_index = card_index;
437 this->mjpeg_encoder = mjpeg_encoder;
438 this->num_queued_frames = num_queued_frames;
439 this->buffer = buffer;
440 this->permissions = permissions;
441 this->map_bits = map_bits;
443 lock_guard<mutex> lock(freelist_mutex);
444 while (!freelist.empty()) {
445 Frame frame = freelist.front();
447 destroy_frame(&frame);
450 for (size_t i = 0; i < num_queued_frames; ++i) {
451 init_frame(i, frame_size, width, height, permissions, map_bits, generation);
454 // There may still be frames out with the old configuration
455 // (for instance, living in GLWidget); they will be destroyed
456 // when they come back in release_frame().