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) {
51 init_frame(frame, &userdata[i], this, pixel_format, frame_size, width, height, permissions, map_bits, buffer, generation);
54 glBindBuffer(buffer, 0);
56 glBindTexture(GL_TEXTURE_2D, 0);
60 void PBOFrameAllocator::init_frame(Frame &frame, Userdata *ud, PBOFrameAllocator *owner, bmusb::PixelFormat pixel_format, size_t frame_size, GLuint width, GLuint height, GLenum permissions, GLenum map_bits, GLenum buffer, int generation)
63 glGenBuffers(1, &pbo);
65 glBindBuffer(buffer, pbo);
67 glBufferStorage(buffer, frame_size, nullptr, permissions | GL_MAP_PERSISTENT_BIT);
70 frame.data = (uint8_t *)glMapBufferRange(buffer, 0, frame_size, permissions | map_bits | GL_MAP_PERSISTENT_BIT);
71 frame.data2 = frame.data + frame_size / 2;
73 frame.size = frame_size;
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);
219 PBOFrameAllocator::~PBOFrameAllocator()
221 while (!freelist.empty()) {
222 Frame frame = freelist.front();
224 destroy_frame(&frame);
228 void PBOFrameAllocator::destroy_frame(Frame *frame)
230 Userdata *ud = (Userdata *)frame->userdata;
231 delete[] ud->data_copy_malloc;
233 GLuint pbo = ud->pbo;
234 glBindBuffer(buffer, pbo);
236 glUnmapBuffer(buffer);
238 glBindBuffer(buffer, 0);
240 glDeleteBuffers(1, &pbo);
242 switch (ud->pixel_format) {
243 case bmusb::PixelFormat_10BitYCbCr:
244 glDeleteTextures(2, ud->tex_v210);
246 glDeleteTextures(2, ud->tex_444);
249 case bmusb::PixelFormat_8BitYCbCr:
250 glDeleteTextures(2, ud->tex_y);
252 glDeleteTextures(2, ud->tex_cbcr);
255 case bmusb::PixelFormat_8BitBGRA:
256 glDeleteTextures(2, ud->tex_rgba);
259 case bmusb::PixelFormat_8BitYCbCrPlanar:
260 glDeleteTextures(2, ud->tex_y);
262 glDeleteTextures(2, ud->tex_cb);
264 glDeleteTextures(2, ud->tex_cr);
271 if (ud->generation != generation) {
272 auto it = lingering_generations.find(ud->generation);
273 assert(it != lingering_generations.end());
274 if (--it->second.num_frames_left == 0) {
275 lingering_generations.erase(it); // Deallocates the userdata block.
279 //static int sumsum = 0;
281 bmusb::FrameAllocator::Frame PBOFrameAllocator::alloc_frame()
285 lock_guard<mutex> lock(freelist_mutex); // Meh.
286 if (freelist.empty()) {
287 printf("Frame overrun (no more spare PBO frames), dropping frame!\n");
289 //fprintf(stderr, "freelist has %d allocated\n", ++sumsum);
290 vf = freelist.front();
291 freelist.pop(); // Meh.
296 if (mjpeg_encoder != nullptr &&
297 mjpeg_encoder->should_encode_mjpeg_for_card(card_index) &&
298 vf.userdata != nullptr) {
299 Userdata *ud = (Userdata *)vf.userdata;
300 vf.data_copy = ud->data_copy_malloc;
301 ud->data_copy_current_src = Userdata::FROM_MALLOC;
303 vf.data_copy = nullptr;
309 bmusb::FrameAllocator::Frame PBOFrameAllocator::create_frame(size_t width, size_t height, size_t stride)
314 lock_guard<mutex> lock(freelist_mutex);
315 if (freelist.empty()) {
316 printf("Frame overrun (no more spare PBO frames), dropping frame!\n");
321 vf = freelist.front();
328 Userdata *userdata = (Userdata *)vf.userdata;
330 if (mjpeg_encoder != nullptr &&
331 mjpeg_encoder->should_encode_mjpeg_for_card(card_index)) {
332 if (mjpeg_encoder->using_vaapi()) {
333 VADisplay va_dpy = mjpeg_encoder->va_dpy->va_dpy;
334 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.
335 ReleaseVAResources release(mjpeg_encoder->get_va_pool(), resources);
337 if (resources.image.pitches[0] == stride) {
338 userdata->va_resources = move(resources);
339 userdata->va_resources_release = move(release);
341 VAStatus va_status = vaMapBuffer(va_dpy, resources.image.buf, (void **)&vf.data_copy);
342 CHECK_VASTATUS(va_status, "vaMapBuffer");
343 vf.data_copy += resources.image.offsets[0];
344 userdata->data_copy_current_src = Userdata::FROM_VA_API;
346 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]);
347 vf.data_copy = userdata->data_copy_malloc;
348 userdata->data_copy_current_src = Userdata::FROM_MALLOC;
351 vf.data_copy = userdata->data_copy_malloc;
352 userdata->data_copy_current_src = Userdata::FROM_MALLOC;
355 vf.data_copy = nullptr;
361 void PBOFrameAllocator::release_frame(Frame frame)
363 if (frame.overflow > 0) {
364 printf("%d bytes overflow after last (PBO) frame\n", int(frame.overflow));
368 // Poison the page. (Note that this might be bogus if you don't have an OpenGL context.)
369 memset(frame.data, 0, frame.size);
370 Userdata *userdata = (Userdata *)frame.userdata;
371 for (unsigned field = 0; field < 2; ++field) {
372 glBindTexture(GL_TEXTURE_2D, userdata->tex_y[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_R8, userdata->last_width[field], userdata->last_height[field], 0, GL_RED, GL_UNSIGNED_BYTE, nullptr);
383 glBindTexture(GL_TEXTURE_2D, userdata->tex_cbcr[field]);
385 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
387 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
389 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
391 glTexImage2D(GL_TEXTURE_2D, 0, GL_RG8, userdata->last_width[field] / 2, userdata->last_height[field], 0, GL_RED, GL_UNSIGNED_BYTE, nullptr);
397 // In case we never got to upload the frame to MJPEGEncoder.
398 Userdata *userdata = (Userdata *)frame.userdata;
399 VAResourcePool::VAResources resources __attribute__((unused)) = move(userdata->va_resources);
400 ReleaseVAResources release = move(userdata->va_resources_release);
402 if (frame.data_copy != nullptr && userdata->data_copy_current_src == Userdata::FROM_VA_API) {
403 VADisplay va_dpy = mjpeg_encoder->va_dpy->va_dpy;
404 VAStatus va_status = vaUnmapBuffer(va_dpy, resources.image.buf);
405 CHECK_VASTATUS(va_status, "vaUnmapBuffer");
407 frame.data_copy = nullptr;
411 lock_guard<mutex> lock(freelist_mutex);
412 Userdata *userdata = (Userdata *)frame.userdata;
413 if (userdata->generation == generation) {
414 freelist.push(frame);
416 destroy_frame(&frame);
421 void PBOFrameAllocator::reconfigure(bmusb::PixelFormat pixel_format,
423 GLuint width, GLuint height,
425 MJPEGEncoder *mjpeg_encoder,
426 size_t num_queued_frames,
431 if (pixel_format == this->pixel_format &&
432 frame_size == this->frame_size &&
433 width == this->width && height == this->height &&
434 card_index == this->card_index &&
435 mjpeg_encoder == this->mjpeg_encoder &&
436 num_queued_frames == this->num_queued_frames &&
437 buffer == this->buffer &&
438 permissions == this->permissions &&
439 map_bits == this->map_bits) {
443 lock_guard<mutex> lock(freelist_mutex);
444 lingering_generations[generation] = LingeringGeneration{ move(userdata), this->num_queued_frames };
447 while (!freelist.empty()) {
448 Frame frame = freelist.front();
450 destroy_frame(&frame);
453 this->pixel_format = pixel_format;
454 this->frame_size = frame_size;
456 this->height = height;
457 this->card_index = card_index;
458 this->mjpeg_encoder = mjpeg_encoder;
459 this->num_queued_frames = num_queued_frames;
460 this->buffer = buffer;
461 this->permissions = permissions;
462 this->map_bits = map_bits;
464 userdata.reset(new Userdata[num_queued_frames]);
465 for (size_t i = 0; i < num_queued_frames; ++i) {
467 init_frame(frame, &userdata[i], this, pixel_format, frame_size, width, height, permissions, map_bits, buffer, generation);
468 freelist.push(frame);
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().