1 #include "pbo_frame_allocator.h"
3 #include <bmusb/bmusb.h>
6 #include <movit/util.h>
15 #include "mjpeg_encoder.h"
16 #include "shared/va_resource_pool.h"
17 #include "v210_converter.h"
18 #include "shared/va_display.h"
24 void set_clamp_to_edge()
26 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
28 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
30 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
36 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)
37 : card_index(card_index),
38 mjpeg_encoder(mjpeg_encoder),
39 pixel_format(pixel_format),
41 frame_size(frame_size),
42 num_queued_frames(num_queued_frames),
45 permissions(permissions),
48 userdata.reset(new Userdata[num_queued_frames]);
49 for (size_t i = 0; i < num_queued_frames; ++i) {
50 init_frame(i, frame_size, width, height, permissions, map_bits, generation);
52 glBindBuffer(buffer, 0);
54 glBindTexture(GL_TEXTURE_2D, 0);
58 void PBOFrameAllocator::init_frame(size_t frame_idx, size_t frame_size, GLuint width, GLuint height, GLenum permissions, GLenum map_bits, int generation)
61 glGenBuffers(1, &pbo);
63 glBindBuffer(buffer, pbo);
65 glBufferStorage(buffer, frame_size, nullptr, permissions | GL_MAP_PERSISTENT_BIT);
69 frame.data = (uint8_t *)glMapBufferRange(buffer, 0, frame_size, permissions | map_bits | GL_MAP_PERSISTENT_BIT);
70 frame.data2 = frame.data + frame_size / 2;
72 frame.size = frame_size;
73 Userdata *ud = &userdata[frame_idx];
75 ud->generation = generation;
77 ud->pixel_format = pixel_format;
78 ud->data_copy_malloc = new uint8_t[frame_size];
81 // For 8-bit non-planar Y'CbCr, we ask the driver to split Y' and Cb/Cr
82 // into separate textures. For 10-bit, the input format (v210)
83 // is complicated enough that we need to interpolate up to 4:4:4,
84 // which we do in a compute shader ourselves. For BGRA, the data
85 // is already 4:4:4:4.
86 frame.interleaved = (pixel_format == bmusb::PixelFormat_8BitYCbCr);
88 // Create textures. We don't allocate any data for the second field at this point
89 // (just create the texture state with the samplers), since our default assumed
90 // resolution is progressive.
91 switch (pixel_format) {
92 case bmusb::PixelFormat_8BitYCbCr:
93 glGenTextures(2, ud->tex_y);
95 glGenTextures(2, ud->tex_cbcr);
98 case bmusb::PixelFormat_10BitYCbCr:
99 glGenTextures(2, ud->tex_v210);
101 glGenTextures(2, ud->tex_444);
104 case bmusb::PixelFormat_8BitBGRA:
105 glGenTextures(2, ud->tex_rgba);
108 case bmusb::PixelFormat_8BitYCbCrPlanar:
109 glGenTextures(2, ud->tex_y);
111 glGenTextures(2, ud->tex_cb);
113 glGenTextures(2, ud->tex_cr);
120 ud->last_width[0] = width;
121 ud->last_height[0] = height;
122 ud->last_cbcr_width[0] = width / 2;
123 ud->last_cbcr_height[0] = height;
124 ud->last_v210_width[0] = 0;
126 ud->last_width[1] = 0;
127 ud->last_height[1] = 0;
128 ud->last_cbcr_width[1] = 0;
129 ud->last_cbcr_height[1] = 0;
130 ud->last_v210_width[1] = 0;
132 ud->last_interlaced = false;
133 ud->last_has_signal = false;
134 ud->last_is_connected = false;
135 for (unsigned field = 0; field < 2; ++field) {
136 switch (pixel_format) {
137 case bmusb::PixelFormat_10BitYCbCr: {
138 const size_t v210_width = v210Converter::get_minimum_v210_texture_width(width);
140 // Seemingly we need to set the minification filter even though
141 // shader image loads don't use them, or NVIDIA will just give us
143 glBindTexture(GL_TEXTURE_2D, ud->tex_v210[field]);
145 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
148 ud->last_v210_width[0] = v210_width;
149 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, v210_width, height, 0, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV, nullptr);
153 glBindTexture(GL_TEXTURE_2D, ud->tex_444[field]);
157 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB10_A2, width, height, 0, GL_RGBA, GL_UNSIGNED_INT_2_10_10_10_REV, nullptr);
162 case bmusb::PixelFormat_8BitYCbCr:
163 glBindTexture(GL_TEXTURE_2D, ud->tex_y[field]);
167 glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, width, height, 0, GL_RED, GL_UNSIGNED_BYTE, nullptr);
171 glBindTexture(GL_TEXTURE_2D, ud->tex_cbcr[field]);
175 glTexImage2D(GL_TEXTURE_2D, 0, GL_RG8, width / 2, height, 0, GL_RG, GL_UNSIGNED_BYTE, nullptr);
179 case bmusb::PixelFormat_8BitBGRA:
180 glBindTexture(GL_TEXTURE_2D, ud->tex_rgba[field]);
184 glTexImage2D(GL_TEXTURE_2D, 0, GL_SRGB8_ALPHA8, width, height, 0, GL_BGRA, GL_UNSIGNED_INT_8_8_8_8_REV, nullptr);
188 case bmusb::PixelFormat_8BitYCbCrPlanar:
189 glBindTexture(GL_TEXTURE_2D, ud->tex_y[field]);
193 glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, width, height, 0, GL_RED, GL_UNSIGNED_BYTE, nullptr);
197 glBindTexture(GL_TEXTURE_2D, ud->tex_cb[field]);
201 glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, width / 2, height, 0, GL_RED, GL_UNSIGNED_BYTE, nullptr);
205 glBindTexture(GL_TEXTURE_2D, ud->tex_cr[field]);
209 glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, width / 2, height, 0, GL_RED, GL_UNSIGNED_BYTE, nullptr);
218 freelist.push(frame);
221 PBOFrameAllocator::~PBOFrameAllocator()
223 while (!freelist.empty()) {
224 Frame frame = freelist.front();
226 destroy_frame(&frame);
230 void PBOFrameAllocator::destroy_frame(Frame *frame)
232 Userdata *ud = (Userdata *)frame->userdata;
233 delete[] ud->data_copy_malloc;
235 GLuint pbo = ud->pbo;
236 glBindBuffer(buffer, pbo);
238 glUnmapBuffer(buffer);
240 glBindBuffer(buffer, 0);
242 glDeleteBuffers(1, &pbo);
244 switch (ud->pixel_format) {
245 case bmusb::PixelFormat_10BitYCbCr:
246 glDeleteTextures(2, ud->tex_v210);
248 glDeleteTextures(2, ud->tex_444);
251 case bmusb::PixelFormat_8BitYCbCr:
252 glDeleteTextures(2, ud->tex_y);
254 glDeleteTextures(2, ud->tex_cbcr);
257 case bmusb::PixelFormat_8BitBGRA:
258 glDeleteTextures(2, ud->tex_rgba);
261 case bmusb::PixelFormat_8BitYCbCrPlanar:
262 glDeleteTextures(2, ud->tex_y);
264 glDeleteTextures(2, ud->tex_cb);
266 glDeleteTextures(2, ud->tex_cr);
273 if (ud->generation != generation) {
274 auto it = lingering_generations.find(ud->generation);
275 assert(it != lingering_generations.end());
276 if (--it->second.num_frames_left == 0) {
277 lingering_generations.erase(it); // Deallocates the userdata block.
281 //static int sumsum = 0;
283 bmusb::FrameAllocator::Frame PBOFrameAllocator::alloc_frame()
287 lock_guard<mutex> lock(freelist_mutex); // Meh.
288 if (freelist.empty()) {
289 printf("Frame overrun (no more spare PBO frames), dropping frame!\n");
291 //fprintf(stderr, "freelist has %d allocated\n", ++sumsum);
292 vf = freelist.front();
293 freelist.pop(); // Meh.
298 if (mjpeg_encoder != nullptr &&
299 mjpeg_encoder->should_encode_mjpeg_for_card(card_index) &&
300 vf.userdata != nullptr) {
301 Userdata *ud = (Userdata *)vf.userdata;
302 vf.data_copy = ud->data_copy_malloc;
303 ud->data_copy_current_src = Userdata::FROM_MALLOC;
305 vf.data_copy = nullptr;
311 bmusb::FrameAllocator::Frame PBOFrameAllocator::create_frame(size_t width, size_t height, size_t stride)
316 lock_guard<mutex> lock(freelist_mutex);
317 if (freelist.empty()) {
318 printf("Frame overrun (no more spare PBO frames), dropping frame!\n");
323 vf = freelist.front();
330 Userdata *userdata = (Userdata *)vf.userdata;
332 if (mjpeg_encoder != nullptr &&
333 mjpeg_encoder->should_encode_mjpeg_for_card(card_index)) {
334 if (mjpeg_encoder->using_vaapi()) {
335 VADisplay va_dpy = mjpeg_encoder->va_dpy->va_dpy;
336 VAResourcePool::VAResources resources = mjpeg_encoder->get_va_pool()->get_va_resources(width, height, VA_FOURCC_UYVY); // Only used by DeckLinkCapture, so always 4:2:2.
337 ReleaseVAResources release(mjpeg_encoder->get_va_pool(), resources);
339 if (resources.image.pitches[0] == stride) {
340 userdata->va_resources = move(resources);
341 userdata->va_resources_release = move(release);
343 VAStatus va_status = vaMapBuffer(va_dpy, resources.image.buf, (void **)&vf.data_copy);
344 CHECK_VASTATUS(va_status, "vaMapBuffer");
345 vf.data_copy += resources.image.offsets[0];
346 userdata->data_copy_current_src = Userdata::FROM_VA_API;
348 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]);
349 vf.data_copy = userdata->data_copy_malloc;
350 userdata->data_copy_current_src = Userdata::FROM_MALLOC;
353 vf.data_copy = userdata->data_copy_malloc;
354 userdata->data_copy_current_src = Userdata::FROM_MALLOC;
357 vf.data_copy = nullptr;
363 void PBOFrameAllocator::release_frame(Frame frame)
365 if (frame.overflow > 0) {
366 printf("%d bytes overflow after last (PBO) frame\n", int(frame.overflow));
370 // Poison the page. (Note that this might be bogus if you don't have an OpenGL context.)
371 memset(frame.data, 0, frame.size);
372 Userdata *userdata = (Userdata *)frame.userdata;
373 for (unsigned field = 0; field < 2; ++field) {
374 glBindTexture(GL_TEXTURE_2D, userdata->tex_y[field]);
376 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
378 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
380 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
382 glTexImage2D(GL_TEXTURE_2D, 0, GL_R8, userdata->last_width[field], userdata->last_height[field], 0, GL_RED, GL_UNSIGNED_BYTE, nullptr);
385 glBindTexture(GL_TEXTURE_2D, userdata->tex_cbcr[field]);
387 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
389 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
391 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
393 glTexImage2D(GL_TEXTURE_2D, 0, GL_RG8, userdata->last_width[field] / 2, userdata->last_height[field], 0, GL_RED, GL_UNSIGNED_BYTE, nullptr);
399 // In case we never got to upload the frame to MJPEGEncoder.
400 Userdata *userdata = (Userdata *)frame.userdata;
401 VAResourcePool::VAResources resources __attribute__((unused)) = move(userdata->va_resources);
402 ReleaseVAResources release = move(userdata->va_resources_release);
404 if (frame.data_copy != nullptr && userdata->data_copy_current_src == Userdata::FROM_VA_API) {
405 VADisplay va_dpy = mjpeg_encoder->va_dpy->va_dpy;
406 VAStatus va_status = vaUnmapBuffer(va_dpy, resources.image.buf);
407 CHECK_VASTATUS(va_status, "vaUnmapBuffer");
409 frame.data_copy = nullptr;
413 lock_guard<mutex> lock(freelist_mutex);
414 Userdata *userdata = (Userdata *)frame.userdata;
415 if (userdata->generation == generation) {
416 freelist.push(frame);
418 destroy_frame(&frame);
423 void PBOFrameAllocator::reconfigure(bmusb::PixelFormat pixel_format,
425 GLuint width, GLuint height,
427 MJPEGEncoder *mjpeg_encoder,
428 size_t num_queued_frames,
433 if (pixel_format == this->pixel_format &&
434 frame_size == this->frame_size &&
435 width == this->width && height == this->height &&
436 card_index == this->card_index &&
437 mjpeg_encoder == this->mjpeg_encoder &&
438 num_queued_frames == this->num_queued_frames &&
439 buffer == this->buffer &&
440 permissions == this->permissions &&
441 map_bits == this->map_bits) {
445 lock_guard<mutex> lock(freelist_mutex);
446 lingering_generations[generation] = LingeringGeneration{ move(userdata), this->num_queued_frames };
449 while (!freelist.empty()) {
450 Frame frame = freelist.front();
452 destroy_frame(&frame);
455 this->pixel_format = pixel_format;
456 this->frame_size = frame_size;
458 this->height = height;
459 this->card_index = card_index;
460 this->mjpeg_encoder = mjpeg_encoder;
461 this->num_queued_frames = num_queued_frames;
462 this->buffer = buffer;
463 this->permissions = permissions;
464 this->map_bits = map_bits;
466 userdata.reset(new Userdata[num_queued_frames]);
467 for (size_t i = 0; i < num_queued_frames; ++i) {
468 init_frame(i, frame_size, width, height, permissions, map_bits, generation);
471 // There may still be frames out with the old configuration
472 // (for instance, living in GLWidget); they will be destroyed
473 // when they come back in release_frame().