]> git.sesse.net Git - nageru/blobdiff - nageru/pbo_frame_allocator.cpp
When uploading MJPEG data to VA-API, do it directly into the buffer.
[nageru] / nageru / pbo_frame_allocator.cpp
index 4c1a55bcd0d4d47aeaffdff324b29f59780d9fdd..d0859b357d98a313fb40587b2663dad517c7e2e5 100644 (file)
@@ -8,7 +8,9 @@
 #include <cstddef>
 
 #include "flags.h"
+#include "mjpeg_encoder.h"
 #include "v210_converter.h"
+#include "va_display_with_cleanup.h"
 
 using namespace std;
 
@@ -26,8 +28,8 @@ void set_clamp_to_edge()
 
 }  // namespace
 
-PBOFrameAllocator::PBOFrameAllocator(bmusb::PixelFormat pixel_format, size_t frame_size, GLuint width, GLuint height, size_t num_queued_frames, GLenum buffer, GLenum permissions, GLenum map_bits)
-        : pixel_format(pixel_format), buffer(buffer)
+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)
 {
        userdata.reset(new Userdata[num_queued_frames]);
        for (size_t i = 0; i < num_queued_frames; ++i) {
@@ -52,13 +54,13 @@ void PBOFrameAllocator::init_frame(size_t frame_idx, size_t frame_size, GLuint w
        Frame frame;
        frame.data = (uint8_t *)glMapBufferRange(buffer, 0, frame_size, permissions | map_bits | GL_MAP_PERSISTENT_BIT);
        frame.data2 = frame.data + frame_size / 2;
-       frame.data_copy = new uint8_t[frame_size];
        check_error();
        frame.size = frame_size;
        Userdata *ud = &userdata[frame_idx];
        frame.userdata = ud;
        ud->pbo = pbo;
        ud->pixel_format = pixel_format;
+       ud->data_copy_malloc = new uint8_t[frame_size];
        frame.owner = this;
 
        // For 8-bit non-planar Y'CbCr, we ask the driver to split Y' and Cb/Cr
@@ -217,7 +219,7 @@ PBOFrameAllocator::~PBOFrameAllocator()
 void PBOFrameAllocator::destroy_frame(Frame *frame)
 {
        Userdata *ud = (Userdata *)frame->userdata;
-       delete[] frame->data_copy;
+       delete[] ud->data_copy_malloc;
 
        GLuint pbo = ud->pbo;
        glBindBuffer(buffer, pbo);
@@ -273,6 +275,71 @@ 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) {
+               Userdata *ud = (Userdata *)vf.userdata;
+               vf.data_copy = ud->data_copy_malloc;
+               ud->data_copy_current_src = Userdata::FROM_MALLOC;
+       } else {
+               vf.data_copy = nullptr;
+       }
+
+       return vf;
+}
+
+bmusb::FrameAllocator::Frame PBOFrameAllocator::create_frame(size_t width, size_t height, size_t stride)
+{
+        Frame vf;
+
+       {
+               lock_guard<mutex> lock(freelist_mutex);
+               if (freelist.empty()) {
+                       printf("Frame overrun (no more spare PBO frames), dropping frame!\n");
+                       vf.len = 0;
+                       vf.overflow = 0;
+                       return vf;
+               } else {
+                       vf = freelist.front();
+                       freelist.pop();
+               }
+       }
+       vf.len = 0;
+       vf.overflow = 0;
+
+       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);
+
+               VAImage image;
+               VAStatus va_status = vaDeriveImage(va_dpy, resources.surface, &image);
+               CHECK_VASTATUS(va_status, "vaDeriveImage");
+
+               if (image.pitches[0] == stride) {
+                       userdata->va_resources = move(resources);
+                       userdata->va_resources_release = move(release);
+                       userdata->va_image = move(image);
+
+                       va_status = vaMapBuffer(va_dpy, image.buf, (void **)&vf.data_copy);
+                       CHECK_VASTATUS(va_status, "vaMapBuffer");
+                       vf.data_copy += 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, image.pitches[0]);
+                       vf.data_copy = userdata->data_copy_malloc;
+                       userdata->data_copy_current_src = Userdata::FROM_MALLOC;
+
+                       va_status = vaDestroyImage(va_dpy, image.image_id);
+                       CHECK_VASTATUS(va_status, "vaDestroyImage");
+               }
+       } else {
+               vf.data_copy = nullptr;
+       }
+
        return vf;
 }