X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=nageru%2Fpbo_frame_allocator.cpp;h=ba5322602a47e4f0903fda950a96e597bd6c5660;hb=2f8f882defb23abe8b2c54e195b72c57fba55cd6;hp=4640d355e25e96a60db88fddc3963419577cd636;hpb=575a3feeaa7420f65a4468b997167d7555794c08;p=nageru diff --git a/nageru/pbo_frame_allocator.cpp b/nageru/pbo_frame_allocator.cpp index 4640d35..ba53226 100644 --- a/nageru/pbo_frame_allocator.cpp +++ b/nageru/pbo_frame_allocator.cpp @@ -29,11 +29,20 @@ void set_clamp_to_edge() } // namespace 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) - : card_index(card_index), mjpeg_encoder(mjpeg_encoder), pixel_format(pixel_format), buffer(buffer) + : card_index(card_index), + mjpeg_encoder(mjpeg_encoder), + pixel_format(pixel_format), + buffer(buffer), + frame_size(frame_size), + num_queued_frames(num_queued_frames), + width(width), + height(height), + permissions(permissions), + map_bits(map_bits) { userdata.reset(new Userdata[num_queued_frames]); for (size_t i = 0; i < num_queued_frames; ++i) { - init_frame(i, frame_size, width, height, permissions, map_bits); + init_frame(i, frame_size, width, height, permissions, map_bits, generation); } glBindBuffer(buffer, 0); check_error(); @@ -41,7 +50,7 @@ PBOFrameAllocator::PBOFrameAllocator(bmusb::PixelFormat pixel_format, size_t fra check_error(); } -void PBOFrameAllocator::init_frame(size_t frame_idx, size_t frame_size, GLuint width, GLuint height, GLenum permissions, GLenum map_bits) +void PBOFrameAllocator::init_frame(size_t frame_idx, size_t frame_size, GLuint width, GLuint height, GLenum permissions, GLenum map_bits, int generation) { GLuint pbo; glGenBuffers(1, &pbo); @@ -58,6 +67,7 @@ void PBOFrameAllocator::init_frame(size_t frame_idx, size_t frame_size, GLuint w frame.size = frame_size; Userdata *ud = &userdata[frame_idx]; frame.userdata = ud; + ud->generation = generation; ud->pbo = pbo; ud->pixel_format = pixel_format; ud->data_copy_malloc = new uint8_t[frame_size]; @@ -254,6 +264,14 @@ void PBOFrameAllocator::destroy_frame(Frame *frame) default: assert(false); } + + if (ud->generation != generation) { + auto it = lingering_generations.find(ud->generation); + assert(it != lingering_generations.end()); + if (--it->second.num_frames_left == 0) { + lingering_generations.erase(it); // Deallocates the userdata block. + } + } } //static int sumsum = 0; @@ -272,8 +290,9 @@ bmusb::FrameAllocator::Frame PBOFrameAllocator::alloc_frame() vf.len = 0; vf.overflow = 0; - if (mjpeg_encoder != nullptr && mjpeg_encoder->using_vaapi() && - mjpeg_encoder->get_mjpeg_stream_for_card(card_index) != -1) { + if (mjpeg_encoder != nullptr && + mjpeg_encoder->should_encode_mjpeg_for_card(card_index) && + vf.userdata != nullptr) { Userdata *ud = (Userdata *)vf.userdata; vf.data_copy = ud->data_copy_malloc; ud->data_copy_current_src = Userdata::FROM_MALLOC; @@ -305,22 +324,27 @@ bmusb::FrameAllocator::Frame PBOFrameAllocator::create_frame(size_t width, size_ Userdata *userdata = (Userdata *)vf.userdata; - if (mjpeg_encoder != nullptr && mjpeg_encoder->using_vaapi() && - mjpeg_encoder->get_mjpeg_stream_for_card(card_index) != -1) { - VADisplay va_dpy = mjpeg_encoder->va_dpy->va_dpy; - MJPEGEncoder::VAResources resources = mjpeg_encoder->get_va_resources(width, height); - MJPEGEncoder::ReleaseVAResources release(mjpeg_encoder, resources); - - if (resources.image.pitches[0] == stride) { - userdata->va_resources = move(resources); - userdata->va_resources_release = move(release); - - VAStatus va_status = vaMapBuffer(va_dpy, resources.image.buf, (void **)&vf.data_copy); - CHECK_VASTATUS(va_status, "vaMapBuffer"); - vf.data_copy += resources.image.offsets[0]; - userdata->data_copy_current_src = Userdata::FROM_VA_API; + if (mjpeg_encoder != nullptr && + mjpeg_encoder->should_encode_mjpeg_for_card(card_index)) { + if (mjpeg_encoder->using_vaapi()) { + VADisplay va_dpy = mjpeg_encoder->va_dpy->va_dpy; + MJPEGEncoder::VAResources resources = mjpeg_encoder->get_va_resources(width, height, VA_FOURCC_UYVY); // Only used by DeckLinkCapture, so always 4:2:2. + MJPEGEncoder::ReleaseVAResources release(mjpeg_encoder, resources); + + if (resources.image.pitches[0] == stride) { + userdata->va_resources = move(resources); + userdata->va_resources_release = move(release); + + VAStatus va_status = vaMapBuffer(va_dpy, resources.image.buf, (void **)&vf.data_copy); + CHECK_VASTATUS(va_status, "vaMapBuffer"); + vf.data_copy += resources.image.offsets[0]; + userdata->data_copy_current_src = Userdata::FROM_VA_API; + } else { + 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]); + vf.data_copy = userdata->data_copy_malloc; + userdata->data_copy_current_src = Userdata::FROM_MALLOC; + } } else { - 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]); vf.data_copy = userdata->data_copy_malloc; userdata->data_copy_current_src = Userdata::FROM_MALLOC; } @@ -382,6 +406,64 @@ void PBOFrameAllocator::release_frame(Frame frame) } lock_guard lock(freelist_mutex); - freelist.push(frame); + Userdata *userdata = (Userdata *)frame.userdata; + if (userdata->generation == generation) { + freelist.push(frame); + } else { + destroy_frame(&frame); + } //--sumsum; } + +void PBOFrameAllocator::reconfigure(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) +{ + if (pixel_format == this->pixel_format && + frame_size == this->frame_size && + width == this->width && height == this->height && + card_index == this->card_index && + mjpeg_encoder == this->mjpeg_encoder && + num_queued_frames == this->num_queued_frames && + buffer == this->buffer && + permissions == this->permissions && + map_bits == this->map_bits) { + return; + } + + lock_guard lock(freelist_mutex); + lingering_generations[generation] = LingeringGeneration{ move(userdata), this->num_queued_frames }; + ++generation; + + while (!freelist.empty()) { + Frame frame = freelist.front(); + freelist.pop(); + destroy_frame(&frame); + } + + this->pixel_format = pixel_format; + this->frame_size = frame_size; + this->width = width; + this->height = height; + this->card_index = card_index; + this->mjpeg_encoder = mjpeg_encoder; + this->num_queued_frames = num_queued_frames; + this->buffer = buffer; + this->permissions = permissions; + this->map_bits = map_bits; + + userdata.reset(new Userdata[num_queued_frames]); + for (size_t i = 0; i < num_queued_frames; ++i) { + init_frame(i, frame_size, width, height, permissions, map_bits, generation); + } + + // There may still be frames out with the old configuration + // (for instance, living in GLWidget); they will be destroyed + // when they come back in release_frame(). +}