]> git.sesse.net Git - nageru/blobdiff - nageru/pbo_frame_allocator.cpp
Constify FFmpeg callbacks.
[nageru] / nageru / pbo_frame_allocator.cpp
index 5910a0237fb6ce725072336666591a3c76f7799d..6ebe13ae4d6f731e7348dc5f6dbcd0e61fefaedb 100644 (file)
@@ -13,6 +13,7 @@
 #include <va/va.h>
 
 #include "mjpeg_encoder.h"
+#include "defs.h"
 #include "shared/va_resource_pool.h"
 #include "v210_converter.h"
 #include "shared/va_display.h"
@@ -34,7 +35,7 @@ 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),
+       : card_index(card_index),
          mjpeg_encoder(mjpeg_encoder),
          pixel_format(pixel_format),
          buffer(buffer),
@@ -280,7 +281,7 @@ void PBOFrameAllocator::destroy_frame(Frame *frame)
 
 bmusb::FrameAllocator::Frame PBOFrameAllocator::alloc_frame()
 {
-        Frame vf;
+       Frame vf;
 
        lock_guard<mutex> lock(freelist_mutex);  // Meh.
        if (freelist.empty()) {
@@ -308,7 +309,12 @@ bmusb::FrameAllocator::Frame PBOFrameAllocator::alloc_frame()
 
 bmusb::FrameAllocator::Frame PBOFrameAllocator::create_frame(size_t width, size_t height, size_t stride)
 {
-        Frame vf;
+       Frame vf;
+
+       size_t desired_frame_bytes = width * stride;
+       if (stride > 8192 * 4 || height > 8192 || desired_frame_bytes > MAX_FRAME_SIZE) {
+               return vf;
+       }
 
        {
                lock_guard<mutex> lock(freelist_mutex);
@@ -322,10 +328,22 @@ bmusb::FrameAllocator::Frame PBOFrameAllocator::create_frame(size_t width, size_
                        freelist.pop();
                }
        }
-       vf.len = 0;
-       vf.overflow = 0;
 
        Userdata *userdata = (Userdata *)vf.userdata;
+       assert(generation == userdata->generation);
+       if (vf.size < desired_frame_bytes || (vf.size > FRAME_SIZE && vf.size > desired_frame_bytes * 2)) {
+               // Frame is either too small or way too large, so reallocate it.
+               // Note that width and height now automatically becomes the right size
+               // (the one we just asked for, instead of the default for the allocator,
+               // which is generally the global resolution); it doesn't matter
+               // for correctness, since we'll recreate the texture on upload if needed,
+               // but it is nice to save that step.
+               destroy_frame(&vf);
+               init_frame(vf, userdata, this, pixel_format, std::max<size_t>(desired_frame_bytes, FRAME_SIZE), width, height, permissions, map_bits, buffer, generation);
+       };
+
+       vf.len = 0;
+       vf.overflow = 0;
 
        if (mjpeg_encoder != nullptr &&
            mjpeg_encoder->should_encode_mjpeg_for_card(card_index)) {